pass.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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",
  18. s->type, s->name, p);
  19. t = p->from.offset + p->width;
  20. if(t > s->value)
  21. diag("initialize bounds (%ld): %s\n%P",
  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", 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. symSB = lookup("setSB", 0);
  88. xdefine("setSB", SDATA, 0L);
  89. xdefine("bdata", SDATA, 0L);
  90. xdefine("edata", SDATA, datsize);
  91. xdefine("end", SBSS, datsize+bsssize);
  92. xdefine("etext", STEXT, 0L);
  93. }
  94. Prog*
  95. brchain(Prog *p)
  96. {
  97. int i;
  98. for(i=0; i<20; i++) {
  99. if(p == P || p->as != AB)
  100. return p;
  101. p = p->cond;
  102. }
  103. return P;
  104. }
  105. void
  106. follow(void)
  107. {
  108. if(debug['v'])
  109. Bprint(&bso, "%5.2f follow\n", cputime());
  110. Bflush(&bso);
  111. firstp = prg();
  112. lastp = firstp;
  113. xfol(textp);
  114. firstp = firstp->link;
  115. lastp->link = P;
  116. }
  117. void
  118. xfol(Prog *p)
  119. {
  120. Prog *q;
  121. int i;
  122. enum as a;
  123. loop:
  124. if(p == P)
  125. return;
  126. if(p->as == ATEXT)
  127. curtext = p;
  128. if(p->as == AB)
  129. if((q = p->cond) != P) {
  130. p->mark = 1;
  131. p = q;
  132. if(p->mark == 0)
  133. goto loop;
  134. }
  135. if(p->mark) {
  136. /* copy up to 4 instructions to avoid branch */
  137. for(i=0,q=p; i<4; i++,q=q->link) {
  138. if(q == P)
  139. break;
  140. if(q == lastp)
  141. break;
  142. a = q->as;
  143. if(a == ANOP) {
  144. i--;
  145. continue;
  146. }
  147. if(a == AB || a == ARTS)
  148. break;
  149. if(q->cond == P || q->cond->mark)
  150. continue;
  151. if(a == ABAL)
  152. continue;
  153. for(;;) {
  154. if(p->as == ANOP) {
  155. p = p->link;
  156. continue;
  157. }
  158. q = copyp(p);
  159. p = p->link;
  160. q->mark = 1;
  161. lastp->link = q;
  162. lastp = q;
  163. if(q->as != a || q->cond == P || q->cond->mark)
  164. continue;
  165. q->as = relinv(q->as);
  166. p = q->cond;
  167. q->cond = q->link;
  168. q->link = p;
  169. xfol(q->link);
  170. p = q->link;
  171. if(p->mark)
  172. return;
  173. goto loop;
  174. }
  175. } /* */
  176. q = prg();
  177. q->as = AB;
  178. q->line = p->line;
  179. q->to.type = D_BRANCH;
  180. q->to.offset = p->pc;
  181. q->cond = p;
  182. p = q;
  183. }
  184. p->mark = 1;
  185. lastp->link = p;
  186. lastp = p;
  187. a = p->as;
  188. if(a == AB || a == ARTS)
  189. return;
  190. if(p->cond != P)
  191. if(a != ABAL) {
  192. q = brchain(p->link);
  193. if(q != P && q->mark) {
  194. p->as = relinv(a);
  195. p->link = p->cond;
  196. p->cond = q;
  197. }
  198. xfol(p->link);
  199. q = brchain(p->cond);
  200. if(q->mark) {
  201. p->cond = q;
  202. return;
  203. }
  204. p = q;
  205. goto loop;
  206. }
  207. p = p->link;
  208. goto loop;
  209. }
  210. int
  211. relinv(int a)
  212. {
  213. switch(a) {
  214. case ABE: return ABNE;
  215. case ABNE: return ABE;
  216. case ABLE: return ABG;
  217. case ABL: return ABGE;
  218. case ABGE: return ABL;
  219. case ABG: return ABLE;
  220. case ABBS: return ABBC;
  221. case ABBC: return ABBS;
  222. case ABO: return ABNO;
  223. case ABNO: return ABO;
  224. }
  225. diag("unknown relation: %s in %s", anames[a], TNAME);
  226. return a;
  227. }
  228. void
  229. doinit(void)
  230. {
  231. Sym *s;
  232. Prog *p;
  233. int x;
  234. for(p = datap; p != P; p = p->link) {
  235. x = p->to.type;
  236. if(x != D_EXTERN && x != D_STATIC)
  237. continue;
  238. s = p->to.sym;
  239. if(s->type == 0 || s->type == SXREF)
  240. diag("undefined %s initializer of %s",
  241. s->name, p->from.sym->name);
  242. p->to.offset += s->value;
  243. p->to.type = D_CONST;
  244. if(s->type == SDATA || s->type == SBSS)
  245. p->to.offset += INITDAT;
  246. }
  247. }
  248. void
  249. patch(void)
  250. {
  251. long c;
  252. Prog *p, *q;
  253. Sym *s;
  254. long vexit;
  255. if(debug['v'])
  256. Bprint(&bso, "%5.2f mkfwd\n", cputime());
  257. Bflush(&bso);
  258. mkfwd();
  259. if(debug['v'])
  260. Bprint(&bso, "%5.2f patch\n", cputime());
  261. Bflush(&bso);
  262. s = lookup("exit", 0);
  263. vexit = s->value;
  264. for(p = firstp; p != P; p = p->link) {
  265. if(p->as == ATEXT)
  266. curtext = p;
  267. if(p->as == ABAL || p->as == ARTS) {
  268. s = p->to.sym;
  269. if(s) {
  270. if(s->type != STEXT && s->type != SLEAF) {
  271. diag("undefined: %s in %s", s->name, TNAME);
  272. s->type = STEXT;
  273. s->value = vexit;
  274. }
  275. p->to.offset = s->value;
  276. p->to.type = D_BRANCH;
  277. }
  278. }
  279. if(p->to.type != D_BRANCH)
  280. continue;
  281. c = p->to.offset;
  282. for(q = firstp; q != P;) {
  283. if(q->forwd != P)
  284. if(c >= q->forwd->pc) {
  285. q = q->forwd;
  286. continue;
  287. }
  288. if(c == q->pc)
  289. break;
  290. q = q->link;
  291. }
  292. if(q == P) {
  293. diag("branch out of range in %s\n%P", TNAME, p);
  294. p->to.type = D_NONE;
  295. }
  296. p->cond = q;
  297. }
  298. for(p = firstp; p != P; p = p->link) {
  299. if(p->as == ATEXT)
  300. curtext = p;
  301. p->mark = 0; /* initialization for follow */
  302. if(p->cond != P) {
  303. p->cond = brloop(p->cond);
  304. if(p->cond != P)
  305. if(p->to.type == D_BRANCH)
  306. p->to.offset = p->cond->pc;
  307. }
  308. }
  309. }
  310. #define LOG 5
  311. void
  312. mkfwd(void)
  313. {
  314. Prog *p;
  315. int i;
  316. long dwn[LOG], cnt[LOG];
  317. Prog *lst[LOG];
  318. for(i=0; i<LOG; i++) {
  319. if(i == 0)
  320. cnt[i] = 1; else
  321. cnt[i] = LOG * cnt[i-1];
  322. dwn[i] = 1;
  323. lst[i] = P;
  324. }
  325. i = 0;
  326. for(p = firstp; p != P; p = p->link) {
  327. if(p->as == ATEXT)
  328. curtext = p;
  329. i--;
  330. if(i < 0)
  331. i = LOG-1;
  332. p->forwd = P;
  333. dwn[i]--;
  334. if(dwn[i] <= 0) {
  335. dwn[i] = cnt[i];
  336. if(lst[i] != P)
  337. lst[i]->forwd = p;
  338. lst[i] = p;
  339. }
  340. }
  341. }
  342. Prog*
  343. brloop(Prog *p)
  344. {
  345. int c;
  346. Prog *q;
  347. c = 0;
  348. for(q = p; q != P; q = q->cond) {
  349. if(q->as != AB)
  350. break;
  351. c++;
  352. if(c >= 5000)
  353. return P;
  354. }
  355. return q;
  356. }
  357. long
  358. atolwhex(char *s)
  359. {
  360. long n;
  361. int f;
  362. n = 0;
  363. f = 0;
  364. while(*s == ' ' || *s == '\t')
  365. s++;
  366. if(*s == '-' || *s == '+') {
  367. if(*s++ == '-')
  368. f = 1;
  369. while(*s == ' ' || *s == '\t')
  370. s++;
  371. }
  372. if(s[0]=='0' && s[1]){
  373. if(s[1]=='x' || s[1]=='X'){
  374. s += 2;
  375. for(;;){
  376. if(*s >= '0' && *s <= '9')
  377. n = n*16 + *s++ - '0';
  378. else if(*s >= 'a' && *s <= 'f')
  379. n = n*16 + *s++ - 'a' + 10;
  380. else if(*s >= 'A' && *s <= 'F')
  381. n = n*16 + *s++ - 'A' + 10;
  382. else
  383. break;
  384. }
  385. } else
  386. while(*s >= '0' && *s <= '7')
  387. n = n*8 + *s++ - '0';
  388. } else
  389. while(*s >= '0' && *s <= '9')
  390. n = n*10 + *s++ - '0';
  391. if(f)
  392. n = -n;
  393. return n;
  394. }
  395. void
  396. undef(void)
  397. {
  398. int i;
  399. Sym *s;
  400. for(i=0; i<NHASH; i++)
  401. for(s = hash[i]; s != S; s = s->link)
  402. if(s->type == SXREF)
  403. diag("%s: not defined", s->name);
  404. }