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