mktab.y 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. %{
  2. #ifndef NORCSID
  3. static char rcsid[] = "$Id$";
  4. #endif
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include "param.h"
  9. #include "types.h"
  10. #include "pattern.h"
  11. #include <em_spec.h>
  12. #include <em_mnem.h>
  13. #include "optim.h"
  14. /*
  15. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  16. * See the copyright notice in the ACK home directory, in the file "Copyright".
  17. *
  18. * Author: Hans van Staveren
  19. */
  20. #define op_CBO (op_plast+1)
  21. #define MAXNODES 1000
  22. expr_t nodes[MAXNODES];
  23. expr_p lastnode = nodes+1;
  24. int curind,prevind;
  25. int patlen,maxpatlen,rpllen;
  26. int lino = 1;
  27. int patno=1;
  28. #define MAX 100
  29. int patmnem[MAX],rplmnem[MAX],rplexpr[MAX];
  30. byte nparam[N_EX_OPS];
  31. bool nonumlab[N_EX_OPS];
  32. bool onlyconst[N_EX_OPS];
  33. int nerrors=0;
  34. char patid[128];
  35. int CBO_instrs[] = {
  36. op_adi,
  37. op_adu,
  38. op_and,
  39. op_ior,
  40. op_xor
  41. /* don't add op_mli and op_mlu! */
  42. };
  43. int patCBO;
  44. int rplCBO;
  45. %}
  46. %union {
  47. int y_int;
  48. }
  49. %left OR2
  50. %left AND2
  51. %left OR1
  52. %left XOR1
  53. %left AND1
  54. %left CMPEQ CMPNE
  55. %left CMPLT CMPLE CMPGT CMPGE
  56. %left RSHIFT LSHIFT
  57. %left ARPLUS ARMINUS
  58. %left ARTIMES ARDIVIDE ARMOD
  59. %nonassoc NOT COMP UMINUS
  60. %nonassoc '$'
  61. %token SFIT UFIT NOTREG PSIZE WSIZE DEFINED SAMESIGN ROM ROTATE STRING
  62. %token <y_int> MNEM
  63. %token <y_int> NUMBER
  64. %type <y_int> expr argno optexpr
  65. %start patternlist
  66. %%
  67. patternlist
  68. : /* empty */
  69. | STRING '\n'
  70. | patternlist '\n'
  71. | patternlist pattern
  72. ;
  73. pattern :
  74. mnemlist optexpr ':' replacement '\n'
  75. {
  76. if (patCBO) {
  77. register int i;
  78. if (! rplCBO) {
  79. yyerror("No CBO in replacement");
  80. }
  81. for (i=0; i<sizeof(CBO_instrs)/sizeof(int); i++) {
  82. outpat($2, CBO_instrs[i]);
  83. }
  84. }
  85. else {
  86. outpat($2, 0);
  87. }
  88. patCBO = rplCBO = 0;
  89. }
  90. | error '\n'
  91. { yyerrok; }
  92. ;
  93. replacement
  94. : expr /* special optimization */
  95. {
  96. #ifdef ALLOWSPECIAL
  97. rpllen=1; rplmnem[0]=0; rplexpr[0]=$1;
  98. #else
  99. yyerror("No specials allowed");
  100. #endif
  101. }
  102. | repllist
  103. ;
  104. repllist: /* empty */
  105. { rpllen=0; }
  106. | repllist repl
  107. ;
  108. repl : MNEM optexpr
  109. { rplmnem[rpllen] = $1; rplexpr[rpllen++] = $2;
  110. if ($1 == op_CBO) {
  111. if (!patCBO) {
  112. yyerror("No CBO in pattern");
  113. }
  114. if (rplCBO) {
  115. yyerror("Only one CBO allowed in replacement");
  116. }
  117. rplCBO = 1;
  118. }
  119. }
  120. ;
  121. mnemlist: MNEM
  122. { patlen=0; patmnem[patlen++] = $1;
  123. if ($1 == op_CBO) {
  124. if (patCBO) {
  125. yyerror("Only one CBO allowed in pattern");
  126. }
  127. patCBO = 1;
  128. }
  129. }
  130. | mnemlist MNEM
  131. { patmnem[patlen++] = $2;
  132. if ($2 == op_CBO) {
  133. if (patCBO) {
  134. yyerror("Only one CBO allowed in pattern");
  135. }
  136. patCBO = 1;
  137. }
  138. }
  139. ;
  140. optexpr : /* empty */
  141. { $$ = 0; }
  142. | expr
  143. ;
  144. expr
  145. : '$' argno
  146. { $$ = lookup(0,EX_ARG,$2,0); }
  147. | NUMBER
  148. { $$ = lookup(0,EX_CON,(int)(short)$1,0); }
  149. | PSIZE
  150. { $$ = lookup(0,EX_POINTERSIZE,0,0); }
  151. | WSIZE
  152. { $$ = lookup(0,EX_WORDSIZE,0,0); }
  153. | DEFINED '(' expr ')'
  154. { $$ = lookup(0,EX_DEFINED,$3,0); }
  155. | SAMESIGN '(' expr ',' expr ')'
  156. { $$ = lookup(1,EX_SAMESIGN,$3,$5); }
  157. | SFIT '(' expr ',' expr ')'
  158. { $$ = lookup(0,EX_SFIT,$3,$5); }
  159. | UFIT '(' expr ',' expr ')'
  160. { $$ = lookup(0,EX_UFIT,$3,$5); }
  161. | ROTATE '(' expr ',' expr ')'
  162. { $$ = lookup(0,EX_ROTATE,$3,$5); }
  163. | NOTREG '(' expr ')'
  164. { $$ = lookup(0,EX_NOTREG,$3,0); }
  165. | ROM '(' argno ',' expr ')'
  166. { $$ = lookup(0,EX_ROM,$3,$5); }
  167. | '(' expr ')'
  168. { $$ = $2; }
  169. | expr CMPEQ expr
  170. { $$ = lookup(1,EX_CMPEQ,$1,$3); }
  171. | expr CMPNE expr
  172. { $$ = lookup(1,EX_CMPNE,$1,$3); }
  173. | expr CMPGT expr
  174. { $$ = lookup(0,EX_CMPGT,$1,$3); }
  175. | expr CMPGE expr
  176. { $$ = lookup(0,EX_CMPGE,$1,$3); }
  177. | expr CMPLT expr
  178. { $$ = lookup(0,EX_CMPLT,$1,$3); }
  179. | expr CMPLE expr
  180. { $$ = lookup(0,EX_CMPLE,$1,$3); }
  181. | expr OR2 expr
  182. { $$ = lookup(0,EX_OR2,$1,$3); }
  183. | expr AND2 expr
  184. { $$ = lookup(0,EX_AND2,$1,$3); }
  185. | expr OR1 expr
  186. { $$ = lookup(1,EX_OR1,$1,$3); }
  187. | expr XOR1 expr
  188. { $$ = lookup(1,EX_XOR1,$1,$3); }
  189. | expr AND1 expr
  190. { $$ = lookup(1,EX_AND1,$1,$3); }
  191. | expr ARPLUS expr
  192. { $$ = lookup(1,EX_PLUS,$1,$3); }
  193. | expr ARMINUS expr
  194. { $$ = lookup(0,EX_MINUS,$1,$3); }
  195. | expr ARTIMES expr
  196. { $$ = lookup(1,EX_TIMES,$1,$3); }
  197. | expr ARDIVIDE expr
  198. { $$ = lookup(0,EX_DIVIDE,$1,$3); }
  199. | expr ARMOD expr
  200. { $$ = lookup(0,EX_MOD,$1,$3); }
  201. | expr LSHIFT expr
  202. { $$ = lookup(0,EX_LSHIFT,$1,$3); }
  203. | expr RSHIFT expr
  204. { $$ = lookup(0,EX_RSHIFT,$1,$3); }
  205. | ARPLUS expr %prec UMINUS
  206. { $$ = $2; }
  207. | ARMINUS expr %prec UMINUS
  208. { $$ = lookup(0,EX_UMINUS,$2,0); }
  209. | NOT expr
  210. { $$ = lookup(0,EX_NOT,$2,0); }
  211. | COMP expr
  212. { $$ = lookup(0,EX_COMP,$2,0); }
  213. ;
  214. argno : NUMBER
  215. { if ($1<1 || $1>patlen) {
  216. YYERROR;
  217. }
  218. $$ = (int) $1;
  219. }
  220. ;
  221. %%
  222. extern char em_mnem[][4];
  223. #define HASHSIZE (2*(sp_lmnem-sp_fmnem))
  224. struct hashmnem {
  225. char h_name[3];
  226. byte h_value;
  227. } hashmnem[HASHSIZE];
  228. inithash() {
  229. register i;
  230. enter("lab",op_lab);
  231. enter("LLP",op_LLP);
  232. enter("LEP",op_LEP);
  233. enter("SLP",op_SLP);
  234. enter("SEP",op_SEP);
  235. enter("CBO",op_CBO);
  236. for(i=0;i<=sp_lmnem-sp_fmnem;i++)
  237. enter(em_mnem[i],i+sp_fmnem);
  238. }
  239. unsigned hashname(name) register char *name; {
  240. register unsigned h;
  241. h = (*name++)&BMASK;
  242. h = (h<<4)^((*name++)&BMASK);
  243. h = (h<<4)^((*name++)&BMASK);
  244. return(h);
  245. }
  246. enter(name,value) char *name; {
  247. register unsigned h;
  248. h=hashname(name)%HASHSIZE;
  249. while (hashmnem[h].h_name[0] != 0)
  250. h = (h+1)%HASHSIZE;
  251. strncpy(hashmnem[h].h_name,name,3);
  252. hashmnem[h].h_value = value;
  253. }
  254. int mlookup(name) char *name; {
  255. register unsigned h;
  256. h = hashname(name)%HASHSIZE;
  257. while (strncmp(hashmnem[h].h_name,name,3) != 0 &&
  258. hashmnem[h].h_name[0] != 0)
  259. h = (h+1)%HASHSIZE;
  260. return(hashmnem[h].h_value&BMASK); /* 0 if not found */
  261. }
  262. main() {
  263. inithash();
  264. initio();
  265. yyparse();
  266. if (nerrors==0)
  267. printnodes();
  268. return nerrors;
  269. }
  270. yyerror(s) char *s; {
  271. fprintf(stderr,"line %d: %s\n",lino,s);
  272. nerrors++;
  273. }
  274. lookup(comm,operator,lnode,rnode) {
  275. register expr_p p;
  276. for (p=nodes+1;p<lastnode;p++) {
  277. if (p->ex_operator != operator)
  278. continue;
  279. if (!(p->ex_lnode == lnode && p->ex_rnode == rnode ||
  280. comm && p->ex_lnode == rnode && p->ex_rnode == lnode))
  281. continue;
  282. return(p-nodes);
  283. }
  284. if (lastnode >= &nodes[MAXNODES])
  285. yyerror("node table overflow");
  286. lastnode++;
  287. p->ex_operator = operator;
  288. p->ex_lnode = lnode;
  289. p->ex_rnode = rnode;
  290. return(p-nodes);
  291. }
  292. printnodes() {
  293. register expr_p p;
  294. printf("};\n\nshort lastind = %d;\n\nexpr_t enodes[] = {\n",prevind);
  295. for (p=nodes;p<lastnode;p++)
  296. printf("/* %3d */\t%3d,%6u,%6u,\n",
  297. p-nodes,p->ex_operator,p->ex_lnode,p->ex_rnode);
  298. printf("};\n\niarg_t iargs[%d];\n", (maxpatlen>0 ? maxpatlen : 1));
  299. if (patid[0])
  300. printf("static char rcsid[] = %s;\n",patid);
  301. }
  302. initio() {
  303. register i;
  304. printf("#include \"param.h\"\n#include \"types.h\"\n");
  305. printf("#include \"pattern.h\"\n\n");
  306. for(i=0;i<N_EX_OPS;i++) {
  307. nparam[i]=2;
  308. nonumlab[i]=TRUE;
  309. onlyconst[i]=TRUE;
  310. }
  311. nparam[EX_POINTERSIZE] = 0;
  312. nparam[EX_WORDSIZE] = 0;
  313. nparam[EX_CON] = 0;
  314. nparam[EX_ROM] = 0;
  315. nparam[EX_ARG] = 0;
  316. nparam[EX_DEFINED] = 0;
  317. nparam[EX_OR2] = 1;
  318. nparam[EX_AND2] = 1;
  319. nparam[EX_UMINUS] = 1;
  320. nparam[EX_NOT] = 1;
  321. nparam[EX_COMP] = 1;
  322. nparam[EX_NOTREG] = 1;
  323. nonumlab[EX_CMPEQ] = FALSE;
  324. nonumlab[EX_CMPNE] = FALSE;
  325. onlyconst[EX_CMPEQ] = FALSE;
  326. onlyconst[EX_CMPNE] = FALSE;
  327. onlyconst[EX_CMPLE] = FALSE;
  328. onlyconst[EX_CMPLT] = FALSE;
  329. onlyconst[EX_CMPGE] = FALSE;
  330. onlyconst[EX_CMPGT] = FALSE;
  331. onlyconst[EX_PLUS] = FALSE;
  332. onlyconst[EX_MINUS] = FALSE;
  333. printf("byte nparam[] = {");
  334. for (i=0;i<N_EX_OPS;i++) printf("%d,",nparam[i]);
  335. printf("};\nbool nonumlab[] = {");
  336. for (i=0;i<N_EX_OPS;i++) printf("%d,",nonumlab[i]);
  337. printf("};\nbool onlyconst[] = {");
  338. for (i=0;i<N_EX_OPS;i++) printf("%d,",onlyconst[i]);
  339. printf("};\n\nbyte pattern[] = { 0\n");
  340. curind = 1;
  341. }
  342. outpat(exprno, instrno)
  343. {
  344. register int i;
  345. outbyte(0); outshort(prevind); prevind=curind-3;
  346. out(patlen);
  347. for (i=0;i<patlen;i++) {
  348. if (patmnem[i] == op_CBO) outbyte(instrno);
  349. else outbyte(patmnem[i]);
  350. }
  351. out(exprno);
  352. out(rpllen);
  353. for (i=0;i<rpllen;i++) {
  354. if (rplmnem[i] == op_CBO) outbyte(instrno);
  355. else outbyte(rplmnem[i]);
  356. out(rplexpr[i]);
  357. }
  358. #ifdef DIAGOPT
  359. outshort(patno);
  360. #endif
  361. patno++;
  362. printf("\n");
  363. if (patlen>maxpatlen) maxpatlen=patlen;
  364. }
  365. outbyte(b) {
  366. printf(",%3d",b);
  367. curind++;
  368. }
  369. outshort(s) {
  370. outbyte(s&0377);
  371. outbyte((s>>8)&0377);
  372. }
  373. out(w) {
  374. if (w<255) {
  375. outbyte(w);
  376. } else {
  377. outbyte(255);
  378. outshort(w);
  379. }
  380. }
  381. #include "scan.c"