obj.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  1. #define EXTERN
  2. #include "l.h"
  3. #include <ar.h>
  4. char symname[] = SYMDEF;
  5. char thechar = '1';
  6. char *thestring = "68000";
  7. /*
  8. * -H0 -T0x40004C -D0x10000000 is garbage unix
  9. * -H1 -T0x80020000 -R4 is garbage format
  10. * -H2 -T8224 -R8192 is plan9 format
  11. * -H3 -Tx -Rx is next boot
  12. * -H4 -T0 -D0 is pilot relocatable
  13. */
  14. void
  15. main(int argc, char *argv[])
  16. {
  17. int i, c;
  18. char *a;
  19. Binit(&bso, 1, OWRITE);
  20. cout = -1;
  21. listinit();
  22. memset(debug, 0, sizeof(debug));
  23. nerrors = 0;
  24. outfile = "1.out";
  25. HEADTYPE = -1;
  26. INITTEXT = -1;
  27. INITDAT = -1;
  28. INITRND = -1;
  29. INITENTRY = 0;
  30. ARGBEGIN {
  31. default:
  32. c = ARGC();
  33. if(c >= 0 && c < sizeof(debug))
  34. debug[c]++;
  35. break;
  36. case 'o': /* output to (next arg) */
  37. outfile = ARGF();
  38. break;
  39. case 'E':
  40. a = ARGF();
  41. if(a)
  42. INITENTRY = a;
  43. break;
  44. case 'H':
  45. a = ARGF();
  46. if(a)
  47. HEADTYPE = atolwhex(a);
  48. break;
  49. case 'T':
  50. a = ARGF();
  51. if(a)
  52. INITTEXT = atolwhex(a);
  53. break;
  54. case 'D':
  55. a = ARGF();
  56. if(a)
  57. INITDAT = atolwhex(a);
  58. break;
  59. case 'R':
  60. a = ARGF();
  61. if(a)
  62. INITRND = atolwhex(a);
  63. break;
  64. } ARGEND
  65. USED(argc);
  66. if(*argv == 0) {
  67. diag("usage: 2l [-options] objects\n");
  68. errorexit();
  69. }
  70. if(HEADTYPE == -1)
  71. HEADTYPE = 6;
  72. if(INITDAT != -1 && INITRND == -1)
  73. INITRND = 0;
  74. switch(HEADTYPE) {
  75. default:
  76. diag("unknown -H option %d", HEADTYPE);
  77. errorexit();
  78. case 0: /* this is garbage */
  79. HEADR = 20L+56L;
  80. if(INITTEXT == -1)
  81. INITTEXT = 0x40004CL;
  82. if(INITDAT == -1)
  83. INITDAT = 0x10000000L;
  84. if(INITDAT != 0 && INITRND == -1)
  85. INITRND = 0;
  86. if(INITRND == -1)
  87. INITRND = 0;
  88. break;
  89. case 1: /* plan9 boot data goes into text */
  90. HEADR = 32L;
  91. if(INITTEXT == -1)
  92. INITTEXT = 8224;
  93. if(INITDAT == -1)
  94. INITDAT = 0;
  95. if(INITDAT != 0 && INITRND == -1)
  96. INITRND = 0;
  97. if(INITRND == -1)
  98. INITRND = 8192;
  99. break;
  100. case 2: /* plan 9 */
  101. HEADR = 32L;
  102. if(INITTEXT == -1)
  103. INITTEXT = 8224;
  104. if(INITDAT == -1)
  105. INITDAT = 0;
  106. if(INITDAT != 0 && INITRND == -1)
  107. INITRND = 0;
  108. if(INITRND == -1)
  109. INITRND = 8192;
  110. break;
  111. case 3: /* next boot */
  112. HEADR = 28+124+192+24;
  113. if(INITTEXT == -1)
  114. INITTEXT = 0x04002000;
  115. if(INITDAT == -1)
  116. INITDAT = 0;
  117. if(INITDAT != 0 && INITRND == -1)
  118. INITRND = 0;
  119. if(INITRND == -1)
  120. INITRND = 8192L;
  121. break;
  122. case 4: /* preprocess pilot */
  123. HEADR = 36L;
  124. if(INITTEXT == -1)
  125. INITTEXT = 0;
  126. if(INITDAT == -1)
  127. INITDAT = 0;
  128. if(INITRND == -1)
  129. INITRND = 0;
  130. break;
  131. }
  132. if(INITDAT != 0 && INITRND != 0)
  133. print("warning: -D0x%lux is ignored because of -R0x%lux\n",
  134. INITDAT, INITRND);
  135. if(debug['v'])
  136. Bprint(&bso, "HEADER = -H0x%ld -T0x%lux -D0x%lux -R0x%lux\n",
  137. HEADTYPE, INITTEXT, INITDAT, INITRND);
  138. Bflush(&bso);
  139. for(i=1; optab[i].as; i++)
  140. if(i != optab[i].as) {
  141. diag("phase error in optab: %d\n", i);
  142. errorexit();
  143. }
  144. maxop = i;
  145. zprg.link = P;
  146. zprg.pcond = P;
  147. zprg.back = 2;
  148. zprg.as = AGOK;
  149. zprg.from.type = D_NONE;
  150. zprg.to = zprg.from;
  151. memset(special, 0, sizeof(special));
  152. special[D_CCR] = 1;
  153. special[D_SR] = 1;
  154. special[D_SFC] = 1;
  155. special[D_CACR] = 1;
  156. special[D_USP] = 1;
  157. special[D_VBR] = 1;
  158. special[D_CAAR] = 1;
  159. special[D_MSP] = 1;
  160. special[D_ISP] = 1;
  161. special[D_DFC] = 1;
  162. special[D_FPCR] = 1;
  163. special[D_FPSR] = 1;
  164. special[D_FPIAR] = 1;
  165. special[D_TC] = 1;
  166. special[D_ITT0] = 1;
  167. special[D_ITT1] = 1;
  168. special[D_DTT0] = 1;
  169. special[D_DTT1] = 1;
  170. special[D_MMUSR] = 1;
  171. special[D_URP] = 1;
  172. special[D_SRP] = 1;
  173. memset(simple, 0177, sizeof(simple));
  174. for(i=0; i<8; i++) {
  175. simple[D_R0+i] = i;
  176. simple[D_F0+i] = i+0100;
  177. simple[D_A0+i] = i+010;
  178. simple[D_A0+I_INDIR+i] = i+020;
  179. simple[D_A0+I_INDINC+i] = i+030;
  180. simple[D_A0+I_INDDEC+i] = i+040;
  181. }
  182. nuxiinit();
  183. histgen = 0;
  184. textp = P;
  185. datap = P;
  186. pc = 0;
  187. cout = create(outfile, 1, 0775);
  188. if(cout < 0) {
  189. diag("cannot create %s\n", outfile);
  190. errorexit();
  191. }
  192. version = 0;
  193. cbp = buf.cbuf;
  194. cbc = sizeof(buf.cbuf);
  195. firstp = prg();
  196. lastp = firstp;
  197. if(INITENTRY == 0) {
  198. INITENTRY = "_main";
  199. if(debug['p'])
  200. INITENTRY = "_mainp";
  201. if(!debug['l'])
  202. lookup(INITENTRY, 0)->type = SXREF;
  203. } else
  204. lookup(INITENTRY, 0)->type = SXREF;
  205. initmuldiv1();
  206. while(*argv)
  207. objfile(*argv++);
  208. if(!debug['l'])
  209. loadlib(0, libraryp);
  210. firstp = firstp->link;
  211. if(firstp == P)
  212. errorexit();
  213. patch();
  214. if(debug['p'])
  215. if(debug['1'])
  216. doprof1();
  217. else
  218. doprof2();
  219. initmuldiv2();
  220. follow();
  221. dodata();
  222. dostkoff();
  223. span();
  224. asmb();
  225. undef();
  226. if(debug['v']) {
  227. Bprint(&bso, "%5.2f cpu time\n", cputime());
  228. Bprint(&bso, "%ld data statements\n", ndata);
  229. Bprint(&bso, "%ld symbols\n", nsymbol);
  230. Bprint(&bso, "%ld memory used\n", thunk);
  231. Bprint(&bso, "%d sizeof adr\n", sizeof(Adr));
  232. Bprint(&bso, "%d sizeof prog\n", sizeof(Prog));
  233. }
  234. Bflush(&bso);
  235. errorexit();
  236. }
  237. void
  238. loadlib(int beg, int end)
  239. {
  240. int i, t;
  241. for(i=end-1; i>=beg; i--) {
  242. t = libraryp;
  243. if(debug['v'])
  244. Bprint(&bso, "%5.2f autolib: %s\n", cputime(), library[i]);
  245. objfile(library[i]);
  246. if(t != libraryp)
  247. loadlib(t, libraryp);
  248. }
  249. }
  250. void
  251. errorexit(void)
  252. {
  253. if(nerrors) {
  254. if(cout >= 0)
  255. remove(outfile);
  256. exits("error");
  257. }
  258. exits(0);
  259. }
  260. void
  261. objfile(char *file)
  262. {
  263. long off, esym, cnt, l;
  264. int f, work;
  265. Sym *s;
  266. char magbuf[SARMAG];
  267. char name[100], pname[150];
  268. struct ar_hdr arhdr;
  269. char *e, *start, *stop;
  270. if(file[0] == '-' && file[1] == 'l') {
  271. sprint(name, "/%s/lib/lib", thestring);
  272. strcat(name, file+2);
  273. strcat(name, ".a");
  274. file = name;
  275. }
  276. if(debug['v'])
  277. Bprint(&bso, "%5.2f ldobj: %s\n", cputime(), file);
  278. Bflush(&bso);
  279. f = open(file, 0);
  280. if(f < 0) {
  281. diag("cannot open file: %s\n", file);
  282. errorexit();
  283. }
  284. l = read(f, magbuf, SARMAG);
  285. if(l != SARMAG || strncmp(magbuf, ARMAG, SARMAG)){
  286. /* load it as a regular file */
  287. l = seek(f, 0L, 2);
  288. seek(f, 0L, 0);
  289. ldobj(f, l, file);
  290. close(f);
  291. return;
  292. }
  293. l = read(f, &arhdr, SAR_HDR);
  294. if(l != SAR_HDR) {
  295. diag("%s: short read on archive file symbol header\n", file);
  296. goto out;
  297. }
  298. if(strncmp(arhdr.name, symname, strlen(symname))) {
  299. diag("%s: first entry not symbol header\n", file);
  300. goto out;
  301. }
  302. esym = SARMAG + SAR_HDR + atolwhex(arhdr.size);
  303. off = SARMAG + SAR_HDR;
  304. /*
  305. * just bang the whole symbol file into memory
  306. */
  307. seek(f, off, 0);
  308. cnt = esym - off;
  309. start = malloc(cnt + 10);
  310. cnt = read(f, start, cnt);
  311. if(cnt <= 0){
  312. close(f);
  313. return;
  314. }
  315. stop = &start[cnt];
  316. memset(stop, 0, 10);
  317. work = 1;
  318. while(work){
  319. if(debug['v'])
  320. Bprint(&bso, "%5.2f library pass: %s\n", cputime(), file);
  321. Bflush(&bso);
  322. work = 0;
  323. for(e = start; e < stop; e = strchr(e+5, 0) + 1) {
  324. s = lookup(e+5, 0);
  325. if(s->type != SXREF)
  326. continue;
  327. sprint(pname, "%s(%s)", file, s->name);
  328. if(debug['v'])
  329. Bprint(&bso, "%5.2f library: %s\n", cputime(), pname);
  330. Bflush(&bso);
  331. l = e[1] & 0xff;
  332. l |= (e[2] & 0xff) << 8;
  333. l |= (e[3] & 0xff) << 16;
  334. l |= (e[4] & 0xff) << 24;
  335. seek(f, l, 0);
  336. l = read(f, &arhdr, SAR_HDR);
  337. if(l != SAR_HDR)
  338. goto bad;
  339. if(strncmp(arhdr.fmag, ARFMAG, sizeof(arhdr.fmag)))
  340. goto bad;
  341. l = atolwhex(arhdr.size);
  342. ldobj(f, l, pname);
  343. if(s->type == SXREF) {
  344. diag("%s: failed to load: %s\n", file, s->name);
  345. errorexit();
  346. }
  347. work = 1;
  348. }
  349. }
  350. return;
  351. bad:
  352. diag("%s: bad or out of date archive\n", file);
  353. out:
  354. close(f);
  355. }
  356. int
  357. zaddr(uchar *p, Adr *a, Sym *h[])
  358. {
  359. int c, t, i;
  360. long l;
  361. Sym *s;
  362. Auto *u;
  363. t = p[0];
  364. /*
  365. * first try the high-time formats
  366. */
  367. if(t == 0) {
  368. a->type = p[1];
  369. return 2;
  370. }
  371. if(t == T_OFFSET) {
  372. a->offset = p[1] | (p[2]<<8) | (p[3]<<16) | (p[4]<<24);
  373. a->type = p[5];
  374. return 6;
  375. }
  376. if(t == (T_OFFSET|T_SYM)) {
  377. a->offset = p[1] | (p[2]<<8) | (p[3]<<16) | (p[4]<<24);
  378. s = h[p[5]];
  379. a->sym = s;
  380. a->type = p[6];
  381. c = 7;
  382. goto dosym;
  383. }
  384. if(t == T_SYM) {
  385. s = h[p[1]];
  386. a->sym = s;
  387. a->type = p[2];
  388. c = 3;
  389. goto dosym;
  390. }
  391. if(t == (T_INDEX|T_OFFSET|T_SYM)) {
  392. a->displace = p[4] | (p[5]<<8) | (p[6]<<16) | (p[7]<<24);
  393. a->offset = p[8] | (p[9]<<8) | (p[10]<<16) | (p[11]<<24);
  394. s = h[p[12]];
  395. a->sym = s;
  396. a->type = p[13];
  397. c = 14;
  398. goto dosym;
  399. }
  400. /*
  401. * now do it the hard way
  402. */
  403. c = 1;
  404. if(t & T_FIELD) {
  405. a->field = p[c] | (p[c+1]<<8);
  406. c += 2;
  407. }
  408. if(t & T_INDEX) {
  409. a->displace = p[c+3] | (p[c+4]<<8) | (p[c+5]<<16) | (p[c+6]<<24);
  410. c += 7;
  411. }
  412. if(t & T_OFFSET) {
  413. a->offset = p[c] | (p[c+1]<<8) | (p[c+2]<<16) | (p[c+3]<<24);
  414. c += 4;
  415. }
  416. if(t & T_SYM) {
  417. a->sym = h[p[c]];
  418. c += 1;
  419. }
  420. if(t & T_FCONST) {
  421. a->ieee.l = p[c] | (p[c+1]<<8) | (p[c+2]<<16) | (p[c+3]<<24);
  422. a->ieee.h = p[c+4] | (p[c+5]<<8) | (p[c+6]<<16) | (p[c+7]<<24);
  423. c += 8;
  424. a->type = D_FCONST;
  425. } else
  426. if(t & T_SCONST) {
  427. for(i=0; i<NSNAME; i++)
  428. a->scon[i] = p[c+i];
  429. c += NSNAME;
  430. a->type = D_SCONST;
  431. } else
  432. if(t & T_TYPE) {
  433. a->type = p[c] | (p[c+1]<<8);
  434. c += 2;
  435. } else {
  436. a->type = p[c];
  437. c++;
  438. }
  439. s = a->sym;
  440. if(s == S)
  441. return c;
  442. dosym:
  443. t = a->type & D_MASK;
  444. if(t != D_AUTO && t != D_PARAM)
  445. return c;
  446. l = a->offset;
  447. for(u=curauto; u; u=u->link) {
  448. if(u->asym == s)
  449. if(u->type == t) {
  450. if(u->aoffset > l)
  451. u->aoffset = l;
  452. return c;
  453. }
  454. }
  455. while(nhunk < sizeof(Auto))
  456. gethunk();
  457. u = (Auto*)hunk;
  458. nhunk -= sizeof(Auto);
  459. hunk += sizeof(Auto);
  460. u->link = curauto;
  461. curauto = u;
  462. u->asym = s;
  463. u->aoffset = l;
  464. u->type = t;
  465. return c;
  466. }
  467. void
  468. addlib(char *obj)
  469. {
  470. char name[1024], comp[256], *p;
  471. int i;
  472. if(histfrogp <= 0)
  473. return;
  474. if(histfrog[0]->name[1] == '/') {
  475. sprint(name, "");
  476. i = 1;
  477. } else
  478. if(histfrog[0]->name[1] == '.') {
  479. sprint(name, ".");
  480. i = 0;
  481. } else {
  482. if(debug['9'])
  483. sprint(name, "/%s/lib", thestring);
  484. else
  485. sprint(name, "/usr/%clib", thechar);
  486. i = 0;
  487. }
  488. for(; i<histfrogp; i++) {
  489. snprint(comp, sizeof comp, histfrog[i]->name+1);
  490. for(;;) {
  491. p = strstr(comp, "$O");
  492. if(p == 0)
  493. break;
  494. memmove(p+1, p+2, strlen(p+2)+1);
  495. p[0] = thechar;
  496. }
  497. for(;;) {
  498. p = strstr(comp, "$M");
  499. if(p == 0)
  500. break;
  501. if(strlen(comp)+strlen(thestring)-2+1 >= sizeof comp) {
  502. diag("library component too long");
  503. return;
  504. }
  505. memmove(p+strlen(thestring), p+2, strlen(p+2)+1);
  506. memmove(p, thestring, strlen(thestring));
  507. }
  508. if(strlen(name) + strlen(comp) + 3 >= sizeof(name)) {
  509. diag("library component too long");
  510. return;
  511. }
  512. strcat(name, "/");
  513. strcat(name, comp);
  514. }
  515. for(i=0; i<libraryp; i++)
  516. if(strcmp(name, library[i]) == 0)
  517. return;
  518. if(libraryp == nelem(library)){
  519. diag("too many autolibs; skipping %s", name);
  520. return;
  521. }
  522. p = malloc(strlen(name) + 1);
  523. strcpy(p, name);
  524. library[libraryp] = p;
  525. p = malloc(strlen(obj) + 1);
  526. strcpy(p, obj);
  527. libraryobj[libraryp] = p;
  528. libraryp++;
  529. }
  530. void
  531. addhist(long line, int type)
  532. {
  533. Auto *u;
  534. Sym *s;
  535. int i, j, k;
  536. u = malloc(sizeof(Auto));
  537. s = malloc(sizeof(Sym));
  538. s->name = malloc(2*(histfrogp+1) + 1);
  539. u->asym = s;
  540. u->type = type;
  541. u->aoffset = line;
  542. u->link = curhist;
  543. curhist = u;
  544. j = 1;
  545. for(i=0; i<histfrogp; i++) {
  546. k = histfrog[i]->value;
  547. s->name[j+0] = k>>8;
  548. s->name[j+1] = k;
  549. j += 2;
  550. }
  551. }
  552. void
  553. histtoauto(void)
  554. {
  555. Auto *l;
  556. while(l = curhist) {
  557. curhist = l->link;
  558. l->link = curauto;
  559. curauto = l;
  560. }
  561. }
  562. void
  563. collapsefrog(Sym *s)
  564. {
  565. int i;
  566. /*
  567. * bad encoding of path components only allows
  568. * MAXHIST components. if there is an overflow,
  569. * first try to collapse xxx/..
  570. */
  571. for(i=1; i<histfrogp; i++)
  572. if(strcmp(histfrog[i]->name+1, "..") == 0) {
  573. memmove(histfrog+i-1, histfrog+i+1,
  574. (histfrogp-i-1)*sizeof(histfrog[0]));
  575. histfrogp--;
  576. goto out;
  577. }
  578. /*
  579. * next try to collapse .
  580. */
  581. for(i=0; i<histfrogp; i++)
  582. if(strcmp(histfrog[i]->name+1, ".") == 0) {
  583. memmove(histfrog+i, histfrog+i+1,
  584. (histfrogp-i-1)*sizeof(histfrog[0]));
  585. goto out;
  586. }
  587. /*
  588. * last chance, just truncate from front
  589. */
  590. memmove(histfrog+0, histfrog+1,
  591. (histfrogp-1)*sizeof(histfrog[0]));
  592. out:
  593. histfrog[histfrogp-1] = s;
  594. }
  595. uchar*
  596. readsome(int f, uchar *buf, uchar *good, uchar *stop, int max)
  597. {
  598. int n;
  599. n = stop - good;
  600. memmove(buf, good, stop - good);
  601. stop = buf + n;
  602. n = MAXIO - n;
  603. if(n > max)
  604. n = max;
  605. n = read(f, stop, n);
  606. if(n <= 0)
  607. return 0;
  608. return stop + n;
  609. }
  610. void
  611. ldobj(int f, long c, char *pn)
  612. {
  613. Prog *p;
  614. Sym *h[NSYM], *s;
  615. int v, o, r;
  616. long ipc, lv;
  617. double dv;
  618. uchar *bloc, *bsize, *stop;
  619. bsize = buf.xbuf;
  620. bloc = buf.xbuf;
  621. newloop:
  622. memset(h, 0, sizeof(h));
  623. version++;
  624. histfrogp = 0;
  625. ipc = pc;
  626. loop:
  627. if(c <= 0)
  628. goto eof;
  629. r = bsize - bloc;
  630. if(r < 100 && r < c) { /* enough for largest prog */
  631. bsize = readsome(f, buf.xbuf, bloc, bsize, c);
  632. if(bsize == 0)
  633. goto eof;
  634. bloc = buf.xbuf;
  635. goto loop;
  636. }
  637. o = bloc[0] | (bloc[1] << 8);
  638. if(o <= 0 || o >= maxop) {
  639. if(o < 0)
  640. goto eof;
  641. diag("%s: opcode out of range %d\n", pn, o);
  642. print(" probably not a .2 file\n");
  643. errorexit();
  644. }
  645. if(o == ANAME) {
  646. stop = memchr(&bloc[4], 0, bsize-&bloc[4]);
  647. if(stop == 0){
  648. bsize = readsome(f, buf.xbuf, bloc, bsize, c);
  649. if(bsize == 0)
  650. goto eof;
  651. bloc = buf.xbuf;
  652. stop = memchr(&bloc[4], 0, bsize-&bloc[4]);
  653. if(stop == 0){
  654. fprint(2, "%s: name too long\n", pn);
  655. errorexit();
  656. }
  657. }
  658. v = bloc[2]; /* type */
  659. o = bloc[3]; /* sym */
  660. bloc += 4;
  661. c -= 4;
  662. r = 0;
  663. if(v == D_STATIC)
  664. r = version;
  665. s = lookup((char*)bloc, r);
  666. c -= &stop[1] - bloc;
  667. bloc = stop + 1;
  668. if(debug['W'])
  669. print(" ANAME %s\n", s->name);
  670. h[o] = s;
  671. if((v == D_EXTERN || v == D_STATIC) && s->type == 0)
  672. s->type = SXREF;
  673. if(v == D_FILE) {
  674. if(s->type != SFILE) {
  675. histgen++;
  676. s->type = SFILE;
  677. s->value = histgen;
  678. }
  679. if(histfrogp < MAXHIST) {
  680. histfrog[histfrogp] = s;
  681. histfrogp++;
  682. } else
  683. collapsefrog(s);
  684. }
  685. goto loop;
  686. }
  687. while(nhunk < sizeof(Prog))
  688. gethunk();
  689. p = (Prog*)hunk;
  690. nhunk -= sizeof(Prog);
  691. hunk += sizeof(Prog);
  692. p->as = o;
  693. p->line = bloc[2] | (bloc[3] << 8) | (bloc[4] << 16) | (bloc[5] << 24);
  694. p->back = 2;
  695. r = zaddr(bloc+6, &p->from, h) + 6;
  696. r += zaddr(bloc+r, &p->to, h);
  697. bloc += r;
  698. c -= r;
  699. if(debug['W'])
  700. print("%P\n", p);
  701. switch(p->as) {
  702. case AHISTORY:
  703. if(p->to.offset == -1) {
  704. addlib(pn);
  705. histfrogp = 0;
  706. goto loop;
  707. }
  708. addhist(p->line, D_FILE); /* 'z' */
  709. if(p->to.offset)
  710. addhist(p->to.offset, D_FILE1); /* 'Z' */
  711. histfrogp = 0;
  712. goto loop;
  713. case AEND:
  714. histtoauto();
  715. if(curtext != P)
  716. curtext->to.autom = curauto;
  717. curauto = 0;
  718. curtext = P;
  719. if(c)
  720. goto newloop;
  721. return;
  722. case AGLOBL:
  723. s = p->from.sym;
  724. if(s->type == 0 || s->type == SXREF) {
  725. s->type = SBSS;
  726. s->value = 0;
  727. }
  728. if(s->type != SBSS) {
  729. diag("%s: redefinition: %s in %s\n",
  730. pn, s->name, TNAME);
  731. s->type = SBSS;
  732. s->value = 0;
  733. }
  734. if(p->to.offset > s->value)
  735. s->value = p->to.offset;
  736. goto loop;
  737. case ADATA:
  738. p->link = datap;
  739. datap = p;
  740. ndata++;
  741. goto loop;
  742. case AGOK:
  743. diag("%s: unknown opcode in %s\n", pn, TNAME);
  744. pc++;
  745. goto loop;
  746. case ATEXT:
  747. if(curtext != P) {
  748. histtoauto();
  749. curtext->to.autom = curauto;
  750. curauto = 0;
  751. }
  752. curtext = p;
  753. lastp->link = p;
  754. lastp = p;
  755. p->pc = pc;
  756. s = p->from.sym;
  757. if(s->type != 0 && s->type != SXREF)
  758. diag("%s: redefinition: %s\n", pn, s->name);
  759. s->type = STEXT;
  760. s->value = p->pc;
  761. pc++;
  762. p->pcond = P;
  763. if(textp == P) {
  764. textp = p;
  765. etextp = p;
  766. goto loop;
  767. }
  768. etextp->pcond = p;
  769. etextp = p;
  770. goto loop;
  771. case AJSR:
  772. p->as = ABSR;
  773. case ABSR:
  774. if(p->to.type != D_EXTERN && p->to.type != D_STATIC)
  775. p->as = AJSR;
  776. goto casdef;
  777. case AMOVL:
  778. case AMOVB:
  779. case AMOVW:
  780. if(p->from.type != D_CONST)
  781. goto casdef;
  782. lv = p->from.offset;
  783. if(lv >= -128 && lv < 128)
  784. if(p->to.type >= D_R0 && p->to.type < D_R0+8) {
  785. p->from.type = D_QUICK;
  786. goto casdef;
  787. }
  788. if(lv >= -0x7fff && lv <= 0x7fff)
  789. if(p->to.type >= D_A0 && p->to.type < D_A0+8)
  790. if(p->as == AMOVL)
  791. p->as = AMOVW;
  792. goto casdef;
  793. case AADDB:
  794. case AADDL:
  795. case AADDW:
  796. if(p->from.type != D_CONST)
  797. goto casdef;
  798. lv = p->from.offset;
  799. if(lv < 0) {
  800. lv = -lv;
  801. p->from.offset = lv;
  802. if(p->as == AADDB)
  803. p->as = ASUBB;
  804. else
  805. if(p->as == AADDW)
  806. p->as = ASUBW;
  807. else
  808. if(p->as == AADDL)
  809. p->as = ASUBL;
  810. }
  811. if(lv > 0)
  812. if(lv <= 8)
  813. p->from.type = D_QUICK;
  814. goto casdef;
  815. case ASUBB:
  816. case ASUBL:
  817. case ASUBW:
  818. if(p->from.type != D_CONST)
  819. goto casdef;
  820. lv = p->from.offset;
  821. if(lv < 0) {
  822. lv = -lv;
  823. p->from.offset = lv;
  824. if(p->as == ASUBB)
  825. p->as = AADDB;
  826. else
  827. if(p->as == ASUBW)
  828. p->as = AADDW;
  829. else
  830. if(p->as == ASUBL)
  831. p->as = AADDL;
  832. }
  833. if(lv > 0)
  834. if(lv <= 8)
  835. p->from.type = D_QUICK;
  836. goto casdef;
  837. case AROTRB:
  838. case AROTRL:
  839. case AROTRW:
  840. case AROTLB:
  841. case AROTLL:
  842. case AROTLW:
  843. case AASLB:
  844. case AASLL:
  845. case AASLW:
  846. case AASRB:
  847. case AASRL:
  848. case AASRW:
  849. case ALSLB:
  850. case ALSLL:
  851. case ALSLW:
  852. case ALSRB:
  853. case ALSRL:
  854. case ALSRW:
  855. if(p->from.type == D_CONST)
  856. if(p->from.offset > 0)
  857. if(p->from.offset <= 8)
  858. p->from.type = D_QUICK;
  859. goto casdef;
  860. case ATSTL:
  861. if(p->to.type >= D_A0 && p->to.type < D_A0+8) {
  862. p->as = ACMPW;
  863. p->from = p->to;
  864. p->to.type = D_CONST;
  865. p->to.offset = 0;
  866. }
  867. goto casdef;
  868. case ACMPL:
  869. if(p->to.type != D_CONST)
  870. goto casdef;
  871. lv = p->to.offset;
  872. if(lv >= -0x7fff && lv <= 0x7fff)
  873. if(p->from.type >= D_A0 && p->from.type < D_A0+8)
  874. p->as = ACMPW;
  875. goto casdef;
  876. case ACLRL:
  877. if(p->to.type >= D_A0 && p->to.type < D_A0+8) {
  878. p->as = AMOVW;
  879. p->from.type = D_CONST;
  880. p->from.offset = 0;
  881. }
  882. goto casdef;
  883. casdef:
  884. default:
  885. if(p->from.type == D_FCONST)
  886. if(optab[p->as].fas != AXXX) {
  887. dv = ieeedtod(&p->from.ieee);
  888. if(dv >= -(1L<<30) && dv <= (1L<<30)) {
  889. lv = dv;
  890. if(lv == dv) {
  891. p->as = optab[p->as].fas;
  892. p->from.type = D_CONST;
  893. p->from.offset = lv;
  894. p->from.displace = 0;
  895. }
  896. }
  897. }
  898. if(p->to.type == D_BRANCH)
  899. p->to.offset += ipc;
  900. lastp->link = p;
  901. lastp = p;
  902. p->pc = pc;
  903. pc++;
  904. goto loop;
  905. }
  906. goto loop;
  907. eof:
  908. diag("%s: truncated object file in %s\n", pn, TNAME);
  909. }
  910. Sym*
  911. lookup(char *symb, int v)
  912. {
  913. Sym *s;
  914. char *p;
  915. long h;
  916. int l, c;
  917. h = v;
  918. for(p=symb; c = *p; p++)
  919. h = h+h+h + c;
  920. l = (p - symb) + 1;
  921. if(h < 0)
  922. h = ~h;
  923. h %= NHASH;
  924. for(s = hash[h]; s != S; s = s->link)
  925. if(s->version == v)
  926. if(memcmp(s->name, symb, l) == 0)
  927. return s;
  928. while(nhunk < sizeof(Sym))
  929. gethunk();
  930. s = (Sym*)hunk;
  931. nhunk -= sizeof(Sym);
  932. hunk += sizeof(Sym);
  933. s->name = malloc(l + 1);
  934. memmove(s->name, symb, l);
  935. s->link = hash[h];
  936. s->type = 0;
  937. s->version = v;
  938. s->value = 0;
  939. hash[h] = s;
  940. nsymbol++;
  941. return s;
  942. }
  943. Prog*
  944. prg(void)
  945. {
  946. Prog *p;
  947. while(nhunk < sizeof(Prog))
  948. gethunk();
  949. p = (Prog*)hunk;
  950. nhunk -= sizeof(Prog);
  951. hunk += sizeof(Prog);
  952. *p = zprg;
  953. return p;
  954. }
  955. Prog*
  956. nprg(Prog *p)
  957. {
  958. Prog *q;
  959. q = prg();
  960. q->line = p->line;
  961. q->pc = p->pc;
  962. q->stkoff = p->stkoff;
  963. q->link = p->link;
  964. p->link = q;
  965. return q;
  966. }
  967. Prog*
  968. copyp(Prog *q)
  969. {
  970. Prog *p;
  971. p = prg();
  972. *p = *q;
  973. return p;
  974. }
  975. void
  976. gethunk(void)
  977. {
  978. char *h;
  979. long nh;
  980. nh = NHUNK;
  981. if(thunk >= 5L*NHUNK) {
  982. nh = 5L*NHUNK;
  983. if(thunk >= 25L*NHUNK)
  984. nh = 25L*NHUNK;
  985. }
  986. h = mysbrk(nh);
  987. if(h == (char*)-1) {
  988. diag("out of memory\n");
  989. errorexit();
  990. }
  991. hunk = h;
  992. nhunk = nh;
  993. thunk += nh;
  994. }
  995. void
  996. doprof1(void)
  997. {
  998. Sym *s;
  999. long n;
  1000. Prog *p, *q;
  1001. if(debug['v'])
  1002. Bprint(&bso, "%5.2f profile 1\n", cputime());
  1003. Bflush(&bso);
  1004. s = lookup("__mcount", 0);
  1005. n = 1;
  1006. for(p = firstp->link; p != P; p = p->link) {
  1007. if(p->as == ATEXT) {
  1008. q = prg();
  1009. q->as = AADDL;
  1010. q->line = p->line;
  1011. q->pc = p->pc;
  1012. q->link = p->link;
  1013. p->link = q;
  1014. q->from.type = D_CONST;
  1015. q->from.offset = 1;
  1016. q->to.type = D_EXTERN;
  1017. q->to.sym = s;
  1018. q->to.offset = n*4 + 4;
  1019. q = prg();
  1020. q->as = ADATA;
  1021. q->line = p->line;
  1022. q->link = datap;
  1023. datap = q;
  1024. q->from.type = D_EXTERN;
  1025. q->from.sym = s;
  1026. q->from.offset = n*4;
  1027. q->from.displace = 4;
  1028. q->to.type = D_EXTERN;
  1029. q->to.sym = p->from.sym;
  1030. n += 2;
  1031. continue;
  1032. }
  1033. }
  1034. q = prg();
  1035. q->line = 0;
  1036. q->as = ADATA;
  1037. q->link = datap;
  1038. datap = q;
  1039. q->from.type = D_EXTERN;
  1040. q->from.sym = s;
  1041. q->from.displace = 4;
  1042. q->to.type = D_CONST;
  1043. q->to.offset = n;
  1044. s->type = SBSS;
  1045. s->value = n*4;
  1046. }
  1047. void
  1048. doprof2(void)
  1049. {
  1050. Sym *s2, *s4;
  1051. Prog *p, *q, *ps2, *ps4;
  1052. if(debug['v'])
  1053. Bprint(&bso, "%5.2f profile 2\n", cputime());
  1054. Bflush(&bso);
  1055. s2 = lookup("_profin", 0);
  1056. s4 = lookup("_profout", 0);
  1057. if(s2->type != STEXT || s4->type != STEXT) {
  1058. diag("_profin/_profout not defined\n");
  1059. return;
  1060. }
  1061. ps2 = P;
  1062. ps4 = P;
  1063. for(p = firstp; p != P; p = p->link) {
  1064. if(p->as == ATEXT) {
  1065. if(p->from.sym == s2) {
  1066. ps2 = p;
  1067. p->from.displace = 1;
  1068. }
  1069. if(p->from.sym == s4) {
  1070. ps4 = p;
  1071. p->from.displace = 1;
  1072. }
  1073. }
  1074. }
  1075. for(p = firstp; p != P; p = p->link) {
  1076. if(p->as == ATEXT) {
  1077. if(p->from.displace != 0) {
  1078. for(;;) {
  1079. q = p->link;
  1080. if(q == P)
  1081. break;
  1082. if(q->as == ATEXT)
  1083. break;
  1084. p = q;
  1085. }
  1086. continue;
  1087. }
  1088. q = prg();
  1089. q->line = p->line;
  1090. q->pc = p->pc;
  1091. q->link = p->link;
  1092. p->link = q;
  1093. p = q;
  1094. p->as = ABSR;
  1095. p->to.type = D_BRANCH;
  1096. p->pcond = ps2;
  1097. p->to.sym = s2;
  1098. continue;
  1099. }
  1100. if(p->as == ARTS) {
  1101. /*
  1102. * RTS
  1103. */
  1104. q = prg();
  1105. q->as = ARTS;
  1106. q->from = p->from;
  1107. q->to = p->to;
  1108. q->link = p->link;
  1109. p->link = q;
  1110. /*
  1111. * BSR profout
  1112. */
  1113. p->as = ABSR;
  1114. p->from = zprg.from;
  1115. p->to = zprg.to;
  1116. p->to.type = D_BRANCH;
  1117. p->pcond = ps4;
  1118. p->to.sym = s4;
  1119. p = q;
  1120. continue;
  1121. }
  1122. }
  1123. }
  1124. long
  1125. reuse(Prog *r, Sym *s)
  1126. {
  1127. Prog *p;
  1128. if(r == P)
  1129. return 0;
  1130. for(p = datap; p != r; p = p->link)
  1131. if(p->to.sym == s)
  1132. return p->from.offset;
  1133. return 0;
  1134. }
  1135. void
  1136. nuxiinit(void)
  1137. {
  1138. int i, c;
  1139. for(i=0; i<4; i++) {
  1140. c = find1(0x01020304L, i+1);
  1141. if(i >= 2)
  1142. inuxi2[i-2] = c;
  1143. if(i >= 3)
  1144. inuxi1[i-3] = c;
  1145. inuxi4[i] = c;
  1146. fnuxi8[i] = c+4;
  1147. fnuxi8[i+4] = c;
  1148. c = find2(0x01020304L, i+1);
  1149. gnuxi8[i] = c+4;
  1150. gnuxi8[i+4] = c;
  1151. }
  1152. if(debug['v']) {
  1153. Bprint(&bso, "inuxi = ");
  1154. for(i=0; i<1; i++)
  1155. Bprint(&bso, "%d", inuxi1[i]);
  1156. Bprint(&bso, " ");
  1157. for(i=0; i<2; i++)
  1158. Bprint(&bso, "%d", inuxi2[i]);
  1159. Bprint(&bso, " ");
  1160. for(i=0; i<4; i++)
  1161. Bprint(&bso, "%d", inuxi4[i]);
  1162. Bprint(&bso, "\n[fg]nuxi = ");
  1163. for(i=0; i<8; i++)
  1164. Bprint(&bso, "%d", fnuxi8[i]);
  1165. Bprint(&bso, " ");
  1166. for(i=0; i<8; i++)
  1167. Bprint(&bso, "%d", gnuxi8[i]);
  1168. Bprint(&bso, "\n");
  1169. }
  1170. Bflush(&bso);
  1171. }
  1172. int
  1173. find1(long l, int c)
  1174. {
  1175. char *p;
  1176. int i;
  1177. p = (char*)&l;
  1178. for(i=0; i<4; i++)
  1179. if(*p++ == c)
  1180. return i;
  1181. return 0;
  1182. }
  1183. int
  1184. find2(long l, int c)
  1185. {
  1186. short *p;
  1187. int i;
  1188. p = (short*)&l;
  1189. for(i=0; i<4; i+=2) {
  1190. if(((*p >> 8) & 0xff) == c)
  1191. return i;
  1192. if((*p++ & 0xff) == c)
  1193. return i+1;
  1194. }
  1195. return 0;
  1196. }
  1197. long
  1198. ieeedtof(Ieee *e)
  1199. {
  1200. int exp;
  1201. long v;
  1202. if(e->h == 0)
  1203. return 0;
  1204. exp = (e->h>>20) & ((1L<<11)-1L);
  1205. exp -= (1L<<10) - 2L;
  1206. v = (e->h & 0xfffffL) << 3;
  1207. v |= (e->l >> 29) & 0x7L;
  1208. if((e->l >> 28) & 1) {
  1209. v++;
  1210. if(v & 0x800000L) {
  1211. v = (v & 0x7fffffL) >> 1;
  1212. exp++;
  1213. }
  1214. }
  1215. if(exp <= -126 || exp >= 130)
  1216. diag("double fp to single fp overflow\n");
  1217. v |= ((exp + 126) & 0xffL) << 23;
  1218. v |= e->h & 0x80000000L;
  1219. return v;
  1220. }
  1221. double
  1222. ieeedtod(Ieee *ieeep)
  1223. {
  1224. Ieee e;
  1225. double fr;
  1226. int exp;
  1227. if(ieeep->h & (1L<<31)) {
  1228. e.h = ieeep->h & ~(1L<<31);
  1229. e.l = ieeep->l;
  1230. return -ieeedtod(&e);
  1231. }
  1232. if(ieeep->l == 0 && ieeep->h == 0)
  1233. return 0;
  1234. fr = ieeep->l & ((1L<<16)-1L);
  1235. fr /= 1L<<16;
  1236. fr += (ieeep->l>>16) & ((1L<<16)-1L);
  1237. fr /= 1L<<16;
  1238. fr += (ieeep->h & (1L<<20)-1L) | (1L<<20);
  1239. fr /= 1L<<21;
  1240. exp = (ieeep->h>>20) & ((1L<<11)-1L);
  1241. exp -= (1L<<10) - 2L;
  1242. return ldexp(fr, exp);
  1243. }