Skip to content

Commit f1fd243

Browse files
committed
add resolve label in builder and reformat
1 parent c474d88 commit f1fd243

6 files changed

Lines changed: 26 additions & 10 deletions

File tree

docs/builder.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ around abi registers, stack setup, and label plumbing, and it looks less scary.
3030
- `cj_builder_call_unary(ctx, scratch, label, arg)`: loads the first argument
3131
register, emits the right call/bl, and—when a scratch stack is supplied—moves
3232
the return value into a fresh scratch slot.
33+
- `cj_resolve_label(ctx, module, label)`: convert a recorded label to a
34+
callable pointer after finalization.
3335

3436
## control flow
3537

examples/minilang.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,8 @@ int main(void)
398398

399399
for (int i = 0; i < function_count; ++i)
400400
{
401-
size_t offset = cj->label_positions[functions[i].entry.id];
402-
functions[i].fn = (int (*)(int))((uint8_t *)(void *)module + offset);
401+
void *addr = cj_resolve_label(cj, module, functions[i].entry);
402+
functions[i].fn = (int (*)(int))addr;
403403
}
404404

405405
printf("minilang demo:\n");

src/builder.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,8 @@ static inline cj_operand cj_builder_scratch_acquire(cj_builder_scratch *scratch)
8484
static inline void cj_builder_scratch_release(cj_builder_scratch *scratch);
8585
static inline unsigned cj_builder_arg_int_capacity(void);
8686
static inline void cj_builder_call_label(cj_ctx *ctx, cj_label target);
87-
static inline cj_operand
88-
cj_builder_call(cj_ctx *ctx, cj_builder_scratch *scratch, cj_label target,
89-
const cj_operand *args, size_t arg_count);
87+
static inline cj_operand cj_builder_call(cj_ctx *ctx, cj_builder_scratch *scratch, cj_label target,
88+
const cj_operand *args, size_t arg_count);
9089
static inline cj_operand cj_builder_call_unary(cj_ctx *ctx, cj_builder_scratch *scratch,
9190
cj_label target, cj_operand arg0);
9291

@@ -640,9 +639,8 @@ static inline cj_operand cj_builder_call_unary(cj_ctx *ctx, cj_builder_scratch *
640639
return cj_builder_call(ctx, scratch, target, args, 1);
641640
}
642641

643-
static inline cj_operand
644-
cj_builder_call(cj_ctx *ctx, cj_builder_scratch *scratch, cj_label target,
645-
const cj_operand *args, size_t arg_count)
642+
static inline cj_operand cj_builder_call(cj_ctx *ctx, cj_builder_scratch *scratch, cj_label target,
643+
const cj_operand *args, size_t arg_count)
646644
{
647645
if (!ctx)
648646
return cj_builder_return_reg();

src/ctx.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,19 @@ void cj_emit_x86_rel(cj_ctx *ctx, const uint8_t *opcode, size_t opcode_len, uint
285285
ctx->num_fixups++;
286286
}
287287
}
288+
289+
void *cj_resolve_label(const cj_ctx *ctx, cj_fn module, cj_label label)
290+
{
291+
if (!ctx || !module)
292+
return NULL;
293+
294+
if (label.id < 0 || label.id >= ctx->num_labels)
295+
return NULL;
296+
297+
uint64_t pos = ctx->label_positions[label.id];
298+
if (pos == UINT64_MAX)
299+
return NULL;
300+
301+
uint8_t *base = (uint8_t *)(void *)module;
302+
return (void *)(base + pos);
303+
}

src/ctx.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,4 @@ void cj_emit_branch(cj_ctx *ctx, uint32_t base_instr, cj_label label, uint8_t of
6767
uint8_t offset_shift);
6868
void cj_emit_x86_rel(cj_ctx *ctx, const uint8_t *opcode, size_t opcode_len, uint8_t disp_width,
6969
cj_label label);
70+
void *cj_resolve_label(const cj_ctx *ctx, cj_fn module, cj_label label);

tests/test_harness_builder.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ static void test_call_helper(void)
100100
cj_fn module = create_cj_fn(cj);
101101
assert(module);
102102

103-
size_t entry_offset = cj->label_positions[entry.id];
104-
fn1_t fn = (fn1_t)((uint8_t *)(void *)module + entry_offset);
103+
fn1_t fn = (fn1_t)cj_resolve_label(cj, module, entry);
105104
assert(fn);
106105
assert(fn(10) == 13);
107106
assert(fn(-4) == -1);

0 commit comments

Comments
 (0)