-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial very leaky implementation of heap allocation
- Loading branch information
1 parent
0e75842
commit 26621df
Showing
14 changed files
with
234 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,33 @@ | ||
//use crate::ir::module::Types; | ||
use crate::ir::try_convert_any_to_basic; | ||
use crate::IrDatabase; | ||
use inkwell::types::{AnyTypeEnum, BasicTypeEnum, StructType}; | ||
use inkwell::values::BasicValueEnum; | ||
use inkwell::types::{BasicTypeEnum, StructType}; | ||
use inkwell::values::{BasicValueEnum, StructValue}; | ||
|
||
pub(super) fn gen_struct_decl(db: &impl IrDatabase, s: hir::Struct) -> StructType { | ||
if let AnyTypeEnum::StructType(struct_type) = db.type_ir(s.ty(db)) { | ||
if struct_type.is_opaque() { | ||
let field_types: Vec<BasicTypeEnum> = s | ||
.fields(db) | ||
.iter() | ||
.map(|field| { | ||
let field_type = field.ty(db); | ||
try_convert_any_to_basic(db.type_ir(field_type)) | ||
.expect("could not convert field type") | ||
}) | ||
.collect(); | ||
let struct_type = db.struct_ty(s); | ||
if struct_type.is_opaque() { | ||
let field_types: Vec<BasicTypeEnum> = s | ||
.fields(db) | ||
.iter() | ||
.map(|field| { | ||
let field_type = field.ty(db); | ||
try_convert_any_to_basic(db.type_ir(field_type)) | ||
.expect("could not convert field type") | ||
}) | ||
.collect(); | ||
|
||
struct_type.set_body(&field_types, false); | ||
} | ||
struct_type | ||
} else { | ||
unreachable!() | ||
struct_type.set_body(&field_types, false); | ||
} | ||
struct_type | ||
} | ||
|
||
pub(crate) fn gen_named_struct_lit( | ||
/// Constructs a struct literal value of type `s`. | ||
pub(super) fn gen_named_struct_lit( | ||
db: &impl IrDatabase, | ||
ty: hir::Ty, | ||
s: hir::Struct, | ||
values: &[BasicValueEnum], | ||
) -> BasicValueEnum { | ||
if let inkwell::types::AnyTypeEnum::StructType(struct_type) = db.type_ir(ty) { | ||
struct_type.const_named_struct(&values).into() | ||
} else { | ||
unreachable!("at this stage there must be a struct type"); | ||
} | ||
) -> StructValue { | ||
let struct_ty = db.struct_ty(s); | ||
struct_ty.const_named_struct(values) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
--- | ||
source: crates/mun_codegen/src/test.rs | ||
expression: "struct(gc) Foo { a: int, b: int };\n\nfn foo() {\n let a = Foo { a: 3, b: 4 };\n a.b += 3;\n let b = a;\n}" | ||
--- | ||
; ModuleID = 'main.mun' | ||
source_filename = "main.mun" | ||
|
||
%Foo = type { i64, i64 } | ||
|
||
define void @foo() { | ||
body: | ||
%b4 = alloca %Foo* | ||
%a = alloca %Foo* | ||
%malloccall = tail call i8* @malloc(i32 trunc (i64 mul nuw (i64 ptrtoint (i64* getelementptr (i64, i64* null, i32 1) to i64), i64 2) to i32)) | ||
%Foo = bitcast i8* %malloccall to %Foo* | ||
store %Foo { i64 3, i64 4 }, %Foo* %Foo | ||
store %Foo* %Foo, %Foo** %a | ||
%deref = load %Foo*, %Foo** %a | ||
%Foo.b = getelementptr inbounds %Foo, %Foo* %deref, i32 0, i32 1 | ||
%b = load i64, i64* %Foo.b | ||
%add = add i64 %b, 3 | ||
%deref1 = load %Foo*, %Foo** %a | ||
%Foo.b2 = getelementptr inbounds %Foo, %Foo* %deref1, i32 0, i32 1 | ||
store i64 %add, i64* %Foo.b2 | ||
%a3 = load %Foo*, %Foo** %a | ||
store %Foo* %a3, %Foo** %b4 | ||
ret void | ||
} | ||
|
||
declare noalias i8* @malloc(i32) | ||
|
Oops, something went wrong.