Class AbstractAsmGenerator
java.lang.Object
org.ek9lang.compiler.backend.jvm.AbstractAsmGenerator
- Direct Known Subclasses:
BranchInstrAsmGenerator, CallInstrAsmGenerator, LabelInstrAsmGenerator, LiteralInstrAsmGenerator, MemoryInstrAsmGenerator, ScopeInstrAsmGenerator
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static class
Metadata for a local variable needed for LocalVariableTable generation.static class
Shared context for a method that must be coordinated across all generators.(package private) static class
Scope boundary information for tracking variable lifetimes.(package private) static class
Represents the source of a temp variable for stack-oriented code generation. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final org.objectweb.asm.ClassWriter
protected final ConstructTargetTuple
protected final OutputVisitor
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
AbstractAsmGenerator
(ConstructTargetTuple constructTargetTuple, OutputVisitor outputVisitor, org.objectweb.asm.ClassWriter classWriter) -
Method Summary
Modifier and TypeMethodDescriptionprotected String
convertToJvmDescriptor
(String ek9TypeName) Convert EK9 type name to JVM descriptor format.protected String
convertToJvmName
(String ek9FullyQualifiedName) Convert EK9 fully qualified name to JVM internal name format.protected void
generateBooleanLiteral
(String literalValue) Generate EK9 Boolean literal from string value.protected void
generateCharacterLiteral
(String literalValue) Generate EK9 Character literal from string value.protected void
generateDebugInfo
(DebugInfo debugInfo) Generate debug line number from EK9 debug info if available.protected void
generateFloatLiteral
(String literalValue) Generate EK9 Float literal from string value.protected void
generateIntegerLiteral
(String literalValue) Generate EK9 Integer literal from string value.protected void
generateLoadVariable
(String variableName) Generate common JVM instructions for loading a variable.protected void
Generate LocalVariableTable entries for all variables tracked during method processing.protected String
generateMethodDescriptor
(List<String> parameterTypes, String returnType) Generate method descriptor from EK9 parameter and return types.protected void
generateObjectLiteral
(String literalValue, String literalType) Generate EK9 type literal from string value for generic/object types.protected void
generateStackOperation
(String variableName) Generate stack operation for a variable - either load from slot or re-generate the operation.protected void
generateStoreVariable
(String variableName) Generate common JVM instructions for storing to a variable.protected void
generateStringLiteral
(String literalValue) Generate EK9 String literal from string value.protected org.objectweb.asm.MethodVisitor
Get the current method visitor.protected AbstractAsmGenerator.MethodContext
Get the shared method context.protected org.objectweb.asm.Label
getOrCreateLabel
(String labelName) Get or create JVM Label for IR label name.protected int
getVariableIndex
(String variableName) Get or assign local variable slot for a variable name.protected boolean
isVariableAllocated
(String variableName) Check if a variable has already been allocated a local variable slot.protected void
setCurrentMethodVisitor
(org.objectweb.asm.MethodVisitor methodVisitor) Set the current method visitor for instruction generation.protected void
Set shared method context for coordinated variable slot allocation across generators.protected void
trackTempVariableFromLiteral
(String tempVar, String literalValue, String literalType) Track that a temp variable was created from a literal value.protected void
trackTempVariableFromLoad
(String tempVar, String sourceVar) Track that a temp variable was created from loading another variable.
-
Field Details
-
constructTargetTuple
-
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
-
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
-
convertToJvmDescriptor
-
generateDebugInfo
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
Generate common JVM instructions for loading a variable. -
generateStoreVariable
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
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
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
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
-
trackTempVariableFromLiteral
-
generateStackOperation
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
Generate EK9 String literal from string value. -
generateIntegerLiteral
Generate EK9 Integer literal from string value. -
generateBooleanLiteral
Generate EK9 Boolean literal from string value. -
generateFloatLiteral
Generate EK9 Float literal from string value. -
generateCharacterLiteral
Generate EK9 Character literal from string value. -
generateObjectLiteral
-
generateMethodDescriptor