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 `DataDescription`?
7 
8 use super::HashMap;
9 use crate::data_context::DataDescription;
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::{Function, VersionMarker};
14 use cranelift_codegen::settings::SetError;
15 use cranelift_codegen::MachReloc;
16 use cranelift_codegen::{ir, isa, CodegenError, CompileError, Context};
17 use cranelift_control::ControlPlane;
18 use std::borrow::{Cow, ToOwned};
19 use std::string::String;
20 
21 /// A module relocation.
22 #[derive(Clone)]
23 pub struct ModuleReloc {
24     /// The offset at which the relocation applies, *relative to the
25     /// containing section*.
26     pub offset: CodeOffset,
27     /// The kind of relocation.
28     pub kind: Reloc,
29     /// The external symbol / name to which this relocation refers.
30     pub name: ModuleExtName,
31     /// The addend to add to the symbol value.
32     pub addend: i64,
33 }
34 
35 impl ModuleReloc {
36     /// Converts a `MachReloc` produced from a `Function` into a `ModuleReloc`.
37     pub fn from_mach_reloc(mach_reloc: &MachReloc, func: &Function) -> Self {
38         let name = match mach_reloc.name {
39             ir::ExternalName::User(reff) => {
40                 let name = &func.params.user_named_funcs()[reff];
41                 ModuleExtName::user(name.namespace, name.index)
42             }
43             ir::ExternalName::TestCase(_) => unimplemented!(),
44             ir::ExternalName::LibCall(libcall) => ModuleExtName::LibCall(libcall),
45             ir::ExternalName::KnownSymbol(ks) => ModuleExtName::KnownSymbol(ks),
46         };
47         Self {
48             offset: mach_reloc.offset,
49             kind: mach_reloc.kind,
50             name,
51             addend: mach_reloc.addend,
52         }
53     }
54 }
55 
56 /// A function identifier for use in the `Module` interface.
57 #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
58 #[cfg_attr(feature = "enable-serde", derive(serde::Serialize, serde::Deserialize))]
59 pub struct FuncId(u32);
60 entity_impl!(FuncId, "funcid");
61 
62 /// Function identifiers are namespace 0 in `ir::ExternalName`
63 impl From<FuncId> for ModuleExtName {
64     fn from(id: FuncId) -> Self {
65         Self::User {
66             namespace: 0,
67             index: id.0,
68         }
69     }
70 }
71 
72 impl FuncId {
73     /// Get the `FuncId` for the function named by `name`.
74     pub fn from_name(name: &ModuleExtName) -> FuncId {
75         if let ModuleExtName::User { namespace, index } = name {
76             debug_assert_eq!(*namespace, 0);
77             FuncId::from_u32(*index)
78         } else {
79             panic!("unexpected name in DataId::from_name")
80         }
81     }
82 }
83 
84 /// A data object identifier for use in the `Module` interface.
85 #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
86 #[cfg_attr(feature = "enable-serde", derive(serde::Serialize, serde::Deserialize))]
87 pub struct DataId(u32);
88 entity_impl!(DataId, "dataid");
89 
90 /// Data identifiers are namespace 1 in `ir::ExternalName`
91 impl From<DataId> for ModuleExtName {
92     fn from(id: DataId) -> Self {
93         Self::User {
94             namespace: 1,
95             index: id.0,
96         }
97     }
98 }
99 
100 impl DataId {
101     /// Get the `DataId` for the data object named by `name`.
102     pub fn from_name(name: &ModuleExtName) -> DataId {
103         if let ModuleExtName::User { namespace, index } = name {
104             debug_assert_eq!(*namespace, 1);
105             DataId::from_u32(*index)
106         } else {
107             panic!("unexpected name in DataId::from_name")
108         }
109     }
110 }
111 
112 /// Linkage refers to where an entity is defined and who can see it.
113 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
114 #[cfg_attr(feature = "enable-serde", derive(serde::Serialize, serde::Deserialize))]
115 pub enum Linkage {
116     /// Defined outside of a module.
117     Import,
118     /// Defined inside the module, but not visible outside it.
119     Local,
120     /// Defined inside the module, visible outside it, and may be preempted.
121     Preemptible,
122     /// Defined inside the module, visible inside the current static linkage unit, but not outside.
123     ///
124     /// A static linkage unit is the combination of all object files passed to a linker to create
125     /// an executable or dynamic library.
126     Hidden,
127     /// Defined inside the module, and visible outside it.
128     Export,
129 }
130 
131 impl Linkage {
132     fn merge(a: Self, b: Self) -> Self {
133         match a {
134             Self::Export => Self::Export,
135             Self::Hidden => match b {
136                 Self::Export => Self::Export,
137                 Self::Preemptible => Self::Preemptible,
138                 _ => Self::Hidden,
139             },
140             Self::Preemptible => match b {
141                 Self::Export => Self::Export,
142                 _ => Self::Preemptible,
143             },
144             Self::Local => match b {
145                 Self::Export => Self::Export,
146                 Self::Hidden => Self::Hidden,
147                 Self::Preemptible => Self::Preemptible,
148                 Self::Local | Self::Import => Self::Local,
149             },
150             Self::Import => b,
151         }
152     }
153 
154     /// Test whether this linkage can have a definition.
155     pub fn is_definable(self) -> bool {
156         match self {
157             Self::Import => false,
158             Self::Local | Self::Preemptible | Self::Hidden | Self::Export => true,
159         }
160     }
161 
162     /// Test whether this linkage will have a definition that cannot be preempted.
163     pub fn is_final(self) -> bool {
164         match self {
165             Self::Import | Self::Preemptible => false,
166             Self::Local | Self::Hidden | Self::Export => true,
167         }
168     }
169 }
170 
171 /// A declared name may refer to either a function or data declaration
172 #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
173 #[cfg_attr(feature = "enable-serde", derive(serde::Serialize, serde::Deserialize))]
174 pub enum FuncOrDataId {
175     /// When it's a FuncId
176     Func(FuncId),
177     /// When it's a DataId
178     Data(DataId),
179 }
180 
181 /// Mapping to `ModuleExtName` is trivial based on the `FuncId` and `DataId` mapping.
182 impl From<FuncOrDataId> for ModuleExtName {
183     fn from(id: FuncOrDataId) -> Self {
184         match id {
185             FuncOrDataId::Func(funcid) => Self::from(funcid),
186             FuncOrDataId::Data(dataid) => Self::from(dataid),
187         }
188     }
189 }
190 
191 /// Information about a function which can be called.
192 #[derive(Debug)]
193 #[cfg_attr(feature = "enable-serde", derive(serde::Serialize, serde::Deserialize))]
194 pub struct FunctionDeclaration {
195     #[allow(missing_docs)]
196     pub name: Option<String>,
197     #[allow(missing_docs)]
198     pub linkage: Linkage,
199     #[allow(missing_docs)]
200     pub signature: ir::Signature,
201 }
202 
203 impl FunctionDeclaration {
204     /// The linkage name of the function.
205     ///
206     /// Synthesized from the given function id if it is an anonymous function.
207     pub fn linkage_name(&self, id: FuncId) -> Cow<'_, str> {
208         match &self.name {
209             Some(name) => Cow::Borrowed(name),
210             // Symbols starting with .L are completely omitted from the symbol table after linking.
211             // Using hexadecimal instead of decimal for slightly smaller symbol names and often
212             // slightly faster linking.
213             None => Cow::Owned(format!(".Lfn{:x}", id.as_u32())),
214         }
215     }
216 
217     fn merge(
218         &mut self,
219         id: FuncId,
220         linkage: Linkage,
221         sig: &ir::Signature,
222     ) -> Result<(), ModuleError> {
223         self.linkage = Linkage::merge(self.linkage, linkage);
224         if &self.signature != sig {
225             return Err(ModuleError::IncompatibleSignature(
226                 self.linkage_name(id).into_owned(),
227                 self.signature.clone(),
228                 sig.clone(),
229             ));
230         }
231         Ok(())
232     }
233 }
234 
235 /// Error messages for all `Module` methods
236 #[derive(Debug)]
237 pub enum ModuleError {
238     /// Indicates an identifier was used before it was declared
239     Undeclared(String),
240 
241     /// Indicates an identifier was used as data/function first, but then used as the other
242     IncompatibleDeclaration(String),
243 
244     /// Indicates a function identifier was declared with a
245     /// different signature than declared previously
246     IncompatibleSignature(String, ir::Signature, ir::Signature),
247 
248     /// Indicates an identifier was defined more than once
249     DuplicateDefinition(String),
250 
251     /// Indicates an identifier was defined, but was declared as an import
252     InvalidImportDefinition(String),
253 
254     /// Wraps a `cranelift-codegen` error
255     Compilation(CodegenError),
256 
257     /// Memory allocation failure from a backend
258     Allocation {
259         /// Tell where the allocation came from
260         message: &'static str,
261         /// Io error the allocation failed with
262         err: std::io::Error,
263     },
264 
265     /// Wraps a generic error from a backend
266     Backend(anyhow::Error),
267 
268     /// Wraps an error from a flag definition.
269     Flag(SetError),
270 }
271 
272 impl<'a> From<CompileError<'a>> for ModuleError {
273     fn from(err: CompileError<'a>) -> Self {
274         Self::Compilation(err.inner)
275     }
276 }
277 
278 // This is manually implementing Error and Display instead of using thiserror to reduce the amount
279 // of dependencies used by Cranelift.
280 impl std::error::Error for ModuleError {
281     fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
282         match self {
283             Self::Undeclared { .. }
284             | Self::IncompatibleDeclaration { .. }
285             | Self::IncompatibleSignature { .. }
286             | Self::DuplicateDefinition { .. }
287             | Self::InvalidImportDefinition { .. } => None,
288             Self::Compilation(source) => Some(source),
289             Self::Allocation { err: source, .. } => Some(source),
290             Self::Backend(source) => Some(&**source),
291             Self::Flag(source) => Some(source),
292         }
293     }
294 }
295 
296 impl std::fmt::Display for ModuleError {
297     fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
298         match self {
299             Self::Undeclared(name) => {
300                 write!(f, "Undeclared identifier: {}", name)
301             }
302             Self::IncompatibleDeclaration(name) => {
303                 write!(f, "Incompatible declaration of identifier: {}", name,)
304             }
305             Self::IncompatibleSignature(name, prev_sig, new_sig) => {
306                 write!(
307                     f,
308                     "Function {} signature {:?} is incompatible with previous declaration {:?}",
309                     name, new_sig, prev_sig,
310                 )
311             }
312             Self::DuplicateDefinition(name) => {
313                 write!(f, "Duplicate definition of identifier: {}", name)
314             }
315             Self::InvalidImportDefinition(name) => {
316                 write!(
317                     f,
318                     "Invalid to define identifier declared as an import: {}",
319                     name,
320                 )
321             }
322             Self::Compilation(err) => {
323                 write!(f, "Compilation error: {}", err)
324             }
325             Self::Allocation { message, err } => {
326                 write!(f, "Allocation error: {}: {}", message, err)
327             }
328             Self::Backend(err) => write!(f, "Backend error: {}", err),
329             Self::Flag(err) => write!(f, "Flag error: {}", err),
330         }
331     }
332 }
333 
334 impl std::convert::From<CodegenError> for ModuleError {
335     fn from(source: CodegenError) -> Self {
336         Self::Compilation { 0: source }
337     }
338 }
339 
340 impl std::convert::From<SetError> for ModuleError {
341     fn from(source: SetError) -> Self {
342         Self::Flag { 0: source }
343     }
344 }
345 
346 /// A convenient alias for a `Result` that uses `ModuleError` as the error type.
347 pub type ModuleResult<T> = Result<T, ModuleError>;
348 
349 /// Information about a data object which can be accessed.
350 #[derive(Debug)]
351 #[cfg_attr(feature = "enable-serde", derive(serde::Serialize, serde::Deserialize))]
352 pub struct DataDeclaration {
353     #[allow(missing_docs)]
354     pub name: Option<String>,
355     #[allow(missing_docs)]
356     pub linkage: Linkage,
357     #[allow(missing_docs)]
358     pub writable: bool,
359     #[allow(missing_docs)]
360     pub tls: bool,
361 }
362 
363 impl DataDeclaration {
364     /// The linkage name of the data object.
365     ///
366     /// Synthesized from the given data id if it is an anonymous function.
367     pub fn linkage_name(&self, id: DataId) -> Cow<'_, str> {
368         match &self.name {
369             Some(name) => Cow::Borrowed(name),
370             // Symbols starting with .L are completely omitted from the symbol table after linking.
371             // Using hexadecimal instead of decimal for slightly smaller symbol names and often
372             // slightly faster linking.
373             None => Cow::Owned(format!(".Ldata{:x}", id.as_u32())),
374         }
375     }
376 
377     fn merge(&mut self, linkage: Linkage, writable: bool, tls: bool) {
378         self.linkage = Linkage::merge(self.linkage, linkage);
379         self.writable = self.writable || writable;
380         assert_eq!(
381             self.tls, tls,
382             "Can't change TLS data object to normal or in the opposite way",
383         );
384     }
385 }
386 
387 /// A translated `ExternalName` into something global we can handle.
388 #[derive(Clone, Debug)]
389 #[cfg_attr(feature = "enable-serde", derive(serde::Serialize, serde::Deserialize))]
390 pub enum ModuleExtName {
391     /// User defined function, converted from `ExternalName::User`.
392     User {
393         /// Arbitrary.
394         namespace: u32,
395         /// Arbitrary.
396         index: u32,
397     },
398     /// Call into a library function.
399     LibCall(ir::LibCall),
400     /// Symbols known to the linker.
401     KnownSymbol(ir::KnownSymbol),
402 }
403 
404 impl ModuleExtName {
405     /// Creates a user-defined external name.
406     pub fn user(namespace: u32, index: u32) -> Self {
407         Self::User { namespace, index }
408     }
409 }
410 
411 impl Display for ModuleExtName {
412     fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
413         match self {
414             Self::User { namespace, index } => write!(f, "u{}:{}", namespace, index),
415             Self::LibCall(lc) => write!(f, "%{}", lc),
416             Self::KnownSymbol(ks) => write!(f, "{}", ks),
417         }
418     }
419 }
420 
421 /// This provides a view to the state of a module which allows `ir::ExternalName`s to be translated
422 /// into `FunctionDeclaration`s and `DataDeclaration`s.
423 #[derive(Debug, Default)]
424 pub struct ModuleDeclarations {
425     /// A version marker used to ensure that serialized clif ir is never deserialized with a
426     /// different version of Cranelift.
427     // Note: This must be the first field to ensure that Serde will deserialize it before
428     // attempting to deserialize other fields that are potentially changed between versions.
429     _version_marker: VersionMarker,
430 
431     names: HashMap<String, FuncOrDataId>,
432     functions: PrimaryMap<FuncId, FunctionDeclaration>,
433     data_objects: PrimaryMap<DataId, DataDeclaration>,
434 }
435 
436 #[cfg(feature = "enable-serde")]
437 mod serialize {
438     // This is manually implementing Serialize and Deserialize to avoid serializing the names field,
439     // which can be entirely reconstructed from the functions and data_objects fields, saving space.
440 
441     use super::*;
442 
443     use serde::de::{Deserialize, Deserializer, Error, MapAccess, SeqAccess, Unexpected, Visitor};
444     use serde::ser::{Serialize, SerializeStruct, Serializer};
445     use std::fmt;
446 
447     fn get_names<E: Error>(
448         functions: &PrimaryMap<FuncId, FunctionDeclaration>,
449         data_objects: &PrimaryMap<DataId, DataDeclaration>,
450     ) -> Result<HashMap<String, FuncOrDataId>, E> {
451         let mut names = HashMap::new();
452         for (func_id, decl) in functions.iter() {
453             if let Some(name) = &decl.name {
454                 let old = names.insert(name.clone(), FuncOrDataId::Func(func_id));
455                 if old.is_some() {
456                     return Err(E::invalid_value(
457                         Unexpected::Other("duplicate name"),
458                         &"FunctionDeclaration's with no duplicate names",
459                     ));
460                 }
461             }
462         }
463         for (data_id, decl) in data_objects.iter() {
464             if let Some(name) = &decl.name {
465                 let old = names.insert(name.clone(), FuncOrDataId::Data(data_id));
466                 if old.is_some() {
467                     return Err(E::invalid_value(
468                         Unexpected::Other("duplicate name"),
469                         &"DataDeclaration's with no duplicate names",
470                     ));
471                 }
472             }
473         }
474         Ok(names)
475     }
476 
477     impl Serialize for ModuleDeclarations {
478         fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
479             let ModuleDeclarations {
480                 _version_marker,
481                 functions,
482                 data_objects,
483                 names: _,
484             } = self;
485 
486             let mut state = s.serialize_struct("ModuleDeclarations", 4)?;
487             state.serialize_field("_version_marker", _version_marker)?;
488             state.serialize_field("functions", functions)?;
489             state.serialize_field("data_objects", data_objects)?;
490             state.end()
491         }
492     }
493 
494     enum ModuleDeclarationsField {
495         VersionMarker,
496         Functions,
497         DataObjects,
498         Ignore,
499     }
500 
501     struct ModuleDeclarationsFieldVisitor;
502 
503     impl<'de> serde::de::Visitor<'de> for ModuleDeclarationsFieldVisitor {
504         type Value = ModuleDeclarationsField;
505 
506         fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
507             f.write_str("field identifier")
508         }
509 
510         fn visit_u64<E: Error>(self, val: u64) -> Result<Self::Value, E> {
511             match val {
512                 0u64 => Ok(ModuleDeclarationsField::VersionMarker),
513                 1u64 => Ok(ModuleDeclarationsField::Functions),
514                 2u64 => Ok(ModuleDeclarationsField::DataObjects),
515                 _ => Ok(ModuleDeclarationsField::Ignore),
516             }
517         }
518 
519         fn visit_str<E: Error>(self, val: &str) -> Result<Self::Value, E> {
520             match val {
521                 "_version_marker" => Ok(ModuleDeclarationsField::VersionMarker),
522                 "functions" => Ok(ModuleDeclarationsField::Functions),
523                 "data_objects" => Ok(ModuleDeclarationsField::DataObjects),
524                 _ => Ok(ModuleDeclarationsField::Ignore),
525             }
526         }
527 
528         fn visit_bytes<E: Error>(self, val: &[u8]) -> Result<Self::Value, E> {
529             match val {
530                 b"_version_marker" => Ok(ModuleDeclarationsField::VersionMarker),
531                 b"functions" => Ok(ModuleDeclarationsField::Functions),
532                 b"data_objects" => Ok(ModuleDeclarationsField::DataObjects),
533                 _ => Ok(ModuleDeclarationsField::Ignore),
534             }
535         }
536     }
537 
538     impl<'de> Deserialize<'de> for ModuleDeclarationsField {
539         #[inline]
540         fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
541             d.deserialize_identifier(ModuleDeclarationsFieldVisitor)
542         }
543     }
544 
545     struct ModuleDeclarationsVisitor;
546 
547     impl<'de> Visitor<'de> for ModuleDeclarationsVisitor {
548         type Value = ModuleDeclarations;
549 
550         fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
551             f.write_str("struct ModuleDeclarations")
552         }
553 
554         #[inline]
555         fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
556             let _version_marker = match seq.next_element()? {
557                 Some(val) => val,
558                 None => {
559                     return Err(Error::invalid_length(
560                         0usize,
561                         &"struct ModuleDeclarations with 4 elements",
562                     ));
563                 }
564             };
565             let functions = match seq.next_element()? {
566                 Some(val) => val,
567                 None => {
568                     return Err(Error::invalid_length(
569                         2usize,
570                         &"struct ModuleDeclarations with 4 elements",
571                     ));
572                 }
573             };
574             let data_objects = match seq.next_element()? {
575                 Some(val) => val,
576                 None => {
577                     return Err(Error::invalid_length(
578                         3usize,
579                         &"struct ModuleDeclarations with 4 elements",
580                     ));
581                 }
582             };
583             let names = get_names(&functions, &data_objects)?;
584             Ok(ModuleDeclarations {
585                 _version_marker,
586                 names,
587                 functions,
588                 data_objects,
589             })
590         }
591 
592         #[inline]
593         fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<Self::Value, A::Error> {
594             let mut _version_marker: Option<VersionMarker> = None;
595             let mut functions: Option<PrimaryMap<FuncId, FunctionDeclaration>> = None;
596             let mut data_objects: Option<PrimaryMap<DataId, DataDeclaration>> = None;
597             while let Some(key) = map.next_key::<ModuleDeclarationsField>()? {
598                 match key {
599                     ModuleDeclarationsField::VersionMarker => {
600                         if _version_marker.is_some() {
601                             return Err(Error::duplicate_field("_version_marker"));
602                         }
603                         _version_marker = Some(map.next_value()?);
604                     }
605                     ModuleDeclarationsField::Functions => {
606                         if functions.is_some() {
607                             return Err(Error::duplicate_field("functions"));
608                         }
609                         functions = Some(map.next_value()?);
610                     }
611                     ModuleDeclarationsField::DataObjects => {
612                         if data_objects.is_some() {
613                             return Err(Error::duplicate_field("data_objects"));
614                         }
615                         data_objects = Some(map.next_value()?);
616                     }
617                     _ => {
618                         map.next_value::<serde::de::IgnoredAny>()?;
619                     }
620                 }
621             }
622             let _version_marker = match _version_marker {
623                 Some(_version_marker) => _version_marker,
624                 None => return Err(Error::missing_field("_version_marker")),
625             };
626             let functions = match functions {
627                 Some(functions) => functions,
628                 None => return Err(Error::missing_field("functions")),
629             };
630             let data_objects = match data_objects {
631                 Some(data_objects) => data_objects,
632                 None => return Err(Error::missing_field("data_objects")),
633             };
634             let names = get_names(&functions, &data_objects)?;
635             Ok(ModuleDeclarations {
636                 _version_marker,
637                 names,
638                 functions,
639                 data_objects,
640             })
641         }
642     }
643 
644     impl<'de> Deserialize<'de> for ModuleDeclarations {
645         fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
646             d.deserialize_struct(
647                 "ModuleDeclarations",
648                 &["_version_marker", "functions", "data_objects"],
649                 ModuleDeclarationsVisitor,
650             )
651         }
652     }
653 }
654 
655 impl ModuleDeclarations {
656     /// Get the module identifier for a given name, if that name
657     /// has been declared.
658     pub fn get_name(&self, name: &str) -> Option<FuncOrDataId> {
659         self.names.get(name).copied()
660     }
661 
662     /// Get an iterator of all function declarations
663     pub fn get_functions(&self) -> impl Iterator<Item = (FuncId, &FunctionDeclaration)> {
664         self.functions.iter()
665     }
666 
667     /// Return whether `name` names a function, rather than a data object.
668     pub fn is_function(name: &ModuleExtName) -> bool {
669         match name {
670             ModuleExtName::User { namespace, .. } => *namespace == 0,
671             ModuleExtName::LibCall(_) | ModuleExtName::KnownSymbol(_) => {
672                 panic!("unexpected module ext name")
673             }
674         }
675     }
676 
677     /// Get the `FunctionDeclaration` for the function named by `name`.
678     pub fn get_function_decl(&self, func_id: FuncId) -> &FunctionDeclaration {
679         &self.functions[func_id]
680     }
681 
682     /// Get an iterator of all data declarations
683     pub fn get_data_objects(&self) -> impl Iterator<Item = (DataId, &DataDeclaration)> {
684         self.data_objects.iter()
685     }
686 
687     /// Get the `DataDeclaration` for the data object named by `name`.
688     pub fn get_data_decl(&self, data_id: DataId) -> &DataDeclaration {
689         &self.data_objects[data_id]
690     }
691 
692     /// Declare a function in this module.
693     pub fn declare_function(
694         &mut self,
695         name: &str,
696         linkage: Linkage,
697         signature: &ir::Signature,
698     ) -> ModuleResult<(FuncId, Linkage)> {
699         // TODO: Can we avoid allocating names so often?
700         use super::hash_map::Entry::*;
701         match self.names.entry(name.to_owned()) {
702             Occupied(entry) => match *entry.get() {
703                 FuncOrDataId::Func(id) => {
704                     let existing = &mut self.functions[id];
705                     existing.merge(id, linkage, signature)?;
706                     Ok((id, existing.linkage))
707                 }
708                 FuncOrDataId::Data(..) => {
709                     Err(ModuleError::IncompatibleDeclaration(name.to_owned()))
710                 }
711             },
712             Vacant(entry) => {
713                 let id = self.functions.push(FunctionDeclaration {
714                     name: Some(name.to_owned()),
715                     linkage,
716                     signature: signature.clone(),
717                 });
718                 entry.insert(FuncOrDataId::Func(id));
719                 Ok((id, self.functions[id].linkage))
720             }
721         }
722     }
723 
724     /// Declare an anonymous function in this module.
725     pub fn declare_anonymous_function(
726         &mut self,
727         signature: &ir::Signature,
728     ) -> ModuleResult<FuncId> {
729         let id = self.functions.push(FunctionDeclaration {
730             name: None,
731             linkage: Linkage::Local,
732             signature: signature.clone(),
733         });
734         Ok(id)
735     }
736 
737     /// Declare a data object in this module.
738     pub fn declare_data(
739         &mut self,
740         name: &str,
741         linkage: Linkage,
742         writable: bool,
743         tls: bool,
744     ) -> ModuleResult<(DataId, Linkage)> {
745         // TODO: Can we avoid allocating names so often?
746         use super::hash_map::Entry::*;
747         match self.names.entry(name.to_owned()) {
748             Occupied(entry) => match *entry.get() {
749                 FuncOrDataId::Data(id) => {
750                     let existing = &mut self.data_objects[id];
751                     existing.merge(linkage, writable, tls);
752                     Ok((id, existing.linkage))
753                 }
754 
755                 FuncOrDataId::Func(..) => {
756                     Err(ModuleError::IncompatibleDeclaration(name.to_owned()))
757                 }
758             },
759             Vacant(entry) => {
760                 let id = self.data_objects.push(DataDeclaration {
761                     name: Some(name.to_owned()),
762                     linkage,
763                     writable,
764                     tls,
765                 });
766                 entry.insert(FuncOrDataId::Data(id));
767                 Ok((id, self.data_objects[id].linkage))
768             }
769         }
770     }
771 
772     /// Declare an anonymous data object in this module.
773     pub fn declare_anonymous_data(&mut self, writable: bool, tls: bool) -> ModuleResult<DataId> {
774         let id = self.data_objects.push(DataDeclaration {
775             name: None,
776             linkage: Linkage::Local,
777             writable,
778             tls,
779         });
780         Ok(id)
781     }
782 }
783 
784 /// A `Module` is a utility for collecting functions and data objects, and linking them together.
785 pub trait Module {
786     /// Return the `TargetIsa` to compile for.
787     fn isa(&self) -> &dyn isa::TargetIsa;
788 
789     /// Get all declarations in this module.
790     fn declarations(&self) -> &ModuleDeclarations;
791 
792     /// Get the module identifier for a given name, if that name
793     /// has been declared.
794     fn get_name(&self, name: &str) -> Option<FuncOrDataId> {
795         self.declarations().get_name(name)
796     }
797 
798     /// Return the target information needed by frontends to produce Cranelift IR
799     /// for the current target.
800     fn target_config(&self) -> isa::TargetFrontendConfig {
801         self.isa().frontend_config()
802     }
803 
804     /// Create a new `Context` initialized for use with this `Module`.
805     ///
806     /// This ensures that the `Context` is initialized with the default calling
807     /// convention for the `TargetIsa`.
808     fn make_context(&self) -> Context {
809         let mut ctx = Context::new();
810         ctx.func.signature.call_conv = self.isa().default_call_conv();
811         ctx
812     }
813 
814     /// Clear the given `Context` and reset it for use with a new function.
815     ///
816     /// This ensures that the `Context` is initialized with the default calling
817     /// convention for the `TargetIsa`.
818     fn clear_context(&self, ctx: &mut Context) {
819         ctx.clear();
820         ctx.func.signature.call_conv = self.isa().default_call_conv();
821     }
822 
823     /// Create a new empty `Signature` with the default calling convention for
824     /// the `TargetIsa`, to which parameter and return types can be added for
825     /// declaring a function to be called by this `Module`.
826     fn make_signature(&self) -> ir::Signature {
827         ir::Signature::new(self.isa().default_call_conv())
828     }
829 
830     /// Clear the given `Signature` and reset for use with a new function.
831     ///
832     /// This ensures that the `Signature` is initialized with the default
833     /// calling convention for the `TargetIsa`.
834     fn clear_signature(&self, sig: &mut ir::Signature) {
835         sig.clear(self.isa().default_call_conv());
836     }
837 
838     /// Declare a function in this module.
839     fn declare_function(
840         &mut self,
841         name: &str,
842         linkage: Linkage,
843         signature: &ir::Signature,
844     ) -> ModuleResult<FuncId>;
845 
846     /// Declare an anonymous function in this module.
847     fn declare_anonymous_function(&mut self, signature: &ir::Signature) -> ModuleResult<FuncId>;
848 
849     /// Declare a data object in this module.
850     fn declare_data(
851         &mut self,
852         name: &str,
853         linkage: Linkage,
854         writable: bool,
855         tls: bool,
856     ) -> ModuleResult<DataId>;
857 
858     /// Declare an anonymous data object in this module.
859     fn declare_anonymous_data(&mut self, writable: bool, tls: bool) -> ModuleResult<DataId>;
860 
861     /// Use this when you're building the IR of a function to reference a function.
862     ///
863     /// TODO: Coalesce redundant decls and signatures.
864     /// TODO: Look into ways to reduce the risk of using a FuncRef in the wrong function.
865     fn declare_func_in_func(&mut self, func_id: FuncId, func: &mut ir::Function) -> ir::FuncRef {
866         let decl = &self.declarations().functions[func_id];
867         let signature = func.import_signature(decl.signature.clone());
868         let user_name_ref = func.declare_imported_user_function(ir::UserExternalName {
869             namespace: 0,
870             index: func_id.as_u32(),
871         });
872         let colocated = decl.linkage.is_final();
873         func.import_function(ir::ExtFuncData {
874             name: ir::ExternalName::user(user_name_ref),
875             signature,
876             colocated,
877         })
878     }
879 
880     /// Use this when you're building the IR of a function to reference a data object.
881     ///
882     /// TODO: Same as above.
883     fn declare_data_in_func(&self, data: DataId, func: &mut ir::Function) -> ir::GlobalValue {
884         let decl = &self.declarations().data_objects[data];
885         let colocated = decl.linkage.is_final();
886         let user_name_ref = func.declare_imported_user_function(ir::UserExternalName {
887             namespace: 1,
888             index: data.as_u32(),
889         });
890         func.create_global_value(ir::GlobalValueData::Symbol {
891             name: ir::ExternalName::user(user_name_ref),
892             offset: ir::immediates::Imm64::new(0),
893             colocated,
894             tls: decl.tls,
895         })
896     }
897 
898     /// TODO: Same as above.
899     fn declare_func_in_data(&self, func_id: FuncId, data: &mut DataDescription) -> ir::FuncRef {
900         data.import_function(ModuleExtName::user(0, func_id.as_u32()))
901     }
902 
903     /// TODO: Same as above.
904     fn declare_data_in_data(&self, data_id: DataId, data: &mut DataDescription) -> ir::GlobalValue {
905         data.import_global_value(ModuleExtName::user(1, data_id.as_u32()))
906     }
907 
908     /// Define a function, producing the function body from the given `Context`.
909     ///
910     /// Returns the size of the function's code and constant data.
911     ///
912     /// Unlike [`define_function_with_control_plane`] this uses a default [`ControlPlane`] for
913     /// convenience.
914     ///
915     /// Note: After calling this function the given `Context` will contain the compiled function.
916     ///
917     /// [`define_function_with_control_plane`]: Self::define_function_with_control_plane
918     fn define_function(&mut self, func: FuncId, ctx: &mut Context) -> ModuleResult<()> {
919         self.define_function_with_control_plane(func, ctx, &mut ControlPlane::default())
920     }
921 
922     /// Define a function, producing the function body from the given `Context`.
923     ///
924     /// Returns the size of the function's code and constant data.
925     ///
926     /// Note: After calling this function the given `Context` will contain the compiled function.
927     fn define_function_with_control_plane(
928         &mut self,
929         func: FuncId,
930         ctx: &mut Context,
931         ctrl_plane: &mut ControlPlane,
932     ) -> ModuleResult<()>;
933 
934     /// Define a function, taking the function body from the given `bytes`.
935     ///
936     /// This function is generally only useful if you need to precisely specify
937     /// the emitted instructions for some reason; otherwise, you should use
938     /// `define_function`.
939     ///
940     /// Returns the size of the function's code.
941     fn define_function_bytes(
942         &mut self,
943         func_id: FuncId,
944         func: &ir::Function,
945         alignment: u64,
946         bytes: &[u8],
947         relocs: &[MachReloc],
948     ) -> ModuleResult<()>;
949 
950     /// Define a data object, producing the data contents from the given `DataContext`.
951     fn define_data(&mut self, data_id: DataId, data: &DataDescription) -> ModuleResult<()>;
952 }
953 
954 impl<M: Module> Module for &mut M {
955     fn isa(&self) -> &dyn isa::TargetIsa {
956         (**self).isa()
957     }
958 
959     fn declarations(&self) -> &ModuleDeclarations {
960         (**self).declarations()
961     }
962 
963     fn get_name(&self, name: &str) -> Option<FuncOrDataId> {
964         (**self).get_name(name)
965     }
966 
967     fn target_config(&self) -> isa::TargetFrontendConfig {
968         (**self).target_config()
969     }
970 
971     fn make_context(&self) -> Context {
972         (**self).make_context()
973     }
974 
975     fn clear_context(&self, ctx: &mut Context) {
976         (**self).clear_context(ctx)
977     }
978 
979     fn make_signature(&self) -> ir::Signature {
980         (**self).make_signature()
981     }
982 
983     fn clear_signature(&self, sig: &mut ir::Signature) {
984         (**self).clear_signature(sig)
985     }
986 
987     fn declare_function(
988         &mut self,
989         name: &str,
990         linkage: Linkage,
991         signature: &ir::Signature,
992     ) -> ModuleResult<FuncId> {
993         (**self).declare_function(name, linkage, signature)
994     }
995 
996     fn declare_anonymous_function(&mut self, signature: &ir::Signature) -> ModuleResult<FuncId> {
997         (**self).declare_anonymous_function(signature)
998     }
999 
1000     fn declare_data(
1001         &mut self,
1002         name: &str,
1003         linkage: Linkage,
1004         writable: bool,
1005         tls: bool,
1006     ) -> ModuleResult<DataId> {
1007         (**self).declare_data(name, linkage, writable, tls)
1008     }
1009 
1010     fn declare_anonymous_data(&mut self, writable: bool, tls: bool) -> ModuleResult<DataId> {
1011         (**self).declare_anonymous_data(writable, tls)
1012     }
1013 
1014     fn declare_func_in_func(&mut self, func: FuncId, in_func: &mut ir::Function) -> ir::FuncRef {
1015         (**self).declare_func_in_func(func, in_func)
1016     }
1017 
1018     fn declare_data_in_func(&self, data: DataId, func: &mut ir::Function) -> ir::GlobalValue {
1019         (**self).declare_data_in_func(data, func)
1020     }
1021 
1022     fn declare_func_in_data(&self, func_id: FuncId, data: &mut DataDescription) -> ir::FuncRef {
1023         (**self).declare_func_in_data(func_id, data)
1024     }
1025 
1026     fn declare_data_in_data(&self, data_id: DataId, data: &mut DataDescription) -> ir::GlobalValue {
1027         (**self).declare_data_in_data(data_id, data)
1028     }
1029 
1030     fn define_function(&mut self, func: FuncId, ctx: &mut Context) -> ModuleResult<()> {
1031         (**self).define_function(func, ctx)
1032     }
1033 
1034     fn define_function_with_control_plane(
1035         &mut self,
1036         func: FuncId,
1037         ctx: &mut Context,
1038         ctrl_plane: &mut ControlPlane,
1039     ) -> ModuleResult<()> {
1040         (**self).define_function_with_control_plane(func, ctx, ctrl_plane)
1041     }
1042 
1043     fn define_function_bytes(
1044         &mut self,
1045         func_id: FuncId,
1046         func: &ir::Function,
1047         alignment: u64,
1048         bytes: &[u8],
1049         relocs: &[MachReloc],
1050     ) -> ModuleResult<()> {
1051         (**self).define_function_bytes(func_id, func, alignment, bytes, relocs)
1052     }
1053 
1054     fn define_data(&mut self, data_id: DataId, data: &DataDescription) -> ModuleResult<()> {
1055         (**self).define_data(data_id, data)
1056     }
1057 }
1058