mktab.y 8.7 KB

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