Debian Codegraph @

internal/engine/wazevo/backend/isa/arm64/util_test.go

raw
1 package arm64
2
3 import (
4 "context"
5 "strings"
6
7 "github.com/tetratelabs/wazero/internal/engine/wazevo/backend"
8 "github.com/tetratelabs/wazero/internal/engine/wazevo/backend/regalloc"
9 "github.com/tetratelabs/wazero/internal/engine/wazevo/ssa"
10 )
11
12 func getPendingInstr(m *machine) *instruction {
13 return m.pendingInstructions[0]
14 }
15
16 func formatEmittedInstructionsInCurrentBlock(m *machine) string {
17 m.FlushPendingInstructions()
18 var strs []string
19 for cur := m.perBlockHead; cur != nil; cur = cur.next {
20 strs = append(strs, cur.String())
21 }
22 return strings.Join(strs, "\n")
23 }
24
25 func newSetup() (ssa.Builder, *machine) {
26 m := NewBackend().(*machine)
27 ssaB := ssa.NewBuilder()
28 backend.NewCompiler(context.Background(), m, ssaB)
29 blk := ssaB.AllocateBasicBlock()
30 ssaB.SetCurrentBlock(blk)
31 return ssaB, m
32 }
33
34 func newSetupWithMockContext() (*mockCompiler, ssa.Builder, *machine) {
35 ctx := newMockCompilationContext()
36 m := NewBackend().(*machine)
37 m.SetCompiler(ctx)
38 ssaB := ssa.NewBuilder()
39 ctx.ssaBuilder = ssaB
40 blk := ssaB.AllocateBasicBlock()
41 ssaB.SetCurrentBlock(blk)
42 return ctx, ssaB, m
43 }
44
45 func regToVReg(reg regalloc.RealReg) regalloc.VReg {
46 return regalloc.VReg(0).SetRealReg(reg).SetRegType(regalloc.RegTypeInt)
47 }
48
49 func intToVReg(i int) regalloc.VReg {
50 return regalloc.VReg(i).SetRegType(regalloc.RegTypeInt)
51 }
52
53 // mockCompiler implements backend.Compiler for testing.
54 type mockCompiler struct {
55 currentGID ssa.InstructionGroupID
56 vRegCounter int
57 vRegMap map[ssa.Value]regalloc.VReg
58 definitions map[ssa.Value]backend.SSAValueDefinition
59 sigs map[ssa.SignatureID]*ssa.Signature
60 typeOf map[regalloc.VRegID]ssa.Type
61 ssaBuilder ssa.Builder
62 relocs []backend.RelocationInfo
63 buf []byte
64 }
65
66 func (m *mockCompiler) BufPtr() *[]byte { return &m.buf }
67
68 func (m *mockCompiler) GetFunctionABI(sig *ssa.Signature) *backend.FunctionABI {
69 // TODO implement me
70 panic("implement me")
71 }
72
73 func (m *mockCompiler) SSABuilder() ssa.Builder { return m.ssaBuilder }
74
75 func (m *mockCompiler) LoopNestingForestRoots() []ssa.BasicBlock { panic("TODO") }
76
77 func (m *mockCompiler) SourceOffsetInfo() []backend.SourceOffsetInfo { return nil }
78
79 func (m *mockCompiler) AddSourceOffsetInfo(int64, ssa.SourceOffset) {}
80
81 func (m *mockCompiler) AddRelocationInfo(funcRef ssa.FuncRef, isTailCall bool) {
82 m.relocs = append(m.relocs, backend.RelocationInfo{FuncRef: funcRef, Offset: int64(len(m.buf))})
83 }
84
85 func (m *mockCompiler) Emit4Bytes(b uint32) {
86 m.buf = append(m.buf, byte(b), byte(b>>8), byte(b>>16), byte(b>>24))
87 }
88
89 func (m *mockCompiler) EmitByte(b byte) {
90 m.buf = append(m.buf, b)
91 }
92
93 func (m *mockCompiler) Emit8Bytes(b uint64) {
94 m.buf = append(m.buf, byte(b), byte(b>>8), byte(b>>16), byte(b>>24), byte(b>>32), byte(b>>40), byte(b>>48), byte(b>>56))
95 }
96
97 func (m *mockCompiler) Encode() {}
98 func (m *mockCompiler) Buf() []byte { return m.buf }
99 func (m *mockCompiler) TypeOf(v regalloc.VReg) (ret ssa.Type) {
100 return m.typeOf[v.ID()]
101 }
102 func (m *mockCompiler) Finalize(context.Context) (err error) { return }
103 func (m *mockCompiler) RegAlloc() {}
104 func (m *mockCompiler) Lower() {}
105 func (m *mockCompiler) Format() string { return "" }
106 func (m *mockCompiler) Init() {}
107
108 func newMockCompilationContext() *mockCompiler {
109 return &mockCompiler{
110 vRegMap: make(map[ssa.Value]regalloc.VReg),
111 definitions: make(map[ssa.Value]backend.SSAValueDefinition),
112 typeOf: map[regalloc.VRegID]ssa.Type{},
113 }
114 }
115
116 // ResolveSignature implements backend.Compiler.
117 func (m *mockCompiler) ResolveSignature(id ssa.SignatureID) *ssa.Signature {
118 return m.sigs[id]
119 }
120
121 // AllocateVReg implements backend.Compiler.
122 func (m *mockCompiler) AllocateVReg(typ ssa.Type) regalloc.VReg {
123 m.vRegCounter++
124 regType := regalloc.RegTypeOf(typ)
125 ret := regalloc.VReg(m.vRegCounter).SetRegType(regType)
126 m.typeOf[ret.ID()] = typ
127 return ret
128 }
129
130 // ValueDefinition implements backend.Compiler.
131 func (m *mockCompiler) ValueDefinition(value ssa.Value) backend.SSAValueDefinition {
132 definition, exists := m.definitions[value]
133 if !exists {
134 return backend.SSAValueDefinition{}
135 }
136 return definition
137 }
138
139 // VRegOf implements backend.Compiler.
140 func (m *mockCompiler) VRegOf(value ssa.Value) regalloc.VReg {
141 vReg, exists := m.vRegMap[value]
142 if !exists {
143 panic("Value does not exist")
144 }
145 return vReg
146 }
147
148 // MatchInstr implements backend.Compiler.
149 func (m *mockCompiler) MatchInstr(def backend.SSAValueDefinition, opcode ssa.Opcode) bool {
150 instr := def.Instr
151 return def.IsFromInstr() &&
152 instr.Opcode() == opcode &&
153 instr.GroupID() == m.currentGID &&
154 def.RefCount < 2
155 }
156
157 // MatchInstrOneOf implements backend.Compiler.
158 func (m *mockCompiler) MatchInstrOneOf(def backend.SSAValueDefinition, opcodes []ssa.Opcode) ssa.Opcode {
159 for _, opcode := range opcodes {
160 if m.MatchInstr(def, opcode) {
161 return opcode
162 }
163 }
164 return ssa.OpcodeInvalid
165 }
166
167 // Compile implements backend.Compiler.
168 func (m *mockCompiler) Compile(context.Context) (_ []byte, _ []backend.RelocationInfo, _ error) {
169 return
170 }