Skip to content

Commit

Permalink
Attach surrounding static variable names to function entries
Browse files Browse the repository at this point in the history
  • Loading branch information
colinator27 committed Mar 1, 2025
1 parent cb88672 commit 7177f32
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
12 changes: 10 additions & 2 deletions Underanalyzer/Compiler/Bytecode/FunctionEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public sealed record FunctionEntry
/// </summary>
public bool DeclaredInRootScope { get; }

/// <summary>
/// Static variable name being assigned to around the function entry, if one exists; <see langword="null"/> otherwise.
/// </summary>
public string? StaticVariableName { get; }

/// <summary>
/// Kind of function entry. Useful for generating a final code entry name.
/// </summary>
Expand All @@ -63,14 +68,16 @@ public sealed record FunctionEntry
/// </summary>
public string? StructName { get; private set; }

internal FunctionEntry(FunctionEntry? parent, int bytecodeOffset, int localCount, int argumentCount, string? functionName, bool declaredInRootScope, FunctionEntryKind kind)
internal FunctionEntry(FunctionEntry? parent, int bytecodeOffset, int localCount, int argumentCount, string? functionName,
bool declaredInRootScope, string? staticVariableName, FunctionEntryKind kind)
{
Parent = parent;
BytecodeOffset = bytecodeOffset;
LocalCount = localCount;
ArgumentCount = argumentCount;
FunctionName = functionName;
DeclaredInRootScope = declaredInRootScope;
StaticVariableName = staticVariableName;
Kind = kind;
}

Expand All @@ -90,7 +97,8 @@ public void ResolveFunction(IGMFunction function, string childFunctionName)
}

/// <summary>
/// Resolves the struct name for this function entry. <see cref="Kind"/> must be <see cref="FunctionEntryKind.StructInstantiation"/>, and <see cref="StructName"/> must be <see langword="null"/> (as initialized).
/// Resolves the struct name for this function entry. <see cref="Kind"/> must be <see cref="FunctionEntryKind.StructInstantiation"/>,
/// and <see cref="StructName"/> must be <see langword="null"/> (as initialized).
/// </summary>
public void ResolveStructName(string name)
{
Expand Down
6 changes: 6 additions & 0 deletions Underanalyzer/Compiler/FunctionScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ public sealed class FunctionScope(bool isFunction)
/// </summary>
internal bool ProcessingBreakContinueContext { get; set; } = false;

/// <summary>
/// If generating code inside of a static variable assignment, this is the name of the
/// static variable being assigned to.
/// </summary>
internal string? StaticVariableName { get; set; } = null;

/// <summary>
/// List of nodes to duplicate when exiting early from a finally block.
/// One node per each try statement.
Expand Down
23 changes: 23 additions & 0 deletions Underanalyzer/Compiler/Nodes/BlockNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,27 @@ public void GenerateCode(BytecodeContext context)
statement.GenerateCode(context);
}
}

/// <summary>
/// Same as <see cref="GenerateCode(BytecodeContext)"/>, but for static initializer blocks.
/// </summary>
public void GenerateStaticCode(BytecodeContext context)
{
context.CurrentScope.GeneratingStaticBlock = true;
foreach (IASTNode statement in Children)
{
if (statement is AssignNode { Expression: SimpleVariableNode { VariableName: string staticName } } assign)
{
// Set new static name, used to assign to function entries
context.CurrentScope.StaticVariableName = staticName;
assign.GenerateCode(context);
context.CurrentScope.StaticVariableName = null;
}
else
{
statement.GenerateCode(context);
}
}
context.CurrentScope.GeneratingStaticBlock = false;
}
}
7 changes: 3 additions & 4 deletions Underanalyzer/Compiler/Nodes/FunctionDeclNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ public void GenerateCode(BytecodeContext context)
ArgumentNames.Count,
FunctionName,
oldScope == context.RootScope,
oldScope.StaticVariableName,
IsStruct ? FunctionEntryKind.StructInstantiation : FunctionEntryKind.FunctionDeclaration
);
context.CurrentFunctionEntry = entry;
Expand Down Expand Up @@ -481,10 +482,8 @@ public void GenerateCode(BytecodeContext context)
context.Emit(ExtendedOpcode.SetStaticInitialized);
}

// Compile block
Scope.GeneratingStaticBlock = true;
staticBlock.GenerateCode(context);
Scope.GeneratingStaticBlock = false;
// Compile block (as a static block, specifically)
staticBlock.GenerateStaticCode(context);

// If allowing re-entrant static, set the static flag here
if (allowReentrantStatic)
Expand Down

0 comments on commit 7177f32

Please sign in to comment.