mun_codegen 0.4.0

LLVM IR code generation for Mun
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
use std::convert::TryFrom;
use std::{collections::HashSet, ffi::CString};

use inkwell::{attributes::Attribute, module::Linkage, types::AnyType};
use itertools::Itertools;

use ir_type_builder::TypeIdBuilder;
use mun_hir::{HirDatabase, TyKind};

use crate::ir::ty::guid_from_struct;
use crate::type_info::HasStaticTypeId;
use crate::{
    ir::ty::HirTypeCache,
    ir::types as ir,
    ir::{
        dispatch_table::{DispatchTable, DispatchableFunction},
        function,
        type_table::TypeTable,
    },
    value::{
        AsValue, CanInternalize, Global, IrValueContext, IterAsIrValue, SizedValueType, Value,
    },
};
use mun_abi as abi;

mod ir_type_builder;

/// Construct a `MunFunctionPrototype` struct for the specified HIR function.
fn gen_prototype_from_function<'ink>(
    db: &dyn HirDatabase,
    context: &IrValueContext<'ink, '_, '_>,
    function: mun_hir::Function,
    hir_types: &HirTypeCache,
    ir_type_builder: &TypeIdBuilder<'ink, '_, '_, '_>,
) -> ir::FunctionPrototype<'ink> {
    let name = function.full_name(db);

    // Internalize the name of the function prototype
    let name_str = CString::new(name.clone())
        .expect("function prototype name is not a valid CString")
        .intern(format!("fn_sig::<{}>::name", &name), context);

    // Get the `ir::TypeInfo` pointer for the return type of the function
    let fn_sig = function.ty(db).callable_sig(db).unwrap();
    let return_type = if fn_sig.ret().is_empty() {
        ir_type_builder.construct_from_type_id(<() as HasStaticTypeId>::type_id())
    } else {
        ir_type_builder.construct_from_type_id(&hir_types.type_id(fn_sig.ret()))
    };

    // Construct an array of pointers to `ir::TypeInfo`s for the arguments of the prototype
    let arg_types = fn_sig
        .params()
        .iter()
        .map(|ty| ir_type_builder.construct_from_type_id(&hir_types.type_id(ty)))
        .into_const_private_pointer_or_null(format!("fn_sig::<{}>::arg_types", &name), context);

    ir::FunctionPrototype {
        name: name_str.as_value(context),
        signature: ir::FunctionSignature {
            arg_types,
            return_type,
            num_arg_types: fn_sig.params().len() as u16,
        },
    }
}

/// Construct a `MunFunctionPrototype` struct for the specified dispatch table function.
fn gen_prototype_from_dispatch_entry<'ink>(
    context: &IrValueContext<'ink, '_, '_>,
    function: &DispatchableFunction,
    ir_type_builder: &TypeIdBuilder<'ink, '_, '_, '_>,
) -> ir::FunctionPrototype<'ink> {
    // Internalize the name of the function prototype
    let name_str = CString::new(function.prototype.name.clone())
        .expect("function prototype name is not a valid CString")
        .intern(
            format!("fn_sig::<{}>::name", function.prototype.name),
            context,
        );

    // Get the `ir::TypeInfo` pointer for the return type of the function
    let return_type = ir_type_builder.construct_from_type_id(&function.prototype.ret_type);

    // Construct an array of pointers to `ir::TypeInfo`s for the arguments of the prototype
    let arg_types = function
        .prototype
        .arg_types
        .iter()
        .map(|type_info| ir_type_builder.construct_from_type_id(type_info))
        .into_const_private_pointer_or_null(
            format!("{}_param_types", function.prototype.name),
            context,
        );

    ir::FunctionPrototype {
        name: name_str.as_value(context),
        signature: ir::FunctionSignature {
            arg_types,
            return_type,
            num_arg_types: function.prototype.arg_types.len() as u16,
        },
    }
}

