1
Fork 0

Don't call finalize_all and then finalize_function

This commit is contained in:
bjorn3 2018-08-11 12:04:54 +02:00
parent b391524b4f
commit dc323d7ffa
2 changed files with 52 additions and 41 deletions

View file

@ -74,23 +74,20 @@ pub fn trans_mono_item<'a, 'tcx: 'a>(
context.func = f; context.func = f;
// TODO: cranelift doesn't yet support some of the things needed // TODO: cranelift doesn't yet support some of the things needed
// cx.module.define_function(func_id, context).unwrap(); if false {
cx.module.define_function(func_id, context).unwrap();
cx.defined_functions.push(func_id);
}
context.clear(); context.clear();
} }
inst => cx inst => unimpl!("Unimplemented instance {:?}", inst),
.tcx
.sess
.warn(&format!("Unimplemented instance {:?}", inst)),
}, },
MonoItem::Static(def_id) => cx MonoItem::Static(def_id) => unimpl!("Unimplemented static mono item {:?}", def_id),
.tcx
.sess
.err(&format!("Unimplemented static mono item {:?}", def_id)),
MonoItem::GlobalAsm(node_id) => cx MonoItem::GlobalAsm(node_id) => cx
.tcx .tcx
.sess .sess
.err(&format!("Unimplemented global asm mono item {:?}", node_id)), .fatal(&format!("Unimplemented global asm mono item {:?}", node_id)),
} }
} }

View file

@ -1,4 +1,4 @@
#![feature(rustc_private, macro_at_most_once_rep)] #![feature(rustc_private, macro_at_most_once_rep, iterator_find_map)]
#![allow(intra_doc_link_resolution_failure)] #![allow(intra_doc_link_resolution_failure)]
extern crate syntax; extern crate syntax;
@ -100,6 +100,7 @@ pub struct CodegenCx<'a, 'tcx: 'a, B: Backend + 'a> {
pub tcx: TyCtxt<'a, 'tcx, 'tcx>, pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
pub module: &'a mut Module<B>, pub module: &'a mut Module<B>,
pub constants: HashMap<AllocId, DataId>, pub constants: HashMap<AllocId, DataId>,
pub defined_functions: Vec<FuncId>,
} }
struct CraneliftMetadataLoader; struct CraneliftMetadataLoader;
@ -241,12 +242,13 @@ impl CodegenBackend for CraneliftCodegenBackend {
let mut module: Module<SimpleJITBackend> = Module::new(SimpleJITBuilder::new()); let mut module: Module<SimpleJITBackend> = Module::new(SimpleJITBuilder::new());
let mut context = Context::new(); let mut context = Context::new();
{ let defined_functions = {
use std::io::Write; use std::io::Write;
let mut cx = CodegenCx { let mut cx = CodegenCx {
tcx, tcx,
module: &mut module, module: &mut module,
constants: HashMap::new(), constants: HashMap::new(),
defined_functions: Vec::new(),
}; };
let mut log = ::std::fs::File::create("../target/log.txt").unwrap(); let mut log = ::std::fs::File::create("../target/log.txt").unwrap();
@ -269,49 +271,61 @@ impl CodegenBackend for CraneliftCodegenBackend {
} }
} }
} }
}
std::mem::replace(&mut cx.defined_functions, Vec::new())
};
tcx.sess.warn("Compiled everything"); tcx.sess.warn("Compiled everything");
// TODO: this doesn't work most of the time // TODO: this doesn't work most of the time
if false { if false {
module.finalize_all(); let call_instance = collector::collect_crate_mono_items(tcx, collector::MonoItemCollectionMode::Eager).0.into_iter().find_map(|mono_item| {
tcx.sess.warn("Finalized everything");
for mono_item in
collector::collect_crate_mono_items(tcx, collector::MonoItemCollectionMode::Eager).0
{
let inst = match mono_item { let inst = match mono_item {
MonoItem::Fn(inst) => inst, MonoItem::Fn(inst) => inst,
_ => continue, _ => return None,
}; };
//if tcx.absolute_item_path_str(inst.def_id()) != "example::ret_42" { //if tcx.absolute_item_path_str(inst.def_id()) != "example::ret_42" {
if tcx.absolute_item_path_str(inst.def_id()) != "example::option_unwrap_or" { if tcx.absolute_item_path_str(inst.def_id()) == "example::option_unwrap_or" {
continue; Some(inst)
} else {
None
} }
}).unwrap();
let fn_ty = inst.ty(tcx);
let sig = cton_sig_from_fn_ty(tcx, fn_ty); let fn_ty = call_instance.ty(tcx);
let def_path_based_names = let sig = cton_sig_from_fn_ty(tcx, fn_ty);
::rustc_mir::monomorphize::item::DefPathBasedNames::new(tcx, false, false); let def_path_based_names =
let mut name = String::new(); ::rustc_mir::monomorphize::item::DefPathBasedNames::new(tcx, false, false);
def_path_based_names.push_instance_as_string(inst, &mut name); let mut name = String::new();
let func_id = module def_path_based_names.push_instance_as_string(call_instance, &mut name);
.declare_function(&name, Linkage::Import, &sig) let called_func_id = module
.unwrap(); .declare_function(&name, Linkage::Import, &sig)
.unwrap();
let finalized_function: *const u8 = module.finalize_function(func_id); for func_id in defined_functions {
/*let f: extern "C" fn(&mut u32) = unsafe { ::std::mem::transmute(finalized_function) }; if func_id != called_func_id {
let mut res = 0u32; module.finalize_function(func_id);
f(&mut res); }
tcx.sess.warn(&format!("ret_42 returned {}", res));*/
let f: extern "C" fn(&mut bool, &u8, bool) =
unsafe { ::std::mem::transmute(finalized_function) };
let mut res = false;
f(&mut res, &3, false);
tcx.sess.warn(&format!("option_unwrap_or returned {}", res));
} }
tcx.sess.warn("Finalized everything");
let finalized_function: *const u8 = module.finalize_function(called_func_id);
/*let f: extern "C" fn(&mut u32) = unsafe { ::std::mem::transmute(finalized_function) };
let mut res = 0u32;
f(&mut res);
tcx.sess.warn(&format!("ret_42 returned {}", res));*/
/*let f: extern "C" fn(&mut bool, &u8, bool) =
- unsafe { ::std::mem::transmute(finalized_function) };
let mut res = false;
f(&mut res, &3, false);
tcx.sess.warn(&format!("option_unwrap_or returned {}", res));*/
let f: extern "C" fn(&mut u8, isize, *const *const u8) =
unsafe { ::std::mem::transmute(finalized_function) };
let mut res = 0;
f(&mut res, 0, 0 as *const _);
tcx.sess.warn(&format!("main returned {}", res));
module.finish(); module.finish();
} }