mktab.y 8.7 KB

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