diff --git a/bin/alchemyc b/bin/alchemyc index 3ec83a0..0f0ad72 100755 --- a/bin/alchemyc +++ b/bin/alchemyc @@ -1,6 +1,12 @@ #!/usr/bin/env node +const process = require("node:process"); const { AlchemyCompilerCli } = require("../dist/cli.js"); const compiler = new AlchemyCompilerCli(); -compiler.compile(process.argv[2], process.argv[3]); +try { + compiler.compile(process.argv[2], process.argv[3]); +} catch (e) { + console.log(`Compiler error: ${e}`); + process.exit(1); +} diff --git a/src/compiler/nasm/nasm.ts b/src/compiler/nasm/nasm.ts index 7e608c3..83047c5 100644 --- a/src/compiler/nasm/nasm.ts +++ b/src/compiler/nasm/nasm.ts @@ -17,7 +17,7 @@ export class NasmCompiler implements Compiler if @@ -59,6 +64,7 @@ sub malloc clone ? u64_max & over + swap drop [Add the node size to the current node address] else swap drop over swap + "Wrote new block" println BLOCK_USED + ! swap return endif diff --git a/test.alc b/test.alc new file mode 100644 index 0000000..61b2a1e --- /dev/null +++ b/test.alc @@ -0,0 +1,140 @@ +include std.io +include std.sys.call +include std.math +include std.size +include std.bool + +sub BLOCK_FREE 0 swap marine +sub BLOCK_USED 1 swap marine +sub BLOCK_STATUS_MASK 1 swap marine +sub BLOCK_SIZE_MASK u64_max 1 - swap marine + +[Get block size] +sub get_block_size + swap ? BLOCK_SIZE_MASK & swap +marine + +[Get block status] +sub get_block_status + swap ? BLOCK_STATUS_MASK & swap +marine + +[Set block status] +sub set_block_status + swap over ? BLOCK_SIZE_MASK & swap BLOCK_STATUS_MASK & + ! +marine + +[Modifies the end address of the heap] +[arg:int New address in number of bytes] +[ret:ptr End address of heap] +sub brk + swap SYS_BRK syscall1 sysread swap +marine + +[Read the current end of the heap] +[ret:ptr End address of heap] +sub heap_end + 0 brk swap +marine + +[Retrieves the starting address of the heap] +[ret:ptr Start address of the heap] +sub heap_start + heap_end ? swap +marine + +[Initializes the memory allocator] +[ret:void] +sub meminit + "Initializing heap allocator @ " print heap_end printx + heap_end 8 + clone brk ! +marine + +sub incheap + swap + 8 int_align + "Increasing heap memory by: " print clone put + heap_end ? + swap heap_end + + brk sysread 0 < if [sysread with value less than 0 means an error occurred] + "Unable to increase heap memory" println + 1 SYS_EXIT syscall1 + endif + swap ! + "New heap end: " print heap_end printx + "With value: " print heap_end ? printx +marine + +[Allocates bytes on the heap] +[arg:int Number of bytes to allocate] +[ret:ptr Address of allocated block] +sub malloc + swap + 8 int_align + "Allocating bytes: " print clone put + + + heap_end ? + while true do + clone heap_end > if + "FATAL: Memory allocator exceeded the heap end address:" println + " Allocator @ " print clone printx + " Heap end @ " print heap_end printx + 1 SYS_EXIT syscall1 + endif + clone ? heap_end = if + "! Reached end of heap @ " print heap_end printx + over incheap + 2clone swap BLOCK_USED + ! + "New layout: " println + " Start @ " print heap_start printx + " End @ " print heap_end printx + swap drop swap + return + endif + + clone get_block_status BLOCK_USED = if + "Next block, this one's used @ " print clone printx + else + "Block free @ " print clone printx + "Block had a size @ address " print clone printx + "Block size " print clone ? printx + + + over over ? < if + "Block was too small. Going to the next block" println + return + endif + over over ? = if + "Block was just right. Allocating" println + clone rev3 BLOCK_USED + ! + swap return + endif + over over ? > if + "Block was just right" println + return + endif + + + over + + [clone get_block_size 0 = if ] + ["Reached end of heap @ " print clone printx] + ["Increasing heap by bytes: " print over put] + [over incheap] + ["Writing new end pointer: " println] + [return] + [else] + [endif] + + endif + wend +marine + + +sub main + meminit + heap_start printx + heap_end printx + 15 malloc printx + 15 malloc printx +marine