pass.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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 (%lld): %s\n%P",
  32. s->value, s->name, p);
  33. }
  34. if(debug['t']) {
  35. /*
  36. * pull out string constants
  37. */
  38. for(p = datap; p != P; p = p->link) {
  39. s = p->from.sym;
  40. if(p->to.type == D_SCONST)
  41. s->type = SSTRING;
  42. }
  43. }
  44. /*
  45. * pass 1
  46. * assign 'small' variables to data segment
  47. * (rational is that data segment is more easily
  48. * addressed through offset on R30)
  49. */
  50. orig = 0;
  51. for(i=0; i<NHASH; i++)
  52. for(s = hash[i]; s != S; s = s->link) {
  53. t = s->type;
  54. if(t != SDATA && t != SBSS)
  55. continue;
  56. v = s->value;
  57. if(v == 0) {
  58. diag("%s: no size", s->name);
  59. v = 1;
  60. }
  61. while(v & 3)
  62. v++;
  63. s->value = v;
  64. if(v > MINSIZ)
  65. continue;
  66. s->value = orig;
  67. orig += v;
  68. s->type = SDATA1;
  69. }
  70. orig1 = orig;
  71. /*
  72. * pass 2
  73. * assign 'data' variables to data segment
  74. */
  75. for(i=0; i<NHASH; i++)
  76. for(s = hash[i]; s != S; s = s->link) {
  77. t = s->type;
  78. if(t != SDATA) {
  79. if(t == SDATA1)
  80. s->type = SDATA;
  81. continue;
  82. }
  83. v = s->value;
  84. s->value = orig;
  85. orig += v;
  86. s->type = SDATA1;
  87. }
  88. while(orig & 7)
  89. orig++;
  90. datsize = orig;
  91. /*
  92. * pass 3
  93. * everything else to bss segment
  94. */
  95. for(i=0; i<NHASH; i++)
  96. for(s = hash[i]; s != S; s = s->link) {
  97. if(s->type != SBSS)
  98. continue;
  99. v = s->value;
  100. s->value = orig;
  101. orig += v;
  102. }
  103. while(orig & 7)
  104. orig++;
  105. bsssize = orig-datsize;
  106. /*
  107. * pass 4
  108. * add literals to all large values.
  109. * at this time:
  110. * small data is allocated DATA
  111. * large data is allocated DATA1
  112. * large bss is allocated BSS
  113. * the new literals are loaded between
  114. * small data and large data.
  115. */
  116. orig = 0;
  117. for(p = firstp; p != P; p = p->link) {
  118. if(p->as != AMOVW)
  119. continue;
  120. if(p->from.type != D_CONST)
  121. continue;
  122. if(s = p->from.sym) {
  123. t = s->type;
  124. if(t != SDATA && t != SDATA1 && t != SBSS)
  125. continue;
  126. t = p->from.name;
  127. if(t != D_EXTERN && t != D_STATIC)
  128. continue;
  129. v = s->value + p->from.offset;
  130. if(v >= 0 && v <= 0xffff)
  131. continue;
  132. if(!strcmp(s->name, "setR30"))
  133. continue;
  134. /* size should be 19 max */
  135. if(strlen(s->name) >= 10) /* has loader address */
  136. sprint(literal, "$%p.%lux", s, p->from.offset);
  137. else
  138. sprint(literal, "$%s.%d.%lux", s->name,
  139. s->version, p->from.offset);
  140. } else {
  141. if(p->from.name != D_NONE)
  142. continue;
  143. if(p->from.reg != NREG)
  144. continue;
  145. v = p->from.offset;
  146. if(v >= -0x7fff && v <= 0xffff)
  147. continue;
  148. if(!(v & 0xffff))
  149. continue;
  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->line = p->line;
  160. p1->as = ADATA;
  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. nocache(p);
  176. continue;
  177. }
  178. while(orig & 7)
  179. orig++;
  180. /*
  181. * pass 5
  182. * re-adjust offsets
  183. */
  184. for(i=0; i<NHASH; i++)
  185. for(s = hash[i]; s != S; s = s->link) {
  186. t = s->type;
  187. if(t == SBSS) {
  188. s->value += orig;
  189. continue;
  190. }
  191. if(t == SDATA1) {
  192. s->type = SDATA;
  193. s->value += orig;
  194. continue;
  195. }
  196. }
  197. datsize += orig;
  198. xdefine("setR30", SDATA, 0L+BIG);
  199. xdefine("bdata", SDATA, 0L);
  200. xdefine("edata", SDATA, datsize);
  201. xdefine("end", SBSS, datsize+bsssize);
  202. xdefine("etext", STEXT, 0L);
  203. }
  204. void
  205. undef(void)
  206. {
  207. int i;
  208. Sym *s;
  209. for(i=0; i<NHASH; i++)
  210. for(s = hash[i]; s != S; s = s->link)
  211. if(s->type == SXREF)
  212. diag("%s: not defined", s->name);
  213. }
  214. void
  215. follow(void)
  216. {
  217. if(debug['v'])
  218. Bprint(&bso, "%5.2f follow\n", cputime());
  219. Bflush(&bso);
  220. firstp = prg();
  221. lastp = firstp;
  222. xfol(textp);
  223. firstp = firstp->link;
  224. lastp->link = P;
  225. }
  226. void
  227. xfol(Prog *p)
  228. {
  229. Prog *q, *r;
  230. int a, i;
  231. loop:
  232. if(p == P)
  233. return;
  234. a = p->as;
  235. if(a == ATEXT)
  236. curtext = p;
  237. if(a == AJMP) {
  238. q = p->cond;
  239. if((p->mark&NOSCHED) || q && (q->mark&NOSCHED)){
  240. p->mark |= FOLL;
  241. lastp->link = p;
  242. lastp = p;
  243. p = p->link;
  244. xfol(p);
  245. p = q;
  246. if(p && !(p->mark & FOLL))
  247. goto loop;
  248. return;
  249. }
  250. if(q != P) {
  251. p->mark |= FOLL;
  252. p = q;
  253. if(!(p->mark & FOLL))
  254. goto loop;
  255. }
  256. }
  257. if(p->mark & FOLL) {
  258. for(i=0,q=p; i<4; i++,q=q->link) {
  259. if(q == lastp || (q->mark&NOSCHED))
  260. break;
  261. a = q->as;
  262. if(a == ANOP) {
  263. i--;
  264. continue;
  265. }
  266. if(a == AJMP || a == ARET || a == ARFE)
  267. goto copy;
  268. if(!q->cond || (q->cond->mark&FOLL))
  269. continue;
  270. if(a != ABEQ && a != ABNE)
  271. continue;
  272. copy:
  273. for(;;) {
  274. r = prg();
  275. *r = *p;
  276. if(!(r->mark&FOLL))
  277. print("cant happen 1\n");
  278. r->mark |= FOLL;
  279. if(p != q) {
  280. p = p->link;
  281. lastp->link = r;
  282. lastp = r;
  283. continue;
  284. }
  285. lastp->link = r;
  286. lastp = r;
  287. if(a == AJMP || a == ARET || a == ARFE)
  288. return;
  289. r->as = ABNE;
  290. if(a == ABNE)
  291. r->as = ABEQ;
  292. r->cond = p->link;
  293. r->link = p->cond;
  294. if(!(r->link->mark&FOLL))
  295. xfol(r->link);
  296. if(!(r->cond->mark&FOLL))
  297. print("cant happen 2\n");
  298. return;
  299. }
  300. }
  301. a = AJMP;
  302. q = prg();
  303. q->as = a;
  304. q->line = p->line;
  305. q->to.type = D_BRANCH;
  306. q->to.offset = p->pc;
  307. q->cond = p;
  308. p = q;
  309. }
  310. p->mark |= FOLL;
  311. lastp->link = p;
  312. lastp = p;
  313. if(a == AJMP || a == ARET || a == ARFE){
  314. if(p->mark & NOSCHED){
  315. p = p->link;
  316. goto loop;
  317. }
  318. return;
  319. }
  320. if(p->cond != P)
  321. if(a != AJAL && p->link != P) {
  322. xfol(p->link);
  323. p = p->cond;
  324. if(p == P || (p->mark&FOLL))
  325. return;
  326. goto loop;
  327. }
  328. p = p->link;
  329. goto loop;
  330. }
  331. void
  332. patch(void)
  333. {
  334. int64_t c, vexit;
  335. Prog *p, *q;
  336. Sym *s;
  337. int a;
  338. if(debug['v'])
  339. Bprint(&bso, "%5.2f patch\n", cputime());
  340. Bflush(&bso);
  341. mkfwd();
  342. s = lookup("exit", 0);
  343. vexit = s->value;
  344. for(p = firstp; p != P; p = p->link) {
  345. a = p->as;
  346. if(a == ATEXT)
  347. curtext = p;
  348. if((a == AJAL || a == AJMP || a == ARET) &&
  349. p->to.type != D_BRANCH && p->to.sym != S) {
  350. s = p->to.sym;
  351. if(s->type != STEXT) {
  352. diag("undefined: %s\n%P", s->name, p);
  353. s->type = STEXT;
  354. s->value = vexit;
  355. }
  356. p->to.offset = s->value;
  357. p->to.type = D_BRANCH;
  358. }
  359. if(p->to.type != D_BRANCH)
  360. continue;
  361. c = p->to.offset;
  362. for(q = firstp; q != P;) {
  363. if(q->forwd != P)
  364. if(c >= q->forwd->pc) {
  365. q = q->forwd;
  366. continue;
  367. }
  368. if(c == q->pc)
  369. break;
  370. q = q->link;
  371. }
  372. if(q == P) {
  373. diag("branch out of range %lld\n%P", c, p);
  374. p->to.type = D_NONE;
  375. }
  376. p->cond = q;
  377. }
  378. for(p = firstp; p != P; p = p->link) {
  379. if(p->as == ATEXT)
  380. curtext = p;
  381. if(p->cond != P) {
  382. p->cond = brloop(p->cond);
  383. if(p->cond != P)
  384. if(p->to.type == D_BRANCH)
  385. p->to.offset = p->cond->pc;
  386. }
  387. }
  388. }
  389. #define LOG 5
  390. void
  391. mkfwd(void)
  392. {
  393. Prog *p;
  394. int32_t dwn[LOG], cnt[LOG], i;
  395. Prog *lst[LOG];
  396. for(i=0; i<LOG; i++) {
  397. if(i == 0)
  398. cnt[i] = 1; else
  399. cnt[i] = LOG * cnt[i-1];
  400. dwn[i] = 1;
  401. lst[i] = P;
  402. }
  403. i = 0;
  404. for(p = firstp; p != P; p = p->link) {
  405. if(p->as == ATEXT)
  406. curtext = p;
  407. i--;
  408. if(i < 0)
  409. i = LOG-1;
  410. p->forwd = P;
  411. dwn[i]--;
  412. if(dwn[i] <= 0) {
  413. dwn[i] = cnt[i];
  414. if(lst[i] != P)
  415. lst[i]->forwd = p;
  416. lst[i] = p;
  417. }
  418. }
  419. }
  420. Prog*
  421. brloop(Prog *p)
  422. {
  423. Prog *q;
  424. int c;
  425. for(c=0; p!=P;) {
  426. if(p->as != AJMP || (p->mark&NOSCHED))
  427. return p;
  428. q = p->cond;
  429. if(q <= p) {
  430. c++;
  431. if(q == p || c > 5000)
  432. break;
  433. }
  434. p = q;
  435. }
  436. return P;
  437. }
  438. int64_t
  439. atolwhex(char *s)
  440. {
  441. int64_t n;
  442. int f;
  443. n = 0;
  444. f = 0;
  445. while(*s == ' ' || *s == '\t')
  446. s++;
  447. if(*s == '-' || *s == '+') {
  448. if(*s++ == '-')
  449. f = 1;
  450. while(*s == ' ' || *s == '\t')
  451. s++;
  452. }
  453. if(s[0]=='0' && s[1]){
  454. if(s[1]=='x' || s[1]=='X'){
  455. s += 2;
  456. for(;;){
  457. if(*s >= '0' && *s <= '9')
  458. n = n*16 + *s++ - '0';
  459. else if(*s >= 'a' && *s <= 'f')
  460. n = n*16 + *s++ - 'a' + 10;
  461. else if(*s >= 'A' && *s <= 'F')
  462. n = n*16 + *s++ - 'A' + 10;
  463. else
  464. break;
  465. }
  466. } else
  467. while(*s >= '0' && *s <= '7')
  468. n = n*8 + *s++ - '0';
  469. } else
  470. while(*s >= '0' && *s <= '9')
  471. n = n*10 + *s++ - '0';
  472. if(f)
  473. n = -n;
  474. return n;
  475. }
  476. int64_t
  477. rnd(int64_t v, int32_t r)
  478. {
  479. int32_t c;
  480. if(r <= 0)
  481. return v;
  482. v += r - 1;
  483. c = v % r;
  484. if(c < 0)
  485. c += r;
  486. v -= c;
  487. return v;
  488. }