cgen.c 18 KB

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