cc.y 16 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  1. %{
  2. #include "cc.h"
  3. %}
  4. %union {
  5. Node* node;
  6. Sym* sym;
  7. Type* type;
  8. struct
  9. {
  10. Type* t;
  11. uchar c;
  12. } tycl;
  13. struct
  14. {
  15. Type* t1;
  16. Type* t2;
  17. Type* t3;
  18. uchar c;
  19. } tyty;
  20. struct
  21. {
  22. char* s;
  23. long l;
  24. } sval;
  25. long lval;
  26. double dval;
  27. vlong vval;
  28. }
  29. %type <sym> ltag
  30. %type <lval> gctname gcname cname gname tname
  31. %type <lval> gctnlist gcnlist zgnlist
  32. %type <type> tlist sbody complex
  33. %type <tycl> types
  34. %type <node> zarglist arglist zcexpr
  35. %type <node> name block stmnt cexpr expr xuexpr pexpr
  36. %type <node> zelist elist adecl slist uexpr string lstring
  37. %type <node> xdecor xdecor2 labels label ulstmnt
  38. %type <node> adlist edecor tag qual qlist
  39. %type <node> abdecor abdecor1 abdecor2 abdecor3
  40. %type <node> zexpr lexpr init ilist forexpr
  41. %left ';'
  42. %left ','
  43. %right '=' LPE LME LMLE LDVE LMDE LRSHE LLSHE LANDE LXORE LORE
  44. %right '?' ':'
  45. %left LOROR
  46. %left LANDAND
  47. %left '|'
  48. %left '^'
  49. %left '&'
  50. %left LEQ LNE
  51. %left '<' '>' LLE LGE
  52. %left LLSH LRSH
  53. %left '+' '-'
  54. %left '*' '/' '%'
  55. %right LMM LPP LMG '.' '[' '('
  56. %token <sym> LNAME LTYPE
  57. %token <dval> LFCONST LDCONST
  58. %token <vval> LCONST LLCONST LUCONST LULCONST LVLCONST LUVLCONST
  59. %token <sval> LSTRING LLSTRING
  60. %token LAUTO LBREAK LCASE LCHAR LCONTINUE LDEFAULT LDO
  61. %token LDOUBLE LELSE LEXTERN LFLOAT LFOR LGOTO
  62. %token LIF LINT LLONG LREGISTER LRETURN LSHORT LSIZEOF LUSED
  63. %token LSTATIC LSTRUCT LSWITCH LTYPEDEF LTYPESTR LUNION LUNSIGNED
  64. %token LWHILE LVOID LENUM LSIGNED LCONSTNT LVOLATILE LSET LSIGNOF
  65. %token LRESTRICT LINLINE
  66. %%
  67. prog:
  68. | prog xdecl
  69. /*
  70. * external declarator
  71. */
  72. xdecl:
  73. zctlist ';'
  74. {
  75. dodecl(xdecl, lastclass, lasttype, Z);
  76. }
  77. | zctlist xdlist ';'
  78. | zctlist xdecor
  79. {
  80. lastdcl = T;
  81. firstarg = S;
  82. dodecl(xdecl, lastclass, lasttype, $2);
  83. if(lastdcl == T || lastdcl->etype != TFUNC) {
  84. diag($2, "not a function");
  85. lastdcl = types[TFUNC];
  86. }
  87. thisfn = lastdcl;
  88. markdcl();
  89. firstdcl = dclstack;
  90. argmark($2, 0);
  91. }
  92. pdecl
  93. {
  94. argmark($2, 1);
  95. }
  96. block
  97. {
  98. Node *n;
  99. n = revertdcl();
  100. if(n)
  101. $6 = new(OLIST, n, $6);
  102. if(!debug['a'] && !debug['Z'])
  103. codgen($6, $2);
  104. }
  105. xdlist:
  106. xdecor
  107. {
  108. dodecl(xdecl, lastclass, lasttype, $1);
  109. }
  110. | xdecor
  111. {
  112. $1 = dodecl(xdecl, lastclass, lasttype, $1);
  113. }
  114. '=' init
  115. {
  116. doinit($1->sym, $1->type, 0L, $4);
  117. }
  118. | xdlist ',' xdlist
  119. xdecor:
  120. xdecor2
  121. | '*' zgnlist xdecor
  122. {
  123. $$ = new(OIND, $3, Z);
  124. $$->garb = simpleg($2);
  125. }
  126. xdecor2:
  127. tag
  128. | '(' xdecor ')'
  129. {
  130. $$ = $2;
  131. }
  132. | xdecor2 '(' zarglist ')'
  133. {
  134. $$ = new(OFUNC, $1, $3);
  135. }
  136. | xdecor2 '[' zexpr ']'
  137. {
  138. $$ = new(OARRAY, $1, $3);
  139. }
  140. /*
  141. * automatic declarator
  142. */
  143. adecl:
  144. ctlist ';'
  145. {
  146. $$ = dodecl(adecl, lastclass, lasttype, Z);
  147. }
  148. | ctlist adlist ';'
  149. {
  150. $$ = $2;
  151. }
  152. adlist:
  153. xdecor
  154. {
  155. dodecl(adecl, lastclass, lasttype, $1);
  156. $$ = Z;
  157. }
  158. | xdecor
  159. {
  160. $1 = dodecl(adecl, lastclass, lasttype, $1);
  161. }
  162. '=' init
  163. {
  164. long w;
  165. w = $1->sym->type->width;
  166. $$ = doinit($1->sym, $1->type, 0L, $4);
  167. $$ = contig($1->sym, $$, w);
  168. }
  169. | adlist ',' adlist
  170. {
  171. $$ = $1;
  172. if($3 != Z) {
  173. $$ = $3;
  174. if($1 != Z)
  175. $$ = new(OLIST, $1, $3);
  176. }
  177. }
  178. /*
  179. * parameter declarator
  180. */
  181. pdecl:
  182. | pdecl ctlist pdlist ';'
  183. pdlist:
  184. xdecor
  185. {
  186. dodecl(pdecl, lastclass, lasttype, $1);
  187. }
  188. | pdlist ',' pdlist
  189. /*
  190. * structure element declarator
  191. */
  192. edecl:
  193. tlist
  194. {
  195. lasttype = $1;
  196. }
  197. zedlist ';'
  198. | edecl tlist
  199. {
  200. lasttype = $2;
  201. }
  202. zedlist ';'
  203. zedlist: /* extension */
  204. {
  205. lastfield = 0;
  206. edecl(CXXX, lasttype, S);
  207. }
  208. | edlist
  209. edlist:
  210. edecor
  211. {
  212. dodecl(edecl, CXXX, lasttype, $1);
  213. }
  214. | edlist ',' edlist
  215. edecor:
  216. xdecor
  217. {
  218. lastbit = 0;
  219. firstbit = 1;
  220. }
  221. | tag ':' lexpr
  222. {
  223. $$ = new(OBIT, $1, $3);
  224. }
  225. | ':' lexpr
  226. {
  227. $$ = new(OBIT, Z, $2);
  228. }
  229. /*
  230. * abstract declarator
  231. */
  232. abdecor:
  233. {
  234. $$ = (Z);
  235. }
  236. | abdecor1
  237. abdecor1:
  238. '*' zgnlist
  239. {
  240. $$ = new(OIND, (Z), Z);
  241. $$->garb = simpleg($2);
  242. }
  243. | '*' zgnlist abdecor1
  244. {
  245. $$ = new(OIND, $3, Z);
  246. $$->garb = simpleg($2);
  247. }
  248. | abdecor2
  249. abdecor2:
  250. abdecor3
  251. | abdecor2 '(' zarglist ')'
  252. {
  253. $$ = new(OFUNC, $1, $3);
  254. }
  255. | abdecor2 '[' zexpr ']'
  256. {
  257. $$ = new(OARRAY, $1, $3);
  258. }
  259. abdecor3:
  260. '(' ')'
  261. {
  262. $$ = new(OFUNC, (Z), Z);
  263. }
  264. | '[' zexpr ']'
  265. {
  266. $$ = new(OARRAY, (Z), $2);
  267. }
  268. | '(' abdecor1 ')'
  269. {
  270. $$ = $2;
  271. }
  272. init:
  273. expr
  274. | '{' ilist '}'
  275. {
  276. $$ = new(OINIT, invert($2), Z);
  277. }
  278. qual:
  279. '[' lexpr ']'
  280. {
  281. $$ = new(OARRAY, $2, Z);
  282. }
  283. | '.' ltag
  284. {
  285. $$ = new(OELEM, Z, Z);
  286. $$->sym = $2;
  287. }
  288. | qual '='
  289. qlist:
  290. init ','
  291. | qlist init ','
  292. {
  293. $$ = new(OLIST, $1, $2);
  294. }
  295. | qual
  296. | qlist qual
  297. {
  298. $$ = new(OLIST, $1, $2);
  299. }
  300. ilist:
  301. qlist
  302. | init
  303. | qlist init
  304. {
  305. $$ = new(OLIST, $1, $2);
  306. }
  307. zarglist:
  308. {
  309. $$ = Z;
  310. }
  311. | arglist
  312. {
  313. $$ = invert($1);
  314. }
  315. arglist:
  316. name
  317. | tlist abdecor
  318. {
  319. $$ = new(OPROTO, $2, Z);
  320. $$->type = $1;
  321. }
  322. | tlist xdecor
  323. {
  324. $$ = new(OPROTO, $2, Z);
  325. $$->type = $1;
  326. }
  327. | '.' '.' '.'
  328. {
  329. $$ = new(ODOTDOT, Z, Z);
  330. }
  331. | arglist ',' arglist
  332. {
  333. $$ = new(OLIST, $1, $3);
  334. }
  335. block:
  336. '{' slist '}'
  337. {
  338. $$ = invert($2);
  339. // if($2 != Z)
  340. // $$ = new(OLIST, $2, $$);
  341. if($$ == Z)
  342. $$ = new(OLIST, Z, Z);
  343. }
  344. slist:
  345. {
  346. $$ = Z;
  347. }
  348. | slist adecl
  349. {
  350. $$ = new(OLIST, $1, $2);
  351. }
  352. | slist stmnt
  353. {
  354. $$ = new(OLIST, $1, $2);
  355. }
  356. labels:
  357. label
  358. | labels label
  359. {
  360. $$ = new(OLIST, $1, $2);
  361. }
  362. label:
  363. LCASE expr ':'
  364. {
  365. $$ = new(OCASE, $2, Z);
  366. }
  367. | LDEFAULT ':'
  368. {
  369. $$ = new(OCASE, Z, Z);
  370. }
  371. | LNAME ':'
  372. {
  373. $$ = new(OLABEL, dcllabel($1, 1), Z);
  374. }
  375. stmnt:
  376. error ';'
  377. {
  378. $$ = Z;
  379. }
  380. | ulstmnt
  381. | labels ulstmnt
  382. {
  383. $$ = new(OLIST, $1, $2);
  384. }
  385. forexpr:
  386. zcexpr
  387. | ctlist adlist
  388. {
  389. $$ = $2;
  390. }
  391. ulstmnt:
  392. zcexpr ';'
  393. | {
  394. markdcl();
  395. }
  396. block
  397. {
  398. $$ = revertdcl();
  399. if($$)
  400. $$ = new(OLIST, $$, $2);
  401. else
  402. $$ = $2;
  403. }
  404. | LIF '(' cexpr ')' stmnt
  405. {
  406. $$ = new(OIF, $3, new(OLIST, $5, Z));
  407. if($5 == Z)
  408. warn($3, "empty if body");
  409. }
  410. | LIF '(' cexpr ')' stmnt LELSE stmnt
  411. {
  412. $$ = new(OIF, $3, new(OLIST, $5, $7));
  413. if($5 == Z)
  414. warn($3, "empty if body");
  415. if($7 == Z)
  416. warn($3, "empty else body");
  417. }
  418. | { markdcl(); } LFOR '(' forexpr ';' zcexpr ';' zcexpr ')' stmnt
  419. {
  420. $$ = revertdcl();
  421. if($$){
  422. if($4)
  423. $4 = new(OLIST, $$, $4);
  424. else
  425. $4 = $$;
  426. }
  427. $$ = new(OFOR, new(OLIST, $6, new(OLIST, $4, $8)), $10);
  428. }
  429. | LWHILE '(' cexpr ')' stmnt
  430. {
  431. $$ = new(OWHILE, $3, $5);
  432. }
  433. | LDO stmnt LWHILE '(' cexpr ')' ';'
  434. {
  435. $$ = new(ODWHILE, $5, $2);
  436. }
  437. | LRETURN zcexpr ';'
  438. {
  439. $$ = new(ORETURN, $2, Z);
  440. $$->type = thisfn->link;
  441. }
  442. | LSWITCH '(' cexpr ')' stmnt
  443. {
  444. $$ = new(OCONST, Z, Z);
  445. $$->vconst = 0;
  446. $$->type = types[TINT];
  447. $3 = new(OSUB, $$, $3);
  448. $$ = new(OCONST, Z, Z);
  449. $$->vconst = 0;
  450. $$->type = types[TINT];
  451. $3 = new(OSUB, $$, $3);
  452. $$ = new(OSWITCH, $3, $5);
  453. }
  454. | LBREAK ';'
  455. {
  456. $$ = new(OBREAK, Z, Z);
  457. }
  458. | LCONTINUE ';'
  459. {
  460. $$ = new(OCONTINUE, Z, Z);
  461. }
  462. | LGOTO ltag ';'
  463. {
  464. $$ = new(OGOTO, dcllabel($2, 0), Z);
  465. }
  466. | LUSED '(' zelist ')' ';'
  467. {
  468. $$ = new(OUSED, $3, Z);
  469. }
  470. | LSET '(' zelist ')' ';'
  471. {
  472. $$ = new(OSET, $3, Z);
  473. }
  474. zcexpr:
  475. {
  476. $$ = Z;
  477. }
  478. | cexpr
  479. zexpr:
  480. {
  481. $$ = Z;
  482. }
  483. | lexpr
  484. lexpr:
  485. expr
  486. {
  487. $$ = new(OCAST, $1, Z);
  488. $$->type = types[TLONG];
  489. }
  490. cexpr:
  491. expr
  492. | cexpr ',' cexpr
  493. {
  494. $$ = new(OCOMMA, $1, $3);
  495. }
  496. expr:
  497. xuexpr
  498. | expr '*' expr
  499. {
  500. $$ = new(OMUL, $1, $3);
  501. }
  502. | expr '/' expr
  503. {
  504. $$ = new(ODIV, $1, $3);
  505. }
  506. | expr '%' expr
  507. {
  508. $$ = new(OMOD, $1, $3);
  509. }
  510. | expr '+' expr
  511. {
  512. $$ = new(OADD, $1, $3);
  513. }
  514. | expr '-' expr
  515. {
  516. $$ = new(OSUB, $1, $3);
  517. }
  518. | expr LRSH expr
  519. {
  520. $$ = new(OASHR, $1, $3);
  521. }
  522. | expr LLSH expr
  523. {
  524. $$ = new(OASHL, $1, $3);
  525. }
  526. | expr '<' expr
  527. {
  528. $$ = new(OLT, $1, $3);
  529. }
  530. | expr '>' expr
  531. {
  532. $$ = new(OGT, $1, $3);
  533. }
  534. | expr LLE expr
  535. {
  536. $$ = new(OLE, $1, $3);
  537. }
  538. | expr LGE expr
  539. {
  540. $$ = new(OGE, $1, $3);
  541. }
  542. | expr LEQ expr
  543. {
  544. $$ = new(OEQ, $1, $3);
  545. }
  546. | expr LNE expr
  547. {
  548. $$ = new(ONE, $1, $3);
  549. }
  550. | expr '&' expr
  551. {
  552. $$ = new(OAND, $1, $3);
  553. }
  554. | expr '^' expr
  555. {
  556. $$ = new(OXOR, $1, $3);
  557. }
  558. | expr '|' expr
  559. {
  560. $$ = new(OOR, $1, $3);
  561. }
  562. | expr LANDAND expr
  563. {
  564. $$ = new(OANDAND, $1, $3);
  565. }
  566. | expr LOROR expr
  567. {
  568. $$ = new(OOROR, $1, $3);
  569. }
  570. | expr '?' cexpr ':' expr
  571. {
  572. $$ = new(OCOND, $1, new(OLIST, $3, $5));
  573. }
  574. | expr '=' expr
  575. {
  576. $$ = new(OAS, $1, $3);
  577. }
  578. | expr LPE expr
  579. {
  580. $$ = new(OASADD, $1, $3);
  581. }
  582. | expr LME expr
  583. {
  584. $$ = new(OASSUB, $1, $3);
  585. }
  586. | expr LMLE expr
  587. {
  588. $$ = new(OASMUL, $1, $3);
  589. }
  590. | expr LDVE expr
  591. {
  592. $$ = new(OASDIV, $1, $3);
  593. }
  594. | expr LMDE expr
  595. {
  596. $$ = new(OASMOD, $1, $3);
  597. }
  598. | expr LLSHE expr
  599. {
  600. $$ = new(OASASHL, $1, $3);
  601. }
  602. | expr LRSHE expr
  603. {
  604. $$ = new(OASASHR, $1, $3);
  605. }
  606. | expr LANDE expr
  607. {
  608. $$ = new(OASAND, $1, $3);
  609. }
  610. | expr LXORE expr
  611. {
  612. $$ = new(OASXOR, $1, $3);
  613. }
  614. | expr LORE expr
  615. {
  616. $$ = new(OASOR, $1, $3);
  617. }
  618. xuexpr:
  619. uexpr
  620. | '(' tlist abdecor ')' xuexpr
  621. {
  622. $$ = new(OCAST, $5, Z);
  623. dodecl(NODECL, CXXX, $2, $3);
  624. $$->type = lastdcl;
  625. $$->xcast = 1;
  626. }
  627. | '(' tlist abdecor ')' '{' ilist '}' /* extension */
  628. {
  629. $$ = new(OSTRUCT, $6, Z);
  630. dodecl(NODECL, CXXX, $2, $3);
  631. $$->type = lastdcl;
  632. }
  633. uexpr:
  634. pexpr
  635. | '*' xuexpr
  636. {
  637. $$ = new(OIND, $2, Z);
  638. }
  639. | '&' xuexpr
  640. {
  641. $$ = new(OADDR, $2, Z);
  642. }
  643. | '+' xuexpr
  644. {
  645. $$ = new(OPOS, $2, Z);
  646. }
  647. | '-' xuexpr
  648. {
  649. $$ = new(ONEG, $2, Z);
  650. }
  651. | '!' xuexpr
  652. {
  653. $$ = new(ONOT, $2, Z);
  654. }
  655. | '~' xuexpr
  656. {
  657. $$ = new(OCOM, $2, Z);
  658. }
  659. | LPP xuexpr
  660. {
  661. $$ = new(OPREINC, $2, Z);
  662. }
  663. | LMM xuexpr
  664. {
  665. $$ = new(OPREDEC, $2, Z);
  666. }
  667. | LSIZEOF uexpr
  668. {
  669. $$ = new(OSIZE, $2, Z);
  670. }
  671. | LSIGNOF uexpr
  672. {
  673. $$ = new(OSIGN, $2, Z);
  674. }
  675. pexpr:
  676. '(' cexpr ')'
  677. {
  678. $$ = $2;
  679. }
  680. | LSIZEOF '(' tlist abdecor ')'
  681. {
  682. $$ = new(OSIZE, Z, Z);
  683. dodecl(NODECL, CXXX, $3, $4);
  684. $$->type = lastdcl;
  685. }
  686. | LSIGNOF '(' tlist abdecor ')'
  687. {
  688. $$ = new(OSIGN, Z, Z);
  689. dodecl(NODECL, CXXX, $3, $4);
  690. $$->type = lastdcl;
  691. }
  692. | pexpr '(' zelist ')'
  693. {
  694. $$ = new(OFUNC, $1, Z);
  695. if($1->op == ONAME)
  696. if($1->type == T)
  697. dodecl(xdecl, CXXX, types[TINT], $$);
  698. $$->right = invert($3);
  699. }
  700. | pexpr '[' cexpr ']'
  701. {
  702. $$ = new(OIND, new(OADD, $1, $3), Z);
  703. }
  704. | pexpr LMG ltag
  705. {
  706. $$ = new(ODOT, new(OIND, $1, Z), Z);
  707. $$->sym = $3;
  708. }
  709. | pexpr '.' ltag
  710. {
  711. $$ = new(ODOT, $1, Z);
  712. $$->sym = $3;
  713. }
  714. | pexpr LPP
  715. {
  716. $$ = new(OPOSTINC, $1, Z);
  717. }
  718. | pexpr LMM
  719. {
  720. $$ = new(OPOSTDEC, $1, Z);
  721. }
  722. | name
  723. | LCONST
  724. {
  725. $$ = new(OCONST, Z, Z);
  726. $$->type = types[TINT];
  727. $$->vconst = $1;
  728. $$->cstring = strdup(symb);
  729. }
  730. | LLCONST
  731. {
  732. $$ = new(OCONST, Z, Z);
  733. $$->type = types[TLONG];
  734. $$->vconst = $1;
  735. $$->cstring = strdup(symb);
  736. }
  737. | LUCONST
  738. {
  739. $$ = new(OCONST, Z, Z);
  740. $$->type = types[TUINT];
  741. $$->vconst = $1;
  742. $$->cstring = strdup(symb);
  743. }
  744. | LULCONST
  745. {
  746. $$ = new(OCONST, Z, Z);
  747. $$->type = types[TULONG];
  748. $$->vconst = $1;
  749. $$->cstring = strdup(symb);
  750. }
  751. | LDCONST
  752. {
  753. $$ = new(OCONST, Z, Z);
  754. $$->type = types[TDOUBLE];
  755. $$->fconst = $1;
  756. $$->cstring = strdup(symb);
  757. }
  758. | LFCONST
  759. {
  760. $$ = new(OCONST, Z, Z);
  761. $$->type = types[TFLOAT];
  762. $$->fconst = $1;
  763. $$->cstring = strdup(symb);
  764. }
  765. | LVLCONST
  766. {
  767. $$ = new(OCONST, Z, Z);
  768. $$->type = types[TVLONG];
  769. $$->vconst = $1;
  770. $$->cstring = strdup(symb);
  771. }
  772. | LUVLCONST
  773. {
  774. $$ = new(OCONST, Z, Z);
  775. $$->type = types[TUVLONG];
  776. $$->vconst = $1;
  777. $$->cstring = strdup(symb);
  778. }
  779. | string
  780. | lstring
  781. string:
  782. LSTRING
  783. {
  784. $$ = new(OSTRING, Z, Z);
  785. $$->type = typ(TARRAY, types[TCHAR]);
  786. $$->type->width = $1.l + 1;
  787. $$->cstring = $1.s;
  788. $$->sym = symstring;
  789. $$->etype = TARRAY;
  790. $$->class = CSTATIC;
  791. }
  792. | string LSTRING
  793. {
  794. char *s;
  795. int n;
  796. n = $1->type->width - 1;
  797. s = alloc(n+$2.l+MAXALIGN);
  798. memcpy(s, $1->cstring, n);
  799. memcpy(s+n, $2.s, $2.l);
  800. s[n+$2.l] = 0;
  801. $$ = $1;
  802. $$->type->width += $2.l;
  803. $$->cstring = s;
  804. }
  805. lstring:
  806. LLSTRING
  807. {
  808. $$ = new(OLSTRING, Z, Z);
  809. $$->type = typ(TARRAY, types[TRUNE]);
  810. $$->type->width = $1.l + sizeof(TRune);
  811. $$->rstring = (TRune*)$1.s;
  812. $$->sym = symstring;
  813. $$->etype = TARRAY;
  814. $$->class = CSTATIC;
  815. }
  816. | lstring LLSTRING
  817. {
  818. char *s;
  819. int n;
  820. n = $1->type->width - sizeof(TRune);
  821. s = alloc(n+$2.l+MAXALIGN);
  822. memcpy(s, $1->rstring, n);
  823. memcpy(s+n, $2.s, $2.l);
  824. *(TRune*)(s+n+$2.l) = 0;
  825. $$ = $1;
  826. $$->type->width += $2.l;
  827. $$->rstring = (TRune*)s;
  828. }
  829. zelist:
  830. {
  831. $$ = Z;
  832. }
  833. | elist
  834. elist:
  835. expr
  836. | elist ',' elist
  837. {
  838. $$ = new(OLIST, $1, $3);
  839. }
  840. sbody:
  841. '{'
  842. {
  843. $<tyty>$.t1 = strf;
  844. $<tyty>$.t2 = strl;
  845. $<tyty>$.t3 = lasttype;
  846. $<tyty>$.c = lastclass;
  847. strf = T;
  848. strl = T;
  849. lastbit = 0;
  850. firstbit = 1;
  851. lastclass = CXXX;
  852. lasttype = T;
  853. }
  854. edecl '}'
  855. {
  856. $$ = strf;
  857. strf = $<tyty>2.t1;
  858. strl = $<tyty>2.t2;
  859. lasttype = $<tyty>2.t3;
  860. lastclass = $<tyty>2.c;
  861. }
  862. zctlist:
  863. {
  864. lastclass = CXXX;
  865. lasttype = types[TINT];
  866. }
  867. | ctlist
  868. types:
  869. complex
  870. {
  871. $$.t = $1;
  872. $$.c = CXXX;
  873. }
  874. | tname
  875. {
  876. $$.t = simplet($1);
  877. $$.c = CXXX;
  878. }
  879. | gcnlist
  880. {
  881. $$.t = simplet($1);
  882. $$.c = simplec($1);
  883. $$.t = garbt($$.t, $1);
  884. }
  885. | complex gctnlist
  886. {
  887. $$.t = $1;
  888. $$.c = simplec($2);
  889. $$.t = garbt($$.t, $2);
  890. if($2 & ~BCLASS & ~BGARB)
  891. diag(Z, "duplicate types given: %T and %Q", $1, $2);
  892. }
  893. | tname gctnlist
  894. {
  895. $$.t = simplet(typebitor($1, $2));
  896. $$.c = simplec($2);
  897. $$.t = garbt($$.t, $2);
  898. }
  899. | gcnlist complex zgnlist
  900. {
  901. $$.t = $2;
  902. $$.c = simplec($1);
  903. $$.t = garbt($$.t, $1|$3);
  904. }
  905. | gcnlist tname
  906. {
  907. $$.t = simplet($2);
  908. $$.c = simplec($1);
  909. $$.t = garbt($$.t, $1);
  910. }
  911. | gcnlist tname gctnlist
  912. {
  913. $$.t = simplet(typebitor($2, $3));
  914. $$.c = simplec($1|$3);
  915. $$.t = garbt($$.t, $1|$3);
  916. }
  917. tlist:
  918. types
  919. {
  920. $$ = $1.t;
  921. if($1.c != CXXX)
  922. diag(Z, "illegal combination of class 4: %s", cnames[$1.c]);
  923. }
  924. ctlist:
  925. types
  926. {
  927. lasttype = $1.t;
  928. lastclass = $1.c;
  929. }
  930. complex:
  931. LSTRUCT ltag
  932. {
  933. dotag($2, TSTRUCT, 0);
  934. $$ = $2->suetag;
  935. }
  936. | LSTRUCT ltag
  937. {
  938. dotag($2, TSTRUCT, autobn);
  939. }
  940. sbody
  941. {
  942. $$ = $2->suetag;
  943. if($$->link != T)
  944. diag(Z, "redeclare tag: %s", $2->name);
  945. $$->link = $4;
  946. sualign($$);
  947. }
  948. | LSTRUCT sbody
  949. {
  950. taggen++;
  951. sprint(symb, "_%d_", taggen);
  952. $$ = dotag(lookup(), TSTRUCT, autobn);
  953. $$->link = $2;
  954. sualign($$);
  955. }
  956. | LUNION ltag
  957. {
  958. dotag($2, TUNION, 0);
  959. $$ = $2->suetag;
  960. }
  961. | LUNION ltag
  962. {
  963. dotag($2, TUNION, autobn);
  964. }
  965. sbody
  966. {
  967. $$ = $2->suetag;
  968. if($$->link != T)
  969. diag(Z, "redeclare tag: %s", $2->name);
  970. $$->link = $4;
  971. sualign($$);
  972. }
  973. | LUNION sbody
  974. {
  975. taggen++;
  976. sprint(symb, "_%d_", taggen);
  977. $$ = dotag(lookup(), TUNION, autobn);
  978. $$->link = $2;
  979. sualign($$);
  980. }
  981. | LENUM ltag
  982. {
  983. dotag($2, TENUM, 0);
  984. $$ = $2->suetag;
  985. if($$->link == T)
  986. $$->link = types[TINT];
  987. $$ = $$->link;
  988. }
  989. | LENUM ltag
  990. {
  991. dotag($2, TENUM, autobn);
  992. }
  993. '{'
  994. {
  995. en.tenum = T;
  996. en.cenum = T;
  997. }
  998. enum '}'
  999. {
  1000. $$ = $2->suetag;
  1001. if($$->link != T)
  1002. diag(Z, "redeclare tag: %s", $2->name);
  1003. if(en.tenum == T) {
  1004. diag(Z, "enum type ambiguous: %s", $2->name);
  1005. en.tenum = types[TINT];
  1006. }
  1007. $$->link = en.tenum;
  1008. $$ = en.tenum;
  1009. }
  1010. | LENUM '{'
  1011. {
  1012. en.tenum = T;
  1013. en.cenum = T;
  1014. }
  1015. enum '}'
  1016. {
  1017. $$ = en.tenum;
  1018. }
  1019. | LTYPE
  1020. {
  1021. $$ = tcopy($1->type);
  1022. }
  1023. gctnlist:
  1024. gctname
  1025. | gctnlist gctname
  1026. {
  1027. $$ = typebitor($1, $2);
  1028. }
  1029. zgnlist:
  1030. {
  1031. $$ = 0;
  1032. }
  1033. | zgnlist gname
  1034. {
  1035. $$ = typebitor($1, $2);
  1036. }
  1037. gctname:
  1038. tname
  1039. | gname
  1040. | cname
  1041. gcnlist:
  1042. gcname
  1043. | gcnlist gcname
  1044. {
  1045. $$ = typebitor($1, $2);
  1046. }
  1047. gcname:
  1048. gname
  1049. | cname
  1050. enum:
  1051. LNAME
  1052. {
  1053. doenum($1, Z);
  1054. }
  1055. | LNAME '=' expr
  1056. {
  1057. doenum($1, $3);
  1058. }
  1059. | enum ','
  1060. | enum ',' enum
  1061. tname: /* type words */
  1062. LCHAR { $$ = BCHAR; }
  1063. | LSHORT { $$ = BSHORT; }
  1064. | LINT { $$ = BINT; }
  1065. | LLONG { $$ = BLONG; }
  1066. | LSIGNED { $$ = BSIGNED; }
  1067. | LUNSIGNED { $$ = BUNSIGNED; }
  1068. | LFLOAT { $$ = BFLOAT; }
  1069. | LDOUBLE { $$ = BDOUBLE; }
  1070. | LVOID { $$ = BVOID; }
  1071. cname: /* class words */
  1072. LAUTO { $$ = BAUTO; }
  1073. | LSTATIC { $$ = BSTATIC; }
  1074. | LEXTERN { $$ = BEXTERN; }
  1075. | LTYPEDEF { $$ = BTYPEDEF; }
  1076. | LTYPESTR { $$ = BTYPESTR; }
  1077. | LREGISTER { $$ = BREGISTER; }
  1078. | LINLINE { $$ = 0; }
  1079. gname: /* garbage words */
  1080. LCONSTNT { $$ = BCONSTNT; }
  1081. | LVOLATILE { $$ = BVOLATILE; }
  1082. | LRESTRICT { $$ = 0; }
  1083. name:
  1084. LNAME
  1085. {
  1086. $$ = new(ONAME, Z, Z);
  1087. if($1->class == CLOCAL)
  1088. $1 = mkstatic($1);
  1089. $$->sym = $1;
  1090. $$->type = $1->type;
  1091. $$->etype = TVOID;
  1092. if($$->type != T)
  1093. $$->etype = $$->type->etype;
  1094. $$->xoffset = $1->offset;
  1095. $$->class = $1->class;
  1096. $1->aused = 1;
  1097. }
  1098. tag:
  1099. ltag
  1100. {
  1101. $$ = new(ONAME, Z, Z);
  1102. $$->sym = $1;
  1103. $$->type = $1->type;
  1104. $$->etype = TVOID;
  1105. if($$->type != T)
  1106. $$->etype = $$->type->etype;
  1107. $$->xoffset = $1->offset;
  1108. $$->class = $1->class;
  1109. }
  1110. ltag:
  1111. LNAME
  1112. | LTYPE
  1113. %%