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
use std::{
    alloc::{alloc_zeroed, Layout},
    collections::HashMap,
};

use parse::{
    classfile::{ClassFile, Field, Resolvable},
    flags::FieldAccessFlag,
};
use support::descriptor::FieldType;

use crate::{error::Throwable, internalise};

use self::types::JavaType;

use super::{builtins::Object, runtime::RuntimeValue};

#[derive(Debug, Clone)]
/// More expensive to clone, but more comprehensive field information.
/// Used to give out the locations of fields.
pub struct FieldInfo {
    pub name: String,
    pub data: Field,
    pub location: FieldLocation,
    pub value: Option<RuntimeValue>,
}

#[derive(Debug, Clone, Copy)]
/// A trivially copyable location for a field within a class
/// Used by FieldRefTo to access the underlying data from a *const Object.
pub struct FieldLocation {
    pub offset: usize,
}

#[derive(Debug, Clone)]
pub struct ClassFileLayout {
    pub(crate) layout: Layout,
    pub(crate) field_info: HashMap<String, FieldInfo>,
    pub(crate) statics: HashMap<String, FieldInfo>,
}

impl ClassFileLayout {
    pub fn from_java_type(ty: JavaType) -> Self {
        Self {
            layout: ty.layout,
            field_info: HashMap::new(),
            statics: HashMap::new(),
        }
    }

    pub fn empty() -> Self {
        Self {
            layout: Layout::new::<()>(),
            field_info: HashMap::new(),
            statics: HashMap::new(),
        }
    }

    pub fn fields(&self) -> &HashMap<String, FieldInfo> {
        &self.field_info
    }

    pub fn layout(&self) -> Layout {
        self.layout
    }

    pub fn field_info(&self, name: &String) -> Option<&FieldInfo> {
        self.field_info.get(name)
    }

    pub fn field_info_mut(&mut self, name: &String) -> Option<&mut FieldInfo> {
        self.field_info.get_mut(name)
    }

    pub fn static_field_info(&self, name: &String) -> Option<&FieldInfo> {
        self.statics.get(name)
    }

    pub fn static_field_info_mut(&mut self, name: &String) -> Option<&mut FieldInfo> {
        self.statics.get_mut(name)
    }

    pub fn alloc(&self) -> *mut Object {
        unsafe { alloc_zeroed(self.layout).cast::<Object>() }
    }
}

pub mod types {
    use std::{alloc::Layout, ops::Deref};

    use support::descriptor::{BaseType, FieldType};

    use crate::{object::{
        builtins::{Array, Object},
        mem::RefTo,
    }, error::Throwable, internal, internalise};

    #[derive(Debug, Clone, Copy)]
    pub struct JavaType {
        pub alignment: Alignment,
        pub size: Size,
        pub layout: Layout,
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
    pub struct Alignment(usize);
    impl Deref for Alignment {
        type Target = usize;

        fn deref(&self) -> &Self::Target {
            &self.0
        }
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
    pub struct Size(usize);
    impl Deref for Size {
        type Target = usize;

        fn deref(&self) -> &Self::Target {
            &self.0
        }
    }

    impl JavaType {
        pub const fn new(layout: Layout) -> Self {
            Self {
                alignment: Alignment(layout.align()),
                size: Size(layout.size()),
                layout,
            }
        }
    }

    // Bools are described as ints
    pub const BOOL: JavaType = INT;

    pub const CHAR: JavaType = JavaType::new(Layout::new::<Char>());
    pub const FLOAT: JavaType = JavaType::new(Layout::new::<Float>());
    pub const DOUBLE: JavaType = JavaType::new(Layout::new::<Double>());
    pub const BYTE: JavaType = JavaType::new(Layout::new::<Byte>());
    pub const SHORT: JavaType = JavaType::new(Layout::new::<Short>());
    pub const LONG: JavaType = JavaType::new(Layout::new::<Long>());
    pub const INT: JavaType = JavaType::new(Layout::new::<Int>());

    pub const OBJECT: JavaType = JavaType::new(Layout::new::<RefTo<Object>>());
    pub const ARRAY_BASE: JavaType = JavaType::new(Layout::new::<RefTo<Array<()>>>());

    pub type Bool = Int;
    pub type Char = u16;
    pub type Float = f32;
    pub type Double = f64;
    pub type Byte = u8;
    pub type Short = i16;
    pub type Long = i64;
    pub type Int = i32;

