polycomp 0.0.0-stage → 0.3.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,272 @@
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
+ Sprintf,
22
+ Len,
23
+ Str,
24
+ Int,
25
+ Float,
26
+ Bool,
27
+ Abs,
28
+ Min,
29
+ Max,
30
+ Sqrt,
31
+ Pow,
32
+ Floor,
33
+ Ceil,
34
+ Round,
35
+ Sin,
36
+ Cos,
37
+ Log,
38
+ Push,
39
+ Pop,
40
+ TypeOf,
41
+ AllocBuf,
42
+ ReadU8,
43
+ WriteU8,
44
+ BufLen,
45
+ ClockMs,
46
+ Assert,
47
+ Range,
48
+ RegexNew,
49
+ RegexTest,
50
+ RegexMatch,
51
+ RegexFindAll,
52
+ RegexReplace,
53
+ RegexSplit,
54
+ StrContains,
55
+ StrSplit,
56
+ StrTrim,
57
+ StrUpper,
58
+ StrLower,
59
+ StrReplace,
60
+ }
61
+
62
+ impl BuiltinId {
63
+ pub fn from_name(name: &str) -> Option<Self> {
64
+ match name {
65
+ "print" => Some(BuiltinId::Print),
66
+ "println" => Some(BuiltinId::Println),
67
+ "printf" => Some(BuiltinId::Printf),
68
+ "sprintf" => Some(BuiltinId::Sprintf),
69
+ "len" => Some(BuiltinId::Len),
70
+ "str" | "String" | "to_string" => Some(BuiltinId::Str),
71
+ "int" => Some(BuiltinId::Int),
72
+ "float" => Some(BuiltinId::Float),
73
+ "bool" => Some(BuiltinId::Bool),
74
+ "abs" | "fabs" => Some(BuiltinId::Abs),
75
+ "min" => Some(BuiltinId::Min),
76
+ "max" => Some(BuiltinId::Max),
77
+ "sqrt" => Some(BuiltinId::Sqrt),
78
+ "pow" => Some(BuiltinId::Pow),
79
+ "floor" => Some(BuiltinId::Floor),
80
+ "ceil" => Some(BuiltinId::Ceil),
81
+ "round" => Some(BuiltinId::Round),
82
+ "sin" => Some(BuiltinId::Sin),
83
+ "cos" => Some(BuiltinId::Cos),
84
+ "log" => Some(BuiltinId::Log),
85
+ "push" | "append" => Some(BuiltinId::Push),
86
+ "pop" => Some(BuiltinId::Pop),
87
+ "typeof" | "type" => Some(BuiltinId::TypeOf),
88
+ "alloc_buf" | "malloc" => Some(BuiltinId::AllocBuf),
89
+ "read_u8" => Some(BuiltinId::ReadU8),
90
+ "write_u8" => Some(BuiltinId::WriteU8),
91
+ "buf_len" => Some(BuiltinId::BufLen),
92
+ "clock_ms" => Some(BuiltinId::ClockMs),
93
+ "assert" => Some(BuiltinId::Assert),
94
+ "range" => Some(BuiltinId::Range),
95
+ "RegExp" => Some(BuiltinId::RegexNew),
96
+ _ => None,
97
+ }
98
+ }
99
+ }
100
+
101
+ #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
102
+ pub enum IrType {
103
+ #[default]
104
+ Dynamic,
105
+ Void,
106
+ Bool,
107
+ I64,
108
+ F64,
109
+ Str,
110
+ }
111
+
112
+ impl IrType {
113
+ pub fn join(self, other: IrType) -> IrType {
114
+ if self == other {
115
+ return self;
116
+ }
117
+ match (self, other) {
118
+ (IrType::I64, IrType::F64) | (IrType::F64, IrType::I64) => IrType::F64,
119
+ (IrType::Bool, IrType::I64) | (IrType::I64, IrType::Bool) => IrType::I64,
120
+ _ => IrType::Dynamic,
121
+ }
122
+ }
123
+
124
+ pub fn is_jit_primitive(self) -> bool {
125
+ matches!(
126
+ self,
127
+ IrType::Void | IrType::Bool | IrType::I64 | IrType::F64 | IrType::Str
128
+ )
129
+ }
130
+ }
131
+
132
+ #[derive(Debug, Clone, PartialEq)]
133
+ pub enum Constant {
134
+ Nil,
135
+ Bool(bool),
136
+ Int(i64),
137
+ Float(f64),
138
+ String(String),
139
+ Regex { pattern: String, flags: String },
140
+ }
141
+
142
+ #[derive(Debug, Clone, Copy, PartialEq, Eq)]
143
+ pub enum IrBinOp {
144
+ Add,
145
+ Sub,
146
+ Mul,
147
+ Div,
148
+ FloorDiv,
149
+ Mod,
150
+ Pow,
151
+ Eq,
152
+ Ne,
153
+ Lt,
154
+ Le,
155
+ Gt,
156
+ Ge,
157
+ And,
158
+ Or,
159
+ BitAnd,
160
+ BitOr,
161
+ BitXor,
162
+ Shl,
163
+ Shr,
164
+ }
165
+
166
+ #[derive(Debug, Clone, Copy, PartialEq, Eq)]
167
+ pub enum IrUnaryOp {
168
+ Neg,
169
+ Not,
170
+ BitNot,
171
+ BitsToF64,
172
+ }
173
+
174
+ #[derive(Debug, Clone)]
175
+ pub enum LazyTarget {
176
+ Direct {
177
+ from_module: usize,
178
+ symbol: String,
179
+ },
180
+ ModuleMember {
181
+ module_id: usize,
182
+ member: String,
183
+ },
184
+ SelfModule {
185
+ module_id: usize,
186
+ symbol: String,
187
+ },
188
+ }
189
+
190
+ #[derive(Debug, Clone)]
191
+ pub struct CallSite {
192
+ pub id: CallSiteId,
193
+ pub target: LazyTarget,
194
+ pub span: Span,
195
+ pub resolved_fn: Option<FnId>,
196
+ }
197
+
198
+ #[derive(Debug, Clone)]
199
+ pub enum IrInstruction {
200
+ LoadConst(Register, Constant),
201
+ Move(Register, Register),
202
+ Cast(Register, IrType, Register),
203
+ BinOp(IrBinOp, Register, Register, Register),
204
+ UnaryOp(IrUnaryOp, Register, Register),
205
+
206
+ CallLazy(Register, CallSiteId, Vec<Register>),
207
+ CallFn(Register, FnId, Vec<Register>),
208
+ CallIndirect(Register, Register, Vec<Register>),
209
+ CallMethod(Register, Register, String, Vec<Register>),
210
+ CallBuiltin(Register, BuiltinId, Vec<Register>),
211
+ ResolveFn(Register, CallSiteId),
212
+ LoadFnRef(Register, FnId),
213
+ Return(Option<Register>),
214
+
215
+ BranchIf(Register, BasicBlockId, BasicBlockId),
216
+ Jump(BasicBlockId),
217
+
218
+ TryBegin(BasicBlockId, Register),
219
+ TryEnd,
220
+ Throw(Register),
221
+
222
+ CreateArray(Register, Vec<Register>),
223
+ GetIndex(Register, Register, Register),
224
+ SetIndex(Register, Register, Register),
225
+
226
+ CreateObject(Register, String),
227
+ SetField(Register, String, Register),
228
+ GetField(Register, Register, String),
229
+
230
+ AllocBuffer(Register, Register),
231
+ StoreByte(Register, Register, Register),
232
+ LoadByte(Register, Register, Register),
233
+ }
234
+
235
+ #[derive(Debug, Clone)]
236
+ pub struct BasicBlock {
237
+ pub id: BasicBlockId,
238
+ pub instructions: Vec<IrInstruction>,
239
+ }
240
+
241
+ #[derive(Debug, Clone)]
242
+ pub struct IrFunction {
243
+ pub id: FnId,
244
+ pub module_id: usize,
245
+ pub qualified_name: String,
246
+ pub params: Vec<(Register, IrType)>,
247
+ pub return_type: IrType,
248
+ pub register_types: Vec<IrType>,
249
+ pub blocks: Vec<BasicBlock>,
250
+ }
251
+
252
+ impl fmt::Display for IrFunction {
253
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
254
+ writeln!(
255
+ f,
256
+ "fn {} ({} params) -> {:?} {{",
257
+ self.qualified_name,
258
+ self.params.len(),
259
+ self.return_type
260
+ )?;
261
+ for (i, ty) in self.register_types.iter().enumerate() {
262
+ writeln!(f, " ; r{i}: {ty:?}")?;
263
+ }
264
+ for bb in &self.blocks {
265
+ writeln!(f, " bb{}:", bb.id.0)?;
266
+ for instr in &bb.instructions {
267
+ writeln!(f, " {instr:?}")?;
268
+ }
269
+ }
270
+ writeln!(f, "}}")
271
+ }
272
+ }