github.com/tetratelabs/wazero@v1.7.3-0.20240513003603-48f702e154b5/internal/engine/wazevo/backend/machine.go (about)

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