Appearance
| 1 | @@IF.dotnet() | |
| 2 | ||
| 3 | namespace IR.Emitter is | |
| 4 | use Semantic.Types.Type | |
| 5 | ||
| 6 | use System.Reflection.Metadata.EntityHandle | |
| 7 | ||
| 8 | // Lowers the async state-machine values to CIL: launching a | |
| 9 | // machine, completing it, suspending at an await, and a task-like | |
| 10 | // that is complete from the start. Each goes through the .NET async | |
| 11 | // builder protocol. | |
| 12 | class CIL_ASYNC_LOWERING is | |
| 13 | _context: IR.Emitter.CIL_CONTEXT | |
| 14 | ||
| 15 | _body: SRM_METHOD_BODY_EMITTER => _context.current_srm_body_emitter! | |
| 16 | ||
| 17 | init(context: IR.Emitter.CIL_CONTEXT) is | |
| 18 | _context = context | |
| 19 | si | |
| 20 | ||
| 21 | // Constructs the state machine into a local, marks it not yet | |
| 22 | // started, creates its builder, starts it, and leaves the | |
| 23 | // builder's task. | |
| 24 | launch(launch: Values.ASYNC_METHOD_LAUNCH) is | |
| 25 | let body = _body | |
| 26 | let builder_type = launch.builder_field.type! | |
| 27 | let local = launch.state_machine_local | |
| 28 | ||
| 29 | body.declare_local(local) | |
| 30 | ||
| 31 | _context.lowering.gen(launch.ctor_arg_loads) | |
| 32 | body.new_object(_context.resolve_call_target(launch.constructor)) | |
| 33 | body.stloc(local) | |
| 34 | ||
| 35 | body.ldloc(local) | |
| 36 | body.ldc_i4(-1) | |
| 37 | body.stfld(_context.resolve_field_target(launch.state_field)) | |
| 38 | ||
| 39 | body.ldloc(local) | |
| 40 | body.call(_context.resolve_builder_member(builder_type, "create", 0)) | |
| 41 | body.stfld(_context.resolve_field_target(launch.builder_field)) | |
| 42 | ||
| 43 | body.ldloc(local) | |
| 44 | _load_builder_field(builder_type, launch.builder_field) | |
| 45 | body.ldloca(local) | |
| 46 | body.call(_context.resolve_async_builder_start(builder_type, launch.state_machine_type)) | |
| 47 | ||
| 48 | body.ldloc(local) | |
| 49 | _load_builder_field(builder_type, launch.builder_field) | |
| 50 | body.call(_context.resolve_builder_property(builder_type, "task")) | |
| 51 | si | |
| 52 | ||
| 53 | // Marks the machine finished and hands the builder its result, | |
| 54 | // or the exception the handler caught. | |
| 55 | // | |
| 56 | // The builder field is resolved before the state field, so the | |
| 57 | // member references are numbered as they always have been. | |
| 58 | complete(completion: Values.ASYNC_COMPLETION) is | |
| 59 | let body = _body | |
| 60 | let builder_type = completion.builder_field.type! | |
| 61 | let builder_reference = _context.resolve_field_target(completion.builder_field) | |
| 62 | ||
| 63 | let exception_local = | |
| 64 | if let exception_type = completion.exception_type then | |
| 65 | IR.LOCAL(".exception", exception_type) | |
| 66 | else | |
| 67 | null | |
| 68 | fi | |
| 69 | ||
| 70 | if let local = exception_local then | |
| 71 | body.declare_local(local) | |
| 72 | body.stloc(local) | |
| 73 | fi | |
| 74 | ||
| 75 | body.ldarg(0) | |
| 76 | body.ldc_i4(-1) | |
| 77 | body.stfld(_context.resolve_field_target(completion.state_field)) | |
| 78 | body.ldarg(0) | |
| 79 | _load_builder(builder_type, builder_reference) | |
| 80 | ||
| 81 | if let local = exception_local then | |
| 82 | body.ldloc(local) | |
| 83 | body.call(_context.resolve_async_builder_set_exception(builder_type)) | |
| 84 | else | |
| 85 | if let result = completion.result_field then | |
| 86 | body.ldarg(0) | |
| 87 | body.ldfld(_context.resolve_field_target(result)) | |
| 88 | fi | |
| 89 | ||
| 90 | body.call(_context.resolve_async_builder_set_result(builder_type, completion.result_field?)) | |
| 91 | fi | |
| 92 | si | |
| 93 | ||
| 94 | // A task-like whose builder is created and given its result at | |
| 95 | // once. | |
| 96 | completed(completed: Values.COMPLETED_TASK_LIKE) is | |
| 97 | let body = _body | |
| 98 | let builder_type = completed.builder_type | |
| 99 | let local = IR.LOCAL(".completed_builder", builder_type) | |
| 100 | ||
| 101 | body.declare_local(local) | |
| 102 | ||
| 103 | body.call(_context.resolve_builder_member(builder_type, "create", 0)) | |
| 104 | body.stloc(local) | |
| 105 | ||
| 106 | _load_builder_local(builder_type, local) | |
| 107 | body.call(_context.resolve_builder_member(builder_type, "set_result", 0)) | |
| 108 | ||
| 109 | _load_builder_local(builder_type, local) | |
| 110 | body.call(_context.resolve_builder_property(builder_type, "task")) | |
| 111 | si | |
| 112 | ||
| 113 | // The member references are resolved before anything is encoded, | |
| 114 | // and in this order, so they are numbered as the steps have always | |
| 115 | // numbered them. | |
| 116 | suspend(suspend: Values.AWAIT_SUSPEND) is | |
| 117 | _context.resolve_field_target(suspend.state_field) | |
| 118 | _context.resolve_field_target(suspend.awaiter_field) | |
| 119 | _context.resolve_field_target(suspend.builder_field) | |
| 120 | ||
| 121 | _context.begin_unmarked() | |
| 122 | ||
| 123 | _context.lowering.gen(suspend.structure()) | |
| 124 | _context.lowering.gen(suspend.get_result) | |
| 125 | ||
| 126 | _context.end_unmarked() | |
| 127 | si | |
| 128 | ||
| 129 | step(step: Values.AWAIT_SUSPEND_STEP) is | |
| 130 | let suspend = step.suspend | |
| 131 | ||
| 132 | case step.kind | |
| 133 | when Values.AwaitSuspendStep.CAPTURE_AWAITER then | |
| 134 | _capture_awaiter(suspend) | |
| 135 | when Values.AwaitSuspendStep.REGISTER_CONTINUATION then | |
| 136 | _register_continuation(suspend) | |
| 137 | when Values.AwaitSuspendStep.CLEAR_STATE then | |
| 138 | _stamp_state(suspend, -1) | |
| 139 | esac | |
| 140 | si | |
| 141 | ||
| 142 | // Capture the awaiter into the frame. | |
| 143 | _capture_awaiter(suspend: Values.AWAIT_SUSPEND) is | |
| 144 | _body.ldarg(0) | |
| 145 | _context.lowering.gen(suspend.get_awaiter) | |
| 146 | _body.stfld(_context.resolve_field_target(suspend.awaiter_field)) | |
| 147 | si | |
| 148 | ||
| 149 | // Record the state and register the continuation through the | |
| 150 | // builder. The builder goes by address for a struct - the shape | |
| 151 | // the BCL builders use - and as the object a class builder's | |
| 152 | // methods receive as `this`. The state machine goes by reference | |
| 153 | // for a builder whose member takes it so, and as the interface | |
| 154 | // object for one that takes it by value. | |
| 155 | _register_continuation(suspend: Values.AWAIT_SUSPEND) is | |
| 156 | let body = _body | |
| 157 | let builder_type = suspend.builder_field.type! | |
| 158 | ||
| 159 | _stamp_state(suspend, suspend.state_number) | |
| 160 | ||
| 161 | body.ldarg(0) | |
| 162 | _load_builder_field(builder_type, suspend.builder_field) | |
| 163 | ||
| 164 | body.ldarg(0) | |
| 165 | body.ldflda(_context.resolve_field_target(suspend.awaiter_field)) | |
| 166 | ||
| 167 | if _context.builder_awaits_sm_by_reference(builder_type, suspend.is_critical) then | |
| 168 | body.ldarga(0) | |
| 169 | else | |
| 170 | body.ldarg(0) | |
| 171 | fi | |
| 172 | ||
| 173 | body.call( | |
| 174 | _context.resolve_await_on_completed( | |
| 175 | builder_type, suspend.awaiter_type, suspend.state_machine_type, suspend.is_critical)) | |
| 176 | si | |
| 177 | ||
| 178 | // Write a state value to both the cached local and the frame's | |
| 179 | // state field, so a `leave` out of a protected region fires | |
| 180 | // finally bodies that read the local and see the right sentinel. | |
| 181 | _stamp_state(suspend: Values.AWAIT_SUSPEND, value: int) is | |
| 182 | let body = _body | |
| 183 | ||
| 184 | body.ldarg(0) | |
| 185 | body.ldc_i4(value) | |
| 186 | body.op(System.Reflection.Metadata.ILOpCode.DUP) | |
| 187 | body.stloc(suspend.state_local) | |
| 188 | body.stfld(_context.resolve_field_target(suspend.state_field)) | |
| 189 | si | |
| 190 | ||
| 191 | // A struct builder is reached through its address - `this` for a | |
| 192 | // byref receiver and the `ref` the builder methods take - while a | |
| 193 | // class builder is reached through the object the field holds. | |
| 194 | _load_builder_field(builder_type: Type, builder_field: Semantic.Symbols.Field) is | |
| 195 | _load_builder(builder_type, _context.resolve_field_target(builder_field)) | |
| 196 | si | |
| 197 | ||
| 198 | _load_builder(builder_type: Type, reference: EntityHandle) is | |
| 199 | if builder_type.is_value_type then | |
| 200 | _body.ldflda(reference) | |
| 201 | else | |
| 202 | _body.ldfld(reference) | |
| 203 | fi | |
| 204 | si | |
| 205 | ||
| 206 | _load_builder_local(builder_type: Type, local: IR.LOCAL) is | |
| 207 | if builder_type.is_value_type then | |
| 208 | _body.ldloca(local) | |
| 209 | else | |
| 210 | _body.ldloc(local) | |
| 211 | fi | |
| 212 | si | |
| 213 | si | |
| 214 | si |