diff --git a/kotstd/Makefile b/kotstd/Makefile index c9232399e05..f73467ffa91 100644 --- a/kotstd/Makefile +++ b/kotstd/Makefile @@ -3,10 +3,14 @@ BUILD_DIR=$(PWD)/build INCLUDE_DIR=$(PWD)/include LIB_ARM_DIR=$(PWD)/lib/arm LIB_X86_DIR=$(PWD)/lib/x86 +LIBC=$(PWD)/libc KT=$(PWD)/../translator/build/libs/translator-1.0.jar LLINK=llvm-link-3.6 +CC=clang-3.6 +CCFLAGS_ARM=-S -Wall -m32 -emit-llvm -nostdlib -ffreestanding -march=armv7-m -mthumb -flto -O0 -target arm-none-eabi -DARM +CCFLAGS=-O0 -S -Wall -emit-llvm -nostdlib -ffreestanding LLINK_FLAGS=-S KT_ALL_DEPS=java -jar $(KT) @@ -29,6 +33,10 @@ $(BUILD_DIR)/classes_x86.ll: $(INCLUDE_DIR)/*.kt $(BUILD_DIR)/classes_arm.ll: $(INCLUDE_DIR)/*.kt $(KT_ALL_DEPS) --arm -o $@ $(filter %.kt,$^) +memory: + $(CC) $(CCFLAGS) $(LIBC)/memory.c -o $(LIB_X86_DIR)/memory.ll + $(CC) $(CCFLAGS_ARM) $(LIBC)/memory.c -o $(LIB_ARM_DIR)/memory.ll + clean: rm -rf $(BUILD_DIR) diff --git a/kotstd/lib/arm/array_c.ll b/kotstd/lib/arm/array_c.ll index 693fc751934..dc6cc482d4b 100644 --- a/kotstd/lib/arm/array_c.ll +++ b/kotstd/lib/arm/array_c.ll @@ -7,12 +7,12 @@ define weak i32 @malloc_array(i32 %x) #0 { %1 = alloca i32, align 4 store i32 %x, i32* %1, align 4 %2 = load i32* %1, align 4 - %3 = call i8* @malloc_static(i32 %2) #2 + %3 = call i8* @malloc_heap(i32 %2) #2 %4 = ptrtoint i8* %3 to i32 ret i32 %4 } -declare i8* @malloc_static(i32) #1 +declare i8* @malloc_heap(i32) #1 ; Function Attrs: nounwind define weak zeroext i8 @kotlinclib_get_byte(i32 %data, i32 %index) #0 { diff --git a/kotstd/lib/arm/memory.ll b/kotstd/lib/arm/memory.ll index 2ae11ef2d8a..4a7be5c0384 100644 --- a/kotstd/lib/arm/memory.ll +++ b/kotstd/lib/arm/memory.ll @@ -1,88 +1,56 @@ - -declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture readonly, i64, i32, i1) - +; ModuleID = '/home/user/Documents/carkot/kotstd/libc/memory.c' target datalayout = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64" target triple = "thumbv7m-none--eabi" -@static_area_ptr = global i32 0, align 4 -@dynamic_area_ptr = global i32 0, align 4 @static_area = common global [30000 x i8] zeroinitializer, align 1 -@dynamic_area = common global [1000 x i8] zeroinitializer, align 1 +@dynamic_area = common global [30000 x i8] zeroinitializer, align 1 +@heaps = global [2 x i8*] [i8* getelementptr inbounds ([30000 x i8]* @static_area, i32 0, i32 0), i8* getelementptr inbounds ([30000 x i8]* @dynamic_area, i32 0, i32 0)], align 4 +@heap_tails = global [2 x i32] zeroinitializer, align 4 +@active_heap = global i32 0, align 4 ; Function Attrs: nounwind -define weak void @init_dynamic_area() #0 { - store i32 0, i32* @dynamic_area_ptr, align 4 +define i8* @malloc_heap(i32 %size) #0 { + %1 = alloca i32, align 4 + %ptr = alloca i8*, align 4 + store i32 %size, i32* %1, align 4 + %2 = load i32* @active_heap, align 4 + %3 = getelementptr inbounds [2 x i8*]* @heaps, i32 0, i32 %2 + %4 = load i8** %3, align 4 + %5 = load i32* @active_heap, align 4 + %6 = getelementptr inbounds [2 x i32]* @heap_tails, i32 0, i32 %5 + %7 = load i32* %6, align 4 + %8 = getelementptr inbounds i8* %4, i32 %7 + store i8* %8, i8** %ptr, align 4 + %9 = load i32* %1, align 4 + %10 = load i32* @active_heap, align 4 + %11 = getelementptr inbounds [2 x i32]* @heap_tails, i32 0, i32 %10 + %12 = load i32* %11, align 4 + %13 = add nsw i32 %12, %9 + store i32 %13, i32* %11, align 4 + %14 = load i8** %ptr, align 4 + ret i8* %14 +} + +; Function Attrs: nounwind +define void @set_active_heap(i32 %heap) #0 { + %1 = alloca i32, align 4 + store i32 %heap, i32* %1, align 4 + %2 = load i32* %1, align 4 + store i32 %2, i32* @active_heap, align 4 ret void } ; Function Attrs: nounwind -define weak i8* @malloc_static(i32 %size) #0 { - %1 = alloca i8*, align 4 - %2 = alloca i32, align 4 - %result = alloca i32, align 4 - store i32 %size, i32* %2, align 4 - %3 = load i32* %2, align 4 - %4 = load i32* @static_area_ptr, align 4 - %5 = add nsw i32 %3, %4 - %6 = icmp sgt i32 %5, 30000 - br i1 %6, label %7, label %8 - -;