aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-06-26 23:04:50 +0100
committerMichael Williamson <mike@zwobble.org>2023-06-26 23:04:50 +0100
commit22ed45bc970309ec63163b1799782356564e3943 (patch)
treee5d28fa927eb5702ab5a51625ec6d36607da0de6 /src/main/java
parent028ef9e1b1502b5380f6110f1870808262129623 (diff)
Split WasmCodeGenContext into module and function parts
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenClosures.java3
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenContext.java347
2 files changed, 209 insertions, 141 deletions
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenClosures.java b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenClosures.java
index c1a56d73..bc21afbf 100644
--- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenClosures.java
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenClosures.java
@@ -56,7 +56,8 @@ public class WasmCodeGenClosures {
functionContext = compileBody.apply(functionContext);
- context = context.exitFunction(functionContext.toFunction(functionName));
+ var function = functionContext.toFunction(functionName);
+ context = context.exitFunction(functionContext).addFunction(function);
return context;
}
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenContext.java b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenContext.java
index b7120624..f8b28d19 100644
--- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenContext.java
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenContext.java
@@ -15,33 +15,191 @@ import java.util.List;
import java.util.Optional;
public record WasmCodeGenContext(
- OrderedPSet<WasmFuncType> types,
- OrderedPMap<String, WasmFunction> functionsByName,
- WasmMemoryBuilder memory,
- WasmFuncType funcType,
- PVector<WasmInstruction> instructions,
- OrderedPMap<String, WasmLocal> locals,
- PMap<String, WasmLocalIndex> shedLocalNameToLocalIndex
+ ModuleContext moduleContext,
+ FunctionContext functionContext
) {
+ record ModuleContext(
+ OrderedPSet<WasmFuncType> types,
+ OrderedPMap<String, WasmFunction> functionsByName,
+ WasmMemoryBuilder memory
+ ) {
+ public static final ModuleContext EMPTY = new ModuleContext(
+ OrderedPSet.empty(),
+ OrderedPMap.empty(),
+ WasmMemoryBuilder.EMPTY
+ );
+
+ public ModuleContext addType(WasmFuncType funcType) {
+ return new ModuleContext(
+ this.types.plus(funcType),
+ this.functionsByName,
+ this.memory
+ );
+ }
+
+ public String generateFunctionName(WasmCodeGenGlobalSpace space, String name) {
+ return NameAllocator.generateName(
+ functionsByName::containsKey,
+ "shed__" + space,
+ Optional.of(name)
+ );
+ }
+
+ public ModuleContext addFunction(WasmFunction function) {
+ return new ModuleContext(
+ this.types.plus(function.type()),
+ this.functionsByName.plus(function.name(), function),
+ this.memory
+ );
+ }
+
+ public Result<WasmMemoryAddress> addData(
+ WasmCodeGenGlobalSpace space,
+ byte[] data,
+ WasmAlignment alignment
+ ) {
+ var result = memory.add(space, data, alignment);
+
+ return new ModuleContext(
+ this.types,
+ this.functionsByName,
+ result.memory()
+ ).toResult(result.offset());
+ }
+
+ public WasmModule toModule() {
+ var module = WasmModule.EMPTY;
+
+ module = module.addImport(new WasmImport(
+ "env",
+ "__indirect_function_table",
+ new WasmImportDescriptor.Table(new WasmTableType(
+ WasmReferenceType.FUNC_REF,
+ // TODO: set size according to number of entries
+ WasmLimits.min(0)
+ ))
+ ));
+
+ for (var type : types) {
+ module = module.addType(type);
+ }
+
+ for (var function : functionsByName.values()) {
+ module = module.addFunction(function);
+ }
+
+ return memory.toModule(module);
+ }
+
+ public <T> Result<T> toResult(T value) {
+ return new Result<>(value, this);
+ }
+
+ record Result<T>(T value, ModuleContext context) {
+ }
+ }
+
+ record FunctionContext(
+ WasmFuncType funcType,
+ PVector<WasmInstruction> instructions,
+ OrderedPMap<String, WasmLocal> locals,
+ PMap<String, WasmLocalIndex> shedLocalNameToLocalIndex
+ ) {
+ public static final FunctionContext EMPTY = new FunctionContext(
+ new WasmFuncType(List.of(), List.of()),
+ Empty.vector(),
+ OrderedPMap.empty(),
+ Empty.map()
+ );
+
+ public FunctionContext addInstruction(WasmInstruction instruction) {
+ return new FunctionContext(
+ this.funcType,
+ this.instructions.plus(instruction),
+ this.locals,
+ this.shedLocalNameToLocalIndex
+ );
+ }
+
+ public Result<WasmLocalIndex> generateLocal(WasmCodeGenLocalSpace space, Optional<String> name) {
+ var localIndex = new WasmLocalIndex(locals.size());
+ var localName = NameAllocator.generateName(
+ x -> locals.containsKey(x),
+ space,
+ name
+ );
+ return new FunctionContext(
+ this.funcType,
+ this.instructions,
+ this.locals.plus(
+ localName,
+ new WasmLocal(localName, WasmCodeGenTypes.VALUE_TYPE)
+ ),
+ this.shedLocalNameToLocalIndex
+ ).toResult(localIndex);
+ }
+
+ public Result<WasmLocalIndex> addShedLocal(String name) {
+ var localIndexResult = generateLocal(WasmCodeGenLocalSpace.LOCAL, Optional.of(name));
+ var localIndex = localIndexResult.value();
+ var context = localIndexResult.context();
+
+ return new FunctionContext(
+ context.funcType,
+ context.instructions,
+ context.locals,
+ context.shedLocalNameToLocalIndex.plus(name, localIndex)
+ ).toResult(localIndex);
+ }
+
+ public WasmLocalIndex shedLocal(String name) {
+ var localIndex = shedLocalNameToLocalIndex.get(name);
+ if (localIndex == null) {
+ // TODO: throw better error
+ throw new RuntimeException("unknown local: " + name);
+ } else {
+ return localIndex;
+ }
+ }
+
+ public WasmFunction toFunction(String name) {
+ return new WasmFunction(
+ name,
+ locals.values().stream().limit(funcType.params().size()).toList(),
+ locals.values().stream().skip(funcType.params().size()).toList(),
+ funcType.results(),
+ new WasmExpression(instructions)
+ );
+ }
+
+ public <T> Result<T> toResult(T value) {
+ return new Result<>(value, this);
+ }
+
+ record Result<T>(T value, FunctionContext context) {
+ }
+ }
+
public static final WasmCodeGenContext EMPTY = new WasmCodeGenContext(
- OrderedPSet.empty(),
- OrderedPMap.empty(),
- WasmMemoryBuilder.EMPTY,
- new WasmFuncType(List.of(), List.of()),
- Empty.vector(),
- OrderedPMap.empty(),
- Empty.map()
+ ModuleContext.EMPTY,
+ FunctionContext.EMPTY
);
public WasmCodeGenContext addType(WasmFuncType funcType) {
return new WasmCodeGenContext(
- this.types.plus(funcType),
- this.functionsByName,
- this.memory,
- this.funcType,
- this.instructions,
- this.locals,
- this.shedLocalNameToLocalIndex
+ this.moduleContext.addType(funcType),
+ this.functionContext
+ );
+ }
+
+ public String generateFunctionName(WasmCodeGenGlobalSpace space, String name) {
+ return moduleContext.generateFunctionName(space, name);
+ }
+
+ public WasmCodeGenContext addFunction(WasmFunction function) {
+ return new WasmCodeGenContext(
+ this.moduleContext.addFunction(function),
+ this.functionContext
);
}
@@ -50,135 +208,49 @@ public record WasmCodeGenContext(
byte[] data,
WasmAlignment alignment
) {
- var result = memory.add(space, data, alignment);
+ var result = this.moduleContext.addData(space, data, alignment);
return new WasmCodeGenContext(
- this.types,
- this.functionsByName,
- result.memory(),
- this.funcType,
- this.instructions,
- this.locals,
- this.shedLocalNameToLocalIndex
- ).toResult(result.offset());
+ result.context,
+ this.functionContext
+ ).toResult(result.value);
}
- public WasmCodeGenContext addInstruction(WasmInstruction instruction) {
- return new WasmCodeGenContext(
- this.types,
- this.functionsByName,
- this.memory,
- this.funcType,
- this.instructions.plus(instruction),
- this.locals,
- this.shedLocalNameToLocalIndex
- );
+ public WasmModule toModule() {
+ return moduleContext.toModule();
}
- public WasmCodeGenContext toFunction(String name) {
- var function = new WasmFunction(
- name,
- locals.values().stream().limit(funcType.params().size()).toList(),
- locals.values().stream().skip(funcType.params().size()).toList(),
- funcType.results(),
- new WasmExpression(instructions)
- );
+ public WasmCodeGenContext addInstruction(WasmInstruction instruction) {
return new WasmCodeGenContext(
- this.types.plus(function.type()),
- this.functionsByName.plus(function.name(), function),
- this.memory,
- new WasmFuncType(List.of(), List.of()),
- Empty.vector(),
- OrderedPMap.empty(),
- Empty.map()
+ this.moduleContext,
+ this.functionContext.addInstruction(instruction)
);
}
- public WasmModule toModule() {
- var module = WasmModule.EMPTY;
-
- module = module.addImport(new WasmImport(
- "env",
- "__indirect_function_table",
- new WasmImportDescriptor.Table(new WasmTableType(
- WasmReferenceType.FUNC_REF,
- // TODO: set size according to number of entries
- WasmLimits.min(0)
- ))
- ));
-
- for (var type : types) {
- module = module.addType(type);
- }
-
- for (var function : functionsByName.values()) {
- module = module.addFunction(function);
- }
-
- return memory.toModule(module);
+ public WasmFunction toFunction(String name) {
+ return this.functionContext.toFunction(name);
}
public WasmCodeGenResult<WasmLocalIndex> addShedLocal(String name) {
- var localIndexResult = generateLocal(WasmCodeGenLocalSpace.LOCAL, name);
- var localIndex = localIndexResult.value();
- var context = localIndexResult.context();
+ var result = this.functionContext.addShedLocal(name);
- return new WasmCodeGenContext(
- context.types,
- context.functionsByName,
- context.memory,
- context.funcType,
- context.instructions,
- context.locals,
- context.shedLocalNameToLocalIndex.plus(name, localIndex)
- ).toResult(localIndex);
+ return new WasmCodeGenContext(moduleContext, result.context).toResult(result.value);
}
public WasmLocalIndex shedLocal(String name) {
- var localIndex = shedLocalNameToLocalIndex.get(name);
- if (localIndex == null) {
- // TODO: throw better error
- throw new RuntimeException("unknown local: " + name);
- } else {
- return localIndex;
- }
+ return functionContext.shedLocal(name);
}
public WasmCodeGenResult<WasmLocalIndex> generateLocal(WasmCodeGenLocalSpace space) {
- return generateLocal(space, Optional.empty());
- }
+ var result = functionContext.generateLocal(space, Optional.empty());
- public WasmCodeGenResult<WasmLocalIndex> generateLocal(WasmCodeGenLocalSpace space, String name) {
- return generateLocal(space, Optional.of(name));
+ return new WasmCodeGenContext(this.moduleContext, result.context).toResult(result.value);
}
- private WasmCodeGenResult<WasmLocalIndex> generateLocal(WasmCodeGenLocalSpace space, Optional<String> name) {
- var localIndex = new WasmLocalIndex(locals.size());
- var localName = NameAllocator.generateName(
- x -> locals.containsKey(x),
- space,
- name
- );
- return new WasmCodeGenContext(
- this.types,
- this.functionsByName,
- this.memory,
- this.funcType,
- this.instructions,
- this.locals.plus(
- localName,
- new WasmLocal(localName, WasmCodeGenTypes.VALUE_TYPE)
- ),
- this.shedLocalNameToLocalIndex
- ).toResult(localIndex);
- }
+ public WasmCodeGenResult<WasmLocalIndex> generateLocal(WasmCodeGenLocalSpace space, String name) {
+ var result = functionContext.generateLocal(space, Optional.of(name));
- public String generateFunctionName(WasmCodeGenGlobalSpace space, String name) {
- return NameAllocator.generateName(
- functionsByName::containsKey,
- "shed__" + space,
- Optional.of(name)
- );
+ return new WasmCodeGenContext(this.moduleContext, result.context).toResult(result.value);
}
public WasmTableIndex indirectFunctionTableIndex() {
@@ -187,26 +259,21 @@ public record WasmCodeGenContext(
public WasmCodeGenContext enterFunction(WasmFuncType funcType) {
return new WasmCodeGenContext(
- this.types,
- this.functionsByName,
- this.memory,
- funcType,
- Empty.vector(),
- OrderedPMap.empty(),
- Empty.map()
+ this.moduleContext,
+ new FunctionContext(
+ funcType,
+ Empty.vector(),
+ OrderedPMap.empty(),
+ Empty.map()
+ )
);
}
public WasmCodeGenContext exitFunction(WasmCodeGenContext functionContext) {
// TODO: merge properly (or throw if changed?) (or use lambda?)
return new WasmCodeGenContext(
- functionContext.types,
- functionContext.functionsByName,
- functionContext.memory,
- this.funcType,
- this.instructions,
- this.locals,
- this.shedLocalNameToLocalIndex
+ functionContext.moduleContext,
+ this.functionContext
);
}