Skip to content

Commit

Permalink
Factor out PrimType from Int,Float,Enum,EnumSet,Bool,Void types (#302)
Browse files Browse the repository at this point in the history
  • Loading branch information
titzer authored Nov 20, 2024
1 parent d47da35 commit dbe928b
Show file tree
Hide file tree
Showing 27 changed files with 89 additions and 136 deletions.
12 changes: 6 additions & 6 deletions aeneas/src/core/Eval.v3
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ def doRefLayoutGetField(args: Arguments, fieldType: Type, ref: ByteArrayOffset,
return Bool.box((v & 1) != 0);
}
x: IntType => {
var v = ref.read(order, offset, x.size);
var v = ref.read(order, offset, x.packedByteSize);
match (x.rank) {
SUBI32 => return Int.box(int.view(v) << x.ishift >> x.ishift);
SUBU32 => return Int.box(int.view(v) << x.ishift >>> x.ishift);
Expand All @@ -948,12 +948,12 @@ def doRefLayoutGetField(args: Arguments, fieldType: Type, ref: ByteArrayOffset,
}
}
x: EnumType => {
var v = ref.read(order, offset, x.packedByteSize());
var v = ref.read(order, offset, x.packedByteSize);
if (v >= x.enumDecl.cases.length) v = 0; // out-of-bounds tag => 0
return Int.box(int.view(v)); // TODO: long enum vals?
}
x: FloatType => {
var v = ref.read(order, offset, x.byteSize());
var v = ref.read(order, offset, x.byteSize);
if (x.is64) return Float64Val.new(v);
else return Float32Val.new(u32.view(v));
}
Expand All @@ -968,14 +968,14 @@ def doRefLayoutSetField(args: Arguments, fieldType: Type, ref: ByteArrayOffset,
size = 1;
}
x: IntType => {
size = x.packedByteSize();
size = x.packedByteSize;
signed = x.signed;
}
x: EnumType => {
size = x.packedByteSize();
size = x.packedByteSize;
}
x: FloatType => {
size = x.byteSize();
size = x.byteSize;
}
_ => return args.throw("EvalException", Strings.format1("invalid RefLayoutField type %q", fieldType.render));
}
Expand Down
6 changes: 3 additions & 3 deletions aeneas/src/debug/Dwarf.v3
Original file line number Diff line number Diff line change
Expand Up @@ -307,13 +307,13 @@ class DwarfInfoSection(abbrev: DwarfAbbrevSection) {
}
FLOAT => {
var x = FloatType.!(t);
byteSize = x.byteSize();
byteSize = x.byteSize;
encoding = DW.DW_ATE_float;
emitBaseType(t, byteSize, encoding);
}
INT => {
var x = IntType.!(t);
byteSize = x.byteSize();
byteSize = x.byteSize;
encoding = if (V3.isSigned(x), DW.DW_ATE_signed, DW.DW_ATE_unsigned);
emitBaseType(t, byteSize, encoding);
}
Expand Down Expand Up @@ -369,7 +369,7 @@ class DwarfInfoSection(abbrev: DwarfAbbrevSection) {
t.render(buf);
buf.send(w.putr);
w.putb(0);
w.putb(t.byteSize());
w.putb(t.byteSize);
for (c in t.enumDecl.cases) {
w.put_uleb32(abbrev.getAbbrev(DwarfAbbrevTag.Enumurator));
buf.reset();
Expand Down
14 changes: 2 additions & 12 deletions aeneas/src/ir/Normalization.v3
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,17 @@ class NormalizerConfig {
if (maxr < MaxReturnValues) MaxReturnValues = maxr;
}
}
def getB32Or64(width: byte) -> Scalar.set {
return if(width <= 32, Scalar.B32 | Scalar.B64, Scalar.B64);
}
def defaultGetScalar(compiler: Compiler, prog: Program, t: Type) -> Scalar.set {
match (t) {
x: EnumType => return getB32Or64(x.enumDecl.tagType.width);
x: EnumSetType => return getB32Or64(x.repType.width);
x: IntType => return getB32Or64(x.width);
x: FloatType => return if(x.is64, Scalar.F64, Scalar.F32);
x: BoolType => return Scalar.B32 | Scalar.B64;
x: PrimType => return if(x.width <= 32, Scalar.B32 | Scalar.B64, Scalar.B64);
_ => return Scalar.Ref;
}
}
def defaultGetBitWidth(compiler: Compiler, prog: Program, t: Type) -> byte {
var target = compiler.target;
match (t) {
x: EnumType => return x.enumDecl.tagType.width;
x: EnumSetType => return x.repType.width;
x: IntType => return x.width;
x: FloatType => return x.total_width;
x: BoolType => return 1;
x: PrimType => return x.width;
_ => return 64;
}
}
Expand Down
4 changes: 2 additions & 2 deletions aeneas/src/ir/PartialSpecialization.v3
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ class SpecSignature {
def putMemory(t: Type) {
match (t.typeCon.kind) {
BOOL => put('\x01');
INT => put(byte.!(IntType.!(t).byteSize()));
INT => put(byte.!(IntType.!(t).byteSize));
ARRAY => put('\x09');
CLASS => put('\x09');
CLOSURE => put('\x0a');
Expand All @@ -508,7 +508,7 @@ class SpecSignature {
def addParam(t: Type) {
match (t.typeCon.kind) {
BOOL => put('\x04');
INT => put(if(IntType.!(t).byteSize() > 4, '\x06', '\x04')); // XXX: machine-dependent
INT => put(if(IntType.!(t).byteSize > 4, '\x06', '\x04')); // XXX: machine-dependent
ARRAY => put('\x05');
CLASS => put('\x05');
CLOSURE => put('\x08');
Expand Down
4 changes: 2 additions & 2 deletions aeneas/src/ir/SsaNormalizer.v3
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ class SsaRaNormalizer extends SsaRebuilder {
x: FloatType => result = curBlock.opByteArrayGetField(x, rangeStartType, offset, order, facts, array, start);
x: EnumType => {
var it = IntType.!(x.enumDecl.tagType);
var wt = Int.getType(false, it.byteSize() * 8);
var wt = Int.getType(false, it.byteSize * 8);
result = curBlock.opByteArrayGetField(wt, rangeStartType, offset, order, facts, array, start);
var caseCount = newGraph.intConst(x.enumDecl.cases.length);
var inBound = curBlock.opIntULt(norm.config.ArrayLengthType, it, result, caseCount);
Expand All @@ -428,7 +428,7 @@ class SsaRaNormalizer extends SsaRebuilder {
x: FloatType => result = curBlock.opByteArraySetField(fn.oldType, rangeStartType, offset, order, facts, array, start, val);
x: EnumType => {
var it = IntType.!(x.enumDecl.tagType);
var wt = Int.getType(false, it.byteSize() * 8);
var wt = Int.getType(false, it.byteSize * 8);
result = curBlock.opByteArraySetField(wt, rangeStartType, offset, order, facts, array, start, val);
}
}
Expand Down
2 changes: 1 addition & 1 deletion aeneas/src/jvm/JvmHeap.v3
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class JvmHeap(jprog: JvmProgram) {
}
}
private def emitIntValue(code: JvmCodeBuilder, tt: IntType, val: Val) {
if (tt.size <= 4) code.iconst(V3.unboxI32(val));
if (tt.byteSize <= 4) code.iconst(V3.unboxI32(val));
else code.lconst(Long.unboxSU(val, tt.signed));
}
private def emitFloatValue(code: JvmCodeBuilder, ft: FloatType, val: Val) {
Expand Down
2 changes: 1 addition & 1 deletion aeneas/src/jvm/JvmRep.v3
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class JvmTypeReps(jprog: JvmProgram) {
return JvmTypes.INT;
}
def getJvmIntType(tt: IntType) -> JvmType {
match(tt.size) {
match(tt.packedByteSize) {
1 => return if(tt == Byte.TYPE, JvmTypes.UBYTE, JvmTypes.BYTE);
2 => return if (tt.signed, JvmTypes.SHORT, JvmTypes.CHAR);
3, 4 => return JvmTypes.INT;
Expand Down
9 changes: 1 addition & 8 deletions aeneas/src/jvm/JvmTarget.v3
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,8 @@ class JvmTarget extends Target {
private def getScalar(compiler: Compiler, prog: Program, t: Type) -> Scalar.set {
var none: Scalar.set;
match (t) {
x: BoolType => return Scalar.B32 | Scalar.B64;
x: IntType => return if(x.width <= 32, Scalar.B32 | Scalar.B64, Scalar.B64);
x: EnumType => {
return Scalar.B32 | Scalar.B64;
}
x: EnumSetType => {
return if(x.repType.width <= 32, Scalar.B32 | Scalar.B64, Scalar.B64);
}
x: FloatType => return if(x.is64, Scalar.F64, Scalar.F32);
x: PrimType => return if(x.width <= 32, Scalar.B32 | Scalar.B64, Scalar.B64);
_ => return Scalar.Ref;
}
}
Expand Down
4 changes: 2 additions & 2 deletions aeneas/src/jvm/SsaJvmGen.v3
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ class SsaJvmGen(jprog: JvmProgram, context: SsaContext, jsig: JvmSig, code: JvmC
match (fieldType) {
x: IntType => {
// XXX: do 1 and 2 byte cases inline
match (x.packedByteSize()) {
match (x.packedByteSize) {
1 => jprog.invokesystem(code, "readBytes1", JvmTypes.SIG_BYTE_ARRAY_INT_INT_BYTE);
2 => jprog.invokesystem(code, appendByteOrder("readBytes2", order), JvmTypes.SIG_BYTE_ARRAY_INT_INT_SHORT);
3 => jprog.invokesystem(code, appendByteOrder("readBytes3", order), JvmTypes.SIG_BYTE_ARRAY_INT_INT_INT);
Expand All @@ -600,7 +600,7 @@ class SsaJvmGen(jprog: JvmProgram, context: SsaContext, jsig: JvmSig, code: JvmC
match (fieldType) {
x: IntType => {
// XXX: do 1 and 2 byte cases inline
match (x.packedByteSize()) {
match (x.packedByteSize) {
1 => jprog.invokesystem(code, "setBytes1", JvmTypes.SIG_BYTE_ARRAY_INT_BYTE_INT_VOID);
2 => jprog.invokesystem(code, appendByteOrder("setBytes2", order), JvmTypes.SIG_BYTE_ARRAY_INT_SHORT_INT_VOID);
3 => jprog.invokesystem(code, appendByteOrder("setBytes3", order), JvmTypes.SIG_BYTE_ARRAY_INT_INT_INT_VOID);
Expand Down
6 changes: 3 additions & 3 deletions aeneas/src/mach/MachLowering.v3
Original file line number Diff line number Diff line change
Expand Up @@ -2104,7 +2104,7 @@ def decomposeByteArrayAccess(mach: MachProgram, config: MachLoweringConfig, fiel
// generate multiple power-of-2 accesses
var vec = Vector<AccessDatum>.new();
var wordType = config.getMinimumArithSize(et.width);
var etWidth = et.packedByteSize() * 8, bitPos = 0;
var etWidth = et.packedByteSize * 8, bitPos = 0;
for (curWidth = mach.intNorm.width; curWidth >= 8; curWidth >>= 1) {
if ((etWidth & curWidth) != 0) {
var curType = Int.getType(false, curWidth);
Expand All @@ -2125,13 +2125,13 @@ def decomposeByteArrayAccess(mach: MachProgram, config: MachLoweringConfig, fiel
for (i = sub.length - 1; i >= 0; i--) {
var compound = compoundAccess(mach, sub[i], fieldOrder, offset);
accesses[i] = compound;
offset += compound.wordType.packedByteSize();
offset += compound.wordType.packedByteSize;
}
} else {
for (i < sub.length) {
var compound = compoundAccess(mach, sub[i], fieldOrder, offset);
accesses[i] = compound;
offset += compound.wordType.packedByteSize();
offset += compound.wordType.packedByteSize;
}
}
return (tn, accesses);
Expand Down
28 changes: 7 additions & 21 deletions aeneas/src/mach/MachProgram.v3
Original file line number Diff line number Diff line change
Expand Up @@ -295,19 +295,12 @@ class MachProgram extends TargetProgram {
return unexpectedType(t, null);
}
def sizeOf(t: Type) -> int {
if (PrimType.?(t)) return PrimType.!(t).byteSize;
match(t.typeCon.kind) {
COMPONENT,
VOID => return 0;
INT => return IntType.!(t).byteSize();
BOOL => return 1;
COMPONENT => return 0;
CLASS,
ARRAY => return refSize;
FLOAT => return FloatType.!(t).total_width >>> 3;
ENUM => return V3.getVariantTagType(t).byteSize();
ENUM_SET => return V3.getEnumSetType(t).byteSize();
VARIANT => {
return if (prog.ir.isEnum(t), V3.getVariantTagType(t).byteSize(), refSize);
}
VARIANT => return if (prog.ir.isEnum(t), V3.getVariantTagType(t).byteSize, refSize);
ANYFUNC,
FUNCREF => return code.addressSize;
RANGE_START,
Expand All @@ -317,19 +310,12 @@ class MachProgram extends TargetProgram {
return unexpectedType(t, 0);
}
def packedSizeOf(t: Type) -> int {
if (PrimType.?(t)) return PrimType.!(t).packedByteSize;
match(t.typeCon.kind) {
COMPONENT,
VOID => return 0;
INT => return IntType.!(t).packedByteSize();
BOOL => return 1;
COMPONENT => return 0;
CLASS,
ARRAY => return refSize;
FLOAT => return FloatType.!(t).total_width >>> 3;
ENUM => return V3.getVariantTagType(t).packedByteSize();
ENUM_SET => return V3.getEnumSetType(t).packedByteSize();
VARIANT => {
return if (prog.ir.isEnum(t), V3.getVariantTagType(t).packedByteSize(), refSize);
}
VARIANT => return if (prog.ir.isEnum(t), V3.getVariantTagType(t).packedByteSize, refSize);
ANYFUNC,
FUNCREF => return code.addressSize;
RANGE_START,
Expand Down Expand Up @@ -453,7 +439,7 @@ class MachProgram extends TargetProgram {
}
}
def encodeInt(w: DataWriter, v: Val, tt: IntType) {
match(tt.size) {
match(tt.packedByteSize) {
1 => w.putb(Byte.unbox(v));
2 => w.put_b16(Int.unbox(v));
3, 4 => w.put_b32(Int.unbox(v));
Expand Down
2 changes: 1 addition & 1 deletion aeneas/src/main/Version.v3
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

// Updated by VCS scripts. DO NOT EDIT.
component Version {
def version: string = "III-7.1769";
def version: string = "III-7.1770";
var buildData: string;
}
15 changes: 3 additions & 12 deletions aeneas/src/os/Linux.v3
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,21 @@ class LinuxTarget extends Target {
private def getScalar(compiler: Compiler, prog: Program, t: Type) -> Scalar.set {
if (space.addressWidth == 32) {
match (t) {
x: BoolType => return Scalar.B32 | Scalar.B64;
x: IntType => return if(x.width <= 32, Scalar.B32 | Scalar.B64, Scalar.B64); // XXX: Scalar.R64, once packed refs
x: EnumSetType => {
return if(x.repType.width <= 32, Scalar.B32 | Scalar.B64, Scalar.B64); // XXX: Scalar.R64, once packed refs
}
x: FloatType => return if(x.is64, Scalar.F64 | Scalar.B64, Scalar.F32 | Scalar.B32);
x: PrimType => return if(x.width <= 32, Scalar.B32 | Scalar.B64, Scalar.B64); // XXX: Scalar.R64, once packed refs
_ => return Scalar.R32;
}
} else {
match (t) {
x: BoolType => return Scalar.B64;
x: EnumType,
x: EnumSetType,
x: IntType => return Scalar.B64; // XXX: Scalar.R64, once packed refs
x: FloatType => return Scalar.F64 | Scalar.B64;
x: PrimType => return Scalar.B64; // XXX: Scalar.R64, once packed refs
_ => return Scalar.R64;
}
}
}
private def getBitWidth(compiler: Compiler, prog: Program, t: Type) -> byte {
match (t) {
x: BoolType => return 1;
x: IntType => return x.width;
x: FloatType => return x.total_width;
x: PrimType => return x.width;
_ => return space.addressWidth;
}
}
Expand Down
2 changes: 1 addition & 1 deletion aeneas/src/ssa/SsaBuilder.v3
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class SsaBuilder extends SsaBlockState {
IntView => return opIntView(op.typeArgs[0], IntType.!(op.typeArgs[1]), args[0]);
FloatSign => {
var ft = FloatType.!(op.typeArgs[0]);
return opFloatBitField(op, ft, ft.total_width - byte.!(1), 0, args[0]);
return opFloatBitField(op, ft, ft.width - byte.!(1), 0, args[0]);
}
FloatExponent => {
var ft = FloatType.!(op.typeArgs[0]);
Expand Down
4 changes: 2 additions & 2 deletions aeneas/src/types/Bool.v3
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ component Bool {
}
}
// The (singleton) {bool} type.
class BoolType extends Type {
new(typeCon: TypeCon) super(typeCon.uid, typeCon, null) { }
class BoolType extends PrimType {
new(typeCon: TypeCon) super(typeCon.uid, typeCon, 1) { }
}
12 changes: 4 additions & 8 deletions aeneas/src/types/Float.v3
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,13 @@ class Float_TypeCon extends TypeCon {
}

}
class FloatType extends Type {
class FloatType extends PrimType {
def exp_width: byte;
def fraction_width: byte;
def total_width = byte.!(1 + exp_width + fraction_width);
def is64 = total_width == 64;
def is64 = (1 + exp_width + fraction_width) == 64;
private var cache: FloatOpCache;

new(exp_width, fraction_width, typeCon: Float_TypeCon) super(typeCon.uid, typeCon, null) { }
new(exp_width, fraction_width, typeCon: Float_TypeCon) super(typeCon.uid, typeCon, byte.!(1 + exp_width + fraction_width)) { }
def opcache() -> FloatOpCache {
if (cache == null) cache = FloatOpCache.new(this);
return cache;
Expand Down Expand Up @@ -153,14 +152,11 @@ class FloatType extends Type {
var shift = u6.view(64 - fraction_width);
var fraction = (v >> shift);
if (v != (fraction << shift)) return null; // requires rounding
var r = u64.view(sign) << u6.view(total_width - 1);
var r = u64.view(sign) << u6.view(width - 1);
r |= u64.view(exponent) << u6.view(fraction_width);
r |= fraction;
return if(is64, Float64Val.new(r), Float32Val.new(u32.view(r)));
}
def byteSize() -> int {
return total_width >> 3;
}
}
class FloatOpCache(ft: FloatType) {
def arr_t: Array<Type> = [ft];
Expand Down
Loading

0 comments on commit dbe928b

Please sign in to comment.