a.y 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. %{
  2. #include <u.h>
  3. #include <libc.h>
  4. #include <bio.h>
  5. #include "../6c/6.out.h"
  6. #include "a.h"
  7. %}
  8. %union {
  9. Sym *sym;
  10. long lval;
  11. double dval;
  12. char sval[8];
  13. Gen gen;
  14. Gen2 gen2;
  15. }
  16. %left '|'
  17. %left '^'
  18. %left '&'
  19. %left '<' '>'
  20. %left '+' '-'
  21. %left '*' '/' '%'
  22. %token <lval> LTYPES LTYPED LTYPEB LTYPE1 LTYPE2
  23. %token <lval> LCONST LSP LFP LPC LSB LTYPEX
  24. %token <lval> LRREG LFREG
  25. %token <dval> LFCONST
  26. %token <sval> LSCONST
  27. %token <sym> LNAME LLAB LVAR
  28. %type <lval> con expr pointer offset reg
  29. %type <gen> addr
  30. %type <gen> imm nam
  31. %type <gen2> regs srrr2 drrr2 spec1 spec2
  32. %%
  33. prog:
  34. | prog line
  35. line:
  36. LLAB ':'
  37. {
  38. if($1->value != pc)
  39. yyerror("redeclaration of %s", $1->name);
  40. $1->value = pc;
  41. }
  42. line
  43. | LNAME ':'
  44. {
  45. $1->type = LLAB;
  46. $1->value = pc;
  47. }
  48. line
  49. | ';'
  50. | inst ';'
  51. | error ';'
  52. inst:
  53. LNAME '=' expr
  54. {
  55. $1->type = LVAR;
  56. $1->value = $3;
  57. }
  58. | LVAR '=' expr
  59. {
  60. if($1->value != $3)
  61. yyerror("redeclaration of %s", $1->name);
  62. $1->value = $3;
  63. }
  64. | LTYPEB regs { outcode($1, &$2); }
  65. | LTYPES srrr2 { outcode($1, &$2); }
  66. | LTYPED drrr2 { outcode($1, &$2); }
  67. | LTYPE1 spec1 { outcode($1, &$2); }
  68. | LTYPE2 spec2 { outcode($1, &$2); }
  69. srrr2:
  70. regs
  71. | addr
  72. {
  73. $$.from = $1;
  74. $$.to = nullgen;
  75. $$.type = D_NONE;
  76. }
  77. drrr2:
  78. regs
  79. | addr
  80. {
  81. $$.from = nullgen;
  82. $$.to = $1;
  83. $$.type = D_NONE;
  84. }
  85. regs:
  86. comma
  87. {
  88. $$.from = nullgen;
  89. $$.to = nullgen;
  90. $$.type = D_NONE;
  91. }
  92. | addr ','
  93. {
  94. $$.from = $1;
  95. $$.to = nullgen;
  96. $$.type = D_NONE;
  97. }
  98. | ',' addr
  99. {
  100. $$.from = nullgen;
  101. $$.to = $2;
  102. $$.type = D_NONE;
  103. }
  104. | addr ',' addr
  105. {
  106. $$.from = $1;
  107. $$.to = $3;
  108. $$.type = D_NONE;
  109. }
  110. | addr ',' reg ',' addr
  111. {
  112. $$.from = $1;
  113. $$.to = $5;
  114. $$.type = $3;
  115. }
  116. spec1: /* DATA */
  117. addr '/' con ',' imm
  118. {
  119. $$.from = $1;
  120. $$.from.scale = $3;
  121. $$.to = $5;
  122. $$.type = D_NONE;
  123. }
  124. spec2: /* SHL/SHR/MOV */
  125. regs
  126. | regs ':' LRREG
  127. {
  128. $$ = $1;
  129. if($$.from.index != D_NONE)
  130. yyerror("dp shift with lhs index");
  131. $$.from.index = $3;
  132. }
  133. addr:
  134. reg
  135. {
  136. $$ = nullgen;
  137. $$.type = $1;
  138. }
  139. | con
  140. {
  141. $$ = nullgen;
  142. $$.type = D_INDIR+D_NONE;
  143. $$.offset = $1;
  144. }
  145. | con '(' LRREG ')'
  146. {
  147. $$ = nullgen;
  148. $$.type = D_INDIR+$3;
  149. $$.offset = $1;
  150. }
  151. | con '(' LSP ')'
  152. {
  153. $$ = nullgen;
  154. $$.type = D_INDIR+REGSP;
  155. $$.offset = $1;
  156. }
  157. | con '(' LRREG '*' con ')'
  158. {
  159. $$ = nullgen;
  160. $$.type = D_INDIR+D_NONE;
  161. $$.offset = $1;
  162. $$.index = $3;
  163. $$.scale = $5;
  164. checkscale($$.scale);
  165. }
  166. | con '(' LRREG ')' '(' LRREG '*' con ')'
  167. {
  168. $$ = nullgen;
  169. $$.type = D_INDIR+$3;
  170. $$.offset = $1;
  171. $$.index = $6;
  172. $$.scale = $8;
  173. checkscale($$.scale);
  174. }
  175. | '(' LRREG ')'
  176. {
  177. $$ = nullgen;
  178. $$.type = D_INDIR+$2;
  179. }
  180. | '(' LSP ')'
  181. {
  182. $$ = nullgen;
  183. $$.type = D_INDIR+REGSP;
  184. }
  185. | '(' LRREG '*' con ')'
  186. {
  187. $$ = nullgen;
  188. $$.type = D_INDIR+D_NONE;
  189. $$.index = $2;
  190. $$.scale = $4;
  191. checkscale($$.scale);
  192. }
  193. | '(' LRREG ')' '(' LRREG '*' con ')'
  194. {
  195. $$ = nullgen;
  196. $$.type = D_INDIR+$2;
  197. $$.index = $5;
  198. $$.scale = $7;
  199. checkscale($$.scale);
  200. }
  201. | nam
  202. {
  203. $$ = $1;
  204. }
  205. | nam '(' LRREG '*' con ')'
  206. {
  207. $$ = $1;
  208. $$.index = $3;
  209. $$.scale = $5;
  210. checkscale($$.scale);
  211. }
  212. | con '(' LPC ')'
  213. {
  214. $$ = nullgen;
  215. $$.type = D_BRANCH;
  216. $$.offset = $1 + pc;
  217. }
  218. | LNAME offset
  219. {
  220. $$ = nullgen;
  221. if(pass == 2)
  222. yyerror("undefined label: %s", $1->name);
  223. $$.type = D_BRANCH;
  224. $$.sym = $1;
  225. $$.offset = $2;
  226. }
  227. | LLAB offset
  228. {
  229. $$ = nullgen;
  230. $$.type = D_BRANCH;
  231. $$.sym = $1;
  232. $$.offset = $1->value + $2;
  233. }
  234. | imm
  235. imm:
  236. '$' con
  237. {
  238. $$ = nullgen;
  239. $$.type = D_CONST;
  240. $$.offset = $2;
  241. }
  242. | '$' nam
  243. {
  244. $$ = $2;
  245. $$.index = $2.type;
  246. $$.type = D_ADDR;
  247. if($2.type == D_AUTO || $2.type == D_PARAM)
  248. yyerror("constant cannot be automatic: %s",
  249. $2.sym->name);
  250. }
  251. | '$' LSCONST
  252. {
  253. $$ = nullgen;
  254. $$.type = D_SCONST;
  255. memcpy($$.sval, $2, sizeof($$.sval));
  256. }
  257. | '$' LFCONST
  258. {
  259. $$ = nullgen;
  260. $$.type = D_FCONST;
  261. $$.dval = $2;
  262. }
  263. | '$' '-' LFCONST
  264. {
  265. $$ = nullgen;
  266. $$.type = D_FCONST;
  267. $$.dval = -$3;
  268. }
  269. reg:
  270. LRREG
  271. | LFREG
  272. nam:
  273. LNAME offset '(' pointer ')'
  274. {
  275. $$ = nullgen;
  276. $$.type = $4;
  277. $$.sym = $1;
  278. $$.offset = $2;
  279. }
  280. | LNAME '<' '>' offset '(' LSB ')'
  281. {
  282. $$ = nullgen;
  283. $$.type = D_STATIC;
  284. $$.sym = $1;
  285. $$.offset = $4;
  286. }
  287. offset:
  288. {
  289. $$ = 0;
  290. }
  291. | '+' con
  292. {
  293. $$ = $2;
  294. }
  295. | '-' con
  296. {
  297. $$ = -$2;
  298. }
  299. pointer:
  300. LSB
  301. | LSP
  302. | LFP
  303. con:
  304. LCONST
  305. | LVAR
  306. {
  307. $$ = $1->value;
  308. }
  309. | '-' con
  310. {
  311. $$ = -$2;
  312. }
  313. | '+' con
  314. {
  315. $$ = $2;
  316. }
  317. | '~' con
  318. {
  319. $$ = ~$2;
  320. }
  321. | '(' expr ')'
  322. {
  323. $$ = $2;
  324. }
  325. expr:
  326. con
  327. | expr '+' expr
  328. {
  329. $$ = $1 + $3;
  330. }
  331. | expr '-' expr
  332. {
  333. $$ = $1 - $3;
  334. }
  335. | expr '*' expr
  336. {
  337. $$ = $1 * $3;
  338. }
  339. | expr '/' expr
  340. {
  341. $$ = $1 / $3;
  342. }
  343. | expr '%' expr
  344. {
  345. $$ = $1 % $3;
  346. }
  347. | expr '<' '<' expr
  348. {
  349. $$ = $1 << $4;
  350. }
  351. | expr '>' '>' expr
  352. {
  353. $$ = $1 >> $4;
  354. }
  355. | expr '&' expr
  356. {
  357. $$ = $1 & $3;
  358. }
  359. | expr '^' expr
  360. {
  361. $$ = $1 ^ $3;
  362. }
  363. | expr '|' expr
  364. {
  365. $$ = $1 | $3;
  366. }
  367. comma:
  368. | ','