cgen.c 18 KB

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