cc.y 16 KB

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