-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathexecuteCallExtern.go
More file actions
57 lines (44 loc) · 1.43 KB
/
Copy pathexecuteCallExtern.go
File metadata and controls
57 lines (44 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package codegen
import (
"slices"
"git.urbach.dev/cli/q/src/asm"
"git.urbach.dev/cli/q/src/cpu"
"git.urbach.dev/cli/q/src/ssa"
)
func (f *Function) executeCallExtern(step *Step, instr *ssa.CallExtern) {
args := instr.Arguments
var pushed []cpu.Register
for i, arg := range slices.Backward(args) {
argStep := f.ValueToStep[arg]
source := f.resolveOperand(argStep, step.Live)
if i >= len(f.CPU.ExternCall.In) {
pushed = append(pushed, source)
continue
}
if source == f.CPU.ExternCall.In[i] {
continue
}
f.Assembler.Append(&asm.Move{
Destination: f.CPU.ExternCall.In[i],
Source: source,
})
}
// Pushing an odd number of registers would not maintain the 16-byte
// stack alignment, so we allocate additional 8 bytes before pushing
// the 5th argument.
if len(pushed)&1 != 0 {
f.Assembler.Append(&asm.SubtractNumber{Destination: f.CPU.StackPointer, Source: f.CPU.StackPointer, Number: 8})
}
// TODO: Replace push instructions with store instructions using a fixed offset.
if len(pushed) > 0 {
f.Assembler.Append(&asm.Push{Registers: pushed})
}
f.Assembler.Append(&asm.CallExtern{Library: instr.Func.Package(), Function: instr.Func.Name()})
if len(pushed) > 0 {
f.Assembler.Append(&asm.Pop{Registers: pushed})
}
if len(pushed)&1 != 0 {
f.Assembler.Append(&asm.AddNumber{Destination: f.CPU.StackPointer, Source: f.CPU.StackPointer, Number: 8})
}
f.moveCallResult(step, f.CPU.ExternCall.Out[0])
}