1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2024-11-27 04:49:27 +00:00

Moved malloc/free trivial implementations to stdlib

This commit is contained in:
jespergravgaard 2019-05-23 15:48:34 +02:00
parent 430c128cc3
commit 06147e2634
4 changed files with 35 additions and 34 deletions

View File

@ -229,11 +229,6 @@ public class Compiler {
} }
} }
private void pass2Optimize() {
List<Pass2SsaOptimization> optimizations = getPass2Optimizations();
pass2Execute(optimizations);
}
private List<Pass2SsaOptimization> getPass2Optimizations() { private List<Pass2SsaOptimization> getPass2Optimizations() {
List<Pass2SsaOptimization> optimizations = new ArrayList<>(); List<Pass2SsaOptimization> optimizations = new ArrayList<>();
optimizations.add(new Pass2FixInlineConstructorsNew(program)); optimizations.add(new Pass2FixInlineConstructorsNew(program));
@ -271,6 +266,11 @@ public class Compiler {
return optimizations; return optimizations;
} }
private void pass2Optimize() {
List<Pass2SsaOptimization> optimizations = getPass2Optimizations();
pass2Execute(optimizations);
}
private void pass2UnrollLoops() { private void pass2UnrollLoops() {
List<Pass2SsaOptimization> loopUnrolling = new ArrayList<>(); List<Pass2SsaOptimization> loopUnrolling = new ArrayList<>();
loopUnrolling.add(new PassNStatementIndices(program)); loopUnrolling.add(new PassNStatementIndices(program));
@ -299,7 +299,6 @@ public class Compiler {
private void pass2InlineConstants() { private void pass2InlineConstants() {
// Constant inlining optimizations - as the last step to ensure that constant identification has been completed // Constant inlining optimizations - as the last step to ensure that constant identification has been completed
List<Pass2SsaOptimization> constantOptimizations = new ArrayList<>(); List<Pass2SsaOptimization> constantOptimizations = new ArrayList<>();
constantOptimizations.add(new PassNStatementIndices(program)); constantOptimizations.add(new PassNStatementIndices(program));
constantOptimizations.add(new PassNVariableReferenceInfos(program)); constantOptimizations.add(new PassNVariableReferenceInfos(program));

View File

@ -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) {
}

View File

@ -1,5 +1,7 @@
// Experiments with malloc() // Experiments with malloc()
import "stdlib"
void main() { void main() {
unsigned char* buf1 = (unsigned char*) malloc(100); unsigned char* buf1 = (unsigned char*) malloc(100);
unsigned char* buf2 = (unsigned char*) malloc(100); unsigned char* buf2 = (unsigned char*) malloc(100);
@ -13,22 +15,3 @@ void main() {
screen[0] = *buf1; screen[0] = *buf1;
screen[1] = *buf2; 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) {
}

View File

@ -14,10 +14,10 @@ main: {
lda #>HEAP_START lda #>HEAP_START
sta heap_head+1 sta heap_head+1
jsr malloc jsr malloc
lda malloc.return_2 lda malloc.return
sta malloc.return sta malloc.return_2
lda malloc.return_2+1 lda malloc.return+1
sta malloc.return+1 sta malloc.return_2+1
jsr malloc jsr malloc
ldy #0 ldy #0
b1: b1:
@ -48,13 +48,12 @@ free: {
// Allocates a block of size bytes of memory, returning a pointer to the beginning of the block. // 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. // The content of the newly allocated block of memory is not initialized, remaining with indeterminate values.
malloc: { malloc: {
.label return = 4 .label return = 6
.label return_1 = 6 .label return_2 = 4
.label return_2 = 6
lda heap_head lda heap_head
sta return_2 sta return
lda heap_head+1 lda heap_head+1
sta return_2+1 sta return+1
lda #$64 lda #$64
clc clc
adc heap_head adc heap_head