Skip to content

Commit 3c00ee1

Browse files
committed
Add ShadowedRISCV64 for cleaner reflections, update comments and add documentation for RISC-V relocations
1 parent 4b4f824 commit 3c00ee1

File tree

12 files changed

+213
-121
lines changed

12 files changed

+213
-121
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright (c) 2022, 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. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package org.graalvm.compiler.core.riscv64;
27+
28+
import jdk.vm.ci.code.Register;
29+
import jdk.vm.ci.code.RegisterArray;
30+
31+
public class ShadowedRISCV64 {
32+
public static final Class<?> riscv64 = RISCV64ReflectionUtil.getArch(false);
33+
public static final Class<?> riscv64OrNull = RISCV64ReflectionUtil.getArch(true);
34+
35+
public static final Register x0 = getRegister("x0");
36+
public static final Register x1 = getRegister("x1");
37+
public static final Register x2 = getRegister("x2");
38+
public static final Register x3 = getRegister("x3");
39+
public static final Register x4 = getRegister("x4");
40+
public static final Register x5 = getRegister("x5");
41+
public static final Register x6 = getRegister("x6");
42+
public static final Register x7 = getRegister("x7");
43+
public static final Register x8 = getRegister("x8");
44+
public static final Register x9 = getRegister("x9");
45+
public static final Register x10 = getRegister("x10");
46+
public static final Register x11 = getRegister("x11");
47+
public static final Register x12 = getRegister("x12");
48+
public static final Register x13 = getRegister("x13");
49+
public static final Register x14 = getRegister("x14");
50+
public static final Register x15 = getRegister("x15");
51+
public static final Register x16 = getRegister("x16");
52+
public static final Register x17 = getRegister("x17");
53+
public static final Register x18 = getRegister("x18");
54+
public static final Register x19 = getRegister("x19");
55+
public static final Register x20 = getRegister("x20");
56+
public static final Register x21 = getRegister("x21");
57+
public static final Register x22 = getRegister("x22");
58+
public static final Register x23 = getRegister("x23");
59+
public static final Register x24 = getRegister("x24");
60+
public static final Register x25 = getRegister("x25");
61+
public static final Register x26 = getRegister("x26");
62+
public static final Register x27 = getRegister("x27");
63+
public static final Register x28 = getRegister("x28");
64+
public static final Register x29 = getRegister("x29");
65+
public static final Register x30 = getRegister("x30");
66+
public static final Register x31 = getRegister("x31");
67+
68+
public static final Register f0 = getRegister("f0");
69+
public static final Register f1 = getRegister("f1");
70+
public static final Register f2 = getRegister("f2");
71+
public static final Register f3 = getRegister("f3");
72+
public static final Register f4 = getRegister("f4");
73+
public static final Register f5 = getRegister("f5");
74+
public static final Register f6 = getRegister("f6");
75+
public static final Register f7 = getRegister("f7");
76+
public static final Register f8 = getRegister("f8");
77+
public static final Register f9 = getRegister("f9");
78+
public static final Register f10 = getRegister("f10");
79+
public static final Register f11 = getRegister("f11");
80+
public static final Register f12 = getRegister("f12");
81+
public static final Register f13 = getRegister("f13");
82+
public static final Register f14 = getRegister("f14");
83+
public static final Register f15 = getRegister("f15");
84+
public static final Register f16 = getRegister("f16");
85+
public static final Register f17 = getRegister("f17");
86+
public static final Register f18 = getRegister("f18");
87+
public static final Register f19 = getRegister("f19");
88+
public static final Register f20 = getRegister("f20");
89+
public static final Register f21 = getRegister("f21");
90+
public static final Register f22 = getRegister("f22");
91+
public static final Register f23 = getRegister("f23");
92+
public static final Register f24 = getRegister("f24");
93+
public static final Register f25 = getRegister("f25");
94+
public static final Register f26 = getRegister("f26");
95+
public static final Register f27 = getRegister("f27");
96+
public static final Register f28 = getRegister("f28");
97+
public static final Register f29 = getRegister("f29");
98+
public static final Register f30 = getRegister("f30");
99+
public static final Register f31 = getRegister("f31");
100+
101+
public static final RegisterArray allRegisters = riscv64OrNull == null ? null : RISCV64ReflectionUtil.readStaticField(riscv64OrNull, "allRegisters");
102+
103+
private static Register getRegister(String register) {
104+
return riscv64OrNull == null ? null : RISCV64ReflectionUtil.readStaticField(riscv64OrNull, register);
105+
}
106+
}

