pass.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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",
  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",
  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", 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", 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", 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. switch(s->type) {
  282. default:
  283. diag("undefined: %s\n%P", s->name, p);
  284. s->type = STEXT;
  285. s->value = vexit;
  286. break;
  287. case STEXT:
  288. p->to.offset = s->value;
  289. p->to.type = D_BRANCH;
  290. break;
  291. case SUNDEF:
  292. if(p->as != ABL)
  293. diag("help: SUNDEF in AB || ARET");
  294. p->to.offset = 0;
  295. p->to.type = D_BRANCH;
  296. p->cond = UP;
  297. break;
  298. }
  299. }
  300. if(p->to.type != D_BRANCH || p->cond == UP)
  301. continue;
  302. c = p->to.offset;
  303. for(q = firstp; q != P;) {
  304. if(q->forwd != P)
  305. if(c >= q->forwd->pc) {
  306. q = q->forwd;
  307. continue;
  308. }
  309. if(c == q->pc)
  310. break;
  311. q = q->link;
  312. }
  313. if(q == P) {
  314. diag("branch out of range %ld\n%P", c, p);
  315. p->to.type = D_NONE;
  316. }
  317. p->cond = q;
  318. }
  319. for(p = firstp; p != P; p = p->link) {
  320. if(p->as == ATEXT)
  321. curtext = p;
  322. if(p->cond != P && p->cond != UP) {
  323. p->cond = brloop(p->cond);
  324. if(p->cond != P)
  325. if(p->to.type == D_BRANCH)
  326. p->to.offset = p->cond->pc;
  327. }
  328. }
  329. }
  330. #define LOG 5
  331. void
  332. mkfwd(void)
  333. {
  334. Prog *p;
  335. long dwn[LOG], cnt[LOG], i;
  336. Prog *lst[LOG];
  337. for(i=0; i<LOG; i++) {
  338. if(i == 0)
  339. cnt[i] = 1; else
  340. cnt[i] = LOG * cnt[i-1];
  341. dwn[i] = 1;
  342. lst[i] = P;
  343. }
  344. i = 0;
  345. for(p = firstp; p != P; p = p->link) {
  346. if(p->as == ATEXT)
  347. curtext = p;
  348. i--;
  349. if(i < 0)
  350. i = LOG-1;
  351. p->forwd = P;
  352. dwn[i]--;
  353. if(dwn[i] <= 0) {
  354. dwn[i] = cnt[i];
  355. if(lst[i] != P)
  356. lst[i]->forwd = p;
  357. lst[i] = p;
  358. }
  359. }
  360. }
  361. Prog*
  362. brloop(Prog *p)
  363. {
  364. Prog *q;
  365. int c;
  366. for(c=0; p!=P;) {
  367. if(p->as != AB)
  368. return p;
  369. q = p->cond;
  370. if(q <= p) {
  371. c++;
  372. if(q == p || c > 5000)
  373. break;
  374. }
  375. p = q;
  376. }
  377. return P;
  378. }
  379. long
  380. atolwhex(char *s)
  381. {
  382. long n;
  383. int f;
  384. n = 0;
  385. f = 0;
  386. while(*s == ' ' || *s == '\t')
  387. s++;
  388. if(*s == '-' || *s == '+') {
  389. if(*s++ == '-')
  390. f = 1;
  391. while(*s == ' ' || *s == '\t')
  392. s++;
  393. }
  394. if(s[0]=='0' && s[1]){
  395. if(s[1]=='x' || s[1]=='X'){
  396. s += 2;
  397. for(;;){
  398. if(*s >= '0' && *s <= '9')
  399. n = n*16 + *s++ - '0';
  400. else if(*s >= 'a' && *s <= 'f')
  401. n = n*16 + *s++ - 'a' + 10;
  402. else if(*s >= 'A' && *s <= 'F')
  403. n = n*16 + *s++ - 'A' + 10;
  404. else
  405. break;
  406. }
  407. } else
  408. while(*s >= '0' && *s <= '7')
  409. n = n*8 + *s++ - '0';
  410. } else
  411. while(*s >= '0' && *s <= '9')
  412. n = n*10 + *s++ - '0';
  413. if(f)
  414. n = -n;
  415. return n;
  416. }
  417. long
  418. rnd(long v, long r)
  419. {
  420. long c;
  421. if(r <= 0)
  422. return v;
  423. v += r - 1;
  424. c = v % r;
  425. if(c < 0)
  426. c += r;
  427. v -= c;
  428. return v;
  429. }
  430. void
  431. import(void)
  432. {
  433. int i;
  434. Sym *s;
  435. for(i = 0; i < NHASH; i++)
  436. for(s = hash[i]; s != S; s = s->link)
  437. if(s->sig != 0 && s->type == SXREF && (nimports == 0 || s->subtype == SIMPORT)){
  438. undefsym(s);
  439. Bprint(&bso, "IMPORT: %s sig=%lux v=%ld\n", s->name, s->sig, s->value);
  440. }
  441. }
  442. void
  443. ckoff(Sym *s, long v)
  444. {
  445. if(v < 0 || v >= 1<<Roffset)
  446. diag("relocation offset %ld for %s out of range", v, s->name);
  447. }
  448. static Prog*
  449. newdata(Sym *s, int o, int w, int t)
  450. {
  451. Prog *p;
  452. p = prg();
  453. p->link = datap;
  454. datap = p;
  455. p->as = ADATA;
  456. p->reg = w;
  457. p->from.type = D_OREG;
  458. p->from.name = t;
  459. p->from.sym = s;
  460. p->from.offset = o;
  461. p->to.type = D_CONST;
  462. p->to.name = D_NONE;
  463. return p;
  464. }
  465. void
  466. export(void)
  467. {
  468. int i, j, n, off, nb, sv, ne;
  469. Sym *s, *et, *str, **esyms;
  470. Prog *p;
  471. char buf[NSNAME], *t;
  472. n = 0;
  473. for(i = 0; i < NHASH; i++)
  474. for(s = hash[i]; s != S; s = s->link)
  475. if(s->sig != 0 && s->type != SXREF && s->type != SUNDEF && (nexports == 0 || s->subtype == SEXPORT))
  476. n++;
  477. esyms = malloc(n*sizeof(Sym*));
  478. ne = n;
  479. n = 0;
  480. for(i = 0; i < NHASH; i++)
  481. for(s = hash[i]; s != S; s = s->link)
  482. if(s->sig != 0 && s->type != SXREF && s->type != SUNDEF && (nexports == 0 || s->subtype == SEXPORT))
  483. esyms[n++] = s;
  484. for(i = 0; i < ne-1; i++)
  485. for(j = i+1; j < ne; j++)
  486. if(strcmp(esyms[i]->name, esyms[j]->name) > 0){
  487. s = esyms[i];
  488. esyms[i] = esyms[j];
  489. esyms[j] = s;
  490. }
  491. nb = 0;
  492. off = 0;
  493. et = lookup(EXPTAB, 0);
  494. if(et->type != 0 && et->type != SXREF)
  495. diag("%s already defined", EXPTAB);
  496. et->type = SDATA;
  497. str = lookup(".string", 0);
  498. if(str->type == 0)
  499. str->type = SDATA;
  500. sv = str->value;
  501. for(i = 0; i < ne; i++){
  502. s = esyms[i];
  503. Bprint(&bso, "EXPORT: %s sig=%lux t=%d\n", s->name, s->sig, s->type);
  504. /* signature */
  505. p = newdata(et, off, sizeof(long), D_EXTERN);
  506. off += sizeof(long);
  507. p->to.offset = s->sig;
  508. /* address */
  509. p = newdata(et, off, sizeof(long), D_EXTERN);
  510. off += sizeof(long);
  511. p->to.name = D_EXTERN;
  512. p->to.sym = s;
  513. /* string */
  514. t = s->name;
  515. n = strlen(t)+1;
  516. for(;;){
  517. buf[nb++] = *t;
  518. sv++;
  519. if(nb >= NSNAME){
  520. p = newdata(str, sv-NSNAME, NSNAME, D_STATIC);
  521. p->to.type = D_SCONST;
  522. p->to.sval = malloc(NSNAME);
  523. memmove(p->to.sval, buf, NSNAME);
  524. nb = 0;
  525. }
  526. if(*t++ == 0)
  527. break;
  528. }
  529. /* name */
  530. p = newdata(et, off, sizeof(long), D_EXTERN);
  531. off += sizeof(long);
  532. p->to.name = D_STATIC;
  533. p->to.sym = str;
  534. p->to.offset = sv-n;
  535. }
  536. if(nb > 0){
  537. p = newdata(str, sv-nb, nb, D_STATIC);
  538. p->to.type = D_SCONST;
  539. p->to.sval = malloc(NSNAME);
  540. memmove(p->to.sval, buf, nb);
  541. }
  542. for(i = 0; i < 3; i++){
  543. newdata(et, off, sizeof(long), D_EXTERN);
  544. off += sizeof(long);
  545. }
  546. et->value = off;
  547. if(sv == 0)
  548. sv = 1;
  549. str->value = sv;
  550. exports = ne;
  551. free(esyms);
  552. }