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