1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#![feature(pointer_byte_offsets)]
#![feature(offset_of)]

use bytecode::decode_instruction;
use bytes::BytesMut;

use error::{Frame, Throwable, VMError};
use object::{
    builtins::{BuiltinString, Class, Object},
    loader::ClassLoader,
    mem::RefTo,
    runtime::RuntimeValue,
};
use parse::attributes::CodeAttribute;

use tracing::{debug, info, trace};

use crate::object::{
    builtins::Array,
    interner::intern_string,
    layout::types::{Bool, Int, Long},
    mem::HasObjectHeader,
};

pub mod bytecode;
pub mod error;
pub mod native;
pub mod object;

pub struct Context {
    pub code: CodeAttribute,
    pub class: RefTo<Class>,

    pub pc: i32,
    pub operands: Vec<RuntimeValue>,
    pub locals: Vec<RuntimeValue>,
}

pub struct VM {
    pub class_loader: ClassLoader,
    pub frames: Vec<Frame>,

    pub main_thread: RefTo<Object>,
}

#[derive(Debug)]
pub struct ThrownState {
    pub pc: i32,
}

impl VM {
    pub fn run(
        &mut self,
        mut ctx: Context,
    ) -> Result<Option<RuntimeValue>, (Throwable, ThrownState)> {
        while ctx.pc < ctx.code.code.len() as i32 {
            let slice = &ctx.code.code[ctx.pc as usize..];
            let consumed_bytes_prev = slice.len();

            let mut code_bytes = BytesMut::new();
            code_bytes.extend_from_slice(slice);

            let mut instruction_bytes = BytesMut::new();
            instruction_bytes.extend_from_slice(slice);

            let instruction = decode_instruction(self, &mut instruction_bytes, &ctx)
                .map_err(|e| (e, ThrownState { pc: ctx.pc }))?;

            let consumed_bytes_post = instruction_bytes.len();
            let bytes_consumed_by_opcode = (consumed_bytes_prev - consumed_bytes_post) as i32;
            trace!(
                "opcode: {:?} consumed {} bytes",
                instruction,
                bytes_consumed_by_opcode
            );

            let progression = instruction
                .handle(self, &mut ctx)
                .map_err(|e| (e, ThrownState { pc: ctx.pc }))?;

            match progression {
                bytecode::Progression::JumpAbs(new_pc) => {
                    debug!("Jumping from {} to {}", ctx.pc, new_pc);
                    ctx.pc = new_pc;
                }
                bytecode::Progression::JumpRel(offset) => {
                    debug!(
                        "Jumping from {} by {} (new: {})",
                        ctx.pc,
                        offset,
                        ctx.pc + offset
                    );
                    ctx.pc += offset;
                }
                bytecode::Progression::Next => {
                    debug!(
                        "Moving to next (jump by {} bytes)",
                        bytes_consumed_by_opcode
                    );
                    ctx.pc += bytes_consumed_by_opcode;
                }
                bytecode::Progression::Return(return_value) => {
                    debug!("Returning");
                    return Ok(return_value);
                }
                bytecode::Progression::Throw(err) => {
                    info!("Throwing {}", err);
                    return Err((err, ThrownState { pc: ctx.pc }));
                }
            };
        }

        Ok(None)
    }

    pub fn initialise_class(&mut self, class: RefTo<Class>) -> Result<(), Throwable> {
        let class_name = class.borrow().name().clone();

        if class.borrow().is_initialised() {
            debug!(
                "Not initialising {}, class is already initialised",
                class_name
            );

            return Ok(());
        }

        let clinit = class
            .borrow()
            .class_file()
            .methods
            .locate("<clinit>".to_string(), "()V".to_string())
            .cloned();

        if let Some(clinit) = clinit {
            debug!("Initialising {}", class_name);

            // Need to drop our lock on the class object before running the class initialiser
            // as it could call instructions which access class data
            class.borrow_mut().set_initialised(true);
            let code = clinit
                .attributes
                .known_attribute(&class.borrow().class_file().constant_pool)?;

            let ctx = Context {
                code,
                class,
                pc: 0,
                operands: vec![],
                locals: vec![],
            };

            // TODO: Handle this properly, as clinit errors should be caught
            self.run(ctx).map_err(|e| e.0)?;
            debug!("Finished initialising {}", class_name);
        } else {
            debug!("No clinit in {}", class_name);
            // Might as well mark this as initialised to avoid future
            // needless method lookups
            class.borrow_mut().set_initialised(true);
        }

        Ok(())
    }

    pub fn main_thread(&self) -> RefTo<Object> {
        self.main_thread.clone()
    }

    pub fn make_error(&mut self, ty: VMError) -> Result<Throwable, Throwable> {
        match ty {
            VMError::ArrayIndexOutOfBounds { .. } => {
                let cls = self.class_loader.for_name(ty.class_name().to_string())?;
                Ok(Throwable::Runtime(error::RuntimeException {
                    message: ty.message(),
                    ty: cls,
                    obj: RuntimeValue::null_ref(),
                    sources: self.frames.clone(),
                }))
            }
        }
    }

