Make the hello world example runnable
This commit is contained in:
parent
9a8c25ae75
commit
46ebc2bc5c
4 changed files with 48 additions and 69 deletions
10
build.sh
10
build.sh
|
@ -14,12 +14,12 @@ else
|
|||
exit 1
|
||||
fi
|
||||
|
||||
RUSTC="rustc -Zcodegen-backend=$(pwd)/../target/debug/librustc_codegen_cranelift.$dylib_ext -L crate=. --crate-type lib -Og"
|
||||
RUSTC="rustc -Zcodegen-backend=$(pwd)/../target/debug/librustc_codegen_cranelift.$dylib_ext -L crate=. -Og"
|
||||
|
||||
$RUSTC mini_core.rs --crate-name mini_core &&
|
||||
$RUSTC example.rs &&
|
||||
$RUSTC mini_core_hello_world.rs &&
|
||||
$RUSTC mini_core.rs --crate-name mini_core --crate-type lib &&
|
||||
$RUSTC example.rs --crate-type lib &&
|
||||
$RUSTC mini_core_hello_world.rs --crate-type bin &&
|
||||
|
||||
$RUSTC ../target/libcore/src/libcore/lib.rs --color=always 2>&1 | (head -n 10; echo "===="; tail -n 1000)
|
||||
$RUSTC ../target/libcore/src/libcore/lib.rs --color=always --crate-type lib 2>&1 | (head -n 20; echo "===="; tail -n 1000)
|
||||
cat ../target/log.txt | sort | uniq -c | grep -v "rval unsize move" | grep -v "rval len"
|
||||
rm *.rlib ../target/log.txt
|
||||
|
|
|
@ -50,7 +50,13 @@ pub fn trans_mono_item<'a, 'tcx: 'a>(
|
|||
|
||||
context.func = func;
|
||||
// TODO: cranelift doesn't yet support some of the things needed
|
||||
if false {
|
||||
if cx
|
||||
.tcx
|
||||
.sess
|
||||
.crate_types
|
||||
.get()
|
||||
.contains(&CrateType::Executable)
|
||||
{
|
||||
cx.module.define_function(func_id, context).unwrap();
|
||||
cx.defined_functions.push(func_id);
|
||||
}
|
||||
|
|
|
@ -79,7 +79,13 @@ fn trans_const_place<'a, 'tcx: 'a>(
|
|||
) -> CPlace<'tcx> {
|
||||
let ty = fx.monomorphize(&const_.ty);
|
||||
let layout = fx.layout_of(ty);
|
||||
if true {
|
||||
if !fx
|
||||
.tcx
|
||||
.sess
|
||||
.crate_types
|
||||
.get()
|
||||
.contains(&CrateType::Executable)
|
||||
{
|
||||
// TODO: cranelift-module api seems to be used wrong,
|
||||
// thus causing panics for some consts, so this disables it
|
||||
return CPlace::Addr(fx.bcx.ins().iconst(types::I64, 0), layout);
|
||||
|
|
63
src/lib.rs
63
src/lib.rs
|
@ -29,10 +29,7 @@ use std::sync::{mpsc, Arc};
|
|||
|
||||
use rustc::dep_graph::DepGraph;
|
||||
use rustc::middle::cstore::MetadataLoader;
|
||||
use rustc::session::{
|
||||
config::{CrateType, OutputFilenames},
|
||||
CompileIncomplete,
|
||||
};
|
||||
use rustc::session::{config::OutputFilenames, CompileIncomplete};
|
||||
use rustc::ty::query::Providers;
|
||||
use rustc_codegen_utils::codegen_backend::CodegenBackend;
|
||||
use rustc_codegen_utils::link::{build_link_meta, out_filename};
|
||||
|
@ -65,7 +62,7 @@ mod prelude {
|
|||
pub use rustc::mir;
|
||||
pub use rustc::mir::interpret::AllocId;
|
||||
pub use rustc::mir::*;
|
||||
pub use rustc::session::Session;
|
||||
pub use rustc::session::{config::CrateType, Session};
|
||||
pub use rustc::ty::layout::{self, LayoutOf, Size, TyLayout};
|
||||
pub use rustc::ty::{
|
||||
self, subst::Substs, FnSig, Instance, InstanceDef, ParamEnv, PolyFnSig, Ty, TyCtxt,
|
||||
|
@ -278,27 +275,11 @@ impl CodegenBackend for CraneliftCodegenBackend {
|
|||
tcx.sess.warn("Compiled everything");
|
||||
|
||||
// TODO: this doesn't work most of the time
|
||||
if false {
|
||||
let call_instance =
|
||||
collector::collect_crate_mono_items(tcx, collector::MonoItemCollectionMode::Eager)
|
||||
.0
|
||||
.into_iter()
|
||||
.find_map(|mono_item| {
|
||||
let inst = match mono_item {
|
||||
MonoItem::Fn(inst) => inst,
|
||||
_ => return None,
|
||||
};
|
||||
if tcx.sess.crate_types.get().contains(&CrateType::Executable) {
|
||||
let start_wrapper = tcx.lang_items().start_fn().expect("no start lang item");
|
||||
|
||||
//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"
|
||||
{
|
||||
Some(inst)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}).unwrap();
|
||||
|
||||
let (name, sig) = crate::abi::get_function_name_and_sig(tcx, call_instance);
|
||||
let (name, sig) =
|
||||
crate::abi::get_function_name_and_sig(tcx, Instance::mono(tcx, start_wrapper));
|
||||
let called_func_id = module
|
||||
.declare_function(&name, Linkage::Import, &sig)
|
||||
.unwrap();
|
||||
|
@ -311,19 +292,9 @@ impl CodegenBackend for CraneliftCodegenBackend {
|
|||
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) =
|
||||
let f: extern "C" fn(*const u8, isize, *const *const u8) -> isize =
|
||||
unsafe { ::std::mem::transmute(finalized_function) };
|
||||
let mut res = 0;
|
||||
f(&mut res, 0, 0 as *const _);
|
||||
let res = f(0 as *const u8, 0, 0 as *const _);
|
||||
tcx.sess.warn(&format!("main returned {}", res));
|
||||
|
||||
module.finish();
|
||||
|
@ -370,11 +341,11 @@ impl CodegenBackend for CraneliftCodegenBackend {
|
|||
).unwrap();
|
||||
|
||||
for &crate_type in sess.opts.crate_types.iter() {
|
||||
if crate_type != CrateType::Rlib
|
||||
/*&& crate_type != CrateType::Dylib*/
|
||||
{
|
||||
sess.fatal(&format!("Unsupported crate type: {:?}", crate_type));
|
||||
}
|
||||
match crate_type {
|
||||
CrateType::Executable => {
|
||||
sess.warn("Rustc codegen cranelift doesn't produce executables, but is a JIT for them");
|
||||
},
|
||||
CrateType::Rlib /* | CrateType::Dylib */ => {
|
||||
let output_name = out_filename(
|
||||
sess,
|
||||
crate_type,
|
||||
|
@ -390,12 +361,8 @@ impl CodegenBackend for CraneliftCodegenBackend {
|
|||
).unwrap();
|
||||
//artifact.write(file).unwrap();
|
||||
}
|
||||
|
||||
sess.abort_if_errors();
|
||||
if !sess.opts.crate_types.contains(&CrateType::Rlib)
|
||||
&& !sess.opts.crate_types.contains(&CrateType::Dylib)
|
||||
{
|
||||
sess.fatal("Executables are not supported by the metadata-only backend.");
|
||||
_ => sess.fatal(&format!("Unsupported crate type: {:?}", crate_type)),
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue