1use super::HashMap;
9use crate::data_context::DataDescription;
10use core::fmt::Display;
11use cranelift_codegen::binemit::{CodeOffset, Reloc};
12use cranelift_codegen::entity::{PrimaryMap, entity_impl};
13use cranelift_codegen::ir::ExternalName;
14use cranelift_codegen::ir::function::{Function, VersionMarker};
15use cranelift_codegen::settings::SetError;
16use cranelift_codegen::{
17 CodegenError, CompileError, Context, FinalizedMachReloc, FinalizedRelocTarget, ir, isa,
18};
19use cranelift_control::ControlPlane;
20use std::borrow::{Cow, ToOwned};
21use std::boxed::Box;
22use std::string::String;
23
24#[derive(Clone)]
26pub struct ModuleReloc {
27 pub offset: CodeOffset,
30 pub kind: Reloc,
32 pub name: ModuleRelocTarget,
34 pub addend: i64,
36}
37
38impl ModuleReloc {
39 pub fn from_mach_reloc(
41 mach_reloc: &FinalizedMachReloc,
42 func: &Function,
43 func_id: FuncId,
44 ) -> Self {
45 let name = match mach_reloc.target {
46 FinalizedRelocTarget::ExternalName(ExternalName::User(reff)) => {
47 let name = &func.params.user_named_funcs()[reff];
48 ModuleRelocTarget::user(name.namespace, name.index)
49 }
50 FinalizedRelocTarget::ExternalName(ExternalName::TestCase(_)) => unimplemented!(),
51 FinalizedRelocTarget::ExternalName(ExternalName::LibCall(libcall)) => {
52 ModuleRelocTarget::LibCall(libcall)
53 }
54 FinalizedRelocTarget::ExternalName(ExternalName::KnownSymbol(ks)) => {
55 ModuleRelocTarget::KnownSymbol(ks)
56 }
57 FinalizedRelocTarget::Func(offset) => {
58 ModuleRelocTarget::FunctionOffset(func_id, offset)
59 }
60 };
61 Self {
62 offset: mach_reloc.offset,
63 kind: mach_reloc.kind,
64 name,
65 addend: mach_reloc.addend,
66 }
67 }
68}
69
70#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
72#[cfg_attr(
73 feature = "enable-serde",
74 derive(serde_derive::Serialize, serde_derive::Deserialize)
75)]
76pub struct FuncId(u32);
77entity_impl!(FuncId, "funcid");
78
79impl From<FuncId> for ModuleRelocTarget {
81 fn from(id: FuncId) -> Self {
82 Self::User {
83 namespace: 0,
84 index: id.0,
85 }
86 }
87}
88
89impl FuncId {
90 pub fn from_name(name: &ModuleRelocTarget) -> FuncId {
92 if let ModuleRelocTarget::User { namespace, index } = name {
93 debug_assert_eq!(*namespace, 0);
94 FuncId::from_u32(*index)
95 } else {
96 panic!("unexpected name in DataId::from_name")
97 }
98 }
99}
100
101#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
103#[cfg_attr(
104 feature = "enable-serde",
105 derive(serde_derive::Serialize, serde_derive::Deserialize)
106)]
107pub struct DataId(u32);
108entity_impl!(DataId, "dataid");
109
110impl From<DataId> for ModuleRelocTarget {
112 fn from(id: DataId) -> Self {
113 Self::User {
114 namespace: 1,
115 index: id.0,
116 }
117 }
118}
119
120impl DataId {
121 pub fn from_name(name: &ModuleRelocTarget) -> DataId {
123 if let ModuleRelocTarget::User { namespace, index } = name {
124 debug_assert_eq!(*namespace, 1);
125 DataId::from_u32(*index)
126 } else {
127 panic!("unexpected name in DataId::from_name")
128 }
129 }
130}
131
132#[derive(Copy, Clone, Debug, PartialEq, Eq)]
134#[cfg_attr(
135 feature = "enable-serde",
136 derive(serde_derive::Serialize, serde_derive::Deserialize)
137)]
138pub enum Linkage {
139 Import,
141 Local,
143 Preemptible,
145 Hidden,
150 Export,
152}
153
154impl Linkage {
155 fn merge(a: Self, b: Self) -> Self {
156 match a {
157 Self::Export => Self::Export,
158 Self::Hidden => match b {
159 Self::Export => Self::Export,
160 Self::Preemptible => Self::Preemptible,
161 _ => Self::Hidden,
162 },
163 Self::Preemptible => match b {
164 Self::Export => Self::Export,
165 _ => Self::Preemptible,
166 },
167 Self::Local => match b {
168 Self::Export => Self::Export,
169 Self::Hidden => Self::Hidden,
170 Self::Preemptible => Self::Preemptible,
171 Self::Local | Self::Import => Self::Local,
172 },
173 Self::Import => b,
174 }
175 }
176
177 pub fn is_definable(self) -> bool {
179 match self {
180 Self::Import => false,
181 Self::Local | Self::Preemptible | Self::Hidden | Self::Export => true,
182 }
183 }
184
185 pub fn requires_definition(self) -> bool {
187 match self {
188 Self::Import | Self::Preemptible => false,
189 Self::Local | Self::Hidden | Self::Export => true,
190 }
191 }
192
193 pub fn is_final(self) -> bool {
195 match self {
196 Self::Import | Self::Preemptible => false,
197 Self::Local | Self::Hidden | Self::Export => true,
198 }
199 }
200}
201
202#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
204#[cfg_attr(
205 feature = "enable-serde",
206 derive(serde_derive::Serialize, serde_derive::Deserialize)
207)]
208pub enum FuncOrDataId {
209 Func(FuncId),
211 Data(DataId),
213}
214
215impl From<FuncOrDataId> for ModuleRelocTarget {
217 fn from(id: FuncOrDataId) -> Self {
218 match id {
219 FuncOrDataId::Func(funcid) => Self::from(funcid),
220 FuncOrDataId::Data(dataid) => Self::from(dataid),
221 }
222 }
223}
224
225#[derive(Debug)]
227#[cfg_attr(
228 feature = "enable-serde",
229 derive(serde_derive::Serialize, serde_derive::Deserialize)
230)]
231#[expect(missing_docs, reason = "self-describing fields")]
232pub struct FunctionDeclaration {
233 pub name: Option<String>,
234 pub linkage: Linkage,
235 pub signature: ir::Signature,
236}
237
238impl FunctionDeclaration {
239 pub fn linkage_name(&self, id: FuncId) -> Cow<'_, str> {
243 match &self.name {
244 Some(name) => Cow::Borrowed(name),
245 None => Cow::Owned(format!(".Lfn{:x}", id.as_u32())),
249 }
250 }
251
252 fn merge(
253 &mut self,
254 id: FuncId,
255 linkage: Linkage,
256 sig: &ir::Signature,
257 ) -> Result<(), ModuleError> {
258 self.linkage = Linkage::merge(self.linkage, linkage);
259 if &self.signature != sig {
260 return Err(ModuleError::IncompatibleSignature(
261 self.linkage_name(id).into_owned(),
262 self.signature.clone(),
263 sig.clone(),
264 ));
265 }
266 Ok(())
267 }
268}
269
270#[derive(Debug)]
272pub enum ModuleError {
273 Undeclared(String),
275
276 IncompatibleDeclaration(String),
278
279 IncompatibleSignature(String, ir::Signature, ir::Signature),
282
283 DuplicateDefinition(String),
285
286 InvalidImportDefinition(String),
288
289 Compilation(CodegenError),
291
292 Allocation {
294 message: &'static str,
296 err: std::io::Error,
298 },
299
300 Backend(anyhow::Error),
302
303 Flag(SetError),
305}
306
307impl<'a> From<CompileError<'a>> for ModuleError {
308 fn from(err: CompileError<'a>) -> Self {
309 Self::Compilation(err.inner)
310 }
311}
312
313impl std::error::Error for ModuleError {
316 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
317 match self {
318 Self::Undeclared { .. }
319 | Self::IncompatibleDeclaration { .. }
320 | Self::IncompatibleSignature { .. }
321 | Self::DuplicateDefinition { .. }
322 | Self::InvalidImportDefinition { .. } => None,
323 Self::Compilation(source) => Some(source),
324 Self::Allocation { err: source, .. } => Some(source),
325 Self::Backend(source) => Some(&**source),
326 Self::Flag(source) => Some(source),
327 }
328 }
329}
330
331impl std::fmt::Display for ModuleError {
332 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
333 match self {
334 Self::Undeclared(name) => {
335 write!(f, "Undeclared identifier: {name}")
336 }
337 Self::IncompatibleDeclaration(name) => {
338 write!(f, "Incompatible declaration of identifier: {name}",)
339 }
340 Self::IncompatibleSignature(name, prev_sig, new_sig) => {
341 write!(
342 f,
343 "Function {name} signature {new_sig:?} is incompatible with previous declaration {prev_sig:?}",
344 )
345 }
346 Self::DuplicateDefinition(name) => {
347 write!(f, "Duplicate definition of identifier: {name}")
348 }
349 Self::InvalidImportDefinition(name) => {
350 write!(
351 f,
352 "Invalid to define identifier declared as an import: {name}",
353 )
354 }
355 Self::Compilation(err) => {
356 write!(f, "Compilation error: {err}")
357 }
358 Self::Allocation { message, err } => {
359 write!(f, "Allocation error: {message}: {err}")
360 }
361 Self::Backend(err) => write!(f, "Backend error: {err}"),
362 Self::Flag(err) => write!(f, "Flag error: {err}"),
363 }
364 }
365}
366
367impl std::convert::From<CodegenError> for ModuleError {
368 fn from(source: CodegenError) -> Self {
369 Self::Compilation { 0: source }
370 }
371}
372
373impl std::convert::From<SetError> for ModuleError {
374 fn from(source: SetError) -> Self {
375 Self::Flag { 0: source }
376 }
377}
378
379pub type ModuleResult<T> = Result<T, ModuleError>;
381
382#[derive(Debug)]
384#[cfg_attr(
385 feature = "enable-serde",
386 derive(serde_derive::Serialize, serde_derive::Deserialize)
387)]
388#[expect(missing_docs, reason = "self-describing fields")]
389pub struct DataDeclaration {
390 pub name: Option<String>,
391 pub linkage: Linkage,
392 pub writable: bool,
393 pub tls: bool,
394}
395
396impl DataDeclaration {
397 pub fn linkage_name(&self, id: DataId) -> Cow<'_, str> {
401 match &self.name {
402 Some(name) => Cow::Borrowed(name),
403 None => Cow::Owned(format!(".Ldata{:x}", id.as_u32())),
407 }
408 }
409
410 fn merge(&mut self, linkage: Linkage, writable: bool, tls: bool) {
411 self.linkage = Linkage::merge(self.linkage, linkage);
412 self.writable = self.writable || writable;
413 assert_eq!(
414 self.tls, tls,
415 "Can't change TLS data object to normal or in the opposite way",
416 );
417 }
418}
419
420#[derive(Clone, Debug)]
422#[cfg_attr(
423 feature = "enable-serde",
424 derive(serde_derive::Serialize, serde_derive::Deserialize)
425)]
426pub enum ModuleRelocTarget {
427 User {
429 namespace: u32,
431 index: u32,
433 },
434 LibCall(ir::LibCall),
436 KnownSymbol(ir::KnownSymbol),
438 FunctionOffset(FuncId, CodeOffset),
440}
441
442impl ModuleRelocTarget {
443 pub fn user(namespace: u32, index: u32) -> Self {
445 Self::User { namespace, index }
446 }
447}
448
449impl Display for ModuleRelocTarget {
450 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
451 match self {
452 Self::User { namespace, index } => write!(f, "u{namespace}:{index}"),
453 Self::LibCall(lc) => write!(f, "%{lc}"),
454 Self::KnownSymbol(ks) => write!(f, "{ks}"),
455 Self::FunctionOffset(fname, offset) => write!(f, "{fname}+{offset}"),
456 }
457 }
458}
459
460#[derive(Debug, Default)]
463pub struct ModuleDeclarations {
464 _version_marker: VersionMarker,
469
470 names: HashMap<String, FuncOrDataId>,
471 functions: PrimaryMap<FuncId, FunctionDeclaration>,
472 data_objects: PrimaryMap<DataId, DataDeclaration>,
473}
474
475#[cfg(feature = "enable-serde")]
476mod serialize {
477 use super::*;
481
482 use serde::de::{Deserialize, Deserializer, Error, MapAccess, SeqAccess, Unexpected, Visitor};
483 use serde::ser::{Serialize, SerializeStruct, Serializer};
484 use std::fmt;
485
486 fn get_names<E: Error>(
487 functions: &PrimaryMap<FuncId, FunctionDeclaration>,
488 data_objects: &PrimaryMap<DataId, DataDeclaration>,
489 ) -> Result<HashMap<String, FuncOrDataId>, E> {
490 let mut names = HashMap::new();
491 for (func_id, decl) in functions.iter() {
492 if let Some(name) = &decl.name {
493 let old = names.insert(name.clone(), FuncOrDataId::Func(func_id));
494 if old.is_some() {
495 return Err(E::invalid_value(
496 Unexpected::Other("duplicate name"),
497 &"FunctionDeclaration's with no duplicate names",
498 ));
499 }
500 }
501 }
502 for (data_id, decl) in data_objects.iter() {
503 if let Some(name) = &decl.name {
504 let old = names.insert(name.clone(), FuncOrDataId::Data(data_id));
505 if old.is_some() {
506 return Err(E::invalid_value(
507 Unexpected::Other("duplicate name"),
508 &"DataDeclaration's with no duplicate names",
509 ));
510 }
511 }
512 }
513 Ok(names)
514 }
515
516 impl Serialize for ModuleDeclarations {
517 fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
518 let ModuleDeclarations {
519 _version_marker,
520 functions,
521 data_objects,
522 names: _,
523 } = self;
524
525 let mut state = s.serialize_struct("ModuleDeclarations", 4)?;
526 state.serialize_field("_version_marker", _version_marker)?;
527 state.serialize_field("functions", functions)?;
528 state.serialize_field("data_objects", data_objects)?;
529 state.end()
530 }
531 }
532
533 enum ModuleDeclarationsField {
534 VersionMarker,
535 Functions,
536 DataObjects,
537 Ignore,
538 }
539
540 struct ModuleDeclarationsFieldVisitor;
541
542 impl<'de> serde::de::Visitor<'de> for ModuleDeclarationsFieldVisitor {
543 type Value = ModuleDeclarationsField;
544
545 fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
546 f.write_str("field identifier")
547 }
548
549 fn visit_u64<E: Error>(self, val: u64) -> Result<Self::Value, E> {
550 match val {
551 0u64 => Ok(ModuleDeclarationsField::VersionMarker),
552 1u64 => Ok(ModuleDeclarationsField::Functions),
553 2u64 => Ok(ModuleDeclarationsField::DataObjects),
554 _ => Ok(ModuleDeclarationsField::Ignore),
555 }
556 }
557
558 fn visit_str<E: Error>(self, val: &str) -> Result<Self::Value, E> {
559 match val {
560 "_version_marker" => Ok(ModuleDeclarationsField::VersionMarker),
561 "functions" => Ok(ModuleDeclarationsField::Functions),
562 "data_objects" => Ok(ModuleDeclarationsField::DataObjects),
563 _ => Ok(ModuleDeclarationsField::Ignore),
564 }
565 }
566
567 fn visit_bytes<E: Error>(self, val: &[u8]) -> Result<Self::Value, E> {
568 match val {
569 b"_version_marker" => Ok(ModuleDeclarationsField::VersionMarker),
570 b"functions" => Ok(ModuleDeclarationsField::Functions),
571 b"data_objects" => Ok(ModuleDeclarationsField::DataObjects),
572 _ => Ok(ModuleDeclarationsField::Ignore),
573 }
574 }
575 }
576
577 impl<'de> Deserialize<'de> for ModuleDeclarationsField {
578 #[inline]
579 fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
580 d.deserialize_identifier(ModuleDeclarationsFieldVisitor)
581 }
582 }
583
584 struct ModuleDeclarationsVisitor;
585
586 impl<'de> Visitor<'de> for ModuleDeclarationsVisitor {
587 type Value = ModuleDeclarations;
588
589 fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
590 f.write_str("struct ModuleDeclarations")
591 }
592
593 #[inline]
594 fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
595 let _version_marker = match seq.next_element()? {
596 Some(val) => val,
597 None => {
598 return Err(Error::invalid_length(
599 0usize,
600 &"struct ModuleDeclarations with 4 elements",
601 ));
602 }
603 };
604 let functions = match seq.next_element()? {
605 Some(val) => val,
606 None => {
607 return Err(Error::invalid_length(
608 2usize,
609 &"struct ModuleDeclarations with 4 elements",
610 ));
611 }
612 };
613 let data_objects = match seq.next_element()? {
614 Some(val) => val,
615 None => {
616 return Err(Error::invalid_length(
617 3usize,
618 &"struct ModuleDeclarations with 4 elements",
619 ));
620 }
621 };
622 let names = get_names(&functions, &data_objects)?;
623 Ok(ModuleDeclarations {
624 _version_marker,
625 names,
626 functions,
627 data_objects,
628 })
629 }
630
631 #[inline]
632 fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<Self::Value, A::Error> {
633 let mut _version_marker: Option<VersionMarker> = None;
634 let mut functions: Option<PrimaryMap<FuncId, FunctionDeclaration>> = None;
635 let mut data_objects: Option<PrimaryMap<DataId, DataDeclaration>> = None;
636 while let Some(key) = map.next_key::<ModuleDeclarationsField>()? {
637 match key {
638 ModuleDeclarationsField::VersionMarker => {
639 if _version_marker.is_some() {
640 return Err(Error::duplicate_field("_version_marker"));
641 }
642 _version_marker = Some(map.next_value()?);
643 }
644 ModuleDeclarationsField::Functions => {
645 if functions.is_some() {
646 return Err(Error::duplicate_field("functions"));
647 }
648 functions = Some(map.next_value()?);
649 }
650 ModuleDeclarationsField::DataObjects => {
651 if data_objects.is_some() {
652 return Err(Error::duplicate_field("data_objects"));
653 }
654 data_objects = Some(map.next_value()?);
655 }
656 _ => {
657 map.next_value::<serde::de::IgnoredAny>()?;
658 }
659 }
660 }
661 let _version_marker = match _version_marker {
662 Some(_version_marker) => _version_marker,
663 None => return Err(Error::missing_field("_version_marker")),
664 };
665 let functions = match functions {
666 Some(functions) => functions,
667 None => return Err(Error::missing_field("functions")),
668 };
669 let data_objects = match data_objects {
670 Some(data_objects) => data_objects,
671 None => return Err(Error::missing_field("data_objects")),
672 };
673 let names = get_names(&functions, &data_objects)?;
674 Ok(ModuleDeclarations {
675 _version_marker,
676 names,
677 functions,
678 data_objects,
679 })
680 }
681 }
682
683 impl<'de> Deserialize<'de> for ModuleDeclarations {
684 fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
685 d.deserialize_struct(
686 "ModuleDeclarations",
687 &["_version_marker", "functions", "data_objects"],
688 ModuleDeclarationsVisitor,
689 )
690 }
691 }
692}
693
694impl ModuleDeclarations {
695 pub fn get_name(&self, name: &str) -> Option<FuncOrDataId> {
698 self.names.get(name).copied()
699 }
700
701 pub fn get_functions(&self) -> impl Iterator<Item = (FuncId, &FunctionDeclaration)> {
703 self.functions.iter()
704 }
705
706 pub fn is_function(name: &ModuleRelocTarget) -> bool {
708 match name {
709 ModuleRelocTarget::User { namespace, .. } => *namespace == 0,
710 ModuleRelocTarget::LibCall(_)
711 | ModuleRelocTarget::KnownSymbol(_)
712 | ModuleRelocTarget::FunctionOffset(..) => {
713 panic!("unexpected module ext name")
714 }
715 }
716 }
717
718 pub fn get_function_decl(&self, func_id: FuncId) -> &FunctionDeclaration {
720 &self.functions[func_id]
721 }
722
723 pub fn get_data_objects(&self) -> impl Iterator<Item = (DataId, &DataDeclaration)> {
725 self.data_objects.iter()
726 }
727
728 pub fn get_data_decl(&self, data_id: DataId) -> &DataDeclaration {
730 &self.data_objects[data_id]
731 }
732
733 pub fn declare_function(
735 &mut self,
736 name: &str,
737 linkage: Linkage,
738 signature: &ir::Signature,
739 ) -> ModuleResult<(FuncId, Linkage)> {
740 use super::hash_map::Entry::*;
742 match self.names.entry(name.to_owned()) {
743 Occupied(entry) => match *entry.get() {
744 FuncOrDataId::Func(id) => {
745 let existing = &mut self.functions[id];
746 existing.merge(id, linkage, signature)?;
747 Ok((id, existing.linkage))
748 }
749 FuncOrDataId::Data(..) => {
750 Err(ModuleError::IncompatibleDeclaration(name.to_owned()))
751 }
752 },
753 Vacant(entry) => {
754 let id = self.functions.push(FunctionDeclaration {
755 name: Some(name.to_owned()),
756 linkage,
757 signature: signature.clone(),
758 });
759 entry.insert(FuncOrDataId::Func(id));
760 Ok((id, self.functions[id].linkage))
761 }
762 }
763 }
764
765 pub fn declare_anonymous_function(
767 &mut self,
768 signature: &ir::Signature,
769 ) -> ModuleResult<FuncId> {
770 let id = self.functions.push(FunctionDeclaration {
771 name: None,
772 linkage: Linkage::Local,
773 signature: signature.clone(),
774 });
775 Ok(id)
776 }
777
778 pub fn declare_data(
780 &mut self,
781 name: &str,
782 linkage: Linkage,
783 writable: bool,
784 tls: bool,
785 ) -> ModuleResult<(DataId, Linkage)> {
786 use super::hash_map::Entry::*;
788 match self.names.entry(name.to_owned()) {
789 Occupied(entry) => match *entry.get() {
790 FuncOrDataId::Data(id) => {
791 let existing = &mut self.data_objects[id];
792 existing.merge(linkage, writable, tls);
793 Ok((id, existing.linkage))
794 }
795
796 FuncOrDataId::Func(..) => {
797 Err(ModuleError::IncompatibleDeclaration(name.to_owned()))
798 }
799 },
800 Vacant(entry) => {
801 let id = self.data_objects.push(DataDeclaration {
802 name: Some(name.to_owned()),
803 linkage,
804 writable,
805 tls,
806 });
807 entry.insert(FuncOrDataId::Data(id));
808 Ok((id, self.data_objects[id].linkage))
809 }
810 }
811 }
812
813 pub fn declare_anonymous_data(&mut self, writable: bool, tls: bool) -> ModuleResult<DataId> {
815 let id = self.data_objects.push(DataDeclaration {
816 name: None,
817 linkage: Linkage::Local,
818 writable,
819 tls,
820 });
821 Ok(id)
822 }
823}
824
825pub trait Module {
827 fn isa(&self) -> &dyn isa::TargetIsa;
829
830 fn declarations(&self) -> &ModuleDeclarations;
832
833 fn get_name(&self, name: &str) -> Option<FuncOrDataId> {
836 self.declarations().get_name(name)
837 }
838
839 fn target_config(&self) -> isa::TargetFrontendConfig {
842 self.isa().frontend_config()
843 }
844
845 fn make_context(&self) -> Context {
850 let mut ctx = Context::new();
851 ctx.func.signature.call_conv = self.isa().default_call_conv();
852 ctx
853 }
854
855 fn clear_context(&self, ctx: &mut Context) {
860 ctx.clear();
861 ctx.func.signature.call_conv = self.isa().default_call_conv();
862 }
863
864 fn make_signature(&self) -> ir::Signature {
868 ir::Signature::new(self.isa().default_call_conv())
869 }
870
871 fn clear_signature(&self, sig: &mut ir::Signature) {
876 sig.clear(self.isa().default_call_conv());
877 }
878
879 fn declare_function(
881 &mut self,
882 name: &str,
883 linkage: Linkage,
884 signature: &ir::Signature,
885 ) -> ModuleResult<FuncId>;
886
887 fn declare_anonymous_function(&mut self, signature: &ir::Signature) -> ModuleResult<FuncId>;
889
890 fn declare_data(
892 &mut self,
893 name: &str,
894 linkage: Linkage,
895 writable: bool,
896 tls: bool,
897 ) -> ModuleResult<DataId>;
898
899 fn declare_anonymous_data(&mut self, writable: bool, tls: bool) -> ModuleResult<DataId>;
901
902 fn declare_func_in_func(&mut self, func_id: FuncId, func: &mut ir::Function) -> ir::FuncRef {
907 let decl = &self.declarations().functions[func_id];
908 let signature = func.import_signature(decl.signature.clone());
909 let user_name_ref = func.declare_imported_user_function(ir::UserExternalName {
910 namespace: 0,
911 index: func_id.as_u32(),
912 });
913 let colocated = decl.linkage.is_final();
914 func.import_function(ir::ExtFuncData {
915 name: ir::ExternalName::user(user_name_ref),
916 signature,
917 colocated,
918 })
919 }
920
921 fn declare_data_in_func(&self, data: DataId, func: &mut ir::Function) -> ir::GlobalValue {
925 let decl = &self.declarations().data_objects[data];
926 let colocated = decl.linkage.is_final();
927 let user_name_ref = func.declare_imported_user_function(ir::UserExternalName {
928 namespace: 1,
929 index: data.as_u32(),
930 });
931 func.create_global_value(ir::GlobalValueData::Symbol {
932 name: ir::ExternalName::user(user_name_ref),
933 offset: ir::immediates::Imm64::new(0),
934 colocated,
935 tls: decl.tls,
936 })
937 }
938
939 fn declare_func_in_data(&self, func_id: FuncId, data: &mut DataDescription) -> ir::FuncRef {
941 data.import_function(ModuleRelocTarget::user(0, func_id.as_u32()))
942 }
943
944 fn declare_data_in_data(&self, data_id: DataId, data: &mut DataDescription) -> ir::GlobalValue {
946 data.import_global_value(ModuleRelocTarget::user(1, data_id.as_u32()))
947 }
948
949 fn define_function(&mut self, func: FuncId, ctx: &mut Context) -> ModuleResult<()> {
960 self.define_function_with_control_plane(func, ctx, &mut ControlPlane::default())
961 }
962
963 fn define_function_with_control_plane(
969 &mut self,
970 func: FuncId,
971 ctx: &mut Context,
972 ctrl_plane: &mut ControlPlane,
973 ) -> ModuleResult<()>;
974
975 fn define_function_bytes(
983 &mut self,
984 func_id: FuncId,
985 alignment: u64,
986 bytes: &[u8],
987 relocs: &[ModuleReloc],
988 ) -> ModuleResult<()>;
989
990 fn define_data(&mut self, data_id: DataId, data: &DataDescription) -> ModuleResult<()>;
992}
993
994impl<M: Module + ?Sized> Module for &mut M {
995 fn isa(&self) -> &dyn isa::TargetIsa {
996 (**self).isa()
997 }
998
999 fn declarations(&self) -> &ModuleDeclarations {
1000 (**self).declarations()
1001 }
1002
1003 fn get_name(&self, name: &str) -> Option<FuncOrDataId> {
1004 (**self).get_name(name)
1005 }
1006
1007 fn target_config(&self) -> isa::TargetFrontendConfig {
1008 (**self).target_config()
1009 }
1010
1011 fn make_context(&self) -> Context {
1012 (**self).make_context()
1013 }
1014
1015 fn clear_context(&self, ctx: &mut Context) {
1016 (**self).clear_context(ctx)
1017 }
1018
1019 fn make_signature(&self) -> ir::Signature {
1020 (**self).make_signature()
1021 }
1022
1023 fn clear_signature(&self, sig: &mut ir::Signature) {
1024 (**self).clear_signature(sig)
1025 }
1026
1027 fn declare_function(
1028 &mut self,
1029 name: &str,
1030 linkage: Linkage,
1031 signature: &ir::Signature,
1032 ) -> ModuleResult<FuncId> {
1033 (**self).declare_function(name, linkage, signature)
1034 }
1035
1036 fn declare_anonymous_function(&mut self, signature: &ir::Signature) -> ModuleResult<FuncId> {
1037 (**self).declare_anonymous_function(signature)
1038 }
1039
1040 fn declare_data(
1041 &mut self,
1042 name: &str,
1043 linkage: Linkage,
1044 writable: bool,
1045 tls: bool,
1046 ) -> ModuleResult<DataId> {
1047 (**self).declare_data(name, linkage, writable, tls)
1048 }
1049
1050 fn declare_anonymous_data(&mut self, writable: bool, tls: bool) -> ModuleResult<DataId> {
1051 (**self).declare_anonymous_data(writable, tls)
1052 }
1053
1054 fn declare_func_in_func(&mut self, func: FuncId, in_func: &mut ir::Function) -> ir::FuncRef {
1055 (**self).declare_func_in_func(func, in_func)
1056 }
1057
1058 fn declare_data_in_func(&self, data: DataId, func: &mut ir::Function) -> ir::GlobalValue {
1059 (**self).declare_data_in_func(data, func)
1060 }
1061
1062 fn declare_func_in_data(&self, func_id: FuncId, data: &mut DataDescription) -> ir::FuncRef {
1063 (**self).declare_func_in_data(func_id, data)
1064 }
1065
1066 fn declare_data_in_data(&self, data_id: DataId, data: &mut DataDescription) -> ir::GlobalValue {
1067 (**self).declare_data_in_data(data_id, data)
1068 }
1069
1070 fn define_function(&mut self, func: FuncId, ctx: &mut Context) -> ModuleResult<()> {
1071 (**self).define_function(func, ctx)
1072 }
1073
1074 fn define_function_with_control_plane(
1075 &mut self,
1076 func: FuncId,
1077 ctx: &mut Context,
1078 ctrl_plane: &mut ControlPlane,
1079 ) -> ModuleResult<()> {
1080 (**self).define_function_with_control_plane(func, ctx, ctrl_plane)
1081 }
1082
1083 fn define_function_bytes(
1084 &mut self,
1085 func_id: FuncId,
1086 alignment: u64,
1087 bytes: &[u8],
1088 relocs: &[ModuleReloc],
1089 ) -> ModuleResult<()> {
1090 (**self).define_function_bytes(func_id, alignment, bytes, relocs)
1091 }
1092
1093 fn define_data(&mut self, data_id: DataId, data: &DataDescription) -> ModuleResult<()> {
1094 (**self).define_data(data_id, data)
1095 }
1096}
1097
1098impl<M: Module + ?Sized> Module for Box<M> {
1099 fn isa(&self) -> &dyn isa::TargetIsa {
1100 (**self).isa()
1101 }
1102
1103 fn declarations(&self) -> &ModuleDeclarations {
1104 (**self).declarations()
1105 }
1106
1107 fn get_name(&self, name: &str) -> Option<FuncOrDataId> {
1108 (**self).get_name(name)
1109 }
1110
1111 fn target_config(&self) -> isa::TargetFrontendConfig {
1112 (**self).target_config()
1113 }
1114
1115 fn make_context(&self) -> Context {
1116 (**self).make_context()
1117 }
1118
1119 fn clear_context(&self, ctx: &mut Context) {
1120 (**self).clear_context(ctx)
1121 }
1122
1123 fn make_signature(&self) -> ir::Signature {
1124 (**self).make_signature()
1125 }
1126
1127 fn clear_signature(&self, sig: &mut ir::Signature) {
1128 (**self).clear_signature(sig)
1129 }
1130
1131 fn declare_function(
1132 &mut self,
1133 name: &str,
1134 linkage: Linkage,
1135 signature: &ir::Signature,
1136 ) -> ModuleResult<FuncId> {
1137 (**self).declare_function(name, linkage, signature)
1138 }
1139
1140 fn declare_anonymous_function(&mut self, signature: &ir::Signature) -> ModuleResult<FuncId> {
1141 (**self).declare_anonymous_function(signature)
1142 }
1143
1144 fn declare_data(
1145 &mut self,
1146 name: &str,
1147 linkage: Linkage,
1148 writable: bool,
1149 tls: bool,
1150 ) -> ModuleResult<DataId> {
1151 (**self).declare_data(name, linkage, writable, tls)
1152 }
1153
1154 fn declare_anonymous_data(&mut self, writable: bool, tls: bool) -> ModuleResult<DataId> {
1155 (**self).declare_anonymous_data(writable, tls)
1156 }
1157
1158 fn declare_func_in_func(&mut self, func: FuncId, in_func: &mut ir::Function) -> ir::FuncRef {
1159 (**self).declare_func_in_func(func, in_func)
1160 }
1161
1162 fn declare_data_in_func(&self, data: DataId, func: &mut ir::Function) -> ir::GlobalValue {
1163 (**self).declare_data_in_func(data, func)
1164 }
1165
1166 fn declare_func_in_data(&self, func_id: FuncId, data: &mut DataDescription) -> ir::FuncRef {
1167 (**self).declare_func_in_data(func_id, data)
1168 }
1169
1170 fn declare_data_in_data(&self, data_id: DataId, data: &mut DataDescription) -> ir::GlobalValue {
1171 (**self).declare_data_in_data(data_id, data)
1172 }
1173
1174 fn define_function(&mut self, func: FuncId, ctx: &mut Context) -> ModuleResult<()> {
1175 (**self).define_function(func, ctx)
1176 }
1177
1178 fn define_function_with_control_plane(
1179 &mut self,
1180 func: FuncId,
1181 ctx: &mut Context,
1182 ctrl_plane: &mut ControlPlane,
1183 ) -> ModuleResult<()> {
1184 (**self).define_function_with_control_plane(func, ctx, ctrl_plane)
1185 }
1186
1187 fn define_function_bytes(
1188 &mut self,
1189 func_id: FuncId,
1190 alignment: u64,
1191 bytes: &[u8],
1192 relocs: &[ModuleReloc],
1193 ) -> ModuleResult<()> {
1194 (**self).define_function_bytes(func_id, alignment, bytes, relocs)
1195 }
1196
1197 fn define_data(&mut self, data_id: DataId, data: &DataDescription) -> ModuleResult<()> {
1198 (**self).define_data(data_id, data)
1199 }
1200}