Skip to content

Commit

Permalink
Small changes as I think about how to represent heap objects other th…
Browse files Browse the repository at this point in the history
…an structs
  • Loading branch information
jimmyhmiller committed Aug 3, 2024
1 parent 615ca06 commit 580e03c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 54 deletions.
10 changes: 5 additions & 5 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,17 +370,17 @@ impl<'a> AstCompiler<'a> {

let compiler_pointer_reg = self.ir.assign_new(self.compiler.get_compiler_ptr());

let allocate_struct = self.compiler.find_function("allocate_struct").unwrap();
let allocate_struct = self.compiler.get_function_pointer(allocate_struct).unwrap();
let allocate_struct = self.ir.assign_new(allocate_struct);
let allocate = self.compiler.find_function("allocate").unwrap();
let allocate = self.compiler.get_function_pointer(allocate).unwrap();
let allocate = self.ir.assign_new(allocate);

// Shift size left one so we can use it to mark
let size_reg = self.ir.assign_new(struct_type.size() + 1);
let stack_pointer = self.ir.get_stack_pointer_imm(0);
// TODO: I need store the struct type here, so I know things about what data is here.

let struct_ptr = self.ir.call_builtin(
allocate_struct.into(),
allocate.into(),
vec![compiler_pointer_reg.into(), size_reg.into(), stack_pointer],
);

Expand All @@ -402,7 +402,7 @@ impl<'a> AstCompiler<'a> {
}

self.ir
.tag(struct_ptr.into(), BuiltInTypes::Struct.get_tag())
.tag(struct_ptr.into(), BuiltInTypes::HeapObject.get_tag())
}
Ast::PropertyAccess { object, property } => {
let object = self.call_compile(object.as_ref());
Expand Down
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ pub unsafe extern "C" fn print_value<Alloc: Allocator>(
0b111
}

extern "C" fn allocate_struct<Alloc: Allocator>(
extern "C" fn allocate<Alloc: Allocator>(
runtime: *mut Runtime<Alloc>,
value: usize,
stack_pointer: usize,
Expand All @@ -134,7 +134,7 @@ extern "C" fn allocate_struct<Alloc: Allocator>(
let runtime = unsafe { &mut *runtime };

runtime
.allocate(value, stack_pointer, BuiltInTypes::Struct)
.allocate(value, stack_pointer, BuiltInTypes::HeapObject)
.unwrap()
}

Expand Down Expand Up @@ -403,8 +403,8 @@ fn main_inner(args: CommandLineArguments) -> Result<(), Box<dyn Error>> {
.compiler
.add_builtin_function("print", print_value::<Alloc> as *const u8, false)?;
runtime.compiler.add_builtin_function(
"allocate_struct",
allocate_struct::<Alloc> as *const u8,
"allocate",
allocate::<Alloc> as *const u8,
true,
)?;
// TODO: Probably needs true
Expand Down
4 changes: 2 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,7 @@ fn parse_struct_style_enum() {
direction
speed
},
stop { time, location}
stop { time, location }
}
};
println!("{:#?}", ast);
Expand All @@ -1182,4 +1182,4 @@ fn parse_enum_creation_complex() {
}
};
println!("{:#?}", ast);
}
}
37 changes: 6 additions & 31 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ pub struct Function {
number_of_locals: usize,
}

#[derive(Debug)]
struct HeapObject<'a> {
size: usize,
data: &'a [u8],
}

#[derive(Debug, Clone)]
pub struct Struct {
Expand Down Expand Up @@ -181,6 +176,9 @@ pub enum AllocateAction {
pub trait Allocator {
fn new() -> Self;


// TODO: I probably want something like kind, but not actually kind
// I might need to allocate things differently based on type
fn allocate(
&mut self,
bytes: usize,
Expand Down Expand Up @@ -636,7 +634,9 @@ impl Compiler {
Some(repr)
}
}
BuiltInTypes::Struct => {
BuiltInTypes::HeapObject => {
// TODO: Once I change the setup for heap objects
// I need to figure out what kind of heap object I have
unsafe {
let value = BuiltInTypes::untag(value);
let pointer = value as *const u8;
Expand All @@ -655,31 +655,6 @@ impl Compiler {
Some(self.get_struct_repr(struct_value?, data[8..].to_vec(), depth + 1)?)
}
}
BuiltInTypes::Array => {
unsafe {
let value = BuiltInTypes::untag(value);
let pointer = value as *const u8;
// get first 8 bytes as size le encoded
let size = *(pointer as *const usize) >> 1;
let pointer = pointer.add(8);
let data = std::slice::from_raw_parts(pointer, size);
let heap_object = HeapObject { size, data };

let mut repr = "[".to_string();
for i in 0..heap_object.size {
let value = &heap_object.data[i * 8..i * 8 + 8];
let mut bytes = [0u8; 8];
bytes.copy_from_slice(value);
let value = usize::from_le_bytes(bytes);
repr.push_str(&self.get_repr(value, depth + 1)?);
if i != heap_object.size - 1 {
repr.push_str(", ");
}
}
repr.push(']');
Some(repr)
}
}
}
}

Expand Down
18 changes: 6 additions & 12 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ pub enum BuiltInTypes {
Bool,
Function,
Closure,
Struct,
Array,
HeapObject,
Null,
}

Expand All @@ -32,8 +31,7 @@ impl BuiltInTypes {
BuiltInTypes::Bool => 0b011,
BuiltInTypes::Function => 0b100,
BuiltInTypes::Closure => 0b101,
BuiltInTypes::Struct => 0b110,
BuiltInTypes::Array => 0b111,
BuiltInTypes::HeapObject => 0b110,
BuiltInTypes::Null => 0b111,
}
}
Expand All @@ -53,8 +51,7 @@ impl BuiltInTypes {
0b011 => BuiltInTypes::Bool,
0b100 => BuiltInTypes::Function,
0b101 => BuiltInTypes::Closure,
0b110 => BuiltInTypes::Struct,
0b111 => BuiltInTypes::Array,
0b110 => BuiltInTypes::HeapObject,
_ => panic!("Invalid tag"),
}
}
Expand All @@ -66,8 +63,7 @@ impl BuiltInTypes {
BuiltInTypes::String => false,
BuiltInTypes::Bool => true,
BuiltInTypes::Function => false,
BuiltInTypes::Struct => false,
BuiltInTypes::Array => false,
BuiltInTypes::HeapObject => false,
BuiltInTypes::Closure => false,
BuiltInTypes::Null => true,
}
Expand Down Expand Up @@ -101,8 +97,7 @@ impl BuiltInTypes {
BuiltInTypes::Bool => false,
BuiltInTypes::Function => false,
BuiltInTypes::Closure => true,
BuiltInTypes::Struct => true,
BuiltInTypes::Array => false,
BuiltInTypes::HeapObject => true,
BuiltInTypes::Null => false,
}
}
Expand All @@ -117,8 +112,7 @@ fn tag_and_untag() {
BuiltInTypes::Bool,
BuiltInTypes::Function,
BuiltInTypes::Closure,
BuiltInTypes::Struct,
BuiltInTypes::Array,
BuiltInTypes::HeapObject,
];
for kind in kinds.iter() {
let value = 123;
Expand Down

0 comments on commit 580e03c

Please sign in to comment.