Skip to content

Commit

Permalink
Print to stderr if DUMP environemnt variable is set
Browse files Browse the repository at this point in the history
  • Loading branch information
vlasakm committed Aug 3, 2023
1 parent c74d72e commit e867b7c
Show file tree
Hide file tree
Showing 14 changed files with 110 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/backend/inst.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ mfunction_finalize_stack(MFunction *mfunction)
if (mfunction->make_stack_space) {
IIMM(mfunction->make_stack_space) = mfunction->stack_space;
}

if (DUMP) {
fprintf(stderr, "Function %.*s after stack finalization:\n", (int) mfunction->func->name.len, mfunction->func->name.str);
print_mfunction(stderr, mfunction);
}
}

void
Expand Down
10 changes: 10 additions & 0 deletions src/backend/regalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,11 @@ rewrite_program(RegAllocState *ras)
for_each_def(inst, insert_stores_of_spilled, ss);
}
}

if (DUMP) {
fprintf(stderr, "Function %.*s after spill insertion:\n", (int) mfunction->func->name.len, mfunction->func->name.str);
print_mfunction(stderr, mfunction);
}
}

void
Expand All @@ -642,6 +647,11 @@ apply_reg_assignment(RegAllocState *ras)
}
}
}

if (DUMP) {
fprintf(stderr, "Function %.*s after register assignment:\n", (int) mfunction->func->name.len, mfunction->func->name.str);
print_mfunction(stderr, mfunction);
}
}

void
Expand Down
6 changes: 6 additions & 0 deletions src/backend/x86-64/lower.c
Original file line number Diff line number Diff line change
Expand Up @@ -623,5 +623,11 @@ translate_function(Arena *arena, GArena *labels, Function *function)

mfunction->vreg_cnt = ts->index;
function->mfunction = mfunction;

if (DUMP) {
fprintf(stderr, "Function %.*s after lowering:\n", (int) mfunction->func->name.len, mfunction->func->name.str);
print_mfunction(stderr, mfunction);
}

return mfunction;
}
10 changes: 10 additions & 0 deletions src/backend/x86-64/peephole.c
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,11 @@ peephole(MFunction *mfunction, Arena *arena, bool last_pass)
goto next;
}
}

if (DUMP) {
fprintf(stderr, "Function %.*s after peephole optimization:\n", (int) mfunction->func->name.len, mfunction->func->name.str);
print_mfunction(stderr, mfunction);
}
}

// Remove:
Expand All @@ -892,4 +897,9 @@ cleanup(MFunction *mfunction)
}
}
}

if (DUMP) {
fprintf(stderr, "Function %.*s after cleanup:\n", (int) mfunction->func->name.len, mfunction->func->name.str);
print_mfunction(stderr, mfunction);
}
}
9 changes: 9 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ parse_source(ErrorContext *ec, Arena *arena, Str source)
Module *module = parse(arena, &scratch, source, parser_verror, ec);
garena_free(&scratch);

if (DUMP) {
fprintf(stderr, "After parsing:\n");
print_module(stderr, module);
}

if (!module) {
arena_restore(arena, arena_start);
longjmp(ec->loc, 1);
Expand Down Expand Up @@ -283,6 +288,7 @@ module_free(Module *module)
garena_free(&module->labels);
}

int DUMP;

int
main(int argc, char **argv)
Expand All @@ -298,6 +304,9 @@ main(int argc, char **argv)
goto end;
}

char *dump_env_var = getenv("DUMP");
DUMP = dump_env_var && dump_env_var[0] != '\0';

Config c_ = {
.assemble = true,
.link = true,
Expand Down
34 changes: 34 additions & 0 deletions src/middleend/ir.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,12 @@ index_values(Function *function, size_t start_index)
}
}
function->value_cnt = index;

if (DUMP) {
fprintf(stderr, "Function %.*s after indexing of values:\n", (int) function->name.len, function->name.str);
print_function(stderr, function);
}

return index;
}

Expand Down Expand Up @@ -487,6 +493,34 @@ print_globals(FILE *f, Module *module)
}
}

void
print_module(FILE *f, Module *module)
{
print_globals(f, module);
fprintf(f, "\n\n");

// Print strings
for (size_t i = 0; i < module->string_cnt; i++) {
StringLiteral *string = module->strings[i];
fprintf(f, "$str%zu = \"", i);
print_str(f, string->str);
fprintf(f, "\"\n");
}
fprintf(f, "\n\n");

// Print functions
for (size_t i = 0; i < module->function_cnt; i++) {
Function *function = module->functions[i];
// Skip external functions
if (!function_is_fully_defined(function)) {
continue;
}

print_function(f, function);
fprintf(f, "\n\n");
}
}

Field *
get_member(Value *value)
{
Expand Down
3 changes: 3 additions & 0 deletions src/middleend/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,7 @@ typedef struct {

void print_globals(FILE *f, Module *module);

void print_module(FILE *f, Module *module);


Field *get_member(Value *value);
5 changes: 5 additions & 0 deletions src/middleend/passes/deconstruct_ssa.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ deconstruct_ssa(Arena *arena, Function *function)
change_to_identity(phi, dummy);
}
}

if (DUMP) {
fprintf(stderr, "Function %.*s after SSA deconstruction:\n", (int) function->name.len, function->name.str);
print_function(stderr, function);
}
}
5 changes: 5 additions & 0 deletions src/middleend/passes/merge_blocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,9 @@ merge_blocks(Arena *arena, Function *function)

// Recompute function->post_order, since we invalidated it.
compute_postorder(function);

if (DUMP) {
fprintf(stderr, "Function %.*s after merging blocks:\n", (int) function->name.len, function->name.str);
print_function(stderr, function);
}
}
5 changes: 5 additions & 0 deletions src/middleend/passes/single_exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,9 @@ single_exit(Arena *arena, Function *function)

// Recompute function->post_order, since we invalidated it.
compute_postorder(function);

if (DUMP) {
fprintf(stderr, "Function %.*s after single exit:\n", (int) function->name.len, function->name.str);
print_function(stderr, function);
}
}
5 changes: 5 additions & 0 deletions src/middleend/passes/split_critical_edges.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ split_critical_edges(Arena *arena, Function *function)

// Recompute function->post_order, since we invalidated it.
compute_postorder(function);

if (DUMP) {
fprintf(stderr, "Function %.*s after splitting of critical edges:\n", (int) function->name.len, function->name.str);
print_function(stderr, function);
}
}
5 changes: 5 additions & 0 deletions src/middleend/passes/thread_jumps.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,9 @@ thread_jumps(Arena *arena, Function *function)

// Recompute function->post_order, since we invalidated it.
compute_postorder(function);

if (DUMP) {
fprintf(stderr, "Function %.*s after jump threading:\n", (int) function->name.len, function->name.str);
print_function(stderr, function);
}
}
5 changes: 5 additions & 0 deletions src/middleend/passes/value_numbering.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,4 +346,9 @@ value_numbering(Arena *arena, Function *function)
do_value_numbering(arena, function);
free_uses(function);
validate_function(function);

if (DUMP) {
fprintf(stderr, "Function %.*s after value numbering:\n", (int) function->name.len, function->name.str);
print_function(stderr, function);
}
}
3 changes: 3 additions & 0 deletions src/utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,6 @@ _Noreturn void unreachable(char *file, size_t line);

#define NOT_IMPLEMENTED(what) not_implemented((what), __FILE__, __LINE__)
_Noreturn void not_implemented(char *what, char *file, size_t line);


extern int DUMP;

0 comments on commit e867b7c

Please sign in to comment.