aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-12-26 21:42:17 +0000
committerMichael Williamson <mike@zwobble.org>2023-12-26 21:42:17 +0000
commit9e8afed0f3e71ac54c10244ec1cf74ec63dec577 (patch)
tree2da46f12650f0dda3bfae07eccefff381d75fc30 /src/main/java
parentc87a5d2246d36baefd1b782d29172d36572fa8aa (diff)
Use platform target to determine Wasm type for Patio Ints
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/PatioToWasm.java75
1 files changed, 53 insertions, 22 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 efaac8da..f62bd994 100644
--- a/src/main/java/org/shedlang/compiler/backends/wasm/PatioToWasm.java
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/PatioToWasm.java
@@ -26,7 +26,7 @@ public class PatioToWasm {
public static WasmModule generateCompilationUnit(
PatioCompilationUnit patioCompilationUnit,
- WasmTarget target
+ WasmTarget platformTarget
) {
var module = WasmModule.EMPTY.addImport(new WasmImport(
"env",
@@ -34,8 +34,8 @@ public class PatioToWasm {
new WasmImportDescriptor.Func(
ALLOC_NAME,
new WasmFuncType(
- List.of(target.sizeT(), target.sizeT(), target.sizeT()),
- List.of(target.dataPointerType())
+ List.of(platformTarget.sizeT(), platformTarget.sizeT(), platformTarget.sizeT()),
+ List.of(platformTarget.dataPointerType())
)
)
));
@@ -46,13 +46,13 @@ public class PatioToWasm {
new WasmImportDescriptor.Func(
STACK_ALLOC_NAME,
new WasmFuncType(
- List.of(target.sizeT()),
- List.of(target.dataPointerType())
+ List.of(platformTarget.sizeT()),
+ List.of(platformTarget.dataPointerType())
)
)
));
- var wasmMemoryBuilder = WasmMemoryBuilder.empty(target.indexType());
+ var wasmMemoryBuilder = WasmMemoryBuilder.empty(platformTarget.indexType());
for (var patioCompilationUnitElement : patioCompilationUnit.elements()) {
switch (patioCompilationUnitElement) {
@@ -75,7 +75,7 @@ public class PatioToWasm {
var wasmFuncType = new WasmFuncType(
patioFunctionDeclaration.params().stream()
- .map(param -> patioTypeToWasmValueType(param.type()))
+ .map(param -> patioTypeToWasmValueType(param.type(), platformTarget))
.toList(),
patioTypeToWasmResultTypes(patioFunctionDeclaration.returnType())
);
@@ -100,7 +100,7 @@ public class PatioToWasm {
var wasmLocals = new ArrayList<WasmLocal>();
for (var patioLocal : patioFunctionDefinition.locals()) {
- var wasmLocal = patioLocalToWasmLocal(patioLocal);
+ var wasmLocal = patioLocalToWasmLocal(patioLocal, platformTarget);
switch (patioLocal.localType()) {
case PARAM -> {
wasmParams.add(wasmLocal);
@@ -115,7 +115,7 @@ public class PatioToWasm {
var context = generateInstructions(
patioFunctionDefinition.body(),
- Context.forFunction(module, Lists.concat(wasmParams, wasmLocals))
+ Context.forFunction(platformTarget, module, Lists.concat(wasmParams, wasmLocals))
);
module = context.module;
@@ -151,6 +151,8 @@ public class PatioToWasm {
PatioInstruction patioInstruction,
Context context
) {
+ var platformTarget = context.platformTarget;
+
return switch (patioInstruction) {
case PatioAllocHeap patioAllocHeap -> {
context = context
@@ -208,9 +210,9 @@ public class PatioToWasm {
var wasmFuncType = new WasmFuncType(
patioCallPointer.args().stream()
- .map(arg -> patioOperandToWasmValueType(arg))
+ .map(arg -> patioOperandToWasmValueType(arg, platformTarget))
.toList(),
- List.of(patioOperandToWasmValueType(patioCallPointer.target()))
+ List.of(patioOperandToWasmValueType(patioCallPointer.target(), platformTarget))
);
context = context.addType(wasmFuncType);
@@ -236,7 +238,7 @@ public class PatioToWasm {
case PatioIf patioIf -> {
context = generateOperand(patioIf.condition(), context);
- var wasmBlockType = generateBlockType(patioIf.targets());
+ var wasmBlockType = generateBlockType(patioIf.targets(), platformTarget);
context = context.enterIfInstruction(wasmBlockType);
context = generateBlockBody(patioIf.trueBody(), context);
@@ -351,7 +353,7 @@ public class PatioToWasm {
case PatioSwitch patioSwitch -> {
// TODO: use br_table?
- var wasmBlockType = generateBlockType(patioSwitch.targets());
+ var wasmBlockType = generateBlockType(patioSwitch.targets(), platformTarget);
for (var patioSwitchCase : patioSwitch.cases()) {
context = generateOperand(patioSwitch.value(), context);
@@ -423,10 +425,13 @@ public class PatioToWasm {
};
}
- private static WasmBlockType generateBlockType(List<PatioLocalRef> targets) {
+ private static WasmBlockType generateBlockType(
+ List<PatioLocalRef> targets,
+ WasmTarget platformTarget
+ ) {
return new WasmBlockType(
targets.stream()
- .map(patioTarget -> patioOperandToWasmValueType(patioTarget))
+ .map(patioTarget -> patioOperandToWasmValueType(patioTarget, platformTarget))
.toList()
);
}
@@ -631,16 +636,25 @@ public class PatioToWasm {
return sizeOf(value.layout());
}
- private static WasmLocal patioLocalToWasmLocal(PatioLocal local) {
- return new WasmLocal(local.name(), patioTypeToWasmValueType(local.type()));
+ private static WasmLocal patioLocalToWasmLocal(
+ PatioLocal local,
+ WasmTarget platformTarget
+ ) {
+ return new WasmLocal(local.name(), patioTypeToWasmValueType(local.type(), platformTarget));
}
- private static WasmValueType patioOperandToWasmValueType(PatioOperand operand) {
- return WasmValueType.I32;
+ private static WasmValueType patioOperandToWasmValueType(
+ PatioOperand operand,
+ WasmTarget platformTarget
+ ) {
+ return platformTarget.sizeT();
}
- private static WasmValueType patioTypeToWasmValueType(PatioType type) {
- return WasmValueType.I32;
+ private static WasmValueType patioTypeToWasmValueType(
+ PatioType type,
+ WasmTarget platformTarget
+ ) {
+ return platformTarget.sizeT();
}
private static List<WasmValueType> patioTypeToWasmResultTypes(PatioType type) {
@@ -648,7 +662,11 @@ public class PatioToWasm {
}
private static class Context {
- public static Context forFunction(WasmModule module, List<WasmLocal> locals) {
+ public static Context forFunction(
+ WasmTarget platformTarget,
+ WasmModule module,
+ List<WasmLocal> locals
+ ) {
var patioLocalNameToWasmLocalIndex = P.<String, WasmLocalIndex>map();
for (var local : locals) {
@@ -659,6 +677,7 @@ public class PatioToWasm {
}
return new Context(
+ platformTarget,
module,
P.vector(),
P.stack(),
@@ -668,6 +687,7 @@ public class PatioToWasm {
);
}
+ private final WasmTarget platformTarget;
private final WasmModule module;
private final PVector<WasmInstruction> instructions;
private final PStack<PVector<WasmInstruction>> instructionsStack;
@@ -676,6 +696,7 @@ public class PatioToWasm {
private final PStack<WasmStructuredInstructionBuilder> structuredInstructionBuilderStack;
private Context(
+ WasmTarget platformTarget,
WasmModule module,
PVector<WasmInstruction> instructions,
PStack<PVector<WasmInstruction>> instructionsStack,
@@ -683,6 +704,7 @@ public class PatioToWasm {
WasmStructuredInstructionBuilder structuredInstructionBuilder,
PStack<WasmStructuredInstructionBuilder> structuredInstructionBuilderStack
) {
+ this.platformTarget = platformTarget;
this.module = module;
this.instructions = instructions;
this.instructionsStack = instructionsStack;
@@ -693,6 +715,7 @@ public class PatioToWasm {
private Context addType(WasmFuncType type) {
return new Context(
+ this.platformTarget,
this.module.addType(type),
this.instructions,
this.instructionsStack,
@@ -704,6 +727,7 @@ public class PatioToWasm {
private Context addInstruction(WasmInstruction instruction) {
return new Context(
+ this.platformTarget,
this.module,
this.instructions.plus(instruction),
this.instructionsStack,
@@ -719,6 +743,7 @@ public class PatioToWasm {
public Context enterBlockInstruction() {
return new Context(
+ this.platformTarget,
this.module,
P.vector(),
this.instructionsStack.plus(this.instructions),
@@ -739,6 +764,7 @@ public class PatioToWasm {
public Context enterLoopInstruction() {
return new Context(
+ this.platformTarget,
this.module,
P.vector(),
this.instructionsStack.plus(this.instructions),
@@ -759,6 +785,7 @@ public class PatioToWasm {
public Context enterIfInstruction(WasmBlockType blockType) {
return new Context(
+ this.platformTarget,
this.module,
P.vector(),
this.instructionsStack.plus(this.instructions),
@@ -770,6 +797,7 @@ public class PatioToWasm {
public Context enterElse() {
return new Context(
+ this.platformTarget,
this.module,
P.vector(),
this.instructionsStack,
@@ -786,6 +814,7 @@ public class PatioToWasm {
public Context enterTryInstruction(WasmBlockType blockType) {
return new Context(
+ this.platformTarget,
this.module,
P.vector(),
this.instructionsStack.plus(this.instructions),
@@ -797,6 +826,7 @@ public class PatioToWasm {
public Context enterCatch(WasmTagName tagName) {
return new Context(
+ this.platformTarget,
this.module,
P.vector(),
this.instructionsStack,
@@ -813,6 +843,7 @@ public class PatioToWasm {
public Context exitStructuredInstruction() {
var context = new Context(
+ this.platformTarget,
this.module,
this.instructionsStack.get(0),
this.instructionsStack.minus(0),