    pub fn bootstrap(&mut self) -> Result<(), Throwable> {
        // Load native modules
        use native::*;
        lang::LangClass::register(self)?;
        lang::Throwable::register(self)?;
        lang::StringUTF16::register(self)?;
        lang::Float::register(self)?;
        lang::LangDouble::register(self)?;
        lang::System::register(self)?;
        lang::LangObject::register(self)?;
        lang::Runtime::register(self)?;
        lang::LangThread::register(self)?;

        jdk::Cds::register(self)?;
        jdk::Reflection::register(self)?;
        jdk::SystemPropsRaw::register(self)?;
        jdk::Unsafe::register(self)?;
        jdk::JdkVm::register(self)?;
        jdk::ScopedMemoryAccess::register(self)?;
        jdk::Signal::register(self)?;

        io::FileDescriptor::register(self)?;
        io::FileOutputStream::register(self)?;

        let jls = self.class_loader.for_name("java/lang/String".to_string())?;
        // Init String so that we can set the static after it's done. The clinit sets it to a default.
        self.initialise_class(jls.clone())?;

        jls.borrow_mut()
            .static_field_info_mut(("COMPACT_STRINGS".to_string(), "Z".to_string()))
            .unwrap()
            .value = Some(RuntimeValue::Integral(0_i32.into()));

        // Init thread
        let thread_class = self.class_loader.for_name("java/lang/Thread".to_string())?;
        // self.initialise_class(thread_class.clone())?;

        let thread_group_class = self
            .class_loader
            .for_name("java/lang/ThreadGroup".to_string())?;
        // self.initialise_class(thread_class.clone())?;

        let thread = BuiltinThread {
            object: Object {
                class: thread_class.clone(),
                super_class: thread_class.borrow().super_class(),
                ref_count: 0,
            },
            name: intern_string("main".to_string())?,
            priority: 0,
            daemon: 0,
            interrupted: 0,
            stillborn: 0,
            eetop: 0,
            target: RefTo::null(),
            thread_group: RefTo::new(BuiltinThreadGroup {
                object: Object {
                    class: thread_group_class.clone(),
                    super_class: thread_group_class.borrow().super_class(),
                    ref_count: 0,
                },
                parent: RefTo::null(),
                name: intern_string("main".to_string())?,
                max_priority: 0,
                destroyed: 0,
                daemon: 0,
                n_unstarted_threads: 0,
                n_threads: 0,
                threads: RefTo::null(),
                n_groups: 0,
                groups: RefTo::null(),
            }),
            context_class_loader: RefTo::null(),
            inherited_access_control_context: RefTo::null(),
            thread_locals: RefTo::null(),
            inheritable_thread_locals: RefTo::null(),
            stack_size: 0,
            tid: 1,
            status: 1,
            park_blocker: RefTo::null(),
            uncaught_exception_handler: RefTo::null(),
            thread_local_random_seed: 0,
            thread_local_random_probe: 0,
            thread_local_random_secondary_seed: 0,
        };
        let thread_ref = RefTo::new(thread);
        self.main_thread = thread_ref.erase();

        Ok(())
    }
}

#[repr(C)]
#[derive(Debug)]
struct BuiltinThread {
    object: Object,

    name: RefTo<BuiltinString>,
    priority: Int,
    daemon: Bool,
    interrupted: Bool,
    stillborn: Bool,
    eetop: Long,
    target: RefTo<Object>,
    thread_group: RefTo<BuiltinThreadGroup>,
    context_class_loader: RefTo<Object>,
    inherited_access_control_context: RefTo<Object>,
    thread_locals: RefTo<Object>,
    inheritable_thread_locals: RefTo<Object>,
    stack_size: Long,
    tid: Long,
    status: Int,
    park_blocker: RefTo<Object>,
    uncaught_exception_handler: RefTo<Object>,

    thread_local_random_seed: Int,
    thread_local_random_probe: Int,
    thread_local_random_secondary_seed: Int,
}

impl HasObjectHeader<BuiltinThread> for BuiltinThread {
    fn header(&self) -> &Object {
        &self.object
    }

    fn header_mut(&mut self) -> &mut Object {
        &mut self.object
    }
}
#[repr(C)]
#[derive(Debug)]
struct BuiltinThreadGroup {
    object: Object,

    parent: RefTo<BuiltinThreadGroup>,
    name: RefTo<BuiltinString>,
    max_priority: Int,
    destroyed: Bool,
    daemon: Bool,
    n_unstarted_threads: Int,

    n_threads: Int,
    threads: RefTo<Array<RefTo<Object>>>,

    n_groups: Int,
    groups: RefTo<Array<RefTo<Object>>>,
}

impl HasObjectHeader<BuiltinThreadGroup> for BuiltinThreadGroup {
    fn header(&self) -> &Object {
        &self.object
    }

    fn header_mut(&mut self) -> &mut Object {
        &mut self.object
    }
}

#[cfg(test)]
mod tests {}