Skip to content

Commit

Permalink
Changes to ffi and change from small to opaque so I can start on some…
Browse files Browse the repository at this point in the history
… string work
  • Loading branch information
jimmyhmiller committed Dec 7, 2024
1 parent ba09843 commit 975fc78
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 37 deletions.
43 changes: 24 additions & 19 deletions resources/ffi_test.bg
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,28 @@ let sdl_get_window_size = ffi/get_function(
// Rect structure: [x, y, w, h] (4 * 4 bytes = 16 bytes)
let rect_size = 16

fn update_state(new_width, new_height) {
swap!(state, fn(s) {
State {
rect_x: s.rect_x + s.dx,
rect_y: s.rect_y + s.dy,
dx: if s.rect_x + s.dx <= 0 || s.rect_x + s.dx + 240 >= s.screen_width {
s.dx * -1
} else {
s.dx
},
dy: if s.rect_y + s.dy <= 0 || s.rect_y + s.dy + 180 >= s.screen_height {
s.dy * -1
} else {
s.dy
},
screen_width: new_width,
screen_height: new_height,
}
})
}


fn loop_it(buffer, renderer, rect_ptr, window) {
sdl_poll_event(buffer)
if ffi/get_u32(buffer, 0) == 256 {
Expand All @@ -69,24 +91,7 @@ fn loop_it(buffer, renderer, rect_ptr, window) {
let new_width = ffi/get_i32(width_ptr, 0)
let new_height = ffi/get_i32(height_ptr, 0)

let updated_state = swap!(state, fn(s) {
State {
rect_x: s.rect_x + s.dx,
rect_y: s.rect_y + s.dy,
dx: if s.rect_x + s.dx <= 0 || s.rect_x + s.dx + 240 >= s.screen_width {
s.dx * -1
} else {
s.dx
},
dy: if s.rect_y + s.dy <= 0 || s.rect_y + s.dy + 180 >= s.screen_height {
s.dy * -1
} else {
s.dy
},
screen_width: new_width,
screen_height: new_height,
}
})
let updated_state = update_state(new_width, new_height)

// Clear the renderer
sdl_set_render_draw_color(renderer, 0, 0, 0, 255) // Black background
Expand All @@ -103,7 +108,7 @@ fn loop_it(buffer, renderer, rect_ptr, window) {
// Present the updated screen
sdl_render_present(renderer)

// sdl_delay(16)
sdl_delay(16)
loop_it(buffer, renderer, rect_ptr, window)
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/gc/compacting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,9 @@ impl CompactingHeap {
if object.marked() {
panic!("We are copying to this space, nothing should be marked");
}
if object.is_small_object() || object.is_zero_size() {
if object.is_opaque_object() || object.is_zero_size() {
// TODO(DuplicatingOpaque): I think right now I'm duplicating opaque objects
// Once I have a good means of visualizing that, it would be obvious
continue;
}
for datum in object.get_fields_mut() {
Expand All @@ -356,7 +358,8 @@ impl CompactingHeap {
unsafe fn copy_using_cheneys_algorithm(&mut self, root: usize) -> usize {
let heap_object = HeapObject::from_tagged(root);

if !heap_object.is_zero_size() && !heap_object.is_small_object() {
if !heap_object.is_zero_size() && !heap_object.is_opaque_object() {
// TODO(DuplicatingOpaque)
let first_field = heap_object.get_field(0);
if BuiltInTypes::is_heap_pointer(heap_object.get_field(0)) {
let untagged_data = BuiltInTypes::untag(first_field);
Expand All @@ -366,7 +369,8 @@ impl CompactingHeap {
}
}
}
if heap_object.is_zero_size() || heap_object.is_small_object() {
if heap_object.is_zero_size() || heap_object.is_opaque_object() {
// TODO(DuplicatingOpaque)
let data = heap_object.get_full_object_data();
let new_pointer = self.to_space.copy_data_to_offset(data);
let tagged_new = BuiltInTypes::get_kind(root).tag(new_pointer) as usize;
Expand Down
31 changes: 16 additions & 15 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,23 +140,23 @@ pub struct Header {
pub type_id: u8,
pub type_data: u32,
pub size: u8,
pub small: bool,
pub opaque: bool,
pub marked: bool,
}

impl Header {
// | Byte 7 | Bytes 3-6 | Byte 2 | Byte 1 | Byte 0 |
// |---------|---------------|--------|---------|----------------------|
// | Type | Type Metadata | Size | Padding | Flag bits |
// | | (4 bytes) | | | Small object (bit 1) |
// | | (4 bytes) | | | Opaque object (bit 1) |
// | | | | | Marked (bit 0) |

fn to_usize(self) -> usize {
let mut data: usize = 0;
data |= (self.type_id as usize) << 56;
data |= (self.type_data as usize) << 24;
data |= (self.size as usize) << 16;
if self.small {
if self.opaque {
data |= 0b10;
}
if self.marked {
Expand All @@ -169,13 +169,13 @@ impl Header {
let _type = (data >> 56) as u8;
let type_data = (data >> 24) as u32;
let size = (data >> 16) as u8;
let small = (data & 0b10) == 0b10;
let opaque = (data & 0b10) == 0b10;
let marked = (data & 0b1) == 0b1;
Header {
type_id: _type,
type_data,
size,
small,
opaque,
marked,
}
}
Expand All @@ -199,7 +199,7 @@ fn header() {
type_id: 0,
type_data: 0,
size: 0b0,
small: true,
opaque: true,
marked: false,
};
let data = header.to_usize();
Expand All @@ -215,7 +215,7 @@ fn header() {
type_id: t,
type_data: u32::MAX,
size: s,
small: *sm,
opaque: *sm,
marked: *m,
};
let data = header.to_usize();
Expand Down Expand Up @@ -302,6 +302,9 @@ impl HeapObject {
}

pub fn get_fields(&self) -> &[usize] {
if self.is_opaque_object() {
return &[];
}
let size = self.fields_size();
let untagged = self.untagged();
let pointer = untagged as *mut usize;
Expand All @@ -321,15 +324,13 @@ impl HeapObject {
let fields = self.get_fields();
fields
.iter()
// Hack to make sure we don't get any references if this is a small object
// Trying to do an empty in a condition makes the type checker complain
.filter(|_x| !self.is_small_object())
.filter(|_x| !self.is_opaque_object())
.filter(|x| BuiltInTypes::is_heap_pointer(**x))
.map(|&pointer| HeapObject::from_tagged(pointer))
}

pub fn get_fields_mut(&mut self) -> &mut [usize] {
if self.is_small_object() {
if self.is_opaque_object() {
return &mut [];
}
let size = self.fields_size();
Expand Down Expand Up @@ -371,7 +372,7 @@ impl HeapObject {
type_id: 0,
type_data: 0,
size: field_size.to_words() as u8,
small: false,
opaque: false,
marked: false,
};

Expand Down Expand Up @@ -445,12 +446,12 @@ impl HeapObject {
header.type_data as usize
}

pub fn is_small_object(&self) -> bool {
pub fn is_opaque_object(&self) -> bool {
let untagged = self.untagged();
let pointer = untagged as *mut usize;
let data: usize = unsafe { *pointer.cast::<usize>() };
let header = Header::from_usize(data);
header.small
header.opaque
}

pub fn get_header(&self) -> Header {
Expand Down Expand Up @@ -554,7 +555,7 @@ impl Ir {
type_id: 0,
type_data: 0,
size: 1,
small: true,
opaque: true,
marked: false,
};
self.heap_store(small_object_pointer, Value::RawValue(header.to_usize()));
Expand Down

0 comments on commit 975fc78

Please sign in to comment.