Class ConstantCopyOnAccessLoader

java.lang.Object
org.ek9lang.compiler.phase7.support.ConstantCopyOnAccessLoader

public final class ConstantCopyOnAccessLoader extends Object
THE shared "read a module-level constant" step - use this, do not re-derive a variant.

A named constant is NOT a local: it lives in module-level constant storage, so it must be read with an explicit LOAD_CONSTANT. Naming it as a bare operand instead (STORE _temp, Two) leaves the IR referencing a value that is declared nowhere in the function - no back-end can process that mechanically. The JVM back-end resolves the operand to a local slot, finds none, and emits a load of an unset slot (VerifyError: bad local variable type); the native back-end has the same problem with no SSA value or alloca of that name. The defect is in the IR, not in either back-end.

The load is always followed by a COPY via the copy constructor. Constants are shared objects, so every access hands out a fresh copy - otherwise a caller mutating what it read would silently change the constant for everything else in the module.

  • Constructor Details

    • ConstantCopyOnAccessLoader

      public ConstantCopyOnAccessLoader(IRGenerationContext stackContext)
  • Method Details

    • isCopyOnAccessConstant

      public boolean isCopyOnAccessConstant(ISymbol symbol)
      Does this symbol need the copy-on-access constant read?

      A literal-derived 'constant' is materialised in place by the literal loader, so it is excluded - only a declared, module-scope constant lives in constant storage.

      Parameters:
      symbol - The resolved symbol behind an identifier
      Returns:
      true if the symbol must be read via apply(ISymbol, String, DebugInfo)
    • apply

      public List<IRInstr> apply(ISymbol constantSymbol, String resultVariable, DebugInfo debugInfo)
      Read the constant into resultVariable as a fresh copy.
      Parameters:
      constantSymbol - The constant to read
      resultVariable - The variable to hold the fresh copy
      debugInfo - Debug information for the instructions
      Returns:
      LOAD_CONSTANT followed by the copy constructor call
    • applyToNewTemp

      public ConstantCopyOnAccessLoader.LoadedConstant applyToNewTemp(ISymbol constantSymbol, DebugInfo debugInfo)
      Read the constant into a freshly generated temp, returning both the instructions and that temp.

      For callers that do not already hold a result variable. The raw temp is allocated BEFORE the result temp, which is the historical ordering the IR goldens were captured with.

      Parameters:
      constantSymbol - The constant to read
      debugInfo - Debug information for the instructions
      Returns:
      The instructions and the temp now holding the fresh copy