cgen.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  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. if(nn && l->op == ONAME) /* in x=++i, emit USED(i) */
  461. gins(ANOP, l, Z);
  462. regfree(&nod);
  463. if(l->addable < INDEXED)
  464. regfree(&nod2);
  465. break;
  466. bitinc:
  467. if(nn != Z && (o == OPOSTINC || o == OPOSTDEC)) {
  468. bitload(l, &nod, &nod1, &nod2, Z);
  469. gopcode(OAS, &nod, Z, nn);
  470. gopcode(OADD, nodconst(v), Z, &nod);
  471. bitstore(l, &nod, &nod1, &nod2, Z);
  472. break;
  473. }
  474. bitload(l, &nod, &nod1, &nod2, nn);
  475. gopcode(OADD, nodconst(v), Z, &nod);
  476. bitstore(l, &nod, &nod1, &nod2, nn);
  477. break;
  478. }
  479. cursafe = curs;
  480. }
  481. void
  482. reglcgen(Node *t, Node *n, Node *nn)
  483. {
  484. Node *r;
  485. long v;
  486. regialloc(t, n, nn);
  487. if(n->op == OIND) {
  488. r = n->left;
  489. while(r->op == OADD)
  490. r = r->right;
  491. if(sconst(r)) {
  492. v = r->vconst;
  493. r->vconst = 0;
  494. lcgen(n, t);
  495. t->xoffset += v;
  496. r->vconst = v;
  497. regind(t, n);
  498. return;
  499. }
  500. }
  501. lcgen(n, t);
  502. regind(t, n);
  503. }
  504. void
  505. lcgen(Node *n, Node *nn)
  506. {
  507. Prog *p1;
  508. Node nod;
  509. if(debug['g']) {
  510. prtree(nn, "lcgen lhs");
  511. prtree(n, "lcgen");
  512. }
  513. if(n == Z || n->type == T)
  514. return;
  515. if(nn == Z) {
  516. nn = &nod;
  517. regalloc(&nod, n, Z);
  518. }
  519. switch(n->op) {
  520. default:
  521. if(n->addable < INDEXED) {
  522. diag(n, "unknown op in lcgen: %O", n->op);
  523. break;
  524. }
  525. nod = *n;
  526. nod.op = OADDR;
  527. nod.left = n;
  528. nod.right = Z;
  529. nod.type = types[TIND];
  530. gopcode(OAS, &nod, Z, nn);
  531. break;
  532. case OCOMMA:
  533. cgen(n->left, n->left);
  534. lcgen(n->right, nn);
  535. break;
  536. case OIND:
  537. cgen(n->left, nn);
  538. break;
  539. case OCOND:
  540. bcgen(n->left, 1);
  541. p1 = p;
  542. lcgen(n->right->left, nn);
  543. gbranch(OGOTO);
  544. patch(p1, pc);
  545. p1 = p;
  546. lcgen(n->right->right, nn);
  547. patch(p1, pc);
  548. break;
  549. }
  550. }
  551. void
  552. bcgen(Node *n, int true)
  553. {
  554. if(n->type == T)
  555. gbranch(OGOTO);
  556. else
  557. boolgen(n, true, Z);
  558. }
  559. void
  560. boolgen(Node *n, int true, Node *nn)
  561. {
  562. int o;
  563. Prog *p1, *p2;
  564. Node *l, *r, nod, nod1;
  565. long curs;
  566. if(debug['g']) {
  567. prtree(nn, "boolgen lhs");
  568. prtree(n, "boolgen");
  569. }
  570. curs = cursafe;
  571. l = n->left;
  572. r = n->right;
  573. switch(n->op) {
  574. default:
  575. if(n->op == OCONST) {
  576. o = vconst(n);
  577. if(!true)
  578. o = !o;
  579. gbranch(OGOTO);
  580. if(o) {
  581. p1 = p;
  582. gbranch(OGOTO);
  583. patch(p1, pc);
  584. }
  585. goto com;
  586. }
  587. regalloc(&nod, n, nn);
  588. cgen(n, &nod);
  589. o = ONE;
  590. if(true)
  591. o = comrel[relindex(o)];
  592. if(typefd[n->type->etype]) {
  593. nodreg(&nod1, n, NREG+FREGZERO);
  594. gopcode(o, &nod, Z, &nod1);
  595. } else
  596. gopcode(o, &nod, Z, nodconst(0));
  597. regfree(&nod);
  598. goto com;
  599. case OCOMMA:
  600. cgen(l, Z);
  601. boolgen(r, true, nn);
  602. break;
  603. case ONOT:
  604. boolgen(l, !true, nn);
  605. break;
  606. case OCOND:
  607. bcgen(l, 1);
  608. p1 = p;
  609. bcgen(r->left, true);
  610. p2 = p;
  611. gbranch(OGOTO);
  612. patch(p1, pc);
  613. p1 = p;
  614. bcgen(r->right, !true);
  615. patch(p2, pc);
  616. p2 = p;
  617. gbranch(OGOTO);
  618. patch(p1, pc);
  619. patch(p2, pc);
  620. goto com;
  621. case OANDAND:
  622. if(!true)
  623. goto caseor;
  624. caseand:
  625. bcgen(l, true);
  626. p1 = p;
  627. bcgen(r, !true);
  628. p2 = p;
  629. patch(p1, pc);
  630. gbranch(OGOTO);
  631. patch(p2, pc);
  632. goto com;
  633. case OOROR:
  634. if(!true)
  635. goto caseand;
  636. caseor:
  637. bcgen(l, !true);
  638. p1 = p;
  639. bcgen(r, !true);
  640. p2 = p;
  641. gbranch(OGOTO);
  642. patch(p1, pc);
  643. patch(p2, pc);
  644. goto com;
  645. case OEQ:
  646. case ONE:
  647. case OLE:
  648. case OLT:
  649. case OGE:
  650. case OGT:
  651. case OHI:
  652. case OHS:
  653. case OLO:
  654. case OLS:
  655. o = n->op;
  656. if(true)
  657. o = comrel[relindex(o)];
  658. if(l->complex >= FNX && r->complex >= FNX) {
  659. regret(&nod, r);
  660. cgen(r, &nod);
  661. regsalloc(&nod1, r);
  662. gopcode(OAS, &nod, Z, &nod1);
  663. regfree(&nod);
  664. nod = *n;
  665. nod.right = &nod1;
  666. boolgen(&nod, true, nn);
  667. break;
  668. }
  669. if(sconst(r)) {
  670. regalloc(&nod, l, nn);
  671. cgen(l, &nod);
  672. gopcode(o, &nod, Z, r);
  673. regfree(&nod);
  674. goto com;
  675. }
  676. if(l->complex >= r->complex) {
  677. regalloc(&nod1, l, nn);
  678. cgen(l, &nod1);
  679. regalloc(&nod, r, Z);
  680. cgen(r, &nod);
  681. } else {
  682. regalloc(&nod, r, nn);
  683. cgen(r, &nod);
  684. regalloc(&nod1, l, Z);
  685. cgen(l, &nod1);
  686. }
  687. gopcode(o, &nod1, Z, &nod);
  688. regfree(&nod);
  689. regfree(&nod1);
  690. com:
  691. if(nn != Z) {
  692. p1 = p;
  693. gopcode(OAS, nodconst(1L), Z, nn);
  694. gbranch(OGOTO);
  695. p2 = p;
  696. patch(p1, pc);
  697. gopcode(OAS, nodconst(0L), Z, nn);
  698. patch(p2, pc);
  699. }
  700. break;
  701. }
  702. cursafe = curs;
  703. }
  704. void
  705. sugen(Node *n, Node *nn, long w)
  706. {
  707. Prog *p1;
  708. Node nod0, nod1, nod2, nod3, nod4, *l, *r;
  709. Type *t;
  710. long pc1;
  711. int i, m, c;
  712. if(n == Z || n->type == T)
  713. return;
  714. if(debug['g']) {
  715. prtree(nn, "sugen lhs");
  716. prtree(n, "sugen");
  717. }
  718. if(nn == nodrat)
  719. if(w > nrathole)
  720. nrathole = w;
  721. switch(n->op) {
  722. case OIND:
  723. if(nn == Z) {
  724. nullwarn(n->left, Z);
  725. break;
  726. }
  727. default:
  728. goto copy;
  729. case OCONST:
  730. if(n->type && typev[n->type->etype]) {
  731. if(nn == Z) {
  732. nullwarn(n->left, Z);
  733. break;
  734. }
  735. t = nn->type;
  736. nn->type = types[TLONG];
  737. reglcgen(&nod1, nn, Z);
  738. nn->type = t;
  739. if(align(0, types[TCHAR], Aarg1)) /* isbigendian */
  740. gopcode(OAS, nod32const(n->vconst>>32), Z, &nod1);
  741. else
  742. gopcode(OAS, nod32const(n->vconst), Z, &nod1);
  743. nod1.xoffset += SZ_LONG;
  744. if(align(0, types[TCHAR], Aarg1)) /* isbigendian */
  745. gopcode(OAS, nod32const(n->vconst), Z, &nod1);
  746. else
  747. gopcode(OAS, nod32const(n->vconst>>32), Z, &nod1);
  748. regfree(&nod1);
  749. break;
  750. }
  751. goto copy;
  752. case ODOT:
  753. l = n->left;
  754. sugen(l, nodrat, l->type->width);
  755. if(nn != Z) {
  756. warn(n, "non-interruptable temporary");
  757. nod1 = *nodrat;
  758. r = n->right;
  759. if(!r || r->op != OCONST) {
  760. diag(n, "DOT and no offset");
  761. break;
  762. }
  763. nod1.xoffset += (long)r->vconst;
  764. nod1.type = n->type;
  765. sugen(&nod1, nn, w);
  766. }
  767. break;
  768. case OSTRUCT:
  769. /*
  770. * rewrite so lhs has no fn call
  771. */
  772. if(nn != Z && nn->complex >= FNX) {
  773. nod1 = *n;
  774. nod1.type = typ(TIND, n->type);
  775. regret(&nod2, &nod1);
  776. lcgen(nn, &nod2);
  777. regsalloc(&nod0, &nod1);
  778. gopcode(OAS, &nod2, Z, &nod0);
  779. regfree(&nod2);
  780. nod1 = *n;
  781. nod1.op = OIND;
  782. nod1.left = &nod0;
  783. nod1.right = Z;
  784. nod1.complex = 1;
  785. sugen(n, &nod1, w);
  786. return;
  787. }
  788. r = n->left;
  789. for(t = n->type->link; t != T; t = t->down) {
  790. l = r;
  791. if(r->op == OLIST) {
  792. l = r->left;
  793. r = r->right;
  794. }
  795. if(nn == Z) {
  796. cgen(l, nn);
  797. continue;
  798. }
  799. /*
  800. * hand craft *(&nn + o) = l
  801. */
  802. nod0 = znode;
  803. nod0.op = OAS;
  804. nod0.type = t;
  805. nod0.left = &nod1;
  806. nod0.right = l;
  807. nod1 = znode;
  808. nod1.op = OIND;
  809. nod1.type = t;
  810. nod1.left = &nod2;
  811. nod2 = znode;
  812. nod2.op = OADD;
  813. nod2.type = typ(TIND, t);
  814. nod2.left = &nod3;
  815. nod2.right = &nod4;
  816. nod3 = znode;
  817. nod3.op = OADDR;
  818. nod3.type = nod2.type;
  819. nod3.left = nn;
  820. nod4 = znode;
  821. nod4.op = OCONST;
  822. nod4.type = nod2.type;
  823. nod4.vconst = t->offset;
  824. ccom(&nod0);
  825. acom(&nod0);
  826. xcom(&nod0);
  827. nod0.addable = 0;
  828. cgen(&nod0, Z);
  829. }
  830. break;
  831. case OAS:
  832. if(nn == Z) {
  833. if(n->addable < INDEXED)
  834. sugen(n->right, n->left, w);
  835. break;
  836. }
  837. /* BOTCH -- functions can clobber rathole */
  838. sugen(n->right, nodrat, w);
  839. warn(n, "non-interruptable temporary");
  840. sugen(nodrat, n->left, w);
  841. sugen(nodrat, nn, w);
  842. break;
  843. case OFUNC:
  844. if(nn == Z) {
  845. sugen(n, nodrat, w);
  846. break;
  847. }
  848. if(nn->op != OIND) {
  849. nn = new1(OADDR, nn, Z);
  850. nn->type = types[TIND];
  851. nn->addable = 0;
  852. } else
  853. nn = nn->left;
  854. n = new(OFUNC, n->left, new(OLIST, nn, n->right));
  855. n->type = types[TVOID];
  856. n->left->type = types[TVOID];
  857. cgen(n, Z);
  858. break;
  859. case OCOND:
  860. bcgen(n->left, 1);
  861. p1 = p;
  862. sugen(n->right->left, nn, w);
  863. gbranch(OGOTO);
  864. patch(p1, pc);
  865. p1 = p;
  866. sugen(n->right->right, nn, w);
  867. patch(p1, pc);
  868. break;
  869. case OCOMMA:
  870. cgen(n->left, Z);
  871. sugen(n->right, nn, w);
  872. break;
  873. }
  874. return;
  875. copy:
  876. if(nn == Z)
  877. return;
  878. if(n->complex >= FNX && nn->complex >= FNX) {
  879. t = nn->type;
  880. nn->type = types[TLONG];
  881. regialloc(&nod1, nn, Z);
  882. lcgen(nn, &nod1);
  883. regsalloc(&nod2, nn);
  884. nn->type = t;
  885. gopcode(OAS, &nod1, Z, &nod2);
  886. regfree(&nod1);
  887. nod2.type = typ(TIND, t);
  888. nod1 = nod2;
  889. nod1.op = OIND;
  890. nod1.left = &nod2;
  891. nod1.right = Z;
  892. nod1.complex = 1;
  893. nod1.type = t;
  894. sugen(n, &nod1, w);
  895. return;
  896. }
  897. if(n->complex > nn->complex) {
  898. t = n->type;
  899. n->type = types[TLONG];
  900. reglcgen(&nod1, n, Z);
  901. n->type = t;
  902. t = nn->type;
  903. nn->type = types[TLONG];
  904. reglcgen(&nod2, nn, Z);
  905. nn->type = t;
  906. } else {
  907. t = nn->type;
  908. nn->type = types[TLONG];
  909. reglcgen(&nod2, nn, Z);
  910. nn->type = t;
  911. t = n->type;
  912. n->type = types[TLONG];
  913. reglcgen(&nod1, n, Z);
  914. n->type = t;
  915. }
  916. w /= SZ_LONG;
  917. if(w <= 5) {
  918. layout(&nod1, &nod2, w, 0, Z);
  919. goto out;
  920. }
  921. /*
  922. * minimize space for unrolling loop
  923. * 3,4,5 times. (6 or more is never minimum)
  924. * if small structure, try 2 also.
  925. */
  926. c = 0; /* set */
  927. m = 100;
  928. i = 3;
  929. if(w <= 15)
  930. i = 2;
  931. for(; i<=5; i++)
  932. if(i + w%i <= m) {
  933. c = i;
  934. m = c + w%c;
  935. }
  936. regalloc(&nod3, &regnode, Z);
  937. layout(&nod1, &nod2, w%c, w/c, &nod3);
  938. pc1 = pc;
  939. layout(&nod1, &nod2, c, 0, Z);
  940. gopcode(OSUB, nodconst(1L), Z, &nod3);
  941. nod1.op = OREGISTER;
  942. gopcode(OADD, nodconst(c*SZ_LONG), Z, &nod1);
  943. nod2.op = OREGISTER;
  944. gopcode(OADD, nodconst(c*SZ_LONG), Z, &nod2);
  945. gopcode(OGT, &nod3, Z, nodconst(0));
  946. patch(p, pc1);
  947. regfree(&nod3);
  948. out:
  949. regfree(&nod1);
  950. regfree(&nod2);
  951. }
  952. void
  953. layout(Node *f, Node *t, int c, int cv, Node *cn)
  954. {
  955. Node t1, t2;
  956. while(c > 3) {
  957. layout(f, t, 2, 0, Z);
  958. c -= 2;
  959. }
  960. regalloc(&t1, &regnode, Z);
  961. regalloc(&t2, &regnode, Z);
  962. if(c > 0) {
  963. gopcode(OAS, f, Z, &t1);
  964. f->xoffset += SZ_LONG;
  965. }
  966. if(cn != Z)
  967. gopcode(OAS, nodconst(cv), Z, cn);
  968. if(c > 1) {
  969. gopcode(OAS, f, Z, &t2);
  970. f->xoffset += SZ_LONG;
  971. }
  972. if(c > 0) {
  973. gopcode(OAS, &t1, Z, t);
  974. t->xoffset += SZ_LONG;
  975. }
  976. if(c > 2) {
  977. gopcode(OAS, f, Z, &t1);
  978. f->xoffset += SZ_LONG;
  979. }
  980. if(c > 1) {
  981. gopcode(OAS, &t2, Z, t);
  982. t->xoffset += SZ_LONG;
  983. }
  984. if(c > 2) {
  985. gopcode(OAS, &t1, Z, t);
  986. t->xoffset += SZ_LONG;
  987. }
  988. regfree(&t1);
  989. regfree(&t2);
  990. }