Skip to content

Commit a66815a

Browse files
committed
use actual instructions when dumping the CFG
1 parent 9966658 commit a66815a

File tree

5 files changed

+89
-24
lines changed

5 files changed

+89
-24
lines changed

src/common/util.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,25 @@ size_t apply_escape_codes(const char* src, size_t size, char* dst) {
3838
return j;
3939
}
4040

41+
size_t unapply_escape_codes(const char* src, size_t size, char* dst) {
42+
char c = '\0';
43+
size_t j = 0;
44+
for (size_t i = 0; i < size; i++) {
45+
c = src[i];
46+
47+
#define ESCAPE_CASE(m, s) if (c == s) { \
48+
dst[j++] = '\\'; \
49+
dst[j++] = m; \
50+
continue; \
51+
} \
52+
53+
ESCAPE_SEQS(ESCAPE_CASE)
54+
55+
dst[j++] = c;
56+
}
57+
return j;
58+
}
59+
4160
static long get_file_size(FILE* f) {
4261
if (fseek(f, 0, SEEK_END) != 0)
4362
return -1;

src/common/util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <stdbool.h>
66

77
size_t apply_escape_codes(const char* src, size_t og_len, char* dst);
8+
size_t unapply_escape_codes(const char* src, size_t og_len, char* dst);
89

910
bool read_file(const char* filename, size_t* size, char** output);
1011
bool write_file(const char* filename, size_t size, const char* data);

src/shady/analysis/scope_dump.c

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
#include "scope.h"
2-
#include "../ir_private.h"
2+
3+
#include "shady/ir_private.h"
4+
#include "shady/print.h"
35

46
#include "list.h"
57
#include "dict.h"
68
#include "util.h"
9+
#include "printer.h"
710

11+
#include <string.h>
812
#include <assert.h>
913

1014
static int extra_uniqueness = 0;
@@ -21,6 +25,26 @@ static CFNode* get_let_pred(const CFNode* n) {
2125
return NULL;
2226
}
2327

28+
static void print_node_helper(Printer* p, const Node* n) {
29+
Growy* tmp_g = new_growy();
30+
Printer* tmp_p = open_growy_as_printer(tmp_g);
31+
32+
PrintConfig config = {
33+
.color = false,
34+
.in_cfg = true,
35+
};
36+
37+
print_node(tmp_p, n, config);
38+
39+
String label = printer_growy_unwrap(tmp_p);
40+
char* escaped_label = calloc(strlen(label) * 2, 1);
41+
unapply_escape_codes(label, strlen(label), escaped_label);
42+
43+
print(p, "%s", escaped_label);
44+
free(escaped_label);
45+
free((void*)label);
46+
}
47+
2448
static void dump_cf_node(FILE* output, const CFNode* n) {
2549
const Node* bb = n->node;
2650
const Node* body = get_abstraction_body(bb);
@@ -35,37 +59,38 @@ static void dump_cf_node(FILE* output, const CFNode* n) {
3559
else if (is_basic_block(bb))
3660
color = "blue";
3761

38-
String label = "";
62+
Growy* g = new_growy();
63+
Printer* p = open_growy_as_printer(g);
3964

4065
String abs_name = get_abstraction_name_unsafe(bb);
4166
if (!abs_name)
4267
abs_name = format_string_interned(bb->arena, "%%%d", bb->id);
43-
label = format_string_arena(bb->arena->arena, "%s: %s", abs_name, label);
68+
69+
print(p, "%s: \n%s: ", abs_name, abs_name);
4470

4571
const CFNode* let_chain_end = n;
4672
while (body->tag == Let_TAG) {
47-
const Node* instr = body->payload.let.instruction;
48-
// label = "";
49-
if (instr->tag == PrimOp_TAG)
50-
label = format_string_arena(bb->arena->arena, "%slet ... = %s (...)\n", label, get_primop_name(instr->payload.prim_op.op));
51-
else
52-
label = format_string_arena(bb->arena->arena, "%slet ... = %s (...)\n", label, node_tags[instr->tag]);
53-
54-
const Node* abs = body->payload.let.tail;
55-
label = format_string_arena(bb->arena->arena, "%s%%%d: ", label, abs->id);
56-
5773
if (entries_count_list(let_chain_end->succ_edges) != 1 || read_list(CFEdge, let_chain_end->succ_edges)[0].type != LetTailEdge)
5874
break;
5975

76+
print_node_helper(p, body);
77+
print(p, "\\l");
78+
79+
const Node* abs = body->payload.let.tail;
80+
print(p, "%%%d: ", abs->id);
81+
6082
let_chain_end = read_list(CFEdge, let_chain_end->succ_edges)[0].dst;
6183
assert(let_chain_end->node == abs);
6284
assert(is_case(abs));
6385
body = get_abstraction_body(abs);
6486
}
6587

66-
label = format_string_arena(bb->arena->arena, "%s%s", label, node_tags[body->tag]);
88+
print_node_helper(p, body);
89+
print(p, "\\l");
6790

68-
fprintf(output, "bb_%zu [label=\"%s\", color=\"%s\", shape=box];\n", (size_t) n, label, color);
91+
String label = printer_growy_unwrap(p);
92+
fprintf(output, "bb_%zu [nojustify=true, label=\"%s\", color=\"%s\", shape=box];\n", (size_t) n, label, color);
93+
free((void*) label);
6994

7095
for (size_t i = 0; i < entries_count_list(n->dominates); i++) {
7196
CFNode* d = read_list(CFNode*, n->dominates)[i];

src/shady/print.c

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,8 @@ static void print_instruction(PrinterCtx* ctx, const Node* node) {
567567
printf("(");
568568
print_node(node->payload.if_instr.condition);
569569
printf(") ");
570+
if (ctx->config.in_cfg)
571+
break;
570572
print_case_body(ctx, node->payload.if_instr.if_true);
571573
if (node->payload.if_instr.if_false) {
572574
printf(GREEN);
@@ -580,6 +582,8 @@ static void print_instruction(PrinterCtx* ctx, const Node* node) {
580582
printf("loop");
581583
printf(RESET);
582584
print_yield_types(ctx, node->payload.loop_instr.yield_types);
585+
if (ctx->config.in_cfg)
586+
break;
583587
const Node* body = node->payload.loop_instr.body;
584588
assert(is_case(body));
585589
print_param_list(ctx, body->payload.case_.params, &node->payload.loop_instr.initial_args);
@@ -593,6 +597,8 @@ static void print_instruction(PrinterCtx* ctx, const Node* node) {
593597
printf("(");
594598
print_node(node->payload.match_instr.inspect);
595599
printf(")");
600+
if (ctx->config.in_cfg)
601+
break;
596602
printf(" {");
597603
indent(ctx->printer);
598604
for (size_t i = 0; i < node->payload.match_instr.literals.count; i++) {
@@ -625,6 +631,8 @@ static void print_instruction(PrinterCtx* ctx, const Node* node) {
625631
printf("control");
626632
printf(RESET);
627633
print_yield_types(ctx, node->payload.control.yield_types);
634+
if (ctx->config.in_cfg)
635+
break;
628636
print_param_list(ctx, node->payload.control.inside->payload.case_.params, NULL);
629637
print_case_body(ctx, node->payload.control.inside);
630638
break;
@@ -681,8 +689,10 @@ static void print_terminator(PrinterCtx* ctx, const Node* node) {
681689
printf(" = ");
682690
}
683691
print_node(instruction);
684-
printf(";\n");
685-
print_abs_body(ctx, tail);
692+
if (!ctx->config.in_cfg) {
693+
printf(";\n");
694+
print_abs_body(ctx, tail);
695+
}
686696
} else {
687697
printf(GREEN);
688698
printf("let");
@@ -973,36 +983,45 @@ void print_node(Printer* printer, const Node* node, PrintConfig config) {
973983
};
974984
print_node_impl(&ctx, node);
975985
flush(ctx.printer);
976-
destroy_printer(ctx.printer);
977986
}
978987

979988
void print_node_into_str(const Node* node, char** str_ptr, size_t* size) {
980989
Growy* g = new_growy();
981-
print_node(open_growy_as_printer(g), node, (PrintConfig) { .reparseable = true });
990+
Printer* p = open_growy_as_printer(g);
991+
print_node(p, node, (PrintConfig) { .reparseable = true });
992+
destroy_printer(p);
982993
*size = growy_size(g);
983994
*str_ptr = growy_deconstruct(g);
984995
}
985996

986997
void print_module_into_str(Module* mod, char** str_ptr, size_t* size) {
987998
Growy* g = new_growy();
988-
print_module(open_growy_as_printer(g), mod, (PrintConfig) { .reparseable = true, });
999+
Printer* p = open_growy_as_printer(g);
1000+
print_module(p, mod, (PrintConfig) { .reparseable = true, });
1001+
destroy_printer(p);
9891002
*size = growy_size(g);
9901003
*str_ptr = growy_deconstruct(g);
9911004
}
9921005

9931006
void dump_node(const Node* node) {
994-
print_node(open_file_as_printer(stdout), node, (PrintConfig) { .color = true });
1007+
Printer* p = open_file_as_printer(stdout);
1008+
print_node(p, node, (PrintConfig) { .color = true });
9951009
printf("\n");
9961010
}
9971011

9981012
void dump_module(Module* mod) {
999-
print_module(open_file_as_printer(stdout), mod, (PrintConfig) { .color = true });
1013+
Printer* p = open_file_as_printer(stdout);
1014+
print_module(p, mod, (PrintConfig) { .color = true });
1015+
destroy_printer(p);
10001016
printf("\n");
10011017
}
10021018

10031019
void log_node(LogLevel level, const Node* node) {
1004-
if (level >= get_log_level())
1005-
print_node(open_file_as_printer(stderr), node, (PrintConfig) { .color = true });
1020+
if (level >= get_log_level()) {
1021+
Printer* p = open_file_as_printer(stderr);
1022+
print_node(p, node, (PrintConfig) { .color = true });
1023+
destroy_printer(p);
1024+
}
10061025
}
10071026

10081027
void log_module(LogLevel level, CompilerConfig* compiler_cfg, Module* mod) {

src/shady/print.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ typedef struct {
1212
bool print_ptrs;
1313
bool color;
1414
bool reparseable;
15+
bool in_cfg;
1516
} PrintConfig;
1617

1718
void print_module(Printer* printer, Module* mod, PrintConfig config);

0 commit comments

Comments
 (0)