1use super::{JITRunTime, context::BuildContext};
2use cranelift::prelude::*;
3use dynamic::{Dynamic, Type};
4use parser::{BinaryOp, Expr};
5
6use anyhow::{Result, anyhow};
7
8impl JITRunTime {
9 fn strcat(&mut self, ctx: &mut BuildContext, left: Value, right: Value) -> Result<Value> {
10 let fn_id = self.strcat_fn.ok_or_else(|| anyhow!("VM strcat runtime is not registered"))?;
11 let fn_ref = self.get_fn_ref(ctx, fn_id);
12 let call_inst = ctx.builder.ins().call(fn_ref, &[left, right]);
13 Ok(ctx.builder.inst_results(call_inst)[0])
14 }
15
16 fn strcat_i64(&mut self, ctx: &mut BuildContext, left: Value, right: Value) -> Result<Value> {
17 let fn_id = self.strcat_i64_fn.ok_or_else(|| anyhow!("VM strcat i64 runtime is not registered"))?;
18 let fn_ref = self.get_fn_ref(ctx, fn_id);
19 let call_inst = ctx.builder.ins().call(fn_ref, &[left, right]);
20 Ok(ctx.builder.inst_results(call_inst)[0])
21 }
22
23 fn strcat_assign(&mut self, ctx: &mut BuildContext, left: Value, right: Value) -> Result<Value> {
24 let fn_id = self.strcat_assign_fn.ok_or_else(|| anyhow!("VM strcat assign runtime is not registered"))?;
25 let fn_ref = self.get_fn_ref(ctx, fn_id);
26 let call_inst = ctx.builder.ins().call(fn_ref, &[left, right]);
27 Ok(ctx.builder.inst_results(call_inst)[0])
28 }
29
30 fn any_to_string(&mut self, ctx: &mut BuildContext, vt: (Value, Type)) -> Result<Value> {
31 let value = self.convert(ctx, vt, Type::Any)?;
32 self.call(ctx, self.get_method(&Type::Any, "to_string")?, vec![value]).map(|(v, _)| v)
33 }
34
35 fn any_logic(&mut self, ctx: &mut BuildContext, left: Value, op: BinaryOp, right: Value) -> Result<(Value, Type)> {
36 let op = ctx.builder.ins().iconst(types::I32, i32::from(op) as i64);
37 self.call(ctx, self.get_method(&Type::Any, "logic")?, vec![left, op, right])
38 }
39
40 fn any_binary(&mut self, ctx: &mut BuildContext, left: Value, op: BinaryOp, right: Value) -> Result<(Value, Type)> {
41 let op = ctx.builder.ins().iconst(types::I32, i32::from(op) as i64);
42 self.call(ctx, self.get_method(&Type::Any, "binary")?, vec![left, op, right])
43 }
44
45 fn struct_to_dynamic(&mut self, ctx: &mut BuildContext, base: Value, ty: &Type) -> Result<Value> {
46 let Type::Struct { params: _, fields: _ } = ty else {
47 return Err(anyhow!("不是结构体 {:?}", ty));
48 };
49 let ty_ptr = Self::type_ptr_const(ctx, ty);
50 let fn_id = self.struct_from_ptr_fn.ok_or_else(|| anyhow!("VM struct Dynamic runtime is not registered"))?;
51 let fn_ref = self.get_fn_ref(ctx, fn_id);
52 let call_inst = ctx.builder.ins().call(fn_ref, &[base, ty_ptr]);
53 Ok(ctx.builder.inst_results(call_inst)[0])
54 }
55
56 fn array_to_dynamic(&mut self, ctx: &mut BuildContext, base: Value, ty: &Type) -> Result<Value> {
57 let Type::Array(_, _) = ty else {
58 return Err(anyhow!("不是数组 {:?}", ty));
59 };
60 let ty_ptr = Self::type_ptr_const(ctx, ty);
61 let fn_id = self.array_from_ptr_fn.ok_or_else(|| anyhow!("VM array Dynamic runtime is not registered"))?;
62 let fn_ref = self.get_fn_ref(ctx, fn_id);
63 let call_inst = ctx.builder.ins().call(fn_ref, &[base, ty_ptr]);
64 Ok(ctx.builder.inst_results(call_inst)[0])
65 }
66
67 pub(crate) fn bool_value(&mut self, ctx: &mut BuildContext, vt: (Value, Type)) -> Result<Value> {
68 if vt.1.is_bool() {
69 Ok(vt.0)
70 } else if vt.1.is_any() {
71 self.call(ctx, self.get_method(&Type::Any, "to_bool")?, vec![vt.0]).map(|(v, _)| v)
72 } else if vt.1.is_int() || vt.1.is_uint() {
73 Ok(ctx.builder.ins().icmp_imm(IntCC::NotEqual, vt.0, 0))
74 } else if vt.1.is_f32() {
75 let zero = ctx.builder.ins().f32const(0.0);
76 Ok(ctx.builder.ins().fcmp(FloatCC::NotEqual, vt.0, zero))
77 } else if vt.1.is_f64() {
78 let zero = ctx.builder.ins().f64const(0.0);
79 Ok(ctx.builder.ins().fcmp(FloatCC::NotEqual, vt.0, zero))
80 } else {
81 Err(anyhow!("cannot convert {:?} to bool", vt.1))
82 }
83 }
84
85 pub fn convert(&mut self, ctx: &mut BuildContext, vt: (Value, Type), ty: Type) -> Result<Value> {
86 let vt = if matches!(vt.1, Type::Symbol { .. }) {
87 let resolved = self.compiler.symbols.get_type(&vt.1).unwrap_or_else(|_| vt.1.clone());
88 (vt.0, resolved)
89 } else {
90 vt
91 };
92 if vt.1 != ty {
93 if ty.is_any() {
94 if self.is_opaque_custom_ty(&vt.1) {
95 return Ok(vt.0);
96 } else if vt.1.is_array() {
97 return self.array_to_dynamic(ctx, vt.0, &vt.1);
98 } else if vt.1.is_struct() {
99 return self.struct_to_dynamic(ctx, vt.0, &vt.1);
100 } else if vt.1.is_bool() {
101 return self.call(ctx, self.get_method(&Type::Any, "from_bool")?, vec![vt.0]).map(|(v, _)| v);
102 } else if vt.1.is_uint() {
103 let v = if vt.1.width() < 8 { ctx.builder.ins().uextend(types::I64, vt.0) } else { vt.0 };
104 return self.call(ctx, self.get_method(&Type::Any, "from_i64")?, vec![v]).map(|(v, _)| v);
105 } else if vt.1.is_int() {
106 let v = if vt.1.width() < 8 { ctx.builder.ins().sextend(types::I64, vt.0) } else { vt.0 };
107 return self.call(ctx, self.get_method(&Type::Any, "from_i64")?, vec![v]).map(|(v, _)| v);
108 } else if vt.1.is_f32() {
109 let v = ctx.builder.ins().fpromote(types::F64, vt.0);
110 return self.call(ctx, self.get_method(&Type::Any, "from_f64")?, vec![v]).map(|(v, _)| v);
111 } else if vt.1.is_f64() {
112 return self.call(ctx, self.get_method(&Type::Any, "from_f64")?, vec![vt.0]).map(|(v, _)| v);
113 } else if vt.1.is_str() {
114 return Ok(vt.0);
115 } else if matches!(vt.1, Type::Map | Type::List(_) | Type::Iter) {
116 return Ok(vt.0);
117 } else if matches!(vt.1, Type::Symbol { .. }) {
118 return Ok(vt.0);
119 }
120 } else if vt.1.is_any() {
121 if ty.is_bool() {
122 return self.call(ctx, self.get_method(&Type::Any, "to_bool")?, vec![vt.0]).map(|(v, _)| v);
123 } else if ty.is_array() {
124 return self.any_to_array(ctx, vt.0, &ty);
125 } else if ty.is_str() {
126 return self.call(ctx, self.get_method(&Type::Any, "to_string")?, vec![vt.0]).map(|(v, _)| v);
127 } else if ty.is_int() | ty.is_uint() {
128 let (v, _) = self.call(ctx, self.get_method(&Type::Any, "to_i64")?, vec![vt.0])?;
129 return Ok(match ty.width() {
130 1 => ctx.builder.ins().ireduce(types::I8, v),
131 2 => ctx.builder.ins().ireduce(types::I16, v),
132 4 => ctx.builder.ins().ireduce(types::I32, v),
133 _ => v,
134 });
135 } else if ty.is_f32() {
136 let v = self.call(ctx, self.get_method(&Type::Any, "to_f64")?, vec![vt.0]).map(|(v, _)| v)?;
137 return Ok(ctx.builder.ins().fdemote(types::F32, v));
138 } else if ty.is_f64() {
139 return self.call(ctx, self.get_method(&Type::Any, "to_f64")?, vec![vt.0]).map(|(v, _)| v);
140 } else {
141 return Ok(vt.0);
142 }
143 } else if ty.is_str() {
144 return self.any_to_string(ctx, vt);
145 } else if ty.is_int() || ty.is_uint() {
146 if vt.1.is_f32() || vt.1.is_f64() {
147 let target = crate::get_type(&ty)?;
148 if ty.is_uint() {
149 return Ok(ctx.builder.ins().fcvt_to_uint(target, vt.0));
150 } else if ty.is_int() {
151 return Ok(ctx.builder.ins().fcvt_to_sint(target, vt.0));
152 }
153 }
154 if vt.1.is_int() || vt.1.is_uint() || vt.1.is_bool() {
155 let target = crate::get_type(&ty)?;
156 let actual = ctx.builder.func.dfg.value_type(vt.0);
157 if actual == target {
158 return Ok(vt.0);
159 }
160 if actual.is_int() && target.is_int() {
161 if actual.bits() > target.bits() {
162 return Ok(ctx.builder.ins().ireduce(target, vt.0));
163 }
164 if actual.bits() < target.bits() {
165 return if vt.1.is_int() { Ok(ctx.builder.ins().sextend(target, vt.0)) } else { Ok(ctx.builder.ins().uextend(target, vt.0)) };
166 }
167 }
168 }
169 if vt.1.is_str() {
170 let (v, _) = self.call(ctx, self.get_method(&Type::Any, "to_i64")?, vec![vt.0])?;
171 return Ok(match ty.width() {
172 1 => ctx.builder.ins().ireduce(types::I8, v),
173 2 => ctx.builder.ins().ireduce(types::I16, v),
174 4 => ctx.builder.ins().ireduce(types::I32, v),
175 _ => v,
176 });
177 }
178 } else if ty.is_f32() {
179 if vt.1.is_int() {
180 return Ok(ctx.builder.ins().fcvt_from_sint(types::F32, vt.0));
181 } else if vt.1.is_uint() {
182 return Ok(ctx.builder.ins().fcvt_from_uint(types::F32, vt.0));
183 } else if vt.1.is_f64() {
184 return Ok(ctx.builder.ins().fdemote(types::F32, vt.0));
185 }
186 } else if ty.is_f64() {
187 if vt.1.is_int() {
188 return Ok(ctx.builder.ins().fcvt_from_sint(types::F64, vt.0));
189 } else if vt.1.is_uint() {
190 return Ok(ctx.builder.ins().fcvt_from_uint(types::F64, vt.0));
191 } else if vt.1.is_f32() {
192 return Ok(ctx.builder.ins().fpromote(types::F64, vt.0));
193 }
194 } else if let Type::Symbol { id: _, params: _ } = ty {
195 log::info!("convert {:?} -> {:?}", vt, ty);
196 return Ok(vt.0); }
198 if vt.1.is_bool() {
199 let v = ctx.builder.ins().sextend(types::I64, vt.0);
200 return self.call(ctx, self.get_method(&Type::Any, "from_i64")?, vec![v]).map(|(v, _)| v);
201 }
202 log::error!("未实现 {:?} {:?}", vt, ty); Ok(vt.0)
204 } else {
205 Ok(vt.0)
206 }
207 }
208
209 pub(crate) fn binary_with_expected(&mut self, ctx: &mut BuildContext, left: (Value, Type), op: BinaryOp, right: &Expr, expected: Option<&Type>) -> Result<(Value, Type)> {
210 if matches!(op, BinaryOp::And | BinaryOp::Or) {
212 return self.short_circuit_logic(ctx, left, op, right);
213 }
214 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()) };
215 let right = if right.is_value() {
216 let right = right.clone().value()?;
217 if right.is_f32() {
218 (ctx.builder.ins().f32const(right.as_float().unwrap() as f32), Type::F32)
219 } else if right.is_f64() {
220 (ctx.builder.ins().f64const(right.as_float().unwrap() as f64), Type::F64)
221 } else if left.1.is_any() {
222 if right.is_int() {
223 (ctx.builder.ins().iconst(types::I64, right.as_int().unwrap()), Type::I64)
224 } else if right.is_null() {
225 self.call(ctx, self.get_method(&Type::Any, "null")?, vec![])?
226 } else {
227 ctx.get_const(&right)?
228 }
229 } else {
230 return self.binary_imm(ctx, left, op, right);
231 }
232 } else {
233 self.eval(ctx, right)?.get(ctx).ok_or_else(|| anyhow!("没有返回值: {:?}", right))?
234 };
235 let right_ty = right_ty_hint.as_ref().unwrap_or(&right.1);
236 let numeric_expected = expected.filter(|ty| (ty.is_int() || ty.is_uint() || ty.is_float()) && (left.1.is_any() || right.1.is_any()));
237 let ty = if (op.is_add() || op.is_logic()) && (left.1.is_str() || right.1.is_str() || right_ty.is_str()) {
238 Type::Str
239 } else if !op.is_logic()
240 && let Some(expected) = numeric_expected
241 {
242 expected.clone()
243 } else if (op.is_add() || op.is_logic()) && (left.1.is_any() || right.1.is_any()) {
244 Type::Any
245 } else {
246 left.1.clone() + right.1.clone()
247 }; if ty.is_str() && op.is_add() {
249 if op == BinaryOp::AddAssign {
250 let left = self.convert(ctx, left, Type::Any)?;
251 let right = self.convert(ctx, right, Type::Any)?;
252 return Ok((self.strcat_assign(ctx, left, right)?, ty));
253 }
254 if left.1.is_str() && right.1.is_str() {
255 return Ok((self.strcat(ctx, left.0, right.0)?, Type::Str));
256 }
257 if left.1.is_str() && right.1.is_int() {
258 let right = self.convert(ctx, right, Type::I64)?;
259 return Ok((self.strcat_i64(ctx, left.0, right)?, Type::Str));
260 }
261 let left = self.convert(ctx, left, Type::Any)?;
262 let right = self.convert(ctx, right, Type::Any)?;
263 let result = self.any_binary(ctx, left, op, right)?.0;
264 return Ok((result, ty));
265 }
266 let left = self.convert(ctx, left, ty.clone())?;
267 let right = self.convert(ctx, right, ty.clone())?;
268 if ty.is_any() {
269 if op.is_logic() {
270 return self.any_logic(ctx, left, op, right);
271 } else {
272 return self.any_binary(ctx, left, op, right);
273 }
274 }
275 if ty.is_str() && op.is_logic() {
276 return self.any_logic(ctx, left, op, right);
277 }
278 match op {
279 BinaryOp::Add | BinaryOp::AddAssign => {
280 if ty.is_int() || ty.is_uint() {
281 return Ok((ctx.builder.ins().iadd(left, right), ty));
282 } else if ty.is_float() {
283 return Ok((ctx.builder.ins().fadd(left, right), ty));
284 } else if ty.is_str() {
285 let result = self.any_binary(ctx, left, op, right)?.0;
286 return Ok((result, ty));
287 }
288 }
289 BinaryOp::Sub | BinaryOp::SubAssign => {
290 if ty.is_int() || ty.is_uint() {
291 return Ok((ctx.builder.ins().isub(left, right), ty));
292 } else if ty.is_float() {
293 return Ok((ctx.builder.ins().fsub(left, right), ty));
294 }
295 }
296 BinaryOp::Mul | BinaryOp::MulAssign => {
297 if ty.is_int() || ty.is_uint() {
298 return Ok((ctx.builder.ins().imul(left, right), ty));
299 } else if ty.is_float() {
300 return Ok((ctx.builder.ins().fmul(left, right), ty));
301 }
302 }
303 BinaryOp::Div | BinaryOp::DivAssign => {
304 if ty.is_int() {
305 return Ok((ctx.builder.ins().sdiv(left, right), ty));
306 } else if ty.is_uint() {
307 return Ok((ctx.builder.ins().udiv(left, right), ty));
308 } else if ty.is_float() {
309 return Ok((ctx.builder.ins().fdiv(left, right), ty));
310 }
311 }
312 BinaryOp::Mod | BinaryOp::ModAssign => {
313 if ty.is_int() {
314 return Ok((ctx.builder.ins().srem(left, right), ty));
315 } else if ty.is_uint() {
316 return Ok((ctx.builder.ins().urem(left, right), ty));
317 }
318 }
319 BinaryOp::Shl | BinaryOp::ShlAssign => {
320 if ty.is_int() || ty.is_uint() {
321 return Ok((ctx.builder.ins().ishl(left, right), ty));
322 }
323 }
324 BinaryOp::Shr | BinaryOp::ShrAssign => {
325 if ty.is_int() {
326 return Ok((ctx.builder.ins().sshr(left, right), ty));
327 } else if ty.is_uint() {
328 return Ok((ctx.builder.ins().ushr(left, right), ty));
329 }
330 }
331 BinaryOp::BitAnd | BinaryOp::BitAndAssign => {
332 return Ok((ctx.builder.ins().band(left, right), ty));
333 }
334 BinaryOp::BitOr | BinaryOp::BitOrAssign => {
335 return Ok((ctx.builder.ins().bor(left, right), ty));
336 }
337 BinaryOp::BitXor | BinaryOp::BitXorAssign => {
338 return Ok((ctx.builder.ins().bxor(left, right), ty));
339 }
340 BinaryOp::Eq => {
341 if ty.is_int() | ty.is_uint() || ty.is_bool() {
342 return Ok((ctx.builder.ins().icmp(IntCC::Equal, left, right), Type::Bool));
343 } else if ty.is_float() {
344 return Ok((ctx.builder.ins().fcmp(FloatCC::Equal, left, right), Type::Bool));
345 }
346 }
347 BinaryOp::Ne => {
348 if ty.is_int() | ty.is_uint() || ty.is_bool() {
349 return Ok((ctx.builder.ins().icmp(IntCC::NotEqual, left, right), Type::Bool));
350 } else if ty.is_float() {
351 return Ok((ctx.builder.ins().fcmp(FloatCC::NotEqual, left, right), Type::Bool));
352 }
353 }
354 BinaryOp::Lt => {
355 if ty.is_int() {
356 return Ok((ctx.builder.ins().icmp(IntCC::SignedLessThan, left, right), Type::Bool));
357 } else if ty.is_uint() {
358 return Ok((ctx.builder.ins().icmp(IntCC::UnsignedLessThan, left, right), Type::Bool));
359 } else if ty.is_float() {
360 return Ok((ctx.builder.ins().fcmp(FloatCC::LessThan, left, right), Type::Bool));
361 }
362 }
363 BinaryOp::Le => {
364 if ty.is_int() {
365 return Ok((ctx.builder.ins().icmp(IntCC::SignedLessThanOrEqual, left, right), Type::Bool));
366 } else if ty.is_uint() {
367 return Ok((ctx.builder.ins().icmp(IntCC::UnsignedLessThanOrEqual, left, right), Type::Bool));
368 } else if ty.is_float() {
369 return Ok((ctx.builder.ins().fcmp(FloatCC::LessThanOrEqual, left, right), Type::Bool));
370 }
371 }
372 BinaryOp::Gt => {
373 if ty.is_int() {
374 return Ok((ctx.builder.ins().icmp(IntCC::SignedGreaterThan, left, right), Type::Bool));
375 } else if ty.is_uint() {
376 return Ok((ctx.builder.ins().icmp(IntCC::UnsignedGreaterThan, left, right), Type::Bool));
377 } else if ty.is_float() {
378 return Ok((ctx.builder.ins().fcmp(FloatCC::GreaterThan, left, right), Type::Bool));
379 }
380 }
381 BinaryOp::Ge => {
382 if ty.is_int() {
383 return Ok((ctx.builder.ins().icmp(IntCC::SignedGreaterThanOrEqual, left, right), Type::Bool));
384 } else if ty.is_uint() {
385 return Ok((ctx.builder.ins().icmp(IntCC::UnsignedGreaterThanOrEqual, left, right), Type::Bool));
386 } else if ty.is_float() {
387 return Ok((ctx.builder.ins().fcmp(FloatCC::GreaterThanOrEqual, left, right), Type::Bool));
388 }
389 }
390 _ => {}
391 }
392 panic!("未实现 {:?} {:?} {:?} {:?}", left, op, right, ty)
393 }
394
395 pub(crate) fn binary_imm<'a>(&mut self, ctx: &'a mut BuildContext, left: (Value, Type), op: BinaryOp, right: Dynamic) -> Result<(Value, Type)> {
396 let ty = left.1.clone() + right.get_type();
397 let bool_imm = || right.as_bool().map(|value| if value { 1 } else { 0 });
398 if ty.is_str() && op.is_add() {
399 if op == BinaryOp::AddAssign {
400 let left = self.convert(ctx, left, Type::Any)?;
401 let right_vt = ctx.get_const(&right).or_else(|_| {
402 let idx = self.compiler.get_const(right.clone());
403 self.get_const_value(ctx, idx)
404 })?;
405 let right = self.convert(ctx, right_vt, Type::Any)?;
406 return Ok((self.strcat_assign(ctx, left, right)?, ty));
407 }
408 if left.1.is_str() && right.is_str() {
409 let right_vt = ctx.get_const(&right).or_else(|_| {
410 let idx = self.compiler.get_const(right.clone());
411 self.get_const_value(ctx, idx)
412 })?;
413 let right = self.convert(ctx, right_vt, Type::Str)?;
414 return Ok((self.strcat(ctx, left.0, right)?, Type::Str));
415 }
416 if left.1.is_str() && right.is_int() {
417 let right = ctx.get_const(&right)?;
418 let right = self.convert(ctx, right, Type::I64)?;
419 return Ok((self.strcat_i64(ctx, left.0, right)?, Type::Str));
420 }
421 let left = self.convert(ctx, left, Type::Any)?;
422 let right_vt = ctx.get_const(&right).or_else(|_| {
423 let idx = self.compiler.get_const(right.clone());
424 self.get_const_value(ctx, idx)
425 })?;
426 let right = self.convert(ctx, right_vt, Type::Any)?;
427 let result = self.any_binary(ctx, left, op, right)?.0;
428 return Ok((result, ty));
429 }
430 let left = self.convert(ctx, left, ty.clone())?;
431 if ty.is_str() && op.is_logic() {
432 let right_vt = ctx.get_const(&right).or_else(|_| {
433 let idx = self.compiler.get_const(right.clone());
434 self.get_const_value(ctx, idx)
435 })?;
436 let right = self.convert(ctx, right_vt, Type::Str)?;
437 return self.any_logic(ctx, left, op, right);
438 }
439 let right_float = if ty.is_float() {
440 let right = right.as_float().ok_or(anyhow!("非数字"))?;
441 Some(if ty.is_f32() { ctx.builder.ins().f32const(right as f32) } else { ctx.builder.ins().f64const(right) })
442 } else {
443 None
444 };
445 match op {
446 BinaryOp::Add | BinaryOp::AddAssign => {
447 if ty.is_str() {
448 let right_vt = ctx.get_const(&right).or_else(|_| {
449 let idx = self.compiler.get_const(right.clone());
450 self.get_const_value(ctx, idx)
451 })?;
452 let right = self.convert(ctx, right_vt, Type::Str)?;
453 let result = self.any_binary(ctx, left, op, right)?.0;
454 return Ok((result, ty));
455 }
456 if ty.is_int() | ty.is_uint() {
457 return Ok((ctx.builder.ins().iadd_imm(left, right.as_int().ok_or(anyhow!("非整数"))?), ty));
458 } else if ty.is_float() {
459 return Ok((ctx.builder.ins().fadd(left, right_float.unwrap()), ty));
460 }
461 }
462 BinaryOp::Sub | BinaryOp::SubAssign => {
463 if ty.is_int() | ty.is_uint() {
464 return Ok((ctx.builder.ins().iadd_imm(left, -right.as_int().ok_or(anyhow!("非整数"))?), ty));
465 } else if ty.is_float() {
466 return Ok((ctx.builder.ins().fsub(left, right_float.unwrap()), ty));
467 }
468 }
469 BinaryOp::Mul | BinaryOp::MulAssign => {
470 if ty.is_int() | ty.is_uint() {
471 return Ok((ctx.builder.ins().imul_imm(left, right.as_int().ok_or(anyhow!("非整数"))?), ty));
472 } else if ty.is_float() {
473 return Ok((ctx.builder.ins().fmul(left, right_float.unwrap()), ty));
474 }
475 }
476 BinaryOp::Div | BinaryOp::DivAssign => {
477 if ty.is_int() {
478 return Ok((ctx.builder.ins().sdiv_imm(left, right.as_int().ok_or(anyhow!("非整数"))?), ty));
479 } else if ty.is_uint() {
480 return Ok((ctx.builder.ins().udiv_imm(left, right.as_int().ok_or(anyhow!("非整数"))?), ty));
481 } else if ty.is_float() {
482 return Ok((ctx.builder.ins().fdiv(left, right_float.unwrap()), ty));
483 }
484 }
485 BinaryOp::Shl | BinaryOp::ShlAssign => {
486 if ty.is_int() || ty.is_uint() {
487 return Ok((ctx.builder.ins().ishl_imm(left, right.as_int().ok_or(anyhow!("非整数"))?), ty));
488 }
489 }
490 BinaryOp::Shr | BinaryOp::ShrAssign => {
491 if ty.is_int() {
492 return Ok((ctx.builder.ins().sshr_imm(left, right.as_int().ok_or(anyhow!("非整数"))?), ty));
493 } else if ty.is_uint() {
494 return Ok((ctx.builder.ins().ushr_imm(left, right.as_int().ok_or(anyhow!("非整数"))?), ty));
495 }
496 }
497 BinaryOp::BitAnd | BinaryOp::BitAndAssign => {
498 return Ok((ctx.builder.ins().band_imm(left, right.as_int().ok_or(anyhow!("非整数"))?), ty));
499 }
500 BinaryOp::BitOr | BinaryOp::BitOrAssign => {
501 return Ok((ctx.builder.ins().bor_imm(left, right.as_int().ok_or(anyhow!("非整数"))?), ty));
502 }
503 BinaryOp::BitXor | BinaryOp::BitXorAssign => {
504 return Ok((ctx.builder.ins().bxor_imm(left, right.as_int().ok_or(anyhow!("非整数"))?), ty));
505 }
506 BinaryOp::Eq => {
507 if ty.is_int() | ty.is_uint() {
508 return Ok((ctx.builder.ins().icmp_imm(IntCC::Equal, left, right.as_int().unwrap()), Type::Bool));
509 } else if ty.is_bool() {
510 return Ok((ctx.builder.ins().icmp_imm(IntCC::Equal, left, bool_imm().unwrap()), Type::Bool));
511 } else if ty.is_float() {
512 return Ok((ctx.builder.ins().fcmp(FloatCC::Equal, left, right_float.unwrap()), Type::Bool));
513 }
514 }
515 BinaryOp::Ne => {
516 if ty.is_int() | ty.is_uint() {
517 return Ok((ctx.builder.ins().icmp_imm(IntCC::NotEqual, left, right.as_int().unwrap()), Type::Bool));
518 } else if ty.is_bool() {
519 return Ok((ctx.builder.ins().icmp_imm(IntCC::NotEqual, left, bool_imm().unwrap()), Type::Bool));
520 } else if ty.is_float() {
521 return Ok((ctx.builder.ins().fcmp(FloatCC::NotEqual, left, right_float.unwrap()), Type::Bool));
522 }
523 }
524 BinaryOp::Le => {
525 if ty.is_int() {
526 return Ok((ctx.builder.ins().icmp_imm(IntCC::SignedLessThanOrEqual, left, right.as_int().unwrap()), Type::Bool));
527 } else if ty.is_uint() {
528 return Ok((ctx.builder.ins().icmp_imm(IntCC::UnsignedLessThanOrEqual, left, right.as_int().unwrap()), Type::Bool));
529 } else if ty.is_float() {
530 return Ok((ctx.builder.ins().fcmp(FloatCC::LessThanOrEqual, left, right_float.unwrap()), Type::Bool));
531 }
532 }
533 BinaryOp::Lt => {
534 if ty.is_int() {
535 return Ok((ctx.builder.ins().icmp_imm(IntCC::SignedLessThan, left, right.as_int().unwrap()), Type::Bool));
536 } else if ty.is_uint() {
537 return Ok((ctx.builder.ins().icmp_imm(IntCC::UnsignedLessThan, left, right.as_int().unwrap()), Type::Bool));
538 } else if ty.is_float() {
539 return Ok((ctx.builder.ins().fcmp(FloatCC::LessThan, left, right_float.unwrap()), Type::Bool));
540 }
541 }
542 BinaryOp::Ge => {
543 if ty.is_int() {
544 return Ok((ctx.builder.ins().icmp_imm(IntCC::SignedGreaterThanOrEqual, left, right.as_int().unwrap()), Type::Bool));
545 } else if ty.is_uint() {
546 return Ok((ctx.builder.ins().icmp_imm(IntCC::UnsignedGreaterThanOrEqual, left, right.as_int().unwrap()), Type::Bool));
547 } else if ty.is_float() {
548 return Ok((ctx.builder.ins().fcmp(FloatCC::GreaterThanOrEqual, left, right_float.unwrap()), Type::Bool));
549 }
550 }
551 BinaryOp::Gt => {
552 if ty.is_int() {
553 return Ok((ctx.builder.ins().icmp_imm(IntCC::SignedGreaterThan, left, right.as_int().unwrap()), Type::Bool));
554 } else if ty.is_uint() {
555 return Ok((ctx.builder.ins().icmp_imm(IntCC::UnsignedGreaterThan, left, right.as_int().unwrap()), Type::Bool));
556 } else if ty.is_float() {
557 return Ok((ctx.builder.ins().fcmp(FloatCC::GreaterThan, left, right_float.unwrap()), Type::Bool));
558 }
559 }
560 BinaryOp::Mod | BinaryOp::ModAssign => {
561 if ty.is_int() {
562 return Ok((ctx.builder.ins().srem_imm(left, right.as_int().unwrap()), ty));
563 } else if ty.is_uint() {
564 return Ok((ctx.builder.ins().urem_imm(left, right.as_int().unwrap()), ty));
565 }
566 }
567 exp => {
568 panic!("不支持的操作 {:?}", exp)
569 }
570 }
571 panic!("未实现 {:?} {:?} {:?}", ty, op, right.get_type())
572 }
573}