Skip to content

Commit

Permalink
Merge pull request #38 from lenawanel/merge_test
Browse files Browse the repository at this point in the history
Make capy fully use the correct platform dependent calling convention.
closes #35
  • Loading branch information
NotAFlyingGoose authored Jul 2, 2024
2 parents 14d6b77 + ed98774 commit b638500
Show file tree
Hide file tree
Showing 13 changed files with 1,068 additions and 304 deletions.
1 change: 1 addition & 0 deletions crates/codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ internment = "0.8.3"
uid_gen = { path = "../uid_gen" }
num-traits = "0.2.16"
glob = "0.3.1"
tinyvec = { version = "1.6.1", features = ["tinyvec_macros"] }

[dev-dependencies]
ast = { path = "../ast" }
Expand Down
31 changes: 15 additions & 16 deletions crates/codegen/src/compiler/comptime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::{
use uid_gen::UIDGenerator;

use crate::{
compiler::{MetaTyData, MetaTyInfoArrays},
compiler::{abi::Abi, MetaTyData, MetaTyInfoArrays},
convert::{FinalTy, GetFinalTy, ToTyId},
layout::GetLayoutInfo,
mangle::Mangle,
Expand Down Expand Up @@ -134,6 +134,7 @@ pub fn eval_comptime_blocks<'a>(
let builder = JITBuilder::with_isa(isa, cranelift_module::default_libcall_names());

let mut module = JITModule::new(builder);
let default_abi = module.target_config().into();

let mut compiler = Compiler {
final_binary: false,
Expand Down Expand Up @@ -163,6 +164,7 @@ pub fn eval_comptime_blocks<'a>(
64 => types::I64,
_ => unreachable!(),
},
default_abi,
};

compiler.finalize_tys();
Expand All @@ -179,7 +181,7 @@ pub fn eval_comptime_blocks<'a>(
let hir::Comptime { body } = compiler.world_bodies[ctc.file][ctc.comptime];
let return_ty = tys[ctc.file][body];

let func_id = compiler.compile_real_function(
let func_id = compiler.compile_real_function_with_abi(
&format!(
"{}.comptime#{}",
ctc.file.to_string(compiler.mod_dir, compiler.interner),
Expand All @@ -190,6 +192,7 @@ pub fn eval_comptime_blocks<'a>(
ctc.expr,
vec![],
return_ty,
Abi::Simplified,
);

let extra: Vec<_> = compiler
Expand All @@ -202,13 +205,7 @@ pub fn eval_comptime_blocks<'a>(
already_done.extend(extra.clone());
to_eval.extend(extra);

comptime_funcs.push((
ctc,
func_id,
return_ty.get_final_ty(),
return_ty.size(),
*return_ty == hir_ty::Ty::Type,
));
comptime_funcs.push((ctc, func_id, return_ty));
}

// Initializing this will force the compiler to create type info data
Expand Down Expand Up @@ -255,10 +252,10 @@ pub fn eval_comptime_blocks<'a>(
}
}

while let Some((ctc, func_id, return_ty, size, is_type)) = comptime_funcs.pop() {
while let Some((ctc, func_id, return_ty)) = comptime_funcs.pop() {
let code_ptr = module.get_finalized_function(func_id);

if is_type {
if *return_ty == hir_ty::Ty::Type {
let comptime = unsafe { mem::transmute::<*const u8, fn() -> u32>(code_ptr) };
let ty = comptime();

Expand All @@ -268,7 +265,7 @@ pub fn eval_comptime_blocks<'a>(
continue;
}

match return_ty {
match return_ty.get_final_ty() {
FinalTy::Number(number_ty) => {
let result = match number_ty.ty {
types::F32 => run_comptime_float::<f32>(code_ptr),
Expand All @@ -290,17 +287,19 @@ pub fn eval_comptime_blocks<'a>(
results.insert(ctc, result);
}
FinalTy::Pointer(_) => {
let layout = Layout::from_size_align(size as usize, std::mem::align_of::<u8>())
.expect("Invalid layout");
let layout =
Layout::from_size_align(return_ty.size() as usize, return_ty.align() as usize)
.expect("Invalid layout");
let raw = unsafe { std::alloc::alloc(layout) };

let comptime =
unsafe { mem::transmute::<*const u8, fn(*const u8) -> *const u8>(code_ptr) };
unsafe { mem::transmute::<*const u8, fn(*mut u8) -> *mut u8>(code_ptr) };

comptime(raw);

let bytes = unsafe {
let slice = std::ptr::slice_from_raw_parts(raw, size as usize) as *mut [u8];
let slice = std::ptr::slice_from_raw_parts(raw, return_ty.size() as usize)
as *mut [u8];

Box::from_raw(slice)
};
Expand Down
Loading

0 comments on commit b638500

Please sign in to comment.