txt.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  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, TLONG):
  713. case CASE( TFLOAT, TIND):
  714. case CASE( TDOUBLE,TCHAR):
  715. case CASE( TDOUBLE,TUCHAR):
  716. case CASE( TDOUBLE,TSHORT):
  717. case CASE( TDOUBLE,TUSHORT):
  718. case CASE( TDOUBLE,TINT):
  719. case CASE( TDOUBLE,TLONG):
  720. case CASE( TDOUBLE,TIND):
  721. if(fproundflg) {
  722. regsalloc(&nod, &regnode);
  723. gins(AFMOVLP, f, &nod);
  724. gmove(&nod, t);
  725. return;
  726. }
  727. regsalloc(&nod, &regnode);
  728. regsalloc(&nod1, &regnode);
  729. gins(AFSTCW, Z, &nod1);
  730. nod1.xoffset += 2;
  731. gins(AMOVW, nodconst(0xf7f), &nod1);
  732. gins(AFLDCW, &nod1, Z);
  733. gins(AFMOVLP, f, &nod);
  734. nod1.xoffset -= 2;
  735. gins(AFLDCW, &nod1, Z);
  736. gmove(&nod, t);
  737. return;
  738. /*
  739. * float to ulong
  740. */
  741. case CASE( TDOUBLE, TULONG):
  742. case CASE( TFLOAT, TULONG):
  743. case CASE( TDOUBLE, TUINT):
  744. case CASE( TFLOAT, TUINT):
  745. regsalloc(&nod, &regnode);
  746. gmove(f, &fregnode0);
  747. gins(AFADDD, nodfconst(-2147483648.), &fregnode0);
  748. gins(AFMOVLP, f, &nod);
  749. gins(ASUBL, nodconst(-2147483648), &nod);
  750. gmove(&nod, t);
  751. return;
  752. /*
  753. * ulong to float
  754. */
  755. case CASE( TULONG, TDOUBLE):
  756. case CASE( TULONG, TFLOAT):
  757. case CASE( TUINT, TDOUBLE):
  758. case CASE( TUINT, TFLOAT):
  759. regalloc(&nod, f, f);
  760. gmove(f, &nod);
  761. regsalloc(&nod1, &regnode);
  762. gmove(&nod, &nod1);
  763. gins(AFMOVL, &nod1, &fregnode0);
  764. gins(ACMPL, &nod, nodconst(0));
  765. gins(AJGE, Z, Z);
  766. p1 = p;
  767. gins(AFADDD, nodfconst(4294967296.), &fregnode0);
  768. patch(p1, pc);
  769. regfree(&nod);
  770. return;
  771. /*
  772. * fix to float
  773. */
  774. case CASE( TCHAR, TFLOAT):
  775. case CASE( TUCHAR, TFLOAT):
  776. case CASE( TSHORT, TFLOAT):
  777. case CASE( TUSHORT,TFLOAT):
  778. case CASE( TINT, TFLOAT):
  779. case CASE( TLONG, TFLOAT):
  780. case CASE( TIND, TFLOAT):
  781. case CASE( TCHAR, TDOUBLE):
  782. case CASE( TUCHAR, TDOUBLE):
  783. case CASE( TSHORT, TDOUBLE):
  784. case CASE( TUSHORT,TDOUBLE):
  785. case CASE( TINT, TDOUBLE):
  786. case CASE( TLONG, TDOUBLE):
  787. case CASE( TIND, TDOUBLE):
  788. regsalloc(&nod, &regnode);
  789. gmove(f, &nod);
  790. gins(AFMOVL, &nod, &fregnode0);
  791. return;
  792. /*
  793. * float to float
  794. */
  795. case CASE( TFLOAT, TFLOAT):
  796. case CASE( TDOUBLE,TFLOAT):
  797. case CASE( TFLOAT, TDOUBLE):
  798. case CASE( TDOUBLE,TDOUBLE):
  799. a = AFMOVD; break;
  800. }
  801. if(a == AMOVL || a == AFMOVD)
  802. if(samaddr(f, t))
  803. return;
  804. gins(a, f, t);
  805. }
  806. void
  807. doindex(Node *n)
  808. {
  809. Node nod, nod1;
  810. long v;
  811. if(debug['Y'])
  812. prtree(n, "index");
  813. if(n->left->complex >= FNX)
  814. print("botch in doindex\n");
  815. regalloc(&nod, &regnode, Z);
  816. v = constnode.vconst;
  817. cgen(n->right, &nod);
  818. idx.ptr = D_NONE;
  819. if(n->left->op == OCONST)
  820. idx.ptr = D_CONST;
  821. else if(n->left->op == OREGISTER)
  822. // else if(n->left->op == OREGISTER && typeil[n->left->type->etype])
  823. idx.ptr = n->left->reg;
  824. else if(n->left->op != OADDR) {
  825. reg[D_BP]++; // cant be used as a base
  826. regalloc(&nod1, &regnode, Z);
  827. cgen(n->left, &nod1);
  828. idx.ptr = nod1.reg;
  829. regfree(&nod1);
  830. reg[D_BP]--;
  831. }
  832. idx.reg = nod.reg;
  833. regfree(&nod);
  834. constnode.vconst = v;
  835. }
  836. void
  837. gins(int a, Node *f, Node *t)
  838. {
  839. if(f != Z && f->op == OINDEX)
  840. doindex(f);
  841. if(t != Z && t->op == OINDEX)
  842. doindex(t);
  843. nextpc();
  844. p->as = a;
  845. if(f != Z)
  846. naddr(f, &p->from);
  847. if(t != Z)
  848. naddr(t, &p->to);
  849. if(debug['g'])
  850. print("%P\n", p);
  851. }
  852. void
  853. fgopcode(int o, Node *f, Node *t, int pop, int rev)
  854. {
  855. int a, et;
  856. Node nod;
  857. et = TLONG;
  858. if(f != Z && f->type != T)
  859. et = f->type->etype;
  860. if(!typefd[et]) {
  861. diag(f, "fop: integer %O", o);
  862. return;
  863. }
  864. if(debug['M']) {
  865. if(t != Z && t->type != T)
  866. print("gop: %O %O-%s Z\n", o, f->op, tnames[et]);
  867. else
  868. print("gop: %O %O-%s %O-%s\n", o,
  869. f->op, tnames[et], t->op, tnames[t->type->etype]);
  870. }
  871. a = AGOK;
  872. switch(o) {
  873. case OASADD:
  874. case OADD:
  875. if(et == TFLOAT)
  876. a = AFADDF;
  877. else
  878. if(et == TDOUBLE || et == TVLONG) {
  879. a = AFADDD;
  880. if(pop)
  881. a = AFADDDP;
  882. }
  883. break;
  884. case OASSUB:
  885. case OSUB:
  886. if(et == TFLOAT) {
  887. a = AFSUBF;
  888. if(rev)
  889. a = AFSUBRF;
  890. } else
  891. if(et == TDOUBLE || et == TVLONG) {
  892. a = AFSUBD;
  893. if(pop)
  894. a = AFSUBDP;
  895. if(rev) {
  896. a = AFSUBRD;
  897. if(pop)
  898. a = AFSUBRDP;
  899. }
  900. }
  901. break;
  902. case OASMUL:
  903. case OMUL:
  904. if(et == TFLOAT)
  905. a = AFMULF;
  906. else
  907. if(et == TDOUBLE || et == TVLONG) {
  908. a = AFMULD;
  909. if(pop)
  910. a = AFMULDP;
  911. }
  912. break;
  913. case OASMOD:
  914. case OMOD:
  915. case OASDIV:
  916. case ODIV:
  917. if(et == TFLOAT) {
  918. a = AFDIVF;
  919. if(rev)
  920. a = AFDIVRF;
  921. } else
  922. if(et == TDOUBLE || et == TVLONG) {
  923. a = AFDIVD;
  924. if(pop)
  925. a = AFDIVDP;
  926. if(rev) {
  927. a = AFDIVRD;
  928. if(pop)
  929. a = AFDIVRDP;
  930. }
  931. }
  932. break;
  933. case OEQ:
  934. case ONE:
  935. case OLT:
  936. case OLE:
  937. case OGE:
  938. case OGT:
  939. pop += rev;
  940. if(et == TFLOAT) {
  941. a = AFCOMF;
  942. if(pop) {
  943. a = AFCOMFP;
  944. if(pop > 1)
  945. a = AGOK;
  946. }
  947. } else
  948. if(et == TDOUBLE || et == TVLONG) {
  949. a = AFCOMF;
  950. if(pop) {
  951. a = AFCOMDP;
  952. if(pop > 1)
  953. a = AFCOMDPP;
  954. }
  955. }
  956. gins(a, f, t);
  957. regalloc(&nod, &regnode, Z);
  958. if(nod.reg != D_AX) {
  959. regfree(&nod);
  960. nod.reg = D_AX;
  961. gins(APUSHL, &nod, Z);
  962. gins(AWAIT, Z, Z);
  963. gins(AFSTSW, Z, &nod);
  964. gins(ASAHF, Z, Z);
  965. gins(APOPL, Z, &nod);
  966. } else {
  967. gins(AWAIT, Z, Z);
  968. gins(AFSTSW, Z, &nod);
  969. gins(ASAHF, Z, Z);
  970. regfree(&nod);
  971. }
  972. switch(o) {
  973. case OEQ: a = AJEQ; break;
  974. case ONE: a = AJNE; break;
  975. case OLT: a = AJCS; break;
  976. case OLE: a = AJLS; break;
  977. case OGE: a = AJCC; break;
  978. case OGT: a = AJHI; break;
  979. }
  980. gins(a, Z, Z);
  981. return;
  982. }
  983. if(a == AGOK)
  984. diag(Z, "bad in gopcode %O", o);
  985. gins(a, f, t);
  986. }
  987. void
  988. gopcode(int o, Type *ty, Node *f, Node *t)
  989. {
  990. int a, et;
  991. et = TLONG;
  992. if(ty != T)
  993. et = ty->etype;
  994. if(typefd[et] && o != OADDR && o != OFUNC) {
  995. diag(f, "gop: float %O", o);
  996. return;
  997. }
  998. if(debug['M']) {
  999. if(f != Z && f->type != T)
  1000. print("gop: %O %O[%s],", o, f->op, tnames[et]);
  1001. else
  1002. print("gop: %O Z,", o);
  1003. if(t != Z && t->type != T)
  1004. print("%O[%s]\n", t->op, tnames[t->type->etype]);
  1005. else
  1006. print("Z\n");
  1007. }
  1008. a = AGOK;
  1009. switch(o) {
  1010. case OCOM:
  1011. a = ANOTL;
  1012. if(et == TCHAR || et == TUCHAR)
  1013. a = ANOTB;
  1014. if(et == TSHORT || et == TUSHORT)
  1015. a = ANOTW;
  1016. break;
  1017. case ONEG:
  1018. a = ANEGL;
  1019. if(et == TCHAR || et == TUCHAR)
  1020. a = ANEGB;
  1021. if(et == TSHORT || et == TUSHORT)
  1022. a = ANEGW;
  1023. break;
  1024. case OADDR:
  1025. a = ALEAL;
  1026. break;
  1027. case OASADD:
  1028. case OADD:
  1029. a = AADDL;
  1030. if(et == TCHAR || et == TUCHAR)
  1031. a = AADDB;
  1032. if(et == TSHORT || et == TUSHORT)
  1033. a = AADDW;
  1034. break;
  1035. case OASSUB:
  1036. case OSUB:
  1037. a = ASUBL;
  1038. if(et == TCHAR || et == TUCHAR)
  1039. a = ASUBB;
  1040. if(et == TSHORT || et == TUSHORT)
  1041. a = ASUBW;
  1042. break;
  1043. case OASOR:
  1044. case OOR:
  1045. a = AORL;
  1046. if(et == TCHAR || et == TUCHAR)
  1047. a = AORB;
  1048. if(et == TSHORT || et == TUSHORT)
  1049. a = AORW;
  1050. break;
  1051. case OASAND:
  1052. case OAND:
  1053. a = AANDL;
  1054. if(et == TCHAR || et == TUCHAR)
  1055. a = AANDB;
  1056. if(et == TSHORT || et == TUSHORT)
  1057. a = AANDW;
  1058. break;
  1059. case OASXOR:
  1060. case OXOR:
  1061. a = AXORL;
  1062. if(et == TCHAR || et == TUCHAR)
  1063. a = AXORB;
  1064. if(et == TSHORT || et == TUSHORT)
  1065. a = AXORW;
  1066. break;
  1067. case OASLSHR:
  1068. case OLSHR:
  1069. a = ASHRL;
  1070. if(et == TCHAR || et == TUCHAR)
  1071. a = ASHRB;
  1072. if(et == TSHORT || et == TUSHORT)
  1073. a = ASHRW;
  1074. break;
  1075. case OASASHR:
  1076. case OASHR:
  1077. a = ASARL;
  1078. if(et == TCHAR || et == TUCHAR)
  1079. a = ASARB;
  1080. if(et == TSHORT || et == TUSHORT)
  1081. a = ASARW;
  1082. break;
  1083. case OASASHL:
  1084. case OASHL:
  1085. a = ASALL;
  1086. if(et == TCHAR || et == TUCHAR)
  1087. a = ASALB;
  1088. if(et == TSHORT || et == TUSHORT)
  1089. a = ASALW;
  1090. break;
  1091. case OFUNC:
  1092. a = ACALL;
  1093. break;
  1094. case OASMUL:
  1095. case OMUL:
  1096. if(f->op == OREGISTER && t != Z && isreg(t, D_AX) && reg[D_DX] == 0)
  1097. t = Z;
  1098. a = AIMULL;
  1099. break;
  1100. case OASMOD:
  1101. case OMOD:
  1102. case OASDIV:
  1103. case ODIV:
  1104. a = AIDIVL;
  1105. break;
  1106. case OASLMUL:
  1107. case OLMUL:
  1108. a = AMULL;
  1109. break;
  1110. case OASLMOD:
  1111. case OLMOD:
  1112. case OASLDIV:
  1113. case OLDIV:
  1114. a = ADIVL;
  1115. break;
  1116. case OEQ:
  1117. case ONE:
  1118. case OLT:
  1119. case OLE:
  1120. case OGE:
  1121. case OGT:
  1122. case OLO:
  1123. case OLS:
  1124. case OHS:
  1125. case OHI:
  1126. a = ACMPL;
  1127. if(et == TCHAR || et == TUCHAR)
  1128. a = ACMPB;
  1129. if(et == TSHORT || et == TUSHORT)
  1130. a = ACMPW;
  1131. gins(a, f, t);
  1132. switch(o) {
  1133. case OEQ: a = AJEQ; break;
  1134. case ONE: a = AJNE; break;
  1135. case OLT: a = AJLT; break;
  1136. case OLE: a = AJLE; break;
  1137. case OGE: a = AJGE; break;
  1138. case OGT: a = AJGT; break;
  1139. case OLO: a = AJCS; break;
  1140. case OLS: a = AJLS; break;
  1141. case OHS: a = AJCC; break;
  1142. case OHI: a = AJHI; break;
  1143. }
  1144. gins(a, Z, Z);
  1145. return;
  1146. }
  1147. if(a == AGOK)
  1148. diag(Z, "bad in gopcode %O", o);
  1149. gins(a, f, t);
  1150. }
  1151. int
  1152. samaddr(Node *f, Node *t)
  1153. {
  1154. if(f->op != t->op)
  1155. return 0;
  1156. switch(f->op) {
  1157. case OREGISTER:
  1158. if(f->reg != t->reg)
  1159. break;
  1160. return 1;
  1161. }
  1162. return 0;
  1163. }
  1164. void
  1165. gbranch(int o)
  1166. {
  1167. int a;
  1168. a = AGOK;
  1169. switch(o) {
  1170. case ORETURN:
  1171. a = ARET;
  1172. break;
  1173. case OGOTO:
  1174. a = AJMP;
  1175. break;
  1176. }
  1177. nextpc();
  1178. if(a == AGOK) {
  1179. diag(Z, "bad in gbranch %O", o);
  1180. nextpc();
  1181. }
  1182. p->as = a;
  1183. }
  1184. void
  1185. patch(Prog *op, long pc)
  1186. {
  1187. op->to.offset = pc;
  1188. op->to.type = D_BRANCH;
  1189. }
  1190. void
  1191. gpseudo(int a, Sym *s, Node *n)
  1192. {
  1193. nextpc();
  1194. p->as = a;
  1195. p->from.type = D_EXTERN;
  1196. p->from.sym = s;
  1197. p->from.scale = (profileflg ? 0 : NOPROF);
  1198. if(s->class == CSTATIC)
  1199. p->from.type = D_STATIC;
  1200. naddr(n, &p->to);
  1201. if(a == ADATA || a == AGLOBL)
  1202. pc--;
  1203. }
  1204. int
  1205. sconst(Node *n)
  1206. {
  1207. long v;
  1208. if(n->op == OCONST && !typefd[n->type->etype]) {
  1209. v = n->vconst;
  1210. if(v >= -32766L && v < 32766L)
  1211. return 1;
  1212. }
  1213. return 0;
  1214. }
  1215. long
  1216. exreg(Type *t)
  1217. {
  1218. USED(t);
  1219. return 0;
  1220. }
  1221. schar ewidth[NTYPE] =
  1222. {
  1223. -1, /*[TXXX]*/
  1224. SZ_CHAR, /*[TCHAR]*/
  1225. SZ_CHAR, /*[TUCHAR]*/
  1226. SZ_SHORT, /*[TSHORT]*/
  1227. SZ_SHORT, /*[TUSHORT]*/
  1228. SZ_INT, /*[TINT]*/
  1229. SZ_INT, /*[TUINT]*/
  1230. SZ_LONG, /*[TLONG]*/
  1231. SZ_LONG, /*[TULONG]*/
  1232. SZ_VLONG, /*[TVLONG]*/
  1233. SZ_VLONG, /*[TUVLONG]*/
  1234. SZ_FLOAT, /*[TFLOAT]*/
  1235. SZ_DOUBLE, /*[TDOUBLE]*/
  1236. SZ_IND, /*[TIND]*/
  1237. 0, /*[TFUNC]*/
  1238. -1, /*[TARRAY]*/
  1239. 0, /*[TVOID]*/
  1240. -1, /*[TSTRUCT]*/
  1241. -1, /*[TUNION]*/
  1242. SZ_INT, /*[TENUM]*/
  1243. };
  1244. long ncast[NTYPE] =
  1245. {
  1246. 0, /*[TXXX]*/
  1247. BCHAR|BUCHAR, /*[TCHAR]*/
  1248. BCHAR|BUCHAR, /*[TUCHAR]*/
  1249. BSHORT|BUSHORT, /*[TSHORT]*/
  1250. BSHORT|BUSHORT, /*[TUSHORT]*/
  1251. BINT|BUINT|BLONG|BULONG|BIND, /*[TINT]*/
  1252. BINT|BUINT|BLONG|BULONG|BIND, /*[TUINT]*/
  1253. BINT|BUINT|BLONG|BULONG|BIND, /*[TLONG]*/
  1254. BINT|BUINT|BLONG|BULONG|BIND, /*[TULONG]*/
  1255. BVLONG|BUVLONG, /*[TVLONG]*/
  1256. BVLONG|BUVLONG, /*[TUVLONG]*/
  1257. BFLOAT, /*[TFLOAT]*/
  1258. BDOUBLE, /*[TDOUBLE]*/
  1259. BLONG|BULONG|BIND, /*[TIND]*/
  1260. 0, /*[TFUNC]*/
  1261. 0, /*[TARRAY]*/
  1262. 0, /*[TVOID]*/
  1263. BSTRUCT, /*[TSTRUCT]*/
  1264. BUNION, /*[TUNION]*/
  1265. 0, /*[TENUM]*/
  1266. };