cgen.c 19 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. #include "gc.h"
  2. void
  3. cgen(Node *n, Node *nn, int inrel)
  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, 0);
  46. regsalloc(&nod1, r);
  47. gopcode(OAS, &nod, Z, &nod1);
  48. regfree(&nod);
  49. nod = *n;
  50. nod.right = &nod1;
  51. cgen(&nod, nn, 0);
  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, 0);
  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, 0);
  94. } else {
  95. regalloc(&nod, r, nn);
  96. cgen(r, &nod, 0);
  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, 0);
  109. } else {
  110. cgen(r, &nod, 0);
  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, 0);
  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, 0);
  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, 0);
  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, 0);
  196. regalloc(&nod1, r, Z);
  197. cgen(r, &nod1, 0);
  198. gopcode(o, &nod1, Z, &nod);
  199. } else {
  200. regalloc(&nod, r, nn);
  201. cgen(r, &nod, 0);
  202. regalloc(&nod1, l, Z);
  203. cgen(l, &nod1, 0);
  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, 0);
  251. } else {
  252. regalloc(&nod1, r, Z);
  253. cgen(r, &nod1, 0);
  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, 0);
  276. } else {
  277. regalloc(&nod3, r, Z);
  278. cgen(r, &nod3, 0);
  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, 0);
  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, 0);
  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, 0);
  343. nod.xoffset += v;
  344. r->vconst = v;
  345. } else
  346. cgen(l, &nod, 0);
  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, 0);
  382. cgen(r, nn, 0);
  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, 0);
  395. break;
  396. }
  397. }
  398. regalloc(&nod, l, nn);
  399. cgen(l, &nod, 0);
  400. regalloc(&nod1, n, &nod);
  401. if(inrel)
  402. gmover(&nod, &nod1);
  403. else
  404. gopcode(OAS, &nod, Z, &nod1);
  405. gopcode(OAS, &nod1, Z, nn);
  406. regfree(&nod1);
  407. regfree(&nod);
  408. break;
  409. case ODOT:
  410. sugen(l, nodrat, l->type->width);
  411. if(nn != Z) {
  412. warn(n, "non-interruptable temporary");
  413. nod = *nodrat;
  414. if(!r || r->op != OCONST) {
  415. diag(n, "DOT and no offset");
  416. break;
  417. }
  418. nod.xoffset += (long)r->vconst;
  419. nod.type = n->type;
  420. cgen(&nod, nn, 0);
  421. }
  422. break;
  423. case OCOND:
  424. bcgen(l, 1);
  425. p1 = p;
  426. cgen(r->left, nn, 0);
  427. gbranch(OGOTO);
  428. patch(p1, pc);
  429. p1 = p;
  430. cgen(r->right, nn, 0);
  431. patch(p1, pc);
  432. break;
  433. case OPOSTINC:
  434. case OPOSTDEC:
  435. v = 1;
  436. if(l->type->etype == TIND)
  437. v = l->type->link->width;
  438. if(o == OPOSTDEC)
  439. v = -v;
  440. if(l->op == OBIT)
  441. goto bitinc;
  442. if(nn == Z)
  443. goto pre;
  444. if(l->addable < INDEXED)
  445. reglcgen(&nod2, l, Z);
  446. else
  447. nod2 = *l;
  448. regalloc(&nod, l, nn);
  449. gopcode(OAS, &nod2, Z, &nod);
  450. regalloc(&nod1, l, Z);
  451. if(typefd[l->type->etype]) {
  452. regalloc(&nod3, l, Z);
  453. if(v < 0) {
  454. gopcode(OAS, nodfconst(-v), Z, &nod3);
  455. gopcode(OSUB, &nod3, &nod, &nod1);
  456. } else {
  457. gopcode(OAS, nodfconst(v), Z, &nod3);
  458. gopcode(OADD, &nod3, &nod, &nod1);
  459. }
  460. regfree(&nod3);
  461. } else
  462. gopcode(OADD, nodconst(v), &nod, &nod1);
  463. gopcode(OAS, &nod1, Z, &nod2);
  464. regfree(&nod);
  465. regfree(&nod1);
  466. if(l->addable < INDEXED)
  467. regfree(&nod2);
  468. break;
  469. case OPREINC:
  470. case OPREDEC:
  471. v = 1;
  472. if(l->type->etype == TIND)
  473. v = l->type->link->width;
  474. if(o == OPREDEC)
  475. v = -v;
  476. if(l->op == OBIT)
  477. goto bitinc;
  478. pre:
  479. if(l->addable < INDEXED)
  480. reglcgen(&nod2, l, Z);
  481. else
  482. nod2 = *l;
  483. regalloc(&nod, l, nn);
  484. gopcode(OAS, &nod2, Z, &nod);
  485. if(typefd[l->type->etype]) {
  486. regalloc(&nod3, l, Z);
  487. if(v < 0) {
  488. gopcode(OAS, nodfconst(-v), Z, &nod3);
  489. gopcode(OSUB, &nod3, Z, &nod);
  490. } else {
  491. gopcode(OAS, nodfconst(v), Z, &nod3);
  492. gopcode(OADD, &nod3, Z, &nod);
  493. }
  494. regfree(&nod3);
  495. } else
  496. gopcode(OADD, nodconst(v), Z, &nod);
  497. gopcode(OAS, &nod, Z, &nod2);
  498. regfree(&nod);
  499. if(l->addable < INDEXED)
  500. regfree(&nod2);
  501. break;
  502. bitinc:
  503. if(nn != Z && (o == OPOSTINC || o == OPOSTDEC)) {
  504. bitload(l, &nod, &nod1, &nod2, Z);
  505. gopcode(OAS, &nod, Z, nn);
  506. gopcode(OADD, nodconst(v), Z, &nod);
  507. bitstore(l, &nod, &nod1, &nod2, Z);
  508. break;
  509. }
  510. bitload(l, &nod, &nod1, &nod2, nn);
  511. gopcode(OADD, nodconst(v), Z, &nod);
  512. bitstore(l, &nod, &nod1, &nod2, nn);
  513. break;
  514. }
  515. cursafe = curs;
  516. return;
  517. }
  518. void
  519. reglcgen(Node *t, Node *n, Node *nn)
  520. {
  521. Node *r;
  522. long v;
  523. regialloc(t, n, nn);
  524. if(n->op == OIND) {
  525. r = n->left;
  526. while(r->op == OADD)
  527. r = r->right;
  528. if(sconst(r) && (v = r->vconst+t->xoffset) > -4096 && v < 4096) {
  529. v = r->vconst;
  530. r->vconst = 0;
  531. lcgen(n, t);
  532. t->xoffset += v;
  533. r->vconst = v;
  534. regind(t, n);
  535. return;
  536. }
  537. } else if(n->op == OINDREG) {
  538. if((v = n->xoffset) > -4096 && v < 4096) {
  539. n->op = OREGISTER;
  540. cgen(n, t, 0);
  541. t->xoffset += v;
  542. n->op = OINDREG;
  543. regind(t, n);
  544. return;
  545. }
  546. }
  547. lcgen(n, t);
  548. regind(t, n);
  549. }
  550. void
  551. reglpcgen(Node *n, Node *nn, int f)
  552. {
  553. Type *t;
  554. t = nn->type;
  555. nn->type = types[TLONG];
  556. if(f)
  557. reglcgen(n, nn, Z);
  558. else {
  559. regialloc(n, nn, Z);
  560. lcgen(nn, n);
  561. regind(n, nn);
  562. }
  563. nn->type = t;
  564. }
  565. void
  566. lcgen(Node *n, Node *nn)
  567. {
  568. Prog *p1;
  569. Node nod;
  570. if(debug['g']) {
  571. prtree(nn, "lcgen lhs");
  572. prtree(n, "lcgen");
  573. }
  574. if(n == Z || n->type == T)
  575. return;
  576. if(nn == Z) {
  577. nn = &nod;
  578. regalloc(&nod, n, Z);
  579. }
  580. switch(n->op) {
  581. default:
  582. if(n->addable < INDEXED) {
  583. diag(n, "unknown op in lcgen: %O", n->op);
  584. break;
  585. }
  586. nod = *n;
  587. nod.op = OADDR;
  588. nod.left = n;
  589. nod.right = Z;
  590. nod.type = types[TIND];
  591. gopcode(OAS, &nod, Z, nn);
  592. break;
  593. case OCOMMA:
  594. cgen(n->left, n->left, 0);
  595. lcgen(n->right, nn);
  596. break;
  597. case OIND:
  598. cgen(n->left, nn, 0);
  599. break;
  600. case OCOND:
  601. bcgen(n->left, 1);
  602. p1 = p;
  603. lcgen(n->right->left, nn);
  604. gbranch(OGOTO);
  605. patch(p1, pc);
  606. p1 = p;
  607. lcgen(n->right->right, nn);
  608. patch(p1, pc);
  609. break;
  610. }
  611. }
  612. void
  613. bcgen(Node *n, int true)
  614. {
  615. if(n->type == T)
  616. gbranch(OGOTO);
  617. else
  618. boolgen(n, true, Z);
  619. }
  620. void
  621. boolgen(Node *n, int true, Node *nn)
  622. {
  623. int o;
  624. Prog *p1, *p2;
  625. Node *l, *r, nod, nod1;
  626. long curs;
  627. if(debug['g']) {
  628. prtree(nn, "boolgen lhs");
  629. prtree(n, "boolgen");
  630. }
  631. curs = cursafe;
  632. l = n->left;
  633. r = n->right;
  634. switch(n->op) {
  635. default:
  636. regalloc(&nod, n, nn);
  637. cgen(n, &nod, 0);
  638. o = ONE;
  639. if(true)
  640. o = comrel[relindex(o)];
  641. if(typefd[n->type->etype]) {
  642. gopcode(o, nodfconst(0), &nod, Z);
  643. } else
  644. gopcode(o, nodconst(0), &nod, Z);
  645. regfree(&nod);
  646. goto com;
  647. case OCONST:
  648. o = vconst(n);
  649. if(!true)
  650. o = !o;
  651. gbranch(OGOTO);
  652. if(o) {
  653. p1 = p;
  654. gbranch(OGOTO);
  655. patch(p1, pc);
  656. }
  657. goto com;
  658. case OCOMMA:
  659. cgen(l, Z, 0);
  660. boolgen(r, true, nn);
  661. break;
  662. case ONOT:
  663. boolgen(l, !true, nn);
  664. break;
  665. case OCOND:
  666. bcgen(l, 1);
  667. p1 = p;
  668. bcgen(r->left, true);
  669. p2 = p;
  670. gbranch(OGOTO);
  671. patch(p1, pc);
  672. p1 = p;
  673. bcgen(r->right, !true);
  674. patch(p2, pc);
  675. p2 = p;
  676. gbranch(OGOTO);
  677. patch(p1, pc);
  678. patch(p2, pc);
  679. goto com;
  680. case OANDAND:
  681. if(!true)
  682. goto caseor;
  683. caseand:
  684. bcgen(l, true);
  685. p1 = p;
  686. bcgen(r, !true);
  687. p2 = p;
  688. patch(p1, pc);
  689. gbranch(OGOTO);
  690. patch(p2, pc);
  691. goto com;
  692. case OOROR:
  693. if(!true)
  694. goto caseand;
  695. caseor:
  696. bcgen(l, !true);
  697. p1 = p;
  698. bcgen(r, !true);
  699. p2 = p;
  700. gbranch(OGOTO);
  701. patch(p1, pc);
  702. patch(p2, pc);
  703. goto com;
  704. case OEQ:
  705. case ONE:
  706. case OLE:
  707. case OLT:
  708. case OGE:
  709. case OGT:
  710. case OHI:
  711. case OHS:
  712. case OLO:
  713. case OLS:
  714. o = n->op;
  715. if(true)
  716. o = comrel[relindex(o)];
  717. if(l->complex >= FNX && r->complex >= FNX) {
  718. regret(&nod, r);
  719. cgen(r, &nod, 1);
  720. regsalloc(&nod1, r);
  721. gopcode(OAS, &nod, Z, &nod1);
  722. regfree(&nod);
  723. nod = *n;
  724. nod.right = &nod1;
  725. boolgen(&nod, true, nn);
  726. break;
  727. }
  728. if(sconst(l)) {
  729. regalloc(&nod, r, nn);
  730. cgen(r, &nod, 1);
  731. o = invrel[relindex(o)];
  732. gopcode(o, l, &nod, Z);
  733. regfree(&nod);
  734. goto com;
  735. }
  736. if(sconst(r)) {
  737. regalloc(&nod, l, nn);
  738. cgen(l, &nod, 1);
  739. gopcode(o, r, &nod, Z);
  740. regfree(&nod);
  741. goto com;
  742. }
  743. if(l->complex >= r->complex) {
  744. regalloc(&nod1, l, nn);
  745. cgen(l, &nod1, 1);
  746. regalloc(&nod, r, Z);
  747. cgen(r, &nod, 1);
  748. } else {
  749. regalloc(&nod, r, nn);
  750. cgen(r, &nod, 1);
  751. regalloc(&nod1, l, Z);
  752. cgen(l, &nod1, 1);
  753. }
  754. gopcode(o, &nod, &nod1, Z);
  755. regfree(&nod);
  756. regfree(&nod1);
  757. com:
  758. if(nn != Z) {
  759. p1 = p;
  760. gopcode(OAS, nodconst(1), Z, nn);
  761. gbranch(OGOTO);
  762. p2 = p;
  763. patch(p1, pc);
  764. gopcode(OAS, nodconst(0), Z, nn);
  765. patch(p2, pc);
  766. }
  767. break;
  768. }
  769. cursafe = curs;
  770. }
  771. void
  772. sugen(Node *n, Node *nn, long w)
  773. {
  774. Prog *p1;
  775. Node nod0, nod1, nod2, nod3, nod4, *l, *r;
  776. Type *t;
  777. long pc1;
  778. int i, m, c;
  779. if(n == Z || n->type == T)
  780. return;
  781. if(debug['g']) {
  782. prtree(nn, "sugen lhs");
  783. prtree(n, "sugen");
  784. }
  785. if(nn == nodrat)
  786. if(w > nrathole)
  787. nrathole = w;
  788. switch(n->op) {
  789. case OIND:
  790. if(nn == Z) {
  791. nullwarn(n->left, Z);
  792. break;
  793. }
  794. default:
  795. goto copy;
  796. case OCONST:
  797. if(n->type && typev[n->type->etype]) {
  798. if(nn == Z) {
  799. nullwarn(n->left, Z);
  800. break;
  801. }
  802. t = nn->type;
  803. nn->type = types[TLONG];
  804. reglcgen(&nod1, nn, Z);
  805. nn->type = t;
  806. if(align(0, types[TCHAR], Aarg1)) /* isbigendian */
  807. gopcode(OAS, nod32const(n->vconst>>32), Z, &nod1);
  808. else
  809. gopcode(OAS, nod32const(n->vconst), Z, &nod1);
  810. nod1.xoffset += SZ_LONG;
  811. if(align(0, types[TCHAR], Aarg1)) /* isbigendian */
  812. gopcode(OAS, nod32const(n->vconst), Z, &nod1);
  813. else
  814. gopcode(OAS, nod32const(n->vconst>>32), Z, &nod1);
  815. regfree(&nod1);
  816. break;
  817. }
  818. goto copy;
  819. case ODOT:
  820. l = n->left;
  821. sugen(l, nodrat, l->type->width);
  822. if(nn != Z) {
  823. warn(n, "non-interruptable temporary");
  824. nod1 = *nodrat;
  825. r = n->right;
  826. if(!r || r->op != OCONST) {
  827. diag(n, "DOT and no offset");
  828. break;
  829. }
  830. nod1.xoffset += (long)r->vconst;
  831. nod1.type = n->type;
  832. sugen(&nod1, nn, w);
  833. }
  834. break;
  835. case OSTRUCT:
  836. /*
  837. * rewrite so lhs has no fn call
  838. */
  839. if(nn != Z && nn->complex >= FNX) {
  840. nod1 = *n;
  841. nod1.type = typ(TIND, n->type);
  842. regret(&nod2, &nod1);
  843. lcgen(nn, &nod2);
  844. regsalloc(&nod0, &nod1);
  845. gopcode(OAS, &nod2, Z, &nod0);
  846. regfree(&nod2);
  847. nod1 = *n;
  848. nod1.op = OIND;
  849. nod1.left = &nod0;
  850. nod1.right = Z;
  851. nod1.complex = 1;
  852. sugen(n, &nod1, w);
  853. return;
  854. }
  855. r = n->left;
  856. for(t = n->type->link; t != T; t = t->down) {
  857. l = r;
  858. if(r->op == OLIST) {
  859. l = r->left;
  860. r = r->right;
  861. }
  862. if(nn == Z) {
  863. cgen(l, nn, 0);
  864. continue;
  865. }
  866. /*
  867. * hand craft *(&nn + o) = l
  868. */
  869. nod0 = znode;
  870. nod0.op = OAS;
  871. nod0.type = t;
  872. nod0.left = &nod1;
  873. nod0.right = l;
  874. nod1 = znode;
  875. nod1.op = OIND;
  876. nod1.type = t;
  877. nod1.left = &nod2;
  878. nod2 = znode;
  879. nod2.op = OADD;
  880. nod2.type = typ(TIND, t);
  881. nod2.left = &nod3;
  882. nod2.right = &nod4;
  883. nod3 = znode;
  884. nod3.op = OADDR;
  885. nod3.type = nod2.type;
  886. nod3.left = nn;
  887. nod4 = znode;
  888. nod4.op = OCONST;
  889. nod4.type = nod2.type;
  890. nod4.vconst = t->offset;
  891. ccom(&nod0);
  892. acom(&nod0);
  893. xcom(&nod0);
  894. nod0.addable = 0;
  895. cgen(&nod0, Z, 0);
  896. }
  897. break;
  898. case OAS:
  899. if(nn == Z) {
  900. if(n->addable < INDEXED)
  901. sugen(n->right, n->left, w);
  902. break;
  903. }
  904. sugen(n->right, nodrat, w);
  905. warn(n, "non-interruptable temporary");
  906. sugen(nodrat, n->left, w);
  907. sugen(nodrat, nn, w);
  908. break;
  909. case OFUNC:
  910. if(nn == Z) {
  911. sugen(n, nodrat, w);
  912. break;
  913. }
  914. if(nn->op != OIND) {
  915. nn = new1(OADDR, nn, Z);
  916. nn->type = types[TIND];
  917. nn->addable = 0;
  918. } else
  919. nn = nn->left;
  920. n = new(OFUNC, n->left, new(OLIST, nn, n->right));
  921. n->type = types[TVOID];
  922. n->left->type = types[TVOID];
  923. cgen(n, Z, 0);
  924. break;
  925. case OCOND:
  926. bcgen(n->left, 1);
  927. p1 = p;
  928. sugen(n->right->left, nn, w);
  929. gbranch(OGOTO);
  930. patch(p1, pc);
  931. p1 = p;
  932. sugen(n->right->right, nn, w);
  933. patch(p1, pc);
  934. break;
  935. case OCOMMA:
  936. cgen(n->left, Z, 0);
  937. sugen(n->right, nn, w);
  938. break;
  939. }
  940. return;
  941. copy:
  942. if(nn == Z)
  943. return;
  944. if(n->complex >= FNX && nn->complex >= FNX) {
  945. t = nn->type;
  946. nn->type = types[TLONG];
  947. regialloc(&nod1, nn, Z);
  948. lcgen(nn, &nod1);
  949. regsalloc(&nod2, nn);
  950. nn->type = t;
  951. gopcode(OAS, &nod1, Z, &nod2);
  952. regfree(&nod1);
  953. nod2.type = typ(TIND, t);
  954. nod1 = nod2;
  955. nod1.op = OIND;
  956. nod1.left = &nod2;
  957. nod1.right = Z;
  958. nod1.complex = 1;
  959. nod1.type = t;
  960. sugen(n, &nod1, w);
  961. return;
  962. }
  963. w /= SZ_LONG;
  964. if(w <= 2) {
  965. if(n->complex > nn->complex) {
  966. reglpcgen(&nod1, n, 1);
  967. reglpcgen(&nod2, nn, 1);
  968. } else {
  969. reglpcgen(&nod2, nn, 1);
  970. reglpcgen(&nod1, n, 1);
  971. }
  972. regalloc(&nod3, &regnode, Z);
  973. regalloc(&nod4, &regnode, Z);
  974. nod0 = *nodconst((1<<nod3.reg)|(1<<nod4.reg));
  975. if(w == 2 && nod1.xoffset == 0)
  976. gmovm(&nod1, &nod0, 0);
  977. else {
  978. gmove(&nod1, &nod3);
  979. if(w == 2) {
  980. nod1.xoffset += SZ_LONG;
  981. gmove(&nod1, &nod4);
  982. }
  983. }
  984. if(w == 2 && nod2.xoffset == 0)
  985. gmovm(&nod0, &nod2, 0);
  986. else {
  987. gmove(&nod3, &nod2);
  988. if(w == 2) {
  989. nod2.xoffset += SZ_LONG;
  990. gmove(&nod4, &nod2);
  991. }
  992. }
  993. regfree(&nod1);
  994. regfree(&nod2);
  995. regfree(&nod3);
  996. regfree(&nod4);
  997. return;
  998. }
  999. if(n->complex > nn->complex) {
  1000. reglpcgen(&nod1, n, 0);
  1001. reglpcgen(&nod2, nn, 0);
  1002. } else {
  1003. reglpcgen(&nod2, nn, 0);
  1004. reglpcgen(&nod1, n, 0);
  1005. }
  1006. m = 0;
  1007. for(c = 0; c < w && c < 4; c++) {
  1008. i = tmpreg();
  1009. if (i == 0)
  1010. break;
  1011. reg[i]++;
  1012. m |= 1<<i;
  1013. }
  1014. nod4 = *(nodconst(m));
  1015. if(w < 3*c) {
  1016. for (; w>c; w-=c) {
  1017. gmovm(&nod1, &nod4, 1);
  1018. gmovm(&nod4, &nod2, 1);
  1019. }
  1020. goto out;
  1021. }
  1022. regalloc(&nod3, &regnode, Z);
  1023. gopcode(OAS, nodconst(w/c), Z, &nod3);
  1024. w %= c;
  1025. pc1 = pc;
  1026. gmovm(&nod1, &nod4, 1);
  1027. gmovm(&nod4, &nod2, 1);
  1028. gopcode(OSUB, nodconst(1), Z, &nod3);
  1029. gopcode(OEQ, nodconst(0), &nod3, Z);
  1030. p->as = ABGT;
  1031. patch(p, pc1);
  1032. regfree(&nod3);
  1033. out:
  1034. if (w) {
  1035. i = 0;
  1036. while (c>w) {
  1037. while ((m&(1<<i)) == 0)
  1038. i++;
  1039. m &= ~(1<<i);
  1040. reg[i] = 0;
  1041. c--;
  1042. i++;
  1043. }
  1044. nod4.vconst = m;
  1045. gmovm(&nod1, &nod4, 0);
  1046. gmovm(&nod4, &nod2, 0);
  1047. }
  1048. i = 0;
  1049. do {
  1050. while ((m&(1<<i)) == 0)
  1051. i++;
  1052. reg[i] = 0;
  1053. c--;
  1054. i++;
  1055. } while (c>0);
  1056. regfree(&nod1);
  1057. regfree(&nod2);
  1058. }