cgen.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  1. #include "gc.h"
  2. static void cmpv(Node*, int, Node*);
  3. static void testv(Node*, int);
  4. static void cgen64(Node*, Node*);
  5. static int isvconstable(int, vlong);
  6. void
  7. cgen(Node *n, Node *nn)
  8. {
  9. Node *l, *r;
  10. Prog *p1;
  11. Node nod, nod1, nod2, nod3, nod4;
  12. int o;
  13. long v, curs;
  14. if(debug['g']) {
  15. prtree(nn, "cgen lhs");
  16. prtree(n, "cgen");
  17. }
  18. if(n == Z || n->type == T)
  19. return;
  20. if(typesu[n->type->etype]) {
  21. sugen(n, nn, n->type->width);
  22. return;
  23. }
  24. if(typev[n->type->etype]) {
  25. switch(n->op) {
  26. case OCONST:
  27. case OFUNC:
  28. cgen64(n, nn);
  29. return;
  30. }
  31. }
  32. l = n->left;
  33. r = n->right;
  34. o = n->op;
  35. if(n->addable >= INDEXED) {
  36. if(nn == Z) {
  37. switch(o) {
  38. default:
  39. nullwarn(Z, Z);
  40. break;
  41. case OINDEX:
  42. nullwarn(l, r);
  43. break;
  44. }
  45. return;
  46. }
  47. gmove(n, nn);
  48. return;
  49. }
  50. curs = cursafe;
  51. if(n->complex >= FNX)
  52. if(l->complex >= FNX)
  53. if(r != Z && r->complex >= FNX)
  54. switch(o) {
  55. default:
  56. if(!typev[r->type->etype]) {
  57. regret(&nod, r);
  58. cgen(r, &nod);
  59. regsalloc(&nod1, r);
  60. gmove(&nod, &nod1);
  61. regfree(&nod);
  62. } else {
  63. regsalloc(&nod1, r);
  64. cgen(r, &nod1);
  65. }
  66. nod = *n;
  67. nod.right = &nod1;
  68. cgen(&nod, nn);
  69. return;
  70. case OFUNC:
  71. case OCOMMA:
  72. case OANDAND:
  73. case OOROR:
  74. case OCOND:
  75. case ODOT:
  76. break;
  77. }
  78. switch(o) {
  79. default:
  80. diag(n, "unknown op in cgen: %O", o);
  81. break;
  82. case ONEG:
  83. case OCOM:
  84. if(nn == Z) {
  85. nullwarn(l, Z);
  86. break;
  87. }
  88. regalloc(&nod, l, nn);
  89. cgen(l, &nod);
  90. gopcode(o, &nod, Z, &nod);
  91. gmove(&nod, nn);
  92. regfree(&nod);
  93. break;
  94. case OAS:
  95. if(l->op == OBIT)
  96. goto bitas;
  97. if(l->addable >= INDEXED) {
  98. if(nn != Z || r->addable < INDEXED) {
  99. regalloc(&nod, r, nn);
  100. cgen(r, &nod);
  101. gmove(&nod, l);
  102. regfree(&nod);
  103. } else
  104. gmove(r, l);
  105. break;
  106. }
  107. if(l->complex >= r->complex) {
  108. reglcgen(&nod1, l, Z);
  109. if(r->addable >= INDEXED) {
  110. gmove(r, &nod1);
  111. if(nn != Z)
  112. gmove(r, nn);
  113. regfree(&nod1);
  114. break;
  115. }
  116. regalloc(&nod, r, nn);
  117. cgen(r, &nod);
  118. } else {
  119. regalloc(&nod, r, nn);
  120. cgen(r, &nod);
  121. reglcgen(&nod1, l, Z);
  122. }
  123. gmove(&nod, &nod1);
  124. regfree(&nod);
  125. regfree(&nod1);
  126. break;
  127. bitas:
  128. n = l->left;
  129. regalloc(&nod, r, nn);
  130. if(l->complex >= r->complex) {
  131. reglcgen(&nod1, n, Z);
  132. cgen(r, &nod);
  133. } else {
  134. cgen(r, &nod);
  135. reglcgen(&nod1, n, Z);
  136. }
  137. regalloc(&nod2, n, Z);
  138. gopcode(OAS, &nod1, Z, &nod2);
  139. bitstore(l, &nod, &nod1, &nod2, nn);
  140. break;
  141. case OBIT:
  142. if(nn == Z) {
  143. nullwarn(l, Z);
  144. break;
  145. }
  146. bitload(n, &nod, Z, Z, nn);
  147. gopcode(OAS, &nod, Z, nn);
  148. regfree(&nod);
  149. break;
  150. case OXOR:
  151. if(nn != Z)
  152. if(r->op == OCONST && r->vconst == -1){
  153. regalloc(&nod, l, nn);
  154. cgen(l, &nod);
  155. gopcode(OCOM, &nod, Z, &nod);
  156. gmove(&nod, nn);
  157. regfree(&nod);
  158. break;
  159. }
  160. case OADD:
  161. case OSUB:
  162. case OAND:
  163. case OOR:
  164. case OLSHR:
  165. case OASHL:
  166. case OASHR:
  167. /*
  168. * immediate operands
  169. */
  170. if(nn != Z && r->op == OCONST && !typefd[n->type->etype] &&
  171. (!typev[n->type->etype] || isvconstable(o, r->vconst))) {
  172. regalloc(&nod, l, nn);
  173. cgen(l, &nod);
  174. if(o == OAND || r->vconst != 0)
  175. gopcode(o, r, Z, &nod);
  176. gmove(&nod, nn);
  177. regfree(&nod);
  178. break;
  179. }
  180. case OMUL:
  181. case OLMUL:
  182. case OLDIV:
  183. case OLMOD:
  184. case ODIV:
  185. case OMOD:
  186. if(nn == Z) {
  187. nullwarn(l, r);
  188. break;
  189. }
  190. if((o == OMUL || o == OLMUL) && !typev[n->type->etype]) {
  191. if(mulcon(n, nn))
  192. break;
  193. if(debug['M'])
  194. print("%L multiply\n", n->lineno);
  195. }
  196. if(l->complex >= r->complex) {
  197. regalloc(&nod, l, nn);
  198. cgen(l, &nod);
  199. if(o != OMUL || typev[n->type->etype] || !sconst(r)) {
  200. regalloc(&nod1, r, Z);
  201. cgen(r, &nod1);
  202. gopcode(o, &nod1, Z, &nod);
  203. regfree(&nod1);
  204. } else
  205. gopcode(o, r, Z, &nod);
  206. } else {
  207. regalloc(&nod1, r, nn);
  208. cgen(r, &nod1);
  209. regalloc(&nod, l, Z);
  210. cgen(l, &nod);
  211. gopcode(o, &nod1, Z, &nod);
  212. regfree(&nod1);
  213. }
  214. gopcode(OAS, &nod, Z, nn);
  215. regfree(&nod);
  216. break;
  217. case OASLSHR:
  218. case OASASHL:
  219. case OASASHR:
  220. case OASAND:
  221. case OASADD:
  222. case OASSUB:
  223. case OASXOR:
  224. case OASOR:
  225. if(l->op == OBIT)
  226. goto asbitop;
  227. if(r->op == OCONST && !typefd[r->type->etype] && !typefd[n->type->etype] &&
  228. (!typev[n->type->etype] || isvconstable(o, r->vconst))) {
  229. if(l->addable < INDEXED)
  230. reglcgen(&nod2, l, Z);
  231. else
  232. nod2 = *l;
  233. regalloc(&nod, l, nn);
  234. gopcode(OAS, &nod2, Z, &nod);
  235. gopcode(o, r, Z, &nod);
  236. gopcode(OAS, &nod, Z, &nod2);
  237. regfree(&nod);
  238. if(l->addable < INDEXED)
  239. regfree(&nod2);
  240. break;
  241. }
  242. case OASLMUL:
  243. case OASLDIV:
  244. case OASLMOD:
  245. case OASMUL:
  246. case OASDIV:
  247. case OASMOD:
  248. if(l->op == OBIT)
  249. goto asbitop;
  250. if(l->complex >= r->complex) {
  251. if(l->addable < INDEXED)
  252. reglcgen(&nod2, l, Z);
  253. else
  254. nod2 = *l;
  255. regalloc(&nod, r, Z);
  256. cgen(r, &nod);
  257. } else {
  258. regalloc(&nod, r, Z);
  259. cgen(r, &nod);
  260. if(l->addable < INDEXED)
  261. reglcgen(&nod2, l, Z);
  262. else
  263. nod2 = *l;
  264. }
  265. regalloc(&nod1, n, nn);
  266. gopcode(OAS, &nod2, Z, &nod1);
  267. gopcode(o, &nod, Z, &nod1);
  268. gopcode(OAS, &nod1, Z, &nod2);
  269. if(nn != Z)
  270. gopcode(OAS, &nod1, Z, nn);
  271. regfree(&nod);
  272. regfree(&nod1);
  273. if(l->addable < INDEXED)
  274. regfree(&nod2);
  275. break;
  276. asbitop:
  277. regalloc(&nod4, n, nn);
  278. regalloc(&nod3, r, Z);
  279. if(l->complex >= r->complex) {
  280. bitload(l, &nod, &nod1, &nod2, &nod4);
  281. cgen(r, &nod3);
  282. } else {
  283. cgen(r, &nod3);
  284. bitload(l, &nod, &nod1, &nod2, &nod4);
  285. }
  286. gmove(&nod, &nod4);
  287. gopcode(n->op, &nod3, Z, &nod4);
  288. regfree(&nod3);
  289. gmove(&nod4, &nod);
  290. regfree(&nod4);
  291. bitstore(l, &nod, &nod1, &nod2, nn);
  292. break;
  293. case OADDR:
  294. if(nn == Z) {
  295. nullwarn(l, Z);
  296. break;
  297. }
  298. lcgen(l, nn);
  299. break;
  300. case OFUNC:
  301. if(l->complex >= FNX) {
  302. if(l->op != OIND)
  303. diag(n, "bad function call");
  304. regret(&nod, l->left);
  305. cgen(l->left, &nod);
  306. regsalloc(&nod1, l->left);
  307. gopcode(OAS, &nod, Z, &nod1);
  308. regfree(&nod);
  309. nod = *n;
  310. nod.left = &nod2;
  311. nod2 = *l;
  312. nod2.left = &nod1;
  313. nod2.complex = 1;
  314. cgen(&nod, nn);
  315. return;
  316. }
  317. o = reg[REGARG];
  318. gargs(r, &nod, &nod1);
  319. if(l->addable < INDEXED) {
  320. reglcgen(&nod, l, Z);
  321. gopcode(OFUNC, Z, Z, &nod);
  322. regfree(&nod);
  323. } else
  324. gopcode(OFUNC, Z, Z, l);
  325. if(REGARG)
  326. if(o != reg[REGARG])
  327. reg[REGARG]--;
  328. if(nn != Z) {
  329. regret(&nod, n);
  330. gopcode(OAS, &nod, Z, nn);
  331. regfree(&nod);
  332. }
  333. break;
  334. case OIND:
  335. if(nn == Z) {
  336. cgen(l, nn);
  337. break;
  338. }
  339. regialloc(&nod, n, nn);
  340. r = l;
  341. while(r->op == OADD)
  342. r = r->right;
  343. if(sconst(r)) {
  344. v = r->vconst;
  345. r->vconst = 0;
  346. cgen(l, &nod);
  347. nod.xoffset += v;
  348. r->vconst = v;
  349. } else
  350. cgen(l, &nod);
  351. regind(&nod, n);
  352. gmove(&nod, nn);
  353. regfree(&nod);
  354. break;
  355. case OEQ:
  356. case ONE:
  357. case OLE:
  358. case OLT:
  359. case OGE:
  360. case OGT:
  361. case OLO:
  362. case OLS:
  363. case OHI:
  364. case OHS:
  365. if(nn == Z) {
  366. nullwarn(l, r);
  367. break;
  368. }
  369. boolgen(n, 1, nn);
  370. break;
  371. case OANDAND:
  372. case OOROR:
  373. boolgen(n, 1, nn);
  374. if(nn == Z)
  375. patch(p, pc);
  376. break;
  377. case ONOT:
  378. if(nn == Z) {
  379. nullwarn(l, Z);
  380. break;
  381. }
  382. boolgen(n, 1, nn);
  383. break;
  384. case OCOMMA:
  385. cgen(l, Z);
  386. cgen(r, nn);
  387. break;
  388. case OCAST:
  389. if(nn == Z) {
  390. nullwarn(l, Z);
  391. break;
  392. }
  393. /*
  394. * convert from types l->n->nn
  395. */
  396. if(nocast(l->type, n->type) && nocast(n->type, nn->type)) {
  397. /* both null, gen l->nn */
  398. cgen(l, nn);
  399. break;
  400. }
  401. if(typev[l->type->etype] || typev[n->type->etype]) {
  402. cgen64(n, nn);
  403. break;
  404. }
  405. regalloc(&nod, l, nn);
  406. cgen(l, &nod);
  407. regalloc(&nod1, n, &nod);
  408. gmove(&nod, &nod1);
  409. gmove(&nod1, nn);
  410. regfree(&nod1);
  411. regfree(&nod);
  412. break;
  413. case ODOT:
  414. sugen(l, nodrat, l->type->width);
  415. if(nn != Z) {
  416. warn(n, "non-interruptable temporary");
  417. nod = *nodrat;
  418. if(!r || r->op != OCONST) {
  419. diag(n, "DOT and no offset");
  420. break;
  421. }
  422. nod.xoffset += (long)r->vconst;
  423. nod.type = n->type;
  424. cgen(&nod, nn);
  425. }
  426. break;
  427. case OCOND:
  428. bcgen(l, 1);
  429. p1 = p;
  430. cgen(r->left, nn);
  431. gbranch(OGOTO);
  432. patch(p1, pc);
  433. p1 = p;
  434. cgen(r->right, nn);
  435. patch(p1, pc);
  436. break;
  437. case OPOSTINC:
  438. case OPOSTDEC:
  439. v = 1;
  440. if(l->type->etype == TIND)
  441. v = l->type->link->width;
  442. if(o == OPOSTDEC)
  443. v = -v;
  444. if(l->op == OBIT)
  445. goto bitinc;
  446. if(nn == Z)
  447. goto pre;
  448. if(l->addable < INDEXED)
  449. reglcgen(&nod2, l, Z);
  450. else
  451. nod2 = *l;
  452. regalloc(&nod, l, nn);
  453. gopcode(OAS, &nod2, Z, &nod);
  454. regalloc(&nod1, l, Z);
  455. if(typefd[l->type->etype]) {
  456. regalloc(&nod3, l, Z);
  457. if(v < 0) {
  458. gopcode(OAS, nodfconst(-v), Z, &nod3);
  459. gopcode(OSUB, &nod3, &nod, &nod1);
  460. } else {
  461. gopcode(OAS, nodfconst(v), Z, &nod3);
  462. gopcode(OADD, &nod3, &nod, &nod1);
  463. }
  464. regfree(&nod3);
  465. } else
  466. gopcode(OADD, nodconst(v), &nod, &nod1);
  467. gopcode(OAS, &nod1, Z, &nod2);
  468. regfree(&nod);
  469. regfree(&nod1);
  470. if(l->addable < INDEXED)
  471. regfree(&nod2);
  472. break;
  473. case OPREINC:
  474. case OPREDEC:
  475. v = 1;
  476. if(l->type->etype == TIND)
  477. v = l->type->link->width;
  478. if(o == OPREDEC)
  479. v = -v;
  480. if(l->op == OBIT)
  481. goto bitinc;
  482. pre:
  483. if(l->addable < INDEXED)
  484. reglcgen(&nod2, l, Z);
  485. else
  486. nod2 = *l;
  487. regalloc(&nod, l, nn);
  488. gopcode(OAS, &nod2, Z, &nod);
  489. if(typefd[l->type->etype]) {
  490. regalloc(&nod3, l, Z);
  491. if(v < 0) {
  492. gopcode(OAS, nodfconst(-v), Z, &nod3);
  493. gopcode(OSUB, &nod3, Z, &nod);
  494. } else {
  495. gopcode(OAS, nodfconst(v), Z, &nod3);
  496. gopcode(OADD, &nod3, Z, &nod);
  497. }
  498. regfree(&nod3);
  499. } else
  500. gopcode(OADD, nodconst(v), Z, &nod);
  501. gopcode(OAS, &nod, Z, &nod2);
  502. if(nn && l->op == ONAME) /* in x=++i, emit USED(i) */
  503. gins(ANOP, l, Z);
  504. regfree(&nod);
  505. if(l->addable < INDEXED)
  506. regfree(&nod2);
  507. break;
  508. bitinc:
  509. if(nn != Z && (o == OPOSTINC || o == OPOSTDEC)) {
  510. bitload(l, &nod, &nod1, &nod2, Z);
  511. gopcode(OAS, &nod, Z, nn);
  512. gopcode(OADD, nodconst(v), Z, &nod);
  513. bitstore(l, &nod, &nod1, &nod2, Z);
  514. break;
  515. }
  516. bitload(l, &nod, &nod1, &nod2, nn);
  517. gopcode(OADD, nodconst(v), Z, &nod);
  518. bitstore(l, &nod, &nod1, &nod2, nn);
  519. break;
  520. }
  521. cursafe = curs;
  522. }
  523. void
  524. reglcgen(Node *t, Node *n, Node *nn)
  525. {
  526. Node *r;
  527. long v;
  528. regialloc(t, n, nn);
  529. if(n->op == OIND) {
  530. r = n->left;
  531. while(r->op == OADD)
  532. r = r->right;
  533. if(sconst(r)) {
  534. v = r->vconst;
  535. r->vconst = 0;
  536. lcgen(n, t);
  537. t->xoffset += v;
  538. r->vconst = v;
  539. regind(t, n);
  540. return;
  541. }
  542. }
  543. lcgen(n, t);
  544. regind(t, n);
  545. }
  546. void
  547. lcgen(Node *n, Node *nn)
  548. {
  549. Prog *p1;
  550. Node nod;
  551. if(debug['g']) {
  552. prtree(nn, "lcgen lhs");
  553. prtree(n, "lcgen");
  554. }
  555. if(n == Z || n->type == T)
  556. return;
  557. if(nn == Z) {
  558. nn = &nod;
  559. regalloc(&nod, n, Z);
  560. }
  561. switch(n->op) {
  562. default:
  563. if(n->addable < INDEXED) {
  564. diag(n, "unknown op in lcgen: %O", n->op);
  565. break;
  566. }
  567. nod = *n;
  568. nod.op = OADDR;
  569. nod.left = n;
  570. nod.right = Z;
  571. nod.type = types[TIND];
  572. gopcode(OAS, &nod, Z, nn);
  573. break;
  574. case OCOMMA:
  575. cgen(n->left, n->left);
  576. lcgen(n->right, nn);
  577. break;
  578. case OIND:
  579. cgen(n->left, nn);
  580. break;
  581. case OCOND:
  582. bcgen(n->left, 1);
  583. p1 = p;
  584. lcgen(n->right->left, nn);
  585. gbranch(OGOTO);
  586. patch(p1, pc);
  587. p1 = p;
  588. lcgen(n->right->right, nn);
  589. patch(p1, pc);
  590. break;
  591. }
  592. }
  593. void
  594. bcgen(Node *n, int true)
  595. {
  596. if(n->type == T)
  597. gbranch(OGOTO);
  598. else
  599. boolgen(n, true, Z);
  600. }
  601. void
  602. boolgen(Node *n, int true, Node *nn)
  603. {
  604. int o, uns;
  605. Prog *p1, *p2;
  606. Node *l, *r, nod, nod1;
  607. long curs;
  608. if(debug['g']) {
  609. prtree(nn, "boolgen lhs");
  610. prtree(n, "boolgen");
  611. }
  612. uns = 0;
  613. curs = cursafe;
  614. l = n->left;
  615. r = n->right;
  616. switch(n->op) {
  617. default:
  618. if(n->op == OCONST) {
  619. o = vconst(n);
  620. if(!true)
  621. o = !o;
  622. gbranch(OGOTO);
  623. if(o) {
  624. p1 = p;
  625. gbranch(OGOTO);
  626. patch(p1, pc);
  627. }
  628. goto com;
  629. }
  630. if(typev[n->type->etype]) {
  631. testv(n, true);
  632. goto com;
  633. }
  634. regalloc(&nod, n, nn);
  635. cgen(n, &nod);
  636. o = ONE;
  637. if(true)
  638. o = comrel[relindex(o)];
  639. if(typefd[n->type->etype]) {
  640. nodreg(&nod1, n, NREG+FREGZERO);
  641. gopcode(o, &nod, Z, &nod1);
  642. } else
  643. gopcode(o, &nod, Z, nodconst(0));
  644. regfree(&nod);
  645. goto com;
  646. case OCOMMA:
  647. cgen(l, Z);
  648. boolgen(r, true, nn);
  649. break;
  650. case ONOT:
  651. boolgen(l, !true, nn);
  652. break;
  653. case OCOND:
  654. bcgen(l, 1);
  655. p1 = p;
  656. bcgen(r->left, true);
  657. p2 = p;
  658. gbranch(OGOTO);
  659. patch(p1, pc);
  660. p1 = p;
  661. bcgen(r->right, !true);
  662. patch(p2, pc);
  663. p2 = p;
  664. gbranch(OGOTO);
  665. patch(p1, pc);
  666. patch(p2, pc);
  667. goto com;
  668. case OANDAND:
  669. if(!true)
  670. goto caseor;
  671. caseand:
  672. bcgen(l, true);
  673. p1 = p;
  674. bcgen(r, !true);
  675. p2 = p;
  676. patch(p1, pc);
  677. gbranch(OGOTO);
  678. patch(p2, pc);
  679. goto com;
  680. case OOROR:
  681. if(!true)
  682. goto caseand;
  683. caseor:
  684. bcgen(l, !true);
  685. p1 = p;
  686. bcgen(r, !true);
  687. p2 = p;
  688. gbranch(OGOTO);
  689. patch(p1, pc);
  690. patch(p2, pc);
  691. goto com;
  692. case OHI:
  693. case OHS:
  694. case OLO:
  695. case OLS:
  696. uns = 1;
  697. /* fall through */
  698. case OEQ:
  699. case ONE:
  700. case OLE:
  701. case OLT:
  702. case OGE:
  703. case OGT:
  704. if(typev[l->type->etype]){
  705. cmpv(n, true, Z);
  706. goto com;
  707. }
  708. o = n->op;
  709. if(true)
  710. o = comrel[relindex(o)];
  711. if(l->complex >= FNX && r->complex >= FNX) {
  712. regret(&nod, r);
  713. cgen(r, &nod);
  714. regsalloc(&nod1, r);
  715. gopcode(OAS, &nod, Z, &nod1);
  716. regfree(&nod);
  717. nod = *n;
  718. nod.right = &nod1;
  719. boolgen(&nod, true, nn);
  720. break;
  721. }
  722. if(!uns && sconst(r) || (uns || o == OEQ || o == ONE) && uconst(r)) {
  723. regalloc(&nod, l, nn);
  724. cgen(l, &nod);
  725. gopcode(o, &nod, Z, r);
  726. regfree(&nod);
  727. goto com;
  728. }
  729. if(l->complex >= r->complex) {
  730. regalloc(&nod1, l, nn);
  731. cgen(l, &nod1);
  732. regalloc(&nod, r, Z);
  733. cgen(r, &nod);
  734. } else {
  735. regalloc(&nod, r, nn);
  736. cgen(r, &nod);
  737. regalloc(&nod1, l, Z);
  738. cgen(l, &nod1);
  739. }
  740. gopcode(o, &nod1, Z, &nod);
  741. regfree(&nod);
  742. regfree(&nod1);
  743. com:
  744. if(nn != Z) {
  745. p1 = p;
  746. gopcode(OAS, nodconst(1L), Z, nn);
  747. gbranch(OGOTO);
  748. p2 = p;
  749. patch(p1, pc);
  750. gopcode(OAS, nodconst(0L), Z, nn);
  751. patch(p2, pc);
  752. }
  753. break;
  754. }
  755. cursafe = curs;
  756. }
  757. void
  758. sugen(Node *n, Node *nn, long w)
  759. {
  760. Prog *p1;
  761. Node nod0, nod1, nod2, nod3, nod4, *l, *r;
  762. Type *t;
  763. long pc1;
  764. int i, m, c;
  765. if(n == Z || n->type == T)
  766. return;
  767. if(nn == nodrat)
  768. if(w > nrathole)
  769. nrathole = w;
  770. if(debug['g']) {
  771. prtree(nn, "sugen lhs");
  772. prtree(n, "sugen");
  773. }
  774. if(typev[n->type->etype]) {
  775. diag(n, "old vlong sugen: %O", n->op);
  776. return;
  777. }
  778. switch(n->op) {
  779. case OIND:
  780. if(nn == Z) {
  781. nullwarn(n->left, Z);
  782. break;
  783. }
  784. default:
  785. goto copy;
  786. case ODOT:
  787. l = n->left;
  788. sugen(l, nodrat, l->type->width);
  789. if(nn != Z) {
  790. warn(n, "non-interruptable temporary");
  791. nod1 = *nodrat;
  792. r = n->right;
  793. if(!r || r->op != OCONST) {
  794. diag(n, "DOT and no offset");
  795. break;
  796. }
  797. nod1.xoffset += (long)r->vconst;
  798. nod1.type = n->type;
  799. sugen(&nod1, nn, w);
  800. }
  801. break;
  802. case OSTRUCT:
  803. /*
  804. * rewrite so lhs has no side effects
  805. */
  806. if(nn != Z && side(nn)) {
  807. nod1 = *n;
  808. nod1.type = typ(TIND, n->type);
  809. regalloc(&nod2, &nod1, Z);
  810. lcgen(nn, &nod2);
  811. regsalloc(&nod0, &nod1);
  812. gopcode(OAS, &nod2, Z, &nod0);
  813. regfree(&nod2);
  814. nod1 = *n;
  815. nod1.op = OIND;
  816. nod1.left = &nod0;
  817. nod1.right = Z;
  818. nod1.complex = 1;
  819. sugen(n, &nod1, w);
  820. return;
  821. }
  822. r = n->left;
  823. for(t = n->type->link; t != T; t = t->down) {
  824. l = r;
  825. if(r->op == OLIST) {
  826. l = r->left;
  827. r = r->right;
  828. }
  829. if(nn == Z) {
  830. cgen(l, nn);
  831. continue;
  832. }
  833. /*
  834. * hand craft *(&nn + o) = l
  835. */
  836. nod0 = znode;
  837. nod0.op = OAS;
  838. nod0.type = t;
  839. nod0.left = &nod1;
  840. nod0.right = l;
  841. nod1 = znode;
  842. nod1.op = OIND;
  843. nod1.type = t;
  844. nod1.left = &nod2;
  845. nod2 = znode;
  846. nod2.op = OADD;
  847. nod2.type = typ(TIND, t);
  848. nod2.left = &nod3;
  849. nod2.right = &nod4;
  850. nod3 = znode;
  851. nod3.op = OADDR;
  852. nod3.type = nod2.type;
  853. nod3.left = nn;
  854. nod4 = znode;
  855. nod4.op = OCONST;
  856. nod4.type = nod2.type;
  857. nod4.vconst = t->offset;
  858. ccom(&nod0);
  859. acom(&nod0);
  860. xcom(&nod0);
  861. nod0.addable = 0;
  862. /* prtree(&nod0, "hand craft"); /* */
  863. cgen(&nod0, Z);
  864. }
  865. break;
  866. case OAS:
  867. if(nn == Z) {
  868. if(n->addable < INDEXED)
  869. sugen(n->right, n->left, w);
  870. break;
  871. }
  872. /* BOTCH -- functions can clobber rathole */
  873. sugen(n->right, nodrat, w);
  874. warn(n, "non-interruptable temporary");
  875. sugen(nodrat, n->left, w);
  876. sugen(nodrat, nn, w);
  877. break;
  878. case OFUNC:
  879. /* this transformation should probably be done earlier */
  880. if(nn == Z) {
  881. sugen(n, nodrat, w);
  882. break;
  883. }
  884. if(nn->op != OIND) {
  885. nn = new1(OADDR, nn, Z);
  886. nn->type = types[TIND];
  887. nn->addable = 0;
  888. } else
  889. nn = nn->left;
  890. n = new(OFUNC, n->left, new(OLIST, nn, n->right));
  891. n->complex = FNX;
  892. n->type = types[TVOID];
  893. n->left->type = types[TVOID];
  894. cgen(n, Z);
  895. break;
  896. case OCOND:
  897. bcgen(n->left, 1);
  898. p1 = p;
  899. sugen(n->right->left, nn, w);
  900. gbranch(OGOTO);
  901. patch(p1, pc);
  902. p1 = p;
  903. sugen(n->right->right, nn, w);
  904. patch(p1, pc);
  905. break;
  906. case OCOMMA:
  907. cgen(n->left, Z);
  908. sugen(n->right, nn, w);
  909. break;
  910. }
  911. return;
  912. copy:
  913. if(nn == Z)
  914. return;
  915. if(n->complex >= FNX && nn->complex >= FNX) {
  916. t = nn->type;
  917. nn->type = types[TLONG];
  918. regialloc(&nod1, nn, Z);
  919. lcgen(nn, &nod1);
  920. regsalloc(&nod2, nn);
  921. nn->type = t;
  922. gmove(&nod1, &nod2);
  923. regfree(&nod1);
  924. nod2.type = typ(TIND, t);
  925. nod1 = nod2;
  926. nod1.op = OIND;
  927. nod1.left = &nod2;
  928. nod1.right = Z;
  929. nod1.complex = 1;
  930. nod1.type = t;
  931. sugen(n, &nod1, w);
  932. return;
  933. }
  934. if(n->complex > nn->complex) {
  935. t = n->type;
  936. n->type = types[TLONG];
  937. reglcgen(&nod1, n, Z);
  938. n->type = t;
  939. t = nn->type;
  940. nn->type = types[TLONG];
  941. reglcgen(&nod2, nn, Z);
  942. nn->type = t;
  943. } else {
  944. t = nn->type;
  945. nn->type = types[TLONG];
  946. reglcgen(&nod2, nn, Z);
  947. nn->type = t;
  948. t = n->type;
  949. n->type = types[TLONG];
  950. reglcgen(&nod1, n, Z);
  951. n->type = t;
  952. }
  953. w /= SZ_LONG;
  954. if(w <= 5) {
  955. layout(&nod1, &nod2, w, 0, Z);
  956. goto out;
  957. }
  958. /*
  959. * minimize space for unrolling loop
  960. * 3,4,5 times. (6 or more is never minimum)
  961. * if small structure, try 2 also.
  962. */
  963. c = 0; /* set */
  964. m = 100;
  965. i = 3;
  966. if(w <= 15)
  967. i = 2;
  968. for(; i<=5; i++)
  969. if(i + w%i <= m) {
  970. c = i;
  971. m = c + w%c;
  972. }
  973. regalloc(&nod3, &regnode, Z);
  974. layout(&nod1, &nod2, w%c, w/c, &nod3);
  975. pc1 = pc;
  976. layout(&nod1, &nod2, c, 0, Z);
  977. gopcode(OSUB, nodconst(1L), Z, &nod3);
  978. nod1.op = OREGISTER;
  979. gopcode(OADD, nodconst(c*SZ_LONG), Z, &nod1);
  980. nod2.op = OREGISTER;
  981. gopcode(OADD, nodconst(c*SZ_LONG), Z, &nod2);
  982. gopcode(OGT, &nod3, Z, nodconst(0));
  983. patch(p, pc1);
  984. regfree(&nod3);
  985. out:
  986. regfree(&nod1);
  987. regfree(&nod2);
  988. }
  989. void
  990. layout(Node *f, Node *t, int c, int cv, Node *cn)
  991. {
  992. Node t1, t2;
  993. while(c > 3) {
  994. layout(f, t, 2, 0, Z);
  995. c -= 2;
  996. }
  997. regalloc(&t1, &regnode, Z);
  998. regalloc(&t2, &regnode, Z);
  999. if(c > 0) {
  1000. gopcode(OAS, f, Z, &t1);
  1001. f->xoffset += SZ_LONG;
  1002. }
  1003. if(cn != Z)
  1004. gopcode(OAS, nodconst(cv), Z, cn);
  1005. if(c > 1) {
  1006. gopcode(OAS, f, Z, &t2);
  1007. f->xoffset += SZ_LONG;
  1008. }
  1009. if(c > 0) {
  1010. gopcode(OAS, &t1, Z, t);
  1011. t->xoffset += SZ_LONG;
  1012. }
  1013. if(c > 2) {
  1014. gopcode(OAS, f, Z, &t1);
  1015. f->xoffset += SZ_LONG;
  1016. }
  1017. if(c > 1) {
  1018. gopcode(OAS, &t2, Z, t);
  1019. t->xoffset += SZ_LONG;
  1020. }
  1021. if(c > 2) {
  1022. gopcode(OAS, &t1, Z, t);
  1023. t->xoffset += SZ_LONG;
  1024. }
  1025. regfree(&t1);
  1026. regfree(&t2);
  1027. }
  1028. /*
  1029. * is the vlong's value directly addressible?
  1030. */
  1031. int
  1032. isvdirect(Node *n)
  1033. {
  1034. return n->op == ONAME || n->op == OCONST || n->op == OINDREG;
  1035. }
  1036. /*
  1037. * can the constant be used with given vlong op?
  1038. */
  1039. static int
  1040. isvconstable(int o, vlong v)
  1041. {
  1042. switch(o) {
  1043. case OADD:
  1044. case OASADD:
  1045. /* there isn't an immediate form for ADDE/SUBE, but there are special ADDME/ADDZE etc */
  1046. return v == 0 || v == -1;
  1047. case OAND:
  1048. case OOR:
  1049. case OXOR:
  1050. case OLSHR:
  1051. case OASHL:
  1052. case OASHR:
  1053. case OASLSHR:
  1054. case OASASHL:
  1055. case OASASHR:
  1056. return 1;
  1057. }
  1058. return 0;
  1059. }
  1060. /*
  1061. * most 64-bit operations: cgen into a register pair, then operate.
  1062. * 64-bit comparisons are handled a little differently because the two underlying
  1063. * comparisons can be compiled separately, since the calculations don't interact.
  1064. */
  1065. static void
  1066. vcgen(Node *n, Node *o, int *f)
  1067. {
  1068. *f = 0;
  1069. if(!isvdirect(n)) {
  1070. if(n->complex >= FNX) {
  1071. regsalloc(o, n);
  1072. cgen(n, o);
  1073. return;
  1074. }
  1075. *f = 1;
  1076. if(n->addable < INDEXED && n->op != OIND && n->op != OINDEX) {
  1077. regalloc(o, n, Z);
  1078. cgen(n, o);
  1079. } else
  1080. reglcgen(o, n, Z);
  1081. } else
  1082. *o = *n;
  1083. }
  1084. static int
  1085. isuns(int op)
  1086. {
  1087. switch(op){
  1088. case OLO:
  1089. case OLS:
  1090. case OHI:
  1091. case OHS:
  1092. return 1;
  1093. default:
  1094. return 0;
  1095. }
  1096. }
  1097. static void
  1098. gcmpv(Node *l, Node *r, void (*mov)(Node*, Node*, int), int op)
  1099. {
  1100. Node vl, vr;
  1101. regalloc(&vl, &regnode, Z);
  1102. mov(l, &vl, 0);
  1103. regalloc(&vr, &regnode, Z);
  1104. mov(r, &vr, 1+isuns(op));
  1105. gopcode(op, &vl, Z, &vr);
  1106. if(vl.op == OREGISTER)
  1107. regfree(&vl);
  1108. if(vr.op == OREGISTER)
  1109. regfree(&vr);
  1110. }
  1111. static void
  1112. brcondv(Node *l, Node *r, int chi, int clo)
  1113. {
  1114. Prog *p1, *p2, *p3, *p4;
  1115. gcmpv(l, r, gloadhi, chi);
  1116. p1 = p;
  1117. gins(ABNE, Z, Z);
  1118. p2 = p;
  1119. gcmpv(l, r, gloadlo, clo);
  1120. p3 = p;
  1121. gbranch(OGOTO);
  1122. p4 = p;
  1123. patch(p1, pc);
  1124. patch(p3, pc);
  1125. gbranch(OGOTO);
  1126. patch(p2, pc);
  1127. patch(p4, pc);
  1128. }
  1129. static void
  1130. testv(Node *n, int true)
  1131. {
  1132. Node nod;
  1133. nod = znode;
  1134. nod.op = ONE;
  1135. nod.left = n;
  1136. nod.right = new1(0, Z, Z);
  1137. *nod.right = *nodconst(0);
  1138. nod.right->type = n->type;
  1139. nod.type = types[TLONG];
  1140. cmpv(&nod, true, Z);
  1141. }
  1142. /*
  1143. * comparison for vlong does high and low order parts separately,
  1144. * which saves loading the latter if the high order comparison suffices
  1145. */
  1146. static void
  1147. cmpv(Node *n, int true, Node *nn)
  1148. {
  1149. Node *l, *r, nod, nod1;
  1150. int o, f1, f2;
  1151. Prog *p1, *p2;
  1152. long curs;
  1153. if(debug['g']) {
  1154. if(nn != nil)
  1155. prtree(nn, "cmpv lhs");
  1156. prtree(n, "cmpv");
  1157. }
  1158. curs = cursafe;
  1159. l = n->left;
  1160. r = n->right;
  1161. if(l->complex >= FNX && r->complex >= FNX) {
  1162. regsalloc(&nod1, r);
  1163. cgen(r, &nod1);
  1164. nod = *n;
  1165. nod.right = &nod1;
  1166. cmpv(&nod, true, nn);
  1167. cursafe = curs;
  1168. return;
  1169. }
  1170. if(l->complex >= r->complex) {
  1171. vcgen(l, &nod1, &f1);
  1172. vcgen(r, &nod, &f2);
  1173. } else {
  1174. vcgen(r, &nod, &f2);
  1175. vcgen(l, &nod1, &f1);
  1176. }
  1177. nod.type = types[TLONG];
  1178. nod1.type = types[TLONG];
  1179. o = n->op;
  1180. if(true)
  1181. o = comrel[relindex(o)];
  1182. switch(o){
  1183. case OEQ:
  1184. gcmpv(&nod1, &nod, gloadhi, ONE);
  1185. p1 = p;
  1186. gcmpv(&nod1, &nod, gloadlo, ONE);
  1187. p2 = p;
  1188. gbranch(OGOTO);
  1189. patch(p1, pc);
  1190. patch(p2, pc);
  1191. break;
  1192. case ONE:
  1193. gcmpv(&nod1, &nod, gloadhi, ONE);
  1194. p1 = p;
  1195. gcmpv(&nod1, &nod, gloadlo, OEQ);
  1196. p2 = p;
  1197. patch(p1, pc);
  1198. gbranch(OGOTO);
  1199. patch(p2, pc);
  1200. break;
  1201. case OLE:
  1202. brcondv(&nod1, &nod, OLT, OLS);
  1203. break;
  1204. case OGT:
  1205. brcondv(&nod1, &nod, OGT, OHI);
  1206. break;
  1207. case OLS:
  1208. brcondv(&nod1, &nod, OLO, OLS);
  1209. break;
  1210. case OHI:
  1211. brcondv(&nod1, &nod, OHI, OHI);
  1212. break;
  1213. case OLT:
  1214. brcondv(&nod1, &nod, OLT, OLO);
  1215. break;
  1216. case OGE:
  1217. brcondv(&nod1, &nod, OGT, OHS);
  1218. break;
  1219. case OLO:
  1220. brcondv(&nod1, &nod, OLO, OLO);
  1221. break;
  1222. case OHS:
  1223. brcondv(&nod1, &nod, OHI, OHS);
  1224. break;
  1225. default:
  1226. diag(n, "bad cmpv");
  1227. return;
  1228. }
  1229. if(f1)
  1230. regfree(&nod1);
  1231. if(f2)
  1232. regfree(&nod);
  1233. cursafe = curs;
  1234. }
  1235. static void
  1236. cgen64(Node *n, Node *nn)
  1237. {
  1238. Node *l, *r, *d;
  1239. Node nod, nod1;
  1240. long curs;
  1241. Type *t;
  1242. int o, m;
  1243. curs = cursafe;
  1244. l = n->left;
  1245. r = n->right;
  1246. o = n->op;
  1247. switch(o) {
  1248. case OCONST:
  1249. if(nn == Z) {
  1250. nullwarn(n->left, Z);
  1251. break;
  1252. }
  1253. if(nn->op != OREGPAIR) {
  1254. //prtree(n, "cgen64 const");
  1255. t = nn->type;
  1256. nn->type = types[TLONG];
  1257. reglcgen(&nod1, nn, Z);
  1258. nn->type = t;
  1259. if(align(0, types[TCHAR], Aarg1)) /* isbigendian */
  1260. gmove(nod32const(n->vconst>>32), &nod1);
  1261. else
  1262. gmove(nod32const(n->vconst), &nod1);
  1263. nod1.xoffset += SZ_LONG;
  1264. if(align(0, types[TCHAR], Aarg1)) /* isbigendian */
  1265. gmove(nod32const(n->vconst), &nod1);
  1266. else
  1267. gmove(nod32const(n->vconst>>32), &nod1);
  1268. regfree(&nod1);
  1269. } else
  1270. gmove(n, nn);
  1271. break;
  1272. case OCAST:
  1273. /*
  1274. * convert from types l->n->nn
  1275. */
  1276. if(typev[l->type->etype]){
  1277. /* vlong to non-vlong */
  1278. if(!isvdirect(l)) {
  1279. if(l->addable < INDEXED && l->op != OIND && l->op != OINDEX) {
  1280. regalloc(&nod, l, l);
  1281. cgen(l, &nod);
  1282. regalloc(&nod1, n, nn);
  1283. gmove(nod.right, &nod1);
  1284. } else {
  1285. reglcgen(&nod, l, Z);
  1286. regalloc(&nod1, n, nn);
  1287. gloadlo(&nod, &nod1, 0); /* TO DO: not correct for typefd */
  1288. }
  1289. regfree(&nod);
  1290. } else {
  1291. regalloc(&nod1, n, nn);
  1292. gloadlo(l, &nod1, 0); /* TO DO: not correct for typefd */
  1293. }
  1294. }else{
  1295. /* non-vlong to vlong */
  1296. regalloc(&nod, l, Z);
  1297. cgen(l, &nod);
  1298. regalloc(&nod1, n, nn);
  1299. gmove(&nod, nod1.right);
  1300. if(typeu[l->type->etype])
  1301. gmove(nodconst(0), nod1.left);
  1302. else
  1303. gopcode(OASHR, nodconst(31), nod1.right, nod1.left);
  1304. regfree(&nod);
  1305. }
  1306. gmove(&nod1, nn);
  1307. regfree(&nod1);
  1308. break;
  1309. case OFUNC:
  1310. /* this transformation should probably be done earlier */
  1311. if(nn == Z) {
  1312. regsalloc(&nod1, n);
  1313. nn = &nod1;
  1314. }
  1315. m = 0;
  1316. if(nn->op != OIND) {
  1317. if(nn->op == OREGPAIR) {
  1318. m = 1;
  1319. regsalloc(&nod1, nn);
  1320. d = &nod1;
  1321. }else
  1322. d = nn;
  1323. d = new1(OADDR, d, Z);
  1324. d->type = types[TIND];
  1325. d->addable = 0;
  1326. } else
  1327. d = nn->left;
  1328. n = new(OFUNC, l, new(OLIST, d, r));
  1329. n->complex = FNX;
  1330. n->type = types[TVOID];
  1331. n->left->type = types[TVOID];
  1332. cgen(n, Z);
  1333. if(m)
  1334. gmove(&nod1, nn);
  1335. break;
  1336. default:
  1337. diag(n, "bad cgen64 %O", o);
  1338. break;
  1339. }
  1340. cursafe = curs;
  1341. }