/// Construct a global that holds a reference to all types. e.g.:
/// MunTypeInfo[] definitions = { ... }
fn get_type_definition_array<'ink>(
    db: &dyn HirDatabase,
    context: &IrValueContext<'ink, '_, '_>,
    types: impl Iterator<Item = mun_hir::Ty>,
    hir_types: &HirTypeCache,
    ir_type_builder: &TypeIdBuilder<'ink, '_, '_, '_>,
) -> Value<'ink, *const ir::TypeDefinition<'ink>> {
    types
        .sorted_by_cached_key(|type_info| match type_info.interned() {
            TyKind::Struct(s) => s.full_name(db),
            _ => unreachable!("unsupported export type"),
        })
        .map(|type_info| match type_info.interned() {
            TyKind::Struct(s) => {
                let inkwell_type = hir_types.get_struct_type(*s);
                let struct_name = s.full_name(db);
                ir::TypeDefinition {
                    name: CString::new(struct_name.clone())
                        .expect("typename is not a valid CString")
                        .intern(format!("type_info::<{}>::name", struct_name), context)
                        .as_value(context),
                    size_in_bits: context
                        .type_context
                        .target_data
                        .get_bit_size(&inkwell_type)
                        .try_into()
                        .expect("could not convert size in bits to smaller size"),
                    alignment: context
                        .type_context
                        .target_data
                        .get_abi_alignment(&inkwell_type)
                        .try_into()
                        .expect("could not convert alignment to smaller size"),
                    data: ir::TypeDefinitionData::Struct(gen_struct_info(
                        db,
                        *s,
                        context,
                        hir_types,
                        ir_type_builder,
                    )),
                }
            }
            _ => unreachable!("unsupported export type"),
        })
        .into_const_private_pointer_or_null("fn.get_info.types", context)
}

fn gen_struct_info<'ink>(
    db: &dyn HirDatabase,
    hir_struct: mun_hir::Struct,
    context: &IrValueContext<'ink, '_, '_>,
    hir_types: &HirTypeCache,
    ir_type_builder: &TypeIdBuilder<'ink, '_, '_, '_>,
) -> ir::StructDefinition<'ink> {
    let struct_ir = hir_types.get_struct_type(hir_struct);
    let name = hir_struct.full_name(db);
    let fields = hir_struct.fields(db);

    // Construct an array of field names (or null if there are no fields)
    let field_names = fields
        .iter()
        .enumerate()
        .map(|(idx, field)| {
            CString::new(field.name(db).to_string())
                .expect("field name is not a valid CString")
                .intern(
                    format!("struct_info::<{}>::field_names.{}", name, idx),
                    context,
                )
                .as_value(context)
        })
        .into_const_private_pointer_or_null(
            format!("struct_info::<{}>::field_names", name),
            context,
        );

    // Construct an array of field types (or null if there are no fields)
    let field_types = fields
        .iter()
        .map(|field| {
            let field_type_info = hir_types.type_id(&field.ty(db));
            ir_type_builder.construct_from_type_id(&field_type_info)
        })
        .into_const_private_pointer_or_null(
            format!("struct_info::<{}>::field_types", name),
            context,
        );

    // Construct an array of field offsets (or null if there are no fields)
    let field_offsets = fields
        .iter()
        .enumerate()
        .map(|(idx, _)| {
            context
                .type_context
                .target_data
                .offset_of_element(&struct_ir, idx as u32)
                .unwrap() as u16
        })
        .into_const_private_pointer_or_null(
            format!("struct_info::<{}>::field_offsets", name),
            context,
        );

    ir::StructDefinition {
        guid: guid_from_struct(db, hir_struct),
        field_names,
        field_types,
        field_offsets,
        num_fields: fields
            .len()
            .try_into()
            .expect("could not convert num_fields to smaller bit size"),
        memory_kind: hir_struct.data(db.upcast()).memory_kind,
    }
}

