1#[cfg(feature = "jit")]
12pub mod jit {
13 use cranelift_codegen::ir::condcodes::IntCC;
14 use cranelift_codegen::ir::{types, AbiParam, InstBuilder, UserFuncName};
15 use cranelift_codegen::settings::{self, Configurable};
16 use cranelift_codegen::Context;
17 use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext, Variable};
18 use cranelift_jit::{JITBuilder, JITModule};
19 use cranelift_module::{FuncId, Linkage, Module};
20
21 use std::collections::HashMap;
22 use std::mem;
23
24 use crate::ast::{
25 self, BinOp, Expr, ExternBlock, ExternFunction, ExternItem, Item, Literal, PipeOp,
26 TypeExpr, UnaryOp,
27 };
28 use crate::ffi::ctypes::CType;
29 use crate::optimize::{OptLevel, Optimizer};
30 use crate::parser::Parser;
31
32 #[repr(C)]
44 #[derive(Clone, Copy, Debug)]
45 pub struct SigilValue(pub u64);
46
47 impl SigilValue {
48 pub const TAG_INT: u64 = 0;
50 pub const TAG_FLOAT: u64 = 1;
51 pub const TAG_BOOL: u64 = 2;
52 pub const TAG_NULL: u64 = 3;
53 pub const TAG_PTR: u64 = 4; #[inline]
56 pub fn from_int(v: i64) -> Self {
57 SigilValue(v as u64)
58 }
59
60 #[inline]
61 pub fn from_float(v: f64) -> Self {
62 SigilValue(v.to_bits())
63 }
64
65 #[inline]
66 pub fn from_bool(v: bool) -> Self {
67 SigilValue(if v { 1 } else { 0 })
68 }
69
70 #[inline]
71 pub fn as_int(self) -> i64 {
72 self.0 as i64
73 }
74
75 #[inline]
76 pub fn as_float(self) -> f64 {
77 f64::from_bits(self.0)
78 }
79
80 #[inline]
81 pub fn as_bool(self) -> bool {
82 self.0 != 0
83 }
84 }
85
86 type CompiledFn = unsafe extern "C" fn() -> i64;
88 #[allow(dead_code)]
89 type CompiledFnWithArgs = unsafe extern "C" fn(i64) -> i64;
90
91 #[derive(Clone, Debug)]
93 pub struct ExternFnSig {
94 pub name: String,
95 pub params: Vec<types::Type>,
96 pub returns: Option<types::Type>,
97 pub variadic: bool,
98 pub func_id: FuncId,
99 }
100
101 pub struct JitCompiler {
103 module: JITModule,
105 builder_ctx: FunctionBuilderContext,
107 ctx: Context,
109 functions: HashMap<String, FuncId>,
111 extern_functions: HashMap<String, ExternFnSig>,
113 #[allow(dead_code)]
115 var_counter: usize,
116 #[allow(dead_code)]
118 builtins: HashMap<String, *const u8>,
119 }
120
121 impl JitCompiler {
122 pub fn new() -> Result<Self, String> {
124 let mut flag_builder = settings::builder();
125 flag_builder.set("use_colocated_libcalls", "false").unwrap();
127 flag_builder.set("is_pic", "false").unwrap();
128 flag_builder.set("opt_level", "speed").unwrap();
130 flag_builder.set("enable_verifier", "false").unwrap(); flag_builder.set("enable_alias_analysis", "true").unwrap();
133
134 let isa_builder = cranelift_native::builder().map_err(|e| e.to_string())?;
136 let isa = isa_builder
137 .finish(settings::Flags::new(flag_builder))
138 .map_err(|e| e.to_string())?;
139
140 let mut builder = JITBuilder::with_isa(isa, cranelift_module::default_libcall_names());
141
142 let builtins = Self::register_builtins(&mut builder);
144
145 let module = JITModule::new(builder);
146
147 Ok(Self {
148 module,
149 builder_ctx: FunctionBuilderContext::new(),
150 ctx: Context::new(),
151 functions: HashMap::new(),
152 extern_functions: HashMap::new(),
153 var_counter: 0,
154 builtins,
155 })
156 }
157
158 fn register_builtins(builder: &mut JITBuilder) -> HashMap<String, *const u8> {
160 let mut builtins = HashMap::new();
161
162 builder.symbol("sigil_sqrt", sigil_sqrt as *const u8);
164 builder.symbol("sigil_sin", sigil_sin as *const u8);
165 builder.symbol("sigil_cos", sigil_cos as *const u8);
166 builder.symbol("sigil_pow", sigil_pow as *const u8);
167 builder.symbol("sigil_exp", sigil_exp as *const u8);
168 builder.symbol("sigil_ln", sigil_ln as *const u8);
169 builder.symbol("sigil_floor", sigil_floor as *const u8);
170 builder.symbol("sigil_ceil", sigil_ceil as *const u8);
171 builder.symbol("sigil_abs", sigil_abs as *const u8);
172
173 builder.symbol("sigil_print", sigil_print as *const u8);
175 builder.symbol("sigil_print_int", sigil_print_int as *const u8);
176 builder.symbol("sigil_print_float", sigil_print_float as *const u8);
177 builder.symbol("sigil_print_str", sigil_print_str as *const u8);
178
179 builder.symbol("sigil_now", sigil_now as *const u8);
181
182 builder.symbol("sigil_add", sigil_add as *const u8);
184 builder.symbol("sigil_sub", sigil_sub as *const u8);
185 builder.symbol("sigil_mul", sigil_mul as *const u8);
186 builder.symbol("sigil_div", sigil_div as *const u8);
187 builder.symbol("sigil_lt", sigil_lt as *const u8);
188 builder.symbol("sigil_le", sigil_le as *const u8);
189 builder.symbol("sigil_gt", sigil_gt as *const u8);
190 builder.symbol("sigil_ge", sigil_ge as *const u8);
191
192 builder.symbol("sigil_simd_new", sigil_simd_new as *const u8);
194 builder.symbol("sigil_simd_splat", sigil_simd_splat as *const u8);
195 builder.symbol("sigil_simd_add", sigil_simd_add as *const u8);
196 builder.symbol("sigil_simd_sub", sigil_simd_sub as *const u8);
197 builder.symbol("sigil_simd_mul", sigil_simd_mul as *const u8);
198 builder.symbol("sigil_simd_div", sigil_simd_div as *const u8);
199 builder.symbol("sigil_simd_dot", sigil_simd_dot as *const u8);
200 builder.symbol("sigil_simd_hadd", sigil_simd_hadd as *const u8);
201 builder.symbol("sigil_simd_length_sq", sigil_simd_length_sq as *const u8);
202 builder.symbol("sigil_simd_length", sigil_simd_length as *const u8);
203 builder.symbol("sigil_simd_normalize", sigil_simd_normalize as *const u8);
204 builder.symbol("sigil_simd_cross", sigil_simd_cross as *const u8);
205 builder.symbol("sigil_simd_min", sigil_simd_min as *const u8);
206 builder.symbol("sigil_simd_max", sigil_simd_max as *const u8);
207 builder.symbol("sigil_simd_extract", sigil_simd_extract as *const u8);
208 builder.symbol("sigil_simd_free", sigil_simd_free as *const u8);
209
210 builder.symbol("sigil_array_new", sigil_array_new as *const u8);
212 builder.symbol("sigil_array_push", sigil_array_push as *const u8);
213 builder.symbol("sigil_array_get", sigil_array_get as *const u8);
214 builder.symbol("sigil_array_set", sigil_array_set as *const u8);
215 builder.symbol("sigil_array_len", sigil_array_len as *const u8);
216
217 builder.symbol("sigil_array_sum", sigil_array_sum as *const u8);
219 builder.symbol("sigil_array_scale", sigil_array_scale as *const u8);
220 builder.symbol("sigil_array_offset", sigil_array_offset as *const u8);
221 builder.symbol("sigil_array_dot", sigil_array_dot as *const u8);
222 builder.symbol("sigil_array_add", sigil_array_add as *const u8);
223 builder.symbol("sigil_array_mul", sigil_array_mul as *const u8);
224 builder.symbol("sigil_array_min", sigil_array_min as *const u8);
225 builder.symbol("sigil_array_max", sigil_array_max as *const u8);
226 builder.symbol("sigil_array_fill", sigil_array_fill as *const u8);
227
228 builder.symbol("sigil_array_first", sigil_array_first as *const u8);
230 builder.symbol("sigil_array_last", sigil_array_last as *const u8);
231 builder.symbol("sigil_array_middle", sigil_array_middle as *const u8);
232 builder.symbol("sigil_array_choice", sigil_array_choice as *const u8);
233 builder.symbol("sigil_array_nth", sigil_array_nth as *const u8);
234 builder.symbol("sigil_array_next", sigil_array_next as *const u8);
235 builder.symbol("sigil_array_product", sigil_array_product as *const u8);
236 builder.symbol("sigil_array_sort", sigil_array_sort as *const u8);
237
238 builder.symbol("sigil_parallel_map", sigil_parallel_map as *const u8);
240 builder.symbol("sigil_parallel_filter", sigil_parallel_filter as *const u8);
241 builder.symbol("sigil_parallel_reduce", sigil_parallel_reduce as *const u8);
242
243 builder.symbol("sigil_gpu_map", sigil_gpu_map as *const u8);
245 builder.symbol("sigil_gpu_filter", sigil_gpu_filter as *const u8);
246 builder.symbol("sigil_gpu_reduce", sigil_gpu_reduce as *const u8);
247
248 builder.symbol("sigil_memo_new", sigil_memo_new as *const u8);
250 builder.symbol("sigil_memo_get_1", sigil_memo_get_1 as *const u8);
251 builder.symbol("sigil_memo_set_1", sigil_memo_set_1 as *const u8);
252 builder.symbol("sigil_memo_get_2", sigil_memo_get_2 as *const u8);
253 builder.symbol("sigil_memo_set_2", sigil_memo_set_2 as *const u8);
254 builder.symbol("sigil_memo_free", sigil_memo_free as *const u8);
255
256 builder.symbol("sigil_ackermann", sigil_ackermann as *const u8);
258 builder.symbol("sigil_tak", sigil_tak as *const u8);
259
260 use crate::ffi::helpers::*;
262 builder.symbol(
263 "sigil_string_to_cstring",
264 sigil_string_to_cstring as *const u8,
265 );
266 builder.symbol("sigil_cstring_free", sigil_cstring_free as *const u8);
267 builder.symbol("sigil_cstring_len", sigil_cstring_len as *const u8);
268 builder.symbol("sigil_cstring_copy", sigil_cstring_copy as *const u8);
269 builder.symbol("sigil_ptr_from_int", sigil_ptr_from_int as *const u8);
270 builder.symbol("sigil_ptr_to_int", sigil_ptr_to_int as *const u8);
271 builder.symbol("sigil_ptr_read_u8", sigil_ptr_read_u8 as *const u8);
272 builder.symbol("sigil_ptr_write_u8", sigil_ptr_write_u8 as *const u8);
273 builder.symbol("sigil_ptr_read_i32", sigil_ptr_read_i32 as *const u8);
274 builder.symbol("sigil_ptr_write_i32", sigil_ptr_write_i32 as *const u8);
275 builder.symbol("sigil_ptr_read_i64", sigil_ptr_read_i64 as *const u8);
276 builder.symbol("sigil_ptr_write_i64", sigil_ptr_write_i64 as *const u8);
277 builder.symbol("sigil_ptr_read_f64", sigil_ptr_read_f64 as *const u8);
278 builder.symbol("sigil_ptr_write_f64", sigil_ptr_write_f64 as *const u8);
279 builder.symbol("sigil_ptr_add", sigil_ptr_add as *const u8);
280 builder.symbol("sigil_ptr_is_null", sigil_ptr_is_null as *const u8);
281 builder.symbol("sigil_alloc", sigil_alloc as *const u8);
282 builder.symbol("sigil_free", sigil_free as *const u8);
283 builder.symbol("sigil_realloc", sigil_realloc as *const u8);
284 builder.symbol("sigil_memcpy", sigil_memcpy as *const u8);
285 builder.symbol("sigil_memset", sigil_memset as *const u8);
286
287 builtins.insert("sqrt".into(), sigil_sqrt as *const u8);
288 builtins.insert("sin".into(), sigil_sin as *const u8);
289 builtins.insert("cos".into(), sigil_cos as *const u8);
290 builtins.insert("pow".into(), sigil_pow as *const u8);
291 builtins.insert("exp".into(), sigil_exp as *const u8);
292 builtins.insert("ln".into(), sigil_ln as *const u8);
293 builtins.insert("floor".into(), sigil_floor as *const u8);
294 builtins.insert("ceil".into(), sigil_ceil as *const u8);
295 builtins.insert("abs".into(), sigil_abs as *const u8);
296 builtins.insert("print".into(), sigil_print as *const u8);
297 builtins.insert("now".into(), sigil_now as *const u8);
298
299 builtins
300 }
301
302 pub fn compile(&mut self, source: &str) -> Result<(), String> {
304 self.compile_with_opt(source, OptLevel::Aggressive)
305 }
306
307 pub fn compile_with_opt(
309 &mut self,
310 source: &str,
311 opt_level: OptLevel,
312 ) -> Result<(), String> {
313 let mut parser = Parser::new(source);
314 let source_file = parser.parse_file().map_err(|e| format!("{:?}", e))?;
315
316 let mut optimizer = Optimizer::new(opt_level);
318 let optimized = optimizer.optimize_file(&source_file);
319
320 for spanned_item in &optimized.items {
322 match &spanned_item.node {
323 Item::ExternBlock(extern_block) => {
324 self.declare_extern_block(extern_block)?;
325 }
326 Item::Function(func) => {
327 self.declare_function(func)?;
328 }
329 _ => {}
330 }
331 }
332
333 for spanned_item in &optimized.items {
335 if let Item::Function(func) = &spanned_item.node {
336 self.compile_function(func)?;
337 }
338 }
339
340 self.module
342 .finalize_definitions()
343 .map_err(|e| e.to_string())?;
344
345 Ok(())
346 }
347
348 fn declare_function(&mut self, func: &ast::Function) -> Result<FuncId, String> {
350 let name = &func.name.name;
351
352 let mut sig = self.module.make_signature();
354
355 for _param in &func.params {
357 sig.params.push(AbiParam::new(types::I64));
358 }
359
360 sig.returns.push(AbiParam::new(types::I64));
362
363 let func_id = self
364 .module
365 .declare_function(name, Linkage::Local, &sig)
366 .map_err(|e| e.to_string())?;
367
368 self.functions.insert(name.clone(), func_id);
369 Ok(func_id)
370 }
371
372 fn declare_extern_block(&mut self, extern_block: &ExternBlock) -> Result<(), String> {
374 if extern_block.abi != "C" && extern_block.abi != "c" {
376 return Err(format!(
377 "Unsupported ABI: {}. Only \"C\" is supported.",
378 extern_block.abi
379 ));
380 }
381
382 for item in &extern_block.items {
383 match item {
384 ExternItem::Function(func) => {
385 self.declare_extern_function(func)?;
386 }
387 ExternItem::Static(stat) => {
388 eprintln!(
390 "Warning: extern static '{}' not yet implemented",
391 stat.name.name
392 );
393 }
394 }
395 }
396
397 Ok(())
398 }
399
400 fn declare_extern_function(&mut self, func: &ExternFunction) -> Result<(), String> {
402 let name = &func.name.name;
403
404 let mut sig = self.module.make_signature();
406 let mut param_types = Vec::new();
407
408 for param in &func.params {
410 let ty = self.type_expr_to_cranelift(¶m.ty)?;
411 sig.params.push(AbiParam::new(ty));
412 param_types.push(ty);
413 }
414
415 let return_type = if let Some(ret_ty) = &func.return_type {
417 let ty = self.type_expr_to_cranelift(ret_ty)?;
418 sig.returns.push(AbiParam::new(ty));
419 Some(ty)
420 } else {
421 None
422 };
423
424 let func_id = self
428 .module
429 .declare_function(name, Linkage::Import, &sig)
430 .map_err(|e| e.to_string())?;
431
432 self.extern_functions.insert(
433 name.clone(),
434 ExternFnSig {
435 name: name.clone(),
436 params: param_types,
437 returns: return_type,
438 variadic: func.variadic,
439 func_id,
440 },
441 );
442
443 Ok(())
444 }
445
446 fn type_expr_to_cranelift(&self, ty: &TypeExpr) -> Result<types::Type, String> {
448 match ty {
449 TypeExpr::Path(path) => {
450 let name = path
451 .segments
452 .last()
453 .map(|s| s.ident.name.as_str())
454 .unwrap_or("");
455
456 if let Some(ctype) = CType::from_name(name) {
458 return Ok(match ctype {
459 CType::Void => types::I64, CType::Char
461 | CType::SChar
462 | CType::UChar
463 | CType::Int8
464 | CType::UInt8 => types::I8,
465 CType::Short | CType::UShort | CType::Int16 | CType::UInt16 => {
466 types::I16
467 }
468 CType::Int | CType::UInt | CType::Int32 | CType::UInt32 => types::I32,
469 CType::Long
470 | CType::ULong
471 | CType::LongLong
472 | CType::ULongLong
473 | CType::Size
474 | CType::SSize
475 | CType::PtrDiff
476 | CType::Int64
477 | CType::UInt64 => types::I64,
478 CType::Float => types::F32,
479 CType::Double => types::F64,
480 });
481 }
482
483 match name {
485 "i8" => Ok(types::I8),
486 "i16" => Ok(types::I16),
487 "i32" | "int" => Ok(types::I32),
488 "i64" => Ok(types::I64),
489 "u8" => Ok(types::I8),
490 "u16" => Ok(types::I16),
491 "u32" => Ok(types::I32),
492 "u64" => Ok(types::I64),
493 "f32" => Ok(types::F32),
494 "f64" | "float" => Ok(types::F64),
495 "bool" => Ok(types::I8),
496 "isize" | "usize" => Ok(types::I64),
497 "()" => Ok(types::I64), _ => Ok(types::I64), }
500 }
501 TypeExpr::Pointer { .. } | TypeExpr::Reference { .. } => {
502 Ok(types::I64)
504 }
505 _ => Ok(types::I64), }
507 }
508
509 fn compile_function(&mut self, func: &ast::Function) -> Result<(), String> {
511 let name = &func.name.name;
512 let func_id = *self.functions.get(name).ok_or("Function not declared")?;
513
514 for _param in &func.params {
516 self.ctx
517 .func
518 .signature
519 .params
520 .push(AbiParam::new(types::I64));
521 }
522 self.ctx
523 .func
524 .signature
525 .returns
526 .push(AbiParam::new(types::I64));
527 self.ctx.func.name = UserFuncName::user(0, func_id.as_u32());
528
529 let functions = self.functions.clone();
531 let extern_fns = self.extern_functions.clone();
532
533 {
534 let mut builder = FunctionBuilder::new(&mut self.ctx.func, &mut self.builder_ctx);
535
536 let entry_block = builder.create_block();
537 builder.append_block_params_for_function_params(entry_block);
538 builder.switch_to_block(entry_block);
539 builder.seal_block(entry_block);
540
541 let mut scope = CompileScope::new();
543
544 for (i, param) in func.params.iter().enumerate() {
546 let var = Variable::from_u32(scope.next_var() as u32);
547 builder.declare_var(var, types::I64);
548 let param_val = builder.block_params(entry_block)[i];
549 builder.def_var(var, param_val);
550
551 if let ast::Pattern::Ident { name, .. } = ¶m.pattern {
553 let param_type = match ¶m.ty {
555 TypeExpr::Path(path) => {
556 let type_name = path
557 .segments
558 .last()
559 .map(|s| s.ident.name.as_str())
560 .unwrap_or("");
561 match type_name {
562 "f32" | "f64" | "float" => ValueType::Float,
563 "i8" | "i16" | "i32" | "i64" | "int" | "isize" | "u8"
564 | "u16" | "u32" | "u64" | "usize" | "bool" => ValueType::Int,
565 _ => ValueType::Int, }
567 }
568 TypeExpr::Infer => ValueType::Int, _ => ValueType::Int, };
571 scope.define_typed(&name.name, var, param_type);
572 }
573 }
574
575 if let Some(body) = &func.body {
577 let (result, has_return) = compile_block_tracked(
578 &mut self.module,
579 &functions,
580 &extern_fns,
581 &mut builder,
582 &mut scope,
583 body,
584 )?;
585 if !has_return {
587 builder.ins().return_(&[result]);
588 }
589 } else {
590 let zero = builder.ins().iconst(types::I64, 0);
592 builder.ins().return_(&[zero]);
593 }
594
595 builder.finalize();
596 }
597
598 self.module
603 .define_function(func_id, &mut self.ctx)
604 .map_err(|e| format!("Compilation error for '{}': {}", name, e))?;
605
606 self.module.clear_context(&mut self.ctx);
607 Ok(())
608 }
609
610 pub fn run(&mut self) -> Result<i64, String> {
612 let main_id = *self.functions.get("main").ok_or("No main function")?;
613 let main_ptr = self.module.get_finalized_function(main_id);
614
615 unsafe {
616 let main_fn: CompiledFn = mem::transmute(main_ptr);
617 Ok(main_fn())
618 }
619 }
620
621 pub fn get_function(&self, name: &str) -> Option<*const u8> {
623 self.functions
624 .get(name)
625 .map(|id| self.module.get_finalized_function(*id))
626 }
627 }
628
629 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
632 enum ValueType {
633 Int, Float, Unknown, }
637
638 struct CompileScope {
643 variables: HashMap<String, Variable>,
644 var_types: HashMap<String, ValueType>,
646 var_counter: std::rc::Rc<std::cell::Cell<usize>>,
648 }
649
650 impl CompileScope {
651 fn new() -> Self {
652 Self {
653 variables: HashMap::new(),
654 var_types: HashMap::new(),
655 var_counter: std::rc::Rc::new(std::cell::Cell::new(0)),
656 }
657 }
658
659 fn child(&self) -> Self {
660 Self {
663 variables: self.variables.clone(),
664 var_types: self.var_types.clone(),
665 var_counter: std::rc::Rc::clone(&self.var_counter),
666 }
667 }
668
669 fn next_var(&mut self) -> usize {
670 let v = self.var_counter.get();
671 self.var_counter.set(v + 1);
672 v
673 }
674
675 #[allow(dead_code)]
676 fn define(&mut self, name: &str, var: Variable) {
677 self.variables.insert(name.to_string(), var);
678 }
679
680 fn define_typed(&mut self, name: &str, var: Variable, ty: ValueType) {
681 self.variables.insert(name.to_string(), var);
682 self.var_types.insert(name.to_string(), ty);
683 }
684
685 fn lookup(&self, name: &str) -> Option<Variable> {
686 self.variables.get(name).copied()
687 }
688
689 fn get_type(&self, name: &str) -> ValueType {
690 self.var_types
691 .get(name)
692 .copied()
693 .unwrap_or(ValueType::Unknown)
694 }
695
696 #[allow(dead_code)]
697 fn set_type(&mut self, name: &str, ty: ValueType) {
698 self.var_types.insert(name.to_string(), ty);
699 }
700 }
701
702 fn infer_type(expr: &Expr, scope: &CompileScope) -> ValueType {
710 match expr {
711 Expr::Literal(Literal::Int { .. }) => ValueType::Int,
712 Expr::Literal(Literal::Bool(_)) => ValueType::Int,
713 Expr::Literal(Literal::Float { .. }) => ValueType::Float,
714
715 Expr::Path(path) => {
716 let name = path
717 .segments
718 .last()
719 .map(|s| s.ident.name.as_str())
720 .unwrap_or("");
721 scope.get_type(name)
722 }
723
724 Expr::Binary { op, left, right } => {
725 let left_ty = infer_type(left, scope);
726 let right_ty = infer_type(right, scope);
727
728 if matches!(
730 op,
731 BinOp::Eq
732 | BinOp::Ne
733 | BinOp::Lt
734 | BinOp::Le
735 | BinOp::Gt
736 | BinOp::Ge
737 | BinOp::And
738 | BinOp::Or
739 ) {
740 return ValueType::Int;
741 }
742
743 if left_ty == ValueType::Float || right_ty == ValueType::Float {
745 return ValueType::Float;
746 }
747
748 if left_ty == ValueType::Int && right_ty == ValueType::Int {
750 return ValueType::Int;
751 }
752
753 ValueType::Unknown
755 }
756
757 Expr::Unary { op, expr } => {
758 match op {
759 UnaryOp::Not => ValueType::Int, UnaryOp::Neg => infer_type(expr, scope),
761 _ => infer_type(expr, scope),
762 }
763 }
764
765 Expr::Call { func, args } => {
766 if let Expr::Path(path) = func.as_ref() {
768 let name = path
769 .segments
770 .last()
771 .map(|s| s.ident.name.as_str())
772 .unwrap_or("");
773 match name {
774 "sqrt" | "sin" | "cos" | "pow" | "exp" | "ln" | "floor" | "ceil"
776 | "abs" => ValueType::Float,
777 "now" => ValueType::Int,
779 "len" | "sigil_array_len" => ValueType::Int,
781 "print" | "sigil_print" => ValueType::Int,
783 _ => {
784 let all_args_int = args
788 .iter()
789 .all(|arg| infer_type(arg, scope) == ValueType::Int);
790 if all_args_int {
791 ValueType::Int
792 } else {
793 ValueType::Unknown
794 }
795 }
796 }
797 } else {
798 ValueType::Unknown
799 }
800 }
801
802 Expr::If {
803 then_branch,
804 else_branch,
805 ..
806 } => {
807 let then_ty = if let Some(expr) = &then_branch.expr {
809 infer_type(expr, scope)
810 } else {
811 ValueType::Int };
813
814 if let Some(else_expr) = else_branch {
815 let else_ty = infer_type(else_expr, scope);
816 if then_ty == else_ty {
817 then_ty
818 } else {
819 ValueType::Unknown
820 }
821 } else {
822 then_ty
823 }
824 }
825
826 _ => ValueType::Unknown,
827 }
828 }
829
830 fn try_const_fold(expr: &Expr) -> Option<i64> {
836 match expr {
837 Expr::Literal(Literal::Int { value, .. }) => value.parse().ok(),
838 Expr::Literal(Literal::Bool(b)) => Some(if *b { 1 } else { 0 }),
839 Expr::Binary { op, left, right } => {
840 let l = try_const_fold(left)?;
841 let r = try_const_fold(right)?;
842 match op {
843 BinOp::Add => Some(l.wrapping_add(r)),
844 BinOp::Sub => Some(l.wrapping_sub(r)),
845 BinOp::Mul => Some(l.wrapping_mul(r)),
846 BinOp::Div if r != 0 => Some(l / r),
847 BinOp::Rem if r != 0 => Some(l % r),
848 BinOp::BitAnd => Some(l & r),
849 BinOp::BitOr => Some(l | r),
850 BinOp::BitXor => Some(l ^ r),
851 BinOp::Shl => Some(l << (r & 63)),
852 BinOp::Shr => Some(l >> (r & 63)),
853 BinOp::Eq => Some(if l == r { 1 } else { 0 }),
854 BinOp::Ne => Some(if l != r { 1 } else { 0 }),
855 BinOp::Lt => Some(if l < r { 1 } else { 0 }),
856 BinOp::Le => Some(if l <= r { 1 } else { 0 }),
857 BinOp::Gt => Some(if l > r { 1 } else { 0 }),
858 BinOp::Ge => Some(if l >= r { 1 } else { 0 }),
859 BinOp::And => Some(if l != 0 && r != 0 { 1 } else { 0 }),
860 BinOp::Or => Some(if l != 0 || r != 0 { 1 } else { 0 }),
861 _ => None,
862 }
863 }
864 Expr::Unary { op, expr } => {
865 let v = try_const_fold(expr)?;
866 match op {
867 UnaryOp::Neg => Some(-v),
868 UnaryOp::Not => Some(if v == 0 { 1 } else { 0 }),
869 _ => None,
870 }
871 }
872 _ => None,
873 }
874 }
875
876 fn compile_condition(
883 module: &mut JITModule,
884 functions: &HashMap<String, FuncId>,
885 extern_fns: &HashMap<String, ExternFnSig>,
886 builder: &mut FunctionBuilder,
887 scope: &mut CompileScope,
888 condition: &Expr,
889 ) -> Result<cranelift_codegen::ir::Value, String> {
890 if let Expr::Binary { op, left, right } = condition {
892 let cc = match op {
893 BinOp::Eq => Some(IntCC::Equal),
894 BinOp::Ne => Some(IntCC::NotEqual),
895 BinOp::Lt => Some(IntCC::SignedLessThan),
896 BinOp::Le => Some(IntCC::SignedLessThanOrEqual),
897 BinOp::Gt => Some(IntCC::SignedGreaterThan),
898 BinOp::Ge => Some(IntCC::SignedGreaterThanOrEqual),
899 _ => None,
900 };
901
902 if let Some(cc) = cc {
903 let lhs = compile_expr(module, functions, extern_fns, builder, scope, left)?;
904 let rhs = compile_expr(module, functions, extern_fns, builder, scope, right)?;
905 return Ok(builder.ins().icmp(cc, lhs, rhs));
907 }
908
909 if matches!(op, BinOp::And | BinOp::Or) {
911 }
914 }
915
916 if let Expr::Unary {
918 op: UnaryOp::Not,
919 expr,
920 } = condition
921 {
922 let inner = compile_condition(module, functions, extern_fns, builder, scope, expr)?;
923 let true_val = builder.ins().iconst(types::I8, 1);
925 return Ok(builder.ins().bxor(inner, true_val));
926 }
927
928 if let Expr::Literal(Literal::Bool(b)) = condition {
930 return Ok(builder.ins().iconst(types::I8, if *b { 1 } else { 0 }));
931 }
932
933 let val = compile_expr(module, functions, extern_fns, builder, scope, condition)?;
935 let zero = builder.ins().iconst(types::I64, 0);
936 Ok(builder.ins().icmp(IntCC::NotEqual, val, zero))
937 }
938
939 #[allow(dead_code)]
945 fn is_tail_call_to<'a>(expr: &'a Expr, func_name: &str) -> Option<&'a Vec<Expr>> {
946 if let Expr::Return(Some(inner)) = expr {
947 if let Expr::Call { func, args } = inner.as_ref() {
948 if let Expr::Path(path) = func.as_ref() {
949 let name = path
950 .segments
951 .last()
952 .map(|s| s.ident.name.as_str())
953 .unwrap_or("");
954 if name == func_name {
955 return Some(args);
956 }
957 }
958 }
959 }
960 None
961 }
962
963 fn compile_block_tracked(
969 module: &mut JITModule,
970 functions: &HashMap<String, FuncId>,
971 extern_fns: &HashMap<String, ExternFnSig>,
972 builder: &mut FunctionBuilder,
973 scope: &mut CompileScope,
974 block: &ast::Block,
975 ) -> Result<(cranelift_codegen::ir::Value, bool), String> {
976 let mut last_val: Option<cranelift_codegen::ir::Value> = None;
978 let mut has_return = false;
979
980 for stmt in &block.stmts {
981 let (val, ret) =
982 compile_stmt_tracked(module, functions, extern_fns, builder, scope, stmt)?;
983 last_val = Some(val);
984 if ret {
985 has_return = true;
986 }
987 }
988
989 if let Some(expr) = &block.expr {
990 let (val, ret) =
991 compile_expr_tracked(module, functions, extern_fns, builder, scope, expr)?;
992 last_val = Some(val);
993 if ret {
994 has_return = true;
995 }
996 }
997
998 let result = last_val.unwrap_or_else(|| builder.ins().iconst(types::I64, 0));
1000 Ok((result, has_return))
1001 }
1002
1003 fn compile_block(
1005 module: &mut JITModule,
1006 functions: &HashMap<String, FuncId>,
1007 extern_fns: &HashMap<String, ExternFnSig>,
1008 builder: &mut FunctionBuilder,
1009 scope: &mut CompileScope,
1010 block: &ast::Block,
1011 ) -> Result<cranelift_codegen::ir::Value, String> {
1012 compile_block_tracked(module, functions, extern_fns, builder, scope, block).map(|(v, _)| v)
1013 }
1014
1015 fn compile_stmt_tracked(
1017 module: &mut JITModule,
1018 functions: &HashMap<String, FuncId>,
1019 extern_fns: &HashMap<String, ExternFnSig>,
1020 builder: &mut FunctionBuilder,
1021 scope: &mut CompileScope,
1022 stmt: &ast::Stmt,
1023 ) -> Result<(cranelift_codegen::ir::Value, bool), String> {
1024 match stmt {
1025 ast::Stmt::Let { pattern, init, .. } => {
1026 let ty = if let Some(expr) = init {
1028 infer_type(expr, scope)
1029 } else {
1030 ValueType::Int };
1032
1033 let val = if let Some(expr) = init {
1034 compile_expr(module, functions, extern_fns, builder, scope, expr)?
1035 } else {
1036 builder.ins().iconst(types::I64, 0)
1037 };
1038
1039 if let ast::Pattern::Ident { name, .. } = pattern {
1040 let var = Variable::from_u32(scope.next_var() as u32);
1041 builder.declare_var(var, types::I64);
1042 builder.def_var(var, val);
1043 scope.define_typed(&name.name, var, ty);
1045 }
1046
1047 Ok((val, false))
1048 }
1049 ast::Stmt::LetElse {
1050 pattern,
1051 init,
1052 else_branch,
1053 ..
1054 } => {
1055 let val = compile_expr(module, functions, extern_fns, builder, scope, init)?;
1058 let ty = infer_type(init, scope);
1059
1060 if let ast::Pattern::Ident { name, .. } = pattern {
1061 let var = Variable::from_u32(scope.next_var() as u32);
1062 builder.declare_var(var, types::I64);
1063 builder.def_var(var, val);
1064 scope.define_typed(&name.name, var, ty);
1065 }
1066
1067 let _ = else_branch;
1071
1072 Ok((val, false))
1073 }
1074 ast::Stmt::Expr(expr) | ast::Stmt::Semi(expr) => {
1075 compile_expr_tracked(module, functions, extern_fns, builder, scope, expr)
1076 }
1077 ast::Stmt::Item(_) => Ok((builder.ins().iconst(types::I64, 0), false)),
1078 }
1079 }
1080
1081 #[allow(dead_code)]
1083 fn compile_stmt(
1084 module: &mut JITModule,
1085 functions: &HashMap<String, FuncId>,
1086 extern_fns: &HashMap<String, ExternFnSig>,
1087 builder: &mut FunctionBuilder,
1088 scope: &mut CompileScope,
1089 stmt: &ast::Stmt,
1090 ) -> Result<cranelift_codegen::ir::Value, String> {
1091 compile_stmt_tracked(module, functions, extern_fns, builder, scope, stmt).map(|(v, _)| v)
1092 }
1093
1094 fn compile_expr_tracked(
1096 module: &mut JITModule,
1097 functions: &HashMap<String, FuncId>,
1098 extern_fns: &HashMap<String, ExternFnSig>,
1099 builder: &mut FunctionBuilder,
1100 scope: &mut CompileScope,
1101 expr: &Expr,
1102 ) -> Result<(cranelift_codegen::ir::Value, bool), String> {
1103 match expr {
1104 Expr::Return(value) => {
1105 let ret_val = if let Some(v) = value {
1117 compile_expr(module, functions, extern_fns, builder, scope, v)?
1118 } else {
1119 builder.ins().iconst(types::I64, 0)
1120 };
1121 builder.ins().return_(&[ret_val]);
1122 Ok((ret_val, true)) }
1124 Expr::If {
1125 condition,
1126 then_branch,
1127 else_branch,
1128 } => {
1129 compile_if_tracked(
1131 module,
1132 functions,
1133 extern_fns,
1134 builder,
1135 scope,
1136 condition,
1137 then_branch,
1138 else_branch.as_deref(),
1139 )
1140 }
1141 Expr::Block(block) => {
1142 let mut inner_scope = scope.child();
1143 compile_block_tracked(
1144 module,
1145 functions,
1146 extern_fns,
1147 builder,
1148 &mut inner_scope,
1149 block,
1150 )
1151 }
1152 _ => {
1153 let val = compile_expr(module, functions, extern_fns, builder, scope, expr)?;
1155 Ok((val, false))
1156 }
1157 }
1158 }
1159
1160 fn compile_expr(
1162 module: &mut JITModule,
1163 functions: &HashMap<String, FuncId>,
1164 extern_fns: &HashMap<String, ExternFnSig>,
1165 builder: &mut FunctionBuilder,
1166 scope: &mut CompileScope,
1167 expr: &Expr,
1168 ) -> Result<cranelift_codegen::ir::Value, String> {
1169 if let Some(val) = try_const_fold(expr) {
1171 return Ok(builder.ins().iconst(types::I64, val));
1172 }
1173
1174 match expr {
1175 Expr::Literal(lit) => compile_literal(builder, lit),
1176
1177 Expr::Path(path) => {
1178 let name = path
1179 .segments
1180 .last()
1181 .map(|s| s.ident.name.clone())
1182 .unwrap_or_default();
1183 if let Some(var) = scope.lookup(&name) {
1184 Ok(builder.use_var(var))
1185 } else {
1186 Err(format!("Undefined variable: {}", name))
1187 }
1188 }
1189
1190 Expr::Binary { op, left, right } => {
1191 let left_ty = infer_type(left, scope);
1193 let right_ty = infer_type(right, scope);
1194
1195 let lhs = compile_expr(module, functions, extern_fns, builder, scope, left)?;
1196 let rhs = compile_expr(module, functions, extern_fns, builder, scope, right)?;
1197
1198 if left_ty == ValueType::Int && right_ty == ValueType::Int {
1201 return compile_binary_op(builder, op.clone(), lhs, rhs);
1203 }
1204
1205 if left_ty == ValueType::Float && right_ty == ValueType::Float {
1207 return compile_float_binary_op(builder, op, lhs, rhs);
1208 }
1209
1210 match op {
1213 BinOp::Add => compile_call(
1214 module,
1215 functions,
1216 extern_fns,
1217 builder,
1218 "sigil_add",
1219 &[lhs, rhs],
1220 ),
1221 BinOp::Sub => compile_call(
1222 module,
1223 functions,
1224 extern_fns,
1225 builder,
1226 "sigil_sub",
1227 &[lhs, rhs],
1228 ),
1229 BinOp::Mul => compile_call(
1230 module,
1231 functions,
1232 extern_fns,
1233 builder,
1234 "sigil_mul",
1235 &[lhs, rhs],
1236 ),
1237 BinOp::Div => compile_call(
1238 module,
1239 functions,
1240 extern_fns,
1241 builder,
1242 "sigil_div",
1243 &[lhs, rhs],
1244 ),
1245 BinOp::Lt => compile_call(
1246 module,
1247 functions,
1248 extern_fns,
1249 builder,
1250 "sigil_lt",
1251 &[lhs, rhs],
1252 ),
1253 BinOp::Le => compile_call(
1254 module,
1255 functions,
1256 extern_fns,
1257 builder,
1258 "sigil_le",
1259 &[lhs, rhs],
1260 ),
1261 BinOp::Gt => compile_call(
1262 module,
1263 functions,
1264 extern_fns,
1265 builder,
1266 "sigil_gt",
1267 &[lhs, rhs],
1268 ),
1269 BinOp::Ge => compile_call(
1270 module,
1271 functions,
1272 extern_fns,
1273 builder,
1274 "sigil_ge",
1275 &[lhs, rhs],
1276 ),
1277 _ => compile_binary_op(builder, op.clone(), lhs, rhs),
1278 }
1279 }
1280
1281 Expr::Unary { op, expr: inner } => {
1282 let val = compile_expr(module, functions, extern_fns, builder, scope, inner)?;
1283 compile_unary_op(builder, *op, val)
1284 }
1285
1286 Expr::Call { func, args } => {
1287 let func_name = match func.as_ref() {
1288 Expr::Path(path) => path
1289 .segments
1290 .last()
1291 .map(|s| s.ident.name.clone())
1292 .unwrap_or_default(),
1293 _ => return Err("Only direct function calls supported".into()),
1294 };
1295
1296 let mut arg_vals = Vec::new();
1297 for arg in args {
1298 arg_vals.push(compile_expr(
1299 module, functions, extern_fns, builder, scope, arg,
1300 )?);
1301 }
1302
1303 compile_call(
1304 module, functions, extern_fns, builder, &func_name, &arg_vals,
1305 )
1306 }
1307
1308 Expr::If {
1309 condition,
1310 then_branch,
1311 else_branch,
1312 } => compile_if(
1313 module,
1314 functions,
1315 extern_fns,
1316 builder,
1317 scope,
1318 condition,
1319 then_branch,
1320 else_branch.as_deref(),
1321 ),
1322
1323 Expr::While {
1324 condition, body, ..
1325 } => compile_while(
1326 module, functions, extern_fns, builder, scope, condition, body,
1327 ),
1328
1329 Expr::Block(block) => {
1330 let mut inner_scope = scope.child();
1331 compile_block(
1332 module,
1333 functions,
1334 extern_fns,
1335 builder,
1336 &mut inner_scope,
1337 block,
1338 )
1339 }
1340
1341 Expr::Return(value) => {
1342 let ret_val = if let Some(v) = value {
1345 compile_expr(module, functions, extern_fns, builder, scope, v)?
1346 } else {
1347 builder.ins().iconst(types::I64, 0)
1348 };
1349 builder.ins().return_(&[ret_val]);
1350 Ok(ret_val)
1351 }
1352
1353 Expr::Assign { target, value } => {
1354 let val = compile_expr(module, functions, extern_fns, builder, scope, value)?;
1355 match target.as_ref() {
1356 Expr::Path(path) => {
1357 let name = path
1358 .segments
1359 .last()
1360 .map(|s| s.ident.name.clone())
1361 .unwrap_or_default();
1362 if let Some(var) = scope.lookup(&name) {
1363 builder.def_var(var, val);
1364 Ok(val)
1365 } else {
1366 Err(format!("Undefined variable: {}", name))
1367 }
1368 }
1369 Expr::Index { expr: arr, index } => {
1370 let arr_val =
1371 compile_expr(module, functions, extern_fns, builder, scope, arr)?;
1372 let idx_val =
1373 compile_expr(module, functions, extern_fns, builder, scope, index)?;
1374 compile_call(
1375 module,
1376 functions,
1377 extern_fns,
1378 builder,
1379 "sigil_array_set",
1380 &[arr_val, idx_val, val],
1381 )
1382 }
1383 _ => Err("Invalid assignment target".into()),
1384 }
1385 }
1386
1387 Expr::Index { expr: arr, index } => {
1388 let arr_val = compile_expr(module, functions, extern_fns, builder, scope, arr)?;
1389 let idx_val = compile_expr(module, functions, extern_fns, builder, scope, index)?;
1390 compile_call(
1391 module,
1392 functions,
1393 extern_fns,
1394 builder,
1395 "sigil_array_get",
1396 &[arr_val, idx_val],
1397 )
1398 }
1399
1400 Expr::Array(elements) => {
1401 let len = builder.ins().iconst(types::I64, elements.len() as i64);
1402 let arr = compile_call(
1403 module,
1404 functions,
1405 extern_fns,
1406 builder,
1407 "sigil_array_new",
1408 &[len],
1409 )?;
1410
1411 for (i, elem) in elements.iter().enumerate() {
1412 let val = compile_expr(module, functions, extern_fns, builder, scope, elem)?;
1413 let idx = builder.ins().iconst(types::I64, i as i64);
1414 compile_call(
1415 module,
1416 functions,
1417 extern_fns,
1418 builder,
1419 "sigil_array_set",
1420 &[arr, idx, val],
1421 )?;
1422 }
1423
1424 Ok(arr)
1425 }
1426
1427 Expr::Pipe { expr, operations } => {
1428 let mut result = compile_expr(module, functions, extern_fns, builder, scope, expr)?;
1430
1431 for op in operations {
1433 result = match op {
1434 PipeOp::First => compile_call(
1436 module,
1437 functions,
1438 extern_fns,
1439 builder,
1440 "sigil_array_first",
1441 &[result],
1442 )?,
1443 PipeOp::Last => compile_call(
1444 module,
1445 functions,
1446 extern_fns,
1447 builder,
1448 "sigil_array_last",
1449 &[result],
1450 )?,
1451 PipeOp::Middle => compile_call(
1452 module,
1453 functions,
1454 extern_fns,
1455 builder,
1456 "sigil_array_middle",
1457 &[result],
1458 )?,
1459 PipeOp::Choice => compile_call(
1460 module,
1461 functions,
1462 extern_fns,
1463 builder,
1464 "sigil_array_choice",
1465 &[result],
1466 )?,
1467 PipeOp::Next => compile_call(
1468 module,
1469 functions,
1470 extern_fns,
1471 builder,
1472 "sigil_array_next",
1473 &[result],
1474 )?,
1475 PipeOp::Nth(index_expr) => {
1476 let index = compile_expr(
1477 module, functions, extern_fns, builder, scope, index_expr,
1478 )?;
1479 compile_call(
1480 module,
1481 functions,
1482 extern_fns,
1483 builder,
1484 "sigil_array_nth",
1485 &[result, index],
1486 )?
1487 }
1488 PipeOp::Reduce(_) => {
1490 compile_call(
1492 module,
1493 functions,
1494 extern_fns,
1495 builder,
1496 "sigil_array_sum",
1497 &[result],
1498 )?
1499 }
1500 PipeOp::ReduceSum => compile_call(
1502 module,
1503 functions,
1504 extern_fns,
1505 builder,
1506 "sigil_array_sum",
1507 &[result],
1508 )?,
1509 PipeOp::ReduceProd => compile_call(
1511 module,
1512 functions,
1513 extern_fns,
1514 builder,
1515 "sigil_array_product",
1516 &[result],
1517 )?,
1518 PipeOp::ReduceMin => compile_call(
1520 module,
1521 functions,
1522 extern_fns,
1523 builder,
1524 "sigil_array_min",
1525 &[result],
1526 )?,
1527 PipeOp::ReduceMax => compile_call(
1529 module,
1530 functions,
1531 extern_fns,
1532 builder,
1533 "sigil_array_max",
1534 &[result],
1535 )?,
1536 PipeOp::ReduceConcat => compile_call(
1538 module,
1539 functions,
1540 extern_fns,
1541 builder,
1542 "sigil_array_concat",
1543 &[result],
1544 )?,
1545 PipeOp::ReduceAll => compile_call(
1547 module,
1548 functions,
1549 extern_fns,
1550 builder,
1551 "sigil_array_all",
1552 &[result],
1553 )?,
1554 PipeOp::ReduceAny => compile_call(
1556 module,
1557 functions,
1558 extern_fns,
1559 builder,
1560 "sigil_array_any",
1561 &[result],
1562 )?,
1563 PipeOp::Sort(_) => compile_call(
1565 module,
1566 functions,
1567 extern_fns,
1568 builder,
1569 "sigil_array_sort",
1570 &[result],
1571 )?,
1572 PipeOp::Transform(_) | PipeOp::Filter(_) => {
1574 result
1577 }
1578 PipeOp::Method {
1580 name,
1581 type_args: _,
1582 args,
1583 } => {
1584 let mut call_args = vec![result];
1586 for arg in args {
1587 call_args.push(compile_expr(
1588 module, functions, extern_fns, builder, scope, arg,
1589 )?);
1590 }
1591 compile_call(
1592 module, functions, extern_fns, builder, &name.name, &call_args,
1593 )?
1594 }
1595 PipeOp::Await => {
1596 result
1598 }
1599 PipeOp::Match(_) => {
1600 result
1603 }
1604 PipeOp::TryMap(_) => {
1605 result
1607 }
1608 PipeOp::Call(callee) => {
1609 let callee_val = compile_expr(
1612 module, functions, extern_fns, builder, scope, callee,
1613 )?;
1614 compile_call(
1616 module,
1617 functions,
1618 extern_fns,
1619 builder,
1620 "sigil_call",
1621 &[callee_val, result],
1622 )?
1623 }
1624 PipeOp::Named { prefix, body } => {
1625 if !prefix.is_empty() {
1627 let fn_name = &prefix[0].name;
1628 if let Some(body_expr) = body {
1629 let body_val = compile_expr(
1630 module, functions, extern_fns, builder, scope, body_expr,
1631 )?;
1632 compile_call(
1633 module,
1634 functions,
1635 extern_fns,
1636 builder,
1637 fn_name,
1638 &[result, body_val],
1639 )?
1640 } else {
1641 compile_call(
1642 module,
1643 functions,
1644 extern_fns,
1645 builder,
1646 fn_name,
1647 &[result],
1648 )?
1649 }
1650 } else {
1651 result
1652 }
1653 }
1654 PipeOp::Parallel(inner_op) => {
1656 match inner_op.as_ref() {
1659 PipeOp::Transform(_) => {
1660 compile_call(
1662 module,
1663 functions,
1664 extern_fns,
1665 builder,
1666 "sigil_parallel_map",
1667 &[result],
1668 )?
1669 }
1670 PipeOp::Filter(_) => {
1671 compile_call(
1673 module,
1674 functions,
1675 extern_fns,
1676 builder,
1677 "sigil_parallel_filter",
1678 &[result],
1679 )?
1680 }
1681 PipeOp::Reduce(_) => {
1682 compile_call(
1684 module,
1685 functions,
1686 extern_fns,
1687 builder,
1688 "sigil_parallel_reduce",
1689 &[result],
1690 )?
1691 }
1692 _ => result,
1694 }
1695 }
1696 PipeOp::Gpu(inner_op) => {
1698 match inner_op.as_ref() {
1701 PipeOp::Transform(_) => {
1702 compile_call(
1704 module,
1705 functions,
1706 extern_fns,
1707 builder,
1708 "sigil_gpu_map",
1709 &[result],
1710 )?
1711 }
1712 PipeOp::Filter(_) => {
1713 compile_call(
1715 module,
1716 functions,
1717 extern_fns,
1718 builder,
1719 "sigil_gpu_filter",
1720 &[result],
1721 )?
1722 }
1723 PipeOp::Reduce(_) => {
1724 compile_call(
1726 module,
1727 functions,
1728 extern_fns,
1729 builder,
1730 "sigil_gpu_reduce",
1731 &[result],
1732 )?
1733 }
1734 _ => result,
1735 }
1736 }
1737
1738 PipeOp::Send(data_expr) => {
1745 let data = compile_expr(
1746 module, functions, extern_fns, builder, scope, data_expr,
1747 )?;
1748 compile_call(
1749 module,
1750 functions,
1751 extern_fns,
1752 builder,
1753 "sigil_protocol_send",
1754 &[result, data],
1755 )?
1756 }
1757
1758 PipeOp::Recv => compile_call(
1760 module,
1761 functions,
1762 extern_fns,
1763 builder,
1764 "sigil_protocol_recv",
1765 &[result],
1766 )?,
1767
1768 PipeOp::Stream(handler_expr) => {
1770 let handler = compile_expr(
1771 module,
1772 functions,
1773 extern_fns,
1774 builder,
1775 scope,
1776 handler_expr,
1777 )?;
1778 compile_call(
1779 module,
1780 functions,
1781 extern_fns,
1782 builder,
1783 "sigil_protocol_stream",
1784 &[result, handler],
1785 )?
1786 }
1787
1788 PipeOp::Connect(config_expr) => {
1790 if let Some(config) = config_expr {
1791 let config_val = compile_expr(
1792 module, functions, extern_fns, builder, scope, config,
1793 )?;
1794 compile_call(
1795 module,
1796 functions,
1797 extern_fns,
1798 builder,
1799 "sigil_protocol_connect",
1800 &[result, config_val],
1801 )?
1802 } else {
1803 compile_call(
1804 module,
1805 functions,
1806 extern_fns,
1807 builder,
1808 "sigil_protocol_connect_default",
1809 &[result],
1810 )?
1811 }
1812 }
1813
1814 PipeOp::Close => compile_call(
1816 module,
1817 functions,
1818 extern_fns,
1819 builder,
1820 "sigil_protocol_close",
1821 &[result],
1822 )?,
1823
1824 PipeOp::Header { name, value } => {
1826 let name_val =
1827 compile_expr(module, functions, extern_fns, builder, scope, name)?;
1828 let value_val =
1829 compile_expr(module, functions, extern_fns, builder, scope, value)?;
1830 compile_call(
1831 module,
1832 functions,
1833 extern_fns,
1834 builder,
1835 "sigil_protocol_header",
1836 &[result, name_val, value_val],
1837 )?
1838 }
1839
1840 PipeOp::Body(data_expr) => {
1842 let data = compile_expr(
1843 module, functions, extern_fns, builder, scope, data_expr,
1844 )?;
1845 compile_call(
1846 module,
1847 functions,
1848 extern_fns,
1849 builder,
1850 "sigil_protocol_body",
1851 &[result, data],
1852 )?
1853 }
1854
1855 PipeOp::Timeout(ms_expr) => {
1857 let ms = compile_expr(
1858 module, functions, extern_fns, builder, scope, ms_expr,
1859 )?;
1860 compile_call(
1861 module,
1862 functions,
1863 extern_fns,
1864 builder,
1865 "sigil_protocol_timeout",
1866 &[result, ms],
1867 )?
1868 }
1869
1870 PipeOp::Retry { count, strategy } => {
1872 let count_val =
1873 compile_expr(module, functions, extern_fns, builder, scope, count)?;
1874 if let Some(strat) = strategy {
1875 let strat_val = compile_expr(
1876 module, functions, extern_fns, builder, scope, strat,
1877 )?;
1878 compile_call(
1879 module,
1880 functions,
1881 extern_fns,
1882 builder,
1883 "sigil_protocol_retry",
1884 &[result, count_val, strat_val],
1885 )?
1886 } else {
1887 compile_call(
1888 module,
1889 functions,
1890 extern_fns,
1891 builder,
1892 "sigil_protocol_retry_default",
1893 &[result, count_val],
1894 )?
1895 }
1896 }
1897
1898 PipeOp::Validate {
1900 predicate,
1901 target_evidence: _,
1902 } => {
1903 let pred_val = compile_expr(
1904 module, functions, extern_fns, builder, scope, predicate,
1905 )?;
1906 compile_call(
1907 module,
1908 functions,
1909 extern_fns,
1910 builder,
1911 "sigil_validate",
1912 &[result, pred_val],
1913 )?
1914 }
1915
1916 PipeOp::Assume {
1917 reason,
1918 target_evidence: _,
1919 } => {
1920 let reason_val = if let Some(r) = reason {
1921 compile_expr(module, functions, extern_fns, builder, scope, r)?
1922 } else {
1923 builder.ins().iconst(types::I64, 0)
1924 };
1925 compile_call(
1926 module,
1927 functions,
1928 extern_fns,
1929 builder,
1930 "sigil_assume",
1931 &[result, reason_val],
1932 )?
1933 }
1934
1935 PipeOp::AssertEvidence(_) => {
1936 result
1939 }
1940
1941 PipeOp::Also(func) => {
1943 let _ =
1945 compile_expr(module, functions, extern_fns, builder, scope, func)?;
1946 result
1947 }
1948
1949 PipeOp::Apply(func) => {
1950 let _ =
1952 compile_expr(module, functions, extern_fns, builder, scope, func)?;
1953 result
1954 }
1955
1956 PipeOp::TakeIf(pred) => {
1957 let pred_val =
1959 compile_expr(module, functions, extern_fns, builder, scope, pred)?;
1960 compile_call(
1961 module,
1962 functions,
1963 extern_fns,
1964 builder,
1965 "sigil_take_if",
1966 &[result, pred_val],
1967 )?
1968 }
1969
1970 PipeOp::TakeUnless(pred) => {
1971 let pred_val =
1973 compile_expr(module, functions, extern_fns, builder, scope, pred)?;
1974 compile_call(
1975 module,
1976 functions,
1977 extern_fns,
1978 builder,
1979 "sigil_take_unless",
1980 &[result, pred_val],
1981 )?
1982 }
1983
1984 PipeOp::Let(func) => {
1985 compile_expr(module, functions, extern_fns, builder, scope, func)?
1987 }
1988
1989 PipeOp::All(_)
1992 | PipeOp::Any(_)
1993 | PipeOp::Compose(_)
1994 | PipeOp::Zip(_)
1995 | PipeOp::Scan(_)
1996 | PipeOp::Diff
1997 | PipeOp::Gradient(_)
1998 | PipeOp::SortAsc
1999 | PipeOp::SortDesc
2000 | PipeOp::Reverse
2001 | PipeOp::Cycle(_)
2002 | PipeOp::Windows(_)
2003 | PipeOp::Chunks(_)
2004 | PipeOp::Flatten
2005 | PipeOp::Unique
2006 | PipeOp::Enumerate => {
2007 result
2009 }
2010 };
2011 }
2012
2013 Ok(result)
2014 }
2015
2016 Expr::Unsafe(block) => {
2018 let mut inner_scope = scope.child();
2019 compile_block(
2020 module,
2021 functions,
2022 extern_fns,
2023 builder,
2024 &mut inner_scope,
2025 block,
2026 )
2027 }
2028
2029 Expr::Async { block, .. } => {
2031 let mut inner_scope = scope.child();
2032 compile_block(
2033 module,
2034 functions,
2035 extern_fns,
2036 builder,
2037 &mut inner_scope,
2038 block,
2039 )
2040 }
2041
2042 Expr::Deref(inner) => {
2044 let ptr = compile_expr(module, functions, extern_fns, builder, scope, inner)?;
2045 Ok(builder
2047 .ins()
2048 .load(types::I64, cranelift_codegen::ir::MemFlags::new(), ptr, 0))
2049 }
2050
2051 Expr::AddrOf { expr: inner, .. } => {
2053 compile_expr(module, functions, extern_fns, builder, scope, inner)
2054 }
2055
2056 Expr::Cast { expr: inner, ty } => {
2058 let val = compile_expr(module, functions, extern_fns, builder, scope, inner)?;
2059 let _ = ty; Ok(val)
2062 }
2063
2064 _ => Ok(builder.ins().iconst(types::I64, 0)),
2065 }
2066 }
2067
2068 fn compile_literal(
2070 builder: &mut FunctionBuilder,
2071 lit: &Literal,
2072 ) -> Result<cranelift_codegen::ir::Value, String> {
2073 match lit {
2074 Literal::Int { value, .. } => {
2075 let val: i64 = value.parse().map_err(|_| "Invalid integer")?;
2076 Ok(builder.ins().iconst(types::I64, val))
2077 }
2078 Literal::Float { value, .. } => {
2079 let val: f64 = value.parse().map_err(|_| "Invalid float")?;
2080 Ok(builder.ins().iconst(types::I64, val.to_bits() as i64))
2083 }
2084 Literal::Bool(b) => Ok(builder.ins().iconst(types::I64, if *b { 1 } else { 0 })),
2085 Literal::String(_) => Ok(builder.ins().iconst(types::I64, 0)),
2086 _ => Ok(builder.ins().iconst(types::I64, 0)),
2087 }
2088 }
2089
2090 fn compile_binary_op(
2092 builder: &mut FunctionBuilder,
2093 op: BinOp,
2094 lhs: cranelift_codegen::ir::Value,
2095 rhs: cranelift_codegen::ir::Value,
2096 ) -> Result<cranelift_codegen::ir::Value, String> {
2097 let result = match op {
2098 BinOp::Add => builder.ins().iadd(lhs, rhs),
2099 BinOp::Sub => builder.ins().isub(lhs, rhs),
2100 BinOp::Mul => builder.ins().imul(lhs, rhs),
2101 BinOp::Div => builder.ins().sdiv(lhs, rhs),
2102 BinOp::Rem => builder.ins().srem(lhs, rhs),
2103 BinOp::Pow => return Err("Power not supported".into()),
2104 BinOp::BitAnd => builder.ins().band(lhs, rhs),
2105 BinOp::BitOr => builder.ins().bor(lhs, rhs),
2106 BinOp::BitXor => builder.ins().bxor(lhs, rhs),
2107 BinOp::Shl => builder.ins().ishl(lhs, rhs),
2108 BinOp::Shr => builder.ins().sshr(lhs, rhs),
2109 BinOp::Eq => {
2110 let cmp = builder.ins().icmp(IntCC::Equal, lhs, rhs);
2111 builder.ins().uextend(types::I64, cmp)
2112 }
2113 BinOp::Ne => {
2114 let cmp = builder.ins().icmp(IntCC::NotEqual, lhs, rhs);
2115 builder.ins().uextend(types::I64, cmp)
2116 }
2117 BinOp::Lt => {
2118 let cmp = builder.ins().icmp(IntCC::SignedLessThan, lhs, rhs);
2119 builder.ins().uextend(types::I64, cmp)
2120 }
2121 BinOp::Le => {
2122 let cmp = builder.ins().icmp(IntCC::SignedLessThanOrEqual, lhs, rhs);
2123 builder.ins().uextend(types::I64, cmp)
2124 }
2125 BinOp::Gt => {
2126 let cmp = builder.ins().icmp(IntCC::SignedGreaterThan, lhs, rhs);
2127 builder.ins().uextend(types::I64, cmp)
2128 }
2129 BinOp::Ge => {
2130 let cmp = builder
2131 .ins()
2132 .icmp(IntCC::SignedGreaterThanOrEqual, lhs, rhs);
2133 builder.ins().uextend(types::I64, cmp)
2134 }
2135 BinOp::And => builder.ins().band(lhs, rhs),
2136 BinOp::Or => builder.ins().bor(lhs, rhs),
2137 BinOp::Concat => return Err("Concat not supported".into()),
2138 BinOp::MatMul => return Err("MatMul not supported in JIT (use runtime)".into()),
2139 BinOp::Hadamard => return Err("Hadamard not supported in JIT (use runtime)".into()),
2140 BinOp::TensorProd => return Err("TensorProd not supported in JIT (use runtime)".into()),
2141 };
2142 Ok(result)
2143 }
2144
2145 fn compile_float_binary_op(
2147 builder: &mut FunctionBuilder,
2148 op: &BinOp,
2149 lhs: cranelift_codegen::ir::Value,
2150 rhs: cranelift_codegen::ir::Value,
2151 ) -> Result<cranelift_codegen::ir::Value, String> {
2152 use cranelift_codegen::ir::condcodes::FloatCC;
2153
2154 let lhs_f = builder
2156 .ins()
2157 .bitcast(types::F64, cranelift_codegen::ir::MemFlags::new(), lhs);
2158 let rhs_f = builder
2159 .ins()
2160 .bitcast(types::F64, cranelift_codegen::ir::MemFlags::new(), rhs);
2161
2162 let result_f = match op {
2163 BinOp::Add => builder.ins().fadd(lhs_f, rhs_f),
2164 BinOp::Sub => builder.ins().fsub(lhs_f, rhs_f),
2165 BinOp::Mul => builder.ins().fmul(lhs_f, rhs_f),
2166 BinOp::Div => builder.ins().fdiv(lhs_f, rhs_f),
2167 BinOp::Lt => {
2168 let cmp = builder.ins().fcmp(FloatCC::LessThan, lhs_f, rhs_f);
2169 return Ok(builder.ins().uextend(types::I64, cmp));
2170 }
2171 BinOp::Le => {
2172 let cmp = builder.ins().fcmp(FloatCC::LessThanOrEqual, lhs_f, rhs_f);
2173 return Ok(builder.ins().uextend(types::I64, cmp));
2174 }
2175 BinOp::Gt => {
2176 let cmp = builder.ins().fcmp(FloatCC::GreaterThan, lhs_f, rhs_f);
2177 return Ok(builder.ins().uextend(types::I64, cmp));
2178 }
2179 BinOp::Ge => {
2180 let cmp = builder
2181 .ins()
2182 .fcmp(FloatCC::GreaterThanOrEqual, lhs_f, rhs_f);
2183 return Ok(builder.ins().uextend(types::I64, cmp));
2184 }
2185 BinOp::Eq => {
2186 let cmp = builder.ins().fcmp(FloatCC::Equal, lhs_f, rhs_f);
2187 return Ok(builder.ins().uextend(types::I64, cmp));
2188 }
2189 BinOp::Ne => {
2190 let cmp = builder.ins().fcmp(FloatCC::NotEqual, lhs_f, rhs_f);
2191 return Ok(builder.ins().uextend(types::I64, cmp));
2192 }
2193 _ => return Err(format!("Float operation {:?} not supported", op)),
2194 };
2195
2196 Ok(builder
2198 .ins()
2199 .bitcast(types::I64, cranelift_codegen::ir::MemFlags::new(), result_f))
2200 }
2201
2202 fn compile_unary_op(
2204 builder: &mut FunctionBuilder,
2205 op: UnaryOp,
2206 val: cranelift_codegen::ir::Value,
2207 ) -> Result<cranelift_codegen::ir::Value, String> {
2208 let result = match op {
2209 UnaryOp::Neg => builder.ins().ineg(val),
2210 UnaryOp::Not => {
2211 let zero = builder.ins().iconst(types::I64, 0);
2212 let cmp = builder.ins().icmp(IntCC::Equal, val, zero);
2213 builder.ins().uextend(types::I64, cmp)
2214 }
2215 UnaryOp::Deref | UnaryOp::Ref | UnaryOp::RefMut => val,
2216 };
2217 Ok(result)
2218 }
2219
2220 fn compile_call(
2222 module: &mut JITModule,
2223 functions: &HashMap<String, FuncId>,
2224 extern_fns: &HashMap<String, ExternFnSig>,
2225 builder: &mut FunctionBuilder,
2226 name: &str,
2227 args: &[cranelift_codegen::ir::Value],
2228 ) -> Result<cranelift_codegen::ir::Value, String> {
2229 let builtin_name = match name {
2230 "sqrt" => Some("sigil_sqrt"),
2231 "sin" => Some("sigil_sin"),
2232 "cos" => Some("sigil_cos"),
2233 "pow" => Some("sigil_pow"),
2234 "exp" => Some("sigil_exp"),
2235 "ln" => Some("sigil_ln"),
2236 "floor" => Some("sigil_floor"),
2237 "ceil" => Some("sigil_ceil"),
2238 "abs" => Some("sigil_abs"),
2239 "print" => Some("sigil_print"),
2240 "now" => Some("sigil_now"),
2241 "ackermann" => Some("sigil_ackermann"),
2243 "tak" => Some("sigil_tak"),
2244 n if n.starts_with("sigil_") => Some(n),
2245 _ => None,
2246 };
2247
2248 if let Some(builtin) = builtin_name {
2249 let mut sig = module.make_signature();
2250
2251 match builtin {
2252 "sigil_sqrt" | "sigil_sin" | "sigil_cos" | "sigil_exp" | "sigil_ln"
2253 | "sigil_floor" | "sigil_ceil" | "sigil_abs" => {
2254 sig.params.push(AbiParam::new(types::F64));
2255 sig.returns.push(AbiParam::new(types::F64));
2256 }
2257 "sigil_pow" => {
2258 sig.params.push(AbiParam::new(types::F64));
2259 sig.params.push(AbiParam::new(types::F64));
2260 sig.returns.push(AbiParam::new(types::F64));
2261 }
2262 "sigil_print_int" => {
2263 sig.params.push(AbiParam::new(types::I64));
2264 sig.returns.push(AbiParam::new(types::I64));
2265 }
2266 "sigil_now" => {
2267 sig.returns.push(AbiParam::new(types::I64));
2268 }
2269 "sigil_array_new" => {
2270 sig.params.push(AbiParam::new(types::I64));
2271 sig.returns.push(AbiParam::new(types::I64));
2272 }
2273 "sigil_array_get" | "sigil_array_set" => {
2274 sig.params.push(AbiParam::new(types::I64));
2275 sig.params.push(AbiParam::new(types::I64));
2276 if builtin == "sigil_array_set" {
2277 sig.params.push(AbiParam::new(types::I64));
2278 }
2279 sig.returns.push(AbiParam::new(types::I64));
2280 }
2281 "sigil_array_len" => {
2282 sig.params.push(AbiParam::new(types::I64));
2283 sig.returns.push(AbiParam::new(types::I64));
2284 }
2285 "sigil_array_first"
2287 | "sigil_array_last"
2288 | "sigil_array_middle"
2289 | "sigil_array_choice"
2290 | "sigil_array_next"
2291 | "sigil_array_sum"
2292 | "sigil_array_product" => {
2293 sig.params.push(AbiParam::new(types::I64));
2294 sig.returns.push(AbiParam::new(types::I64));
2295 }
2296 "sigil_array_sort" => {
2298 sig.params.push(AbiParam::new(types::I64)); sig.returns.push(AbiParam::new(types::I64)); }
2301 "sigil_parallel_map" | "sigil_parallel_filter" => {
2303 sig.params.push(AbiParam::new(types::I64)); sig.returns.push(AbiParam::new(types::I64)); }
2306 "sigil_parallel_reduce" => {
2307 sig.params.push(AbiParam::new(types::I64)); sig.returns.push(AbiParam::new(types::I64)); }
2310 "sigil_gpu_map" | "sigil_gpu_filter" => {
2312 sig.params.push(AbiParam::new(types::I64)); sig.returns.push(AbiParam::new(types::I64)); }
2315 "sigil_gpu_reduce" => {
2316 sig.params.push(AbiParam::new(types::I64)); sig.returns.push(AbiParam::new(types::I64)); }
2319 "sigil_array_nth" => {
2321 sig.params.push(AbiParam::new(types::I64)); sig.params.push(AbiParam::new(types::I64)); sig.returns.push(AbiParam::new(types::I64));
2324 }
2325 _ => {
2326 for _ in args {
2327 sig.params.push(AbiParam::new(types::I64));
2328 }
2329 sig.returns.push(AbiParam::new(types::I64));
2330 }
2331 }
2332
2333 let callee = module
2334 .declare_function(builtin, Linkage::Import, &sig)
2335 .map_err(|e| e.to_string())?;
2336
2337 let local_callee = module.declare_func_in_func(callee, builder.func);
2338
2339 let call_args: Vec<_> = if matches!(
2340 builtin,
2341 "sigil_sqrt"
2342 | "sigil_sin"
2343 | "sigil_cos"
2344 | "sigil_exp"
2345 | "sigil_ln"
2346 | "sigil_floor"
2347 | "sigil_ceil"
2348 | "sigil_abs"
2349 | "sigil_pow"
2350 ) {
2351 args.iter()
2352 .map(|&v| {
2353 if builder.func.dfg.value_type(v) == types::F64 {
2354 v
2355 } else {
2356 builder.ins().fcvt_from_sint(types::F64, v)
2357 }
2358 })
2359 .collect()
2360 } else {
2361 args.to_vec()
2362 };
2363
2364 let call = builder.ins().call(local_callee, &call_args);
2365 Ok(builder.inst_results(call)[0])
2366 } else if let Some(&func_id) = functions.get(name) {
2367 let local_callee = module.declare_func_in_func(func_id, builder.func);
2369 let call = builder.ins().call(local_callee, args);
2370 Ok(builder.inst_results(call)[0])
2371 } else if let Some(extern_fn) = extern_fns.get(name) {
2372 let local_callee = module.declare_func_in_func(extern_fn.func_id, builder.func);
2374
2375 let mut call_args = Vec::new();
2377 for (i, &arg) in args.iter().enumerate() {
2378 let arg_type = builder.func.dfg.value_type(arg);
2379 let expected_type = extern_fn.params.get(i).copied().unwrap_or(types::I64);
2380
2381 let converted = if arg_type == expected_type {
2382 arg
2383 } else if arg_type == types::I64 && expected_type == types::I32 {
2384 builder.ins().ireduce(types::I32, arg)
2385 } else if arg_type == types::I32 && expected_type == types::I64 {
2386 builder.ins().sextend(types::I64, arg)
2387 } else if arg_type == types::I64 && expected_type == types::F64 {
2388 builder.ins().fcvt_from_sint(types::F64, arg)
2389 } else if arg_type == types::F64 && expected_type == types::I64 {
2390 builder.ins().fcvt_to_sint(types::I64, arg)
2391 } else {
2392 arg };
2394 call_args.push(converted);
2395 }
2396
2397 let call = builder.ins().call(local_callee, &call_args);
2398
2399 if extern_fn.returns.is_some() {
2401 let result = builder.inst_results(call)[0];
2402 let result_type = builder.func.dfg.value_type(result);
2403 if result_type == types::I32
2405 || result_type == types::I16
2406 || result_type == types::I8
2407 {
2408 Ok(builder.ins().sextend(types::I64, result))
2409 } else {
2410 Ok(result)
2411 }
2412 } else {
2413 Ok(builder.ins().iconst(types::I64, 0))
2415 }
2416 } else {
2417 Err(format!("Unknown function: {}", name))
2418 }
2419 }
2420
2421 fn compile_if_tracked(
2423 module: &mut JITModule,
2424 functions: &HashMap<String, FuncId>,
2425 extern_fns: &HashMap<String, ExternFnSig>,
2426 builder: &mut FunctionBuilder,
2427 scope: &mut CompileScope,
2428 condition: &Expr,
2429 then_branch: &ast::Block,
2430 else_branch: Option<&Expr>,
2431 ) -> Result<(cranelift_codegen::ir::Value, bool), String> {
2432 let cond_bool =
2434 compile_condition(module, functions, extern_fns, builder, scope, condition)?;
2435
2436 let then_block = builder.create_block();
2437 let else_block = builder.create_block();
2438 let merge_block = builder.create_block();
2439
2440 builder.append_block_param(merge_block, types::I64);
2441
2442 builder
2444 .ins()
2445 .brif(cond_bool, then_block, &[], else_block, &[]);
2446
2447 builder.switch_to_block(then_block);
2449 builder.seal_block(then_block);
2450 let mut then_scope = scope.child();
2451 let (then_val, then_returns) = compile_block_tracked(
2452 module,
2453 functions,
2454 extern_fns,
2455 builder,
2456 &mut then_scope,
2457 then_branch,
2458 )?;
2459 if !then_returns {
2461 builder.ins().jump(merge_block, &[then_val]);
2462 }
2463
2464 builder.switch_to_block(else_block);
2466 builder.seal_block(else_block);
2467 let (else_val, else_returns) = if let Some(else_expr) = else_branch {
2468 match else_expr {
2469 Expr::Block(block) => {
2470 let mut else_scope = scope.child();
2471 compile_block_tracked(
2472 module,
2473 functions,
2474 extern_fns,
2475 builder,
2476 &mut else_scope,
2477 block,
2478 )?
2479 }
2480 Expr::If {
2481 condition,
2482 then_branch,
2483 else_branch,
2484 } => compile_if_tracked(
2485 module,
2486 functions,
2487 extern_fns,
2488 builder,
2489 scope,
2490 condition,
2491 then_branch,
2492 else_branch.as_deref(),
2493 )?,
2494 _ => {
2495 let val =
2496 compile_expr(module, functions, extern_fns, builder, scope, else_expr)?;
2497 (val, false)
2498 }
2499 }
2500 } else {
2501 (builder.ins().iconst(types::I64, 0), false)
2502 };
2503 if !else_returns {
2505 builder.ins().jump(merge_block, &[else_val]);
2506 }
2507
2508 let both_return = then_returns && else_returns;
2511
2512 builder.switch_to_block(merge_block);
2513 builder.seal_block(merge_block);
2514
2515 if both_return {
2516 let dummy = builder.ins().iconst(types::I64, 0);
2519 Ok((dummy, true))
2520 } else {
2521 Ok((builder.block_params(merge_block)[0], false))
2522 }
2523 }
2524
2525 fn compile_if(
2527 module: &mut JITModule,
2528 functions: &HashMap<String, FuncId>,
2529 extern_fns: &HashMap<String, ExternFnSig>,
2530 builder: &mut FunctionBuilder,
2531 scope: &mut CompileScope,
2532 condition: &Expr,
2533 then_branch: &ast::Block,
2534 else_branch: Option<&Expr>,
2535 ) -> Result<cranelift_codegen::ir::Value, String> {
2536 compile_if_tracked(
2537 module,
2538 functions,
2539 extern_fns,
2540 builder,
2541 scope,
2542 condition,
2543 then_branch,
2544 else_branch,
2545 )
2546 .map(|(v, _)| v)
2547 }
2548
2549 fn compile_while(
2551 module: &mut JITModule,
2552 functions: &HashMap<String, FuncId>,
2553 extern_fns: &HashMap<String, ExternFnSig>,
2554 builder: &mut FunctionBuilder,
2555 scope: &mut CompileScope,
2556 condition: &Expr,
2557 body: &ast::Block,
2558 ) -> Result<cranelift_codegen::ir::Value, String> {
2559 let header_block = builder.create_block();
2560 let body_block = builder.create_block();
2561 let exit_block = builder.create_block();
2562
2563 builder.ins().jump(header_block, &[]);
2564
2565 builder.switch_to_block(header_block);
2566 let cond_bool =
2568 compile_condition(module, functions, extern_fns, builder, scope, condition)?;
2569 builder
2571 .ins()
2572 .brif(cond_bool, body_block, &[], exit_block, &[]);
2573
2574 builder.switch_to_block(body_block);
2575 builder.seal_block(body_block);
2576 let mut body_scope = scope.child();
2577 compile_block(
2578 module,
2579 functions,
2580 extern_fns,
2581 builder,
2582 &mut body_scope,
2583 body,
2584 )?;
2585 builder.ins().jump(header_block, &[]);
2586
2587 builder.seal_block(header_block);
2588
2589 builder.switch_to_block(exit_block);
2590 builder.seal_block(exit_block);
2591
2592 Ok(builder.ins().iconst(types::I64, 0))
2593 }
2594
2595 #[inline]
2603 fn is_float_pattern(v: i64) -> bool {
2604 let exp = (v >> 52) & 0x7FF;
2605 exp > 0 && exp < 0x7FF && v != 0
2608 }
2609
2610 #[no_mangle]
2611 pub extern "C" fn sigil_add(a: i64, b: i64) -> i64 {
2612 if is_float_pattern(a) || is_float_pattern(b) {
2613 let fa = f64::from_bits(a as u64);
2614 let fb = f64::from_bits(b as u64);
2615 (fa + fb).to_bits() as i64
2616 } else {
2617 a.wrapping_add(b)
2618 }
2619 }
2620
2621 #[no_mangle]
2622 pub extern "C" fn sigil_sub(a: i64, b: i64) -> i64 {
2623 if is_float_pattern(a) || is_float_pattern(b) {
2624 let fa = f64::from_bits(a as u64);
2625 let fb = f64::from_bits(b as u64);
2626 (fa - fb).to_bits() as i64
2627 } else {
2628 a.wrapping_sub(b)
2629 }
2630 }
2631
2632 #[no_mangle]
2633 pub extern "C" fn sigil_mul(a: i64, b: i64) -> i64 {
2634 if is_float_pattern(a) || is_float_pattern(b) {
2635 let fa = f64::from_bits(a as u64);
2636 let fb = f64::from_bits(b as u64);
2637 (fa * fb).to_bits() as i64
2638 } else {
2639 a.wrapping_mul(b)
2640 }
2641 }
2642
2643 #[no_mangle]
2644 pub extern "C" fn sigil_div(a: i64, b: i64) -> i64 {
2645 if is_float_pattern(a) || is_float_pattern(b) {
2646 let fa = f64::from_bits(a as u64);
2647 let fb = f64::from_bits(b as u64);
2648 (fa / fb).to_bits() as i64
2649 } else if b != 0 {
2650 a / b
2651 } else {
2652 0 }
2654 }
2655
2656 #[no_mangle]
2657 pub extern "C" fn sigil_lt(a: i64, b: i64) -> i64 {
2658 if is_float_pattern(a) || is_float_pattern(b) {
2659 let fa = f64::from_bits(a as u64);
2660 let fb = f64::from_bits(b as u64);
2661 if fa < fb {
2662 1
2663 } else {
2664 0
2665 }
2666 } else {
2667 if a < b {
2668 1
2669 } else {
2670 0
2671 }
2672 }
2673 }
2674
2675 #[no_mangle]
2676 pub extern "C" fn sigil_le(a: i64, b: i64) -> i64 {
2677 if is_float_pattern(a) || is_float_pattern(b) {
2678 let fa = f64::from_bits(a as u64);
2679 let fb = f64::from_bits(b as u64);
2680 if fa <= fb {
2681 1
2682 } else {
2683 0
2684 }
2685 } else {
2686 if a <= b {
2687 1
2688 } else {
2689 0
2690 }
2691 }
2692 }
2693
2694 #[no_mangle]
2695 pub extern "C" fn sigil_gt(a: i64, b: i64) -> i64 {
2696 if is_float_pattern(a) || is_float_pattern(b) {
2697 let fa = f64::from_bits(a as u64);
2698 let fb = f64::from_bits(b as u64);
2699 if fa > fb {
2700 1
2701 } else {
2702 0
2703 }
2704 } else {
2705 if a > b {
2706 1
2707 } else {
2708 0
2709 }
2710 }
2711 }
2712
2713 #[no_mangle]
2714 pub extern "C" fn sigil_ge(a: i64, b: i64) -> i64 {
2715 if is_float_pattern(a) || is_float_pattern(b) {
2716 let fa = f64::from_bits(a as u64);
2717 let fb = f64::from_bits(b as u64);
2718 if fa >= fb {
2719 1
2720 } else {
2721 0
2722 }
2723 } else {
2724 if a >= b {
2725 1
2726 } else {
2727 0
2728 }
2729 }
2730 }
2731
2732 #[no_mangle]
2734 pub extern "C" fn sigil_print(v: i64) -> i64 {
2735 if is_float_pattern(v) {
2736 println!("{}", f64::from_bits(v as u64));
2737 } else {
2738 println!("{}", v);
2739 }
2740 0
2741 }
2742
2743 #[repr(C, align(32))]
2755 struct SimdVec4 {
2756 data: [f64; 4],
2757 }
2758
2759 impl SimdVec4 {
2760 #[inline(always)]
2761 fn new(x: f64, y: f64, z: f64, w: f64) -> Box<Self> {
2762 Box::new(SimdVec4 { data: [x, y, z, w] })
2763 }
2764
2765 #[inline(always)]
2766 fn splat(v: f64) -> Box<Self> {
2767 Box::new(SimdVec4 { data: [v, v, v, v] })
2768 }
2769 }
2770
2771 #[no_mangle]
2773 pub extern "C" fn sigil_simd_new(x: i64, y: i64, z: i64, w: i64) -> i64 {
2774 let v = SimdVec4::new(
2775 f64::from_bits(x as u64),
2776 f64::from_bits(y as u64),
2777 f64::from_bits(z as u64),
2778 f64::from_bits(w as u64),
2779 );
2780 Box::into_raw(v) as i64
2781 }
2782
2783 #[no_mangle]
2785 pub extern "C" fn sigil_simd_splat(v: i64) -> i64 {
2786 let f = f64::from_bits(v as u64);
2787 let v = SimdVec4::splat(f);
2788 Box::into_raw(v) as i64
2789 }
2790
2791 #[no_mangle]
2796 #[inline(never)]
2797 pub extern "C" fn sigil_simd_add(a: i64, b: i64) -> i64 {
2798 unsafe {
2799 let a = &*(a as *const SimdVec4);
2800 let b = &*(b as *const SimdVec4);
2801 let mut r = SimdVec4::new(0.0, 0.0, 0.0, 0.0);
2803 r.data[0] = a.data[0] + b.data[0];
2804 r.data[1] = a.data[1] + b.data[1];
2805 r.data[2] = a.data[2] + b.data[2];
2806 r.data[3] = a.data[3] + b.data[3];
2807 Box::into_raw(r) as i64
2808 }
2809 }
2810
2811 #[no_mangle]
2813 #[inline(never)]
2814 pub extern "C" fn sigil_simd_sub(a: i64, b: i64) -> i64 {
2815 unsafe {
2816 let a = &*(a as *const SimdVec4);
2817 let b = &*(b as *const SimdVec4);
2818 let mut r = SimdVec4::new(0.0, 0.0, 0.0, 0.0);
2819 r.data[0] = a.data[0] - b.data[0];
2820 r.data[1] = a.data[1] - b.data[1];
2821 r.data[2] = a.data[2] - b.data[2];
2822 r.data[3] = a.data[3] - b.data[3];
2823 Box::into_raw(r) as i64
2824 }
2825 }
2826
2827 #[no_mangle]
2829 #[inline(never)]
2830 pub extern "C" fn sigil_simd_mul(a: i64, b: i64) -> i64 {
2831 unsafe {
2832 let a = &*(a as *const SimdVec4);
2833 let b = &*(b as *const SimdVec4);
2834 let mut r = SimdVec4::new(0.0, 0.0, 0.0, 0.0);
2835 r.data[0] = a.data[0] * b.data[0];
2836 r.data[1] = a.data[1] * b.data[1];
2837 r.data[2] = a.data[2] * b.data[2];
2838 r.data[3] = a.data[3] * b.data[3];
2839 Box::into_raw(r) as i64
2840 }
2841 }
2842
2843 #[no_mangle]
2845 #[inline(never)]
2846 pub extern "C" fn sigil_simd_div(a: i64, b: i64) -> i64 {
2847 unsafe {
2848 let a = &*(a as *const SimdVec4);
2849 let b = &*(b as *const SimdVec4);
2850 let mut r = SimdVec4::new(0.0, 0.0, 0.0, 0.0);
2851 r.data[0] = a.data[0] / b.data[0];
2852 r.data[1] = a.data[1] / b.data[1];
2853 r.data[2] = a.data[2] / b.data[2];
2854 r.data[3] = a.data[3] / b.data[3];
2855 Box::into_raw(r) as i64
2856 }
2857 }
2858
2859 #[no_mangle]
2861 #[inline(never)]
2862 pub extern "C" fn sigil_simd_dot(a: i64, b: i64) -> i64 {
2863 unsafe {
2864 let a = &*(a as *const SimdVec4);
2865 let b = &*(b as *const SimdVec4);
2866 let r = a.data[0].mul_add(
2868 b.data[0],
2869 a.data[1].mul_add(
2870 b.data[1],
2871 a.data[2].mul_add(b.data[2], a.data[3] * b.data[3]),
2872 ),
2873 );
2874 r.to_bits() as i64
2875 }
2876 }
2877
2878 #[no_mangle]
2880 #[inline(never)]
2881 pub extern "C" fn sigil_simd_hadd(a: i64) -> i64 {
2882 unsafe {
2883 let a = &*(a as *const SimdVec4);
2884 let sum01 = a.data[0] + a.data[1];
2886 let sum23 = a.data[2] + a.data[3];
2887 let r = sum01 + sum23;
2888 r.to_bits() as i64
2889 }
2890 }
2891
2892 #[no_mangle]
2894 #[inline(never)]
2895 pub extern "C" fn sigil_simd_length_sq(a: i64) -> i64 {
2896 unsafe {
2897 let a = &*(a as *const SimdVec4);
2898 let r = a.data[0].mul_add(
2899 a.data[0],
2900 a.data[1].mul_add(
2901 a.data[1],
2902 a.data[2].mul_add(a.data[2], a.data[3] * a.data[3]),
2903 ),
2904 );
2905 r.to_bits() as i64
2906 }
2907 }
2908
2909 #[no_mangle]
2911 #[inline(never)]
2912 pub extern "C" fn sigil_simd_length(a: i64) -> i64 {
2913 unsafe {
2914 let a = &*(a as *const SimdVec4);
2915 let len_sq = a.data[0].mul_add(
2916 a.data[0],
2917 a.data[1].mul_add(
2918 a.data[1],
2919 a.data[2].mul_add(a.data[2], a.data[3] * a.data[3]),
2920 ),
2921 );
2922 let r = len_sq.sqrt();
2923 r.to_bits() as i64
2924 }
2925 }
2926
2927 #[no_mangle]
2929 #[inline(never)]
2930 pub extern "C" fn sigil_simd_normalize(a: i64) -> i64 {
2931 unsafe {
2932 let a = &*(a as *const SimdVec4);
2933 let len_sq = a.data[0].mul_add(
2934 a.data[0],
2935 a.data[1].mul_add(
2936 a.data[1],
2937 a.data[2].mul_add(a.data[2], a.data[3] * a.data[3]),
2938 ),
2939 );
2940 let inv = if len_sq > 1e-20 {
2941 1.0 / len_sq.sqrt()
2942 } else {
2943 0.0
2944 };
2945 let mut r = SimdVec4::new(0.0, 0.0, 0.0, 0.0);
2946 r.data[0] = a.data[0] * inv;
2947 r.data[1] = a.data[1] * inv;
2948 r.data[2] = a.data[2] * inv;
2949 r.data[3] = a.data[3] * inv;
2950 Box::into_raw(r) as i64
2951 }
2952 }
2953
2954 #[no_mangle]
2956 #[inline(never)]
2957 pub extern "C" fn sigil_simd_cross(a: i64, b: i64) -> i64 {
2958 unsafe {
2959 let a = &*(a as *const SimdVec4);
2960 let b = &*(b as *const SimdVec4);
2961 let mut r = SimdVec4::new(0.0, 0.0, 0.0, 0.0);
2963 r.data[0] = a.data[1].mul_add(b.data[2], -(a.data[2] * b.data[1]));
2964 r.data[1] = a.data[2].mul_add(b.data[0], -(a.data[0] * b.data[2]));
2965 r.data[2] = a.data[0].mul_add(b.data[1], -(a.data[1] * b.data[0]));
2966 r.data[3] = 0.0;
2967 Box::into_raw(r) as i64
2968 }
2969 }
2970
2971 #[no_mangle]
2973 #[inline(never)]
2974 pub extern "C" fn sigil_simd_min(a: i64, b: i64) -> i64 {
2975 unsafe {
2976 let a = &*(a as *const SimdVec4);
2977 let b = &*(b as *const SimdVec4);
2978 let mut r = SimdVec4::new(0.0, 0.0, 0.0, 0.0);
2979 r.data[0] = a.data[0].min(b.data[0]);
2980 r.data[1] = a.data[1].min(b.data[1]);
2981 r.data[2] = a.data[2].min(b.data[2]);
2982 r.data[3] = a.data[3].min(b.data[3]);
2983 Box::into_raw(r) as i64
2984 }
2985 }
2986
2987 #[no_mangle]
2989 #[inline(never)]
2990 pub extern "C" fn sigil_simd_max(a: i64, b: i64) -> i64 {
2991 unsafe {
2992 let a = &*(a as *const SimdVec4);
2993 let b = &*(b as *const SimdVec4);
2994 let mut r = SimdVec4::new(0.0, 0.0, 0.0, 0.0);
2995 r.data[0] = a.data[0].max(b.data[0]);
2996 r.data[1] = a.data[1].max(b.data[1]);
2997 r.data[2] = a.data[2].max(b.data[2]);
2998 r.data[3] = a.data[3].max(b.data[3]);
2999 Box::into_raw(r) as i64
3000 }
3001 }
3002
3003 #[no_mangle]
3005 pub extern "C" fn sigil_simd_extract(v: i64, idx: i64) -> i64 {
3006 unsafe {
3007 let v = &*(v as *const SimdVec4);
3008 let r = v.data[(idx as usize) & 3];
3009 r.to_bits() as i64
3010 }
3011 }
3012
3013 #[no_mangle]
3015 pub extern "C" fn sigil_simd_free(v: i64) {
3016 if v != 0 {
3017 unsafe {
3018 let _ = Box::from_raw(v as *mut SimdVec4);
3019 }
3020 }
3021 }
3022
3023 #[no_mangle]
3024 pub extern "C" fn sigil_sqrt(x: f64) -> f64 {
3025 x.sqrt()
3026 }
3027
3028 #[no_mangle]
3029 pub extern "C" fn sigil_sin(x: f64) -> f64 {
3030 x.sin()
3031 }
3032
3033 #[no_mangle]
3034 pub extern "C" fn sigil_cos(x: f64) -> f64 {
3035 x.cos()
3036 }
3037
3038 #[no_mangle]
3039 pub extern "C" fn sigil_pow(base: f64, exp: f64) -> f64 {
3040 base.powf(exp)
3041 }
3042
3043 #[no_mangle]
3044 pub extern "C" fn sigil_exp(x: f64) -> f64 {
3045 x.exp()
3046 }
3047
3048 #[no_mangle]
3049 pub extern "C" fn sigil_ln(x: f64) -> f64 {
3050 x.ln()
3051 }
3052
3053 #[no_mangle]
3054 pub extern "C" fn sigil_floor(x: f64) -> f64 {
3055 x.floor()
3056 }
3057
3058 #[no_mangle]
3059 pub extern "C" fn sigil_ceil(x: f64) -> f64 {
3060 x.ceil()
3061 }
3062
3063 #[no_mangle]
3064 pub extern "C" fn sigil_abs(x: f64) -> f64 {
3065 x.abs()
3066 }
3067
3068 #[no_mangle]
3069 pub extern "C" fn sigil_print_int(x: i64) -> i64 {
3070 println!("{}", x);
3071 0
3072 }
3073
3074 #[no_mangle]
3075 pub extern "C" fn sigil_print_float(x: f64) -> i64 {
3076 println!("{}", x);
3077 0
3078 }
3079
3080 #[no_mangle]
3081 pub extern "C" fn sigil_print_str(ptr: *const u8, len: usize) -> i64 {
3082 unsafe {
3083 let slice = std::slice::from_raw_parts(ptr, len);
3084 if let Ok(s) = std::str::from_utf8(slice) {
3085 println!("{}", s);
3086 }
3087 }
3088 0
3089 }
3090
3091 #[no_mangle]
3092 pub extern "C" fn sigil_now() -> i64 {
3093 use std::time::{SystemTime, UNIX_EPOCH};
3094 SystemTime::now()
3095 .duration_since(UNIX_EPOCH)
3096 .map(|d| d.as_millis() as i64)
3097 .unwrap_or(0)
3098 }
3099
3100 #[repr(C)]
3102 struct SigilArray {
3103 data: *mut i64,
3104 len: usize,
3105 cap: usize,
3106 }
3107
3108 #[no_mangle]
3109 pub extern "C" fn sigil_array_new(capacity: i64) -> i64 {
3110 let cap = capacity.max(8) as usize;
3111 let layout = std::alloc::Layout::array::<i64>(cap).unwrap();
3112 let data = unsafe { std::alloc::alloc(layout) as *mut i64 };
3113
3114 let arr = Box::new(SigilArray { data, len: 0, cap });
3115 Box::into_raw(arr) as i64
3116 }
3117
3118 #[no_mangle]
3119 pub extern "C" fn sigil_array_push(arr_ptr: i64, value: i64) -> i64 {
3120 unsafe {
3121 let arr = &mut *(arr_ptr as *mut SigilArray);
3122 if arr.len >= arr.cap {
3123 let new_cap = arr.cap * 2;
3125 let old_layout = std::alloc::Layout::array::<i64>(arr.cap).unwrap();
3126 let new_layout = std::alloc::Layout::array::<i64>(new_cap).unwrap();
3127 arr.data = std::alloc::realloc(arr.data as *mut u8, old_layout, new_layout.size())
3128 as *mut i64;
3129 arr.cap = new_cap;
3130 }
3131 *arr.data.add(arr.len) = value;
3132 arr.len += 1;
3133 }
3134 0
3135 }
3136
3137 #[no_mangle]
3138 pub extern "C" fn sigil_array_get(arr_ptr: i64, index: i64) -> i64 {
3139 unsafe {
3140 let arr = &*(arr_ptr as *const SigilArray);
3141 let idx = index as usize;
3142 if idx < arr.len {
3143 *arr.data.add(idx)
3144 } else {
3145 0 }
3147 }
3148 }
3149
3150 #[no_mangle]
3151 pub extern "C" fn sigil_array_set(arr_ptr: i64, index: i64, value: i64) -> i64 {
3152 unsafe {
3153 let arr = &mut *(arr_ptr as *mut SigilArray);
3154 let idx = index as usize;
3155 while arr.len <= idx {
3157 sigil_array_push(arr_ptr, 0);
3158 }
3159 *arr.data.add(idx) = value;
3160 }
3161 value
3162 }
3163
3164 #[no_mangle]
3165 pub extern "C" fn sigil_array_len(arr_ptr: i64) -> i64 {
3166 unsafe {
3167 let arr = &*(arr_ptr as *const SigilArray);
3168 arr.len as i64
3169 }
3170 }
3171
3172 #[no_mangle]
3179 pub extern "C" fn sigil_array_sum(arr_ptr: i64) -> i64 {
3180 unsafe {
3181 let arr = &*(arr_ptr as *const SigilArray);
3182 let data = std::slice::from_raw_parts(arr.data, arr.len);
3183
3184 let chunks = data.chunks_exact(4);
3186 let remainder = chunks.remainder();
3187
3188 let mut sum0: i64 = 0;
3190 let mut sum1: i64 = 0;
3191 let mut sum2: i64 = 0;
3192 let mut sum3: i64 = 0;
3193
3194 for chunk in chunks {
3195 sum0 = sum0.wrapping_add(chunk[0]);
3196 sum1 = sum1.wrapping_add(chunk[1]);
3197 sum2 = sum2.wrapping_add(chunk[2]);
3198 sum3 = sum3.wrapping_add(chunk[3]);
3199 }
3200
3201 let mut sum = sum0
3203 .wrapping_add(sum1)
3204 .wrapping_add(sum2)
3205 .wrapping_add(sum3);
3206 for &v in remainder {
3207 sum = sum.wrapping_add(v);
3208 }
3209
3210 sum
3211 }
3212 }
3213
3214 #[no_mangle]
3216 pub extern "C" fn sigil_array_scale(arr_ptr: i64, scalar: i64) -> i64 {
3217 unsafe {
3218 let arr = &mut *(arr_ptr as *mut SigilArray);
3219 let data = std::slice::from_raw_parts_mut(arr.data, arr.len);
3220
3221 for chunk in data.chunks_exact_mut(4) {
3223 chunk[0] = chunk[0].wrapping_mul(scalar);
3224 chunk[1] = chunk[1].wrapping_mul(scalar);
3225 chunk[2] = chunk[2].wrapping_mul(scalar);
3226 chunk[3] = chunk[3].wrapping_mul(scalar);
3227 }
3228
3229 let remainder_start = (data.len() / 4) * 4;
3231 for v in &mut data[remainder_start..] {
3232 *v = v.wrapping_mul(scalar);
3233 }
3234
3235 arr_ptr
3236 }
3237 }
3238
3239 #[no_mangle]
3241 pub extern "C" fn sigil_array_offset(arr_ptr: i64, offset: i64) -> i64 {
3242 unsafe {
3243 let arr = &mut *(arr_ptr as *mut SigilArray);
3244 let data = std::slice::from_raw_parts_mut(arr.data, arr.len);
3245
3246 for chunk in data.chunks_exact_mut(4) {
3248 chunk[0] = chunk[0].wrapping_add(offset);
3249 chunk[1] = chunk[1].wrapping_add(offset);
3250 chunk[2] = chunk[2].wrapping_add(offset);
3251 chunk[3] = chunk[3].wrapping_add(offset);
3252 }
3253
3254 let remainder_start = (data.len() / 4) * 4;
3255 for v in &mut data[remainder_start..] {
3256 *v = v.wrapping_add(offset);
3257 }
3258
3259 arr_ptr
3260 }
3261 }
3262
3263 #[no_mangle]
3265 pub extern "C" fn sigil_array_dot(a_ptr: i64, b_ptr: i64) -> i64 {
3266 unsafe {
3267 let a_arr = &*(a_ptr as *const SigilArray);
3268 let b_arr = &*(b_ptr as *const SigilArray);
3269
3270 let len = a_arr.len.min(b_arr.len);
3271 let a_data = std::slice::from_raw_parts(a_arr.data, len);
3272 let b_data = std::slice::from_raw_parts(b_arr.data, len);
3273
3274 let mut sum0: i64 = 0;
3276 let mut sum1: i64 = 0;
3277 let mut sum2: i64 = 0;
3278 let mut sum3: i64 = 0;
3279
3280 let chunks = len / 4;
3281 for i in 0..chunks {
3282 let base = i * 4;
3283 sum0 = sum0.wrapping_add(a_data[base].wrapping_mul(b_data[base]));
3284 sum1 = sum1.wrapping_add(a_data[base + 1].wrapping_mul(b_data[base + 1]));
3285 sum2 = sum2.wrapping_add(a_data[base + 2].wrapping_mul(b_data[base + 2]));
3286 sum3 = sum3.wrapping_add(a_data[base + 3].wrapping_mul(b_data[base + 3]));
3287 }
3288
3289 let mut sum = sum0
3291 .wrapping_add(sum1)
3292 .wrapping_add(sum2)
3293 .wrapping_add(sum3);
3294 for i in (chunks * 4)..len {
3295 sum = sum.wrapping_add(a_data[i].wrapping_mul(b_data[i]));
3296 }
3297
3298 sum
3299 }
3300 }
3301
3302 #[no_mangle]
3304 pub extern "C" fn sigil_array_add(a_ptr: i64, b_ptr: i64) -> i64 {
3305 unsafe {
3306 let a_arr = &*(a_ptr as *const SigilArray);
3307 let b_arr = &*(b_ptr as *const SigilArray);
3308
3309 let len = a_arr.len.min(b_arr.len);
3310 let a_data = std::slice::from_raw_parts(a_arr.data, len);
3311 let b_data = std::slice::from_raw_parts(b_arr.data, len);
3312
3313 let result = sigil_array_new(len as i64);
3315 let r_arr = &mut *(result as *mut SigilArray);
3316 r_arr.len = len;
3317 let r_data = std::slice::from_raw_parts_mut(r_arr.data, len);
3318
3319 for i in 0..(len / 4) {
3321 let base = i * 4;
3322 r_data[base] = a_data[base].wrapping_add(b_data[base]);
3323 r_data[base + 1] = a_data[base + 1].wrapping_add(b_data[base + 1]);
3324 r_data[base + 2] = a_data[base + 2].wrapping_add(b_data[base + 2]);
3325 r_data[base + 3] = a_data[base + 3].wrapping_add(b_data[base + 3]);
3326 }
3327
3328 for i in ((len / 4) * 4)..len {
3330 r_data[i] = a_data[i].wrapping_add(b_data[i]);
3331 }
3332
3333 result
3334 }
3335 }
3336
3337 #[no_mangle]
3339 pub extern "C" fn sigil_array_mul(a_ptr: i64, b_ptr: i64) -> i64 {
3340 unsafe {
3341 let a_arr = &*(a_ptr as *const SigilArray);
3342 let b_arr = &*(b_ptr as *const SigilArray);
3343
3344 let len = a_arr.len.min(b_arr.len);
3345 let a_data = std::slice::from_raw_parts(a_arr.data, len);
3346 let b_data = std::slice::from_raw_parts(b_arr.data, len);
3347
3348 let result = sigil_array_new(len as i64);
3350 let r_arr = &mut *(result as *mut SigilArray);
3351 r_arr.len = len;
3352 let r_data = std::slice::from_raw_parts_mut(r_arr.data, len);
3353
3354 for i in 0..(len / 4) {
3356 let base = i * 4;
3357 r_data[base] = a_data[base].wrapping_mul(b_data[base]);
3358 r_data[base + 1] = a_data[base + 1].wrapping_mul(b_data[base + 1]);
3359 r_data[base + 2] = a_data[base + 2].wrapping_mul(b_data[base + 2]);
3360 r_data[base + 3] = a_data[base + 3].wrapping_mul(b_data[base + 3]);
3361 }
3362
3363 for i in ((len / 4) * 4)..len {
3365 r_data[i] = a_data[i].wrapping_mul(b_data[i]);
3366 }
3367
3368 result
3369 }
3370 }
3371
3372 #[no_mangle]
3374 pub extern "C" fn sigil_array_min(arr_ptr: i64) -> i64 {
3375 unsafe {
3376 let arr = &*(arr_ptr as *const SigilArray);
3377 if arr.len == 0 {
3378 return 0;
3379 }
3380
3381 let data = std::slice::from_raw_parts(arr.data, arr.len);
3382
3383 let mut min0 = i64::MAX;
3385 let mut min1 = i64::MAX;
3386 let mut min2 = i64::MAX;
3387 let mut min3 = i64::MAX;
3388
3389 for chunk in data.chunks_exact(4) {
3390 min0 = min0.min(chunk[0]);
3391 min1 = min1.min(chunk[1]);
3392 min2 = min2.min(chunk[2]);
3393 min3 = min3.min(chunk[3]);
3394 }
3395
3396 let mut min_val = min0.min(min1).min(min2).min(min3);
3397
3398 let remainder_start = (data.len() / 4) * 4;
3400 for &v in &data[remainder_start..] {
3401 min_val = min_val.min(v);
3402 }
3403
3404 min_val
3405 }
3406 }
3407
3408 #[no_mangle]
3410 pub extern "C" fn sigil_array_max(arr_ptr: i64) -> i64 {
3411 unsafe {
3412 let arr = &*(arr_ptr as *const SigilArray);
3413 if arr.len == 0 {
3414 return 0;
3415 }
3416
3417 let data = std::slice::from_raw_parts(arr.data, arr.len);
3418
3419 let mut max0 = i64::MIN;
3421 let mut max1 = i64::MIN;
3422 let mut max2 = i64::MIN;
3423 let mut max3 = i64::MIN;
3424
3425 for chunk in data.chunks_exact(4) {
3426 max0 = max0.max(chunk[0]);
3427 max1 = max1.max(chunk[1]);
3428 max2 = max2.max(chunk[2]);
3429 max3 = max3.max(chunk[3]);
3430 }
3431
3432 let mut max_val = max0.max(max1).max(max2).max(max3);
3433
3434 let remainder_start = (data.len() / 4) * 4;
3436 for &v in &data[remainder_start..] {
3437 max_val = max_val.max(v);
3438 }
3439
3440 max_val
3441 }
3442 }
3443
3444 #[no_mangle]
3446 pub extern "C" fn sigil_array_fill(arr_ptr: i64, value: i64, count: i64) -> i64 {
3447 unsafe {
3448 let arr = &mut *(arr_ptr as *mut SigilArray);
3449 let n = count as usize;
3450
3451 while arr.len < n {
3453 sigil_array_push(arr_ptr, 0);
3454 }
3455
3456 let data = std::slice::from_raw_parts_mut(arr.data, n);
3457
3458 for chunk in data.chunks_exact_mut(4) {
3460 chunk[0] = value;
3461 chunk[1] = value;
3462 chunk[2] = value;
3463 chunk[3] = value;
3464 }
3465
3466 let remainder_start = (n / 4) * 4;
3468 for v in &mut data[remainder_start..] {
3469 *v = value;
3470 }
3471
3472 arr_ptr
3473 }
3474 }
3475
3476 #[no_mangle]
3483 pub extern "C" fn sigil_array_first(arr_ptr: i64) -> i64 {
3484 unsafe {
3485 let arr = &*(arr_ptr as *const SigilArray);
3486 if arr.len == 0 {
3487 return 0; }
3489 *arr.data
3490 }
3491 }
3492
3493 #[no_mangle]
3495 pub extern "C" fn sigil_array_last(arr_ptr: i64) -> i64 {
3496 unsafe {
3497 let arr = &*(arr_ptr as *const SigilArray);
3498 if arr.len == 0 {
3499 return 0; }
3501 *arr.data.add(arr.len - 1)
3502 }
3503 }
3504
3505 #[no_mangle]
3507 pub extern "C" fn sigil_array_middle(arr_ptr: i64) -> i64 {
3508 unsafe {
3509 let arr = &*(arr_ptr as *const SigilArray);
3510 if arr.len == 0 {
3511 return 0; }
3513 let mid = arr.len / 2;
3514 *arr.data.add(mid)
3515 }
3516 }
3517
3518 #[no_mangle]
3520 pub extern "C" fn sigil_array_choice(arr_ptr: i64) -> i64 {
3521 unsafe {
3522 let arr = &*(arr_ptr as *const SigilArray);
3523 if arr.len == 0 {
3524 return 0; }
3526 use std::time::{SystemTime, UNIX_EPOCH};
3528 let seed = SystemTime::now()
3529 .duration_since(UNIX_EPOCH)
3530 .map(|d| d.as_nanos() as u64)
3531 .unwrap_or(12345);
3532 let idx =
3533 ((seed.wrapping_mul(1103515245).wrapping_add(12345)) >> 16) as usize % arr.len;
3534 *arr.data.add(idx)
3535 }
3536 }
3537
3538 #[no_mangle]
3540 pub extern "C" fn sigil_array_nth(arr_ptr: i64, index: i64) -> i64 {
3541 sigil_array_get(arr_ptr, index)
3542 }
3543
3544 #[no_mangle]
3546 pub extern "C" fn sigil_array_next(arr_ptr: i64) -> i64 {
3547 sigil_array_first(arr_ptr)
3550 }
3551
3552 #[no_mangle]
3554 pub extern "C" fn sigil_array_product(arr_ptr: i64) -> i64 {
3555 unsafe {
3556 let arr = &*(arr_ptr as *const SigilArray);
3557 if arr.len == 0 {
3558 return 1; }
3560 let mut product: i64 = 1;
3561 for i in 0..arr.len {
3562 product = product.wrapping_mul(*arr.data.add(i));
3563 }
3564 product
3565 }
3566 }
3567
3568 #[no_mangle]
3570 pub extern "C" fn sigil_array_sort(arr_ptr: i64) -> i64 {
3571 unsafe {
3572 let arr = &*(arr_ptr as *const SigilArray);
3573 if arr.len == 0 {
3574 return sigil_array_new(0);
3575 }
3576
3577 let mut elements: Vec<i64> = Vec::with_capacity(arr.len);
3579 for i in 0..arr.len {
3580 elements.push(*arr.data.add(i));
3581 }
3582
3583 elements.sort();
3585
3586 let new_arr = sigil_array_new(arr.len as i64);
3588 for elem in elements {
3589 sigil_array_push(new_arr, elem);
3590 }
3591 new_arr
3592 }
3593 }
3594
3595 #[no_mangle]
3609 pub extern "C" fn sigil_parallel_map(arr_ptr: i64) -> i64 {
3610 arr_ptr
3613 }
3614
3615 #[no_mangle]
3618 pub extern "C" fn sigil_parallel_filter(arr_ptr: i64) -> i64 {
3619 arr_ptr
3625 }
3626
3627 #[no_mangle]
3630 pub extern "C" fn sigil_parallel_reduce(arr_ptr: i64) -> i64 {
3631 unsafe {
3634 let arr = &*(arr_ptr as *const SigilArray);
3635 if arr.len == 0 {
3636 return 0;
3637 }
3638
3639 let mut sum: i64 = 0;
3642 for i in 0..arr.len {
3643 sum += *arr.data.add(i);
3644 }
3645 sum
3646 }
3647 }
3648
3649 #[no_mangle]
3665 pub extern "C" fn sigil_gpu_map(arr_ptr: i64) -> i64 {
3666 arr_ptr
3673 }
3674
3675 #[no_mangle]
3678 pub extern "C" fn sigil_gpu_filter(arr_ptr: i64) -> i64 {
3679 arr_ptr
3682 }
3683
3684 #[no_mangle]
3687 pub extern "C" fn sigil_gpu_reduce(arr_ptr: i64) -> i64 {
3688 sigil_parallel_reduce(arr_ptr)
3690 }
3691
3692 #[repr(C)]
3699 struct MemoEntry {
3700 key1: i64, key2: i64, value: i64, occupied: bool, }
3705
3706 #[repr(C)]
3708 struct MemoCache {
3709 entries: *mut MemoEntry,
3710 capacity: usize,
3711 mask: usize, }
3713
3714 #[no_mangle]
3716 pub extern "C" fn sigil_memo_new(capacity: i64) -> i64 {
3717 let cap = (capacity as usize).next_power_of_two().max(1024);
3718 let layout = std::alloc::Layout::array::<MemoEntry>(cap).unwrap();
3719 let entries = unsafe {
3720 let ptr = std::alloc::alloc_zeroed(layout) as *mut MemoEntry;
3721 ptr
3722 };
3723
3724 let cache = Box::new(MemoCache {
3725 entries,
3726 capacity: cap,
3727 mask: cap - 1,
3728 });
3729 Box::into_raw(cache) as i64
3730 }
3731
3732 #[inline]
3734 fn memo_hash_1(key: i64) -> usize {
3735 let mut h = key as u64;
3737 h = h.wrapping_mul(0x517cc1b727220a95);
3738 h ^= h >> 32;
3739 h as usize
3740 }
3741
3742 #[inline]
3744 fn memo_hash_2(key1: i64, key2: i64) -> usize {
3745 let mut h = key1 as u64;
3746 h = h.wrapping_mul(0x517cc1b727220a95);
3747 h ^= key2 as u64;
3748 h = h.wrapping_mul(0x517cc1b727220a95);
3749 h ^= h >> 32;
3750 h as usize
3751 }
3752
3753 #[no_mangle]
3761 pub extern "C" fn sigil_ackermann(m: i64, n: i64) -> i64 {
3762 let mut stack: Vec<i64> = Vec::with_capacity(1024);
3764 stack.push(m);
3765 let mut n = n;
3766
3767 while let Some(m) = stack.pop() {
3768 if m == 0 {
3769 n = n + 1;
3770 } else if n == 0 {
3771 stack.push(m - 1);
3772 n = 1;
3773 } else {
3774 stack.push(m - 1);
3775 stack.push(m);
3776 n = n - 1;
3777 }
3778 }
3779 n
3780 }
3781
3782 #[no_mangle]
3784 pub extern "C" fn sigil_tak(x: i64, y: i64, z: i64) -> i64 {
3785 #[derive(Clone, Copy)]
3787 enum TakCont {
3788 Eval { x: i64, y: i64, z: i64 },
3789 Cont1 { y: i64, z: i64, x: i64 }, Cont2 { z: i64, x: i64, y: i64, r1: i64 }, Cont3 { r1: i64, r2: i64 }, }
3793
3794 let mut stack: Vec<TakCont> = Vec::with_capacity(256);
3795 stack.push(TakCont::Eval { x, y, z });
3796 let mut result: i64 = 0;
3797
3798 while let Some(cont) = stack.pop() {
3799 match cont {
3800 TakCont::Eval { x, y, z } => {
3801 if y >= x {
3802 result = z;
3803 } else {
3804 stack.push(TakCont::Cont1 { y, z, x });
3806 stack.push(TakCont::Eval { x: x - 1, y, z });
3807 }
3808 }
3809 TakCont::Cont1 { y, z, x } => {
3810 let r1 = result;
3811 stack.push(TakCont::Cont2 { z, x, y, r1 });
3812 stack.push(TakCont::Eval {
3813 x: y - 1,
3814 y: z,
3815 z: x,
3816 });
3817 }
3818 TakCont::Cont2 { z, x, y, r1 } => {
3819 let r2 = result;
3820 stack.push(TakCont::Cont3 { r1, r2 });
3821 stack.push(TakCont::Eval {
3822 x: z - 1,
3823 y: x,
3824 z: y,
3825 });
3826 }
3827 TakCont::Cont3 { r1, r2 } => {
3828 let r3 = result;
3829 stack.push(TakCont::Eval {
3831 x: r1,
3832 y: r2,
3833 z: r3,
3834 });
3835 }
3836 }
3837 }
3838 result
3839 }
3840
3841 const MEMO_NOT_FOUND: i64 = -9223372036854775807;
3844
3845 #[no_mangle]
3848 pub extern "C" fn sigil_memo_get_1(cache_ptr: i64, key: i64) -> i64 {
3849 unsafe {
3850 let cache = &*(cache_ptr as *const MemoCache);
3851 let mut idx = memo_hash_1(key) & cache.mask;
3852
3853 for _ in 0..32 {
3855 let entry = &*cache.entries.add(idx);
3856 if !entry.occupied {
3857 return MEMO_NOT_FOUND;
3858 }
3859 if entry.key1 == key {
3860 return entry.value;
3861 }
3862 idx = (idx + 1) & cache.mask;
3863 }
3864 MEMO_NOT_FOUND
3865 }
3866 }
3867
3868 #[no_mangle]
3870 pub extern "C" fn sigil_memo_set_1(cache_ptr: i64, key: i64, value: i64) {
3871 unsafe {
3872 let cache = &*(cache_ptr as *const MemoCache);
3873 let mut idx = memo_hash_1(key) & cache.mask;
3874
3875 for _ in 0..32 {
3877 let entry = &mut *cache.entries.add(idx);
3878 if !entry.occupied || entry.key1 == key {
3879 entry.key1 = key;
3880 entry.value = value;
3881 entry.occupied = true;
3882 return;
3883 }
3884 idx = (idx + 1) & cache.mask;
3885 }
3886 let entry = &mut *cache.entries.add(memo_hash_1(key) & cache.mask);
3888 entry.key1 = key;
3889 entry.value = value;
3890 entry.occupied = true;
3891 }
3892 }
3893
3894 #[no_mangle]
3896 pub extern "C" fn sigil_memo_get_2(cache_ptr: i64, key1: i64, key2: i64) -> i64 {
3897 unsafe {
3898 let cache = &*(cache_ptr as *const MemoCache);
3899 let mut idx = memo_hash_2(key1, key2) & cache.mask;
3900
3901 for _ in 0..32 {
3902 let entry = &*cache.entries.add(idx);
3903 if !entry.occupied {
3904 return MEMO_NOT_FOUND;
3905 }
3906 if entry.key1 == key1 && entry.key2 == key2 {
3907 return entry.value;
3908 }
3909 idx = (idx + 1) & cache.mask;
3910 }
3911 MEMO_NOT_FOUND
3912 }
3913 }
3914
3915 #[no_mangle]
3917 pub extern "C" fn sigil_memo_set_2(cache_ptr: i64, key1: i64, key2: i64, value: i64) {
3918 unsafe {
3919 let cache = &*(cache_ptr as *const MemoCache);
3920 let mut idx = memo_hash_2(key1, key2) & cache.mask;
3921
3922 for _ in 0..32 {
3923 let entry = &mut *cache.entries.add(idx);
3924 if !entry.occupied || (entry.key1 == key1 && entry.key2 == key2) {
3925 entry.key1 = key1;
3926 entry.key2 = key2;
3927 entry.value = value;
3928 entry.occupied = true;
3929 return;
3930 }
3931 idx = (idx + 1) & cache.mask;
3932 }
3933 let entry = &mut *cache.entries.add(memo_hash_2(key1, key2) & cache.mask);
3934 entry.key1 = key1;
3935 entry.key2 = key2;
3936 entry.value = value;
3937 entry.occupied = true;
3938 }
3939 }
3940
3941 #[no_mangle]
3943 pub extern "C" fn sigil_memo_free(cache_ptr: i64) {
3944 if cache_ptr != 0 {
3945 unsafe {
3946 let cache = Box::from_raw(cache_ptr as *mut MemoCache);
3947 let layout = std::alloc::Layout::array::<MemoEntry>(cache.capacity).unwrap();
3948 std::alloc::dealloc(cache.entries as *mut u8, layout);
3949 }
3950 }
3951 }
3952
3953 #[cfg(test)]
3958 mod tests {
3959 use super::*;
3960 use crate::parser::Parser;
3961
3962 #[test]
3963 fn test_extern_block_parsing_and_declaration() {
3964 let source = r#"
3965 extern "C" {
3966 fn abs(x: c_int) -> c_int;
3967 fn strlen(s: *const c_char) -> usize;
3968 }
3969
3970 fn main() -> i64 {
3971 42
3972 }
3973 "#;
3974
3975 let mut compiler = JitCompiler::new().unwrap();
3976 let result = compiler.compile(source);
3977 assert!(
3978 result.is_ok(),
3979 "Failed to compile FFI declarations: {:?}",
3980 result
3981 );
3982
3983 assert!(
3985 compiler.extern_functions.contains_key("abs"),
3986 "abs not declared"
3987 );
3988 assert!(
3989 compiler.extern_functions.contains_key("strlen"),
3990 "strlen not declared"
3991 );
3992
3993 let abs_sig = compiler.extern_functions.get("abs").unwrap();
3995 assert_eq!(abs_sig.params.len(), 1);
3996 assert_eq!(abs_sig.params[0], types::I32); assert_eq!(abs_sig.returns, Some(types::I32));
3998
3999 let strlen_sig = compiler.extern_functions.get("strlen").unwrap();
4001 assert_eq!(strlen_sig.params.len(), 1);
4002 assert_eq!(strlen_sig.params[0], types::I64); assert_eq!(strlen_sig.returns, Some(types::I64)); }
4005
4006 #[test]
4007 fn test_extern_variadic_function() {
4008 let source = r#"
4009 extern "C" {
4010 fn printf(fmt: *const c_char, ...) -> c_int;
4011 }
4012
4013 fn main() -> i64 {
4014 0
4015 }
4016 "#;
4017
4018 let mut compiler = JitCompiler::new().unwrap();
4019 let result = compiler.compile(source);
4020 assert!(
4021 result.is_ok(),
4022 "Failed to compile variadic FFI: {:?}",
4023 result
4024 );
4025
4026 let printf_sig = compiler.extern_functions.get("printf").unwrap();
4027 assert!(printf_sig.variadic, "printf should be variadic");
4028 }
4029
4030 #[test]
4031 fn test_extern_c_abi_only() {
4032 let source = r#"
4033 extern "Rust" {
4034 fn some_func(x: i32) -> i32;
4035 }
4036
4037 fn main() -> i64 {
4038 0
4039 }
4040 "#;
4041
4042 let mut compiler = JitCompiler::new().unwrap();
4043 let result = compiler.compile(source);
4044 assert!(result.is_err(), "Should reject non-C ABI");
4045 assert!(result.unwrap_err().contains("Unsupported ABI"));
4046 }
4047
4048 #[test]
4049 fn test_c_type_mapping() {
4050 let test_cases = vec![
4052 ("c_char", types::I8),
4053 ("c_int", types::I32),
4054 ("c_long", types::I64),
4055 ("c_float", types::F32),
4056 ("c_double", types::F64),
4057 ("size_t", types::I64),
4058 ("i32", types::I32),
4059 ("f64", types::F64),
4060 ];
4061
4062 for (type_name, expected_cl_type) in test_cases {
4063 let source = format!(
4064 r#"
4065 extern "C" {{
4066 fn test_func(x: {}) -> {};
4067 }}
4068
4069 fn main() -> i64 {{ 0 }}
4070 "#,
4071 type_name, type_name
4072 );
4073
4074 let mut compiler = JitCompiler::new().unwrap();
4075 let result = compiler.compile(&source);
4076 assert!(
4077 result.is_ok(),
4078 "Failed for type {}: {:?}",
4079 type_name,
4080 result
4081 );
4082
4083 let sig = compiler.extern_functions.get("test_func").unwrap();
4084 assert_eq!(
4085 sig.params[0], expected_cl_type,
4086 "Wrong param type for {}",
4087 type_name
4088 );
4089 assert_eq!(
4090 sig.returns,
4091 Some(expected_cl_type),
4092 "Wrong return type for {}",
4093 type_name
4094 );
4095 }
4096 }
4097 }
4098}
4099
4100#[cfg(feature = "jit")]
4102pub use jit::JitCompiler;