zust-vm 0.9.8

Cranelift JIT runtime for executing Zust modules.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
use super::{JITRunTime, context::BuildContext};
use cranelift::prelude::*;
use cranelift_module::{DataDescription, Module};
use dynamic::{Dynamic, Type};
use parser::{BinaryOp, Expr};

use anyhow::{Result, anyhow};

impl JITRunTime {
    fn any_to_string(&mut self, ctx: &mut BuildContext, vt: (Value, Type)) -> Result<Value> {
        let value = self.convert(ctx, vt, Type::Any)?;
        self.call(ctx, self.get_method(&Type::Any, "to_string")?, vec![value]).map(|(v, _)| v)
    }

    fn any_logic(&mut self, ctx: &mut BuildContext, left: Value, op: BinaryOp, right: Value) -> Result<(Value, Type)> {
        let op = ctx.builder.ins().iconst(types::I32, i32::from(op) as i64);
        self.call(ctx, self.get_method(&Type::Any, "logic")?, vec![left, op, right])
    }

    fn struct_to_dynamic(&mut self, ctx: &mut BuildContext, base: Value, ty: &Type) -> Result<Value> {
        let Type::Struct { params: _, fields: _ } = ty else {
            return Err(anyhow!("不是结构体 {:?}", ty));
        };
        let id = self.module.declare_anonymous_data(true, false)?;
        let mut desc = DataDescription::new();
        let ty_ptr = Box::into_raw(Box::new(ty.clone()));
        desc.define((ty_ptr as i64).to_le_bytes().into());
        self.module.define_data(id, &desc)?;
        let ty_data = self.module.declare_data_in_func(id, &mut ctx.builder.func);
        let ty_addr = ctx.builder.ins().global_value(crate::ptr_type(), ty_data);
        let ty_ptr = ctx.builder.ins().load(crate::ptr_type(), MemFlags::new(), ty_addr, 0);
        let f = self.get_fn(self.get_id("__struct_from_ptr")?, &[Type::I64, Type::I64])?;
        self.call(ctx, f, vec![base, ty_ptr]).map(|(v, _)| v)
    }

    pub(crate) fn bool_value(&mut self, ctx: &mut BuildContext, vt: (Value, Type)) -> Result<Value> {
        if vt.1.is_bool() {
            Ok(vt.0)
        } else if vt.1.is_any() {
            self.call(ctx, self.get_method(&Type::Any, "to_bool")?, vec![vt.0]).map(|(v, _)| v)
        } else if vt.1.is_int() || vt.1.is_uint() {
            Ok(ctx.builder.ins().icmp_imm(IntCC::NotEqual, vt.0, 0))
        } else if vt.1.is_f32() {
            let zero = ctx.builder.ins().f32const(0.0);
            Ok(ctx.builder.ins().fcmp(FloatCC::NotEqual, vt.0, zero))
        } else if vt.1.is_f64() {
            let zero = ctx.builder.ins().f64const(0.0);
            Ok(ctx.builder.ins().fcmp(FloatCC::NotEqual, vt.0, zero))
        } else {
            Err(anyhow!("cannot convert {:?} to bool", vt.1))
        }
    }

