swt.c 17 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  1. #include "gc.h"
  2. int
  3. swcmp(const void *a1, const void *a2)
  4. {
  5. C1 *p1, *p2;
  6. p1 = (C1*)a1;
  7. p2 = (C1*)a2;
  8. if(p1->val < p2->val)
  9. return -1;
  10. return p1->val > p2->val;
  11. }
  12. void
  13. doswit(int g, Node *n)
  14. {
  15. Case *c;
  16. C1 *q, *iq;
  17. long def, nc, i;
  18. def = 0;
  19. nc = 0;
  20. for(c = cases; c->link != C; c = c->link) {
  21. if(c->def) {
  22. if(def)
  23. diag(n, "more than one default in switch");
  24. def = c->label;
  25. continue;
  26. }
  27. nc++;
  28. }
  29. iq = alloc(nc*sizeof(C1));
  30. q = iq;
  31. for(c = cases; c->link != C; c = c->link) {
  32. if(c->def)
  33. continue;
  34. q->label = c->label;
  35. q->val = c->val;
  36. q++;
  37. }
  38. qsort(iq, nc, sizeof(C1), swcmp);
  39. if(def == 0)
  40. def = breakpc;
  41. for(i=0; i<nc-1; i++)
  42. if(iq[i].val == iq[i+1].val)
  43. diag(n, "duplicate cases in switch %ld", iq[i].val);
  44. swit1(iq, nc, def, g, n);
  45. }
  46. #define N1 4 /* ncase: always linear */
  47. #define N2 5 /* min ncase: direct */
  48. #define N3 4 /* range/ncase: direct */
  49. /* else binary */
  50. void
  51. swit1(C1 *q, int nc, long def, int g, Node *n)
  52. {
  53. C1 *r, *s;
  54. int i, l, m, y;
  55. long v, range;
  56. Prog *sp1, *sp2;
  57. /* note that g and g+1 are not allocated */
  58. if(nc <= N1)
  59. goto linear;
  60. y = 23*nc/100 + 5; /* number of cases needed to make */
  61. if(y < N2) /* direct switch worthwile */
  62. y = N2; /* try to do better than n**2 here */
  63. for(m=nc; m>=y; m--) { /* m is number of cases */
  64. s = q+nc;
  65. r = s-m;
  66. for(l=nc-m; l>=0; l--) { /* l is base of contig cases */
  67. s--;
  68. range = s->val - r->val;
  69. if(range > 0 && range <= N3*m)
  70. goto direct;
  71. r--;
  72. }
  73. }
  74. /*
  75. * divide and conquer
  76. */
  77. i = nc / 2;
  78. r = q+i;
  79. v = r->val;
  80. /* compare median */
  81. if(v >= -128 && v < 128) {
  82. gopcode(OAS, n->type, D_CONST, nodconst(v), g+1, n);
  83. gopcode(OEQ, n->type, g, n, g+1, n);
  84. } else
  85. gopcode(OEQ, n->type, g, n, D_CONST, nodconst(v));
  86. gbranch(OLT);
  87. sp1 = p;
  88. gbranch(OGT);
  89. sp2 = p;
  90. gbranch(OGOTO);
  91. patch(p, r->label);
  92. patch(sp1, pc);
  93. swit1(q, i, def, g, n);
  94. patch(sp2, pc);
  95. swit1(r+1, nc-i-1, def, g, n);
  96. return;
  97. direct:
  98. /* compare low bound */
  99. v = r->val;
  100. if(v >= -128 && v < 128) {
  101. gopcode(OAS, n->type, D_CONST, nodconst(v), g+1, n);
  102. gopcode(OEQ, n->type, g, n, g+1, n);
  103. } else
  104. gopcode(OEQ, n->type, g, n, D_CONST, nodconst(v));
  105. gbranch(OLT);
  106. sp1 = p;
  107. /* compare high bound */
  108. v = s->val;
  109. if(v >= -128 && v < 128) {
  110. gopcode(OAS, n->type, D_CONST, nodconst(v), g+1, n);
  111. gopcode(OEQ, n->type, g, n, g+1, n);
  112. } else
  113. gopcode(OEQ, n->type, g, n, D_CONST, nodconst(v));
  114. gbranch(OGT);
  115. sp2 = p;
  116. /* switch */
  117. v = r->val;
  118. gpseudo(AMOVW, symstatic, D_R0, 0L);
  119. p->from.offset = nstatic - v*2;
  120. p->from.index = g|I_INDEX1;
  121. p->from.scale = 5;
  122. nextpc();
  123. p->as = ACASEW;
  124. /* table */
  125. for(i=0; i<=range; i++) {
  126. gbranch(OCASE);
  127. if(v == r->val) {
  128. patch(p, r->label);
  129. r++;
  130. } else
  131. patch(p, def);
  132. p->from.type = D_STATIC;
  133. p->from.sym = symstatic;
  134. p->from.offset = nstatic;
  135. nstatic += types[TSHORT]->width;
  136. v++;
  137. }
  138. gbranch(OGOTO);
  139. patch(p, def);
  140. if(r != s+1)
  141. print("smelly direct switch\n");
  142. if(l > 0) {
  143. patch(sp1, pc);
  144. swit1(q, l, def, g, n);
  145. } else
  146. patch(sp1, def);
  147. m += l;
  148. if(m < nc) {
  149. patch(sp2, pc);
  150. swit1(q+m, nc-m, def, g, n);
  151. } else
  152. patch(sp2, def);
  153. return;
  154. linear:
  155. for(i=0; i<nc; i++) {
  156. v = q->val;
  157. if(v >= -128 && v < 128) {
  158. gopcode(OAS, n->type, D_CONST, nodconst(v), g+1, n);
  159. gopcode(OEQ, n->type, g+1, n, g, n);
  160. } else
  161. gopcode(OEQ, n->type, g, n, D_CONST, nodconst(v));
  162. gbranch(OEQ);
  163. patch(p, q->label);
  164. q++;
  165. }
  166. gbranch(OGOTO);
  167. patch(p, def);
  168. }
  169. void
  170. cas(void)
  171. {
  172. Case *c;
  173. c = alloc(sizeof(*c));
  174. c->link = cases;
  175. cases = c;
  176. }
  177. int
  178. bitload(Node *b, int n1, int n2, int n3, Node *nn)
  179. {
  180. int sh, g, gs;
  181. long v;
  182. Node *l;
  183. Type *t;
  184. /*
  185. * n1 gets adjusted/masked value
  186. * n2 gets address of cell
  187. * n3 gets contents of cell
  188. */
  189. gs = 0;
  190. t = tfield;
  191. l = b->left;
  192. g = regalloc(t, n3);
  193. if(n2 != D_NONE) {
  194. lcgen(l, n2, Z);
  195. n2 |= I_INDIR;
  196. gmove(t, t, n2, l, g, l);
  197. gmove(t, t, g, l, n1, l);
  198. } else
  199. cgen(l, g, nn);
  200. if(b->type->shift == 0 && typeu[b->type->etype]) {
  201. v = ~0 + (1L << b->type->nbits);
  202. gopcode(OAND, t, D_CONST, nodconst(v), g, l);
  203. } else {
  204. sh = 32 - b->type->shift - b->type->nbits;
  205. if(sh > 0)
  206. if(sh >= 8) {
  207. gs = regalloc(t, D_NONE);
  208. gmove(t, t, D_CONST, nodconst(sh), gs, l);
  209. gopcode(OASHL, t, gs, l, g, l);
  210. if(b->type->shift)
  211. regfree(gs);
  212. } else
  213. gopcode(OASHL, t, D_CONST, nodconst(sh), g, l);
  214. sh += b->type->shift;
  215. if(sh > 0) {
  216. if(sh >= 8) {
  217. if(b->type->shift) {
  218. gs = regalloc(t, D_NONE);
  219. gmove(t, t, D_CONST, nodconst(sh), gs, l);
  220. }
  221. if(typeu[b->type->etype])
  222. gopcode(OLSHR, t, gs, l, g, l);
  223. else
  224. gopcode(OASHR, t, gs, l, g, l);
  225. regfree(gs);
  226. } else {
  227. if(typeu[b->type->etype])
  228. gopcode(OLSHR, t, D_CONST, nodconst(sh), g, l);
  229. else
  230. gopcode(OASHR, t, D_CONST, nodconst(sh), g, l);
  231. }
  232. }
  233. }
  234. return g;
  235. }
  236. void
  237. bitstore(Node *b, int n1, int n2, int n3, int result, Node *nn)
  238. {
  239. long v;
  240. Node *l;
  241. Type *t;
  242. int sh, g, gs;
  243. /*
  244. * n1 has adjusted/masked value
  245. * n2 has address of cell
  246. * n3 has contents of cell
  247. */
  248. t = tfield;
  249. l = b->left;
  250. g = regalloc(t, D_NONE);
  251. v = ~0 + (1L << b->type->nbits);
  252. gopcode(OAND, t, D_CONST, nodconst(v), n1, l);
  253. gmove(t, t, n1, l, g, l);
  254. if(result != D_NONE)
  255. gmove(t, nn->type, n1, l, result, nn);
  256. sh = b->type->shift;
  257. if(sh > 0) {
  258. if(sh >= 8) {
  259. gs = regalloc(t, D_NONE);
  260. gmove(t, t, D_CONST, nodconst(sh), gs, l);
  261. gopcode(OASHL, t, gs, l, g, l);
  262. regfree(gs);
  263. } else
  264. gopcode(OASHL, t, D_CONST, nodconst(sh), g, l);
  265. }
  266. v <<= sh;
  267. gopcode(OAND, t, D_CONST, nodconst(~v), n3, l);
  268. gopcode(OOR, t, n3, l, g, l);
  269. gmove(t, t, g, l, n2|I_INDIR, l);
  270. regfree(g);
  271. regfree(n1);
  272. regfree(n2);
  273. regfree(n3);
  274. }
  275. long
  276. outstring(char *s, long n)
  277. {
  278. long r;
  279. r = nstring;
  280. while(n) {
  281. string[mnstring] = *s++;
  282. mnstring++;
  283. nstring++;
  284. if(mnstring >= NSNAME) {
  285. gpseudo(ADATA, symstring, D_SCONST, 0L);
  286. memmove(p->to.sval, string, NSNAME);
  287. p->from.offset = nstring - NSNAME;
  288. p->from.displace = NSNAME;
  289. mnstring = 0;
  290. }
  291. n--;
  292. }
  293. return r;
  294. }
  295. long
  296. outlstring(ushort *s, long n)
  297. {
  298. char buf[2];
  299. int c;
  300. long r;
  301. while(nstring & 1)
  302. outstring("", 1);
  303. r = nstring;
  304. while(n > 0) {
  305. c = *s++;
  306. if(align(0, types[TCHAR], Aarg1)) {
  307. buf[0] = c>>8;
  308. buf[1] = c;
  309. } else {
  310. buf[0] = c;
  311. buf[1] = c>>8;
  312. }
  313. outstring(buf, 2);
  314. n -= sizeof(ushort);
  315. }
  316. return r;
  317. }
  318. int
  319. doinc(Node *n, int f)
  320. {
  321. Node *l;
  322. int a;
  323. loop:
  324. if(n == Z)
  325. return 0;
  326. l = n->left;
  327. switch(n->op) {
  328. case OPOSTINC:
  329. case OPOSTDEC:
  330. if(f & POST) {
  331. a = n->addable;
  332. if(a >= INDEXED) {
  333. if(f & TEST)
  334. return 1;
  335. n->addable = 0;
  336. cgen(n, D_NONE, n);
  337. n->addable = a;
  338. }
  339. }
  340. break;
  341. case OAS:
  342. case OASLMUL:
  343. case OASLDIV:
  344. case OASLMOD:
  345. case OASMUL:
  346. case OASDIV:
  347. case OASMOD:
  348. case OASXOR:
  349. case OASOR:
  350. case OASADD:
  351. case OASSUB:
  352. case OASLSHR:
  353. case OASASHR:
  354. case OASASHL:
  355. case OASAND:
  356. case OPREINC:
  357. case OPREDEC:
  358. if(f & PRE) {
  359. a = n->addable;
  360. if(a >= INDEXED) {
  361. if(f & TEST)
  362. return 1;
  363. n->addable = 0;
  364. doinc(n, PRE);
  365. cgen(n, D_NONE, n);
  366. n->addable = a;
  367. return 0;
  368. }
  369. }
  370. break;
  371. case OFUNC:
  372. if(f & PRE)
  373. break;
  374. return 0;
  375. case ONAME:
  376. case OREGISTER:
  377. case OSTRING:
  378. case OCONST:
  379. case OANDAND:
  380. case OOROR:
  381. return 0;
  382. case OCOND:
  383. return 0;
  384. case OCOMMA:
  385. n = n->right;
  386. if(f & PRE)
  387. n = l;
  388. goto loop;
  389. }
  390. if(l != Z)
  391. if(doinc(l, f))
  392. return 1;
  393. n = n->right;
  394. goto loop;
  395. }
  396. void
  397. setsp(void)
  398. {
  399. nextpc();
  400. p->as = AADJSP;
  401. p->from.type = D_CONST;
  402. p->from.offset = 0;
  403. }
  404. void
  405. adjsp(long o)
  406. {
  407. if(o != 0) {
  408. nextpc();
  409. p->as = AADJSP;
  410. p->from.type = D_CONST;
  411. p->from.offset = o;
  412. argoff += o;
  413. }
  414. }
  415. int
  416. simplv(Node *n)
  417. {
  418. if(n->addable <= INDEXED)
  419. return 0;
  420. while(n->op == OIND)
  421. n = n->left;
  422. if(n->op == ONAME)
  423. return 1;
  424. return 0;
  425. }
  426. int
  427. eval(Node *n, int g)
  428. {
  429. if(n->addable >= INDEXED)
  430. return D_TREE;
  431. g = regalloc(n->type, g);
  432. cgen(n, g, n);
  433. return g;
  434. }
  435. void outhist(Biobuf*);
  436. void zname(Biobuf*, Sym*, int);
  437. void zaddr(Biobuf*, Adr*, int);
  438. void zwrite(Biobuf*, Prog*, int, int);
  439. void
  440. outcode(void)
  441. {
  442. struct { Sym *sym; short type; } h[NSYM];
  443. Prog *p;
  444. Sym *s;
  445. int f, sf, st, t, sym;
  446. Biobuf b;
  447. if(debug['S']) {
  448. for(p = firstp; p != P; p = p->link)
  449. if(p->as != ADATA && p->as != AGLOBL)
  450. pc--;
  451. for(p = firstp; p != P; p = p->link) {
  452. print("%P\n", p);
  453. if(p->as != ADATA && p->as != AGLOBL)
  454. pc++;
  455. }
  456. }
  457. f = open(outfile, OWRITE);
  458. if(f < 0) {
  459. diag(Z, "cant open %s", outfile);
  460. errorexit();
  461. }
  462. Binit(&b, f, OWRITE);
  463. Bseek(&b, 0L, 2);
  464. outhist(&b);
  465. for(sym=0; sym<NSYM; sym++) {
  466. h[sym].sym = S;
  467. h[sym].type = 0;
  468. }
  469. sym = 1;
  470. for(p = firstp; p != P; p = p->link) {
  471. jackpot:
  472. sf = 0;
  473. s = p->from.sym;
  474. while(s != S) {
  475. sf = s->sym;
  476. if(sf < 0 || sf >= NSYM)
  477. sf = 0;
  478. t = p->from.type & D_MASK;
  479. if(h[sf].type == t)
  480. if(h[sf].sym == s)
  481. break;
  482. s->sym = sym;
  483. zname(&b, s, t);
  484. h[sym].sym = s;
  485. h[sym].type = t;
  486. sf = sym;
  487. sym++;
  488. if(sym >= NSYM)
  489. sym = 1;
  490. break;
  491. }
  492. st = 0;
  493. s = p->to.sym;
  494. while(s != S) {
  495. st = s->sym;
  496. if(st < 0 || st >= NSYM)
  497. st = 0;
  498. t = p->to.type & D_MASK;
  499. if(h[st].type == t)
  500. if(h[st].sym == s)
  501. break;
  502. s->sym = sym;
  503. zname(&b, s, t);
  504. h[sym].sym = s;
  505. h[sym].type = t;
  506. st = sym;
  507. sym++;
  508. if(sym >= NSYM)
  509. sym = 1;
  510. if(st == sf)
  511. goto jackpot;
  512. break;
  513. }
  514. zwrite(&b, p, sf, st);
  515. }
  516. Bflush(&b);
  517. close(f);
  518. firstp = P;
  519. lastp = P;
  520. }
  521. void
  522. zwrite(Biobuf *b, Prog *p, int sf, int st)
  523. {
  524. long l;
  525. l = p->as;
  526. Bputc(b, l);
  527. Bputc(b, l>>8);
  528. l = p->lineno;
  529. Bputc(b, l);
  530. Bputc(b, l>>8);
  531. Bputc(b, l>>16);
  532. Bputc(b, l>>24);
  533. zaddr(b, &p->from, sf);
  534. zaddr(b, &p->to, st);
  535. }
  536. void
  537. zname(Biobuf *b, Sym *s, int t)
  538. {
  539. char *n;
  540. ulong sig;
  541. if(debug['T'] && t == D_EXTERN && s->sig != SIGDONE && s->type != types[TENUM] && s != symrathole){
  542. sig = sign(s);
  543. Bputc(b, ASIGNAME);
  544. Bputc(b, ASIGNAME>>8);
  545. Bputc(b, sig);
  546. Bputc(b, sig>>8);
  547. Bputc(b, sig>>16);
  548. Bputc(b, sig>>24);
  549. s->sig = SIGDONE;
  550. }
  551. else{
  552. Bputc(b, ANAME); /* as */
  553. Bputc(b, ANAME>>8); /* as */
  554. }
  555. Bputc(b, t); /* type */
  556. Bputc(b, s->sym); /* sym */
  557. n = s->name;
  558. while(*n) {
  559. Bputc(b, *n);
  560. n++;
  561. }
  562. Bputc(b, 0);
  563. }
  564. void
  565. zaddr(Biobuf *b, Adr *a, int s)
  566. {
  567. long l;
  568. int i, t;
  569. char *n;
  570. Ieee e;
  571. t = 0;
  572. if(a->field)
  573. t |= T_FIELD;
  574. if(a->index != D_NONE)
  575. t |= T_INDEX;
  576. if(s)
  577. t |= T_SYM;
  578. switch(a->type) {
  579. default:
  580. if(a->offset)
  581. t |= T_OFFSET;
  582. if(a->displace)
  583. t |= T_INDEX;
  584. if(a->type & ~0xff)
  585. t |= T_TYPE;
  586. break;
  587. case D_FCONST:
  588. t |= T_FCONST;
  589. break;
  590. case D_SCONST:
  591. t |= T_SCONST;
  592. break;
  593. }
  594. Bputc(b, t);
  595. if(t & T_FIELD) { /* implies field */
  596. i = a->field;
  597. Bputc(b, i);
  598. Bputc(b, i>>8);
  599. }
  600. if(t & T_INDEX) { /* implies index, scale, displace */
  601. i = a->index;
  602. Bputc(b, i);
  603. Bputc(b, i>>8);
  604. Bputc(b, a->scale);
  605. l = a->displace;
  606. Bputc(b, l);
  607. Bputc(b, l>>8);
  608. Bputc(b, l>>16);
  609. Bputc(b, l>>24);
  610. }
  611. if(t & T_OFFSET) { /* implies offset */
  612. l = a->offset;
  613. Bputc(b, l);
  614. Bputc(b, l>>8);
  615. Bputc(b, l>>16);
  616. Bputc(b, l>>24);
  617. }
  618. if(t & T_SYM) /* implies sym */
  619. Bputc(b, s);
  620. if(t & T_FCONST) {
  621. ieeedtod(&e, a->dval);
  622. l = e.l;
  623. Bputc(b, l);
  624. Bputc(b, l>>8);
  625. Bputc(b, l>>16);
  626. Bputc(b, l>>24);
  627. l = e.h;
  628. Bputc(b, l);
  629. Bputc(b, l>>8);
  630. Bputc(b, l>>16);
  631. Bputc(b, l>>24);
  632. return;
  633. }
  634. if(t & T_SCONST) {
  635. n = a->sval;
  636. for(i=0; i<NSNAME; i++) {
  637. Bputc(b, *n);
  638. n++;
  639. }
  640. return;
  641. }
  642. i = a->type;
  643. Bputc(b, i);
  644. if(t & T_TYPE)
  645. Bputc(b, i>>8);
  646. }
  647. void
  648. outhist(Biobuf *b)
  649. {
  650. Hist *h;
  651. char *p, *q, *op, c;
  652. Prog pg;
  653. int n;
  654. pg = zprog;
  655. pg.as = AHISTORY;
  656. c = pathchar();
  657. for(h = hist; h != H; h = h->link) {
  658. p = h->name;
  659. op = 0;
  660. /* on windows skip drive specifier in pathname */
  661. if(systemtype(Windows) && p && p[1] == ':'){
  662. p += 2;
  663. c = *p;
  664. }
  665. if(p && p[0] != c && h->offset == 0 && pathname){
  666. /* on windows skip drive specifier in pathname */
  667. if(systemtype(Windows) && pathname[1] == ':') {
  668. op = p;
  669. p = pathname+2;
  670. c = *p;
  671. } else if(pathname[0] == c){
  672. op = p;
  673. p = pathname;
  674. }
  675. }
  676. while(p) {
  677. q = utfrune(p, c);
  678. if(q) {
  679. n = q-p;
  680. if(n == 0){
  681. n = 1; /* leading "/" */
  682. *p = '/'; /* don't emit "\" on windows */
  683. }
  684. q++;
  685. } else {
  686. n = strlen(p);
  687. q = 0;
  688. }
  689. if(n) {
  690. Bputc(b, ANAME);
  691. Bputc(b, ANAME>>8);
  692. Bputc(b, D_FILE);
  693. Bputc(b, 1);
  694. Bputc(b, '<');
  695. Bwrite(b, p, n);
  696. Bputc(b, 0);
  697. }
  698. p = q;
  699. if(p == 0 && op) {
  700. p = op;
  701. op = 0;
  702. }
  703. }
  704. pg.lineno = h->line;
  705. pg.to.type = zprog.to.type;
  706. pg.to.offset = h->offset;
  707. if(h->offset)
  708. pg.to.type = D_CONST;
  709. zwrite(b, &pg, 0, 0);
  710. }
  711. }
  712. void
  713. ieeedtod(Ieee *ieee, double native)
  714. {
  715. double fr, ho, f;
  716. int exp;
  717. if(native < 0) {
  718. ieeedtod(ieee, -native);
  719. ieee->h |= 0x80000000L;
  720. return;
  721. }
  722. if(native == 0) {
  723. ieee->l = 0;
  724. ieee->h = 0;
  725. return;
  726. }
  727. fr = frexp(native, &exp);
  728. f = 2097152L; /* shouldnt use fp constants here */
  729. fr = modf(fr*f, &ho);
  730. ieee->h = ho;
  731. ieee->h &= 0xfffffL;
  732. ieee->h |= (exp+1022L) << 20;
  733. f = 65536L;
  734. fr = modf(fr*f, &ho);
  735. ieee->l = ho;
  736. ieee->l <<= 16;
  737. ieee->l |= (long)(fr*f);
  738. }
  739. int
  740. nodalloc(Type *t, int g, Node *n)
  741. {
  742. n->type = t;
  743. n->op = OREGISTER;
  744. n->addable = 12;
  745. n->complex = 0;
  746. g = regaddr(g);
  747. n->reg = g | I_INDIR;
  748. n->xoffset = 0;
  749. return g;
  750. }
  751. int
  752. mulcon(Node *n, Node *c, int result, Node *nn)
  753. {
  754. long v;
  755. if(typefd[n->type->etype])
  756. return 0;
  757. v = c->vconst;
  758. if(mulcon1(n, v, result, nn))
  759. return 1;
  760. return 0;
  761. }
  762. int
  763. shlcon(Node *n, Node *c, int result, Node *nn)
  764. {
  765. long v;
  766. v = 1L << c->vconst;
  767. return mulcon1(n, v, result, nn);
  768. }
  769. int
  770. mulcon1(Node *n, long v, int result, Node *nn)
  771. {
  772. int g, g1, a1, a2, neg;
  773. int o;
  774. char code[10], *p;
  775. if(result == D_NONE)
  776. return 0;
  777. neg = 0;
  778. if(v < 0) {
  779. v = -v;
  780. neg++;
  781. }
  782. a1 = 0;
  783. a2 = multabsize;
  784. for(;;) {
  785. if(a1 >= a2)
  786. return 0;
  787. g1 = (a2 + a1)/2;
  788. if(v < multab[g1].val) {
  789. a2 = g1;
  790. continue;
  791. }
  792. if(v > multab[g1].val) {
  793. a1 = g1+1;
  794. continue;
  795. }
  796. break;
  797. }
  798. strcpy(code, "0");
  799. strncat(code, multab[g1].code, sizeof(multab[0].code));
  800. p = code;
  801. if(p[1] == 'i')
  802. p += 2;
  803. g = regalloc(n->type, result);
  804. cgen(n, g, n);
  805. if(neg)
  806. gopcode(ONEG, n->type, D_NONE, n, g, n);
  807. g1 = regalloc(n->type, D_NONE);
  808. loop:
  809. switch(*p) {
  810. case 0:
  811. regfree(g1);
  812. gmove(n->type, nn->type, g, n, result, nn);
  813. regfree(g);
  814. return 1;
  815. case '0':
  816. o = OAS;
  817. *p -= '0';
  818. goto com;
  819. case '1':
  820. case '2':
  821. o = OSUB;
  822. *p -= '1';
  823. goto com;
  824. case '3':
  825. case '4':
  826. case '5':
  827. case '6':
  828. o = OADD;
  829. *p -= '3';
  830. com:
  831. a1 = g;
  832. if(*p == 1 || *p == 3)
  833. a1 = g1;
  834. a2 = g;
  835. if(*p == 0 || *p == 3)
  836. a2 = g1;
  837. gopcode(o, n->type, a1, n, a2, n);
  838. p++;
  839. break;
  840. default:
  841. a1 = *p++ - 'a' + 1;
  842. a2 = g;
  843. if(a1 > 8) {
  844. a2 = g1;
  845. a1 -= 8;
  846. }
  847. gopcode(OASHL, n->type, D_CONST, nodconst(a1), a2, n);
  848. break;
  849. }
  850. goto loop;
  851. }
  852. void
  853. nullwarn(Node *l, Node *r)
  854. {
  855. warn(Z, "result of operation not used");
  856. if(l != Z)
  857. cgen(l, D_NONE, Z);
  858. if(r != Z)
  859. cgen(r, D_NONE, Z);
  860. }
  861. void
  862. sextern(Sym *s, Node *a, long o, long w)
  863. {
  864. long e, lw;
  865. for(e=0; e<w; e+=NSNAME) {
  866. lw = NSNAME;
  867. if(w-e < lw)
  868. lw = w-e;
  869. gpseudo(ADATA, s, D_SCONST, 0L);
  870. p->from.offset += o+e;
  871. p->from.displace = lw;
  872. memmove(p->to.sval, a->cstring+e, lw);
  873. }
  874. }
  875. void
  876. gextern(Sym *s, Node *a, long o, long w)
  877. {
  878. if(a->op == OCONST && typev[a->type->etype]) {
  879. gpseudo(ADATA, s, D_CONST, (long)(a->vconst>>32));
  880. p->from.offset += o;
  881. p->from.displace = 4;
  882. gpseudo(ADATA, s, D_CONST, (long)(a->vconst));
  883. p->from.offset += o + 4;
  884. p->from.displace = 4;
  885. return;
  886. }
  887. gpseudo(ADATA, s, D_TREE, (long)a);
  888. p->from.offset += o;
  889. p->from.displace = w;
  890. }
  891. long
  892. align(long i, Type *t, int op)
  893. {
  894. long o;
  895. Type *v;
  896. int w;
  897. o = i;
  898. w = 1;
  899. switch(op) {
  900. default:
  901. diag(Z, "unknown align opcode %d", op);
  902. break;
  903. case Asu2: /* padding at end of a struct */
  904. w = SZ_LONG;
  905. if(packflg)
  906. w = packflg;
  907. break;
  908. case Ael1: /* initial allign of struct element */
  909. for(v=t; v->etype==TARRAY; v=v->link)
  910. ;
  911. w = ewidth[v->etype];
  912. if(w <= 0 || w >= SZ_SHORT)
  913. w = SZ_SHORT;
  914. if(packflg)
  915. w = packflg;
  916. break;
  917. case Ael2: /* width of a struct element */
  918. o += t->width;
  919. break;
  920. case Aarg0: /* initial passbyptr argument in arg list */
  921. if(typesuv[t->etype]) {
  922. o = align(o, types[TIND], Aarg1);
  923. o = align(o, types[TIND], Aarg2);
  924. }
  925. break;
  926. case Aarg1: /* initial allign of parameter */
  927. w = ewidth[t->etype];
  928. if(w <= 0 || w >= SZ_LONG) {
  929. w = SZ_LONG;
  930. break;
  931. }
  932. o += SZ_LONG - w; /* big endian adjustment */
  933. w = 1;
  934. break;
  935. case Aarg2: /* width of a parameter */
  936. o += t->width;
  937. w = SZ_LONG;
  938. break;
  939. case Aaut3: /* total allign of automatic */
  940. o = align(o, t, Ael1);
  941. o = align(o, t, Ael2);
  942. break;
  943. }
  944. o = round(o, w);
  945. if(debug['A'])
  946. print("align %s %ld %T = %ld\n", bnames[op], i, t, o);
  947. return o;
  948. }
  949. long
  950. maxround(long max, long v)
  951. {
  952. v += SZ_LONG-1;
  953. if(v > max)
  954. max = round(v, SZ_LONG);
  955. return max;
  956. }