Skip to content

Commit

Permalink
move generated node ctors to header-only
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugobros3 committed Oct 6, 2024
1 parent 8953b6c commit 1ed4a7e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 59 deletions.
49 changes: 48 additions & 1 deletion src/shady/api/generator_grammar.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,50 @@ static void generate_header_getters_for_class(Growy* g, json_object* src, json_o
}
}

void generate_node_ctor(Growy* g, json_object* nodes) {
for (size_t i = 0; i < json_object_array_length(nodes); i++) {
json_object* node = json_object_array_get_idx(nodes, i);

String name = json_object_get_string(json_object_object_get(node, "name"));
assert(name);

if (has_custom_ctor(node))
continue;

if (i > 0)
shd_growy_append_formatted(g, "\n");

String snake_name = json_object_get_string(json_object_object_get(node, "snake_name"));
const void* alloc = NULL;
if (!snake_name) {
alloc = snake_name = to_snake_case(name);
}

json_object* ops = json_object_object_get(node, "ops");
if (ops)
shd_growy_append_formatted(g, "static inline const Node* %s(IrArena* arena, %s payload)", snake_name, name);
else
shd_growy_append_formatted(g, "static inline const Node* %s(IrArena* arena)", snake_name);

shd_growy_append_formatted(g, " {\n");
shd_growy_append_formatted(g, "\tNode node;\n");
shd_growy_append_formatted(g, "\tmemset((void*) &node, 0, sizeof(Node));\n");
shd_growy_append_formatted(g, "\tnode = (Node) {\n");
shd_growy_append_formatted(g, "\t\t.arena = arena,\n");
shd_growy_append_formatted(g, "\t\t.tag = %s_TAG,\n", name);
if (ops)
shd_growy_append_formatted(g, "\t\t.payload.%s = payload,\n", snake_name);
shd_growy_append_formatted(g, "\t\t.type = NULL,\n");
shd_growy_append_formatted(g, "\t};\n");
shd_growy_append_formatted(g, "\treturn _shd_create_node_helper(arena, node, NULL);\n");
shd_growy_append_formatted(g, "}\n");

if (alloc)
free((void*) alloc);
}
shd_growy_append_formatted(g, "\n");
}

