translator, kotstd: malloc_heap
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
+42
-74
@@ -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
|
||||
|
||||
; <label>:7 ; preds = %0
|
||||
store i8* null, i8** %1
|
||||
br label %17
|
||||
|
||||
; <label>:8 ; preds = %0
|
||||
%9 = load i32* @static_area_ptr, align 4
|
||||
%10 = getelementptr inbounds i8* getelementptr inbounds ([30000 x i8]* @static_area, i32 0, i32 0), i32 %9
|
||||
%11 = ptrtoint i8* %10 to i32
|
||||
store i32 %11, i32* %result, align 4
|
||||
%12 = load i32* %2, align 4
|
||||
%13 = load i32* @static_area_ptr, align 4
|
||||
%14 = add nsw i32 %13, %12
|
||||
store i32 %14, i32* @static_area_ptr, align 4
|
||||
%15 = load i32* %result, align 4
|
||||
%16 = inttoptr i32 %15 to i8*
|
||||
store i8* %16, i8** %1
|
||||
br label %17
|
||||
|
||||
; <label>:17 ; preds = %8, %7
|
||||
%18 = load i8** %1
|
||||
ret i8* %18
|
||||
define void @clean_dynamic_heap() #0 {
|
||||
store i32 0, i32* getelementptr inbounds ([2 x i32]* @heap_tails, i32 0, i32 1), align 4
|
||||
ret void
|
||||
}
|
||||
|
||||
; Function Attrs: nounwind
|
||||
define weak i8* @malloc_dynamic(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* @dynamic_area_ptr, align 4
|
||||
%5 = add nsw i32 %3, %4
|
||||
%6 = icmp sgt i32 %5, 1000
|
||||
br i1 %6, label %7, label %8
|
||||
attributes #0 = { nounwind "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
|
||||
|
||||
; <label>:7 ; preds = %0
|
||||
store i8* null, i8** %1
|
||||
br label %17
|
||||
!llvm.module.flags = !{!0, !1}
|
||||
!llvm.ident = !{!2}
|
||||
|
||||
; <label>:8 ; preds = %0
|
||||
%9 = load i32* @dynamic_area_ptr, align 4
|
||||
%10 = getelementptr inbounds i8* getelementptr inbounds ([1000 x i8]* @dynamic_area, i32 0, i32 0), i32 %9
|
||||
%11 = ptrtoint i8* %10 to i32
|
||||
store i32 %11, i32* %result, align 4
|
||||
%12 = load i32* %2, align 4
|
||||
%13 = load i32* @dynamic_area_ptr, align 4
|
||||
%14 = add nsw i32 %13, %12
|
||||
store i32 %14, i32* @dynamic_area_ptr, align 4
|
||||
%15 = load i32* %result, align 4
|
||||
%16 = inttoptr i32 %15 to i8*
|
||||
store i8* %16, i8** %1
|
||||
br label %17
|
||||
|
||||
; <label>:17 ; preds = %8, %7
|
||||
%18 = load i8** %1
|
||||
ret i8* %18
|
||||
}
|
||||
|
||||
attributes #0 = { nounwind "less-precise-fpmad"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
|
||||
!0 = !{i32 1, !"wchar_size", i32 4}
|
||||
!1 = !{i32 1, !"min_enum_size", i32 4}
|
||||
!2 = !{!"Ubuntu clang version 3.6.2-3ubuntu2 (tags/RELEASE_362/final) (based on LLVM 3.6.2)"}
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
|
||||
declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture readonly, i64, i32, i1)
|
||||
declare i8* @malloc(i32) #0
|
||||
|
||||
define weak i8* @malloc_static(i32 %size) #0 {
|
||||
%1 = alloca i32, align 4
|
||||
store i32 %size, i32* %1, align 4
|
||||
%2 = load i32* %1, align 4
|
||||
%3 = call i8* @malloc(i32 %2)
|
||||
ret i8* %3
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
|
||||
#define STATIC_AREA_SIZE 30000
|
||||
#define DYNAMIC_AREA_SIZE 30000
|
||||
|
||||
#define STATIC_HEAP 0
|
||||
#define DYNAMIC_HEAP 1
|
||||
|
||||
#ifdef ARM
|
||||
|
||||
char static_area[STATIC_AREA_SIZE];
|
||||
char dynamic_area[DYNAMIC_AREA_SIZE];
|
||||
|
||||
char* heaps[2] = {
|
||||
(char*) static_area,
|
||||
(char*) dynamic_area
|
||||
};
|
||||
|
||||
int heap_tails[2] = {0, 0};
|
||||
int active_heap = STATIC_HEAP;
|
||||
|
||||
#else
|
||||
char* malloc(int);
|
||||
#endif
|
||||
|
||||
char* malloc_heap(int size) {
|
||||
#ifdef ARM
|
||||
char* ptr = heaps[active_heap] + heap_tails[active_heap];
|
||||
heap_tails[active_heap] += size;
|
||||
|
||||
return ptr;
|
||||
|
||||
#else
|
||||
return malloc(size);
|
||||
#endif
|
||||
}
|
||||
|
||||
void set_active_heap(int heap) {
|
||||
#ifdef ARM
|
||||
active_heap = heap;
|
||||
#endif
|
||||
}
|
||||
|
||||
void clean_dynamic_heap() {
|
||||
#ifdef ARM
|
||||
heap_tails[DYNAMIC_HEAP] = 0;
|
||||
#endif
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
-1
@@ -10,7 +10,6 @@
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="module" module-name="translator_main" />
|
||||
<orderEntry type="library" name="Gradle: org.jetbrains.kotlin:kotlin-stdlib:1.0.1" level="project" />
|
||||
<orderEntry type="library" name="Gradle: org.jetbrains.kotlin:kotlin-compiler:1.0.3" level="project" />
|
||||
<orderEntry type="library" name="Gradle: com.github.jshmrsn:karg:a636b3e" level="project" />
|
||||
|
||||
@@ -20,7 +20,7 @@ class LLVMBuilder(val arm: Boolean = false) {
|
||||
private fun initBuilder() {
|
||||
val declares = arrayOf(
|
||||
"declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture readonly, i64, i32, i1)",
|
||||
"declare i8* @malloc_static(i32)",
|
||||
"declare i8* @malloc_heap(i32)",
|
||||
"declare i32 @printf(i8*, ...)",
|
||||
"%class.Nothing = type { }",
|
||||
"declare void @abort()")
|
||||
@@ -38,7 +38,7 @@ class LLVMBuilder(val arm: Boolean = false) {
|
||||
|
||||
fun getNewVariable(type: LLVMType, pointer: Int = 0, kotlinName: String? = null, scope: LLVMScope = LLVMRegisterScope(), prefix: String = "var"): LLVMVariable {
|
||||
variableCount++
|
||||
return LLVMVariable("${prefix}$variableCount", type, kotlinName, scope, pointer)
|
||||
return LLVMVariable("$prefix$variableCount", type, kotlinName, scope, pointer)
|
||||
}
|
||||
|
||||
fun getNewLabel(scope: LLVMScope = LLVMRegisterScope(), prefix: String): LLVMLabel {
|
||||
@@ -182,17 +182,17 @@ class LLVMBuilder(val arm: Boolean = false) {
|
||||
}
|
||||
|
||||
fun allocStackVar(target: LLVMVariable, asValue: Boolean = false) {
|
||||
localCode.appendln("$target = alloca ${if (asValue) target.type else target.getType()}, align ${target.type.align}")
|
||||
localCode.appendln("$target = alloca ${if (asValue) target.type.toString() else target.getType()}, align ${target.type.align}")
|
||||
}
|
||||
|
||||
fun allocStaticVar(target: LLVMVariable, asValue: Boolean = false) {
|
||||
val allocated = getNewVariable(LLVMCharType(), pointer = 1)
|
||||
|
||||
val size = if (target.pointer > 0) POINTER_SIZE else target.type.size
|
||||
val alloc = "$allocated = call i8* @malloc_static(i32 $size)"
|
||||
val alloc = "$allocated = call i8* @malloc_heap(i32 $size)"
|
||||
localCode.appendln(alloc)
|
||||
|
||||
val cast = "$target = bitcast ${allocated.getType()} $allocated to ${if (asValue) target.type else target.getType()}*"
|
||||
val cast = "$target = bitcast ${allocated.getType()} $allocated to ${if (asValue) target.type.toString() else target.getType()}*"
|
||||
localCode.appendln(cast)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
package org.kotlinnative.translator.llvm.types
|
||||
|
||||
import org.kotlinnative.translator.llvm.LLVMExpression
|
||||
import org.kotlinnative.translator.llvm.LLVMSingleValue
|
||||
|
||||
class LLVMNullType(var basetype: LLVMType? = null) : LLVMReferred, LLVMType() {
|
||||
override val align: Int = 1
|
||||
override var size: Int = 0
|
||||
|
||||
Reference in New Issue
Block a user