com.c 18 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  1. #include "cc.h"
  2. void
  3. complex(Node *n)
  4. {
  5. if(n == Z)
  6. return;
  7. nearln = n->lineno;
  8. if(debug['t'])
  9. if(n->op != OCONST)
  10. prtree(n, "pre complex");
  11. if(tcom(n))
  12. return;
  13. if(debug['t'])
  14. if(n->op != OCONST)
  15. prtree(n, "t complex");
  16. ccom(n);
  17. if(debug['t'])
  18. if(n->op != OCONST)
  19. prtree(n, "c complex");
  20. acom(n);
  21. if(debug['t'])
  22. if(n->op != OCONST)
  23. prtree(n, "a complex");
  24. xcom(n);
  25. if(debug['t'])
  26. if(n->op != OCONST)
  27. prtree(n, "x complex");
  28. }
  29. /*
  30. * evaluate types
  31. * evaluate lvalues (addable == 1)
  32. */
  33. enum
  34. {
  35. ADDROF = 1<<0,
  36. CASTOF = 1<<1,
  37. ADDROP = 1<<2,
  38. };
  39. int
  40. tcom(Node *n)
  41. {
  42. return tcomo(n, ADDROF);
  43. }
  44. int
  45. tcomo(Node *n, int f)
  46. {
  47. Node *l, *r;
  48. Type *t;
  49. int o;
  50. if(n == Z) {
  51. diag(Z, "Z in tcom");
  52. errorexit();
  53. }
  54. n->addable = 0;
  55. l = n->left;
  56. r = n->right;
  57. switch(n->op) {
  58. default:
  59. diag(n, "unknown op in type complex: %O", n->op);
  60. goto bad;
  61. case ODOTDOT:
  62. /*
  63. * tcom has already been called on this subtree
  64. */
  65. *n = *n->left;
  66. if(n->type == T)
  67. goto bad;
  68. break;
  69. case OCAST:
  70. if(n->type == T)
  71. break;
  72. if(n->type->width == types[TLONG]->width) {
  73. if(tcomo(l, ADDROF|CASTOF))
  74. goto bad;
  75. } else
  76. if(tcom(l))
  77. goto bad;
  78. if(isfunct(n))
  79. break;
  80. if(tcompat(n, l->type, n->type, tcast))
  81. goto bad;
  82. break;
  83. case ORETURN:
  84. if(l == Z) {
  85. if(n->type->etype != TVOID)
  86. warn(n, "null return of a typed function");
  87. break;
  88. }
  89. if(tcom(l))
  90. goto bad;
  91. typeext(n->type, l);
  92. if(tcompat(n, n->type, l->type, tasign))
  93. break;
  94. constas(n, n->type, l->type);
  95. if(!sametype(n->type, l->type)) {
  96. l = new1(OCAST, l, Z);
  97. l->type = n->type;
  98. n->left = l;
  99. }
  100. break;
  101. case OASI: /* same as as, but no test for const */
  102. n->op = OAS;
  103. o = tcom(l);
  104. if(o | tcom(r))
  105. goto bad;
  106. typeext(l->type, r);
  107. if(tlvalue(l) || tcompat(n, l->type, r->type, tasign))
  108. goto bad;
  109. if(!sametype(l->type, r->type)) {
  110. r = new1(OCAST, r, Z);
  111. r->type = l->type;
  112. n->right = r;
  113. }
  114. n->type = l->type;
  115. break;
  116. case OAS:
  117. o = tcom(l);
  118. if(o | tcom(r))
  119. goto bad;
  120. if(tlvalue(l))
  121. goto bad;
  122. if(isfunct(n))
  123. break;
  124. typeext(l->type, r);
  125. if(tcompat(n, l->type, r->type, tasign))
  126. goto bad;
  127. constas(n, l->type, r->type);
  128. if(!sametype(l->type, r->type)) {
  129. r = new1(OCAST, r, Z);
  130. r->type = l->type;
  131. n->right = r;
  132. }
  133. n->type = l->type;
  134. break;
  135. case OASADD:
  136. case OASSUB:
  137. o = tcom(l);
  138. if(o | tcom(r))
  139. goto bad;
  140. if(tlvalue(l))
  141. goto bad;
  142. if(isfunct(n))
  143. break;
  144. typeext1(l->type, r);
  145. if(tcompat(n, l->type, r->type, tasadd))
  146. goto bad;
  147. constas(n, l->type, r->type);
  148. t = l->type;
  149. arith(n, 0);
  150. while(n->left->op == OCAST)
  151. n->left = n->left->left;
  152. if(!sametype(t, n->type) && !mixedasop(t, n->type)) {
  153. r = new1(OCAST, n->right, Z);
  154. r->type = t;
  155. n->right = r;
  156. n->type = t;
  157. }
  158. break;
  159. case OASMUL:
  160. case OASLMUL:
  161. case OASDIV:
  162. case OASLDIV:
  163. o = tcom(l);
  164. if(o | tcom(r))
  165. goto bad;
  166. if(tlvalue(l))
  167. goto bad;
  168. if(isfunct(n))
  169. break;
  170. typeext1(l->type, r);
  171. if(tcompat(n, l->type, r->type, tmul))
  172. goto bad;
  173. constas(n, l->type, r->type);
  174. t = l->type;
  175. arith(n, 0);
  176. while(n->left->op == OCAST)
  177. n->left = n->left->left;
  178. if(!sametype(t, n->type) && !mixedasop(t, n->type)) {
  179. r = new1(OCAST, n->right, Z);
  180. r->type = t;
  181. n->right = r;
  182. n->type = t;
  183. }
  184. if(typeu[n->type->etype]) {
  185. if(n->op == OASDIV)
  186. n->op = OASLDIV;
  187. if(n->op == OASMUL)
  188. n->op = OASLMUL;
  189. }
  190. break;
  191. case OASLSHR:
  192. case OASASHR:
  193. case OASASHL:
  194. o = tcom(l);
  195. if(o | tcom(r))
  196. goto bad;
  197. if(tlvalue(l))
  198. goto bad;
  199. if(isfunct(n))
  200. break;
  201. if(tcompat(n, l->type, r->type, tand))
  202. goto bad;
  203. n->type = l->type;
  204. if(typeu[n->type->etype]) {
  205. if(n->op == OASASHR)
  206. n->op = OASLSHR;
  207. }
  208. break;
  209. case OASMOD:
  210. case OASLMOD:
  211. case OASOR:
  212. case OASAND:
  213. case OASXOR:
  214. o = tcom(l);
  215. if(o | tcom(r))
  216. goto bad;
  217. if(tlvalue(l))
  218. goto bad;
  219. if(isfunct(n))
  220. break;
  221. if(tcompat(n, l->type, r->type, tand))
  222. goto bad;
  223. t = l->type;
  224. arith(n, 0);
  225. while(n->left->op == OCAST)
  226. n->left = n->left->left;
  227. if(!sametype(t, n->type) && !mixedasop(t, n->type)) {
  228. r = new1(OCAST, n->right, Z);
  229. r->type = t;
  230. n->right = r;
  231. n->type = t;
  232. }
  233. if(typeu[n->type->etype]) {
  234. if(n->op == OASMOD)
  235. n->op = OASLMOD;
  236. }
  237. break;
  238. case OPREINC:
  239. case OPREDEC:
  240. case OPOSTINC:
  241. case OPOSTDEC:
  242. if(tcom(l))
  243. goto bad;
  244. if(tlvalue(l))
  245. goto bad;
  246. if(isfunct(n))
  247. break;
  248. if(tcompat(n, l->type, types[TINT], tadd))
  249. goto bad;
  250. n->type = l->type;
  251. if(n->type->etype == TIND)
  252. if(n->type->link->width < 1)
  253. diag(n, "inc/dec of a void pointer");
  254. break;
  255. case OEQ:
  256. case ONE:
  257. o = tcom(l);
  258. if(o | tcom(r))
  259. goto bad;
  260. if(isfunct(n))
  261. break;
  262. typeext(l->type, r);
  263. typeext(r->type, l);
  264. if(tcompat(n, l->type, r->type, trel))
  265. goto bad;
  266. arith(n, 0);
  267. n->type = types[TINT];
  268. break;
  269. case OLT:
  270. case OGE:
  271. case OGT:
  272. case OLE:
  273. o = tcom(l);
  274. if(o | tcom(r))
  275. goto bad;
  276. if(isfunct(n))
  277. break;
  278. typeext1(l->type, r);
  279. typeext1(r->type, l);
  280. if(tcompat(n, l->type, r->type, trel))
  281. goto bad;
  282. arith(n, 0);
  283. if(typeu[n->type->etype])
  284. n->op = logrel[relindex(n->op)];
  285. n->type = types[TINT];
  286. break;
  287. case OCOND:
  288. o = tcom(l);
  289. o |= tcom(r->left);
  290. if(o | tcom(r->right))
  291. goto bad;
  292. if(r->right->type->etype == TIND && vconst(r->left) == 0) {
  293. r->left->type = r->right->type;
  294. r->left->vconst = 0;
  295. }
  296. if(r->left->type->etype == TIND && vconst(r->right) == 0) {
  297. r->right->type = r->left->type;
  298. r->right->vconst = 0;
  299. }
  300. if(sametype(r->right->type, r->left->type)) {
  301. r->type = r->right->type;
  302. n->type = r->type;
  303. break;
  304. }
  305. if(tcompat(r, r->left->type, r->right->type, trel))
  306. goto bad;
  307. arith(r, 0);
  308. n->type = r->type;
  309. break;
  310. case OADD:
  311. o = tcom(l);
  312. if(o | tcom(r))
  313. goto bad;
  314. if(isfunct(n))
  315. break;
  316. if(tcompat(n, l->type, r->type, tadd))
  317. goto bad;
  318. arith(n, 1);
  319. break;
  320. case OSUB:
  321. o = tcom(l);
  322. if(o | tcom(r))
  323. goto bad;
  324. if(isfunct(n))
  325. break;
  326. if(tcompat(n, l->type, r->type, tsub))
  327. goto bad;
  328. arith(n, 1);
  329. break;
  330. case OMUL:
  331. case OLMUL:
  332. case ODIV:
  333. case OLDIV:
  334. o = tcom(l);
  335. if(o | tcom(r))
  336. goto bad;
  337. if(isfunct(n))
  338. break;
  339. if(tcompat(n, l->type, r->type, tmul))
  340. goto bad;
  341. arith(n, 1);
  342. if(typeu[n->type->etype]) {
  343. if(n->op == ODIV)
  344. n->op = OLDIV;
  345. if(n->op == OMUL)
  346. n->op = OLMUL;
  347. }
  348. break;
  349. case OLSHR:
  350. case OASHL:
  351. case OASHR:
  352. o = tcom(l);
  353. if(o | tcom(r))
  354. goto bad;
  355. if(isfunct(n))
  356. break;
  357. if(tcompat(n, l->type, r->type, tand))
  358. goto bad;
  359. n->right = Z;
  360. arith(n, 1);
  361. n->right = new1(OCAST, r, Z);
  362. n->right->type = types[TINT];
  363. if(typeu[n->type->etype])
  364. if(n->op == OASHR)
  365. n->op = OLSHR;
  366. break;
  367. case OAND:
  368. case OOR:
  369. case OXOR:
  370. o = tcom(l);
  371. if(o | tcom(r))
  372. goto bad;
  373. if(isfunct(n))
  374. break;
  375. if(tcompat(n, l->type, r->type, tand))
  376. goto bad;
  377. arith(n, 1);
  378. break;
  379. case OMOD:
  380. case OLMOD:
  381. o = tcom(l);
  382. if(o | tcom(r))
  383. goto bad;
  384. if(isfunct(n))
  385. break;
  386. if(tcompat(n, l->type, r->type, tand))
  387. goto bad;
  388. arith(n, 1);
  389. if(typeu[n->type->etype])
  390. n->op = OLMOD;
  391. break;
  392. case OPOS:
  393. if(tcom(l))
  394. goto bad;
  395. if(isfunct(n))
  396. break;
  397. r = l;
  398. l = new(OCONST, Z, Z);
  399. l->vconst = 0;
  400. l->type = types[TINT];
  401. n->op = OADD;
  402. n->right = r;
  403. n->left = l;
  404. if(tcom(l))
  405. goto bad;
  406. if(tcompat(n, l->type, r->type, tsub))
  407. goto bad;
  408. arith(n, 1);
  409. break;
  410. case ONEG:
  411. if(tcom(l))
  412. goto bad;
  413. if(isfunct(n))
  414. break;
  415. r = l;
  416. l = new(OCONST, Z, Z);
  417. l->vconst = 0;
  418. l->type = types[TINT];
  419. n->op = OSUB;
  420. n->right = r;
  421. n->left = l;
  422. if(tcom(l))
  423. goto bad;
  424. if(tcompat(n, l->type, r->type, tsub))
  425. goto bad;
  426. arith(n, 1);
  427. break;
  428. case OCOM:
  429. if(tcom(l))
  430. goto bad;
  431. if(isfunct(n))
  432. break;
  433. r = l;
  434. l = new(OCONST, Z, Z);
  435. l->vconst = -1;
  436. l->type = types[TINT];
  437. n->op = OXOR;
  438. n->right = r;
  439. n->left = l;
  440. if(tcom(l))
  441. goto bad;
  442. if(tcompat(n, l->type, r->type, tand))
  443. goto bad;
  444. arith(n, 1);
  445. break;
  446. case ONOT:
  447. if(tcom(l))
  448. goto bad;
  449. if(isfunct(n))
  450. break;
  451. if(tcompat(n, T, l->type, tnot))
  452. goto bad;
  453. n->type = types[TINT];
  454. break;
  455. case OANDAND:
  456. case OOROR:
  457. o = tcom(l);
  458. if(o | tcom(r))
  459. goto bad;
  460. if(tcompat(n, T, l->type, tnot) |
  461. tcompat(n, T, r->type, tnot))
  462. goto bad;
  463. n->type = types[TINT];
  464. break;
  465. case OCOMMA:
  466. o = tcom(l);
  467. if(o | tcom(r))
  468. goto bad;
  469. n->type = r->type;
  470. break;
  471. case OSIGN: /* extension signof(type) returns a hash */
  472. if(l != Z) {
  473. if(l->op != OSTRING && l->op != OLSTRING)
  474. if(tcomo(l, 0))
  475. goto bad;
  476. if(l->op == OBIT) {
  477. diag(n, "signof bitfield");
  478. goto bad;
  479. }
  480. n->type = l->type;
  481. }
  482. if(n->type == T)
  483. goto bad;
  484. if(n->type->width < 0) {
  485. diag(n, "signof undefined type");
  486. goto bad;
  487. }
  488. n->op = OCONST;
  489. n->left = Z;
  490. n->right = Z;
  491. n->vconst = convvtox(signature(n->type, 10), TULONG);
  492. n->type = types[TULONG];
  493. break;
  494. case OSIZE:
  495. if(l != Z) {
  496. if(l->op != OSTRING && l->op != OLSTRING)
  497. if(tcomo(l, 0))
  498. goto bad;
  499. if(l->op == OBIT) {
  500. diag(n, "sizeof bitfield");
  501. goto bad;
  502. }
  503. n->type = l->type;
  504. }
  505. if(n->type == T)
  506. goto bad;
  507. if(n->type->width <= 0) {
  508. diag(n, "sizeof undefined type");
  509. goto bad;
  510. }
  511. if(n->type->etype == TFUNC) {
  512. diag(n, "sizeof function");
  513. goto bad;
  514. }
  515. n->op = OCONST;
  516. n->left = Z;
  517. n->right = Z;
  518. n->vconst = convvtox(n->type->width, TINT);
  519. n->type = types[TINT];
  520. break;
  521. case OFUNC:
  522. o = tcomo(l, 0);
  523. if(o)
  524. goto bad;
  525. if(l->type->etype == TIND && l->type->link->etype == TFUNC) {
  526. l = new1(OIND, l, Z);
  527. l->type = l->left->type->link;
  528. n->left = l;
  529. }
  530. if(tcompat(n, T, l->type, tfunct))
  531. goto bad;
  532. if(o | tcoma(l, r, l->type->down, 1))
  533. goto bad;
  534. n->type = l->type->link;
  535. if(!debug['B'])
  536. if(l->type->down == T || l->type->down->etype == TOLD) {
  537. nerrors--;
  538. diag(n, "function args not checked: %F", l);
  539. }
  540. dpcheck(n);
  541. break;
  542. case ONAME:
  543. if(n->type == T) {
  544. diag(n, "name not declared: %F", n);
  545. goto bad;
  546. }
  547. if(n->type->etype == TENUM) {
  548. n->op = OCONST;
  549. n->type = n->sym->tenum;
  550. if(!typefd[n->type->etype])
  551. n->vconst = n->sym->vconst;
  552. else
  553. n->fconst = n->sym->fconst;
  554. break;
  555. }
  556. n->addable = 1;
  557. if(n->class == CEXREG) {
  558. n->op = OREGISTER;
  559. n->reg = n->sym->offset;
  560. n->xoffset = 0;
  561. break;
  562. }
  563. break;
  564. case OLSTRING:
  565. if(n->type->link != types[TUSHORT]) {
  566. o = outstring(0, 0);
  567. while(o & 3) {
  568. outlstring(L"", sizeof(ushort));
  569. o = outlstring(0, 0);
  570. }
  571. }
  572. n->op = ONAME;
  573. n->xoffset = outlstring(n->rstring, n->type->width);
  574. n->addable = 1;
  575. break;
  576. case OSTRING:
  577. if(n->type->link != types[TCHAR]) {
  578. o = outstring(0, 0);
  579. while(o & 3) {
  580. outstring("", 1);
  581. o = outstring(0, 0);
  582. }
  583. }
  584. n->op = ONAME;
  585. n->xoffset = outstring(n->cstring, n->type->width);
  586. n->addable = 1;
  587. break;
  588. case OCONST:
  589. break;
  590. case ODOT:
  591. if(tcom(l))
  592. goto bad;
  593. if(tcompat(n, T, l->type, tdot))
  594. goto bad;
  595. if(tcomd(n))
  596. goto bad;
  597. break;
  598. case OADDR:
  599. if(tcomo(l, ADDROP))
  600. goto bad;
  601. if(tlvalue(l))
  602. goto bad;
  603. if(l->type->nbits) {
  604. diag(n, "address of a bit field");
  605. goto bad;
  606. }
  607. if(l->op == OREGISTER) {
  608. diag(n, "address of a register");
  609. goto bad;
  610. }
  611. n->type = typ(TIND, l->type);
  612. n->type->width = types[TIND]->width;
  613. break;
  614. case OIND:
  615. if(tcom(l))
  616. goto bad;
  617. if(tcompat(n, T, l->type, tindir))
  618. goto bad;
  619. n->type = l->type->link;
  620. n->addable = 1;
  621. break;
  622. case OSTRUCT:
  623. if(tcomx(n))
  624. goto bad;
  625. break;
  626. }
  627. t = n->type;
  628. if(t == T)
  629. goto bad;
  630. if(t->width < 0) {
  631. snap(t);
  632. if(t->width < 0) {
  633. if(typesu[t->etype] && t->tag)
  634. diag(n, "structure not fully declared %s", t->tag->name);
  635. else
  636. diag(n, "structure not fully declared");
  637. goto bad;
  638. }
  639. }
  640. if(typeaf[t->etype]) {
  641. if(f & ADDROF)
  642. goto addaddr;
  643. if(f & ADDROP)
  644. warn(n, "address of array/func ignored");
  645. }
  646. return 0;
  647. addaddr:
  648. if(tlvalue(n))
  649. goto bad;
  650. l = new1(OXXX, Z, Z);
  651. *l = *n;
  652. n->op = OADDR;
  653. if(l->type->etype == TARRAY)
  654. l->type = l->type->link;
  655. n->left = l;
  656. n->right = Z;
  657. n->addable = 0;
  658. n->type = typ(TIND, l->type);
  659. n->type->width = types[TIND]->width;
  660. return 0;
  661. bad:
  662. n->type = T;
  663. return 1;
  664. }
  665. int
  666. tcoma(Node *l, Node *n, Type *t, int f)
  667. {
  668. Node *n1;
  669. int o;
  670. if(t != T)
  671. if(t->etype == TOLD || t->etype == TDOT) /* .../old in prototype */
  672. t = T;
  673. if(n == Z) {
  674. if(t != T && !sametype(t, types[TVOID])) {
  675. diag(n, "not enough function arguments: %F", l);
  676. return 1;
  677. }
  678. return 0;
  679. }
  680. if(n->op == OLIST) {
  681. o = tcoma(l, n->left, t, 0);
  682. if(t != T) {
  683. t = t->down;
  684. if(t == T)
  685. t = types[TVOID];
  686. }
  687. return o | tcoma(l, n->right, t, 1);
  688. }
  689. if(f && t != T)
  690. tcoma(l, Z, t->down, 0);
  691. if(tcom(n) || tcompat(n, T, n->type, targ))
  692. return 1;
  693. if(sametype(t, types[TVOID])) {
  694. diag(n, "too many function arguments: %F", l);
  695. return 1;
  696. }
  697. if(t != T) {
  698. typeext(t, n);
  699. if(stcompat(nodproto, t, n->type, tasign)) {
  700. diag(l, "argument prototype mismatch \"%T\" for \"%T\": %F",
  701. n->type, t, l);
  702. return 1;
  703. }
  704. switch(t->etype) {
  705. case TCHAR:
  706. case TSHORT:
  707. t = types[TINT];
  708. break;
  709. case TUCHAR:
  710. case TUSHORT:
  711. t = types[TUINT];
  712. break;
  713. }
  714. } else
  715. switch(n->type->etype)
  716. {
  717. case TCHAR:
  718. case TSHORT:
  719. t = types[TINT];
  720. break;
  721. case TUCHAR:
  722. case TUSHORT:
  723. t = types[TUINT];
  724. break;
  725. case TFLOAT:
  726. t = types[TDOUBLE];
  727. }
  728. if(t != T && !sametype(t, n->type)) {
  729. n1 = new1(OXXX, Z, Z);
  730. *n1 = *n;
  731. n->op = OCAST;
  732. n->left = n1;
  733. n->right = Z;
  734. n->type = t;
  735. n->addable = 0;
  736. }
  737. return 0;
  738. }
  739. int
  740. tcomd(Node *n)
  741. {
  742. Type *t;
  743. long o;
  744. o = 0;
  745. t = dotsearch(n->sym, n->left->type->link, n, &o);
  746. if(t == T) {
  747. diag(n, "not a member of struct/union: %F", n);
  748. return 1;
  749. }
  750. makedot(n, t, o);
  751. return 0;
  752. }
  753. int
  754. tcomx(Node *n)
  755. {
  756. Type *t;
  757. Node *l, *r, **ar, **al;
  758. int e;
  759. e = 0;
  760. if(n->type->etype != TSTRUCT) {
  761. diag(n, "constructor must be a structure");
  762. return 1;
  763. }
  764. l = invert(n->left);
  765. n->left = l;
  766. al = &n->left;
  767. for(t = n->type->link; t != T; t = t->down) {
  768. if(l == Z) {
  769. diag(n, "constructor list too short");
  770. return 1;
  771. }
  772. if(l->op == OLIST) {
  773. r = l->left;
  774. ar = &l->left;
  775. al = &l->right;
  776. l = l->right;
  777. } else {
  778. r = l;
  779. ar = al;
  780. l = Z;
  781. }
  782. if(tcom(r))
  783. e++;
  784. typeext(t, r);
  785. if(tcompat(n, t, r->type, tasign))
  786. e++;
  787. constas(n, t, r->type);
  788. if(!e && !sametype(t, r->type)) {
  789. r = new1(OCAST, r, Z);
  790. r->type = t;
  791. *ar = r;
  792. }
  793. }
  794. if(l != Z) {
  795. diag(n, "constructor list too long");
  796. return 1;
  797. }
  798. return e;
  799. }
  800. int
  801. tlvalue(Node *n)
  802. {
  803. if(!n->addable) {
  804. diag(n, "not an l-value");
  805. return 1;
  806. }
  807. return 0;
  808. }
  809. /*
  810. * general rewrite
  811. * (IND(ADDR x)) ==> x
  812. * (ADDR(IND x)) ==> x
  813. * remove some zero operands
  814. * remove no op casts
  815. * evaluate constants
  816. */
  817. void
  818. ccom(Node *n)
  819. {
  820. Node *l, *r;
  821. int t;
  822. loop:
  823. if(n == Z)
  824. return;
  825. l = n->left;
  826. r = n->right;
  827. switch(n->op) {
  828. case OAS:
  829. case OASXOR:
  830. case OASAND:
  831. case OASOR:
  832. case OASMOD:
  833. case OASLMOD:
  834. case OASLSHR:
  835. case OASASHR:
  836. case OASASHL:
  837. case OASDIV:
  838. case OASLDIV:
  839. case OASMUL:
  840. case OASLMUL:
  841. case OASSUB:
  842. case OASADD:
  843. ccom(l);
  844. ccom(r);
  845. if(n->op == OASLSHR || n->op == OASASHR || n->op == OASASHL)
  846. if(r->op == OCONST) {
  847. t = n->type->width * 8; /* bits per byte */
  848. if(r->vconst >= t || r->vconst < 0)
  849. warn(n, "stupid shift: %lld", r->vconst);
  850. }
  851. break;
  852. case OCAST:
  853. ccom(l);
  854. if(l->op == OCONST) {
  855. evconst(n);
  856. if(n->op == OCONST)
  857. break;
  858. }
  859. if(nocast(l->type, n->type)) {
  860. l->type = n->type;
  861. *n = *l;
  862. }
  863. break;
  864. case OCOND:
  865. ccom(l);
  866. ccom(r);
  867. if(l->op == OCONST)
  868. if(vconst(l) == 0)
  869. *n = *r->right;
  870. else
  871. *n = *r->left;
  872. break;
  873. case OREGISTER:
  874. case OINDREG:
  875. case OCONST:
  876. case ONAME:
  877. break;
  878. case OADDR:
  879. ccom(l);
  880. l->etype = TVOID;
  881. if(l->op == OIND) {
  882. l->left->type = n->type;
  883. *n = *l->left;
  884. break;
  885. }
  886. goto common;
  887. case OIND:
  888. ccom(l);
  889. if(l->op == OADDR) {
  890. l->left->type = n->type;
  891. *n = *l->left;
  892. break;
  893. }
  894. goto common;
  895. case OEQ:
  896. case ONE:
  897. case OLE:
  898. case OGE:
  899. case OLT:
  900. case OGT:
  901. case OLS:
  902. case OHS:
  903. case OLO:
  904. case OHI:
  905. ccom(l);
  906. ccom(r);
  907. relcon(l, r);
  908. relcon(r, l);
  909. goto common;
  910. case OASHR:
  911. case OASHL:
  912. case OLSHR:
  913. ccom(l);
  914. if(vconst(l) == 0 && !side(r)) {
  915. *n = *l;
  916. break;
  917. }
  918. ccom(r);
  919. if(vconst(r) == 0) {
  920. *n = *l;
  921. break;
  922. }
  923. if(r->op == OCONST) {
  924. t = n->type->width * 8; /* bits per byte */
  925. if(r->vconst >= t || r->vconst <= -t)
  926. warn(n, "stupid shift: %lld", r->vconst);
  927. }
  928. goto common;
  929. case OMUL:
  930. case OLMUL:
  931. ccom(l);
  932. t = vconst(l);
  933. if(t == 0 && !side(r)) {
  934. *n = *l;
  935. break;
  936. }
  937. if(t == 1) {
  938. *n = *r;
  939. goto loop;
  940. }
  941. ccom(r);
  942. t = vconst(r);
  943. if(t == 0 && !side(l)) {
  944. *n = *r;
  945. break;
  946. }
  947. if(t == 1) {
  948. *n = *l;
  949. break;
  950. }
  951. goto common;
  952. case ODIV:
  953. case OLDIV:
  954. ccom(l);
  955. if(vconst(l) == 0 && !side(r)) {
  956. *n = *l;
  957. break;
  958. }
  959. ccom(r);
  960. t = vconst(r);
  961. if(t == 0) {
  962. diag(n, "divide check");
  963. *n = *r;
  964. break;
  965. }
  966. if(t == 1) {
  967. *n = *l;
  968. break;
  969. }
  970. goto common;
  971. case OSUB:
  972. ccom(r);
  973. if(r->op == OCONST) {
  974. if(typefd[r->type->etype]) {
  975. n->op = OADD;
  976. r->fconst = -r->fconst;
  977. goto loop;
  978. } else {
  979. n->op = OADD;
  980. r->vconst = -r->vconst;
  981. goto loop;
  982. }
  983. }
  984. ccom(l);
  985. goto common;
  986. case OXOR:
  987. case OOR:
  988. case OADD:
  989. ccom(l);
  990. if(vconst(l) == 0) {
  991. *n = *r;
  992. goto loop;
  993. }
  994. ccom(r);
  995. if(vconst(r) == 0) {
  996. *n = *l;
  997. break;
  998. }
  999. goto commun;
  1000. case OAND:
  1001. ccom(l);
  1002. ccom(r);
  1003. if(vconst(l) == 0 && !side(r)) {
  1004. *n = *l;
  1005. break;
  1006. }
  1007. if(vconst(r) == 0 && !side(l)) {
  1008. *n = *r;
  1009. break;
  1010. }
  1011. commun:
  1012. /* look for commutative constant */
  1013. if(r->op == OCONST) {
  1014. if(l->op == n->op) {
  1015. if(l->left->op == OCONST) {
  1016. n->right = l->right;
  1017. l->right = r;
  1018. goto loop;
  1019. }
  1020. if(l->right->op == OCONST) {
  1021. n->right = l->left;
  1022. l->left = r;
  1023. goto loop;
  1024. }
  1025. }
  1026. }
  1027. if(l->op == OCONST) {
  1028. if(r->op == n->op) {
  1029. if(r->left->op == OCONST) {
  1030. n->left = r->right;
  1031. r->right = l;
  1032. goto loop;
  1033. }
  1034. if(r->right->op == OCONST) {
  1035. n->left = r->left;
  1036. r->left = l;
  1037. goto loop;
  1038. }
  1039. }
  1040. }
  1041. goto common;
  1042. case OANDAND:
  1043. ccom(l);
  1044. if(vconst(l) == 0) {
  1045. *n = *l;
  1046. break;
  1047. }
  1048. ccom(r);
  1049. goto common;
  1050. case OOROR:
  1051. ccom(l);
  1052. if(l->op == OCONST && l->vconst != 0) {
  1053. *n = *l;
  1054. n->vconst = 1;
  1055. break;
  1056. }
  1057. ccom(r);
  1058. goto common;
  1059. default:
  1060. if(l != Z)
  1061. ccom(l);
  1062. if(r != Z)
  1063. ccom(r);
  1064. common:
  1065. if(l != Z)
  1066. if(l->op != OCONST)
  1067. break;
  1068. if(r != Z)
  1069. if(r->op != OCONST)
  1070. break;
  1071. evconst(n);
  1072. }
  1073. }