diff options
| author | Kimplul <kimi.h.kuparinen@gmail.com> | 2026-05-01 14:02:37 +0300 |
|---|---|---|
| committer | Kimplul <kimi.h.kuparinen@gmail.com> | 2026-05-01 14:33:59 +0300 |
| commit | 48cd04e1a57f076287fb6ecdd0a4236e191ff7d3 (patch) | |
| tree | 7d8a1bfadbcde983c5792e5ac8f9ae861023874b /src/lower.c | |
| parent | f887b6178de8d62f69607e75d3e929ed8b0dcf34 (diff) | |
| download | fwd-48cd04e1a57f076287fb6ecdd0a4236e191ff7d3.tar.gz fwd-48cd04e1a57f076287fb6ecdd0a4236e191ff7d3.zip | |
add write_codeline to make lowering a bit prettier
Diffstat (limited to 'src/lower.c')
| -rw-r--r-- | src/lower.c | 357 |
1 files changed, 196 insertions, 161 deletions
diff --git a/src/lower.c b/src/lower.c index 3b6193f..f6d6154 100644 --- a/src/lower.c +++ b/src/lower.c @@ -28,9 +28,10 @@ static inline void mangle(FILE *f, struct ast *id) { assert(id->k == AST_STRUCT_DEF - || id->k == AST_PROC_DEF - || id->k == AST_VAR_DEF - || id->k == AST_ID); + || id->k == AST_PROC_DEF + || id->k == AST_VAR_DEF + || id->k == AST_ID + ); assert(id->s && id->scope); fprintf(f, "%s_s%zu", id->s, id->scope->number); @@ -97,16 +98,16 @@ struct state { static struct state create_state(struct state *parent) { return (struct state){ - .uniq = parent->uniq, - .prefix = parent->prefix, - .indent = parent->indent, - .current = parent->current, - .ctx = parent->ctx, - .code = parent->code, - .decls = parent->decls, - .defns = parent->defns, - .types = parent->types, - .procs = parent->procs, + .uniq = parent->uniq, + .prefix = parent->prefix, + .indent = parent->indent, + .current = parent->current, + .ctx = parent->ctx, + .code = parent->code, + .decls = parent->decls, + .defns = parent->defns, + .types = parent->types, + .procs = parent->procs, }; } @@ -127,12 +128,23 @@ static void decrease_indent(struct state *state) state->indent--; } -static void indent(struct state *state) +static void indent_codeline(struct state *state) { if (state->indent != 0) fprintf(state->code, "%*c", (int)(2 * state->indent), ' '); } +static void write_codeline(struct state *state, const char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + + indent_codeline(state); + vfprintf(state->code, fmt, args); + + va_end(args); +} + static bool proc_lowered(struct state *state, struct ast *proc) { assert(proc->k == AST_PROC_DEF); @@ -167,15 +179,18 @@ static void add_type(struct state *state, char *type) string_vec_append(state->types, type); } -static int lower_stmt_list(struct state *state, struct ast *stmt_list, bool last); +static int lower_stmt_list(struct state *state, struct ast *stmt_list, + bool last); static int lower_block(struct state *state, struct ast *block, bool last); static int lower_params(struct state *state, struct ast *params); static int lower_expr(struct state *state, struct ast *expr); static int lower_proc(struct state *state, struct ast *proc); static int lower_type_def(struct state *state, struct ast *type); -static char *lower_type_str(struct state *state, struct scope *scope, struct type *type); +static char *lower_type_str(struct state *state, struct scope *scope, + struct type *type); -static int _type_str(FILE *f, struct state *state, struct scope *scope, struct type *type) +static int _type_str(FILE *f, struct state *state, struct scope *scope, + struct type *type) { assert(type); switch (type->k) { @@ -223,8 +238,8 @@ static int _type_str(FILE *f, struct state *state, struct scope *scope, struct t struct ast *def = file_scope_find_type(scope, type_str(type)); if (!ast_flags(def, AST_FLAG_LOWERED)) - if (lower_type_def(state, def)) - return -1; + if (lower_type_def(state, def)) + return -1; char *name = mangle2(def); fprintf(f, "%s", name); @@ -257,7 +272,9 @@ static int _type_str(FILE *f, struct state *state, struct scope *scope, struct t } default: - internal_error("unhandled type lowering for %s", type_str(type)); + internal_error("unhandled type lowering for %s", + type_str(type) + ); abort(); break; } @@ -265,7 +282,8 @@ static int _type_str(FILE *f, struct state *state, struct scope *scope, struct t return 0; } -static char *lower_type_str(struct state *state, struct scope *scope, struct type *type) +static char *lower_type_str(struct state *state, struct scope *scope, + struct type *type) { assert(type); @@ -285,7 +303,8 @@ static char *lower_type_str(struct state *state, struct scope *scope, struct typ return type_buf; } -static int lower_closure_call(struct state *state, struct ast *call, struct ast *def, bool last) +static int lower_closure_call(struct state *state, struct ast *call, + struct ast *def, bool last) { char *q = buildstr("%s_call%zu", state->prefix, uniq(state)); char *args = mangle2(def); @@ -294,11 +313,10 @@ static int lower_closure_call(struct state *state, struct ast *call, struct ast FILE *ctx = open_memstream(&ctx_buf, &ctx_size); fprintf(ctx, "struct %s {\n fwd_start_t start;\n", q); - indent(state); - fprintf(state->code, "fwd_call_t %s = ctx->%s.call;\n", q, args); - - indent(state); - fprintf(state->code, "struct %s *%s_ctx = ctx->%s.args;\n", q, q, args); + write_codeline(state, "fwd_call_t %s = ctx->%s.call;\n", q, args); + write_codeline(state, "struct %s *%s_ctx = ctx->%s.args;\n", q, q, + args + ); int ret = 0; size_t idx = 0; @@ -310,8 +328,8 @@ static int lower_closure_call(struct state *state, struct ast *call, struct ast fprintf(ctx, " %s a%zu;\n", type, idx); free(type); - indent(state); - fprintf(state->code, "%s_ctx->a%zu = ", q, idx); + write_codeline(state, "%s_ctx->a%zu = ", q, idx); + int ret = lower_expr(state, a); fprintf(state->code, ";\n"); if (ret) @@ -322,18 +340,19 @@ static int lower_closure_call(struct state *state, struct ast *call, struct ast out: if (!returning && last && ast_flags(state->current, AST_REQ_FRAME)) { - indent(state); - fprintf(state->code, "fwd_stack_free(&stack, ctx);\n"); + write_codeline(state, "fwd_stack_free(&stack, ctx);\n"); } - indent(state); if (last) { - fprintf(state->code, "FWD_MUSTTAIL return %s(stack, %s_ctx);\n", - q, q); + write_codeline(state, + "FWD_MUSTTAIL return %s(stack, %s_ctx);\n", + q, q + ); } else { - fprintf(state->code, "stack = %s(stack, %s_ctx);\n", - q, q); + write_codeline(state, "stack = %s(stack, %s_ctx);\n", + q, q + ); } fprintf(ctx, "};\n\n"); @@ -346,10 +365,15 @@ out: return ret; } -static int lower_closure(struct state *state, struct ast *closure, char **name_out, char **args_out) +static int lower_closure(struct state *state, struct ast *closure, + char **name_out, char **args_out) { char *name = buildstr("%s_closure%zu", state->prefix, uniq(state)); - char *proto = buildstr("static fwd_stack_t %s(fwd_stack_t stack, fwd_args_t args)", name); + char *proto = buildstr( + "static fwd_stack_t %s(fwd_stack_t stack, fwd_args_t args)", + name + ); + char *decl = buildstr("%s;\n", proto); char *start_of = buildstr("%s_start", name); add_decl(state, decl); @@ -362,19 +386,21 @@ static int lower_closure(struct state *state, struct ast *closure, char **name_o new_state.indent = 0; assert(new_state.code); - fprintf(new_state.code, "%s\n{\n", proto); + write_codeline(&new_state, "%s\n{\n", proto); /** @todo unsure if this has the same address as the first element in - * the frame or the last in the previous, I guess we shall soon see */ + * the frame or the last in the previous, I guess we shall soon see */ fprintf(new_state.ctx, " fwd_start_t %s;\n", start_of); int ret = lower_params(&new_state, closure_bindings(closure)); assert(ret == 0); increase_indent(&new_state); - indent(&new_state); - fprintf(new_state.code, "struct %s_ctx *ctx = FWD_CONTAINER_OF(args, struct %s_ctx, %s);\n", - state->prefix, state->prefix, start_of); + write_codeline(&new_state, + "struct %s_ctx *ctx = FWD_CONTAINER_OF" + "(args, struct %s_ctx, %s);\n", + state->prefix, state->prefix, start_of + ); struct ast *block = closure_body(closure); ret = lower_stmt_list(&new_state, block_body(block), true); @@ -424,7 +450,9 @@ static int lower_comparison(struct state *state, struct ast *expr) break; default: - internal_error("unhandled lowering for comparison %s", ast_str(expr->k)); + internal_error("unhandled lowering for comparison %s", + ast_str(expr->k) + ); abort(); } @@ -471,14 +499,18 @@ static int lower_expr(struct state *state, struct ast *expr) if (lower_closure(state, expr, &name, &args)) return -1; - fprintf(state->code, "(fwd_closure_t){%s, &ctx->%s}", name, args); + fprintf(state->code, "(fwd_closure_t){%s, &ctx->%s}", name, + args + ); free(name); free(args); break; } case AST_ID: { - struct ast *def = file_scope_find_symbol(expr->scope, id_str(expr)); + struct ast *def = file_scope_find_symbol(expr->scope, + id_str(expr) + ); char *name = mangle2(def); fprintf(state->code, "ctx->%s", name); free(name); @@ -525,7 +557,9 @@ static int lower_expr(struct state *state, struct ast *expr) } case AST_SIZEOF: { - char *type = lower_type_str(state, expr->scope, sizeof_type(expr)); + char *type = lower_type_str(state, expr->scope, + sizeof_type(expr) + ); fprintf(state->code, "sizeof(%s)", type); free(type); break; @@ -584,8 +618,7 @@ static int lower_call(struct state *state, struct ast *call, bool last) FILE *ctx = open_memstream(&ctx_buf, &ctx_size); fprintf(ctx, "struct %s {\n fwd_start_t start;\n", q); - indent(state); - fprintf(state->code, "struct %s *%s = ctx->global_args;\n", q, q); + write_codeline(state, "struct %s *%s = ctx->global_args;\n", q, q); int ret = 0; size_t idx = 0; @@ -597,8 +630,7 @@ static int lower_call(struct state *state, struct ast *call, bool last) fprintf(ctx, " %s a%zu;\n", type, idx); free(type); - indent(&ctx_state); - fprintf(ctx_state.code, "%s->a%zu = ", q, idx); + write_codeline(&ctx_state, "%s->a%zu = ", q, idx); int ret = lower_expr(&ctx_state, a); fprintf(ctx_state.code, ";\n"); @@ -612,19 +644,20 @@ out: if (!returning && last && ast_flags(state->current, AST_REQ_FRAME)) { /** @todo unsure if this applies to all cases but seems to work * for the simple examples I've tried so far */ - indent(&ctx_state); - fprintf(ctx_state.code, "fwd_stack_free(&stack, ctx);\n"); + write_codeline(&ctx_state, "fwd_stack_free(&stack, ctx);\n"); } char *target = mangle2(def); - indent(&ctx_state); if (last) { - fprintf(ctx_state.code, "FWD_MUSTTAIL return %s(stack, %s);\n", - target, q); + write_codeline(&ctx_state, + "FWD_MUSTTAIL return %s(stack, %s);\n", + target, q + ); } else { - fprintf(ctx_state.code, "stack = %s(stack, %s);\n", - target, q); + write_codeline(&ctx_state, "stack = %s(stack, %s);\n", + target, q + ); } fprintf(ctx, "};\n\n"); @@ -639,8 +672,7 @@ out: static int lower_if(struct state *state, struct ast *stmt, bool last) { - indent(state); - fprintf(state->code, "if ("); + write_codeline(state, "if ("); if (lower_expr(state, if_cond(stmt))) return -1; @@ -651,8 +683,7 @@ static int lower_if(struct state *state, struct ast *stmt, bool last) if (!if_else(stmt)) return 0; - indent(state); - fprintf(state->code, "else\n"); + write_codeline(state, "else\n"); return lower_block(state, if_else(stmt), last); } @@ -664,20 +695,17 @@ static int lower_nil_check(struct state *state, struct ast *stmt, bool last) fprintf(state->ctx, " %s %s;\n", type, name); - indent(state); - fprintf(state->code, "ctx->%s = ", name); + write_codeline(state, "ctx->%s = ", name); if (lower_expr(state, nil_check_expr(stmt))) return -1; fprintf(state->code, ";\n"); - indent(state); - fprintf(state->code, "if (!ctx->%s)", name); + write_codeline(state, "if (!ctx->%s)", name); if (lower_block(state, nil_check_body(stmt), last)) return -1; - indent(state); - fprintf(state->code, "else\n"); + write_codeline(state, "else\n"); free(type); free(name); @@ -695,8 +723,7 @@ static int lower_explode(struct state *state, struct ast *stmt, bool last) char *type = lower_type_str(state, expr->scope, expr->t); - indent(state); - fprintf(state->code, "%s explode_%d = ", type, u); + write_codeline(state, "%s explode_%d = ", type, u); free(type); if (lower_expr(state, expr)) @@ -710,9 +737,9 @@ static int lower_explode(struct state *state, struct ast *stmt, bool last) char *type = lower_type_str(state, var->scope, var->t); char *name = mangle2(var); - indent(state); - fprintf(state->code, "ctx->%s = explode_%d.%s;\n", - name, u, deconstruction_id(node)); + write_codeline(state, "ctx->%s = explode_%d.%s;\n", + name, u, deconstruction_id(node) + ); fprintf(state->ctx, " %s %s;\n", type, name); @@ -731,8 +758,7 @@ static int lower_let(struct state *state, struct ast *stmt, bool last) fprintf(state->ctx, " %s %s;\n", type, name); - indent(state); - fprintf(state->code, "ctx->%s = ", name); + write_codeline(state, "ctx->%s = ", name); free(type); free(name); @@ -743,8 +769,7 @@ static int lower_let(struct state *state, struct ast *stmt, bool last) fprintf(state->code, ";\n"); if (last) { - indent(state); - fprintf(state->code, "return stack;\n"); + write_codeline(state, "return stack;\n"); } return 0; @@ -752,7 +777,7 @@ static int lower_let(struct state *state, struct ast *stmt, bool last) static int lower_write(struct state *state, struct ast *stmt) { - indent(state); + indent_codeline(state); if (lower_expr(state, write_dst(stmt))) return -1; @@ -767,19 +792,18 @@ static int lower_write(struct state *state, struct ast *stmt) static int lower_stmt(struct state *state, struct ast *stmt, bool last) { switch (stmt->k) { - case AST_CALL: return lower_call(state, stmt, last); case AST_IF: return lower_if(state, stmt, last); - case AST_NIL_CHECK: return lower_nil_check(state, stmt, last); - case AST_EXPLODE: return lower_explode(state, stmt, last); case AST_LET: return lower_let(state, stmt, last); + case AST_CALL: return lower_call(state, stmt, last); case AST_WRITE: return lower_write(state, stmt); + case AST_EXPLODE: return lower_explode(state, stmt, last); + case AST_NIL_CHECK: return lower_nil_check(state, stmt, last); case AST_FORGET: case AST_EMPTY: - if (last) { - indent(state); - fprintf(state->code, "return stack;\n"); - } - break; + if (last) + write_codeline(state, "return stack;\n"); + + break; default: internal_error("unhandled statement kind %s", ast_str(stmt->k)); abort(); @@ -788,7 +812,8 @@ static int lower_stmt(struct state *state, struct ast *stmt, bool last) return 0; } -static int lower_stmt_list(struct state *state, struct ast *stmt_list, bool last) +static int lower_stmt_list(struct state *state, struct ast *stmt_list, + bool last) { foreach_node(stmt, stmt_list) { bool req_ret = last && !stmt->n; @@ -801,15 +826,13 @@ static int lower_stmt_list(struct state *state, struct ast *stmt_list, bool last static int lower_block(struct state *state, struct ast *block, bool last) { - indent(state); - fprintf(state->code, "{\n"); + write_codeline(state, "{\n"); increase_indent(state); int ret = lower_stmt_list(state, block_body(block), last); decrease_indent(state); - indent(state); - fprintf(state->code, "}\n"); + write_codeline(state, "}\n"); return ret; } @@ -850,7 +873,7 @@ static const char *fwd_typeparam(struct type *t) case TYPE_NIL: return "p"; case TYPE_FUNC_PTR: return "p"; default: - abort(); + abort(); } return NULL; @@ -871,13 +894,14 @@ static const char *fwd_ctypestr(struct type *t) case TYPE_NIL: return "FWD_PTR"; /* void ptr */ case TYPE_FUNC_PTR: return "FWD_PTR"; default: - abort(); + abort(); } return NULL; } -static int lower_extern_closure_call(struct state *state, struct scope *scope, struct type *rtype, size_t idx) +static int lower_extern_closure_call(struct state *state, struct scope *scope, + struct type *rtype, size_t idx) { char *q = buildstr("%s_call%zu", state->prefix, uniq(state)); @@ -885,7 +909,9 @@ static int lower_extern_closure_call(struct state *state, struct scope *scope, s FILE *ctx = open_memstream(&ctx_buf, &ctx_size); char *type = lower_type_str(state, scope, rtype); - fprintf(ctx, "struct %s {\n fwd_start_t start;\n %s a0;\n};\n", q, type); + fprintf(ctx, "struct %s {\n fwd_start_t start;\n %s a0;\n};\n", q, + type + ); free(type); fclose(ctx); @@ -893,21 +919,15 @@ static int lower_extern_closure_call(struct state *state, struct scope *scope, s add_type(state, ctx_buf); - indent(state); - fprintf(state->code, "fwd_call_t call = ctx->a%zu.call;\n", idx); - - indent(state); - fprintf(state->code, "struct %s *call_ctx = ctx->a%zu.args;\n", q, idx); - - indent(state); - fprintf(state->code, "call_ctx->a0 = extern_args[0].%s;\n", - fwd_typeparam(rtype)); - - indent(state); - fprintf(state->code, "fwd_stack_free(&stack, extern_args);\n"); - - indent(state); - fprintf(state->code, "FWD_MUSTTAIL return call(stack, call_ctx);\n"); + write_codeline(state, "fwd_call_t call = ctx->a%zu.call;\n", idx); + write_codeline(state, "struct %s *call_ctx = ctx->a%zu.args;\n", q, + idx + ); + write_codeline(state, "call_ctx->a0 = extern_args[0].%s;\n", + fwd_typeparam(rtype) + ); + write_codeline(state, "fwd_stack_free(&stack, extern_args);\n"); + write_codeline(state, "FWD_MUSTTAIL return call(stack, call_ctx);\n"); free(q); return 0; @@ -917,8 +937,10 @@ static int lower_extern_proc(struct state *state, struct ast *proc) { add_proc(state, proc); char *name = mangle2(proc); - char *proto = buildstr("static fwd_stack_t %s(fwd_stack_t stack, fwd_args_t args)", - name); + char *proto = buildstr( + "static fwd_stack_t %s(fwd_stack_t stack, fwd_args_t args)", + name + ); char *decl = buildstr("%s;\n", proto); add_decl(state, decl); @@ -939,17 +961,17 @@ static int lower_extern_proc(struct state *state, struct ast *proc) fprintf(new_state.code, "%s\n{\n", proto); increase_indent(&new_state); - indent(&new_state); - fprintf(new_state.code, "extern long %s(fwd_extern_args_t);\n", proc_id(proc)); + write_codeline(&new_state, "extern long %s(fwd_extern_args_t);\n", + proc_id(proc) + ); /* for now, allocate a new frame for arguments. Might want to * simplify this by always using the 'external' format for argument * passing, even internally */ - indent(&new_state); - fprintf(new_state.code, "struct %s_ctx *ctx = args;\n", name); - - indent(&new_state); - fprintf(new_state.code, "struct fwd_arg *extern_args = fwd_stack_alloc(&stack);\n"); + write_codeline(&new_state, "struct %s_ctx *ctx = args;\n", name); + write_codeline(&new_state, + "struct fwd_arg *extern_args = fwd_stack_alloc(&stack);\n" + ); size_t idx = 0; struct type *rtype = NULL; @@ -966,40 +988,43 @@ static int lower_extern_proc(struct state *state, struct ast *proc) break; } - indent(&new_state); /* leave place for return value */ - fprintf(new_state.code, "extern_args[%zu] = (fwd_arg_t){%s, " - "{.%s = ctx->a%zu}};\n", - idx + 1, fwd_ctypestr(p->t), fwd_typeparam(p->t), idx); + write_codeline(&new_state, "extern_args[%zu] = (fwd_arg_t){%s, " + "{.%s = ctx->a%zu}};\n", + idx + 1, fwd_ctypestr(p->t), fwd_typeparam(p->t), + idx + ); idx++; } - indent(&new_state); - fprintf(new_state.code, "%s((fwd_extern_args_t){.argc = %zu, .args = extern_args});\n", - proc_id(proc), idx); + write_codeline(&new_state, + "%s((fwd_extern_args_t){.argc = %zu, .args = extern_args});\n", + proc_id(proc), idx + ); if (rtype) { struct type *ctype = rtype->k == TYPE_PURE_CLOSURE - ? tpure_closure_args(rtype) - : tclosure_args(rtype) - ; + ? tpure_closure_args(rtype) + : tclosure_args(rtype) + ; - indent(&new_state); - fprintf(new_state.code, "assert(extern_args[0].t == %s);\n", - fwd_ctypestr(ctype)); + write_codeline(&new_state, "assert(extern_args[0].t == %s);\n", + fwd_ctypestr(ctype) + ); - if (lower_extern_closure_call(&new_state, proc->scope, ctype, idx)) + if (lower_extern_closure_call(&new_state, proc->scope, + ctype, idx + )) return -1; } else { /* void func */ - indent(&new_state); - fprintf(new_state.code, "fwd_stack_free(&stack, extern_args);\n"); - - indent(&new_state); - fprintf(new_state.code, "return stack;\n"); + write_codeline(&new_state, + "fwd_stack_free(&stack, extern_args);\n" + ); + write_codeline(&new_state, "return stack;\n"); } fprintf(new_state.code, "}\n\n"); @@ -1018,16 +1043,15 @@ static int lower_extern_proc(struct state *state, struct ast *proc) return 0; } -static int lower_param_copy(struct state *state, struct ast *param, FILE *f, size_t idx) +static int lower_param_copy(struct state *state, struct ast *param, FILE *f, + size_t idx) { char *type = lower_type_str(state, param->scope, param->t); fprintf(f, " %s a%zu;\n", type, idx); free(type); - indent(state); - char *p = mangle2(param); - fprintf(state->code, "ctx->%s = params->a%zu;\n", p, idx); + write_codeline(state, "ctx->%s = params->a%zu;\n", p, idx); free(p); return 0; } @@ -1109,7 +1133,11 @@ static int lower_proc(struct state *state, struct ast *proc) add_proc(state, proc); char *name = mangle2(proc); - char *proto = buildstr("static fwd_stack_t %s(fwd_stack_t stack, fwd_args_t args)", name); + char *proto = buildstr( + "static fwd_stack_t %s(fwd_stack_t stack, fwd_args_t args)", + name + ); + char *decl = buildstr("%s;\n", proto); add_decl(state, decl); @@ -1130,30 +1158,35 @@ static int lower_proc(struct state *state, struct ast *proc) fprintf(new_state.code, "%s\n{\n", proto); fprintf(new_state.ctx, "struct %s_ctx {\n", name); - fprintf(new_state.ctx, " fwd_args_t global_args;\n fwd_start_t %s;\n", start_of); + fprintf(new_state.ctx, " fwd_args_t global_args;\n fwd_start_t %s;\n", + start_of + ); increase_indent(&new_state); - indent(&new_state); - fprintf(new_state.code, "static_assert(FWD_FRAME_SIZE >= sizeof(struct %s_ctx), \"context exceeds frame size\");\n", - name); + write_codeline(&new_state, + "static_assert(FWD_FRAME_SIZE >= sizeof(struct %s_ctx)," + " \"context exceeds frame size\");\n", + name + ); if (ast_flags(proc, AST_REQ_FRAME)) { - indent(&new_state); - fprintf(new_state.code, "struct %s_ctx *ctx " - "= fwd_stack_alloc(&stack);\n", - name); + write_codeline(&new_state, "struct %s_ctx *ctx " + "= fwd_stack_alloc(&stack);\n", + name + ); } else { - indent(&new_state); - fprintf(new_state.code, "struct %s_ctx ctx_buf;\n", name); - indent(&new_state); - fprintf(new_state.code, "struct %s_ctx *ctx = &ctx_buf;\n", name); + write_codeline(&new_state, "struct %s_ctx ctx_buf;\n", name); + write_codeline(&new_state, "struct %s_ctx *ctx = &ctx_buf;\n", + name + ); } if (proc_params(proc)) { - indent(&new_state); - fprintf(new_state.code, "struct %s_params *params = (struct %s_params *)args;\n", - name, name); + write_codeline(&new_state, + "struct %s_params *params = (struct %s_params *)args;\n", + name, name + ); } /* allocates parameter slots */ @@ -1164,8 +1197,7 @@ static int lower_proc(struct state *state, struct ast *proc) ret = lower_param_copies(&new_state, proc_params(proc)); assert(ret == 0); - indent(&new_state); - fprintf(new_state.code, "ctx->global_args = args;\n"); + write_codeline(&new_state, "ctx->global_args = args;\n"); struct ast *block = proc_body(proc); ret = lower_stmt_list(&new_state, block_body(block), true); @@ -1251,7 +1283,10 @@ int lower(struct scope *root) printf("}\n"); /* modules require a register function of some kind, implement a stub */ - printf("int fwd_register(struct fwd_state *state, const char *name, fwd_extern_t func, fwd_type_t rtype, ...) {return 0;}\n"); + printf( + "int fwd_register(struct fwd_state *state, const char *name," + " fwd_extern_t func, fwd_type_t rtype, ...) {return 1;}\n" + ); string_vec_destroy(&defns); string_vec_destroy(&decls); |
