Skip to content

Commit

Permalink
Move ModuleDeclarations to backends
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn3 committed Sep 30, 2020
1 parent 7a6e909 commit b44c5bb
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 107 deletions.
20 changes: 12 additions & 8 deletions cranelift/module/src/backend.rs
Expand Up @@ -44,28 +44,34 @@ where
/// Return the `TargetIsa` to compile for.
fn isa(&self) -> &dyn TargetIsa;

/// Get all declarations in this module.
fn declarations(&self) -> &ModuleDeclarations;

/// Declare a function.
fn declare_function(&mut self, id: FuncId, name: &str, linkage: Linkage);
fn declare_function(
&mut self,
name: &str,
linkage: Linkage,
signature: &ir::Signature,
) -> ModuleResult<FuncId>;

/// Declare a data object.
fn declare_data(
&mut self,
id: DataId,
name: &str,
linkage: Linkage,
writable: bool,
tls: bool,
align: Option<u8>,
);
) -> ModuleResult<DataId>;

/// Define a function, producing the function body from the given `Context`.
///
/// Functions must be declared before being defined.
fn define_function<TS>(
&mut self,
id: FuncId,
func: FuncId,
ctx: &mut Context,
declarations: &ModuleDeclarations,
trap_sink: &mut TS,
) -> ModuleResult<ModuleCompiledFunction>
where
Expand All @@ -78,7 +84,6 @@ where
&mut self,
id: FuncId,
bytes: &[u8],
declarations: &ModuleDeclarations,
) -> ModuleResult<ModuleCompiledFunction>;

/// Define a zero-initialized data object of the given size.
Expand All @@ -88,12 +93,11 @@ where
&mut self,
id: DataId,
data_ctx: &DataContext,
declarations: &ModuleDeclarations,
) -> ModuleResult<()>;

/// Consume this `Backend` and return a result. Some implementations may
/// provide additional functionality through this result.
fn finish(self, declarations: ModuleDeclarations) -> Self::Product;
fn finish(self) -> Self::Product;
}

/// Default names for `ir::LibCall`s. A function by this name is imported into the object as
Expand Down
39 changes: 9 additions & 30 deletions cranelift/module/src/module.rs
Expand Up @@ -327,7 +327,6 @@ pub struct Module<B>
where
B: Backend,
{
declarations: ModuleDeclarations,
backend: B,
}

Expand All @@ -344,19 +343,14 @@ where
/// Create a new `Module`.
pub fn new(backend_builder: B::Builder) -> Self {
Self {
declarations: ModuleDeclarations {
names: HashMap::new(),
functions: PrimaryMap::new(),
data_objects: PrimaryMap::new(),
},
backend: B::new(backend_builder),
}
}

/// Get the module identifier for a given name, if that name
/// has been declared.
pub fn get_name(&self, name: &str) -> Option<FuncOrDataId> {
self.declarations.names.get(name).cloned()
self.backend.declarations().get_name(name)
}

/// Return the target information needed by frontends to produce Cranelift IR
Expand Down Expand Up @@ -406,16 +400,7 @@ where
linkage: Linkage,
signature: &ir::Signature,
) -> ModuleResult<FuncId> {
let (id, decl) = self
.declarations
.declare_function(name, linkage, signature)?;
self.backend.declare_function(id, name, decl.linkage);
Ok(id)
}

/// An iterator over functions that have been declared in this module.
pub fn declared_functions(&self) -> core::slice::Iter<'_, FunctionDeclaration> {
self.declarations.functions.values()
self.backend.declare_function(name, linkage, signature)
}

/// Declare a data object in this module.
Expand All @@ -427,20 +412,16 @@ where
tls: bool,
align: Option<u8>, // An alignment bigger than 128 is unlikely
) -> ModuleResult<DataId> {
let (id, decl) = self
.declarations
.declare_data(name, linkage, writable, tls, align)?;
self.backend
.declare_data(id, name, decl.linkage, decl.writable, decl.tls, decl.align);
Ok(id)
.declare_data(name, linkage, writable, tls, align)
}

