Skip to content
← Back

src/syntax/process/generate-il/generate_il_pack_thunk.ghul

1
namespace Syntax.Process is
2
use IR
3
use IR.Values
4
5
// An argument pack thunk has no literal behind it, so its body is
6
// generated from the description on its closure rather than walked.
7
// At the pack, it reads its own parameters, turns the pack from the
8
// shape the thunk takes into the shape the function it stands for
9
// takes, calls that function and returns what it returns. Above the
10
// pack, it calls that function with its parameters and returns the
11
// next thunk's delegate, built closed over the result.
12
partial GENERATE_IL is
13
_gen_pack_thunk(closure: Semantic.Symbols.Closure) is
14
let thunk = closure.pack_thunk
15
16
if !thunk? then
17
return
18
fi
19
20
closure.map_type_arguments()
21
22
_backend.begin_body(closure)
23
24
enter_block()
25
26
let callee: Value? =
27
if thunk.takes_callee then
28
_pack_thunk_parameter(closure, "$pack_callee")
29
else
30
null
31
fi
32
33
if let inner = thunk.inner, inner_presented = thunk.inner_presented then
34
let hop_arguments = Collections.LIST[Value]()
35
36
for i in 0..closure.arguments.count - 1 do
37
hop_arguments.add(_pack_thunk_parameter(closure, "$pack_{i}"))
38
od
39
40
let result_type = thunk.callee_type.arguments[thunk.callee_type.arguments.count - 1]
41
42
let result = Values.Call.CLOSURE(callee!, result_type, false, thunk.callee_type, hop_arguments)
43
44
add(inner.load_closed_over(closure.location, result, inner_presented))
45
46
_finish_pack_thunk(closure)
47
48
return
49
fi
50
51
let arguments = Collections.LIST[Value]()
52
53
for i in 0..thunk.fixed_count do
54
arguments.add(_pack_thunk_parameter(closure, "$pack_{i}"))
55
od
56
57
if thunk.is_unpack then
58
let tuple = IR.TEMP(current_block, "pack_thunk_tuple", _pack_thunk_parameter(closure, "$pack_{thunk.fixed_count}"))
59
60
let strategy = DESTRUCTURE_RESOLVER.resolve_strategy(tuple.type, thunk.element_types.count)
61
62
for i in 0..thunk.element_types.count do
63
arguments.add(strategy.members[i]!.load(closure.location, tuple.load(), _symbol_loader))
64
od
65
else
66
let elements = Collections.LIST[Value]()
67
68
for i in 0..thunk.element_types.count do
69
elements.add(_pack_thunk_parameter(closure, "$pack_{thunk.fixed_count + i}"))
70
od
71
72
arguments.add(
73
Values.TUPLE(
74
_innate_symbol_lookup.get_tuple_type(thunk.element_types, null),
75
elements
76
)
77
)
78
fi
79
80
let return_type = closure.return_type!
81
82
if let target = thunk.target then
83
add(target.call(closure.location, null, arguments, null, _function_caller))
84
else
85
add(
86
Values.Call.CLOSURE(
87
callee!,
88
return_type,
89
return_type.compare(_innate_symbol_lookup.get_void_type()) == Semantic.Types.MATCH.SAME,
90
thunk.callee_type,
91
arguments
92
)
93
)
94
fi
95
96
_finish_pack_thunk(closure)
97
si
98
99
_finish_pack_thunk(closure: Semantic.Symbols.Closure) is
100
add(Values.RET())
101
102
_backend.write(current_block)
103
104
leave_block()
105
106
_backend.finish_body(closure)
107
_backend.end_body()
108
109
closure.unmap_type_arguments()
110
si
111
112
_pack_thunk_parameter(closure: Semantic.Symbols.Closure, name: string) -> Value =>
113
closure.find_direct(name)!.load(closure.location, null, _symbol_loader)
114
si
115
si