1package backend
2
3import (
4 "context"
5
6 "github.com/tetratelabs/wazero/internal/engine/wazevo/backend/regalloc"
7 "github.com/tetratelabs/wazero/internal/engine/wazevo/ssa"
8 "github.com/tetratelabs/wazero/internal/engine/wazevo/wazevoapi"
9)
10
11type (
12 // Machine is a backend for a specific ISA machine.
13 Machine interface {
14 // StartLoweringFunction is called when the compilation of the given function is started.
15 // The maxBlockID is the maximum ssa.BasicBlockID in the function.
16 StartLoweringFunction(maxBlockID ssa.BasicBlockID)
17
18 // LinkAdjacentBlocks is called after finished lowering all blocks in order to create one single instruction list.
19 LinkAdjacentBlocks(prev, next ssa.BasicBlock)
20
21 // StartBlock is called when the compilation of the given block is started.
22 // The order of this being called is the reverse post order of the ssa.BasicBlock(s) as we iterate with
23 // ssa.Builder BlockIteratorReversePostOrderBegin and BlockIteratorReversePostOrderEnd.
24 StartBlock(ssa.BasicBlock)
25
26 // EndBlock is called when the compilation of the current block is finished.
27 EndBlock()
28
29 // FlushPendingInstructions flushes the pending instructions to the buffer.
30 // This will be called after the lowering of each SSA Instruction.
31 FlushPendingInstructions()
32
33 // DisableStackCheck disables the stack check for the current compilation for debugging/testing.
34 DisableStackCheck()
35
36 // SetCurrentABI initializes the FunctionABI for the given signature.
37 SetCurrentABI(abi *FunctionABI)
38
39 // SetCompiler sets the compilation context used for the lifetime of Machine.
40 // This is only called once per Machine, i.e. before the first compilation.
41 SetCompiler(Compiler)
42
43 // LowerSingleBranch is called when the compilation of the given single branch is started.
44 LowerSingleBranch(b *ssa.Instruction)
45
46 // LowerConditionalBranch is called when the compilation of the given conditional branch is started.
47 LowerConditionalBranch(b *ssa.Instruction)
48
49 // LowerInstr is called for each instruction in the given block except for the ones marked as already lowered
50 // via Compiler.MarkLowered. The order is reverse, i.e. from the last instruction to the first one.
51 //
52 // Note: this can lower multiple instructions (which produce the inputs) at once whenever it's possible
53 // for optimization.
54 LowerInstr(*ssa.Instruction)
55
56 // Reset resets the machine state for the next compilation.
57 Reset()
58
59 // InsertMove inserts a move instruction from src to dst whose type is typ.
60 InsertMove(dst, src regalloc.VReg, typ ssa.Type)
61
62 // InsertReturn inserts the return instruction to return from the current function.
63 InsertReturn()
64
65 // InsertLoadConstantBlockArg inserts the instruction(s) to load the constant value into the given regalloc.VReg.
66 InsertLoadConstantBlockArg(instr *ssa.Instruction, vr regalloc.VReg)
67
68 // Format returns the string representation of the currently compiled machine code.
69 // This is only for testing purpose.
70 Format() string
71
72 // RegAlloc does the register allocation after lowering.
73 RegAlloc()
74
75 // PostRegAlloc does the post register allocation, e.g. setting up prologue/epilogue, redundant move elimination, etc.
76 PostRegAlloc()
77
78 // ResolveRelocations resolves the relocations after emitting machine code.
79 // * refToBinaryOffset: the map from the function reference (ssa.FuncRef) to the executable offset.
80 // * importedFns: the max index of the imported functions at the beginning of refToBinaryOffset
81 // * executable: the binary to resolve the relocations.
82 // * relocations: the relocations to resolve.
83 // * callTrampolineIslandOffsets: the offsets of the trampoline islands in the executable.
84 ResolveRelocations(
85 refToBinaryOffset []int,
86 importedFns int,
87 executable []byte,
88 relocations []RelocationInfo,
89 callTrampolineIslandOffsets []int,
90 )
91
92 // Encode encodes the machine instructions to the Compiler.
93 Encode(ctx context.Context) error
94
95 // CompileGoFunctionTrampoline compiles the trampoline function to call a Go function of the given exit code and signature.
96 CompileGoFunctionTrampoline(exitCode wazevoapi.ExitCode, sig *ssa.Signature, needModuleContextPtr bool) []byte
97
98 // CompileStackGrowCallSequence returns the sequence of instructions shared by all functions to
99 // call the stack grow builtin function.
100 CompileStackGrowCallSequence() []byte
101
102 // CompileEntryPreamble returns the sequence of instructions shared by multiple functions to
103 // enter the function from Go.
104 CompileEntryPreamble(signature *ssa.Signature) []byte
105
106 // LowerParams lowers the given parameters.
107 LowerParams(params []ssa.Value)
108
109 // LowerReturns lowers the given returns.
110 LowerReturns(returns []ssa.Value)
111
112 // ArgsResultsRegs returns the registers used for arguments and return values.
113 ArgsResultsRegs() (argResultInts, argResultFloats []regalloc.RealReg)
114
115 // CallTrampolineIslandInfo returns the interval of the offset where the trampoline island is placed, and
116 // the size of the trampoline island. If islandSize is zero, the trampoline island is not used on this machine.
117 CallTrampolineIslandInfo(numFunctions int) (interval, islandSize int, err error)
118 }
119)