Konstantin Anisimov d86b387f9c CODEGEN: Implemented support for "instanceOf", operations "Boolean" type implemented, operator NOT_INSTANCEOF implemented
for code
--------8<--------
> cat ../backend.native/tests/codegen/basics/check_type.kt
interface I
class A() : I {}
class B() {}

//-----------------------------------------------------------------------------//

fun isTypeOf(a: Any) : Boolean {
  return a is A
}

fun check_type(): Boolean {
  val a = A()
  return isTypeOf(a)
}

//-----------------------------------------------------------------------------//

fun isNotTypeOf(a: Any) : Boolean {
  return a !is A
}

fun check_not_type(): Boolean {
  val b = B()
  return isNotTypeOf(b)
}

//-----------------------------------------------------------------------------//

fun isTypeOfInterface(a: Any) : Boolean {
  return a is I
}

fun check_interface(): Boolean {
  val a = A()
  return isTypeOfInterface(a)
}
--------8<--------

translator generates:

--------8<--------
> llvm-dis-mp-3.8 ../backend.native/tests/codegen/basics/check_type.kt.bc -o -
; ModuleID = '../backend.native/tests/codegen/basics/check_type.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 i1 @"kfun:isTypeOf(Any)"(i8*) {
entry:
  %a = alloca i8*
  store i8* %0, i8** %a
  %tmp_1 = load i8*, i8** %a
  %tmp_2 = bitcast i8* %tmp_1 to %struct.ObjHeader*
  %tmp_3 = call i8 @IsInstance(%struct.ObjHeader* %tmp_2, %struct.TypeInfo* @"ktype:A")
  %tmp_0 = trunc i8 %tmp_3 to i1
  ret i1 %tmp_0
}
...
define i1 @"kfun:isNotTypeOf(Any)"(i8*) {
entry:
  %a = alloca i8*
  store i8* %0, i8** %a
  %tmp_2 = load i8*, i8** %a
  %tmp_3 = bitcast i8* %tmp_2 to %struct.ObjHeader*
  %tmp_4 = call i8 @IsInstance(%struct.ObjHeader* %tmp_3, %struct.TypeInfo* @"ktype:A")
  %tmp_1 = trunc i8 %tmp_4 to i1
  %tmp_0 = xor i1 %tmp_1, true
  ret i1 %tmp_0
}
...
define i1 @"kfun:isTypeOfInterface(Any)"(i8*) {
entry:
  %a = alloca i8*
  store i8* %0, i8** %a
  %tmp_1 = load i8*, i8** %a
  %tmp_2 = bitcast i8* %tmp_1 to %struct.ObjHeader*
  %tmp_3 = call i8 @IsInstance(%struct.ObjHeader* %tmp_2, %struct.TypeInfo* @"ktype:I")
  %tmp_0 = trunc i8 %tmp_3 to i1
  ret i1 %tmp_0
}

--------8<--------
2016-11-18 16:22:27 +03:00
2016-10-27 13:52:00 +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%