Path: blob/master/sourcetools/j9constantpool/com/ibm/oti/VMCPTool/FieldRef.java
6004 views
/*******************************************************************************1* Copyright (c) 2004, 2021 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* 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-exception20*******************************************************************************/21package com.ibm.oti.VMCPTool;2223import java.io.PrintWriter;24import java.util.Map;2526import org.w3c.dom.Element;2728public class FieldRef extends PrimaryItem implements Constants {2930protected static class Alias extends PrimaryItem.AliasWithClass {31final NameAndSignature nas;32final String cast;3334Alias(VersionRange[] versions, String[] flags, ClassRef classRef, NameAndSignature nas, String cast) {35super(versions, flags, classRef);36this.nas = nas;37this.cast = cast;38}3940void writeSecondaryItems(ConstantPoolStream ds) {41if (checkClassForWriteSecondary(ds)) {42ds.writeSecondaryItem(nas);43}44}4546void write(ConstantPoolStream ds) {47if (checkClassForWrite(ds)) {48ds.alignTo(4);49ds.markInstanceField();50ds.writeInt(ds.getIndex(classRef));51ds.writeInt(ds.getOffset(nas) - ds.getOffset());52}53}54}5556protected static class Factory implements Alias.Factory {5758private final Map<String, ClassRef> classes;59private ClassRef classRef;6061Factory(Map<String, ClassRef> classes) {62this.classes = classes;63this.classRef = null;64}6566public PrimaryItem.Alias alias(Element e, PrimaryItem.Alias proto) {67Alias p = (Alias) proto;68return new Alias(69versions(e, p),70flags(e, p),71classRef(e),72new NameAndSignature(73attribute(e, "name", p != null ? p.nas.name.data : ""),74attribute(e, "signature", p != null ? p.nas.signature.data : "")),75attribute(e, "cast", p != null ? p.cast : ""));76}7778protected ClassRef classRef(Element e) {79String name = attribute(e, "class", null);80if (name == null) {81return classRef;82}83if (classRef == null) {84classRef = classes.get(name);85}86return classes.get(name);87}88}8990public FieldRef(Element e, Map<String, ClassRef> classes) {91super(e, FIELDALIAS, new Factory(classes));92}9394protected FieldRef(Element e, String nodeName, com.ibm.oti.VMCPTool.PrimaryItem.Alias.Factory factory) {95super(e, nodeName, factory);96}9798protected String cMacroName() {99return ((Alias) primary).classRef.cMacroName() + "_" + ((Alias) primary).nas.name.data.toUpperCase();100}101102protected String cSetterMacroName() {103return ((Alias) primary).classRef.cMacroName() + "_SET_" + ((Alias) primary).nas.name.data.toUpperCase();104}105106protected String fieldType() {107// helpers are:108// j9gc_objaccess_mixedObjectReadI32109// j9gc_objaccess_mixedObjectReadU32110// j9gc_objaccess_mixedObjectReadI64111// j9gc_objaccess_mixedObjectReadU64112// j9gc_objaccess_mixedObjectReadObject113// j9gc_objaccess_mixedObjectReadAddress114// j9gc_objaccess_mixedObjectStoreI32115// j9gc_objaccess_mixedObjectStoreU32116// j9gc_objaccess_mixedObjectStoreI64117// j9gc_objaccess_mixedObjectStoreU64118// j9gc_objaccess_mixedObjectStoreObject119// j9gc_objaccess_mixedObjectStoreAddress120// j9gc_objaccess_mixedObjectStoreU64Split121122// Arrays and objects take precedence over cast to support pointer compression123switch (((Alias) primary).nas.signature.data.charAt(0)) {124case '[':125case 'L':126return "OBJECT";127default:128// Do nothing129}130131// The cast then has first dibs to determine the field type132String cast = ((Alias) primary).cast;133if (cast.length() > 0) {134if (cast.indexOf('*') >= 0) {135return "ADDRESS";136} else if ("I_64".equals(cast)) {137return "I64";138} else if ("I_32".equals(cast)) {139return "I32";140} else if ("U_64".equals(cast)) {141return "U64";142} else if ("U_32".equals(cast)) {143return "U32";144} else if ("UDATA".equals(cast)) {145return "UDATA";146} else {147throw new UnsupportedOperationException("Unrecognized cast: " + cast);148}149}150151// Otherwise just determine it from the signature152switch (((Alias) primary).nas.signature.data.charAt(0)) {153case '[':154case 'L':155return "OBJECT";156case 'J':157return "I64";158case 'D':159throw new UnsupportedOperationException("Double fields not supported by memory manager functions");160case 'F':161throw new UnsupportedOperationException("Float fields not supported by memory manager functions");162default:163return "U32";164}165}166167public void writeMacros(ConstantPool pool, PrintWriter out) {168super.writeMacros(pool, out);169String type = fieldType();170String cast = ((Alias) primary).cast;171String castTo = cast.length() == 0 ? "" : "(" + cast + ")";172String macroName = cMacroName();173String fieldOffset = "J9VMCONSTANTPOOL_FIELD_OFFSET(J9VMTHREAD_JAVAVM(vmThread), J9VMCONSTANTPOOL_" + macroName + ")";174if (type.equals("ADDRESS")) {175fieldOffset = "J9VMCONSTANTPOOL_ADDRESS_OFFSET(J9VMTHREAD_JAVAVM(vmThread), J9VMCONSTANTPOOL_" + macroName + ")";176}177178out.println("#define J9VM" + macroName + "_OFFSET(vmThread) " + fieldOffset);179out.println("#define J9VM" + macroName + "(vmThread, object) ((void)0, \\");180out.println("\t" + castTo + "J9OBJECT_" + type + "_LOAD(vmThread, object, J9VM" + macroName + "_OFFSET(vmThread)))");181out.println("#define J9VM" + cSetterMacroName() + "(vmThread, object, value) ((void)0, \\");182out.println("\tJ9OBJECT_" + type + "_STORE(vmThread, object, J9VM" + macroName + "_OFFSET(vmThread), (value)))");183184/* Generate a second set of macros that take a J9JavaVM parameter instead of a J9VMThread */185186fieldOffset = "J9VMCONSTANTPOOL_FIELD_OFFSET(javaVM, J9VMCONSTANTPOOL_" + macroName + ")";187if (type.equals("ADDRESS")) {188fieldOffset = "J9VMCONSTANTPOOL_ADDRESS_OFFSET(javaVM, J9VMCONSTANTPOOL_" + macroName + ")";189}190191out.println("#define J9VM" + macroName + "_OFFSET_VM(javaVM) " + fieldOffset);192out.println("#define J9VM" + macroName + "_VM(javaVM, object) ((void)0, \\");193out.println("\t" + castTo + "J9OBJECT_" + type + "_LOAD_VM(javaVM, object, J9VM" + macroName + "_OFFSET_VM(javaVM)))");194out.println("#define J9VM" + cSetterMacroName() + "_VM(javaVM, object, value) ((void)0, \\");195out.println("\tJ9OBJECT_" + type + "_STORE_VM(javaVM, object, J9VM" + macroName + "_OFFSET_VM(javaVM), (value)))");196}197198protected void superWriteMacros(ConstantPool pool, PrintWriter out) {199super.writeMacros(pool, out);200}201202public String commentText() {203Alias alias = (Alias) primary;204return "FieldRef[" + alias.classRef.getClassName() + "." + alias.nas.name.data + " " + alias.nas.signature.data + "]";205}206207}208209210