Commit Graph

199 Commits

Author SHA1 Message Date
Alexander Gorshenev b0d10e45d8 changed DynamicTypesAllowed() to DynamicTypesSettings() in KonanPlatformConfigurator.kt 2016-11-18 19:11:38 +04:00
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
Konstantin Anisimov c1bb762417 TESTS: "for" and INSTANCEOF, NOT_INSTANCEOF, isInstanceOf on interface are added 2016-11-18 16:22:27 +03:00
Konstantin Anisimov 3a49e69046 Support for objects' cast implemented
for code

--------8<--------
> cat ../backend.native/tests/codegen/basics/cast_simple.kt
open class A() {}
class B(): A() {}

fun castSimple(o: Any) : A = o as A

fun castTest(): Boolean {
  val b = B()
  castSimple(b)
  return true
}
--------8<--------
translator generates

--------8<--------
>llvm-dis-mp-3.8 ../backend.native/tests/codegen/basics/cast_simple.kt.bc -o -
; ModuleID = '../backend.native/tests/codegen/basics/cast_simple.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:castSimple(Any)"(i8*) {
entry:
  %o = alloca i8*
  store i8* %0, i8** %o
  %tmp_1 = load i8*, i8** %o
  %tmp_2 = bitcast i8* %tmp_1 to %struct.ObjHeader*
  call void @CheckInstance(%struct.ObjHeader* %tmp_2, %struct.TypeInfo* @"ktype:A")
  ret i8* %tmp_1
}
--------8<--------
2016-11-18 15:47:28 +03:00
Konstantin Anisimov 9c28555e55 Test for cast added 2016-11-18 15:47:28 +03:00
Konstantin Anisimov 103509d75d Tests fixed 2016-11-18 14:30:32 +03:00
Vasily Levchenko f588814927 tests: disable method_call test until #74 won't be applied 2016-11-18 13:56:14 +03:00
Vasily Levchenko 514d1a16ec test: minus_eq fix 2016-11-18 13:54:34 +03:00
Konstantin Anisimov eb2046fe5d Tests fixed 2016-11-18 12:57:47 +03:00
Konstantin Anisimov 04d441ef21 Test fixed 2016-11-18 12:44:00 +03:00
Vasily Levchenko 8b37001cdf typo in unit test runner 2016-11-18 12:30:27 +03:00
Vasily Levchenko 26710e52a1 use exit(3) in unit tests 2016-11-18 12:27:13 +03:00
Vasily Levchenko 72ef6d43a9 let processing IrWhen as any level expresssion
(cherry picked from commit 00bf82fc5c1a67af16aa2c9c386da8e873a02449)
2016-11-18 06:21:51 +03:00
Alexander Gorshenev 8321c14e36 Introduced KonanPlatform, KonanConfig, TopDownAnalyzerFacadeForKonan
and several other supporting facilities
2016-11-17 18:16:15 +04:00
Vasily Levchenko cae9094705 cosmetic: visitField log message formating 2016-11-17 14:57:19 +03:00
Vasily Levchenko 4495c86c2c call of void function
(cherry picked from commit feeacab2bb6e1789e91075dc1b519340cd4f4172)
2016-11-17 14:45:31 +03:00
Vasily Levchenko e4c384e99a replace in-place generation llvm values for true and false with constants
(cherry picked from commit 4a16460b2d778e9406b805f7b6b517b162e77dd8)
2016-11-17 14:45:31 +03:00
Nikolay Igotti fa7bcd90e3 Remove duplicated test. 2016-11-17 14:27:45 +03:00
Alexander Gorshenev 16e4107dee kotlin-compiler: 1.1-20161116.235936-256 2016-11-17 14:56:07 +04:00
Konstantin Anisimov 0fff59a516 Support for object properties access implemented. Minor refactoring
following code:

--------8<--------
> cat ../backend.native/tests/codegen/object/method_call.kt
class A(val a:Int) {
  fun foo(i:Int) = a + i
}

fun fortyTwo() = A(41).foo(1)
--------8<--------

translator generates following getter

