polycomp 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/ir.rs ADDED
@@ -0,0 +1,239 @@
1
+ use crate::diagnostics::Span;
2
+ use std::fmt;
3
+
4
+ #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5
+ pub struct Register(pub usize);
6
+
7
+ #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8
+ pub struct BasicBlockId(pub usize);
9
+
10
+ #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
11
+ pub struct FnId(pub u32);
12
+
13
+ #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
14
+ pub struct CallSiteId(pub u32);
15
+
16
+ #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
17
+ pub enum BuiltinId {
18
+ Print,
19
+ Println,
20
+ Printf,
21
+ Len,
22
+ Str,
23
+ Int,
24
+ Float,
25
+ Bool,
26
+ Abs,
27
+ Min,
28
+ Max,
29
+ Sqrt,
30
+ Pow,
31
+ Push,
32
+ Pop,
33
+ TypeOf,
34
+ AllocBuf,
35
+ ReadU8,
36
+ WriteU8,
37
+ BufLen,
38
+ ClockMs,
39
+ Assert,
40
+ }
41
+
42
+ impl BuiltinId {
43
+ pub fn from_name(name: &str) -> Option<Self> {
44
+ match name {
45
+ "print" => Some(BuiltinId::Print),
46
+ "println" => Some(BuiltinId::Println),
47
+ "printf" => Some(BuiltinId::Printf),
48
+ "len" => Some(BuiltinId::Len),
49
+ "str" | "String" => Some(BuiltinId::Str),
50
+ "int" => Some(BuiltinId::Int),
51
+ "float" => Some(BuiltinId::Float),
52
+ "bool" => Some(BuiltinId::Bool),
53
+ "abs" | "fabs" => Some(BuiltinId::Abs),
54
+ "min" => Some(BuiltinId::Min),
55
+ "max" => Some(BuiltinId::Max),
56
+ "sqrt" => Some(BuiltinId::Sqrt),
57
+ "pow" => Some(BuiltinId::Pow),
58
+ "push" | "append" => Some(BuiltinId::Push),
59
+ "pop" => Some(BuiltinId::Pop),
60
+ "typeof" | "type" => Some(BuiltinId::TypeOf),
61
+ "alloc_buf" | "malloc" => Some(BuiltinId::AllocBuf),
62
+ "read_u8" => Some(BuiltinId::ReadU8),
63
+ "write_u8" => Some(BuiltinId::WriteU8),
64
+ "buf_len" => Some(BuiltinId::BufLen),
65
+ "clock_ms" => Some(BuiltinId::ClockMs),
66
+ "assert" => Some(BuiltinId::Assert),
67
+ _ => None,
68
+ }
69
+ }
70
+ }
71
+
72
+ #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
73
+ pub enum IrType {
74
+ #[default]
75
+ Dynamic,
76
+ Void,
77
+ Bool,
78
+ I64,
79
+ F64,
80
+ Str,
81
+ }
82
+
83
+ impl IrType {
84
+ pub fn join(self, other: IrType) -> IrType {
85
+ if self == other {
86
+ return self;
87
+ }
88
+ match (self, other) {
89
+ (IrType::I64, IrType::F64) | (IrType::F64, IrType::I64) => IrType::F64,
90
+ (IrType::Bool, IrType::I64) | (IrType::I64, IrType::Bool) => IrType::I64,
91
+ _ => IrType::Dynamic,
92
+ }
93
+ }
94
+
95
+ pub fn is_jit_primitive(self) -> bool {
96
+ matches!(
97
+ self,
98
+ IrType::Void | IrType::Bool | IrType::I64 | IrType::F64 | IrType::Str
99
+ )
100
+ }
101
+ }
102
+
103
+ #[derive(Debug, Clone, PartialEq)]
104
+ pub enum Constant {
105
+ Nil,
106
+ Bool(bool),
107
+ Int(i64),
108
+ Float(f64),
109
+ String(String),
110
+ }
111
+
112
+ #[derive(Debug, Clone, Copy, PartialEq, Eq)]
113
+ pub enum IrBinOp {
114
+ Add,
115
+ Sub,
116
+ Mul,
117
+ Div,
118
+ FloorDiv,
119
+ Mod,
120
+ Eq,
121
+ Ne,
122
+ Lt,
123
+ Le,
124
+ Gt,
125
+ Ge,
126
+ And,
127
+ Or,
128
+ BitAnd,
129
+ BitOr,
130
+ BitXor,
131
+ Shl,
132
+ Shr,
133
+ }
134
+
135
+ #[derive(Debug, Clone, Copy, PartialEq, Eq)]
136
+ pub enum IrUnaryOp {
137
+ Neg,
138
+ Not,
139
+ BitNot,
140
+ BitsToF64,
141
+ }
142
+
143
+ #[derive(Debug, Clone)]
144
+ pub enum LazyTarget {
145
+ Direct {
146
+ from_module: usize,
147
+ symbol: String,
148
+ },
149
+ ModuleMember {
150
+ module_id: usize,
151
+ member: String,
152
+ },
153
+ SelfModule {
154
+ module_id: usize,
155
+ symbol: String,
156
+ },
157
+ }
158
+
159
+ #[derive(Debug, Clone)]
160
+ pub struct CallSite {
161
+ pub id: CallSiteId,
162
+ pub target: LazyTarget,
163
+ pub span: Span,
164
+ pub resolved_fn: Option<FnId>,
165
+ }
166
+
167
+ #[derive(Debug, Clone)]
168
+ pub enum IrInstruction {
169
+ LoadConst(Register, Constant),
170
+ Move(Register, Register),
171
+ Cast(Register, IrType, Register),
172
+ BinOp(IrBinOp, Register, Register, Register),
173
+ UnaryOp(IrUnaryOp, Register, Register),
174
+
175
+ /// Demand-driven, lazy cross-language call site.
176
+ /// The target module is only parsed & target function only lowered when executed.
177
+ CallLazy(Register, CallSiteId, Vec<Register>),
178
+ /// Fast direct call once resolved/known (e.g., self-recursion or resolved target).
179
+ CallFn(Register, FnId, Vec<Register>),
180
+ CallIndirect(Register, Register, Vec<Register>),
181
+ CallBuiltin(Register, BuiltinId, Vec<Register>),
182
+ ResolveFn(Register, CallSiteId),
183
+ LoadFnRef(Register, FnId),
184
+ Return(Option<Register>),
185
+
186
+ BranchIf(Register, BasicBlockId, BasicBlockId),
187
+ Jump(BasicBlockId),
188
+
189
+ CreateArray(Register, Vec<Register>),
190
+ GetIndex(Register, Register, Register),
191
+ SetIndex(Register, Register, Register),
192
+
193
+ CreateObject(Register, String),
194
+ SetField(Register, String, Register),
195
+ GetField(Register, Register, String),
196
+
197
+ AllocBuffer(Register, Register),
198
+ StoreByte(Register, Register, Register),
199
+ LoadByte(Register, Register, Register),
200
+ }
201
+
202
+ #[derive(Debug, Clone)]
203
+ pub struct BasicBlock {
204
+ pub id: BasicBlockId,
205
+ pub instructions: Vec<IrInstruction>,
206
+ }
207
+
208
+ #[derive(Debug, Clone)]
209
+ pub struct IrFunction {
210
+ pub id: FnId,
211
+ pub module_id: usize,
212
+ pub qualified_name: String,
213
+ pub params: Vec<(Register, IrType)>,
214
+ pub return_type: IrType,
215
+ pub register_types: Vec<IrType>,
216
+ pub blocks: Vec<BasicBlock>,
217
+ }
218
+
219
+ impl fmt::Display for IrFunction {
220
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
221
+ writeln!(
222
+ f,
223
+ "fn {} ({} params) -> {:?} {{",
224
+ self.qualified_name,
225
+ self.params.len(),
226
+ self.return_type
227
+ )?;
228
+ for (i, ty) in self.register_types.iter().enumerate() {
229
+ writeln!(f, " ; r{i}: {ty:?}")?;
230
+ }
231
+ for bb in &self.blocks {
232
+ writeln!(f, " bb{}:", bb.id.0)?;
233
+ for instr in &bb.instructions {
234
+ writeln!(f, " {instr:?}")?;
235
+ }
236
+ }
237
+ writeln!(f, "}}")
238
+ }
239
+ }