From 091a24d53f0d130740a73b571bc4123263c1bcec Mon Sep 17 00:00:00 2001 From: Anatol Belski Date: Mon, 27 Mar 2023 00:29:58 +0200 Subject: [PATCH 1/4] C++: Add guards for relevant headers Signed-off-by: Anatol Belski --- ir.h | 8 ++++++++ ir_builder.h | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/ir.h b/ir.h index 9fb62bc..63109db 100644 --- a/ir.h +++ b/ir.h @@ -8,6 +8,10 @@ #ifndef IR_H #define IR_H +#ifdef __cplusplus +extern "C" { +#endif + #include #include #include @@ -756,4 +760,8 @@ int ir_mem_protect(void *ptr, size_t size); int ir_mem_unprotect(void *ptr, size_t size); int ir_mem_flush(void *ptr, size_t size); +#ifdef __cplusplus +} /* extern "C" */ +#endif + #endif /* IR_H */ diff --git a/ir_builder.h b/ir_builder.h index eb0c70e..4333e28 100644 --- a/ir_builder.h +++ b/ir_builder.h @@ -8,6 +8,10 @@ #ifndef IR_BUILDER_H #define IR_BUILDER_H +#ifdef __cplusplus +extern "C" { +#endif + /* _ir_CTX may be redefined by the user */ #define _ir_CTX ctx @@ -589,4 +593,8 @@ ir_ref _ir_SNAPSHOT(ir_ctx *ctx, ir_ref n); void _ir_SNAPSHOT_SET_OP(ir_ctx *ctx, ir_ref snapshot, ir_ref pos, ir_ref val); ir_ref _ir_EXITCALL(ir_ctx *ctx, ir_ref func); +#ifdef __cplusplus +} /* extern "C" */ +#endif + #endif /* IR_BUILDER_H */ From f55b9b094bf9d2aba732d36448cbfefc3fd74e08 Mon Sep 17 00:00:00 2001 From: Anatol Belski Date: Mon, 27 Mar 2023 00:51:05 +0200 Subject: [PATCH 2/4] doc: Add short comments to some flags Signed-off-by: Anatol Belski --- ir.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ir.h b/ir.h index 63109db..bc5a064 100644 --- a/ir.h +++ b/ir.h @@ -456,9 +456,9 @@ void ir_strtab_apply(ir_strtab *strtab, ir_strtab_apply_t func); void ir_strtab_free(ir_strtab *strtab); /* IR Context Flags */ -#define IR_FUNCTION (1<<0) -#define IR_FASTCALL_FUNC (1<<1) -#define IR_SKIP_PROLOGUE (1<<2) +#define IR_FUNCTION (1<<0) /* Generate a function. */ +#define IR_FASTCALL_FUNC (1<<1) /* Generate a function with fastcall calling convention, x86 32-bit only. */ +#define IR_SKIP_PROLOGUE (1<<2) /* Don't generate function prologue. */ #define IR_USE_FRAME_POINTER (1<<3) #define IR_PREALLOCATED_STACK (1<<4) #define IR_HAS_ALLOCA (1<<5) @@ -509,10 +509,10 @@ typedef void (*ir_snapshot_create_t)(ir_ctx *ctx, ir_ref addr); struct _ir_ctx { ir_insn *ir_base; /* two directional array - instructions grow down, constants grow up */ ir_ref insns_count; - ir_ref insns_limit; + ir_ref insns_limit; /* initial instructions count */ ir_ref consts_count; - ir_ref consts_limit; - uint32_t flags; + ir_ref consts_limit; /* initial constants count */ + uint32_t flags; /* IR context flags */ ir_ref fold_cse_limit; ir_insn fold_insn; ir_hashtab *binding; From b701684704ce256df1e1e25088c1621bd5975dfb Mon Sep 17 00:00:00 2001 From: Anatol Belski Date: Mon, 27 Mar 2023 00:54:08 +0200 Subject: [PATCH 3/4] ir_init: Expose min limit for constants and instructions Signed-off-by: Anatol Belski --- ir.c | 4 ++-- ir.h | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/ir.c b/ir.c index 4e02867..b88e685 100644 --- a/ir.c +++ b/ir.c @@ -286,8 +286,8 @@ void ir_init(ir_ctx *ctx, ir_ref consts_limit, ir_ref insns_limit) { ir_insn *buf; - IR_ASSERT(consts_limit >= -(IR_TRUE - 1)); - IR_ASSERT(insns_limit >= IR_UNUSED + 1); + IR_ASSERT(consts_limit >= IR_CONSTS_LIMIT_MIN); + IR_ASSERT(insns_limit >= IR_INSNS_LIMIT_MIN); ctx->insns_count = IR_UNUSED + 1; ctx->insns_limit = insns_limit; diff --git a/ir.h b/ir.h index bc5a064..22ad2cb 100644 --- a/ir.h +++ b/ir.h @@ -347,6 +347,9 @@ typedef int32_t ir_ref; #define IR_TRUE (-3) #define IR_LAST_FOLDABLE_OP IR_COPY +#define IR_CONSTS_LIMIT_MIN (-(IR_TRUE - 1)) +#define IR_INSNS_LIMIT_MIN (IR_UNUSED + 1) + /* IR Constant Value */ #ifndef IR_64 # define ADDR_MEMBER uintptr_t addr; From d0b4f108ee83e610ffb95a9bf6d754a60b9855b4 Mon Sep 17 00:00:00 2001 From: Anatol Belski Date: Mon, 27 Mar 2023 01:09:35 +0200 Subject: [PATCH 4/4] ir_init: Accept flags as an additional arguments Signed-off-by: Anatol Belski --- ir.c | 4 ++-- ir.h | 2 +- ir_gcm.c | 3 +-- ir_main.c | 11 +++++------ ir_test.c | 7 +++---- 5 files changed, 12 insertions(+), 15 deletions(-) diff --git a/ir.c b/ir.c index b88e685..bf5473c 100644 --- a/ir.c +++ b/ir.c @@ -282,7 +282,7 @@ void ir_truncate(ir_ctx *ctx) ctx->ir_base = buf + ctx->consts_limit; } -void ir_init(ir_ctx *ctx, ir_ref consts_limit, ir_ref insns_limit) +void ir_init(ir_ctx *ctx, uint32_t flags, ir_ref consts_limit, ir_ref insns_limit) { ir_insn *buf; @@ -294,7 +294,7 @@ void ir_init(ir_ctx *ctx, ir_ref consts_limit, ir_ref insns_limit) ctx->consts_count = -(IR_TRUE - 1); ctx->consts_limit = consts_limit; ctx->fold_cse_limit = IR_UNUSED + 1; - ctx->flags = 0; + ctx->flags = flags; ctx->binding = NULL; diff --git a/ir.h b/ir.h index 22ad2cb..67d706f 100644 --- a/ir.h +++ b/ir.h @@ -557,7 +557,7 @@ struct _ir_ctx { }; /* Basic IR Construction API (implementation in ir.c) */ -void ir_init(ir_ctx *ctx, ir_ref consts_limit, ir_ref insns_limit); +void ir_init(ir_ctx *ctx, uint32_t flags, ir_ref consts_limit, ir_ref insns_limit); void ir_free(ir_ctx *ctx); void ir_truncate(ir_ctx *ctx); diff --git a/ir_gcm.c b/ir_gcm.c index 09b67e4..6181af7 100644 --- a/ir_gcm.c +++ b/ir_gcm.c @@ -658,9 +658,8 @@ restart: ir_mem_free(_prev); - ir_init(&new_ctx, consts_count, insns_count); + ir_init(&new_ctx, ctx->flags, consts_count, insns_count); new_ctx.insns_count = insns_count; - new_ctx.flags = ctx->flags; new_ctx.spill_base = ctx->spill_base; new_ctx.fixed_stack_red_zone = ctx->fixed_stack_red_zone; new_ctx.fixed_stack_frame_size = ctx->fixed_stack_frame_size; diff --git a/ir_main.c b/ir_main.c index e8c097f..e817b8f 100644 --- a/ir_main.c +++ b/ir_main.c @@ -381,19 +381,18 @@ int main(int argc, char **argv) ir_loader_init(); - ir_init(&ctx, 256, 1024); - ctx.flags |= IR_FUNCTION; - ctx.flags |= mflags; + uint32_t flags = IR_FUNCTION | mflags; if (opt_level > 0) { - ctx.flags |= IR_OPT_FOLDING | IR_OPT_CFG | IR_OPT_CODEGEN; + flags |= IR_OPT_FOLDING | IR_OPT_CFG | IR_OPT_CODEGEN; } if (emit_c) { - ctx.flags |= IR_GEN_C; + flags |= IR_GEN_C; } if (dump_asm || run) { - ctx.flags |= IR_GEN_NATIVE; + flags |= IR_GEN_NATIVE; } + ir_init(&ctx, flags, 256, 1024); ctx.fixed_regset = ~debug_regset; if (!ir_load(&ctx, f)) { diff --git a/ir_test.c b/ir_test.c index b3c4436..b0a1f1e 100644 --- a/ir_test.c +++ b/ir_test.c @@ -134,12 +134,11 @@ int main(int argc, char **argv) } } - ir_init(&ctx, 256, 1024); - ctx.flags |= IR_FUNCTION; - ctx.flags |= mflags; + uint32_t flags = IR_FUNCTION | mflags; if (opt_level > 0) { - ctx.flags |= IR_OPT_FOLDING | IR_OPT_CFG | IR_OPT_CODEGEN; + flags |= IR_OPT_FOLDING | IR_OPT_CFG | IR_OPT_CODEGEN; } + ir_init(&ctx, flags, 256, 1024); ctx.fixed_regset = ~debug_regset; gen_mandelbrot(&ctx); // ir_save(&ctx, stderr);