a.y 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. %{
  2. #include "a.h"
  3. %}
  4. %union {
  5. Sym *sym;
  6. long lval;
  7. double dval;
  8. char sval[8];
  9. Addr addr;
  10. Gen gen;
  11. Gen2 gen2;
  12. }
  13. %left '|'
  14. %left '^'
  15. %left '&'
  16. %left '<' '>'
  17. %left '+' '-'
  18. %left '*' '/' '%'
  19. %token <lval> LTYPE1 LTYPE2 LTYPE3 LTYPE4 LTYPE5
  20. %token <lval> LTYPE6 LTYPE7 LTYPE8 LTYPE9 LTYPEA LTYPEB
  21. %token <lval> LCONST LSP LSB LFP LPC LTOS LAREG LDREG LFREG LWID
  22. %token <dval> LFCONST
  23. %token <sval> LSCONST
  24. %token <sym> LNAME LLAB LVAR
  25. %type <lval> con expr scale type pointer reg offset
  26. %type <addr> name areg xreg
  27. %type <gen> gen rel
  28. %type <gen2> noaddr gengen dstgen spec1 spec2 spec3 srcgen dstrel genrel
  29. %%
  30. prog:
  31. | prog line
  32. line:
  33. LLAB ':'
  34. {
  35. if($1->value != pc)
  36. yyerror("redeclaration of %s", $1->name);
  37. $1->value = pc;
  38. }
  39. line
  40. | LNAME ':'
  41. {
  42. $1->type = LLAB;
  43. $1->value = pc;
  44. }
  45. line
  46. | ';'
  47. | inst ';'
  48. | error ';'
  49. inst:
  50. LNAME '=' expr
  51. {
  52. $1->type = LVAR;
  53. $1->value = $3;
  54. }
  55. | LVAR '=' expr
  56. {
  57. if($1->value != $3)
  58. yyerror("redeclaration of %s", $1->name);
  59. $1->value = $3;
  60. }
  61. | LTYPE1 gengen { outcode($1, &$2); }
  62. | LTYPE2 noaddr { outcode($1, &$2); }
  63. | LTYPE3 dstgen { outcode($1, &$2); }
  64. | LTYPE4 spec1 { outcode($1, &$2); }
  65. | LTYPE5 srcgen { outcode($1, &$2); }
  66. | LTYPE6 dstrel { outcode($1, &$2); }
  67. | LTYPE7 genrel { outcode($1, &$2); }
  68. | LTYPE8 dstgen { outcode($1, &$2); }
  69. | LTYPE8 gengen { outcode($1, &$2); }
  70. | LTYPE9 noaddr { outcode($1, &$2); }
  71. | LTYPE9 dstgen { outcode($1, &$2); }
  72. | LTYPEA spec2 { outcode($1, &$2); }
  73. | LTYPEB spec3 { outcode($1, &$2); }
  74. noaddr:
  75. {
  76. $$.from = nullgen;
  77. $$.to = nullgen;
  78. }
  79. | ','
  80. {
  81. $$.from = nullgen;
  82. $$.to = nullgen;
  83. }
  84. srcgen:
  85. gen
  86. {
  87. $$.from = $1;
  88. $$.to = nullgen;
  89. }
  90. | gen ','
  91. {
  92. $$.from = $1;
  93. $$.to = nullgen;
  94. }
  95. dstgen:
  96. gen
  97. {
  98. $$.from = nullgen;
  99. $$.to = $1;
  100. }
  101. | ',' gen
  102. {
  103. $$.from = nullgen;
  104. $$.to = $2;
  105. }
  106. gengen:
  107. gen ',' gen
  108. {
  109. $$.from = $1;
  110. $$.to = $3;
  111. }
  112. dstrel:
  113. rel
  114. {
  115. $$.from = nullgen;
  116. $$.to = $1;
  117. }
  118. | ',' rel
  119. {
  120. $$.from = nullgen;
  121. $$.to = $2;
  122. }
  123. genrel:
  124. gen ',' rel
  125. {
  126. $$.from = $1;
  127. $$.to = $3;
  128. }
  129. spec1: /* DATA opcode */
  130. gen '/' con ',' gen
  131. {
  132. $1.displace = $3;
  133. $$.from = $1;
  134. $$.to = $5;
  135. }
  136. spec2: /* bit field opcodes */
  137. gen ',' gen ',' con ',' con
  138. {
  139. $1.field = $7;
  140. $3.field = $5;
  141. $$.from = $1;
  142. $$.to = $3;
  143. }
  144. spec3: /* TEXT opcode */
  145. gengen
  146. | gen ',' con ',' gen
  147. {
  148. $1.displace = $3;
  149. $$.from = $1;
  150. $$.to = $5;
  151. }
  152. rel:
  153. con '(' LPC ')'
  154. {
  155. $$ = nullgen;
  156. $$.type = D_BRANCH;
  157. $$.offset = $1 + pc;
  158. }
  159. | LNAME offset
  160. {
  161. $$ = nullgen;
  162. if(pass == 2)
  163. yyerror("undefined label: %s", $1->name);
  164. $$.type = D_BRANCH;
  165. $$.sym = $1;
  166. $$.offset = $2;
  167. }
  168. | LLAB offset
  169. {
  170. $$ = nullgen;
  171. $$.type = D_BRANCH;
  172. $$.sym = $1;
  173. $$.offset = $1->value + $2;
  174. }
  175. gen:
  176. type
  177. {
  178. $$ = nullgen;
  179. $$.type = $1;
  180. }
  181. | '$' con
  182. {
  183. $$ = nullgen;
  184. $$.type = D_CONST;
  185. $$.offset = $2;
  186. }
  187. | '$' name
  188. {
  189. $$ = nullgen;
  190. {
  191. Addr *a;
  192. a = &$$;
  193. *a = $2;
  194. }
  195. if($2.type == D_AUTO || $2.type == D_PARAM)
  196. yyerror("constant cannot be automatic: %s",
  197. $2.sym->name);
  198. $$.type = $2.type | I_ADDR;
  199. }
  200. | '$' LSCONST
  201. {
  202. $$ = nullgen;
  203. $$.type = D_SCONST;
  204. memcpy($$.sval, $2, sizeof($$.sval));
  205. }
  206. | '$' LFCONST
  207. {
  208. $$ = nullgen;
  209. $$.type = D_FCONST;
  210. $$.dval = $2;
  211. }
  212. | '$' '-' LFCONST
  213. {
  214. $$ = nullgen;
  215. $$.type = D_FCONST;
  216. $$.dval = -$3;
  217. }
  218. | LTOS '+' con
  219. {
  220. $$ = nullgen;
  221. $$.type = D_STACK;
  222. $$.offset = $3;
  223. }
  224. | LTOS '-' con
  225. {
  226. $$ = nullgen;
  227. $$.type = D_STACK;
  228. $$.offset = -$3;
  229. }
  230. | con
  231. {
  232. $$ = nullgen;
  233. $$.type = D_CONST | I_INDIR;
  234. $$.offset = $1;
  235. }
  236. | '-' '(' LAREG ')'
  237. {
  238. $$ = nullgen;
  239. $$.type = $3 | I_INDDEC;
  240. }
  241. | '(' LAREG ')' '+'
  242. {
  243. $$ = nullgen;
  244. $$.type = $2 | I_INDINC;
  245. }
  246. | areg
  247. {
  248. $$ = nullgen;
  249. $$.type = $1.type;
  250. {
  251. Addr *a;
  252. a = &$$;
  253. *a = $1;
  254. }
  255. if(($$.type & D_MASK) == D_NONE) {
  256. $$.index = D_NONE | I_INDEX1;
  257. $$.scale = 0;
  258. $$.displace = 0;
  259. }
  260. }
  261. | areg xreg
  262. {
  263. $$ = nullgen;
  264. $$.type = $1.type;
  265. {
  266. Addr *a;
  267. a = &$$;
  268. *a = $1;
  269. }
  270. $$.index = $2.type | I_INDEX1;
  271. $$.scale = $2.offset;
  272. }
  273. | '(' areg ')' xreg
  274. {
  275. $$ = nullgen;
  276. $$.type = $2.type;
  277. {
  278. Addr *a;
  279. a = &$$;
  280. *a = $2;
  281. }
  282. $$.index = $4.type | I_INDEX2;
  283. $$.scale = $4.offset;
  284. $$.displace = 0;
  285. }
  286. | con '(' areg ')' xreg
  287. {
  288. $$ = nullgen;
  289. $$.type = $3.type;
  290. {
  291. Addr *a;
  292. a = &$$;
  293. *a = $3;
  294. }
  295. $$.index = $5.type | I_INDEX2;
  296. $$.scale = $5.offset;
  297. $$.displace = $1;
  298. }
  299. | '(' areg ')'
  300. {
  301. $$ = nullgen;
  302. $$.type = $2.type;
  303. {
  304. Addr *a;
  305. a = &$$;
  306. *a = $2;
  307. }
  308. $$.index = D_NONE | I_INDEX3;
  309. $$.scale = 0;
  310. $$.displace = 0;
  311. }
  312. | con '(' areg ')'
  313. {
  314. $$ = nullgen;
  315. $$.type = $3.type;
  316. {
  317. Addr *a;
  318. a = &$$;
  319. *a = $3;
  320. }
  321. $$.index = D_NONE | I_INDEX3;
  322. $$.scale = 0;
  323. $$.displace = $1;
  324. }
  325. | '(' areg xreg ')'
  326. {
  327. $$ = nullgen;
  328. $$.type = $2.type;
  329. {
  330. Addr *a;
  331. a = &$$;
  332. *a = $2;
  333. }
  334. $$.index = $3.type | I_INDEX3;
  335. $$.scale = $3.offset;
  336. $$.displace = 0;
  337. }
  338. | con '(' areg xreg ')'
  339. {
  340. $$ = nullgen;
  341. $$.type = $3.type;
  342. {
  343. Addr *a;
  344. a = &$$;
  345. *a = $3;
  346. }
  347. $$.index = $4.type | I_INDEX3;
  348. $$.scale = $4.offset;
  349. $$.displace = $1;
  350. }
  351. type:
  352. reg
  353. | LFREG
  354. xreg:
  355. /*
  356. * .W*1 0
  357. * .W*2 1
  358. * .W*4 2
  359. * .W*8 3
  360. * .L*1 4
  361. * .L*2 5
  362. * .L*4 6
  363. * .L*8 7
  364. */
  365. '(' reg LWID scale ')'
  366. {
  367. $$.type = $2;
  368. $$.offset = $3+$4;
  369. $$.sym = S;
  370. }
  371. reg:
  372. LAREG
  373. | LDREG
  374. | LTOS
  375. scale:
  376. '*' con
  377. {
  378. switch($2) {
  379. case 1:
  380. $$ = 0;
  381. break;
  382. case 2:
  383. $$ = 1;
  384. break;
  385. default:
  386. yyerror("bad scale: %ld", $2);
  387. case 4:
  388. $$ = 2;
  389. break;
  390. case 8:
  391. $$ = 3;
  392. break;
  393. }
  394. }
  395. areg:
  396. '(' LAREG ')'
  397. {
  398. $$.type = $2 | I_INDIR;
  399. $$.sym = S;
  400. $$.offset = 0;
  401. }
  402. | con '(' LAREG ')'
  403. {
  404. $$.type = $3 | I_INDIR;
  405. $$.sym = S;
  406. $$.offset = $1;
  407. }
  408. | '(' ')'
  409. {
  410. $$.type = D_NONE | I_INDIR;
  411. $$.sym = S;
  412. $$.offset = 0;
  413. }
  414. | con '(' ')'
  415. {
  416. $$.type = D_NONE | I_INDIR;
  417. $$.sym = S;
  418. $$.offset = $1;
  419. }
  420. | name
  421. name:
  422. LNAME offset '(' pointer ')'
  423. {
  424. $$.type = $4;
  425. $$.sym = $1;
  426. $$.offset = $2;
  427. }
  428. | LNAME '<' '>' offset '(' LSB ')'
  429. {
  430. $$.type = D_STATIC;
  431. $$.sym = $1;
  432. $$.offset = $4;
  433. }
  434. offset:
  435. {
  436. $$ = 0;
  437. }
  438. | '+' con
  439. {
  440. $$ = $2;
  441. }
  442. | '-' con
  443. {
  444. $$ = -$2;
  445. }
  446. pointer:
  447. LSB
  448. | LSP
  449. | LFP
  450. con:
  451. LCONST
  452. | LVAR
  453. {
  454. $$ = $1->value;
  455. }
  456. | '-' con
  457. {
  458. $$ = -$2;
  459. }
  460. | '+' con
  461. {
  462. $$ = $2;
  463. }
  464. | '~' con
  465. {
  466. $$ = ~$2;
  467. }
  468. | '(' expr ')'
  469. {
  470. $$ = $2;
  471. }
  472. expr:
  473. con
  474. | expr '+' expr
  475. {
  476. $$ = $1 + $3;
  477. }
  478. | expr '-' expr
  479. {
  480. $$ = $1 - $3;
  481. }
  482. | expr '*' expr
  483. {
  484. $$ = $1 * $3;
  485. }
  486. | expr '/' expr
  487. {
  488. $$ = $1 / $3;
  489. }
  490. | expr '%' expr
  491. {
  492. $$ = $1 % $3;
  493. }
  494. | expr '<' '<' expr
  495. {
  496. $$ = $1 << $4;
  497. }
  498. | expr '>' '>' expr
  499. {
  500. $$ = $1 >> $4;
  501. }
  502. | expr '&' expr
  503. {
  504. $$ = $1 & $3;
  505. }
  506. | expr '^' expr
  507. {
  508. $$ = $1 ^ $3;
  509. }
  510. | expr '|' expr
  511. {
  512. $$ = $1 | $3;
  513. }