This source file includes following definitions.
- addlib
- addlibpath
- find1
- nuxiinit
- mkfwd
- copyp
- appendp
- atolwhex
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <link.h>
void
addlib(Link *ctxt, char *src, char *obj, char *pathname)
{
char name[1024], pname[1024], *p;
int i;
if(strlen(pathname) >= sizeof name)
sysfatal("addlib pathname too long");
strcpy(name, pathname);
cleanname(name);
p = nil;
if(strlen(name) > 2 && name[strlen(name)-2] == '.') {
p = name+strlen(name)-2;
*p = '\0';
}
for(i=0; i<ctxt->libraryp; i++)
if(strcmp(ctxt->library[i].pkg, name) == 0)
return;
if(p != nil)
*p = '.';
if((!ctxt->windows && name[0] == '/') || (ctxt->windows && name[1] == ':'))
snprint(pname, sizeof pname, "%s", name);
else {
for(i=0; i<ctxt->nlibdir; i++) {
snprint(pname, sizeof pname, "%s/%s", ctxt->libdir[i], name);
if(access(pname, AEXIST) >= 0)
break;
}
}
cleanname(pname);
if(p != nil)
*p = '\0';
if(ctxt->debugvlog > 1 && ctxt->bso)
Bprint(ctxt->bso, "%5.2f addlib: %s %s pulls in %s\n", cputime(), obj, src, pname);
addlibpath(ctxt, src, obj, pname, name);
}
void
addlibpath(Link *ctxt, char *srcref, char *objref, char *file, char *pkg)
{
int i;
Library *l;
for(i=0; i<ctxt->libraryp; i++)
if(strcmp(file, ctxt->library[i].file) == 0)
return;
if(ctxt->debugvlog > 1 && ctxt->bso)
Bprint(ctxt->bso, "%5.2f addlibpath: srcref: %s objref: %s file: %s pkg: %s\n",
cputime(), srcref, objref, file, pkg);
if(ctxt->libraryp == ctxt->nlibrary){
ctxt->nlibrary = 50 + 2*ctxt->libraryp;
ctxt->library = erealloc(ctxt->library, sizeof ctxt->library[0] * ctxt->nlibrary);
}
l = &ctxt->library[ctxt->libraryp++];
l->objref = estrdup(objref);
l->srcref = estrdup(srcref);
l->file = estrdup(file);
l->pkg = estrdup(pkg);
}
int
find1(int32 l, int c)
{
char *p;
int i;
p = (char*)&l;
for(i=0; i<4; i++)
if(*p++ == c)
return i;
return 0;
}
void
nuxiinit(void)
{
int i, c;
for(i=0; i<4; i++) {
c = find1(0x04030201L, i+1);
if(i < 2)
inuxi2[i] = c;
if(i < 1)
inuxi1[i] = c;
inuxi4[i] = c;
if(c == i) {
inuxi8[i] = c;
inuxi8[i+4] = c+4;
} else {
inuxi8[i] = c+4;
inuxi8[i+4] = c;
}
fnuxi4[i] = c;
fnuxi8[i] = c;
fnuxi8[i+4] = c+4;
}
}
uchar fnuxi8[8];
uchar fnuxi4[4];
uchar inuxi1[1];
uchar inuxi2[2];
uchar inuxi4[4];
uchar inuxi8[8];
#define LOG 5
void
mkfwd(LSym *sym)
{
Prog *p;
int i;
int32 dwn[LOG], cnt[LOG];
Prog *lst[LOG];
for(i=0; i<LOG; i++) {
if(i == 0)
cnt[i] = 1;
else
cnt[i] = LOG * cnt[i-1];
dwn[i] = 1;
lst[i] = nil;
}
i = 0;
for(p = sym->text; p != nil && p->link != nil; p = p->link) {
i--;
if(i < 0)
i = LOG-1;
p->forwd = nil;
dwn[i]--;
if(dwn[i] <= 0) {
dwn[i] = cnt[i];
if(lst[i] != nil)
lst[i]->forwd = p;
lst[i] = p;
}
}
}
Prog*
copyp(Link *ctxt, Prog *q)
{
Prog *p;
p = ctxt->arch->prg();
*p = *q;
return p;
}
Prog*
appendp(Link *ctxt, Prog *q)
{
Prog *p;
p = ctxt->arch->prg();
p->link = q->link;
q->link = p;
p->lineno = q->lineno;
p->mode = q->mode;
return p;
}
vlong
atolwhex(char *s)
{
vlong n;
int f;
n = 0;
f = 0;
while(*s == ' ' || *s == '\t')
s++;
if(*s == '-' || *s == '+') {
if(*s++ == '-')
f = 1;
while(*s == ' ' || *s == '\t')
s++;
}
if(s[0]=='0' && s[1]){
if(s[1]=='x' || s[1]=='X'){
s += 2;
for(;;){
if(*s >= '0' && *s <= '9')
n = n*16 + *s++ - '0';
else if(*s >= 'a' && *s <= 'f')
n = n*16 + *s++ - 'a' + 10;
else if(*s >= 'A' && *s <= 'F')
n = n*16 + *s++ - 'A' + 10;
else
break;
}
} else
while(*s >= '0' && *s <= '7')
n = n*8 + *s++ - '0';
} else
while(*s >= '0' && *s <= '9')
n = n*10 + *s++ - '0';
if(f)
n = -n;
return n;
}