mktab.y 8.6 KB

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