From 07b352ff545427881c32d6dd8b03539025bbca80 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Thu, 2 Oct 2025 19:05:33 -0400 Subject: np: codegen: Implement initial stack based codegen This commit introduces the groundwork for the AMD64 PIIR backend. We will not follow through with the plans of using abstract syntax trees as they'll overly complicate the project. Instead we'll utilize a ring buffer to store the list of instructions and a stack machine for the IR code. Signed-off-by: Ian Moffett --- src/sys/np/codegen/gen_piir.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'src/sys/np/codegen') diff --git a/src/sys/np/codegen/gen_piir.c b/src/sys/np/codegen/gen_piir.c index e139465..e6623ac 100644 --- a/src/sys/np/codegen/gen_piir.c +++ b/src/sys/np/codegen/gen_piir.c @@ -66,12 +66,12 @@ piir_push(struct piir_stack *stack, ir_byte_t byte) return -EINVAL; } - if (stack->op_i >= PIIR_STACK_SIZE - 1) { + if (stack->op_head >= PIIR_STACK_SIZE - 1) { return -1; } spinlock_acquire(&stack->lock); - stack->opstore[stack->op_i++] = byte; + stack->opstore[stack->op_head++] = byte; spinlock_release(&stack->lock); return 0; } @@ -88,12 +88,22 @@ piir_pop(struct piir_stack *stack) return -EINVAL; } - if (stack->op_i == 0) { + /* Don't read past what we can */ + if (stack->op_tail == stack->op_head) { + stack->op_tail = 0; + stack->op_head = 0; return -1; } spinlock_acquire(&stack->lock); - byte = stack->opstore[--stack->op_i]; + + /* Grab a byte, reset pointers if empty */ + byte = stack->opstore[stack->op_tail++]; + if (stack->op_tail == stack->op_head) { + stack->op_tail = 0; + stack->op_head = 0; + } + spinlock_release(&stack->lock); return byte; } -- cgit v1.2.3