Expand description
Core JVM program representation
§JVM Program Model
This module defines the abstract representation (Intermediate Representation) of a JVM program. It serves as the central data structure that links various formats (Class binary, JASM assembly) and tools (Analyzer, Optimizer).
§Core Components
- JvmProgram: The top-level structure representing a single JVM class, including its name, superclass, interfaces, fields, methods, and attributes.
- JvmMethod: Represents a method within a class, containing its descriptor, access flags, bytecode instructions, and exception handlers.
- JvmField: Represents a field within a class, including its type, initial value, and attributes.
- JvmInstruction: A comprehensive enum covering all JVM opcodes, using high-level symbolic representations (e.g., using strings for class names instead of constant pool indices).
- JvmConstantPool: A managed constant pool that handles deduplication and indexing automatically during serialization.
§Key Features
- Type Safety: Uses Rust’s type system to ensure valid instruction parameters and class structures.
- Symbolic Representation: Instructions use human-readable names and descriptors, which are resolved to constant pool indices only during the final writing phase.
- Attribute Support: Support for standard JVM attributes like
Code,LineNumberTable,LocalVariableTable,StackMapTable, etc.
§Usage Example
use jvm_assembler::program::{JvmProgram, JvmMethod, JvmInstruction};
// Create a new program (class)
let mut program = JvmProgram::new("com/example/MyClass".into());
program.super_class = Some("java/lang/Object".into());
// Add a method
let mut main_method = JvmMethod::new(
"main".into(),
"([Ljava/lang/String;)V".into(),
vec![
JvmInstruction::Getstatic("java/lang/System".into(), "out".into(), "Ljava/io/PrintStream;".into()),
JvmInstruction::LdcString("Hello from IR!".into()),
JvmInstruction::Invokevirtual("java/io/PrintStream".into(), "println".into(), "(Ljava/lang/String;)V".into()),
JvmInstruction::Return,
]
);
main_method.access_flags = 0x0009; // public static
program.methods.push(main_method);Re-exports§
pub use entities::*;pub use instructions::*;pub use legacy::*;pub use pool::*;pub use types::*;
Modules§
- entities
- JVM program entity definitions
- instructions
- JVM instruction definitions
- legacy
- Legacy compatibility layer
- pool
- Constant pool management
- types
- Core JVM types and enumerations