    pub fn for_field_type(ty: FieldType) -> Result<JavaType, Throwable> {
        Ok(match ty {
            FieldType::Base(base) => match base {
                BaseType::Boolean => BOOL,
                BaseType::Char => CHAR,
                BaseType::Float => FLOAT,
                BaseType::Double => DOUBLE,
                BaseType::Byte => BYTE,
                BaseType::Short => SHORT,
                BaseType::Int => INT,
                BaseType::Long => LONG,
                BaseType::Void => return Err(internal!("void is not supported")),
            },
            FieldType::Array(ty) => {
                let component = match *ty.field_type {
                    FieldType::Base(ty) => for_field_type(FieldType::Base(ty))?,
                    FieldType::Object(_) => OBJECT,
                    FieldType::Array(_) => todo!(
                        "not sure how to describe nested arrays because Array is not fully implemented"
                    ),
                };

                // We can only allocate the struct and nothing else (such as the elements)
                // The runtime has to re-allocate the array if we want to grow it to a real size
                let layout = Layout::from_size_align(
                    // So, we allocate only the base struct (this assumes the "value" is a zero sized type)
                    // which should be semantically identical to zero values
                    *ARRAY_BASE.size,
                    // Select the largest alignment out of the component or the values in the array struct itself,
                    // this is probably always going to be 8, but we should check anyways.
                    *component.alignment.max(ARRAY_BASE.alignment),
                ).map_err(internalise!())?;

                JavaType::new(layout)
            }
            FieldType::Object(_) => OBJECT,
        })
    }
}

#[derive(Debug)]
pub struct BasicLayout {
    layout: Layout,
    fields: Vec<Field>,
    names: Vec<String>,
    offsets: Vec<usize>,
    statics: Vec<FieldInfo>,
}

pub fn basic_layout(class_file: &ClassFile, base_layout: Layout) -> Result<BasicLayout, Throwable> {
    // Handle statics differently so that they are not included in the layout
    let mut instance_fields = vec![];
    let mut static_fields = vec![];

    for field in class_file.fields.clone().values.into_iter() {
        if field.flags.has(FieldAccessFlag::STATIC) {
            static_fields.push(FieldInfo {
                name: field.name.resolve().string(),
                value: Some(RuntimeValue::default_for_field(&FieldType::parse(
                    field.descriptor.resolve().string(),
                )?)),
                data: field,
                location: FieldLocation { offset: 0 },
            });
        } else {
            instance_fields.push(field);
        }
    }

    let descriptors = instance_fields
        .iter()
        .map(|f| {
            let desc = f.descriptor.resolve().string();
            FieldType::parse(desc).map_err(internalise!())
        })
        .collect::<Result<Vec<_>, Throwable>>()?;

    let names = instance_fields
        .iter()
        .map(|f| f.name.resolve().string())
        .collect::<Vec<_>>();

    let field_layouts = descriptors
        .into_iter()
        .map(|desc| {
            let ty = types::for_field_type(desc)?;
            Layout::from_size_align(*ty.size, *ty.alignment).map_err(internalise!())
        })
        .collect::<Result<Vec<_>, Throwable>>()?;

    // Find the overall alignment for the struct
    let alignment = field_layouts
        .iter()
        .map(|d| d.align())
        .chain(vec![base_layout.align()])
        .max()
        .unwrap(); // Iterator can't be empty because we chain with at least one value

    let mut final_layout = Layout::from_size_align(base_layout.size(), alignment).map_err(internalise!())?;
    let mut offsets = Vec::new();

    for layout in field_layouts {
        let (new_layout, offset) = final_layout.extend(layout).map_err(internalise!())?;
        final_layout = new_layout;
        offsets.push(offset);
    }

    Ok(BasicLayout {
        layout: final_layout.pad_to_align(),
        fields: instance_fields,
        names,
        offsets,
        statics: static_fields,
    })
}

pub fn full_layout(class_file: &ClassFile, base_layout: Layout) -> Result<ClassFileLayout, Throwable> {
    let basic_layout = basic_layout(class_file, base_layout)?;

    let field_info = basic_layout
        .fields
        .iter()
        .zip(basic_layout.offsets)
        .zip(basic_layout.names)
        .map(|((field, offset), name)| {
            (
                name.clone(),
                FieldInfo {
                    name,
                    data: field.clone(),
                    location: FieldLocation { offset },
                    value: None,
                },
            )
        })
        .collect::<HashMap<_, _>>();

    Ok(ClassFileLayout {
        layout: basic_layout.layout,
        field_info,
        statics: basic_layout
            .statics
            .into_iter()
            .map(|s| (s.name.clone(), s))
            .collect(),
    })
}