Skip to content

Commit

Permalink
Bugfix with dynamic arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
cody-quinn committed Jun 26, 2023
1 parent 849ba9f commit 4172dc2
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 39 deletions.
2 changes: 1 addition & 1 deletion examples/hello.sloth
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fn test() [Int 3] {
var list: [Int 3] = [9, 5, 7];
var list: [Int 3] = [500, 5, 7];

vpushi(list, 3);
vpushi(list, 3);
Expand Down
58 changes: 30 additions & 28 deletions sloth/src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use inkwell::module::Module;
use inkwell::targets::{
CodeModel, FileType, InitializationConfig, RelocMode, Target, TargetMachine,
};
use inkwell::types::{BasicMetadataTypeEnum, BasicType, BasicTypeEnum, PointerType};
use inkwell::types::{BasicMetadataTypeEnum, BasicType, BasicTypeEnum};
use inkwell::values::{
BasicMetadataValueEnum, BasicValue, BasicValueEnum, FunctionValue, PointerValue,
};
Expand Down Expand Up @@ -210,8 +210,8 @@ impl<'ctx> Codegen<'ctx> {

let llvm_function_type = match output_typ {
Type::Void => self.context.void_type().fn_type(&inputs_typ, false),
Type::Integer => self.context.i64_type().fn_type(&inputs_typ, false),
Type::Float => self.context.f64_type().fn_type(&inputs_typ, false),
Type::Integer => self.context.i32_type().fn_type(&inputs_typ, false),
Type::Float => self.context.f32_type().fn_type(&inputs_typ, false),
Type::Boolean => self.context.bool_type().fn_type(&inputs_typ, false),
Type::Array { ref typ, .. } => {
let i32_type = self.context.i32_type().as_basic_type_enum();
Expand Down Expand Up @@ -345,13 +345,13 @@ impl<'ctx> Codegen<'ctx> {
match value {
Literal::Integer(value) => self
.context
.i64_type()
.i32_type()
.const_int(value as u64, true)
.as_basic_value_enum(),
Literal::Float(value) => self
.context
.f64_type()
.const_float(value)
.f32_type()
.const_float(value as f64)
.as_basic_value_enum(),
Literal::Boolean(value) => self
.context
Expand All @@ -372,7 +372,7 @@ impl<'ctx> Codegen<'ctx> {
let value = self.codegen_expr(value).unwrap();
let value_ptr = unsafe {
self.builder.build_gep(
i32_type,
element_type,
inner_ptr,
&[i32_type.const_int(idx as u64, false)],
"",
Expand All @@ -394,24 +394,25 @@ impl<'ctx> Codegen<'ctx> {
let vector_ptr = self.builder.build_malloc(vector_type, "vecptr").unwrap();

// Set the size and capacity values
let size = self
let size_ptr = self
.builder
.build_struct_gep(vector_type, vector_ptr, 0, "gepvec")
.build_struct_gep(vector_type, vector_ptr, 0, "size")
.unwrap();
self.builder
.build_store(size, i32_type.const_int(values.len() as u64, false));

let cap = self
let cap_ptr = self
.builder
.build_struct_gep(vector_type, vector_ptr, 1, "gepvec")
.build_struct_gep(vector_type, vector_ptr, 1, "cap")
.unwrap();
self.builder
.build_store(cap, i32_type.const_int(100, false));

let inner = self
.builder
.build_struct_gep(vector_type, vector_ptr, 2, "gepvec")
.build_struct_gep(vector_type, vector_ptr, 2, "inner")
.unwrap();

self.builder
.build_store(size_ptr, i32_type.const_int(values.len() as u64, false));

self.builder
.build_store(cap_ptr, i32_type.const_int(100, false));

self.builder.build_store(inner, inner_ptr);

vector_ptr.as_basic_value_enum()
Expand All @@ -423,8 +424,8 @@ impl<'ctx> Codegen<'ctx> {
fn type_as_basic_type(&self, typ: Type) -> BasicTypeEnum<'ctx> {
// self.context.i64_type().ptr_type(Address)
match typ {
Type::Integer => self.context.i64_type().into(),
Type::Float => self.context.f64_type().into(),
Type::Integer => self.context.i32_type().into(),
Type::Float => self.context.f32_type().into(),
Type::Boolean => self.context.bool_type().into(),
Type::Array { typ, .. } => {
let i32_type = self.context.i32_type().as_basic_type_enum();
Expand Down Expand Up @@ -502,15 +503,15 @@ impl<'ctx> Codegen<'ctx> {

// Writing the logic
let element_type = self.type_as_basic_type(typ);
let element_ptr_type = element_type.ptr_type(AddressSpace::default());

let i32_type = self.context.i32_type();

let vector_type = self.context.struct_type(
&[
i32_type.as_basic_type_enum(),
i32_type.as_basic_type_enum(),
element_type
.ptr_type(AddressSpace::default())
.as_basic_type_enum(),
element_ptr_type.as_basic_type_enum(),
],
false,
);
Expand Down Expand Up @@ -538,14 +539,15 @@ impl<'ctx> Codegen<'ctx> {
.builder
.build_load(i32_type, cap_ptr, "cap")
.into_int_value();
let inner = self
.builder
.build_load(element_ptr_type, inner_ptr, "inner")
.into_pointer_value();

// Put the new element into backing array
let slot_ptr = unsafe {
self.builder
.build_gep(element_type, inner_ptr, &[size], "slot")
};

let element = func.get_nth_param(1).unwrap();
let slot_ptr = unsafe { self.builder.build_gep(element_type, inner, &[size], "slot") };

self.builder.build_store(slot_ptr, element);

// TODO: Handle going over capacity
Expand Down
8 changes: 4 additions & 4 deletions sloth/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ pub enum TokenType {

#[derive(Debug, Clone, PartialEq)]
pub enum Literal {
Integer(i64),
Float(f64),
Integer(i32),
Float(f32),
Boolean(bool),
Character(char),
String(String),
Expand Down Expand Up @@ -249,9 +249,9 @@ impl<'a> Lexer<'a> {
value.push(self.advance());
}

Literal::Float(value.parse::<f64>().expect("Expected float")).into()
Literal::Float(value.parse::<f32>().expect("Expected float")).into()
} else {
Literal::Integer(value.parse::<i64>().expect("Expected integer")).into()
Literal::Integer(value.parse::<i32>().expect("Expected integer")).into()
}
}

Expand Down
4 changes: 2 additions & 2 deletions sloth/src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ impl Display for TypeIdentifier {

#[derive(Debug, Clone, PartialEq)]
pub enum Literal {
Integer(i64),
Float(f64),
Integer(i32),
Float(f32),
Boolean(bool),
Character(char),
String(String),
Expand Down
8 changes: 4 additions & 4 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
typedef struct {
int size;
int cap;
long* inner;
int* inner;
} IntVec;

IntVec* test();
Expand All @@ -13,14 +13,14 @@ int main() {

int size = (*v).size;
int cap = (*v).cap;
long* inner = (*v).inner;
int* inner = (*v).inner;

printf("%d\n", size);
printf("%d\n", cap);

for (int i = 0; i < size; ++i) {
long value = inner[i];
printf("%d ", i);
int value = inner[i];
printf("%d ", value);
}
puts("\n");
}

0 comments on commit 4172dc2

Please sign in to comment.