cc.y 16 KB

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