pgen.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. goto rloop;
  191. }
  192. complex(l);
  193. if(l->type == T)
  194. goto rloop;
  195. if(l->op == OCONST)
  196. if(typeword[l->type->etype] && l->type->etype != TIND) {
  197. cas();
  198. cases->val = l->vconst;
  199. cases->def = 0;
  200. cases->label = pc;
  201. cases->isv = typev[l->type->etype];
  202. goto rloop;
  203. }
  204. diag(n, "case expression must be integer constant");
  205. goto rloop;
  206. case OSWITCH:
  207. l = n->left;
  208. complex(l);
  209. if(l->type == T)
  210. break;
  211. if(!typeword[l->type->etype] || l->type->etype == TIND) {
  212. diag(n, "switch expression must be integer");
  213. break;
  214. }
  215. gbranch(OGOTO); /* entry */
  216. sp = p;
  217. cn = cases;
  218. cases = C;
  219. cas();
  220. sbc = breakpc;
  221. breakpc = pc;
  222. snbreak = nbreak;
  223. nbreak = 0;
  224. gbranch(OGOTO);
  225. spb = p;
  226. gen(n->right); /* body */
  227. if(canreach){
  228. gbranch(OGOTO);
  229. patch(p, breakpc);
  230. nbreak++;
  231. }
  232. patch(sp, pc);
  233. regalloc(&nod, l, Z);
  234. /* always signed */
  235. if(typev[l->type->etype])
  236. nod.type = types[TVLONG];
  237. else
  238. nod.type = types[TLONG];
  239. cgen(l, &nod);
  240. doswit(&nod);
  241. regfree(&nod);
  242. patch(spb, pc);
  243. cases = cn;
  244. breakpc = sbc;
  245. canreach = nbreak!=0;
  246. if(canreach == 0)
  247. warnreach = !suppress;
  248. nbreak = snbreak;
  249. break;
  250. case OWHILE:
  251. case ODWHILE:
  252. l = n->left;
  253. gbranch(OGOTO); /* entry */
  254. sp = p;
  255. scc = continpc;
  256. continpc = pc;
  257. gbranch(OGOTO);
  258. spc = p;
  259. sbc = breakpc;
  260. breakpc = pc;
  261. snbreak = nbreak;
  262. nbreak = 0;
  263. gbranch(OGOTO);
  264. spb = p;
  265. patch(spc, pc);
  266. if(n->op == OWHILE)
  267. patch(sp, pc);
  268. bcomplex(l, Z); /* test */
  269. patch(p, breakpc);
  270. if(l->op != OCONST || vconst(l) == 0)
  271. nbreak++;
  272. if(n->op == ODWHILE)
  273. patch(sp, pc);
  274. gen(n->right); /* body */
  275. gbranch(OGOTO);
  276. patch(p, continpc);
  277. patch(spb, pc);
  278. continpc = scc;
  279. breakpc = sbc;
  280. canreach = nbreak!=0;
  281. if(canreach == 0)
  282. warnreach = !suppress;
  283. nbreak = snbreak;
  284. break;
  285. case OFOR:
  286. l = n->left;
  287. if(!canreach && l->right->left && warnreach) {
  288. warn(n, "unreachable code FOR");
  289. warnreach = 0;
  290. }
  291. gen(l->right->left); /* init */
  292. gbranch(OGOTO); /* entry */
  293. sp = p;
  294. /*
  295. * if there are no incoming labels in the
  296. * body and the top's not reachable, warn
  297. */
  298. if(!canreach && warnreach && deadheads(n)) {
  299. warn(n, "unreachable code %O", o);
  300. warnreach = 0;
  301. }
  302. scc = continpc;
  303. continpc = pc;
  304. gbranch(OGOTO);
  305. spc = p;
  306. sbc = breakpc;
  307. breakpc = pc;
  308. snbreak = nbreak;
  309. nbreak = 0;
  310. sncontin = ncontin;
  311. ncontin = 0;
  312. gbranch(OGOTO);
  313. spb = p;
  314. patch(spc, pc);
  315. gen(l->right->right); /* inc */
  316. patch(sp, pc);
  317. if(l->left != Z) { /* test */
  318. bcomplex(l->left, Z);
  319. patch(p, breakpc);
  320. if(l->left->op != OCONST || vconst(l->left) == 0)
  321. nbreak++;
  322. }
  323. canreach = 1;
  324. gen(n->right); /* body */
  325. if(canreach){
  326. gbranch(OGOTO);
  327. patch(p, continpc);
  328. ncontin++;
  329. }
  330. if(!ncontin && l->right->right && warnreach) {
  331. warn(l->right->right, "unreachable FOR inc");
  332. warnreach = 0;
  333. }
  334. patch(spb, pc);
  335. continpc = scc;
  336. breakpc = sbc;
  337. canreach = nbreak!=0;
  338. if(canreach == 0)
  339. warnreach = !suppress;
  340. nbreak = snbreak;
  341. ncontin = sncontin;
  342. break;
  343. case OCONTINUE:
  344. if(continpc < 0) {
  345. diag(n, "continue not in a loop");
  346. break;
  347. }
  348. gbranch(OGOTO);
  349. patch(p, continpc);
  350. ncontin++;
  351. canreach = 0;
  352. warnreach = !suppress;
  353. break;
  354. case OBREAK:
  355. if(breakpc < 0) {
  356. diag(n, "break not in a loop");
  357. break;
  358. }
  359. /*
  360. * Don't complain about unreachable break statements.
  361. * There are breaks hidden in yacc's output and some people
  362. * write return; break; in their switch statements out of habit.
  363. * However, don't confuse the analysis by inserting an
  364. * unreachable reference to breakpc either.
  365. */
  366. if(!canreach)
  367. break;
  368. gbranch(OGOTO);
  369. patch(p, breakpc);
  370. nbreak++;
  371. canreach = 0;
  372. warnreach = !suppress;
  373. break;
  374. case OIF:
  375. l = n->left;
  376. if(bcomplex(l, n->right)) {
  377. if(typefd[l->type->etype])
  378. f = !l->fconst;
  379. else
  380. f = !l->vconst;
  381. if(debug['c'])
  382. print("%L const if %s\n", nearln, f ? "false" : "true");
  383. if(f) {
  384. canreach = 1;
  385. supgen(n->right->left);
  386. oldreach = canreach;
  387. canreach = 1;
  388. gen(n->right->right);
  389. /*
  390. * treat constant ifs as regular ifs for
  391. * reachability warnings.
  392. */
  393. if(!canreach && oldreach && debug['w'] < 2)
  394. warnreach = 0;
  395. }
  396. else {
  397. canreach = 1;
  398. gen(n->right->left);
  399. oldreach = canreach;
  400. canreach = 1;
  401. supgen(n->right->right);
  402. /*
  403. * treat constant ifs as regular ifs for
  404. * reachability warnings.
  405. */
  406. if(!oldreach && canreach && debug['w'] < 2)
  407. warnreach = 0;
  408. canreach = oldreach;
  409. }
  410. }
  411. else {
  412. sp = p;
  413. canreach = 1;
  414. if(n->right->left != Z)
  415. gen(n->right->left);
  416. oldreach = canreach;
  417. canreach = 1;
  418. if(n->right->right != Z) {
  419. gbranch(OGOTO);
  420. patch(sp, pc);
  421. sp = p;
  422. gen(n->right->right);
  423. }
  424. patch(sp, pc);
  425. canreach = canreach || oldreach;
  426. if(canreach == 0)
  427. warnreach = !suppress;
  428. }
  429. break;
  430. case OSET:
  431. case OUSED:
  432. usedset(n->left, o);
  433. break;
  434. }
  435. }
  436. void
  437. usedset(Node *n, int o)
  438. {
  439. if(n->op == OLIST) {
  440. usedset(n->left, o);
  441. usedset(n->right, o);
  442. return;
  443. }
  444. complex(n);
  445. switch(n->op) {
  446. case OADDR: /* volatile */
  447. gins(ANOP, n, Z);
  448. break;
  449. case ONAME:
  450. if(o == OSET)
  451. gins(ANOP, Z, n);
  452. else
  453. gins(ANOP, n, Z);
  454. break;
  455. }
  456. }
  457. int
  458. bcomplex(Node *n, Node *c)
  459. {
  460. complex(n);
  461. if(n->type != T)
  462. if(tcompat(n, T, n->type, tnot))
  463. n->type = T;
  464. if(n->type == T) {
  465. gbranch(OGOTO);
  466. return 0;
  467. }
  468. if(c != Z && n->op == OCONST && deadheads(c))
  469. return 1;
  470. bool64(n);
  471. boolgen(n, 1, Z);
  472. return 0;
  473. }