/// Construct a global that holds a reference to all functions. e.g.:
/// MunFunctionDefinition[] definitions = { ... }
fn get_function_definition_array<'ink, 'a>(
    db: &dyn HirDatabase,
    context: &IrValueContext<'ink, '_, '_>,
    functions: impl Iterator<Item = &'a mun_hir::Function>,
    hir_types: &HirTypeCache,
    ir_type_builder: &TypeIdBuilder<'ink, '_, '_, '_>,
) -> Global<'ink, [ir::FunctionDefinition<'ink>]> {
    let module = context.module;
    functions
        .sorted_by_cached_key(|f| f.full_name(db))
        .map(|f| {
            let name = f.name(db).to_string();

            // Get the function from the cloned module and modify the linkage of the function.
            let value = module
                // If a wrapper function exists, use that (required for struct types)
                .get_function(&format!("{}_wrapper", name))
                // Otherwise, use the normal function
                .or_else(|| module.get_function(&name))
                .unwrap();
            value.set_linkage(Linkage::Private);

            // Generate the signature from the function
            let prototype =
                gen_prototype_from_function(db, context, *f, hir_types, ir_type_builder);
            ir::FunctionDefinition {
                prototype,
                fn_ptr: Value::<*const fn()>::with_cast(
                    value.as_global_value().as_pointer_value(),
                    context,
                ),
            }
        })
        .into_value(context)
        .into_const_private_global("fn.get_info.functions", context)
}

/// Generate the type lookup table information. e.g.:
/// ```c
/// MunTypeLut typeLut = { ... }
/// ```
fn gen_type_lut<'ink>(
    context: &IrValueContext<'ink, '_, '_>,
    type_table: &TypeTable,
    ir_type_builder: &TypeIdBuilder<'ink, '_, '_, '_>,
) -> ir::TypeLut<'ink> {
    let module = context.module;

    // Get a list of all Guids
    let type_ids = type_table
        .entries()
        .iter()
        .map(|ty| ir_type_builder.construct_from_type_id(ty))
        .into_const_private_pointer("fn.get_info.typeLut.typeIds", context);

    let type_names = type_table
        .entries()
        .iter()
        .map(|ty| {
            CString::new(ty.name.as_str())
                .expect("unable to create CString from typeinfo name")
                .intern(&ty.name, context)
                .as_value(context)
        })
        .into_const_private_pointer("fn.get_info.typeLut.typeNames", context);

    let type_ptrs = TypeTable::find_global(module)
        .map(|type_table| {
            Value::<*mut *const std::ffi::c_void>::with_cast(
                type_table.as_value(context).value,
                context,
            )
        })
        .unwrap_or_else(|| Value::null(context));

    ir::TypeLut {
        type_ids,
        type_ptrs,
        type_names,
        num_entries: type_table.num_types().try_into().expect("too many types"),
    }
}

/// Generate the dispatch table information. e.g.:
/// ```c
/// MunDispatchTable dispatchTable = { ... }
/// ```
fn gen_dispatch_table<'ink>(
    context: &IrValueContext<'ink, '_, '_>,
    dispatch_table: &DispatchTable<'ink>,
    ir_type_builder: &TypeIdBuilder<'ink, '_, '_, '_>,
) -> ir::DispatchTable<'ink> {
    let module = context.module;

    // Generate an internal array that holds all the function prototypes
    let prototypes = dispatch_table
        .entries()
        .iter()
        .map(|entry| gen_prototype_from_dispatch_entry(context, entry, ir_type_builder))
        .into_const_private_pointer("fn.get_info.dispatchTable.signatures", context);

    // Get the pointer to the global table (or nullptr if no global table was defined).
    let fn_ptrs = dispatch_table
        .global_value()
        .map(|_g|
            // TODO: This is a hack, the passed module here is a clone of the module with which the
            // dispatch table was created. Because of this we have to lookup the dispatch table
            // global again. There is however not a `GlobalValue::get_name` method so I just
            // hardcoded the name here.
            Value::<*mut *const fn()>::with_cast(module.get_global("dispatchTable").unwrap().as_pointer_value(), context))
        .unwrap_or_else(|| Value::null(context));

    ir::DispatchTable {
        prototypes,
        fn_ptrs,
        num_entries: dispatch_table.entries().len() as u32,
    }
}

