com.c 24 KB

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