Skip to main content

Crate jvm_assembler

Crate jvm_assembler 

Source
Expand description

§JVM Assembler Library

jvm-assembler is a robust Rust library for processing Java Virtual Machine (JVM) bytecode and JASM (Java ASseMbler) assembly language. It provides a comprehensive toolkit for parsing, generating, and optimizing JVM Class files.

§Core Features

  • Program IR: A high-level, type-safe intermediate representation of JVM programs.
  • Class I/O: High-performance binary reader and writer for .class files.
  • JASM Support: Full support for the JASM assembly language, enabling human-readable bytecode manipulation.
  • Bytecode Analysis: Tools for control flow analysis and stack map frame generation.
  • Fluent Builder: (In builder module) A convenient API for programmatically constructing classes.

§Module Structure

  • program: The core IR model (JvmProgram, JvmMethod, etc.).
  • formats: Logic for reading/writing .class and .jasm files.
  • analyzer: Bytecode analysis and verification tools.
  • optimizer: Bytecode optimization passes.
  • builder: High-level API for class construction.
  • helpers: Internal utilities for JVM spec compliance.

§Usage Example

§Building and Writing a Class

use jvm_assembler::program::{JvmProgram, JvmMethod, JvmInstruction};
use jvm_assembler::formats::class::writer::ClassWriter;

fn main() -> jvm_assembler::Result<()> {
    // 1. Create a program model
    let mut program = JvmProgram::new("HelloWorld".into());
    
    // 2. Add a 'main' method
    program.methods.push(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, Rust!".into()),
            JvmInstruction::Invokevirtual("java/io/PrintStream".into(), "println".into(), "(Ljava/lang/String;)V".into()),
            JvmInstruction::Return,
        ]
    ));

    // 3. Serialize to binary
    let mut buffer = Vec::new();
    let writer = ClassWriter::new(&mut buffer);
    writer.write(&program).result?;
    
    std::fs::write("HelloWorld.class", buffer).expect("Failed to write file");
    Ok(())
}

§Error Handling

The library uses a diagnostic-oriented error handling system via GaiaDiagnostics<T>. This allows the library to report multiple warnings or non-fatal issues while still producing a result when possible.

let diagnostics = reader.read();
for diag in diagnostics.diagnostics {
    println!("Warning/Error: {:?}", diag);
}
let program = diagnostics.result?; // Returns Result<T, GaiaError>

Re-exports§

pub use crate::program::JvmConstantPoolEntry;
pub use crate::program::JvmField;
pub use crate::program::JvmInstruction;
pub use crate::program::JvmMethod;

Modules§

analyzer
JVM program analysis and stack map frame generation
builder
Fluent API for building JVM programs and methods
formats
Support for various JVM class file formats (binary, etc.)
helpers
Helper utilities for JVM bytecode manipulation
program
Core JVM program representation
validator
JVM bytecode validator

Structs§

GaiaError
Gaia error type, wrapping a specific error kind GaiaErrorKind

Type Aliases§

Result
Result type for this crate, using GaiaError as the error type