1 //! Shared definitions for the Cranelift intermediate language. 2 3 pub mod entities; 4 pub mod formats; 5 pub mod immediates; 6 pub mod instructions; 7 pub mod legalize; 8 pub mod settings; 9 pub mod types; 10 11 use crate::cdsl::formats::FormatRegistry; 12 use crate::cdsl::inst::InstructionGroup; 13 use crate::cdsl::operands::OperandKind; 14 use crate::cdsl::settings::SettingGroup; 15 use crate::cdsl::xform::TransformGroups; 16 17 pub struct Definitions { 18 pub settings: SettingGroup, 19 pub instructions: InstructionGroup, 20 pub operand_kinds: OperandKinds, 21 pub format_registry: FormatRegistry, 22 pub transform_groups: TransformGroups, 23 } 24 25 pub struct OperandKinds(Vec<OperandKind>); 26 27 impl OperandKinds { 28 pub fn new() -> Self { 29 Self(Vec::new()) 30 } 31 32 pub fn by_name(&self, name: &'static str) -> &OperandKind { 33 self.0 34 .iter() 35 .find(|op| op.name == name) 36 .expect(&format!("unknown Operand name: {}", name)) 37 } 38 39 pub fn push(&mut self, operand_kind: OperandKind) { 40 assert!( 41 self.0 42 .iter() 43 .find(|existing| existing.name == operand_kind.name) 44 .is_none(), 45 "trying to insert operand kind '{}' for the second time", 46 operand_kind.name 47 ); 48 self.0.push(operand_kind); 49 } 50 } 51 52 pub fn define() -> Definitions { 53 let immediates = OperandKinds(immediates::define()); 54 let entities = OperandKinds(entities::define()); 55 let format_registry = formats::define(&immediates, &entities); 56 let instructions = instructions::define(&format_registry, &immediates, &entities); 57 let transform_groups = legalize::define(&instructions, &immediates); 58 59 Definitions { 60 settings: settings::define(), 61 instructions, 62 operand_kinds: immediates, 63 format_registry, 64 transform_groups, 65 } 66 } 67