txt.c 27 KB

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