From 0267e1e81e5140e09de199f14ff70c5170491d95 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 16 Sep 2023 15:21:34 +0100 Subject: Move name allocator to backends package --- .../shedlang/compiler/backends/NameAllocator.java | 62 ++++++++++++++++++++++ .../compiler/backends/llvm/LlvmPatioCodeGen.java | 1 + .../compiler/backends/llvm/NameAllocator.java | 62 ---------------------- .../compiler/backends/wasm/WasmCodeGenContext.java | 2 +- .../compiler/patio/PatioCodeGenContext.java | 2 +- 5 files changed, 65 insertions(+), 64 deletions(-) create mode 100644 src/main/java/org/shedlang/compiler/backends/NameAllocator.java delete mode 100644 src/main/java/org/shedlang/compiler/backends/llvm/NameAllocator.java (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/backends/NameAllocator.java b/src/main/java/org/shedlang/compiler/backends/NameAllocator.java new file mode 100644 index 00000000..bb7075b2 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/backends/NameAllocator.java @@ -0,0 +1,62 @@ +package org.shedlang.compiler.backends; + +import org.pcollections.Empty; +import org.pcollections.PSet; + +import java.util.Optional; +import java.util.function.Predicate; + +public record NameAllocator(PSet existingNames) { + public static NameAllocator empty() { + return new NameAllocator<>(Empty.set()); + } + + public Result allocateName( + TPrefix prefix, + Optional name + ) { + var fullName = allocateName(existingNames::contains, prefix, name); + var newAllocator = new NameAllocator(existingNames.plus(fullName)); + return new Result<>(fullName, newAllocator); + } + + public Result allocateFixedName(TPrefix prefix, String name) { + var fullName = allocateFixedName(existingNames::contains, prefix, name); + var newAllocator = new NameAllocator(existingNames.plus(fullName)); + return new Result<>(fullName, newAllocator); + } + + public static String allocateName( + Predicate isUsed, + TPrefix prefix, + Optional name + ) { + var fullPrefix = prefix.toString(); + if (name.isPresent()) { + fullPrefix += "__" + name.get(); + } + + var index = 0; + while (true) { + var fullName = fullPrefix + "__" + index; + if (isUsed.test(fullName)) { + index++; + } else { + return fullName; + } + } + } + + public static String allocateFixedName(Predicate isUsed, TPrefix prefix, String name) { + var fullName = prefix + "__" + name; + + if (isUsed.test(fullName)) { + throw new RuntimeException("name is already allocated: " + fullName); + } + + return fullName; + } + + public record Result(String name, NameAllocator allocator) { + } +} diff --git a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmPatioCodeGen.java b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmPatioCodeGen.java index ed704b85..78fb6fb9 100644 --- a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmPatioCodeGen.java +++ b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmPatioCodeGen.java @@ -4,6 +4,7 @@ import org.pcollections.PMap; import org.pcollections.PStack; import org.pcollections.PVector; import org.pcollections.TreePVector; +import org.shedlang.compiler.backends.NameAllocator; import org.shedlang.compiler.backends.llvm.ir.ast.*; import org.shedlang.compiler.backends.llvm.ir.ast.instructions.*; import org.shedlang.compiler.backends.llvm.ir.ast.operands.*; diff --git a/src/main/java/org/shedlang/compiler/backends/llvm/NameAllocator.java b/src/main/java/org/shedlang/compiler/backends/llvm/NameAllocator.java deleted file mode 100644 index 7a1aba01..00000000 --- a/src/main/java/org/shedlang/compiler/backends/llvm/NameAllocator.java +++ /dev/null @@ -1,62 +0,0 @@ -package org.shedlang.compiler.backends.llvm; - -import org.pcollections.Empty; -import org.pcollections.PSet; - -import java.util.Optional; -import java.util.function.Predicate; - -public record NameAllocator(PSet existingNames) { - public static NameAllocator empty() { - return new NameAllocator<>(Empty.set()); - } - - public Result allocateName( - TPrefix prefix, - Optional name - ) { - var fullName = allocateName(existingNames::contains, prefix, name); - var newAllocator = new NameAllocator(existingNames.plus(fullName)); - return new Result<>(fullName, newAllocator); - } - - public Result allocateFixedName(TPrefix prefix, String name) { - var fullName = allocateFixedName(existingNames::contains, prefix, name); - var newAllocator = new NameAllocator(existingNames.plus(fullName)); - return new Result<>(fullName, newAllocator); - } - - public static String allocateName( - Predicate isUsed, - TPrefix prefix, - Optional name - ) { - var fullPrefix = prefix.toString(); - if (name.isPresent()) { - fullPrefix += "__" + name.get(); - } - - var index = 0; - while (true) { - var fullName = fullPrefix + "__" + index; - if (isUsed.test(fullName)) { - index++; - } else { - return fullName; - } - } - } - - public static String allocateFixedName(Predicate isUsed, TPrefix prefix, String name) { - var fullName = prefix + "__" + name; - - if (isUsed.test(fullName)) { - throw new RuntimeException("name is already allocated: " + fullName); - } - - return fullName; - } - - public record Result(String name, NameAllocator allocator) { - } -} 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 02761563..c533763c 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenContext.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenContext.java @@ -1,7 +1,7 @@ package org.shedlang.compiler.backends.wasm; import org.pcollections.*; -import org.shedlang.compiler.backends.llvm.NameAllocator; +import org.shedlang.compiler.backends.NameAllocator; import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmExpression; import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmInstruction; import org.shedlang.compiler.backends.wasm.structure.ast.linking.WasmMemoryAddress; diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java index 034d945d..44495f3b 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java @@ -4,7 +4,7 @@ import org.pcollections.OrderedPMap; import org.pcollections.PMap; import org.pcollections.PStack; import org.pcollections.PVector; -import org.shedlang.compiler.backends.llvm.NameAllocator; +import org.shedlang.compiler.backends.NameAllocator; import org.shedlang.compiler.patio.ast.*; import org.shedlang.compiler.patio.ast.instructions.PatioInstruction; import org.shedlang.compiler.patio.ast.instructions.PatioLocalStore; -- cgit v1.2.3