Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
Signed-off-by: Shaw Summa <[email protected]>
  • Loading branch information
ShawSumma committed Oct 26, 2024
1 parent 8b3c059 commit 8e2a4b5
Show file tree
Hide file tree
Showing 28 changed files with 258 additions and 231 deletions.
5 changes: 3 additions & 2 deletions main/minivm.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#include "../vm/ast/comp.h"
#include "../vm/backend/backend.h"
#include "../vm/vm.h"
#include "../vm/ir.h"
#include "../vm/io.h"
#include "../vm/gc.h"
#include "../vm/std.h"
#include "../vm/lua/repl.h"
#include "../vm/lib.h"

#include <stdlib.h>

#if VM_USE_SPALL
#define SPALL_AUTO_IMPLEMENTATION
Expand Down
4 changes: 2 additions & 2 deletions main/primes.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import sympy
import math

max_width = 60
n = 53
ls = [1, 3, 7, 13, 29]
ls = [2, 5, 11, 23]

with open('vm/primes.inc', 'w') as f:
p32 = 2 ** 32
Expand All @@ -18,6 +17,7 @@
ml = max(map(len, map(str, ls)))
n = (max_width - 5) // (ml + 2)
f.write(f'// generated by: python3 main/primes.py\n\n')
f.write(f'#include <stdint.h>\n\n')
f.write(f'static uint32_t vm_primes_table[{len(ls)}] = ' + '{\n ')
for i, p in enumerate(ls):
s = str(p)
Expand Down
2 changes: 1 addition & 1 deletion vendor/tree-sitter
Submodule tree-sitter updated 225 files
1 change: 1 addition & 0 deletions vm/ast/build.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

#include <stdarg.h>
#include "build.h"

#define VM_MACRO_SELECT(_0, _1, _2, NAME, ...) NAME
Expand Down
6 changes: 5 additions & 1 deletion vm/ast/comp.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@

#include <stdarg.h>

#include "comp.h"
#include "ast.h"
#include "build.h"
#include "print.h"
#include "../ir.h"
#include "../gc.h"
#include "../errors.h"

Expand Down Expand Up @@ -116,10 +118,12 @@ static vm_ir_arg_t *vm_ast_args(size_t nargs, ...) {
}

