mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-12 15:03:41 +00:00
compiler: lower interfaces in a separate pass
This commit changes many things:
* Most interface-related operations are moved into an optimization
pass for more modularity. IR construction creates pseudo-calls which
are lowered in this pass.
* Type codes are assigned in this interface lowering pass, after DCE.
* Type codes are sorted by usage: types more often used in type
asserts are assigned lower numbers to ease jump table construction
during machine code generation.
* Interface assertions are optimized: they are replaced by constant
false, comparison against a constant, or a typeswitch with only
concrete types in the general case.
* Interface calls are replaced with unreachable, direct calls, or a
concrete type switch with direct calls depending on the number of
implementing types. This hopefully makes some interface patterns
zero-cost.
These changes lead to a ~0.5K reduction in code size on Cortex-M for
testdata/interface.go. It appears that a major cause for this is the
replacement of function pointers with direct calls, which are far more
susceptible to optimization. Also, not having a fixed global array of
function pointers greatly helps dead code elimination.
This change also makes future optimizations easier, like optimizations
on interface value comparisons.
This commit is contained in:
@@ -205,7 +205,14 @@ func printitf(msg interface{}) {
|
||||
// cast to underlying type
|
||||
itf := *(*_interface)(unsafe.Pointer(&msg))
|
||||
putchar('(')
|
||||
print(itf.typecode)
|
||||
switch unsafe.Sizeof(itf.typecode) {
|
||||
case 2:
|
||||
printuint16(uint16(itf.typecode))
|
||||
case 4:
|
||||
printuint32(uint32(itf.typecode))
|
||||
case 8:
|
||||
printuint64(uint64(itf.typecode))
|
||||
}
|
||||
putchar(':')
|
||||
print(itf.value)
|
||||
putchar(')')
|
||||
|
||||
Reference in New Issue
Block a user