    pub fn convert(&mut self, ctx: &mut BuildContext, vt: (Value, Type), ty: Type) -> Result<Value> {
        if vt.1 != ty {
            if ty.is_any() {
                if vt.1.is_struct() {
                    return self.struct_to_dynamic(ctx, vt.0, &vt.1);
                } else if vt.1.is_bool() {
                    return self.call(ctx, self.get_method(&Type::Any, "from_bool")?, vec![vt.0]).map(|(v, _)| v);
                } else if vt.1.is_uint() {
                    let v = if vt.1.width() < 8 { ctx.builder.ins().uextend(types::I64, vt.0) } else { vt.0 };
                    return self.call(ctx, self.get_method(&Type::Any, "from_i64")?, vec![v]).map(|(v, _)| v);
                } else if vt.1.is_int() {
                    let v = if vt.1.width() < 8 { ctx.builder.ins().sextend(types::I64, vt.0) } else { vt.0 };
                    return self.call(ctx, self.get_method(&Type::Any, "from_i64")?, vec![v]).map(|(v, _)| v);
                } else if vt.1.is_f32() {
                    let v = ctx.builder.ins().fpromote(types::F64, vt.0);
                    return self.call(ctx, self.get_method(&Type::Any, "from_f64")?, vec![v]).map(|(v, _)| v);
                } else if vt.1.is_f64() {
                    return self.call(ctx, self.get_method(&Type::Any, "from_f64")?, vec![vt.0]).map(|(v, _)| v);
                } else if vt.1.is_str() {
                    return Ok(vt.0);
                }
            } else if vt.1.is_any() {
                if ty.is_bool() {
                    return self.call(ctx, self.get_method(&Type::Any, "to_bool")?, vec![vt.0]).map(|(v, _)| v);
                } else if ty.is_str() {
                    return self.call(ctx, self.get_method(&Type::Any, "to_string")?, vec![vt.0]).map(|(v, _)| v);
                } else if ty.is_int() | ty.is_uint() {
                    let (v, _) = self.call(ctx, self.get_method(&Type::Any, "to_i64")?, vec![vt.0])?;
                    return Ok(match ty.width() {
                        1 => ctx.builder.ins().ireduce(types::I8, v),
                        2 => ctx.builder.ins().ireduce(types::I16, v),
                        4 => ctx.builder.ins().ireduce(types::I32, v),
                        _ => v,
                    });
                } else if ty.is_f32() {
                    let v = self.call(ctx, self.get_method(&Type::Any, "to_f64")?, vec![vt.0]).map(|(v, _)| v)?;
                    return Ok(ctx.builder.ins().fdemote(types::F32, v));
                } else if ty.is_f64() {
                    return self.call(ctx, self.get_method(&Type::Any, "to_f64")?, vec![vt.0]).map(|(v, _)| v);
                } else {
                    return Ok(vt.0);
                }
            } else if ty.is_str() {
                return self.any_to_string(ctx, vt);
            } else if ty.is_int() || ty.is_uint() {
                if vt.1.is_f32() || vt.1.is_f64() {
                    let target = crate::get_type(&ty)?;
                    if ty.is_uint() {
                        return Ok(ctx.builder.ins().fcvt_to_uint(target, vt.0));
                    } else if ty.is_int() {
                        return Ok(ctx.builder.ins().fcvt_to_sint(target, vt.0));
                    }
                }
                if vt.1.is_int() || vt.1.is_uint() || vt.1.is_bool() {
                    let target = crate::get_type(&ty)?;
                    let actual = ctx.builder.func.dfg.value_type(vt.0);
                    if actual == target {
                        return Ok(vt.0);
                    }
                    if actual.is_int() && target.is_int() {
                        if actual.bits() > target.bits() {
                            return Ok(ctx.builder.ins().ireduce(target, vt.0));
                        }
                        if actual.bits() < target.bits() {
                            return if vt.1.is_int() { Ok(ctx.builder.ins().sextend(target, vt.0)) } else { Ok(ctx.builder.ins().uextend(target, vt.0)) };
                        }
                    }
                }
            } else if ty.is_f32() {
                if vt.1.is_int() {
                    return Ok(ctx.builder.ins().fcvt_from_sint(types::F32, vt.0));
                } else if vt.1.is_uint() {
                    return Ok(ctx.builder.ins().fcvt_from_uint(types::F32, vt.0));
                } else if vt.1.is_f64() {
                    return Ok(ctx.builder.ins().fdemote(types::F32, vt.0));
                }
            } else if ty.is_f64() {
                if vt.1.is_int() {
                    return Ok(ctx.builder.ins().fcvt_from_sint(types::F64, vt.0));
                } else if vt.1.is_uint() {
                    return Ok(ctx.builder.ins().fcvt_from_uint(types::F64, vt.0));
                } else if vt.1.is_f32() {
                    return Ok(ctx.builder.ins().fpromote(types::F64, vt.0));
                }
            } else if let Type::Symbol { id: _, params: _ } = ty {
                log::info!("convert {:?} -> {:?}", vt, ty);
                return Ok(vt.0); //结构类型 可以看作 External 类型
            }
            if vt.1.is_bool() {
                let v = ctx.builder.ins().sextend(types::I64, vt.0);
                return self.call(ctx, self.get_method(&Type::Any, "from_i64")?, vec![v]).map(|(v, _)| v);
            }
            log::error!("未实现 {:?} {:?}", vt, ty); //暂时还没有实现 struct 的 初始化
            Ok(vt.0)
        } else {
            Ok(vt.0)
        }
    }

