pass.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. #include "l.h"
  2. void
  3. dodata(void)
  4. {
  5. int i;
  6. Sym *s;
  7. Prog *p;
  8. long t, u;
  9. if(debug['v'])
  10. Bprint(&bso, "%5.2f dodata\n", cputime());
  11. Bflush(&bso);
  12. for(p = datap; p != P; p = p->link) {
  13. s = p->from.sym;
  14. if(s->type == SBSS)
  15. s->type = SDATA;
  16. if(s->type != SDATA)
  17. diag("initialize non-data (%d): %s\n%P\n",
  18. s->type, s->name, p);
  19. t = p->from.offset + p->from.displace;
  20. if(t > s->value)
  21. diag("initialize bounds (%ld): %s\n%P\n",
  22. s->value, s->name, p);
  23. }
  24. /* allocate small guys */
  25. datsize = 0;
  26. for(i=0; i<NHASH; i++)
  27. for(s = hash[i]; s != S; s = s->link) {
  28. if(s->type != SDATA)
  29. if(s->type != SBSS)
  30. continue;
  31. t = s->value;
  32. if(t == 0) {
  33. diag("%s: no size\n", s->name);
  34. t = 1;
  35. }
  36. t = rnd(t, 4);;
  37. s->value = t;
  38. if(t > MINSIZ)
  39. continue;
  40. s->value = datsize;
  41. datsize += t;
  42. s->type = SDATA1;
  43. }
  44. /* allocate the rest of the data */
  45. for(i=0; i<NHASH; i++)
  46. for(s = hash[i]; s != S; s = s->link) {
  47. if(s->type != SDATA) {
  48. if(s->type == SDATA1)
  49. s->type = SDATA;
  50. continue;
  51. }
  52. t = s->value;
  53. s->value = datsize;
  54. datsize += t;
  55. }
  56. if(debug['j']) {
  57. /*
  58. * pad data with bss that fits up to next
  59. * 8k boundary, then push data to 8k
  60. */
  61. u = rnd(datsize, 8192);
  62. u -= datsize;
  63. for(i=0; i<NHASH; i++)
  64. for(s = hash[i]; s != S; s = s->link) {
  65. if(s->type != SBSS)
  66. continue;
  67. t = s->value;
  68. if(t > u)
  69. continue;
  70. u -= t;
  71. s->value = datsize;
  72. s->type = SDATA;
  73. datsize += t;
  74. }
  75. datsize += u;
  76. }
  77. /* now the bss */
  78. bsssize = 0;
  79. for(i=0; i<NHASH; i++)
  80. for(s = hash[i]; s != S; s = s->link) {
  81. if(s->type != SBSS)
  82. continue;
  83. t = s->value;
  84. s->value = bsssize + datsize;
  85. bsssize += t;
  86. }
  87. xdefine("bdata", SDATA, 0L);
  88. xdefine("edata", SDATA, datsize);
  89. xdefine("end", SBSS, datsize+bsssize);
  90. }
  91. Prog*
  92. brchain(Prog *p)
  93. {
  94. int i;
  95. for(i=0; i<20; i++) {
  96. if(p == P || p->as != ABRA)
  97. return p;
  98. p = p->pcond;
  99. }
  100. return P;
  101. }
  102. void
  103. follow(void)
  104. {
  105. Prog *p;
  106. long o;
  107. if(debug['v'])
  108. Bprint(&bso, "%5.2f follow\n", cputime());
  109. Bflush(&bso);
  110. firstp = prg();
  111. lastp = firstp;
  112. xfol(textp);
  113. lastp->link = P;
  114. firstp = firstp->link;
  115. o = 0; /* set */
  116. for(p = firstp; p != P; p = p->link) {
  117. if(p->as == ATEXT)
  118. curtext = p;
  119. p->stkoff = -1; /* initialization for stkoff */
  120. if(p->as == ATEXT) {
  121. p->stkoff = 0;
  122. o = p->to.offset;
  123. continue;
  124. }
  125. if(p->as == AADJSP && p->from.offset == 0) {
  126. p->stkoff = o;
  127. continue;
  128. }
  129. }
  130. }
  131. void
  132. xfol(Prog *p)
  133. {
  134. Prog *q;
  135. int i;
  136. enum as a;
  137. loop:
  138. if(p == P)
  139. return;
  140. if(p->as == ATEXT)
  141. curtext = p;
  142. if(p->as == ABRA)
  143. if((q = p->pcond) != P) {
  144. p->mark = 1;
  145. p = q;
  146. if(p->mark == 0)
  147. goto loop;
  148. }
  149. if(p->mark) {
  150. /* copy up to 4 instructions to avoid branch */
  151. for(i=0,q=p; i<4; i++,q=q->link) {
  152. if(q == P)
  153. break;
  154. if(q == lastp)
  155. break;
  156. a = q->as;
  157. if(a == ANOP) {
  158. i--;
  159. continue;
  160. }
  161. if(a == ABRA || a == ARTS || a == ARTE)
  162. break;
  163. if(q->pcond == P || q->pcond->mark)
  164. continue;
  165. if(a == ABSR || a == ADBF)
  166. continue;
  167. for(;;) {
  168. if(p->as == ANOP) {
  169. p = p->link;
  170. continue;
  171. }
  172. q = copyp(p);
  173. p = p->link;
  174. q->mark = 1;
  175. lastp->link = q;
  176. lastp = q;
  177. if(q->as != a || q->pcond == P || q->pcond->mark)
  178. continue;
  179. q->as = relinv(q->as);
  180. p = q->pcond;
  181. q->pcond = q->link;
  182. q->link = p;
  183. xfol(q->link);
  184. p = q->link;
  185. if(p->mark)
  186. return;
  187. goto loop;
  188. }
  189. } /* */
  190. q = prg();
  191. q->as = ABRA;
  192. q->line = p->line;
  193. q->to.type = D_BRANCH;
  194. q->to.offset = p->pc;
  195. q->pcond = p;
  196. p = q;
  197. }
  198. p->mark = 1;
  199. lastp->link = p;
  200. lastp = p;
  201. a = p->as;
  202. if(a == ARTS || a == ABRA || a == ARTE)
  203. return;
  204. if(p->pcond != P)
  205. if(a != ABSR) {
  206. q = brchain(p->link);
  207. if(q != P && q->mark)
  208. if(a != ADBF) {
  209. p->as = relinv(a);
  210. p->link = p->pcond;
  211. p->pcond = q;
  212. }
  213. xfol(p->link);
  214. q = brchain(p->pcond);
  215. if(q->mark) {
  216. p->pcond = q;
  217. return;
  218. }
  219. p = q;
  220. goto loop;
  221. }
  222. p = p->link;
  223. goto loop;
  224. }
  225. int
  226. relinv(int a)
  227. {
  228. switch(a) {
  229. case ABEQ: return ABNE;
  230. case ABNE: return ABEQ;
  231. case ABLE: return ABGT;
  232. case ABLS: return ABHI;
  233. case ABLT: return ABGE;
  234. case ABMI: return ABPL;
  235. case ABGE: return ABLT;
  236. case ABPL: return ABMI;
  237. case ABGT: return ABLE;
  238. case ABHI: return ABLS;
  239. case ABCS: return ABCC;
  240. case ABCC: return ABCS;
  241. case AFBEQ: return AFBNE;
  242. case AFBF: return AFBT;
  243. case AFBGE: return AFBLT;
  244. case AFBGT: return AFBLE;
  245. case AFBLE: return AFBGT;
  246. case AFBLT: return AFBGE;
  247. case AFBNE: return AFBEQ;
  248. case AFBT: return AFBF;
  249. }
  250. diag("unknown relation: %s in %s\n", anames[a], TNAME);
  251. return a;
  252. }
  253. void
  254. patch(void)
  255. {
  256. long c;
  257. Prog *p, *q;
  258. Sym *s;
  259. long vexit;
  260. if(debug['v'])
  261. Bprint(&bso, "%5.2f mkfwd\n", cputime());
  262. Bflush(&bso);
  263. mkfwd();
  264. if(debug['v'])
  265. Bprint(&bso, "%5.2f patch\n", cputime());
  266. Bflush(&bso);
  267. s = lookup("exit", 0);
  268. vexit = s->value;
  269. for(p = firstp; p != P; p = p->link) {
  270. if(p->as == ATEXT)
  271. curtext = p;
  272. if((p->as == ABSR || p->as == ARTS) && p->to.sym != S) {
  273. s = p->to.sym;
  274. if(s->type != STEXT) {
  275. diag("undefined: %s in %s\n", s->name, TNAME);
  276. s->type = STEXT;
  277. s->value = vexit;
  278. }
  279. p->to.offset = s->value;
  280. p->to.type = D_BRANCH;
  281. }
  282. if(p->to.type != D_BRANCH)
  283. continue;
  284. c = p->to.offset;
  285. for(q = firstp; q != P;) {
  286. if(q->forwd != P)
  287. if(c >= q->forwd->pc) {
  288. q = q->forwd;
  289. continue;
  290. }
  291. if(c == q->pc)
  292. break;
  293. q = q->link;
  294. }
  295. if(q == P) {
  296. diag("branch out of range in %s\n%P\n", TNAME, p);
  297. p->to.type = D_NONE;
  298. }
  299. p->pcond = q;
  300. }
  301. for(p = firstp; p != P; p = p->link) {
  302. if(p->as == ATEXT)
  303. curtext = p;
  304. p->mark = 0; /* initialization for follow */
  305. if(p->pcond != P) {
  306. p->pcond = brloop(p->pcond);
  307. if(p->pcond != P)
  308. if(p->to.type == D_BRANCH)
  309. p->to.offset = p->pcond->pc;
  310. }
  311. }
  312. }
  313. #define LOG 5
  314. void
  315. mkfwd(void)
  316. {
  317. Prog *p;
  318. int i;
  319. long dwn[LOG], cnt[LOG];
  320. Prog *lst[LOG];
  321. for(i=0; i<LOG; i++) {
  322. if(i == 0)
  323. cnt[i] = 1; else
  324. cnt[i] = LOG * cnt[i-1];
  325. dwn[i] = 1;
  326. lst[i] = P;
  327. }
  328. i = 0;
  329. for(p = firstp; p != P; p = p->link) {
  330. if(p->as == ATEXT)
  331. curtext = p;
  332. i--;
  333. if(i < 0)
  334. i = LOG-1;
  335. p->forwd = P;
  336. dwn[i]--;
  337. if(dwn[i] <= 0) {
  338. dwn[i] = cnt[i];
  339. if(lst[i] != P)
  340. lst[i]->forwd = p;
  341. lst[i] = p;
  342. }
  343. }
  344. }
  345. Prog*
  346. brloop(Prog *p)
  347. {
  348. int c;
  349. Prog *q;
  350. c = 0;
  351. for(q = p; q != P; q = q->pcond) {
  352. if(q->as != ABRA)
  353. break;
  354. c++;
  355. if(c >= 5000)
  356. return P;
  357. }
  358. return q;
  359. }
  360. void
  361. dostkoff(void)
  362. {
  363. Prog *p, *q, *qq;
  364. long s, t;
  365. int a;
  366. Optab *o;
  367. if(debug['v'])
  368. Bprint(&bso, "%5.2f stkoff\n", cputime());
  369. Bflush(&bso);
  370. s = 0;
  371. for(p = firstp; p != P; p = p->link) {
  372. if(p->as == ATEXT) {
  373. curtext = p;
  374. s = p->to.offset;
  375. if(s == 0)
  376. continue;
  377. p = nprg(p);
  378. p->as = AADJSP;
  379. p->from.type = D_CONST;
  380. p->from.offset = s;
  381. p->stkoff = 0;
  382. continue;
  383. }
  384. t = 0;
  385. for(q = p; q != P; q = q->pcond) {
  386. if(q->as == ATEXT)
  387. break;
  388. if(q->stkoff >= 0)
  389. if(q->stkoff != s)
  390. diag("stack offset %ld is %ld sb %ld in %s\n%P\n",
  391. q->pc, q->stkoff, s, q, TNAME, p);
  392. q->stkoff = s;
  393. if(t++ > 100) {
  394. diag("loop in stack offset 1: %P", p);
  395. break;
  396. }
  397. }
  398. o = &optab[p->as];
  399. if(p->to.type == D_TOS)
  400. s -= o->dstsp;
  401. if(p->from.type == D_TOS)
  402. s -= o->srcsp;
  403. if(p->as == AADJSP)
  404. s += p->from.offset;
  405. if(p->as == APEA)
  406. s += 4;
  407. t = 0;
  408. for(q = p->link; q != P; q = q->pcond) {
  409. if(q->as == ATEXT) {
  410. q = P;
  411. break;
  412. }
  413. if(q->stkoff >= 0)
  414. break;
  415. if(t++ > 100) {
  416. diag("loop in stack offset 2: %P", p);
  417. break;
  418. }
  419. }
  420. if(q == P || q->stkoff == s)
  421. continue;
  422. if(p->as == ABRA || p->as == ARTS || p->as == ARTE) {
  423. s = q->stkoff;
  424. continue;
  425. }
  426. t = q->stkoff - s;
  427. s = q->stkoff;
  428. p = nprg(p);
  429. p->as = AADJSP;
  430. p->stkoff = s - t;
  431. p->from.type = D_CONST;
  432. p->from.offset = t;
  433. }
  434. if(debug['v'])
  435. Bprint(&bso, "%5.2f rewrite\n", cputime());
  436. Bflush(&bso);
  437. for(p = firstp; p != P; p = p->link) {
  438. if(p->as == ATEXT)
  439. curtext = p;
  440. a = p->from.type & D_MASK;
  441. if(a == D_AUTO)
  442. p->from.offset += p->stkoff;
  443. if(a == D_PARAM)
  444. p->from.offset += p->stkoff + 4;
  445. a = p->to.type & D_MASK;
  446. if(a == D_AUTO)
  447. p->to.offset += p->stkoff;
  448. if(a == D_PARAM)
  449. p->to.offset += p->stkoff + 4;
  450. switch(p->as) {
  451. default:
  452. continue;
  453. case AMOVW:
  454. if(p->from.type != D_CCR)
  455. continue;
  456. a = p->to.type;
  457. if((a < D_R0 || a > D_R0+7) && a != D_TOS)
  458. diag("bad dest for MOVCC %P", p);
  459. p->as = ALEA;
  460. p->from.type = I_INDIR|(D_A0+7);
  461. p->from.offset = -2;
  462. p->to.type = D_A0+7;
  463. p = nprg(p);
  464. p->as = ABSR;
  465. p->to.type = D_BRANCH;
  466. p->pcond = prog_ccr;
  467. p->to.sym = prog_ccr->from.sym;
  468. if(a != D_TOS) {
  469. p = nprg(p);
  470. p->as = AMOVW;
  471. p->from.type = D_TOS;
  472. p->to.type = a;
  473. }
  474. continue;
  475. case AEXTBL:
  476. a = p->to.type;
  477. if(a < D_R0 || a > D_R0+7)
  478. diag("bad dest for EXTB");
  479. p->as = AEXTBW;
  480. p = nprg(p);
  481. p->as = AEXTWL;
  482. p->to.type = a;
  483. continue;
  484. case AMULSL:
  485. case AMULUL:
  486. qq = prog_mull;
  487. goto mdcom;
  488. case ADIVSL:
  489. qq = prog_divsl;
  490. goto mdcom;
  491. case ADIVUL:
  492. qq = prog_divul;
  493. mdcom:
  494. if(debug['m'])
  495. continue;
  496. a = p->to.type;
  497. if(a < D_R0 || a > D_R0+7)
  498. diag("bad dest for mul/div");
  499. p->as = AMOVL;
  500. p->to.type = D_TOS;
  501. p = nprg(p);
  502. p->as = AMOVL;
  503. p->from.type = a;
  504. p->to.type = D_TOS;
  505. p = nprg(p);
  506. p->as = ABSR;
  507. p->to.type = D_BRANCH;
  508. p->pcond = qq;
  509. p->to.sym = qq->from.sym;
  510. p = nprg(p);
  511. p->as = AMOVL;
  512. p->from.type = D_TOS;
  513. p->to.type = a;
  514. p = nprg(p);
  515. p->as = AMOVL;
  516. p->from.type = D_TOS;
  517. p->to.type = a+1;
  518. if(qq == prog_mull)
  519. p->to.type = a;
  520. continue;
  521. case ARTS:
  522. break;
  523. }
  524. if(p->stkoff == 0)
  525. continue;
  526. p->as = AADJSP;
  527. p->from.type = D_CONST;
  528. p->from.offset = -p->stkoff;
  529. p = nprg(p);
  530. p->as = ARTS;
  531. p->stkoff = 0;
  532. }
  533. }
  534. long
  535. atolwhex(char *s)
  536. {
  537. long n;
  538. int f;
  539. n = 0;
  540. f = 0;
  541. while(*s == ' ' || *s == '\t')
  542. s++;
  543. if(*s == '-' || *s == '+') {
  544. if(*s++ == '-')
  545. f = 1;
  546. while(*s == ' ' || *s == '\t')
  547. s++;
  548. }
  549. if(s[0]=='0' && s[1]){
  550. if(s[1]=='x' || s[1]=='X'){
  551. s += 2;
  552. for(;;){
  553. if(*s >= '0' && *s <= '9')
  554. n = n*16 + *s++ - '0';
  555. else if(*s >= 'a' && *s <= 'f')
  556. n = n*16 + *s++ - 'a' + 10;
  557. else if(*s >= 'A' && *s <= 'F')
  558. n = n*16 + *s++ - 'A' + 10;
  559. else
  560. break;
  561. }
  562. } else
  563. while(*s >= '0' && *s <= '7')
  564. n = n*8 + *s++ - '0';
  565. } else
  566. while(*s >= '0' && *s <= '9')
  567. n = n*10 + *s++ - '0';
  568. if(f)
  569. n = -n;
  570. return n;
  571. }
  572. void
  573. undef(void)
  574. {
  575. int i;
  576. Sym *s;
  577. for(i=0; i<NHASH; i++)
  578. for(s = hash[i]; s != S; s = s->link)
  579. if(s->type == SXREF)
  580. diag("%s: not defined\n", s->name);
  581. }
  582. void
  583. initmuldiv1(void)
  584. {
  585. lookup("_mull", 0)->type = SXREF;
  586. lookup("_divsl", 0)->type = SXREF;
  587. lookup("_divul", 0)->type = SXREF;
  588. lookup("_ccr", 0)->type = SXREF;
  589. }
  590. void
  591. initmuldiv2(void)
  592. {
  593. Sym *s1, *s2, *s3, *s4;
  594. Prog *p;
  595. if(prog_mull != P)
  596. return;
  597. s1 = lookup("_mull", 0);
  598. s2 = lookup("_divsl", 0);
  599. s3 = lookup("_divul", 0);
  600. s4 = lookup("_ccr", 0);
  601. for(p = firstp; p != P; p = p->link)
  602. if(p->as == ATEXT) {
  603. if(p->from.sym == s1)
  604. prog_mull = p;
  605. if(p->from.sym == s2)
  606. prog_divsl = p;
  607. if(p->from.sym == s3)
  608. prog_divul = p;
  609. if(p->from.sym == s4)
  610. prog_ccr = p;
  611. }
  612. if(prog_mull == P) {
  613. diag("undefined: %s\n", s1->name);
  614. prog_mull = curtext;
  615. }
  616. if(prog_divsl == P) {
  617. diag("undefined: %s\n", s2->name);
  618. prog_divsl = curtext;
  619. }
  620. if(prog_divul == P) {
  621. diag("undefined: %s\n", s3->name);
  622. prog_divul = curtext;
  623. }
  624. if(prog_ccr == P) {
  625. diag("undefined: %s\n", s4->name);
  626. prog_ccr = curtext;
  627. }
  628. }