Skip to content

Commit

Permalink
fix align
Browse files Browse the repository at this point in the history
  • Loading branch information
ShawSumma committed Jun 18, 2024
1 parent 64ce796 commit d5d7146
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
1 change: 0 additions & 1 deletion in.lua

This file was deleted.

1 change: 1 addition & 0 deletions test/app/snake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,6 @@ draw = {}
draw.draw = grid
draw.frame = frame
draw.onkey = onkey
draw.vars = vars

app()
5 changes: 5 additions & 0 deletions vm/ast/comp.c
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,11 @@ static void vm_ast_comp_br(vm_ast_comp_t *comp, vm_ast_node_t node, vm_block_t *
}

static vm_arg_t vm_ast_comp_to(vm_ast_comp_t *comp, vm_ast_node_t node) {
// {
// vm_io_buffer_t *buf = vm_io_buffer_new();
// vm_ast_print_node(buf, 0, "ast = ", node);
// printf("%s\n", buf->buf);
// }
switch (node.type) {
case VM_AST_NODE_FORM: {
vm_ast_form_t form = node.value.form;
Expand Down
39 changes: 36 additions & 3 deletions vm/backend/backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,17 @@ static VM_INLINE vm_std_value_t vm_interp_mod(vm_std_value_t v1, vm_std_value_t
#include "binop.inc"
}


#if VM_ALIGNED
#define vm_interp_push(T, v_) ({\
T val = (v_);\
if (len + sizeof(T) >= alloc) { \
alloc += (len + sizeof(T)) * 2; \
code = vm_realloc(code, sizeof(uint8_t) * alloc); \
} \
memcpy(&code[len], &val, sizeof(T)); \
len += sizeof(T);\
})
#else
#define vm_interp_push_num(size_) ({ \
size_t size = (size_); \
if (len + size >= alloc) { \
Expand All @@ -168,6 +178,8 @@ static VM_INLINE vm_std_value_t vm_interp_mod(vm_std_value_t v1, vm_std_value_t
})

#define vm_interp_push(t, v) (*(t *)vm_interp_push_num(sizeof(t)) = (v))
#endif

#define vm_interp_push_op(v) vm_interp_push(void *, ptrs[v])

void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_block_t *block) {
Expand Down Expand Up @@ -651,11 +663,20 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_block_t *block) {
return code;
}

#if VM_ALIGNED
#define vm_run_repl_read(T) ({ \
T ptr; \
memcpy(&ptr, code, sizeof(T)); \
code += sizeof(T); \
ptr; \
})
#else
#define vm_run_repl_read(T) ({ \
T *ptr = (T*) code;\
code += sizeof(T);\
*ptr;\
})
#endif

#define vm_run_repl_lit() ({ \
vm_tag_t tag = vm_run_repl_read(vm_interp_tag_t); \
Expand Down Expand Up @@ -728,14 +749,14 @@ vm_std_value_t vm_run_repl(vm_t *vm, vm_block_t *block) {
[VM_OP_GET] = &&VM_OP_GET,
[VM_OP_CALL] = &&VM_OP_CALL,
};
vm_std_value_t *next_regs = &regs[block->nregs];

new_block:;
vm_std_value_t *next_regs = &regs[block->nregs];

uint8_t *code = block->code;
if (block->code == NULL) {
code = vm_interp_renumber_block(vm, &ptrs[0], block);
block->code = block->code;
block->code = code;
}

vm_run_repl_jump();
Expand All @@ -744,6 +765,12 @@ new_block:;
vm_std_value_t v1 = vm_run_repl_arg();
vm_std_value_t v2 = vm_run_repl_arg();
vm_std_value_t v3 = vm_run_repl_arg();
if (v1.tag != VM_TAG_TAB) {
return (vm_std_value_t) {
.tag = VM_TAG_ERROR,
.value.str = "can only index tables",
};
}
vm_table_set(v1.value.table, v2.value, v3.value, v2.tag, v3.tag);
vm_run_repl_jump();
}
Expand Down Expand Up @@ -1062,6 +1089,12 @@ new_block:;
.key_tag = v2.tag,
.key_val = v2.value,
};
if (v1.tag != VM_TAG_TAB) {
return (vm_std_value_t) {
.tag = VM_TAG_ERROR,
.value.str = "can only index tables",
};
}
vm_table_get_pair(v1.value.table, &pair);
vm_std_value_t v3 = (vm_std_value_t) {
.tag = pair.val_tag,
Expand Down

0 comments on commit d5d7146

Please sign in to comment.