void generate(Growy* g, json_object* src) {
generate_header(g, src);

Expand All @@ -145,7 +189,10 @@ void generate(Growy* g, json_object* src) {
shd_growy_append_formatted(g, "NodeClass get_node_class_from_tag(NodeTag tag);\n\n");
generate_node_payloads(g, src, nodes);
generate_node_type(g, nodes);
generate_node_ctor(g, nodes, false);

shd_growy_append_formatted(g, "#include <string.h>\n");
shd_growy_append_formatted(g, "Node* _shd_create_node_helper(IrArena* arena, Node node, bool* pfresh);\n");
generate_node_ctor(g, nodes);

for (size_t i = 0; i < json_object_array_length(node_classes); i++) {
json_object* node_class = json_object_array_get_idx(node_classes, i);
Expand Down
14 changes: 7 additions & 7 deletions src/shady/constructors.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const Node* fold_node_operand(NodeTag tag, NodeClass, String, const Node* op);

const Type* check_type_generated(IrArena* a, const Node* node);

static Node* create_node_helper(IrArena* arena, Node node, bool* pfresh) {
Node* _shd_create_node_helper(IrArena* arena, Node node, bool* pfresh) {
pre_construction_validation(arena, &node);
if (arena->config.check_types)
node.type = check_type_generated(arena, &node);
Expand Down Expand Up @@ -74,7 +74,7 @@ Node* param(IrArena* arena, const Type* type, const char* name) {
.tag = Param_TAG,
.payload.param = param
};
return create_node_helper(arena, node, NULL);
return _shd_create_node_helper(arena, node, NULL);
}

const Node* composite_helper(IrArena* a, const Type* t, Nodes contents) {
Expand Down Expand Up @@ -123,7 +123,7 @@ Node* function(Module* mod, Nodes params, const char* name, Nodes annotations, N
.tag = Function_TAG,
.payload.fun = payload
};
Node* fn = create_node_helper(arena, node, NULL);
Node* fn = _shd_create_node_helper(arena, node, NULL);
register_decl_module(mod, fn);

for (size_t i = 0; i < params.count; i++) {
Expand Down Expand Up @@ -152,7 +152,7 @@ Node* basic_block(IrArena* arena, Nodes params, const char* name) {
.payload.basic_block = payload
};

Node* bb = create_node_helper(arena, node, NULL);
Node* bb = _shd_create_node_helper(arena, node, NULL);

for (size_t i = 0; i < params.count; i++) {
Node* param = (Node*) params.nodes[i];
Expand Down Expand Up @@ -180,7 +180,7 @@ Node* constant(Module* mod, Nodes annotations, const Type* hint, String name) {
.tag = Constant_TAG,
.payload.constant = cnst
};
Node* decl = create_node_helper(arena, node, NULL);
Node* decl = _shd_create_node_helper(arena, node, NULL);
register_decl_module(mod, decl);
return decl;
}
Expand Down Expand Up @@ -211,7 +211,7 @@ Node* global_var(Module* mod, Nodes annotations, const Type* type, const char* n
.tag = GlobalVariable_TAG,
.payload.global_variable = gvar
};
Node* decl = create_node_helper(arena, node, NULL);
Node* decl = _shd_create_node_helper(arena, node, NULL);
register_decl_module(mod, decl);
return decl;
}
Expand All @@ -233,7 +233,7 @@ Type* nominal_type(Module* mod, Nodes annotations, String name) {
.tag = NominalType_TAG,
.payload.nom_type = payload
};
Node* decl = create_node_helper(arena, node, NULL);
Node* decl = _shd_create_node_helper(arena, node, NULL);
register_decl_module(mod, decl);
return decl;
}
Expand Down
1 change: 0 additions & 1 deletion src/shady/generator/generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ String capitalize(String str);
bool starts_with_vowel(String str);

bool has_custom_ctor(json_object* node);
void generate_node_ctor(Growy* g, json_object* nodes, bool definition);

json_object* lookup_node_class(json_object* src, String name);
String class_to_type(json_object* src, String class, bool list);
Expand Down
49 changes: 0 additions & 49 deletions src/shady/generator/generator_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,6 @@ bool has_custom_ctor(json_object* node) {
return (constructor && strcmp(constructor, "custom") == 0);
}

void generate_node_ctor(Growy* g, json_object* nodes, bool definition) {
for (size_t i = 0; i < json_object_array_length(nodes); i++) {
json_object* node = json_object_array_get_idx(nodes, i);

String name = json_object_get_string(json_object_object_get(node, "name"));
assert(name);

if (has_custom_ctor(node))
continue;

if (definition && i > 0)
shd_growy_append_formatted(g, "\n");

String snake_name = json_object_get_string(json_object_object_get(node, "snake_name"));
const void* alloc = NULL;
if (!snake_name) {
alloc = snake_name = to_snake_case(name);
}

String ap = definition ? " arena" : "";
json_object* ops = json_object_object_get(node, "ops");
if (ops)
shd_growy_append_formatted(g, "const Node* %s(IrArena*%s, %s%s)", snake_name, ap, name, definition ? " payload" : "");
else
shd_growy_append_formatted(g, "const Node* %s(IrArena*%s)", snake_name, ap);

if (definition) {
shd_growy_append_formatted(g, " {\n");
shd_growy_append_formatted(g, "\tNode node;\n");
shd_growy_append_formatted(g, "\tmemset((void*) &node, 0, sizeof(Node));\n");
shd_growy_append_formatted(g, "\tnode = (Node) {\n");
shd_growy_append_formatted(g, "\t\t.arena = arena,\n");
shd_growy_append_formatted(g, "\t\t.tag = %s_TAG,\n", name);
if (ops)
shd_growy_append_formatted(g, "\t\t.payload.%s = payload,\n", snake_name);
shd_growy_append_formatted(g, "\t\t.type = NULL,\n");
shd_growy_append_formatted(g, "\t};\n");
shd_growy_append_formatted(g, "\treturn create_node_helper(arena, node, NULL);\n");
shd_growy_append_formatted(g, "}\n");
} else {
shd_growy_append_formatted(g, ";\n");
}

if (alloc)
free((void*) alloc);
}
shd_growy_append_formatted(g, "\n");
}

json_object* lookup_node_class(json_object* src, String name) {
json_object* node_classes = json_object_object_get(src, "node-classes");
for (size_t i = 0; i < json_object_array_length(node_classes); i++) {
Expand Down
1 change: 0 additions & 1 deletion src/shady/generator_constructors.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,5 @@ void generate(Growy* g, json_object* src) {
generate_header(g, src);

json_object* nodes = json_object_object_get(src, "nodes");
generate_node_ctor(g, nodes, true);
generate_pre_construction_validation(g, src);
}

0 comments on commit 1ed4a7e

Please sign in to comment.