cgen.c 19 KB

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