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.sparc;
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.code.ValueUtil.asRegister;
29import static jdk.vm.ci.code.ValueUtil.isRegister;
30import static jdk.vm.ci.sparc.SPARC.o7;
31
32import org.graalvm.compiler.asm.sparc.SPARCAddress;
33import org.graalvm.compiler.asm.sparc.SPARCMacroAssembler;
34import org.graalvm.compiler.asm.sparc.SPARCMacroAssembler.ScratchRegister;
35import org.graalvm.compiler.core.common.spi.ForeignCallLinkage;
36import org.graalvm.compiler.debug.GraalError;
37import org.graalvm.compiler.lir.LIRFrameState;
38import org.graalvm.compiler.lir.LIRInstructionClass;
39import org.graalvm.compiler.lir.Opcode;
40import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
41
42import jdk.vm.ci.code.Register;
43import jdk.vm.ci.meta.InvokeTarget;
44import jdk.vm.ci.meta.ResolvedJavaMethod;
45import jdk.vm.ci.meta.Value;
46
47public class SPARCCall {
48
49    public abstract static class CallOp extends SPARCLIRInstruction {
50        @Def({REG, ILLEGAL}) protected Value result;
51        @Use({REG, STACK}) protected Value[] parameters;
52        @Temp({REG, STACK}) protected Value[] temps;
53        @State protected LIRFrameState state;
54
55        protected CallOp(LIRInstructionClass<? extends CallOp> c, SizeEstimate size, Value result, Value[] parameters, Value[] temps, LIRFrameState state) {
56            super(c, size);
57            this.result = result;
58            this.parameters = parameters;
59            this.state = state;
60            this.temps = addStackSlotsToTemporaries(parameters, temps);
61            assert temps != null;
62        }
63
64        @Override
65        public boolean destroysCallerSavedRegisters() {
66            return true;
67        }
68    }
69
70    public abstract static class MethodCallOp extends CallOp {
71
72        protected final ResolvedJavaMethod callTarget;
73
74        protected MethodCallOp(LIRInstructionClass<? extends MethodCallOp> c, SizeEstimate size, ResolvedJavaMethod callTarget, Value result, Value[] parameters, Value[] temps, LIRFrameState state) {
75            super(c, size, result, parameters, temps, state);
76            this.callTarget = callTarget;
77        }
78
79    }
80
81    @Opcode("CALL_DIRECT")
82    public abstract static class DirectCallOp extends MethodCallOp {
83        private boolean emitted = false;
84        private int before = -1;
85
86        public DirectCallOp(LIRInstructionClass<? extends DirectCallOp> c, SizeEstimate size, ResolvedJavaMethod callTarget, Value result, Value[] parameters, Value[] temps, LIRFrameState state) {
87            super(c, size, callTarget, result, parameters, temps, state);
88        }
89
90        @Override
91        public void emitCode(CompilationResultBuilder crb, SPARCMacroAssembler masm) {
92            if (!emitted) {
93                emitCallPrefixCode(crb, masm);
94                directCall(crb, masm, callTarget, null, state);
95            } else {
96                int after = masm.position();
97                if (after - before == 4) {
98                    masm.nop();
99                } else if (after - before == 8) {
100                    // everything is fine;
101                } else {
102                    GraalError.shouldNotReachHere("" + (after - before));
103                }
104                after = masm.position();
105                crb.recordDirectCall(before, after, callTarget, state);
106                crb.recordExceptionHandlers(after, state);
107                masm.ensureUniquePC();
108            }
109        }
110
111        @SuppressWarnings("unused")
112        public void emitCallPrefixCode(CompilationResultBuilder crb, SPARCMacroAssembler masm) {
113            //
114        }
115
116        public void emitControlTransfer(CompilationResultBuilder crb, SPARCMacroAssembler masm) {
117            assert !emitted;
118            emitCallPrefixCode(crb, masm);
119            before = masm.call(0);
120            emitted = true;
121        }
122
123        public void resetState() {
124            emitted = false;
125            before = -1;
126        }
127    }
128
129    @Opcode("CALL_INDIRECT")
130    public abstract static class IndirectCallOp extends MethodCallOp {
131        @Use({REG}) protected Value targetAddress;
132
133        protected IndirectCallOp(LIRInstructionClass<? extends IndirectCallOp> c, SizeEstimate size, ResolvedJavaMethod callTarget, Value result, Value[] parameters, Value[] temps,
134                        Value targetAddress, LIRFrameState state) {
135            super(c, size, callTarget, result, parameters, temps, state);
136            this.targetAddress = targetAddress;
137        }
138
139        @Override
140        public void emitCode(CompilationResultBuilder crb, SPARCMacroAssembler masm) {
141            indirectCall(crb, masm, asRegister(targetAddress), callTarget, state);
142        }
143
144        @Override
145        public void verify() {
146            super.verify();
147            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";
148        }
149    }
150
151    public abstract static class ForeignCallOp extends CallOp {
152        public static final LIRInstructionClass<ForeignCallOp> TYPE = LIRInstructionClass.create(ForeignCallOp.class);
153
154        protected final ForeignCallLinkage callTarget;
155
156        public ForeignCallOp(LIRInstructionClass<? extends ForeignCallOp> c, SizeEstimate size, ForeignCallLinkage callTarget, Value result, Value[] parameters, Value[] temps, LIRFrameState state) {
157            super(c, size, result, parameters, temps, state);
158            this.callTarget = callTarget;
159        }
160
161        @Override
162        public boolean destroysCallerSavedRegisters() {
163            return callTarget.destroysRegisters();
164        }
165    }
166
167    @Opcode("NEAR_FOREIGN_CALL")
168    public static final class DirectNearForeignCallOp extends ForeignCallOp {
169        public static final LIRInstructionClass<DirectNearForeignCallOp> TYPE = LIRInstructionClass.create(DirectNearForeignCallOp.class);
170        public static final SizeEstimate SIZE = SizeEstimate.create(1);
171
172        public DirectNearForeignCallOp(ForeignCallLinkage linkage, Value result, Value[] parameters, Value[] temps, LIRFrameState state) {
173            super(TYPE, SIZE, linkage, result, parameters, temps, state);
174        }
175
176        @Override
177        public void emitCode(CompilationResultBuilder crb, SPARCMacroAssembler masm) {
178            directCall(crb, masm, callTarget, null, state);
179        }
180    }
181
182    @Opcode("FAR_FOREIGN_CALL")
183    public static final class DirectFarForeignCallOp extends ForeignCallOp {
184        public static final LIRInstructionClass<DirectFarForeignCallOp> TYPE = LIRInstructionClass.create(DirectFarForeignCallOp.class);
185        public static final SizeEstimate SIZE = SizeEstimate.create(1);
186
187        public DirectFarForeignCallOp(ForeignCallLinkage callTarget, Value result, Value[] parameters, Value[] temps, LIRFrameState state) {
188            super(TYPE, SIZE, callTarget, result, parameters, temps, state);
189        }
190
191        @Override
192        public void emitCode(CompilationResultBuilder crb, SPARCMacroAssembler masm) {
193            try (ScratchRegister scratch = masm.getScratchRegister()) {
194                directCall(crb, masm, callTarget, scratch.getRegister(), state);
195            }
196        }
197    }
198
199    public static void directCall(CompilationResultBuilder crb, SPARCMacroAssembler masm, InvokeTarget callTarget, Register scratch, LIRFrameState info) {
200        int before;
201        if (scratch != null) {
202            // offset might not fit a 30-bit displacement, generate an
203            // indirect call with a 64-bit immediate
204            before = masm.position();
205            masm.sethix(0L, scratch, true);
206            masm.jmpl(scratch, 0, o7);
207        } else {
208            before = masm.call(0);
209        }
210        masm.nop();  // delay slot
211        int after = masm.position();
212        crb.recordDirectCall(before, after, callTarget, info);
213        crb.recordExceptionHandlers(after, info);
214        masm.ensureUniquePC();
215    }
216
217    public static void indirectJmp(CompilationResultBuilder crb, SPARCMacroAssembler masm, Register dst, InvokeTarget target) {
218        int before = masm.position();
219        masm.sethix(0L, dst, true);
220        masm.jmp(new SPARCAddress(dst, 0));
221        masm.nop();  // delay slot
222        int after = masm.position();
223        crb.recordIndirectCall(before, after, target, null);
224        masm.ensureUniquePC();
225    }
226
227    public static void indirectCall(CompilationResultBuilder crb, SPARCMacroAssembler masm, Register dst, InvokeTarget callTarget, LIRFrameState info) {
228        int before = masm.jmpl(dst, 0, o7);
229        masm.nop();  // delay slot
230        int after = masm.position();
231        crb.recordIndirectCall(before, after, callTarget, info);
232        crb.recordExceptionHandlers(after, info);
233        masm.ensureUniquePC();
234    }
235}
236