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:
Ayke van Laethem
2018-08-26 23:31:29 +02:00
parent 309de00fd6
commit 539de9db9e
3 changed files with 88 additions and 69 deletions
+59
View File
@@ -0,0 +1,59 @@
package runtime
// This file implements Go interfaces.
//
// Interfaces are represented as a pair of {typecode, value}, where value can be
// anything (including non-pointers).
//
// Signatures itself are not matched on strings, but on uniqued numbers that
// contain the name and the signature of the function (to save space), think of
// signatures as interned strings at compile time.
//
// The typecode is a small number unique for the Go type. All typecodes <
// firstInterfaceNum do not have any methods and typecodes >= firstInterfaceNum
// all have at least one method. This means that methodSetRanges does not need
// to contain types without methods and is thus indexed starting at a typecode
// with number firstInterfaceNum.
//
// To further conserve some space, the methodSetRange (as the name indicates)
// doesn't contain a list of methods and function pointers directly, but instead
// just indexes into methodSetSignatures and methodSetFunctions which contains
// the mapping from uniqued signature to function pointer.
type _interface struct {
typecode uint32
value *uint8
}
// This struct indicates the range of methods in the methodSetSignatures and
// methodSetFunctions arrays that belong to this named type.
type methodSetRange struct {
index uint32 // start index into interfaceSignatures and interfaceFunctions
length uint32 // number of methods
}
// Global constants that will be set by the compiler. The arrays are of size 0,
// which is a dummy value, but will be bigger after the compiler has filled them
// in.
var (
methodSetRanges [0]methodSetRange // indexes into methodSetSignatures and methodSetFunctions
methodSetSignatures [0]uint32 // uniqued method ID
methodSetFunctions [0]*uint8 // function pointer of method
firstInterfaceNum uint32 // the lowest typecode that has at least one method
)
// Get the function pointer for the method on the interface.
// This is a compiler intrinsic.
func itfmethod(itf _interface, method uint32) *uint8 {
// This function doesn't do bounds checking as the supplied method must be
// in the list of signatures. The compiler will only emit runtime.itfmethod
// calls when the method actually exists on this interface (proven by the
// typechecker).
i := methodSetRanges[itf.typecode-firstInterfaceNum].index
for {
if methodSetSignatures[i] == method {
return methodSetFunctions[i]
}
i++
}
}