cardinal_codegen/entities.rs
1//! Entities that the code generator use.
2
3use crate::instruction::InstructionInfo;
4
5/// An opaque reference to a Cardinal SSA value. These can be used as instruction parameters,
6/// if a value is not used, it will not be included in the generated code.
7#[derive(Clone, Copy, PartialEq)]
8pub struct Value(pub u32);
9
10/// An opaque reference to a Cardinal IR block.
11#[derive(Clone, Copy, PartialEq)]
12pub struct Block(pub u32);
13
14/// An opaque reference to a Cardinal variable.
15#[derive(Clone, PartialEq)]
16pub struct Variable(pub String);
17
18impl Variable {
19
20 pub fn named(&self) -> Named {
21 Named::new(self.0.to_string())
22 }
23
24}
25
26/// An opaque reference to a Cardinal global variable.
27#[derive(Clone, PartialEq)]
28pub struct GlobalVariable(pub String);
29
30impl GlobalVariable {
31
32 pub fn named(&self) -> Named {
33 Named::new(self.0.to_string())
34 }
35
36}
37
38/// Different types of types that can be declared.
39#[derive(Clone, PartialEq)]
40pub enum Type {
41
42 /// A plain type, such as `int` or `double`.
43 Plain,
44
45 /// An array type, such as `char[]` or `int[]`. `-1` should be used as the size argument if
46 /// a size should be declared implicitly.
47 Array(isize),
48
49 /// A pointer type, such as `char*` or `int*`.
50 Pointer,
51
52}
53
54/// An ABI type.
55#[derive(Clone, PartialEq)]
56pub struct AbiType(pub String, pub Type);
57
58/// An ABI value used for function parameters.
59#[derive(Clone, PartialEq)]
60pub struct AbiParam(pub String, pub AbiType);
61
62/// Properties of a `Named` struct that may be basic properties, static properties, pointer
63/// properties or index properties.
64#[derive(Clone, PartialEq)]
65pub enum NamedProperty {
66
67 /// A basic property, for example, `Named.Basic`.
68 Basic(String),
69
70 /// A reference to a static property, which is not supported by the C backend. For example,
71 /// `Named::Basic` in C++ or Rust.
72 Static(String),
73
74 /// A pointer property, for example, `Named->Pointer`.
75 Pointer(String),
76
77 /// An index in a named value, for example, `Named[Index]`. It uses a value that is
78 /// determined at compile time.
79 Index(Value),
80
81}
82
83/// Used as a named reference to an object.
84#[derive(Clone, PartialEq)]
85pub struct Named {
86
87 /// The name of the first object in the reference.
88 pub name: String,
89
90 /// A list of indexes performed on the Named reference.
91 ///
92 /// For example,
93 /// ```c
94 /// Named.Index1->Index2
95 /// ```
96 pub properties: Vec<NamedProperty>
97
98}
99
100impl Named {
101
102 /// Creates a Named object with no properties.
103 pub fn new(name: String) -> Self {
104 Self {
105 name,
106 properties: vec![]
107 }
108 }
109
110 /// Creates a Named object with a list of properties.
111 pub fn new_props(name: String, properties: Vec<NamedProperty>) -> Self {
112 Self {
113 name,
114 properties
115 }
116 }
117
118}
119
120
121/// Information about a value.
122#[derive(Clone)]
123pub enum ValueInfo {
124
125 /// An integer constant.
126 IntegerConstant(u64),
127
128 /// A floating point number constant.
129 FloatConstant(f64),
130
131 /// A double constant.
132 DoubleConstant(f64),
133
134 /// A boolean constant.
135 BooleanConstant(bool),
136
137 /// A string constant.
138 StringConstant(String),
139
140 /// A character constant.
141 CharConstant(String),
142
143 /// A named reference.
144 Named(Named),
145
146 /// A value reference to a block.
147 Block(Block),
148
149 /// A pointer to an instruction.
150 Instruction(InstructionInfo),
151
152}