From bb017326bef9e0a7af87043d6e7ce7a62a52da75 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sat, 30 Jun 2018 16:37:02 +0200 Subject: [PATCH] Move some code around --- src/base.rs | 140 ++++++++++++++-------------------------------------- src/lib.rs | 68 ++++++++++++++++++++++++- 2 files changed, 103 insertions(+), 105 deletions(-) diff --git a/src/base.rs b/src/base.rs index a044620fd74..f077e8f9e4a 100644 --- a/src/base.rs +++ b/src/base.rs @@ -1,121 +1,55 @@ -use rustc_mir::monomorphize::MonoItem; - -use cretonne_module::{Module, Backend, FuncId, Linkage}; -use cretonne_simplejit::{SimpleJITBuilder, SimpleJITBackend}; - -use std::any::Any; -use std::collections::HashMap; - use prelude::*; -pub fn trans_crate<'a, 'tcx: 'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Box { - let link_meta = ::build_link_meta(tcx.crate_hash(LOCAL_CRATE)); - let metadata = tcx.encode_metadata(&link_meta); +pub fn trans_mono_item<'a, 'tcx: 'a>(cx: &mut CodegenCx<'a, 'tcx, CurrentBackend>, context: &mut Context, mono_item: MonoItem<'tcx>) { + let tcx = cx.tcx; - let mut module: Module = Module::new(SimpleJITBuilder::new()); - let mut context = Context::new(); - let mut def_id_fn_id_map = HashMap::new(); + match mono_item { + MonoItem::Fn(inst) => match inst { + Instance { + def: InstanceDef::Item(def_id), + substs, + } => { + let sig = tcx.fn_sig(def_id); + let sig = cton_sig_from_fn_sig(tcx, sig, substs); + let func_id = { + let module = &mut cx.module; + *cx.def_id_fn_id_map.entry(inst).or_insert_with(|| { + module.declare_function(&tcx.absolute_item_path_str(def_id), Linkage::Local, &sig).unwrap() + }) + }; - { - let mut cx = CodegenCx { - tcx, - module: &mut module, - def_id_fn_id_map: &mut def_id_fn_id_map, - }; - let cx = &mut cx; + let mut f = Function::with_name_signature(ExternalName::user(0, func_id.index() as u32), sig); - for mono_item in - collector::collect_crate_mono_items( - tcx, - collector::MonoItemCollectionMode::Eager - ).0 { - match mono_item { - MonoItem::Fn(inst) => match inst { - Instance { - def: InstanceDef::Item(def_id), - substs, - } => { - let sig = tcx.fn_sig(def_id); - let sig = cton_sig_from_fn_sig(tcx, sig, substs); - let func_id = { - let module = &mut cx.module; - *cx.def_id_fn_id_map.entry(inst).or_insert_with(|| { - module.declare_function(&tcx.absolute_item_path_str(def_id), Linkage::Local, &sig).unwrap() - }) - }; + let mut mir = ::std::io::Cursor::new(Vec::new()); + ::rustc_mir::util::write_mir_pretty(tcx, Some(def_id), &mut mir).unwrap(); + tcx.sess.warn(&format!("{:?}:\n\n{}", def_id, String::from_utf8_lossy(&mir.into_inner()))); - let mut f = Function::with_name_signature(ExternalName::user(0, func_id.index() as u32), sig); + ::base::trans_fn(cx, &mut f, inst); - let mut mir = ::std::io::Cursor::new(Vec::new()); - ::rustc_mir::util::write_mir_pretty(cx.tcx, Some(def_id), &mut mir).unwrap(); - tcx.sess.warn(&format!("{:?}:\n\n{}", def_id, String::from_utf8_lossy(&mir.into_inner()))); + let mut cton = String::new(); + ::cretonne::codegen::write_function(&mut cton, &f, None).unwrap(); + tcx.sess.warn(&cton); - trans_fn(cx, &mut f, inst); - - let mut cton = String::new(); - ::cretonne::codegen::write_function(&mut cton, &f, None).unwrap(); - tcx.sess.warn(&cton); - - let flags = settings::Flags::new(settings::builder()); - match ::cretonne::codegen::verify_function(&f, &flags) { - Ok(_) => {} - Err(err) => { - let pretty_error = ::cretonne::codegen::print_errors::pretty_verifier_error(&f, None, &err); - tcx.sess.fatal(&format!("cretonne verify error:\n{}", pretty_error)); - } - } - - context.func = f; - cx.module.define_function(func_id, &mut context).unwrap(); - context.clear(); + let flags = settings::Flags::new(settings::builder()); + match ::cretonne::codegen::verify_function(&f, &flags) { + Ok(_) => {} + Err(err) => { + let pretty_error = ::cretonne::codegen::print_errors::pretty_verifier_error(&f, None, &err); + tcx.sess.fatal(&format!("cretonne verify error:\n{}", pretty_error)); } - _ => {} } - _ => {} + + context.func = f; + cx.module.define_function(func_id, context).unwrap(); + context.clear(); } + _ => {} } + _ => {} } - - tcx.sess.warn("Compiled everything"); - - module.finalize_all(); - - tcx.sess.warn("Finalized everything"); - - for (inst, func_id) in def_id_fn_id_map.iter() { - //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" { - continue; - } - let finalized_function: *const u8 = module.finalize_function(*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)); - } - - module.finish(); - - tcx.sess.fatal("unimplemented"); - - Box::new(::OngoingCodegen { - metadata: metadata, - //translated_module: Module::new(::cretonne_faerie::FaerieBuilder::new(, - crate_name: tcx.crate_name(LOCAL_CRATE), - }) } -struct CodegenCx<'a, 'tcx: 'a, B: Backend + 'a> { - tcx: TyCtxt<'a, 'tcx, 'tcx>, - module: &'a mut Module, - def_id_fn_id_map: &'a mut HashMap, FuncId>, -} - -fn trans_fn<'a, 'tcx: 'a>(cx: &mut CodegenCx<'a, 'tcx, CurrentBackend>, f: &mut Function, instance: Instance<'tcx>) { +pub fn trans_fn<'a, 'tcx: 'a>(cx: &mut CodegenCx<'a, 'tcx, CurrentBackend>, f: &mut Function, instance: Instance<'tcx>) { let mir = cx.tcx.optimized_mir(instance.def_id()); let mut func_ctx = FunctionBuilderContext::new(); let mut bcx: FunctionBuilder = FunctionBuilder::new(f, &mut func_ctx); diff --git a/src/lib.rs b/src/lib.rs index 5cd275b94db..d37352b89e8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -37,6 +37,7 @@ mod base; mod common; mod prelude { + pub use std::any::Any; pub use std::collections::HashMap; pub use rustc::hir::def_id::{DefId, LOCAL_CRATE}; @@ -49,20 +50,30 @@ mod prelude { TypeFoldable, TypeVariants, }; pub use rustc_data_structures::{indexed_vec::Idx, sync::Lrc}; - pub use rustc_mir::monomorphize::collector; + pub use rustc_mir::monomorphize::{MonoItem, collector}; pub use cretonne::codegen::ir::{ condcodes::IntCC, function::Function, ExternalName, FuncRef, StackSlot, }; pub use cretonne::codegen::Context; pub use cretonne::prelude::*; + pub use cretonne_module::{Module, Backend, FuncId, Linkage}; + pub use cretonne_simplejit::{SimpleJITBuilder, SimpleJITBackend}; pub use common::Variable; pub use common::*; + + pub use CodegenCx; } use prelude::*; +pub struct CodegenCx<'a, 'tcx: 'a, B: Backend + 'a> { + pub tcx: TyCtxt<'a, 'tcx, 'tcx>, + pub module: &'a mut Module, + pub def_id_fn_id_map: &'a mut HashMap, FuncId>, +} + struct CretonneCodegenBackend(()); struct OngoingCodegen { @@ -151,7 +162,60 @@ impl CodegenBackend for CretonneCodegenBackend { } tcx.sess.abort_if_errors(); - base::trans_crate(tcx) + let link_meta = ::build_link_meta(tcx.crate_hash(LOCAL_CRATE)); + let metadata = tcx.encode_metadata(&link_meta); + + let mut module: Module = Module::new(SimpleJITBuilder::new()); + let mut context = Context::new(); + let mut def_id_fn_id_map = HashMap::new(); + + { + let mut cx = CodegenCx { + tcx, + module: &mut module, + def_id_fn_id_map: &mut def_id_fn_id_map, + }; + + for mono_item in + collector::collect_crate_mono_items( + tcx, + collector::MonoItemCollectionMode::Eager + ).0 { + base::trans_mono_item(&mut cx, &mut context, mono_item) + } + } + + tcx.sess.warn("Compiled everything"); + + module.finalize_all(); + + tcx.sess.warn("Finalized everything"); + + for (inst, func_id) in def_id_fn_id_map.iter() { + //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" { + continue; + } + let finalized_function: *const u8 = module.finalize_function(*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)); + } + + module.finish(); + + tcx.sess.fatal("unimplemented"); + + Box::new(::OngoingCodegen { + metadata: metadata, + //translated_module: Module::new(::cretonne_faerie::FaerieBuilder::new(, + crate_name: tcx.crate_name(LOCAL_CRATE), + }) } fn join_codegen_and_link(