Vasily Levchenko 2a0d01191a CODEGEN: primary/secondary constructor fields initialization
for code:
--------8<--------
>cat ../backend.native/tests/codegen/object/fields1.kt
class B(val a:Int, b:Int) {
    constructor(pos:Int):this(1, pos) {}
    val pos = b + 1
}

fun primaryConstructorCall(a:Int, b:Int) = B(a, b).pos

fun secondaryConstructorCall(a:Int) = B(a).pos
--------8<--------
translator generates:
--------8<--------
; ModuleID = '../backend.native/tests/codegen/object/fields1.kt.bc'
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-macosx10.12.0"
...
define i8* @"kfun:B.<init>(Int;Int)"(i8*, i32, i32) {                          ;primary constructor
entry:
  %this = alloca i8*
  store i8* %0, i8** %this
  %a = alloca i32
  store i32 %1, i32* %a
  %b = alloca i32
  store i32 %2, i32* %b
  %tmp_0 = load i8*, i8** %this
  %tmp_2 = load i8*, i8** %this
  %tmp_1 = call i8* @"kfun:kotlin.Any.<init>()"(i8* %tmp_2)
  %tmp_3 = bitcast i8* %tmp_0 to %struct.ObjHeader*
  %tmp_4 = getelementptr %struct.ObjHeader, %struct.ObjHeader* %tmp_3, i32 1
  %tmp_5 = bitcast %struct.ObjHeader* %tmp_4 to %"kclass:B"*
  %tmp_6 = getelementptr inbounds %"kclass:B", %"kclass:B"* %tmp_5, i32 0, i32 0
  %tmp_7 = load i32, i32* %a
  store i32 %tmp_7, i32* %tmp_6
  %tmp_8 = bitcast i8* %tmp_0 to %struct.ObjHeader*
  %tmp_9 = getelementptr %struct.ObjHeader, %struct.ObjHeader* %tmp_8, i32 1
  %tmp_10 = bitcast %struct.ObjHeader* %tmp_9 to %"kclass:B"*
  %tmp_11 = getelementptr inbounds %"kclass:B", %"kclass:B"* %tmp_10, i32 0, i32 1
  %tmp_13 = load i32, i32* %b
  %tmp_12 = call i32 @Kotlin_Int_plus_Int(i32 %tmp_13, i32 1)
  store i32 %tmp_12, i32* %tmp_11                                 ; <-- store b + 1 value to pos field
  ret i8* %tmp_0
}

...

define i8* @"kfun:B.<init>(Int)"(i8*, i32) {                      ;secondary constructor
entry:
  %this = alloca i8*
  store i8* %0, i8** %this
  %pos = alloca i32
  store i32 %1, i32* %pos
  %tmp_0 = load i8*, i8** %this
  %tmp_3 = load i32, i32* %pos
  %tmp_4 = load i8*, i8** %this
  %tmp_1 = call i8* @"kfun:B.<init>(Int;Int)"(i8* %tmp_4, i32 1, i32 %tmp_3) ; call primary constructor
  ret i8* %tmp_0
}

--------8<--------
2016-11-24 15:52:34 +03:00
2016-10-27 13:52:00 +03:00
2016-11-23 13:08:55 +03:00
2016-10-07 14:14:00 +03:00
2016-10-27 13:52:00 +03:00
2016-10-27 13:52:00 +03:00

Kotlin-native backend

Build

Download dependencies:

gradle :dependencies:update

To run native translator just use:

gradle :backend.native:run

And it will run simple example (currently prints out IR of test file). For more tests, use:

gradle :backend.native:tests:run
S
Description
The Kotlin Programming Language.
Readme 2.1 GiB
Languages
Kotlin 79.9%
Java 10.4%
Swift 4.3%
C 2.8%
C++ 2.1%
Other 0.3%