txt.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388
  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. goto err;
  273. case TFLOAT:
  274. case TDOUBLE:
  275. case TVLONG:
  276. i = D_F0;
  277. goto out;
  278. }
  279. diag(tn, "unknown type in regalloc: %T", tn->type);
  280. err:
  281. i = 0;
  282. out:
  283. if(i)
  284. reg[i]++;
  285. nodreg(n, tn, i);
  286. }
  287. void
  288. regialloc(Node *n, Node *tn, Node *o)
  289. {
  290. Node nod;
  291. nod = *tn;
  292. nod.type = types[TIND];
  293. regalloc(n, &nod, o);
  294. }
  295. void
  296. regfree(Node *n)
  297. {
  298. int i;
  299. i = 0;
  300. if(n->op != OREGISTER && n->op != OINDREG)
  301. goto err;
  302. i = n->reg;
  303. if(i < 0 || i >= sizeof(reg))
  304. goto err;
  305. if(reg[i] <= 0)
  306. goto err;
  307. reg[i]--;
  308. return;
  309. err:
  310. diag(n, "error in regfree: %d", i);
  311. }
  312. void
  313. regsalloc(Node *n, Node *nn)
  314. {
  315. cursafe = align(cursafe, nn->type, Aaut3);
  316. maxargsafe = maxround(maxargsafe, cursafe+curarg);
  317. *n = *nodsafe;
  318. n->xoffset = -(stkoff + cursafe);
  319. n->type = nn->type;
  320. n->etype = nn->type->etype;
  321. n->lineno = nn->lineno;
  322. }
  323. void
  324. regaalloc1(Node *n, Node *nn)
  325. {
  326. nodreg(n, nn, REGARG);
  327. reg[REGARG]++;
  328. curarg = align(curarg, nn->type, Aarg1);
  329. curarg = align(curarg, nn->type, Aarg2);
  330. maxargsafe = maxround(maxargsafe, cursafe+curarg);
  331. }
  332. void
  333. regaalloc(Node *n, Node *nn)
  334. {
  335. curarg = align(curarg, nn->type, Aarg1);
  336. *n = *nn;
  337. n->op = OINDREG;
  338. n->reg = REGSP;
  339. n->xoffset = curarg;
  340. n->complex = 0;
  341. n->addable = 20;
  342. curarg = align(curarg, nn->type, Aarg2);
  343. maxargsafe = maxround(maxargsafe, cursafe+curarg);
  344. }
  345. void
  346. regind(Node *n, Node *nn)
  347. {
  348. if(n->op != OREGISTER) {
  349. diag(n, "regind not OREGISTER");
  350. return;
  351. }
  352. n->op = OINDREG;
  353. n->type = nn->type;
  354. }
  355. void
  356. naddr(Node *n, Adr *a)
  357. {
  358. long v;
  359. a->type = D_NONE;
  360. if(n == Z)
  361. return;
  362. switch(n->op) {
  363. default:
  364. bad:
  365. diag(n, "bad in naddr: %O %D", n->op, a);
  366. break;
  367. case OREGISTER:
  368. a->type = n->reg;
  369. a->sym = S;
  370. break;
  371. case OIND:
  372. naddr(n->left, a);
  373. if(a->type >= D_AX && a->type <= D_DI)
  374. a->type += D_INDIR;
  375. else
  376. if(a->type == D_CONST)
  377. a->type = D_NONE+D_INDIR;
  378. else
  379. if(a->type == D_ADDR) {
  380. a->type = a->index;
  381. a->index = D_NONE;
  382. } else
  383. goto bad;
  384. break;
  385. case OINDEX:
  386. a->type = idx.ptr;
  387. if(n->left->op == OADDR)
  388. naddr(n->left, a);
  389. if(a->type >= D_AX && a->type <= D_DI)
  390. a->type += D_INDIR;
  391. else
  392. if(a->type == D_CONST)
  393. a->type = D_NONE+D_INDIR;
  394. else
  395. if(a->type == D_ADDR) {
  396. a->type = a->index;
  397. a->index = D_NONE;
  398. } else
  399. goto bad;
  400. a->index = idx.reg;
  401. a->scale = n->scale;
  402. a->offset += n->xoffset;
  403. break;
  404. case OINDREG:
  405. a->type = n->reg+D_INDIR;
  406. a->sym = S;
  407. a->offset = n->xoffset;
  408. break;
  409. case ONAME:
  410. a->etype = n->etype;
  411. a->type = D_STATIC;
  412. a->sym = n->sym;
  413. a->offset = n->xoffset;
  414. if(n->class == CSTATIC)
  415. break;
  416. if(n->class == CEXTERN || n->class == CGLOBL) {
  417. a->type = D_EXTERN;
  418. break;
  419. }
  420. if(n->class == CAUTO) {
  421. a->type = D_AUTO;
  422. break;
  423. }
  424. if(n->class == CPARAM) {
  425. a->type = D_PARAM;
  426. break;
  427. }
  428. goto bad;
  429. case OCONST:
  430. if(typefd[n->type->etype]) {
  431. a->type = D_FCONST;
  432. a->dval = n->fconst;
  433. break;
  434. }
  435. a->sym = S;
  436. a->type = D_CONST;
  437. a->offset = n->vconst;
  438. break;
  439. case OADDR:
  440. naddr(n->left, a);
  441. if(a->type >= D_INDIR) {
  442. a->type -= D_INDIR;
  443. break;
  444. }
  445. if(a->type == D_EXTERN || a->type == D_STATIC ||
  446. a->type == D_AUTO || a->type == D_PARAM)
  447. if(a->index == D_NONE) {
  448. a->index = a->type;
  449. a->type = D_ADDR;
  450. break;
  451. }
  452. goto bad;
  453. case OADD:
  454. if(n->right->op == OCONST) {
  455. v = n->right->vconst;
  456. naddr(n->left, a);
  457. } else
  458. if(n->left->op == OCONST) {
  459. v = n->left->vconst;
  460. naddr(n->right, a);
  461. } else
  462. goto bad;
  463. a->offset += v;
  464. break;
  465. }
  466. }
  467. #define CASE(a,b) ((a<<8)|(b<<0))
  468. void
  469. gmove(Node *f, Node *t)
  470. {
  471. int ft, tt, a;
  472. Node nod, nod1;
  473. Prog *p1;
  474. ft = f->type->etype;
  475. tt = t->type->etype;
  476. if(debug['M'])
  477. print("gop: %O %O[%s],%O[%s]\n", OAS,
  478. f->op, tnames[ft], t->op, tnames[tt]);
  479. if(typefd[ft] && f->op == OCONST) {
  480. if(f->fconst == 0)
  481. gins(AFLDZ, Z, Z);
  482. else
  483. if(f->fconst == 1)
  484. gins(AFLD1, Z, Z);
  485. else
  486. gins(AFMOVD, f, &fregnode0);
  487. gmove(&fregnode0, t);
  488. return;
  489. }
  490. /*
  491. * load
  492. */
  493. if(f->op == ONAME || f->op == OINDREG ||
  494. f->op == OIND || f->op == OINDEX)
  495. switch(ft) {
  496. case TCHAR:
  497. a = AMOVBLSX;
  498. goto ld;
  499. case TUCHAR:
  500. a = AMOVBLZX;
  501. goto ld;
  502. case TSHORT:
  503. if(typefd[tt]) {
  504. gins(AFMOVW, f, &fregnode0);
  505. gmove(&fregnode0, t);
  506. return;
  507. }
  508. a = AMOVWLSX;
  509. goto ld;
  510. case TUSHORT:
  511. a = AMOVWLZX;
  512. goto ld;
  513. case TINT:
  514. case TUINT:
  515. case TLONG:
  516. case TULONG:
  517. case TIND:
  518. if(typefd[tt]) {
  519. gins(AFMOVL, f, &fregnode0);
  520. gmove(&fregnode0, t);
  521. return;
  522. }
  523. a = AMOVL;
  524. ld:
  525. regalloc(&nod, f, t);
  526. nod.type = types[TLONG];
  527. gins(a, f, &nod);
  528. gmove(&nod, t);
  529. regfree(&nod);
  530. return;
  531. case TFLOAT:
  532. gins(AFMOVF, f, t);
  533. return;
  534. case TDOUBLE:
  535. gins(AFMOVD, f, t);
  536. return;
  537. case TVLONG:
  538. gins(AFMOVV, f, t);
  539. return;
  540. }
  541. /*
  542. * store
  543. */
  544. if(t->op == ONAME || t->op == OINDREG ||
  545. t->op == OIND || t->op == OINDEX)
  546. switch(tt) {
  547. case TCHAR:
  548. case TUCHAR:
  549. a = AMOVB; goto st;
  550. case TSHORT:
  551. case TUSHORT:
  552. a = AMOVW; goto st;
  553. case TINT:
  554. case TUINT:
  555. case TLONG:
  556. case TULONG:
  557. case TIND:
  558. a = AMOVL; goto st;
  559. st:
  560. if(f->op == OCONST) {
  561. gins(a, f, t);
  562. return;
  563. }
  564. regalloc(&nod, t, f);
  565. gmove(f, &nod);
  566. gins(a, &nod, t);
  567. regfree(&nod);
  568. return;
  569. case TFLOAT:
  570. gins(AFMOVFP, f, t);
  571. return;
  572. case TDOUBLE:
  573. gins(AFMOVDP, f, t);
  574. return;
  575. case TVLONG:
  576. gins(AFMOVVP, f, t);
  577. return;
  578. }
  579. /*
  580. * convert
  581. */
  582. switch(CASE(ft,tt)) {
  583. default:
  584. /*
  585. * integer to integer
  586. ********
  587. a = AGOK; break;
  588. case CASE( TCHAR, TCHAR):
  589. case CASE( TUCHAR, TCHAR):
  590. case CASE( TSHORT, TCHAR):
  591. case CASE( TUSHORT,TCHAR):
  592. case CASE( TINT, TCHAR):
  593. case CASE( TUINT, TCHAR):
  594. case CASE( TLONG, TCHAR):
  595. case CASE( TULONG, TCHAR):
  596. case CASE( TIND, TCHAR):
  597. case CASE( TCHAR, TUCHAR):
  598. case CASE( TUCHAR, TUCHAR):
  599. case CASE( TSHORT, TUCHAR):
  600. case CASE( TUSHORT,TUCHAR):
  601. case CASE( TINT, TUCHAR):
  602. case CASE( TUINT, TUCHAR):
  603. case CASE( TLONG, TUCHAR):
  604. case CASE( TULONG, TUCHAR):
  605. case CASE( TIND, TUCHAR):
  606. case CASE( TSHORT, TSHORT):
  607. case CASE( TUSHORT,TSHORT):
  608. case CASE( TINT, TSHORT):
  609. case CASE( TUINT, TSHORT):
  610. case CASE( TLONG, TSHORT):
  611. case CASE( TULONG, TSHORT):
  612. case CASE( TIND, TSHORT):
  613. case CASE( TSHORT, TUSHORT):
  614. case CASE( TUSHORT,TUSHORT):
  615. case CASE( TINT, TUSHORT):
  616. case CASE( TUINT, TUSHORT):
  617. case CASE( TLONG, TUSHORT):
  618. case CASE( TULONG, TUSHORT):
  619. case CASE( TIND, TUSHORT):
  620. case CASE( TINT, TINT):
  621. case CASE( TUINT, TINT):
  622. case CASE( TLONG, TINT):
  623. case CASE( TULONG, TINT):
  624. case CASE( TIND, TINT):
  625. case CASE( TINT, TUINT):
  626. case CASE( TUINT, TUINT):
  627. case CASE( TLONG, TUINT):
  628. case CASE( TULONG, TUINT):
  629. case CASE( TIND, TUINT):
  630. case CASE( TINT, TLONG):
  631. case CASE( TUINT, TLONG):
  632. case CASE( TLONG, TLONG):
  633. case CASE( TULONG, TLONG):
  634. case CASE( TIND, TLONG):
  635. case CASE( TINT, TULONG):
  636. case CASE( TUINT, TULONG):
  637. case CASE( TLONG, TULONG):
  638. case CASE( TULONG, TULONG):
  639. case CASE( TIND, TULONG):
  640. case CASE( TINT, TIND):
  641. case CASE( TUINT, TIND):
  642. case CASE( TLONG, TIND):
  643. case CASE( TULONG, TIND):
  644. case CASE( TIND, TIND):
  645. *****/
  646. a = AMOVL;
  647. break;
  648. case CASE( TSHORT, TINT):
  649. case CASE( TSHORT, TUINT):
  650. case CASE( TSHORT, TLONG):
  651. case CASE( TSHORT, TULONG):
  652. case CASE( TSHORT, TIND):
  653. a = AMOVWLSX;
  654. if(f->op == OCONST) {
  655. f->vconst &= 0xffff;
  656. if(f->vconst & 0x8000)
  657. f->vconst |= 0xffff0000;
  658. a = AMOVL;
  659. }
  660. break;
  661. case CASE( TUSHORT,TINT):
  662. case CASE( TUSHORT,TUINT):
  663. case CASE( TUSHORT,TLONG):
  664. case CASE( TUSHORT,TULONG):
  665. case CASE( TUSHORT,TIND):
  666. a = AMOVWLZX;
  667. if(f->op == OCONST) {
  668. f->vconst &= 0xffff;
  669. a = AMOVL;
  670. }
  671. break;
  672. case CASE( TCHAR, TSHORT):
  673. case CASE( TCHAR, TUSHORT):
  674. case CASE( TCHAR, TINT):
  675. case CASE( TCHAR, TUINT):
  676. case CASE( TCHAR, TLONG):
  677. case CASE( TCHAR, TULONG):
  678. case CASE( TCHAR, TIND):
  679. a = AMOVBLSX;
  680. if(f->op == OCONST) {
  681. f->vconst &= 0xff;
  682. if(f->vconst & 0x80)
  683. f->vconst |= 0xffffff00;
  684. a = AMOVL;
  685. }
  686. break;
  687. case CASE( TUCHAR, TSHORT):
  688. case CASE( TUCHAR, TUSHORT):
  689. case CASE( TUCHAR, TINT):
  690. case CASE( TUCHAR, TUINT):
  691. case CASE( TUCHAR, TLONG):
  692. case CASE( TUCHAR, TULONG):
  693. case CASE( TUCHAR, TIND):
  694. a = AMOVBLZX;
  695. if(f->op == OCONST) {
  696. f->vconst &= 0xff;
  697. a = AMOVL;
  698. }
  699. break;
  700. /*
  701. * float to fix
  702. */
  703. case CASE( TFLOAT, TCHAR):
  704. case CASE( TFLOAT, TUCHAR):
  705. case CASE( TFLOAT, TSHORT):
  706. case CASE( TFLOAT, TUSHORT):
  707. case CASE( TFLOAT, TINT):
  708. case CASE( TFLOAT, TUINT):
  709. case CASE( TFLOAT, TLONG):
  710. case CASE( TFLOAT, TULONG):
  711. case CASE( TFLOAT, TIND):
  712. case CASE( TDOUBLE,TCHAR):
  713. case CASE( TDOUBLE,TUCHAR):
  714. case CASE( TDOUBLE,TSHORT):
  715. case CASE( TDOUBLE,TUSHORT):
  716. case CASE( TDOUBLE,TINT):
  717. case CASE( TDOUBLE,TUINT):
  718. case CASE( TDOUBLE,TLONG):
  719. case CASE( TDOUBLE,TULONG):
  720. case CASE( TDOUBLE,TIND):
  721. case CASE( TVLONG, TCHAR):
  722. case CASE( TVLONG, TUCHAR):
  723. case CASE( TVLONG, TSHORT):
  724. case CASE( TVLONG, TUSHORT):
  725. case CASE( TVLONG, TINT):
  726. case CASE( TVLONG, TUINT):
  727. case CASE( TVLONG, TLONG):
  728. case CASE( TVLONG, TULONG):
  729. case CASE( TVLONG, TIND):
  730. if(fproundflg) {
  731. regsalloc(&nod, &regnode);
  732. gins(AFMOVLP, f, &nod);
  733. gmove(&nod, t);
  734. return;
  735. }
  736. regsalloc(&nod, &regnode);
  737. regsalloc(&nod1, &regnode);
  738. gins(AFSTCW, Z, &nod1);
  739. nod1.xoffset += 2;
  740. gins(AMOVW, nodconst(0xf7f), &nod1);
  741. gins(AFLDCW, &nod1, Z);
  742. gins(AFMOVLP, f, &nod);
  743. nod1.xoffset -= 2;
  744. gins(AFLDCW, &nod1, Z);
  745. gmove(&nod, t);
  746. return;
  747. /*
  748. * ulong to float
  749. */
  750. case CASE( TULONG, TDOUBLE):
  751. case CASE( TULONG, TVLONG):
  752. case CASE( TULONG, TFLOAT):
  753. case CASE( TUINT, TDOUBLE):
  754. case CASE( TUINT, TVLONG):
  755. case CASE( TUINT, TFLOAT):
  756. regalloc(&nod, f, f);
  757. gmove(f, &nod);
  758. regsalloc(&nod1, &regnode);
  759. gmove(&nod, &nod1);
  760. gins(AFMOVL, &nod1, &fregnode0);
  761. gins(ACMPL, &nod, nodconst(0));
  762. gins(AJGE, Z, Z);
  763. p1 = p;
  764. gins(AFADDD, nodfconst(4294967296.), &fregnode0);
  765. patch(p1, pc);
  766. regfree(&nod);
  767. return;
  768. /*
  769. * fix to float
  770. */
  771. case CASE( TCHAR, TFLOAT):
  772. case CASE( TUCHAR, TFLOAT):
  773. case CASE( TSHORT, TFLOAT):
  774. case CASE( TUSHORT,TFLOAT):
  775. case CASE( TINT, TFLOAT):
  776. case CASE( TLONG, TFLOAT):
  777. case CASE( TIND, TFLOAT):
  778. case CASE( TCHAR, TDOUBLE):
  779. case CASE( TUCHAR, TDOUBLE):
  780. case CASE( TSHORT, TDOUBLE):
  781. case CASE( TUSHORT,TDOUBLE):
  782. case CASE( TINT, TDOUBLE):
  783. case CASE( TLONG, TDOUBLE):
  784. case CASE( TIND, TDOUBLE):
  785. case CASE( TCHAR, TVLONG):
  786. case CASE( TUCHAR, TVLONG):
  787. case CASE( TSHORT, TVLONG):
  788. case CASE( TUSHORT,TVLONG):
  789. case CASE( TINT, TVLONG):
  790. case CASE( TLONG, TVLONG):
  791. case CASE( TIND, TVLONG):
  792. regsalloc(&nod, &regnode);
  793. gmove(f, &nod);
  794. gins(AFMOVL, &nod, &fregnode0);
  795. return;
  796. /*
  797. * float to float
  798. */
  799. case CASE( TFLOAT, TFLOAT):
  800. case CASE( TDOUBLE,TFLOAT):
  801. case CASE( TVLONG, TFLOAT):
  802. case CASE( TFLOAT, TDOUBLE):
  803. case CASE( TDOUBLE,TDOUBLE):
  804. case CASE( TVLONG, TDOUBLE):
  805. case CASE( TFLOAT, TVLONG):
  806. case CASE( TDOUBLE,TVLONG):
  807. case CASE( TVLONG, TVLONG):
  808. a = AFMOVD; break;
  809. }
  810. if(a == AMOVL || a == AFMOVD)
  811. if(samaddr(f, t))
  812. return;
  813. gins(a, f, t);
  814. }
  815. void
  816. doindex(Node *n)
  817. {
  818. Node nod, nod1;
  819. long v;
  820. if(debug['Y'])
  821. prtree(n, "index");
  822. if(n->left->complex >= FNX)
  823. print("botch in doindex\n");
  824. regalloc(&nod, &regnode, Z);
  825. v = constnode.vconst;
  826. cgen(n->right, &nod);
  827. idx.reg = nod.reg;
  828. idx.ptr = D_NONE;
  829. if(n->left->op != OADDR) {
  830. reg[D_BP]++; // cant be used as a base
  831. regalloc(&nod1, &regnode, Z);
  832. cgen(n->left, &nod1);
  833. idx.ptr = nod1.reg;
  834. regfree(&nod1);
  835. reg[D_BP]--;
  836. }
  837. regfree(&nod);
  838. constnode.vconst = v;
  839. }
  840. void
  841. gins(int a, Node *f, Node *t)
  842. {
  843. if(f != Z && f->op == OINDEX)
  844. doindex(f);
  845. if(t != Z && t->op == OINDEX)
  846. doindex(t);
  847. nextpc();
  848. p->as = a;
  849. if(f != Z)
  850. naddr(f, &p->from);
  851. if(t != Z)
  852. naddr(t, &p->to);
  853. if(debug['g'])
  854. print("%P\n", p);
  855. }
  856. void
  857. fgopcode(int o, Node *f, Node *t, int pop, int rev)
  858. {
  859. int a, et;
  860. Node nod;
  861. et = TLONG;
  862. if(f != Z && f->type != T)
  863. et = f->type->etype;
  864. if(!typefd[et]) {
  865. diag(f, "fop: integer %O", o);
  866. return;
  867. }
  868. if(debug['M']) {
  869. if(t != Z && t->type != T)
  870. print("gop: %O %O-%s Z\n", o, f->op, tnames[et]);
  871. else
  872. print("gop: %O %O-%s %O-%s\n", o,
  873. f->op, tnames[et], t->op, tnames[t->type->etype]);
  874. }
  875. a = AGOK;
  876. switch(o) {
  877. case OASADD:
  878. case OADD:
  879. if(et == TFLOAT)
  880. a = AFADDF;
  881. else
  882. if(et == TDOUBLE || et == TVLONG) {
  883. a = AFADDD;
  884. if(pop)
  885. a = AFADDDP;
  886. }
  887. break;
  888. case OASSUB:
  889. case OSUB:
  890. if(et == TFLOAT) {
  891. a = AFSUBF;
  892. if(rev)
  893. a = AFSUBRF;
  894. } else
  895. if(et == TDOUBLE || et == TVLONG) {
  896. a = AFSUBD;
  897. if(pop)
  898. a = AFSUBDP;
  899. if(rev) {
  900. a = AFSUBRD;
  901. if(pop)
  902. a = AFSUBRDP;
  903. }
  904. }
  905. break;
  906. case OASMUL:
  907. case OMUL:
  908. if(et == TFLOAT)
  909. a = AFMULF;
  910. else
  911. if(et == TDOUBLE || et == TVLONG) {
  912. a = AFMULD;
  913. if(pop)
  914. a = AFMULDP;
  915. }
  916. break;
  917. case OASMOD:
  918. case OMOD:
  919. case OASDIV:
  920. case ODIV:
  921. if(et == TFLOAT) {
  922. a = AFDIVF;
  923. if(rev)
  924. a = AFDIVRF;
  925. } else
  926. if(et == TDOUBLE || et == TVLONG) {
  927. a = AFDIVD;
  928. if(pop)
  929. a = AFDIVDP;
  930. if(rev) {
  931. a = AFDIVRD;
  932. if(pop)
  933. a = AFDIVRDP;
  934. }
  935. }
  936. break;
  937. case OEQ:
  938. case ONE:
  939. case OLT:
  940. case OLE:
  941. case OGE:
  942. case OGT:
  943. pop += rev;
  944. if(et == TFLOAT) {
  945. a = AFCOMF;
  946. if(pop) {
  947. a = AFCOMFP;
  948. if(pop > 1)
  949. a = AGOK;
  950. }
  951. } else
  952. if(et == TDOUBLE || et == TVLONG) {
  953. a = AFCOMF;
  954. if(pop) {
  955. a = AFCOMDP;
  956. if(pop > 1)
  957. a = AFCOMDPP;
  958. }
  959. }
  960. gins(a, f, t);
  961. regalloc(&nod, &regnode, Z);
  962. if(nod.reg != D_AX) {
  963. regfree(&nod);
  964. nod.reg = D_AX;
  965. gins(APUSHL, &nod, Z);
  966. gins(AWAIT, Z, Z);
  967. gins(AFSTSW, Z, &nod);
  968. gins(ASAHF, Z, Z);
  969. gins(APOPL, Z, &nod);
  970. } else {
  971. gins(AWAIT, Z, Z);
  972. gins(AFSTSW, Z, &nod);
  973. gins(ASAHF, Z, Z);
  974. regfree(&nod);
  975. }
  976. switch(o) {
  977. case OEQ: a = AJEQ; break;
  978. case ONE: a = AJNE; break;
  979. case OLT: a = AJCS; break;
  980. case OLE: a = AJLS; break;
  981. case OGE: a = AJCC; break;
  982. case OGT: a = AJHI; break;
  983. }
  984. gins(a, Z, Z);
  985. return;
  986. }
  987. if(a == AGOK)
  988. diag(Z, "bad in gopcode %O", o);
  989. gins(a, f, t);
  990. }
  991. void
  992. gopcode(int o, Type *ty, Node *f, Node *t)
  993. {
  994. int a, et;
  995. et = TLONG;
  996. if(ty != T)
  997. et = ty->etype;
  998. if(typefd[et] && o != OADDR && o != OFUNC) {
  999. diag(f, "gop: float %O", o);
  1000. return;
  1001. }
  1002. if(debug['M']) {
  1003. if(f != Z && f->type != T)
  1004. print("gop: %O %O[%s],", o, f->op, tnames[et]);
  1005. else
  1006. print("gop: %O Z,", o);
  1007. if(t != Z && t->type != T)
  1008. print("%O[%s]\n", t->op, tnames[t->type->etype]);
  1009. else
  1010. print("Z\n");
  1011. }
  1012. a = AGOK;
  1013. switch(o) {
  1014. case OADDR:
  1015. a = ALEAL;
  1016. break;
  1017. case OASADD:
  1018. case OADD:
  1019. a = AADDL;
  1020. if(et == TCHAR || et == TUCHAR)
  1021. a = AADDB;
  1022. if(et == TSHORT || et == TUSHORT)
  1023. a = AADDW;
  1024. break;
  1025. case OASSUB:
  1026. case OSUB:
  1027. a = ASUBL;
  1028. if(et == TCHAR || et == TUCHAR)
  1029. a = ASUBB;
  1030. if(et == TSHORT || et == TUSHORT)
  1031. a = ASUBW;
  1032. break;
  1033. case OASOR:
  1034. case OOR:
  1035. a = AORL;
  1036. if(et == TCHAR || et == TUCHAR)
  1037. a = AORB;
  1038. if(et == TSHORT || et == TUSHORT)
  1039. a = AORW;
  1040. break;
  1041. case OASAND:
  1042. case OAND:
  1043. a = AANDL;
  1044. if(et == TCHAR || et == TUCHAR)
  1045. a = AANDB;
  1046. if(et == TSHORT || et == TUSHORT)
  1047. a = AANDW;
  1048. break;
  1049. case OASXOR:
  1050. case OXOR:
  1051. a = AXORL;
  1052. if(et == TCHAR || et == TUCHAR)
  1053. a = AXORB;
  1054. if(et == TSHORT || et == TUSHORT)
  1055. a = AXORW;
  1056. break;
  1057. case OASLSHR:
  1058. case OLSHR:
  1059. a = ASHRL;
  1060. if(et == TCHAR || et == TUCHAR)
  1061. a = ASHRB;
  1062. if(et == TSHORT || et == TUSHORT)
  1063. a = ASHRW;
  1064. break;
  1065. case OASASHR:
  1066. case OASHR:
  1067. a = ASARL;
  1068. if(et == TCHAR || et == TUCHAR)
  1069. a = ASARB;
  1070. if(et == TSHORT || et == TUSHORT)
  1071. a = ASARW;
  1072. break;
  1073. case OASASHL:
  1074. case OASHL:
  1075. a = ASALL;
  1076. if(et == TCHAR || et == TUCHAR)
  1077. a = ASALB;
  1078. if(et == TSHORT || et == TUSHORT)
  1079. a = ASALW;
  1080. break;
  1081. case OFUNC:
  1082. a = ACALL;
  1083. break;
  1084. case OASMUL:
  1085. case OMUL:
  1086. a = AIMULL;
  1087. break;
  1088. case OASMOD:
  1089. case OMOD:
  1090. case OASDIV:
  1091. case ODIV:
  1092. a = AIDIVL;
  1093. break;
  1094. case OASLMUL:
  1095. case OLMUL:
  1096. a = AMULL;
  1097. break;
  1098. case OASLMOD:
  1099. case OLMOD:
  1100. case OASLDIV:
  1101. case OLDIV:
  1102. a = ADIVL;
  1103. break;
  1104. case OEQ:
  1105. case ONE:
  1106. case OLT:
  1107. case OLE:
  1108. case OGE:
  1109. case OGT:
  1110. case OLO:
  1111. case OLS:
  1112. case OHS:
  1113. case OHI:
  1114. a = ACMPL;
  1115. if(et == TCHAR || et == TUCHAR)
  1116. a = ACMPB;
  1117. if(et == TSHORT || et == TUSHORT)
  1118. a = ACMPW;
  1119. gins(a, f, t);
  1120. switch(o) {
  1121. case OEQ: a = AJEQ; break;
  1122. case ONE: a = AJNE; break;
  1123. case OLT: a = AJLT; break;
  1124. case OLE: a = AJLE; break;
  1125. case OGE: a = AJGE; break;
  1126. case OGT: a = AJGT; break;
  1127. case OLO: a = AJCS; break;
  1128. case OLS: a = AJLS; break;
  1129. case OHS: a = AJCC; break;
  1130. case OHI: a = AJHI; break;
  1131. }
  1132. gins(a, Z, Z);
  1133. return;
  1134. }
  1135. if(a == AGOK)
  1136. diag(Z, "bad in gopcode %O", o);
  1137. gins(a, f, t);
  1138. }
  1139. int
  1140. samaddr(Node *f, Node *t)
  1141. {
  1142. if(f->op != t->op)
  1143. return 0;
  1144. switch(f->op) {
  1145. case OREGISTER:
  1146. if(f->reg != t->reg)
  1147. break;
  1148. return 1;
  1149. }
  1150. return 0;
  1151. }
  1152. void
  1153. gbranch(int o)
  1154. {
  1155. int a;
  1156. a = AGOK;
  1157. switch(o) {
  1158. case ORETURN:
  1159. a = ARET;
  1160. break;
  1161. case OGOTO:
  1162. a = AJMP;
  1163. break;
  1164. }
  1165. nextpc();
  1166. if(a == AGOK) {
  1167. diag(Z, "bad in gbranch %O", o);
  1168. nextpc();
  1169. }
  1170. p->as = a;
  1171. }
  1172. void
  1173. patch(Prog *op, long pc)
  1174. {
  1175. op->to.offset = pc;
  1176. op->to.type = D_BRANCH;
  1177. }
  1178. void
  1179. gpseudo(int a, Sym *s, Node *n)
  1180. {
  1181. nextpc();
  1182. p->as = a;
  1183. p->from.type = D_EXTERN;
  1184. p->from.sym = s;
  1185. p->from.scale = (profileflg ? 0 : NOPROF);
  1186. if(s->class == CSTATIC)
  1187. p->from.type = D_STATIC;
  1188. naddr(n, &p->to);
  1189. if(a == ADATA || a == AGLOBL)
  1190. pc--;
  1191. }
  1192. int
  1193. sconst(Node *n)
  1194. {
  1195. long v;
  1196. if(n->op == OCONST && !typefd[n->type->etype]) {
  1197. v = n->vconst;
  1198. if(v >= -32766L && v < 32766L)
  1199. return 1;
  1200. }
  1201. return 0;
  1202. }
  1203. long
  1204. exreg(Type *t)
  1205. {
  1206. USED(t);
  1207. return 0;
  1208. }
  1209. schar ewidth[NTYPE] =
  1210. {
  1211. -1, /*[TXXX]*/
  1212. SZ_CHAR, /*[TCHAR]*/
  1213. SZ_CHAR, /*[TUCHAR]*/
  1214. SZ_SHORT, /*[TSHORT]*/
  1215. SZ_SHORT, /*[TUSHORT]*/
  1216. SZ_INT, /*[TINT]*/
  1217. SZ_INT, /*[TUINT]*/
  1218. SZ_LONG, /*[TLONG]*/
  1219. SZ_LONG, /*[TULONG]*/
  1220. SZ_VLONG, /*[TVLONG]*/
  1221. SZ_VLONG, /*[TUVLONG]*/
  1222. SZ_FLOAT, /*[TFLOAT]*/
  1223. SZ_DOUBLE, /*[TDOUBLE]*/
  1224. SZ_IND, /*[TIND]*/
  1225. 0, /*[TFUNC]*/
  1226. -1, /*[TARRAY]*/
  1227. 0, /*[TVOID]*/
  1228. -1, /*[TSTRUCT]*/
  1229. -1, /*[TUNION]*/
  1230. SZ_INT, /*[TENUM]*/
  1231. };
  1232. long ncast[NTYPE] =
  1233. {
  1234. 0, /*[TXXX]*/
  1235. BCHAR|BUCHAR, /*[TCHAR]*/
  1236. BCHAR|BUCHAR, /*[TUCHAR]*/
  1237. BSHORT|BUSHORT, /*[TSHORT]*/
  1238. BSHORT|BUSHORT, /*[TUSHORT]*/
  1239. BINT|BUINT|BLONG|BULONG|BIND, /*[TINT]*/
  1240. BINT|BUINT|BLONG|BULONG|BIND, /*[TUINT]*/
  1241. BINT|BUINT|BLONG|BULONG|BIND, /*[TLONG]*/
  1242. BINT|BUINT|BLONG|BULONG|BIND, /*[TULONG]*/
  1243. BVLONG|BUVLONG, /*[TVLONG]*/
  1244. BVLONG|BUVLONG, /*[TUVLONG]*/
  1245. BFLOAT, /*[TFLOAT]*/
  1246. BDOUBLE, /*[TDOUBLE]*/
  1247. BLONG|BULONG|BIND, /*[TIND]*/
  1248. 0, /*[TFUNC]*/
  1249. 0, /*[TARRAY]*/
  1250. 0, /*[TVOID]*/
  1251. BSTRUCT, /*[TSTRUCT]*/
  1252. BUNION, /*[TUNION]*/
  1253. 0, /*[TENUM]*/
  1254. };