From 6366198588909777722d4834085ebdf9838f5828 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Tue, 13 Jun 2023 20:21:45 +0100 Subject: Extract POrderedMap --- .../backends/llvm/LlvmCompilerContext.java | 35 ++++++++++++---------- 1 file changed, 19 insertions(+), 16 deletions(-) (limited to 'src/main/java/org/shedlang/compiler/backends') diff --git a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCompilerContext.java b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCompilerContext.java index 44f14d73..b24025b6 100644 --- a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCompilerContext.java +++ b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCompilerContext.java @@ -1,24 +1,28 @@ package org.shedlang.compiler.backends.llvm; -import org.pcollections.*; +import org.pcollections.Empty; +import org.pcollections.PMap; +import org.pcollections.PStack; +import org.pcollections.PVector; import org.shedlang.compiler.backends.llvm.ir.ast.LlvmGlobalDefinition; import org.shedlang.compiler.backends.llvm.ir.ast.LlvmTopLevelEntity; import org.shedlang.compiler.backends.llvm.ir.ast.instructions.LlvmInstruction; import org.shedlang.compiler.backends.llvm.ir.ast.operands.LlvmOperand; +import org.shedlang.compiler.util.POrderedMap; + +import java.util.Collection; public record LlvmCompilerContext( PStack stack, PMap locals, PVector instructions, - PVector topLevelEntities, - PSet topLevelEntityNames + POrderedMap topLevelEntitiesByName ) { public static LlvmCompilerContext EMPTY = new LlvmCompilerContext( Empty.stack(), Empty.map(), Empty.vector(), - Empty.vector(), - Empty.set() + POrderedMap.empty() ); public LlvmCompilerContext push(LlvmOperand operand) { @@ -26,8 +30,7 @@ public record LlvmCompilerContext( stack.plus(operand), locals, instructions, - topLevelEntities, - topLevelEntityNames + topLevelEntitiesByName ); } @@ -36,8 +39,7 @@ public record LlvmCompilerContext( stack.minus(0), locals, instructions, - topLevelEntities, - topLevelEntityNames + topLevelEntitiesByName ).toResult(stack.get(0)); } @@ -46,8 +48,7 @@ public record LlvmCompilerContext( stack, locals.plus(name, value), instructions, - topLevelEntities, - topLevelEntityNames + topLevelEntitiesByName ); } @@ -60,8 +61,7 @@ public record LlvmCompilerContext( stack, locals, instructions.plus(instruction), - topLevelEntities, - topLevelEntityNames + topLevelEntitiesByName ); } @@ -81,15 +81,14 @@ public record LlvmCompilerContext( stack, locals, instructions, - topLevelEntities.plus(globalDefinition), - topLevelEntityNames.plus(globalDefinition.name()) + topLevelEntitiesByName.plus(globalDefinition.name(), globalDefinition) ).toResult(globalDefinition); } private String generateTopLevelEntityName(String prefix) { var name = prefix; var nextSuffix = 1; - while (topLevelEntityNames.contains(name)) { + while (topLevelEntitiesByName().containsKey(name)) { name = prefix + "_" + nextSuffix; nextSuffix++; } @@ -99,4 +98,8 @@ public record LlvmCompilerContext( public LlvmCompilerResult toResult(T value) { return new LlvmCompilerResult<>(value, this); } + + public Collection topLevelEntities() { + return topLevelEntitiesByName.values(); + } } -- cgit v1.2.3