1
0
mirror of https://gitlab.os-k.eu/os-k-team/kvisc.git synced 2023-08-25 14:05:46 +02:00
kvisc/vm/pc/dump.c
2019-06-17 20:59:30 +02:00

230 lines
4.5 KiB
C

// The OS/K Team licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#include <pc/arch.h>
char *cond_suffixes[] =
{
"-",
"c", "o", "z", "s", "p",
"a", "ae", "b", "be",
"g", "ge", "l", "le",
"cxz",
"?"
};
#ifndef _ATT_STYLE
#define _ATT_STYLE 0
#endif
static void dump_acc(ctx_t *ctx, acc_t *p);
void dump_instr(ctx_t *ctx,
instr_t *in,
acc_t *p1,
acc_t *p2,
bool lock,
bool rep,
uint cond,
ulong pc)
{
log("0x%lX:\t", pc);
if (lock)
log("lock");
log("%s", in->name);
#if _ATT_STYLE == 1
if (p1 || p2)
{
int l1 = 8, l2 = 8;
if (p1 && ACC_IS_MEM(p1)) {
l1 = p1->mlen;
}
if (p2 && ACC_IS_MEM(p2)) {
l2 = p2->mlen;
}
log("%c", getmempref(l1<l2 ? l1 : l2));
}
#endif
if (rep)
#if _ATT_STYLE == 0
log(".rep");
#else
log("$rep");
#endif
if (cond)
{
#if _ATT_STYLE == 0
log(".");
#else
log("?");
#endif
if (cond & (1 << 4))
{
cond &= ~(1 << 4);
log("n");
}
assert(cond <= sizeof(cond_suffixes)/sizeof(char *));
log("%s", cond_suffixes[cond]);
}
if (!rep
#if _ATT_STYLE == 1
&& cond != CD_CXZ
#endif
) log("\t\t");
else
log("\t");
if (p1)
{
#if _ATT_STYLE == 0
dump_acc(ctx, p1);
if (p2) {
log(", ");
dump_acc(ctx, p2);
}
#else
if (p2) {
dump_acc(ctx, p2);
log(", ");
}
dump_acc(ctx, p1);
#endif
}
log("\n");
}
#if _ATT_STYLE == 0
void dump_acc(ctx_t *ctx, acc_t *p)
{
uint mfmt;
if (p->type == A_REG)
log("%s", ctx->r[p->reg].name);
else if (p->type == A_IMM64)
{
if (p->val < 0xA)
log("%lu", p->val);
else
log("0x%lX", p->val);
}
else
{
log("%c[", getmempref(p->mlen));
mfmt = p->type & AM_MFMT_MASK;
if (mfmt == AM_IMM64)
log("0x%lX]", p->addr);
else if (mfmt == AM_RR)
{
if (p->reg1 && p->reg2)
log("%s+%s]", ctx->r[p->reg1].name, ctx->r[p->reg2].name);
else log("%s]", ctx->r[p->reg1 ? p->reg1 : p->reg2].name);
}
else if (mfmt == AM_RRI)
{
if (p->reg1 && p->reg2)
log("%s+%s+%hd]", ctx->r[p->reg1].name,
ctx->r[p->reg2].name, p->imm2);
else log("%s+%hd]",
ctx->r[p->reg1 ? p->reg1 : p->reg2].name, p->imm2);
}
else if (mfmt == AM_RRII)
{
if (p->reg1)
log("%s+%s*%u+%hd]",
ctx->r[p->reg1].name,
ctx->r[p->reg2].name,
p->imm1, p->imm2);
else
log("%s*%u+%hd]",
ctx->r[p->reg2].name,
p->imm1, p->imm2);
}
}
}
#else
void dump_acc(ctx_t *ctx, acc_t *p)
{
uint mfmt;
if (p->type == A_REG)
log("%%%s", ctx->r[p->reg].name);
else if (p->type == A_IMM64)
{
if (p->val < 0xA)
log("$%lu", p->val);
else
log("$0x%lX", p->val);
}
else
{
mfmt = p->type & AM_MFMT_MASK;
if (mfmt == AM_IMM64)
log("($0x%lX)", p->addr);
else if (mfmt == AM_RR)
{
if (p->reg1 && p->reg2)
log("(%%%s,%%%s)", ctx->r[p->reg1].name, ctx->r[p->reg2].name);
else log("(%%%s)", ctx->r[p->reg1 ? p->reg1 : p->reg2].name);
}
else if (mfmt == AM_RRI)
{
if (p->reg1 && p->reg2)
log("%hd(%%%s,%%%s)", p->imm2, ctx->r[p->reg1].name,
ctx->r[p->reg2].name);
else log("%hd(%%%s)", p->imm2,
ctx->r[p->reg1 ? p->reg1 : p->reg2].name);
}
else if (mfmt == AM_RRII)
{
if (p->reg1)
log("%hd(%s,%s,$%u)", p->imm2,
ctx->r[p->reg1].name,
ctx->r[p->reg2].name,
p->imm1);
else
log("%hd(*,%s,$%u)", p->imm2,
ctx->r[p->reg2].name,
p->imm1);
}
}
}
#endif