Path: blob/master/sourcetools/j9constantpool/com/ibm/oti/VMCPTool/StaticFieldRef.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 StaticFieldRef extends FieldRef implements Constants {2930private static class Alias extends FieldRef.Alias {31Alias(VersionRange[] versions, String[] flags, ClassRef classRef, NameAndSignature nas, String cast) {32super(versions, flags, classRef, nas, cast);33}3435void write(ConstantPoolStream ds) {36if (checkClassForWrite(ds)) {37ds.alignTo(4);38ds.markStaticField();39ds.writeInt(ds.getIndex(classRef));40ds.writeInt(ds.getOffset(nas) - ds.getOffset());41}42}43}4445private static class Factory extends FieldRef.Factory {4647Factory(Map<String, ClassRef> classes) {48super(classes);49}5051public PrimaryItem.Alias alias(Element e, PrimaryItem.Alias proto) {52Alias p = (Alias) proto;53return new Alias(54versions(e, p),55flags(e, p),56classRef(e),57new NameAndSignature(58attribute(e, "name", p != null ? p.nas.name.data : ""),59attribute(e, "signature", p != null ? p.nas.signature.data : "")),60attribute(e, "cast", p != null ? p.cast : ""));61}6263}6465public StaticFieldRef(Element e, Map<String, ClassRef> classes) {66super(e, FIELDALIAS, new Factory(classes));67}6869public void writeMacros(ConstantPool pool, PrintWriter out) {70superWriteMacros(pool, out);71String type = fieldType();72String cast = ((FieldRef.Alias) primary).cast;73String castTo = cast.length() == 0 ? "" : "(" + cast + ")";74String macroName = cMacroName();75String fieldOffset = "J9VMCONSTANTPOOL_STATICFIELD_ADDRESS(J9VMTHREAD_JAVAVM(vmThread), J9VMCONSTANTPOOL_" + macroName + ")";7677out.println("#define J9VM" + macroName + "_ADDRESS(vmThread) " + fieldOffset);78out.println("#define J9VM" + macroName + "(vmThread, clazz) ((void)0, \\");79out.println("\t" + castTo + "J9STATIC_" + type + "_LOAD(vmThread, clazz, J9VM" + macroName + "_ADDRESS(vmThread)))");80out.println("#define J9VM" + cSetterMacroName() + "(vmThread, clazz, value) ((void)0, \\");81out.println("\tJ9STATIC_" + type + "_STORE(vmThread, clazz, J9VM" + macroName + "_ADDRESS(vmThread), (value)))");8283/* Generate a second set of macros that take a J9JavaVM parameter instead of a J9VMThread */8485fieldOffset = "J9VMCONSTANTPOOL_STATICFIELD_ADDRESS_VM(javaVM, J9VMCONSTANTPOOL_" + macroName + ")";8687out.println("#define J9VM" + macroName + "_ADDRESS_VM(javaVM) " + fieldOffset);88out.println("#define J9VM" + macroName + "_VM(javaVM, clazz) ((void)0, \\");89out.println("\t" + castTo + "J9STATIC_" + type + "_LOAD_VM(javaVM, clazz, J9VM" + macroName + "_ADDRESS_VM(javaVM)))");90out.println("#define J9VM" + cSetterMacroName() + "_VM(javaVM, clazz, value) ((void)0, \\");91out.println("\tJ9STATIC_" + type + "_STORE_VM(javaVM, clazz, J9VM" + macroName + "_ADDRESS_VM(javaVM), (value)))");92}9394}959697