txt.c 24 KB

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