/// Constructs IR that exposes the types and symbols in the specified module. A function called
/// `get_info` is constructed that returns a struct `MunAssemblyInfo`. See the `mun_abi` crate
/// for the ABI that `get_info` exposes.
#[allow(clippy::too_many_arguments)]
pub(super) fn gen_reflection_ir<'db, 'ink>(
    db: &'db dyn HirDatabase,
    context: &IrValueContext<'ink, '_, '_>,
    function_definitions: &HashSet<mun_hir::Function>,
    type_definitions: &HashSet<mun_hir::Ty>,
    dispatch_table: &DispatchTable<'ink>,
    type_table: &TypeTable<'ink>,
    hir_types: &HirTypeCache<'db, 'ink>,
    optimization_level: inkwell::OptimizationLevel,
    dependencies: Vec<String>,
) {
    let ir_type_builder = TypeIdBuilder::new(context);

    let num_functions = function_definitions.len() as u32;
    let functions = get_function_definition_array(
        db,
        context,
        function_definitions.iter(),
        hir_types,
        &ir_type_builder,
    );

    // Get the TypeTable global
    let num_types = type_definitions.len() as u32;
    let types = get_type_definition_array(
        db,
        context,
        type_definitions.iter().cloned(),
        hir_types,
        &ir_type_builder,
    );

    // Construct the module info struct
    let module_info = ir::ModuleInfo {
        path: CString::new("")
            .unwrap()
            .intern("module_info::path", context)
            .as_value(context),
        functions: functions.as_value(context),
        num_functions,
        types,
        num_types,
    };

    // Construct the dispatch table struct
    let dispatch_table = gen_dispatch_table(context, dispatch_table, &ir_type_builder);

    let type_lut = gen_type_lut(context, type_table, &ir_type_builder);

    // Construct the actual `get_info` function
    gen_get_info_fn(
        db,
        context,
        module_info,
        dispatch_table,
        type_lut,
        optimization_level,
        dependencies,
    );
    gen_set_allocator_handle_fn(context);
    gen_get_version_fn(context);
}

