From 0d30ea292a78ff87fde57e2c63e563ae2d2fdfa9 Mon Sep 17 00:00:00 2001 From: zongor Date: Sun, 7 Dec 2025 15:29:49 -0800 Subject: [PATCH] Fix assembler, update tests, update roms, add back nogui mode for speed. --- Makefile | 23 +- bench/fib.lua | 2 +- bench/fib.pl | 2 +- bench/fib.py | 2 +- bench/fib.zl | 2 +- bench/run.sh | 6 +- bench/simple.lua | 2 +- bench/simple.pl | 2 +- bench/simple.py | 2 +- bench/simple.zl | 2 +- src/arch/linux/devices.c | 5 + src/arch/linux/main.c | 48 ++++- src/tools/assembler/assembler.c | 367 +++++++++++++++++--------------- src/tools/assembler/assembler.h | 5 +- src/tools/compiler/compiler.h | 4 + src/vm/vm.c | 21 +- test/add.asm.lisp | 26 --- test/add.rom | Bin 0 -> 151 bytes test/add.ul.ir | 6 +- test/fib.asm.lisp | 33 --- test/fib.rom | Bin 0 -> 187 bytes test/fib.ul.ir | 12 +- test/hello.asm.lisp | 19 -- test/hello.rom | Bin 0 -> 135 bytes test/hello.ul.ir | 4 +- test/loop.asm.lisp | 44 ---- test/loop.rom | Bin 0 -> 257 bytes test/loop.ul.ir | 10 +- test/malloc.asm.lisp | 26 --- test/malloc.rom | Bin 0 -> 187 bytes test/malloc.ul.ir | 9 +- test/paint-bw.asm.lisp | 147 ------------- test/paint-bw.rom | Bin 0 -> 550 bytes test/paint-bw.ul.ir | 101 +++++---- test/paint.asm.lisp | 261 ----------------------- test/paint.rom | Bin 0 -> 1161 bytes test/paint.ul.ir | 203 ++++++++++++++---- test/simple.asm.lisp | 22 -- test/simple.rom | Bin 0 -> 132 bytes test/simple.ul.ir | 6 +- test/window.asm.lisp | 71 ------ test/window.rom | Bin 0 -> 332 bytes test/window.ul.ir | 36 ++-- 43 files changed, 538 insertions(+), 993 deletions(-) delete mode 100644 test/add.asm.lisp create mode 100644 test/add.rom delete mode 100644 test/fib.asm.lisp create mode 100644 test/fib.rom delete mode 100644 test/hello.asm.lisp create mode 100644 test/hello.rom delete mode 100644 test/loop.asm.lisp create mode 100644 test/loop.rom delete mode 100644 test/malloc.asm.lisp create mode 100644 test/malloc.rom delete mode 100644 test/paint-bw.asm.lisp create mode 100644 test/paint-bw.rom delete mode 100644 test/paint.asm.lisp create mode 100644 test/paint.rom delete mode 100644 test/simple.asm.lisp create mode 100644 test/simple.rom delete mode 100644 test/window.asm.lisp create mode 100644 test/window.rom diff --git a/Makefile b/Makefile index 6aa77f9..359fd8e 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,9 @@ PLATFORM ?= linux BUILD_MODE ?= debug # 'debug' or 'release' +# Ensure BUILD_MODE is fixed before any conditionals +$(eval BUILD_MODE := $(or $(BUILD_MODE),debug)) + # --- DIRECTORIES --- SRC_DIR := src BUILD_DIR := build/$(PLATFORM) @@ -107,14 +110,12 @@ DEPS := $(VM_OBJS:.o=.d) $(PLATFORM_OBJ:.o=.d) # Default target all: $(TARGET) -# 'debug' target — just set BUILD_MODE and build -debug: BUILD_MODE=debug -debug: $(TARGET) - -# 'release' target — just set BUILD_MODE and build -release: BUILD_MODE=release -release: $(TARGET) +debug: + $(MAKE) BUILD_MODE=debug all +release: + $(MAKE) BUILD_MODE=release all + # --- COMPILE VM CORE (freestanding) --- $(BUILD_DIR)/vm/%.o: $(SRC_DIR)/vm/%.c @mkdir -p $(dir $@) @@ -155,11 +156,11 @@ clean-all: # --- TEST COMPILATION TARGET --- # Compiles all .asm.lisp test files to .rom using the debug VM executable -# Usage: make compile-tests PLATFORM=linux -compile-tests: $(BUILD_DIR)/undar-$(PLATFORM)$(TARGET_SUFFIX) +# Usage: make tests PLATFORM=linux +tests: $(BUILD_DIR)/undar-$(PLATFORM)$(TARGET_SUFFIX) @echo "Compiling test assembly files for $(PLATFORM)..." - @for f in ./test/*.asm.lisp; do \ - base=$$(basename "$$f" .asm.lisp); \ + @for f in ./test/*.ul.ir; do \ + base=$$(basename "$$f" .ul.ir); \ echo " [$$base] $$f -> ./test/$$base.rom"; \ $(BUILD_DIR)/undar-$(PLATFORM)$(TARGET_SUFFIX) "$$f" -o "./test/$$base.rom"; \ done diff --git a/bench/fib.lua b/bench/fib.lua index be357e9..d7ce19a 100644 --- a/bench/fib.lua +++ b/bench/fib.lua @@ -3,6 +3,6 @@ function fib(n) return fib(n-1) + fib(n-2) end -local result = fib(36) +local result = fib(35) print(result) diff --git a/bench/fib.pl b/bench/fib.pl index 776351e..e246993 100644 --- a/bench/fib.pl +++ b/bench/fib.pl @@ -7,6 +7,6 @@ sub fib { return fib($n-1) + fib($n-2); } -my $result = fib(36); +my $result = fib(35); print "$result\n"; diff --git a/bench/fib.py b/bench/fib.py index 4ab00e6..c4ea615 100644 --- a/bench/fib.py +++ b/bench/fib.py @@ -3,5 +3,5 @@ def fib(n): return n return fib(n-1) + fib(n-2) -result = fib(36) +result = fib(35) print(result) diff --git a/bench/fib.zl b/bench/fib.zl index dd93fc8..1b95c65 100644 --- a/bench/fib.zl +++ b/bench/fib.zl @@ -3,5 +3,5 @@ fn fib(n) { return fib(n - 2) + fib(n - 1); } -let result = fib(36); +let result = fib(35); print result; diff --git a/bench/run.sh b/bench/run.sh index ab75bcd..ac7deaf 100755 --- a/bench/run.sh +++ b/bench/run.sh @@ -41,9 +41,9 @@ print_section "zre ($FILENAME.t.ul)" echo "test input" | time ../build/old/zre -t "$FILENAME.ul" # Undâr Implementation (inline assembled) -print_section "undar ($FILENAME.asm.lisp)" -echo "test input" | time ../build/linux/undar-linux-release "../test/$FILENAME.asm.lisp" +print_section "undar ($FILENAME.ul.ir)" +echo "test input" | time ../build/linux/undar-linux-release -t "../test/$FILENAME.ul.ir" # Undâr Implementation (binary) print_section "undar ($FILENAME.rom)" -echo "test input" | time ../build/linux/undar-linux-release "../test/$FILENAME.rom" +echo "test input" | time ../build/linux/undar-linux-release -t "../test/$FILENAME.rom" diff --git a/bench/simple.lua b/bench/simple.lua index cb25114..55dc375 100644 --- a/bench/simple.lua +++ b/bench/simple.lua @@ -1 +1 @@ -print(tostring(1 + 2)) +print(tostring(1.0 + 2.0)) diff --git a/bench/simple.pl b/bench/simple.pl index 69a038c..2924ef9 100644 --- a/bench/simple.pl +++ b/bench/simple.pl @@ -1 +1 @@ -print((1 + 2) . "\n"); +print((1.0 + 2.0) . "\n"); diff --git a/bench/simple.py b/bench/simple.py index b02db97..07d8fb6 100644 --- a/bench/simple.py +++ b/bench/simple.py @@ -1 +1 @@ -print(str(1 + 2)) +print(str(1.0 + 2.0)) diff --git a/bench/simple.zl b/bench/simple.zl index e391eec..7549032 100644 --- a/bench/simple.zl +++ b/bench/simple.zl @@ -1,2 +1,2 @@ -let sum = 1 + 2; +let sum = 1.0 + 2.0; print sum; diff --git a/src/arch/linux/devices.c b/src/arch/linux/devices.c index 4d667b1..03aba8f 100644 --- a/src/arch/linux/devices.c +++ b/src/arch/linux/devices.c @@ -29,6 +29,11 @@ i32 console_read(void *data, u8 *buffer, u32 size) { i32 console_write(void *data, const u8 *buffer, u32 size) { USED(data); + + if (size > MEMORY_SIZE) { + return 0; + } + for (u32 i = 0; i < size; i++) { putchar(buffer[i]); } diff --git a/src/arch/linux/main.c b/src/arch/linux/main.c index eef3085..7a4453c 100644 --- a/src/arch/linux/main.c +++ b/src/arch/linux/main.c @@ -129,6 +129,38 @@ bool compileAndSave(const char *source_file, const char *output_file, VM *vm) { return true; } +#ifdef STATIC + #define SYMBOLS_COUNT 2048 + Symbol symbols[SYMBOLS_COUNT]; +#endif + +void symbol_table_init(SymbolTable *t) { + #ifdef STATIC + memset(symbols, 0, SYMBOLS_COUNT*sizeof(Symbol)); + t->symbols = symbols; + t->count = 0; + t->capacity = SYMBOLS_COUNT; + #else + t->symbols = calloc(16, sizeof(Symbol)); + t->count = 0; + t->capacity = 16; + #endif +} + +bool resize_or_check_size(SymbolTable *table) { + #ifdef STATIC + if (table->count >= table->capacity) { + return false; + } + #else + if (table->count >= table->capacity) { + table->capacity *= 2; + table->symbols = realloc(table->symbols, table->capacity * sizeof(Symbol)); + } + #endif + return true; +} + // Function to assemble and optionally save bool assembleAndSave(const char *source_file, const char *output_file, VM *vm) { FILE *f = fopen(source_file, "rb"); @@ -151,7 +183,12 @@ bool assembleAndSave(const char *source_file, const char *output_file, VM *vm) { source[read] = '\0'; fclose(f); - assemble(vm, source); + SymbolTable table = {0}; + symbol_table_init(&table); + assemble(vm, &table, source); +#ifndef STATIC + free(table.symbols); +#endif if (output_file) { if (!saveVM(output_file, vm)) { @@ -167,6 +204,7 @@ i32 main(i32 argc, char *argv[]) { bool dump_rom = false; char *input_file = nil; char *output_file = nil; + bool terminal_only_mode = false; bool is_rom = false; bool is_ir = false; @@ -174,6 +212,8 @@ i32 main(i32 argc, char *argv[]) { for (i32 i = 1; i < argc; i++) { if (strcmp(argv[i], "-o") == 0 || strcmp(argv[i], "--dump-rom") == 0) { dump_rom = true; + } else if (strcmp(argv[i], "-t") == 0 || strcmp(argv[i], "--term") == 0) { + terminal_only_mode = true; } else if (input_file == nil) { // This is the input file input_file = argv[i]; @@ -234,6 +274,11 @@ i32 main(i32 argc, char *argv[]) { vm_register_device(&vm, "/dev/term/0", "terminal", &console_data, &console_device_ops, 4); + if (terminal_only_mode) { + while (step_vm(&vm)); + return 0; + } + if (SDL_Init(SDL_INIT_VIDEO) < 0) { printf("SDL initialization failed: %s\n", SDL_GetError()); return 1; @@ -326,6 +371,7 @@ i32 main(i32 argc, char *argv[]) { i32 cycles_this_frame = 0; i32 max_cycles_per_frame = 100; // Adjust this value while (cycles_this_frame < max_cycles_per_frame) { + //printf("code[%d] = %s\n", vm.pc, opcode_to_string(vm.code[vm.pc])); if (!step_vm(&vm)) { running = false; break; diff --git a/src/tools/assembler/assembler.c b/src/tools/assembler/assembler.c index 0ae0285..d69c183 100644 --- a/src/tools/assembler/assembler.c +++ b/src/tools/assembler/assembler.c @@ -1,9 +1,12 @@ -#include "assembler.h" #include "../../vm/common.h" #include "../../vm/fixed.h" #include "../../vm/libc.h" #include "../../vm/opcodes.h" + +#include "assembler.h" #include "lexer.h" + +/* FIXME: remove these and replace with libc.h instead */ #include #include #include @@ -12,124 +15,123 @@ const char *opcode_to_string(Opcode op) { static const char *names[] = { [OP_EXIT] = "exit", [OP_JMP] = "jump", - [OP_JMPF] = "jump-if-flag", + [OP_JMPF] = "jump_if_flag", [OP_CALL] = "call", [OP_RETURN] = "return", - /* Immediate loads (only 32-bit variant needed) */ - [OP_LOAD_IMM] = "load-immediate", + [OP_LOAD_IMM] = "load_immediate", - /* Register-indirect loads */ - [OP_LOAD_IND_8] = "load-indirect-8", - [OP_LOAD_IND_16] = "load-indirect-16", - [OP_LOAD_IND_32] = "load-indirect-32", + /* Register_indirect loads */ + [OP_LOAD_IND_8] = "load_indirect_8", + [OP_LOAD_IND_16] = "load_indirect_16", + [OP_LOAD_IND_32] = "load_indirect_32", /* Absolute address loads */ - [OP_LOAD_ABS_8] = "load-absolute-8", - [OP_LOAD_ABS_16] = "load-absolute-16", - [OP_LOAD_ABS_32] = "load-absolute-32", + [OP_LOAD_ABS_8] = "load_absolute_8", + [OP_LOAD_ABS_16] = "load_absolute_16", + [OP_LOAD_ABS_32] = "load_absolute_32", /* Base+offset loads */ - [OP_LOAD_OFF_8] = "load-offset-8", - [OP_LOAD_OFF_16] = "load-offset-16", - [OP_LOAD_OFF_32] = "load-offset-32", + [OP_LOAD_OFF_8] = "load_offset_8", + [OP_LOAD_OFF_16] = "load_offset_16", + [OP_LOAD_OFF_32] = "load_offset_32", /* Absolute address stores */ - [OP_STORE_ABS_8] = "store-absolute-8", - [OP_STORE_ABS_16] = "store-absolute-16", - [OP_STORE_ABS_32] = "store-absolute-32", + [OP_STORE_ABS_8] = "store_absolute_8", + [OP_STORE_ABS_16] = "store_absolute_16", + [OP_STORE_ABS_32] = "store_absolute_32", - /* Register-indirect stores */ - [OP_STORE_IND_8] = "store-indirect-8", - [OP_STORE_IND_16] = "store-indirect-16", - [OP_STORE_IND_32] = "store-indirect-32", + /* Register_indirect stores */ + [OP_STORE_IND_8] = "store_indirect_8", + [OP_STORE_IND_16] = "store_indirect_16", + [OP_STORE_IND_32] = "store_indirect_32", /* Base+offset stores */ - [OP_STORE_OFF_8] = "store-offset-8", - [OP_STORE_OFF_16] = "store-offset-16", - [OP_STORE_OFF_32] = "store-offset-32", + [OP_STORE_OFF_8] = "store_offset_8", + [OP_STORE_OFF_16] = "store_offset_16", + [OP_STORE_OFF_32] = "store_offset_32", /* Memory operations */ [OP_MALLOC] = "malloc", - [OP_MEMSET_8] = "memset-8", - [OP_MEMSET_16] = "memset-16", - [OP_MEMSET_32] = "memset-32", + [OP_MEMSET_8] = "memset_8", + [OP_MEMSET_16] = "memset_16", + [OP_MEMSET_32] = "memset_32", /* Register operations */ - [OP_REG_MOV] = "register-move", + [OP_REG_MOV] = "register_move", [OP_SYSCALL] = "syscall", /* Bit operations */ - [OP_BIT_SHIFT_LEFT] = "bit-shift-left", - [OP_BIT_SHIFT_RIGHT] = "bit-shift-right", - [OP_BIT_SHIFT_R_EXT] = "bit-shift-re", - [OP_BAND] = "bit-and", - [OP_BOR] = "bit-or", - [OP_BXOR] = "bit-xor", + [OP_BIT_SHIFT_LEFT] = "bit_shift_left", + [OP_BIT_SHIFT_RIGHT] = "bit_shift_right", + [OP_BIT_SHIFT_R_EXT] = "bit_shift_re", + [OP_BAND] = "bit_and", + [OP_BOR] = "bit_or", + [OP_BXOR] = "bit_xor", /* Integer arithmetic */ - [OP_ADD_INT] = "add-int", - [OP_SUB_INT] = "sub-int", - [OP_MUL_INT] = "mul-int", - [OP_DIV_INT] = "div-int", + [OP_ADD_INT] = "add_int", + [OP_SUB_INT] = "sub_int", + [OP_MUL_INT] = "mul_int", + [OP_DIV_INT] = "div_int", /* Natural number arithmetic */ - [OP_ADD_NAT] = "add-nat", - [OP_SUB_NAT] = "sub-nat", - [OP_MUL_NAT] = "mul-nat", - [OP_DIV_NAT] = "div-nat", + [OP_ADD_NAT] = "add_nat", + [OP_SUB_NAT] = "sub_nat", + [OP_MUL_NAT] = "mul_nat", + [OP_DIV_NAT] = "div_nat", /* Floating point operations */ - [OP_ADD_REAL] = "add-real", - [OP_SUB_REAL] = "sub-real", - [OP_MUL_REAL] = "mul-real", - [OP_DIV_REAL] = "div-real", + [OP_ADD_REAL] = "add_real", + [OP_SUB_REAL] = "sub_real", + [OP_MUL_REAL] = "mul_real", + [OP_DIV_REAL] = "div_real", /* Type conversions */ - [OP_INT_TO_REAL] = "int-to-real", - [OP_NAT_TO_REAL] = "nat-to-real", - [OP_REAL_TO_INT] = "real-to-int", - [OP_REAL_TO_NAT] = "real-to-nat", + [OP_INT_TO_REAL] = "int_to_real", + [OP_NAT_TO_REAL] = "nat_to_real", + [OP_REAL_TO_INT] = "real_to_int", + [OP_REAL_TO_NAT] = "real_to_nat", /* Integer comparisons */ - [OP_JEQ_INT] = "jump-eq-int", - [OP_JNEQ_INT] = "jump-neq-int", - [OP_JGT_INT] = "jump-gt-int", - [OP_JLT_INT] = "jump-lt-int", - [OP_JLE_INT] = "jump-le-int", - [OP_JGE_INT] = "jump-ge-int", + [OP_JEQ_INT] = "jump_eq_int", + [OP_JNEQ_INT] = "jump_neq_int", + [OP_JGT_INT] = "jump_gt_int", + [OP_JLT_INT] = "jump_lt_int", + [OP_JLE_INT] = "jump_le_int", + [OP_JGE_INT] = "jump_ge_int", /* Natural number comparisons */ - [OP_JEQ_NAT] = "jump-eq-nat", - [OP_JNEQ_NAT] = "jump-neq-nat", - [OP_JGT_NAT] = "jump-gt-nat", - [OP_JLT_NAT] = "jump-lt-nat", - [OP_JLE_NAT] = "jump-le-nat", - [OP_JGE_NAT] = "jump-ge-nat", + [OP_JEQ_NAT] = "jump_eq_nat", + [OP_JNEQ_NAT] = "jump_neq_nat", + [OP_JGT_NAT] = "jump_gt_nat", + [OP_JLT_NAT] = "jump_lt_nat", + [OP_JLE_NAT] = "jump_le_nat", + [OP_JGE_NAT] = "jump_ge_nat", /* Floating point comparisons */ - [OP_JEQ_REAL] = "jump-eq-real", - [OP_JNEQ_REAL] = "jump-neq-real", - [OP_JGE_REAL] = "jump-ge-real", - [OP_JGT_REAL] = "jump-gt-real", - [OP_JLT_REAL] = "jump-lt-real", - [OP_JLE_REAL] = "jump-le-real", + [OP_JEQ_REAL] = "jump_eq_real", + [OP_JNEQ_REAL] = "jump_neq_real", + [OP_JGE_REAL] = "jump_ge_real", + [OP_JGT_REAL] = "jump_gt_real", + [OP_JLT_REAL] = "jump_lt_real", + [OP_JLE_REAL] = "jump_le_real", /* String operations */ - [OP_STRLEN] = "string-length", - [OP_STREQ] = "string-eq", - [OP_STRCAT] = "string-concat", - [OP_STR_GET_CHAR] = "string-get-char", - [OP_STR_FIND_CHAR] = "string-find-char", - [OP_STR_SLICE] = "string-slice", + [OP_STRLEN] = "string_length", + [OP_STREQ] = "string_eq", + [OP_STRCAT] = "string_concat", + [OP_STR_GET_CHAR] = "string_get_char", + [OP_STR_FIND_CHAR] = "string_find_char", + [OP_STR_SLICE] = "string_slice", /* String conversions */ - [OP_INT_TO_STRING] = "int-to-string", - [OP_NAT_TO_STRING] = "nat-to-string", - [OP_REAL_TO_STRING] = "real-to-string", - [OP_STRING_TO_INT] = "string-to-int", - [OP_STRING_TO_NAT] = "string-to-nat", - [OP_STRING_TO_REAL] = "string-to-real"}; + [OP_INT_TO_STRING] = "int_to_string", + [OP_NAT_TO_STRING] = "nat_to_string", + [OP_REAL_TO_STRING] = "real_to_string", + [OP_STRING_TO_INT] = "string_to_int", + [OP_STRING_TO_NAT] = "string_to_nat", + [OP_STRING_TO_REAL] = "string_to_real"}; if (op < 0 || op >= (int)(sizeof(names) / sizeof(names[0]))) { return ""; @@ -139,27 +141,28 @@ const char *opcode_to_string(Opcode op) { return name ? name : ""; } + void emit_op(VM *vm, u8 byte) { - printf("vm->code[%d] = %s\n", vm->cp, opcode_to_string(byte)); +#ifdef DEBUG_PRINT + printf("code[%d] = %s\n", vm->cp, opcode_to_string(byte)); +#endif vm->code[vm->cp] = byte; } void emit_byte(VM *vm, u8 byte) { - printf("vm->code[%d] = %d\n", vm->cp, byte); +#ifdef DEBUG_PRINT + printf("code[%d] = %d\n", vm->cp, byte); +#endif vm->code[vm->cp] = byte; } void emit_u32(VM *vm, u32 value) { - printf("vm->code[%d..%d] = %d\n", vm->cp, vm->cp + 3, value); +#ifdef DEBUG_PRINT + printf("code[%d..%d] = %d\n", vm->cp, vm->cp + 3, value); +#endif write_u32(vm, code, vm->cp, value); } -void symbol_table_init(SymbolTable *table) { - table->symbols = calloc(16, sizeof(Symbol)); - table->count = 0; - table->capacity = 16; -} - Symbol *symbol_table_lookup(SymbolTable *table, const char *name, u32 length) { for (u32 i = 0; i < table->count; i++) { if (table->symbols[i].name_length == length) { @@ -182,19 +185,23 @@ u32 symbol_table_add(SymbolTable *table, Symbol s) { exit(1); } - if (table->count >= table->capacity) { - table->capacity *= 2; - table->symbols = realloc(table->symbols, table->capacity * sizeof(Symbol)); + if (!resize_or_check_size(table)) { + fprintf(stderr, + "Error: Symbol table is out of memory! This is likely because you built this in static mode." + "if you built using malloc, that means your computer is out of memory. Close a few tabs in your web browser and try again." + "Count was %d, while capacity was %d\n", + table->count, table->capacity); + exit(1); } - +#ifdef DEBUG_PRINT if (s.scope == VAR) { - // ignore for now - // printf("$%d = %s\n", s.ref, s.name); + printf("$%d = %s\n", s.ref, s.name); } else if (s.scope == GLOBAL) { printf("memory[%d] = %s\n", s.ref, s.name); } else { printf("code[%d] = %s\n", s.ref, s.name); } +#endif table->symbols[table->count] = s; u32 index = table->count; @@ -232,7 +239,7 @@ u32 get_ptr(Token token, SymbolTable *st) { return out; } - fprintf(stderr, "Error: Not a register or symbol '%.*s'\n", token.length, + fprintf(stderr, "Error: Not a pointer or symbol '%.*s'\n", token.length, token.start); exit(1); } @@ -595,6 +602,7 @@ void define_branch(VM *vm, SymbolTable *st) { } memcpy(s.name, name.start, name.length); s.name_length = name.length; + s.name[name.length] = '\0'; s.ref = vm->cp; symbol_table_add(st, s); @@ -625,9 +633,6 @@ int get_instruction_byte_size(const char *opname) { strcmp(opname, "real_to_nat") == 0 || strcmp(opname, "nat_to_int") == 0 || strcmp(opname, "int_to_nat") == 0 || strcmp(opname, "string_length") == 0 || - strcmp(opname, "store_absolute_32") == 0 || - strcmp(opname, "store_absolute_8") == 0 || - strcmp(opname, "store_absolute_16") == 0 || strcmp(opname, "memset") == 0 || strcmp(opname, "memset") == 0 || strcmp(opname, "memset_8") == 0 || strcmp(opname, "memset_16") == 0 || strcmp(opname, "register_move") == 0 || strcmp(opname, "malloc") == 0) { @@ -660,7 +665,10 @@ int get_instruction_byte_size(const char *opname) { strcmp(opname, "load_immediate") == 0 || strcmp(opname, "load_address") == 0 || strcmp(opname, "load_absolute_16") == 0 || - strcmp(opname, "load_absolute_8") == 0) { + strcmp(opname, "load_absolute_8") == 0 || + strcmp(opname, "store_absolute_32") == 0 || + strcmp(opname, "store_absolute_8") == 0 || + strcmp(opname, "store_absolute_16") == 0) { return 6; } @@ -696,12 +704,15 @@ int get_instruction_byte_size(const char *opname) { exit(-1); } -#define FAKE_OP(op) \ - } \ - else if (strleq(token.start, op, token.length)) { \ - while (token.type != TOKEN_SEMICOLON) \ - token = next_token(); \ - vm->cp += get_instruction_byte_size(op); +#define FAKE_OP(op) \ + } else if (strleq(token.start, op, token.length)) { \ + do { \ + while (token.type != TOKEN_SEMICOLON) { \ + token = next_token(); \ + } \ + /*printf("code[%d]=%s\n %d + %d = %d\n", vm->cp, op, get_instruction_byte_size(op), vm->cp, vm->cp + get_instruction_byte_size(op)); */\ + vm->cp += get_instruction_byte_size(op); \ + } while(0); /** * Build the symbol table and calculate the types/size/offsets of all values. @@ -760,6 +771,9 @@ void build_symbol_table(VM *vm, char *source, SymbolTable *st) { continue; } + #ifdef DEBUG_PRINT + printf("-- %.*s --\n", token.length, token.start); + #endif if (token.type == TOKEN_IDENTIFIER) { // check to see if it is an opcode first if (strleq(token.start, "exit", token.length)) { @@ -769,6 +783,10 @@ void build_symbol_table(VM *vm, char *source, SymbolTable *st) { next_token(); vm->cp += 4; + #ifdef DEBUG_PRINT + printf("code[%d] = exit\n", vm->cp); + #endif + next_token_is(TOKEN_SEMICOLON); } else if (strleq(token.start, "call", token.length)) { @@ -777,27 +795,27 @@ void build_symbol_table(VM *vm, char *source, SymbolTable *st) { next_token_is(TOKEN_IDENTIFIER); vm->cp += 4; - bool has_return = false; - u8 arg_count = 0; vm->cp++; - - Token next = next_token(); - while (next.type != TOKEN_SEMICOLON) { - if (next.type != TOKEN_ARROW_RIGHT) { + Token next = next_token_is(TOKEN_LPAREN); + next = next_token(); + while (next.type != TOKEN_RPAREN) { get_reg(next, st); vm->cp++; - arg_count++; - } else { - has_return = true; - arg_count--; // is a return not an arg - } - next = next_token(); + next = next_token(); } - if (!has_return) { + next = next_token(); + if (next.type == TOKEN_SEMICOLON) { + vm->cp++; + } else { + next = next_token(); + get_reg(next, st); vm->cp++; - continue; } + #ifdef DEBUG_PRINT + printf("code[%d] = call\n", vm->cp); + #endif + continue; } else if (strleq(token.start, "syscall", token.length)) { vm->cp++; @@ -807,13 +825,14 @@ void build_symbol_table(VM *vm, char *source, SymbolTable *st) { next = next_token(); while (next.type != TOKEN_SEMICOLON) { - if (next.type != TOKEN_ARROW_RIGHT) { - get_reg(next, st); - vm->cp++; - } + get_reg(next, st); + vm->cp++; next = next_token(); } - + #ifdef DEBUG_PRINT + printf("code[%d] = syscall\n", vm->cp); + #endif + continue; FAKE_OP("load_immediate") FAKE_OP("load_address") FAKE_OP("malloc") @@ -982,6 +1001,9 @@ void emit_bytecode(VM *vm, char *source, SymbolTable *st) { continue; } + #ifdef DEBUG_PRINT + printf("-- %.*s --\n", token.length, token.start); + #endif if (token.type == TOKEN_IDENTIFIER) { // check to see if it is an opcode first if (strleq(token.start, "exit", token.length)) { @@ -1005,35 +1027,36 @@ void emit_bytecode(VM *vm, char *source, SymbolTable *st) { emit_u32(vm, ptr); vm->cp += 4; - bool has_return = false; u8 arg_count = 0; u32 arg_pos = vm->cp++; - printf("vm->code[%d] = ?\n", arg_pos); - - Token next = next_token(); - while (next.type != TOKEN_SEMICOLON) { - if (next.type != TOKEN_ARROW_RIGHT) { + Token next = next_token_is(TOKEN_LPAREN); + next = next_token(); + while (next.type != TOKEN_RPAREN) { u8 arg = get_reg(next, st); emit_byte(vm, arg); vm->cp++; arg_count++; - } else { - has_return = true; - arg_count--; // is a return not an arg - } - next = next_token(); + next = next_token(); } - /* patch number of args */ vm->code[arg_pos] = arg_count; - - printf("^vm->code[%d] = %d\n", arg_pos, arg_count); - - if (!has_return) { - vm->cp++; + + #ifdef DEBUG_PRINT + printf("^code[%d] = %d\n", arg_pos, arg_count); + #endif + + next = next_token(); + if (next.type == TOKEN_SEMICOLON) { emit_byte(vm, 255); - continue; + vm->cp++; + } else { + next = next_token(); + u8 arg = get_reg(next, st); + emit_byte(vm, arg); + vm->cp++; } + + continue; } else if (strleq(token.start, "syscall", token.length)) { emit_op(vm, OP_SYSCALL); @@ -1062,15 +1085,20 @@ void emit_bytecode(VM *vm, char *source, SymbolTable *st) { vm->cp += 4; next = next_token(); - while (next.type != TOKEN_SEMICOLON) { - if (next.type != TOKEN_ARROW_RIGHT) { - u8 arg = get_reg(next, st); - emit_byte(vm, arg); - vm->cp++; - } + while (next.type != TOKEN_SEMICOLON && next.type != TOKEN_ARROW_RIGHT) { + u8 arg =get_reg(next, st); + emit_byte(vm, arg); + vm->cp++; next = next_token(); } + if (next.type == TOKEN_ARROW_RIGHT) { + next = next_token(); + u8 arg = get_reg(next, st); + emit_byte(vm, arg); + vm->cp++; + } + } else if (strleq(token.start, "load_immediate", token.length)) { emit_op(vm, OP_LOAD_IMM); @@ -1168,6 +1196,8 @@ void emit_bytecode(VM *vm, char *source, SymbolTable *st) { emit_byte(vm, arg); vm->cp++; + next_token_is(TOKEN_ARROW_RIGHT); + reg = next_token(); arg = get_reg(reg, st); emit_byte(vm, arg); @@ -1188,6 +1218,8 @@ void emit_bytecode(VM *vm, char *source, SymbolTable *st) { emit_byte(vm, arg); vm->cp++; + next_token_is(TOKEN_ARROW_RIGHT); + reg = next_token(); arg = get_reg(reg, st); emit_byte(vm, arg); @@ -1208,6 +1240,8 @@ void emit_bytecode(VM *vm, char *source, SymbolTable *st) { emit_byte(vm, arg); vm->cp++; + next_token_is(TOKEN_ARROW_RIGHT); + reg = next_token(); arg = get_reg(reg, st); emit_byte(vm, arg); @@ -1444,10 +1478,10 @@ void emit_bytecode(VM *vm, char *source, SymbolTable *st) { next_token_is(TOKEN_ARROW_RIGHT); - Token id = next_token(); - u32 ptr = get_ptr(id, st); - emit_u32(vm, ptr); - vm->cp += 4; + reg = next_token(); + arg = get_reg(reg, st); + emit_byte(vm, arg); + vm->cp++; next_token_is(TOKEN_SEMICOLON); } else if (strleq(token.start, "store_indirect_16", token.length)) { @@ -1461,10 +1495,10 @@ void emit_bytecode(VM *vm, char *source, SymbolTable *st) { next_token_is(TOKEN_ARROW_RIGHT); - Token id = next_token(); - u32 ptr = get_ptr(id, st); - emit_u32(vm, ptr); - vm->cp += 4; + reg = next_token(); + arg = get_reg(reg, st); + emit_byte(vm, arg); + vm->cp++; next_token_is(TOKEN_SEMICOLON); } else if (strleq(token.start, "store_indirect_32", token.length)) { @@ -1478,10 +1512,10 @@ void emit_bytecode(VM *vm, char *source, SymbolTable *st) { next_token_is(TOKEN_ARROW_RIGHT); - Token id = next_token(); - u32 ptr = get_ptr(id, st); - emit_u32(vm, ptr); - vm->cp += 4; + reg = next_token(); + arg = get_reg(reg, st); + emit_byte(vm, arg); + vm->cp++; next_token_is(TOKEN_SEMICOLON); } else if (strleq(token.start, "store_offset_8", token.length)) { @@ -2383,11 +2417,8 @@ void emit_bytecode(VM *vm, char *source, SymbolTable *st) { /** * Emit bytecode to the VM from the source string. */ -void assemble(VM *vm, char *source) { - SymbolTable st = {0}; - symbol_table_init(&st); - build_symbol_table(vm, source, &st); - vm->cp = 0; /* actuall start emitting code */ - emit_bytecode(vm, source, &st); - free(st.symbols); +void assemble(VM *vm, SymbolTable *st, char *source) { + build_symbol_table(vm, source, st); + vm->cp = 0; /* actually start emitting code */ + emit_bytecode(vm, source, st); } diff --git a/src/tools/assembler/assembler.h b/src/tools/assembler/assembler.h index 107f2be..106aee5 100644 --- a/src/tools/assembler/assembler.h +++ b/src/tools/assembler/assembler.h @@ -43,6 +43,9 @@ struct symbol_tab_s { u32 capacity; }; -void assemble(VM *vm, char *source); +void assemble(VM *vm, SymbolTable *st, char *source); +extern bool resize_or_check_size(SymbolTable *table);/* implement this in arch/ not here */ + +const char *opcode_to_string(Opcode op); #endif diff --git a/src/tools/compiler/compiler.h b/src/tools/compiler/compiler.h index e223513..778a5e3 100644 --- a/src/tools/compiler/compiler.h +++ b/src/tools/compiler/compiler.h @@ -103,6 +103,10 @@ struct names_tab_s { u32 capacity; }; +/** + * FIXME: + * Symbols need to be inside a scope so we can have duplicates + */ struct symbol_tab_s { Symbol *symbols; u32 count; diff --git a/src/vm/vm.c b/src/vm/vm.c index 45c78af..9ae7ac0 100644 --- a/src/vm/vm.c +++ b/src/vm/vm.c @@ -349,37 +349,34 @@ bool step_vm(VM *vm) { } case OP_STORE_ABS_32: { u32 v, ptr; - u8 dest, src1; + u8 src1; src1 = read_u8(vm, code, vm->pc); vm->pc++; - dest = read_u8(vm, code, vm->pc); - vm->pc++; + ptr = read_u32(vm, code, vm->pc); + vm->pc += 4; v = frame->locals[src1]; - ptr = frame->locals[dest]; write_u32(vm, memory, ptr, v); return true; } case OP_STORE_ABS_16: { u32 v, ptr; - u8 dest, src1; + u8 src1; src1 = read_u8(vm, code, vm->pc); vm->pc++; - dest = read_u8(vm, code, vm->pc); - vm->pc++; + ptr = read_u32(vm, code, vm->pc); + vm->pc += 4; v = frame->locals[src1]; - ptr = frame->locals[dest]; write_u16(vm, memory, ptr, v); return true; } case OP_STORE_ABS_8: { u32 v, ptr; - u8 dest, src1; + u8 src1; src1 = read_u8(vm, code, vm->pc); vm->pc++; - dest = read_u8(vm, code, vm->pc); - vm->pc++; + ptr = read_u32(vm, code, vm->pc); + vm->pc += 4; v = frame->locals[src1]; - ptr = frame->locals[dest]; write_u8(vm, memory, ptr, v); return true; } diff --git a/test/add.asm.lisp b/test/add.asm.lisp deleted file mode 100644 index 1c783c7..0000000 --- a/test/add.asm.lisp +++ /dev/null @@ -1,26 +0,0 @@ -((code - (label main - (load-immediate $0 1) - (load-immediate $1 1) - (call &add ($0 $1) $2) - (int-to-string $3 $2) - (call &pln ($3) nil) - (exit 0)) - - (label add - (add-int $2 $1 $0) - (return $2)) - - (label pln - (load-immediate $1 &terminal-namespace) ; get terminal device - (load-immediate $11 0) - (syscall OPEN $1 $1 $11) - (string-length $2 $0) - (syscall WRITE $1 $0 $2) - (load-immediate $3 &new-line) - (string-length $4 $3) - (syscall WRITE $1 $3 $4) - (return nil))) -(data - (label terminal-namespace "/dev/term/0") - (label new-line "\n"))) diff --git a/test/add.rom b/test/add.rom new file mode 100644 index 0000000000000000000000000000000000000000..7ced01fc9fae8f7f892509e8edb8dd33468d3f47 GIT binary patch literal 151 zcmXwyp$>pB3`DQ(h8PBgAOwd(j4zqO;y_{u;rHPNS(CoLUYZmDR(7eO2xn;X5;{U; zBo(5InoK#dhXi_NsxGVlSeei(F9~W!6{u@CEt&fRbNbXw; $0; load_absolute_32 y -> $1; - call add $0 $1 -> $2; + call add ($0 $1) -> $2; int_to_string $2 -> $3; - call pln $3; + call pln ($3); exit 0; function add (int a $0, int b $1) @@ -26,7 +26,7 @@ function pln (str message $0) load_immediate 0 -> mode; load_address terminal_namespace -> term_ns; - syscall OPEN term_ns mode -> term; + syscall OPEN term_ns mode term; string_length message -> msg_length; syscall WRITE term message msg_length; load_address new_line -> nl; diff --git a/test/fib.asm.lisp b/test/fib.asm.lisp deleted file mode 100644 index 6004071..0000000 --- a/test/fib.asm.lisp +++ /dev/null @@ -1,33 +0,0 @@ -((code - (label main - (load-immediate $0 35) - (call &fib ($0) $0) - (int-to-string $1 $0) - (call &pln ($1) nil) - (exit 0)) - (label fib - (load-immediate $1 2) - (jump-lt-int &base-case $0 $1) - (load-immediate $3 2) - (sub-int $4 $0 $3) - (call &fib ($4) $5) - (load-immediate $3 1) - (sub-int $4 $0 $3) - (call &fib ($4) $6) - (add-int $7 $6 $5) - (return $7) - (label base-case - (return $0))) - (label pln - (load-immediate $1 &terminal-namespace) ; get terminal device - (load-immediate $11 0) - (syscall OPEN $1 $1 $11) - (load-immediate $3 &new-line) - (string-length $2 $0) - (syscall WRITE $1 $0 $2) - (string-length $4 $3) - (syscall WRITE $1 $3 $4) - (return nil))) -(data - (label terminal-namespace "/dev/term/0") - (label new-line "\n"))) diff --git a/test/fib.rom b/test/fib.rom new file mode 100644 index 0000000000000000000000000000000000000000..f09af70d3e9f81ed2e30cdce403d2b250de2c6f6 GIT binary patch literal 187 zcmYjLI}XB74095{R|ys-CSc(Jj~j4-*{>v# zX9+v$05_ODL~WD}y2SM^ZnKCZf=ir*S;4^ UppV;h+H?4}&+vY=i@w1I6PA$)*8l(j literal 0 HcmV?d00001 diff --git a/test/fib.ul.ir b/test/fib.ul.ir index 36f4de0..caf8152 100644 --- a/test/fib.ul.ir +++ b/test/fib.ul.ir @@ -4,10 +4,10 @@ global str new_line = "\n"; function main () int str_n $1; - load_immediate 36 -> $0; - call fib $0 -> $0; + load_immediate 35 -> $0; + call fib ($0) -> $0; int_to_string $0 -> str_n; - call pln str_n; + call pln (str_n); exit 0; function fib (int n $0) @@ -17,11 +17,11 @@ function fib (int n $0) load_immediate 2 -> $3; sub_int n $3 -> $4; - call fib $4 -> $5; + call fib ($4) -> $5; load_immediate 1 -> $3; sub_int n $3 -> $4; - call fib $4 -> $6; + call fib ($4) -> $6; add_int $6 $5 -> $7; return $7; @@ -39,7 +39,7 @@ function pln (str message $0) load_immediate 0 -> mode; load_address terminal_namespace -> term_ns; - syscall OPEN term_ns mode -> term; + syscall OPEN term_ns mode term; string_length message -> msg_length; syscall WRITE term message msg_length; load_address new_line -> nl; diff --git a/test/hello.asm.lisp b/test/hello.asm.lisp deleted file mode 100644 index f14933f..0000000 --- a/test/hello.asm.lisp +++ /dev/null @@ -1,19 +0,0 @@ -((code - (label main - (load-immediate $1 &hello-str) ; load hello string ptr - (call &pln ($1) nil) - (exit 0)) ; done - (label pln - (load-immediate $1 &terminal-namespace) ; get terminal device - (load-immediate $11 0) - (syscall OPEN $1 $1 $11) - (load-immediate $3 &new-line) - (string-length $2 $0) - (syscall WRITE $1 $0 $2) - (string-length $4 $3) - (syscall WRITE $1 $3 $4) - (return nil))) -(data - (label terminal-namespace "/dev/term/0") - (label new-line "\n") - (label hello-str "nuqneH 'u'?"))) diff --git a/test/hello.rom b/test/hello.rom new file mode 100644 index 0000000000000000000000000000000000000000..74ed6378c0b6d7890e4365c4e21a0c4a89fdcff8 GIT binary patch literal 135 zcmZQzU|?_sVi-_^F@Q9S7>L6t3?dl*gV+o#Ac7T4vN1CP*=($gehf^^%s?h10~3n? xkY@H{W`PJWvoQVV28!sXq?YNIq!#7s8vs=?FmN$|MDt1u^HMz&)JxUv832-t3ikj2 literal 0 HcmV?d00001 diff --git a/test/hello.ul.ir b/test/hello.ul.ir index 8243a1e..d43c9fa 100644 --- a/test/hello.ul.ir +++ b/test/hello.ul.ir @@ -6,7 +6,7 @@ function main () str msg $0; load_address hello -> msg; - call pln msg; + call pln (msg); exit 0; function pln (str message $0) @@ -19,7 +19,7 @@ function pln (str message $0) load_immediate 0 -> mode; load_address terminal_namespace -> term_ns; - syscall OPEN term_ns mode -> term; + syscall OPEN term_ns mode term; string_length message -> msg_length; syscall WRITE term message msg_length; load_address new_line -> nl; diff --git a/test/loop.asm.lisp b/test/loop.asm.lisp deleted file mode 100644 index 7e2b95e..0000000 --- a/test/loop.asm.lisp +++ /dev/null @@ -1,44 +0,0 @@ -((code - (label main - (load-immediate $0 5.0) - (load-immediate $1 5000) - (load-immediate $2 0) - (load-immediate $3 -1) - (load-immediate $5 5.0) - (label loop-body - (add-real $0 $0 $5) - (add-int $1 $1 $3) - (jump-ge-int &loop-body $1 $2)) - (load-immediate $10 &terminal-namespace) - (load-immediate $11 0) - (syscall OPEN $10 $10 $11) ; Terminal term = open(namespace, flags) - - (real-to-nat $1 $0) - (load-immediate $7 &prompt) - (string-length $8 $7) - (syscall WRITE $10 $7 $8) ; print prompt - - (load-immediate $8 32) - (malloc $11 $8) - (syscall READ $10 $11 $8) ; read in max 32 byte string - - (call &pln ($11) nil) - (nat-to-string $4 $1) - (call &pln ($4) nil) - (real-to-string $3 $0) - (call &pln ($3) nil) - (exit 0)) - (label pln - (load-immediate $1 &terminal-namespace) ; get terminal device - (load-immediate $11 0) - (syscall OPEN $1 $1 $11) - (load-immediate $3 &new-line) - (string-length $2 $0) - (syscall WRITE $1 $0 $2) - (string-length $4 $3) - (syscall WRITE $1 $3 $4) - (return nil))) -(data - (label terminal-namespace "/dev/term/0") - (label prompt "Enter a string: ") - (label new-line "\n"))) diff --git a/test/loop.rom b/test/loop.rom new file mode 100644 index 0000000000000000000000000000000000000000..48f675eb1a74b105b694330d617f6620ec3e002e GIT binary patch literal 257 zcmXv|F$%&!5S+ceBbTI5u&@zHVIejV?CdOT(`hdxh=nO)@D2JVA9qeJWZ`yZXZDr> zfFIvfyNM7j;MhM4K*#}O152aswlr6wa$3_76Bn}Ba}`Qio*npaPMTDSR~@v(+k)tq zK2=zye&>Dn0Qi{YQr6^)Q*B$6LKnL-Mq`b>7L~t UGtTFhLvbr!efMbZN5qi$1H~B_fB*mh literal 0 HcmV?d00001 diff --git a/test/loop.ul.ir b/test/loop.ul.ir index 54dbede..2edad6a 100644 --- a/test/loop.ul.ir +++ b/test/loop.ul.ir @@ -20,7 +20,7 @@ function main () load_address terminal_namespace -> in_term; load_immediate 0 -> in_mode; - syscall OPEN in_term in_mode -> in_term; // Terminal term = open("/dev/term/0", 0); + syscall OPEN in_term in_mode in_term; // Terminal term = open("/dev/term/0", 0); nat b $1; real_to_nat a -> b; @@ -33,11 +33,11 @@ function main () malloc $8 -> user_string; syscall READ in_term user_string $8; // read in max 32 byte string - call pln user_string; + call pln (user_string); nat_to_string b -> $4; - call pln $4; + call pln ($4); real_to_string a -> $3; - call pln $3; + call pln ($3); exit 0; function pln (str message $0) @@ -49,7 +49,7 @@ function pln (str message $0) load_address terminal_namespace -> term; load_immediate 0 -> mode; - syscall OPEN term mode -> term; // Terminal term = open("/dev/term/0", 0); + syscall OPEN term mode term; // Terminal term = open("/dev/term/0", 0); string_length message -> msg_length; syscall WRITE term message msg_length; load_address new_line -> nl; diff --git a/test/malloc.asm.lisp b/test/malloc.asm.lisp deleted file mode 100644 index 4f76cf6..0000000 --- a/test/malloc.asm.lisp +++ /dev/null @@ -1,26 +0,0 @@ -((code - (label main - (load-immediate $0 &terminal-namespace) ; get terminal device - (load-immediate $11 0) - (syscall OPEN $0 $0 $11) - - (load-immediate $1 &help) ; print help message - (call &pln ($0 $1) nil) - - (load-immediate $1 32) ; read in a string of max 32 char length - (malloc $4 $1) ; allocate memory for the string - (syscall READ $0 $4 $1) ; read the string - - (call &pln ($0 $4) nil) ; print the string - (exit 0)) - (label pln - (load-immediate $3 &new-line) - (string-length $2 $1) - (syscall WRITE $0 $1 $2) - (string-length $4 $3) - (syscall WRITE $0 $3 $4) - (return nil))) -(data - (label terminal-namespace "/dev/term/0") - (label help "Enter a string: ") - (label new-line "\n"))) diff --git a/test/malloc.rom b/test/malloc.rom new file mode 100644 index 0000000000000000000000000000000000000000..a5fd5992e3528092d03acb9b3b8577a051917b73 GIT binary patch literal 187 zcmXv|I}XAy47Fpoahd@()(lKt!pw?Ta|a|U#6X8qxPI#xYRP^d*-r)l_-Jp{Z$yzR z?`%fG%45fSMPIxSRl^G>ML9?ySB)TeQN*z5{6{nv(ON{?U;$^x2#$}sLxuQf5W6L+ dlkdYf&%^Y}C)laJjw;eEz31sUKF(mjzzWsc4vYW* literal 0 HcmV?d00001 diff --git a/test/malloc.ul.ir b/test/malloc.ul.ir index 8eb06fd..dca8380 100644 --- a/test/malloc.ul.ir +++ b/test/malloc.ul.ir @@ -8,7 +8,7 @@ function main () load_address terminal_namespace -> in_term; load_immediate 0 -> in_mode; - syscall OPEN in_term in_mode -> in_term; // Terminal term = open("/dev/term/0", 0); + syscall OPEN in_term in_mode in_term; // Terminal term = open("/dev/term/0", 0); load_address prompt -> $7; string_length $7 -> $8; @@ -19,7 +19,7 @@ function main () malloc $8 -> user_string; syscall READ in_term user_string $8; // read in max 32 byte string - call pln user_string; + call pln (user_string); exit 0; function pln (str message $0) @@ -32,10 +32,11 @@ function pln (str message $0) load_immediate 0 -> mode; load_address terminal_namespace -> term_ns; - syscall OPEN term_ns mode -> term; + syscall OPEN term_ns mode term; string_length message -> msg_length; syscall WRITE term message msg_length; load_address new_line -> nl; string_length nl -> nl_length; syscall WRITE term nl nl_length; - return; \ No newline at end of file + return; + \ No newline at end of file diff --git a/test/paint-bw.asm.lisp b/test/paint-bw.asm.lisp deleted file mode 100644 index 81bf808..0000000 --- a/test/paint-bw.asm.lisp +++ /dev/null @@ -1,147 +0,0 @@ -((code - (label main - ; Open screen - ; use load immediate because it is a pointer to a string, not a value - (load-immediate $0 &screen-namespace) - (load-immediate $11 0) - (syscall OPEN $0 $18 $11) ; open(out Plex screen, in namespace, in flags) - - (load-offset-32 $20 $0 8) ; load width - (load-offset-32 $22 $0 12) ; load size - (load-immediate $1 16) ; offset for screen buffer - (add-nat $21 $0 $1) - - ; open mouse - (load-immediate $16 &mouse-namespace) - (syscall OPEN $15 $16 $11) ; open(out Plex mouse, in namespace, in flags) - - ; outline_swatch(screen, BLACK, 1, 1); - (load-absolute-32 $1 &BLACK) - (load-immediate $12 1) - (load-immediate $13 1) - (call &draw-outlined-swatch ($21 $1 $12 $13 $20) nil) - - ; outline_swatch(screen, WHITE, 1, 1); - (load-absolute-32 $1 &WHITE) - (load-immediate $12 21) - (load-immediate $13 1) - (call &draw-outlined-swatch ($21 $1 $12 $13 $20) nil) - - ; screen.draw(); - (syscall WRITE $0 $21 $22) - - (label draw-loop - ; load mouse click data - (syscall REFRESH $15) - (load-offset-8 $9 $15 16) ; load btn1 pressed - - (jump-eq-nat &draw-loop $9 $11) - - (load-offset-32 $7 $15 8) ; load x - (load-offset-32 $8 $15 12) ; load y - - (load-immediate $14 20) ; box size - - ; first row - (load-absolute-32 $1 &BLACK) - (load-immediate $12 1) - (load-immediate $13 1) - (call &draw-outlined-swatch ($21 $1 $12 $13 $20) nil) - - (call &set-color-if-clicked ($7 $8 $12 $13 $1 $14) nil) - - (load-absolute-32 $1 &WHITE) - (load-immediate $12 21) - (load-immediate $13 1) - (call &draw-outlined-swatch ($21 $1 $12 $13 $20) nil) - - (call &set-color-if-clicked ($7 $8 $12 $13 $1 $14) nil) - - (syscall WRITE $0 $21 $22) - - (load-absolute-32 $22 &SELECTED-COLOR) ; color - (load-immediate $1 5) ; size of brush - - (call &draw-box ($21 $20 $22 $7 $8 $1 $1) nil) - - (jump &draw-loop)) - - ; Flush and exit - (exit 0)) - - (label set-color-if-clicked - ; (click_x, click_y, box_x, box_y, color, box_size) - - ; Compute right = box_x + box_size - (add-int $6 $2 $5) ; $6 = right edge - - ; Compute bottom = box_y + box_size - (add-int $7 $3 $5) ; $7 = bottom edge - - ; Bounds check: x in [box_x, right] and y in [box_y, bottom] - (jump-lt-int &fail $0 $2) - (jump-gt-int &fail $0 $6) - (jump-lt-int &fail $1 $3) - (jump-gt-int &fail $1 $7) - - (load-immediate $10 &SELECTED-COLOR) - (store-absolute-8 $10 $4) - - (label fail) - (return nil)) - - (label draw-outlined-swatch - ; (base, color, x, y, width) - - ; Constants - (load-absolute-32 $5 &GRAY) - (load-absolute-32 $10 &SELECTED-COLOR) - (jump-eq-int &set-selected $10 $1) - (jump-eq-int &end-set-selected $5 $5) - (label set-selected) - (load-absolute-32 $5 &DARK-GRAY) - (label end-set-selected) - - (load-immediate $6 20) ; outline size - (load-immediate $7 17) ; fill size - (load-immediate $8 2) ; offset - - (call &draw-box ($0 $4 $5 $2 $3 $6 $6) nil) - - (add-int $9 $2 $8) ; x + 2 - (add-int $10 $3 $8) ; y + 2 - - (call &draw-box ($0 $4 $1 $9 $10 $7 $7) nil) - - (return nil)) - - (label draw-box - ; (base, screen_width, color, x_start, y_start, width, height) - - ; Compute start address: base + y*640 + x - (mul-int $15 $4 $1) ; $15 = y * 640 - (add-int $15 $15 $3) ; $15 += x - (add-nat $15 $0 $15) ; $15 = base + pixel_offset - (load-immediate $25 4) - (add-nat $15 $15 $25) ; need to add offset for fat pointer size - - ; Outer loop: height times - (load-immediate $30 1) ; increment - - (label draw-box-outer - (add-int $27 $15 $5) ; $27 = row end = current + width - (register-move $29 $15) ; $7 = pixel pointer - (memset-8 $29 $2 $5) ; draw row - (add-int $15 $15 $1) ; next row (+= 640) - (sub-int $6 $6 $30) ; decrement row count - (jump-gt-int &draw-box-outer $6 0)) - (return nil))) -(data - (label screen-namespace "/dev/screen/0") - (label mouse-namespace "/dev/mouse/0") - (label SELECTED-COLOR 255) - (label BLACK 0) - (label WHITE 255) - (label DARK-GRAY 73) - (label GRAY 146) - (label LIGHT-GRAY 182))) diff --git a/test/paint-bw.rom b/test/paint-bw.rom new file mode 100644 index 0000000000000000000000000000000000000000..1f22c521cbf5b225334371b4ac3c992fac5726ba GIT binary patch literal 550 zcmah`NlpVX5OjN=#c{~YL`*c~5Eg|~I7cW*+_)ls0CQuJ62Xy2@C+WnEBFL2G2IP0 zAR+Lfs=Hj%Wsijr;sX%A*YUPM)>Mxe3q_t$A%gH&)IpR;V?9KKnSe%*uo|*!SVX^1 zx~6YR#sF8?mK!lFm1>dIxK#C8Gbz6xv8-ZR(`YI_2}N3R&)4l4+_Tc2UJy6j9TI zwX@hp{_|6QzsasN=zzFqr*`#@fp#KOK screen_name; load_immediate 0 -> mode; - syscall OPEN screen_name mode -> screen; // Screen screen = open("/dev/screen/0", 0); + syscall OPEN screen_name mode screen; // Screen screen = open("/dev/screen/0", 0); nat width $20; nat size $22; @@ -30,26 +30,26 @@ function main () plex mouse $15; str mouse_name $16; load_address mouse_namespace -> mouse_name; - syscall OPEN mouse_name mode -> mouse; // Mouse mouse = open("/dev/mouse/0", 0); + syscall OPEN mouse_name mode mouse; // Mouse mouse = open("/dev/mouse/0", 0); byte color $1; nat x_pos $12; nat y_pos $13; - load_absolute_32 BLACK -> color; + load_absolute_8 BLACK -> color; load_immediate 1 -> x_pos; load_immediate 1 -> y_pos; - call draw_outlined_swatch screen_buffer color x_pos y_pos width -> void; + call draw_outlined_swatch (screen_buffer color x_pos y_pos width); - load_absolute_32 WHITE -> color; + load_absolute_8 WHITE -> color; load_immediate 21 -> x_pos; load_immediate 1 -> y_pos; - call draw_outlined_swatch screen_buffer color x_pos y_pos width -> void; + call draw_outlined_swatch (screen_buffer color x_pos y_pos width); // screen.draw syscall WRITE screen screen_buffer size; - nat zero $11; + nat m_zero $11; loop draw_loop // load mouse click data @@ -58,7 +58,7 @@ function main () byte left_down $9; load_offset_8 mouse 16 -> left_down; // load btn1 pressed - jump_eq_nat draw_loop left_down zero; + jump_eq_nat draw_loop left_down m_zero; nat mouse_x $7; nat mouse_y $8; @@ -69,28 +69,28 @@ function main () load_immediate 20 -> box_size; // first row - load_absolute_32 BLACK -> color; + load_absolute_8 BLACK -> color; load_immediate 1 -> x_pos; load_immediate 1 -> y_pos; - call draw_outlined_swatch screen_buffer color x_pos y_pos width -> void; - call set_color_if_clicked mouse_x mouse_y x_pos y_pos color box_size -> void; + call draw_outlined_swatch (screen_buffer color x_pos y_pos width); + call set_color_if_clicked (mouse_x mouse_y x_pos y_pos color box_size); - load_absolute_32 WHITE -> color; + load_absolute_8 WHITE -> color; load_immediate 21 -> x_pos; load_immediate 1 -> y_pos; - call draw_outlined_swatch screen_buffer color x_pos y_pos width -> void; - call set_color_if_clicked mouse_x mouse_y x_pos y_pos color box_size -> void; + call draw_outlined_swatch (screen_buffer color x_pos y_pos width); + call set_color_if_clicked (mouse_x mouse_y x_pos y_pos color box_size); syscall WRITE screen screen_buffer size; byte selected_color $25; - load_absolute_32 SELECTED_COLOR -> selected_color; + load_absolute_8 SELECTED_COLOR -> selected_color; nat brush_size $19; load_immediate 5 -> brush_size; - call draw_box screen_buffer width selected_color mouse_x mouse_y brush_size brush_size -> void; + call draw_box (screen_buffer width selected_color mouse_x mouse_y brush_size brush_size); jump draw_loop; @@ -98,41 +98,41 @@ function main () exit 0; function set_color_if_clicked (int click_x $0, int click_y $1, - int box_x $2, int box_y $3, byte color $4, int box_size $5) + int box_x $2, int box_y $3, byte check_color $4, int bsize $5) // Compute right int right_edge $6; - add_int box_x box_size -> right_edge; + add_int box_x bsize -> right_edge; - // Compute bottom = box_y + box_size + // Compute bottom = box_y + bsize int bottom_edge $7; - add_int box_y box_size -> bottom_edge; + add_int box_y bsize -> bottom_edge; // Bounds check: x in [box_x, right] and y in [box_y, bottom] jump_lt_int fail click_x box_x; - jump_ge_int fail click_x right_edge; + jump_gt_int fail click_x right_edge; jump_lt_int fail click_y box_y; - jump_ge_int fail click_y bottom_edge; + jump_gt_int fail click_y bottom_edge; - store_absolute_8 SELECTED_COLOR color; + store_absolute_8 check_color -> SELECTED_COLOR; else fail return; -function draw_outlined_swatch(nat base $0, - byte color $1, int x $2, int y $3, int width $4) +function draw_outlined_swatch(nat dos_base $0, + byte swatch_color $1, int x $2, int y $3, int dos_width $4) // Constants nat background_color $5; - load_absolute_32 GRAY -> background_color; + load_absolute_8 GRAY -> background_color; - byte selected_color $10; - load_absolute_32 SELECTED_COLOR -> selected_color; + byte dos_selected_color $10; + load_absolute_8 SELECTED_COLOR -> dos_selected_color; - jump_eq_int set_selected selected_color color; + jump_eq_int set_selected swatch_color dos_selected_color; jump end_set_selected; do set_selected - load_absolute_32 DARK_GRAY -> background_color; + load_absolute_8 DARK_GRAY -> background_color; else end_set_selected nat outline_size $6; @@ -141,27 +141,27 @@ function draw_outlined_swatch(nat base $0, nat fill_size $7; load_immediate 17 -> fill_size; - nat offset $8; - load_immediate 2 -> offset; + nat dos_offset $8; + load_immediate 2 -> dos_offset; - call draw_box base width background_color x y outline_size outline_size -> void; + call draw_box (dos_base dos_width background_color x y outline_size outline_size); - add_int x offset -> $9; // x + 2 - add_int y offset -> $10; // y + 2 + add_int x dos_offset -> $9; // x + 2 + add_int y dos_offset -> $10; // y + 2 - call draw_box base width color $9 $10 fill_size fill_size -> void; + call draw_box (dos_base dos_width swatch_color $9 $10 fill_size fill_size); return; -function draw_box (nat base $0, nat screen_width $1, - byte color $2, nat x_start $3, nat y_start $4, - nat width $5, nat height $6) +function draw_box (nat db_base $0, nat screen_width $1, + byte box_color $2, nat x_start $3, nat y_start $4, + nat db_width $5, nat height $6) // Compute start address: base + y*640 + x nat offset $15; mul_int y_start screen_width -> offset; add_int offset x_start -> offset; - add_nat offset base -> offset; + add_nat offset db_base -> offset; nat fat_ptr_size $25; load_immediate 4 -> fat_ptr_size; add_nat offset fat_ptr_size -> offset; // need to add offset for fat pointer size @@ -172,13 +172,8 @@ function draw_box (nat base $0, nat screen_width $1, int zero $26; load_immediate 0 -> zero; - int row_end $27; - nat pixel_ptr $29; - loop draw_box_outer - add_int offset width -> row_end; // current + width - register_move offset -> pixel_ptr; // set pixel point - memset_8 pixel_ptr color width; // draw row + memset_8 box_color db_width -> offset; // draw row add_int offset screen_width -> offset; // next row += 640 sub_int height i -> height; // decrement row count jump_gt_int draw_box_outer height zero; diff --git a/test/paint.asm.lisp b/test/paint.asm.lisp deleted file mode 100644 index 2e946d5..0000000 --- a/test/paint.asm.lisp +++ /dev/null @@ -1,261 +0,0 @@ -((code - (label main - ; Open screen - ; use load immediate because it is a pointer to a string, not a value - (load-immediate $0 &screen-namespace) - (load-immediate $11 0) - (syscall OPEN $0 $0 $11) ; Screen screen = open(namespace, flags) - - (load-offset-32 $20 $0 8) ; load width - (load-offset-32 $22 $0 12) ; load size - (load-immediate $1 16) ; pointer offset for screen buffer - (add-nat $21 $0 $1) - - ; open mouse - (load-immediate $16 &mouse-namespace) - (syscall OPEN $15 $16 $11) ; Mouse mouse = open(namespace, flags) - - ; outline_swatch(screen, BLACK, 1, 1); - (load-absolute-32 $1 &BLACK) - (load-immediate $12 1) - (load-immediate $13 1) - (call &draw-outlined-swatch ($21 $1 $12 $13 $20) nil) - - ; outline_swatch(screen, WHITE, 1, 1); - (load-absolute-32 $1 &WHITE) - (load-immediate $12 21) - (load-immediate $13 1) - (call &draw-outlined-swatch ($21 $1 $12 $13 $20) nil) - - (load-absolute-32 $1 &CHARCOAL) - (load-immediate $12 1) - (load-immediate $13 21) - (call &draw-outlined-swatch ($21 $1 $12 $13 $20) nil) - - (load-absolute-32 $1 &DARK-GRAY) - (load-immediate $12 21) - (load-immediate $13 21) - (call &draw-outlined-swatch ($21 $1 $12 $13 $20) nil) - - (load-absolute-32 $1 &RED) - (load-immediate $12 1) - (load-immediate $13 41) - (call &draw-outlined-swatch ($21 $1 $12 $13 $20) nil) - - (load-absolute-32 $1 &ORANGE) - (load-immediate $12 21) - (load-immediate $13 41) - (call &draw-outlined-swatch ($21 $1 $12 $13 $20) nil) - - (load-absolute-32 $1 &YELLOW) - (load-immediate $12 1) - (load-immediate $13 61) - (call &draw-outlined-swatch ($21 $1 $12 $13 $20) nil) - - (load-absolute-32 $1 &GREEN) - (load-immediate $12 21) - (load-immediate $13 61) - (call &draw-outlined-swatch ($21 $1 $12 $13 $20) nil) - - (load-absolute-32 $1 &BLUE) - (load-immediate $12 1) - (load-immediate $13 81) - (call &draw-outlined-swatch ($21 $1 $12 $13 $20) nil) - - (load-absolute-32 $1 &PURPLE) - (load-immediate $12 21) - (load-immediate $13 81) - (call &draw-outlined-swatch ($21 $1 $12 $13 $20) nil) - - ; screen.draw(); - (syscall WRITE $0 $21 $22) - - (label draw-loop - ; load mouse click data - (syscall REFRESH $15) - (load-offset-8 $9 $15 16) ; load btn1 pressed - - (jump-eq-nat &draw-loop $9 $11) - - (load-offset-32 $7 $15 8) ; load x - (load-offset-32 $8 $15 12) ; load y - - (load-immediate $14 20) ; box size - - ; outline_swatch(screen, BLACK, 1, 1); - (load-absolute-32 $1 &BLACK) - (load-immediate $12 1) - (load-immediate $13 1) - (call &draw-outlined-swatch ($21 $1 $12 $13 $20) nil) - (call &set-color-if-clicked ($7 $8 $12 $13 $1 $14) nil) - - ; outline_swatch(screen, WHITE, 1, 1); - (load-absolute-32 $1 &WHITE) - (load-immediate $12 21) - (load-immediate $13 1) - (call &draw-outlined-swatch ($21 $1 $12 $13 $20) nil) - (call &set-color-if-clicked ($7 $8 $12 $13 $1 $14) nil) - - (load-absolute-32 $1 &CHARCOAL) - (load-immediate $12 1) - (load-immediate $13 21) - (call &draw-outlined-swatch ($21 $1 $12 $13 $20) nil) - (call &set-color-if-clicked ($7 $8 $12 $13 $1 $14) nil) - - (load-absolute-32 $1 &DARK-GRAY) - (load-immediate $12 21) - (load-immediate $13 21) - (call &draw-outlined-swatch ($21 $1 $12 $13 $20) nil) - (call &set-color-if-clicked ($7 $8 $12 $13 $1 $14) nil) - - (load-absolute-32 $1 &RED) - (load-immediate $12 1) - (load-immediate $13 41) - (call &draw-outlined-swatch ($21 $1 $12 $13 $20) nil) - (call &set-color-if-clicked ($7 $8 $12 $13 $1 $14) nil) - - (load-absolute-32 $1 &ORANGE) - (load-immediate $12 21) - (load-immediate $13 41) - (call &draw-outlined-swatch ($21 $1 $12 $13 $20) nil) - (call &set-color-if-clicked ($7 $8 $12 $13 $1 $14) nil) - - (load-absolute-32 $1 &YELLOW) - (load-immediate $12 1) - (load-immediate $13 61) - (call &draw-outlined-swatch ($21 $1 $12 $13 $20) nil) - (call &set-color-if-clicked ($7 $8 $12 $13 $1 $14) nil) - - (load-absolute-32 $1 &GREEN) - (load-immediate $12 21) - (load-immediate $13 61) - (call &draw-outlined-swatch ($21 $1 $12 $13 $20) nil) - (call &set-color-if-clicked ($7 $8 $12 $13 $1 $14) nil) - - (load-absolute-32 $1 &BLUE) - (load-immediate $12 1) - (load-immediate $13 81) - (call &draw-outlined-swatch ($21 $1 $12 $13 $20) nil) - (call &set-color-if-clicked ($7 $8 $12 $13 $1 $14) nil) - - (load-absolute-32 $1 &PURPLE) - (load-immediate $12 21) - (load-immediate $13 81) - (call &draw-outlined-swatch ($21 $1 $12 $13 $20) nil) - (call &set-color-if-clicked ($7 $8 $12 $13 $1 $14) nil) - - (syscall WRITE $0 $21 $22) - - (load-absolute-32 $22 &SELECTED-COLOR) ; color - (load-immediate $1 5) ; size of brush - - (call &draw-box ($21 $20 $22 $7 $8 $1 $1) nil) - - (jump &draw-loop)) - - ; Flush and exit - (exit 0)) - - (label set-color-if-clicked - ; (click_x, click_y, box_x, box_y, color, box_size) - - ; Compute right = box_x + box_size - (add-int $6 $2 $5) ; $6 = right edge - - ; Compute bottom = box_y + box_size - (add-int $7 $3 $5) ; $7 = bottom edge - - ; Bounds check: x in [box_x, right] and y in [box_y, bottom] - (jump-lt-int &fail $0 $2) - (jump-gt-int &fail $0 $6) - (jump-lt-int &fail $1 $3) - (jump-gt-int &fail $1 $7) - - (load-immediate $10 &SELECTED-COLOR) - (store-absolute-8 $10 $4) - - (label fail) - (return nil)) - - (label draw-outlined-swatch - ; (base, color, x, y, width) - - ; Constants - (load-absolute-32 $5 &GRAY) - (load-absolute-32 $10 &SELECTED-COLOR) - (jump-eq-int &set-selected $10 $1) - (jump-eq-int &end-set-selected $5 $5) - (label set-selected) - (load-absolute-32 $5 &DARK-GRAY) - (label end-set-selected) - - (load-immediate $6 20) ; outline size - (load-immediate $7 17) ; fill size - (load-immediate $8 2) ; offset - - (call &draw-box ($0 $4 $5 $2 $3 $6 $6) nil) - - (add-int $9 $2 $8) ; x + 2 - (add-int $10 $3 $8) ; y + 2 - - (call &draw-box ($0 $4 $1 $9 $10 $7 $7) nil) - - (return nil)) - - (label draw-box - ; (base, screen_width, color, x_start, y_start, width, height) - - ; Compute start address: base + y*640 + x - (mul-int $15 $4 $1) ; $15 = y * 640 - (add-int $15 $15 $3) ; $15 += x - (add-nat $15 $0 $15) ; $15 = base + pixel_offset - (load-immediate $25 4) - (add-nat $15 $15 $25) ; need to add offset for fat pointer size - - ; Outer loop: height times - (load-immediate $30 1) ; increment - - (label draw-box-outer - (add-int $27 $15 $5) ; $27 = row end = current + width - (register-move $29 $15) ; $7 = pixel pointer - (memset-8 $29 $2 $5) ; draw row - (add-int $15 $15 $1) ; next row (+= 640) - (sub-int $6 $6 $30) ; decrement row count - (jump-gt-int &draw-box-outer $6 0)) - (return nil))) -(data - (label screen-namespace "/dev/screen/0") - (label mouse-namespace "/dev/mouse/0") - (label SELECTED-COLOR 255) - (label BLACK 0) - (label WHITE 255) - (label CHARCOAL 36) - (label DARK-GRAY 73) - (label GRAY 146) - (label LIGHT-GRAY 182) - (label DARK-RED 128) - (label RED 224) - (label DARK-YELLOW 144) - (label YELLOW 252) - (label DARK-TEAL 9) - (label TEAL 18) - (label DARK-GREEN 12) - (label GREEN 16) - (label LIME 28) - (label LIGHT-CYAN 159) - (label NAVY 2) - (label BLUE 3) - (label DEEP-SKY-BLUE 10) - (label LIGHT-BLUE 19) - (label PURPLE 131) - (label LIGHT-PURPLE 147) - (label DARK-MAGENTA 130) - (label MAGENTA 227) - (label PLUM 129) - (label PINK 226) - (label SADDLE-BROWN 72) - (label PERU 141) - (label SIENNA 136) - (label ORANGE 241) - (label DARK-ORANGE 208) - (label GOLD 244))) diff --git a/test/paint.rom b/test/paint.rom new file mode 100644 index 0000000000000000000000000000000000000000..062f0d9051f5c0690b216aef098c8a5a63655c79 GIT binary patch literal 1161 zcmah}yGjHx6wPar%xkmm&WJ1H7V85QVYd*3#TRO!rQe`i`asdzM`I(1b|Ux_V&PZ# z38G)1SPJnbx-emIOmS~=?>QOfoFoV#G{qqNjLmvr8tTcEyrv({i8dX;~I?19nW!#-RT^6s=!^& zaf{uV9Cx9>?c}({?re^`S>Vp)xP>kSVGv3ODTky&WhC`ic0oHojFnMa#LB2GSpu;myM}jQc#O(@GyTYQv%tl1Z2}qvZ zHJE<@JPx`~kfbrkvnPeK0hkL$Aj+R_BIXgwxz336Gb)3A75jk@n&>9ODpORo6jIDw z{6 screen_name; load_immediate 0 -> mode; - syscall OPEN screen_name mode -> screen; // Screen screen = open("/dev/screen/0", 0); + syscall OPEN screen_name mode screen; // Screen screen = open("/dev/screen/0", 0); nat width $20; nat size $22; @@ -30,26 +57,66 @@ function main () plex mouse $15; str mouse_name $16; load_address mouse_namespace -> mouse_name; - syscall OPEN mouse_name mode -> mouse; // Mouse mouse = open("/dev/mouse/0", 0); + syscall OPEN mouse_name mode mouse; // Mouse mouse = open("/dev/mouse/0", 0); byte color $1; nat x_pos $12; nat y_pos $13; - load_absolute_32 BLACK -> color; + load_absolute_8 BLACK -> color; load_immediate 1 -> x_pos; load_immediate 1 -> y_pos; - call draw_outlined_swatch screen_buffer color x_pos y_pos width -> void; + call draw_outlined_swatch (screen_buffer color x_pos y_pos width); - load_absolute_32 WHITE -> color; + load_absolute_8 WHITE -> color; load_immediate 21 -> x_pos; load_immediate 1 -> y_pos; - call draw_outlined_swatch screen_buffer color x_pos y_pos width -> void; + call draw_outlined_swatch (screen_buffer color x_pos y_pos width); + + load_absolute_8 CHARCOAL -> color; + load_immediate 1 -> x_pos; + load_immediate 21 -> y_pos; + call draw_outlined_swatch (screen_buffer color x_pos y_pos width); + + load_absolute_8 DARK_GRAY -> color; + load_immediate 21 -> x_pos; + load_immediate 21 -> y_pos; + call draw_outlined_swatch (screen_buffer color x_pos y_pos width); + + load_absolute_8 RED -> color; + load_immediate 1 -> x_pos; + load_immediate 41 -> y_pos; + call draw_outlined_swatch (screen_buffer color x_pos y_pos width); + + load_absolute_8 ORANGE -> color; + load_immediate 21 -> x_pos; + load_immediate 41 -> y_pos; + call draw_outlined_swatch (screen_buffer color x_pos y_pos width); + + load_absolute_8 YELLOW -> color; + load_immediate 1 -> x_pos; + load_immediate 61 -> y_pos; + call draw_outlined_swatch (screen_buffer color x_pos y_pos width); + + load_absolute_8 GREEN -> color; + load_immediate 21 -> x_pos; + load_immediate 61 -> y_pos; + call draw_outlined_swatch (screen_buffer color x_pos y_pos width); + + load_absolute_8 BLUE -> color; + load_immediate 1 -> x_pos; + load_immediate 81 -> y_pos; + call draw_outlined_swatch (screen_buffer color x_pos y_pos width); + + load_absolute_8 PURPLE -> color; + load_immediate 21 -> x_pos; + load_immediate 81 -> y_pos; + call draw_outlined_swatch (screen_buffer color x_pos y_pos width); // screen.draw syscall WRITE screen screen_buffer size; - nat zero $11; + nat m_zero $11; loop draw_loop // load mouse click data @@ -58,7 +125,7 @@ function main () byte left_down $9; load_offset_8 mouse 16 -> left_down; // load btn1 pressed - jump_eq_nat draw_loop left_down zero; + jump_eq_nat draw_loop left_down m_zero; nat mouse_x $7; nat mouse_y $8; @@ -69,28 +136,75 @@ function main () load_immediate 20 -> box_size; // first row - load_absolute_32 BLACK -> color; + load_absolute_8 BLACK -> color; load_immediate 1 -> x_pos; load_immediate 1 -> y_pos; - call draw_outlined_swatch screen_buffer color x_pos y_pos width -> void; - call set_color_if_clicked mouse_x mouse_y x_pos y_pos color box_size -> void; + call draw_outlined_swatch (screen_buffer color x_pos y_pos width); + call set_color_if_clicked (mouse_x mouse_y x_pos y_pos color box_size); - - load_absolute_32 WHITE -> color; + load_absolute_8 WHITE -> color; load_immediate 21 -> x_pos; load_immediate 1 -> y_pos; - call draw_outlined_swatch screen_buffer color x_pos y_pos width -> void; - call set_color_if_clicked mouse_x mouse_y x_pos y_pos color box_size -> void; + call draw_outlined_swatch (screen_buffer color x_pos y_pos width); + call set_color_if_clicked (mouse_x mouse_y x_pos y_pos color box_size); + + load_absolute_8 CHARCOAL -> color; + load_immediate 1 -> x_pos; + load_immediate 21 -> y_pos; + call draw_outlined_swatch (screen_buffer color x_pos y_pos width); + call set_color_if_clicked (mouse_x mouse_y x_pos y_pos color box_size); + + load_absolute_8 DARK_GRAY -> color; + load_immediate 21 -> x_pos; + load_immediate 21 -> y_pos; + call draw_outlined_swatch (screen_buffer color x_pos y_pos width); + call set_color_if_clicked (mouse_x mouse_y x_pos y_pos color box_size); + + load_absolute_8 RED -> color; + load_immediate 1 -> x_pos; + load_immediate 41 -> y_pos; + call draw_outlined_swatch (screen_buffer color x_pos y_pos width); + call set_color_if_clicked (mouse_x mouse_y x_pos y_pos color box_size); + + load_absolute_8 ORANGE -> color; + load_immediate 21 -> x_pos; + load_immediate 41 -> y_pos; + call draw_outlined_swatch (screen_buffer color x_pos y_pos width); + call set_color_if_clicked (mouse_x mouse_y x_pos y_pos color box_size); + + load_absolute_8 YELLOW -> color; + load_immediate 1 -> x_pos; + load_immediate 61 -> y_pos; + call draw_outlined_swatch (screen_buffer color x_pos y_pos width); + call set_color_if_clicked (mouse_x mouse_y x_pos y_pos color box_size); + + load_absolute_8 GREEN -> color; + load_immediate 21 -> x_pos; + load_immediate 61 -> y_pos; + call draw_outlined_swatch (screen_buffer color x_pos y_pos width); + call set_color_if_clicked (mouse_x mouse_y x_pos y_pos color box_size); + + load_absolute_8 BLUE -> color; + load_immediate 1 -> x_pos; + load_immediate 81 -> y_pos; + call draw_outlined_swatch (screen_buffer color x_pos y_pos width); + call set_color_if_clicked (mouse_x mouse_y x_pos y_pos color box_size); + + load_absolute_8 PURPLE -> color; + load_immediate 21 -> x_pos; + load_immediate 81 -> y_pos; + call draw_outlined_swatch (screen_buffer color x_pos y_pos width); + call set_color_if_clicked (mouse_x mouse_y x_pos y_pos color box_size); syscall WRITE screen screen_buffer size; byte selected_color $25; - load_absolute_32 SELECTED_COLOR -> selected_color; + load_absolute_8 SELECTED_COLOR -> selected_color; nat brush_size $19; load_immediate 5 -> brush_size; - call draw_box screen_buffer width selected_color mouse_x mouse_y brush_size brush_size -> void; + call draw_box (screen_buffer width selected_color mouse_x mouse_y brush_size brush_size); jump draw_loop; @@ -98,41 +212,41 @@ function main () exit 0; function set_color_if_clicked (int click_x $0, int click_y $1, - int box_x $2, int box_y $3, byte color $4, int box_size $5) + int box_x $2, int box_y $3, byte check_color $4, int bsize $5) // Compute right int right_edge $6; - add_int box_x box_size -> right_edge; + add_int box_x bsize -> right_edge; - // Compute bottom = box_y + box_size + // Compute bottom = box_y + bsize int bottom_edge $7; - add_int box_y box_size -> bottom_edge; + add_int box_y bsize -> bottom_edge; // Bounds check: x in [box_x, right] and y in [box_y, bottom] jump_lt_int fail click_x box_x; - jump_ge_int fail click_x right_edge; + jump_gt_int fail click_x right_edge; jump_lt_int fail click_y box_y; - jump_ge_int fail click_y bottom_edge; + jump_gt_int fail click_y bottom_edge; - store_absolute_8 color -> SELECTED_COLOR; + store_absolute_8 check_color -> SELECTED_COLOR; else fail return; -function draw_outlined_swatch(nat base $0, - byte color $1, int x $2, int y $3, int width $4) +function draw_outlined_swatch(nat dos_base $0, + byte swatch_color $1, int x $2, int y $3, int dos_width $4) // Constants nat background_color $5; - load_absolute_32 GRAY -> background_color; + load_absolute_8 GRAY -> background_color; - byte selected_color $10; - load_absolute_32 SELECTED_COLOR -> selected_color; + byte dos_selected_color $10; + load_absolute_8 SELECTED_COLOR -> dos_selected_color; - jump_eq_int set_selected selected_color color; + jump_eq_int set_selected swatch_color dos_selected_color; jump end_set_selected; do set_selected - load_absolute_32 DARK_GRAY -> background_color; + load_absolute_8 DARK_GRAY -> background_color; else end_set_selected nat outline_size $6; @@ -141,27 +255,27 @@ function draw_outlined_swatch(nat base $0, nat fill_size $7; load_immediate 17 -> fill_size; - nat offset $8; - load_immediate 2 -> offset; + nat dos_offset $8; + load_immediate 2 -> dos_offset; - call draw_box base width background_color x y outline_size outline_size -> void; + call draw_box (dos_base dos_width background_color x y outline_size outline_size); - add_int x offset -> $9; // x + 2 - add_int y offset -> $10; // y + 2 + add_int x dos_offset -> $9; // x + 2 + add_int y dos_offset -> $10; // y + 2 - call draw_box base width color $9 $10 fill_size fill_size -> void; + call draw_box (dos_base dos_width swatch_color $9 $10 fill_size fill_size); return; -function draw_box (nat base $0, nat screen_width $1, - byte color $2, nat x_start $3, nat y_start $4, - nat width $5, nat height $6) +function draw_box (nat db_base $0, nat screen_width $1, + byte box_color $2, nat x_start $3, nat y_start $4, + nat db_width $5, nat height $6) // Compute start address: base + y*640 + x nat offset $15; mul_int y_start screen_width -> offset; add_int offset x_start -> offset; - add_nat offset base -> offset; + add_nat offset db_base -> offset; nat fat_ptr_size $25; load_immediate 4 -> fat_ptr_size; add_nat offset fat_ptr_size -> offset; // need to add offset for fat pointer size @@ -172,13 +286,8 @@ function draw_box (nat base $0, nat screen_width $1, int zero $26; load_immediate 0 -> zero; - int row_end $27; - nat pixel_ptr $29; - loop draw_box_outer - add_int offset width -> row_end; // current + width - register_move offset -> pixel_ptr; // set pixel point - memset_8 pixel_ptr color width; // draw row + memset_8 box_color db_width -> offset; // draw row add_int offset screen_width -> offset; // next row += 640 sub_int height i -> height; // decrement row count jump_gt_int draw_box_outer height zero; diff --git a/test/simple.asm.lisp b/test/simple.asm.lisp deleted file mode 100644 index 1cd4d01..0000000 --- a/test/simple.asm.lisp +++ /dev/null @@ -1,22 +0,0 @@ -((code - (label main - (load-absolute-32 $0 &x) - (load-absolute-32 $1 &y) - (add-real $2 $1 $0) - (real-to-string $3 $2) - (call &pln ($3) nil) - (exit 0)) - (label pln - (load-immediate $1 &terminal-namespace) ; get terminal device - (load-immediate $11 0) - (syscall OPEN $1 $1 $11) - (load-immediate $3 &new-line) - (string-length $2 $0) - (syscall WRITE $1 $0 $2) - (string-length $4 $3) - (syscall WRITE $1 $3 $4) - (return nil))) -(data (label terminal-namespace "/dev/term/0") - (label new-line "\n") - (label x 1.0) - (label y 2.0))) diff --git a/test/simple.rom b/test/simple.rom new file mode 100644 index 0000000000000000000000000000000000000000..9ea8e30f3987b053c87f58ec31024966ea6a3e46 GIT binary patch literal 132 zcmXwxp$>pR3`5^;cL*>jG!l1w2?D_vfW#2O?;i!ilC@pi3}Dv&)BQpm!Cnm5Ai3!7 rZ literal 0 HcmV?d00001 diff --git a/test/simple.ul.ir b/test/simple.ul.ir index 7c90ce9..3c04c23 100644 --- a/test/simple.ul.ir +++ b/test/simple.ul.ir @@ -3,10 +3,10 @@ global str new_line = "\n"; function main () load_immediate 1.0 -> $0; - load_immediate 1.0 -> $1; + load_immediate 2.0 -> $1; add_real $0 $1 -> $0; real_to_string $0 -> $0; - call pln $0; + call pln ($0); exit 0; function pln (str message $0) @@ -19,7 +19,7 @@ function pln (str message $0) load_immediate 0 -> mode; load_address terminal_namespace -> term_ns; - syscall OPEN term_ns mode -> term; + syscall OPEN term_ns mode term; string_length message -> msg_length; syscall WRITE term message msg_length; load_address new_line -> nl; diff --git a/test/window.asm.lisp b/test/window.asm.lisp deleted file mode 100644 index b697ebe..0000000 --- a/test/window.asm.lisp +++ /dev/null @@ -1,71 +0,0 @@ -((code - (label main - ; Open screen - ; use load immediate because it is a pointer to a string, not a value - (load-immediate $18 &screen-namespace) - (syscall OPEN $0 $18 $11) ; open(out Plex screen, in namespace, in flags) - - (nat-to-string $5 $0) - (call &pln ($5) nil) - - (load-offset-32 $20 $0 8) ; load width - - (nat-to-string $5 $20) - (call &pln ($5) nil) - - (load-offset-32 $22 $0 12) ; load size - - (nat-to-string $5 $22) - (call &pln ($5) nil) - - (load-immediate $1 16) ; offset for screen buffer - (add-nat $21 $0 $1) - - (nat-to-string $5 $21) - (call &pln ($5) nil) - - ; open mouse - (load-immediate $16 &mouse-namespace) - (syscall OPEN $15 $16 $11) ; open(out Plex mouse, in namespace, in flags) - - (syscall WRITE $0 $21 $22) ; redraw - - (label draw-loop - ; load mouse click data - (syscall REFRESH $15) - (load-offset-8 $9 $15 16) ; load btn1 pressed - - (jump-eq-nat &draw-loop $9 $11) - - (load-offset-32 $7 $15 8) ; load x - (load-offset-32 $8 $15 12) ; load y - - ; Compute start address: y*width + x - (mul-nat $30 $8 $20) ; $15 = y * width - (add-nat $30 $30 $7) ; $15 += x - (add-nat $30 $30 $21) ; $15 += pixel_offset - (load-immediate $1 4) ; need to add offset for fat pointer size - (add-nat $30 $30 $1) - - (load-absolute-32 $3 &WHITE) ; color - (store-absolute-8 $30 $3) ; draw color at screen [x,y] - (syscall WRITE $0 $21 $22) ; redraw - - (jump &draw-loop)) - (exit 0)) - (label pln - (load-immediate $1 &terminal-namespace) ; get terminal device - (load-immediate $11 0) - (syscall OPEN $1 $1 $11) - (load-immediate $3 &new-line) - (string-length $2 $0) - (syscall WRITE $1 $0 $2) - (string-length $4 $3) - (syscall WRITE $1 $3 $4) - (return nil))) -(data - (label screen-namespace "/dev/screen/0") - (label mouse-namespace "/dev/mouse/0") - (label terminal-namespace "/dev/term/0") - (label new-line "\n") - (label WHITE 255))) diff --git a/test/window.rom b/test/window.rom new file mode 100644 index 0000000000000000000000000000000000000000..002b86542fbc13977ac473d9d95d6c72df310d58 GIT binary patch literal 332 zcmYk2Jr2S!42A7FN!$DRU6k(glQ0Nr{5$k-yazzI1MYq3)mh?L~{i~XDg0N~AY z8K*_?u98lxCX_%!3mLvxVKxOQSd?w~M?4ePx-U^{2BrdITl<3>F-VT6k%%L(j*L); zQlZYhtU92y$ODRRu`5bn<=$7;dnMNjl@Hz?S<#9lW$`*mc^dQW)s_R3IvR})WGN9L sQwKhvhSUs5$+H0JYkbz@Wr(q__aM%RbhpPC8Mp=9 screen; + load_address screen_namespace -> screen; load_immediate 0 -> mode; - syscall OPEN screen mode -> screen; + syscall OPEN screen mode screen; nat_to_string screen -> tmp_str; - call pln tmp_str -> void; + call pln (tmp_str); load_offset_32 screen 8 -> width; nat_to_string width -> tmp_str; - call pln tmp_str -> void; + call pln (tmp_str); load_offset_32 screen 12 -> buffer_size; nat_to_string buffer_size -> tmp_str; - call pln tmp_str -> void; + call pln (tmp_str); load_immediate 16 -> offset_temp; add_nat screen offset_temp -> screen_buffer; nat_to_string screen_buffer -> tmp_str; - call pln tmp_str -> void; + call pln (tmp_str); // open mouse - load_immediate mouse_namespace -> mouse; - syscall OPEN mouse mode -> mouse; + load_address mouse_namespace -> mouse; + syscall OPEN mouse mode mouse; syscall WRITE screen screen_buffer buffer_size; // redraw loop draw_loop // load mouse click data - syscall STAT mouse; + syscall REFRESH mouse; load_offset_8 mouse 16 -> left_down; @@ -64,11 +65,12 @@ function main () load_immediate 4 -> fat_ptr_size; add_nat pixel_pos fat_ptr_size -> pixel_pos; - load_absolute_32 white -> color; - store_absolute_8 pixel_pos color; // draw color at screen [x,y] + load_absolute_32 WHITE -> color; + store_absolute_8 color -> pixel_pos; // draw color at screen [x,y] + syscall WRITE screen screen_buffer buffer_size; // redraw - jump draw_loop; + jump draw_loop; exit 0; function pln (str message $0) @@ -76,15 +78,15 @@ function pln (str message $0) int msg_length $2; str nl $3; int nl_length $4; - int mode $5; + int pln_mode $5; str term_ns $6; - load_immediate 0 -> mode; + load_immediate 0 -> pln_mode; load_address terminal_namespace -> term_ns; - syscall OPEN term_ns mode -> term; + syscall OPEN term_ns pln_mode term; string_length message -> msg_length; syscall WRITE term message msg_length; load_address new_line -> nl; string_length nl -> nl_length; syscall WRITE term nl nl_length; - return; \ No newline at end of file + return;