Skip to content

Commit

Permalink
WIP: squash with others
Browse files Browse the repository at this point in the history
Bool and Nil now use LLVM's i1 type, which turns into a byte. In
addition, this commit sets things up for supporting futures.
  • Loading branch information
yorickpeterse committed Sep 17, 2024
1 parent a1a8b4e commit 71cb971
Show file tree
Hide file tree
Showing 14 changed files with 221 additions and 149 deletions.
26 changes: 12 additions & 14 deletions compiler/src/llvm/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,16 @@ impl<'ctx> Builder<'ctx> {
.into_float_value()
}

pub(crate) fn load_bool(
&self,
variable: PointerValue<'ctx>,
) -> IntValue<'ctx> {
self.inner
.build_load(self.context.bool_type(), variable, "")
.unwrap()
.into_int_value()
}

pub(crate) fn load_pointer(
&self,
variable: PointerValue<'ctx>,
Expand Down Expand Up @@ -304,7 +314,7 @@ impl<'ctx> Builder<'ctx> {
pointer: PointerValue<'ctx>,
old: V,
new: V,
) -> BasicValueEnum<'ctx> {
) -> IntValue<'ctx> {
let res = self
.inner
.build_cmpxchg(
Expand All @@ -316,7 +326,7 @@ impl<'ctx> Builder<'ctx> {
)
.unwrap();

self.extract_field(res, 0)
self.extract_field(res, 1).into_int_value()
}

pub(crate) fn load_atomic_counter(
Expand Down Expand Up @@ -504,18 +514,6 @@ impl<'ctx> Builder<'ctx> {
self.inner.build_int_cast_sign_flag(value, target, signed, "").unwrap()
}

pub(crate) fn bool_to_int(&self, value: IntValue<'ctx>) -> IntValue<'ctx> {
let typ = self.context.i64_type();

self.inner.build_int_cast_sign_flag(value, typ, false, "").unwrap()
}

pub(crate) fn int_to_bool(&self, value: IntValue<'ctx>) -> IntValue<'ctx> {
let typ = self.context.bool_type();

self.inner.build_int_cast_sign_flag(value, typ, true, "").unwrap()
}

pub(crate) fn float_to_float(
&self,
value: FloatValue<'ctx>,
Expand Down
17 changes: 8 additions & 9 deletions compiler/src/llvm/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,15 @@ impl Context {
layouts: &Layouts<'a>,
type_ref: TypeRef,
) -> BasicTypeEnum<'a> {
if let TypeRef::Pointer(_) = type_ref {
return self.pointer_type().as_basic_type_enum();
}

let Ok(id) = type_ref.type_id(db) else {
return self.pointer_type().as_basic_type_enum();
};

let base = match id {
match id {
TypeId::Foreign(ForeignType::Int(8, _)) => {
self.i8_type().as_basic_type_enum()
}
Expand All @@ -186,21 +190,16 @@ impl Context {
.as_basic_type_enum()
} else {
match cls.0 {
INT_ID | BOOL_ID | NIL_ID => {
self.i64_type().as_basic_type_enum()
BOOL_ID | NIL_ID => {
self.bool_type().as_basic_type_enum()
}
INT_ID => self.i64_type().as_basic_type_enum(),
FLOAT_ID => self.f64_type().as_basic_type_enum(),
_ => self.pointer_type().as_basic_type_enum(),
}
}
}
_ => self.pointer_type().as_basic_type_enum(),
};

if let TypeRef::Pointer(_) = type_ref {
self.pointer_type().as_basic_type_enum()
} else {
base
}
}

Expand Down
8 changes: 1 addition & 7 deletions compiler/src/llvm/layouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,7 @@ impl<'ctx> Layouts<'ctx> {
// defined.
for &id in mir.classes.keys() {
let instance = match id.0 {
INT_ID => {
context.builtin_type(header, context.i64_type().into())
}
FLOAT_ID => {
context.builtin_type(header, context.f64_type().into())
}
BOOL_ID | NIL_ID => {
INT_ID | FLOAT_ID | BOOL_ID | NIL_ID => {
let typ = context.opaque_struct("");

typ.set_body(&[header.into()], false);
Expand Down
Loading

0 comments on commit 71cb971

Please sign in to comment.