com.c 24 KB

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