edlc_codegen_cranelift 0.2.16

Cranelift codegen backend for the EDL compiler
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/*
 *     EDLc, a compiler for the EDL programming language.
 *     Copyright (C) 2026  Adrian Paskert
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Affero General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Affero General Public License for more details.
 *
 *     You should have received a copy of the GNU Affero General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

use edlc_core::inline_code;
use edlc_core::prelude::{CompilerError, EdlCompiler, FromFunction};
use cranelift_codegen::ir::condcodes::FloatCC;
use cranelift_codegen::ir::{InstBuilder, MemFlags, types, MemFlagsData};
use crate::compiler::integer_math::*;
use crate::compiler::JIT;
use crate::executor::CraneliftJIT;
use crate::jit_func;

impl<Runtime> JIT<Runtime> {
    pub fn load_f32_math(&mut self, compiler: &mut EdlCompiler) -> Result<(), CompilerError> {
        impl_binop!(self, compiler,
            f32::add from "core::Add" -> fadd; |lhs, rhs| { (lhs + rhs) }
            f32::sub from "core::Sub" -> fsub; |lhs, rhs| { (lhs - rhs) }
            f32::mul from "core::Mul" -> fmul; |lhs, rhs| { (lhs * rhs) }
            f32::div from "core::Div" -> fdiv; |lhs, rhs| { (lhs / rhs) }

            f32::add_assign from "core::AddAssign" -> fadd; |lhs, rhs| { (lhs + rhs) }
            f32::sub_assign from "core::SubAssign" -> fsub; |lhs, rhs| { (lhs - rhs) }
            f32::mul_assign from "core::MulAssign" -> fmul; |lhs, rhs| { (lhs * rhs) }
            f32::div_assign from "core::DivAssign" -> fdiv; |lhs, rhs| { (lhs / rhs) }
        );
        impl_binop!(self, compiler,
            impl ["core::PartialEq"]<f32, f32> for f32 {
                fn partial_eq(builder, lhs, rhs: f32) -> bool {
                    #[allow(unused_braces)]
                    { builder.fcmp(FloatCC::Equal, lhs, rhs) }
                }; { (lhs == rhs) }
            }
            impl ["core::Ord"]<f32, f32> for f32 {
                fn lesser_than(builder, lhs, rhs: f32) -> bool {
                    #[allow(unused_braces)]
                    { builder.fcmp(FloatCC::LessThan, lhs, rhs) }
                }; { (lhs < rhs) }
                fn greater_than(builder, lhs, rhs: f32) -> bool {
                    #[allow(unused_braces)]
                    { builder.fcmp(FloatCC::GreaterThan, lhs, rhs) }
                }; { (lhs > rhs) }
            }
        );
        impl_binop!(self, compiler,
            impl ["core::Unary"]<f32> for f32 {
                fn unary(builder, val) -> f32 {
                    #[allow(unused_braces)]
                    { builder.fneg(val) }
                }; { (-val) }
            }
        );
        impl_binop!(self, compiler,
            impl f32 {
                fn sqrt(builder, val) -> f32 {
                    #[allow(unused_braces)]
                    { builder.sqrt(val) }
                }; { (f32::sqrt(val)) }
            }
            impl f32 {
                fn abs(builder, val) -> f32 {
                    #[allow(unused_braces)]
                    { builder.fabs(val) }
                }; { (f32::abs(val)) }
            }
            impl f32 {
                fn ceil(builder, val) -> f32 {
                    #[allow(unused_braces)]
                    { builder.ceil(val) }
                }; { (f32::ceil(val)) }
            }
            impl f32 {
                fn floor(builder, val) -> f32 {
                    #[allow(unused_braces)]
                    { builder.floor(val) }
                }; { (f32::floor(val)) }
            }
            impl f32 {
                fn trunc(builder, val) -> f32 {
                    #[allow(unused_braces)]
                    { builder.trunc(val) }
                }; { (f32::trunc(val)) }
            }
            impl f32 {
                fn nearest(builder, val) -> f32 {
                    #[allow(unused_braces)]
                    { builder.nearest(val) }
                }; { (f32::round(val)) }
            }
            impl f32 {
                fn as_u32_bits(builder, val) -> u32 {
                    { builder.bitcast(types::I32, MemFlagsData::new(), val) }
                }; { (f32::to_bits(val)) }
            }
            impl f32 {
                fn promote(builder, val) -> f64 {
                    { builder.fpromote(types::F64, val) }
                }; { (val as f64) }
            }
        );
        impl_binop!(self, compiler,
            impl f32 {
                fn from_u32_bits(builder, val: u32) -> f32 {
                    { builder.bitcast(types::F32, MemFlagsData::new(), val) }
                }; { (f32::from_bits(val)) }
            }
        );
        impl_binop!(self, compiler,
            impl f32 {
                fn min(builder, lhs, rhs: f32) -> f32 {
                    { builder.fmin(lhs, rhs) }
                }; { (f32::min(lhs, rhs)) }
            }
            impl f32 {
                fn max(builder, lhs, rhs: f32) -> f32 {
                    { builder.fmax(lhs, rhs) }
                }; { (f32::max(lhs, rhs)) }
            }
            impl f32 {
                fn cpy_sign(builder, dst, src: f32) -> f32 {
                    { builder.fcopysign(dst, src) }
                }; { (f32::copysign(dst, src)) }
            }
        );
        impl_binop!(self, compiler,
            impl f32 {
                fn fma(builder, lhs, rhs: f32, add: f32) -> f32 {
                    { builder.fma(lhs, rhs, add) }
                }; { (f32::mul_add(lhs, rhs, add)) }
            }
        );
        Ok(())
    }

    pub fn load_f64_math(&mut self, compiler: &mut EdlCompiler) -> Result<(), CompilerError> {
        impl_binop!(self, compiler,
            f64::add from "core::Add" -> fadd; |lhs, rhs| { (lhs + rhs) }
            f64::sub from "core::Sub" -> fsub; |lhs, rhs| { (lhs - rhs) }
            f64::mul from "core::Mul" -> fmul; |lhs, rhs| { (lhs * rhs) }
            f64::div from "core::Div" -> fdiv; |lhs, rhs| { (lhs / rhs) }

            f64::add_assign from "core::AddAssign" -> fadd; |lhs, rhs| { (lhs + rhs) }
            f64::sub_assign from "core::SubAssign" -> fsub; |lhs, rhs| { (lhs - rhs) }
            f64::mul_assign from "core::MulAssign" -> fmul; |lhs, rhs| { (lhs * rhs) }
            f64::div_assign from "core::DivAssign" -> fdiv; |lhs, rhs| { (lhs / rhs) }
        );
        impl_binop!(self, compiler,
            impl ["core::PartialEq"]<f64, f64> for f64 {
                fn partial_eq(builder, lhs, rhs: f64) -> bool {
                    { builder.fcmp(FloatCC::Equal, lhs, rhs) }
                }; { (lhs == rhs) }
            }
            impl ["core::Ord"]<f64, f64> for f64 {
                fn lesser_than(builder, lhs, rhs: f64) -> bool {
                    { builder.fcmp(FloatCC::LessThan, lhs, rhs) }
                }; { (lhs < rhs) }
                fn greater_than(builder, lhs, rhs: f64) -> bool {
                    { builder.fcmp(FloatCC::GreaterThan, lhs, rhs) }
                }; { (lhs > rhs) }
            }
        );
        impl_binop!(self, compiler,
            impl ["core::Unary"]<f64> for f64 {
                fn unary(builder, val) -> f64 {
                    { builder.fneg(val) }
                }; { (-val) }
            }
        );
        impl_binop!(self, compiler,
            impl f64 {
                fn sqrt(builder, val) -> f64 {
                    { builder.sqrt(val) }
                }; { (f64::sqrt(val)) }
            }
            impl f64 {
                fn abs(builder, val) -> f64 {
                    { builder.fabs(val) }
                }; { (f64::abs(val)) }
            }
            impl f64 {
                fn ceil(builder, val) -> f64 {
                    { builder.ceil(val) }
                }; { (f64::ceil(val)) }
            }
            impl f64 {
                fn floor(builder, val) -> f64 {
                    { builder.floor(val) }
                }; { (f64::floor(val)) }
            }
            impl f64 {
                fn trunc(builder, val) -> f64 {
                    { builder.trunc(val) }
                }; { (f64::trunc(val)) }
            }
            impl f64 {
                fn nearest(builder, val) -> f64 {
                    { builder.nearest(val) }
                }; { (f64::round(val)) }
            }
            impl f64 {
                fn as_u64_bits(builder, val) -> u64 {
                    { builder.bitcast(types::I64, MemFlagsData::new(), val) }
                }; { (f64::to_bits(val)) }
            }
            impl f64 {
                fn demote(builder, val) -> f32 {
                    { builder.fdemote(types::F32, val) }
                }; { (val as f32) }
            }
        );
        impl_binop!(self, compiler,
            impl f64 {
                fn from_u64_bits(builder, val: u64) -> f64 {
                    { builder.bitcast(types::F64, MemFlagsData::new(), val) }
                }; { (f64::from_bits(val)) }
            }
        );
        impl_binop!(self, compiler,
            impl f64 {
                fn min(builder, lhs, rhs: f64) -> f64 {
                    { builder.fmin(lhs, rhs) }
                }; { (f64::min(lhs, rhs)) }
            }
            impl f64 {
                fn max(builder, lhs, rhs: f64) -> f64 {
                    { builder.fmax(lhs, rhs) }
                }; { (f64::max(lhs, rhs)) }
            }
            impl f64 {
                fn cpy_sign(builder, dst, src: f64) -> f64 {
                    { builder.fcopysign(dst, src) }
                }; { (f64::copysign(dst, src)) }
            }
        );
        impl_binop!(self, compiler,
            impl f64 {
                fn fma(builder, lhs, rhs: f64, add: f64) -> f64 {
                    { builder.fma(lhs, rhs, add) }
                }; { (f64::mul_add(lhs, rhs, add)) }
            }
        );
        Ok(())
    }
}

impl<Runtime: 'static> CraneliftJIT<Runtime> {
    pub fn load_f64_trigonometry(&mut self) -> Result<(), CompilerError> {
        // load trigonometric functions
        let fs = self.compiler.parse_impl(
            inline_code!("<>"),
            inline_code!("f64"),
            [
                inline_code!(r#"
                ?comptime fn sin(self) -> Self"#),

                inline_code!(r#"
                ?comptime fn cos(self) -> Self"#),

                inline_code!(r#"
                ?comptime fn tan(self) -> Self"#),

                inline_code!(r#"
                ?comptime fn asin(self) -> Self"#),

                inline_code!(r#"
                ?comptime fn acos(self) -> Self"#),

                inline_code!(r#"
                ?comptime fn atan(self) -> Self"#),

                inline_code!(r#"
                ?comptime fn sinh(self) -> Self"#),

                inline_code!(r#"
                ?comptime fn cosh(self) -> Self"#),

                inline_code!(r#"
                ?comptime fn tanh(self) -> Self"#),

                inline_code!(r#"
                ?comptime fn asinh(self) -> Self"#),

                inline_code!(r#"
                ?comptime fn acosh(self) -> Self"#),

                inline_code!(r#"
                ?comptime fn atanh(self) -> Self"#),

                inline_code!(r#"
                ?comptime fn exp(self) -> Self"#),

                inline_code!(r#"
                ?comptime fn ln(self) -> Self"#),

                inline_code!(r#"
                ?comptime fn log(self, base: Self) -> Self"#),

                inline_code!(r#"
                ?comptime fn log2(self) -> Self"#),

                inline_code!(r#"
                ?comptime fn log10(self) -> Self"#),

                inline_code!(r#"
                ?comptime fn pow(self, pow: Self) -> Self"#),

                inline_code!(r#"
                ?comptime fn midpoint(self, y: Self) -> Self"#),
            ],
            None,
        )?;

        jit_func!(for ("f64") impl self, fn(fs[0]),
            const fn sin_f64<>(val: f64) -> f64 where; {
                f64::sin(val)
            }
        );
        jit_func!(for ("f64") impl self, fn(fs[1]),
            const fn cos_f64<>(val: f64) -> f64 where; {
                f64::cos(val)
            }
        );
        jit_func!(for ("f64") impl self, fn(fs[2]),
            const fn tan_f64<>(val: f64) -> f64 where; {
                f64::tan(val)
            }
        );

        jit_func!(for ("f64") impl self, fn(fs[3]),
            const fn asin_f64<>(val: f64) -> f64 where; {
                f64::asin(val)
            }
        );
        jit_func!(for ("f64") impl self, fn(fs[4]),
            const fn acos_f64<>(val: f64) -> f64 where; {
                f64::acos(val)
            }
        );
        jit_func!(for ("f64") impl self, fn(fs[5]),
            const fn atan_f64<>(val: f64) -> f64 where; {
                f64::atan(val)
            }
        );

        jit_func!(for ("f64") impl self, fn(fs[6]),
            const fn sinh_f64<>(val: f64) -> f64 where; {
                f64::sinh(val)
            }
        );
        jit_func!(for ("f64") impl self, fn(fs[7]),
            const fn cosh_f64<>(val: f64) -> f64 where; {
                f64::cosh(val)
            }
        );
        jit_func!(for ("f64") impl self, fn(fs[8]),
            const fn tanh_f64<>(val: f64) -> f64 where; {
                f64::tanh(val)
            }
        );

        jit_func!(for ("f64") impl self, fn(fs[9]),
            const fn asinh_f64<>(val: f64) -> f64 where; {
                f64::asinh(val)
            }
        );
        jit_func!(for ("f64") impl self, fn(fs[10]),
            const fn acosh_f64<>(val: f64) -> f64 where; {
                f64::acosh(val)
            }
        );
        jit_func!(for ("f64") impl self, fn(fs[11]),
            const fn atanh_f64<>(val: f64) -> f64 where; {
                f64::atanh(val)
            }
        );

        jit_func!(for ("f64") impl self, fn(fs[12]),
            const fn exp_f64<>(val: f64) -> f64 where; {
                f64::exp(val)
            }
        );
        jit_func!(for ("f64") impl self, fn(fs[13]),
            const fn ln_f64<>(val: f64) -> f64 where; {
                f64::ln(val)
            }
        );
        jit_func!(for ("f64") impl self, fn(fs[14]),
            const fn log_f64<>(val: f64, base: f64) -> f64 where; {
                f64::log(val, base)
            }
        );
        jit_func!(for ("f64") impl self, fn(fs[15]),
            const fn log2_f64<>(val: f64) -> f64 where; {
                f64::log2(val)
            }
        );
        jit_func!(for ("f64") impl self, fn(fs[16]),
            const fn log10_f64<>(val: f64) -> f64 where; {
                f64::log10(val)
            }
        );
        jit_func!(for ("f64") impl self, fn(fs[17]),
            const fn pow_f64<>(val: f64, pow: f64) -> f64 where; {
                f64::powf(val, pow)
            }
        );
        jit_func!(for ("f64") impl self, fn(fs[18]),
            const fn midpoint_f64<>(x: f64, y: f64) -> f64 where; {
                f64::midpoint(x, y)
            }
        );
        Ok(())
    }

    pub fn load_f32_trigonometry(&mut self) -> Result<(), CompilerError> {
        // load trigonometric functions
        let fs = self.compiler.parse_impl(
            inline_code!("<>"),
            inline_code!("f32"),
            [
                inline_code!(r#"
                ?comptime fn sin(self) -> Self"#),

                inline_code!(r#"
                ?comptime fn cos(self) -> Self"#),

                inline_code!(r#"
                ?comptime fn tan(self) -> Self"#),

                inline_code!(r#"
                ?comptime fn asin(self) -> Self"#),

                inline_code!(r#"
                ?comptime fn acos(self) -> Self"#),

                inline_code!(r#"
                ?comptime fn atan(self) -> Self"#),

                inline_code!(r#"
                ?comptime fn sinh(self) -> Self"#),

                inline_code!(r#"
                ?comptime fn cosh(self) -> Self"#),

                inline_code!(r#"
                ?comptime fn tanh(self) -> Self"#),

                inline_code!(r#"
                ?comptime fn asinh(self) -> Self"#),

                inline_code!(r#"
                ?comptime fn acosh(self) -> Self"#),

                inline_code!(r#"
                ?comptime fn atanh(self) -> Self"#),

                inline_code!(r#"
                ?comptime fn exp(self) -> Self"#),

                inline_code!(r#"
                ?comptime fn ln(self) -> Self"#),

                inline_code!(r#"
                ?comptime fn log(self, base: Self) -> Self"#),

                inline_code!(r#"
                ?comptime fn log2(self) -> Self"#),

                inline_code!(r#"
                ?comptime fn log10(self) -> Self"#),

                inline_code!(r#"
                ?comptime fn pow(self, y: Self) -> Self"#),

                inline_code!(r#"
                ?comptime fn midpoint(self, y: Self) -> Self"#),
            ],
            None,
        )?;

        jit_func!(for ("f32") impl self, fn(fs[0]),
            const fn sin_f32<>(val: f32) -> f32 where; {
                f32::sin(val)
            }
        );
        jit_func!(for ("f32") impl self, fn(fs[1]),
            const fn cos_f32<>(val: f32) -> f32 where; {
                f32::cos(val)
            }
        );
        jit_func!(for ("f32") impl self, fn(fs[2]),
            const fn tan_f32<>(val: f32) -> f32 where; {
                f32::tan(val)
            }
        );

        jit_func!(for ("f32") impl self, fn(fs[3]),
            const fn asin_f32<>(val: f32) -> f32 where; {
                f32::asin(val)
            }
        );
        jit_func!(for ("f32") impl self, fn(fs[4]),
            const fn acos_f32<>(val: f32) -> f32 where; {
                f32::acos(val)
            }
        );
        jit_func!(for ("f32") impl self, fn(fs[5]),
            const fn atan_f32<>(val: f32) -> f32 where; {
                f32::atan(val)
            }
        );

        jit_func!(for ("f32") impl self, fn(fs[6]),
            const fn sinh_f32<>(val: f32) -> f32 where; {
                f32::sinh(val)
            }
        );
        jit_func!(for ("f32") impl self, fn(fs[7]),
            const fn cosh_f32<>(val: f32) -> f32 where; {
                f32::cosh(val)
            }
        );
        jit_func!(for ("f32") impl self, fn(fs[8]),
            const fn tanh_f32<>(val: f32) -> f32 where; {
                f32::tanh(val)
            }
        );

        jit_func!(for ("f32") impl self, fn(fs[9]),
            const fn asinh_f32<>(val: f32) -> f32 where; {
                f32::asinh(val)
            }
        );
        jit_func!(for ("f32") impl self, fn(fs[10]),
            const fn acosh_f32<>(val: f32) -> f32 where; {
                f32::acosh(val)
            }
        );
        jit_func!(for ("f32") impl self, fn(fs[11]),
            const fn atanh_f32<>(val: f32) -> f32 where; {
                f32::atanh(val)
            }
        );

        jit_func!(for ("f32") impl self, fn(fs[12]),
            const fn exp_f32<>(val: f32) -> f32 where; {
                f32::exp(val)
            }
        );
        jit_func!(for ("f32") impl self, fn(fs[13]),
            const fn ln_f32<>(val: f32) -> f32 where; {
                f32::ln(val)
            }
        );
        jit_func!(for ("f32") impl self, fn(fs[14]),
            const fn log_f32<>(val: f32, base: f32) -> f32 where; {
                f32::log(val, base)
            }
        );
        jit_func!(for ("f32") impl self, fn(fs[15]),
            const fn log2_f32<>(val: f32) -> f32 where; {
                f32::log2(val)
            }
        );
        jit_func!(for ("f32") impl self, fn(fs[16]),
            const fn log10_f32<>(val: f32) -> f32 where; {
                f32::log10(val)
            }
        );
        jit_func!(for ("f32") impl self, fn(fs[17]),
            const fn pow_f32<>(val: f32, pow: f32) -> f32 where; {
                f32::powf(val, pow)
            }
        );
        jit_func!(for ("f32") impl self, fn(fs[18]),
            const fn midpoint_f32<>(x: f32, y: f32) -> f32 where; {
                f32::midpoint(x, y)
            }
        );
        Ok(())
    }
}