12 Bprint(&bso, "%5.2f dodata\n", cputime());
14 for(p = datap; p != P; p = p->link) {
16 if(p->as == ADYNT || p->as == AINIT)
21 diag("initialize non-data (%d): %s\n%P",
23 v = p->from.offset + p->reg;
25 diag("initialize bounds (%ld): %s\n%P",
26 s->value, s->name, p);
31 * assign 'small' variables to data segment
32 * (rational is that data segment is more easily
33 * addressed through offset on REGSB)
36 for(i=0; i<NHASH; i++)
37 for(s = hash[i]; s != S; s = s->link) {
39 if(t != SDATA && t != SBSS)
43 diag("%s: no size", s->name);
62 * assign 'data' variables to data segment
64 for(i=0; i<NHASH; i++)
65 for(s = hash[i]; s != S; s = s->link) {
87 * everything else to bss segment
89 for(i=0; i<NHASH; i++)
90 for(s = hash[i]; s != S; s = s->link) {
102 bsssize = orig-datsize;
106 * add literals to all large values.
108 * small data is allocated DATA
109 * large data is allocated DATA1
110 * large bss is allocated BSS
111 * the new literals are loaded between
112 * small data and large data.
115 for(p = firstp; p != P; p = p->link) {
118 if(p->from.type != D_CONST)
120 if(s = p->from.sym) {
122 if(t != SDATA && t != SDATA1 && t != SBSS)
125 if(t != D_EXTERN && t != D_STATIC)
127 v = s->value + p->from.offset;
128 if(v >= 0 && v <= 0xffff)
130 if(!strcmp(s->name, "setSB"))
132 /* size should be 19 max */
133 if(strlen(s->name) >= 10) /* has loader address */
134 snprint(literal, sizeof literal, "$%p.%lux", s, p->from.offset);
136 snprint(literal, sizeof literal, "$%s.%d.%lux", s->name, s->version, p->from.offset);
138 if(p->from.name != D_NONE)
140 if(p->from.reg != NREG)
143 if(v >= -0x7fff-1 && v <= 0x7fff)
148 continue; /* quicker to build it than load it */
149 /* size should be 9 max */
150 snprint(literal, sizeof literal, "$%lux", v);
152 s = lookup(literal, 0);
155 s->value = orig1+orig;
160 p1->from.type = D_OREG;
162 p1->from.name = D_EXTERN;
169 diag("literal not data: %s", s->name);
170 p->from.type = D_OREG;
172 p->from.name = D_EXTERN;
182 for(i=0; i<NHASH; i++)
183 for(s = hash[i]; s != S; s = s->link) {
196 xdefine("setSB", SDATA, 0L+BIG);
197 xdefine("bdata", SDATA, 0L);
198 xdefine("edata", SDATA, datsize);
199 xdefine("end", SBSS, datsize+bsssize);
200 xdefine("etext", STEXT, 0L);
209 for(i=0; i<NHASH; i++)
210 for(s = hash[i]; s != S; s = s->link)
212 diag("%s: not defined", s->name);
220 case ABEQ: return ABNE;
221 case ABNE: return ABEQ;
223 case ABGE: return ABLT;
224 case ABLT: return ABGE;
226 case ABGT: return ABLE;
227 case ABLE: return ABGT;
229 case ABVC: return ABVS;
230 case ABVS: return ABVC;
240 Bprint(&bso, "%5.2f follow\n", cputime());
248 firstp = firstp->link;
266 if((p->mark&NOSCHED) || q && (q->mark&NOSCHED)){
273 if(p && !(p->mark & FOLL))
280 if(!(p->mark & FOLL))
285 for(i=0,q=p; i<4; i++,q=q->link) {
286 if(q == lastp || (q->mark&NOSCHED))
294 if(a == ABR || a == ARETURN || a == ARFI || a == ARFCI)
296 if(!q->cond || (q->cond->mark&FOLL))
306 print("cant happen 1\n");
316 if(a == ABR || a == ARETURN || a == ARFI || a == ARFCI)
321 if(!(r->link->mark&FOLL))
323 if(!(r->cond->mark&FOLL))
324 print("cant happen 2\n");
333 q->to.type = D_BRANCH;
334 q->to.offset = p->pc;
341 if(a == ABR || a == ARETURN || a == ARFI || a == ARFCI){
342 if(p->mark & NOSCHED){
349 if(a != ABL && p->link != P) {
352 if(p == P || (p->mark&FOLL))
369 Bprint(&bso, "%5.2f patch\n", cputime());
372 s = lookup("exit", 0);
374 for(p = firstp; p != P; p = p->link) {
378 if((a == ABL || a == ARETURN) && p->to.sym != S) {
380 if(s->type != STEXT && s->type != SUNDEF) {
381 diag("undefined: %s\n%P", s->name, p);
385 if(s->type == SUNDEF){
390 p->to.offset = s->value;
391 p->to.type = D_BRANCH;
393 if(p->to.type != D_BRANCH || p->cond == UP)
396 for(q = firstp; q != P;) {
398 if(c >= q->forwd->pc) {
407 diag("branch out of range %ld\n%P", c, p);
413 for(p = firstp; p != P; p = p->link) {
416 p->mark = 0; /* initialization for follow */
417 if(p->cond != P && p->cond != UP) {
418 p->cond = brloop(p->cond);
420 if(p->to.type == D_BRANCH)
421 p->to.offset = p->cond->pc;
431 long dwn[LOG], cnt[LOG], i;
434 for(i=0; i<LOG; i++) {
437 cnt[i] = LOG * cnt[i-1];
442 for(p = firstp; p != P; p = p->link) {
466 if(p->as != ABR || (p->mark&NOSCHED))
471 if(q == p || c > 5000)
487 while(*s == ' ' || *s == '\t')
489 if(*s == '-' || *s == '+') {
492 while(*s == ' ' || *s == '\t')
495 if(s[0]=='0' && s[1]){
496 if(s[1]=='x' || s[1]=='X'){
499 if(*s >= '0' && *s <= '9')
500 n = n*16 + *s++ - '0';
501 else if(*s >= 'a' && *s <= 'f')
502 n = n*16 + *s++ - 'a' + 10;
503 else if(*s >= 'A' && *s <= 'F')
504 n = n*16 + *s++ - 'A' + 10;
509 while(*s >= '0' && *s <= '7')
510 n = n*8 + *s++ - '0';
512 while(*s >= '0' && *s <= '9')
513 n = n*10 + *s++ - '0';
540 for(i = 0; i < NHASH; i++)
541 for(s = hash[i]; s != S; s = s->link)
542 if(s->sig != 0 && s->type == SXREF && (nimports == 0 || s->subtype == SIMPORT)){
544 Bprint(&bso, "IMPORT: %s sig=%lux v=%ld\n", s->name, s->sig, s->value);
549 ckoff(Sym *s, long v)
551 if(v < 0 || v >= 1<<Roffset)
552 diag("relocation offset %ld for %s out of range", v, s->name);
556 newdata(Sym *s, int o, int w, int t)
565 p->from.type = D_OREG;
569 p->to.type = D_CONST;
577 int i, j, n, off, nb, sv, ne;
578 Sym *s, *et, *str, **esyms;
580 char buf[NSNAME], *t;
583 for(i = 0; i < NHASH; i++)
584 for(s = hash[i]; s != S; s = s->link)
585 if(s->sig != 0 && s->type != SXREF && s->type != SUNDEF && (nexports == 0 || s->subtype == SEXPORT))
587 esyms = malloc(n*sizeof(Sym*));
590 for(i = 0; i < NHASH; i++)
591 for(s = hash[i]; s != S; s = s->link)
592 if(s->sig != 0 && s->type != SXREF && s->type != SUNDEF && (nexports == 0 || s->subtype == SEXPORT))
594 for(i = 0; i < ne-1; i++)
595 for(j = i+1; j < ne; j++)
596 if(strcmp(esyms[i]->name, esyms[j]->name) > 0){
604 et = lookup(EXPTAB, 0);
605 if(et->type != 0 && et->type != SXREF)
606 diag("%s already defined", EXPTAB);
608 str = lookup(".string", 0);
612 for(i = 0; i < ne; i++){
614 Bprint(&bso, "EXPORT: %s sig=%lux t=%d\n", s->name, s->sig, s->type);
617 p = newdata(et, off, sizeof(long), D_EXTERN);
619 p->to.offset = s->sig;
622 p = newdata(et, off, sizeof(long), D_EXTERN);
624 p->to.name = D_EXTERN;
634 p = newdata(str, sv-NSNAME, NSNAME, D_STATIC);
635 p->to.type = D_SCONST;
636 memmove(p->to.sval, buf, NSNAME);
644 p = newdata(et, off, sizeof(long), D_EXTERN);
646 p->to.name = D_STATIC;
652 p = newdata(str, sv-nb, nb, D_STATIC);
653 p->to.type = D_SCONST;
654 memmove(p->to.sval, buf, nb);
657 for(i = 0; i < 3; i++){
658 newdata(et, off, sizeof(long), D_EXTERN);