com.c 22 KB

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