aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-07-21 16:16:19 +0100
committerMichael Williamson <mike@zwobble.org>2023-07-21 16:16:19 +0100
commitf12f949dda91f596c8ec401375e48cef584218c2 (patch)
tree9ebd7000309764bfb7bd91ba008d4661d4bd31c1 /src/main/java
parent8a0d86df0f63e69f4bd6c5699411caddfb4c72f8 (diff)
Introduce type for Stack IR namespaces
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/ShedCompiler.java13
-rw-r--r--src/main/java/org/shedlang/compiler/backends/Backend.java6
-rw-r--r--src/main/java/org/shedlang/compiler/backends/GenerateInstructionsBackend.java12
-rw-r--r--src/main/java/org/shedlang/compiler/backends/PatioBackend.java12
-rw-r--r--src/main/java/org/shedlang/compiler/backends/llvm/LlvmBackend.java18
-rw-r--r--src/main/java/org/shedlang/compiler/backends/llvm/LlvmCodeGen.java8
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/WasmBackend.java14
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java8
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/StackCompiler.java4
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/StackIrNamespace.java12
10 files changed, 56 insertions, 51 deletions
diff --git a/src/main/java/org/shedlang/compiler/ShedCompiler.java b/src/main/java/org/shedlang/compiler/ShedCompiler.java
index 9b030916..45411d3c 100644
--- a/src/main/java/org/shedlang/compiler/ShedCompiler.java
+++ b/src/main/java/org/shedlang/compiler/ShedCompiler.java
@@ -5,7 +5,7 @@ import org.shedlang.compiler.config.ProjectConfig;
import org.shedlang.compiler.parser.Parser;
import org.shedlang.compiler.sources.Sources;
import org.shedlang.compiler.stackir.StackCompiler;
-import org.shedlang.compiler.stackir.instructions.Instruction;
+import org.shedlang.compiler.stackir.StackIrNamespace;
import org.shedlang.compiler.typechecker.TypeChecker;
import org.shedlang.compiler.typechecker.TypeCheckerContext;
import org.shedlang.compiler.types.Effects;
@@ -20,7 +20,10 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
import java.util.stream.StreamSupport;
import static org.shedlang.compiler.parser.Tokeniser.tokenise;
@@ -33,7 +36,7 @@ public class ShedCompiler {
var sourceFilePaths = collectSourceFilePaths(sourceRoot);
- var namespaces = new LinkedHashMap<NamespaceName, List<Instruction>>();
+ var namespaces = new ArrayList<StackIrNamespace>();
var typeCheckerContext = TypeCheckerContext.EMPTY
.addNamespaceFields(ShedBuiltins.NAMESPACE_NAME, P.orderedMap(
@@ -67,8 +70,8 @@ public class ShedCompiler {
var typeCheckNamespaceResult = TypeChecker.typeCheckNamespace(untypedNode, typeCheckerContext);
typeCheckerContext = typeCheckNamespaceResult.context();
- var instructions = StackCompiler.compileNamespace(typeCheckNamespaceResult.value());
- namespaces.put(namespaceName, instructions);
+ var stackIrNamespace = StackCompiler.compileNamespace(typeCheckNamespaceResult.value());
+ namespaces.add(stackIrNamespace);
}
backend.compile(config, namespaces, typeCheckerContext.toTypeInfo(), config.targetDir().resolve(backend.name()));
diff --git a/src/main/java/org/shedlang/compiler/backends/Backend.java b/src/main/java/org/shedlang/compiler/backends/Backend.java
index dd993cbd..3bfb77ab 100644
--- a/src/main/java/org/shedlang/compiler/backends/Backend.java
+++ b/src/main/java/org/shedlang/compiler/backends/Backend.java
@@ -1,19 +1,17 @@
package org.shedlang.compiler.backends;
import org.shedlang.compiler.config.ProjectConfig;
-import org.shedlang.compiler.stackir.instructions.Instruction;
+import org.shedlang.compiler.stackir.StackIrNamespace;
import org.shedlang.compiler.typechecker.TypeInfo;
-import org.shedlang.compiler.types.NamespaceName;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
-import java.util.Map;
public interface Backend {
void compile(
ProjectConfig config,
- Map<NamespaceName, List<Instruction>> namespaces,
+ List<StackIrNamespace> namespaces,
TypeInfo typeInfo,
Path targetDir
) throws IOException, InterruptedException;
diff --git a/src/main/java/org/shedlang/compiler/backends/GenerateInstructionsBackend.java b/src/main/java/org/shedlang/compiler/backends/GenerateInstructionsBackend.java
index 88cfa631..fe64a16d 100644
--- a/src/main/java/org/shedlang/compiler/backends/GenerateInstructionsBackend.java
+++ b/src/main/java/org/shedlang/compiler/backends/GenerateInstructionsBackend.java
@@ -2,26 +2,24 @@ package org.shedlang.compiler.backends;
import org.shedlang.compiler.config.ProjectConfig;
import org.shedlang.compiler.stackir.InstructionSerializer;
-import org.shedlang.compiler.stackir.instructions.Instruction;
+import org.shedlang.compiler.stackir.StackIrNamespace;
import org.shedlang.compiler.typechecker.TypeInfo;
-import org.shedlang.compiler.types.NamespaceName;
import java.nio.file.Path;
import java.util.List;
-import java.util.Map;
public class GenerateInstructionsBackend implements Backend {
public void compile(
ProjectConfig config,
- Map<NamespaceName, List<Instruction>> namespaces,
+ List<StackIrNamespace> namespaces,
TypeInfo typeInfo,
Path targetDir
) {
System.out.println("main: " + config.main());
- for (var namespaceEntry : namespaces.entrySet()) {
- var namespaceName = namespaceEntry.getKey();
- var instructions = namespaceEntry.getValue();
+ for (var namespace : namespaces) {
+ var namespaceName = namespace.namespaceName();
+ var instructions = namespace.instructions();
System.out.println(namespaceName);
diff --git a/src/main/java/org/shedlang/compiler/backends/PatioBackend.java b/src/main/java/org/shedlang/compiler/backends/PatioBackend.java
index bde6f901..9e8e5560 100644
--- a/src/main/java/org/shedlang/compiler/backends/PatioBackend.java
+++ b/src/main/java/org/shedlang/compiler/backends/PatioBackend.java
@@ -4,18 +4,16 @@ import org.shedlang.compiler.config.ProjectConfig;
import org.shedlang.compiler.patio.PatioCodeGen;
import org.shedlang.compiler.patio.PatioCodeGenContext;
import org.shedlang.compiler.patio.ast.PatioSerializer;
-import org.shedlang.compiler.stackir.instructions.Instruction;
+import org.shedlang.compiler.stackir.StackIrNamespace;
import org.shedlang.compiler.typechecker.TypeInfo;
-import org.shedlang.compiler.types.NamespaceName;
import java.nio.file.Path;
import java.util.List;
-import java.util.Map;
public class PatioBackend implements Backend {
public void compile(
ProjectConfig config,
- Map<NamespaceName, List<Instruction>> namespaces,
+ List<StackIrNamespace> namespaces,
TypeInfo typeInfo,
Path targetDir
) {
@@ -23,9 +21,9 @@ public class PatioBackend implements Backend {
var context = PatioCodeGenContext.forTypeInfo(typeInfo);
- for (var namespaceEntry : namespaces.entrySet()) {
- var namespaceName = namespaceEntry.getKey();
- var instructions = namespaceEntry.getValue();
+ for (var namespace : namespaces) {
+ var namespaceName = namespace.namespaceName();
+ var instructions = namespace.instructions();
System.out.println(namespaceName);
diff --git a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmBackend.java b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmBackend.java
index 9bb113dd..0dead3ac 100644
--- a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmBackend.java
+++ b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmBackend.java
@@ -5,9 +5,8 @@ import org.shedlang.compiler.backends.llvm.ir.LlvmIrSerializer;
import org.shedlang.compiler.backends.llvm.ir.ast.LlvmModule;
import org.shedlang.compiler.config.ProjectConfig;
import org.shedlang.compiler.filesystem.ShedCompilerFiles;
-import org.shedlang.compiler.stackir.instructions.Instruction;
+import org.shedlang.compiler.stackir.StackIrNamespace;
import org.shedlang.compiler.typechecker.TypeInfo;
-import org.shedlang.compiler.types.NamespaceName;
import java.io.BufferedWriter;
import java.io.FileWriter;
@@ -17,7 +16,6 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
-import java.util.Map;
import static org.shedlang.compiler.backends.ProcessRunner.runInheritIo;
@@ -25,20 +23,22 @@ public class LlvmBackend implements Backend {
@Override
public void compile(
ProjectConfig config,
- Map<NamespaceName, List<Instruction>> namespaces,
+ List<StackIrNamespace> namespaces,
TypeInfo typeInfo,
Path targetDir
) throws IOException, InterruptedException {
var objectFilePaths = new ArrayList<Path>();
- for (var namespace : namespaces.entrySet()) {
- var namespaceName = namespace.getKey();
+ for (var namespace : namespaces) {
var llvmModule = LlvmCodeGen.generateNamespace(
- namespaceName,
- namespace.getValue(),
+ namespace,
typeInfo
);
- var objectPath = compileLlvmModule(llvmModule, targetDir.resolve("namespaces"), String.join("_", namespaceName.parts()));
+ var objectPath = compileLlvmModule(
+ llvmModule,
+ targetDir.resolve("namespaces"),
+ String.join("_", namespace.namespaceName().parts())
+ );
objectFilePaths.add(objectPath);
}
diff --git a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCodeGen.java b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCodeGen.java
index f937de4e..8763d6f8 100644
--- a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCodeGen.java
+++ b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCodeGen.java
@@ -9,6 +9,7 @@ import org.shedlang.compiler.backends.llvm.ir.ast.operands.LlvmOperandInt;
import org.shedlang.compiler.backends.llvm.ir.ast.operands.LlvmVariable;
import org.shedlang.compiler.backends.llvm.ir.ast.operands.LlvmVariableLocal;
import org.shedlang.compiler.backends.llvm.ir.ast.types.LlvmTypes;
+import org.shedlang.compiler.stackir.StackIrNamespace;
import org.shedlang.compiler.stackir.instructions.*;
import org.shedlang.compiler.typechecker.TypeInfo;
import org.shedlang.compiler.types.NamespaceName;
@@ -54,15 +55,14 @@ public class LlvmCodeGen {
}
public static LlvmModule generateNamespace(
- NamespaceName namespaceName,
- List<Instruction> instructions,
+ StackIrNamespace namespace,
TypeInfo typeInfo
) {
var context = defaultContext(typeInfo);
context = LlvmCodeGenNamespaces.define(
- namespaceName,
- initContext -> generateInstructions(instructions, initContext),
+ namespace.namespaceName(),
+ initContext -> generateInstructions(namespace.instructions(), initContext),
context
);
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/WasmBackend.java b/src/main/java/org/shedlang/compiler/backends/wasm/WasmBackend.java
index 1526671d..d127525e 100644
--- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmBackend.java
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmBackend.java
@@ -5,9 +5,8 @@ import org.shedlang.compiler.backends.wasm.structure.WasmBinaryFormat;
import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmModule;
import org.shedlang.compiler.config.ProjectConfig;
import org.shedlang.compiler.filesystem.ShedCompilerFiles;
-import org.shedlang.compiler.stackir.instructions.Instruction;
+import org.shedlang.compiler.stackir.StackIrNamespace;
import org.shedlang.compiler.typechecker.TypeInfo;
-import org.shedlang.compiler.types.NamespaceName;
import java.io.FileOutputStream;
import java.io.IOException;
@@ -15,7 +14,6 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
-import java.util.Map;
import static org.shedlang.compiler.backends.ProcessRunner.runInheritIo;
@@ -23,23 +21,21 @@ public class WasmBackend implements Backend {
@Override
public void compile(
ProjectConfig config,
- Map<NamespaceName, List<Instruction>> namespaces,
+ List<StackIrNamespace> namespaces,
TypeInfo typeInfo,
Path targetDir
) throws IOException, InterruptedException {
var objectFilePaths = new ArrayList<Path>();
- for (var namespace : namespaces.entrySet()) {
- var namespaceName = namespace.getKey();
+ for (var namespace : namespaces) {
var wasmModule = WasmCodeGen.generateNamespace(
- namespaceName,
- namespace.getValue(),
+ namespace,
typeInfo
);
var objectPath = writeObjectFile(
wasmModule,
targetDir.resolve("namespaces"),
- String.join("_", namespaceName.parts())
+ String.join("_", namespace.namespaceName().parts())
);
objectFilePaths.add(objectPath);
}
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java
index 254d0b86..87cc5ad5 100644
--- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java
@@ -5,6 +5,7 @@ import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmLocalIndex;
import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmModule;
import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmFuncType;
import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmValueType;
+import org.shedlang.compiler.stackir.StackIrNamespace;
import org.shedlang.compiler.stackir.instructions.*;
import org.shedlang.compiler.typechecker.TypeInfo;
import org.shedlang.compiler.types.NamespaceName;
@@ -82,16 +83,15 @@ public class WasmCodeGen {
}
public static WasmModule generateNamespace(
- NamespaceName namespaceName,
- List<Instruction> instructions,
+ StackIrNamespace namespace,
TypeInfo typeInfo
) {
var context = WasmCodeGenContext.fromTypeInfo(typeInfo);
context = WasmCodeGenNamespaces.define(
- namespaceName,
+ namespace.namespaceName(),
initContext -> {
- for (var instruction : instructions) {
+ for (var instruction : namespace.instructions()) {
initContext = generateInstruction(instruction, initContext);
}
diff --git a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java
index 85d3cc5a..537e9322 100644
--- a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java
+++ b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java
@@ -186,7 +186,7 @@ public class StackCompiler {
return P.vector(new PushIntInstruction(node.value(), node.source()));
}
- public static PVector<Instruction> compileNamespace(TypedNamespaceNode node) {
+ public static StackIrNamespace compileNamespace(TypedNamespaceNode node) {
var instructions = P.<Instruction>vector();
instructions = instructions
@@ -208,7 +208,7 @@ public class StackCompiler {
));
}
- return instructions;
+ return new StackIrNamespace(node.namespaceName(), instructions);
}
public static PVector<Instruction> compileNamespaceStatement(TypedNamespaceStatementNode node) {
diff --git a/src/main/java/org/shedlang/compiler/stackir/StackIrNamespace.java b/src/main/java/org/shedlang/compiler/stackir/StackIrNamespace.java
new file mode 100644
index 00000000..65fd41d3
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/stackir/StackIrNamespace.java
@@ -0,0 +1,12 @@
+package org.shedlang.compiler.stackir;
+
+import org.shedlang.compiler.stackir.instructions.Instruction;
+import org.shedlang.compiler.types.NamespaceName;
+
+import java.util.List;
+
+public record StackIrNamespace(
+ NamespaceName namespaceName,
+ List<Instruction> instructions
+) {
+}