mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-06 20:13:40 +00:00
Move interface method calls in Go from LLVM IR + documentation
This commit moves the itfmethod call implemented directly in LLVM IR to a Go implementation in the runtime. Additionally, it fixes variable names to be more obvious and adds a lot of documentation to explain how interfaces actually work in TinyGo. Code size changes for src/examples/hello: nrf: -144 unix: -93 I'm guessing this code size reduction is a result of removing the 'noinline' function attribute.
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
source_filename = "runtime/runtime.ll"
|
||||
|
||||
%interface = type { i32, i8* }
|
||||
|
||||
declare void @runtime.initAll()
|
||||
declare void @main.main()
|
||||
declare i8* @main.main$async(i8*)
|
||||
@@ -10,15 +8,6 @@ declare void @runtime.scheduler(i8*)
|
||||
; Will be changed to true if there are 'go' statements in the compiled program.
|
||||
@has_scheduler = private unnamed_addr constant i1 false
|
||||
|
||||
; Will be changed by the compiler to the first type number with methods.
|
||||
@first_interface_num = private unnamed_addr constant i32 0
|
||||
|
||||
; Will be filled by the compiler with runtime type information.
|
||||
%interface_tuple = type { i32, i32 } ; { index, len }
|
||||
@interface_tuples = external global [0 x %interface_tuple]
|
||||
@interface_signatures = external global [0 x i32] ; array of method IDs
|
||||
@interface_functions = external global [0 x i8*] ; array of function pointers
|
||||
|
||||
define i32 @main() {
|
||||
call void @runtime.initAll()
|
||||
%has_scheduler = load i1, i1* @has_scheduler
|
||||
@@ -36,34 +25,3 @@ without_scheduler:
|
||||
call void @main.main()
|
||||
ret i32 0
|
||||
}
|
||||
|
||||
; Get the function pointer for the method on the interface.
|
||||
; This function only reads constant global data and it's own arguments so it can
|
||||
; be 'readnone' (a pure function).
|
||||
define i8* @itfmethod(%interface %itf, i32 %method) noinline readnone {
|
||||
entry:
|
||||
; Calculate the index in @interface_tuples
|
||||
%concrete_type_num = extractvalue %interface %itf, 0
|
||||
%first_interface_num = load i32, i32* @first_interface_num
|
||||
%index = sub i32 %concrete_type_num, %first_interface_num
|
||||
|
||||
; Calculate the index for @interface_signatures and @interface_functions
|
||||
%itf_index_ptr = getelementptr inbounds [0 x %interface_tuple], [0 x %interface_tuple]* @interface_tuples, i32 0, i32 %index, i32 0
|
||||
%itf_index = load i32, i32* %itf_index_ptr
|
||||
br label %find_method
|
||||
|
||||
; This is a while loop until the method has been found.
|
||||
; It must be in here, so avoid checking the length.
|
||||
find_method:
|
||||
%itf_index.phi = phi i32 [ %itf_index, %entry], [ %itf_index.phi.next, %find_method]
|
||||
%m_ptr = getelementptr inbounds [0 x i32], [0 x i32]* @interface_signatures, i32 0, i32 %itf_index.phi
|
||||
%m = load i32, i32* %m_ptr
|
||||
%found = icmp eq i32 %m, %method
|
||||
%itf_index.phi.next = add i32 %itf_index.phi, 1
|
||||
br i1 %found, label %found_method, label %find_method
|
||||
|
||||
found_method:
|
||||
%fp_ptr = getelementptr inbounds [0 x i8*], [0 x i8*]* @interface_functions, i32 0, i32 %itf_index.phi
|
||||
%fp = load i8*, i8** %fp_ptr
|
||||
ret i8* %fp
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user