Skip to content
← Back

src/ir/values/await_suspend.ghul

1
namespace IR.Values is
2
use TypeTyped = Semantic.Types.Typed
3
use Semantic.Types.Type
4
5
// Per-`await` IL emission inside MoveNext. Stashes the awaiter
6
// to a frame field, IsCompleted-short-circuits when the awaited
7
// value is already done, otherwise records the state, registers
8
// the continuation through the builder and leaves the resumable
9
// MoveNext body. A dispatch routes re-entry back to the resume
10
// point at `cold_resume_label`, where state is cleared and
11
// execution falls through to call `GetResult` and leave T on the
12
// stack (or stack-effect 0 for a void result).
13
//
14
// The control flow is built from structured nodes and encoded
15
// without marking source positions, so the await marks only its
16
// own.
17
//
18
// The three awaiter-pattern calls arrive as ordinary call values:
19
// `get_awaiter` over the awaited operand, and `is_completed` /
20
// `get_result` over a load of the awaiter field, so the receiver
21
// conventions (address of a struct, dispatch on a class) are the
22
// ones every other call uses.
23
//
24
// ghūl uses CLASS-based state machines (matches the generator
25
// shape) — `ldarg.0` is the SM instance and `ldarga.s 0` is the
26
// byref needed for `ref TStateMachine`. C# Release uses STRUCT-
27
// based for performance; the CLASS path costs one alloc per
28
// async call.
29
class AWAIT_SUSPEND: Value, TypeTyped is
30
type: Type
31
32
get_awaiter: Value
33
is_completed: Value
34
get_result: Value
35
awaiter_type: Type
36
37
// The awaiter implements `ICriticalNotifyCompletion`, so the
38
// continuation is registered with `AwaitUnsafeOnCompleted`
39
// rather than `AwaitOnCompleted`.
40
is_critical: bool
41
42
state_number: int
43
state_local: IR.LOCAL
44
cold_resume_label: IR.LABEL
45
resumable: RESUMABLE
46
state_machine_type: Type
47
state_field: Semantic.Symbols.Field
48
awaiter_field: Semantic.Symbols.Field
49
builder_field: Semantic.Symbols.Field
50
51
init(
52
type: Type,
53
get_awaiter: Value,
54
is_completed: Value,
55
get_result: Value,
56
awaiter_type: Type,
57
is_critical: bool,
58
state_number: int,
59
state_local: IR.LOCAL,
60
cold_resume_label: IR.LABEL,
61
resumable: RESUMABLE,
62
state_machine_type: Type,
63
state_field: Semantic.Symbols.Field,
64
awaiter_field: Semantic.Symbols.Field,
65
builder_field: Semantic.Symbols.Field
66
) is
67
super.init()
68
69
self.type = type
70
self.get_awaiter = get_awaiter
71
self.is_completed = is_completed
72
self.get_result = get_result
73
self.awaiter_type = awaiter_type
74
self.is_critical = is_critical
75
self.state_number = state_number
76
self.state_local = state_local
77
self.cold_resume_label = cold_resume_label
78
self.resumable = resumable
79
self.state_machine_type = state_machine_type
80
self.state_field = state_field
81
self.awaiter_field = awaiter_field
82
self.builder_field = builder_field
83
si
84
85
// The control flow an await suspends through: the completed
86
// check, registering the continuation, leaving the resumable
87
// body, and the point a resumed MoveNext comes back to.
88
structure() -> REGION is
89
let void_type = IoC.CONTAINER.instance.innate_symbol_lookup.get_void_type()
90
91
// Left when the awaited value has already completed, so no
92
// continuation is registered and GetResult runs at once.
93
let completed = REGION(IR.LABEL(), void_type)
94
let skip = BREAK(completed, IR.BRANCH.NZ, is_completed)
95
let suspend = BREAK(resumable)
96
let resume = RESUME_POINT(cold_resume_label)
97
98
completed.clear_position()
99
skip.clear_position()
100
suspend.clear_position()
101
resume.clear_position()
102
103
completed.body.add(AWAIT_SUSPEND_STEP(self, AwaitSuspendStep.CAPTURE_AWAITER))
104
completed.body.add(skip)
105
completed.body.add(AWAIT_SUSPEND_STEP(self, AwaitSuspendStep.REGISTER_CONTINUATION))
106
completed.body.add(suspend)
107
completed.body.add(resume)
108
completed.body.add(AWAIT_SUSPEND_STEP(self, AwaitSuspendStep.CLEAR_STATE))
109
completed.close()
110
111
return completed
112
si
113
114
accept(visitor: Visitor) is
115
visitor.visit(self)
116
si
117
118
to_string() -> string =>
119
"await-suspend:[{type}](state={state_number})"
120
si
121
122
enum AwaitSuspendStep is
123
CAPTURE_AWAITER,
124
REGISTER_CONTINUATION,
125
CLEAR_STATE
126
si
127
128
// One straight-line step of an AWAIT_SUSPEND, which the await's
129
// control flow is built around.
130
class AWAIT_SUSPEND_STEP: Value is
131
suspend: AWAIT_SUSPEND
132
kind: AwaitSuspendStep
133
134
has_location: bool => false
135
136
init(suspend: AWAIT_SUSPEND, kind: AwaitSuspendStep) is
137
super.init()
138
139
self.suspend = suspend
140
self.kind = kind
141
si
142
143
accept(visitor: Visitor) is
144
visitor.visit(self)
145
si
146
147
to_string() -> string => "await-step:{kind}"
148
si
149
si