Skip to main content

jvm_assembler/program/
entities.rs

1use super::{
2    instructions::JvmInstruction,
3    pool::JvmConstantPool,
4    types::{JvmAccessFlags, JvmAttribute, JvmField, JvmVersion},
5};
6
7/// High-level representation of a JVM program
8#[derive(Debug, Clone)]
9pub struct JvmProgram {
10    /// Program name (class name)
11    pub name: String,
12    /// Access flags
13    pub access_flags: JvmAccessFlags,
14    /// Super class name
15    pub super_class: Option<String>,
16    /// List of implemented interfaces
17    pub interfaces: Vec<String>,
18    /// List of fields
19    pub fields: Vec<JvmField>,
20    /// List of methods
21    pub methods: Vec<JvmMethod>,
22    /// List of attributes
23    pub attributes: Vec<JvmAttribute>,
24    /// Constant pool (high-level representation)
25    pub constant_pool: JvmConstantPool,
26    /// Version information
27    pub version: JvmVersion,
28    /// Source file information
29    pub source_file: Option<String>,
30}
31
32impl JvmProgram {
33    /// Creates a new JVM program with given name
34    pub fn new(name: String) -> Self {
35        Self {
36            name,
37            access_flags: JvmAccessFlags::public(),
38            super_class: None,
39            interfaces: Vec::new(),
40            fields: Vec::new(),
41            methods: Vec::new(),
42            attributes: Vec::new(),
43            constant_pool: JvmConstantPool::new(),
44            version: JvmVersion::java_8(),
45            source_file: None,
46        }
47    }
48
49    /// Sets JVM version
50    pub fn set_version(&mut self, major: u16, minor: u16) {
51        self.version = JvmVersion::new(major, minor);
52    }
53
54    /// Sets super class name
55    pub fn set_super_class(&mut self, super_class: String) {
56        self.super_class = Some(super_class);
57    }
58
59    /// Adds an interface to the program
60    pub fn add_interface(&mut self, interface: String) {
61        self.interfaces.push(interface);
62    }
63
64    /// Adds a method to the program
65    pub fn add_method(&mut self, method: JvmMethod) {
66        self.methods.push(method);
67    }
68
69    /// Adds a field to the program
70    pub fn add_field(&mut self, field: JvmField) {
71        self.fields.push(field);
72    }
73
74    /// Adds an attribute to the program
75    pub fn add_attribute(&mut self, attribute: JvmAttribute) {
76        self.attributes.push(attribute);
77    }
78
79    /// Sets source file name and adds SourceFile attribute
80    pub fn set_source_file(&mut self, filename: String) {
81        self.source_file = Some(filename.clone());
82        self.add_attribute(JvmAttribute::SourceFile { filename });
83    }
84
85    /// Validates the program
86    pub fn validate(&self) -> bool {
87        // Basic validation logic
88        if self.name.is_empty() {
89            return false;
90        }
91        true
92    }
93}
94
95/// JVM method information
96#[derive(Debug, Clone)]
97pub struct JvmMethod {
98    /// Method name
99    pub name: String,
100    /// Method descriptor
101    pub descriptor: String,
102    /// Access flags
103    pub access_flags: JvmAccessFlags,
104    /// Method instructions
105    pub instructions: Vec<JvmInstruction>,
106    /// Maximum stack size
107    pub max_stack: u16,
108    /// Maximum local variables
109    pub max_locals: u16,
110    /// Exception handler table
111    pub exception_handlers: Vec<JvmExceptionHandler>,
112    /// Exceptions thrown by the method
113    pub exceptions: Vec<String>,
114    /// Method attributes
115    pub attributes: Vec<JvmAttribute>,
116}
117
118impl JvmMethod {
119    /// Creates a new JVM method
120    pub fn new(name: String, descriptor: String) -> Self {
121        Self {
122            name,
123            descriptor,
124            access_flags: JvmAccessFlags::public(),
125            instructions: Vec::new(),
126            max_stack: 0,
127            max_locals: 0,
128            exception_handlers: Vec::new(),
129            exceptions: Vec::new(),
130            attributes: Vec::new(),
131        }
132    }
133
134    /// Adds an instruction to the method
135    pub fn add_instruction(&mut self, inst: JvmInstruction) {
136        self.instructions.push(inst);
137    }
138
139    /// Adds an exception handler to the method
140    pub fn add_exception_handler(&mut self, handler: JvmExceptionHandler) {
141        self.exception_handlers.push(handler);
142    }
143
144    /// Adds an exception to the method
145    pub fn add_exception(&mut self, exception: String) {
146        self.exceptions.push(exception);
147    }
148
149    /// Adds an attribute to the method
150    pub fn add_attribute(&mut self, attribute: JvmAttribute) {
151        self.attributes.push(attribute);
152    }
153}
154
155/// JVM exception handler
156#[derive(Debug, Clone)]
157pub struct JvmExceptionHandler {
158    /// Start label
159    pub start_label: String,
160    /// End label
161    pub end_label: String,
162    /// Handler label
163    pub handler_label: String,
164    /// Catch type (class name)
165    pub catch_type: Option<String>,
166}