com.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  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. if(!machcap(n)) {
  416. r = l;
  417. l = new(OCONST, Z, Z);
  418. l->vconst = 0;
  419. l->type = types[TINT];
  420. n->op = OSUB;
  421. n->right = r;
  422. n->left = l;
  423. if(tcom(l))
  424. goto bad;
  425. if(tcompat(n, l->type, r->type, tsub))
  426. goto bad;
  427. }
  428. arith(n, 1);
  429. break;
  430. case OCOM:
  431. if(tcom(l))
  432. goto bad;
  433. if(isfunct(n))
  434. break;
  435. if(!machcap(n)) {
  436. r = l;
  437. l = new(OCONST, Z, Z);
  438. l->vconst = -1;
  439. l->type = types[TINT];
  440. n->op = OXOR;
  441. n->right = r;
  442. n->left = l;
  443. if(tcom(l))
  444. goto bad;
  445. if(tcompat(n, l->type, r->type, tand))
  446. goto bad;
  447. }
  448. arith(n, 1);
  449. break;
  450. case ONOT:
  451. if(tcom(l))
  452. goto bad;
  453. if(isfunct(n))
  454. break;
  455. if(tcompat(n, T, l->type, tnot))
  456. goto bad;
  457. n->type = types[TINT];
  458. break;
  459. case OANDAND:
  460. case OOROR:
  461. o = tcom(l);
  462. if(o | tcom(r))
  463. goto bad;
  464. if(tcompat(n, T, l->type, tnot) |
  465. tcompat(n, T, r->type, tnot))
  466. goto bad;
  467. n->type = types[TINT];
  468. break;
  469. case OCOMMA:
  470. o = tcom(l);
  471. if(o | tcom(r))
  472. goto bad;
  473. n->type = r->type;
  474. break;
  475. case OSIGN: /* extension signof(type) returns a hash */
  476. if(l != Z) {
  477. if(l->op != OSTRING && l->op != OLSTRING)
  478. if(tcomo(l, 0))
  479. goto bad;
  480. if(l->op == OBIT) {
  481. diag(n, "signof bitfield");
  482. goto bad;
  483. }
  484. n->type = l->type;
  485. }
  486. if(n->type == T)
  487. goto bad;
  488. if(n->type->width < 0) {
  489. diag(n, "signof undefined type");
  490. goto bad;
  491. }
  492. n->op = OCONST;
  493. n->left = Z;
  494. n->right = Z;
  495. n->vconst = convvtox(signature(n->type), TULONG);
  496. n->type = types[TULONG];
  497. break;
  498. case OSIZE:
  499. if(l != Z) {
  500. if(l->op != OSTRING && l->op != OLSTRING)
  501. if(tcomo(l, 0))
  502. goto bad;
  503. if(l->op == OBIT) {
  504. diag(n, "sizeof bitfield");
  505. goto bad;
  506. }
  507. n->type = l->type;
  508. }
  509. if(n->type == T)
  510. goto bad;
  511. if(n->type->width <= 0) {
  512. diag(n, "sizeof undefined type");
  513. goto bad;
  514. }
  515. if(n->type->etype == TFUNC) {
  516. diag(n, "sizeof function");
  517. goto bad;
  518. }
  519. n->op = OCONST;
  520. n->left = Z;
  521. n->right = Z;
  522. n->vconst = convvtox(n->type->width, TINT);
  523. n->type = types[TINT];
  524. break;
  525. case OFUNC:
  526. o = tcomo(l, 0);
  527. if(o)
  528. goto bad;
  529. if(l->type->etype == TIND && l->type->link->etype == TFUNC) {
  530. l = new1(OIND, l, Z);
  531. l->type = l->left->type->link;
  532. n->left = l;
  533. }
  534. if(tcompat(n, T, l->type, tfunct))
  535. goto bad;
  536. if(o | tcoma(l, r, l->type->down, 1))
  537. goto bad;
  538. n->type = l->type->link;
  539. if(!debug['B'])
  540. if(l->type->down == T || l->type->down->etype == TOLD) {
  541. nerrors--;
  542. diag(n, "function args not checked: %F", l);
  543. }
  544. dpcheck(n);
  545. break;
  546. case ONAME:
  547. if(n->type == T) {
  548. diag(n, "name not declared: %F", n);
  549. goto bad;
  550. }
  551. if(n->type->etype == TENUM) {
  552. n->op = OCONST;
  553. n->type = n->sym->tenum;
  554. if(!typefd[n->type->etype])
  555. n->vconst = n->sym->vconst;
  556. else
  557. n->fconst = n->sym->fconst;
  558. break;
  559. }
  560. n->addable = 1;
  561. if(n->class == CEXREG) {
  562. n->op = OREGISTER;
  563. n->reg = n->sym->offset;
  564. n->xoffset = 0;
  565. break;
  566. }
  567. break;
  568. case OLSTRING:
  569. if(n->type->link != types[TUSHORT]) {
  570. o = outstring(0, 0);
  571. while(o & 3) {
  572. outlstring(L"", sizeof(ushort));
  573. o = outlstring(0, 0);
  574. }
  575. }
  576. n->op = ONAME;
  577. n->xoffset = outlstring(n->rstring, n->type->width);
  578. n->addable = 1;
  579. break;
  580. case OSTRING:
  581. if(n->type->link != types[TCHAR]) {
  582. o = outstring(0, 0);
  583. while(o & 3) {
  584. outstring("", 1);
  585. o = outstring(0, 0);
  586. }
  587. }
  588. n->op = ONAME;
  589. n->xoffset = outstring(n->cstring, n->type->width);
  590. n->addable = 1;
  591. break;
  592. case OCONST:
  593. break;
  594. case ODOT:
  595. if(tcom(l))
  596. goto bad;
  597. if(tcompat(n, T, l->type, tdot))
  598. goto bad;
  599. if(tcomd(n))
  600. goto bad;
  601. break;
  602. case OADDR:
  603. if(tcomo(l, ADDROP))
  604. goto bad;
  605. if(tlvalue(l))
  606. goto bad;
  607. if(l->type->nbits) {
  608. diag(n, "address of a bit field");
  609. goto bad;
  610. }
  611. if(l->op == OREGISTER) {
  612. diag(n, "address of a register");
  613. goto bad;
  614. }
  615. n->type = typ(TIND, l->type);
  616. n->type->width = types[TIND]->width;
  617. break;
  618. case OIND:
  619. if(tcom(l))
  620. goto bad;
  621. if(tcompat(n, T, l->type, tindir))
  622. goto bad;
  623. n->type = l->type->link;
  624. n->addable = 1;
  625. break;
  626. case OSTRUCT:
  627. if(tcomx(n))
  628. goto bad;
  629. break;
  630. }
  631. t = n->type;
  632. if(t == T)
  633. goto bad;
  634. if(t->width < 0) {
  635. snap(t);
  636. if(t->width < 0) {
  637. if(typesu[t->etype] && t->tag)
  638. diag(n, "structure not fully declared %s", t->tag->name);
  639. else
  640. diag(n, "structure not fully declared");
  641. goto bad;
  642. }
  643. }
  644. if(typeaf[t->etype]) {
  645. if(f & ADDROF)
  646. goto addaddr;
  647. if(f & ADDROP)
  648. warn(n, "address of array/func ignored");
  649. }
  650. return 0;
  651. addaddr:
  652. if(tlvalue(n))
  653. goto bad;
  654. l = new1(OXXX, Z, Z);
  655. *l = *n;
  656. n->op = OADDR;
  657. if(l->type->etype == TARRAY)
  658. l->type = l->type->link;
  659. n->left = l;
  660. n->right = Z;
  661. n->addable = 0;
  662. n->type = typ(TIND, l->type);
  663. n->type->width = types[TIND]->width;
  664. return 0;
  665. bad:
  666. n->type = T;
  667. return 1;
  668. }
  669. int
  670. tcoma(Node *l, Node *n, Type *t, int f)
  671. {
  672. Node *n1;
  673. int o;
  674. if(t != T)
  675. if(t->etype == TOLD || t->etype == TDOT) /* .../old in prototype */
  676. t = T;
  677. if(n == Z) {
  678. if(t != T && !sametype(t, types[TVOID])) {
  679. diag(n, "not enough function arguments: %F", l);
  680. return 1;
  681. }
  682. return 0;
  683. }
  684. if(n->op == OLIST) {
  685. o = tcoma(l, n->left, t, 0);
  686. if(t != T) {
  687. t = t->down;
  688. if(t == T)
  689. t = types[TVOID];
  690. }
  691. return o | tcoma(l, n->right, t, 1);
  692. }
  693. if(f && t != T)
  694. tcoma(l, Z, t->down, 0);
  695. if(tcom(n) || tcompat(n, T, n->type, targ))
  696. return 1;
  697. if(sametype(t, types[TVOID])) {
  698. diag(n, "too many function arguments: %F", l);
  699. return 1;
  700. }
  701. if(t != T) {
  702. typeext(t, n);
  703. if(stcompat(nodproto, t, n->type, tasign)) {
  704. diag(l, "argument prototype mismatch \"%T\" for \"%T\": %F",
  705. n->type, t, l);
  706. return 1;
  707. }
  708. switch(t->etype) {
  709. case TCHAR:
  710. case TSHORT:
  711. t = types[TINT];
  712. break;
  713. case TUCHAR:
  714. case TUSHORT:
  715. t = types[TUINT];
  716. break;
  717. }
  718. } else
  719. switch(n->type->etype)
  720. {
  721. case TCHAR:
  722. case TSHORT:
  723. t = types[TINT];
  724. break;
  725. case TUCHAR:
  726. case TUSHORT:
  727. t = types[TUINT];
  728. break;
  729. case TFLOAT:
  730. t = types[TDOUBLE];
  731. }
  732. if(t != T && !sametype(t, n->type)) {
  733. n1 = new1(OXXX, Z, Z);
  734. *n1 = *n;
  735. n->op = OCAST;
  736. n->left = n1;
  737. n->right = Z;
  738. n->type = t;
  739. n->addable = 0;
  740. }
  741. return 0;
  742. }
  743. int
  744. tcomd(Node *n)
  745. {
  746. Type *t;
  747. long o;
  748. o = 0;
  749. t = dotsearch(n->sym, n->left->type->link, n, &o);
  750. if(t == T) {
  751. diag(n, "not a member of struct/union: %F", n);
  752. return 1;
  753. }
  754. makedot(n, t, o);
  755. return 0;
  756. }
  757. int
  758. tcomx(Node *n)
  759. {
  760. Type *t;
  761. Node *l, *r, **ar, **al;
  762. int e;
  763. e = 0;
  764. if(n->type->etype != TSTRUCT) {
  765. diag(n, "constructor must be a structure");
  766. return 1;
  767. }
  768. l = invert(n->left);
  769. n->left = l;
  770. al = &n->left;
  771. for(t = n->type->link; t != T; t = t->down) {
  772. if(l == Z) {
  773. diag(n, "constructor list too short");
  774. return 1;
  775. }
  776. if(l->op == OLIST) {
  777. r = l->left;
  778. ar = &l->left;
  779. al = &l->right;
  780. l = l->right;
  781. } else {
  782. r = l;
  783. ar = al;
  784. l = Z;
  785. }
  786. if(tcom(r))
  787. e++;
  788. typeext(t, r);
  789. if(tcompat(n, t, r->type, tasign))
  790. e++;
  791. constas(n, t, r->type);
  792. if(!e && !sametype(t, r->type)) {
  793. r = new1(OCAST, r, Z);
  794. r->type = t;
  795. *ar = r;
  796. }
  797. }
  798. if(l != Z) {
  799. diag(n, "constructor list too long");
  800. return 1;
  801. }
  802. return e;
  803. }
  804. int
  805. tlvalue(Node *n)
  806. {
  807. if(!n->addable) {
  808. diag(n, "not an l-value");
  809. return 1;
  810. }
  811. return 0;
  812. }
  813. /*
  814. * general rewrite
  815. * (IND(ADDR x)) ==> x
  816. * (ADDR(IND x)) ==> x
  817. * remove some zero operands
  818. * remove no op casts
  819. * evaluate constants
  820. */
  821. void
  822. ccom(Node *n)
  823. {
  824. Node *l, *r;
  825. int t;
  826. loop:
  827. if(n == Z)
  828. return;
  829. l = n->left;
  830. r = n->right;
  831. switch(n->op) {
  832. case OAS:
  833. case OASXOR:
  834. case OASAND:
  835. case OASOR:
  836. case OASMOD:
  837. case OASLMOD:
  838. case OASLSHR:
  839. case OASASHR:
  840. case OASASHL:
  841. case OASDIV:
  842. case OASLDIV:
  843. case OASMUL:
  844. case OASLMUL:
  845. case OASSUB:
  846. case OASADD:
  847. ccom(l);
  848. ccom(r);
  849. if(n->op == OASLSHR || n->op == OASASHR || n->op == OASASHL)
  850. if(r->op == OCONST) {
  851. t = n->type->width * 8; /* bits per byte */
  852. if(r->vconst >= t || r->vconst < 0)
  853. warn(n, "stupid shift: %lld", r->vconst);
  854. }
  855. break;
  856. case OCAST:
  857. ccom(l);
  858. if(l->op == OCONST) {
  859. evconst(n);
  860. if(n->op == OCONST)
  861. break;
  862. }
  863. if(nocast(l->type, n->type)) {
  864. l->type = n->type;
  865. *n = *l;
  866. }
  867. break;
  868. case OCOND:
  869. ccom(l);
  870. ccom(r);
  871. if(l->op == OCONST)
  872. if(vconst(l) == 0)
  873. *n = *r->right;
  874. else
  875. *n = *r->left;
  876. break;
  877. case OREGISTER:
  878. case OINDREG:
  879. case OCONST:
  880. case ONAME:
  881. break;
  882. case OADDR:
  883. ccom(l);
  884. l->etype = TVOID;
  885. if(l->op == OIND) {
  886. l->left->type = n->type;
  887. *n = *l->left;
  888. break;
  889. }
  890. goto common;
  891. case OIND:
  892. ccom(l);
  893. if(l->op == OADDR) {
  894. l->left->type = n->type;
  895. *n = *l->left;
  896. break;
  897. }
  898. goto common;
  899. case OEQ:
  900. case ONE:
  901. case OLE:
  902. case OGE:
  903. case OLT:
  904. case OGT:
  905. case OLS:
  906. case OHS:
  907. case OLO:
  908. case OHI:
  909. ccom(l);
  910. ccom(r);
  911. relcon(l, r);
  912. relcon(r, l);
  913. goto common;
  914. case OASHR:
  915. case OASHL:
  916. case OLSHR:
  917. ccom(l);
  918. if(vconst(l) == 0 && !side(r)) {
  919. *n = *l;
  920. break;
  921. }
  922. ccom(r);
  923. if(vconst(r) == 0) {
  924. *n = *l;
  925. break;
  926. }
  927. if(r->op == OCONST) {
  928. t = n->type->width * 8; /* bits per byte */
  929. if(r->vconst >= t || r->vconst <= -t)
  930. warn(n, "stupid shift: %lld", r->vconst);
  931. }
  932. goto common;
  933. case OMUL:
  934. case OLMUL:
  935. ccom(l);
  936. t = vconst(l);
  937. if(t == 0 && !side(r)) {
  938. *n = *l;
  939. break;
  940. }
  941. if(t == 1) {
  942. *n = *r;
  943. goto loop;
  944. }
  945. ccom(r);
  946. t = vconst(r);
  947. if(t == 0 && !side(l)) {
  948. *n = *r;
  949. break;
  950. }
  951. if(t == 1) {
  952. *n = *l;
  953. break;
  954. }
  955. goto common;
  956. case ODIV:
  957. case OLDIV:
  958. ccom(l);
  959. if(vconst(l) == 0 && !side(r)) {
  960. *n = *l;
  961. break;
  962. }
  963. ccom(r);
  964. t = vconst(r);
  965. if(t == 0) {
  966. diag(n, "divide check");
  967. *n = *r;
  968. break;
  969. }
  970. if(t == 1) {
  971. *n = *l;
  972. break;
  973. }
  974. goto common;
  975. case OSUB:
  976. ccom(r);
  977. if(r->op == OCONST) {
  978. if(typefd[r->type->etype]) {
  979. n->op = OADD;
  980. r->fconst = -r->fconst;
  981. goto loop;
  982. } else {
  983. n->op = OADD;
  984. r->vconst = -r->vconst;
  985. goto loop;
  986. }
  987. }
  988. ccom(l);
  989. goto common;
  990. case OXOR:
  991. case OOR:
  992. case OADD:
  993. ccom(l);
  994. if(vconst(l) == 0) {
  995. *n = *r;
  996. goto loop;
  997. }
  998. ccom(r);
  999. if(vconst(r) == 0) {
  1000. *n = *l;
  1001. break;
  1002. }
  1003. goto commun;
  1004. case OAND:
  1005. ccom(l);
  1006. ccom(r);
  1007. if(vconst(l) == 0 && !side(r)) {
  1008. *n = *l;
  1009. break;
  1010. }
  1011. if(vconst(r) == 0 && !side(l)) {
  1012. *n = *r;
  1013. break;
  1014. }
  1015. commun:
  1016. /* look for commutative constant */
  1017. if(r->op == OCONST) {
  1018. if(l->op == n->op) {
  1019. if(l->left->op == OCONST) {
  1020. n->right = l->right;
  1021. l->right = r;
  1022. goto loop;
  1023. }
  1024. if(l->right->op == OCONST) {
  1025. n->right = l->left;
  1026. l->left = r;
  1027. goto loop;
  1028. }
  1029. }
  1030. }
  1031. if(l->op == OCONST) {
  1032. if(r->op == n->op) {
  1033. if(r->left->op == OCONST) {
  1034. n->left = r->right;
  1035. r->right = l;
  1036. goto loop;
  1037. }
  1038. if(r->right->op == OCONST) {
  1039. n->left = r->left;
  1040. r->left = l;
  1041. goto loop;
  1042. }
  1043. }
  1044. }
  1045. goto common;
  1046. case OANDAND:
  1047. ccom(l);
  1048. if(vconst(l) == 0) {
  1049. *n = *l;
  1050. break;
  1051. }
  1052. ccom(r);
  1053. goto common;
  1054. case OOROR:
  1055. ccom(l);
  1056. if(l->op == OCONST && l->vconst != 0) {
  1057. *n = *l;
  1058. n->vconst = 1;
  1059. break;
  1060. }
  1061. ccom(r);
  1062. goto common;
  1063. default:
  1064. if(l != Z)
  1065. ccom(l);
  1066. if(r != Z)
  1067. ccom(r);
  1068. common:
  1069. if(l != Z)
  1070. if(l->op != OCONST)
  1071. break;
  1072. if(r != Z)
  1073. if(r->op != OCONST)
  1074. break;
  1075. evconst(n);
  1076. }
  1077. }