/// Construct the actual `get_info` function.
fn gen_get_info_fn<'ink>(
    db: &dyn HirDatabase,
    context: &IrValueContext<'ink, '_, '_>,
    module_info: ir::ModuleInfo<'ink>,
    dispatch_table: ir::DispatchTable<'ink>,
    type_lut: ir::TypeLut<'ink>,
    optimization_level: inkwell::OptimizationLevel,
    dependencies: Vec<String>,
) {
    let target = db.target();

    // Construct the return type of the `get_info` method. Depending on the C ABI this is either the
    // `MunAssemblyInfo` struct or void. On windows the return argument is passed back to the caller
    // through a pointer to the return type as the first argument. e.g.:
    // On Windows:
    // ```c
    // void get_info(MunModuleInfo* result) {...}
    // ```
    // Whereas on other platforms the signature of the `get_info` function is:
    // ```c
    // MunModuleInfo get_info() { ... }
    // ```
    let get_symbols_type = if target.options.is_like_windows {
        Value::<'ink, fn(*mut ir::AssemblyInfo<'ink>)>::get_ir_type(context.type_context)
    } else {
        Value::<'ink, fn() -> ir::AssemblyInfo<'ink>>::get_ir_type(context.type_context)
    };

    let get_symbols_fn =
        context
            .module
            .add_function("get_info", get_symbols_type, Some(Linkage::DLLExport));

    if target.options.is_like_windows {
        let type_attribute = context.context.create_type_attribute(
            Attribute::get_named_enum_kind_id("sret"),
            ir::AssemblyInfo::get_ir_type(context.type_context).as_any_type_enum(),
        );

        get_symbols_fn.add_attribute(inkwell::attributes::AttributeLoc::Param(0), type_attribute);
    }

    let builder = context.context.create_builder();
    let body_ir = context.context.append_basic_block(get_symbols_fn, "body");
    builder.position_at_end(body_ir);

    // Get a pointer to the IR value that will hold the return value. Again this differs depending
    // on the C ABI.
    let result_ptr = if target.options.is_like_windows {
        get_symbols_fn
            .get_nth_param(0)
            .unwrap()
            .into_pointer_value()
    } else {
        builder.build_alloca(
            Value::<ir::AssemblyInfo>::get_ir_type(context.type_context),
            "",
        )
    };

    // Get access to the structs internals
    let symbols_addr = builder
        .build_struct_gep(result_ptr, 1, "symbols")
        .expect("could not retrieve `symbols` from result struct");
    let dispatch_table_addr = builder
        .build_struct_gep(result_ptr, 3, "dispatch_table")
        .expect("could not retrieve `dispatch_table` from result struct");
    let type_lut_addr = builder
        .build_struct_gep(result_ptr, 5, "type_lut")
        .expect("could not retrieve `type_lut` from result struct");
    let dependencies_addr = builder
        .build_struct_gep(result_ptr, 7, "dependencies")
        .expect("could not retrieve `dependencies` from result struct");
    let num_dependencies_addr = builder
        .build_struct_gep(result_ptr, 9, "num_dependencies")
        .expect("could not retrieve `num_dependencies` from result struct");

    // Assign the struct values one by one.
    builder.build_store(symbols_addr, module_info.as_value(context).value);
    builder.build_store(dispatch_table_addr, dispatch_table.as_value(context).value);
    builder.build_store(type_lut_addr, type_lut.as_value(context).value);
    builder.build_store(
        dependencies_addr,
        dependencies
            .iter()
            .enumerate()
            .map(|(idx, name)| {
                CString::new(name.as_str())
                    .expect("could not convert dependency name to string")
                    .intern(format!("dependency{}", idx), context)
                    .as_value(context)
            })
            .into_const_private_pointer_or_null("dependencies", context)
            .value,
    );
    builder.build_store(
        num_dependencies_addr,
        context.context.i32_type().const_int(
            u32::try_from(dependencies.len()).expect("too many dependencies") as u64,
            false,
        ),
    );

    // Construct the return statement of the function.
    if target.options.is_like_windows {
        builder.build_return(None);
    } else {
        builder.build_return(Some(&builder.build_load(result_ptr, "")));
    }

    // Run the function optimizer on the generate function
    function::create_pass_manager(context.module, optimization_level).run_on(&get_symbols_fn);
}

/// Generates a method `void set_allocator_handle(void*)` that stores the argument into the global
/// `allocatorHandle`. This global is used internally to reference the allocator used by this
/// munlib.
fn gen_set_allocator_handle_fn(context: &IrValueContext) {
    let set_allocator_handle_fn = context.module.add_function(
        "set_allocator_handle",
        Value::<fn(*const u8)>::get_ir_type(context.type_context),
        Some(Linkage::DLLExport),
    );

    let builder = context.context.create_builder();
    let body_ir = context
        .context
        .append_basic_block(set_allocator_handle_fn, "body");
    builder.position_at_end(body_ir);

    if let Some(allocator_handle_global) = context.module.get_global("allocatorHandle") {
        builder.build_store(
            allocator_handle_global.as_pointer_value(),
            set_allocator_handle_fn.get_nth_param(0).unwrap(),
        );
    }

    builder.build_return(None);
}

/// Generates a `get_version` method that returns the current abi version.
/// Specifically, it returns the abi version the function was generated in.
fn gen_get_version_fn(context: &IrValueContext) {
    let get_version_fn = context.module.add_function(
        abi::GET_VERSION_FN_NAME,
        Value::<fn() -> u32>::get_ir_type(context.type_context),
        Some(Linkage::DLLExport),
    );

    let builder = context.context.create_builder();
    let body_ir = context.context.append_basic_block(get_version_fn, "body");
    builder.position_at_end(body_ir);

    builder.build_return(Some(&abi::ABI_VERSION.as_value(context).value));
}