From 06147e2634bf189a207bc9844c3b4fe6135db8fa Mon Sep 17 00:00:00 2001 From: jespergravgaard Date: Thu, 23 May 2019 15:48:34 +0200 Subject: [PATCH] Moved malloc/free trivial implementations to stdlib --- .../java/dk/camelot64/kickc/Compiler.java | 11 +++++----- src/main/kc/stdlib/stdlib.kc | 20 ++++++++++++++++++ src/test/kc/memory-heap.kc | 21 ++----------------- src/test/ref/memory-heap.asm | 17 +++++++-------- 4 files changed, 35 insertions(+), 34 deletions(-) create mode 100644 src/main/kc/stdlib/stdlib.kc diff --git a/src/main/java/dk/camelot64/kickc/Compiler.java b/src/main/java/dk/camelot64/kickc/Compiler.java index 8d964707d..667ad696e 100644 --- a/src/main/java/dk/camelot64/kickc/Compiler.java +++ b/src/main/java/dk/camelot64/kickc/Compiler.java @@ -229,11 +229,6 @@ public class Compiler { } } - private void pass2Optimize() { - List optimizations = getPass2Optimizations(); - pass2Execute(optimizations); - } - private List getPass2Optimizations() { List optimizations = new ArrayList<>(); optimizations.add(new Pass2FixInlineConstructorsNew(program)); @@ -271,6 +266,11 @@ public class Compiler { return optimizations; } + private void pass2Optimize() { + List optimizations = getPass2Optimizations(); + pass2Execute(optimizations); + } + private void pass2UnrollLoops() { List loopUnrolling = new ArrayList<>(); loopUnrolling.add(new PassNStatementIndices(program)); @@ -299,7 +299,6 @@ public class Compiler { private void pass2InlineConstants() { // Constant inlining optimizations - as the last step to ensure that constant identification has been completed - List constantOptimizations = new ArrayList<>(); constantOptimizations.add(new PassNStatementIndices(program)); constantOptimizations.add(new PassNVariableReferenceInfos(program)); diff --git a/src/main/kc/stdlib/stdlib.kc b/src/main/kc/stdlib/stdlib.kc new file mode 100644 index 000000000..bd6686a8f --- /dev/null +++ b/src/main/kc/stdlib/stdlib.kc @@ -0,0 +1,20 @@ +// Implementation of functions found int C stdlib.h / stdlib.c + +// Start of the heap used by malloc() +unsigned char* HEAP_START = 0xc000; + +// Head of the heap. Moved forward for each malloc() +unsigned char* heap_head = HEAP_START; + +// Allocates a block of size bytes of memory, returning a pointer to the beginning of the block. +// The content of the newly allocated block of memory is not initialized, remaining with indeterminate values. +unsigned char* malloc(unsigned int size) { + unsigned char* mem = heap_head; + heap_head+= size; + return mem; +} + +// A block of memory previously allocated by a call to malloc is deallocated, making it available again for further allocations. +// If ptr is a null pointer, the function does nothing. +void free(unsigned char* ptr) { +} \ No newline at end of file diff --git a/src/test/kc/memory-heap.kc b/src/test/kc/memory-heap.kc index 2fa915342..5c62db2fd 100644 --- a/src/test/kc/memory-heap.kc +++ b/src/test/kc/memory-heap.kc @@ -1,5 +1,7 @@ // Experiments with malloc() +import "stdlib" + void main() { unsigned char* buf1 = (unsigned char*) malloc(100); unsigned char* buf2 = (unsigned char*) malloc(100); @@ -13,22 +15,3 @@ void main() { screen[0] = *buf1; screen[1] = *buf2; } - -// Start of the heap used by malloc() -unsigned char* HEAP_START = 0xc000; - -// Head of the heap. Moved forward for each malloc() -unsigned char* heap_head = HEAP_START; - -// Allocates a block of size bytes of memory, returning a pointer to the beginning of the block. -// The content of the newly allocated block of memory is not initialized, remaining with indeterminate values. -unsigned char* malloc(unsigned int size) { - unsigned char* mem = heap_head; - heap_head+= size; - return mem; -} - -// A block of memory previously allocated by a call to malloc is deallocated, making it available again for further allocations. -// If ptr is a null pointer, the function does nothing. -void free(unsigned char* ptr) { -} \ No newline at end of file diff --git a/src/test/ref/memory-heap.asm b/src/test/ref/memory-heap.asm index 769942503..c07e8c978 100644 --- a/src/test/ref/memory-heap.asm +++ b/src/test/ref/memory-heap.asm @@ -14,10 +14,10 @@ main: { lda #>HEAP_START sta heap_head+1 jsr malloc - lda malloc.return_2 - sta malloc.return - lda malloc.return_2+1 - sta malloc.return+1 + lda malloc.return + sta malloc.return_2 + lda malloc.return+1 + sta malloc.return_2+1 jsr malloc ldy #0 b1: @@ -48,13 +48,12 @@ free: { // Allocates a block of size bytes of memory, returning a pointer to the beginning of the block. // The content of the newly allocated block of memory is not initialized, remaining with indeterminate values. malloc: { - .label return = 4 - .label return_1 = 6 - .label return_2 = 6 + .label return = 6 + .label return_2 = 4 lda heap_head - sta return_2 + sta return lda heap_head+1 - sta return_2+1 + sta return+1 lda #$64 clc adc heap_head