static vm_ir_block_t *vm_ast_comp_new_block(vm_ast_comp_t *comp) {
static const int64_t no_code[1] = { 0 };
vm_ir_block_t *block = vm_malloc(sizeof(vm_ir_block_t));
*block = (vm_ir_block_t){
.id = (uint32_t)comp->vm->nblocks++,
.range = comp->range,
.code = &no_code[0],
// .nregs = VM_NREGS,
};
vm_gc_add(comp->vm, vm_obj_of_block(block));
Expand Down
287 changes: 151 additions & 136 deletions vm/backend/backend.c

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion vm/backend/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#define VM_HEADER_BACKEND_INTERP

#include "../vm.h"
#include "../ir.h"

vm_obj_t vm_run_main(vm_t *vm, vm_ir_block_t *entry);
vm_obj_t vm_run_repl(vm_t *vm, vm_ir_block_t *entry);
Expand Down
1 change: 1 addition & 0 deletions vm/errors.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

#include "lib.h"
#include "errors.h"
#include "io.h"

Expand Down
3 changes: 2 additions & 1 deletion vm/errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#if !defined(VM_HEADER_ERRORS)
#define VM_HEADER_ERRORS

#include "lib.h"
#include "vm.h"
#include <stdio.h>

struct vm_location_t;
struct vm_location_range_t;
Expand Down
6 changes: 5 additions & 1 deletion vm/gc.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

#include "gc.h"
#include "ir.h"
#include "lib.h"
#include <stddef.h>

struct vm_gc_objs_t;
struct vm_gc_t;
Expand Down Expand Up @@ -170,7 +172,9 @@ void vm_gc_sweep(vm_t *vm) {
}
vm_free(block->instrs);
vm_free(block->branch.args);
vm_free(block->code);
if (* (const int64_t *) block->code != 0) {
vm_free(block->code);
}
vm_free(block);
} else {
block->mark = false;
Expand Down
2 changes: 1 addition & 1 deletion vm/gc.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#if !defined(VM_HEADER_GC)
#define VM_HEADER_GC

#include "lib.h"
#include "vm.h"

void vm_gc_run(vm_t *vm, vm_obj_t *top);
void vm_gc_init(vm_t *vm);
Expand Down
4 changes: 3 additions & 1 deletion vm/io.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@

#include "io.h"
#include "ir.h"
#include "math.h"
#include "lib.h"

#include <stdio.h>

void vm_io_buffer_vformat(vm_io_buffer_t *buf, const char *fmt, va_list ap) {
if (buf->buf == NULL) {
Expand Down
6 changes: 4 additions & 2 deletions vm/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
#if !defined(VM_HEADER_STD_LIBS_IO)
#define VM_HEADER_STD_LIBS_IO

#include <stdarg.h>

#include "vm.h"

struct vm_io_debug_t;
typedef struct vm_io_debug_t vm_io_debug_t;

#include "obj.h"

struct vm_io_debug_t {
vm_io_debug_t *next;
vm_obj_t value;
Expand Down
3 changes: 2 additions & 1 deletion vm/ir.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

#include "ir.h"
#include "io.h"
#include "ir.h"
#include "lib.h"

void vm_block_realloc(vm_ir_block_t *block, vm_ir_instr_t instr) {
if (block->len + 4 >= block->alloc) {
Expand Down
7 changes: 3 additions & 4 deletions vm/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ typedef struct vm_ir_arg_t vm_ir_arg_t;
typedef struct vm_ir_branch_t vm_ir_branch_t;
typedef struct vm_ir_instr_t vm_ir_instr_t;

#include "lib.h"
#include "io.h"
#include "errors.h"

enum {
Expand Down Expand Up @@ -96,10 +94,11 @@ struct vm_ir_block_t {

vm_ir_branch_t branch;

void *code;
const void *code;

uint32_t nregs: 32;
uint32_t nregs: 31;
uint32_t alloc: 30;
bool done: 1;
bool isfunc: 1;
uint8_t mark: 1;
};
Expand Down
50 changes: 47 additions & 3 deletions vm/lib.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,50 @@

#include "lib.h"
#include "vm.h"

#if defined(__TINYC__)
void *end;
#if VM_MALLOC_MI
#include "../vendor/mimalloc/include/mimalloc.h"

void *vm_malloc(size_t x) {
return mi_malloc(x);
}

void *vm_calloc(size_t x) {
return mi_calloc(x, 1);
}

void *vm_realloc(void *x, size_t y) {
return mi_realloc(x, y);
}

void vm_free(const void *x) {
mi_free((void *) (x));
}

char *vm_strdup(const char *x) {
return mi_strdup(x);
}
#endif

#if VM_MALLOC_SYS
#include <stdlib.h>

void *vm_malloc(size_t x) {
return malloc(x);
}

void *vm_calloc(size_t x) {
return calloc(x, 1);
}

void *vm_realloc(void *x, size_t y) {
return realloc(x, y);
}

void vm_free(const void *x) {
free((void *) (x));
}

char *vm_strdup(const char *x) {
return strdup(x);
}
#endif
65 changes: 5 additions & 60 deletions vm/lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,68 +6,13 @@
#define _CRT_SECURE_NO_DEPRECATE
#endif

#if defined(__TINYC__)
#define __builtin_trap() exit(1)
#define __builtin_unreachable() exit(1)
#define __pure2 __attribute__((__const__))
#define __unused __attribute__((__unused__))
#define __used __attribute__((__used__))
#define __packed __attribute__((__packed__))
#define __aligned(x) __attribute__((__aligned__(x)))
#define __section(x) __attribute__((__section__(x)))
#endif

#define VM_ARCH_IS_AMD64 0
#define VM_ARCH_IS_ARM64 0
#define VM_ARCH_IS_OTHER 0

#if defined(__x86_64__) || defined(_M_AMD64)
#undef VM_ARCH_IS_AMD64
#define VM_ARCH_IS_AMD64 1
#elif defined(__aarch64__)
#undef VM_ARCH_IS_ARM64
#define VM_ARCH_IS_ARM64 1
#else
#undef VM_ARCH_IS_OTHER
#define VM_ARCH_IS_OTHER 1
#endif

#include <inttypes.h>
#include <math.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>

#include "vm.h"
void *vm_malloc(size_t size);
void *vm_calloc(size_t size);
void *vm_realloc(void *ptr, size_t size);
void vm_free(const void *ptr);
char *vm_strdup(const char *str);

#if 0
#define __builtin_trap() \
printf("file %s, line %zu\n", __FILE__, (size_t)__LINE__); \
exit(1);
#endif

#include "../vendor/mimalloc/include/mimalloc.h"

#if VM_MALLOC_MI
#define vm_malloc(x) mi_malloc(x)
#define vm_calloc(x) mi_calloc(x, 1)
#define vm_realloc(x, y) mi_realloc(x, y)
#define vm_free(x) mi_free((void *) (x))
#define vm_strdup(x) mi_strdup(x)
#endif

#if VM_MALLOC_SYS
#define vm_malloc(x) malloc(x)
#define vm_calloc(x) calloc(x, 1)
#define vm_realloc(x, y) realloc(x, y)
#define vm_free(x) free((void *) (x))
#define vm_strdup(x) strdup(x)
#endif

#endif
7 changes: 3 additions & 4 deletions vm/lua/repl.c
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@

#include <ctype.h>

#include "repl.h"
#include "../ir.h"
#include "../io.h"
#include "../vm.h"
#include "../obj.h"
#include "../ast/ast.h"
#include "../ast/comp.h"
#include "../backend/backend.h"

#include "../../vendor/tree-sitter/lib/include/tree_sitter/api.h"

#include "../../vendor/isocline/include/isocline.h"
#include "../../vendor/isocline/src/completions.h"

const TSLanguage *tree_sitter_lua(void);
vm_ast_node_t vm_lang_lua_parse(vm_t *vm, const char *str, const char *file);

void vm_lang_lua_repl_completer(ic_completion_env_t *cenv, const char *prefix) {
vm_t *vm = cenv->arg;
vm_t *vm = ic_completion_arg(cenv);
const char *last_word;
{
ptrdiff_t len = strlen(prefix);
Expand Down
2 changes: 1 addition & 1 deletion vm/lua/repl.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#if !defined(VM_HEADER_LANG_LUA_REPL)
#define VM_HEADER_LANG_LUA_REPL

#include "../lib.h"
#include "../vm.h"

void vm_lang_lua_repl(vm_t *vm);

Expand Down
2 changes: 1 addition & 1 deletion vm/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#if !defined(VM_HEADER_MATH)
#define VM_HEADER_MATH

#include "lib.h"
#include "vm.h"

bool vm_obj_unsafe_eq(vm_obj_t v1, vm_obj_t v2);
bool vm_obj_unsafe_lt(vm_obj_t v1, vm_obj_t v2);
Expand Down
4 changes: 4 additions & 0 deletions vm/obj.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@

#include <math.h>

#include "obj.h"
#include "gc.h"
#include "io.h"
#include "math.h"
#include "lib.h"

vm_obj_t vm_obj_of_string(vm_t *vm, const char *str) {
vm_obj_t ret = vm_obj_of_buffer(vm_io_buffer_from_str(str));
Expand Down
2 changes: 1 addition & 1 deletion vm/obj.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#if !defined(VM_HEADER_OBJ)
#define VM_HEADER_OBJ

#include "lib.h"
#include "vm.h"

vm_obj_t vm_obj_of_string(vm_t *vm, const char *str);
uint32_t vm_obj_hash(vm_obj_t value);
Expand Down
2 changes: 0 additions & 2 deletions vm/obj.inc
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

#include "vm.h"

static inline bool vm_obj_is_nil(vm_obj_t o) {
return nanbox_is_empty(o);
}
Expand Down
2 changes: 2 additions & 0 deletions vm/primes.inc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// generated by: python3 main/primes.py

#include <stdint.h>

static uint32_t vm_primes_table[32] = {
1, 3, 7, 13, 29,
53, 89, 157, 283, 491,
Expand Down
Loading

0 comments on commit 8e2a4b5

Please sign in to comment.