aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-06-30 19:51:50 +0100
committerMichael Williamson <mike@zwobble.org>2023-06-30 19:51:50 +0100
commit334782898b59817a00a3ee9b8031cd974178d1ec (patch)
treed5b731976eeab3c52f47cb47e857e31a8f2639f7 /src/main/java
parent71c588ca231d275c5263b3e9ca5f7f57c4e048a1 (diff)
Define builtin modules outside of code gen
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/ShedCompiler.java8
-rw-r--r--src/main/java/org/shedlang/compiler/backends/llvm/LlvmCodeGenNamespaces.java6
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenNamespaces.java20
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/StackCompiler.java14
4 files changed, 20 insertions, 28 deletions
diff --git a/src/main/java/org/shedlang/compiler/ShedCompiler.java b/src/main/java/org/shedlang/compiler/ShedCompiler.java
index 3aedc946..bf26436e 100644
--- a/src/main/java/org/shedlang/compiler/ShedCompiler.java
+++ b/src/main/java/org/shedlang/compiler/ShedCompiler.java
@@ -37,6 +37,14 @@ public class ShedCompiler {
var namespaces = new LinkedHashMap<NamespaceName, List<Instruction>>();
var typeCheckerContext = TypeCheckerContext.EMPTY
+ // TODO: remove duplication of builtin namespace definition
+ .addNamespaceFields(NamespaceName.of("Shed", "Builtins"), P.orderedMap(
+ Map.entry("print", new FunctionType(
+ List.of(Types.STRING),
+ List.of(),
+ Types.UNIT
+ ))
+ ))
.enter(P.map(
Map.entry("Io", Types.effectType(Effects.IO)),
Map.entry("Unit", Types.metaType(Types.UNIT)),
diff --git a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCodeGenNamespaces.java b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCodeGenNamespaces.java
index 62ecaa9f..b3ef2010 100644
--- a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCodeGenNamespaces.java
+++ b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCodeGenNamespaces.java
@@ -107,12 +107,6 @@ public class LlvmCodeGenNamespaces {
));
context = context.createFunction(initContext -> {
- // TODO: handle default locals more gracefully
- initContext = initContext.addShedLocal("print", new LlvmOperandPtrToInt(
- new LlvmVariableGlobal("shed__runtime__print_closure"),
- LlvmCodeGenTypes.VALUE_TYPE
- ));
-
initContext = generateInitBody.apply(initContext);
initContext = initContext.addInstruction(new LlvmStore(
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenNamespaces.java b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenNamespaces.java
index 9704c787..9cf3a575 100644
--- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenNamespaces.java
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenNamespaces.java
@@ -51,24 +51,7 @@ public class WasmCodeGenNamespaces {
globalNameNamespaceInit(namespaceName),
new WasmFuncType(List.of(), List.of()),
initContext -> {
-
- // TODO: handle default locals more gracefully
- var printResult = initContext.addShedLocal("print");
- var print = printResult.value();
- initContext = printResult.context();
-
- initContext = initContext
- .addImport(new WasmImport(
- "env",
- "shed__runtime__print_closure__get",
- new WasmImportDescriptor.Func(
- "shed__runtime__print_closure__get",
- new WasmFuncType(List.of(), List.of(WasmValueType.I32))
- )
- ))
- .addInstruction(new WasmCall(new WasmFuncName("shed__runtime__print_closure__get")))
- .addInstruction(new WasmLocalSet(print));
-
+ // TODO: set isInited to true
return generateInitBody.apply(initContext);
}
);
@@ -83,7 +66,6 @@ public class WasmCodeGenNamespaces {
globalNameNamespaceImport(namespaceName),
new WasmFuncType(List.of(), List.of(WasmCodeGenTypes.VALUE_TYPE)),
bodyContext -> {
- // TODO: only init when necessary
return bodyContext
.addInstruction(new WasmGlobalGet(
new WasmGlobalName(globalNameNamespaceIsInited(namespaceName))
diff --git a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java
index b8356ab4..e4c4f3e1 100644
--- a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java
+++ b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java
@@ -3,6 +3,7 @@ package org.shedlang.compiler.stackir;
import org.pcollections.PVector;
import org.shedlang.compiler.ast.typed.*;
import org.shedlang.compiler.stackir.instructions.*;
+import org.shedlang.compiler.types.NamespaceName;
import org.shedlang.compiler.util.P;
import java.util.stream.Collectors;
@@ -104,9 +105,16 @@ public class StackCompiler {
}
public static PVector<Instruction> compileNamespace(TypedNamespaceNode node) {
- var instructions = node.body().stream()
- .map(statement -> compileNamespaceStatement(statement))
- .reduce(P.vector(), PVector::plusAll);
+ var instructions = P.<Instruction>vector();
+
+ instructions = instructions
+ .plus(new NamespaceImportInstruction(NamespaceName.of("Shed", "Builtins"), node.source()))
+ .plus(new FieldLoadInstruction(NamespaceName.of("Shed", "Builtins"), "print", node.source()))
+ .plus(new LocalStoreInstruction("print", node.source()));
+
+ for (var statement : node.body()) {
+ instructions = instructions.plusAll(compileNamespaceStatement(statement));
+ }
for (var fieldName : node.fieldNames()) {
instructions = instructions