cgen.c 19 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  1. #include "gc.h"
  2. void
  3. cgen(Node *n, Node *nn)
  4. {
  5. Node *l, *r;
  6. Prog *p1;
  7. Node nod, nod1, nod2, nod3, nod4;
  8. int o, t;
  9. long v, curs;
  10. if(debug['g']) {
  11. prtree(nn, "cgen lhs");
  12. prtree(n, "cgen");
  13. }
  14. if(n == Z || n->type == T)
  15. return;
  16. if(typesuv[n->type->etype]) {
  17. sugen(n, nn, n->type->width);
  18. return;
  19. }
  20. l = n->left;
  21. r = n->right;
  22. o = n->op;
  23. if(n->addable >= INDEXED) {
  24. if(nn == Z) {
  25. switch(o) {
  26. default:
  27. nullwarn(Z, Z);
  28. break;
  29. case OINDEX:
  30. nullwarn(l, r);
  31. break;
  32. }
  33. return;
  34. }
  35. gmove(n, nn);
  36. return;
  37. }
  38. curs = cursafe;
  39. if(n->complex >= FNX)
  40. if(l->complex >= FNX)
  41. if(r != Z && r->complex >= FNX)
  42. switch(o) {
  43. default:
  44. regret(&nod, r);
  45. cgen(r, &nod);
  46. regsalloc(&nod1, r);
  47. gopcode(OAS, &nod, Z, &nod1);
  48. regfree(&nod);
  49. nod = *n;
  50. nod.right = &nod1;
  51. cgen(&nod, nn);
  52. return;
  53. case OFUNC:
  54. case OCOMMA:
  55. case OANDAND:
  56. case OOROR:
  57. case OCOND:
  58. case ODOT:
  59. break;
  60. }
  61. switch(o) {
  62. default:
  63. diag(n, "unknown op in cgen: %O", o);
  64. break;
  65. case OAS:
  66. if(l->op == OBIT)
  67. goto bitas;
  68. if(l->addable >= INDEXED && l->complex < FNX) {
  69. if(nn != Z || r->addable < INDEXED) {
  70. if(r->complex >= FNX && nn == Z)
  71. regret(&nod, r);
  72. else
  73. regalloc(&nod, r, nn);
  74. cgen(r, &nod);
  75. gmove(&nod, l);
  76. if(nn != Z)
  77. gmove(&nod, nn);
  78. regfree(&nod);
  79. } else
  80. gmove(r, l);
  81. break;
  82. }
  83. if(l->complex >= r->complex) {
  84. reglcgen(&nod1, l, Z);
  85. if(r->addable >= INDEXED) {
  86. gmove(r, &nod1);
  87. if(nn != Z)
  88. gmove(r, nn);
  89. regfree(&nod1);
  90. break;
  91. }
  92. regalloc(&nod, r, nn);
  93. cgen(r, &nod);
  94. } else {
  95. regalloc(&nod, r, nn);
  96. cgen(r, &nod);
  97. reglcgen(&nod1, l, Z);
  98. }
  99. gmove(&nod, &nod1);
  100. regfree(&nod);
  101. regfree(&nod1);
  102. break;
  103. bitas:
  104. n = l->left;
  105. regalloc(&nod, r, nn);
  106. if(l->complex >= r->complex) {
  107. reglcgen(&nod1, n, Z);
  108. cgen(r, &nod);
  109. } else {
  110. cgen(r, &nod);
  111. reglcgen(&nod1, n, Z);
  112. }
  113. regalloc(&nod2, n, Z);
  114. gopcode(OAS, &nod1, Z, &nod2);
  115. bitstore(l, &nod, &nod1, &nod2, nn);
  116. break;
  117. case OBIT:
  118. if(nn == Z) {
  119. nullwarn(l, Z);
  120. break;
  121. }
  122. bitload(n, &nod, Z, Z, nn);
  123. gopcode(OAS, &nod, Z, nn);
  124. regfree(&nod);
  125. break;
  126. case ODIV:
  127. case OMOD:
  128. if(nn != Z)
  129. if((t = vlog(r)) >= 0) {
  130. /* signed div/mod by constant power of 2 */
  131. cgen(l, nn);
  132. gopcode(OGE, nodconst(0), nn, Z);
  133. p1 = p;
  134. if(o == ODIV) {
  135. gopcode(OADD, nodconst((1<<t)-1), Z, nn);
  136. patch(p1, pc);
  137. gopcode(OASHR, nodconst(t), Z, nn);
  138. } else {
  139. gopcode(OSUB, nn, nodconst(0), nn);
  140. gopcode(OAND, nodconst((1<<t)-1), Z, nn);
  141. gopcode(OSUB, nn, nodconst(0), nn);
  142. gbranch(OGOTO);
  143. patch(p1, pc);
  144. p1 = p;
  145. gopcode(OAND, nodconst((1<<t)-1), Z, nn);
  146. patch(p1, pc);
  147. }
  148. break;
  149. }
  150. goto muldiv;
  151. case OSUB:
  152. if(nn != Z)
  153. if(l->op == OCONST)
  154. if(!typefd[n->type->etype]) {
  155. cgen(r, nn);
  156. gopcode(o, Z, l, nn);
  157. break;
  158. }
  159. case OADD:
  160. case OAND:
  161. case OOR:
  162. case OXOR:
  163. case OLSHR:
  164. case OASHL:
  165. case OASHR:
  166. /*
  167. * immediate operands
  168. */
  169. if(nn != Z)
  170. if(r->op == OCONST)
  171. if(!typefd[n->type->etype]) {
  172. cgen(l, nn);
  173. if(r->vconst == 0)
  174. if(o != OAND)
  175. break;
  176. if(nn != Z)
  177. gopcode(o, r, Z, nn);
  178. break;
  179. }
  180. case OLMUL:
  181. case OLDIV:
  182. case OLMOD:
  183. case OMUL:
  184. muldiv:
  185. if(nn == Z) {
  186. nullwarn(l, r);
  187. break;
  188. }
  189. if(o == OMUL || o == OLMUL) {
  190. if(mulcon(n, nn))
  191. break;
  192. }
  193. if(l->complex >= r->complex) {
  194. regalloc(&nod, l, nn);
  195. cgen(l, &nod);
  196. regalloc(&nod1, r, Z);
  197. cgen(r, &nod1);
  198. gopcode(o, &nod1, Z, &nod);
  199. } else {
  200. regalloc(&nod, r, nn);
  201. cgen(r, &nod);
  202. regalloc(&nod1, l, Z);
  203. cgen(l, &nod1);
  204. gopcode(o, &nod, &nod1, &nod);
  205. }
  206. gopcode(OAS, &nod, Z, nn);
  207. regfree(&nod);
  208. regfree(&nod1);
  209. break;
  210. case OASLSHR:
  211. case OASASHL:
  212. case OASASHR:
  213. case OASAND:
  214. case OASADD:
  215. case OASSUB:
  216. case OASXOR:
  217. case OASOR:
  218. if(l->op == OBIT)
  219. goto asbitop;
  220. if(r->op == OCONST)
  221. if(!typefd[r->type->etype])
  222. if(!typefd[n->type->etype]) {
  223. if(l->addable < INDEXED)
  224. reglcgen(&nod2, l, Z);
  225. else
  226. nod2 = *l;
  227. regalloc(&nod, r, nn);
  228. gopcode(OAS, &nod2, Z, &nod);
  229. gopcode(o, r, Z, &nod);
  230. gopcode(OAS, &nod, Z, &nod2);
  231. regfree(&nod);
  232. if(l->addable < INDEXED)
  233. regfree(&nod2);
  234. break;
  235. }
  236. case OASLMUL:
  237. case OASLDIV:
  238. case OASLMOD:
  239. case OASMUL:
  240. case OASDIV:
  241. case OASMOD:
  242. if(l->op == OBIT)
  243. goto asbitop;
  244. if(l->complex >= r->complex) {
  245. if(l->addable < INDEXED)
  246. reglcgen(&nod2, l, Z);
  247. else
  248. nod2 = *l;
  249. regalloc(&nod1, r, Z);
  250. cgen(r, &nod1);
  251. } else {
  252. regalloc(&nod1, r, Z);
  253. cgen(r, &nod1);
  254. if(l->addable < INDEXED)
  255. reglcgen(&nod2, l, Z);
  256. else
  257. nod2 = *l;
  258. }
  259. regalloc(&nod, n, nn);
  260. gmove(&nod2, &nod);
  261. gopcode(o, &nod1, Z, &nod);
  262. gmove(&nod, &nod2);
  263. if(nn != Z)
  264. gopcode(OAS, &nod, Z, nn);
  265. regfree(&nod);
  266. regfree(&nod1);
  267. if(l->addable < INDEXED)
  268. regfree(&nod2);
  269. break;
  270. asbitop:
  271. regalloc(&nod4, n, nn);
  272. if(l->complex >= r->complex) {
  273. bitload(l, &nod, &nod1, &nod2, &nod4);
  274. regalloc(&nod3, r, Z);
  275. cgen(r, &nod3);
  276. } else {
  277. regalloc(&nod3, r, Z);
  278. cgen(r, &nod3);
  279. bitload(l, &nod, &nod1, &nod2, &nod4);
  280. }
  281. gmove(&nod, &nod4);
  282. gopcode(o, &nod3, Z, &nod4);
  283. regfree(&nod3);
  284. gmove(&nod4, &nod);
  285. regfree(&nod4);
  286. bitstore(l, &nod, &nod1, &nod2, nn);
  287. break;
  288. case OADDR:
  289. if(nn == Z) {
  290. nullwarn(l, Z);
  291. break;
  292. }
  293. lcgen(l, nn);
  294. break;
  295. case OFUNC:
  296. if(l->complex >= FNX) {
  297. if(l->op != OIND)
  298. diag(n, "bad function call");
  299. regret(&nod, l->left);
  300. cgen(l->left, &nod);
  301. regsalloc(&nod1, l->left);
  302. gopcode(OAS, &nod, Z, &nod1);
  303. regfree(&nod);
  304. nod = *n;
  305. nod.left = &nod2;
  306. nod2 = *l;
  307. nod2.left = &nod1;
  308. nod2.complex = 1;
  309. cgen(&nod, nn);
  310. return;
  311. }
  312. if(REGARG >= 0)
  313. o = reg[REGARG];
  314. gargs(r, &nod, &nod1);
  315. if(l->addable < INDEXED) {
  316. reglcgen(&nod, l, Z);
  317. gopcode(OFUNC, Z, Z, &nod);
  318. regfree(&nod);
  319. } else
  320. gopcode(OFUNC, Z, Z, l);
  321. if(REGARG >= 0)
  322. if(o != reg[REGARG])
  323. reg[REGARG]--;
  324. if(nn != Z) {
  325. regret(&nod, n);
  326. gopcode(OAS, &nod, Z, nn);
  327. regfree(&nod);
  328. }
  329. break;
  330. case OIND:
  331. if(nn == Z) {
  332. nullwarn(l, Z);
  333. break;
  334. }
  335. regialloc(&nod, n, nn);
  336. r = l;
  337. while(r->op == OADD)
  338. r = r->right;
  339. if(sconst(r) && (v = r->vconst+nod.xoffset) > -4096 && v < 4096) {
  340. v = r->vconst;
  341. r->vconst = 0;
  342. cgen(l, &nod);
  343. nod.xoffset += v;
  344. r->vconst = v;
  345. } else
  346. cgen(l, &nod);
  347. regind(&nod, n);
  348. gopcode(OAS, &nod, Z, nn);
  349. regfree(&nod);
  350. break;
  351. case OEQ:
  352. case ONE:
  353. case OLE:
  354. case OLT:
  355. case OGE:
  356. case OGT:
  357. case OLO:
  358. case OLS:
  359. case OHI:
  360. case OHS:
  361. if(nn == Z) {
  362. nullwarn(l, r);
  363. break;
  364. }
  365. boolgen(n, 1, nn);
  366. break;
  367. case OANDAND:
  368. case OOROR:
  369. boolgen(n, 1, nn);
  370. if(nn == Z)
  371. patch(p, pc);
  372. break;
  373. case ONOT:
  374. if(nn == Z) {
  375. nullwarn(l, Z);
  376. break;
  377. }
  378. boolgen(n, 1, nn);
  379. break;
  380. case OCOMMA:
  381. cgen(l, Z);
  382. cgen(r, nn);
  383. break;
  384. case OCAST:
  385. if(nn == Z) {
  386. nullwarn(l, Z);
  387. break;
  388. }
  389. /*
  390. * convert from types l->n->nn
  391. */
  392. if(nocast(l->type, n->type)) {
  393. if(nocast(n->type, nn->type)) {
  394. cgen(l, nn);
  395. break;
  396. }
  397. }
  398. regalloc(&nod, l, nn);
  399. cgen(l, &nod);
  400. regalloc(&nod1, n, &nod);
  401. gopcode(OAS, &nod, Z, &nod1);
  402. gopcode(OAS, &nod1, Z, nn);
  403. regfree(&nod1);
  404. regfree(&nod);
  405. break;
  406. case ODOT:
  407. sugen(l, nodrat, l->type->width);
  408. if(nn != Z) {
  409. warn(n, "non-interruptable temporary");
  410. nod = *nodrat;
  411. if(!r || r->op != OCONST) {
  412. diag(n, "DOT and no offset");
  413. break;
  414. }
  415. nod.xoffset += (long)r->vconst;
  416. nod.type = n->type;
  417. cgen(&nod, nn);
  418. }
  419. break;
  420. case OCOND:
  421. bcgen(l, 1);
  422. p1 = p;
  423. cgen(r->left, nn);
  424. gbranch(OGOTO);
  425. patch(p1, pc);
  426. p1 = p;
  427. cgen(r->right, nn);
  428. patch(p1, pc);
  429. break;
  430. case OPOSTINC:
  431. case OPOSTDEC:
  432. v = 1;
  433. if(l->type->etype == TIND)
  434. v = l->type->link->width;
  435. if(o == OPOSTDEC)
  436. v = -v;
  437. if(l->op == OBIT)
  438. goto bitinc;
  439. if(nn == Z)
  440. goto pre;
  441. if(l->addable < INDEXED)
  442. reglcgen(&nod2, l, Z);
  443. else
  444. nod2 = *l;
  445. regalloc(&nod, l, nn);
  446. gopcode(OAS, &nod2, Z, &nod);
  447. regalloc(&nod1, l, Z);
  448. if(typefd[l->type->etype]) {
  449. regalloc(&nod3, l, Z);
  450. if(v < 0) {
  451. gopcode(OAS, nodfconst(-v), Z, &nod3);
  452. gopcode(OSUB, &nod3, &nod, &nod1);
  453. } else {
  454. gopcode(OAS, nodfconst(v), Z, &nod3);
  455. gopcode(OADD, &nod3, &nod, &nod1);
  456. }
  457. regfree(&nod3);
  458. } else
  459. gopcode(OADD, nodconst(v), &nod, &nod1);
  460. gopcode(OAS, &nod1, Z, &nod2);
  461. regfree(&nod);
  462. regfree(&nod1);
  463. if(l->addable < INDEXED)
  464. regfree(&nod2);
  465. break;
  466. case OPREINC:
  467. case OPREDEC:
  468. v = 1;
  469. if(l->type->etype == TIND)
  470. v = l->type->link->width;
  471. if(o == OPREDEC)
  472. v = -v;
  473. if(l->op == OBIT)
  474. goto bitinc;
  475. pre:
  476. if(l->addable < INDEXED)
  477. reglcgen(&nod2, l, Z);
  478. else
  479. nod2 = *l;
  480. regalloc(&nod, l, nn);
  481. gopcode(OAS, &nod2, Z, &nod);
  482. if(typefd[l->type->etype]) {
  483. regalloc(&nod3, l, Z);
  484. if(v < 0) {
  485. gopcode(OAS, nodfconst(-v), Z, &nod3);
  486. gopcode(OSUB, &nod3, Z, &nod);
  487. } else {
  488. gopcode(OAS, nodfconst(v), Z, &nod3);
  489. gopcode(OADD, &nod3, Z, &nod);
  490. }
  491. regfree(&nod3);
  492. } else
  493. gopcode(OADD, nodconst(v), Z, &nod);
  494. gopcode(OAS, &nod, Z, &nod2);
  495. regfree(&nod);
  496. if(l->addable < INDEXED)
  497. regfree(&nod2);
  498. break;
  499. bitinc:
  500. if(nn != Z && (o == OPOSTINC || o == OPOSTDEC)) {
  501. bitload(l, &nod, &nod1, &nod2, Z);
  502. gopcode(OAS, &nod, Z, nn);
  503. gopcode(OADD, nodconst(v), Z, &nod);
  504. bitstore(l, &nod, &nod1, &nod2, Z);
  505. break;
  506. }
  507. bitload(l, &nod, &nod1, &nod2, nn);
  508. gopcode(OADD, nodconst(v), Z, &nod);
  509. bitstore(l, &nod, &nod1, &nod2, nn);
  510. break;
  511. }
  512. cursafe = curs;
  513. return;
  514. }
  515. void
  516. reglcgen(Node *t, Node *n, Node *nn)
  517. {
  518. Node *r;
  519. long v;
  520. regialloc(t, n, nn);
  521. if(n->op == OIND) {
  522. r = n->left;
  523. while(r->op == OADD)
  524. r = r->right;
  525. if(sconst(r) && (v = r->vconst+t->xoffset) > -4096 && v < 4096) {
  526. v = r->vconst;
  527. r->vconst = 0;
  528. lcgen(n, t);
  529. t->xoffset += v;
  530. r->vconst = v;
  531. regind(t, n);
  532. return;
  533. }
  534. } else if(n->op == OINDREG) {
  535. if((v = n->xoffset) > -4096 && v < 4096) {
  536. n->op = OREGISTER;
  537. cgen(n, t);
  538. t->xoffset += v;
  539. n->op = OINDREG;
  540. regind(t, n);
  541. return;
  542. }
  543. }
  544. lcgen(n, t);
  545. regind(t, n);
  546. }
  547. void
  548. reglpcgen(Node *n, Node *nn, int f)
  549. {
  550. Type *t;
  551. t = nn->type;
  552. nn->type = types[TLONG];
  553. if(f)
  554. reglcgen(n, nn, Z);
  555. else {
  556. regialloc(n, nn, Z);
  557. lcgen(nn, n);
  558. regind(n, nn);
  559. }
  560. nn->type = t;
  561. }
  562. void
  563. lcgen(Node *n, Node *nn)
  564. {
  565. Prog *p1;
  566. Node nod;
  567. if(debug['g']) {
  568. prtree(nn, "lcgen lhs");
  569. prtree(n, "lcgen");
  570. }
  571. if(n == Z || n->type == T)
  572. return;
  573. if(nn == Z) {
  574. nn = &nod;
  575. regalloc(&nod, n, Z);
  576. }
  577. switch(n->op) {
  578. default:
  579. if(n->addable < INDEXED) {
  580. diag(n, "unknown op in lcgen: %O", n->op);
  581. break;
  582. }
  583. nod = *n;
  584. nod.op = OADDR;
  585. nod.left = n;
  586. nod.right = Z;
  587. nod.type = types[TIND];
  588. gopcode(OAS, &nod, Z, nn);
  589. break;
  590. case OCOMMA:
  591. cgen(n->left, n->left);
  592. lcgen(n->right, nn);
  593. break;
  594. case OIND:
  595. cgen(n->left, nn);
  596. break;
  597. case OCOND:
  598. bcgen(n->left, 1);
  599. p1 = p;
  600. lcgen(n->right->left, nn);
  601. gbranch(OGOTO);
  602. patch(p1, pc);
  603. p1 = p;
  604. lcgen(n->right->right, nn);
  605. patch(p1, pc);
  606. break;
  607. }
  608. }
  609. void
  610. bcgen(Node *n, int true)
  611. {
  612. if(n->type == T)
  613. gbranch(OGOTO);
  614. else
  615. boolgen(n, true, Z);
  616. }
  617. void
  618. boolgen(Node *n, int true, Node *nn)
  619. {
  620. int o;
  621. Prog *p1, *p2;
  622. Node *l, *r, nod, nod1;
  623. long curs;
  624. if(debug['g']) {
  625. prtree(nn, "boolgen lhs");
  626. prtree(n, "boolgen");
  627. }
  628. curs = cursafe;
  629. l = n->left;
  630. r = n->right;
  631. switch(n->op) {
  632. default:
  633. regalloc(&nod, n, nn);
  634. cgen(n, &nod);
  635. o = ONE;
  636. if(true)
  637. o = comrel[relindex(o)];
  638. if(typefd[n->type->etype]) {
  639. gopcode(o, nodfconst(0), &nod, Z);
  640. } else
  641. gopcode(o, nodconst(0), &nod, Z);
  642. regfree(&nod);
  643. goto com;
  644. case OCONST:
  645. o = vconst(n);
  646. if(!true)
  647. o = !o;
  648. gbranch(OGOTO);
  649. if(o) {
  650. p1 = p;
  651. gbranch(OGOTO);
  652. patch(p1, pc);
  653. }
  654. goto com;
  655. case OCOMMA:
  656. cgen(l, Z);
  657. boolgen(r, true, nn);
  658. break;
  659. case ONOT:
  660. boolgen(l, !true, nn);
  661. break;
  662. case OCOND:
  663. bcgen(l, 1);
  664. p1 = p;
  665. bcgen(r->left, true);
  666. p2 = p;
  667. gbranch(OGOTO);
  668. patch(p1, pc);
  669. p1 = p;
  670. bcgen(r->right, !true);
  671. patch(p2, pc);
  672. p2 = p;
  673. gbranch(OGOTO);
  674. patch(p1, pc);
  675. patch(p2, pc);
  676. goto com;
  677. case OANDAND:
  678. if(!true)
  679. goto caseor;
  680. caseand:
  681. bcgen(l, true);
  682. p1 = p;
  683. bcgen(r, !true);
  684. p2 = p;
  685. patch(p1, pc);
  686. gbranch(OGOTO);
  687. patch(p2, pc);
  688. goto com;
  689. case OOROR:
  690. if(!true)
  691. goto caseand;
  692. caseor:
  693. bcgen(l, !true);
  694. p1 = p;
  695. bcgen(r, !true);
  696. p2 = p;
  697. gbranch(OGOTO);
  698. patch(p1, pc);
  699. patch(p2, pc);
  700. goto com;
  701. case OEQ:
  702. case ONE:
  703. case OLE:
  704. case OLT:
  705. case OGE:
  706. case OGT:
  707. case OHI:
  708. case OHS:
  709. case OLO:
  710. case OLS:
  711. o = n->op;
  712. if(true)
  713. o = comrel[relindex(o)];
  714. if(l->complex >= FNX && r->complex >= FNX) {
  715. regret(&nod, r);
  716. cgen(r, &nod);
  717. regsalloc(&nod1, r);
  718. gopcode(OAS, &nod, Z, &nod1);
  719. regfree(&nod);
  720. nod = *n;
  721. nod.right = &nod1;
  722. boolgen(&nod, true, nn);
  723. break;
  724. }
  725. if(sconst(l)) {
  726. regalloc(&nod, r, nn);
  727. cgen(r, &nod);
  728. o = invrel[relindex(o)];
  729. gopcode(o, l, &nod, Z);
  730. regfree(&nod);
  731. goto com;
  732. }
  733. if(sconst(r)) {
  734. regalloc(&nod, l, nn);
  735. cgen(l, &nod);
  736. gopcode(o, r, &nod, Z);
  737. regfree(&nod);
  738. goto com;
  739. }
  740. if(l->complex >= r->complex) {
  741. regalloc(&nod1, l, nn);
  742. cgen(l, &nod1);
  743. regalloc(&nod, r, Z);
  744. cgen(r, &nod);
  745. } else {
  746. regalloc(&nod, r, nn);
  747. cgen(r, &nod);
  748. regalloc(&nod1, l, Z);
  749. cgen(l, &nod1);
  750. }
  751. gopcode(o, &nod, &nod1, Z);
  752. regfree(&nod);
  753. regfree(&nod1);
  754. com:
  755. if(nn != Z) {
  756. p1 = p;
  757. gopcode(OAS, nodconst(1), Z, nn);
  758. gbranch(OGOTO);
  759. p2 = p;
  760. patch(p1, pc);
  761. gopcode(OAS, nodconst(0), Z, nn);
  762. patch(p2, pc);
  763. }
  764. break;
  765. }
  766. cursafe = curs;
  767. }
  768. void
  769. sugen(Node *n, Node *nn, long w)
  770. {
  771. Prog *p1;
  772. Node nod0, nod1, nod2, nod3, nod4, *l, *r;
  773. Type *t;
  774. long pc1;
  775. int i, m, c;
  776. if(n == Z || n->type == T)
  777. return;
  778. if(debug['g']) {
  779. prtree(nn, "sugen lhs");
  780. prtree(n, "sugen");
  781. }
  782. if(nn == nodrat)
  783. if(w > nrathole)
  784. nrathole = w;
  785. switch(n->op) {
  786. case OIND:
  787. if(nn == Z) {
  788. nullwarn(n->left, Z);
  789. break;
  790. }
  791. default:
  792. goto copy;
  793. case OCONST:
  794. if(n->type && typev[n->type->etype]) {
  795. if(nn == Z) {
  796. nullwarn(n->left, Z);
  797. break;
  798. }
  799. t = nn->type;
  800. nn->type = types[TLONG];
  801. reglcgen(&nod1, nn, Z);
  802. nn->type = t;
  803. if(align(0, types[TCHAR], Aarg1)) /* isbigendian */
  804. gopcode(OAS, nod32const(n->vconst>>32), Z, &nod1);
  805. else
  806. gopcode(OAS, nod32const(n->vconst), Z, &nod1);
  807. nod1.xoffset += SZ_LONG;
  808. if(align(0, types[TCHAR], Aarg1)) /* isbigendian */
  809. gopcode(OAS, nod32const(n->vconst), Z, &nod1);
  810. else
  811. gopcode(OAS, nod32const(n->vconst>>32), Z, &nod1);
  812. regfree(&nod1);
  813. break;
  814. }
  815. goto copy;
  816. case ODOT:
  817. l = n->left;
  818. sugen(l, nodrat, l->type->width);
  819. if(nn != Z) {
  820. warn(n, "non-interruptable temporary");
  821. nod1 = *nodrat;
  822. r = n->right;
  823. if(!r || r->op != OCONST) {
  824. diag(n, "DOT and no offset");
  825. break;
  826. }
  827. nod1.xoffset += (long)r->vconst;
  828. nod1.type = n->type;
  829. sugen(&nod1, nn, w);
  830. }
  831. break;
  832. case OSTRUCT:
  833. /*
  834. * rewrite so lhs has no fn call
  835. */
  836. if(nn != Z && nn->complex >= FNX) {
  837. nod1 = *n;
  838. nod1.type = typ(TIND, n->type);
  839. regret(&nod2, &nod1);
  840. lcgen(nn, &nod2);
  841. regsalloc(&nod0, &nod1);
  842. gopcode(OAS, &nod2, Z, &nod0);
  843. regfree(&nod2);
  844. nod1 = *n;
  845. nod1.op = OIND;
  846. nod1.left = &nod0;
  847. nod1.right = Z;
  848. nod1.complex = 1;
  849. sugen(n, &nod1, w);
  850. return;
  851. }
  852. r = n->left;
  853. for(t = n->type->link; t != T; t = t->down) {
  854. l = r;
  855. if(r->op == OLIST) {
  856. l = r->left;
  857. r = r->right;
  858. }
  859. if(nn == Z) {
  860. cgen(l, nn);
  861. continue;
  862. }
  863. /*
  864. * hand craft *(&nn + o) = l
  865. */
  866. nod0 = znode;
  867. nod0.op = OAS;
  868. nod0.type = t;
  869. nod0.left = &nod1;
  870. nod0.right = l;
  871. nod1 = znode;
  872. nod1.op = OIND;
  873. nod1.type = t;
  874. nod1.left = &nod2;
  875. nod2 = znode;
  876. nod2.op = OADD;
  877. nod2.type = typ(TIND, t);
  878. nod2.left = &nod3;
  879. nod2.right = &nod4;
  880. nod3 = znode;
  881. nod3.op = OADDR;
  882. nod3.type = nod2.type;
  883. nod3.left = nn;
  884. nod4 = znode;
  885. nod4.op = OCONST;
  886. nod4.type = nod2.type;
  887. nod4.vconst = t->offset;
  888. ccom(&nod0);
  889. acom(&nod0);
  890. xcom(&nod0);
  891. nod0.addable = 0;
  892. cgen(&nod0, Z);
  893. }
  894. break;
  895. case OAS:
  896. if(nn == Z) {
  897. if(n->addable < INDEXED)
  898. sugen(n->right, n->left, w);
  899. break;
  900. }
  901. sugen(n->right, nodrat, w);
  902. warn(n, "non-interruptable temporary");
  903. sugen(nodrat, n->left, w);
  904. sugen(nodrat, nn, w);
  905. break;
  906. case OFUNC:
  907. if(nn == Z) {
  908. sugen(n, nodrat, w);
  909. break;
  910. }
  911. if(nn->op != OIND) {
  912. nn = new1(OADDR, nn, Z);
  913. nn->type = types[TIND];
  914. nn->addable = 0;
  915. } else
  916. nn = nn->left;
  917. n = new(OFUNC, n->left, new(OLIST, nn, n->right));
  918. n->type = types[TVOID];
  919. n->left->type = types[TVOID];
  920. cgen(n, Z);
  921. break;
  922. case OCOND:
  923. bcgen(n->left, 1);
  924. p1 = p;
  925. sugen(n->right->left, nn, w);
  926. gbranch(OGOTO);
  927. patch(p1, pc);
  928. p1 = p;
  929. sugen(n->right->right, nn, w);
  930. patch(p1, pc);
  931. break;
  932. case OCOMMA:
  933. cgen(n->left, Z);
  934. sugen(n->right, nn, w);
  935. break;
  936. }
  937. return;
  938. copy:
  939. if(nn == Z)
  940. return;
  941. if(n->complex >= FNX && nn->complex >= FNX) {
  942. t = nn->type;
  943. nn->type = types[TLONG];
  944. regialloc(&nod1, nn, Z);
  945. lcgen(nn, &nod1);
  946. regsalloc(&nod2, nn);
  947. nn->type = t;
  948. gopcode(OAS, &nod1, Z, &nod2);
  949. regfree(&nod1);
  950. nod2.type = typ(TIND, t);
  951. nod1 = nod2;
  952. nod1.op = OIND;
  953. nod1.left = &nod2;
  954. nod1.right = Z;
  955. nod1.complex = 1;
  956. nod1.type = t;
  957. sugen(n, &nod1, w);
  958. return;
  959. }
  960. w /= SZ_LONG;
  961. if(w <= 2) {
  962. if(n->complex > nn->complex) {
  963. reglpcgen(&nod1, n, 1);
  964. reglpcgen(&nod2, nn, 1);
  965. } else {
  966. reglpcgen(&nod2, nn, 1);
  967. reglpcgen(&nod1, n, 1);
  968. }
  969. regalloc(&nod3, &regnode, Z);
  970. regalloc(&nod4, &regnode, Z);
  971. nod0 = *nodconst((1<<nod3.reg)|(1<<nod4.reg));
  972. if(w == 2 && nod1.xoffset == 0)
  973. gmovm(&nod1, &nod0, 0);
  974. else {
  975. gmove(&nod1, &nod3);
  976. if(w == 2) {
  977. nod1.xoffset += SZ_LONG;
  978. gmove(&nod1, &nod4);
  979. }
  980. }
  981. if(w == 2 && nod2.xoffset == 0)
  982. gmovm(&nod0, &nod2, 0);
  983. else {
  984. gmove(&nod3, &nod2);
  985. if(w == 2) {
  986. nod2.xoffset += SZ_LONG;
  987. gmove(&nod4, &nod2);
  988. }
  989. }
  990. regfree(&nod1);
  991. regfree(&nod2);
  992. regfree(&nod3);
  993. regfree(&nod4);
  994. return;
  995. }
  996. if(n->complex > nn->complex) {
  997. reglpcgen(&nod1, n, 0);
  998. reglpcgen(&nod2, nn, 0);
  999. } else {
  1000. reglpcgen(&nod2, nn, 0);
  1001. reglpcgen(&nod1, n, 0);
  1002. }
  1003. m = 0;
  1004. for(c = 0; c < w && c < 4; c++) {
  1005. i = tmpreg();
  1006. if (i == 0)
  1007. break;
  1008. reg[i]++;
  1009. m |= 1<<i;
  1010. }
  1011. nod4 = *(nodconst(m));
  1012. if(w < 3*c) {
  1013. for (; w>c; w-=c) {
  1014. gmovm(&nod1, &nod4, 1);
  1015. gmovm(&nod4, &nod2, 1);
  1016. }
  1017. goto out;
  1018. }
  1019. regalloc(&nod3, &regnode, Z);
  1020. gopcode(OAS, nodconst(w/c), Z, &nod3);
  1021. w %= c;
  1022. pc1 = pc;
  1023. gmovm(&nod1, &nod4, 1);
  1024. gmovm(&nod4, &nod2, 1);
  1025. gopcode(OSUB, nodconst(1), Z, &nod3);
  1026. gopcode(OEQ, nodconst(0), &nod3, Z);
  1027. p->as = ABGT;
  1028. patch(p, pc1);
  1029. regfree(&nod3);
  1030. out:
  1031. if (w) {
  1032. i = 0;
  1033. while (c>w) {
  1034. while ((m&(1<<i)) == 0)
  1035. i++;
  1036. m &= ~(1<<i);
  1037. reg[i] = 0;
  1038. c--;
  1039. i++;
  1040. }
  1041. nod4.vconst = m;
  1042. gmovm(&nod1, &nod4, 0);
  1043. gmovm(&nod4, &nod2, 0);
  1044. }
  1045. i = 0;
  1046. do {
  1047. while ((m&(1<<i)) == 0)
  1048. i++;
  1049. reg[i] = 0;
  1050. c--;
  1051. i++;
  1052. } while (c>0);
  1053. regfree(&nod1);
  1054. regfree(&nod2);
  1055. }