| 9 | ) |
| 10 | |
| 11 | func (f *Function) executeCallExtern(step *Step, instr *ssa.CallExtern) { |
| 12 | args := instr.Arguments |
| 13 | var pushed []cpu.Register |
| 14 | |
| 15 | for i, arg := range slices.Backward(args) { |
| 16 | argStep := f.ValueToStep[arg] |
| 17 | source := f.resolveOperand(argStep, step.Live) |
| 18 | |
| 19 | if i >= len(f.CPU.ExternCall.In) { |
| 20 | pushed = append(pushed, source) |
| 21 | continue |
| 22 | } |
| 23 | |
| 24 | if source == f.CPU.ExternCall.In[i] { |
| 25 | continue |
| 26 | } |
| 27 | |
| 28 | f.Assembler.Append(&asm.Move{ |
| 29 | Destination: f.CPU.ExternCall.In[i], |
| 30 | Source: source, |
| 31 | }) |
| 32 | } |
| 33 | |
| 34 | // Pushing an odd number of registers would not maintain the 16-byte |
| 35 | // stack alignment, so we allocate additional 8 bytes before pushing |
| 36 | // the 5th argument. |
| 37 | if len(pushed)&1 != 0 { |
| 38 | f.Assembler.Append(&asm.SubtractNumber{Destination: f.CPU.StackPointer, Source: f.CPU.StackPointer, Number: 8}) |
| 39 | } |
| 40 | |
| 41 | // TODO: Replace push instructions with store instructions using a fixed offset. |
| 42 | if len(pushed) > 0 { |
| 43 | f.Assembler.Append(&asm.Push{Registers: pushed}) |
| 44 | } |
| 45 | |
| 46 | f.Assembler.Append(&asm.CallExtern{Library: instr.Func.Package(), Function: instr.Func.Name()}) |
| 47 | |
| 48 | if len(pushed) > 0 { |
| 49 | f.Assembler.Append(&asm.Pop{Registers: pushed}) |
| 50 | } |
| 51 | |
| 52 | if len(pushed)&1 != 0 { |
| 53 | f.Assembler.Append(&asm.AddNumber{Destination: f.CPU.StackPointer, Source: f.CPU.StackPointer, Number: 8}) |
| 54 | } |
| 55 | |
| 56 | f.moveCallResult(step, f.CPU.ExternCall.Out[0]) |
| 57 | } |