Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Serial-ATA committed Nov 16, 2024
1 parent b263f13 commit c002dbb
Show file tree
Hide file tree
Showing 44 changed files with 577 additions and 296 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ symbols = { path = "symbols" }
byte-slice-cast = "1.2.2"
clap = "4.5.20"
const_format = "0.2.30"
libc = "0.2"
libloading = "0.8.5"
tracing = "0.1.40"
once_cell = "1.17.1"
Expand Down
8 changes: 4 additions & 4 deletions generators/native_methods/src/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ fn generate_methods_for_class(class: &Class, definitions_file: &mut File) {
let mut method_call = String::new();
write!(
method_call,
"\t\tsuper::{}(env,{}",
"super::{}(env,{}",
method.name(),
if is_static { "" } else { "this," }
)
Expand All @@ -113,18 +113,18 @@ fn generate_methods_for_class(class: &Class, definitions_file: &mut File) {
match method.return_ty {
Type::Void => {
// Cannot implement From<()> for NativeReturn, need a special case for void returns
writeln!(definitions_file, "{};\n\t\tNone\n\t}}", method_call).unwrap();
writeln!(definitions_file, "\t\t{};\n\t\tNone\n\t}}", method_call).unwrap();
},
Type::Class(_) | Type::Array(_) => {
writeln!(
definitions_file,
"Some(instructions::Operand::Reference({}))\n\t}}",
"\t\tSome(instructions::Operand::Reference({}))\n\t}}",
method_call
)
.unwrap();
},
_ => {
writeln!(definitions_file, "Some({}.into())\n\t}}", method_call).unwrap();
writeln!(definitions_file, "\t\tSome({}.into())\n\t}}", method_call).unwrap();
},
}
}
Expand Down
1 change: 0 additions & 1 deletion platform/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ platform
└── family (`target_family` specific definitions)
└── <family>
└── <os> (`target_os` specific definitions)
└── os_arch (`target_os` + `target_arch` specific definitions)
```
4 changes: 2 additions & 2 deletions platform/src/arch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ match_cfg_meta! {
pub use x86::*;
},
"x86_64" => {
mod x86_64;
pub use x86_64::*;
mod x86;
pub use x86::*;
},
_ => {
compile_error!("target architecture is not supported!");
Expand Down
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions platform/src/arch/x86/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod features;
pub mod ordering;
File renamed without changes.
Empty file.
1 change: 0 additions & 1 deletion platform/src/arch/x86_64/mod.rs

This file was deleted.

2 changes: 0 additions & 2 deletions platform/src/family/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ match_cfg_meta! {
match cfg(target_family) {
"unix" => {
mod unix;
pub use unix::os_arch;
},
"windows" => {
mod windows;
pub use windows::os_arch;
},
_ => {
compile_error!("target family is not supported!");
Expand Down
1 change: 0 additions & 1 deletion platform/src/family/unix/linux/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
pub mod os_arch;
pub mod properties;
21 changes: 0 additions & 21 deletions platform/src/family/unix/linux/os_arch/mod.rs

This file was deleted.

1 change: 0 additions & 1 deletion platform/src/family/unix/linux/os_arch/x86/mod.rs

This file was deleted.

1 change: 0 additions & 1 deletion platform/src/family/unix/macos/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
pub mod properties;
pub mod os_arch;
Empty file.
6 changes: 0 additions & 6 deletions platform/src/family/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,9 @@ match_cfg_meta! {
match cfg(target_os) {
"linux" => {
mod linux;

/// Items for specific OS + architecture combinations
pub use linux::os_arch as os_arch;
},
"macos" => {
mod macos;

/// Items for specific OS + architecture combinations
pub use macos::os_arch as os_arch;
},
_ => {
compile_error!("target OS is not supported!");
Expand Down
Empty file.
4 changes: 3 additions & 1 deletion runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ classfile = { path = "../classfile" }
common = { path = "../common" }
instructions = { path = "../instructions" }
jimage = { path = "../jimage" }
jni = { path = "../jni" }

bytemuck = "1.13.0"
byte-slice-cast = { workspace = true }
jni = { path = "../jni" }
tracing = { workspace = true }
libc.workspace = true
num_cpus = "1.15.0"
paste = { workspace = true }
platform = { path = "../platform" }
Expand Down
25 changes: 21 additions & 4 deletions runtime/src/classpath/classloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,31 @@ impl ClassLoader {
// Otherwise, the following steps are performed to create C:
//
// If the component type is a reference type, the algorithm of this section (§5.3) is applied recursively using L in order to load and thereby create the component type of C.
let component = FieldType::parse(&mut descriptor.as_str().as_bytes()).unwrap(); // TODO: Error handling
let mut descriptor_str = descriptor.as_str();
let array = FieldType::parse(&mut descriptor_str.as_bytes()).unwrap(); // TODO: Error handling
let FieldType::Array(mut component) = array else {
unreachable!("The descriptor was validated as an array prior");
};

loop {
if let FieldType::Object(obj) = &*component {
self.load(Symbol::intern_bytes(&obj));
break;
}

if let FieldType::Array(array) = *component {
// Just strip '[' until we finally reach the component type.
descriptor_str = &descriptor_str[1..];
self.load(Symbol::intern(descriptor_str));
component = array;
continue;
}

if let FieldType::Object(ref obj) = component {
self.load(Symbol::intern_bytes(&obj));
break;
}

// The Java Virtual Machine creates a new array class with the indicated component type and number of dimensions.
let array_class = unsafe { Class::new_array(descriptor, component, self) };
let array_class = unsafe { Class::new_array(descriptor, *component, self) };

// If the component type is a reference type, the Java Virtual Machine marks C to have the defining loader of the component type as its defining loader.
// Otherwise, the Java Virtual Machine marks C to have the bootstrap class loader as its defining loader.
Expand Down
33 changes: 23 additions & 10 deletions runtime/src/frame.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::method::Method;
use crate::stack::local_stack::LocalStack;
use crate::stack::operand_stack::OperandStack;
use crate::thread::ThreadRef;
use crate::thread::JavaThread;

use std::cell::UnsafeCell;
use std::fmt::{Debug, Formatter};
use std::sync::atomic::{AtomicIsize, Ordering};
use std::sync::Arc;
Expand All @@ -11,6 +12,7 @@ use classfile::ConstantPoolRef;
use common::int_types::{s1, s2, s4, u1, u2, u4};
use common::traits::PtrType;

// TODO: Make these fields private
// https://docs.oracle.com/javase/specs/jvms/se19/html/jvms-2.html#jvms-2.6
#[rustfmt::skip]
pub struct Frame {
Expand All @@ -23,7 +25,7 @@ pub struct Frame {
// and a reference to the run-time constant pool (§2.5.5)
pub constant_pool: ConstantPoolRef,
pub method: &'static Method,
pub thread: ThreadRef,
pub thread: UnsafeCell<*mut JavaThread>,

// Used to remember the last pc when we return to a frame after a method invocation
pub cached_pc: AtomicIsize,
Expand All @@ -49,8 +51,12 @@ impl FrameRef {
Self(Arc::new(ptr))
}

pub fn thread(&self) -> ThreadRef {
Arc::clone(&self.0.get().thread)
pub fn thread(&self) -> &'static JavaThread {
unsafe { &**self.0.get().thread.get() }
}

pub fn thread_mut(&self) -> &'static mut JavaThread {
unsafe { &mut **self.0.get().thread.get() }
}

pub fn method(&self) -> &Method {
Expand All @@ -66,10 +72,13 @@ impl FrameRef {
}

pub fn read_byte(&self) -> u1 {
let frame = self.0.get_mut();
let thread = frame.thread.get();
let pc;
{
let thread = self.thread();
pc = thread.pc.fetch_add(1, Ordering::Relaxed);
}

let pc = thread.pc.fetch_add(1, Ordering::Relaxed);
let frame = self.0.get_mut();
frame.method.code.code[pc as usize]
}

Expand Down Expand Up @@ -102,8 +111,7 @@ impl FrameRef {
}

pub fn skip_padding(&self) {
let frame = self.0.get_mut();
let thread = frame.thread.get();
let thread = self.thread();

let mut pc = thread.pc.load(Ordering::Relaxed);
while pc % 4 != 0 {
Expand All @@ -114,8 +122,13 @@ impl FrameRef {
}

pub fn stash_pc(&self) {
let current_pc;
{
let thread = self.thread();
current_pc = thread.pc.load(Ordering::Relaxed);
}

let frame = self.0.get_mut();
let current_pc = frame.thread.get().pc.load(Ordering::Relaxed);
frame.cached_pc = AtomicIsize::from(current_pc);
}

Expand Down
1 change: 1 addition & 0 deletions runtime/src/globals/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ define_classes!(
java_lang_ThreadGroup,
java_lang_Throwable,
java_lang_Cloneable,
java_io_FileDescriptor,
// Primitive arrays
bool_array,
byte_array,
Expand Down
12 changes: 12 additions & 0 deletions runtime/src/globals/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod modules;
pub mod threads;

use classfile::FieldType;
use symbols::{sym, Symbol};

pub const TYPES: &[(&str, FieldType)] = &[
("boolean", FieldType::Boolean),
Expand All @@ -16,3 +17,14 @@ pub const TYPES: &[(&str, FieldType)] = &[
("long", FieldType::Long),
("void", FieldType::Void),
];

pub const PRIMITIVES: &[Symbol] = &[
sym!(bool),
sym!(byte),
sym!(char),
sym!(double),
sym!(float),
sym!(int),
sym!(long),
sym!(short),
];
2 changes: 1 addition & 1 deletion runtime/src/initialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ fn create_thread_object(thread: &mut JavaThread) {
)
.expect("java.lang.ThreadGroup should have an initializer");

let name = StringInterner::get_java_string("main");
let name = StringInterner::intern_str("main");
java_call!(
thread,
init_method,
Expand Down
Loading

0 comments on commit c002dbb

Please sign in to comment.