harec

[hare] Hare compiler, written in C11 for POSIX OSs
Log | Files | Refs | README | LICENSE

commit 375d1d2d7d0358da55a02242efd184bb94e63698
parent 4afbfe036d2a30cbc67333c58bfdddc49f991210
Author: Bor Grošelj Simić <bgs@turminal.net>
Date:   Wed,  8 Feb 2023 05:15:59 +0100

move gen_name to util and use it more often

Signed-off-by: Bor Grošelj Simić <bgs@turminal.net>

Diffstat:
Minclude/gen.h | 1-
Minclude/util.h | 2++
Msrc/check.c | 25+++++--------------------
Msrc/gen.c | 16++++++++--------
Msrc/genutil.c | 14++------------
Msrc/qtype.c | 18++++--------------
Msrc/util.c | 11+++++++++++
7 files changed, 32 insertions(+), 55 deletions(-)

diff --git a/include/gen.h b/include/gen.h @@ -83,7 +83,6 @@ void gen(const struct unit *unit, struct qbe_program *out); // genutil.c -char *gen_name(struct gen_context *ctx, const char *fmt); struct gen_value mkgtemp(struct gen_context *ctx, const struct type *type, const char *fmt); struct qbe_value mkqval(struct gen_context *ctx, struct gen_value *value); diff --git a/include/util.h b/include/util.h @@ -21,6 +21,8 @@ char *xstrdup(const char *s); #define realloc(a, b) (void *)sizeof(struct { static_assert(0, "Use xrealloc instead"); int _; }) #define strdup(s) (char *)(sizeof(struct { static_assert(0, "Use xstrdup instead"); int _; }) +char *gen_name(int *id, const char *fmt); + struct pathspec { const char *var; const char *path; diff --git a/src/check.c b/src/check.c @@ -1094,10 +1094,7 @@ check_binding_unpack(struct context *ctx, struct identifier gen = {0}; // Generate a static declaration identifier - int n = snprintf(NULL, 0, "static.%d", ctx->id); - gen.name = xcalloc(n + 1, 1); - snprintf(gen.name, n + 1, "static.%d", ctx->id); - ++ctx->id; + gen.name = gen_name(&ctx->id, "static.%d"); unpack->object = scope_insert( ctx->scope, O_DECL, &gen, &ident, @@ -1177,10 +1174,7 @@ check_expr_binding(struct context *ctx, struct identifier gen = {0}; if (abinding->is_static) { // Generate a static declaration identifier - int n = snprintf(NULL, 0, "static.%d", ctx->id); - gen.name = xcalloc(n + 1, 1); - snprintf(gen.name, n + 1, "static.%d", ctx->id); - ++ctx->id; + gen.name = gen_name(&ctx->id, "static.%d"); } bool context = abinding->type @@ -2360,20 +2354,14 @@ check_expr_propagate(struct context *ctx, struct match_case *case_err = xcalloc(1, sizeof(struct match_case)); struct identifier ok_name = {0}, err_name = {0}; - int n = snprintf(NULL, 0, "ok.%d", ctx->id); - ok_name.name = xcalloc(n + 1, 1); - snprintf(ok_name.name, n + 1, "ok.%d", ctx->id); - ++ctx->id; + ok_name.name = gen_name(&ctx->id, "ok.%d"); const struct scope_object *ok_obj = NULL; if (result_type->size != 0 && result_type->size != SIZE_UNDEFINED) { ok_obj = scope_insert(scope, O_BIND, &ok_name, &ok_name, result_type, NULL); } - n = snprintf(NULL, 0, "err.%d", ctx->id); - err_name.name = xcalloc(n + 1, 1); - snprintf(err_name.name, n + 1, "err.%d", ctx->id); - ++ctx->id; + err_name.name = gen_name(&ctx->id, "err.%d"); const struct scope_object *err_obj = NULL; if (return_type->size != 0 && return_type->size != SIZE_UNDEFINED) { err_obj = scope_insert(scope, O_BIND, &err_name, @@ -4015,10 +4003,7 @@ scan_decl(struct context *ctx, struct scope *imports, struct ast_decl *decl) template = "finifunc.%d"; } assert(template); - - int n = snprintf(NULL, 0, template, ctx->id); - ident.name = xcalloc(n + 1, 1); - snprintf(ident.name, n + 1, template, ctx->id); + ident.name = gen_name(&ctx->id, template); ++ctx->id; name = &ident; diff --git a/src/gen.c b/src/gen.c @@ -164,7 +164,7 @@ gen_load(struct gen_context *ctx, struct gen_value object) struct gen_value value = { .kind = GV_TEMP, .type = object.type, - .name = gen_name(ctx, "load.%d"), + .name = gen_name(&ctx->id, "load.%d"), }; struct qbe_value qobj = mkqval(ctx, &object), qval = mkqval(ctx, &value); @@ -1081,7 +1081,7 @@ gen_expr_binding_unpack(struct gen_context *ctx, assert(binding->object == NULL); const struct type *type = binding->initializer->result; - char *tuple_name = gen_name(ctx, "tupleunpack.%d"); + char *tuple_name = gen_name(&ctx->id, "tupleunpack.%d"); struct gen_value tuple_gv = { .kind = GV_TEMP, .type = type, @@ -1105,7 +1105,7 @@ gen_expr_binding_unpack(struct gen_context *ctx, struct gen_value item_gv = { .kind = GV_TEMP, .type = type, - .name = gen_name(ctx, "binding.%d"), + .name = gen_name(&ctx->id, "binding.%d"), }; struct gen_binding *gb = xcalloc(1, sizeof(struct gen_binding)); gb->value = item_gv; @@ -1150,7 +1150,7 @@ gen_expr_binding(struct gen_context *ctx, const struct expression *expr) struct gen_binding *gb = xcalloc(1, sizeof(struct gen_binding)); gb->value.kind = GV_TEMP; gb->value.type = type; - gb->value.name = gen_name(ctx, "binding.%d"); + gb->value.name = gen_name(&ctx->id, "binding.%d"); gb->object = binding->object; gb->next = ctx->bindings; ctx->bindings = gb; @@ -2572,7 +2572,7 @@ gen_match_with_tagged(struct gen_context *ctx, struct gen_binding *gb = xcalloc(1, sizeof(struct gen_binding)); gb->value.kind = GV_TEMP; gb->value.type = _case->type; - gb->value.name = gen_name(ctx, "binding.%d"); + gb->value.name = gen_name(&ctx->id, "binding.%d"); gb->object = _case->object; gb->next = ctx->bindings; ctx->bindings = gb; @@ -3336,7 +3336,7 @@ gen_function_decl(struct gen_context *ctx, const struct declaration *decl) // No need to copy to stack gb->value.name = xstrdup(param->name); } else { - gb->value.name = gen_name(ctx, "param.%d"); + gb->value.name = gen_name(&ctx->id, "param.%d"); struct qbe_value qv = mklval(ctx, &gb->value); struct qbe_value sz = constl(type->size); @@ -3552,7 +3552,7 @@ gen_data_item(struct gen_context *ctx, struct expression *expr, break; case STORAGE_STRING: def = xcalloc(1, sizeof(struct qbe_def)); - def->name = gen_name(ctx, "strdata.%d"); + def->name = gen_name(&ctx->id, "strdata.%d"); def->kind = Q_DATA; def->data.align = ALIGN_UNDEFINED; def->data.items.type = QD_STRING; @@ -3583,7 +3583,7 @@ gen_data_item(struct gen_context *ctx, struct expression *expr, break; case STORAGE_SLICE: def = xcalloc(1, sizeof(struct qbe_def)); - def->name = gen_name(ctx, "sldata.%d"); + def->name = gen_name(&ctx->id, "sldata.%d"); def->kind = Q_DATA; def->data.align = ALIGN_UNDEFINED; diff --git a/src/genutil.c b/src/genutil.c @@ -6,16 +6,6 @@ #include "types.h" #include "util.h" -char * -gen_name(struct gen_context *ctx, const char *fmt) -{ - int n = snprintf(NULL, 0, fmt, ctx->id); - char *str = xcalloc(1, n + 1); - snprintf(str, n + 1, fmt, ctx->id); - ++ctx->id; - return str; -} - struct qbe_value mkqval(struct gen_context *ctx, struct gen_value *value) { @@ -62,7 +52,7 @@ mkqtmp(struct gen_context *ctx, const struct qbe_type *qtype, const char *fmt) return (struct qbe_value){ .kind = QV_TEMPORARY, .type = qtype, - .name = gen_name(ctx, fmt), + .name = gen_name(&ctx->id, fmt), }; } @@ -72,7 +62,7 @@ mkgtemp(struct gen_context *ctx, const struct type *type, const char *fmt) return (struct gen_value){ .kind = GV_TEMP, .type = type, - .name = gen_name(ctx, fmt), + .name = gen_name(&ctx->id, fmt), }; } diff --git a/src/qtype.c b/src/qtype.c @@ -19,18 +19,13 @@ sf_compar(const void *_a, const void *_b) static const struct qbe_type * tagged_qtype(struct gen_context *ctx, const struct type *type) { - int n = snprintf(NULL, 0, "tags.%d", ctx->id); - char *name = xcalloc(1, n + 1); - snprintf(name, n + 1, "tags.%d", ctx->id); - ++ctx->id; - struct qbe_def *def = xcalloc(1, sizeof(struct qbe_def)); def->kind = Q_TYPE; - def->name = name; + def->name = gen_name(&ctx->id, "tags.%d"); def->exported = false; def->type.stype = Q__AGGREGATE; def->type.base = NULL; - def->type.name = name; + def->type.name = def->name; def->type.size = type->size - type->align; struct qbe_field *field = &def->type.fields; @@ -66,17 +61,12 @@ aggregate_lookup(struct gen_context *ctx, const struct type *type) } } - int n = snprintf(NULL, 0, "type.%d", ctx->id); - char *name = xcalloc(1, n + 1); - snprintf(name, n + 1, "type.%d", ctx->id); - ++ctx->id; - struct qbe_def *def = xcalloc(1, sizeof(struct qbe_def)); def->kind = Q_TYPE; - def->name = name; + def->name = gen_name(&ctx->id, "type.%d"); def->type.stype = Q__AGGREGATE; def->type.base = type; - def->type.name = name; + def->type.name = def->name; const struct type *final = type_dealias(type); assert((final->storage == STORAGE_STRUCT && final->struct_union.packed) diff --git a/src/util.c b/src/util.c @@ -1,6 +1,7 @@ #include <stdlib.h> #include <stdint.h> #include <string.h> +#include <stdio.h> #include "util.h" // Remove safety macros: #undef malloc @@ -91,6 +92,16 @@ xstrdup(const char *s) } char * +gen_name(int *id, const char *fmt) +{ + int n = snprintf(NULL, 0, fmt, *id); + char *str = xcalloc(1, n + 1); + snprintf(str, n + 1, fmt, *id); + ++*id; + return str; +} + +char * getpath(const struct pathspec *paths, size_t npaths) { for (size_t i = 0; i < npaths; i++) { const char *var = "";