com.c 22 KB

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