mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-26 16:54:01 +00:00
Support emitting object files (fixes #5)
This commit is contained in:
parent
0f26781a86
commit
a8ca0f02fc
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
/target
|
||||
**/*.rs.bk
|
||||
*.rlib
|
||||
*.o
|
||||
|
28
build.sh
28
build.sh
@ -12,12 +12,28 @@ else
|
||||
exit 1
|
||||
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=."
|
||||
|
||||
SHOULD_CODEGEN=1 $RUSTC examples/mini_core.rs --crate-name mini_core --crate-type lib &&
|
||||
$RUSTC examples/example.rs --crate-type lib &&
|
||||
$RUSTC examples/mini_core_hello_world.rs --crate-type bin &&
|
||||
pushd target/libcore
|
||||
|
||||
$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)
|
||||
cat target/log.txt | sort | uniq -c | grep -v "rval unsize move" | grep -v "rval len"
|
||||
rm *.rlib target/log.txt
|
||||
SHOULD_CODEGEN=1 $RUSTC ../../examples/mini_core.rs --crate-name mini_core --crate-type lib &&
|
||||
extract_data libmini_core.rlib mini_core.o &&
|
||||
|
||||
$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"
|
||||
|
@ -101,7 +101,7 @@ fn trans_fn<'a, 'tcx: 'a>(
|
||||
|
||||
// Step 9. Define function
|
||||
// TODO: cranelift doesn't yet support some of the things needed
|
||||
if should_codegen(tcx) {
|
||||
if should_codegen(tcx.sess) {
|
||||
context.func = func;
|
||||
module.define_function(func_id, context).unwrap();
|
||||
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));
|
||||
// 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 mut jt_data = JumpTableData::new();
|
||||
for (i, value) in values.iter().enumerate() {
|
||||
|
50
src/lib.rs
50
src/lib.rs
@ -34,6 +34,7 @@ use rustc::ty::query::Providers;
|
||||
use rustc_codegen_utils::codegen_backend::CodegenBackend;
|
||||
use rustc_codegen_utils::link::{build_link_meta, out_filename};
|
||||
use rustc_data_structures::owning_ref::{self, OwningRef};
|
||||
use rustc_data_structures::svh::Svh;
|
||||
use syntax::symbol::Symbol;
|
||||
|
||||
use cranelift::codegen::settings;
|
||||
@ -99,9 +100,9 @@ mod prelude {
|
||||
|
||||
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()
|
||||
|| 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> {
|
||||
jit: Option<T>,
|
||||
#[allow(dead_code)]
|
||||
faerie: Option<T>,
|
||||
}
|
||||
|
||||
@ -135,7 +137,7 @@ impl MetadataLoader for CraneliftMetadataLoader {
|
||||
// Iterate over all entries in the archive:
|
||||
while let Some(entry_result) = archive.next_entry() {
|
||||
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();
|
||||
::std::io::copy(&mut entry, &mut buf).map_err(|e| format!("{:?}", e))?;
|
||||
let buf: OwningRef<Vec<u8>, [u8]> = OwningRef::new(buf).into();
|
||||
@ -182,6 +184,7 @@ struct OngoingCodegen {
|
||||
product: cranelift_faerie::FaerieProduct,
|
||||
metadata: Vec<u8>,
|
||||
crate_name: Symbol,
|
||||
crate_hash: Svh,
|
||||
}
|
||||
|
||||
impl CodegenBackend for CraneliftCodegenBackend {
|
||||
@ -190,8 +193,8 @@ impl CodegenBackend for CraneliftCodegenBackend {
|
||||
match *cty {
|
||||
CrateType::Rlib | CrateType::Dylib | CrateType::Executable => {}
|
||||
_ => {
|
||||
sess.parse_sess.span_diagnostic.warn(&format!(
|
||||
"LLVM unsupported, so output type {} is not supported",
|
||||
sess.err(&format!(
|
||||
"Rustc codegen cranelift doesn't support output type {}",
|
||||
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();
|
||||
|
||||
let link_meta = ::build_link_meta(tcx.crate_hash(LOCAL_CRATE));
|
||||
@ -287,7 +298,7 @@ impl CodegenBackend for CraneliftCodegenBackend {
|
||||
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 mono_items =
|
||||
@ -338,7 +349,8 @@ impl CodegenBackend for CraneliftCodegenBackend {
|
||||
tcx.sess.warn("Compiled everything");
|
||||
|
||||
// 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 (name, sig) =
|
||||
@ -361,7 +373,8 @@ impl CodegenBackend for CraneliftCodegenBackend {
|
||||
tcx.sess.warn(&format!("main returned {}", res));
|
||||
|
||||
jit_module.finish();
|
||||
} else if should_codegen(tcx) {
|
||||
::std::process::exit(0);
|
||||
} else if should_codegen(tcx.sess) {
|
||||
jit_module.finalize_all();
|
||||
faerie_module.finalize_all();
|
||||
|
||||
@ -372,6 +385,7 @@ impl CodegenBackend for CraneliftCodegenBackend {
|
||||
product: faerie_module.finish(),
|
||||
metadata: metadata.raw_data,
|
||||
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 metadata = ongoing_codegen.metadata;
|
||||
|
||||
let metadata_name = ".rustc.clif_metadata".to_string() + &ongoing_codegen.crate_hash.to_string();
|
||||
artifact
|
||||
.declare_with(
|
||||
".rustc.clif_metadata",
|
||||
&metadata_name,
|
||||
faerie::artifact::Decl::Data {
|
||||
global: true,
|
||||
writeable: false,
|
||||
@ -401,10 +416,8 @@ impl CodegenBackend for CraneliftCodegenBackend {
|
||||
|
||||
for &crate_type in sess.opts.crate_types.iter() {
|
||||
match crate_type {
|
||||
CrateType::Executable => {
|
||||
sess.warn("Rustc codegen cranelift doesn't produce executables, but is a JIT for them");
|
||||
},
|
||||
CrateType::Rlib /* | CrateType::Dylib */ => {
|
||||
// TODO: link executable
|
||||
CrateType::Executable | CrateType::Rlib => {
|
||||
let output_name = out_filename(
|
||||
sess,
|
||||
crate_type,
|
||||
@ -415,10 +428,17 @@ impl CodegenBackend for CraneliftCodegenBackend {
|
||||
let mut builder = ar::Builder::new(file);
|
||||
builder
|
||||
.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()),
|
||||
).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)),
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user