pass.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. #include "l.h"
  2. void
  3. dodata(void)
  4. {
  5. int i, t;
  6. Sym *s;
  7. Prog *p, *p1;
  8. long orig, orig1, v;
  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(p->as == ADYNT || p->as == AINIT)
  15. s->value = dtype;
  16. if(s->type == SBSS)
  17. s->type = SDATA;
  18. if(s->type != SDATA)
  19. diag("initialize non-data (%d): %s\n%P",
  20. s->type, s->name, p);
  21. v = p->from.offset + p->reg;
  22. if(v > s->value)
  23. diag("initialize bounds (%ld): %s\n%P",
  24. s->value, s->name, p);
  25. }
  26. /*
  27. * pass 1
  28. * assign 'small' variables to data segment
  29. * (rational is that data segment is more easily
  30. * addressed through offset on REGSB)
  31. */
  32. orig = 0;
  33. for(i=0; i<NHASH; i++)
  34. for(s = hash[i]; s != S; s = s->link) {
  35. t = s->type;
  36. if(t != SDATA && t != SBSS)
  37. continue;
  38. v = s->value;
  39. if(v == 0) {
  40. diag("%s: no size", s->name);
  41. v = 1;
  42. }
  43. while(v & 3)
  44. v++;
  45. s->value = v;
  46. if(v > MINSIZ)
  47. continue;
  48. if(v >= 8)
  49. while(orig & 7)
  50. orig++;
  51. s->value = orig;
  52. orig += v;
  53. s->type = SDATA1;
  54. }
  55. orig1 = orig;
  56. /*
  57. * pass 2
  58. * assign 'data' variables to data segment
  59. */
  60. for(i=0; i<NHASH; i++)
  61. for(s = hash[i]; s != S; s = s->link) {
  62. t = s->type;
  63. if(t != SDATA) {
  64. if(t == SDATA1)
  65. s->type = SDATA;
  66. continue;
  67. }
  68. v = s->value;
  69. if(v >= 8)
  70. while(orig & 7)
  71. orig++;
  72. s->value = orig;
  73. orig += v;
  74. s->type = SDATA1;
  75. }
  76. while(orig & 7)
  77. orig++;
  78. datsize = orig;
  79. /*
  80. * pass 3
  81. * everything else to bss segment
  82. */
  83. for(i=0; i<NHASH; i++)
  84. for(s = hash[i]; s != S; s = s->link) {
  85. if(s->type != SBSS)
  86. continue;
  87. v = s->value;
  88. if(v >= 8)
  89. while(orig & 7)
  90. orig++;
  91. s->value = orig;
  92. orig += v;
  93. }
  94. while(orig & 7)
  95. orig++;
  96. bsssize = orig-datsize;
  97. /*
  98. * pass 4
  99. * add literals to all large values.
  100. * at this time:
  101. * small data is allocated DATA
  102. * large data is allocated DATA1
  103. * large bss is allocated BSS
  104. * the new literals are loaded between
  105. * small data and large data.
  106. */
  107. orig = 0;
  108. for(p = firstp; p != P; p = p->link) {
  109. if(p->as != AMOVW)
  110. continue;
  111. if(p->from.type != D_CONST)
  112. continue;
  113. if(s = p->from.sym) {
  114. t = s->type;
  115. if(t != SDATA && t != SDATA1 && t != SBSS)
  116. continue;
  117. t = p->from.name;
  118. if(t != D_EXTERN && t != D_STATIC)
  119. continue;
  120. v = s->value + p->from.offset;
  121. if(v >= 0 && v <= 0xffff)
  122. continue;
  123. if(!strcmp(s->name, "setSB"))
  124. continue;
  125. /* size should be 19 max */
  126. if(strlen(s->name) >= 10) /* has loader address */
  127. sprint(literal, "$%p.%lux", s, p->from.offset);
  128. else
  129. sprint(literal, "$%s.%d.%lux", s->name, s->version, p->from.offset);
  130. } else {
  131. if(p->from.name != D_NONE)
  132. continue;
  133. if(p->from.reg != NREG)
  134. continue;
  135. v = p->from.offset;
  136. if(v >= -0x7fff-1 && v <= 0x7fff)
  137. continue;
  138. if(!(v & 0xffff))
  139. continue;
  140. if(v)
  141. continue; /* quicker to build it than load it */
  142. /* size should be 9 max */
  143. sprint(literal, "$%lux", v);
  144. }
  145. s = lookup(literal, 0);
  146. if(s->type == 0) {
  147. s->type = SDATA;
  148. s->value = orig1+orig;
  149. orig += 4;
  150. p1 = prg();
  151. p1->as = ADATA;
  152. p1->line = p->line;
  153. p1->from.type = D_OREG;
  154. p1->from.sym = s;
  155. p1->from.name = D_EXTERN;
  156. p1->reg = 4;
  157. p1->to = p->from;
  158. p1->link = datap;
  159. datap = p1;
  160. }
  161. if(s->type != SDATA)
  162. diag("literal not data: %s", s->name);
  163. p->from.type = D_OREG;
  164. p->from.sym = s;
  165. p->from.name = D_EXTERN;
  166. p->from.offset = 0;
  167. continue;
  168. }
  169. while(orig & 7)
  170. orig++;
  171. /*
  172. * pass 5
  173. * re-adjust offsets
  174. */
  175. for(i=0; i<NHASH; i++)
  176. for(s = hash[i]; s != S; s = s->link) {
  177. t = s->type;
  178. if(t == SBSS) {
  179. s->value += orig;
  180. continue;
  181. }
  182. if(t == SDATA1) {
  183. s->type = SDATA;
  184. s->value += orig;
  185. continue;
  186. }
  187. }
  188. datsize += orig;
  189. xdefine("setSB", SDATA, 0L+BIG);
  190. xdefine("bdata", SDATA, 0L);
  191. xdefine("edata", SDATA, datsize);
  192. xdefine("end", SBSS, datsize+bsssize);
  193. xdefine("etext", STEXT, 0L);
  194. }
  195. void
  196. undef(void)
  197. {
  198. int i;
  199. Sym *s;
  200. for(i=0; i<NHASH; i++)
  201. for(s = hash[i]; s != S; s = s->link)
  202. if(s->type == SXREF)
  203. diag("%s: not defined", s->name);
  204. }
  205. int
  206. relinv(int a)
  207. {
  208. switch(a) {
  209. case ABEQ: return ABNE;
  210. case ABNE: return ABEQ;
  211. case ABGE: return ABLT;
  212. case ABLT: return ABGE;
  213. case ABGT: return ABLE;
  214. case ABLE: return ABGT;
  215. case ABVC: return ABVS;
  216. case ABVS: return ABVC;
  217. }
  218. return 0;
  219. }
  220. void
  221. follow(void)
  222. {
  223. if(debug['v'])
  224. Bprint(&bso, "%5.2f follow\n", cputime());
  225. Bflush(&bso);
  226. firstp = prg();
  227. lastp = firstp;
  228. xfol(textp);
  229. firstp = firstp->link;
  230. lastp->link = P;
  231. }
  232. void
  233. xfol(Prog *p)
  234. {
  235. Prog *q, *r;
  236. int a, b, i;
  237. loop:
  238. if(p == P)
  239. return;
  240. a = p->as;
  241. if(a == ATEXT)
  242. curtext = p;
  243. if(a == ABR) {
  244. q = p->cond;
  245. if((p->mark&NOSCHED) || q && (q->mark&NOSCHED)){
  246. p->mark |= FOLL;
  247. lastp->link = p;
  248. lastp = p;
  249. p = p->link;
  250. xfol(p);
  251. p = q;
  252. if(p && !(p->mark & FOLL))
  253. goto loop;
  254. return;
  255. }
  256. if(q != P) {
  257. p->mark |= FOLL;
  258. p = q;
  259. if(!(p->mark & FOLL))
  260. goto loop;
  261. }
  262. }
  263. if(p->mark & FOLL) {
  264. for(i=0,q=p; i<4; i++,q=q->link) {
  265. if(q == lastp || (q->mark&NOSCHED))
  266. break;
  267. b = 0; /* set */
  268. a = q->as;
  269. if(a == ANOP) {
  270. i--;
  271. continue;
  272. }
  273. if(a == ABR || a == ARETURN || a == ARFI || a == ARFCI)
  274. goto copy;
  275. if(!q->cond || (q->cond->mark&FOLL))
  276. continue;
  277. b = relinv(a);
  278. if(!b)
  279. continue;
  280. copy:
  281. for(;;) {
  282. r = prg();
  283. *r = *p;
  284. if(!(r->mark&FOLL))
  285. print("cant happen 1\n");
  286. r->mark |= FOLL;
  287. if(p != q) {
  288. p = p->link;
  289. lastp->link = r;
  290. lastp = r;
  291. continue;
  292. }
  293. lastp->link = r;
  294. lastp = r;
  295. if(a == ABR || a == ARETURN || a == ARFI || a == ARFCI)
  296. return;
  297. r->as = b;
  298. r->cond = p->link;
  299. r->link = p->cond;
  300. if(!(r->link->mark&FOLL))
  301. xfol(r->link);
  302. if(!(r->cond->mark&FOLL))
  303. print("cant happen 2\n");
  304. return;
  305. }
  306. }
  307. a = ABR;
  308. q = prg();
  309. q->as = a;
  310. q->line = p->line;
  311. q->to.type = D_BRANCH;
  312. q->to.offset = p->pc;
  313. q->cond = p;
  314. p = q;
  315. }
  316. p->mark |= FOLL;
  317. lastp->link = p;
  318. lastp = p;
  319. if(a == ABR || a == ARETURN || a == ARFI || a == ARFCI){
  320. if(p->mark & NOSCHED){
  321. p = p->link;
  322. goto loop;
  323. }
  324. return;
  325. }
  326. if(p->cond != P)
  327. if(a != ABL && p->link != P) {
  328. xfol(p->link);
  329. p = p->cond;
  330. if(p == P || (p->mark&FOLL))
  331. return;
  332. goto loop;
  333. }
  334. p = p->link;
  335. goto loop;
  336. }
  337. void
  338. patch(void)
  339. {
  340. long c, vexit;
  341. Prog *p, *q;
  342. Sym *s;
  343. int a;
  344. if(debug['v'])
  345. Bprint(&bso, "%5.2f patch\n", cputime());
  346. Bflush(&bso);
  347. mkfwd();
  348. s = lookup("exit", 0);
  349. vexit = s->value;
  350. for(p = firstp; p != P; p = p->link) {
  351. a = p->as;
  352. if(a == ATEXT)
  353. curtext = p;
  354. if((a == ABL || a == ARETURN) && p->to.sym != S) {
  355. s = p->to.sym;
  356. if(s->type != STEXT && s->type != SUNDEF) {
  357. diag("undefined: %s\n%P", s->name, p);
  358. s->type = STEXT;
  359. s->value = vexit;
  360. }
  361. if(s->type == SUNDEF){
  362. p->to.offset = 0;
  363. p->cond = UP;
  364. }
  365. else
  366. p->to.offset = s->value;
  367. p->to.type = D_BRANCH;
  368. }
  369. if(p->to.type != D_BRANCH || p->cond == UP)
  370. continue;
  371. c = p->to.offset;
  372. for(q = firstp; q != P;) {
  373. if(q->forwd != P)
  374. if(c >= q->forwd->pc) {
  375. q = q->forwd;
  376. continue;
  377. }
  378. if(c == q->pc)
  379. break;
  380. q = q->link;
  381. }
  382. if(q == P) {
  383. diag("branch out of range %ld\n%P", c, p);
  384. p->to.type = D_NONE;
  385. }
  386. p->cond = q;
  387. }
  388. for(p = firstp; p != P; p = p->link) {
  389. if(p->as == ATEXT)
  390. curtext = p;
  391. p->mark = 0; /* initialization for follow */
  392. if(p->cond != P && p->cond != UP) {
  393. p->cond = brloop(p->cond);
  394. if(p->cond != P)
  395. if(p->to.type == D_BRANCH)
  396. p->to.offset = p->cond->pc;
  397. }
  398. }
  399. }
  400. #define LOG 5
  401. void
  402. mkfwd(void)
  403. {
  404. Prog *p;
  405. long dwn[LOG], cnt[LOG], i;
  406. Prog *lst[LOG];
  407. for(i=0; i<LOG; i++) {
  408. if(i == 0)
  409. cnt[i] = 1; else
  410. cnt[i] = LOG * cnt[i-1];
  411. dwn[i] = 1;
  412. lst[i] = P;
  413. }
  414. i = 0;
  415. for(p = firstp; p != P; p = p->link) {
  416. if(p->as == ATEXT)
  417. curtext = p;
  418. i--;
  419. if(i < 0)
  420. i = LOG-1;
  421. p->forwd = P;
  422. dwn[i]--;
  423. if(dwn[i] <= 0) {
  424. dwn[i] = cnt[i];
  425. if(lst[i] != P)
  426. lst[i]->forwd = p;
  427. lst[i] = p;
  428. }
  429. }
  430. }
  431. Prog*
  432. brloop(Prog *p)
  433. {
  434. Prog *q;
  435. int c;
  436. for(c=0; p!=P;) {
  437. if(p->as != ABR || (p->mark&NOSCHED))
  438. return p;
  439. q = p->cond;
  440. if(q <= p) {
  441. c++;
  442. if(q == p || c > 5000)
  443. break;
  444. }
  445. p = q;
  446. }
  447. return P;
  448. }
  449. long
  450. atolwhex(char *s)
  451. {
  452. long n;
  453. int f;
  454. n = 0;
  455. f = 0;
  456. while(*s == ' ' || *s == '\t')
  457. s++;
  458. if(*s == '-' || *s == '+') {
  459. if(*s++ == '-')
  460. f = 1;
  461. while(*s == ' ' || *s == '\t')
  462. s++;
  463. }
  464. if(s[0]=='0' && s[1]){
  465. if(s[1]=='x' || s[1]=='X'){
  466. s += 2;
  467. for(;;){
  468. if(*s >= '0' && *s <= '9')
  469. n = n*16 + *s++ - '0';
  470. else if(*s >= 'a' && *s <= 'f')
  471. n = n*16 + *s++ - 'a' + 10;
  472. else if(*s >= 'A' && *s <= 'F')
  473. n = n*16 + *s++ - 'A' + 10;
  474. else
  475. break;
  476. }
  477. } else
  478. while(*s >= '0' && *s <= '7')
  479. n = n*8 + *s++ - '0';
  480. } else
  481. while(*s >= '0' && *s <= '9')
  482. n = n*10 + *s++ - '0';
  483. if(f)
  484. n = -n;
  485. return n;
  486. }
  487. long
  488. rnd(long v, long r)
  489. {
  490. long c;
  491. if(r <= 0)
  492. return v;
  493. v += r - 1;
  494. c = v % r;
  495. if(c < 0)
  496. c += r;
  497. v -= c;
  498. return v;
  499. }
  500. void
  501. import(void)
  502. {
  503. int i;
  504. Sym *s;
  505. for(i = 0; i < NHASH; i++)
  506. for(s = hash[i]; s != S; s = s->link)
  507. if(s->sig != 0 && s->type == SXREF && (nimports == 0 || s->subtype == SIMPORT)){
  508. undefsym(s);
  509. Bprint(&bso, "IMPORT: %s sig=%lux v=%ld\n", s->name, s->sig, s->value);
  510. }
  511. }
  512. void
  513. ckoff(Sym *s, long v)
  514. {
  515. if(v < 0 || v >= 1<<Roffset)
  516. diag("relocation offset %ld for %s out of range", v, s->name);
  517. }
  518. static Prog*
  519. newdata(Sym *s, int o, int w, int t)
  520. {
  521. Prog *p;
  522. p = prg();
  523. p->link = datap;
  524. datap = p;
  525. p->as = ADATA;
  526. p->reg = w;
  527. p->from.type = D_OREG;
  528. p->from.name = t;
  529. p->from.sym = s;
  530. p->from.offset = o;
  531. p->to.type = D_CONST;
  532. p->to.name = D_NONE;
  533. return p;
  534. }
  535. void
  536. export(void)
  537. {
  538. int i, j, n, off, nb, sv, ne;
  539. Sym *s, *et, *str, **esyms;
  540. Prog *p;
  541. char buf[NSNAME], *t;
  542. n = 0;
  543. for(i = 0; i < NHASH; i++)
  544. for(s = hash[i]; s != S; s = s->link)
  545. if(s->sig != 0 && s->type != SXREF && s->type != SUNDEF && (nexports == 0 || s->subtype == SEXPORT))
  546. n++;
  547. esyms = malloc(n*sizeof(Sym*));
  548. ne = n;
  549. n = 0;
  550. for(i = 0; i < NHASH; i++)
  551. for(s = hash[i]; s != S; s = s->link)
  552. if(s->sig != 0 && s->type != SXREF && s->type != SUNDEF && (nexports == 0 || s->subtype == SEXPORT))
  553. esyms[n++] = s;
  554. for(i = 0; i < ne-1; i++)
  555. for(j = i+1; j < ne; j++)
  556. if(strcmp(esyms[i]->name, esyms[j]->name) > 0){
  557. s = esyms[i];
  558. esyms[i] = esyms[j];
  559. esyms[j] = s;
  560. }
  561. nb = 0;
  562. off = 0;
  563. et = lookup(EXPTAB, 0);
  564. if(et->type != 0 && et->type != SXREF)
  565. diag("%s already defined", EXPTAB);
  566. et->type = SDATA;
  567. str = lookup(".string", 0);
  568. if(str->type == 0)
  569. str->type = SDATA;
  570. sv = str->value;
  571. for(i = 0; i < ne; i++){
  572. s = esyms[i];
  573. Bprint(&bso, "EXPORT: %s sig=%lux t=%d\n", s->name, s->sig, s->type);
  574. /* signature */
  575. p = newdata(et, off, sizeof(long), D_EXTERN);
  576. off += sizeof(long);
  577. p->to.offset = s->sig;
  578. /* address */
  579. p = newdata(et, off, sizeof(long), D_EXTERN);
  580. off += sizeof(long);
  581. p->to.name = D_EXTERN;
  582. p->to.sym = s;
  583. /* string */
  584. t = s->name;
  585. n = strlen(t)+1;
  586. for(;;){
  587. buf[nb++] = *t;
  588. sv++;
  589. if(nb >= NSNAME){
  590. p = newdata(str, sv-NSNAME, NSNAME, D_STATIC);
  591. p->to.type = D_SCONST;
  592. memmove(p->to.sval, buf, NSNAME);
  593. nb = 0;
  594. }
  595. if(*t++ == 0)
  596. break;
  597. }
  598. /* name */
  599. p = newdata(et, off, sizeof(long), D_EXTERN);
  600. off += sizeof(long);
  601. p->to.name = D_STATIC;
  602. p->to.sym = str;
  603. p->to.offset = sv-n;
  604. }
  605. if(nb > 0){
  606. p = newdata(str, sv-nb, nb, D_STATIC);
  607. p->to.type = D_SCONST;
  608. memmove(p->to.sval, buf, nb);
  609. }
  610. for(i = 0; i < 3; i++){
  611. newdata(et, off, sizeof(long), D_EXTERN);
  612. off += sizeof(long);
  613. }
  614. et->value = off;
  615. if(sv == 0)
  616. sv = 1;
  617. str->value = sv;
  618. exports = ne;
  619. free(esyms);
  620. }