1
Fork 0

Support emitting object files (fixes #5)

This commit is contained in:
bjorn3 2018-08-14 22:01:18 +02:00
parent 0f26781a86
commit a8ca0f02fc
4 changed files with 60 additions and 23 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
/target /target
**/*.rs.bk **/*.rs.bk
*.rlib *.rlib
*.o

View file

@ -12,12 +12,28 @@ else
exit 1 exit 1
fi fi
extract_data() {
ar x $1 data.o &&
chmod +rw data.o &&
mv data.o $2
}
RUSTC="rustc -Zcodegen-backend=$(pwd)/target/debug/librustc_codegen_cranelift.$dylib_ext -L crate=." RUSTC="rustc -Zcodegen-backend=$(pwd)/target/debug/librustc_codegen_cranelift.$dylib_ext -L crate=."
SHOULD_CODEGEN=1 $RUSTC examples/mini_core.rs --crate-name mini_core --crate-type lib && pushd target/libcore
$RUSTC examples/example.rs --crate-type lib &&
$RUSTC examples/mini_core_hello_world.rs --crate-type bin &&
$RUSTC target/libcore/src/libcore/lib.rs --color=always --crate-type lib -Cincremental=target/libcore/incremental 2>&1 | (head -n 20; echo "===="; tail -n 1000) SHOULD_CODEGEN=1 $RUSTC ../../examples/mini_core.rs --crate-name mini_core --crate-type lib &&
cat target/log.txt | sort | uniq -c | grep -v "rval unsize move" | grep -v "rval len" extract_data libmini_core.rlib mini_core.o &&
rm *.rlib target/log.txt
$RUSTC ../../examples/example.rs --crate-type lib &&
SHOULD_RUN=1 $RUSTC ../../examples/mini_core_hello_world.rs --crate-type bin &&
$RUSTC ../../examples/mini_core_hello_world.rs --crate-type bin &&
extract_data mini_core_hello_world mini_core_hello_world.o
gcc mini_core.o mini_core_hello_world.o -o mini_core_hello_world &&
./mini_core_hello_world &&
$RUSTC src/libcore/lib.rs --color=always --crate-type lib -Cincremental=incremental 2>&1 | (head -n 20; echo "===="; tail -n 1000)
cat log.txt | sort | uniq -c | grep -v "rval unsize move" | grep -v "rval len"

View file

@ -101,7 +101,7 @@ fn trans_fn<'a, 'tcx: 'a>(
// Step 9. Define function // Step 9. Define function
// TODO: cranelift doesn't yet support some of the things needed // TODO: cranelift doesn't yet support some of the things needed
if should_codegen(tcx) { if should_codegen(tcx.sess) {
context.func = func; context.func = func;
module.define_function(func_id, context).unwrap(); module.define_function(func_id, context).unwrap();
context.clear(); context.clear();
@ -182,7 +182,7 @@ fn codegen_fn_content<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx, impl Backend>)
} => { } => {
fx.bcx.ins().trap(TrapCode::User(0)); fx.bcx.ins().trap(TrapCode::User(0));
// TODO: prevent panics on large and negative disciminants // TODO: prevent panics on large and negative disciminants
if should_codegen(fx.tcx) { if should_codegen(fx.tcx.sess) {
let discr = trans_operand(fx, discr).load_value(fx); let discr = trans_operand(fx, discr).load_value(fx);
let mut jt_data = JumpTableData::new(); let mut jt_data = JumpTableData::new();
for (i, value) in values.iter().enumerate() { for (i, value) in values.iter().enumerate() {

View file

@ -34,6 +34,7 @@ use rustc::ty::query::Providers;
use rustc_codegen_utils::codegen_backend::CodegenBackend; use rustc_codegen_utils::codegen_backend::CodegenBackend;
use rustc_codegen_utils::link::{build_link_meta, out_filename}; use rustc_codegen_utils::link::{build_link_meta, out_filename};
use rustc_data_structures::owning_ref::{self, OwningRef}; use rustc_data_structures::owning_ref::{self, OwningRef};
use rustc_data_structures::svh::Svh;
use syntax::symbol::Symbol; use syntax::symbol::Symbol;
use cranelift::codegen::settings; use cranelift::codegen::settings;
@ -99,9 +100,9 @@ mod prelude {
pub use crate::{CodegenCx, ModuleTup}; pub use crate::{CodegenCx, ModuleTup};
pub fn should_codegen(tcx: TyCtxt) -> bool { pub fn should_codegen(sess: &Session) -> bool {
::std::env::var("SHOULD_CODEGEN").is_ok() ::std::env::var("SHOULD_CODEGEN").is_ok()
|| tcx.sess.crate_types.get().contains(&CrateType::Executable) || sess.crate_types.get().contains(&CrateType::Executable)
} }
} }
@ -120,6 +121,7 @@ pub struct CodegenCx<'a, 'tcx: 'a> {
pub struct ModuleTup<T> { pub struct ModuleTup<T> {
jit: Option<T>, jit: Option<T>,
#[allow(dead_code)]
faerie: Option<T>, faerie: Option<T>,
} }
@ -135,7 +137,7 @@ impl MetadataLoader for CraneliftMetadataLoader {
// Iterate over all entries in the archive: // Iterate over all entries in the archive:
while let Some(entry_result) = archive.next_entry() { while let Some(entry_result) = archive.next_entry() {
let mut entry = entry_result.map_err(|e| format!("{:?}", e))?; let mut entry = entry_result.map_err(|e| format!("{:?}", e))?;
if entry.header().identifier() == b".rustc.clif_metadata" { if entry.header().identifier().starts_with(b".rustc.clif_metadata") {
let mut buf = Vec::new(); let mut buf = Vec::new();
::std::io::copy(&mut entry, &mut buf).map_err(|e| format!("{:?}", e))?; ::std::io::copy(&mut entry, &mut buf).map_err(|e| format!("{:?}", e))?;
let buf: OwningRef<Vec<u8>, [u8]> = OwningRef::new(buf).into(); let buf: OwningRef<Vec<u8>, [u8]> = OwningRef::new(buf).into();
@ -182,6 +184,7 @@ struct OngoingCodegen {
product: cranelift_faerie::FaerieProduct, product: cranelift_faerie::FaerieProduct,
metadata: Vec<u8>, metadata: Vec<u8>,
crate_name: Symbol, crate_name: Symbol,
crate_hash: Svh,
} }
impl CodegenBackend for CraneliftCodegenBackend { impl CodegenBackend for CraneliftCodegenBackend {
@ -190,8 +193,8 @@ impl CodegenBackend for CraneliftCodegenBackend {
match *cty { match *cty {
CrateType::Rlib | CrateType::Dylib | CrateType::Executable => {} CrateType::Rlib | CrateType::Dylib | CrateType::Executable => {}
_ => { _ => {
sess.parse_sess.span_diagnostic.warn(&format!( sess.err(&format!(
"LLVM unsupported, so output type {} is not supported", "Rustc codegen cranelift doesn't support output type {}",
cty cty
)); ));
} }
@ -255,6 +258,14 @@ impl CodegenBackend for CraneliftCodegenBackend {
_ => {} _ => {}
} }
} }
if !tcx.sess.crate_types.get().contains(&CrateType::Executable)
&& std::env::var("SHOULD_RUN").is_ok()
{
tcx.sess
.err("Can't JIT run non executable (SHOULD_RUN env var is set)");
}
tcx.sess.abort_if_errors(); tcx.sess.abort_if_errors();
let link_meta = ::build_link_meta(tcx.crate_hash(LOCAL_CRATE)); let link_meta = ::build_link_meta(tcx.crate_hash(LOCAL_CRATE));
@ -287,7 +298,7 @@ impl CodegenBackend for CraneliftCodegenBackend {
context: Context::new(), context: Context::new(),
}; };
let mut log = ::std::fs::File::create("target/log.txt").unwrap(); let mut log = ::std::fs::File::create("log.txt").unwrap();
let before = ::std::time::Instant::now(); let before = ::std::time::Instant::now();
let mono_items = let mono_items =
@ -338,7 +349,8 @@ impl CodegenBackend for CraneliftCodegenBackend {
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 tcx.sess.crate_types.get().contains(&CrateType::Executable) { if std::env::var("SHOULD_RUN").is_ok() {
tcx.sess.warn("Rustc codegen cranelift will JIT run the executable, because the SHOULD_RUN env var is set");
let start_wrapper = tcx.lang_items().start_fn().expect("no start lang item"); let start_wrapper = tcx.lang_items().start_fn().expect("no start lang item");
let (name, sig) = let (name, sig) =
@ -361,7 +373,8 @@ impl CodegenBackend for CraneliftCodegenBackend {
tcx.sess.warn(&format!("main returned {}", res)); tcx.sess.warn(&format!("main returned {}", res));
jit_module.finish(); jit_module.finish();
} else if should_codegen(tcx) { ::std::process::exit(0);
} else if should_codegen(tcx.sess) {
jit_module.finalize_all(); jit_module.finalize_all();
faerie_module.finalize_all(); faerie_module.finalize_all();
@ -372,6 +385,7 @@ impl CodegenBackend for CraneliftCodegenBackend {
product: faerie_module.finish(), product: faerie_module.finish(),
metadata: metadata.raw_data, metadata: metadata.raw_data,
crate_name: tcx.crate_name(LOCAL_CRATE), crate_name: tcx.crate_name(LOCAL_CRATE),
crate_hash: tcx.crate_hash(LOCAL_CRATE),
}) })
} }
@ -389,9 +403,10 @@ impl CodegenBackend for CraneliftCodegenBackend {
let mut artifact = ongoing_codegen.product.artifact; let mut artifact = ongoing_codegen.product.artifact;
let metadata = ongoing_codegen.metadata; let metadata = ongoing_codegen.metadata;
let metadata_name = ".rustc.clif_metadata".to_string() + &ongoing_codegen.crate_hash.to_string();
artifact artifact
.declare_with( .declare_with(
".rustc.clif_metadata", &metadata_name,
faerie::artifact::Decl::Data { faerie::artifact::Decl::Data {
global: true, global: true,
writeable: false, writeable: false,
@ -401,10 +416,8 @@ impl CodegenBackend for CraneliftCodegenBackend {
for &crate_type in sess.opts.crate_types.iter() { for &crate_type in sess.opts.crate_types.iter() {
match crate_type { match crate_type {
CrateType::Executable => { // TODO: link executable
sess.warn("Rustc codegen cranelift doesn't produce executables, but is a JIT for them"); CrateType::Executable | CrateType::Rlib => {
},
CrateType::Rlib /* | CrateType::Dylib */ => {
let output_name = out_filename( let output_name = out_filename(
sess, sess,
crate_type, crate_type,
@ -415,10 +428,17 @@ impl CodegenBackend for CraneliftCodegenBackend {
let mut builder = ar::Builder::new(file); let mut builder = ar::Builder::new(file);
builder builder
.append( .append(
&ar::Header::new(b".rustc.clif_metadata".to_vec(), metadata.len() as u64), &ar::Header::new(metadata_name.as_bytes().to_vec(), metadata.len() as u64),
::std::io::Cursor::new(metadata.clone()), ::std::io::Cursor::new(metadata.clone()),
).unwrap(); ).unwrap();
//artifact.write(file).unwrap(); if should_codegen(sess) {
let obj = artifact.emit().unwrap();
builder
.append(
&ar::Header::new(b"data.o".to_vec(), obj.len() as u64),
::std::io::Cursor::new(obj),
).unwrap();
}
} }
_ => sess.fatal(&format!("Unsupported crate type: {:?}", crate_type)), _ => sess.fatal(&format!("Unsupported crate type: {:?}", crate_type)),
} }