From 326e8c13f777ca6583f11e1be39a3025eec08cf4 Mon Sep 17 00:00:00 2001 From: Joe Savona Date: Thu, 16 Feb 2023 15:17:30 -0800 Subject: [PATCH] Scaffolding for LoadGlobal instruction Adds a new `LoadGlobal` InstructionValue variant which will be used to represent identifiers that refer to globals. We don't construct this value type yet. --- compiler/forget/src/HIR/HIR.ts | 1 + compiler/forget/src/HIR/PrintHIR.ts | 4 ++++ compiler/forget/src/HIR/visitors.ts | 2 ++ compiler/forget/src/Inference/InferReferenceEffects.ts | 1 + compiler/forget/src/Optimization/DeadCodeElimination.ts | 1 + compiler/forget/src/ReactiveScopes/CodegenReactiveFunction.ts | 4 ++++ .../forget/src/ReactiveScopes/InferReactiveScopeVariables.ts | 1 + 7 files changed, 14 insertions(+) diff --git a/compiler/forget/src/HIR/HIR.ts b/compiler/forget/src/HIR/HIR.ts index d6abb1cf8e..2733601827 100644 --- a/compiler/forget/src/HIR/HIR.ts +++ b/compiler/forget/src/HIR/HIR.ts @@ -442,6 +442,7 @@ export type InstructionData = | { kind: "ComputedStore"; object: Place; property: Place; value: Place } // load `object[index]` - like PropertyLoad but with a dynamic property | { kind: "ComputedLoad"; object: Place; property: Place } + | { kind: "LoadGlobal"; name: string } | FunctionExpression | { kind: "TaggedTemplateExpression"; diff --git a/compiler/forget/src/HIR/PrintHIR.ts b/compiler/forget/src/HIR/PrintHIR.ts index 05164b718c..a34d79bc35 100644 --- a/compiler/forget/src/HIR/PrintHIR.ts +++ b/compiler/forget/src/HIR/PrintHIR.ts @@ -381,6 +381,10 @@ export function printInstructionValue(instrValue: ReactiveValue): string { value += instrValue.quasis.at(-1)!.raw + "`"; break; } + case "LoadGlobal": { + value = `Global ${instrValue.name}`; + break; + } default: { assertExhaustive( instrValue, diff --git a/compiler/forget/src/HIR/visitors.ts b/compiler/forget/src/HIR/visitors.ts index 0d376e66fc..edad1e8ca6 100644 --- a/compiler/forget/src/HIR/visitors.ts +++ b/compiler/forget/src/HIR/visitors.ts @@ -131,6 +131,7 @@ export function* eachInstructionValueOperand( yield* instrValue.subexprs; break; } + case "LoadGlobal": case "UnsupportedNode": case "Primitive": case "JSXText": { @@ -259,6 +260,7 @@ export function mapInstructionOperands( instrValue.subexprs = instrValue.subexprs.map(fn); break; } + case "LoadGlobal": case "UnsupportedNode": case "Primitive": case "JSXText": { diff --git a/compiler/forget/src/Inference/InferReferenceEffects.ts b/compiler/forget/src/Inference/InferReferenceEffects.ts index c239f1a389..93d3fce9ca 100644 --- a/compiler/forget/src/Inference/InferReferenceEffects.ts +++ b/compiler/forget/src/Inference/InferReferenceEffects.ts @@ -640,6 +640,7 @@ function inferBlock( effectKind = Effect.Read; break; } + case "LoadGlobal": case "JSXText": case "Primitive": { valueKind = ValueKind.Immutable; diff --git a/compiler/forget/src/Optimization/DeadCodeElimination.ts b/compiler/forget/src/Optimization/DeadCodeElimination.ts index 41af43e617..1addf063f7 100644 --- a/compiler/forget/src/Optimization/DeadCodeElimination.ts +++ b/compiler/forget/src/Optimization/DeadCodeElimination.ts @@ -94,6 +94,7 @@ function pruneableValue(value: InstructionValue): boolean { // Potentially safe to prune, since they should just be creating new values return false; } + case "LoadGlobal": case "ArrayExpression": case "BinaryExpression": case "ComputedLoad": diff --git a/compiler/forget/src/ReactiveScopes/CodegenReactiveFunction.ts b/compiler/forget/src/ReactiveScopes/CodegenReactiveFunction.ts index 47ade2dbeb..2705d39233 100644 --- a/compiler/forget/src/ReactiveScopes/CodegenReactiveFunction.ts +++ b/compiler/forget/src/ReactiveScopes/CodegenReactiveFunction.ts @@ -788,6 +788,10 @@ function codegenInstructionValue( ); break; } + case "LoadGlobal": { + value = t.identifier(instrValue.name); + break; + } default: { assertExhaustive( instrValue, diff --git a/compiler/forget/src/ReactiveScopes/InferReactiveScopeVariables.ts b/compiler/forget/src/ReactiveScopes/InferReactiveScopeVariables.ts index 1c7a5d5acf..9c67fe1506 100644 --- a/compiler/forget/src/ReactiveScopes/InferReactiveScopeVariables.ts +++ b/compiler/forget/src/ReactiveScopes/InferReactiveScopeVariables.ts @@ -168,6 +168,7 @@ function isMutable({ id }: Instruction, place: Place): boolean { function mayAllocate(value: InstructionValue): boolean { switch (value.kind) { + case "LoadGlobal": case "TypeCastExpression": case "BinaryExpression": case "Identifier":