Class AbstractAsmGenerator

java.lang.Object
org.ek9lang.compiler.backend.jvm.AbstractAsmGenerator
Direct Known Subclasses:
BranchInstrAsmGenerator, CallInstrAsmGenerator, LabelInstrAsmGenerator, LiteralInstrAsmGenerator, MemoryInstrAsmGenerator, ScopeInstrAsmGenerator

public abstract class AbstractAsmGenerator extends Object
Common abstract base class for all specialized ASM generators. Provides shared functionality for JVM bytecode generation including: - Access to ClassWriter and current MethodVisitor - EK9 to JVM name conversion using existing utilities - Debug information handling - Common JVM instruction patterns
  • Field Details

    • constructTargetTuple

      protected final ConstructTargetTuple constructTargetTuple
    • outputVisitor

      protected final OutputVisitor outputVisitor
    • classWriter

      protected final org.objectweb.asm.ClassWriter classWriter
  • Constructor Details

    • AbstractAsmGenerator

      protected AbstractAsmGenerator(ConstructTargetTuple constructTargetTuple, OutputVisitor outputVisitor, org.objectweb.asm.ClassWriter classWriter)
  • Method Details

    • setSharedMethodContext

      protected void setSharedMethodContext(AbstractAsmGenerator.MethodContext context)
      Set shared method context for coordinated variable slot allocation across generators. This MUST be called before processing instructions to ensure all generators use the same context.
    • getMethodContext

      protected AbstractAsmGenerator.MethodContext getMethodContext()
      Get the shared method context. Provides access to variable maps, label maps, and local variable metadata.
    • setCurrentMethodVisitor

      protected void setCurrentMethodVisitor(org.objectweb.asm.MethodVisitor methodVisitor)
      Set the current method visitor for instruction generation. Note: Does NOT reset variable maps - that should be done by the caller if needed.
    • getCurrentMethodVisitor

      protected org.objectweb.asm.MethodVisitor getCurrentMethodVisitor()
      Get the current method visitor.
    • convertToJvmName

      protected String convertToJvmName(String ek9FullyQualifiedName)
      Convert EK9 fully qualified name to JVM internal name format. Uses existing FullyQualifiedJvmName utility.
      Parameters:
      ek9FullyQualifiedName - EK9 name like "org.ek9.lang::String"
      Returns:
      JVM internal name like "org/ek9/lang/String"
    • convertToJvmDescriptor

      protected String convertToJvmDescriptor(String ek9TypeName)
      Convert EK9 type name to JVM descriptor format. Uses FullyQualifiedJvmName utility for consistent conversion.
    • generateDebugInfo

      protected void generateDebugInfo(DebugInfo debugInfo)
      Generate debug line number from EK9 debug info if available. Creates a JVM Label to mark the bytecode position and associates it with the source line number.
    • generateLocalVariableTable

      protected void generateLocalVariableTable()
      Generate LocalVariableTable entries for all variables tracked during method processing. Must be called after method body processing but before visitMaxs().

      LocalVariableTable enables jdb debugger to show variable names and values via 'locals' command. Each entry maps: variable name + type descriptor + scope (start/end labels) + JVM slot.

      Handles two cases: 1. Method parameters: Have REFERENCE but no SCOPE_REGISTER (caller-owned memory) 2. Local variables: Have REFERENCE + SCOPE_REGISTER (callee-owned memory)

    • generateLoadVariable

      protected void generateLoadVariable(String variableName)
      Generate common JVM instructions for loading a variable.
    • generateStoreVariable

      protected void generateStoreVariable(String variableName)
      Generate common JVM instructions for storing to a variable. In stack-oriented approach, temp variables that will be consumed immediately should not be stored to local variables at all.
    • getVariableIndex

      protected int getVariableIndex(String variableName)
      Get or assign local variable slot for a variable name. Uses opaque identifier mapping - treats variable names as unique identifiers and assigns sequential slots per method (1, 2, 3, 4, etc.) regardless of the actual name.

      Example: _temp1 -> slot 1, _temp10 -> slot 2, _temp3 -> slot 3 (in order encountered)

    • isVariableAllocated

      protected boolean isVariableAllocated(String variableName)
      Check if a variable has already been allocated a local variable slot. Returns true for method parameters (pre-registered) and already-declared local variables. This is used to distinguish parameters from new local variable declarations.
    • getOrCreateLabel

      protected org.objectweb.asm.Label getOrCreateLabel(String labelName)
      Get or create JVM Label for IR label name. Reuses the same Label object for both label definition (LABEL instruction) and branch targets (BRANCH, BRANCH_TRUE, BRANCH_FALSE instructions).
    • trackTempVariableFromLoad

      protected void trackTempVariableFromLoad(String tempVar, String sourceVar)
      Track that a temp variable was created from loading another variable.
    • trackTempVariableFromLiteral

      protected void trackTempVariableFromLiteral(String tempVar, String literalValue, String literalType)
      Track that a temp variable was created from a literal value.
    • generateStackOperation

      protected void generateStackOperation(String variableName)
      Generate stack operation for a variable - either load from slot or re-generate the operation. This is the key method for stack-oriented code generation.
    • generateStringLiteral

      protected void generateStringLiteral(String literalValue)
      Generate EK9 String literal from string value.
    • generateIntegerLiteral

      protected void generateIntegerLiteral(String literalValue)
      Generate EK9 Integer literal from string value.
    • generateBooleanLiteral

      protected void generateBooleanLiteral(String literalValue)
      Generate EK9 Boolean literal from string value.
    • generateFloatLiteral

      protected void generateFloatLiteral(String literalValue)
      Generate EK9 Float literal from string value.
    • generateCharacterLiteral

      protected void generateCharacterLiteral(String literalValue)
      Generate EK9 Character literal from string value.
    • generateObjectLiteral

      protected void generateObjectLiteral(String literalValue, String literalType)
      Generate EK9 type literal from string value for generic/object types. This handles complex literals that don't fit standard primitive categories.
    • generateMethodDescriptor

      protected String generateMethodDescriptor(List<String> parameterTypes, String returnType)
      Generate method descriptor from EK9 parameter and return types.