    pub(crate) fn binary(&mut self, ctx: &mut BuildContext, left: (Value, Type), op: BinaryOp, right: &Expr) -> Result<(Value, Type)> {
        //处理可以计算的简单情形
        if matches!(op, BinaryOp::And | BinaryOp::Or) {
            return self.short_circuit_logic(ctx, left, op, right);
        }
        let right_ty_hint = if right.is_value() { right.clone().value().ok().map(|v| v.get_type()) } else { self.get_dynamic(right).map(|v| v.get_type()) };
        let right = if right.is_value() {
            let right = right.clone().value()?;
            if right.is_f32() {
                (ctx.builder.ins().f32const(right.as_float().unwrap() as f32), Type::F32)
            } else if right.is_f64() {
                (ctx.builder.ins().f64const(right.as_float().unwrap() as f64), Type::F64)
            } else if left.1.is_any() {
                if right.is_int() {
                    (ctx.builder.ins().iconst(types::I64, right.as_int().unwrap()), Type::I64)
                } else if right.is_null() {
                    self.call(ctx, self.get_method(&Type::Any, "null")?, vec![])?
                } else {
                    ctx.get_const(&right)?
                }
            } else {
                return self.binary_imm(ctx, left, op, right);
            }
        } else {
            self.eval(ctx, right)?.get(ctx).ok_or_else(|| anyhow!("没有返回值: {:?}", right))?
        };
        let right_ty = right_ty_hint.as_ref().unwrap_or(&right.1);
        let ty = if (op.is_add() || op.is_logic()) && (left.1.is_str() || right.1.is_str() || right_ty.is_str()) {
            Type::Str
        } else if (op.is_add() || op.is_logic()) && (left.1.is_any() || right.1.is_any()) {
            Type::Any
        } else {
            left.1.clone() + right.1.clone()
        }; //为了支持字符串的加法需要单独处理
        let left = self.convert(ctx, left, ty.clone())?;
        let right = self.convert(ctx, right, ty.clone())?;
        if ty.is_any() {
            if op.is_logic() {
                return self.any_logic(ctx, left, op, right);
            } else {
                let op = ctx.builder.ins().iconst(types::I32, i32::from(op) as i64);
                return self.call(ctx, self.get_method(&Type::Any, "binary")?, vec![left, op, right]);
            }
        }
        if ty.is_str() && op.is_logic() {
            return self.any_logic(ctx, left, op, right);
        }
        match op {
            BinaryOp::Add | BinaryOp::AddAssign => {
                if ty.is_int() || ty.is_uint() {
                    return Ok((ctx.builder.ins().iadd(left, right), ty));
                } else if ty.is_float() {
                    return Ok((ctx.builder.ins().fadd(left, right), ty));
                } else if ty.is_str() {
                    let op = ctx.builder.ins().iconst(types::I32, i32::from(op) as i64);
                    let result = self.call(ctx, self.get_method(&Type::Any, "binary")?, vec![left, op, right])?.0;
                    return Ok((result, ty));
                }
            }
            BinaryOp::Sub | BinaryOp::SubAssign => {
                if ty.is_int() || ty.is_uint() {
                    return Ok((ctx.builder.ins().isub(left, right), ty));
                } else if ty.is_float() {
                    return Ok((ctx.builder.ins().fsub(left, right), ty));
                }
            }
            BinaryOp::Mul | BinaryOp::MulAssign => {
                if ty.is_int() || ty.is_uint() {
                    return Ok((ctx.builder.ins().imul(left, right), ty));
                } else if ty.is_float() {
                    return Ok((ctx.builder.ins().fmul(left, right), ty));
                }
            }
            BinaryOp::Div | BinaryOp::DivAssign => {
                if ty.is_int() {
                    return Ok((ctx.builder.ins().sdiv(left, right), ty));
                } else if ty.is_uint() {
                    return Ok((ctx.builder.ins().udiv(left, right), ty));
                } else if ty.is_float() {
                    return Ok((ctx.builder.ins().fdiv(left, right), ty));
                }
            }
            BinaryOp::Mod | BinaryOp::ModAssign => {
                if ty.is_int() {
                    return Ok((ctx.builder.ins().srem(left, right), ty));
                } else if ty.is_uint() {
                    return Ok((ctx.builder.ins().urem(left, right), ty));
                }
            }
            BinaryOp::Shl | BinaryOp::ShlAssign => {
                if ty.is_int() || ty.is_uint() {
                    return Ok((ctx.builder.ins().ishl(left, right), ty));
                }
            }
            BinaryOp::Shr | BinaryOp::ShrAssign => {
                if ty.is_int() {
                    return Ok((ctx.builder.ins().sshr(left, right), ty));
                } else if ty.is_uint() {
                    return Ok((ctx.builder.ins().ushr(left, right), ty));
                }
            }
            BinaryOp::BitAnd | BinaryOp::BitAndAssign => {
                return Ok((ctx.builder.ins().band(left, right), ty));
            }
            BinaryOp::BitOr | BinaryOp::BitOrAssign => {
                return Ok((ctx.builder.ins().bor(left, right), ty));
            }
            BinaryOp::BitXor | BinaryOp::BitXorAssign => {
                return Ok((ctx.builder.ins().bxor(left, right), ty));
            }
            BinaryOp::Eq => {
                if ty.is_int() | ty.is_uint() || ty.is_bool() {
                    return Ok((ctx.builder.ins().icmp(IntCC::Equal, left, right), Type::Bool));
                } else if ty.is_float() {
                    return Ok((ctx.builder.ins().fcmp(FloatCC::Equal, left, right), Type::Bool));
                }
            }
            BinaryOp::Ne => {
                if ty.is_int() | ty.is_uint() || ty.is_bool() {
                    return Ok((ctx.builder.ins().icmp(IntCC::NotEqual, left, right), Type::Bool));
                } else if ty.is_float() {
                    return Ok((ctx.builder.ins().fcmp(FloatCC::NotEqual, left, right), Type::Bool));
                }
            }
            BinaryOp::Lt => {
                if ty.is_int() {
                    return Ok((ctx.builder.ins().icmp(IntCC::SignedLessThan, left, right), Type::Bool));
                } else if ty.is_uint() {
                    return Ok((ctx.builder.ins().icmp(IntCC::UnsignedLessThan, left, right), Type::Bool));
                } else if ty.is_float() {
                    return Ok((ctx.builder.ins().fcmp(FloatCC::LessThan, left, right), Type::Bool));
                }
            }
            BinaryOp::Le => {
                if ty.is_int() {
                    return Ok((ctx.builder.ins().icmp(IntCC::SignedLessThanOrEqual, left, right), Type::Bool));
                } else if ty.is_uint() {
                    return Ok((ctx.builder.ins().icmp(IntCC::UnsignedLessThanOrEqual, left, right), Type::Bool));
                } else if ty.is_float() {
                    return Ok((ctx.builder.ins().fcmp(FloatCC::LessThanOrEqual, left, right), Type::Bool));
                }
            }
            BinaryOp::Gt => {
                if ty.is_int() {
                    return Ok((ctx.builder.ins().icmp(IntCC::SignedGreaterThan, left, right), Type::Bool));
                } else if ty.is_uint() {
                    return Ok((ctx.builder.ins().icmp(IntCC::UnsignedGreaterThan, left, right), Type::Bool));
                } else if ty.is_float() {
                    return Ok((ctx.builder.ins().fcmp(FloatCC::GreaterThan, left, right), Type::Bool));
                }
            }
            BinaryOp::Ge => {
                if ty.is_int() {
                    return Ok((ctx.builder.ins().icmp(IntCC::SignedGreaterThanOrEqual, left, right), Type::Bool));
                } else if ty.is_uint() {
                    return Ok((ctx.builder.ins().icmp(IntCC::UnsignedGreaterThanOrEqual, left, right), Type::Bool));
                } else if ty.is_float() {
                    return Ok((ctx.builder.ins().fcmp(FloatCC::GreaterThanOrEqual, left, right), Type::Bool));
                }
            }
            _ => {}
        }
        panic!("未实现 {:?} {:?} {:?} {:?}", left, op, right, ty)
    }

