txt.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750
  1. #include "gc.h"
  2. static int resvreg[nelem(reg)];
  3. static void gopcode64(int, Node*, Node*, Node*);
  4. static void gori64(int, Node*, Node*, Node*);
  5. static void gandi64(int, Node*, Node*, Node*);
  6. void
  7. ginit(void)
  8. {
  9. Type *t;
  10. thechar = 'q';
  11. thestring = "power";
  12. exregoffset = REGEXT;
  13. exfregoffset = FREGEXT;
  14. newvlongcode = 1;
  15. listinit();
  16. nstring = 0;
  17. mnstring = 0;
  18. nrathole = 0;
  19. pc = 0;
  20. breakpc = -1;
  21. continpc = -1;
  22. cases = C;
  23. firstp = P;
  24. lastp = P;
  25. tfield = types[TLONG];
  26. typeswitch = typechlv;
  27. zprog.link = P;
  28. zprog.as = AGOK;
  29. zprog.reg = NREG;
  30. zprog.from.type = D_NONE;
  31. zprog.from.name = D_NONE;
  32. zprog.from.reg = NREG;
  33. zprog.from3 = zprog.from;
  34. zprog.to = zprog.from;
  35. regnode.op = OREGISTER;
  36. regnode.class = CEXREG;
  37. regnode.reg = 0;
  38. regnode.complex = 0;
  39. regnode.addable = 11;
  40. regnode.type = types[TLONG];
  41. constnode.op = OCONST;
  42. constnode.class = CXXX;
  43. constnode.complex = 0;
  44. constnode.addable = 20;
  45. constnode.type = types[TLONG];
  46. fconstnode.op = OCONST;
  47. fconstnode.class = CXXX;
  48. fconstnode.complex = 0;
  49. fconstnode.addable = 20;
  50. fconstnode.type = types[TDOUBLE];
  51. nodsafe = new(ONAME, Z, Z);
  52. nodsafe->sym = slookup(".safe");
  53. nodsafe->type = types[TINT];
  54. nodsafe->etype = types[TINT]->etype;
  55. nodsafe->class = CAUTO;
  56. complex(nodsafe);
  57. t = typ(TARRAY, types[TCHAR]);
  58. symrathole = slookup(".rathole");
  59. symrathole->class = CGLOBL;
  60. symrathole->type = t;
  61. nodrat = new(ONAME, Z, Z);
  62. nodrat->sym = symrathole;
  63. nodrat->type = types[TIND];
  64. nodrat->etype = TVOID;
  65. nodrat->class = CGLOBL;
  66. complex(nodrat);
  67. nodrat->type = t;
  68. com64init();
  69. memset(reg, 0, sizeof(reg));
  70. reg[REGZERO] = 1; /* don't use */
  71. reg[REGTMP] = 1;
  72. reg[FREGCVI+NREG] = 1;
  73. reg[FREGZERO+NREG] = 1;
  74. reg[FREGHALF+NREG] = 1;
  75. reg[FREGONE+NREG] = 1;
  76. reg[FREGTWO+NREG] = 1;
  77. memmove(resvreg, reg, sizeof(reg));
  78. }
  79. void
  80. gclean(void)
  81. {
  82. int i;
  83. Sym *s;
  84. for(i=0; i<NREG; i++)
  85. if(reg[i] && !resvreg[i])
  86. diag(Z, "reg %d left allocated", i);
  87. for(i=NREG; i<NREG+NREG; i++)
  88. if(reg[i] && !resvreg[i])
  89. diag(Z, "freg %d left allocated", i-NREG);
  90. while(mnstring)
  91. outstring("", 1L);
  92. symstring->type->width = nstring;
  93. symrathole->type->width = nrathole;
  94. for(i=0; i<NHASH; i++)
  95. for(s = hash[i]; s != S; s = s->link) {
  96. if(s->type == T)
  97. continue;
  98. if(s->type->width == 0)
  99. continue;
  100. if(s->class != CGLOBL && s->class != CSTATIC)
  101. continue;
  102. if(s->type == types[TENUM])
  103. continue;
  104. gpseudo(AGLOBL, s, nodconst(s->type->width));
  105. }
  106. nextpc();
  107. p->as = AEND;
  108. outcode();
  109. }
  110. void
  111. nextpc(void)
  112. {
  113. p = alloc(sizeof(*p));
  114. *p = zprog;
  115. p->lineno = nearln;
  116. pc++;
  117. if(firstp == P) {
  118. firstp = p;
  119. lastp = p;
  120. return;
  121. }
  122. lastp->link = p;
  123. lastp = p;
  124. }
  125. void
  126. gargs(Node *n, Node *tn1, Node *tn2)
  127. {
  128. long regs;
  129. Node fnxargs[20], *fnxp;
  130. regs = cursafe;
  131. fnxp = fnxargs;
  132. garg1(n, tn1, tn2, 0, &fnxp); /* compile fns to temps */
  133. curarg = 0;
  134. fnxp = fnxargs;
  135. garg1(n, tn1, tn2, 1, &fnxp); /* compile normal args and temps */
  136. cursafe = regs;
  137. }
  138. void
  139. garg1(Node *n, Node *tn1, Node *tn2, int f, Node **fnxp)
  140. {
  141. Node nod;
  142. if(n == Z)
  143. return;
  144. if(n->op == OLIST) {
  145. garg1(n->left, tn1, tn2, f, fnxp);
  146. garg1(n->right, tn1, tn2, f, fnxp);
  147. return;
  148. }
  149. if(f == 0) {
  150. if(n->complex >= FNX) {
  151. regsalloc(*fnxp, n);
  152. nod = znode;
  153. nod.op = OAS;
  154. nod.left = *fnxp;
  155. nod.right = n;
  156. nod.type = n->type;
  157. cgen(&nod, Z);
  158. (*fnxp)++;
  159. }
  160. return;
  161. }
  162. if(typesuv[n->type->etype]) {
  163. regaalloc(tn2, n);
  164. if(n->complex >= FNX) {
  165. cgen(*fnxp, tn2);
  166. (*fnxp)++;
  167. } else
  168. cgen(n, tn2);
  169. return;
  170. }
  171. if(REGARG && curarg == 0 && typechlp[n->type->etype]) {
  172. regaalloc1(tn1, n);
  173. if(n->complex >= FNX) {
  174. cgen(*fnxp, tn1);
  175. (*fnxp)++;
  176. } else
  177. cgen(n, tn1);
  178. return;
  179. }
  180. if(vconst(n) == 0) {
  181. regaalloc(tn2, n);
  182. gopcode(OAS, n, Z, tn2);
  183. return;
  184. }
  185. regalloc(tn1, n, Z);
  186. if(n->complex >= FNX) {
  187. cgen(*fnxp, tn1);
  188. (*fnxp)++;
  189. } else
  190. cgen(n, tn1);
  191. regaalloc(tn2, n);
  192. gopcode(OAS, tn1, Z, tn2);
  193. regfree(tn1);
  194. }
  195. Node*
  196. nod32const(vlong v)
  197. {
  198. constnode.vconst = v & MASK(32);
  199. return &constnode;
  200. }
  201. Node*
  202. nodconst(long v)
  203. {
  204. constnode.vconst = v;
  205. return &constnode;
  206. }
  207. Node*
  208. nodfconst(double d)
  209. {
  210. fconstnode.fconst = d;
  211. return &fconstnode;
  212. }
  213. void
  214. nodreg(Node *n, Node *nn, int reg)
  215. {
  216. *n = regnode;
  217. n->reg = reg;
  218. n->type = nn->type;
  219. n->lineno = nn->lineno;
  220. }
  221. void
  222. regret(Node *n, Node *nn)
  223. {
  224. int r;
  225. r = REGRET;
  226. if(typefd[nn->type->etype])
  227. r = FREGRET+NREG;
  228. nodreg(n, nn, r);
  229. reg[r]++;
  230. }
  231. void
  232. regalloc(Node *n, Node *tn, Node *o)
  233. {
  234. int i, j;
  235. static int lasti;
  236. switch(tn->type->etype) {
  237. case TCHAR:
  238. case TUCHAR:
  239. case TSHORT:
  240. case TUSHORT:
  241. case TINT:
  242. case TUINT:
  243. case TLONG:
  244. case TULONG:
  245. case TIND:
  246. if(o != Z && o->op == OREGISTER) {
  247. i = o->reg;
  248. if(i > 0 && i < NREG)
  249. goto out;
  250. }
  251. j = lasti + REGRET+1;
  252. for(i=REGRET+1; i<NREG; i++) {
  253. if(j >= NREG)
  254. j = REGRET+1;
  255. if(reg[j] == 0) {
  256. i = j;
  257. goto out;
  258. }
  259. j++;
  260. }
  261. diag(tn, "out of fixed registers");
  262. goto err;
  263. case TFLOAT:
  264. case TDOUBLE:
  265. if(o != Z && o->op == OREGISTER) {
  266. i = o->reg;
  267. if(i >= NREG && i < NREG+NREG)
  268. goto out;
  269. }
  270. j = lasti + NREG;
  271. for(i=NREG; i<NREG+NREG; i++) {
  272. if(j >= NREG+NREG)
  273. j = NREG;
  274. if(reg[j] == 0) {
  275. i = j;
  276. goto out;
  277. }
  278. j++;
  279. }
  280. diag(tn, "out of float registers");
  281. goto err;
  282. case TVLONG:
  283. case TUVLONG:
  284. n->op = OREGPAIR;
  285. n->complex = 0; /* already in registers */
  286. n->addable = 11;
  287. n->type = tn->type;
  288. n->lineno = nearln;
  289. n->left = alloc(sizeof(Node));
  290. n->right = alloc(sizeof(Node));
  291. if(o != Z && o->op == OREGPAIR) {
  292. regalloc(n->left, &regnode, o->left);
  293. regalloc(n->right, &regnode, o->right);
  294. } else {
  295. regalloc(n->left, &regnode, Z);
  296. regalloc(n->right, &regnode, Z);
  297. }
  298. n->right->type = types[TULONG];
  299. if(tn->type->etype == TUVLONG)
  300. n->left->type = types[TULONG]; /* TO DO: is this a bad idea? */
  301. return;
  302. }
  303. diag(tn, "unknown type in regalloc: %T", tn->type);
  304. err:
  305. i = 0;
  306. out:
  307. if(i)
  308. reg[i]++;
  309. lasti++;
  310. if(lasti >= 5)
  311. lasti = 0;
  312. nodreg(n, tn, i);
  313. }
  314. void
  315. regialloc(Node *n, Node *tn, Node *o)
  316. {
  317. Node nod;
  318. nod = *tn;
  319. nod.type = types[TIND];
  320. regalloc(n, &nod, o);
  321. }
  322. void
  323. regfree(Node *n)
  324. {
  325. int i;
  326. if(n->op == OREGPAIR) {
  327. regfree(n->left);
  328. regfree(n->right);
  329. return;
  330. }
  331. i = 0;
  332. if(n->op != OREGISTER && n->op != OINDREG)
  333. goto err;
  334. i = n->reg;
  335. if(i < 0 || i >= sizeof(reg))
  336. goto err;
  337. if(reg[i] <= 0)
  338. goto err;
  339. reg[i]--;
  340. return;
  341. err:
  342. diag(n, "error in regfree: %d [%d]", i, reg[i]);
  343. prtree(n, "regfree");
  344. }
  345. void
  346. regsalloc(Node *n, Node *nn)
  347. {
  348. cursafe = align(cursafe+stkoff, nn->type, Aaut3)-stkoff;
  349. maxargsafe = maxround(maxargsafe, cursafe+curarg);
  350. // if(nn->type->etype == TDOUBLE || nn->type->etype == TVLONG){
  351. // extern int hasdoubled;
  352. // fprint(2, "stkoff=%ld cursafe=%ld curarg=%ld %d\n", stkoff, cursafe, curarg, hasdoubled);
  353. // }
  354. *n = *nodsafe;
  355. n->xoffset = -(stkoff + cursafe);
  356. n->type = nn->type;
  357. n->etype = nn->type->etype;
  358. n->lineno = nn->lineno;
  359. }
  360. void
  361. regaalloc1(Node *n, Node *nn)
  362. {
  363. nodreg(n, nn, REGARG);
  364. reg[REGARG]++;
  365. curarg = align(curarg, nn->type, Aarg1);
  366. curarg = align(curarg, nn->type, Aarg2);
  367. maxargsafe = maxround(maxargsafe, cursafe+curarg);
  368. }
  369. void
  370. regaalloc(Node *n, Node *nn)
  371. {
  372. curarg = align(curarg, nn->type, Aarg1);
  373. *n = *nn;
  374. n->op = OINDREG;
  375. n->reg = REGSP;
  376. n->xoffset = curarg + SZ_LONG;
  377. n->complex = 0;
  378. n->addable = 20;
  379. curarg = align(curarg, nn->type, Aarg2);
  380. maxargsafe = maxround(maxargsafe, cursafe+curarg);
  381. }
  382. void
  383. regind(Node *n, Node *nn)
  384. {
  385. if(n->op != OREGISTER) {
  386. diag(n, "regind not OREGISTER");
  387. return;
  388. }
  389. n->op = OINDREG;
  390. n->type = nn->type;
  391. }
  392. void
  393. raddr(Node *n, Prog *p)
  394. {
  395. Adr a;
  396. naddr(n, &a);
  397. if(R0ISZERO && a.type == D_CONST && a.offset == 0) {
  398. a.type = D_REG;
  399. a.reg = REGZERO;
  400. }
  401. if(a.type != D_REG && a.type != D_FREG) {
  402. if(n)
  403. diag(n, "bad in raddr: %O", n->op);
  404. else
  405. diag(n, "bad in raddr: <null>");
  406. p->reg = NREG;
  407. } else
  408. p->reg = a.reg;
  409. }
  410. void
  411. naddr(Node *n, Adr *a)
  412. {
  413. long v;
  414. a->type = D_NONE;
  415. if(n == Z)
  416. return;
  417. switch(n->op) {
  418. default:
  419. bad:
  420. diag(n, "bad in naddr: %O", n->op);
  421. break;
  422. case OREGISTER:
  423. a->type = D_REG;
  424. a->sym = S;
  425. a->reg = n->reg;
  426. if(a->reg >= NREG) {
  427. a->type = D_FREG;
  428. a->reg -= NREG;
  429. }
  430. break;
  431. case OIND:
  432. naddr(n->left, a);
  433. a->offset += n->xoffset; /* little hack for reglcgenv */
  434. if(a->type == D_REG) {
  435. a->type = D_OREG;
  436. break;
  437. }
  438. if(a->type == D_CONST) {
  439. a->type = D_OREG;
  440. break;
  441. }
  442. goto bad;
  443. case OINDREG:
  444. a->type = D_OREG;
  445. a->sym = S;
  446. a->offset = n->xoffset;
  447. a->reg = n->reg;
  448. break;
  449. case ONAME:
  450. a->etype = n->etype;
  451. a->type = D_OREG;
  452. a->name = D_STATIC;
  453. a->sym = n->sym;
  454. a->offset = n->xoffset;
  455. if(n->class == CSTATIC)
  456. break;
  457. if(n->class == CEXTERN || n->class == CGLOBL) {
  458. a->name = D_EXTERN;
  459. break;
  460. }
  461. if(n->class == CAUTO) {
  462. a->name = D_AUTO;
  463. break;
  464. }
  465. if(n->class == CPARAM) {
  466. a->name = D_PARAM;
  467. break;
  468. }
  469. goto bad;
  470. case OCONST:
  471. a->sym = S;
  472. a->reg = NREG;
  473. if(typefd[n->type->etype]) {
  474. a->type = D_FCONST;
  475. a->dval = n->fconst;
  476. } else {
  477. a->type = D_CONST;
  478. a->offset = n->vconst;
  479. }
  480. break;
  481. case OADDR:
  482. naddr(n->left, a);
  483. if(a->type == D_OREG) {
  484. a->type = D_CONST;
  485. break;
  486. }
  487. goto bad;
  488. case OADD:
  489. if(n->left->op == OCONST) {
  490. naddr(n->left, a);
  491. v = a->offset;
  492. naddr(n->right, a);
  493. } else {
  494. naddr(n->right, a);
  495. v = a->offset;
  496. naddr(n->left, a);
  497. }
  498. a->offset += v;
  499. break;
  500. }
  501. }
  502. void
  503. gloadhi(Node *f, Node *t, int c)
  504. {
  505. Type *ot;
  506. if(f->op == OCONST){
  507. f = nodconst((long)(f->vconst>>32));
  508. if(c==1 && sconst(f) || c==2 && uconst(f)){
  509. if(t->op == OREGISTER)
  510. regfree(t);
  511. *t = *f;
  512. return;
  513. }
  514. }
  515. if(f->op == OREGPAIR) {
  516. gmove(f->left, t);
  517. return;
  518. }
  519. ot = f->type;
  520. f->type = types[TLONG];
  521. gmove(f, t);
  522. f->type = ot;
  523. }
  524. void
  525. gloadlo(Node *f, Node *t, int c)
  526. {
  527. Type *ot;
  528. if(f->op == OCONST){
  529. f = nodconst((long)f->vconst);
  530. if(c && uconst(f)){
  531. if(t->op == OREGISTER)
  532. regfree(t);
  533. *t = *f;
  534. return;
  535. }
  536. }
  537. if(f->op == OREGPAIR) {
  538. gmove(f->right, t);
  539. return;
  540. }
  541. ot = f->type;
  542. f->type = types[TLONG];
  543. f->xoffset += SZ_LONG;
  544. if(0){
  545. prtree(f, "gloadlo f"); prtree(t, "gloadlo t");
  546. }
  547. gmove(f, t);
  548. f->xoffset -= SZ_LONG;
  549. f->type = ot;
  550. }
  551. void
  552. fop(int as, int f1, int f2, Node *t)
  553. {
  554. Node nod1, nod2, nod3;
  555. nodreg(&nod1, t, NREG+f1);
  556. nodreg(&nod2, t, NREG+f2);
  557. regalloc(&nod3, t, t);
  558. gopcode(as, &nod1, &nod2, &nod3);
  559. gmove(&nod3, t);
  560. regfree(&nod3);
  561. }
  562. void
  563. gmove(Node *f, Node *t)
  564. {
  565. int ft, tt, a;
  566. Node nod, fxc0, fxc1, fxc2, fxrat;
  567. Prog *p1;
  568. double d;
  569. ft = f->type->etype;
  570. tt = t->type->etype;
  571. if(ft == TDOUBLE && f->op == OCONST) {
  572. d = f->fconst;
  573. if(d == 0.0) {
  574. a = FREGZERO;
  575. goto ffreg;
  576. }
  577. if(d == 0.5) {
  578. a = FREGHALF;
  579. goto ffreg;
  580. }
  581. if(d == 1.0) {
  582. a = FREGONE;
  583. goto ffreg;
  584. }
  585. if(d == 2.0) {
  586. a = FREGTWO;
  587. goto ffreg;
  588. }
  589. if(d == -.5) {
  590. fop(OSUB, FREGHALF, FREGZERO, t);
  591. return;
  592. }
  593. if(d == -1.0) {
  594. fop(OSUB, FREGONE, FREGZERO, t);
  595. return;
  596. }
  597. if(d == -2.0) {
  598. fop(OSUB, FREGTWO, FREGZERO, t);
  599. return;
  600. }
  601. if(d == 1.5) {
  602. fop(OADD, FREGONE, FREGHALF, t);
  603. return;
  604. }
  605. if(d == 2.5) {
  606. fop(OADD, FREGTWO, FREGHALF, t);
  607. return;
  608. }
  609. if(d == 3.0) {
  610. fop(OADD, FREGTWO, FREGONE, t);
  611. return;
  612. }
  613. }
  614. if(ft == TFLOAT && f->op == OCONST) {
  615. d = f->fconst;
  616. if(d == 0) {
  617. a = FREGZERO;
  618. ffreg:
  619. nodreg(&nod, f, NREG+a);
  620. gmove(&nod, t);
  621. return;
  622. }
  623. }
  624. if((ft == TVLONG || ft == TUVLONG) && f->op == OCONST && t->op == OREGPAIR) {
  625. if(align(0, types[TCHAR], Aarg1)) /* isbigendian */
  626. gmove(nod32const(f->vconst>>32), t->left);
  627. else
  628. gmove(nod32const(f->vconst), t->left);
  629. if(align(0, types[TCHAR], Aarg1)) /* isbigendian */
  630. gmove(nod32const(f->vconst), t->right);
  631. else
  632. gmove(nod32const(f->vconst>>32), t->right);
  633. return;
  634. }
  635. /*
  636. * a load --
  637. * put it into a register then
  638. * worry what to do with it.
  639. */
  640. if(f->op == ONAME || f->op == OINDREG || f->op == OIND) {
  641. switch(ft) {
  642. default:
  643. a = AMOVW;
  644. break;
  645. case TFLOAT:
  646. a = AFMOVS;
  647. break;
  648. case TDOUBLE:
  649. a = AFMOVD;
  650. break;
  651. case TCHAR:
  652. a = AMOVB;
  653. break;
  654. case TUCHAR:
  655. a = AMOVBZ;
  656. break;
  657. case TSHORT:
  658. a = AMOVH;
  659. break;
  660. case TUSHORT:
  661. a = AMOVHZ;
  662. break;
  663. }
  664. if(typev[ft]) {
  665. if(typev[tt]) {
  666. regalloc(&nod, f, t);
  667. /* low order first, because its value will be used first */
  668. f->xoffset += SZ_LONG;
  669. gins(AMOVW, f, nod.right);
  670. f->xoffset -= SZ_LONG;
  671. gins(AMOVW, f, nod.left);
  672. } else {
  673. /* assumed not float or double */
  674. regalloc(&nod, &regnode, t);
  675. f->xoffset += SZ_LONG;
  676. gins(AMOVW, f, &nod);
  677. f->xoffset -= SZ_LONG;
  678. }
  679. } else {
  680. regalloc(&nod, f, t);
  681. gins(a, f, &nod);
  682. }
  683. gmove(&nod, t);
  684. regfree(&nod);
  685. return;
  686. }
  687. /*
  688. * a store --
  689. * put it into a register then
  690. * store it.
  691. */
  692. if(t->op == ONAME || t->op == OINDREG || t->op == OIND) {
  693. switch(tt) {
  694. default:
  695. a = AMOVW;
  696. break;
  697. case TUCHAR:
  698. a = AMOVBZ;
  699. break;
  700. case TCHAR:
  701. a = AMOVB;
  702. break;
  703. case TUSHORT:
  704. a = AMOVHZ;
  705. break;
  706. case TSHORT:
  707. a = AMOVH;
  708. break;
  709. case TFLOAT:
  710. a = AFMOVS;
  711. break;
  712. case TDOUBLE:
  713. a = AFMOVD;
  714. break;
  715. }
  716. if(R0ISZERO && !typefd[ft] && vconst(f) == 0) {
  717. gins(a, f, t);
  718. if(typev[tt]) {
  719. t->xoffset += SZ_LONG;
  720. gins(a, f, t);
  721. t->xoffset -= SZ_LONG;
  722. }
  723. return;
  724. }
  725. if(ft == tt)
  726. regalloc(&nod, t, f);
  727. else
  728. regalloc(&nod, t, Z);
  729. gmove(f, &nod);
  730. if(typev[tt]) {
  731. t->xoffset += SZ_LONG;
  732. gins(a, nod.right, t);
  733. t->xoffset -= SZ_LONG;
  734. gins(a, nod.left, t);
  735. } else
  736. gins(a, &nod, t);
  737. regfree(&nod);
  738. return;
  739. }
  740. /*
  741. * type x type cross table
  742. */
  743. a = AGOK;
  744. switch(ft) {
  745. case TDOUBLE:
  746. case TFLOAT:
  747. switch(tt) {
  748. case TDOUBLE:
  749. a = AFMOVD;
  750. if(ft == TFLOAT)
  751. a = AFMOVS; /* AFMOVSD */
  752. break;
  753. case TFLOAT:
  754. a = AFRSP;
  755. if(ft == TFLOAT)
  756. a = AFMOVS;
  757. break;
  758. case TINT:
  759. case TUINT:
  760. case TLONG:
  761. case TULONG:
  762. case TIND:
  763. case TSHORT:
  764. case TUSHORT:
  765. case TCHAR:
  766. case TUCHAR:
  767. /* BUG: not right for unsigned long */
  768. regalloc(&nod, f, Z); /* should be type float */
  769. regsalloc(&fxrat, &fconstnode);
  770. gins(AFCTIWZ, f, &nod);
  771. gins(AFMOVD, &nod, &fxrat);
  772. regfree(&nod);
  773. fxrat.type = nodrat->type;
  774. fxrat.etype = nodrat->etype;
  775. fxrat.xoffset += 4;
  776. gins(AMOVW, &fxrat, t);
  777. gmove(t, t);
  778. return;
  779. }
  780. break;
  781. case TINT:
  782. case TUINT:
  783. case TLONG:
  784. case TULONG:
  785. case TIND:
  786. switch(tt) {
  787. case TDOUBLE:
  788. case TFLOAT:
  789. goto fxtofl;
  790. case TINT:
  791. case TUINT:
  792. case TLONG:
  793. case TULONG:
  794. case TIND:
  795. case TSHORT:
  796. case TUSHORT:
  797. case TCHAR:
  798. case TUCHAR:
  799. a = AMOVW;
  800. break;
  801. }
  802. break;
  803. case TSHORT:
  804. switch(tt) {
  805. case TDOUBLE:
  806. case TFLOAT:
  807. goto fxtofl;
  808. case TINT:
  809. case TUINT:
  810. case TLONG:
  811. case TULONG:
  812. case TIND:
  813. a = AMOVH;
  814. break;
  815. case TSHORT:
  816. case TUSHORT:
  817. case TCHAR:
  818. case TUCHAR:
  819. a = AMOVW;
  820. break;
  821. }
  822. break;
  823. case TUSHORT:
  824. switch(tt) {
  825. case TDOUBLE:
  826. case TFLOAT:
  827. goto fxtofl;
  828. case TINT:
  829. case TUINT:
  830. case TLONG:
  831. case TULONG:
  832. case TIND:
  833. a = AMOVHZ;
  834. break;
  835. case TSHORT:
  836. case TUSHORT:
  837. case TCHAR:
  838. case TUCHAR:
  839. a = AMOVW;
  840. break;
  841. }
  842. break;
  843. case TCHAR:
  844. switch(tt) {
  845. case TDOUBLE:
  846. case TFLOAT:
  847. goto fxtofl;
  848. case TINT:
  849. case TUINT:
  850. case TLONG:
  851. case TULONG:
  852. case TIND:
  853. case TSHORT:
  854. case TUSHORT:
  855. a = AMOVB;
  856. break;
  857. case TCHAR:
  858. case TUCHAR:
  859. a = AMOVW;
  860. break;
  861. }
  862. break;
  863. case TUCHAR:
  864. switch(tt) {
  865. case TDOUBLE:
  866. case TFLOAT:
  867. fxtofl:
  868. /*
  869. * rat[0] = 0x43300000; rat[1] = f^0x80000000;
  870. * t = *(double*)rat - FREGCVI;
  871. * is-unsigned(t) => if(t<0) t += 2^32;
  872. * could be streamlined for int-to-float
  873. */
  874. regalloc(&fxc0, f, Z);
  875. regalloc(&fxc2, f, Z);
  876. regsalloc(&fxrat, &fconstnode); /* should be type float */
  877. gins(AMOVW, nodconst(0x43300000L), &fxc0);
  878. gins(AMOVW, f, &fxc2);
  879. gins(AMOVW, &fxc0, &fxrat);
  880. gins(AXOR, nodconst(0x80000000L), &fxc2);
  881. fxc1 = fxrat;
  882. fxc1.type = nodrat->type;
  883. fxc1.etype = nodrat->etype;
  884. fxc1.xoffset += SZ_LONG;
  885. gins(AMOVW, &fxc2, &fxc1);
  886. regfree(&fxc2);
  887. regfree(&fxc0);
  888. regalloc(&nod, t, t); /* should be type float */
  889. gins(AFMOVD, &fxrat, &nod);
  890. nodreg(&fxc1, t, NREG+FREGCVI);
  891. gins(AFSUB, &fxc1, &nod);
  892. a = AFMOVD;
  893. if(tt == TFLOAT)
  894. a = AFRSP;
  895. gins(a, &nod, t);
  896. regfree(&nod);
  897. if(ft == TULONG) {
  898. regalloc(&nod, t, Z);
  899. if(tt == TFLOAT) {
  900. gins(AFCMPU, t, Z);
  901. p->to.type = D_FREG;
  902. p->to.reg = FREGZERO;
  903. gins(ABGE, Z, Z);
  904. p1 = p;
  905. gins(AFMOVS, nodfconst(4294967296.), &nod);
  906. gins(AFADDS, &nod, t);
  907. } else {
  908. gins(AFCMPU, t, Z);
  909. p->to.type = D_FREG;
  910. p->to.reg = FREGZERO;
  911. gins(ABGE, Z, Z);
  912. p1 = p;
  913. gins(AFMOVD, nodfconst(4294967296.), &nod);
  914. gins(AFADD, &nod, t);
  915. }
  916. patch(p1, pc);
  917. regfree(&nod);
  918. }
  919. return;
  920. case TINT:
  921. case TUINT:
  922. case TLONG:
  923. case TULONG:
  924. case TIND:
  925. case TSHORT:
  926. case TUSHORT:
  927. a = AMOVBZ;
  928. break;
  929. case TCHAR:
  930. case TUCHAR:
  931. a = AMOVW;
  932. break;
  933. }
  934. break;
  935. case TVLONG:
  936. case TUVLONG:
  937. switch(tt) {
  938. case TVLONG:
  939. case TUVLONG:
  940. a = AMOVW;
  941. break;
  942. }
  943. break;
  944. }
  945. if(a == AGOK)
  946. diag(Z, "bad opcode in gmove %T -> %T", f->type, t->type);
  947. if(a == AMOVW || a == AFMOVS || a == AFMOVD)
  948. if(samaddr(f, t))
  949. return;
  950. if(typev[ft]) {
  951. if(f->op != OREGPAIR || t->op != OREGPAIR)
  952. diag(Z, "bad vlong in gmove (%O->%O)", f->op, t->op);
  953. gins(a, f->left, t->left);
  954. gins(a, f->right, t->right);
  955. } else
  956. gins(a, f, t);
  957. }
  958. void
  959. gins(int a, Node *f, Node *t)
  960. {
  961. nextpc();
  962. p->as = a;
  963. if(f != Z)
  964. naddr(f, &p->from);
  965. if(t != Z)
  966. naddr(t, &p->to);
  967. if(debug['g'])
  968. print("%P\n", p);
  969. }
  970. void
  971. gins3(int a, Node *f1, Node *f2, Node *t)
  972. {
  973. Adr ta;
  974. nextpc();
  975. p->as = a;
  976. if(f1 != Z)
  977. naddr(f1, &p->from);
  978. if(f2 != Z && (f2->op != OREGISTER || !samaddr(f2, t))) {
  979. ta = zprog.from; /* TO DO */
  980. naddr(f2, &ta);
  981. p->reg = ta.reg;
  982. if(ta.type == D_CONST && ta.offset == 0) {
  983. if(R0ISZERO)
  984. p->reg = REGZERO;
  985. else
  986. diag(Z, "REGZERO in gins3 %A", a);
  987. }else if(ta.type == D_CONST)
  988. p->from3 = ta;
  989. }
  990. if(t != Z)
  991. naddr(t, &p->to);
  992. if(debug['g'])
  993. print("%P\n", p);
  994. }
  995. void
  996. gins4(int a, Node *f1, Node *f2, Node *f3, Node *t)
  997. {
  998. Adr ta;
  999. nextpc();
  1000. p->as = a;
  1001. naddr(f1, &p->from);
  1002. if(f2->op != OREGISTER && (f2->op != OCONST || vconst(f2) != 0))
  1003. diag(f2, "invalid gins4");
  1004. naddr(f2, &ta);
  1005. p->reg = ta.reg;
  1006. if(ta.type == D_CONST && ta.offset == 0)
  1007. p->reg = REGZERO;
  1008. naddr(f3, &p->from3);
  1009. naddr(t, &p->to);
  1010. if(debug['g'])
  1011. print("%P\n", p);
  1012. }
  1013. void
  1014. gopcode(int o, Node *f1, Node *f2, Node *t)
  1015. {
  1016. int a, et, uns;
  1017. if(o == OAS) {
  1018. gmove(f1, t);
  1019. return;
  1020. }
  1021. et = TLONG;
  1022. if(f1 != Z && f1->type != T) {
  1023. if(f1->op == OCONST && t != Z && t->type != T)
  1024. et = t->type->etype;
  1025. else
  1026. et = f1->type->etype;
  1027. }
  1028. if((typev[et] || t->type != T && typev[t->type->etype]) && o != OFUNC) {
  1029. gopcode64(o, f1, f2, t);
  1030. return;
  1031. }
  1032. uns = 0;
  1033. a = AGOK;
  1034. switch(o) {
  1035. case OASADD:
  1036. case OADD:
  1037. a = AADD;
  1038. if(et == TFLOAT)
  1039. a = AFADDS;
  1040. else
  1041. if(et == TDOUBLE)
  1042. a = AFADD;
  1043. break;
  1044. case OASSUB:
  1045. case OSUB:
  1046. a = ASUB;
  1047. if(et == TFLOAT)
  1048. a = AFSUBS;
  1049. else
  1050. if(et == TDOUBLE)
  1051. a = AFSUB;
  1052. break;
  1053. case OASOR:
  1054. case OOR:
  1055. a = AOR;
  1056. break;
  1057. case OASAND:
  1058. case OAND:
  1059. a = AAND;
  1060. if(f1->op == OCONST)
  1061. a = AANDCC;
  1062. break;
  1063. case OASXOR:
  1064. case OXOR:
  1065. a = AXOR;
  1066. break;
  1067. case OASLSHR:
  1068. case OLSHR:
  1069. a = ASRW;
  1070. break;
  1071. case OASASHR:
  1072. case OASHR:
  1073. a = ASRAW;
  1074. break;
  1075. case OASASHL:
  1076. case OASHL:
  1077. a = ASLW;
  1078. break;
  1079. case OFUNC:
  1080. a = ABL;
  1081. break;
  1082. case OASLMUL:
  1083. case OLMUL:
  1084. case OASMUL:
  1085. case OMUL:
  1086. if(et == TFLOAT) {
  1087. a = AFMULS;
  1088. break;
  1089. } else
  1090. if(et == TDOUBLE) {
  1091. a = AFMUL;
  1092. break;
  1093. }
  1094. a = AMULLW;
  1095. break;
  1096. case OASDIV:
  1097. case ODIV:
  1098. if(et == TFLOAT) {
  1099. a = AFDIVS;
  1100. break;
  1101. } else
  1102. if(et == TDOUBLE) {
  1103. a = AFDIV;
  1104. break;
  1105. }
  1106. a = ADIVW;
  1107. break;
  1108. case OASMOD:
  1109. case OMOD:
  1110. a = AREM;
  1111. break;
  1112. case OASLMOD:
  1113. case OLMOD:
  1114. a = AREMU;
  1115. break;
  1116. case OASLDIV:
  1117. case OLDIV:
  1118. a = ADIVWU;
  1119. break;
  1120. case OCOM:
  1121. a = ANOR;
  1122. break;
  1123. case ONEG:
  1124. a = ANEG;
  1125. if(et == TFLOAT || et == TDOUBLE)
  1126. a = AFNEG;
  1127. break;
  1128. case OEQ:
  1129. a = ABEQ;
  1130. if(t->op == OCONST && t->vconst >= (1<<15))
  1131. goto cmpu;
  1132. goto cmp;
  1133. case ONE:
  1134. a = ABNE;
  1135. if(t->op == OCONST && t->vconst >= (1<<15))
  1136. goto cmpu;
  1137. goto cmp;
  1138. case OLT:
  1139. a = ABLT;
  1140. goto cmp;
  1141. case OLE:
  1142. a = ABLE;
  1143. goto cmp;
  1144. case OGE:
  1145. a = ABGE;
  1146. goto cmp;
  1147. case OGT:
  1148. a = ABGT;
  1149. goto cmp;
  1150. case OLO:
  1151. a = ABLT;
  1152. goto cmpu;
  1153. case OLS:
  1154. a = ABLE;
  1155. goto cmpu;
  1156. case OHS:
  1157. a = ABGE;
  1158. goto cmpu;
  1159. case OHI:
  1160. a = ABGT;
  1161. goto cmpu;
  1162. cmpu:
  1163. uns = 1;
  1164. cmp:
  1165. nextpc();
  1166. p->as = uns? ACMPU: ACMP;
  1167. if(et == TFLOAT)
  1168. p->as = AFCMPU;
  1169. else
  1170. if(et == TDOUBLE)
  1171. p->as = AFCMPU;
  1172. if(f1 != Z)
  1173. naddr(f1, &p->from);
  1174. if(t != Z)
  1175. naddr(t, &p->to);
  1176. if(f1 == Z || t == Z || f2 != Z)
  1177. diag(Z, "bad cmp in gopcode %O", o);
  1178. if(debug['g'])
  1179. print("%P\n", p);
  1180. f1 = Z;
  1181. f2 = Z;
  1182. t = Z;
  1183. break;
  1184. }
  1185. if(a == AGOK)
  1186. diag(Z, "bad in gopcode %O", o);
  1187. gins3(a, f1, f2, t);
  1188. }
  1189. static void
  1190. gopcode64(int o, Node *f1, Node *f2, Node *t)
  1191. {
  1192. int a1, a2;
  1193. Node nod, nod1, nod2, sh;
  1194. ulong m;
  1195. Prog *p1;
  1196. if(t->op != OREGPAIR || f2 != Z && f2->op != OREGPAIR) {
  1197. diag(Z, "bad f2/dest in gopcode64 %O", o);
  1198. return;
  1199. }
  1200. if(f1->op != OCONST &&
  1201. (typev[f1->type->etype] && f1->op != OREGPAIR || !typev[f1->type->etype] && f1->op != OREGISTER)) {
  1202. diag(Z, "bad f1[%O] in gopcode64 %O", f1->op, o);
  1203. return;
  1204. }
  1205. /* a1 for low-order, a2 for high-order */
  1206. a1 = AGOK;
  1207. a2 = AGOK;
  1208. switch(o) {
  1209. case OASADD:
  1210. case OADD:
  1211. if(f1->op == OCONST && sconst(f1)) {
  1212. if(f2 == Z)
  1213. f2 = t;
  1214. gins3(AADDC, f1, f2->right, t->right);
  1215. if((f1->vconst>>32) == 0)
  1216. gins(AADDZE, f2->left, t->left);
  1217. else if((f1->vconst>>32) == -1)
  1218. gins(AADDME, f2->left, t->left);
  1219. else
  1220. diag(t, "odd vlong ADD: %lld", f1->vconst);
  1221. return;
  1222. }
  1223. a1 = AADDC;
  1224. a2 = AADDE;
  1225. break;
  1226. case OASSUB:
  1227. case OSUB:
  1228. a1 = ASUBC;
  1229. a2 = ASUBE;
  1230. break;
  1231. case OASOR:
  1232. case OOR:
  1233. if(f1->op == OCONST) {
  1234. gori64(AOR, f1, f2, t);
  1235. return;
  1236. }
  1237. a1 = a2 = AOR;
  1238. break;
  1239. case OASAND:
  1240. case OAND:
  1241. if(f1->op == OCONST) {
  1242. gandi64(AANDCC, f1, f2, t);
  1243. return;
  1244. }
  1245. a1 = a2 = AAND;
  1246. break;
  1247. case OASXOR:
  1248. case OXOR:
  1249. if(f1->op == OCONST) {
  1250. gori64(AXOR, f1, f2, t);
  1251. return;
  1252. }
  1253. a1 = a2 = AXOR;
  1254. break;
  1255. case OASLSHR:
  1256. case OLSHR:
  1257. if(f2 == Z)
  1258. f2 = t;
  1259. if(f1->op == OCONST) {
  1260. if(f1->vconst >= 32) {
  1261. if(f1->vconst == 32)
  1262. gmove(f2->left, t->right);
  1263. else if(f1->vconst < 64)
  1264. gins3(ASRW, nodconst(f1->vconst-32), f2->left, t->right);
  1265. else
  1266. gmove(nodconst(0), t->right);
  1267. gmove(nodconst(0), t->left);
  1268. return;
  1269. }
  1270. if(f1->vconst <= 0) {
  1271. if(f2 != t)
  1272. gmove(f2, t);
  1273. return;
  1274. }
  1275. sh = *nodconst(32 - f1->vconst);
  1276. m = 0xFFFFFFFFUL >> f1->vconst;
  1277. gins4(ARLWNM, &sh, f2->right, nodconst(m), t->right);
  1278. gins4(ARLWMI, &sh, f2->left, nodconst(~m), t->right);
  1279. gins4(ARLWNM, &sh, f2->left, nodconst(m), t->left);
  1280. return;
  1281. }
  1282. regalloc(&nod, &regnode, Z);
  1283. gins3(ASUBC, f1, nodconst(32), &nod);
  1284. gins3(ASRW, f1, f2->right, t->right);
  1285. regalloc(&nod1, &regnode, Z);
  1286. gins3(ASLW, &nod, f2->left, &nod1);
  1287. gins(AOR, &nod1, t->right);
  1288. gins3(AADD, nodconst(-32), f1, &nod);
  1289. gins3(ASRW, &nod, f2->left, &nod1);
  1290. gins(AOR, &nod1, t->right);
  1291. gins3(ASRW, f1, f2->left, t->left);
  1292. regfree(&nod);
  1293. regfree(&nod1);
  1294. return;
  1295. case OASASHR:
  1296. case OASHR:
  1297. if(f2 == Z)
  1298. f2 = t;
  1299. if(f1->op == OCONST) {
  1300. if(f1->vconst >= 32) {
  1301. if(f1->vconst == 32)
  1302. gmove(f2->left, t->right);
  1303. else if(f1->vconst < 64)
  1304. gins3(ASRAW, nodconst(f1->vconst-32), f2->left, t->right);
  1305. gins3(ASRAW, nodconst(31), f2->left, t->left);
  1306. if(f1->vconst >= 64) {
  1307. gmove(t->left, t->right);
  1308. return;
  1309. }
  1310. return;
  1311. }
  1312. if(f1->vconst <= 0) {
  1313. if(f2 != t)
  1314. gmove(f2, t);
  1315. return;
  1316. }
  1317. sh = *nodconst(32 - f1->vconst);
  1318. m = 0xFFFFFFFFUL >> f1->vconst;
  1319. gins4(ARLWNM, &sh, f2->right, nodconst(m), t->right);
  1320. gins4(ARLWMI, &sh, f2->left, nodconst(~m), t->right);
  1321. gins3(ASRAW, &sh, f2->left, t->left);
  1322. return;
  1323. }
  1324. regalloc(&nod, &regnode, Z);
  1325. gins3(ASUBC, f1, nodconst(32), &nod);
  1326. gins3(ASRW, f1, f2->right, t->right);
  1327. regalloc(&nod1, &regnode, Z);
  1328. gins3(ASLW, &nod, f2->left, &nod1);
  1329. gins(AOR, &nod1, t->right);
  1330. gins3(AADDCCC, nodconst(-32), f1, &nod);
  1331. gins3(ASRAW, &nod, f2->left, &nod1);
  1332. gins(ABLE, Z, Z);
  1333. p1 = p;
  1334. gins(AMOVW, &nod1, t->right);
  1335. patch(p1, pc);
  1336. gins3(ASRAW, f1, f2->left, t->left);
  1337. regfree(&nod);
  1338. regfree(&nod1);
  1339. return;
  1340. case OASASHL:
  1341. case OASHL:
  1342. if(f2 == Z)
  1343. f2 = t;
  1344. if(f1->op == OCONST) {
  1345. if(f1->vconst >= 32) {
  1346. if(f1->vconst == 32)
  1347. gmove(f2->right, t->left);
  1348. else if(f1->vconst >= 64)
  1349. gmove(nodconst(0), t->left);
  1350. else
  1351. gins3(ASLW, nodconst(f1->vconst-32), f2->right, t->left);
  1352. gmove(nodconst(0), t->right);
  1353. return;
  1354. }
  1355. if(f1->vconst <= 0) {
  1356. if(f2 != t)
  1357. gmove(f2, t);
  1358. return;
  1359. }
  1360. m = 0xFFFFFFFFUL << f1->vconst;
  1361. gins4(ARLWNM, f1, f2->left, nodconst(m), t->left);
  1362. gins4(ARLWMI, f1, f2->right, nodconst(~m), t->left);
  1363. gins4(ARLWNM, f1, f2->right, nodconst(m), t->right);
  1364. return;
  1365. }
  1366. regalloc(&nod, &regnode, Z);
  1367. gins3(ASUBC, f1, nodconst(32), &nod);
  1368. gins3(ASLW, f1, f2->left, t->left);
  1369. regalloc(&nod1, &regnode, Z);
  1370. gins3(ASRW, &nod, f2->right, &nod1);
  1371. gins(AOR, &nod1, t->left);
  1372. gins3(AADD, nodconst(-32), f1, &nod);
  1373. gins3(ASLW, &nod, f2->right, &nod1);
  1374. gins(AOR, &nod1, t->left);
  1375. gins3(ASLW, f1, f2->right, t->right);
  1376. regfree(&nod);
  1377. regfree(&nod1);
  1378. return;
  1379. case OASLMUL:
  1380. case OLMUL:
  1381. case OASMUL:
  1382. case OMUL:
  1383. if(f2 == Z)
  1384. f2 = t;
  1385. regalloc(&nod, &regnode, Z);
  1386. gins3(AMULLW, f1->right, f2->right, &nod); /* lo(f2.low*f1.low) */
  1387. regalloc(&nod1, &regnode, Z);
  1388. gins3(AMULHWU, f1->right, f2->right, &nod1); /* hi(f2.low*f1.low) */
  1389. regalloc(&nod2, &regnode, Z);
  1390. gins3(AMULLW, f2->right, f1->left, &nod2); /* lo(f2.low*f1.high) */
  1391. gins(AADD, &nod2, &nod1);
  1392. gins3(AMULLW, f1->right, f2->left, &nod2); /* lo(f2.high*f1.low) */
  1393. gins(AADD, &nod2, &nod1);
  1394. regfree(&nod2);
  1395. gmove(&nod, t->right);
  1396. gmove(&nod1, t->left);
  1397. regfree(&nod);
  1398. regfree(&nod1);
  1399. return;
  1400. case OCOM:
  1401. a1 = a2 = ANOR;
  1402. break;
  1403. case ONEG:
  1404. gins3(ASUBC, t->right, nodconst(0), t->right);
  1405. gins(ASUBZE, t->left, t->left);
  1406. return;
  1407. }
  1408. if(a1 == AGOK || a2 == AGOK)
  1409. diag(Z, "bad in gopcode64 %O", o);
  1410. if(f1->op == OCONST) {
  1411. if(f2 != Z & f2 != t)
  1412. diag(Z, "bad const in gopcode64 %O", o);
  1413. gins(a1, nod32const(f1->vconst), t->right);
  1414. gins(a2, nod32const(f1->vconst>>32), t->left);
  1415. } else {
  1416. if(f2 != Z && f2 != t) {
  1417. gins3(a1, f1->right, f2->right, t->right);
  1418. gins3(a2, f1->left, f2->left, t->left);
  1419. } else {
  1420. gins(a1, f1->right, t->right);
  1421. gins(a2, f1->left, t->left);
  1422. }
  1423. }
  1424. }
  1425. samaddr(Node *f, Node *t)
  1426. {
  1427. if(f->op != t->op)
  1428. return 0;
  1429. switch(f->op) {
  1430. case OREGISTER:
  1431. if(f->reg != t->reg)
  1432. break;
  1433. return 1;
  1434. case OREGPAIR:
  1435. return samaddr(f->left, t->left) && samaddr(f->right, t->right);
  1436. }
  1437. return 0;
  1438. }
  1439. static void
  1440. gori64(int a, Node *f1, Node *f2, Node *t)
  1441. {
  1442. ulong lo, hi;
  1443. if(f2 == Z)
  1444. f2 = t;
  1445. lo = f1->vconst & MASK(32);
  1446. hi = (f1->vconst >> 32) & MASK(32);
  1447. if(lo & 0xFFFF)
  1448. gins3(a, nodconst(lo & 0xFFFF), f2->right, t->right);
  1449. if((lo >> 16) != 0)
  1450. gins3(a, nodconst(lo & 0xFFFF0000UL), f2->right, t->right);
  1451. if(hi & 0xFFFF)
  1452. gins3(a, nodconst(hi & 0xFFFF), f2->left, t->left);
  1453. if((hi >> 16) != 0)
  1454. gins3(a, nodconst(hi & 0xFFFF0000UL), f2->left, t->left);
  1455. }
  1456. static void
  1457. gandi64(int a, Node *f1, Node *f2, Node *t)
  1458. {
  1459. ulong lo, hi;
  1460. if(f2 == Z)
  1461. f2 = t;
  1462. lo = f1->vconst & MASK(32);
  1463. hi = (f1->vconst >> 32) & MASK(32);
  1464. if(lo == 0)
  1465. gins(AMOVW, nodconst(0), t->right);
  1466. else
  1467. gins3(a, nodconst(lo), f2->right, t->right);
  1468. if(hi == 0)
  1469. gins(AMOVW, nodconst(0), t->left);
  1470. else
  1471. gins3(a, nodconst(hi), f2->left, t->left);
  1472. }
  1473. void
  1474. gbranch(int o)
  1475. {
  1476. int a;
  1477. a = AGOK;
  1478. switch(o) {
  1479. case ORETURN:
  1480. a = ARETURN;
  1481. break;
  1482. case OGOTO:
  1483. a = ABR;
  1484. break;
  1485. }
  1486. nextpc();
  1487. if(a == AGOK) {
  1488. diag(Z, "bad in gbranch %O", o);
  1489. nextpc();
  1490. }
  1491. p->as = a;
  1492. }
  1493. void
  1494. patch(Prog *op, long pc)
  1495. {
  1496. op->to.offset = pc;
  1497. op->to.type = D_BRANCH;
  1498. }
  1499. void
  1500. gpseudo(int a, Sym *s, Node *n)
  1501. {
  1502. nextpc();
  1503. p->as = a;
  1504. p->from.type = D_OREG;
  1505. p->from.sym = s;
  1506. if(a == ATEXT)
  1507. p->reg = (profileflg ? 0 : NOPROF);
  1508. p->from.name = D_EXTERN;
  1509. if(s->class == CSTATIC)
  1510. p->from.name = D_STATIC;
  1511. naddr(n, &p->to);
  1512. if(a == ADATA || a == AGLOBL)
  1513. pc--;
  1514. }
  1515. int
  1516. sval(long v)
  1517. {
  1518. if(v >= -(1<<15) && v < (1<<15))
  1519. return 1;
  1520. return 0;
  1521. }
  1522. int
  1523. sconst(Node *n)
  1524. {
  1525. vlong vv;
  1526. if(n->op == OCONST) {
  1527. if(!typefd[n->type->etype]) {
  1528. vv = n->vconst;
  1529. if(vv >= -(((vlong)1)<<15) && vv < (((vlong)1)<<15))
  1530. return 1;
  1531. }
  1532. }
  1533. return 0;
  1534. }
  1535. int
  1536. uconst(Node *n)
  1537. {
  1538. vlong vv;
  1539. if(n->op == OCONST) {
  1540. if(!typefd[n->type->etype]) {
  1541. vv = n->vconst;
  1542. if(vv >= 0 && vv < (((vlong)1)<<16))
  1543. return 1;
  1544. }
  1545. }
  1546. return 0;
  1547. }
  1548. long
  1549. exreg(Type *t)
  1550. {
  1551. long o;
  1552. if(typechlp[t->etype]) {
  1553. if(exregoffset <= 3)
  1554. return 0;
  1555. o = exregoffset;
  1556. exregoffset--;
  1557. return o;
  1558. }
  1559. if(typefd[t->etype]) {
  1560. if(exfregoffset <= 16)
  1561. return 0;
  1562. o = exfregoffset + NREG;
  1563. exfregoffset--;
  1564. return o;
  1565. }
  1566. return 0;
  1567. }
  1568. schar ewidth[NTYPE] =
  1569. {
  1570. -1, /* [TXXX] */
  1571. SZ_CHAR, /* [TCHAR] */
  1572. SZ_CHAR, /* [TUCHAR] */
  1573. SZ_SHORT, /* [TSHORT] */
  1574. SZ_SHORT, /* [TUSHORT] */
  1575. SZ_INT, /* [TINT] */
  1576. SZ_INT, /* [TUINT] */
  1577. SZ_LONG, /* [TLONG] */
  1578. SZ_LONG, /* [TULONG] */
  1579. SZ_VLONG, /* [TVLONG] */
  1580. SZ_VLONG, /* [TUVLONG] */
  1581. SZ_FLOAT, /* [TFLOAT] */
  1582. SZ_DOUBLE, /* [TDOUBLE] */
  1583. SZ_IND, /* [TIND] */
  1584. 0, /* [TFUNC] */
  1585. -1, /* [TARRAY] */
  1586. 0, /* [TVOID] */
  1587. -1, /* [TSTRUCT] */
  1588. -1, /* [TUNION] */
  1589. SZ_INT, /* [TENUM] */
  1590. };
  1591. long ncast[NTYPE] =
  1592. {
  1593. 0, /* [TXXX] */
  1594. BCHAR|BUCHAR, /* [TCHAR] */
  1595. BCHAR|BUCHAR, /* [TUCHAR] */
  1596. BSHORT|BUSHORT, /* [TSHORT] */
  1597. BSHORT|BUSHORT, /* [TUSHORT] */
  1598. BINT|BUINT|BLONG|BULONG|BIND, /* [TINT] */
  1599. BINT|BUINT|BLONG|BULONG|BIND, /* [TUINT] */
  1600. BINT|BUINT|BLONG|BULONG|BIND, /* [TLONG] */
  1601. BINT|BUINT|BLONG|BULONG|BIND, /* [TULONG] */
  1602. BVLONG|BUVLONG, /* [TVLONG] */
  1603. BVLONG|BUVLONG, /* [TUVLONG] */
  1604. BFLOAT, /* [TFLOAT] */
  1605. BDOUBLE, /* [TDOUBLE] */
  1606. BLONG|BULONG|BIND, /* [TIND] */
  1607. 0, /* [TFUNC] */
  1608. 0, /* [TARRAY] */
  1609. 0, /* [TVOID] */
  1610. BSTRUCT, /* [TSTRUCT] */
  1611. BUNION, /* [TUNION] */
  1612. 0, /* [TENUM] */
  1613. };