📄 x86nasm.md
字号:
vmask[IREG] = 0;
tmask[FREG] = 0xff;
vmask[FREG] = 0;
cseg = 0;
quo = mkreg("eax", EAX, 1, IREG);
quo->x.regnode->mask |= 1<<EDX;
rem = mkreg("edx", EDX, 1, IREG);
rem->x.regnode->mask |= 1<<EAX;
}
static void segment(n) int n; {
if (n == cseg)
return;
cseg = n;
if (cseg == CODE)
print("[section .text]\n");
else if (cseg == DATA || cseg == LIT)
print("[section .data]\n");
else if (cseg == BSS)
print("[section .bss]\n");
}
static void progend() {
}
static void target(p) Node p; {
assert(p);
switch (p->op) {
case RSHI: case RSHU: case LSHI: case LSHU:
if (generic(p->kids[1]->op) != CNST
&& !( generic(p->kids[1]->op) == INDIR
&& p->kids[1]->kids[0]->op == VREG+P
&& p->kids[1]->syms[RX]->u.t.cse
&& generic(p->kids[1]->syms[RX]->u.t.cse->op) == CNST
)) {
rtarget(p, 1, intreg[ECX]);
setreg(p, intreg[EAX]);
}
break;
case MULU:
setreg(p, quo);
rtarget(p, 0, intreg[EAX]);
break;
case DIVI: case DIVU:
setreg(p, quo);
rtarget(p, 0, intreg[EAX]);
rtarget(p, 1, intreg[ECX]);
break;
case MODI: case MODU:
setreg(p, rem);
rtarget(p, 0, intreg[EAX]);
rtarget(p, 1, intreg[ECX]);
break;
case ASGNB:
rtarget(p, 0, intreg[EDI]);
rtarget(p->kids[1], 0, intreg[ESI]);
break;
case ARGB:
rtarget(p->kids[0], 0, intreg[ESI]);
break;
case CALLI: case CALLV:
setreg(p, intreg[EAX]);
break;
case RETI:
rtarget(p, 0, intreg[EAX]);
break;
}
}
static void clobber(p) Node p; {
static int nstack = 0;
assert(p);
nstack = ckstack(p, nstack);
assert(p->count > 0 || nstack == 0);
switch (p->op) {
case ASGNB: case ARGB:
spill(1<<ECX | 1<<ESI | 1<<EDI, IREG, p);
break;
case EQD: case LED: case GED: case LTD: case GTD: case NED:
case EQF: case LEF: case GEF: case LTF: case GTF: case NEF:
spill(1<<EAX, IREG, p);
break;
case CALLD: case CALLF:
spill(1<<EDX | 1<<EAX, IREG, p);
break;
}
}
#define isfp(p) (optype((p)->op)==F || optype((p)->op)==D)
static int ckstack(p, n) Node p; int n; {
int i;
for (i = 0; i < NELEMS(p->x.kids) && p->x.kids[i]; i++)
if (isfp(p->x.kids[i]))
n--;
if (isfp(p) && p->count > 0)
n++;
if (n > 8)
error("expression too complicated\n");
debug(fprint(2, "(ckstack(%x)=%d)\n", p, n));
assert(n >= 0);
return n;
}
static int memop(p) Node p; {
assert(p);
assert(generic(p->op) == ASGN);
assert(p->kids[0]);
assert(p->kids[1]);
if (generic(p->kids[1]->kids[0]->op) == INDIR
&& sametree(p->kids[0], p->kids[1]->kids[0]->kids[0]))
return 3;
else
return LBURG_MAX;
}
static int sametree(p, q) Node p, q; {
return p == NULL && q == NULL
|| p && q && p->op == q->op && p->syms[0] == q->syms[0]
&& sametree(p->kids[0], q->kids[0])
&& sametree(p->kids[1], q->kids[1]);
}
static void emit2(p) Node p; {
#define preg(f) ((f)[getregnum(p->x.kids[0])]->x.name)
if (p->op == CVCI)
print("movsx %s,%s\n", p->syms[RX]->x.name
, preg(charreg));
else if (p->op == CVCU)
print("movzx %s,%s\n", p->syms[RX]->x.name
, preg(charreg));
else if (p->op == CVSI)
print("movsx %s,%s\n", p->syms[RX]->x.name
, preg(shortreg));
else if (p->op == CVSU)
print("movzx %s,%s\n", p->syms[RX]->x.name
, preg(shortreg));
else if (p->op == CVIC || p->op == CVIS
|| p->op == CVUC || p->op == CVUS) {
char *dst = shortreg[getregnum(p)]->x.name;
char *src = preg(shortreg);
if (dst != src)
print("mov %s,%s\n", dst, src);
}
}
static void doarg(p) Node p; {
assert(p && p->syms[0]);
mkactual(4, p->syms[0]->u.c.v.i);
}
static void blkfetch(k, off, reg, tmp)
int k, off, reg, tmp; {}
static void blkstore(k, off, reg, tmp)
int k, off, reg, tmp; {}
static void blkloop(dreg, doff, sreg, soff, size, tmps)
int dreg, doff, sreg, soff, size, tmps[]; {}
static void local(p) Symbol p; {
if (isfloat(p->type))
p->sclass = AUTO;
if (askregvar(p, rmap[ttob(p->type)]) == 0)
mkauto(p);
}
static void function(f, caller, callee, n)
Symbol f, callee[], caller[]; int n; {
int i;
print("%s:\n", f->x.name);
print("push ebx\n");
print("push esi\n");
print("push edi\n");
print("push ebp\n");
print("mov ebp,esp\n");
usedmask[0] = usedmask[1] = 0;
freemask[0] = freemask[1] = ~(unsigned)0;
offset = 16 + 4;
for (i = 0; callee[i]; i++) {
Symbol p = callee[i];
Symbol q = caller[i];
assert(q);
p->x.offset = q->x.offset = offset;
p->x.name = q->x.name = stringf("%d", p->x.offset);
p->sclass = q->sclass = AUTO;
offset += roundup(q->type->size, 4);
}
assert(caller[i] == 0);
offset = maxoffset = 0;
gencode(caller, callee);
framesize = roundup(maxoffset, 4);
if (framesize > 0)
print("sub esp,%d\n", framesize);
emitcode();
print("mov esp,ebp\n");
print("pop ebp\n");
print("pop edi\n");
print("pop esi\n");
print("pop ebx\n");
print("ret\n");
}
static void defsymbol(p) Symbol p; {
if (p->scope >= LOCAL && p->sclass == STATIC)
p->x.name = stringf("L%d", genlabel(1));
else if (p->generated)
p->x.name = stringf("$L%s", p->name);
else if (p->scope == GLOBAL || p->sclass == EXTERN)
/* CHANGE THIS FOR a.out */
#if 0
p->x.name = stringf("$_%s", p->name);
#else
p->x.name = stringf("$%s", p->name);
#endif
else if (p->scope == CONSTANTS
&& (isint(p->type) || isptr(p->type))
&& p->name[0] == '0' && p->name[1] == 'x')
p->x.name = stringf("0%sH", &p->name[2]);
else
p->x.name = p->name;
}
static void address(q, p, n) Symbol q, p; int n; {
if (p->scope == GLOBAL
|| p->sclass == STATIC || p->sclass == EXTERN)
q->x.name = stringf("%s%s%d",
p->x.name, n >= 0 ? "+" : "", n);
else {
q->x.offset = p->x.offset + n;
q->x.name = stringd(q->x.offset);
}
}
static void defconst(ty, v) int ty; Value v; {
switch (ty) {
case C: print("db %d\n", v.uc); return;
case S: print("dw %d\n", v.ss); return;
case I: print("dd %d\n", v.i ); return;
case U: print("dd 0%xH\n", v.u ); return;
case P: print("dd 0%xH\n", v.p ); return;
case F:
print("dd 0%xH\n", *(unsigned *)&v.f);
return;
case D: {
unsigned *p = (unsigned *)&v.d;
print("dd 0%xH,0%xH\n", p[swap], p[1 - swap]);
return;
}
}
assert(0);
}
static void defaddress(p) Symbol p; {
print("dd %s\n", p->x.name);
}
static void defstring(n, str) int n; char *str; {
char *s;
int inquote = 1;
print("db '");
for (s = str; s < str + n; s++)
{
if ((*s & 0x7F) == *s && *s >= ' ' && *s != '\'') {
if (!inquote){
print(", '");
inquote = 1;
}
print("%c",*s);
}
else
{
if (inquote){
print("', ");
inquote = 0;
}
else
print(", ");
print("%d",*s);
}
}
if (inquote) print("'");
print("\n");
}
static void export(p) Symbol p; {
print("[global %s]\n", p->x.name);
}
static void import(p) Symbol p; {
if (p->ref > 0) {
print("[extern %s]\n", p->x.name);
}
}
static void global(p) Symbol p; {
int i;
if (p->u.seg == BSS)
print("resb ($-$$) & %d\n",
p->type->align > 4 ? 3 : p->type->align-1);
else
print("times ($-$$) & %d nop\n",
p->type->align > 4 ? 3 : p->type->align-1);
print("%s:\n", p->x.name);
if (p->u.seg == BSS)
print("resb %d\n", p->type->size);
}
static void space(n) int n; {
int i;
if (cseg != BSS)
print("times %d db 0\n", n);
}
Interface x86nasmIR = {
1, 1, 0, /* char */
2, 2, 0, /* short */
4, 4, 0, /* int */
4, 4, 1, /* float */
8, 4, 1, /* double */
4, 4, 0, /* T * */
0, 4, 0, /* struct; so that ARGB keeps stack aligned */
1, /* little_endian */
0, /* mulops_calls */
0, /* wants_callb */
1, /* wants_argb */
0, /* left_to_right */
0, /* wants_dag */
address,
blockbeg,
blockend,
defaddress,
defconst,
defstring,
defsymbol,
emit,
export,
function,
gen,
global,
import,
local,
progbeg,
progend,
segment,
space,
0, 0, 0, 0, 0, 0, 0,
{1, blkfetch, blkstore, blkloop,
_label,
_rule,
_nts,
_kids,
_opname,
_arity,
_string,
_templates,
_isinstruction,
_ntname,
emit2,
doarg,
target,
clobber,
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -