Skip to content
← Back

src/ir/llvm/llvm_exceptions.ghul

1
namespace IR.LLVMTarget is
2
use LLVM.FUNCTION
3
use LLVM.MODULE
4
use LLVM.PARAMETER
5
use LLVM.Types
6
use LLVM.Values
7
8
/// What the native runtime needs from the program to describe an
9
/// exception nothing caught.
10
class LLVM_EXCEPTIONS is
11
init() is si
12
13
/// The getter of the core library's `Ghul.Exception.message`, or null
14
/// where the program declares none.
15
message_getter: Semantic.Symbols.Function? static =>
16
if let exception = IR.Program.CORE_LIBRARY.root_exception_class() then
17
IR.Program.CORE_LIBRARY.find_getter(exception, "message")
18
else
19
null
20
fi
21
22
/// Adds `$ghul_report_unhandled`, which writes the first line .NET
23
/// prints for an exception nothing caught: `Unhandled exception.`,
24
/// then the exception's type name and its message where it has them.
25
///
26
/// - module: where the function is added
27
/// - strings: the literals and the runtime's write operation
28
/// - message: the compiled getter of an exception's message, or null where the program has none
29
add_report_unhandled(module: MODULE, strings: LLVM_STRINGS, message: FUNCTION?) static is
30
let function = FUNCTION(LLVM_STRINGS.REPORT_UNHANDLED_NAME, LLVM.Type.VOID, [PARAMETER(Types.pointer, "exception")])
31
32
let write = strings.runtime(LLVM_STRINGS.host_name("write"), LLVM.Type.VOID, [Types.pointer, Types.i32, Types.i32])
33
34
let entry = function.add_block("entry")
35
let described = function.add_block("described")
36
let messaged = function.add_block("messaged")
37
let done = function.add_block("done")
38
39
let exception = function.parameter(0)
40
41
let write_text = (block: LLVM.BLOCK, text: LLVM.Value) is
42
block.call(write, [strings.chars(block, text), strings.length(block, text), Values.i32(2)])
43
si
44
45
write_text(entry, strings.literal("Unhandled exception."))
46
entry.branch(entry.compare(LLVM.IntegerPredicate.EQ, exception, Values.null_pointer), done, described)
47
48
let descriptor = strings.load_descriptor(described, exception)
49
let name = described.load(Types.pointer, described.element_pointer(Types.i8, descriptor, [Values.i32(LLVM_OBJECTS.DESCRIPTOR_NAME)], true), 8)
50
51
write_text(described, strings.literal(" "))
52
write_text(described, name)
53
54
if let getter = message then
55
let text = described.call(getter, [exception])!
56
57
described.branch(described.compare(LLVM.IntegerPredicate.EQ, text, Values.null_pointer), done, messaged)
58
59
write_text(messaged, strings.literal(": "))
60
write_text(messaged, text)
61
messaged.branch(done)
62
else
63
described.branch(done)
64
messaged.unreachable()
65
fi
66
67
write_text(done, strings.literal("\n"))
68
done.return_void()
69
70
module.add_function(function)
71
si
72
73
/// The constructor taking nothing of the core library's
74
/// null-reference exception, or null where it declares none.
75
null_reference_constructor: Semantic.Symbols.Function? static =>
76
IR.Program.CORE_LIBRARY.constructor(
77
IR.Program.CORE_LIBRARY.exception_class(Semantic.Lookups.RuntimeException.NULL_REFERENCE), 0)
78
79
/// Adds `$ghul_make_null_reference`, which makes the exception the
80
/// native runtime throws for a read through null that faulted.
81
///
82
/// - module: where the function is added
83
/// - strings: the runtime's allocation
84
/// - objects: the layout of the exception's class
85
/// - type: the null-reference exception's class
86
/// - constructor: its compiled constructor taking nothing
87
add_make_null_reference(module: MODULE, strings: LLVM_STRINGS, objects: LLVM_OBJECTS, type: IR.Program.CLASS_INSTANCE, constructor: FUNCTION) static is
88
let function = FUNCTION(LLVM_STRINGS.MAKE_NULL_REFERENCE_NAME, Types.pointer, [])
89
let entry = function.add_block("entry")
90
91
let exception =
92
strings.allocate_object(entry, Values.global(objects.descriptor_name(type)), Values.i64(cast long(objects.size_of(type))), true)
93
94
entry.call(constructor, [exception])
95
entry.return_value(exception)
96
97
module.add_function(function)
98
si
99
si
100
si