use of com.ibm.j9ddr.StructureReader.ConstantDescriptor in project openj9 by eclipse.
the class BytecodeGenerator method getClassBytes.
public static byte[] getClassBytes(StructureDescriptor structure, String fullClassName) throws ClassNotFoundException {
// The className we are trying to load is FooOffsets.
// The structure name stored in the reader is Foo
ClassWriter cw = new ClassWriter(0);
MethodVisitor mv;
FieldVisitor fv;
int bitFieldBitCount = 0;
// Class definition
cw.visit(V1_5, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, fullClassName, null, "java/lang/Object", null);
// Constants
// SIZEOF
fv = cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, "SIZEOF", "J", null, Long.valueOf(structure.getSizeOf()));
// Declared Constants
for (ConstantDescriptor constantDescriptor : structure.getConstants()) {
fv = cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, constantDescriptor.getName(), "J", null, Long.valueOf(constantDescriptor.getValue()));
fv.visitEnd();
}
// Offset Fields
for (FieldDescriptor field : structure.getFields()) {
String fieldName = field.getName();
String type = field.getType();
// make sure match a bit-field, not a C++ namespace
int colonIndex = type.replace("::", "__").indexOf(':');
if (colonIndex != -1) {
// Bitfield
String getter = fieldName;
int bitSize = Integer.parseInt(type.substring(colonIndex + 1).trim());
if (bitSize > (StructureReader.BIT_FIELD_CELL_SIZE - (bitFieldBitCount % StructureReader.BIT_FIELD_CELL_SIZE))) {
throw new InternalError(String.format("Bitfield %s->%s must not span cells", structure.getName(), type));
}
// s field
String name = String.format("_%s_s_", getter);
fv = cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, name, "I", null, Integer.valueOf(bitFieldBitCount));
fv.visitEnd();
// b field
name = String.format("_%s_b_", getter);
fv = cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, name, "I", null, new Integer(bitSize));
fv.visitEnd();
bitFieldBitCount += bitSize;
} else {
// Regular field
String name = String.format("_%sOffset_", fieldName);
// Offset fields
fv = cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, name, "I", null, Integer.valueOf(field.getOffset()));
fv.visitEnd();
}
}
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
mv.visitInsn(RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
cw.visitEnd();
return cw.toByteArray();
}
use of com.ibm.j9ddr.StructureReader.ConstantDescriptor in project openj9 by eclipse.
the class StructureStubGenerator method writeConstantInitializer.
private static void writeConstantInitializer(PrintWriter writer, StructureDescriptor structure) {
writer.format("\t\t%s = 0;%n", "SIZEOF");
Collections.sort(structure.getConstants());
for (ConstantDescriptor constant : structure.getConstants()) {
writer.format("\t\t%s = 0;%n", constant.getName());
}
}
use of com.ibm.j9ddr.StructureReader.ConstantDescriptor in project openj9 by eclipse.
the class EnumFormatter method format.
@Override
public FormatWalkResult format(String name, String type, String declaredType, int typeCode, long address, PrintStream out, Context context, IStructureFormatter structureFormatter) throws CorruptDataException {
if (typeCode != TYPE_ENUM) {
return FormatWalkResult.KEEP_WALKING;
}
// Get a handle on the descriptor for the enum - to find out how big it is
type = type.replace("enum", "");
type = type.trim();
StructureDescriptor desc = StructureCommandUtil.getStructureDescriptor(type, context);
if (null == desc) {
out.print("<<Missing description of " + type + ">>");
return FormatWalkResult.STOP_WALKING;
}
long value;
switch(desc.getSizeOf()) {
case 1:
value = U8Pointer.cast(address).at(0).longValue();
break;
case 2:
value = U16Pointer.cast(address).at(0).longValue();
break;
case 4:
value = U32Pointer.cast(address).at(0).longValue();
break;
case 8:
value = U64Pointer.cast(address).at(0).longValue();
break;
default:
out.print("<<Unhandled enum size: " + desc.getSizeOf() + ">>");
return FormatWalkResult.STOP_WALKING;
}
out.print("0x" + Long.toHexString(value));
out.print(" (");
out.print(value);
out.print(")");
boolean mnemonicWritten = false;
for (ConstantDescriptor constant : desc.getConstants()) {
if (constant.getValue() == value) {
out.print(" //");
out.print(constant.getName());
mnemonicWritten = true;
break;
}
}
if (!mnemonicWritten) {
out.print(" <<Not matched to enum constant>>");
}
return FormatWalkResult.STOP_WALKING;
}
use of com.ibm.j9ddr.StructureReader.ConstantDescriptor in project openj9 by eclipse.
the class PointerGenerator method writeBuildFlags.
private void writeBuildFlags(PrintWriter writer, StructureDescriptor structure) {
writer.println("\t// Build Flags");
Collections.sort(structure.getConstants());
for (ConstantDescriptor constant : structure.getConstants()) {
writer.format("\tpublic static final boolean %s;%n", constant.getName());
}
}
use of com.ibm.j9ddr.StructureReader.ConstantDescriptor in project openj9 by eclipse.
the class PointerGenerator method writeBuildFlagsStaticInitializer.
private void writeBuildFlagsStaticInitializer(PrintWriter writer, StructureDescriptor structure) {
Collections.sort(structure.getConstants());
writer.println("\tstatic {");
writer.println("\t\tHashMap<String, Boolean> defaultValues = new HashMap<String, Boolean>();");
writer.println();
writer.println("\t\t// Edit default values here");
for (ConstantDescriptor constant : structure.getConstants()) {
writer.format("\t\tdefaultValues.put(\"%s\", Boolean.FALSE);%n", constant.getName());
}
writer.println();
writer.println("\t\ttry {");
writer.println("\t\t\tClassLoader loader = " + structure.getName() + ".class.getClassLoader();");
writer.println("\t\t\tif (!(loader instanceof com.ibm.j9ddr.J9DDRClassLoader)) {");
writer.println("\t\t\t\tthrow new IllegalArgumentException(\"Cannot determine the runtime loader\");");
writer.println("\t\t\t}");
writer.println("\t\t\tClass<?> runtimeClass = ((com.ibm.j9ddr.J9DDRClassLoader) loader).loadClassRelativeToStream(\"structure." + structure.getName() + "\", false);");
writer.println("\t\t\tField[] fields = runtimeClass.getFields();");
writer.println("\t\t\tfor (int i = 0; i < fields.length; i++) {");
writer.println("\t\t\t\tField field = fields[i];");
writer.println("\t\t\t\t// Overwrite default value with real value if it exists.");
writer.println("\t\t\t\tdefaultValues.put(field.getName(), field.getLong(runtimeClass) != 0);");
writer.println("\t\t\t}");
writer.println("\t\t} catch (ClassNotFoundException e) {");
writer.println("\t\t\tthrow new IllegalArgumentException(String.format(\"Can not initialize flags from core file.%n%s\", e.getMessage()));");
writer.println("\t\t} catch (IllegalAccessException e) {");
writer.println("\t\t\tthrow new IllegalArgumentException(String.format(\"Can not initialize flags from core file.%n%s\", e.getMessage()));");
writer.println("\t\t}");
writer.println();
for (ConstantDescriptor constant : structure.getConstants()) {
writer.format("\t\t%s = defaultValues.get(\"%s\");%n", constant.getName(), constant.getName());
}
writer.println("\t}");
}
Aggregations