--------8<--------
> llvm-dis-mp-3.8 ../backend.native/tests/codegen/object/method_call.kt.bc -o -
; ModuleID = '../backend.native/tests/codegen/object/method_call.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 i32 @"kfun:A.<get-a>()"(i8*) {
entry:
  %this = alloca i8*
  store i8* %0, i8** %this
  %tmp_1 = load i8*, i8** %this
  %tmp_2 = bitcast i8* %tmp_1 to %struct.ObjHeader*
  %tmp_3 = getelementptr %struct.ObjHeader, %struct.ObjHeader* %tmp_2, i32 1
  %tmp_4 = bitcast %struct.ObjHeader* %tmp_3 to %"kclass:A"*
  %tmp_5 = getelementptr inbounds %"kclass:A", %"kclass:A"* %tmp_4, i32 0, i32 0
  %tmp_6 = load i32, i32* %tmp_5
  ret i32 %tmp_6
}
...
--------8<--------

similar history for global properties:

--------8<--------
> cat ../backend.native/tests/codegen/object/fields.kt
private var globalValue = 1
var global:Int
    get() = globalValue
    set(value:Int) {globalValue = value}

fun globalTest(i:Int):Int {
    global += i
    return global
}
--------8<--------

translator generates following code:

--------8<--------
> llvm-dis-mp-3.8 ../backend.native/tests/codegen/object/fields.kt.bc -o -
; ModuleID = '../backend.native/tests/codegen/object/fields.kt.bc'
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-macosx10.12.0"

...

@"kvar:<root>.globalValue" = global i32 1

...

define i32 @"kfun:<get-globalValue>()"() {
entry:
  %tmp_1 = load i32, i32* @"kvar:<root>.globalValue"
  ret i32 %tmp_1
}

define void @"kfun:<set-globalValue>(Int)"(i32) {
entry:
  %"<set-?>" = alloca i32
  store i32 %0, i32* %"<set-?>"
  %tmp_0 = load i32, i32* %"<set-?>"
  store i32 %tmp_0, i32* @"kvar:<root>.globalValue"
  ret void
}

define i32 @"kfun:<get-global>()"() {
entry:
  %tmp_0 = call i32 @"kfun:<get-globalValue>()"()
  ret i32 %tmp_0
}

define void @"kfun:<set-global>(Int)"(i32) {
entry:
  %value = alloca i32
  store i32 %0, i32* %value
  %tmp_0 = load i32, i32* %value
  call void @"kfun:<set-globalValue>(Int)"(i32 %tmp_0)
  ret void
}

