Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/sourcetools/j9constantpool/com/ibm/oti/VMCPTool/FieldRef.java
6004 views
1
/*******************************************************************************
2
* Copyright (c) 2004, 2021 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
package com.ibm.oti.VMCPTool;
23
24
import java.io.PrintWriter;
25
import java.util.Map;
26
27
import org.w3c.dom.Element;
28
29
public class FieldRef extends PrimaryItem implements Constants {
30
31
protected static class Alias extends PrimaryItem.AliasWithClass {
32
final NameAndSignature nas;
33
final String cast;
34
35
Alias(VersionRange[] versions, String[] flags, ClassRef classRef, NameAndSignature nas, String cast) {
36
super(versions, flags, classRef);
37
this.nas = nas;
38
this.cast = cast;
39
}
40
41
void writeSecondaryItems(ConstantPoolStream ds) {
42
if (checkClassForWriteSecondary(ds)) {
43
ds.writeSecondaryItem(nas);
44
}
45
}
46
47
void write(ConstantPoolStream ds) {
48
if (checkClassForWrite(ds)) {
49
ds.alignTo(4);
50
ds.markInstanceField();
51
ds.writeInt(ds.getIndex(classRef));
52
ds.writeInt(ds.getOffset(nas) - ds.getOffset());
53
}
54
}
55
}
56
57
protected static class Factory implements Alias.Factory {
58
59
private final Map<String, ClassRef> classes;
60
private ClassRef classRef;
61
62
Factory(Map<String, ClassRef> classes) {
63
this.classes = classes;
64
this.classRef = null;
65
}
66
67
public PrimaryItem.Alias alias(Element e, PrimaryItem.Alias proto) {
68
Alias p = (Alias) proto;
69
return new Alias(
70
versions(e, p),
71
flags(e, p),
72
classRef(e),
73
new NameAndSignature(
74
attribute(e, "name", p != null ? p.nas.name.data : ""),
75
attribute(e, "signature", p != null ? p.nas.signature.data : "")),
76
attribute(e, "cast", p != null ? p.cast : ""));
77
}
78
79
protected ClassRef classRef(Element e) {
80
String name = attribute(e, "class", null);
81
if (name == null) {
82
return classRef;
83
}
84
if (classRef == null) {
85
classRef = classes.get(name);
86
}
87
return classes.get(name);
88
}
89
}
90
91
public FieldRef(Element e, Map<String, ClassRef> classes) {
92
super(e, FIELDALIAS, new Factory(classes));
93
}
94
95
protected FieldRef(Element e, String nodeName, com.ibm.oti.VMCPTool.PrimaryItem.Alias.Factory factory) {
96
super(e, nodeName, factory);
97
}
98
99
protected String cMacroName() {
100
return ((Alias) primary).classRef.cMacroName() + "_" + ((Alias) primary).nas.name.data.toUpperCase();
101
}
102
103
protected String cSetterMacroName() {
104
return ((Alias) primary).classRef.cMacroName() + "_SET_" + ((Alias) primary).nas.name.data.toUpperCase();
105
}
106
107
protected String fieldType() {
108
// helpers are:
109
// j9gc_objaccess_mixedObjectReadI32
110
// j9gc_objaccess_mixedObjectReadU32
111
// j9gc_objaccess_mixedObjectReadI64
112
// j9gc_objaccess_mixedObjectReadU64
113
// j9gc_objaccess_mixedObjectReadObject
114
// j9gc_objaccess_mixedObjectReadAddress
115
// j9gc_objaccess_mixedObjectStoreI32
116
// j9gc_objaccess_mixedObjectStoreU32
117
// j9gc_objaccess_mixedObjectStoreI64
118
// j9gc_objaccess_mixedObjectStoreU64
119
// j9gc_objaccess_mixedObjectStoreObject
120
// j9gc_objaccess_mixedObjectStoreAddress
121
// j9gc_objaccess_mixedObjectStoreU64Split
122
123
// Arrays and objects take precedence over cast to support pointer compression
124
switch (((Alias) primary).nas.signature.data.charAt(0)) {
125
case '[':
126
case 'L':
127
return "OBJECT";
128
default:
129
// Do nothing
130
}
131
132
// The cast then has first dibs to determine the field type
133
String cast = ((Alias) primary).cast;
134
if (cast.length() > 0) {
135
if (cast.indexOf('*') >= 0) {
136
return "ADDRESS";
137
} else if ("I_64".equals(cast)) {
138
return "I64";
139
} else if ("I_32".equals(cast)) {
140
return "I32";
141
} else if ("U_64".equals(cast)) {
142
return "U64";
143
} else if ("U_32".equals(cast)) {
144
return "U32";
145
} else if ("UDATA".equals(cast)) {
146
return "UDATA";
147
} else {
148
throw new UnsupportedOperationException("Unrecognized cast: " + cast);
149
}
150
}
151
152
// Otherwise just determine it from the signature
153
switch (((Alias) primary).nas.signature.data.charAt(0)) {
154
case '[':
155
case 'L':
156
return "OBJECT";
157
case 'J':
158
return "I64";
159
case 'D':
160
throw new UnsupportedOperationException("Double fields not supported by memory manager functions");
161
case 'F':
162
throw new UnsupportedOperationException("Float fields not supported by memory manager functions");
163
default:
164
return "U32";
165
}
166
}
167
168
public void writeMacros(ConstantPool pool, PrintWriter out) {
169
super.writeMacros(pool, out);
170
String type = fieldType();
171
String cast = ((Alias) primary).cast;
172
String castTo = cast.length() == 0 ? "" : "(" + cast + ")";
173
String macroName = cMacroName();
174
String fieldOffset = "J9VMCONSTANTPOOL_FIELD_OFFSET(J9VMTHREAD_JAVAVM(vmThread), J9VMCONSTANTPOOL_" + macroName + ")";
175
if (type.equals("ADDRESS")) {
176
fieldOffset = "J9VMCONSTANTPOOL_ADDRESS_OFFSET(J9VMTHREAD_JAVAVM(vmThread), J9VMCONSTANTPOOL_" + macroName + ")";
177
}
178
179
out.println("#define J9VM" + macroName + "_OFFSET(vmThread) " + fieldOffset);
180
out.println("#define J9VM" + macroName + "(vmThread, object) ((void)0, \\");
181
out.println("\t" + castTo + "J9OBJECT_" + type + "_LOAD(vmThread, object, J9VM" + macroName + "_OFFSET(vmThread)))");
182
out.println("#define J9VM" + cSetterMacroName() + "(vmThread, object, value) ((void)0, \\");
183
out.println("\tJ9OBJECT_" + type + "_STORE(vmThread, object, J9VM" + macroName + "_OFFSET(vmThread), (value)))");
184
185
/* Generate a second set of macros that take a J9JavaVM parameter instead of a J9VMThread */
186
187
fieldOffset = "J9VMCONSTANTPOOL_FIELD_OFFSET(javaVM, J9VMCONSTANTPOOL_" + macroName + ")";
188
if (type.equals("ADDRESS")) {
189
fieldOffset = "J9VMCONSTANTPOOL_ADDRESS_OFFSET(javaVM, J9VMCONSTANTPOOL_" + macroName + ")";
190
}
191
192
out.println("#define J9VM" + macroName + "_OFFSET_VM(javaVM) " + fieldOffset);
193
out.println("#define J9VM" + macroName + "_VM(javaVM, object) ((void)0, \\");
194
out.println("\t" + castTo + "J9OBJECT_" + type + "_LOAD_VM(javaVM, object, J9VM" + macroName + "_OFFSET_VM(javaVM)))");
195
out.println("#define J9VM" + cSetterMacroName() + "_VM(javaVM, object, value) ((void)0, \\");
196
out.println("\tJ9OBJECT_" + type + "_STORE_VM(javaVM, object, J9VM" + macroName + "_OFFSET_VM(javaVM), (value)))");
197
}
198
199
protected void superWriteMacros(ConstantPool pool, PrintWriter out) {
200
super.writeMacros(pool, out);
201
}
202
203
public String commentText() {
204
Alias alias = (Alias) primary;
205
return "FieldRef[" + alias.classRef.getClassName() + "." + alias.nas.name.data + " " + alias.nas.signature.data + "]";
206
}
207
208
}
209
210