pass.c 12 KB

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