--------8<--------
2016-11-17 12:25:12 +03:00
Konstantin Anisimov f80697817e Test for getters/setters added 2016-11-17 12:25:12 +03:00
Svyatoslav Scherbina 69ee5a619a backend: add tests for === 2016-11-17 12:17:47 +07:00
Svyatoslav Scherbina 5db25a3985 backend: add tests for duplicated string literals 2016-11-17 12:17:47 +07:00
Svyatoslav Scherbina c622d4cd02 backend: implement === operator 2016-11-17 12:17:47 +07:00
Svyatoslav Scherbina e5d4dc932b backend: don't generate the same string literal twice 2016-11-17 12:17:47 +07:00
Nikolay Igotti 16a44ef765 Fix test that would require autoboxing. 2016-11-16 16:28:25 +03:00
Nikolay Igotti 26e8b4860a Enable array1 test. 2016-11-16 16:24:33 +03:00
Vasily Levchenko ab83f5b681 pasto fixed in const evaluation 2016-11-16 15:11:44 +03:00
Nikolay Igotti 0f12daa591 Fix expectations. 2016-11-16 11:23:05 +03:00
Svyatoslav Scherbina 1ff59e2cd2 backend: add some trivial tests for RTTI 2016-11-16 14:58:57 +07:00
Svyatoslav Scherbina f147860fea backend: fix some bugs and improve RTTI
* fix incorrect assert at RTTIGenerator.kt:109
* add `final override` methods to methods table (aka openMethods)
* fix NPE when building vtable  with `final override` methods
* make vtable generation impl more clear
2016-11-16 14:58:57 +07:00
Nikolay Igotti db15540a74 Review feedback on previous changes. (#64) 2016-11-16 10:46:56 +03:00
SvyatoslavScherbina 755194486b backend, runtime: improve support for abstract methods (#62)
backend, runtime: improve support for abstract methods

Do not generate vtable and openMethods for abstract classes and interfaces
2016-11-15 17:30:49 +07:00
Nikolay Igotti c5cfa0ac4f Add String.subSequence implementation. (#61) 2016-11-15 13:27:21 +03:00
Konstantin Anisimov 69b7cdb866 Support for built in types com parision implemented
for following code snipped:
---------------8<---------------
> cat ../backend.native/tests/codegen/function/eqeq.kt
fun eqeqB  (a:Byte,   b:Byte  ) = a == b
fun eqeqS  (a:Short,  b:Short ) = a == b
fun eqeqI  (a:Int,    b:Int   ) = a == b
fun eqeqL  (a:Long,   b:Long  ) = a == b
fun eqeqF  (a:Float,  b:Float ) = a == b
fun eqeqD  (a:Double, b:Double) = a == b
fun eqeqStr(a:String, b:String) = a == b

fun gtI  (a:Int,    b:Int   ) = a >  b
fun ltI  (a:Int,    b:Int   ) = a >  b
fun geI  (a:Int,    b:Int   ) = a >= b
fun leI  (a:Int,    b:Int   ) = a <= b
fun neI  (a:Int,    b:Int   ) = a != b

fun gtF  (a:Float,  b:Float ) = a >  b
fun ltF  (a:Float,  b:Float ) = a >  b
fun geF  (a:Float,  b:Float ) = a >= b
fun leF  (a:Float,  b:Float ) = a <= b
fun neF  (a:Float,  b:Float ) = a != b

fun helloString()   =  "Hello"
fun goodbyeString() =  "Goodbye"

---------------8<---------------

interesting cases here are '==' for primitive and objects and any < =< > >= and separatelly !=

---------------8<---------------
; ModuleID = '../backend.native/tests/codegen/function/eqeq.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:eqeqB(Byte;Byte)"(i8, i8) {
entry:
  %a = alloca i8
  store i8 %0, i8* %a
  %b = alloca i8
  store i8 %1, i8* %b
  %tmp_1 = load i8, i8* %a
  %tmp_2 = load i8, i8* %b
  %tmp_0 = icmp eq i8 %tmp_1, %tmp_2 <- '==' for primitive
  ret i1 %tmp_0
}
...

define i1 @"kfun:eqeqStr(String;String)"(i8*, i8*) {
entry:
  %a = alloca i8*
  store i8* %0, i8** %a
  %b = alloca i8*
  store i8* %1, i8** %b
  %tmp_1 = load i8*, i8** %a
  %tmp_2 = load i8*, i8** %b
  %tmp_0 = call i1 @Kotlin_String_equals(i8* %tmp_1, i8* %tmp_2) <-- '==' for object
  ret i1 %tmp_0
}
...

define i1 @"kfun:gtI(Int;Int)"(i32, i32) {
entry:
  %a = alloca i32
  store i32 %0, i32* %a
  %b = alloca i32
  store i32 %1, i32* %b
  %tmp_2 = load i32, i32* %a
  %tmp_3 = load i32, i32* %b
  %tmp_1 = call i32 @Kotlin_Int_compareTo_Int(i32 %tmp_2, i32 %tmp_3) <-- '>' for primitive
  %tmp_0 = icmp sgt i32 %tmp_1, 0                                     <--/
  ret i1 %tmp_0
}
...

define i1 @"kfun:neI(Int;Int)"(i32, i32) {
entry:
  %a = alloca i32
  store i32 %0, i32* %a
  %b = alloca i32
  store i32 %1, i32* %b
  %tmp_2 = load i32, i32* %a
  %tmp_3 = load i32, i32* %b
  %tmp_1 = icmp eq i32 %tmp_2, %tmp_3  <---  '!=' fro primitive
  %tmp_0 = icmp ne i1 %tmp_1, true     <--/
  ret i1 %tmp_0
}

---------------8<---------------
2016-11-15 09:41:25 +03:00
Konstantin Anisimov 04822dda69 Tests for integer cmp operation added 2016-11-15 09:41:25 +03:00
Konstantin Anisimov 59fff52e0c to_master: formatting fix
(cherry picked from commit f7b1ce10f84371bc9f00e91c1f1fb7ed56df9a87)
2016-11-15 09:22:57 +03:00
Vasily Levchenko c99baeaf5d kotlin-compiler: 1.1-20161111.171344-253 2016-11-14 14:41:22 +03:00
Nikolay Igotti ae5e838305 Add overrides by name. (#54) 2016-11-14 13:25:50 +03:00
SvyatoslavScherbina 248d9ad2ef Merge pull request #56 from JetBrains/add-fun-signatures
Add function signatures to symbol names and RTTI
2016-11-14 14:13:12 +07:00
Svyatoslav Scherbina 3d36973022 backend/build: improve LLVM link options
Update options according to llvm-config:

* delete "--stdlib=libc++"  (it seems to provoke runtime link errors on Linux)
* remove "-lffi" (it doesn't seem to be required)
* remove "-lc++" on macOS

Also move Linux system libs options to llvm.def (as for macOS)
2016-11-14 14:12:25 +07:00
Konstantin Anisimov d1a303bb25 Refactoring:
1. generator -> codegen
2. tmpVariable -> newVar
3. All calls for LLVM* functions migrated to CodeGenerator
4. mutable list -> list for arguments.
2016-11-14 10:01:23 +03:00
Svyatoslav Scherbina f389ebf687 backend/tests: update resolved symbol names
according to new mangling scheme
2016-11-14 13:33:47 +07:00
Svyatoslav Scherbina cc4f0ca1a4 backend: add basic support for override by name 2016-11-14 13:31:14 +07:00
Konstantin Anisimov c47a58d34f codegen: implementation virtual calls and bit refactoring... callVirtual and callDirect become members of Ir2Bitcode.
some details: this is runtime based function resolving will be replaced with compile time soon.

example:
-----------------8<-----------------
> cat ../backend.native/tests/runtime/basic/hello2.kt
// TODO: remove kotlin_native.io once overrides are in place.
fun main(args : Array<String>) {
  print("you entered '" + readLine() + "'")
}
-----------------8<-----------------
translator generates following (reduced)
-----------------8<-----------------
> llvm-dis-mp-3.8  ../backend.native/tests/runtime/basic/hello2.kt.bc -o -
; ModuleID = '../backend.native/tests/runtime/basic/hello2.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 void @"kfun:main"(i8*) {
entry:
  %args = alloca i8*
  store i8* %0, i8** %args
  %tmp_3 = call i8* @Kotlin_io_Console_readLine()
  %tmp_1 = call i8* @"kfun:kotlin.String.plus"(i8* @"kstr:bwW55yLpGKEB25tD4IQg8ZiUSe0=", i8* %tmp_3)
  %tmp_0 = call i8* @"kfun:kotlin.String.plus"(i8* %tmp_1, i8* @"kstr:u1idBiHlRy9HD6NCWiNMdLHiAug=")
  call void @Kotlin_io_Console_print(i8* %tmp_0)
  ret void
}

declare i8* @Kotlin_io_Console_readLine()

define i8* @"kfun:kotlin.String.plus"(i8*, i8*) {
entry:
  %this = alloca i8*
  store i8* %0, i8** %this
  %other = alloca i8*
  store i8* %1, i8** %other
  %tmp_1 = load i8*, i8** %this
  %tmp_3 = load i8*, i8** %other
  %tmp_4 = bitcast i8* %tmp_3 to i8**
  %tmp_5 = load i8*, i8** %tmp_4
  %tmp_6 = bitcast i8* %tmp_5 to %struct.TypeInfo*
  %tmp_7 = call i8* @LookupOpenMethod(%struct.TypeInfo* %tmp_6, i64 5382438289122769377) <-- runtime resolve method
  %tmp_8 = bitcast i8* %tmp_7 to i8* (i8*)*
  %tmp_2 = call i8* %tmp_8(i8* %tmp_3)
  %tmp_0 = call i8* @Kotlin_String_plusImpl(i8* %tmp_1, i8* %tmp_2)
  ret i8* %tmp_0
}

-----------------8<-----------------
2016-11-11 17:43:44 +03:00
Konstantin Anisimov 24e0ae7d00 rtti: introduces functionName extension function to FunctionDescriptor for filling vtable with hashcode and reusing it in codegeneration 2016-11-11 17:43:44 +03:00
Konstantin Anisimov fa3943c8bd TEST: enabled hello2 test case 2016-11-11 17:43:44 +03:00
Nikolay Igotti 4b7f67d976 Fix array accessor signatures, add array test to fix. 2016-11-11 16:21:48 +03:00
Svyatoslav Scherbina edf3708515 build: fix linking tests on Linux
runtime now requires "-lm"
2016-11-11 19:22:34 +07:00
Svyatoslav Scherbina bee2a76008 build: refactor platform checks
also improve verbosity when downloading dependencies fails
2016-11-11 19:22:34 +07:00