    pub(crate) fn binary_imm<'a>(&mut self, ctx: &'a mut BuildContext, left: (Value, Type), op: BinaryOp, right: Dynamic) -> Result<(Value, Type)> {
        let ty = left.1.clone() + right.get_type();
        let left = self.convert(ctx, left, ty.clone())?;
        if ty.is_str() && op.is_logic() {
            let right_vt = ctx.get_const(&right).or_else(|_| {
                let idx = self.compiler.get_const(right.clone());
                self.get_const_value(ctx, idx)
            })?;
            let right = self.convert(ctx, right_vt, Type::Str)?;
            return self.any_logic(ctx, left, op, right);
        }
        match op {
            BinaryOp::Add | BinaryOp::AddAssign => {
                if ty.is_str() {
                    let right_vt = ctx.get_const(&right).or_else(|_| {
                        let idx = self.compiler.get_const(right.clone());
                        self.get_const_value(ctx, idx)
                    })?;
                    let right = self.convert(ctx, right_vt, Type::Str)?;
                    let op = ctx.builder.ins().iconst(types::I32, i32::from(op) as i64);
                    let result = self.call(ctx, self.get_method(&Type::Any, "binary")?, vec![left, op, right])?.0;
                    return Ok((result, ty));
                }
                if ty.is_int() | ty.is_uint() {
                    return Ok((ctx.builder.ins().iadd_imm(left, right.as_int().ok_or(anyhow!("非整数"))?), ty));
                }
            }
            BinaryOp::Sub | BinaryOp::SubAssign => {
                if ty.is_int() | ty.is_uint() {
                    return Ok((ctx.builder.ins().iadd_imm(left, -right.as_int().ok_or(anyhow!("非整数"))?), ty));
                }
            }
            BinaryOp::Mul | BinaryOp::MulAssign => {
                if ty.is_int() | ty.is_uint() {
                    return Ok((ctx.builder.ins().imul_imm(left, right.as_int().ok_or(anyhow!("非整数"))?), ty));
                }
            }
            BinaryOp::Div | BinaryOp::DivAssign => {
                if ty.is_int() {
                    return Ok((ctx.builder.ins().sdiv_imm(left, right.as_int().ok_or(anyhow!("非整数"))?), ty));
                } else if ty.is_uint() {
                    return Ok((ctx.builder.ins().udiv_imm(left, right.as_int().ok_or(anyhow!("非整数"))?), ty));
                }
            }
            BinaryOp::Shl | BinaryOp::ShlAssign => {
                if ty.is_int() || ty.is_uint() {
                    return Ok((ctx.builder.ins().ishl_imm(left, right.as_int().ok_or(anyhow!("非整数"))?), ty));
                }
            }
            BinaryOp::Shr | BinaryOp::ShrAssign => {
                if ty.is_int() {
                    return Ok((ctx.builder.ins().sshr_imm(left, right.as_int().ok_or(anyhow!("非整数"))?), ty));
                } else if ty.is_uint() {
                    return Ok((ctx.builder.ins().ushr_imm(left, right.as_int().ok_or(anyhow!("非整数"))?), ty));
                }
            }
            BinaryOp::BitAnd | BinaryOp::BitAndAssign => {
                return Ok((ctx.builder.ins().band_imm(left, right.as_int().ok_or(anyhow!("非整数"))?), ty));
            }
            BinaryOp::BitOr | BinaryOp::BitOrAssign => {
                return Ok((ctx.builder.ins().bor_imm(left, right.as_int().ok_or(anyhow!("非整数"))?), ty));
            }
            BinaryOp::BitXor | BinaryOp::BitXorAssign => {
                return Ok((ctx.builder.ins().bxor_imm(left, right.as_int().ok_or(anyhow!("非整数"))?), ty));
            }
            BinaryOp::Eq => {
                if ty.is_int() | ty.is_uint() {
                    return Ok((ctx.builder.ins().icmp_imm(IntCC::Equal, left, right.as_int().unwrap()), Type::Bool));
                }
            }
            BinaryOp::Ne => {
                if ty.is_int() | ty.is_uint() {
                    return Ok((ctx.builder.ins().icmp_imm(IntCC::NotEqual, left, right.as_int().unwrap()), Type::Bool));
                }
            }
            BinaryOp::Le => {
                if ty.is_int() {
                    return Ok((ctx.builder.ins().icmp_imm(IntCC::SignedLessThanOrEqual, left, right.as_int().unwrap()), Type::Bool));
                } else if ty.is_uint() {
                    return Ok((ctx.builder.ins().icmp_imm(IntCC::UnsignedLessThanOrEqual, left, right.as_int().unwrap()), Type::Bool));
                }
            }
            BinaryOp::Lt => {
                if ty.is_int() {
                    return Ok((ctx.builder.ins().icmp_imm(IntCC::SignedLessThan, left, right.as_int().unwrap()), Type::Bool));
                } else if ty.is_uint() {
                    return Ok((ctx.builder.ins().icmp_imm(IntCC::UnsignedLessThan, left, right.as_int().unwrap()), Type::Bool));
                }
            }
            BinaryOp::Ge => {
                if ty.is_int() {
                    return Ok((ctx.builder.ins().icmp_imm(IntCC::SignedGreaterThanOrEqual, left, right.as_int().unwrap()), Type::Bool));
                } else if ty.is_uint() {
                    return Ok((ctx.builder.ins().icmp_imm(IntCC::UnsignedGreaterThanOrEqual, left, right.as_int().unwrap()), Type::Bool));
                }
            }
            BinaryOp::Gt => {
                if ty.is_int() {
                    return Ok((ctx.builder.ins().icmp_imm(IntCC::SignedGreaterThan, left, right.as_int().unwrap()), Type::Bool));
                } else if ty.is_uint() {
                    return Ok((ctx.builder.ins().icmp_imm(IntCC::UnsignedGreaterThan, left, right.as_int().unwrap()), Type::Bool));
                }
            }
            BinaryOp::Mod | BinaryOp::ModAssign => {
                if ty.is_int() {
                    return Ok((ctx.builder.ins().srem_imm(left, right.as_int().unwrap()), ty));
                } else if ty.is_uint() {
                    return Ok((ctx.builder.ins().urem_imm(left, right.as_int().unwrap()), ty));
                }
            }
            exp => {
                panic!("不支持的操作 {:?}", exp)
            }
        }
        panic!("未实现 {:?} {:?} {:?}", ty, op, right.get_type())
    }
}