From cf7b8b87f78687b8e4a867d3b18bd7f072a955ee Mon Sep 17 00:00:00 2001 From: Kenneth Russel Date: Wed, 9 Nov 2005 00:32:37 +0000 Subject: Refactored computations of sizes of data types and offsets of fields in data structures in GlueGen to be performed lazily via SizeThunks. The concrete size of primitive data types is computed only by passing a MachineDescription into one of these thunks. Changed generated glue code for struct accessors to delegate their instantiation and field access to specialized 32- or 64-bit versions. This should allow one jar file to support both 32-bit and 64-bit CPUs; the native code is of course still specialized for the processor and data model. Changed default build to generate both 32-bit and 64-bit accessors for all generated data structures. Tested on Windows; more testing, including build testing, is needed on other platforms. git-svn-id: file:///usr/local/projects/SUN/JOGL/git-svn/svn-server-sync/jogl/trunk@426 232f8b59-042b-4e1e-8c03-345bb8c30851 --- src/classes/com/sun/gluegen/cgram/HeaderParser.g | 52 +++---- .../com/sun/gluegen/cgram/types/ArrayType.java | 4 +- .../com/sun/gluegen/cgram/types/CompoundType.java | 6 +- .../com/sun/gluegen/cgram/types/DoubleType.java | 2 +- .../com/sun/gluegen/cgram/types/EnumType.java | 12 +- src/classes/com/sun/gluegen/cgram/types/Field.java | 23 +-- .../com/sun/gluegen/cgram/types/FloatType.java | 2 +- .../com/sun/gluegen/cgram/types/FunctionType.java | 2 +- .../com/sun/gluegen/cgram/types/IntType.java | 4 +- .../com/sun/gluegen/cgram/types/PointerType.java | 6 +- .../com/sun/gluegen/cgram/types/PrimitiveType.java | 2 +- .../com/sun/gluegen/cgram/types/SizeThunk.java | 171 +++++++++++++++++++++ src/classes/com/sun/gluegen/cgram/types/Type.java | 18 ++- .../com/sun/gluegen/cgram/types/VoidType.java | 2 +- 14 files changed, 238 insertions(+), 68 deletions(-) create mode 100755 src/classes/com/sun/gluegen/cgram/types/SizeThunk.java (limited to 'src/classes/com/sun/gluegen/cgram') diff --git a/src/classes/com/sun/gluegen/cgram/HeaderParser.g b/src/classes/com/sun/gluegen/cgram/HeaderParser.g index 212e56091..78ba5c54a 100644 --- a/src/classes/com/sun/gluegen/cgram/HeaderParser.g +++ b/src/classes/com/sun/gluegen/cgram/HeaderParser.g @@ -56,17 +56,6 @@ options { /** Name assigned to a anonymous EnumType (e.g., "enum { ... }"). */ public static final String ANONYMOUS_ENUM_NAME = ""; - /** Set the machine description for this HeaderParser. Must be - done before parsing. */ - public void setMachineDescription(MachineDescription machDesc) { - this.machDesc = machDesc; - } - - /** Returns the MachineDescription this HeaderParser uses. */ - public MachineDescription machineDescription() { - return machDesc; - } - /** Set the dictionary mapping typedef names to types for this HeaderParser. Must be done before parsing. */ public void setTypedefDictionary(TypeDictionary dict) { @@ -130,7 +119,7 @@ options { int cvAttrs) { CompoundType t = (CompoundType) structDictionary.get(typeName); if (t == null) { - t = new CompoundType(null, -1, kind, cvAttrs); + t = new CompoundType(null, null, kind, cvAttrs); t.setStructName(typeName); structDictionary.put(typeName, t); } @@ -207,7 +196,6 @@ options { } } - private MachineDescription machDesc; private boolean doDeclaration; // Used to only process function typedefs private String declId; private List parameters; @@ -242,7 +230,7 @@ options { private void processDeclaration(Type returnType) { if (doDeclaration) { - FunctionSymbol sym = new FunctionSymbol(declId, new FunctionType(null, -1, returnType, 0)); + FunctionSymbol sym = new FunctionSymbol(declId, new FunctionType(null, null, returnType, 0)); if (parameters != null) { // handle funcs w/ empty parameter lists (e.g., "foo()") for (Iterator iter = parameters.iterator(); iter.hasNext(); ) { ParameterDeclaration pd = (ParameterDeclaration) iter.next(); @@ -272,13 +260,13 @@ options { // FIXME: this doesn't take into account struct alignment, which may be necessary // See also FIXMEs in ArrayType.java int len = parseIntConstExpr(t); - tb.setType(canonicalize(new ArrayType(tb.type(), len * tb.type().getSize(), len, 0))); + tb.setType(canonicalize(new ArrayType(tb.type(), SizeThunk.mul(SizeThunk.constant(len), tb.type().getSize()), len, 0))); return; } catch (RecognitionException e) { // Fall through } } - tb.setType(canonicalize(new PointerType(machDesc.pointerSizeInBytes(), + tb.setType(canonicalize(new PointerType(SizeThunk.POINTER, tb.type(), 0))); } @@ -301,7 +289,7 @@ options { } if (enumType == null) { - enumType = new EnumType(enumTypeName, machDesc.longSizeInBytes()); + enumType = new EnumType(enumTypeName, SizeThunk.LONG); } return enumType; @@ -348,7 +336,7 @@ declarator[TypeBox tb] returns [String s] { doDeclaration(); } else if ( funcPointerName != null ) { /* TypeBox becomes function pointer in this case */ - FunctionType ft = new FunctionType(null, -1, tb.type(), 0); + FunctionType ft = new FunctionType(null, null, tb.type(), 0); if (params == null) { // If the function pointer has no declared parameters, it's a // void function. I'm not sure if the parameter name is @@ -362,7 +350,7 @@ declarator[TypeBox tb] returns [String s] { ft.addArgument(pd.type(), pd.id()); } } - tb.setType(canonicalize(new PointerType(machDesc.pointerSizeInBytes(), + tb.setType(canonicalize(new PointerType(SizeThunk.POINTER, ft, 0))); s = funcPointerName; @@ -431,7 +419,7 @@ declSpecifiers returns [TypeBox tb] { { if (t == null && (x & (SIGNED | UNSIGNED)) != 0) { - t = new IntType("int", machDesc.intSizeInBytes(), ((x & UNSIGNED) != 0), attrs2CVAttrs(x)); + t = new IntType("int", SizeThunk.INT, ((x & UNSIGNED) != 0), attrs2CVAttrs(x)); } tb = new TypeBox(t, ((x & TYPEDEF) != 0)); } @@ -465,13 +453,13 @@ typeSpecifier[int attributes] returns [Type t] { boolean unsigned = ((attributes & UNSIGNED) != 0); } : "void" { t = new VoidType(cvAttrs); } - | "char" { t = new IntType("char" , machDesc.charSizeInBytes(), unsigned, cvAttrs); } - | "short" { t = new IntType("short", machDesc.shortSizeInBytes(), unsigned, cvAttrs); } - | "int" { t = new IntType("int" , machDesc.intSizeInBytes(), unsigned, cvAttrs); } - | "long" { t = new IntType("long" , machDesc.longSizeInBytes(), unsigned, cvAttrs); } - | "__int64" { t = new IntType("__int64", machDesc.int64SizeInBytes(), unsigned, cvAttrs); } - | "float" { t = new FloatType("float", machDesc.floatSizeInBytes(), cvAttrs); } - | "double" { t = new DoubleType("double", machDesc.doubleSizeInBytes(), cvAttrs); } + | "char" { t = new IntType("char" , SizeThunk.CHAR, unsigned, cvAttrs); } + | "short" { t = new IntType("short", SizeThunk.SHORT, unsigned, cvAttrs); } + | "int" { t = new IntType("int" , SizeThunk.INT, unsigned, cvAttrs); } + | "long" { t = new IntType("long" , SizeThunk.LONG, unsigned, cvAttrs); } + | "__int64" { t = new IntType("__int64", SizeThunk.INT64, unsigned, cvAttrs); } + | "float" { t = new FloatType("float", SizeThunk.FLOAT, cvAttrs); } + | "double" { t = new DoubleType("double", SizeThunk.DOUBLE, cvAttrs); } | t = structSpecifier[cvAttrs] ( attributeDecl )* | t = unionSpecifier [cvAttrs] ( attributeDecl )* | t = enumSpecifier [cvAttrs] @@ -507,7 +495,7 @@ structOrUnionBody[CompoundTypeKind kind, int cvAttrs] returns [CompoundType t] { t = (CompoundType) canonicalize(lookupInStructDictionary(id.getText(), kind, cvAttrs)); } ( structDeclarationList[t] )? RCURLY { t.setBodyParsed(); } - | LCURLY { t = new CompoundType(null, -1, kind, cvAttrs); } + | LCURLY { t = new CompoundType(null, null, kind, cvAttrs); } ( structDeclarationList[t] )? RCURLY { t.setBodyParsed(); } | id2:ID { t = (CompoundType) canonicalize(lookupInStructDictionary(id2.getText(), kind, cvAttrs)); } @@ -528,7 +516,7 @@ structDeclaration[CompoundType containingType] { CompoundType ct = t.asCompound(); if (ct.isUnion()) { // Anonymous union - containingType.addField(new Field(null, t, -1)); + containingType.addField(new Field(null, t, null)); } } } @@ -544,7 +532,7 @@ specifierQualifierList returns [Type t] { )+ { if (t == null && (x & (SIGNED | UNSIGNED)) != 0) { - t = new IntType("int", machDesc.intSizeInBytes(), ((x & UNSIGNED) != 0), attrs2CVAttrs(x)); + t = new IntType("int", SizeThunk.INT, ((x & UNSIGNED) != 0), attrs2CVAttrs(x)); } } ; @@ -563,7 +551,7 @@ structDeclarator[CompoundType containingType, Type t] returns [boolean addedAny] } : #( NStructDeclarator - ( s = declarator[tb] { containingType.addField(new Field(s, tb.type(), -1)); addedAny = true; } )? + ( s = declarator[tb] { containingType.addField(new Field(s, tb.type(), null)); addedAny = true; } )? ( COLON expr { /* FIXME: bit types not handled yet */ } ) ? ( attributeDecl )* ) @@ -659,7 +647,7 @@ pointerGroup[TypeBox tb] { int x = 0; int y = 0; } { //System.err.println("IN PTR GROUP: TB=" + tb); if (tb != null) { - tb.setType(canonicalize(new PointerType(machDesc.pointerSizeInBytes(), + tb.setType(canonicalize(new PointerType(SizeThunk.POINTER, tb.type(), attrs2CVAttrs(x)))); } diff --git a/src/classes/com/sun/gluegen/cgram/types/ArrayType.java b/src/classes/com/sun/gluegen/cgram/types/ArrayType.java index c61e6457f..0e23d356d 100644 --- a/src/classes/com/sun/gluegen/cgram/types/ArrayType.java +++ b/src/classes/com/sun/gluegen/cgram/types/ArrayType.java @@ -49,7 +49,7 @@ public class ArrayType extends Type { private int length; private String computedName; - public ArrayType(Type elementType, int sizeInBytes, int length, int cvAttributes) { + public ArrayType(Type elementType, SizeThunk sizeInBytes, int length, int cvAttributes) { super(elementType.getName() + " *", sizeInBytes, cvAttributes); this.elementType = elementType; this.length = length; @@ -100,7 +100,7 @@ public class ArrayType extends Type { } // FIXME: this doesn't take into account struct alignment, which may be necessary // See also FIXME below and in HeaderParser.g - super.setSize(getLength() * elementType.getSize()); + super.setSize(SizeThunk.mul(SizeThunk.constant(getLength()), elementType.getSize())); } public String toString() { diff --git a/src/classes/com/sun/gluegen/cgram/types/CompoundType.java b/src/classes/com/sun/gluegen/cgram/types/CompoundType.java index 6ad7580e0..28d7b7664 100644 --- a/src/classes/com/sun/gluegen/cgram/types/CompoundType.java +++ b/src/classes/com/sun/gluegen/cgram/types/CompoundType.java @@ -55,11 +55,11 @@ public class CompoundType extends Type { private boolean computedHashcode; private int hashcode; - public CompoundType(String name, int size, CompoundTypeKind kind, int cvAttributes) { + public CompoundType(String name, SizeThunk size, CompoundTypeKind kind, int cvAttributes) { this(name, size, kind, cvAttributes, null); } - private CompoundType(String name, int size, CompoundTypeKind kind, int cvAttributes, String structName) { + private CompoundType(String name, SizeThunk size, CompoundTypeKind kind, int cvAttributes, String structName) { super(name, size, cvAttributes); assert kind != null; this.kind = kind; @@ -107,7 +107,7 @@ public class CompoundType extends Type { this.structName = structName; } - public void setSize(int size) { + public void setSize(SizeThunk size) { super.setSize(size); } diff --git a/src/classes/com/sun/gluegen/cgram/types/DoubleType.java b/src/classes/com/sun/gluegen/cgram/types/DoubleType.java index e2be470fb..6d6e62c98 100644 --- a/src/classes/com/sun/gluegen/cgram/types/DoubleType.java +++ b/src/classes/com/sun/gluegen/cgram/types/DoubleType.java @@ -42,7 +42,7 @@ package com.sun.gluegen.cgram.types; /** Represents a double-word floating-point type (C type "double".) */ public class DoubleType extends PrimitiveType { - public DoubleType(String name, int size, int cvAttributes) { + public DoubleType(String name, SizeThunk size, int cvAttributes) { super(name, size, cvAttributes); } diff --git a/src/classes/com/sun/gluegen/cgram/types/EnumType.java b/src/classes/com/sun/gluegen/cgram/types/EnumType.java index 7f4b9e559..717d3892c 100644 --- a/src/classes/com/sun/gluegen/cgram/types/EnumType.java +++ b/src/classes/com/sun/gluegen/cgram/types/EnumType.java @@ -60,16 +60,14 @@ public class EnumType extends IntType { } private List/**/ enums; - private static final int longSizeBytes = 8; - public EnumType(String name) { - super(name, longSizeBytes, false, CVAttributes.CONST ); - this.underlyingType = new IntType(name, longSizeBytes, false, CVAttributes.CONST); + super(name, SizeThunk.LONG, false, CVAttributes.CONST ); + this.underlyingType = new IntType(name, SizeThunk.LONG, false, CVAttributes.CONST); } - public EnumType(String name, int enumSizeBytes) { - super(name, enumSizeBytes, false, CVAttributes.CONST ); - this.underlyingType = new IntType(name, enumSizeBytes, false, CVAttributes.CONST); + public EnumType(String name, SizeThunk enumSizeInBytes) { + super(name, enumSizeInBytes, false, CVAttributes.CONST ); + this.underlyingType = new IntType(name, enumSizeInBytes, false, CVAttributes.CONST); } protected EnumType(String name, IntType underlyingType, int cvAttributes) { diff --git a/src/classes/com/sun/gluegen/cgram/types/Field.java b/src/classes/com/sun/gluegen/cgram/types/Field.java index 3514c8f7e..e16ffbc5b 100644 --- a/src/classes/com/sun/gluegen/cgram/types/Field.java +++ b/src/classes/com/sun/gluegen/cgram/types/Field.java @@ -42,11 +42,11 @@ package com.sun.gluegen.cgram.types; /** Represents a field in a struct or union. */ public class Field { - private String name; - private Type type; - private long offset; + private String name; + private Type type; + private SizeThunk offset; - public Field(String name, Type type, long offset) { + public Field(String name, Type type, SizeThunk offset) { this.name = name; this.type = type; this.offset = offset; @@ -62,10 +62,11 @@ public class Field { } Field f = (Field) arg; + // Note: don't know how to examine offset any more since it's + // implemented in terms of code and they're not canonicalized return (((name != null && name.equals(f.name)) || (name == null && f.name == null)) && - type.equals(f.type) && - offset == f.offset); + type.equals(f.type)); } /** Name of this field in the containing data structure. */ @@ -74,11 +75,15 @@ public class Field { /** Type of this field. */ public Type getType() { return type; } - /** Offset, in bytes, of this field in the containing data structure. */ - public long getOffset() { return offset; } + /** SizeThunk computing offset, in bytes, of this field in the containing data structure. */ + public SizeThunk getOffset() { return offset; } + + /** Offset, in bytes, of this field in the containing data structure + given the specified MachineDescription. */ + public long getOffset(MachineDescription machDesc) { return offset.compute(machDesc); } /** Sets the offset of this field in the containing data structure. */ - public void setOffset(long offset) { this.offset = offset; } + public void setOffset(SizeThunk offset) { this.offset = offset; } public String toString() { if (!getType().isFunctionPointer()) { diff --git a/src/classes/com/sun/gluegen/cgram/types/FloatType.java b/src/classes/com/sun/gluegen/cgram/types/FloatType.java index 9f59b3564..7a6fa4d64 100644 --- a/src/classes/com/sun/gluegen/cgram/types/FloatType.java +++ b/src/classes/com/sun/gluegen/cgram/types/FloatType.java @@ -42,7 +42,7 @@ package com.sun.gluegen.cgram.types; /** Represents a single-word floating-point type (C type "float".) */ public class FloatType extends PrimitiveType { - public FloatType(String name, int size, int cvAttributes) { + public FloatType(String name, SizeThunk size, int cvAttributes) { super(name, size, cvAttributes); } diff --git a/src/classes/com/sun/gluegen/cgram/types/FunctionType.java b/src/classes/com/sun/gluegen/cgram/types/FunctionType.java index 9c94daea2..5e22e47d7 100644 --- a/src/classes/com/sun/gluegen/cgram/types/FunctionType.java +++ b/src/classes/com/sun/gluegen/cgram/types/FunctionType.java @@ -49,7 +49,7 @@ public class FunctionType extends Type { private ArrayList argumentTypes; private ArrayList argumentNames; - public FunctionType(String name, int size, Type returnType, int cvAttributes) { + public FunctionType(String name, SizeThunk size, Type returnType, int cvAttributes) { super(name, size, cvAttributes); this.returnType = returnType; } diff --git a/src/classes/com/sun/gluegen/cgram/types/IntType.java b/src/classes/com/sun/gluegen/cgram/types/IntType.java index 2816c561c..acd6a3d2b 100644 --- a/src/classes/com/sun/gluegen/cgram/types/IntType.java +++ b/src/classes/com/sun/gluegen/cgram/types/IntType.java @@ -43,11 +43,11 @@ public class IntType extends PrimitiveType { private boolean unsigned; private boolean typedefedUnsigned; - public IntType(String name, int size, boolean unsigned, int cvAttributes) { + public IntType(String name, SizeThunk size, boolean unsigned, int cvAttributes) { this(name, size, unsigned, cvAttributes, false); } - private IntType(String name, int size, boolean unsigned, int cvAttributes, boolean typedefedUnsigned) { + private IntType(String name, SizeThunk size, boolean unsigned, int cvAttributes, boolean typedefedUnsigned) { super(name, size, cvAttributes); this.unsigned = unsigned; this.typedefedUnsigned = typedefedUnsigned; diff --git a/src/classes/com/sun/gluegen/cgram/types/PointerType.java b/src/classes/com/sun/gluegen/cgram/types/PointerType.java index 142a2f12d..3fe69a164 100644 --- a/src/classes/com/sun/gluegen/cgram/types/PointerType.java +++ b/src/classes/com/sun/gluegen/cgram/types/PointerType.java @@ -44,13 +44,13 @@ public class PointerType extends Type { private String computedName; private boolean hasTypedefedName; - public PointerType(int size, Type targetType, int cvAttributes) { + public PointerType(SizeThunk size, Type targetType, int cvAttributes) { // can pass null for the final name parameter because the PointerType's getName() // completely replaces superclass behavior - this(size, targetType, cvAttributes, false, null); + this(size, targetType, cvAttributes, false, null); } - private PointerType(int size, Type targetType, int cvAttributes, boolean hasTypedefedName, String typedefedName) { + private PointerType(SizeThunk size, Type targetType, int cvAttributes, boolean hasTypedefedName, String typedefedName) { super(targetType.getName() + " *", size, cvAttributes); this.hasTypedefedName = false; this.targetType = targetType; diff --git a/src/classes/com/sun/gluegen/cgram/types/PrimitiveType.java b/src/classes/com/sun/gluegen/cgram/types/PrimitiveType.java index 8807e2615..80dab9aef 100644 --- a/src/classes/com/sun/gluegen/cgram/types/PrimitiveType.java +++ b/src/classes/com/sun/gluegen/cgram/types/PrimitiveType.java @@ -40,7 +40,7 @@ package com.sun.gluegen.cgram.types; public abstract class PrimitiveType extends Type { - protected PrimitiveType(String name, int size, int cvAttributes) { + protected PrimitiveType(String name, SizeThunk size, int cvAttributes) { super(name, size, cvAttributes); } diff --git a/src/classes/com/sun/gluegen/cgram/types/SizeThunk.java b/src/classes/com/sun/gluegen/cgram/types/SizeThunk.java new file mode 100755 index 000000000..74feef950 --- /dev/null +++ b/src/classes/com/sun/gluegen/cgram/types/SizeThunk.java @@ -0,0 +1,171 @@ +/* + * Copyright (c) 2005 Sun Microsystems, Inc. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * - Redistribution of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistribution in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * Neither the name of Sun Microsystems, Inc. or the names of + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * This software is provided "AS IS," without a warranty of any kind. ALL + * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, + * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN + * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR + * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR + * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR + * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR + * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE + * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, + * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF + * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + * + * You acknowledge that this software is not designed or intended for use + * in the design, construction, operation or maintenance of any nuclear + * facility. + * + * Sun gratefully acknowledges that this software was originally authored + * and developed by Kenneth Bradley Russell and Christopher John Kline. + */ + +package com.sun.gluegen.cgram.types; + +/** Provides a level of indirection between the definition of a type's + size and the absolute value of this size. Necessary when + generating glue code for two different CPU architectures (e.g., + 32-bit and 64-bit) from the same internal representation of the + various types involved. */ + +public abstract class SizeThunk { + // Private constructor because there are only a few of these + private SizeThunk() {} + + public abstract long compute(MachineDescription machDesc); + + public static final SizeThunk CHAR = new SizeThunk() { + public long compute(MachineDescription machDesc) { + return machDesc.charSizeInBytes(); + } + }; + + public static final SizeThunk SHORT = new SizeThunk() { + public long compute(MachineDescription machDesc) { + return machDesc.shortSizeInBytes(); + } + }; + + public static final SizeThunk INT = new SizeThunk() { + public long compute(MachineDescription machDesc) { + return machDesc.intSizeInBytes(); + } + }; + + public static final SizeThunk LONG = new SizeThunk() { + public long compute(MachineDescription machDesc) { + return machDesc.longSizeInBytes(); + } + }; + + public static final SizeThunk INT64 = new SizeThunk() { + public long compute(MachineDescription machDesc) { + return machDesc.int64SizeInBytes(); + } + }; + + public static final SizeThunk FLOAT = new SizeThunk() { + public long compute(MachineDescription machDesc) { + return machDesc.floatSizeInBytes(); + } + }; + + public static final SizeThunk DOUBLE = new SizeThunk() { + public long compute(MachineDescription machDesc) { + return machDesc.doubleSizeInBytes(); + } + }; + + public static final SizeThunk POINTER = new SizeThunk() { + public long compute(MachineDescription machDesc) { + return machDesc.pointerSizeInBytes(); + } + }; + + // Factory methods for performing certain limited kinds of + // arithmetic on these values + public static SizeThunk add(final SizeThunk thunk1, + final SizeThunk thunk2) { + return new SizeThunk() { + public long compute(MachineDescription machDesc) { + return thunk1.compute(machDesc) + thunk2.compute(machDesc); + } + }; + } + + public static SizeThunk sub(final SizeThunk thunk1, + final SizeThunk thunk2) { + return new SizeThunk() { + public long compute(MachineDescription machDesc) { + return thunk1.compute(machDesc) - thunk2.compute(machDesc); + } + }; + } + + public static SizeThunk mul(final SizeThunk thunk1, + final SizeThunk thunk2) { + return new SizeThunk() { + public long compute(MachineDescription machDesc) { + return thunk1.compute(machDesc) * thunk2.compute(machDesc); + } + }; + } + + public static SizeThunk mod(final SizeThunk thunk1, + final SizeThunk thunk2) { + return new SizeThunk() { + public long compute(MachineDescription machDesc) { + return thunk1.compute(machDesc) % thunk2.compute(machDesc); + } + }; + } + + public static SizeThunk roundUp(final SizeThunk thunk1, + final SizeThunk thunk2) { + return new SizeThunk() { + public long compute(MachineDescription machDesc) { + long sz1 = thunk1.compute(machDesc); + long sz2 = thunk2.compute(machDesc); + long rem = (sz1 % sz2); + if (rem == 0) { + return sz1; + } + return sz1 + (sz2 - rem); + } + }; + } + + public static SizeThunk max(final SizeThunk thunk1, + final SizeThunk thunk2) { + return new SizeThunk() { + public long compute(MachineDescription machDesc) { + return Math.max(thunk1.compute(machDesc), thunk2.compute(machDesc)); + } + }; + } + + public static SizeThunk constant(final int constant) { + return new SizeThunk() { + public long compute(MachineDescription machDesc) { + return constant; + } + }; + } +} diff --git a/src/classes/com/sun/gluegen/cgram/types/Type.java b/src/classes/com/sun/gluegen/cgram/types/Type.java index 2ac492d3f..0dcaabcef 100644 --- a/src/classes/com/sun/gluegen/cgram/types/Type.java +++ b/src/classes/com/sun/gluegen/cgram/types/Type.java @@ -48,12 +48,12 @@ import java.util.List; public abstract class Type { private String name; - private int size; + private SizeThunk size; private int cvAttributes; private int typedefedCVAttributes; private boolean hasTypedefName; - protected Type(String name, int size, int cvAttributes) { + protected Type(String name, SizeThunk size, int cvAttributes) { setName(name); this.size = size; this.cvAttributes = cvAttributes; @@ -88,10 +88,18 @@ public abstract class Type { hasTypedefName = true; } - /** Size of this type in bytes. */ - public int getSize() { return size; } + /** SizeThunk which computes size of this type in bytes. */ + public SizeThunk getSize() { return size; } + /** Size of this type in bytes according to the given MachineDescription. */ + public long getSize(MachineDescription machDesc) { + SizeThunk thunk = getSize(); + if (thunk == null) { + throw new RuntimeException("No size set for type \"" + getName() + "\""); + } + return thunk.compute(machDesc); + } /** Set the size of this type; only available for CompoundTypes. */ - void setSize(int size) { this.size = size; } + void setSize(SizeThunk size) { this.size = size; } /** Casts this to a BitType or returns null if not a BitType. */ public BitType asBit() { return null; } diff --git a/src/classes/com/sun/gluegen/cgram/types/VoidType.java b/src/classes/com/sun/gluegen/cgram/types/VoidType.java index db4c43f81..3a2f1b93f 100644 --- a/src/classes/com/sun/gluegen/cgram/types/VoidType.java +++ b/src/classes/com/sun/gluegen/cgram/types/VoidType.java @@ -45,7 +45,7 @@ public class VoidType extends Type { } private VoidType(String name, int cvAttributes) { - super(name, 0, cvAttributes); + super(name, null, cvAttributes); } public VoidType asVoid() { return this; } -- cgit v1.2.3