/// Use this when you're building the IR of a function to reference a function.
///
/// TODO: Coalesce redundant decls and signatures.
/// TODO: Look into ways to reduce the risk of using a FuncRef in the wrong function.
pub fn declare_func_in_func(&self, func: FuncId, in_func: &mut ir::Function) -> ir::FuncRef {
let decl = &self.declarations.functions[func];
let decl = &self.backend.declarations().functions[func];
let signature = in_func.import_signature(decl.signature.clone());
let colocated = decl.linkage.is_final();
in_func.import_function(ir::ExtFuncData {
Expand All @@ -454,7 +435,7 @@ where
///
/// TODO: Same as above.
pub fn declare_data_in_func(&self, data: DataId, func: &mut ir::Function) -> ir::GlobalValue {
let decl = &self.declarations.data_objects[data];
let decl = &self.backend.declarations().data_objects[data];
let colocated = decl.linkage.is_final();
func.create_global_value(ir::GlobalValueData::Symbol {
name: ir::ExternalName::user(1, data.as_u32()),
Expand Down Expand Up @@ -488,8 +469,7 @@ where
where
TS: binemit::TrapSink,
{
self.backend
.define_function(func, ctx, &self.declarations, trap_sink)
self.backend.define_function(func, ctx, trap_sink)
}

/// Define a function, taking the function body from the given `bytes`.
Expand All @@ -504,13 +484,12 @@ where
func: FuncId,
bytes: &[u8],
) -> ModuleResult<ModuleCompiledFunction> {
self.backend
.define_function_bytes(func, bytes, &self.declarations)
self.backend.define_function_bytes(func, bytes)
}

/// Define a data object, producing the data contents from the given `DataContext`.
pub fn define_data(&mut self, data: DataId, data_ctx: &DataContext) -> ModuleResult<()> {
self.backend.define_data(data, data_ctx, &self.declarations)
self.backend.define_data(data, data_ctx)
}

/// Return the target isa
Expand All @@ -522,6 +501,6 @@ where
/// implementations may provide additional functionality available after
/// a `Module` is complete.
pub fn finish(self) -> B::Product {
self.backend.finish(self.declarations)
self.backend.finish()
}
}
69 changes: 40 additions & 29 deletions cranelift/object/src/backend.rs
Expand Up @@ -114,6 +114,7 @@ impl ObjectBuilder {
pub struct ObjectBackend {
isa: Box<dyn TargetIsa>,
object: Object,
declarations: ModuleDeclarations,
functions: SecondaryMap<FuncId, Option<(SymbolId, bool)>>,
data_objects: SecondaryMap<DataId, Option<(SymbolId, bool)>>,
relocs: Vec<SymbolRelocs>,
Expand All @@ -135,6 +136,7 @@ impl Backend for ObjectBackend {
Self {
isa: builder.isa,
object,
declarations: ModuleDeclarations::default(),
functions: SecondaryMap::new(),
data_objects: SecondaryMap::new(),
relocs: Vec::new(),
Expand All @@ -149,8 +151,21 @@ impl Backend for ObjectBackend {
&*self.isa
}

fn declare_function(&mut self, id: FuncId, name: &str, linkage: Linkage) {
let (scope, weak) = translate_linkage(linkage);
fn declarations(&self) -> &ModuleDeclarations {
&self.declarations
}

fn declare_function(
&mut self,
name: &str,
linkage: Linkage,
signature: &ir::Signature,
) -> ModuleResult<FuncId> {
let (id, decl) = self
.declarations
.declare_function(name, linkage, signature)?;

let (scope, weak) = translate_linkage(decl.linkage);

if let Some((function, _defined)) = self.functions[id] {
let symbol = self.object.symbol_mut(function);
Expand All @@ -169,23 +184,28 @@ impl Backend for ObjectBackend {
});
self.functions[id] = Some((symbol_id, false));
}

Ok(id)
}

fn declare_data(
&mut self,
id: DataId,
name: &str,
linkage: Linkage,
_writable: bool,
writable: bool,
tls: bool,
_align: Option<u8>,
) {
let kind = if tls {
align: Option<u8>,
) -> ModuleResult<DataId> {
let (id, decl) = self
.declarations
.declare_data(name, linkage, writable, tls, align)?;

let kind = if decl.tls {
SymbolKind::Tls
} else {
SymbolKind::Data
};
let (scope, weak) = translate_linkage(linkage);
let (scope, weak) = translate_linkage(decl.linkage);

if let Some((data, _defined)) = self.data_objects[id] {
let symbol = self.object.symbol_mut(data);
Expand All @@ -205,13 +225,14 @@ impl Backend for ObjectBackend {
});
self.data_objects[id] = Some((symbol_id, false));
}

Ok(id)
}

fn define_function<TS>(
&mut self,
func_id: FuncId,
ctx: &mut cranelift_codegen::Context,
declarations: &ModuleDeclarations,
trap_sink: &mut TS,
) -> ModuleResult<ModuleCompiledFunction>
where
Expand All @@ -227,7 +248,7 @@ impl Backend for ObjectBackend {
..
} = ctx.compile(self.isa())?;

let decl = declarations.get_function_decl(func_id);
let decl = self.declarations.get_function_decl(func_id);
if !decl.linkage.is_definable() {
return Err(ModuleError::InvalidImportDefinition(decl.name.clone()));
}
Expand Down Expand Up @@ -286,11 +307,10 @@ impl Backend for ObjectBackend {
&mut self,
func_id: FuncId,
bytes: &[u8],
declarations: &ModuleDeclarations,
) -> ModuleResult<ModuleCompiledFunction> {
info!("defining function {} with bytes", func_id);

let decl = declarations.get_function_decl(func_id);
let decl = self.declarations.get_function_decl(func_id);
if !decl.linkage.is_definable() {
return Err(ModuleError::InvalidImportDefinition(decl.name.clone()));
}
Expand Down Expand Up @@ -326,13 +346,8 @@ impl Backend for ObjectBackend {
Ok(ModuleCompiledFunction { size: total_size })
}

fn define_data(
&mut self,
data_id: DataId,
data_ctx: &DataContext,
declarations: &ModuleDeclarations,
) -> ModuleResult<()> {
let decl = declarations.get_data_decl(data_id);
fn define_data(&mut self, data_id: DataId, data_ctx: &DataContext) -> ModuleResult<()> {
let decl = self.declarations.get_data_decl(data_id);
if !decl.linkage.is_definable() {
return Err(ModuleError::InvalidImportDefinition(decl.name.clone()));
}
Expand Down Expand Up @@ -438,7 +453,7 @@ impl Backend for ObjectBackend {
Ok(())
}

fn finish(mut self, declarations: ModuleDeclarations) -> ObjectProduct {
fn finish(mut self) -> ObjectProduct {
let symbol_relocs = mem::take(&mut self.relocs);
for symbol in symbol_relocs {
for &RelocRecord {
Expand All @@ -450,7 +465,7 @@ impl Backend for ObjectBackend {
addend,
} in &symbol.relocs
{
let target_symbol = self.get_symbol(&declarations, name);
let target_symbol = self.get_symbol(name);
self.object
.add_relocation(
symbol.section,
Expand Down Expand Up @@ -487,18 +502,14 @@ impl Backend for ObjectBackend {
impl ObjectBackend {
// This should only be called during finish because it creates
// symbols for missing libcalls.
fn get_symbol(
&mut self,
declarations: &ModuleDeclarations,
name: &ir::ExternalName,
) -> SymbolId {
fn get_symbol(&mut self, name: &ir::ExternalName) -> SymbolId {
match *name {
ir::ExternalName::User { .. } => {
if declarations.is_function(name) {
let id = declarations.get_function_id(name);
if self.declarations.is_function(name) {
let id = self.declarations.get_function_id(name);
self.functions[id].unwrap().0
} else {
let id = declarations.get_data_id(name);
let id = self.declarations.get_data_id(name);
self.data_objects[id].unwrap().0
}
}
Expand Down

0 comments on commit b44c5bb

Please sign in to comment.