diff options
author | Ayke van Laethem <[email protected]> | 2021-03-24 16:07:44 +0100 |
---|---|---|
committer | Ron Evans <[email protected]> | 2021-03-28 14:00:37 +0200 |
commit | bcce296ca315d81633802183a8ba8658450f7ee3 (patch) | |
tree | 4beb06c01dfea1d97cfb68a098c0dc0cc9acb4ea /compiler | |
parent | c5ec95508127678ebbd11df8bdcf30d83766a141 (diff) | |
download | tinygo-bcce296ca315d81633802183a8ba8658450f7ee3.tar.gz tinygo-bcce296ca315d81633802183a8ba8658450f7ee3.zip |
transform: optimize reflect.Type Implements() method
This commit adds a new transform that converts reflect Implements()
calls to runtime.interfaceImplements. At the moment, the Implements()
method is not yet implemented (how ironic) but if the value passed to
Implements is known at compile time the method call can be optimized to
runtime.interfaceImplements to make it a regular interface assert.
This commit is the last change necessary to add basic support for the
encoding/json package. The json package is certainly not yet fully
supported, but some trivial objects can be converted to JSON.
Diffstat (limited to 'compiler')
-rw-r--r-- | compiler/compiler.go | 2 | ||||
-rw-r--r-- | compiler/interface.go | 3 | ||||
-rw-r--r-- | compiler/testdata/interface.ll | 11 |
3 files changed, 10 insertions, 6 deletions
diff --git a/compiler/compiler.go b/compiler/compiler.go index 8a6992240..d012724b7 100644 --- a/compiler/compiler.go +++ b/compiler/compiler.go @@ -23,7 +23,7 @@ import ( // Version of the compiler pacakge. Must be incremented each time the compiler // package changes in a way that affects the generated LLVM module. // This version is independent of the TinyGo version number. -const Version = 4 // last change: runtime.typeAssert signature +const Version = 5 // last change: add method set to interface types func init() { llvm.InitializeAllTargets() diff --git a/compiler/interface.go b/compiler/interface.go index ed75b3577..dae098577 100644 --- a/compiler/interface.go +++ b/compiler/interface.go @@ -62,6 +62,9 @@ func (c *compilerContext) getTypeCode(typ types.Type) llvm.Value { // Take a pointer to the typecodeID of the first field (if it exists). structGlobal := c.makeStructTypeFields(typ) references = llvm.ConstBitCast(structGlobal, global.Type()) + case *types.Interface: + methodSetGlobal := c.getInterfaceMethodSet(typ) + references = llvm.ConstBitCast(methodSetGlobal, global.Type()) } if _, ok := typ.Underlying().(*types.Interface); !ok { methodSet = c.getTypeMethodSet(typ) diff --git a/compiler/testdata/interface.ll b/compiler/testdata/interface.ll index b3c2f86a6..1405abf18 100644 --- a/compiler/testdata/interface.ll +++ b/compiler/testdata/interface.ll @@ -12,14 +12,15 @@ target triple = "i686--linux" @"reflect/types.type:pointer:basic:int" = linkonce_odr constant %runtime.typecodeID { %runtime.typecodeID* @"reflect/types.type:basic:int", i32 0, %runtime.interfaceMethodInfo* null } @"reflect/types.type:pointer:named:error" = linkonce_odr constant %runtime.typecodeID { %runtime.typecodeID* @"reflect/types.type:named:error", i32 0, %runtime.interfaceMethodInfo* null } @"reflect/types.type:named:error" = linkonce_odr constant %runtime.typecodeID { %runtime.typecodeID* @"reflect/types.type:interface:{Error:func:{}{basic:string}}", i32 0, %runtime.interfaceMethodInfo* null } -@"reflect/types.type:interface:{Error:func:{}{basic:string}}" = external constant %runtime.typecodeID -@"reflect/types.type:pointer:interface:{String:func:{}{basic:string}}" = linkonce_odr constant %runtime.typecodeID { %runtime.typecodeID* @"reflect/types.type:interface:{String:func:{}{basic:string}}", i32 0, %runtime.interfaceMethodInfo* null } -@"reflect/types.type:interface:{String:func:{}{basic:string}}" = external constant %runtime.typecodeID -@"reflect/types.type:basic:int$id" = external constant i8 +@"reflect/types.type:interface:{Error:func:{}{basic:string}}" = linkonce_odr constant %runtime.typecodeID { %runtime.typecodeID* bitcast ([1 x i8*]* @"reflect/types.interface:interface{Error() string}$interface" to %runtime.typecodeID*), i32 0, %runtime.interfaceMethodInfo* null } @"func Error() string" = external constant i8 -@"error$interface" = linkonce_odr constant [1 x i8*] [i8* @"func Error() string"] +@"reflect/types.interface:interface{Error() string}$interface" = linkonce_odr constant [1 x i8*] [i8* @"func Error() string"] +@"reflect/types.type:pointer:interface:{String:func:{}{basic:string}}" = linkonce_odr constant %runtime.typecodeID { %runtime.typecodeID* @"reflect/types.type:interface:{String:func:{}{basic:string}}", i32 0, %runtime.interfaceMethodInfo* null } +@"reflect/types.type:interface:{String:func:{}{basic:string}}" = linkonce_odr constant %runtime.typecodeID { %runtime.typecodeID* bitcast ([1 x i8*]* @"reflect/types.interface:interface{String() string}$interface" to %runtime.typecodeID*), i32 0, %runtime.interfaceMethodInfo* null } @"func String() string" = external constant i8 @"reflect/types.interface:interface{String() string}$interface" = linkonce_odr constant [1 x i8*] [i8* @"func String() string"] +@"reflect/types.type:basic:int$id" = external constant i8 +@"error$interface" = linkonce_odr constant [1 x i8*] [i8* @"func Error() string"] declare noalias nonnull i8* @runtime.alloc(i32, i8*, i8*) |