github.com/bananabytelabs/wazero@v0.0.0-20240105073314-54b22a776da8/internal/engine/compiler/compiler.go (about) 1 package compiler 2 3 import ( 4 "github.com/bananabytelabs/wazero/internal/asm" 5 "github.com/bananabytelabs/wazero/internal/wasm" 6 "github.com/bananabytelabs/wazero/internal/wazeroir" 7 ) 8 9 // compiler is the interface of architecture-specific native code compiler, 10 // and this is responsible for compiling native code for all wazeroir operations. 11 type compiler interface { 12 Init(functionType *wasm.FunctionType, ir *wazeroir.CompilationResult, withListener bool) 13 14 // String is for debugging purpose. 15 String() string 16 // compilePreamble is called before compiling any wazeroir operation. 17 // This is used, for example, to initialize the reserved registers, etc. 18 compilePreamble() error 19 // compile generates the native code into buf. 20 // stackPointerCeil is the max stack pointer that the target function would reach. 21 compile(buf asm.Buffer) (stackPointerCeil uint64, err error) 22 // compileGoHostFunction adds the trampoline code from which native code can jump into the Go-defined host function. 23 // TODO: maybe we wouldn't need to have trampoline for host functions. 24 compileGoDefinedHostFunction() error 25 // compileLabel notify compilers of the beginning of a label. 26 // Return true if the compiler decided to skip the entire label. 27 // See wazeroir.NewOperationLabel 28 compileLabel(o *wazeroir.UnionOperation) (skipThisLabel bool) 29 // compileUnreachable adds instruction to perform wazeroir.OperationUnreachable. 30 compileUnreachable() error 31 // compileSet adds instruction to perform wazeroir.OperationSet. 32 compileSet(o *wazeroir.UnionOperation) error 33 // compileGlobalGet adds instructions to perform wazeroir.OperationGlobalGet. 34 compileGlobalGet(o *wazeroir.UnionOperation) error 35 // compileGlobalSet adds instructions to perform wazeroir.OperationGlobalSet. 36 compileGlobalSet(o *wazeroir.UnionOperation) error 37 // compileBr adds instructions to perform wazeroir.NewOperationBr. 38 compileBr(o *wazeroir.UnionOperation) error 39 // compileBrIf adds instructions to perform wazeroir.NewOperationBrIf. 40 compileBrIf(o *wazeroir.UnionOperation) error 41 // compileBrTable adds instructions to perform wazeroir.NewOperationBrTable. 42 compileBrTable(o *wazeroir.UnionOperation) error 43 // compileCall adds instructions to perform wazeroir.OperationCall. 44 compileCall(o *wazeroir.UnionOperation) error 45 // compileCallIndirect adds instructions to perform wazeroir.OperationCallIndirect. 46 compileCallIndirect(o *wazeroir.UnionOperation) error 47 // compileDrop adds instructions to perform wazeroir.NewOperationDrop. 48 compileDrop(o *wazeroir.UnionOperation) error 49 // compileSelect adds instructions to perform wazeroir.OperationSelect. 50 compileSelect(o *wazeroir.UnionOperation) error 51 // compilePick adds instructions to perform wazeroir.OperationPick. 52 compilePick(o *wazeroir.UnionOperation) error 53 // compileAdd adds instructions to perform wazeroir.OperationAdd. 54 compileAdd(o *wazeroir.UnionOperation) error 55 // compileSub adds instructions to perform wazeroir.OperationSub. 56 compileSub(o *wazeroir.UnionOperation) error 57 // compileMul adds instructions to perform wazeroir.OperationMul. 58 compileMul(o *wazeroir.UnionOperation) error 59 // compileClz adds instructions to perform wazeroir.OperationClz. 60 compileClz(o *wazeroir.UnionOperation) error 61 // compileCtz adds instructions to perform wazeroir.OperationCtz. 62 compileCtz(o *wazeroir.UnionOperation) error 63 // compilePopcnt adds instructions to perform wazeroir.OperationPopcnt. 64 compilePopcnt(o *wazeroir.UnionOperation) error 65 // compileDiv adds instructions to perform wazeroir.OperationDiv. 66 compileDiv(o *wazeroir.UnionOperation) error 67 // compileRem adds instructions to perform wazeroir.OperationRem. 68 compileRem(o *wazeroir.UnionOperation) error 69 // compileAnd adds instructions to perform wazeroir.OperationAnd. 70 compileAnd(o *wazeroir.UnionOperation) error 71 // compileOr adds instructions to perform wazeroir.OperationOr. 72 compileOr(o *wazeroir.UnionOperation) error 73 // compileXor adds instructions to perform wazeroir.OperationXor. 74 compileXor(o *wazeroir.UnionOperation) error 75 // compileShl adds instructions to perform wazeroir.OperationShl. 76 compileShl(o *wazeroir.UnionOperation) error 77 // compileShr adds instructions to perform wazeroir.OperationShr. 78 compileShr(o *wazeroir.UnionOperation) error 79 // compileRotl adds instructions to perform wazeroir.OperationRotl. 80 compileRotl(o *wazeroir.UnionOperation) error 81 // compileRotr adds instructions to perform wazeroir.OperationRotr. 82 compileRotr(o *wazeroir.UnionOperation) error 83 // compileNeg adds instructions to perform wazeroir.OperationAbs. 84 compileAbs(o *wazeroir.UnionOperation) error 85 // compileNeg adds instructions to perform wazeroir.OperationNeg. 86 compileNeg(o *wazeroir.UnionOperation) error 87 // compileCeil adds instructions to perform wazeroir.OperationCeil. 88 compileCeil(o *wazeroir.UnionOperation) error 89 // compileFloor adds instructions to perform wazeroir.OperationFloor. 90 compileFloor(o *wazeroir.UnionOperation) error 91 // compileTrunc adds instructions to perform wazeroir.OperationTrunc. 92 compileTrunc(o *wazeroir.UnionOperation) error 93 // compileNearest adds instructions to perform wazeroir.OperationNearest. 94 compileNearest(o *wazeroir.UnionOperation) error 95 // compileSqrt adds instructions perform wazeroir.OperationSqrt. 96 compileSqrt(o *wazeroir.UnionOperation) error 97 // compileMin adds instructions perform wazeroir.OperationMin. 98 compileMin(o *wazeroir.UnionOperation) error 99 // compileMax adds instructions perform wazeroir.OperationMax. 100 compileMax(o *wazeroir.UnionOperation) error 101 // compileCopysign adds instructions to perform wazeroir.OperationCopysign. 102 compileCopysign(o *wazeroir.UnionOperation) error 103 // compileI32WrapFromI64 adds instructions to perform wazeroir.OperationI32WrapFromI64. 104 compileI32WrapFromI64() error 105 // compileITruncFromF adds instructions to perform wazeroir.NewOperationITruncFromF. 106 compileITruncFromF(o *wazeroir.UnionOperation) error 107 // compileFConvertFromI adds instructions to perform wazeroir.NewOperationFConvertFromI. 108 compileFConvertFromI(o *wazeroir.UnionOperation) error 109 // compileF32DemoteFromF64 adds instructions to perform wazeroir.OperationF32DemoteFromF64. 110 compileF32DemoteFromF64() error 111 // compileF64PromoteFromF32 adds instructions to perform wazeroir.OperationF64PromoteFromF32. 112 compileF64PromoteFromF32() error 113 // compileI32ReinterpretFromF32 adds instructions to perform wazeroir.OperationI32ReinterpretFromF32. 114 compileI32ReinterpretFromF32() error 115 // compileI64ReinterpretFromF64 adds instructions to perform wazeroir.OperationI64ReinterpretFromF64. 116 compileI64ReinterpretFromF64() error 117 // compileF32ReinterpretFromI32 adds instructions to perform wazeroir.OperationF32ReinterpretFromI32. 118 compileF32ReinterpretFromI32() error 119 // compileF64ReinterpretFromI64 adds instructions to perform wazeroir.OperationF64ReinterpretFromI64. 120 compileF64ReinterpretFromI64() error 121 // compileExtend adds instructions to perform wazeroir.NewOperationExtend. 122 compileExtend(o *wazeroir.UnionOperation) error 123 // compileEq adds instructions to perform wazeroir.OperationEq. 124 compileEq(o *wazeroir.UnionOperation) error 125 // compileEq adds instructions to perform wazeroir.OperationNe. 126 compileNe(o *wazeroir.UnionOperation) error 127 // compileEq adds instructions to perform wazeroir.OperationEqz. 128 compileEqz(o *wazeroir.UnionOperation) error 129 // compileLt adds instructions to perform wazeroir.OperationLt. 130 compileLt(o *wazeroir.UnionOperation) error 131 // compileGt adds instructions to perform wazeroir.OperationGt. 132 compileGt(o *wazeroir.UnionOperation) error 133 // compileLe adds instructions to perform wazeroir.OperationLe. 134 compileLe(o *wazeroir.UnionOperation) error 135 // compileLe adds instructions to perform wazeroir.OperationGe. 136 compileGe(o *wazeroir.UnionOperation) error 137 // compileLoad adds instructions to perform wazeroir.OperationLoad. 138 compileLoad(o *wazeroir.UnionOperation) error 139 // compileLoad8 adds instructions to perform wazeroir.OperationLoad8. 140 compileLoad8(o *wazeroir.UnionOperation) error 141 // compileLoad16 adds instructions to perform wazeroir.OperationLoad16. 142 compileLoad16(o *wazeroir.UnionOperation) error 143 // compileLoad32 adds instructions to perform wazeroir.OperationLoad32. 144 compileLoad32(o *wazeroir.UnionOperation) error 145 // compileStore adds instructions to perform wazeroir.OperationStore. 146 compileStore(o *wazeroir.UnionOperation) error 147 // compileStore8 adds instructions to perform wazeroir.OperationStore8. 148 compileStore8(o *wazeroir.UnionOperation) error 149 // compileStore16 adds instructions to perform wazeroir.OperationStore16. 150 compileStore16(o *wazeroir.UnionOperation) error 151 // compileStore32 adds instructions to perform wazeroir.OperationStore32. 152 compileStore32(o *wazeroir.UnionOperation) error 153 // compileMemorySize adds instruction to perform wazeroir.OperationMemoryGrow. 154 compileMemoryGrow() error 155 // compileMemorySize adds instruction to perform wazeroir.OperationMemorySize. 156 compileMemorySize() error 157 // compileConstI32 adds instruction to perform wazeroir.NewOperationConstI32. 158 compileConstI32(o *wazeroir.UnionOperation) error 159 // compileConstI64 adds instruction to perform wazeroir.NewOperationConstI64. 160 compileConstI64(o *wazeroir.UnionOperation) error 161 // compileConstF32 adds instruction to perform wazeroir.NewOperationConstF32. 162 compileConstF32(o *wazeroir.UnionOperation) error 163 // compileConstF64 adds instruction to perform wazeroir.NewOperationConstF64. 164 compileConstF64(o *wazeroir.UnionOperation) error 165 // compileSignExtend32From8 adds instructions to perform wazeroir.OperationSignExtend32From8. 166 compileSignExtend32From8() error 167 // compileSignExtend32From16 adds instructions to perform wazeroir.OperationSignExtend32From16. 168 compileSignExtend32From16() error 169 // compileSignExtend64From8 adds instructions to perform wazeroir.OperationSignExtend64From8. 170 compileSignExtend64From8() error 171 // compileSignExtend64From16 adds instructions to perform wazeroir.OperationSignExtend64From16. 172 compileSignExtend64From16() error 173 // compileSignExtend64From32 adds instructions to perform wazeroir.OperationSignExtend64From32. 174 compileSignExtend64From32() error 175 // compileMemoryInit adds instructions to perform wazeroir.NewOperationMemoryInit. 176 compileMemoryInit(*wazeroir.UnionOperation) error 177 // compileDataDrop adds instructions to perform wazeroir.NewOperationDataDrop. 178 compileDataDrop(*wazeroir.UnionOperation) error 179 // compileMemoryCopy adds instructions to perform wazeroir.OperationMemoryCopy. 180 compileMemoryCopy() error 181 // compileMemoryFill adds instructions to perform wazeroir.OperationMemoryFill. 182 compileMemoryFill() error 183 // compileTableInit adds instructions to perform wazeroir.NewOperationTableInit. 184 compileTableInit(*wazeroir.UnionOperation) error 185 // compileTableCopy adds instructions to perform wazeroir.NewOperationTableCopy. 186 compileTableCopy(*wazeroir.UnionOperation) error 187 // compileElemDrop adds instructions to perform wazeroir.NewOperationElemDrop. 188 compileElemDrop(*wazeroir.UnionOperation) error 189 // compileRefFunc adds instructions to perform wazeroir.NewOperationRefFunc. 190 compileRefFunc(*wazeroir.UnionOperation) error 191 // compileTableGet adds instructions to perform wazeroir.NewOperationTableGet. 192 compileTableGet(*wazeroir.UnionOperation) error 193 // compileTableSet adds instructions to perform wazeroir.NewOperationTableSet. 194 compileTableSet(*wazeroir.UnionOperation) error 195 // compileTableGrow adds instructions to perform wazeroir.NewOperationTableGrow. 196 compileTableGrow(*wazeroir.UnionOperation) error 197 // compileTableSize adds instructions to perform wazeroir.NewOperationTableSize. 198 compileTableSize(*wazeroir.UnionOperation) error 199 // compileTableFill adds instructions to perform wazeroir.NewOperationTableFill. 200 compileTableFill(*wazeroir.UnionOperation) error 201 // compileV128Const adds instructions to perform wazeroir.NewOperationV128Const. 202 compileV128Const(*wazeroir.UnionOperation) error 203 // compileV128Add adds instructions to perform wazeroir.OperationV128Add. 204 compileV128Add(o *wazeroir.UnionOperation) error 205 // compileV128Sub adds instructions to perform wazeroir.OperationV128Sub. 206 compileV128Sub(o *wazeroir.UnionOperation) error 207 // compileV128Load adds instructions to perform wazeroir.OperationV128Load. 208 compileV128Load(o *wazeroir.UnionOperation) error 209 // compileV128LoadLane adds instructions to perform wazeroir.OperationV128LoadLane. 210 compileV128LoadLane(o *wazeroir.UnionOperation) error 211 // compileV128Store adds instructions to perform wazeroir.NewOperationV128Store. 212 compileV128Store(o *wazeroir.UnionOperation) error 213 // compileV128StoreLane adds instructions to perform wazeroir.NewOperationV128StoreLane. 214 compileV128StoreLane(o *wazeroir.UnionOperation) error 215 // compileV128ExtractLane adds instructions to perform wazeroir.NewOperationV128ExtractLane. 216 compileV128ExtractLane(o *wazeroir.UnionOperation) error 217 // compileV128ReplaceLane adds instructions to perform wazeroir.NewOperationV128ReplaceLane. 218 compileV128ReplaceLane(o *wazeroir.UnionOperation) error 219 // compileV128Splat adds instructions to perform wazeroir.NewOperationV128Splat. 220 compileV128Splat(o *wazeroir.UnionOperation) error 221 // compileV128Shuffle adds instructions to perform wazeroir.NewOperationV128Shuffle. 222 compileV128Shuffle(o *wazeroir.UnionOperation) error 223 // compileV128Swizzle adds instructions to perform wazeroir.OperationV128Swizzle. 224 compileV128Swizzle(o *wazeroir.UnionOperation) error 225 // compileV128AnyTrue adds instructions to perform wazeroir.OperationV128AnyTrue. 226 compileV128AnyTrue(o *wazeroir.UnionOperation) error 227 // compileV128AllTrue adds instructions to perform wazeroir.NewOperationV128AllTrue. 228 compileV128AllTrue(o *wazeroir.UnionOperation) error 229 // compileV128BitMask adds instructions to perform wazeroir.NewOperationV128BitMask. 230 compileV128BitMask(*wazeroir.UnionOperation) error 231 // compileV128And adds instructions to perform wazeroir.OperationV128And. 232 compileV128And(*wazeroir.UnionOperation) error 233 // compileV128Not adds instructions to perform wazeroir.OperationV128Not. 234 compileV128Not(*wazeroir.UnionOperation) error 235 // compileV128Or adds instructions to perform wazeroir.OperationV128Or. 236 compileV128Or(*wazeroir.UnionOperation) error 237 // compileV128Xor adds instructions to perform wazeroir.OperationV128Xor. 238 compileV128Xor(*wazeroir.UnionOperation) error 239 // compileV128Bitselect adds instructions to perform wazeroir.OperationV128Bitselect. 240 compileV128Bitselect(*wazeroir.UnionOperation) error 241 // compileV128AndNot adds instructions to perform wazeroir.OperationV128AndNot. 242 compileV128AndNot(*wazeroir.UnionOperation) error 243 // compileV128Shr adds instructions to perform wazeroir.NewOperationV128Shr. 244 compileV128Shr(*wazeroir.UnionOperation) error 245 // compileV128Shl adds instructions to perform wazeroir.NewOperationV128Shl. 246 compileV128Shl(*wazeroir.UnionOperation) error 247 // compileV128Cmp adds instructions to perform wazeroir.NewOperationV128Cmp. 248 compileV128Cmp(*wazeroir.UnionOperation) error 249 // compileV128AddSat adds instructions to perform wazeroir.NewOperationV128AddSat. 250 compileV128AddSat(*wazeroir.UnionOperation) error 251 // compileV128SubSat adds instructions to perform wazeroir.NewOperationV128SubSat. 252 compileV128SubSat(*wazeroir.UnionOperation) error 253 // compileV128Mul adds instructions to perform wazeroir.NewOperationV128Mul. 254 compileV128Mul(*wazeroir.UnionOperation) error 255 // compileV128Div adds instructions to perform wazeroir.NewOperationV128Div. 256 compileV128Div(*wazeroir.UnionOperation) error 257 // compileV128Neg adds instructions to perform wazeroir.NewOperationV128Neg. 258 compileV128Neg(*wazeroir.UnionOperation) error 259 // compileV128Sqrt adds instructions to perform wazeroir.NewOperationV128Sqrt. 260 compileV128Sqrt(*wazeroir.UnionOperation) error 261 // compileV128Abs adds instructions to perform wazeroir.NewOperationV128Abs. 262 compileV128Abs(*wazeroir.UnionOperation) error 263 // compileV128Popcnt adds instructions to perform wazeroir.NewOperationV128Popcnt. 264 compileV128Popcnt(*wazeroir.UnionOperation) error 265 // compileV128Min adds instructions to perform wazeroir.NewOperationV128Min. 266 compileV128Min(*wazeroir.UnionOperation) error 267 // compileV128Max adds instructions to perform wazeroir.NewOperationV128Max. 268 compileV128Max(*wazeroir.UnionOperation) error 269 // compileV128AvgrU adds instructions to perform wazeroir.NewOperationV128AvgrU. 270 compileV128AvgrU(*wazeroir.UnionOperation) error 271 // compileV128Pmin adds instructions to perform wazeroir.NewOperationV128Pmin. 272 compileV128Pmin(*wazeroir.UnionOperation) error 273 // compileV128Pmax adds instructions to perform wazeroir.NewOperationV128Pmax. 274 compileV128Pmax(*wazeroir.UnionOperation) error 275 // compileV128Ceil adds instructions to perform wazeroir.NewOperationV128Ceil. 276 compileV128Ceil(*wazeroir.UnionOperation) error 277 // compileV128Floor adds instructions to perform wazeroir.NewOperationV128Floor. 278 compileV128Floor(*wazeroir.UnionOperation) error 279 // compileV128Trunc adds instructions to perform wazeroir.NewOperationV128Trunc. 280 compileV128Trunc(*wazeroir.UnionOperation) error 281 // compileV128Nearest adds instructions to perform wazeroir.NewOperationV128Nearest. 282 compileV128Nearest(*wazeroir.UnionOperation) error 283 // compileV128Extend adds instructions to perform wazeroir.NewOperationV128Extend. 284 compileV128Extend(*wazeroir.UnionOperation) error 285 // compileV128ExtMul adds instructions to perform wazeroir.NewOperationV128ExtMul. 286 compileV128ExtMul(*wazeroir.UnionOperation) error 287 // compileV128Q15mulrSatS adds instructions to perform wazeroir.OperationV128Q15mulrSatS. 288 compileV128Q15mulrSatS(*wazeroir.UnionOperation) error 289 // compileV128ExtAddPairwise adds instructions to perform wazeroir.NewOperationV128ExtAddPairwise. 290 compileV128ExtAddPairwise(o *wazeroir.UnionOperation) error 291 // compileV128FloatPromote adds instructions to perform wazeroir.OperationV128FloatPromote. 292 compileV128FloatPromote(o *wazeroir.UnionOperation) error 293 // compileV128FloatDemote adds instructions to perform wazeroir.OperationV128FloatDemote. 294 compileV128FloatDemote(o *wazeroir.UnionOperation) error 295 // compileV128FConvertFromI adds instructions to perform wazeroir.NewOperationV128FConvertFromI. 296 compileV128FConvertFromI(o *wazeroir.UnionOperation) error 297 // compileV128Dot adds instructions to perform wazeroir.OperationV128Dot. 298 compileV128Dot(o *wazeroir.UnionOperation) error 299 // compileV128Narrow adds instructions to perform wazeroir.NewOperationV128Narrow. 300 compileV128Narrow(o *wazeroir.UnionOperation) error 301 // compileV128ITruncSatFromF adds instructions to perform wazeroir.NewOperationV128ITruncSatFromF. 302 compileV128ITruncSatFromF(o *wazeroir.UnionOperation) error 303 304 // compileBuiltinFunctionCheckExitCode adds instructions to perform wazeroir.OperationBuiltinFunctionCheckExitCode. 305 compileBuiltinFunctionCheckExitCode() error 306 307 // compileReleaseRegisterToStack adds instructions to write the value on a register back to memory stack region. 308 compileReleaseRegisterToStack(loc *runtimeValueLocation) 309 // compileLoadValueOnStackToRegister adds instructions to load the value located on the stack to the assigned register. 310 compileLoadValueOnStackToRegister(loc *runtimeValueLocation) 311 312 // maybeCompileMoveTopConditionalToGeneralPurposeRegister moves the top value on the stack 313 // if the value is located on a conditional register. 314 // 315 // This is usually called at the beginning of methods on compiler interface where we possibly 316 // compile instructions without saving the conditional register value. 317 // The compileXXX functions without calling this function is saving the conditional 318 // value to the stack or register by invoking compileEnsureOnRegister for the top. 319 maybeCompileMoveTopConditionalToGeneralPurposeRegister() error 320 // allocateRegister returns an unused register of the given type. The register will be taken 321 // either from the free register pool or by stealing a used register. 322 // 323 // Note: resulting registers will not be marked as used so the call site should 324 // mark it used if necessary. 325 allocateRegister(t registerType) (reg asm.Register, err error) 326 // runtimeValueLocationStack returns the current runtimeValueLocationStack of the compiler implementation. 327 runtimeValueLocationStack() *runtimeValueLocationStack 328 // pushRuntimeValueLocationOnRegister pushes a new runtimeValueLocation on a register `reg` and of the type `vt`. 329 pushRuntimeValueLocationOnRegister(reg asm.Register, vt runtimeValueType) (ret *runtimeValueLocation) 330 // pushRuntimeValueLocationOnRegister pushes a new vector value's runtimeValueLocation on a register `reg`. 331 pushVectorRuntimeValueLocationOnRegister(reg asm.Register) (lowerBitsLocation *runtimeValueLocation) 332 // compileNOP compiles NOP instruction and returns the corresponding asm.Node in the assembled native code. 333 // This is used to emit DWARF based stack traces. 334 compileNOP() asm.Node 335 }