Skip to content

Commit

Permalink
Testing memory allocator
Browse files Browse the repository at this point in the history
  • Loading branch information
Kruhlmann committed Nov 26, 2024
1 parent 4f2feab commit 0f3ea34
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 4 deletions.
8 changes: 7 additions & 1 deletion bin/alchemyc
Original file line number Diff line number Diff line change
@@ -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);
}
2 changes: 1 addition & 1 deletion src/compiler/nasm/nasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class NasmCompiler implements Compiler<NasmCompilerParameters, string, st
const object_file = `${parameters.output_file}.o`;
Logger.debug(`Writing NASM-style assembly to ${asm_file}`);
fs.writeFileSync(asm_file, parameters.asm_source);
const nasm_result = new BinaryRuntime("nasm", ["-felf64", asm_file]).run();
const nasm_result = new BinaryRuntime("nasm", ["-felf64", "-g", "-F", "dwarf", asm_file]).run();
if (nasm_result.exit_code !== "0") {
throw new Error(`${this.constructor.name}: ${nasm_result.stderr}`);
}
Expand Down
8 changes: 8 additions & 0 deletions std/math.alc
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,11 @@ sub mod
2clone div_i_f
mul - swap
marine

[Aligns a value up to the next multiple of a given alignment]
[arg:int Alignment value]
[arg:int Value to align]
[ret:int Aligned value]
sub int_align
rev3 swap 2clone mod - + swap return
marine
10 changes: 8 additions & 2 deletions std/sys/mem.alc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
include std.io
include std.math
include std.bool
include std.size
include std.sys.call

Expand All @@ -20,7 +21,7 @@ sub heap_end
marine

[Increases the heap by a set amount of qwords]
[arg:int number of bytes]
[arg:int number of qwords]
[ret:void]
sub incheap
[16 bytes from the end is where the 0-sized memory block lives]
Expand All @@ -44,21 +45,26 @@ marine
[ret:ptr address of allocated block]
sub malloc
swap
"Allocating bytes: " print
clone put
clone 2 mod 1 = if
"You can't allocate an odd number of bytes" println
1 SYS_EXIT syscall1
endif
"Allocating from: " print heap_start printx
heap_start
while 1 do
while true do
clone ? 1 & BLOCK_FREE != if
clone ? u64_max & over + swap drop [Add the node size to the current node address]
"Block was not free. Going to the next one." println
else
clone ? u64_max &
swap rev3 2clone > if
swap drop swap
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
Expand Down
140 changes: 140 additions & 0 deletions test.alc
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 0f3ea34

Please sign in to comment.