pass.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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\n",
  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\n",
  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\n", 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, "$%lux.%lux", (long)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\n", 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)
  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)
  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){
  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) {
  357. diag("undefined: %s\n%P\n", s->name, p);
  358. s->type = STEXT;
  359. s->value = vexit;
  360. }
  361. p->to.offset = s->value;
  362. p->to.type = D_BRANCH;
  363. }
  364. if(p->to.type != D_BRANCH)
  365. continue;
  366. c = p->to.offset;
  367. for(q = firstp; q != P;) {
  368. if(q->forwd != P)
  369. if(c >= q->forwd->pc) {
  370. q = q->forwd;
  371. continue;
  372. }
  373. if(c == q->pc)
  374. break;
  375. q = q->link;
  376. }
  377. if(q == P) {
  378. diag("branch out of range %ld\n%P\n", c, p);
  379. p->to.type = D_NONE;
  380. }
  381. p->cond = q;
  382. }
  383. for(p = firstp; p != P; p = p->link) {
  384. if(p->as == ATEXT)
  385. curtext = p;
  386. if(p->cond != P) {
  387. p->cond = brloop(p->cond);
  388. if(p->cond != P)
  389. if(p->to.type == D_BRANCH)
  390. p->to.offset = p->cond->pc;
  391. }
  392. }
  393. }
  394. #define LOG 5
  395. void
  396. mkfwd(void)
  397. {
  398. Prog *p;
  399. long dwn[LOG], cnt[LOG], i;
  400. Prog *lst[LOG];
  401. for(i=0; i<LOG; i++) {
  402. if(i == 0)
  403. cnt[i] = 1; else
  404. cnt[i] = LOG * cnt[i-1];
  405. dwn[i] = 1;
  406. lst[i] = P;
  407. }
  408. i = 0;
  409. for(p = firstp; p != P; p = p->link) {
  410. if(p->as == ATEXT)
  411. curtext = p;
  412. i--;
  413. if(i < 0)
  414. i = LOG-1;
  415. p->forwd = P;
  416. dwn[i]--;
  417. if(dwn[i] <= 0) {
  418. dwn[i] = cnt[i];
  419. if(lst[i] != P)
  420. lst[i]->forwd = p;
  421. lst[i] = p;
  422. }
  423. }
  424. }
  425. Prog*
  426. brloop(Prog *p)
  427. {
  428. Prog *q;
  429. int c;
  430. for(c=0; p!=P;) {
  431. if(p->as != ABR || (p->mark&NOSCHED))
  432. return p;
  433. q = p->cond;
  434. if(q <= p) {
  435. c++;
  436. if(q == p || c > 5000)
  437. break;
  438. }
  439. p = q;
  440. }
  441. return P;
  442. }
  443. long
  444. atolwhex(char *s)
  445. {
  446. long n;
  447. int f;
  448. n = 0;
  449. f = 0;
  450. while(*s == ' ' || *s == '\t')
  451. s++;
  452. if(*s == '-' || *s == '+') {
  453. if(*s++ == '-')
  454. f = 1;
  455. while(*s == ' ' || *s == '\t')
  456. s++;
  457. }
  458. if(s[0]=='0' && s[1]){
  459. if(s[1]=='x' || s[1]=='X'){
  460. s += 2;
  461. for(;;){
  462. if(*s >= '0' && *s <= '9')
  463. n = n*16 + *s++ - '0';
  464. else if(*s >= 'a' && *s <= 'f')
  465. n = n*16 + *s++ - 'a' + 10;
  466. else if(*s >= 'A' && *s <= 'F')
  467. n = n*16 + *s++ - 'A' + 10;
  468. else
  469. break;
  470. }
  471. } else
  472. while(*s >= '0' && *s <= '7')
  473. n = n*8 + *s++ - '0';
  474. } else
  475. while(*s >= '0' && *s <= '9')
  476. n = n*10 + *s++ - '0';
  477. if(f)
  478. n = -n;
  479. return n;
  480. }
  481. long
  482. rnd(long v, long r)
  483. {
  484. long c;
  485. if(r <= 0)
  486. return v;
  487. v += r - 1;
  488. c = v % r;
  489. if(c < 0)
  490. c += r;
  491. v -= c;
  492. return v;
  493. }