ctrl flow analysis -> dasm layer

This commit is contained in:
Ryan Fleury
2024-08-29 10:58:16 -07:00
parent 5907783a2e
commit 9bba4f224c
4 changed files with 93 additions and 101 deletions
+30
View File
@@ -134,6 +134,36 @@ dasm_inst_from_code(Arena *arena, Architecture arch, U64 vaddr, String8 code, DA
return inst;
}
////////////////////////////////
//~ rjf: Control Flow Analysis
internal DASM_CtrlFlowInfo
dasm_ctrl_flow_info_from_arch_vaddr_code(Arena *arena, DASM_InstFlags exit_points_mask, Architecture arch, U64 vaddr, String8 code)
{
Temp scratch = scratch_begin(&arena, 1);
DASM_CtrlFlowInfo info = {0};
for(U64 offset = 0; offset < code.size;)
{
DASM_Inst inst = dasm_inst_from_code(scratch.arena, arch, vaddr+offset, str8_skip(code, offset), DASM_Syntax_Intel);
U64 inst_vaddr = vaddr+offset;
offset += inst.size;
info.total_size += inst.size;
if(inst.flags & exit_points_mask)
{
DASM_CtrlFlowPoint point = {0};
point.inst_flags = inst.flags;
point.vaddr = inst_vaddr;
point.jump_dest_vaddr = inst.jump_dest_vaddr;
DASM_CtrlFlowPointNode *node = push_array(arena, DASM_CtrlFlowPointNode, 1);
node->v = point;
SLLQueuePush(info.exit_points.first, info.exit_points.last, node);
info.exit_points.count += 1;
}
}
scratch_end(scratch);
return info;
}
////////////////////////////////
//~ rjf: Parameter Type Functions
+38
View File
@@ -40,6 +40,39 @@ struct DASM_Inst
U64 jump_dest_vaddr;
};
////////////////////////////////
//~ rjf: Control Flow Analysis Types
typedef struct DASM_CtrlFlowPoint DASM_CtrlFlowPoint;
struct DASM_CtrlFlowPoint
{
U64 vaddr;
U64 jump_dest_vaddr;
DASM_InstFlags inst_flags;
};
typedef struct DASM_CtrlFlowPointNode DASM_CtrlFlowPointNode;
struct DASM_CtrlFlowPointNode
{
DASM_CtrlFlowPointNode *next;
DASM_CtrlFlowPoint v;
};
typedef struct DASM_CtrlFlowPointList DASM_CtrlFlowPointList;
struct DASM_CtrlFlowPointList
{
DASM_CtrlFlowPointNode *first;
DASM_CtrlFlowPointNode *last;
U64 count;
};
typedef struct DASM_CtrlFlowInfo DASM_CtrlFlowInfo;
struct DASM_CtrlFlowInfo
{
DASM_CtrlFlowPointList exit_points;
U64 total_size;
};
////////////////////////////////
//~ rjf: Disassembly Text Decoration Types
@@ -249,6 +282,11 @@ global DASM_Shared *dasm_shared = 0;
internal DASM_Inst dasm_inst_from_code(Arena *arena, Architecture arch, U64 vaddr, String8 code, DASM_Syntax syntax);
////////////////////////////////
//~ rjf: Control Flow Analysis
internal DASM_CtrlFlowInfo dasm_ctrl_flow_info_from_arch_vaddr_code(Arena *arena, DASM_InstFlags exit_points_mask, Architecture arch, U64 vaddr, String8 code);
////////////////////////////////
//~ rjf: Parameter Type Functions