compiler/src/org.graalvm.compiler.hotspot.riscv64/src/org/graalvm/compiler/hotspot/riscv64/RISCV64HotSpotBackendFactory.java

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import org.graalvm.compiler.core.common.alloc.RegisterAllocationConfig;
3131
import org.graalvm.compiler.core.riscv64.RISCV64ReflectionUtil;
32+
import org.graalvm.compiler.core.riscv64.ShadowedRISCV64;
3233
import org.graalvm.compiler.debug.GraalError;
3334
import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
3435
import org.graalvm.compiler.hotspot.HotSpotBackend;
@@ -76,9 +77,8 @@ public String getName() {
7677

7778
@Override
7879
public Class<? extends Architecture> getArchitecture() {
79-
Class<?> riscv64 = RISCV64ReflectionUtil.getArch(true);
80-
if (riscv64 != null) {
81-
return riscv64.asSubclass(Architecture.class);
80+
if (ShadowedRISCV64.riscv64OrNull != null) {
81+
return ShadowedRISCV64.riscv64OrNull.asSubclass(Architecture.class);
8282
} else {
8383
return null;
8484
}
@@ -119,10 +119,9 @@ public ReferenceMapBuilder newReferenceMapBuilder(int totalFrameSize) {
119119

120120
@Override
121121
protected HotSpotRegistersProvider createRegisters() {
122-
Class<?> riscv64 = RISCV64ReflectionUtil.getArch(false);
123122
Class<?> riscv64HotSpotRegisterConfig = RISCV64ReflectionUtil.lookupClass(false, RISCV64ReflectionUtil.hotSpotClass);
124123
Register tp = RISCV64ReflectionUtil.readStaticField(riscv64HotSpotRegisterConfig, "tp");
125-
Register x27 = RISCV64ReflectionUtil.readStaticField(riscv64, "x27");
124+
Register x27 = ShadowedRISCV64.x27;
126125
Register sp = RISCV64ReflectionUtil.readStaticField(riscv64HotSpotRegisterConfig, "sp");
127126
return new HotSpotRegisters(tp, x27, sp);
128127
}
@@ -152,34 +151,33 @@ protected HotSpotLoweringProvider createLowerer(HotSpotGraalRuntimeProvider graa
152151
protected Value[] createNativeABICallerSaveRegisters(@SuppressWarnings("unused") GraalHotSpotVMConfig config, RegisterConfig regConfig) {
153152
List<Register> callerSave = new ArrayList<>(regConfig.getAllocatableRegisters().asList());
154153
// Removing callee-saved registers.
155-
Class<?> riscv64 = RISCV64ReflectionUtil.getArch(false);
156154
/* General Purpose Registers. */
157-
callerSave.remove(RISCV64ReflectionUtil.readStaticField(riscv64, "x2"));
158-
callerSave.remove(RISCV64ReflectionUtil.readStaticField(riscv64, "x8"));
159-
callerSave.remove(RISCV64ReflectionUtil.readStaticField(riscv64, "x9"));
160-
callerSave.remove(RISCV64ReflectionUtil.readStaticField(riscv64, "x10"));
161-
callerSave.remove(RISCV64ReflectionUtil.readStaticField(riscv64, "x19"));
162-
callerSave.remove(RISCV64ReflectionUtil.readStaticField(riscv64, "x20"));
163-
callerSave.remove(RISCV64ReflectionUtil.readStaticField(riscv64, "x21"));
164-
callerSave.remove(RISCV64ReflectionUtil.readStaticField(riscv64, "x22"));
165-
callerSave.remove(RISCV64ReflectionUtil.readStaticField(riscv64, "x23"));
166-
callerSave.remove(RISCV64ReflectionUtil.readStaticField(riscv64, "x24"));
167-
callerSave.remove(RISCV64ReflectionUtil.readStaticField(riscv64, "x25"));
168-
callerSave.remove(RISCV64ReflectionUtil.readStaticField(riscv64, "x26"));
169-
callerSave.remove(RISCV64ReflectionUtil.readStaticField(riscv64, "x27"));
155+
callerSave.remove(ShadowedRISCV64.x2);
156+
callerSave.remove(ShadowedRISCV64.x8);
157+
callerSave.remove(ShadowedRISCV64.x9);
158+
callerSave.remove(ShadowedRISCV64.x10);
159+
callerSave.remove(ShadowedRISCV64.x19);
160+
callerSave.remove(ShadowedRISCV64.x20);
161+
callerSave.remove(ShadowedRISCV64.x21);
162+
callerSave.remove(ShadowedRISCV64.x22);
163+
callerSave.remove(ShadowedRISCV64.x23);
164+
callerSave.remove(ShadowedRISCV64.x24);
165+
callerSave.remove(ShadowedRISCV64.x25);
166+
callerSave.remove(ShadowedRISCV64.x26);
167+
callerSave.remove(ShadowedRISCV64.x27);
170168
/* Floating-Point Registers. */
171-
callerSave.remove(RISCV64ReflectionUtil.readStaticField(riscv64, "f8"));
172-
callerSave.remove(RISCV64ReflectionUtil.readStaticField(riscv64, "f9"));
173-
callerSave.remove(RISCV64ReflectionUtil.readStaticField(riscv64, "f10"));
174-
callerSave.remove(RISCV64ReflectionUtil.readStaticField(riscv64, "f19"));
175-
callerSave.remove(RISCV64ReflectionUtil.readStaticField(riscv64, "f20"));
176-
callerSave.remove(RISCV64ReflectionUtil.readStaticField(riscv64, "f21"));
177-
callerSave.remove(RISCV64ReflectionUtil.readStaticField(riscv64, "f22"));
178-
callerSave.remove(RISCV64ReflectionUtil.readStaticField(riscv64, "f23"));
179-
callerSave.remove(RISCV64ReflectionUtil.readStaticField(riscv64, "f24"));
180-
callerSave.remove(RISCV64ReflectionUtil.readStaticField(riscv64, "f25"));
181-
callerSave.remove(RISCV64ReflectionUtil.readStaticField(riscv64, "f26"));
182-
callerSave.remove(RISCV64ReflectionUtil.readStaticField(riscv64, "f27"));
169+
callerSave.remove(ShadowedRISCV64.f8);
170+
callerSave.remove(ShadowedRISCV64.f9);
171+
callerSave.remove(ShadowedRISCV64.f10);
172+
callerSave.remove(ShadowedRISCV64.f19);
173+
callerSave.remove(ShadowedRISCV64.f20);
174+
callerSave.remove(ShadowedRISCV64.f21);
175+
callerSave.remove(ShadowedRISCV64.f22);
176+
callerSave.remove(ShadowedRISCV64.f23);
177+
callerSave.remove(ShadowedRISCV64.f24);
178+
callerSave.remove(ShadowedRISCV64.f25);
179+
callerSave.remove(ShadowedRISCV64.f26);
180+
callerSave.remove(ShadowedRISCV64.f27);
183181

184182
Value[] nativeABICallerSaveRegisters = new Value[callerSave.size()];
185183
for (int i = 0; i < callerSave.size(); i++) {

compiler/src/org.graalvm.compiler.hotspot.riscv64/src/org/graalvm/compiler/hotspot/riscv64/RISCV64HotSpotForeignCallsProvider.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@
2525
package org.graalvm.compiler.hotspot.riscv64;
2626

2727
import static jdk.vm.ci.meta.Value.ILLEGAL;
28+
import static org.graalvm.compiler.hotspot.HotSpotBackend.EXCEPTION_HANDLER;
29+
import static org.graalvm.compiler.hotspot.HotSpotBackend.EXCEPTION_HANDLER_IN_CALLER;
2830
import static org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.JUMP_ADDRESS;
2931
import static org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.RegisterEffect.DESTROYS_ALL_CALLER_SAVE_REGISTERS;
3032

3133
import org.graalvm.compiler.core.common.LIRKind;
32-
import org.graalvm.compiler.core.riscv64.RISCV64ReflectionUtil;
33-
import org.graalvm.compiler.hotspot.HotSpotBackend;
34+
import org.graalvm.compiler.core.riscv64.ShadowedRISCV64;
3435
import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkageImpl;
3536
import org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider;
3637
import org.graalvm.compiler.hotspot.meta.HotSpotHostForeignCallsProvider;
@@ -39,7 +40,6 @@
3940
import org.graalvm.compiler.options.OptionValues;
4041

4142
import jdk.vm.ci.code.CallingConvention;
42-
import jdk.vm.ci.code.Register;
4343
import jdk.vm.ci.code.RegisterValue;
4444
import jdk.vm.ci.code.TargetDescription;
4545
import jdk.vm.ci.hotspot.HotSpotCodeCacheProvider;
@@ -65,15 +65,11 @@ public void initialize(HotSpotProviders providers, OptionValues options) {
6565

6666
// The calling convention for the exception handler stub is (only?) defined in
6767
// TemplateInterpreterGenerator::generate_throw_exception()
68-
Class<?> riscv64 = RISCV64ReflectionUtil.getArch(false);
69-
70-
RegisterValue exception = ((Register) RISCV64ReflectionUtil.readStaticField(riscv64, "x5")).asValue(LIRKind.reference(word));
71-
RegisterValue exceptionPc = ((Register) RISCV64ReflectionUtil.readStaticField(riscv64, "x7")).asValue(LIRKind.value(word));
72-
68+
RegisterValue exception = ShadowedRISCV64.x5.asValue(LIRKind.reference(word));
69+
RegisterValue exceptionPc = ShadowedRISCV64.x7.asValue(LIRKind.value(word));
7370
CallingConvention exceptionCc = new CallingConvention(0, ILLEGAL, exception, exceptionPc);
74-
register(new HotSpotForeignCallLinkageImpl(HotSpotBackend.EXCEPTION_HANDLER, 0L, DESTROYS_ALL_CALLER_SAVE_REGISTERS, exceptionCc, null));
75-
register(new HotSpotForeignCallLinkageImpl(HotSpotBackend.EXCEPTION_HANDLER_IN_CALLER, JUMP_ADDRESS, DESTROYS_ALL_CALLER_SAVE_REGISTERS, exceptionCc,
76-
null));
71+
register(new HotSpotForeignCallLinkageImpl(EXCEPTION_HANDLER, 0L, DESTROYS_ALL_CALLER_SAVE_REGISTERS, exceptionCc, null));
72+
register(new HotSpotForeignCallLinkageImpl(EXCEPTION_HANDLER_IN_CALLER, JUMP_ADDRESS, DESTROYS_ALL_CALLER_SAVE_REGISTERS, exceptionCc, null));
7773

7874
super.initialize(providers, options);
7975
}

substratevm/src/com.oracle.objectfile/src/com/oracle/objectfile/elf/ELFMachine.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,9 @@ public long toLong() {
413413
}
414414
}
415415

416+
/**
417+
* Reference: https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-elf.adoc.
418+
*/
416419
enum ELFRISCV64Relocation implements ELFRelocationMethod {
417420
R_RISCV_NONE(0),
418421
R_RISCV_32(1),
@@ -468,7 +471,7 @@ enum ELFRISCV64Relocation implements ELFRelocationMethod {
468471
R_RISCV_SET16(55),
469472
R_RISCV_SET32(56),
470473
R_RISCV_32_PCREL(57),
471-
R_RISCV_NUM(58);
474+
R_RISCV_IRELATIVE(58);
472475

473476
private final long code;
474477

substratevm/src/com.oracle.svm.core.graal.riscv64/src/com/oracle/svm/core/graal/riscv64/RISCV64ReservedRegisters.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*/
2525
package com.oracle.svm.core.graal.riscv64;
2626

27-
import org.graalvm.compiler.core.riscv64.RISCV64ReflectionUtil;
27+
import org.graalvm.compiler.core.riscv64.ShadowedRISCV64;
2828
import org.graalvm.nativeimage.Platform;
2929
import org.graalvm.nativeimage.Platforms;
3030

@@ -39,12 +39,9 @@ public final class RISCV64ReservedRegisters extends ReservedRegisters {
3939
public static Register stackBaseRegisterCandidate;
4040

4141
static {
42-
Class<?> riscv64 = RISCV64ReflectionUtil.getArch(true);
43-
if (riscv64 != null) {
44-
stackBaseRegisterCandidate = RISCV64ReflectionUtil.readStaticField(riscv64, "x2");
45-
threadRegisterCandidate = RISCV64ReflectionUtil.readStaticField(riscv64, "x23");
46-
heapBaseRegisterCandidate = RISCV64ReflectionUtil.readStaticField(riscv64, "x27");
47-
}
42+
stackBaseRegisterCandidate = ShadowedRISCV64.x2;
43+
threadRegisterCandidate = ShadowedRISCV64.x23;
44+
heapBaseRegisterCandidate = ShadowedRISCV64.x27;
4845
}
4946

5047
@Platforms(Platform.HOSTED_ONLY.class)

0 commit comments

Comments
 (0)