Skip to content

Commit 41ca09c

Browse files
committed
try to fix pendanticity errors
1 parent f1fd243 commit 41ca09c

3 files changed

Lines changed: 69 additions & 90 deletions

File tree

src/builder.h

Lines changed: 25 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ static inline cj_operand cj_builder_call_unary(cj_ctx *ctx, cj_builder_scratch *
9696
static inline size_t align_stack_size(size_t size)
9797
{
9898
const size_t alignment = 16;
99-
if (size == 0)
100-
return 0;
99+
if (size == 0) return 0;
100+
101101
size_t mask = alignment - 1;
102102
return (size + mask) & ~mask;
103103
}
@@ -116,8 +116,7 @@ static inline void cj_builder_fn_prologue_with_link_save(cj_ctx *ctx, size_t req
116116

117117
static inline void cj_builder_fn_epilogue(cj_ctx *ctx, const cj_builder_frame *frame)
118118
{
119-
if (!ctx)
120-
return;
119+
if (!ctx) return;
121120
size_t aligned = frame ? frame->stack_size : 0;
122121
int save_lr = (frame && frame->save_lr);
123122

@@ -157,8 +156,7 @@ static inline void cj_builder_fn_epilogue(cj_ctx *ctx, const cj_builder_frame *f
157156

158157
static inline void cj_builder_return(cj_ctx *ctx, const cj_builder_frame *frame)
159158
{
160-
if (!ctx)
161-
return;
159+
if (!ctx) return;
162160
cj_builder_fn_epilogue(ctx, frame);
163161
cj_ret(ctx);
164162
}
@@ -333,30 +331,26 @@ static inline cj_builder_block cj_builder_loop_begin(cj_ctx *ctx)
333331
static inline void cj_builder_loop_condition(cj_ctx *ctx, cj_builder_block block, cj_operand lhs,
334332
cj_operand rhs, cj_condition exit_cond)
335333
{
336-
if (!ctx)
337-
return;
334+
if (!ctx) return;
338335
cj_cmp(ctx, lhs, rhs);
339336
branch_on_condition(ctx, exit_cond, block.exit);
340337
}
341338

342339
static inline void cj_builder_loop_continue(cj_ctx *ctx, cj_builder_block block)
343340
{
344-
if (!ctx)
345-
return;
341+
if (!ctx) return;
346342
branch_unconditional(ctx, block.entry);
347343
}
348344

349345
static inline void cj_builder_loop_break(cj_ctx *ctx, cj_builder_block block)
350346
{
351-
if (!ctx)
352-
return;
347+
if (!ctx) return;
353348
branch_unconditional(ctx, block.exit);
354349
}
355350

356351
static inline void cj_builder_loop_end(cj_ctx *ctx, cj_builder_block block)
357352
{
358-
if (!ctx)
359-
return;
353+
if (!ctx) return;
360354
branch_unconditional(ctx, block.entry);
361355
cj_mark_label(ctx, block.exit);
362356
}
@@ -375,17 +369,15 @@ cj_builder_if_block cj_builder_if(cj_ctx *ctx, cj_operand lhs, cj_operand rhs, c
375369

376370
void cj_builder_else(cj_ctx *ctx, cj_builder_if_block *block)
377371
{
378-
if (!ctx || !block)
379-
return;
372+
if (!ctx || !block) return;
380373
branch_unconditional(ctx, block->end_label);
381374
cj_mark_label(ctx, block->else_label);
382375
block->has_else = 1;
383376
}
384377

385378
void cj_builder_endif(cj_ctx *ctx, cj_builder_if_block *block)
386379
{
387-
if (!ctx || !block)
388-
return;
380+
if (!ctx || !block) return;
389381
if (!block->has_else)
390382
{
391383
cj_mark_label(ctx, block->else_label);
@@ -404,10 +396,7 @@ static inline cj_builder_for_loop cj_builder_for_begin(cj_ctx *ctx, cj_operand c
404396
.exit_cond = exit_cond,
405397
};
406398

407-
if (counter.type == CJ_REGISTER)
408-
{
409-
cj_mov(ctx, counter, start);
410-
}
399+
if (counter.type == CJ_REGISTER) cj_mov(ctx, counter, start);
411400

412401
loop.block = cj_builder_loop_begin(ctx);
413402
cj_builder_loop_condition(ctx, loop.block, counter, limit, exit_cond);
@@ -416,23 +405,20 @@ static inline cj_builder_for_loop cj_builder_for_begin(cj_ctx *ctx, cj_operand c
416405

417406
static inline void cj_builder_for_continue(cj_ctx *ctx, cj_builder_for_loop *loop)
418407
{
419-
if (!ctx || !loop)
420-
return;
408+
if (!ctx || !loop) return;
421409
cj_add(ctx, loop->counter, loop->step);
422410
cj_builder_loop_continue(ctx, loop->block);
423411
}
424412

425413
static inline void cj_builder_for_break(cj_ctx *ctx, cj_builder_for_loop *loop)
426414
{
427-
if (!ctx || !loop)
428-
return;
415+
if (!ctx || !loop) return;
429416
cj_builder_loop_break(ctx, loop->block);
430417
}
431418

432419
static inline void cj_builder_for_end(cj_ctx *ctx, cj_builder_for_loop *loop)
433420
{
434-
if (!ctx || !loop)
435-
return;
421+
if (!ctx || !loop) return;
436422
cj_add(ctx, loop->counter, loop->step);
437423
cj_builder_loop_end(ctx, loop->block);
438424
}
@@ -456,8 +442,8 @@ static inline cj_operand cj_builder_assign(cj_ctx *ctx, cj_operand dst, cj_opera
456442
for (int shift = 16; shift < (is64 ? 64 : 32); shift += 16)
457443
{
458444
uint16_t part = (uint16_t)((value >> shift) & 0xFFFFu);
459-
if (!part)
460-
continue;
445+
if (!part) continue;
446+
461447
uint64_t encoded = (uint64_t)part | ((uint64_t)(shift / 16) << 16);
462448
cj_operand next = cj_make_constant(encoded);
463449
cj_movk(ctx, dst, next);
@@ -525,21 +511,15 @@ static inline cj_operand cj_builder_arg_int(cj_ctx *ctx, unsigned index)
525511
static inline void cj_builder_return_value(cj_ctx *ctx, const cj_builder_frame *frame,
526512
cj_operand value)
527513
{
528-
if (!ctx)
529-
return;
514+
if (!ctx) return;
530515
cj_operand ret = get_return_operand();
531516
int needs_move = 1;
532517
if (value.type == CJ_REGISTER && value.reg && ret.reg)
533518
{
534-
if (strcmp(value.reg, ret.reg) == 0)
535-
{
536-
needs_move = 0;
537-
}
538-
}
539-
if (needs_move)
540-
{
541-
cj_mov(ctx, ret, value);
519+
if (strcmp(value.reg, ret.reg) == 0) needs_move = 0;
542520
}
521+
if (needs_move) cj_mov(ctx, ret, value);
522+
543523
cj_builder_return(ctx, frame);
544524
}
545525

@@ -554,8 +534,7 @@ static inline cj_operand cj_builder_zero_operand(void)
554534

555535
static inline void cj_builder_clear(cj_ctx *ctx, cj_operand dst)
556536
{
557-
if (!ctx)
558-
return;
537+
if (!ctx) return;
559538
#if defined(__x86_64__) || defined(_M_X64)
560539
if (dst.type == CJ_REGISTER)
561540
{
@@ -603,8 +582,7 @@ static inline unsigned cj_builder_arg_int_capacity(void)
603582

604583
static inline void cj_builder_scratch_init(cj_builder_scratch *scratch)
605584
{
606-
if (!scratch)
607-
return;
585+
if (!scratch) return;
608586
scratch->depth = 0;
609587
}
610588

@@ -642,8 +620,7 @@ static inline cj_operand cj_builder_call_unary(cj_ctx *ctx, cj_builder_scratch *
642620
static inline cj_operand cj_builder_call(cj_ctx *ctx, cj_builder_scratch *scratch, cj_label target,
643621
const cj_operand *args, size_t arg_count)
644622
{
645-
if (!ctx)
646-
return cj_builder_return_reg();
623+
if (!ctx) return cj_builder_return_reg();
647624

648625
unsigned capacity = cj_builder_arg_int_capacity();
649626
assert(arg_count <= capacity);
@@ -654,8 +631,7 @@ static inline cj_operand cj_builder_call(cj_ctx *ctx, cj_builder_scratch *scratc
654631
cj_builder_assign(ctx, reg, args[i]);
655632
}
656633

657-
if (scratch)
658-
cj_builder_scratch_release(scratch);
634+
if (scratch) cj_builder_scratch_release(scratch);
659635

660636
cj_builder_call_label(ctx, target);
661637

@@ -672,8 +648,7 @@ static inline cj_operand cj_builder_call(cj_ctx *ctx, cj_builder_scratch *scratc
672648
static inline void cj_builder_fn_prologue_ex(cj_ctx *ctx, size_t requested_stack_bytes,
673649
cj_builder_frame *frame, int save_lr)
674650
{
675-
if (!ctx)
676-
return;
651+
if (!ctx) return;
677652
assert(!save_lr || frame);
678653

679654
size_t aligned = align_stack_size(requested_stack_bytes);

src/ctx.c

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,24 @@ cj_ctx *create_cj_ctx(void)
2222
res->num_fixups = 0;
2323
res->fixups = malloc(sizeof(cj_fixup) * res->fixup_capacity);
2424

25+
res->executable_base = NULL;
26+
res->executable_raw = NULL;
27+
res->executable_size = 0;
28+
res->executable_code_size = 0;
29+
2530
return res;
2631
}
2732

2833
void grow_cj_ctx(cj_ctx *ctx)
2934
{
30-
if (!ctx)
31-
return;
35+
if (!ctx) return;
3236

3337
uint64_t old_size = ctx->size;
3438
uint64_t new_size = old_size * 2;
35-
if (new_size < old_size)
36-
return;
39+
if (new_size < old_size) return;
40+
3741
uint8_t *new_mem = realloc(ctx->mem, new_size);
38-
if (!new_mem)
39-
return;
42+
if (!new_mem) return;
4043

4144
ctx->mem = new_mem;
4245
memset(ctx->mem + old_size, 0, old_size);
@@ -53,17 +56,13 @@ void destroy_cj_ctx(cj_ctx *ctx)
5356

5457
cj_fn create_cj_fn(cj_ctx *ctx)
5558
{
56-
if (!ctx->len)
57-
return NULL;
59+
if (!ctx->len) return NULL;
5860

5961
uint64_t code_size = ctx->len;
6062
size_t total_size = sizeof(uint64_t) + (size_t)code_size;
6163

6264
uint8_t *raw = mmap(NULL, total_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
63-
if (raw == MAP_FAILED)
64-
{
65-
return NULL;
66-
}
65+
if (raw == MAP_FAILED) return NULL;
6766

6867
assert(ctx->mem);
6968
uint8_t *dest = raw + sizeof(uint64_t);
@@ -76,6 +75,11 @@ cj_fn create_cj_fn(cj_ctx *ctx)
7675
return NULL;
7776
}
7877

78+
ctx->executable_raw = raw;
79+
ctx->executable_base = dest;
80+
ctx->executable_size = total_size;
81+
ctx->executable_code_size = code_size;
82+
7983
// clear the I cache for ARM64
8084
__builtin___clear_cache((char *)raw, (char *)raw + total_size);
8185

@@ -89,8 +93,7 @@ cj_fn create_cj_fn(cj_ctx *ctx)
8993
void destroy_cj_fn(cj_ctx *ctx, cj_fn mem)
9094
{
9195
(void)ctx;
92-
if (!mem)
93-
return;
96+
if (!mem) return;
9497

9598
// yes, yes. unsafe. boo-hoo.
9699
#pragma GCC diagnostic push
@@ -102,12 +105,19 @@ void destroy_cj_fn(cj_ctx *ctx, cj_fn mem)
102105
size_t total_size = sizeof(uint64_t) + (size_t)code_size;
103106

104107
munmap(raw, total_size);
108+
109+
if (ctx)
110+
{
111+
ctx->executable_base = NULL;
112+
ctx->executable_raw = NULL;
113+
ctx->executable_size = 0;
114+
ctx->executable_code_size = 0;
115+
}
105116
}
106117

107118
void cj_add_u8(cj_ctx *ctx, uint8_t byte)
108119
{
109-
if (ctx->len >= ctx->size)
110-
grow_cj_ctx(ctx);
120+
if (ctx->len >= ctx->size) grow_cj_ctx(ctx);
111121

112122
ctx->mem[ctx->len++] = byte;
113123
}
@@ -132,8 +142,7 @@ void cj_add_u64(cj_ctx *ctx, uint64_t b8)
132142

133143
void cj_add_bytes(cj_ctx *ctx, uint8_t *bytes, uint64_t len)
134144
{
135-
for (uint64_t i = 0; i < len; i++)
136-
cj_add_u8(ctx, bytes[i]);
145+
for (uint64_t i = 0; i < len; i++) cj_add_u8(ctx, bytes[i]);
137146
}
138147

139148
cj_label cj_create_label(cj_ctx *ctx)
@@ -241,19 +250,12 @@ void cj_emit_branch(cj_ctx *ctx, uint32_t base_instr, cj_label label, uint8_t of
241250
void cj_emit_x86_rel(cj_ctx *ctx, const uint8_t *opcode, size_t opcode_len, uint8_t disp_width,
242251
cj_label label)
243252
{
244-
if (!ctx || !opcode || opcode_len == 0 || disp_width == 0)
245-
return;
253+
if (!ctx || !opcode || opcode_len == 0 || disp_width == 0) return;
246254

247-
for (size_t i = 0; i < opcode_len; i++)
248-
{
249-
cj_add_u8(ctx, opcode[i]);
250-
}
255+
for (size_t i = 0; i < opcode_len; i++) cj_add_u8(ctx, opcode[i]);
251256

252257
uint64_t disp_pos = ctx->len;
253-
for (uint8_t i = 0; i < disp_width; i++)
254-
{
255-
cj_add_u8(ctx, 0);
256-
}
258+
for (uint8_t i = 0; i < disp_width; i++) cj_add_u8(ctx, 0);
257259

258260
int label_known =
259261
(label.id >= 0 && label.id < ctx->num_labels && ctx->label_positions[label.id] != UINT64_MAX);
@@ -266,10 +268,7 @@ void cj_emit_x86_rel(cj_ctx *ctx, const uint8_t *opcode, size_t opcode_len, uint
266268
if (rel < min || rel > max)
267269
return;
268270

269-
for (uint8_t b = 0; b < disp_width; b++)
270-
{
271-
ctx->mem[disp_pos + b] = (uint8_t)((rel >> (8 * b)) & 0xFF);
272-
}
271+
for (uint8_t b = 0; b < disp_width; b++) ctx->mem[disp_pos + b] = (uint8_t)((rel >> (8 * b)) & 0xFF);
273272
}
274273
else
275274
{
@@ -288,16 +287,16 @@ void cj_emit_x86_rel(cj_ctx *ctx, const uint8_t *opcode, size_t opcode_len, uint
288287

289288
void *cj_resolve_label(const cj_ctx *ctx, cj_fn module, cj_label label)
290289
{
291-
if (!ctx || !module)
292-
return NULL;
290+
if (!ctx || !module) return NULL;
293291

294-
if (label.id < 0 || label.id >= ctx->num_labels)
295-
return NULL;
292+
if (label.id < 0 || label.id >= ctx->num_labels) return NULL;
296293

297294
uint64_t pos = ctx->label_positions[label.id];
298-
if (pos == UINT64_MAX)
299-
return NULL;
295+
if (pos == UINT64_MAX) return NULL;
296+
297+
if (!ctx->executable_base) return NULL;
298+
299+
if (pos >= ctx->executable_code_size) return NULL;
300300

301-
uint8_t *base = (uint8_t *)(void *)module;
302-
return (void *)(base + pos);
301+
return (void *)(ctx->executable_base + pos);
303302
}

src/ctx.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ typedef struct
4848
cj_fixup *fixups;
4949
int num_fixups;
5050
int fixup_capacity;
51+
52+
uint8_t *executable_base;
53+
uint8_t *executable_raw;
54+
size_t executable_size;
55+
uint64_t executable_code_size;
5156
} cj_ctx;
5257

5358
cj_ctx *create_cj_ctx(void);

0 commit comments

Comments
 (0)