diff options
| author | Michael Williamson <mike@zwobble.org> | 2024-02-02 22:29:19 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2024-02-02 22:47:19 +0000 |
| commit | 23a031cb69312a50eed7d42dc072f42a8595376b (patch) | |
| tree | d784eb34b51213a9aa691180ccd096b8f96e9049 /src/main/java | |
| parent | 26092973fe989c6b6ff16d6713c32d76603458a1 (diff) | |
Use runtime type tag as struct tag
Diffstat (limited to 'src/main/java')
5 files changed, 50 insertions, 24 deletions
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/PatioToWasm.java b/src/main/java/org/shedlang/compiler/backends/wasm/PatioToWasm.java index 6c5f96da..63ab8d3a 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/PatioToWasm.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/PatioToWasm.java @@ -380,11 +380,11 @@ public class PatioToWasm { for (var patioSwitchCase : patioSwitch.cases()) { context = generateOperand(patioSwitch.value(), context); - context = context.addInstruction(wasmIntConst( - target.patioIntType(), + // TODO: should probably allow any int type in switches + context = context.addInstruction(new WasmI32Const( patioSwitchCase.value() )); - context = context.addInstruction(wasmIntEq(target.patioIntType())); + context = context.addInstruction(new WasmI32Eq()); context = context.enterIfInstruction(wasmBlockType); context = generateBlockBody(patioSwitchCase.body(), context); context = context.enterElse(); diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioContext.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioContext.java index 04644052..cab33a07 100644 --- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioContext.java +++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioContext.java @@ -38,13 +38,12 @@ public record ShidToPatioContext( @CheckReturnValue public static GlobalContext forTypeInfos(TypeInfos typeInfos) { - var structTags = generateStructTags(typeInfos); var effectTags = generateEffectTags(typeInfos); var runtimeTypeTags = P.<RuntimeType, Integer>orderedPMap(); var context = new GlobalContext( typeInfos, - structTags, + P.map(), effectTags, runtimeTypeTags ); @@ -53,18 +52,33 @@ public record ShidToPatioContext( // runtime (for instance, when allocating ints and strings). context = context.addRuntimeTypeTag(ScalarRuntimeType.INSTANCE); + context = generateStructTags(context); + return context; } @CheckReturnValue - private static PMap<SimpleStructType, Integer> generateStructTags(TypeInfos typeInfos) { - var tags = P.<SimpleStructType, Integer>map(); - var nextTag = 0; - for (var structType : typeInfos.structTypes()) { - tags = tags.plus(structType, nextTag); - nextTag++; + private static GlobalContext generateStructTags(GlobalContext context) { + var structTags = P.<SimpleStructType, Integer>map(); + + for (var structType : context.typeInfos.structTypes()) { + // TODO: get struct types with infos to avoid potential throw. + var structTypeInfo = context.typeInfos.structTypeInfo(structType).orElseThrow(); + var structRuntimeType = ShidToPatioStructs.structRuntimeType(structType, structTypeInfo); + + var runtimeTypeTagResult = context.runtimeTypeTag(structRuntimeType); + var runtimeTypeTag = runtimeTypeTagResult.value(); + context = runtimeTypeTagResult.context(); + + structTags = structTags.plus(structType, runtimeTypeTag); } - return tags; + + return new GlobalContext( + context.typeInfos, + structTags, + context.effectTags, + context.runtimeTypeTags + ); } @CheckReturnValue diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioGc.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioGc.java index dff665f9..dd6e161b 100644 --- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioGc.java +++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioGc.java @@ -20,17 +20,23 @@ public class ShidToPatioGc { private ShidToPatioGc() { } + public static final PatioType RUNTIME_TYPE_TAG_TYPE = PatioTypes.INT32; + public static PatioDataLayout gcObjectDataLayout(PatioDataLayout valueLayout) { return PatioDataLayouts.struct(List.of( PatioDataLayouts.struct(List.of( PatioDataLayouts.DATA_PTR, // next PatioDataLayouts.INT32, // mark - PatioDataLayouts.INT32 // runtime type tag + PatioDataLayouts.fromType(RUNTIME_TYPE_TAG_TYPE) // runtime type tag )), valueLayout )); } + public static List<Integer> runtimeTypeTagOffset() { + return List.of(0, 2); + } + public static List<Integer> gcObjectOffset(List<Integer> valueOffset) { return Lists.concat(List.of(1), valueOffset); } diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioStructs.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioStructs.java index 8cce8d9a..edf1707e 100644 --- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioStructs.java +++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioStructs.java @@ -12,6 +12,7 @@ import org.shedlang.compiler.sources.Source; import org.shedlang.compiler.shid.instructions.ShidDefineFunction; import org.shedlang.compiler.shid.instructions.ShidDefineStruct; import org.shedlang.compiler.types.SimpleStructType; +import org.shedlang.compiler.types.StructTypeInfo; import org.shedlang.compiler.util.Lists; import java.util.List; @@ -21,36 +22,40 @@ public class ShidToPatioStructs { } private static PatioDataLayout unknownStructDataLayout() { - return ShidToPatioGc.gcObjectDataLayout(PatioDataLayouts.struct(List.of( - PatioDataLayouts.INT - ))); + return ShidToPatioGc.gcObjectDataLayout(PatioDataLayouts.struct(List.of())); } private static final List<Integer> TAG_OFFSET - = ShidToPatioGc.gcObjectOffset(List.of(0)); + = ShidToPatioGc.runtimeTypeTagOffset(); private static StructRuntimeType structRuntimeType( SimpleStructType type, ShidToPatioContext context ) { var structTypeInfo = context.structTypeInfo(type); + return structRuntimeType(type, structTypeInfo); + } + + public static StructRuntimeType structRuntimeType( + SimpleStructType type, + StructTypeInfo structTypeInfo + ) { return new StructRuntimeType(type, structTypeInfo.fieldCount()); } - private record StructRuntimeType(SimpleStructType type, int fieldCount) implements RuntimeType { + public record StructRuntimeType(SimpleStructType type, int fieldCount) implements RuntimeType { @Override public PatioDataLayout layout() { - return ShidToPatioGc.gcObjectDataLayout(PatioDataLayouts.struct(List.of( - PatioDataLayouts.INT, + return ShidToPatioGc.gcObjectDataLayout( PatioDataLayouts.struct(Lists.repeat( PatioDataLayouts.INT, fieldCount )) - ))); + ); } private static final List<Integer> GC_CHILDREN_OFFSET = - ShidToPatioGc.gcObjectOffset(List.of(1)); + ShidToPatioGc.gcObjectOffset(List.of()); @Override public List<Integer> gcChildrenOffset() { @@ -67,7 +72,7 @@ public class ShidToPatioStructs { } public List<Integer> fieldOffset(int fieldIndex) { - return ShidToPatioGc.gcObjectOffset(List.of(1, fieldIndex)); + return ShidToPatioGc.gcObjectOffset(List.of(fieldIndex)); } } diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioTypes.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioTypes.java index 1288d430..74f9bd09 100644 --- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioTypes.java +++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioTypes.java @@ -6,6 +6,7 @@ import org.shedlang.compiler.patio.types.PatioTypes; public class ShidToPatioTypes { private ShidToPatioTypes() {} - public static final PatioType TAG_TYPE = PatioTypes.INT; + // TODO: remove duplication with ShidToPatioStructs + public static final PatioType TAG_TYPE = PatioTypes.INT32; public static final PatioType VALUE_TYPE = PatioTypes.INT; } |
