You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
wasm3/source/m3_info.c

491 lines
13 KiB
C

5 years ago
//
// m3_info.c
//
// Created by Steven Massey on 4/27/19.
// Copyright © 2019 Steven Massey. All rights reserved.
//
#include "m3_env.h"
5 years ago
#include "m3_info.h"
#include "m3_emit.h"
5 years ago
#include "m3_compile.h"
3 years ago
#include "m3_exec.h"
5 years ago
#ifdef DEBUG
// a central function you can be breakpoint:
void ExceptionBreakpoint (cstr_t i_exception, cstr_t i_message)
{
3 years ago
printf ("\nexception: '%s' @ %s\n", i_exception, i_message);
return;
}
typedef struct OpInfo
{
IM3OpInfo info;
m3opcode_t opcode;
}
OpInfo;
5 years ago
void m3_PrintM3Info ()
{
printf ("\n-- m3 configuration --------------------------------------------\n");
// printf (" sizeof M3CodePage : %zu bytes (%d slots) \n", sizeof (M3CodePage), c_m3CodePageNumSlots);
printf (" sizeof M3MemPage : %u bytes \n", d_m3MemPageSize);
printf (" sizeof M3Compilation : %zu bytes \n", sizeof (M3Compilation));
3 years ago
printf (" sizeof M3Function : %zu bytes \n", sizeof (M3Function));
printf ("----------------------------------------------------------------\n\n");
5 years ago
}
void * v_PrintEnvModuleInfo (IM3Module i_module, u32 * io_index)
{
printf (" module [%u] name: '%s'; funcs: %d \n", * io_index++, i_module->name, i_module->numFunctions);
return NULL;
5 years ago
}
void m3_PrintRuntimeInfo (IM3Runtime i_runtime)
{
printf ("\n-- m3 runtime -------------------------------------------------\n");
4 years ago
printf (" stack-size: %zu \n\n", i_runtime->numStackSlots * sizeof (m3slot_t));
u32 moduleIndex = 0;
ForEachModule (i_runtime, (ModuleVisitor) v_PrintEnvModuleInfo, & moduleIndex);
printf ("----------------------------------------------------------------\n\n");
5 years ago
}
cstr_t GetTypeName (u8 i_m3Type)
{
if (i_m3Type < 5)
return c_waTypes [i_m3Type];
else
return "?";
5 years ago
}
3 years ago
// TODO: these 'static char string []' aren't thread-friendly. though these functions are
// mainly for simple diagnostics during development, it'd be nice if they were fully reliable.
cstr_t SPrintFuncTypeSignature (IM3FuncType i_funcType)
5 years ago
{
static char string [256];
sprintf (string, "(");
for (u32 i = 0; i < i_funcType->numArgs; ++i)
{
if (i != 0)
strcat (string, ", ");
4 years ago
strcat (string, GetTypeName (d_FuncArgType(i_funcType, i)));
}
strcat (string, ") -> ");
for (u32 i = 0; i < i_funcType->numRets; ++i)
{
if (i != 0)
strcat (string, ", ");
strcat (string, GetTypeName (d_FuncRetType(i_funcType, i)));
}
4 years ago
return string;
5 years ago
}
size_t SPrintArg (char * o_string, size_t i_stringBufferSize, m3stack_t i_sp, u8 i_type)
5 years ago
{
int len = 0;
* o_string = 0;
if (i_type == c_m3Type_i32)
len = snprintf (o_string, i_stringBufferSize, "%" PRIi32, * (i32 *) i_sp);
else if (i_type == c_m3Type_i64)
len = snprintf (o_string, i_stringBufferSize, "%" PRIi64, * (i64 *) i_sp);
#if d_m3HasFloat
else if (i_type == c_m3Type_f32)
len = snprintf (o_string, i_stringBufferSize, "%" PRIf32, * (f32 *) i_sp);
else if (i_type == c_m3Type_f64)
len = snprintf (o_string, i_stringBufferSize, "%" PRIf64, * (f64 *) i_sp);
#endif
len = M3_MAX (0, len);
return len;
5 years ago
}
cstr_t SPrintValue (void * i_value, u8 i_type)
{
3 years ago
static char string [100];
SPrintArg (string, 100, (m3stack_t) i_value, i_type);
return string;
}
cstr_t SPrintFunctionArgList (IM3Function i_function, m3stack_t i_sp)
5 years ago
{
int ret;
static char string [256];
char * s = string;
ccstr_t e = string + sizeof(string) - 1;
ret = snprintf (s, e-s, "(");
s += M3_MAX (0, ret);
4 years ago
m3stack_t argSp = i_sp;
4 years ago
IM3FuncType funcType = i_function->funcType;
if (funcType)
{
u32 numArgs = funcType->numArgs;
for (u32 i = 0; i < numArgs; ++i)
{
u8 type = d_FuncArgType(funcType, i);
ret = snprintf (s, e-s, "%s: ", c_waTypes [type]);
s += M3_MAX (0, ret);
4 years ago
s += SPrintArg (s, e-s, argSp + i, type);
if (i != numArgs - 1) {
ret = snprintf (s, e-s, ", ");
s += M3_MAX (0, ret);
}
}
}
else printf ("null signature");
ret = snprintf (s, e-s, ")");
s += M3_MAX (0, ret);
return string;
5 years ago
}
static
OpInfo find_operation_info (IM3Operation i_operation)
5 years ago
{
4 years ago
OpInfo opInfo = { NULL, 0 };
if (!i_operation) return opInfo;
// TODO: find also extended opcodes
for (u32 i = 0; i <= 0xff; ++i)
{
3 years ago
IM3OpInfo oi = GetOpInfo (i);
if (oi->type != c_m3Type_unknown)
{
for (u32 o = 0; o < 4; ++o)
{
if (oi->operations [o] == i_operation)
{
opInfo.info = oi;
opInfo.opcode = i;
break;
}
}
}
else break;
}
return opInfo;
5 years ago
}
5 years ago
#undef fetch
#define fetch(TYPE) (* (TYPE *) ((*o_pc)++))
5 years ago
5 years ago
#define d_m3Decoder(FUNC) void Decode_##FUNC (char * o_string, u8 i_opcode, IM3Operation i_operation, IM3OpInfo i_opInfo, pc_t * o_pc)
d_m3Decoder (Call)
5 years ago
{
5 years ago
void * function = fetch (void *);
i32 stackOffset = fetch (i32);
5 years ago
5 years ago
sprintf (o_string, "%p; stack-offset: %d", function, stackOffset);
}
5 years ago
d_m3Decoder (Entry)
5 years ago
{
5 years ago
IM3Function function = fetch (IM3Function);
5 years ago
// only prints out the first registered name for the function
3 years ago
sprintf (o_string, "%s", m3_GetFunctionName(function));
5 years ago
}
5 years ago
d_m3Decoder (f64_Store)
{
if (i_operation == i_opInfo->operations [0])
{
u32 operand = fetch (u32);
u32 offset = fetch (u32);
5 years ago
5 years ago
sprintf (o_string, "offset= slot:%d + immediate:%d", operand, offset);
}
5 years ago
5 years ago
// sprintf (o_string, "%s", function->name);
}
5 years ago
5 years ago
d_m3Decoder (Branch)
5 years ago
{
5 years ago
void * target = fetch (void *);
sprintf (o_string, "%p", target);
}
5 years ago
d_m3Decoder (BranchTable)
5 years ago
{
u32 slot = fetch (u32);
5 years ago
sprintf (o_string, "slot: %" PRIu32 "; targets: ", slot);
5 years ago
5 years ago
// IM3Function function = fetch2 (IM3Function);
5 years ago
i32 targets = fetch (i32);
5 years ago
5 years ago
char str [1000];
5 years ago
for (i32 i = 0; i < targets; ++i)
{
pc_t addr = fetch (pc_t);
sprintf (str, "%" PRIi32 "=%p, ", i, addr);
strcat (o_string, str);
}
5 years ago
5 years ago
pc_t addr = fetch (pc_t);
sprintf (str, "def=%p ", addr);
strcat (o_string, str);
}
5 years ago
d_m3Decoder (Const)
5 years ago
{
u64 value = fetch (u64); i32 offset = fetch (i32);
sprintf (o_string, " slot [%d] = %" PRIu64, offset, value);
}
5 years ago
#undef fetch
5 years ago
void DecodeOperation (char * o_string, u8 i_opcode, IM3Operation i_operation, IM3OpInfo i_opInfo, pc_t * o_pc)
5 years ago
{
5 years ago
#define d_m3Decode(OPCODE, FUNC) case OPCODE: Decode_##FUNC (o_string, i_opcode, i_operation, i_opInfo, o_pc); break;
5 years ago
switch (i_opcode)
{
4 years ago
// d_m3Decode (0xc0, Const)
3 years ago
d_m3Decode (0xc5, Entry)
5 years ago
d_m3Decode (c_waOp_call, Call)
d_m3Decode (c_waOp_branch, Branch)
d_m3Decode (c_waOp_branchTable, BranchTable)
5 years ago
d_m3Decode (0x39, f64_Store)
5 years ago
}
5 years ago
}
// WARNING/TODO: this isn't fully implemented. it blindly assumes each word is a Operation pointer
// and, if an operation happens to missing from the c_operations table it won't be recognized here
void dump_code_page (IM3CodePage i_codePage, pc_t i_startPC)
5 years ago
{
m3log (code, "code page seq: %d", i_codePage->info.sequence);
5 years ago
pc_t pc = i_startPC ? i_startPC : GetPageStartPC (i_codePage);
pc_t end = GetPagePC (i_codePage);
m3log (code, "---------------------------------------------------------------------------------------");
while (pc < end)
{
5 years ago
pc_t operationPC = pc;
IM3Operation op = (IM3Operation) (* pc++);
OpInfo i = find_operation_info (op);
if (i.info)
{
char infoString [1000] = { 0 };
5 years ago
5 years ago
DecodeOperation (infoString, i.opcode, op, i.info, & pc);
5 years ago
m3log (code, "%p | %20s %s", operationPC, i.info->name, infoString);
}
5 years ago
else
m3log (code, "%p | %p", operationPC, op);
}
m3log (code, "---------------------------------------------------------------------------------------");
5 years ago
5 years ago
m3log (code, "free-lines: %d", i_codePage->info.numLines - i_codePage->info.lineIndex);
5 years ago
}
void dump_type_stack (IM3Compilation o)
{
/* Reminders about how the stack works! :)
-- args & locals remain on the type stack for duration of the function. Denoted with a constant 'A' and 'L' in this dump.
4 years ago
-- the initial stack dumps originate from the CompileLocals () function, so these identifiers won't/can't be
applied until this compilation stage is finished
-- constants are not statically represented in the type stack (like args & constants) since they don't have/need
write counts
4 years ago
-- the number shown for static args and locals (value in wasmStack [i]) represents the write count for the variable
4 years ago
-- (does Wasm ever write to an arg? I dunno/don't remember.)
-- the number for the dynamic stack values represents the slot number.
-- if the slot index points to arg, local or constant it's denoted with a lowercase 'a', 'l' or 'c'
5 years ago
*/
5 years ago
// for the assert at end of dump:
i32 regAllocated [2] = { (i32) IsRegisterAllocated (o, 0), (i32) IsRegisterAllocated (o, 1) };
5 years ago
// display whether r0 or fp0 is allocated. these should then also be reflected somewhere in the stack too.
3 years ago
d_m3Log(stack, "\n");
d_m3Log(stack, " ");
printf ("%s %s ", regAllocated [0] ? "(r0)" : " ", regAllocated [1] ? "(fp0)" : " ");
5 years ago
3 years ago
// printf ("%d", o->stackIndex -)
3 years ago
for (u32 i = o->stackFirstDynamicIndex; i < o->stackIndex; ++i)
{
3 years ago
if (i == o->block.blockStackIndex)
printf (" |");
printf (" %s", c_waCompactTypes [o->typeStack [i]]);
5 years ago
4 years ago
u16 slot = o->wasmStack [i];
5 years ago
3 years ago
if (IsRegisterSlotAlias (slot))
4 years ago
{
3 years ago
bool isFp = IsFpRegisterSlotAlias (slot);
4 years ago
printf ("%s", isFp ? "f0" : "r0");
5 years ago
4 years ago
regAllocated [isFp]--;
}
else
{
3 years ago
if (slot < o->slotFirstDynamicIndex)
4 years ago
{
3 years ago
if (slot >= o->slotFirstConstIndex)
4 years ago
printf ("c");
else if (slot >= o->function->numRetAndArgSlots)
4 years ago
printf ("L");
4 years ago
else
printf ("a");
}
5 years ago
4 years ago
printf ("%d", (i32) slot); // slot
}
printf (" ");
}
4 years ago
printf ("\n");
5 years ago
for (u32 r = 0; r < 2; ++r)
d_m3Assert (regAllocated [r] == 0); // reg allocation & stack out of sync
3 years ago
u16 maxSlot = GetMaxUsedSlotPlusOne (o);
if (maxSlot > o->slotFirstDynamicIndex)
{
d_m3Log (stack, " -");
for (u16 i = o->slotFirstDynamicIndex; i < maxSlot; ++i)
printf ("----");
printf ("\n");
d_m3Log (stack, " slot |");
for (u16 i = o->slotFirstDynamicIndex; i < maxSlot; ++i)
printf ("%3d|", i);
printf ("\n");
d_m3Log (stack, " alloc |");
for (u16 i = o->slotFirstDynamicIndex; i < maxSlot; ++i)
{
printf ("%3d|", o->m3Slots [i]);
}
printf ("\n");
}
d_m3Log(stack, "\n");
}
5 years ago
static const char * GetOpcodeIndentionString (i32 blockDepth)
{
blockDepth += 1;
5 years ago
if (blockDepth < 0)
blockDepth = 0;
5 years ago
static const char * s_spaces = ".......................................................................................";
const char * indent = s_spaces + strlen (s_spaces);
indent -= (blockDepth * 2);
if (indent < s_spaces)
indent = s_spaces;
5 years ago
return indent;
}
const char * get_indention_string (IM3Compilation o)
{
return GetOpcodeIndentionString (o->block.depth+4);
}
void log_opcode (IM3Compilation o, m3opcode_t i_opcode)
{
i32 depth = o->block.depth;
if (i_opcode == c_waOp_end or i_opcode == c_waOp_else)
depth--;
5 years ago
m3log (compile, "%4d | 0x%02x %s %s", o->numOpcodes++, i_opcode, GetOpcodeIndentionString (depth), GetOpInfo(i_opcode)->name);
}
5 years ago
5 years ago
void emit_stack_dump (IM3Compilation o)
{
4 years ago
# if d_m3EnableOpTracing
5 years ago
if (o->numEmits)
{
EmitOp (o, op_DumpStack);
EmitConstant32 (o, o->numOpcodes);
4 years ago
EmitConstant32 (o, GetMaxUsedSlotPlusOne(o));
EmitPointer (o, o->function);
5 years ago
5 years ago
o->numEmits = 0;
}
4 years ago
# endif
5 years ago
}
5 years ago
5 years ago
void log_emit (IM3Compilation o, IM3Operation i_operation)
{
OpInfo i = find_operation_info (i_operation);
5 years ago
4 years ago
d_m3Log(emit, "");
5 years ago
if (i.info)
{
4 years ago
printf ("%p: %s\n", GetPC (o), i.info->name);
5 years ago
}
4 years ago
else printf ("not found: %p\n", i_operation);
5 years ago
}
#endif // DEBUG