pgen.c 8.7 KB

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