1 //! Defines `Module` and related types.
2 
3 // TODO: Should `ir::Function` really have a `name`?
4 
5 // TODO: Factor out `ir::Function`'s `ext_funcs` and `global_values` into a struct
6 // shared with `DataContext`?
7 
8 use super::HashMap;
9 use crate::data_context::DataContext;
10 use core::fmt::Display;
11 use cranelift_codegen::binemit::{CodeOffset, Reloc};
12 use cranelift_codegen::entity::{entity_impl, PrimaryMap};
13 use cranelift_codegen::ir::Function;
14 use cranelift_codegen::{binemit, MachReloc};
15 use cranelift_codegen::{ir, isa, CodegenError, CompileError, Context};
16 use std::borrow::ToOwned;
17 use std::string::String;
18 
19 /// A module relocation.
20 #[derive(Clone)]
21 pub struct ModuleReloc {
22     /// The offset at which the relocation applies, *relative to the
23     /// containing section*.
24     pub offset: CodeOffset,
25     /// The kind of relocation.
26     pub kind: Reloc,
27     /// The external symbol / name to which this relocation refers.
28     pub name: ModuleExtName,
29     /// The addend to add to the symbol value.
30     pub addend: i64,
31 }
32 
33 impl ModuleReloc {
34     /// Converts a `MachReloc` produced from a `Function` into a `ModuleReloc`.
35     pub fn from_mach_reloc(mach_reloc: &MachReloc, func: &Function) -> Self {
36         let name = match mach_reloc.name {
37             ir::ExternalName::User(reff) => {
38                 let name = &func.params.user_named_funcs()[reff];
39                 ModuleExtName::user(name.namespace, name.index)
40             }
41             ir::ExternalName::TestCase(_) => unimplemented!(),
42             ir::ExternalName::LibCall(libcall) => ModuleExtName::LibCall(libcall),
43             ir::ExternalName::KnownSymbol(ks) => ModuleExtName::KnownSymbol(ks),
44         };
45         Self {
46             offset: mach_reloc.offset,
47             kind: mach_reloc.kind,
48             name,
49             addend: mach_reloc.addend,
50         }
51     }
52 }
53 
54 /// A function identifier for use in the `Module` interface.
55 #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
56 pub struct FuncId(u32);
57 entity_impl!(FuncId, "funcid");
58 
59 /// Function identifiers are namespace 0 in `ir::ExternalName`
60 impl From<FuncId> for ModuleExtName {
61     fn from(id: FuncId) -> Self {
62         Self::User {
63             namespace: 0,
64             index: id.0,
65         }
66     }
67 }
68 
69 impl FuncId {
70     /// Get the `FuncId` for the function named by `name`.
71     pub fn from_name(name: &ModuleExtName) -> FuncId {
72         if let ModuleExtName::User { namespace, index } = name {
73             debug_assert_eq!(*namespace, 0);
74             FuncId::from_u32(*index)
75         } else {
76             panic!("unexpected name in DataId::from_name")
77         }
78     }
79 }
80 
81 /// A data object identifier for use in the `Module` interface.
82 #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
83 pub struct DataId(u32);
84 entity_impl!(DataId, "dataid");
85 
86 /// Data identifiers are namespace 1 in `ir::ExternalName`
87 impl From<DataId> for ModuleExtName {
88     fn from(id: DataId) -> Self {
89         Self::User {
90             namespace: 1,
91             index: id.0,
92         }
93     }
94 }
95 
96 impl DataId {
97     /// Get the `DataId` for the data object named by `name`.
98     pub fn from_name(name: &ModuleExtName) -> DataId {
99         if let ModuleExtName::User { namespace, index } = name {
100             debug_assert_eq!(*namespace, 1);
101             DataId::from_u32(*index)
102         } else {
103             panic!("unexpected name in DataId::from_name")
104         }
105     }
106 }
107 
108 /// Linkage refers to where an entity is defined and who can see it.
109 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
110 pub enum Linkage {
111     /// Defined outside of a module.
112     Import,
113     /// Defined inside the module, but not visible outside it.
114     Local,
115     /// Defined inside the module, visible outside it, and may be preempted.
116     Preemptible,
117     /// Defined inside the module, visible inside the current static linkage unit, but not outside.
118     ///
119     /// A static linkage unit is the combination of all object files passed to a linker to create
120     /// an executable or dynamic library.
121     Hidden,
122     /// Defined inside the module, and visible outside it.
123     Export,
124 }
125 
126 impl Linkage {
127     fn merge(a: Self, b: Self) -> Self {
128         match a {
129             Self::Export => Self::Export,
130             Self::Hidden => match b {
131                 Self::Export => Self::Export,
132                 Self::Preemptible => Self::Preemptible,
133                 _ => Self::Hidden,
134             },
135             Self::Preemptible => match b {
136                 Self::Export => Self::Export,
137                 _ => Self::Preemptible,
138             },
139             Self::Local => match b {
140                 Self::Export => Self::Export,
141                 Self::Hidden => Self::Hidden,
142                 Self::Preemptible => Self::Preemptible,
143                 Self::Local | Self::Import => Self::Local,
144             },
145             Self::Import => b,
146         }
147     }
148 
149     /// Test whether this linkage can have a definition.
150     pub fn is_definable(self) -> bool {
151         match self {
152             Self::Import => false,
153             Self::Local | Self::Preemptible | Self::Hidden | Self::Export => true,
154         }
155     }
156 
157     /// Test whether this linkage will have a definition that cannot be preempted.
158     pub fn is_final(self) -> bool {
159         match self {
160             Self::Import | Self::Preemptible => false,
161             Self::Local | Self::Hidden | Self::Export => true,
162         }
163     }
164 }
165 
166 /// A declared name may refer to either a function or data declaration
167 #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
168 pub enum FuncOrDataId {
169     /// When it's a FuncId
170     Func(FuncId),
171     /// When it's a DataId
172     Data(DataId),
173 }
174 
175 /// Mapping to `ModuleExtName` is trivial based on the `FuncId` and `DataId` mapping.
176 impl From<FuncOrDataId> for ModuleExtName {
177     fn from(id: FuncOrDataId) -> Self {
178         match id {
179             FuncOrDataId::Func(funcid) => Self::from(funcid),
180             FuncOrDataId::Data(dataid) => Self::from(dataid),
181         }
182     }
183 }
184 
185 /// Information about a function which can be called.
186 #[derive(Debug)]
187 pub struct FunctionDeclaration {
188     #[allow(missing_docs)]
189     pub name: String,
190     #[allow(missing_docs)]
191     pub linkage: Linkage,
192     #[allow(missing_docs)]
193     pub signature: ir::Signature,
194 }
195 
196 impl FunctionDeclaration {
197     fn merge(&mut self, linkage: Linkage, sig: &ir::Signature) -> Result<(), ModuleError> {
198         self.linkage = Linkage::merge(self.linkage, linkage);
199         if &self.signature != sig {
200             return Err(ModuleError::IncompatibleSignature(
201                 self.name.clone(),
202                 self.signature.clone(),
203                 sig.clone(),
204             ));
205         }
206         Ok(())
207     }
208 }
209 
210 /// Error messages for all `Module` methods
211 #[derive(Debug)]
212 pub enum ModuleError {
213     /// Indicates an identifier was used before it was declared
214     Undeclared(String),
215 
216     /// Indicates an identifier was used as data/function first, but then used as the other
217     IncompatibleDeclaration(String),
218 
219     /// Indicates a function identifier was declared with a
220     /// different signature than declared previously
221     IncompatibleSignature(String, ir::Signature, ir::Signature),
222 
223     /// Indicates an identifier was defined more than once
224     DuplicateDefinition(String),
225 
226     /// Indicates an identifier was defined, but was declared as an import
227     InvalidImportDefinition(String),
228 
229     /// Wraps a `cranelift-codegen` error
230     Compilation(CodegenError),
231 
232     /// Memory allocation failure from a backend
233     Allocation {
234         /// Tell where the allocation came from
235         message: &'static str,
236         /// Io error the allocation failed with
237         err: std::io::Error,
238     },
239 
240     /// Wraps a generic error from a backend
241     Backend(anyhow::Error),
242 }
243 
244 impl<'a> From<CompileError<'a>> for ModuleError {
245     fn from(err: CompileError<'a>) -> Self {
246         Self::Compilation(err.inner)
247     }
248 }
249 
250 // This is manually implementing Error and Display instead of using thiserror to reduce the amount
251 // of dependencies used by Cranelift.
252 impl std::error::Error for ModuleError {
253     fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
254         match self {
255             Self::Undeclared { .. }
256             | Self::IncompatibleDeclaration { .. }
257             | Self::IncompatibleSignature { .. }
258             | Self::DuplicateDefinition { .. }
259             | Self::InvalidImportDefinition { .. } => None,
260             Self::Compilation(source) => Some(source),
261             Self::Allocation { err: source, .. } => Some(source),
262             Self::Backend(source) => Some(&**source),
263         }
264     }
265 }
266 
267 impl std::fmt::Display for ModuleError {
268     fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
269         match self {
270             Self::Undeclared(name) => {
271                 write!(f, "Undeclared identifier: {}", name)
272             }
273             Self::IncompatibleDeclaration(name) => {
274                 write!(f, "Incompatible declaration of identifier: {}", name,)
275             }
276             Self::IncompatibleSignature(name, prev_sig, new_sig) => {
277                 write!(
278                     f,
279                     "Function {} signature {:?} is incompatible with previous declaration {:?}",
280                     name, new_sig, prev_sig,
281                 )
282             }
283             Self::DuplicateDefinition(name) => {
284                 write!(f, "Duplicate definition of identifier: {}", name)
285             }
286             Self::InvalidImportDefinition(name) => {
287                 write!(
288                     f,
289                     "Invalid to define identifier declared as an import: {}",
290                     name,
291                 )
292             }
293             Self::Compilation(err) => {
294                 write!(f, "Compilation error: {}", err)
295             }
296             Self::Allocation { message, err } => {
297                 write!(f, "Allocation error: {}: {}", message, err)
298             }
299             Self::Backend(err) => write!(f, "Backend error: {}", err),
300         }
301     }
302 }
303 
304 impl std::convert::From<CodegenError> for ModuleError {
305     fn from(source: CodegenError) -> Self {
306         Self::Compilation { 0: source }
307     }
308 }
309 
310 /// A convenient alias for a `Result` that uses `ModuleError` as the error type.
311 pub type ModuleResult<T> = Result<T, ModuleError>;
312 
313 /// Information about a data object which can be accessed.
314 #[derive(Debug)]
315 pub struct DataDeclaration {
316     #[allow(missing_docs)]
317     pub name: String,
318     #[allow(missing_docs)]
319     pub linkage: Linkage,
320     #[allow(missing_docs)]
321     pub writable: bool,
322     #[allow(missing_docs)]
323     pub tls: bool,
324 }
325 
326 impl DataDeclaration {
327     fn merge(&mut self, linkage: Linkage, writable: bool, tls: bool) {
328         self.linkage = Linkage::merge(self.linkage, linkage);
329         self.writable = self.writable || writable;
330         assert_eq!(
331             self.tls, tls,
332             "Can't change TLS data object to normal or in the opposite way",
333         );
334     }
335 }
336 
337 /// A translated `ExternalName` into something global we can handle.
338 #[derive(Clone)]
339 pub enum ModuleExtName {
340     /// User defined function, converted from `ExternalName::User`.
341     User {
342         /// Arbitrary.
343         namespace: u32,
344         /// Arbitrary.
345         index: u32,
346     },
347     /// Call into a library function.
348     LibCall(ir::LibCall),
349     /// Symbols known to the linker.
350     KnownSymbol(ir::KnownSymbol),
351 }
352 
353 impl ModuleExtName {
354     /// Creates a user-defined external name.
355     pub fn user(namespace: u32, index: u32) -> Self {
356         Self::User { namespace, index }
357     }
358 }
359 
360 impl Display for ModuleExtName {
361     fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
362         match self {
363             Self::User { namespace, index } => write!(f, "u{}:{}", namespace, index),
364             Self::LibCall(lc) => write!(f, "%{}", lc),
365             Self::KnownSymbol(ks) => write!(f, "{}", ks),
366         }
367     }
368 }
369 
370 /// This provides a view to the state of a module which allows `ir::ExternalName`s to be translated
371 /// into `FunctionDeclaration`s and `DataDeclaration`s.
372 #[derive(Debug, Default)]
373 pub struct ModuleDeclarations {
374     names: HashMap<String, FuncOrDataId>,
375     functions: PrimaryMap<FuncId, FunctionDeclaration>,
376     data_objects: PrimaryMap<DataId, DataDeclaration>,
377 }
378 
379 impl ModuleDeclarations {
380     /// Get the module identifier for a given name, if that name
381     /// has been declared.
382     pub fn get_name(&self, name: &str) -> Option<FuncOrDataId> {
383         self.names.get(name).copied()
384     }
385 
386     /// Get an iterator of all function declarations
387     pub fn get_functions(&self) -> impl Iterator<Item = (FuncId, &FunctionDeclaration)> {
388         self.functions.iter()
389     }
390 
391     /// Return whether `name` names a function, rather than a data object.
392     pub fn is_function(name: &ModuleExtName) -> bool {
393         match name {
394             ModuleExtName::User { namespace, .. } => *namespace == 0,
395             ModuleExtName::LibCall(_) | ModuleExtName::KnownSymbol(_) => {
396                 panic!("unexpected module ext name")
397             }
398         }
399     }
400 
401     /// Get the `FunctionDeclaration` for the function named by `name`.
402     pub fn get_function_decl(&self, func_id: FuncId) -> &FunctionDeclaration {
403         &self.functions[func_id]
404     }
405 
406     /// Get an iterator of all data declarations
407     pub fn get_data_objects(&self) -> impl Iterator<Item = (DataId, &DataDeclaration)> {
408         self.data_objects.iter()
409     }
410 
411     /// Get the `DataDeclaration` for the data object named by `name`.
412     pub fn get_data_decl(&self, data_id: DataId) -> &DataDeclaration {
413         &self.data_objects[data_id]
414     }
415 
416     /// Declare a function in this module.
417     pub fn declare_function(
418         &mut self,
419         name: &str,
420         linkage: Linkage,
421         signature: &ir::Signature,
422     ) -> ModuleResult<(FuncId, Linkage)> {
423         // TODO: Can we avoid allocating names so often?
424         use super::hash_map::Entry::*;
425         match self.names.entry(name.to_owned()) {
426             Occupied(entry) => match *entry.get() {
427                 FuncOrDataId::Func(id) => {
428                     let existing = &mut self.functions[id];
429                     existing.merge(linkage, signature)?;
430                     Ok((id, existing.linkage))
431                 }
432                 FuncOrDataId::Data(..) => {
433                     Err(ModuleError::IncompatibleDeclaration(name.to_owned()))
434                 }
435             },
436             Vacant(entry) => {
437                 let id = self.functions.push(FunctionDeclaration {
438                     name: name.to_owned(),
439                     linkage,
440                     signature: signature.clone(),
441                 });
442                 entry.insert(FuncOrDataId::Func(id));
443                 Ok((id, self.functions[id].linkage))
444             }
445         }
446     }
447 
448     /// Declare an anonymous function in this module.
449     pub fn declare_anonymous_function(
450         &mut self,
451         signature: &ir::Signature,
452     ) -> ModuleResult<FuncId> {
453         let id = self.functions.push(FunctionDeclaration {
454             name: String::new(),
455             linkage: Linkage::Local,
456             signature: signature.clone(),
457         });
458         self.functions[id].name = format!(".L{:?}", id);
459         Ok(id)
460     }
461 
462     /// Declare a data object in this module.
463     pub fn declare_data(
464         &mut self,
465         name: &str,
466         linkage: Linkage,
467         writable: bool,
468         tls: bool,
469     ) -> ModuleResult<(DataId, Linkage)> {
470         // TODO: Can we avoid allocating names so often?
471         use super::hash_map::Entry::*;
472         match self.names.entry(name.to_owned()) {
473             Occupied(entry) => match *entry.get() {
474                 FuncOrDataId::Data(id) => {
475                     let existing = &mut self.data_objects[id];
476                     existing.merge(linkage, writable, tls);
477                     Ok((id, existing.linkage))
478                 }
479 
480                 FuncOrDataId::Func(..) => {
481                     Err(ModuleError::IncompatibleDeclaration(name.to_owned()))
482                 }
483             },
484             Vacant(entry) => {
485                 let id = self.data_objects.push(DataDeclaration {
486                     name: name.to_owned(),
487                     linkage,
488                     writable,
489                     tls,
490                 });
491                 entry.insert(FuncOrDataId::Data(id));
492                 Ok((id, self.data_objects[id].linkage))
493             }
494         }
495     }
496 
497     /// Declare an anonymous data object in this module.
498     pub fn declare_anonymous_data(&mut self, writable: bool, tls: bool) -> ModuleResult<DataId> {
499         let id = self.data_objects.push(DataDeclaration {
500             name: String::new(),
501             linkage: Linkage::Local,
502             writable,
503             tls,
504         });
505         self.data_objects[id].name = format!(".L{:?}", id);
506         Ok(id)
507     }
508 }
509 
510 /// Information about the compiled function.
511 pub struct ModuleCompiledFunction {
512     /// The size of the compiled function.
513     pub size: binemit::CodeOffset,
514 }
515 
516 /// A `Module` is a utility for collecting functions and data objects, and linking them together.
517 pub trait Module {
518     /// Return the `TargetIsa` to compile for.
519     fn isa(&self) -> &dyn isa::TargetIsa;
520 
521     /// Get all declarations in this module.
522     fn declarations(&self) -> &ModuleDeclarations;
523 
524     /// Get the module identifier for a given name, if that name
525     /// has been declared.
526     fn get_name(&self, name: &str) -> Option<FuncOrDataId> {
527         self.declarations().get_name(name)
528     }
529 
530     /// Return the target information needed by frontends to produce Cranelift IR
531     /// for the current target.
532     fn target_config(&self) -> isa::TargetFrontendConfig {
533         self.isa().frontend_config()
534     }
535 
536     /// Create a new `Context` initialized for use with this `Module`.
537     ///
538     /// This ensures that the `Context` is initialized with the default calling
539     /// convention for the `TargetIsa`.
540     fn make_context(&self) -> Context {
541         let mut ctx = Context::new();
542         ctx.func.signature.call_conv = self.isa().default_call_conv();
543         ctx
544     }
545 
546     /// Clear the given `Context` and reset it for use with a new function.
547     ///
548     /// This ensures that the `Context` is initialized with the default calling
549     /// convention for the `TargetIsa`.
550     fn clear_context(&self, ctx: &mut Context) {
551         ctx.clear();
552         ctx.func.signature.call_conv = self.isa().default_call_conv();
553     }
554 
555     /// Create a new empty `Signature` with the default calling convention for
556     /// the `TargetIsa`, to which parameter and return types can be added for
557     /// declaring a function to be called by this `Module`.
558     fn make_signature(&self) -> ir::Signature {
559         ir::Signature::new(self.isa().default_call_conv())
560     }
561 
562     /// Clear the given `Signature` and reset for use with a new function.
563     ///
564     /// This ensures that the `Signature` is initialized with the default
565     /// calling convention for the `TargetIsa`.
566     fn clear_signature(&self, sig: &mut ir::Signature) {
567         sig.clear(self.isa().default_call_conv());
568     }
569 
570     /// Declare a function in this module.
571     fn declare_function(
572         &mut self,
573         name: &str,
574         linkage: Linkage,
575         signature: &ir::Signature,
576     ) -> ModuleResult<FuncId>;
577 
578     /// Declare an anonymous function in this module.
579     fn declare_anonymous_function(&mut self, signature: &ir::Signature) -> ModuleResult<FuncId>;
580 
581     /// Declare a data object in this module.
582     fn declare_data(
583         &mut self,
584         name: &str,
585         linkage: Linkage,
586         writable: bool,
587         tls: bool,
588     ) -> ModuleResult<DataId>;
589 
590     /// Declare an anonymous data object in this module.
591     fn declare_anonymous_data(&mut self, writable: bool, tls: bool) -> ModuleResult<DataId>;
592 
593     /// Use this when you're building the IR of a function to reference a function.
594     ///
595     /// TODO: Coalesce redundant decls and signatures.
596     /// TODO: Look into ways to reduce the risk of using a FuncRef in the wrong function.
597     fn declare_func_in_func(&mut self, func_id: FuncId, func: &mut ir::Function) -> ir::FuncRef {
598         let decl = &self.declarations().functions[func_id];
599         let signature = func.import_signature(decl.signature.clone());
600         let user_name_ref = func.declare_imported_user_function(ir::UserExternalName {
601             namespace: 0,
602             index: func_id.as_u32(),
603         });
604         let colocated = decl.linkage.is_final();
605         func.import_function(ir::ExtFuncData {
606             name: ir::ExternalName::user(user_name_ref),
607             signature,
608             colocated,
609         })
610     }
611 
612     /// Use this when you're building the IR of a function to reference a data object.
613     ///
614     /// TODO: Same as above.
615     fn declare_data_in_func(&self, data: DataId, func: &mut ir::Function) -> ir::GlobalValue {
616         let decl = &self.declarations().data_objects[data];
617         let colocated = decl.linkage.is_final();
618         let user_name_ref = func.declare_imported_user_function(ir::UserExternalName {
619             namespace: 1,
620             index: data.as_u32(),
621         });
622         func.create_global_value(ir::GlobalValueData::Symbol {
623             name: ir::ExternalName::user(user_name_ref),
624             offset: ir::immediates::Imm64::new(0),
625             colocated,
626             tls: decl.tls,
627         })
628     }
629 
630     /// TODO: Same as above.
631     fn declare_func_in_data(&self, func: FuncId, ctx: &mut DataContext) -> ir::FuncRef {
632         ctx.import_function(ModuleExtName::user(0, func.as_u32()))
633     }
634 
635     /// TODO: Same as above.
636     fn declare_data_in_data(&self, data: DataId, ctx: &mut DataContext) -> ir::GlobalValue {
637         ctx.import_global_value(ModuleExtName::user(1, data.as_u32()))
638     }
639 
640     /// Define a function, producing the function body from the given `Context`.
641     ///
642     /// Returns the size of the function's code and constant data.
643     ///
644     /// Note: After calling this function the given `Context` will contain the compiled function.
645     fn define_function(
646         &mut self,
647         func: FuncId,
648         ctx: &mut Context,
649     ) -> ModuleResult<ModuleCompiledFunction>;
650 
651     /// Define a function, taking the function body from the given `bytes`.
652     ///
653     /// This function is generally only useful if you need to precisely specify
654     /// the emitted instructions for some reason; otherwise, you should use
655     /// `define_function`.
656     ///
657     /// Returns the size of the function's code.
658     fn define_function_bytes(
659         &mut self,
660         func_id: FuncId,
661         func: &ir::Function,
662         alignment: u64,
663         bytes: &[u8],
664         relocs: &[MachReloc],
665     ) -> ModuleResult<ModuleCompiledFunction>;
666 
667     /// Define a data object, producing the data contents from the given `DataContext`.
668     fn define_data(&mut self, data: DataId, data_ctx: &DataContext) -> ModuleResult<()>;
669 }
670 
671 impl<M: Module> Module for &mut M {
672     fn isa(&self) -> &dyn isa::TargetIsa {
673         (**self).isa()
674     }
675 
676     fn declarations(&self) -> &ModuleDeclarations {
677         (**self).declarations()
678     }
679 
680     fn get_name(&self, name: &str) -> Option<FuncOrDataId> {
681         (**self).get_name(name)
682     }
683 
684     fn target_config(&self) -> isa::TargetFrontendConfig {
685         (**self).target_config()
686     }
687 
688     fn make_context(&self) -> Context {
689         (**self).make_context()
690     }
691 
692     fn clear_context(&self, ctx: &mut Context) {
693         (**self).clear_context(ctx)
694     }
695 
696     fn make_signature(&self) -> ir::Signature {
697         (**self).make_signature()
698     }
699 
700     fn clear_signature(&self, sig: &mut ir::Signature) {
701         (**self).clear_signature(sig)
702     }
703 
704     fn declare_function(
705         &mut self,
706         name: &str,
707         linkage: Linkage,
708         signature: &ir::Signature,
709     ) -> ModuleResult<FuncId> {
710         (**self).declare_function(name, linkage, signature)
711     }
712 
713     fn declare_anonymous_function(&mut self, signature: &ir::Signature) -> ModuleResult<FuncId> {
714         (**self).declare_anonymous_function(signature)
715     }
716 
717     fn declare_data(
718         &mut self,
719         name: &str,
720         linkage: Linkage,
721         writable: bool,
722         tls: bool,
723     ) -> ModuleResult<DataId> {
724         (**self).declare_data(name, linkage, writable, tls)
725     }
726 
727     fn declare_anonymous_data(&mut self, writable: bool, tls: bool) -> ModuleResult<DataId> {
728         (**self).declare_anonymous_data(writable, tls)
729     }
730 
731     fn declare_func_in_func(&mut self, func: FuncId, in_func: &mut ir::Function) -> ir::FuncRef {
732         (**self).declare_func_in_func(func, in_func)
733     }
734 
735     fn declare_data_in_func(&self, data: DataId, func: &mut ir::Function) -> ir::GlobalValue {
736         (**self).declare_data_in_func(data, func)
737     }
738 
739     fn declare_func_in_data(&self, func: FuncId, ctx: &mut DataContext) -> ir::FuncRef {
740         (**self).declare_func_in_data(func, ctx)
741     }
742 
743     fn declare_data_in_data(&self, data: DataId, ctx: &mut DataContext) -> ir::GlobalValue {
744         (**self).declare_data_in_data(data, ctx)
745     }
746 
747     fn define_function(
748         &mut self,
749         func: FuncId,
750         ctx: &mut Context,
751     ) -> ModuleResult<ModuleCompiledFunction> {
752         (**self).define_function(func, ctx)
753     }
754 
755     fn define_function_bytes(
756         &mut self,
757         func_id: FuncId,
758         func: &ir::Function,
759         alignment: u64,
760         bytes: &[u8],
761         relocs: &[MachReloc],
762     ) -> ModuleResult<ModuleCompiledFunction> {
763         (**self).define_function_bytes(func_id, func, alignment, bytes, relocs)
764     }
765 
766     fn define_data(&mut self, data: DataId, data_ctx: &DataContext) -> ModuleResult<()> {
767         (**self).define_data(data, data_ctx)
768     }
769 }
770