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
/*
 *     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 crate::codegen::{Compilable, FunctionTranslator};
use crate::compiler::JIT;
use crate::prelude::SSARepr;
use cranelift_codegen::ir;
use cranelift_codegen::ir::InstBuilder;
use edlc_core::lexer::SrcPos;
use edlc_core::prelude::mir_expr::mir_as::MirAs;
use edlc_core::prelude::mir_expr::{MirExprId, MirFlowGraph, MirValue};
use edlc_core::prelude::mir_type::MirTypeId;
use edlc_core::prelude::{MirError, MirPhase};
use std::cmp::Ordering;

impl<Runtime> Compilable<Runtime> for MirAs {
    fn compile(
        &self,
        backend: &mut FunctionTranslator<Runtime>,
        phase: &mut MirPhase,
        _cfg: &MirFlowGraph,
        target: &MirValue,
        _expr_id: &MirExprId,
    ) -> Result<(), MirError<JIT<Runtime>>> {
        let reg = &phase.types;
        let input = *backend.layout.get_ty(&self.val).unwrap();
        let output = self.ty;
        let value = backend.layout.load_pod(
            &self.val, &backend.ir_values, &mut backend.builder, &phase.types).unwrap();

        let output: ir::Value = if reg.is_i_type(input) || reg.is_u_type(input) {
            if reg.is_i_type(output) || reg.is_u_type(output) {
                convert_int_2_int(input, output, value, backend, phase)
            } else if reg.is_f_type(output) {
                convert_int_2_float(input, output, value, backend, phase)
            } else if output == reg.char() {
                convert_int_2_char(input, output, value, backend, phase)
            } else {
                return Err(MirError::IllegalConversion {
                    pos: SrcPos::default(),
                    input,
                    output,
                });
            }
        } else if reg.is_f_type(input) {
            if reg.is_i_type(output) || reg.is_u_type(output) {
                convert_float_2_int(input, output, value, backend, phase)
            } else if reg.is_f_type(output) {
                convert_float_2_float(input, output, value, backend, phase)
            } else if output == reg.char() {
                return Err(MirError::IllegalConversion {
                    pos: SrcPos::default(),
                    input,
                    output,
                });
            } else {
                return Err(MirError::IllegalConversion {
                    pos: SrcPos::default(),
                    input,
                    output,
                });
            }
        } else if input == reg.char() {
            if reg.is_i_type(output) || reg.is_u_type(output) {
                convert_char_2_int(input, output, value, backend, phase)
            } else if reg.is_f_type(output) {
                return Err(MirError::IllegalConversion {
                    pos: SrcPos::default(),
                    input,
                    output,
                });
            } else if output == reg.char() {
                value
            } else {
                return Err(MirError::IllegalConversion {
                    pos: SrcPos::default(),
                    input,
                    output,
                });
            }
        } else {
            return Err(MirError::IllegalConversion {
                pos: SrcPos::default(),
                input,
                output,
            });
        };

        backend.layout.store_pod(
            output, target, &mut backend.ir_values, &mut backend.builder, &phase.types);
        Ok(())
    }
}

fn convert_int_2_int<Runtime>(
    input: MirTypeId,
    output: MirTypeId,
    value: ir::Value,
    backend: &mut FunctionTranslator<Runtime>,
    phase: &mut MirPhase,
) -> ir::Value {
    // if input bits > output bits -> ireduce
    // if input bits < output bits -> uextend / sextend
    assert!(phase.types.is_i_type(input) || phase.types.is_u_type(input));
    assert!(phase.types.is_i_type(output) || phase.types.is_u_type(output));

    let ir_out = SSARepr::pod(&output, &phase.types).unwrap();
    match usize::cmp(&phase.types.byte_size(input).unwrap(), &phase.types.byte_size(output).unwrap()) {
        Ordering::Greater => backend.builder.ins().ireduce(ir_out, value),
        Ordering::Less => {
            if phase.types.is_i_type(input) {
                backend.builder.ins().sextend(ir_out, value)
            } else {
                backend.builder.ins().uextend(ir_out, value)
            }
        }
        Ordering::Equal => value,
    }
}

fn convert_int_2_float<Runtime>(
    input: MirTypeId,
    output: MirTypeId,
    value: ir::Value,
    backend: &mut FunctionTranslator<Runtime>,
    phase: &mut MirPhase,
) -> ir::Value {
    // for unsigned integers -> fcvt_from_uint
    // for   signed integers -> fcvt_from_sint
    assert!(phase.types.is_i_type(input) || phase.types.is_u_type(input));
    assert!(phase.types.is_f_type(output));

    let ir_out = SSARepr::pod(&output, &phase.types).unwrap();
    if phase.types.is_u_type(input) {
        backend.builder.ins().fcvt_from_uint(ir_out, value)
    } else {
        backend.builder.ins().fcvt_from_sint(ir_out, value)
    }
}

fn convert_float_2_int<Runtime>(
    input: MirTypeId,
    output: MirTypeId,
    value: ir::Value,
    backend: &mut FunctionTranslator<Runtime>,
    phase: &mut MirPhase,
) -> ir::Value {
    // for unsigned integers -> fcvt_to_uint
    // for   signed integers -> fcvt_to_sint
    assert!(phase.types.is_f_type(input));
    assert!(phase.types.is_i_type(output) || phase.types.is_u_type(output));

    let ir_out = SSARepr::pod(&output, &phase.types).unwrap();
    if phase.types.is_u_type(output) {
        backend.builder.ins().fcvt_to_uint(ir_out, value)
    } else {
        backend.builder.ins().fcvt_to_sint(ir_out, value)
    }
}

fn convert_float_2_float<Runtime>(
    input: MirTypeId,
    output: MirTypeId,
    value: ir::Value,
    backend: &mut FunctionTranslator<Runtime>,
    phase: &mut MirPhase,
) -> ir::Value {
    // if input bits > output bits -> fdemote
    // if input bits < output bits -> fpromote
    assert!(phase.types.is_f_type(input));
    assert!(phase.types.is_f_type(output));

    let ir_out = SSARepr::pod(&output, &phase.types).unwrap();
    match usize::cmp(&phase.types.byte_size(input).unwrap(), &phase.types.byte_size(output).unwrap()) {
        Ordering::Greater => backend.builder.ins().fdemote(ir_out, value),
        Ordering::Less => backend.builder.ins().fpromote(ir_out, value),
        Ordering::Equal => value,
    }
}

fn convert_char_2_int<Runtime>(
    input: MirTypeId,
    output: MirTypeId,
    value: ir::Value,
    backend: &mut FunctionTranslator<Runtime>,
    phase: &mut MirPhase,
) -> ir::Value {
    // chars are naturally represented as 32-bit integer values.
    // thus, convert from ir::types::I32 and treat the rest of the conversion as implicit
    assert_eq!(input, phase.types.char());
    assert!(phase.types.is_i_type(output) || phase.types.is_u_type(output));
    convert_int_zero_extend(input, output, value, backend, phase)
}

fn convert_int_2_char<Runtime>(
    input: MirTypeId,
    output: MirTypeId,
    value: ir::Value,
    backend: &mut FunctionTranslator<Runtime>,
    phase: &mut MirPhase,
) -> ir::Value {
    // chars are naturally represented as 32-bit integer values.
    // thus, convert to ir::types::I32 and treat the rest of the conversion as implicit
    // since char -> int & int -> char conversions are, at heart, just integer conversions, the
    // conversion is commutative
    assert!(phase.types.is_i_type(input) || phase.types.is_u_type(input));
    assert_eq!(output, phase.types.char());
    convert_int_zero_extend(input, output, value, backend, phase)
}

fn convert_int_zero_extend<Runtime>(
    input: MirTypeId,
    output: MirTypeId,
    value: ir::Value,
    backend: &mut FunctionTranslator<Runtime>,
    phase: &mut MirPhase,
) -> ir::Value {
    let ir_out = SSARepr::pod(&output, &phase.types).unwrap();
    match usize::cmp(&phase.types.byte_size(input).unwrap(), &phase.types.byte_size(output).unwrap()) {
        Ordering::Greater => backend.builder.ins().ireduce(ir_out, value),
        Ordering::Less => {
            backend.builder.ins().uextend(ir_out, value)
        }
        Ordering::Equal => value,
    }
}



#[cfg(test)]
mod test {
    use crate::prelude::*;
    use crate::{jit_func, setup_logger};
    use edlc_core::inline_code;
    use edlc_core::prelude::mir_str::FatPtr;
    use std::slice;

    fn setup_compiler() -> Result<CraneliftJIT<()>, anyhow::Error> {
        let _ = setup_logger();
        let mut compiler = CraneliftJIT::<()>::default();
        compiler.init()?;

        compiler.compiler.prepare_module(&vec!["std"].into())?;
        let fn_id = compiler.compiler.parse_fn_signature(inline_code!("fn print<T>(val: T)"))?;
        jit_func!(compiler, fn<"str";>(fn_id),
            fn println<>(line: FatPtr) -> () where; {
                let msg = unsafe {
                    std::str::from_utf8_unchecked(
                        slice::from_raw_parts(line.ptr.0 as *const u8, line.size)
                    )
                };
                print!("{msg}");
            }
        );
        jit_func!(compiler, fn<"i32";>(fn_id),
            fn print_i32<>(val: i32) -> () where; {
                print!("{val}");
            }
        );
        jit_func!(compiler, fn<"u32";>(fn_id),
            fn print_i32<>(val: u32) -> () where; {
                print!("{val}");
            }
        );
        jit_func!(compiler, fn<"i64";>(fn_id),
            fn print_i64<>(val: i64) -> () where; {
                print!("{val}");
            }
        );
        jit_func!(compiler, fn<"u64";>(fn_id),
            fn print_i64<>(val: u64) -> () where; {
                print!("{val}");
            }
        );
        jit_func!(compiler, fn<"usize";>(fn_id),
            fn print_i32<>(val: usize) -> () where; {
                print!("{val}");
            }
        );
        jit_func!(compiler, fn<"isize";>(fn_id),
            fn print_isize<>(val: isize) -> () where; {
                print!("{val}")
            }
        );
        jit_func!(compiler, fn<"f64";>(fn_id),
            fn print_i32<>(val: f64) -> () where; {
                print!("{val}");
            }
        );
        jit_func!(compiler, fn<"f32";>(fn_id),
            fn print_i32<>(val: f32) -> () where; {
                print!("{val}");
            }
        );
        jit_func!(compiler, fn<"bool";>(fn_id),
            fn print_bool<>(val: bool) -> () where; {
                print!("{val}");
            }
        );
        jit_func!(compiler, fn<"u8";>(fn_id),
            fn print_u8<>(val: u8) -> () where; {
                print!("{val}");
            }
        );
        jit_func!(compiler, fn<"char";>(fn_id),
            fn print_char<>(val: char) -> () where; {
                print!("{val}");
            }
        );

        let panic_fn = compiler.compiler.parse_fn_signature(inline_code!("fn panic(msg: str)"))?;
        jit_func!(compiler, fn<;>(panic_fn),
            fn panic<>(msg: FatPtr) -> () where; {
                let msg = unsafe {
                    std::str::from_utf8_unchecked(
                        std::slice::from_raw_parts(msg.ptr.0 as *const u8, msg.size)
                    )
                };
                panic!("{}", msg);
            }
        );
        Ok(compiler)
    }

    #[test]
    fn conversions() -> Result<(), anyhow::Error> {
        let mut compiler = setup_compiler()?;
        compiler.compile_module(vec!["test"].into(), edlc_core::inline_code!(r#"
use std::print;

fn assert_eq<T>(x: T, y: T, msg: str) {
    print(x);
    print(" == ");
    print(y);
    print("\n");

    // if x != y {
    //     std::panic(msg);
    // }
}

fn test() {
    // test integer conversions
    assert_eq(42_usize as u32, 42_u32, "usize as u32 conversion failed");
    assert_eq(-42_isize as i32, -42_i32, "isize as i32 conversion failed");
    assert_eq(42_u32 as usize, 42_usize, "u32 as usize conversion failed");
    assert_eq(-42_i32 as isize, -42_isize, "i32 as isize conversion failed");

    // test int 2 float conversions
    assert_eq(42_u32 as f32, 42.0_f32, "u32 as f32 conversion failed");
    assert_eq(-32_i32 as f32, -32.0_f32, "i32 as f32 conversion failed");
    assert_eq(42_i32 as f64, 42.0_f64, "i32 as f64 conversion failed");
    assert_eq(-32_i64 as f64, -32.0_f64, "i64 as f64 conversion failed");

    // test float 2 int conversions
    assert_eq(42.0_f32 as u32, 42_u32, "f32 as u32 conversion failed");
    assert_eq(-32.1_f32 as i32, -32_i32, "f32 as i32 conversion failed");
    assert_eq(42.0_f64 as i32, 42_i32, "f64 as i32 conversion failed");
    assert_eq(-32.8_f64 as i64, -32_i64, "f64 as i64 conversion failed");

    // test elicit conversion
    assert_eq(42.0_f32 as _, 42_u32, "elicit f32 as u32 conversion failed");
    assert_eq(-32.1_f32 as _, -32_i32, "elicit f32 as i32 conversion failed");
    assert_eq(42.0_f64 as _, 42_i32, "elicit f64 as i32 conversion failed");
    assert_eq(-32.8_f64 as _, -32_i64, "elicit f64 as i64 conversion failed");

    // test float 2 float conversions
    assert_eq(3.14159265_f64 as f32, 3.1415927_f32, "f64 as f32 conversion failed");
    assert_eq(3.1415927_f32 as f64, 3.1415927410125732_f64, "f32 as f64 conversion failed");

    // test char conversions
    assert_eq('a' as u32, 97, "char as u32 conversion failed");
    assert_eq('A' as u8, 65, "char as u8 conversion failed");
    assert_eq(97_u8 as char, 'a', "u8 as char conversion failed");
    assert_eq(65_u32 as char, 'A', "u32 as char conversion failed");
    assert_eq('🦀' as u32, 129408, "crab to int conversion failed");
    assert_eq('🦀', 129408_u32 as char, "int to char conversion failed");

    // test error out on type mismatch
    let a: usize = 32;
}
        "#))?;

        let program: TypedProgram<(), _> = compiler
            .compile_expr(&vec!["test"].into(), edlc_core::inline_code!("test()"))?;
        program.exec(&mut compiler.backend)?;
        Ok(())
    }
}