pass.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. #include "l.h"
  2. void
  3. dodata(void)
  4. {
  5. int i, t;
  6. Sym *s;
  7. Prog *p;
  8. long orig, 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. if(debug['t']) {
  27. /*
  28. * pull out string constants
  29. */
  30. for(p = datap; p != P; p = p->link) {
  31. s = p->from.sym;
  32. if(p->to.type == D_SCONST)
  33. s->type = SSTRING;
  34. }
  35. }
  36. /*
  37. * pass 1
  38. * assign 'small' variables to data segment
  39. * (rational is that data segment is more easily
  40. * addressed through offset on R12)
  41. */
  42. orig = 0;
  43. for(i=0; i<NHASH; i++)
  44. for(s = hash[i]; s != S; s = s->link) {
  45. t = s->type;
  46. if(t != SDATA && t != SBSS)
  47. continue;
  48. v = s->value;
  49. if(v == 0) {
  50. diag("%s: no size\n", s->name);
  51. v = 1;
  52. }
  53. while(v & 3)
  54. v++;
  55. s->value = v;
  56. if(v > MINSIZ)
  57. continue;
  58. s->value = orig;
  59. orig += v;
  60. s->type = SDATA1;
  61. }
  62. /*
  63. * pass 2
  64. * assign large 'data' variables to data segment
  65. */
  66. for(i=0; i<NHASH; i++)
  67. for(s = hash[i]; s != S; s = s->link) {
  68. t = s->type;
  69. if(t != SDATA) {
  70. if(t == SDATA1)
  71. s->type = SDATA;
  72. continue;
  73. }
  74. v = s->value;
  75. s->value = orig;
  76. orig += v;
  77. }
  78. while(orig & 7)
  79. orig++;
  80. datsize = orig;
  81. /*
  82. * pass 3
  83. * everything else to bss segment
  84. */
  85. for(i=0; i<NHASH; i++)
  86. for(s = hash[i]; s != S; s = s->link) {
  87. if(s->type != SBSS)
  88. continue;
  89. v = s->value;
  90. s->value = orig;
  91. orig += v;
  92. }
  93. while(orig & 7)
  94. orig++;
  95. bsssize = orig-datsize;
  96. xdefine("setR12", SDATA, 0L+BIG);
  97. xdefine("bdata", SDATA, 0L);
  98. xdefine("edata", SDATA, datsize);
  99. xdefine("end", SBSS, datsize+bsssize);
  100. xdefine("etext", STEXT, 0L);
  101. }
  102. void
  103. undef(void)
  104. {
  105. int i;
  106. Sym *s;
  107. for(i=0; i<NHASH; i++)
  108. for(s = hash[i]; s != S; s = s->link)
  109. if(s->type == SXREF)
  110. diag("%s: not defined\n", s->name);
  111. }
  112. Prog*
  113. brchain(Prog *p)
  114. {
  115. int i;
  116. for(i=0; i<20; i++) {
  117. if(p == P || p->as != AB)
  118. return p;
  119. p = p->cond;
  120. }
  121. return P;
  122. }
  123. int
  124. relinv(int a)
  125. {
  126. switch(a) {
  127. case ABEQ: return ABNE;
  128. case ABNE: return ABEQ;
  129. case ABCS: return ABCC;
  130. case ABHS: return ABLO;
  131. case ABCC: return ABCS;
  132. case ABLO: return ABHS;
  133. case ABMI: return ABPL;
  134. case ABPL: return ABMI;
  135. case ABVS: return ABVC;
  136. case ABVC: return ABVS;
  137. case ABHI: return ABLS;
  138. case ABLS: return ABHI;
  139. case ABGE: return ABLT;
  140. case ABLT: return ABGE;
  141. case ABGT: return ABLE;
  142. case ABLE: return ABGT;
  143. }
  144. diag("unknown relation: %s\n", anames[a]);
  145. return a;
  146. }
  147. void
  148. follow(void)
  149. {
  150. if(debug['v'])
  151. Bprint(&bso, "%5.2f follow\n", cputime());
  152. Bflush(&bso);
  153. firstp = prg();
  154. lastp = firstp;
  155. xfol(textp);
  156. firstp = firstp->link;
  157. lastp->link = P;
  158. }
  159. void
  160. xfol(Prog *p)
  161. {
  162. Prog *q, *r;
  163. int a, i;
  164. loop:
  165. if(p == P)
  166. return;
  167. a = p->as;
  168. if(a == ATEXT)
  169. curtext = p;
  170. if(a == AB) {
  171. q = p->cond;
  172. if(q != P) {
  173. p->mark |= FOLL;
  174. p = q;
  175. if(!(p->mark & FOLL))
  176. goto loop;
  177. }
  178. }
  179. if(p->mark & FOLL) {
  180. for(i=0,q=p; i<4; i++,q=q->link) {
  181. if(q == lastp)
  182. break;
  183. a = q->as;
  184. if(a == ANOP) {
  185. i--;
  186. continue;
  187. }
  188. if(a == AB || (a == ARET && q->scond == 14) || a == ARFE)
  189. goto copy;
  190. if(!q->cond || (q->cond->mark&FOLL))
  191. continue;
  192. if(a != ABEQ && a != ABNE)
  193. continue;
  194. copy:
  195. for(;;) {
  196. r = prg();
  197. *r = *p;
  198. if(!(r->mark&FOLL))
  199. print("cant happen 1\n");
  200. r->mark |= FOLL;
  201. if(p != q) {
  202. p = p->link;
  203. lastp->link = r;
  204. lastp = r;
  205. continue;
  206. }
  207. lastp->link = r;
  208. lastp = r;
  209. if(a == AB || (a == ARET && q->scond == 14) || a == ARFE)
  210. return;
  211. r->as = ABNE;
  212. if(a == ABNE)
  213. r->as = ABEQ;
  214. r->cond = p->link;
  215. r->link = p->cond;
  216. if(!(r->link->mark&FOLL))
  217. xfol(r->link);
  218. if(!(r->cond->mark&FOLL))
  219. print("cant happen 2\n");
  220. return;
  221. }
  222. }
  223. a = AB;
  224. q = prg();
  225. q->as = a;
  226. q->line = p->line;
  227. q->to.type = D_BRANCH;
  228. q->to.offset = p->pc;
  229. q->cond = p;
  230. p = q;
  231. }
  232. p->mark |= FOLL;
  233. lastp->link = p;
  234. lastp = p;
  235. if(a == AB || (a == ARET && p->scond == 14) || a == ARFE){
  236. return;
  237. }
  238. if(p->cond != P)
  239. if(a != ABL && p->link != P) {
  240. q = brchain(p->link);
  241. if(a != ATEXT && a != ABCASE)
  242. if(q != P && (q->mark&FOLL)) {
  243. p->as = relinv(a);
  244. p->link = p->cond;
  245. p->cond = q;
  246. }
  247. xfol(p->link);
  248. q = brchain(p->cond);
  249. if(q == P)
  250. q = p->cond;
  251. if(q->mark&FOLL) {
  252. p->cond = q;
  253. return;
  254. }
  255. p = q;
  256. goto loop;
  257. }
  258. p = p->link;
  259. goto loop;
  260. }
  261. void
  262. patch(void)
  263. {
  264. long c, vexit;
  265. Prog *p, *q;
  266. Sym *s;
  267. int a;
  268. if(debug['v'])
  269. Bprint(&bso, "%5.2f patch\n", cputime());
  270. Bflush(&bso);
  271. mkfwd();
  272. s = lookup("exit", 0);
  273. vexit = s->value;
  274. for(p = firstp; p != P; p = p->link) {
  275. a = p->as;
  276. if(a == ATEXT)
  277. curtext = p;
  278. if((a == ABL || a == AB || a == ARET) &&
  279. p->to.type != D_BRANCH && p->to.sym != S) {
  280. s = p->to.sym;
  281. if(s->type != STEXT) {
  282. diag("undefined: %s\n%P\n", s->name, p);
  283. s->type = STEXT;
  284. s->value = vexit;
  285. }
  286. p->to.offset = s->value;
  287. p->to.type = D_BRANCH;
  288. }
  289. if(p->to.type != D_BRANCH)
  290. continue;
  291. c = p->to.offset;
  292. if(reloc && p->as == ABL && c == -1) {
  293. p->cond = UP;
  294. continue;
  295. }
  296. for(q = firstp; q != P;) {
  297. if(q->forwd != P)
  298. if(c >= q->forwd->pc) {
  299. q = q->forwd;
  300. continue;
  301. }
  302. if(c == q->pc)
  303. break;
  304. q = q->link;
  305. }
  306. if(q == P) {
  307. diag("branch out of range %ld\n%P\n", c, p);
  308. p->to.type = D_NONE;
  309. }
  310. p->cond = q;
  311. }
  312. for(p = firstp; p != P; p = p->link) {
  313. if(p->as == ATEXT)
  314. curtext = p;
  315. if(p->cond != P && p->cond != UP) {
  316. p->cond = brloop(p->cond);
  317. if(p->cond != P)
  318. if(p->to.type == D_BRANCH)
  319. p->to.offset = p->cond->pc;
  320. }
  321. }
  322. }
  323. #define LOG 5
  324. void
  325. mkfwd(void)
  326. {
  327. Prog *p;
  328. long dwn[LOG], cnt[LOG], i;
  329. Prog *lst[LOG];
  330. for(i=0; i<LOG; i++) {
  331. if(i == 0)
  332. cnt[i] = 1; else
  333. cnt[i] = LOG * cnt[i-1];
  334. dwn[i] = 1;
  335. lst[i] = P;
  336. }
  337. i = 0;
  338. for(p = firstp; p != P; p = p->link) {
  339. if(p->as == ATEXT)
  340. curtext = p;
  341. i--;
  342. if(i < 0)
  343. i = LOG-1;
  344. p->forwd = P;
  345. dwn[i]--;
  346. if(dwn[i] <= 0) {
  347. dwn[i] = cnt[i];
  348. if(lst[i] != P)
  349. lst[i]->forwd = p;
  350. lst[i] = p;
  351. }
  352. }
  353. }
  354. Prog*
  355. brloop(Prog *p)
  356. {
  357. Prog *q;
  358. int c;
  359. for(c=0; p!=P;) {
  360. if(p->as != AB)
  361. return p;
  362. q = p->cond;
  363. if(q <= p) {
  364. c++;
  365. if(q == p || c > 5000)
  366. break;
  367. }
  368. p = q;
  369. }
  370. return P;
  371. }
  372. long
  373. atolwhex(char *s)
  374. {
  375. long n;
  376. int f;
  377. n = 0;
  378. f = 0;
  379. while(*s == ' ' || *s == '\t')
  380. s++;
  381. if(*s == '-' || *s == '+') {
  382. if(*s++ == '-')
  383. f = 1;
  384. while(*s == ' ' || *s == '\t')
  385. s++;
  386. }
  387. if(s[0]=='0' && s[1]){
  388. if(s[1]=='x' || s[1]=='X'){
  389. s += 2;
  390. for(;;){
  391. if(*s >= '0' && *s <= '9')
  392. n = n*16 + *s++ - '0';
  393. else if(*s >= 'a' && *s <= 'f')
  394. n = n*16 + *s++ - 'a' + 10;
  395. else if(*s >= 'A' && *s <= 'F')
  396. n = n*16 + *s++ - 'A' + 10;
  397. else
  398. break;
  399. }
  400. } else
  401. while(*s >= '0' && *s <= '7')
  402. n = n*8 + *s++ - '0';
  403. } else
  404. while(*s >= '0' && *s <= '9')
  405. n = n*10 + *s++ - '0';
  406. if(f)
  407. n = -n;
  408. return n;
  409. }
  410. long
  411. rnd(long v, long r)
  412. {
  413. long c;
  414. if(r <= 0)
  415. return v;
  416. v += r - 1;
  417. c = v % r;
  418. if(c < 0)
  419. c += r;
  420. v -= c;
  421. return v;
  422. }