Skip to content

Commit

Permalink
Refactor V3Kind into an enum
Browse files Browse the repository at this point in the history
  • Loading branch information
titzer committed Feb 13, 2024
1 parent 2e60ea4 commit 77b48b6
Show file tree
Hide file tree
Showing 50 changed files with 446 additions and 417 deletions.
2 changes: 1 addition & 1 deletion aeneas/src/core/Eval.v3
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ def evalOp(op: Operator, args: Arguments) -> Result {
if (prog.initState[index] == InitState.Uninit) {
var ctype = method.receiver;
var receiver = Values.BOTTOM;
if (ctype.typeCon.kind == V3Kind.COMPONENT) {
if (ctype.typeCon.kind == Kind.COMPONENT) {
receiver = prog.getComponentRecord(V3.componentDecl(ctype));
}
prog.initState[index] = InitState.Running;
Expand Down
2 changes: 1 addition & 1 deletion aeneas/src/core/Member.v3
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// See LICENSE for details of Apache 2.0 license.

class Member_TypeCon extends TypeCon {
new(name: string, kind: byte, arity: int, typeCache: TypeCache)
new(name: string, kind: Kind, arity: int, typeCache: TypeCache)
super(name, kind, arity, typeCache) { }
def lookupMember(t: Type, name: string) -> LookupResult { return LookupResult.None; }
}
32 changes: 16 additions & 16 deletions aeneas/src/core/Operator.v3
Original file line number Diff line number Diff line change
Expand Up @@ -342,18 +342,18 @@ component V3Op {
def newEqual(t: Type) -> Operator {
var opcode: Opcode = Opcode.OverloadedEq;
match (t.typeCon.kind) {
V3Kind.BOOL => return opBoolEq;
V3Kind.ENUM,
V3Kind.ENUM_SET,
V3Kind.INT => opcode = Opcode.IntEq;
V3Kind.FLOAT => opcode = Opcode.FloatEq(V3.isDouble(t));

V3Kind.POINTER,
V3Kind.ARRAY,
V3Kind.CLASS,
V3Kind.FUNCREF,
V3Kind.RANGE_START => opcode = Opcode.RefEq;
V3Kind.VARIANT => opcode = Opcode.VariantEq;
BOOL => return opBoolEq;
ENUM,
ENUM_SET,
INT => opcode = Opcode.IntEq;
FLOAT => opcode = Opcode.FloatEq(V3.isDouble(t));
POINTER,
ARRAY,
CLASS,
FUNCREF,
RANGE_START => opcode = Opcode.RefEq;
VARIANT => opcode = Opcode.VariantEq;
_ => ;
}
return newOp0(opcode, [t], [t, t], type_z);
}
Expand Down Expand Up @@ -409,7 +409,7 @@ component V3Op {
}
def newCallFunction(ftype: Type) -> Operator {
ftype = Function.funcRefType(Function.prependParamType(AnyRef.TYPE, ftype));
if (ftype.typeCon.kind != V3Kind.FUNCREF) return V3.fail("only function types allowed");
if (ftype.typeCon.kind != Kind.FUNCREF) return V3.fail("only function types allowed");
var paramTypes = Arrays.prepend(ftype, Function.getParamTypeArray(ftype));
return newOp0(Opcode.CallFunction, [ftype], paramTypes, Function.getReturnType(ftype));
}
Expand Down Expand Up @@ -558,7 +558,7 @@ component V3Op {
}
//----------------------------------------------------------------------------
def bestCallVirtual(spec: IrSpec) -> Operator {
if (spec.receiver.typeCon.kind == V3Kind.CLASS) {
if (spec.receiver.typeCon.kind == Kind.CLASS) {
if (!spec.member.facts.M_OVERRIDDEN) return newCallClassMethod(spec);
return newCallClassVirtual(spec);
} else {
Expand All @@ -567,7 +567,7 @@ component V3Op {
}
}
def bestGetVirtual(spec: IrSpec) -> Operator {
if (spec.receiver.typeCon.kind == V3Kind.CLASS) {
if (spec.receiver.typeCon.kind == Kind.CLASS) {
if (!spec.member.facts.M_OVERRIDDEN) return newClassGetMethod(spec);
else return V3Op.newClassGetVirtual(spec);
} else {
Expand All @@ -576,7 +576,7 @@ component V3Op {
}
}
def bestGetSelector(spec: IrSpec) -> Operator {
return if(spec.receiver.typeCon.kind == V3Kind.CLASS, newClassGetSelector(spec), newVariantGetSelector(spec));
return if(spec.receiver.typeCon.kind == Kind.CLASS, newClassGetSelector(spec), newVariantGetSelector(spec));
}

// XXX: migrate to Compiler.nullCheckFacts
Expand Down
40 changes: 23 additions & 17 deletions aeneas/src/debug/Dwarf.v3
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Dwarf {
entry.low_pc = addr.absolute;
entry.high_pc = u32.!(addr.size);
entry.rettype = m.ssa.returnType;
var hasRet = entry.rettype.typeCon.kind != V3Kind.VOID;
var hasRet = entry.rettype.typeCon.kind != Kind.VOID;
if (hasRet) info.putType(entry.rettype);
var hasFile = if(m.source.body != null, true);
if (hasFile) {
Expand Down Expand Up @@ -175,7 +175,7 @@ class Dwarf {
if (tn != null) return tn;
// not in the hashmap, build appropriately
match (t.typeCon.kind) {
V3Kind.TUPLE => {
TUPLE => {
// flatten tuples
var vecT = Vector<Type>.new();
var vecO = Vector<int>.new();
Expand Down Expand Up @@ -299,33 +299,39 @@ class DwarfInfoSection(abbrev: DwarfAbbrevSection) {
def emitType(t: Type, v: int) {
typeRef[t] = w.pos - info_start;
var byteSize: int, encoding: int;
match(t.typeCon.kind) {
V3Kind.VOID => return;
V3Kind.BOOL => {
match (t.typeCon.kind) {
VOID,
COMPONENT => return;
BOOL => {
emitBaseType(t, 1, DW.DW_ATE_boolean);
}
V3Kind.FLOAT => {
FLOAT => {
var x = FloatType.!(t);
byteSize = x.byteSize();
encoding = DW.DW_ATE_float;
emitBaseType(t, byteSize, encoding);
}
V3Kind.INT => {
INT => {
var x = IntType.!(t);
byteSize = x.byteSize();
encoding = if (V3.isSigned(x), DW.DW_ATE_signed, DW.DW_ATE_unsigned);
emitBaseType(t, byteSize, encoding);
}
V3Kind.VARIANT,
V3Kind.CLASS => emitClassType(ClassType.!(t));
V3Kind.ENUM => emitEnumType(EnumType.!(t));
V3Kind.ARRAY => emitArrayType(ArrayType.!(t));
V3Kind.ENUM_SET,
V3Kind.ANYFUNC,
V3Kind.POINTER,
V3Kind.COMPONENT,
V3Kind.FUNCREF,
V3Kind.TUPLE => emitUnspecifiedType(t);
VARIANT,
CLASS => emitClassType(ClassType.!(t));
ENUM => emitEnumType(EnumType.!(t));
ARRAY => emitArrayType(ArrayType.!(t));
ENUM_SET,
ANYFUNC,
POINTER,
FUNCREF,
CLOSURE,
RANGE,
RANGE_START,
REF,
TYPE_VAR,
TYPE_PARAM,
TUPLE => emitUnspecifiedType(t);
}
}
def emitBaseType(t: Type, byteSize: int, encoding: int) {
Expand Down
5 changes: 3 additions & 2 deletions aeneas/src/ic/Ic.v3
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ class IcAssembler(code: Vector<IcInstr>) {
var fkind = ft.typeCon.kind, tkind = tt.typeCon.kind;
for (t in [fkind, tkind]) {
match (t) {
V3Kind.BOOL, V3Kind.CLOSURE, V3Kind.ARRAY, V3Kind.CLASS, V3Kind.VARIANT, V3Kind.ENUM, V3Kind.ENUM_SET => return true;
V3Kind.INT, V3Kind.FLOAT => return false;
BOOL, CLOSURE, ARRAY, CLASS, VARIANT, ENUM, ENUM_SET => return true;
INT, FLOAT => return false;
_ => ;
}
}
return false;
Expand Down
25 changes: 13 additions & 12 deletions aeneas/src/ir/Normalization.v3
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,14 @@ class ReachabilityNormalizer(config: NormalizerConfig, ra: ReachabilityAnalyzer)
if (tn != null) return tn;
// not in the hashmap, build appropriately
match (t.typeCon.kind) {
V3Kind.VOID => {
VOID => {
tn = TypeNorm.new(t, Void.TYPE, TypeUtil.NO_TYPES);
}
V3Kind.COMPONENT => {
COMPONENT => {
tn = TypeNorm.new(t, Void.TYPE, TypeUtil.NO_TYPES);
}
// TODO V3Kind.CLASS, V3Kind.INT, V3Kind.FLOAT => ; // leave as-is
V3Kind.ARRAY => {
// TODO CLASS, INT, FLOAT => ; // leave as-is
ARRAY => {
var at: ArrayNorm;
var enorm = norm(V3Array.elementType(t));
if (enorm.size == 0) {
Expand All @@ -172,7 +172,7 @@ class ReachabilityNormalizer(config: NormalizerConfig, ra: ReachabilityAnalyzer)
tn = at = ArrayNorm.new(t, Tuple.newType(Lists.fromArray(et)), et);
}
}
V3Kind.RANGE => {
RANGE => {
var et = V3Array.elementType(t);
var an = ArrayNorm.!(norm(V3Array.newType(et)));
var vec = Vector<Type>.new().grow(an.size + 2);
Expand All @@ -183,15 +183,15 @@ class ReachabilityNormalizer(config: NormalizerConfig, ra: ReachabilityAnalyzer)
var newType = Tuple.fromTypeArray(sub);
tn = RangeNorm.new(t, newType, sub, an);
}
V3Kind.CLOSURE => {
CLOSURE => {
// translate closure into (funcref, object) pair
var pt = limit(Function.getParamType(t), config.MaxParams);
var rt = limit(Function.getReturnType(t), config.MaxReturnValues);
var ft = Function.FUNCREF.create(Lists.cons2(pt.0, rt.0));
var ta = [ft, AnyRef.TYPE];
tn = FuncNorm.new(t, Tuple.newType(Lists.cons2(ft, AnyRef.TYPE)), pt.1, rt.1, ta);
}
V3Kind.TUPLE => {
TUPLE => {
// flatten tuples
var vecT = Vector<Type>.new();
var vecO = Vector<int>.new();
Expand All @@ -205,16 +205,16 @@ class ReachabilityNormalizer(config: NormalizerConfig, ra: ReachabilityAnalyzer)
var ta = vecT.extract();
tn = TupleNorm.new(t, Tuple.newType(Lists.fromArray(ta)), ta, vecN.extract(), vecO.extract());
}
V3Kind.VARIANT => {
VARIANT => {
// try flattening variants and data types
var rc = ra.getClass(t);
if (rc != null) tn = vs.normVariant(t, rc);
else tn = TypeNorm.new(t, t, null);
}
V3Kind.ENUM => {
ENUM => {
tn = TypeNorm.new(t, V3.getVariantTagType(t), null);
}
V3Kind.REF => {
REF => {
var sub = [
V3.arrayByteType,
if(config.NormalizeRange, V3Range.START_TYPE, Int.TYPE)
Expand Down Expand Up @@ -629,8 +629,9 @@ class OverflowFieldAllocator(decl: VstComponent, receiver: Type, anyref: bool) {
def next(t: Type) -> IrSpec {
if (anyref) {
match (t.typeCon.kind) {
V3Kind.CLASS, V3Kind.ARRAY, V3Kind.CLOSURE => t = AnyRef.TYPE;
V3Kind.FUNCREF => t = AnyFunction.TYPE;
CLASS, ARRAY, CLOSURE => t = AnyRef.TYPE;
FUNCREF => t = AnyFunction.TYPE;
_ => ;
}
}
var entry = map[t];
Expand Down
62 changes: 32 additions & 30 deletions aeneas/src/ir/PartialSpecialization.v3
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,20 @@ class Specializer(ra: ReachabilityAnalyzer, rn: ReachabilityNormalizer) {
VariantEq => {
var kind = t0.oldType.typeCon.kind;
match (kind) {
V3Kind.ENUM => { sig.put(V3Kind.INT); sig.put(rankOf(V3.getVariantTagType(t0.oldType))); }
V3Kind.ENUM_SET => { sig.put(V3Kind.INT); sig.put(rankOf(V3.getVariantTagType(t0.oldType))); }
V3Kind.INT => { sig.put(V3Kind.INT); sig.put(rankOf(IntType.!(t0.oldType))); }
V3Kind.FLOAT => { sig.put(V3Kind.FLOAT); sig.put(if(V3.isDouble(t0.oldType), 1, 0)); }
V3Kind.POINTER,
V3Kind.ARRAY,
V3Kind.CLASS,
V3Kind.FUNCREF => sig.put(V3Kind.CLASS);
V3Kind.VARIANT,
V3Kind.TUPLE => {
sig.put(kind);
ENUM => { sig.put(Kind.INT.tag); sig.put(rankOf(V3.getVariantTagType(t0.oldType))); }
ENUM_SET => { sig.put(Kind.INT.tag); sig.put(rankOf(V3.getVariantTagType(t0.oldType))); }
INT => { sig.put(Kind.INT.tag); sig.put(rankOf(IntType.!(t0.oldType))); }
FLOAT => { sig.put(Kind.FLOAT.tag); sig.put(if(V3.isDouble(t0.oldType), 1, 0)); }
POINTER,
ARRAY,
CLASS,
FUNCREF => sig.put(Kind.CLASS.tag);
VARIANT,
TUPLE => {
sig.put(kind.tag);
sig.addOldType(this, t0);
}
_ => sig.put(kind);
_ => sig.put(kind.tag);
}
}
TypeCast => {
Expand Down Expand Up @@ -486,37 +486,39 @@ class SpecSignature {
// add a memory reference to the signature
def putMemory(t: Type) {
match (t.typeCon.kind) {
V3Kind.BOOL => put('\x01');
V3Kind.INT => put(byte.!(IntType.!(t).byteSize()));
V3Kind.ARRAY => put('\x09');
V3Kind.CLASS => put('\x09');
V3Kind.CLOSURE => put('\x0a');
V3Kind.FUNCREF => put('\x0b'); // XXX: could be equal to the word size of target.
V3Kind.COMPONENT,
V3Kind.VOID => put('\x00');
V3Kind.FLOAT => put(if(V3.isDouble(t), '\x0c', '\x0d'));
V3Kind.TUPLE => {
BOOL => put('\x01');
INT => put(byte.!(IntType.!(t).byteSize()));
ARRAY => put('\x09');
CLASS => put('\x09');
CLOSURE => put('\x0a');
FUNCREF => put('\x0b'); // XXX: could be equal to the word size of target.
COMPONENT,
VOID => put('\x00');
FLOAT => put(if(V3.isDouble(t), '\x0c', '\x0d'));
TUPLE => {
put(TUPLE_MARK);
Lists.apply(Tuple.toTypeList(t), putMemory);
put(TUPLE_MARK);
}
_ => ; // TODO
}
}
// add a parameter type to the signature
def addParam(t: Type) {
match (t.typeCon.kind) {
V3Kind.BOOL => put('\x04');
V3Kind.INT => put(if(IntType.!(t).byteSize() > 4, '\x06', '\x04')); // XXX: machine-dependent
V3Kind.ARRAY => put('\x05');
V3Kind.CLASS => put('\x05');
V3Kind.CLOSURE => put('\x08');
V3Kind.FUNCREF => put('\x04');
V3Kind.FLOAT => put(if(V3.isDouble(t), '\x0c', '\x0d'));
V3Kind.TUPLE => {
BOOL => put('\x04');
INT => put(if(IntType.!(t).byteSize() > 4, '\x06', '\x04')); // XXX: machine-dependent
ARRAY => put('\x05');
CLASS => put('\x05');
CLOSURE => put('\x08');
FUNCREF => put('\x04');
FLOAT => put(if(V3.isDouble(t), '\x0c', '\x0d'));
TUPLE => {
put(TUPLE_MARK);
Lists.apply(Tuple.toTypeList(t), addParam);
put(TUPLE_MARK);
}
_ => ; // TODO
}
}
// finish the current array and associate it with the given item
Expand Down
Loading

0 comments on commit 77b48b6

Please sign in to comment.