1/*
2 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package org.graalvm.compiler.lir.aarch64;
24
25import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.ILLEGAL;
26import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.REG;
27import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.STACK;
28import static jdk.vm.ci.aarch64.AArch64.r8;
29import static jdk.vm.ci.code.ValueUtil.asRegister;
30import static jdk.vm.ci.code.ValueUtil.isRegister;
31
32import org.graalvm.compiler.asm.Label;
33import org.graalvm.compiler.asm.aarch64.AArch64Assembler;
34import org.graalvm.compiler.asm.aarch64.AArch64MacroAssembler;
35import org.graalvm.compiler.core.common.spi.ForeignCallLinkage;
36import org.graalvm.compiler.lir.LIRFrameState;
37import org.graalvm.compiler.lir.LIRInstructionClass;
38import org.graalvm.compiler.lir.Opcode;
39import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
40
41import jdk.vm.ci.code.Register;
42import jdk.vm.ci.meta.InvokeTarget;
43import jdk.vm.ci.meta.ResolvedJavaMethod;
44import jdk.vm.ci.meta.Value;
45
46public class AArch64Call {
47
48    public abstract static class CallOp extends AArch64LIRInstruction {
49        @Def({REG, ILLEGAL}) protected Value result;
50        @Use({REG, STACK}) protected Value[] parameters;
51        @Temp({REG, STACK}) protected Value[] temps;
52        @State protected LIRFrameState state;
53
54        protected CallOp(LIRInstructionClass<? extends CallOp> c, Value result, Value[] parameters, Value[] temps, LIRFrameState state) {
55            super(c);
56            this.result = result;
57            this.parameters = parameters;
58            this.state = state;
59            this.temps = addStackSlotsToTemporaries(parameters, temps);
60            assert temps != null;
61        }
62
63        @Override
64        public boolean destroysCallerSavedRegisters() {
65            return true;
66        }
67    }
68
69    public abstract static class MethodCallOp extends CallOp {
70        protected final ResolvedJavaMethod callTarget;
71
72        protected MethodCallOp(LIRInstructionClass<? extends MethodCallOp> c, ResolvedJavaMethod callTarget, Value result, Value[] parameters, Value[] temps, LIRFrameState state) {
73            super(c, result, parameters, temps, state);
74            this.callTarget = callTarget;
75        }
76    }
77
78    @Opcode("CALL_INDIRECT")
79    public static class IndirectCallOp extends MethodCallOp {
80        public static final LIRInstructionClass<IndirectCallOp> TYPE = LIRInstructionClass.create(IndirectCallOp.class);
81
82        @Use({REG}) protected Value targetAddress;
83
84        public IndirectCallOp(ResolvedJavaMethod callTarget, Value result, Value[] parameters, Value[] temps, Value targetAddress, LIRFrameState state) {
85            this(TYPE, callTarget, result, parameters, temps, targetAddress, state);
86        }
87
88        protected IndirectCallOp(LIRInstructionClass<? extends IndirectCallOp> c, ResolvedJavaMethod callTarget, Value result, Value[] parameters, Value[] temps, Value targetAddress,
89                        LIRFrameState state) {
90            super(c, callTarget, result, parameters, temps, state);
91            this.targetAddress = targetAddress;
92        }
93
94        @Override
95        public void emitCode(CompilationResultBuilder crb, AArch64MacroAssembler masm) {
96            Register target = asRegister(targetAddress);
97            indirectCall(crb, masm, target, callTarget, state);
98        }
99
100        @Override
101        public void verify() {
102            super.verify();
103            assert isRegister(targetAddress) : "The current register allocator cannot handle variables to be used at call sites, " + "it must be in a fixed register for now";
104        }
105    }
106
107    @Opcode("CALL_DIRECT")
108    public abstract static class DirectCallOp extends MethodCallOp {
109        public static final LIRInstructionClass<DirectCallOp> TYPE = LIRInstructionClass.create(DirectCallOp.class);
110
111        public DirectCallOp(ResolvedJavaMethod target, Value result, Value[] parameters, Value[] temps, LIRFrameState state) {
112            super(TYPE, target, result, parameters, temps, state);
113        }
114
115        protected DirectCallOp(LIRInstructionClass<? extends DirectCallOp> c, ResolvedJavaMethod callTarget, Value result, Value[] parameters, Value[] temps, LIRFrameState state) {
116            super(c, callTarget, result, parameters, temps, state);
117        }
118
119        @Override
120        public void emitCode(CompilationResultBuilder crb, AArch64MacroAssembler masm) {
121            directCall(crb, masm, callTarget, null, state);
122        }
123    }
124
125    public abstract static class ForeignCallOp extends CallOp {
126        protected final ForeignCallLinkage callTarget;
127        protected final Label label;
128
129        protected ForeignCallOp(LIRInstructionClass<? extends ForeignCallOp> c, ForeignCallLinkage callTarget, Value result, Value[] parameters, Value[] temps, LIRFrameState state, Label label) {
130            super(c, result, parameters, temps, state);
131            this.callTarget = callTarget;
132            this.label = label;
133        }
134
135        @Override
136        public boolean destroysCallerSavedRegisters() {
137            return callTarget.destroysRegisters();
138        }
139
140        @Override
141        public void emitCode(CompilationResultBuilder crb, AArch64MacroAssembler masm) {
142            emitCall(crb, masm);
143        }
144
145        protected abstract void emitCall(CompilationResultBuilder crb, AArch64MacroAssembler masm);
146    }
147
148    @Opcode("NEAR_FOREIGN_CALL")
149    public static class DirectNearForeignCallOp extends ForeignCallOp {
150        public static final LIRInstructionClass<DirectNearForeignCallOp> TYPE = LIRInstructionClass.create(DirectNearForeignCallOp.class);
151
152        public DirectNearForeignCallOp(ForeignCallLinkage callTarget, Value result, Value[] parameters, Value[] temps, LIRFrameState state, Label label) {
153            super(TYPE, callTarget, result, parameters, temps, state, label);
154        }
155
156        @Override
157        protected void emitCall(CompilationResultBuilder crb, AArch64MacroAssembler masm) {
158            directCall(crb, masm, callTarget, null, state, label);
159        }
160    }
161
162    @Opcode("FAR_FOREIGN_CALL")
163    public static class DirectFarForeignCallOp extends ForeignCallOp {
164        public static final LIRInstructionClass<DirectFarForeignCallOp> TYPE = LIRInstructionClass.create(DirectFarForeignCallOp.class);
165
166        public DirectFarForeignCallOp(ForeignCallLinkage callTarget, Value result, Value[] parameters, Value[] temps, LIRFrameState state, Label label) {
167            super(TYPE, callTarget, result, parameters, temps, state, label);
168        }
169
170        @Override
171        protected void emitCall(CompilationResultBuilder crb, AArch64MacroAssembler masm) {
172            // We can use any scratch register we want, since we know that they have been saved
173            // before calling.
174            directCall(crb, masm, callTarget, r8, state, label);
175        }
176    }
177
178    /**
179     * Tests whether linkage can be called directly under all circumstances without the need for a
180     * scratch register.
181     *
182     * Note this is a pessimistic assumption: This may return false despite a near call/jump being
183     * adequate.
184     *
185     * @param linkage Foreign call description
186     * @return true if foreign call can be called directly and does not need a scratch register to
187     *         load the address into.
188     */
189    public static boolean isNearCall(ForeignCallLinkage linkage) {
190        long maxOffset = linkage.getMaxCallTargetOffset();
191        return maxOffset != -1 && AArch64MacroAssembler.isBranchImmediateOffset(maxOffset);
192    }
193
194    public static void directCall(CompilationResultBuilder crb, AArch64MacroAssembler masm, InvokeTarget callTarget, Register scratch, LIRFrameState info) {
195        directCall(crb, masm, callTarget, scratch, info, null);
196    }
197
198    public static void directCall(CompilationResultBuilder crb, AArch64MacroAssembler masm, InvokeTarget callTarget, Register scratch, LIRFrameState info, Label label) {
199        int before = masm.position();
200        if (scratch != null) {
201            /*
202             * Offset might not fit into a 28-bit immediate, generate an indirect call with a 64-bit
203             * immediate address which is fixed up by HotSpot.
204             */
205            masm.movNativeAddress(scratch, 0L);
206            masm.blr(scratch);
207        } else {
208            // Address is fixed up by HotSpot.
209            masm.bl(0);
210        }
211        if (label != null) {
212            // We need this label to be the return address.
213            masm.bind(label);
214        }
215        int after = masm.position();
216        crb.recordDirectCall(before, after, callTarget, info);
217        crb.recordExceptionHandlers(after, info);
218        masm.ensureUniquePC();
219    }
220
221    public static void indirectCall(CompilationResultBuilder crb, AArch64MacroAssembler masm, Register dst, InvokeTarget callTarget, LIRFrameState info) {
222        int before = masm.position();
223        masm.blr(dst);
224        int after = masm.position();
225        crb.recordIndirectCall(before, after, callTarget, info);
226        crb.recordExceptionHandlers(after, info);
227        masm.ensureUniquePC();
228    }
229
230    public static void directJmp(CompilationResultBuilder crb, AArch64MacroAssembler masm, InvokeTarget target) {
231        int before = masm.position();
232        // Address is fixed up later by c++ code.
233        masm.jmp();
234        int after = masm.position();
235        crb.recordDirectCall(before, after, target, null);
236        masm.ensureUniquePC();
237    }
238
239    public static void indirectJmp(CompilationResultBuilder crb, AArch64MacroAssembler masm, Register dst, InvokeTarget target) {
240        int before = masm.position();
241        masm.jmp(dst);
242        int after = masm.position();
243        crb.recordIndirectCall(before, after, target, null);
244        masm.ensureUniquePC();
245    }
246
247    public static void directConditionalJmp(CompilationResultBuilder crb, AArch64MacroAssembler masm, InvokeTarget target, AArch64Assembler.ConditionFlag cond) {
248        int before = masm.position();
249        masm.branchConditionally(cond);
250        int after = masm.position();
251        crb.recordDirectCall(before, after, target, null);
252        masm.ensureUniquePC();
253    }
254
255}
256