pgen.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. #include "gc.h"
  2. void
  3. codgen(Node *n, Node *nn)
  4. {
  5. Prog *sp;
  6. Node *n1, nod, nod1;
  7. cursafe = 0;
  8. curarg = 0;
  9. maxargsafe = 0;
  10. hasdoubled = 0;
  11. /*
  12. * isolate name
  13. */
  14. for(n1 = nn;; n1 = n1->left) {
  15. if(n1 == Z) {
  16. diag(nn, "cant find function name");
  17. return;
  18. }
  19. if(n1->op == ONAME)
  20. break;
  21. }
  22. nearln = nn->lineno;
  23. gpseudo(ATEXT, n1->sym, nodconst(stkoff));
  24. sp = p;
  25. if(typecmplx[thisfn->link->etype]) {
  26. if(nodret == nil) {
  27. nodret = new(ONAME, Z, Z);
  28. nodret->sym = slookup(".ret");
  29. nodret->class = CPARAM;
  30. nodret->type = types[TIND];
  31. nodret->etype = TIND;
  32. nodret = new(OIND, nodret, Z);
  33. }
  34. n1 = nodret->left;
  35. if(n1->type == T || n1->type->link != thisfn->link) {
  36. n1->type = typ(TIND, thisfn->link);
  37. n1->etype = n1->type->etype;
  38. nodret = new(OIND, n1, Z);
  39. complex(nodret);
  40. }
  41. }
  42. /*
  43. * isolate first argument
  44. */
  45. if(REGARG >= 0) {
  46. if(typecmplx[thisfn->link->etype]) {
  47. nod1 = *nodret->left;
  48. nodreg(&nod, &nod1, REGARG);
  49. gmove(&nod, &nod1);
  50. } else
  51. if(firstarg && typeword[firstargtype->etype]) {
  52. nod1 = znode;
  53. nod1.op = ONAME;
  54. nod1.sym = firstarg;
  55. nod1.type = firstargtype;
  56. nod1.class = CPARAM;
  57. nod1.xoffset = align(0, firstargtype, Aarg1);
  58. nod1.etype = firstargtype->etype;
  59. xcom(&nod1);
  60. nodreg(&nod, &nod1, REGARG);
  61. gmove(&nod, &nod1);
  62. }
  63. }
  64. canreach = 1;
  65. warnreach = 1;
  66. gen(n);
  67. if(canreach && thisfn->link->etype != TVOID)
  68. warn(Z, "no return at end of function: %s", n1->sym->name);
  69. noretval(3);
  70. gbranch(ORETURN);
  71. if(!debug['N'] || debug['R'] || debug['P'])
  72. regopt(sp);
  73. if(thechar=='6' || thechar=='7' || thechar=='9' || hasdoubled) /* [sic] */
  74. maxargsafe = round(maxargsafe, 8);
  75. sp->to.offset += maxargsafe;
  76. }
  77. void
  78. supgen(Node *n)
  79. {
  80. int owarn;
  81. long spc;
  82. Prog *sp;
  83. if(n == Z)
  84. return;
  85. suppress++;
  86. owarn = warnreach;
  87. warnreach = 0;
  88. spc = pc;
  89. sp = lastp;
  90. gen(n);
  91. lastp = sp;
  92. pc = spc;
  93. sp->link = nil;
  94. suppress--;
  95. warnreach = owarn;
  96. }
  97. void
  98. gen(Node *n)
  99. {
  100. Node *l, nod;
  101. Prog *sp, *spc, *spb;
  102. Case *cn;
  103. long sbc, scc;
  104. int snbreak, sncontin;
  105. int f, o, oldreach;
  106. loop:
  107. if(n == Z)
  108. return;
  109. nearln = n->lineno;
  110. o = n->op;
  111. if(debug['G'])
  112. if(o != OLIST)
  113. print("%L %O\n", nearln, o);
  114. if(!canreach) {
  115. switch(o) {
  116. case OLABEL:
  117. case OCASE:
  118. case OLIST:
  119. case OBREAK:
  120. case OFOR:
  121. case OWHILE:
  122. case ODWHILE:
  123. /* all handled specially - see switch body below */
  124. break;
  125. default:
  126. if(warnreach) {
  127. warn(n, "unreachable code %O", o);
  128. warnreach = 0;
  129. }
  130. }
  131. }
  132. switch(o) {
  133. default:
  134. complex(n);
  135. cgen(n, Z);
  136. break;
  137. case OLIST:
  138. gen(n->left);
  139. rloop:
  140. n = n->right;
  141. goto loop;
  142. case ORETURN:
  143. canreach = 0;
  144. warnreach = !suppress;
  145. complex(n);
  146. if(n->type == T)
  147. break;
  148. l = n->left;
  149. if(l == Z) {
  150. noretval(3);
  151. gbranch(ORETURN);
  152. break;
  153. }
  154. if(typecmplx[n->type->etype]) {
  155. nod = znode;
  156. nod.op = OAS;
  157. nod.left = nodret;
  158. nod.right = l;
  159. nod.type = n->type;
  160. nod.complex = l->complex;
  161. cgen(&nod, Z);
  162. noretval(3);
  163. gbranch(ORETURN);
  164. break;
  165. }
  166. regret(&nod, n);
  167. cgen(l, &nod);
  168. regfree(&nod);
  169. if(typefd[n->type->etype])
  170. noretval(1);
  171. else
  172. noretval(2);
  173. gbranch(ORETURN);
  174. break;
  175. case OLABEL:
  176. canreach = 1;
  177. l = n->left;
  178. if(l) {
  179. l->pc = pc;
  180. if(l->label)
  181. patch(l->label, pc);
  182. }
  183. gbranch(OGOTO); /* prevent self reference in reg */
  184. patch(p, pc);
  185. goto rloop;
  186. case OGOTO:
  187. canreach = 0;
  188. warnreach = !suppress;
  189. n = n->left;
  190. if(n == Z)
  191. return;
  192. if(n->complex == 0) {
  193. diag(Z, "label undefined: %s", n->sym->name);
  194. return;
  195. }
  196. if(suppress)
  197. return;
  198. gbranch(OGOTO);
  199. if(n->pc) {
  200. patch(p, n->pc);
  201. return;
  202. }
  203. if(n->label)
  204. patch(n->label, pc-1);
  205. n->label = p;
  206. return;
  207. case OCASE:
  208. canreach = 1;
  209. l = n->left;
  210. if(cases == C)
  211. diag(n, "case/default outside a switch");
  212. if(l == Z) {
  213. cas();
  214. cases->val = 0;
  215. cases->def = 1;
  216. cases->label = pc;
  217. cases->isv = 0;
  218. goto rloop;
  219. }
  220. complex(l);
  221. if(l->type == T)
  222. goto rloop;
  223. if(l->op == OCONST)
  224. if(typeword[l->type->etype] && l->type->etype != TIND) {
  225. cas();
  226. cases->val = l->vconst;
  227. cases->def = 0;
  228. cases->label = pc;
  229. cases->isv = typev[l->type->etype];
  230. goto rloop;
  231. }
  232. diag(n, "case expression must be integer constant");
  233. goto rloop;
  234. case OSWITCH:
  235. l = n->left;
  236. complex(l);
  237. if(l->type == T)
  238. break;
  239. if(!typeword[l->type->etype] || l->type->etype == TIND) {
  240. diag(n, "switch expression must be integer");
  241. break;
  242. }
  243. gbranch(OGOTO); /* entry */
  244. sp = p;
  245. cn = cases;
  246. cases = C;
  247. cas();
  248. sbc = breakpc;
  249. breakpc = pc;
  250. snbreak = nbreak;
  251. nbreak = 0;
  252. gbranch(OGOTO);
  253. spb = p;
  254. gen(n->right); /* body */
  255. if(canreach){
  256. gbranch(OGOTO);
  257. patch(p, breakpc);
  258. nbreak++;
  259. }
  260. patch(sp, pc);
  261. regalloc(&nod, l, Z);
  262. /* always signed */
  263. if(typev[l->type->etype])
  264. nod.type = types[TVLONG];
  265. else
  266. nod.type = types[TLONG];
  267. cgen(l, &nod);
  268. doswit(&nod);
  269. regfree(&nod);
  270. patch(spb, pc);
  271. cases = cn;
  272. breakpc = sbc;
  273. canreach = nbreak!=0;
  274. if(canreach == 0)
  275. warnreach = !suppress;
  276. nbreak = snbreak;
  277. break;
  278. case OWHILE:
  279. case ODWHILE:
  280. l = n->left;
  281. gbranch(OGOTO); /* entry */
  282. sp = p;
  283. scc = continpc;
  284. continpc = pc;
  285. gbranch(OGOTO);
  286. spc = p;
  287. sbc = breakpc;
  288. breakpc = pc;
  289. snbreak = nbreak;
  290. nbreak = 0;
  291. gbranch(OGOTO);
  292. spb = p;
  293. patch(spc, pc);
  294. if(n->op == OWHILE)
  295. patch(sp, pc);
  296. bcomplex(l, Z); /* test */
  297. patch(p, breakpc);
  298. if(l->op != OCONST || vconst(l) == 0)
  299. nbreak++;
  300. if(n->op == ODWHILE)
  301. patch(sp, pc);
  302. gen(n->right); /* body */
  303. gbranch(OGOTO);
  304. patch(p, continpc);
  305. patch(spb, pc);
  306. continpc = scc;
  307. breakpc = sbc;
  308. canreach = nbreak!=0;
  309. if(canreach == 0)
  310. warnreach = !suppress;
  311. nbreak = snbreak;
  312. break;
  313. case OFOR:
  314. l = n->left;
  315. if(!canreach && l->right->left && warnreach) {
  316. warn(n, "unreachable code FOR");
  317. warnreach = 0;
  318. }
  319. gen(l->right->left); /* init */
  320. gbranch(OGOTO); /* entry */
  321. sp = p;
  322. /*
  323. * if there are no incoming labels in the
  324. * body and the top's not reachable, warn
  325. */
  326. if(!canreach && warnreach && deadheads(n)) {
  327. warn(n, "unreachable code %O", o);
  328. warnreach = 0;
  329. }
  330. scc = continpc;
  331. continpc = pc;
  332. gbranch(OGOTO);
  333. spc = p;
  334. sbc = breakpc;
  335. breakpc = pc;
  336. snbreak = nbreak;
  337. nbreak = 0;
  338. sncontin = ncontin;
  339. ncontin = 0;
  340. gbranch(OGOTO);
  341. spb = p;
  342. patch(spc, pc);
  343. gen(l->right->right); /* inc */
  344. patch(sp, pc);
  345. if(l->left != Z) { /* test */
  346. bcomplex(l->left, Z);
  347. patch(p, breakpc);
  348. if(l->left->op != OCONST || vconst(l->left) == 0)
  349. nbreak++;
  350. }
  351. canreach = 1;
  352. gen(n->right); /* body */
  353. if(canreach){
  354. gbranch(OGOTO);
  355. patch(p, continpc);
  356. ncontin++;
  357. }
  358. if(!ncontin && l->right->right && warnreach) {
  359. warn(l->right->right, "unreachable FOR inc");
  360. warnreach = 0;
  361. }
  362. patch(spb, pc);
  363. continpc = scc;
  364. breakpc = sbc;
  365. canreach = nbreak!=0;
  366. if(canreach == 0)
  367. warnreach = !suppress;
  368. nbreak = snbreak;
  369. ncontin = sncontin;
  370. break;
  371. case OCONTINUE:
  372. if(continpc < 0) {
  373. diag(n, "continue not in a loop");
  374. break;
  375. }
  376. gbranch(OGOTO);
  377. patch(p, continpc);
  378. ncontin++;
  379. canreach = 0;
  380. warnreach = !suppress;
  381. break;
  382. case OBREAK:
  383. if(breakpc < 0) {
  384. diag(n, "break not in a loop");
  385. break;
  386. }
  387. /*
  388. * Don't complain about unreachable break statements.
  389. * There are breaks hidden in yacc's output and some people
  390. * write return; break; in their switch statements out of habit.
  391. * However, don't confuse the analysis by inserting an
  392. * unreachable reference to breakpc either.
  393. */
  394. if(!canreach)
  395. break;
  396. gbranch(OGOTO);
  397. patch(p, breakpc);
  398. nbreak++;
  399. canreach = 0;
  400. warnreach = !suppress;
  401. break;
  402. case OIF:
  403. l = n->left;
  404. if(bcomplex(l, n->right)) {
  405. if(typefd[l->type->etype])
  406. f = !l->fconst;
  407. else
  408. f = !l->vconst;
  409. if(debug['c'])
  410. print("%L const if %s\n", nearln, f ? "false" : "true");
  411. if(f) {
  412. canreach = 1;
  413. supgen(n->right->left);
  414. oldreach = canreach;
  415. canreach = 1;
  416. gen(n->right->right);
  417. /*
  418. * treat constant ifs as regular ifs for
  419. * reachability warnings.
  420. */
  421. if(!canreach && oldreach && debug['w'] < 2)
  422. warnreach = 0;
  423. }
  424. else {
  425. canreach = 1;
  426. gen(n->right->left);
  427. oldreach = canreach;
  428. canreach = 1;
  429. supgen(n->right->right);
  430. /*
  431. * treat constant ifs as regular ifs for
  432. * reachability warnings.
  433. */
  434. if(!oldreach && canreach && debug['w'] < 2)
  435. warnreach = 0;
  436. canreach = oldreach;
  437. }
  438. }
  439. else {
  440. sp = p;
  441. canreach = 1;
  442. if(n->right->left != Z)
  443. gen(n->right->left);
  444. oldreach = canreach;
  445. canreach = 1;
  446. if(n->right->right != Z) {
  447. gbranch(OGOTO);
  448. patch(sp, pc);
  449. sp = p;
  450. gen(n->right->right);
  451. }
  452. patch(sp, pc);
  453. canreach = canreach || oldreach;
  454. if(canreach == 0)
  455. warnreach = !suppress;
  456. }
  457. break;
  458. case OSET:
  459. case OUSED:
  460. usedset(n->left, o);
  461. break;
  462. }
  463. }
  464. void
  465. usedset(Node *n, int o)
  466. {
  467. if(n->op == OLIST) {
  468. usedset(n->left, o);
  469. usedset(n->right, o);
  470. return;
  471. }
  472. complex(n);
  473. switch(n->op) {
  474. case OADDR: /* volatile */
  475. gins(ANOP, n, Z);
  476. break;
  477. case ONAME:
  478. if(o == OSET)
  479. gins(ANOP, Z, n);
  480. else
  481. gins(ANOP, n, Z);
  482. break;
  483. }
  484. }
  485. int
  486. bcomplex(Node *n, Node *c)
  487. {
  488. complex(n);
  489. if(n->type != T)
  490. if(tcompat(n, T, n->type, tnot))
  491. n->type = T;
  492. if(n->type == T) {
  493. gbranch(OGOTO);
  494. return 0;
  495. }
  496. if(c != Z && n->op == OCONST && deadheads(c))
  497. return 1;
  498. bool64(n);
  499. boolgen(n, 1, Z);
  500. return 0;
  501. }