txt.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "gc.h"
  10. static int resvreg[nelem(reg)];
  11. void
  12. ginit(void)
  13. {
  14. int i;
  15. Type *t;
  16. thechar = '6';
  17. thestring = "amd64";
  18. exregoffset = REGEXT;
  19. exfregoffset = FREGEXT;
  20. listinit();
  21. nstring = 0;
  22. mnstring = 0;
  23. nrathole = 0;
  24. pc = 0;
  25. breakpc = -1;
  26. continpc = -1;
  27. cases = C;
  28. firstp = P;
  29. lastp = P;
  30. tfield = types[TINT];
  31. typeword = typechlvp;
  32. typeswitch = typechlv;
  33. typecmplx = typesu;
  34. /* TO DO */
  35. memmove(typechlpv, typechlp, sizeof(typechlpv));
  36. typechlpv[TVLONG] = 1;
  37. typechlpv[TUVLONG] = 1;
  38. zprog.link = P;
  39. zprog.as = AGOK;
  40. zprog.from.type = D_NONE;
  41. zprog.from.index = D_NONE;
  42. zprog.from.scale = 0;
  43. zprog.to = zprog.from;
  44. lregnode.op = OREGISTER;
  45. lregnode.class = CEXREG;
  46. lregnode.reg = REGTMP;
  47. lregnode.complex = 0;
  48. lregnode.addable = 11;
  49. lregnode.type = types[TLONG];
  50. qregnode = lregnode;
  51. qregnode.type = types[TVLONG];
  52. constnode.op = OCONST;
  53. constnode.class = CXXX;
  54. constnode.complex = 0;
  55. constnode.addable = 20;
  56. constnode.type = types[TLONG];
  57. vconstnode = constnode;
  58. vconstnode.type = types[TVLONG];
  59. fconstnode.op = OCONST;
  60. fconstnode.class = CXXX;
  61. fconstnode.complex = 0;
  62. fconstnode.addable = 20;
  63. fconstnode.type = types[TDOUBLE];
  64. nodsafe = new(ONAME, Z, Z);
  65. nodsafe->sym = slookup(".safe");
  66. nodsafe->type = types[TINT];
  67. nodsafe->etype = types[TINT]->etype;
  68. nodsafe->class = CAUTO;
  69. complex(nodsafe);
  70. t = typ(TARRAY, types[TCHAR]);
  71. symrathole = slookup(".rathole");
  72. symrathole->class = CGLOBL;
  73. symrathole->type = t;
  74. nodrat = new(ONAME, Z, Z);
  75. nodrat->sym = symrathole;
  76. nodrat->type = types[TIND];
  77. nodrat->etype = TVOID;
  78. nodrat->class = CGLOBL;
  79. complex(nodrat);
  80. nodrat->type = t;
  81. nodret = new(ONAME, Z, Z);
  82. nodret->sym = slookup(".ret");
  83. nodret->type = types[TIND];
  84. nodret->etype = TIND;
  85. nodret->class = CPARAM;
  86. nodret = new(OIND, nodret, Z);
  87. complex(nodret);
  88. if(0)
  89. com64init();
  90. memset(reg, 0, sizeof(reg));
  91. for(i=0; i<nelem(reg); i++) {
  92. reg[i] = 1;
  93. if(i >= D_AX && i <= D_R15 && i != D_SP)
  94. reg[i] = 0;
  95. if(i >= D_X0 && i <= D_X7)
  96. reg[i] = 0;
  97. }
  98. /* keep two external registers */
  99. reg[REGEXT] = 1;
  100. reg[REGEXT-1] = 1;
  101. memmove(resvreg, reg, sizeof(resvreg));
  102. }
  103. void
  104. gclean(void)
  105. {
  106. int i;
  107. Sym *s;
  108. reg[D_SP]--;
  109. for(i=D_AX; i<=D_R15; i++)
  110. if(reg[i] && !resvreg[i])
  111. diag(Z, "reg %R left allocated", i);
  112. for(i=D_X0; i<=D_X7; i++)
  113. if(reg[i] && !resvreg[i])
  114. diag(Z, "reg %R left allocated", i);
  115. while(mnstring)
  116. outstring("", 1L);
  117. symstring->type->width = nstring;
  118. symrathole->type->width = nrathole;
  119. for(i=0; i<NHASH; i++)
  120. for(s = hash[i]; s != S; s = s->link) {
  121. if(s->type == T)
  122. continue;
  123. if(s->type->width == 0)
  124. continue;
  125. if(s->class != CGLOBL && s->class != CSTATIC)
  126. continue;
  127. if(s->type == types[TENUM])
  128. continue;
  129. gpseudo(AGLOBL, s, nodconst(s->type->width));
  130. }
  131. nextpc();
  132. p->as = AEND;
  133. outcode();
  134. }
  135. void
  136. nextpc(void)
  137. {
  138. p = alloc(sizeof(*p));
  139. *p = zprog;
  140. p->lineno = nearln;
  141. pc++;
  142. if(firstp == P) {
  143. firstp = p;
  144. lastp = p;
  145. return;
  146. }
  147. lastp->link = p;
  148. lastp = p;
  149. }
  150. void
  151. gargs(Node *n, Node *tn1, Node *tn2)
  152. {
  153. int32_t regs;
  154. Node fnxargs[20], *fnxp;
  155. regs = cursafe;
  156. fnxp = fnxargs;
  157. garg1(n, tn1, tn2, 0, &fnxp); /* compile fns to temps */
  158. curarg = 0;
  159. fnxp = fnxargs;
  160. garg1(n, tn1, tn2, 1, &fnxp); /* compile normal args and temps */
  161. cursafe = regs;
  162. }
  163. int
  164. nareg(void)
  165. {
  166. int i, n;
  167. n = 0;
  168. for(i=D_AX; i<=D_R15; i++)
  169. if(reg[i] == 0 && !resvreg[i])
  170. n++;
  171. return n;
  172. }
  173. void
  174. garg1(Node *n, Node *tn1, Node *tn2, int f, Node **fnxp)
  175. {
  176. Node nod;
  177. if(n == Z)
  178. return;
  179. if(n->op == OLIST) {
  180. garg1(n->left, tn1, tn2, f, fnxp);
  181. garg1(n->right, tn1, tn2, f, fnxp);
  182. return;
  183. }
  184. if(f == 0) {
  185. if(n->complex >= FNX) {
  186. regsalloc(*fnxp, n);
  187. nod = znode;
  188. nod.op = OAS;
  189. nod.left = *fnxp;
  190. nod.right = n;
  191. nod.type = n->type;
  192. cgen(&nod, Z);
  193. (*fnxp)++;
  194. }
  195. return;
  196. }
  197. if(typesu[n->type->etype]) {
  198. regaalloc(tn2, n);
  199. if(n->complex >= FNX) {
  200. sugen(*fnxp, tn2, n->type->width);
  201. (*fnxp)++;
  202. } else
  203. sugen(n, tn2, n->type->width);
  204. return;
  205. }
  206. if(REGARG && curarg == 0 && typechlpv[n->type->etype]) {
  207. regaalloc1(tn1, n);
  208. if(n->complex >= FNX) {
  209. cgen(*fnxp, tn1);
  210. (*fnxp)++;
  211. } else
  212. cgen(n, tn1);
  213. return;
  214. }
  215. if(vconst(n) == 0) {
  216. regaalloc(tn2, n);
  217. gmove(n, tn2);
  218. return;
  219. }
  220. regalloc(tn1, n, Z);
  221. if(n->complex >= FNX) {
  222. cgen(*fnxp, tn1);
  223. (*fnxp)++;
  224. } else
  225. cgen(n, tn1);
  226. regaalloc(tn2, n);
  227. gmove(tn1, tn2);
  228. regfree(tn1);
  229. }
  230. Node*
  231. nodgconst(int64_t v, Type *t)
  232. {
  233. if(!typev[t->etype])
  234. return nodconst((int32_t)v);
  235. vconstnode.vconst = v;
  236. return &vconstnode;
  237. }
  238. Node*
  239. nodconst(int32_t v)
  240. {
  241. constnode.vconst = v;
  242. return &constnode;
  243. }
  244. Node*
  245. nodfconst(double d)
  246. {
  247. fconstnode.fconst = d;
  248. return &fconstnode;
  249. }
  250. int
  251. isreg(Node *n, int r)
  252. {
  253. if(n->op == OREGISTER)
  254. if(n->reg == r)
  255. return 1;
  256. return 0;
  257. }
  258. int
  259. nodreg(Node *n, Node *nn, int r)
  260. {
  261. int et;
  262. *n = qregnode;
  263. n->reg = r;
  264. if(nn != Z){
  265. et = nn->type->etype;
  266. if(!typefd[et] && nn->type->width <= SZ_LONG && 0)
  267. n->type = typeu[et]? types[TUINT]: types[TINT];
  268. else
  269. n->type = nn->type;
  270. //print("nodreg %s [%s]\n", tnames[et], tnames[n->type->etype]);
  271. n->lineno = nn->lineno;
  272. }
  273. if(reg[r] == 0)
  274. return 0;
  275. if(nn != Z) {
  276. if(nn->op == OREGISTER)
  277. if(nn->reg == r)
  278. return 0;
  279. }
  280. return 1;
  281. }
  282. void
  283. regret(Node *n, Node *nn)
  284. {
  285. int r;
  286. r = REGRET;
  287. if(typefd[nn->type->etype])
  288. r = FREGRET;
  289. nodreg(n, nn, r);
  290. reg[r]++;
  291. }
  292. void
  293. regalloc(Node *n, Node *tn, Node *o)
  294. {
  295. int i;
  296. switch(tn->type->etype) {
  297. case TCHAR:
  298. case TUCHAR:
  299. case TSHORT:
  300. case TUSHORT:
  301. case TINT:
  302. case TUINT:
  303. case TLONG:
  304. case TULONG:
  305. case TVLONG:
  306. case TUVLONG:
  307. case TIND:
  308. if(o != Z && o->op == OREGISTER) {
  309. i = o->reg;
  310. if(i >= D_AX && i <= D_R15)
  311. goto out;
  312. }
  313. for(i=D_AX; i<=D_R15; i++)
  314. if(reg[i] == 0 && !resvreg[i])
  315. goto out;
  316. diag(tn, "out of fixed registers");
  317. goto err;
  318. case TFLOAT:
  319. case TDOUBLE:
  320. if(o != Z && o->op == OREGISTER) {
  321. i = o->reg;
  322. if(i >= D_X0 && i <= D_X7)
  323. goto out;
  324. }
  325. for(i=D_X0; i<=D_X7; i++)
  326. if(reg[i] == 0 && !resvreg[i])
  327. goto out;
  328. diag(tn, "out of float registers");
  329. goto out;
  330. }
  331. diag(tn, "unknown type in regalloc: %T", tn->type);
  332. err:
  333. i = 0;
  334. out:
  335. if(i)
  336. reg[i]++;
  337. nodreg(n, tn, i);
  338. }
  339. void
  340. regialloc(Node *n, Node *tn, Node *o)
  341. {
  342. Node nod;
  343. nod = *tn;
  344. nod.type = types[TIND];
  345. regalloc(n, &nod, o);
  346. }
  347. void
  348. regfree(Node *n)
  349. {
  350. int i;
  351. i = 0;
  352. if(n->op != OREGISTER && n->op != OINDREG)
  353. goto err;
  354. i = n->reg;
  355. if(i < 0 || i >= sizeof(reg))
  356. goto err;
  357. if(reg[i] <= 0)
  358. goto err;
  359. reg[i]--;
  360. return;
  361. err:
  362. diag(n, "error in regfree: %R", i);
  363. }
  364. void
  365. regsalloc(Node *n, Node *nn)
  366. {
  367. cursafe = align(cursafe, nn->type, Aaut3);
  368. maxargsafe = maxround(maxargsafe, cursafe+curarg);
  369. *n = *nodsafe;
  370. n->xoffset = -(stkoff + cursafe);
  371. n->type = nn->type;
  372. n->etype = nn->type->etype;
  373. n->lineno = nn->lineno;
  374. }
  375. void
  376. regaalloc1(Node *n, Node *nn)
  377. {
  378. nodreg(n, nn, REGARG);
  379. reg[REGARG]++;
  380. curarg = align(curarg, nn->type, Aarg1);
  381. curarg = align(curarg, nn->type, Aarg2);
  382. maxargsafe = maxround(maxargsafe, cursafe+curarg);
  383. }
  384. void
  385. regaalloc(Node *n, Node *nn)
  386. {
  387. curarg = align(curarg, nn->type, Aarg1);
  388. *n = *nn;
  389. n->op = OINDREG;
  390. n->reg = REGSP;
  391. n->xoffset = curarg;
  392. n->complex = 0;
  393. n->addable = 20;
  394. curarg = align(curarg, nn->type, Aarg2);
  395. maxargsafe = maxround(maxargsafe, cursafe+curarg);
  396. }
  397. void
  398. regind(Node *n, Node *nn)
  399. {
  400. if(n->op != OREGISTER) {
  401. diag(n, "regind not OREGISTER");
  402. return;
  403. }
  404. n->op = OINDREG;
  405. n->type = nn->type;
  406. }
  407. void
  408. naddr(Node *n, Adr *a)
  409. {
  410. int32_t v;
  411. a->type = D_NONE;
  412. if(n == Z)
  413. return;
  414. switch(n->op) {
  415. default:
  416. bad:
  417. diag(n, "bad in naddr: %O %D", n->op, a);
  418. break;
  419. case OREGISTER:
  420. a->type = n->reg;
  421. a->sym = S;
  422. break;
  423. case OIND:
  424. naddr(n->left, a);
  425. if(a->type >= D_AX && a->type <= D_R15)
  426. a->type += D_INDIR;
  427. else
  428. if(a->type == D_CONST)
  429. a->type = D_NONE+D_INDIR;
  430. else
  431. if(a->type == D_ADDR) {
  432. a->type = a->index;
  433. a->index = D_NONE;
  434. } else
  435. goto bad;
  436. break;
  437. case OINDEX:
  438. a->type = idx.ptr;
  439. if(n->left->op == OADDR || n->left->op == OCONST)
  440. naddr(n->left, a);
  441. if(a->type >= D_AX && a->type <= D_R15)
  442. a->type += D_INDIR;
  443. else
  444. if(a->type == D_CONST)
  445. a->type = D_NONE+D_INDIR;
  446. else
  447. if(a->type == D_ADDR) {
  448. a->type = a->index;
  449. a->index = D_NONE;
  450. } else
  451. goto bad;
  452. a->index = idx.reg;
  453. a->scale = n->scale;
  454. a->offset += n->xoffset;
  455. break;
  456. case OINDREG:
  457. a->type = n->reg+D_INDIR;
  458. a->sym = S;
  459. a->offset = n->xoffset;
  460. break;
  461. case ONAME:
  462. a->etype = n->etype;
  463. a->type = D_STATIC;
  464. a->sym = n->sym;
  465. a->offset = n->xoffset;
  466. if(n->class == CSTATIC)
  467. break;
  468. if(n->class == CEXTERN || n->class == CGLOBL) {
  469. a->type = D_EXTERN;
  470. break;
  471. }
  472. if(n->class == CAUTO) {
  473. a->type = D_AUTO;
  474. break;
  475. }
  476. if(n->class == CPARAM) {
  477. a->type = D_PARAM;
  478. break;
  479. }
  480. goto bad;
  481. case OCONST:
  482. if(typefd[n->type->etype]) {
  483. a->type = D_FCONST;
  484. a->dval = n->fconst;
  485. break;
  486. }
  487. a->sym = S;
  488. a->type = D_CONST;
  489. if(typev[n->type->etype] || n->type->etype == TIND)
  490. a->offset = n->vconst;
  491. else
  492. a->offset = convvtox(n->vconst, typeu[n->type->etype]? TULONG: TLONG);
  493. break;
  494. case OADDR:
  495. naddr(n->left, a);
  496. if(a->type >= D_INDIR) {
  497. a->type -= D_INDIR;
  498. break;
  499. }
  500. if(a->type == D_EXTERN || a->type == D_STATIC ||
  501. a->type == D_AUTO || a->type == D_PARAM)
  502. if(a->index == D_NONE) {
  503. a->index = a->type;
  504. a->type = D_ADDR;
  505. break;
  506. }
  507. goto bad;
  508. case OADD:
  509. if(n->right->op == OCONST) {
  510. v = n->right->vconst;
  511. naddr(n->left, a);
  512. } else
  513. if(n->left->op == OCONST) {
  514. v = n->left->vconst;
  515. naddr(n->right, a);
  516. } else
  517. goto bad;
  518. a->offset += v;
  519. break;
  520. }
  521. }
  522. void
  523. gcmp(int op, Node *n, int64_t val)
  524. {
  525. Node *cn, nod;
  526. cn = nodgconst(val, n->type);
  527. if(!immconst(cn)){
  528. regalloc(&nod, n, Z);
  529. gmove(cn, &nod);
  530. gopcode(op, n->type, n, &nod);
  531. regfree(&nod);
  532. }else
  533. gopcode(op, n->type, n, cn);
  534. }
  535. #define CASE(a,b) ((a<<8)|(b<<0))
  536. void
  537. gmove(Node *f, Node *t)
  538. {
  539. int ft, tt, t64, a;
  540. Node nod, nod1, nod2, nod3;
  541. Prog *p1, *p2;
  542. ft = f->type->etype;
  543. tt = t->type->etype;
  544. t64 = tt == TVLONG || tt == TUVLONG || tt == TIND;
  545. if(debug['M'])
  546. print("gop: %O %O[%s],%O[%s]\n", OAS,
  547. f->op, tnames[ft], t->op, tnames[tt]);
  548. if(typefd[ft] && f->op == OCONST) {
  549. /* TO DO: pick up special constants, possibly preloaded */
  550. if(f->fconst == 0.0){
  551. regalloc(&nod, t, t);
  552. gins(AXORPD, &nod, &nod);
  553. gmove(&nod, t);
  554. regfree(&nod);
  555. return;
  556. }
  557. }
  558. /*
  559. * load
  560. */
  561. if(f->op == ONAME || f->op == OINDREG ||
  562. f->op == OIND || f->op == OINDEX)
  563. switch(ft) {
  564. case TCHAR:
  565. a = AMOVBLSX;
  566. if(t64)
  567. a = AMOVBQSX;
  568. goto ld;
  569. case TUCHAR:
  570. a = AMOVBLZX;
  571. if(t64)
  572. a = AMOVBQZX;
  573. goto ld;
  574. case TSHORT:
  575. a = AMOVWLSX;
  576. if(t64)
  577. a = AMOVWQSX;
  578. goto ld;
  579. case TUSHORT:
  580. a = AMOVWLZX;
  581. if(t64)
  582. a = AMOVWQZX;
  583. goto ld;
  584. case TINT:
  585. case TLONG:
  586. if(typefd[tt]) {
  587. regalloc(&nod, t, t);
  588. if(tt == TDOUBLE)
  589. a = ACVTSL2SD;
  590. else
  591. a = ACVTSL2SS;
  592. gins(a, f, &nod);
  593. gmove(&nod, t);
  594. regfree(&nod);
  595. return;
  596. }
  597. a = AMOVL;
  598. if(t64)
  599. a = AMOVLQSX;
  600. goto ld;
  601. case TUINT:
  602. case TULONG:
  603. a = AMOVL;
  604. if(t64)
  605. a = AMOVLQZX; /* could probably use plain MOVL */
  606. goto ld;
  607. case TVLONG:
  608. if(typefd[tt]) {
  609. regalloc(&nod, t, t);
  610. if(tt == TDOUBLE)
  611. a = ACVTSQ2SD;
  612. else
  613. a = ACVTSQ2SS;
  614. gins(a, f, &nod);
  615. gmove(&nod, t);
  616. regfree(&nod);
  617. return;
  618. }
  619. case TUVLONG:
  620. a = AMOVQ;
  621. goto ld;
  622. case TIND:
  623. a = AMOVQ;
  624. ld:
  625. regalloc(&nod, f, t);
  626. nod.type = t64? types[TVLONG]: types[TINT];
  627. gins(a, f, &nod);
  628. gmove(&nod, t);
  629. regfree(&nod);
  630. return;
  631. case TFLOAT:
  632. a = AMOVSS;
  633. goto fld;
  634. case TDOUBLE:
  635. a = AMOVSD;
  636. fld:
  637. regalloc(&nod, f, t);
  638. if(tt != TDOUBLE && tt != TFLOAT){ /* TO DO: why is this here */
  639. prtree(f, "odd tree");
  640. nod.type = t64? types[TVLONG]: types[TINT];
  641. }
  642. gins(a, f, &nod);
  643. gmove(&nod, t);
  644. regfree(&nod);
  645. return;
  646. }
  647. /*
  648. * store
  649. */
  650. if(t->op == ONAME || t->op == OINDREG ||
  651. t->op == OIND || t->op == OINDEX)
  652. switch(tt) {
  653. case TCHAR:
  654. case TUCHAR:
  655. a = AMOVB; goto st;
  656. case TSHORT:
  657. case TUSHORT:
  658. a = AMOVW; goto st;
  659. case TINT:
  660. case TUINT:
  661. case TLONG:
  662. case TULONG:
  663. a = AMOVL; goto st;
  664. case TVLONG:
  665. case TUVLONG:
  666. case TIND:
  667. a = AMOVQ; goto st;
  668. st:
  669. if(f->op == OCONST) {
  670. gins(a, f, t);
  671. return;
  672. }
  673. fst:
  674. regalloc(&nod, t, f);
  675. gmove(f, &nod);
  676. gins(a, &nod, t);
  677. regfree(&nod);
  678. return;
  679. case TFLOAT:
  680. a = AMOVSS;
  681. goto fst;
  682. case TDOUBLE:
  683. a = AMOVSD;
  684. goto fst;
  685. }
  686. /*
  687. * convert
  688. */
  689. switch(CASE(ft,tt)) {
  690. default:
  691. /*
  692. * integer to integer
  693. ********
  694. a = AGOK; break;
  695. case CASE( TCHAR, TCHAR):
  696. case CASE( TUCHAR, TCHAR):
  697. case CASE( TSHORT, TCHAR):
  698. case CASE( TUSHORT,TCHAR):
  699. case CASE( TINT, TCHAR):
  700. case CASE( TUINT, TCHAR):
  701. case CASE( TLONG, TCHAR):
  702. case CASE( TULONG, TCHAR):
  703. case CASE( TIND, TCHAR):
  704. case CASE( TCHAR, TUCHAR):
  705. case CASE( TUCHAR, TUCHAR):
  706. case CASE( TSHORT, TUCHAR):
  707. case CASE( TUSHORT,TUCHAR):
  708. case CASE( TINT, TUCHAR):
  709. case CASE( TUINT, TUCHAR):
  710. case CASE( TLONG, TUCHAR):
  711. case CASE( TULONG, TUCHAR):
  712. case CASE( TIND, TUCHAR):
  713. case CASE( TSHORT, TSHORT):
  714. case CASE( TUSHORT,TSHORT):
  715. case CASE( TINT, TSHORT):
  716. case CASE( TUINT, TSHORT):
  717. case CASE( TLONG, TSHORT):
  718. case CASE( TULONG, TSHORT):
  719. case CASE( TIND, TSHORT):
  720. case CASE( TSHORT, TUSHORT):
  721. case CASE( TUSHORT,TUSHORT):
  722. case CASE( TINT, TUSHORT):
  723. case CASE( TUINT, TUSHORT):
  724. case CASE( TLONG, TUSHORT):
  725. case CASE( TULONG, TUSHORT):
  726. case CASE( TIND, TUSHORT):
  727. case CASE( TINT, TINT):
  728. case CASE( TUINT, TINT):
  729. case CASE( TLONG, TINT):
  730. case CASE( TULONG, TINT):
  731. case CASE( TIND, TINT):
  732. case CASE( TINT, TUINT):
  733. case CASE( TUINT, TUINT):
  734. case CASE( TLONG, TUINT):
  735. case CASE( TULONG, TUINT):
  736. case CASE( TIND, TUINT):
  737. case CASE( TUINT, TIND):
  738. case CASE( TVLONG, TUINT):
  739. case CASE( TVLONG, TULONG):
  740. case CASE( TUVLONG, TUINT):
  741. case CASE( TUVLONG, TULONG):
  742. *****/
  743. a = AMOVL;
  744. break;
  745. case CASE( TVLONG, TCHAR):
  746. case CASE( TVLONG, TSHORT):
  747. case CASE( TVLONG, TINT):
  748. case CASE( TVLONG, TLONG):
  749. case CASE( TUVLONG, TCHAR):
  750. case CASE( TUVLONG, TSHORT):
  751. case CASE( TUVLONG, TINT):
  752. case CASE( TUVLONG, TLONG):
  753. case CASE( TINT, TVLONG):
  754. case CASE( TINT, TUVLONG):
  755. case CASE( TLONG, TVLONG):
  756. case CASE( TINT, TIND):
  757. case CASE( TLONG, TIND):
  758. a = AMOVLQSX;
  759. if(f->op == OCONST) {
  760. f->vconst &= (uint64_t)0xffffffffU;
  761. if(f->vconst & 0x80000000)
  762. f->vconst |= (int64_t)0xffffffff << 32;
  763. a = AMOVQ;
  764. }
  765. break;
  766. case CASE( TUINT, TIND):
  767. case CASE( TUINT, TVLONG):
  768. case CASE( TUINT, TUVLONG):
  769. case CASE( TULONG, TVLONG):
  770. case CASE( TULONG, TUVLONG):
  771. case CASE( TULONG, TIND):
  772. a = AMOVL; /* same effect as AMOVLQZX */
  773. if(f->op == OCONST) {
  774. f->vconst &= (uint64_t)0xffffffffU;
  775. a = AMOVQ;
  776. }
  777. break;
  778. case CASE( TIND, TVLONG):
  779. case CASE( TVLONG, TVLONG):
  780. case CASE( TUVLONG, TVLONG):
  781. case CASE( TVLONG, TUVLONG):
  782. case CASE( TUVLONG, TUVLONG):
  783. case CASE( TIND, TUVLONG):
  784. case CASE( TVLONG, TIND):
  785. case CASE( TUVLONG, TIND):
  786. case CASE( TIND, TIND):
  787. a = AMOVQ;
  788. break;
  789. case CASE( TSHORT, TINT):
  790. case CASE( TSHORT, TUINT):
  791. case CASE( TSHORT, TLONG):
  792. case CASE( TSHORT, TULONG):
  793. a = AMOVWLSX;
  794. if(f->op == OCONST) {
  795. f->vconst &= 0xffff;
  796. if(f->vconst & 0x8000)
  797. f->vconst |= 0xffff0000;
  798. a = AMOVL;
  799. }
  800. break;
  801. case CASE( TSHORT, TVLONG):
  802. case CASE( TSHORT, TUVLONG):
  803. case CASE( TSHORT, TIND):
  804. a = AMOVWQSX;
  805. if(f->op == OCONST) {
  806. f->vconst &= 0xffff;
  807. if(f->vconst & 0x8000){
  808. f->vconst |= 0xffff0000;
  809. f->vconst |= (int64_t)~0 << 32;
  810. }
  811. a = AMOVL;
  812. }
  813. break;
  814. case CASE( TUSHORT,TINT):
  815. case CASE( TUSHORT,TUINT):
  816. case CASE( TUSHORT,TLONG):
  817. case CASE( TUSHORT,TULONG):
  818. a = AMOVWLZX;
  819. if(f->op == OCONST) {
  820. f->vconst &= 0xffff;
  821. a = AMOVL;
  822. }
  823. break;
  824. case CASE( TUSHORT,TVLONG):
  825. case CASE( TUSHORT,TUVLONG):
  826. case CASE( TUSHORT,TIND):
  827. a = AMOVWQZX;
  828. if(f->op == OCONST) {
  829. f->vconst &= 0xffff;
  830. a = AMOVL; /* MOVL also zero-extends to 64 bits */
  831. }
  832. break;
  833. case CASE( TCHAR, TSHORT):
  834. case CASE( TCHAR, TUSHORT):
  835. case CASE( TCHAR, TINT):
  836. case CASE( TCHAR, TUINT):
  837. case CASE( TCHAR, TLONG):
  838. case CASE( TCHAR, TULONG):
  839. a = AMOVBLSX;
  840. if(f->op == OCONST) {
  841. f->vconst &= 0xff;
  842. if(f->vconst & 0x80)
  843. f->vconst |= 0xffffff00;
  844. a = AMOVL;
  845. }
  846. break;
  847. case CASE( TCHAR, TVLONG):
  848. case CASE( TCHAR, TUVLONG):
  849. case CASE( TCHAR, TIND):
  850. a = AMOVBQSX;
  851. if(f->op == OCONST) {
  852. f->vconst &= 0xff;
  853. if(f->vconst & 0x80){
  854. f->vconst |= 0xffffff00;
  855. f->vconst |= (int64_t)~0 << 32;
  856. }
  857. a = AMOVQ;
  858. }
  859. break;
  860. case CASE( TUCHAR, TSHORT):
  861. case CASE( TUCHAR, TUSHORT):
  862. case CASE( TUCHAR, TINT):
  863. case CASE( TUCHAR, TUINT):
  864. case CASE( TUCHAR, TLONG):
  865. case CASE( TUCHAR, TULONG):
  866. a = AMOVBLZX;
  867. if(f->op == OCONST) {
  868. f->vconst &= 0xff;
  869. a = AMOVL;
  870. }
  871. break;
  872. case CASE( TUCHAR, TVLONG):
  873. case CASE( TUCHAR, TUVLONG):
  874. case CASE( TUCHAR, TIND):
  875. a = AMOVBQZX;
  876. if(f->op == OCONST) {
  877. f->vconst &= 0xff;
  878. a = AMOVL; /* zero-extends to 64-bits */
  879. }
  880. break;
  881. /*
  882. * float to fix
  883. */
  884. case CASE( TFLOAT, TCHAR):
  885. case CASE( TFLOAT, TUCHAR):
  886. case CASE( TFLOAT, TSHORT):
  887. case CASE( TFLOAT, TUSHORT):
  888. case CASE( TFLOAT, TINT):
  889. case CASE( TFLOAT, TUINT):
  890. case CASE( TFLOAT, TLONG):
  891. case CASE( TFLOAT, TULONG):
  892. case CASE( TFLOAT, TVLONG):
  893. case CASE( TFLOAT, TUVLONG):
  894. case CASE( TFLOAT, TIND):
  895. case CASE( TDOUBLE,TCHAR):
  896. case CASE( TDOUBLE,TUCHAR):
  897. case CASE( TDOUBLE,TSHORT):
  898. case CASE( TDOUBLE,TUSHORT):
  899. case CASE( TDOUBLE,TINT):
  900. case CASE( TDOUBLE,TUINT):
  901. case CASE( TDOUBLE,TLONG):
  902. case CASE( TDOUBLE,TULONG):
  903. case CASE( TDOUBLE,TVLONG):
  904. case CASE( TDOUBLE,TUVLONG):
  905. case CASE( TDOUBLE,TIND):
  906. regalloc(&nod, t, Z);
  907. if(ewidth[tt] == SZ_VLONG || typeu[tt] && ewidth[tt] == SZ_INT){
  908. if(ft == TFLOAT)
  909. a = ACVTTSS2SQ;
  910. else
  911. a = ACVTTSD2SQ;
  912. }else{
  913. if(ft == TFLOAT)
  914. a = ACVTTSS2SL;
  915. else
  916. a = ACVTTSD2SL;
  917. }
  918. gins(a, f, &nod);
  919. gmove(&nod, t);
  920. regfree(&nod);
  921. return;
  922. /*
  923. * ulong to float
  924. */
  925. case CASE( TUVLONG, TDOUBLE):
  926. case CASE( TUVLONG, TFLOAT):
  927. a = ACVTSQ2SS;
  928. if(tt == TDOUBLE)
  929. a = ACVTSQ2SD;
  930. regalloc(&nod, f, f);
  931. gmove(f, &nod);
  932. regalloc(&nod1, t, t);
  933. gins(ACMPQ, &nod, nodconst(0));
  934. gins(AJLT, Z, Z);
  935. p1 = p;
  936. gins(a, &nod, &nod1);
  937. gins(AJMP, Z, Z);
  938. p2 = p;
  939. patch(p1, pc);
  940. regalloc(&nod2, f, Z);
  941. regalloc(&nod3, f, Z);
  942. gmove(&nod, &nod2);
  943. gins(ASHRQ, nodconst(1), &nod2);
  944. gmove(&nod, &nod3);
  945. gins(AANDL, nodconst(1), &nod3);
  946. gins(AORQ, &nod3, &nod2);
  947. gins(a, &nod2, &nod1);
  948. gins(tt == TDOUBLE? AADDSD: AADDSS, &nod1, &nod1);
  949. regfree(&nod2);
  950. regfree(&nod3);
  951. patch(p2, pc);
  952. regfree(&nod);
  953. regfree(&nod1);
  954. return;
  955. case CASE( TULONG, TDOUBLE):
  956. case CASE( TUINT, TDOUBLE):
  957. case CASE( TULONG, TFLOAT):
  958. case CASE( TUINT, TFLOAT):
  959. a = ACVTSQ2SS;
  960. if(tt == TDOUBLE)
  961. a = ACVTSQ2SD;
  962. regalloc(&nod, f, f);
  963. gins(AMOVLQZX, f, &nod);
  964. regalloc(&nod1, t, t);
  965. gins(a, &nod, &nod1);
  966. gmove(&nod1, t);
  967. regfree(&nod);
  968. regfree(&nod1);
  969. return;
  970. /*
  971. * fix to float
  972. */
  973. case CASE( TCHAR, TFLOAT):
  974. case CASE( TUCHAR, TFLOAT):
  975. case CASE( TSHORT, TFLOAT):
  976. case CASE( TUSHORT,TFLOAT):
  977. case CASE( TINT, TFLOAT):
  978. case CASE( TLONG, TFLOAT):
  979. case CASE( TVLONG, TFLOAT):
  980. case CASE( TIND, TFLOAT):
  981. case CASE( TCHAR, TDOUBLE):
  982. case CASE( TUCHAR, TDOUBLE):
  983. case CASE( TSHORT, TDOUBLE):
  984. case CASE( TUSHORT,TDOUBLE):
  985. case CASE( TINT, TDOUBLE):
  986. case CASE( TLONG, TDOUBLE):
  987. case CASE( TVLONG, TDOUBLE):
  988. case CASE( TIND, TDOUBLE):
  989. regalloc(&nod, t, t);
  990. if(ewidth[ft] == SZ_VLONG){
  991. if(tt == TFLOAT)
  992. a = ACVTSQ2SS;
  993. else
  994. a = ACVTSQ2SD;
  995. }else{
  996. if(tt == TFLOAT)
  997. a = ACVTSL2SS;
  998. else
  999. a = ACVTSL2SD;
  1000. }
  1001. gins(a, f, &nod);
  1002. gmove(&nod, t);
  1003. regfree(&nod);
  1004. return;
  1005. /*
  1006. * float to float
  1007. */
  1008. case CASE( TFLOAT, TFLOAT):
  1009. a = AMOVSS;
  1010. break;
  1011. case CASE( TDOUBLE,TFLOAT):
  1012. a = ACVTSD2SS;
  1013. break;
  1014. case CASE( TFLOAT, TDOUBLE):
  1015. a = ACVTSS2SD;
  1016. break;
  1017. case CASE( TDOUBLE,TDOUBLE):
  1018. a = AMOVSD;
  1019. break;
  1020. }
  1021. if(a == AMOVQ || a == AMOVSD || a == AMOVSS || a == AMOVL && ewidth[ft] == ewidth[tt]) /* TO DO: check AMOVL */
  1022. if(samaddr(f, t))
  1023. return;
  1024. gins(a, f, t);
  1025. }
  1026. void
  1027. doindex(Node *n)
  1028. {
  1029. Node nod, nod1;
  1030. int32_t v;
  1031. if(debug['Y'])
  1032. prtree(n, "index");
  1033. if(n->left->complex >= FNX)
  1034. print("botch in doindex\n");
  1035. regalloc(&nod, &qregnode, Z);
  1036. v = constnode.vconst;
  1037. cgen(n->right, &nod);
  1038. idx.ptr = D_NONE;
  1039. if(n->left->op == OCONST)
  1040. idx.ptr = D_CONST;
  1041. else if(n->left->op == OREGISTER)
  1042. idx.ptr = n->left->reg;
  1043. else if(n->left->op != OADDR) {
  1044. reg[D_BP]++; // cant be used as a base
  1045. regalloc(&nod1, &qregnode, Z);
  1046. cgen(n->left, &nod1);
  1047. idx.ptr = nod1.reg;
  1048. regfree(&nod1);
  1049. reg[D_BP]--;
  1050. }
  1051. idx.reg = nod.reg;
  1052. regfree(&nod);
  1053. constnode.vconst = v;
  1054. }
  1055. void
  1056. gins(int a, Node *f, Node *t)
  1057. {
  1058. if(f != Z && f->op == OINDEX)
  1059. doindex(f);
  1060. if(t != Z && t->op == OINDEX)
  1061. doindex(t);
  1062. nextpc();
  1063. p->as = a;
  1064. if(f != Z)
  1065. naddr(f, &p->from);
  1066. if(t != Z)
  1067. naddr(t, &p->to);
  1068. if(debug['g'])
  1069. print("%P\n", p);
  1070. }
  1071. void
  1072. gopcode(int o, Type *ty, Node *f, Node *t)
  1073. {
  1074. int a, et;
  1075. et = TLONG;
  1076. if(ty != T)
  1077. et = ty->etype;
  1078. if(debug['M']) {
  1079. if(f != Z && f->type != T)
  1080. print("gop: %O %O[%s],", o, f->op, tnames[et]);
  1081. else
  1082. print("gop: %O Z,", o);
  1083. if(t != Z && t->type != T)
  1084. print("%O[%s]\n", t->op, tnames[t->type->etype]);
  1085. else
  1086. print("Z\n");
  1087. }
  1088. a = AGOK;
  1089. switch(o) {
  1090. case OCOM:
  1091. a = ANOTL;
  1092. if(et == TCHAR || et == TUCHAR)
  1093. a = ANOTB;
  1094. if(et == TSHORT || et == TUSHORT)
  1095. a = ANOTW;
  1096. if(et == TVLONG || et == TUVLONG || et == TIND)
  1097. a = ANOTQ;
  1098. break;
  1099. case ONEG:
  1100. a = ANEGL;
  1101. if(et == TCHAR || et == TUCHAR)
  1102. a = ANEGB;
  1103. if(et == TSHORT || et == TUSHORT)
  1104. a = ANEGW;
  1105. if(et == TVLONG || et == TUVLONG || et == TIND)
  1106. a = ANEGQ;
  1107. break;
  1108. case OADDR:
  1109. a = ALEAQ;
  1110. break;
  1111. case OASADD:
  1112. case OADD:
  1113. a = AADDL;
  1114. if(et == TCHAR || et == TUCHAR)
  1115. a = AADDB;
  1116. if(et == TSHORT || et == TUSHORT)
  1117. a = AADDW;
  1118. if(et == TVLONG || et == TUVLONG || et == TIND)
  1119. a = AADDQ;
  1120. if(et == TFLOAT)
  1121. a = AADDSS;
  1122. if(et == TDOUBLE)
  1123. a = AADDSD;
  1124. break;
  1125. case OASSUB:
  1126. case OSUB:
  1127. a = ASUBL;
  1128. if(et == TCHAR || et == TUCHAR)
  1129. a = ASUBB;
  1130. if(et == TSHORT || et == TUSHORT)
  1131. a = ASUBW;
  1132. if(et == TVLONG || et == TUVLONG || et == TIND)
  1133. a = ASUBQ;
  1134. if(et == TFLOAT)
  1135. a = ASUBSS;
  1136. if(et == TDOUBLE)
  1137. a = ASUBSD;
  1138. break;
  1139. case OASOR:
  1140. case OOR:
  1141. a = AORL;
  1142. if(et == TCHAR || et == TUCHAR)
  1143. a = AORB;
  1144. if(et == TSHORT || et == TUSHORT)
  1145. a = AORW;
  1146. if(et == TVLONG || et == TUVLONG || et == TIND)
  1147. a = AORQ;
  1148. break;
  1149. case OASAND:
  1150. case OAND:
  1151. a = AANDL;
  1152. if(et == TCHAR || et == TUCHAR)
  1153. a = AANDB;
  1154. if(et == TSHORT || et == TUSHORT)
  1155. a = AANDW;
  1156. if(et == TVLONG || et == TUVLONG || et == TIND)
  1157. a = AANDQ;
  1158. break;
  1159. case OASXOR:
  1160. case OXOR:
  1161. a = AXORL;
  1162. if(et == TCHAR || et == TUCHAR)
  1163. a = AXORB;
  1164. if(et == TSHORT || et == TUSHORT)
  1165. a = AXORW;
  1166. if(et == TVLONG || et == TUVLONG || et == TIND)
  1167. a = AXORQ;
  1168. break;
  1169. case OASLSHR:
  1170. case OLSHR:
  1171. a = ASHRL;
  1172. if(et == TCHAR || et == TUCHAR)
  1173. a = ASHRB;
  1174. if(et == TSHORT || et == TUSHORT)
  1175. a = ASHRW;
  1176. if(et == TVLONG || et == TUVLONG || et == TIND)
  1177. a = ASHRQ;
  1178. break;
  1179. case OASASHR:
  1180. case OASHR:
  1181. a = ASARL;
  1182. if(et == TCHAR || et == TUCHAR)
  1183. a = ASARB;
  1184. if(et == TSHORT || et == TUSHORT)
  1185. a = ASARW;
  1186. if(et == TVLONG || et == TUVLONG || et == TIND)
  1187. a = ASARQ;
  1188. break;
  1189. case OASASHL:
  1190. case OASHL:
  1191. a = ASALL;
  1192. if(et == TCHAR || et == TUCHAR)
  1193. a = ASALB;
  1194. if(et == TSHORT || et == TUSHORT)
  1195. a = ASALW;
  1196. if(et == TVLONG || et == TUVLONG || et == TIND)
  1197. a = ASALQ;
  1198. break;
  1199. case OFUNC:
  1200. a = ACALL;
  1201. break;
  1202. case OASMUL:
  1203. case OMUL:
  1204. if(f->op == OREGISTER && t != Z && isreg(t, D_AX) && reg[D_DX] == 0)
  1205. t = Z;
  1206. a = AIMULL;
  1207. if(et == TVLONG || et == TUVLONG || et == TIND)
  1208. a = AIMULQ;
  1209. if(et == TFLOAT)
  1210. a = AMULSS;
  1211. if(et == TDOUBLE)
  1212. a = AMULSD;
  1213. break;
  1214. case OASMOD:
  1215. case OMOD:
  1216. case OASDIV:
  1217. case ODIV:
  1218. a = AIDIVL;
  1219. if(et == TVLONG || et == TUVLONG || et == TIND)
  1220. a = AIDIVQ;
  1221. if(et == TFLOAT)
  1222. a = ADIVSS;
  1223. if(et == TDOUBLE)
  1224. a = ADIVSD;
  1225. break;
  1226. case OASLMUL:
  1227. case OLMUL:
  1228. a = AMULL;
  1229. if(et == TVLONG || et == TUVLONG || et == TIND)
  1230. a = AMULQ;
  1231. break;
  1232. case OASLMOD:
  1233. case OLMOD:
  1234. case OASLDIV:
  1235. case OLDIV:
  1236. a = ADIVL;
  1237. if(et == TVLONG || et == TUVLONG || et == TIND)
  1238. a = ADIVQ;
  1239. break;
  1240. case OEQ:
  1241. case ONE:
  1242. case OLT:
  1243. case OLE:
  1244. case OGE:
  1245. case OGT:
  1246. case OLO:
  1247. case OLS:
  1248. case OHS:
  1249. case OHI:
  1250. a = ACMPL;
  1251. if(et == TCHAR || et == TUCHAR)
  1252. a = ACMPB;
  1253. if(et == TSHORT || et == TUSHORT)
  1254. a = ACMPW;
  1255. if(et == TVLONG || et == TUVLONG || et == TIND)
  1256. a = ACMPQ;
  1257. if(et == TFLOAT)
  1258. a = AUCOMISS;
  1259. if(et == TDOUBLE)
  1260. a = AUCOMISD;
  1261. gins(a, f, t);
  1262. switch(o) {
  1263. case OEQ: a = AJEQ; break;
  1264. case ONE: a = AJNE; break;
  1265. case OLT: a = AJLT; break;
  1266. case OLE: a = AJLE; break;
  1267. case OGE: a = AJGE; break;
  1268. case OGT: a = AJGT; break;
  1269. case OLO: a = AJCS; break;
  1270. case OLS: a = AJLS; break;
  1271. case OHS: a = AJCC; break;
  1272. case OHI: a = AJHI; break;
  1273. }
  1274. gins(a, Z, Z);
  1275. return;
  1276. }
  1277. if(a == AGOK)
  1278. diag(Z, "bad in gopcode %O", o);
  1279. gins(a, f, t);
  1280. }
  1281. int
  1282. samaddr(Node *f, Node *t)
  1283. {
  1284. return f->op == OREGISTER && t->op == OREGISTER && f->reg == t->reg;
  1285. }
  1286. void
  1287. gbranch(int o)
  1288. {
  1289. int a;
  1290. a = AGOK;
  1291. switch(o) {
  1292. case ORETURN:
  1293. a = ARET;
  1294. break;
  1295. case OGOTO:
  1296. a = AJMP;
  1297. break;
  1298. }
  1299. nextpc();
  1300. if(a == AGOK) {
  1301. diag(Z, "bad in gbranch %O", o);
  1302. nextpc();
  1303. }
  1304. p->as = a;
  1305. }
  1306. void
  1307. patch(Prog *op, int32_t pc)
  1308. {
  1309. op->to.offset = pc;
  1310. op->to.type = D_BRANCH;
  1311. }
  1312. void
  1313. gpseudo(int a, Sym *s, Node *n)
  1314. {
  1315. nextpc();
  1316. p->as = a;
  1317. p->from.type = D_EXTERN;
  1318. p->from.sym = s;
  1319. p->from.scale = (profileflg ? 0 : NOPROF);
  1320. if(s->class == CSTATIC)
  1321. p->from.type = D_STATIC;
  1322. naddr(n, &p->to);
  1323. if(a == ADATA || a == AGLOBL)
  1324. pc--;
  1325. }
  1326. int
  1327. sconst(Node *n)
  1328. {
  1329. int32_t v;
  1330. if(n->op == OCONST && !typefd[n->type->etype]) {
  1331. v = n->vconst;
  1332. if(v >= -32766L && v < 32766L)
  1333. return 1;
  1334. }
  1335. return 0;
  1336. }
  1337. int32_t
  1338. exreg(Type *t)
  1339. {
  1340. int32_t o;
  1341. if(typechlpv[t->etype]) {
  1342. if(exregoffset <= REGEXT-4)
  1343. return 0;
  1344. o = exregoffset;
  1345. exregoffset--;
  1346. return o;
  1347. }
  1348. return 0;
  1349. }
  1350. schar ewidth[NTYPE] =
  1351. {
  1352. -1, /*[TXXX]*/
  1353. SZ_CHAR, /*[TCHAR]*/
  1354. SZ_CHAR, /*[TUCHAR]*/
  1355. SZ_SHORT, /*[TSHORT]*/
  1356. SZ_SHORT, /*[TUSHORT]*/
  1357. SZ_INT, /*[TINT]*/
  1358. SZ_INT, /*[TUINT]*/
  1359. SZ_LONG, /*[TLONG]*/
  1360. SZ_LONG, /*[TULONG]*/
  1361. SZ_VLONG, /*[TVLONG]*/
  1362. SZ_VLONG, /*[TUVLONG]*/
  1363. SZ_FLOAT, /*[TFLOAT]*/
  1364. SZ_DOUBLE, /*[TDOUBLE]*/
  1365. SZ_IND, /*[TIND]*/
  1366. 0, /*[TFUNC]*/
  1367. -1, /*[TARRAY]*/
  1368. 0, /*[TVOID]*/
  1369. -1, /*[TSTRUCT]*/
  1370. -1, /*[TUNION]*/
  1371. SZ_INT, /*[TENUM]*/
  1372. };
  1373. int32_t ncast[NTYPE] =
  1374. {
  1375. 0, /*[TXXX]*/
  1376. BCHAR|BUCHAR, /*[TCHAR]*/
  1377. BCHAR|BUCHAR, /*[TUCHAR]*/
  1378. BSHORT|BUSHORT, /*[TSHORT]*/
  1379. BSHORT|BUSHORT, /*[TUSHORT]*/
  1380. BINT|BUINT|BLONG|BULONG, /*[TINT]*/
  1381. BINT|BUINT|BLONG|BULONG, /*[TUINT]*/
  1382. BINT|BUINT|BLONG|BULONG, /*[TLONG]*/
  1383. BINT|BUINT|BLONG|BULONG, /*[TULONG]*/
  1384. BVLONG|BUVLONG|BIND, /*[TVLONG]*/
  1385. BVLONG|BUVLONG|BIND, /*[TUVLONG]*/
  1386. BFLOAT, /*[TFLOAT]*/
  1387. BDOUBLE, /*[TDOUBLE]*/
  1388. BVLONG|BUVLONG|BIND, /*[TIND]*/
  1389. 0, /*[TFUNC]*/
  1390. 0, /*[TARRAY]*/
  1391. 0, /*[TVOID]*/
  1392. BSTRUCT, /*[TSTRUCT]*/
  1393. BUNION, /*[TUNION]*/
  1394. 0, /*[TENUM]*/
  1395. };