-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobal_plan.rs
More file actions
583 lines (568 loc) · 19.7 KB
/
Copy pathglobal_plan.rs
File metadata and controls
583 lines (568 loc) · 19.7 KB
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
use std::collections::HashMap;
use wasm_encoder::{ConstExpr, GlobalSection, GlobalType, HeapType, RefType, ValType};
use crate::{
ast::{ActionKind, EnumVariantId, Program, ValueId},
managed::ManagedBindingPlan,
semantic::{FunctionInstance, SemanticModel},
stdlib::{
CoreTypeId, RuntimeRepresentation, StandardLibrary, StateProviderAttachment,
StdlibStateProviderId, StdlibTypeId,
},
wasm_ir,
};
use super::{
GcLayout, STATE_TYPE, Type, constant, is_wasm_global_constant,
managed_state_reads::{self, ManagedStateReadCache},
semantic_type, value_type,
};
/// User attachment initialization completed and normal state polling may run.
pub(super) const ATTACH_READY: i32 = 1;
/// This process was rejected after acquisition and remains held only until it
/// closes, preventing the discovery loop from immediately selecting it again.
pub(super) const ATTACH_REJECTED: i32 = 2;
/// Provider preparation and automatic shape selection completed; user
/// `onAttach` is still pending.
pub(super) const ATTACH_PREPARED: i32 = 3;
pub(super) struct GlobalPlan {
pub section: GlobalSection,
pub runtime: RuntimeGlobals,
pub variables: HashMap<ValueId, u32>,
pub variable_types: HashMap<ValueId, Type>,
pub settings: HashMap<ValueId, SettingStorage>,
pub managed_state_reads: ManagedStateReadCache,
pub provider_values: HashMap<StdlibStateProviderId, u32>,
pub provider_attachment_frames: HashMap<EnumVariantId, u32>,
}
#[derive(Clone, Copy)]
pub(super) struct SettingStorage {
pub current: u32,
pub old: u32,
pub ty: Type,
}
#[derive(Debug, Clone, Copy)]
pub(super) struct RuntimeGlobals {
pub process: u32,
/// Index of the source-declared process name that successfully attached.
/// `-1` means no process is attached.
pub process_name: u32,
/// Physical native pointer width in bytes, detected once per attachment.
/// Storage is omitted when no reachable operation depends on it.
pub process_pointer_size: Option<u32>,
pub provider_value: Option<u32>,
/// Pending source-defined state-provider attachment future.
pub provider_attachment_frame: Option<u32>,
/// Opaque provider-specific context produced before user attachment code.
pub provider_preparation_value: Option<u32>,
/// Pending source-defined state-provider preparation future.
pub provider_preparation_frame: Option<u32>,
/// Whether preparation completed for the current process attachment.
pub provider_prepared: Option<u32>,
/// The typed discriminator for a multi-provider state.
pub selected_provider: Option<u32>,
pub current: u32,
pub old: u32,
pub attach_ready: u32,
/// Whether this attachment has committed at least one complete snapshot.
pub state_ready: u32,
/// Last timer state observed by the module-wide lifecycle monitor. `-1`
/// means the loaded script has not established its baseline yet.
/// Storage exists only when a timer lifecycle observer is declared.
pub observed_timer_state: Option<u32>,
/// Whether an observed start transition has completed `onStart` for the
/// current timer attempt. Storage exists only for attempt-scoped globals.
pub attempt_ready: Option<u32>,
/// Current compiler-derived structural formatting depth. This bounds
/// recursive container graphs without allocating traversal state.
pub debug_depth: u32,
/// Monotonically wrapping identity of the current host update. First-class
/// future dispatch uses it to prevent aliased handles from advancing more
/// than once during one update.
pub future_poll_epoch: u32,
pub async_frame: u32,
/// Boolean completion of a suspending `whileAttached` invocation. The
/// action's Wasm return remains the poll readiness flag.
pub while_attached_result: Option<u32>,
}
pub(super) struct Inputs<'a> {
pub program: &'a Program,
pub semantics: &'a SemanticModel,
pub gc: &'a GcLayout,
pub wasm_ir: &'a wasm_ir::Program,
pub managed: &'a ManagedBindingPlan,
pub provider_attachment: Option<&'a FunctionInstance>,
pub provider_alternatives: &'a [(EnumVariantId, StdlibStateProviderId, FunctionInstance)],
pub provider_preparation: Option<&'a FunctionInstance>,
pub needs_native_pointer_size: bool,
}
pub(super) fn encode(inputs: Inputs<'_>) -> GlobalPlan {
let Inputs {
program,
semantics,
gc,
wasm_ir,
managed,
provider_attachment,
provider_alternatives,
provider_preparation,
needs_native_pointer_size,
} = inputs;
let mut section = GlobalSection::new();
let process = section.len();
section.global(
GlobalType {
val_type: ValType::I64,
mutable: true,
shared: false,
},
&ConstExpr::i64_const(0),
);
let process_name = section.len();
section.global(
GlobalType {
val_type: ValType::I32,
mutable: true,
shared: false,
},
&ConstExpr::i32_const(-1),
);
let process_pointer_size = needs_native_pointer_size.then(|| {
let index = section.len();
section.global(
GlobalType {
val_type: ValType::I32,
mutable: true,
shared: false,
},
&ConstExpr::i32_const(0),
);
index
});
let provider_value = semantics.state_provider().and_then(|provider| {
let ty = wasm_ir
.standard_library()
.state_provider(provider)
.process_type;
if wasm_ir
.standard_library()
.state_provider(provider)
.attachment
== StateProviderAttachment::Identity
{
return None;
}
let index = section.len();
let initial = match wasm_ir.standard_library().type_decl(ty).representation {
RuntimeRepresentation::Scalar {
storage: CoreTypeId::I64,
} => ConstExpr::i64_const(0),
RuntimeRepresentation::GcStruct { .. }
| RuntimeRepresentation::GcArray { .. }
| RuntimeRepresentation::Enum { .. } => {
ConstExpr::ref_null(HeapType::Concrete(gc.standard_index(ty)))
}
representation => {
unreachable!("unsupported state-provider representation: {representation:?}")
}
};
section.global(
GlobalType {
val_type: gc.val_type(Type::Standard(ty)),
mutable: true,
shared: false,
},
&initial,
);
Some(index)
});
let mut provider_values = HashMap::new();
for (_, provider, _) in provider_alternatives {
let declaration = wasm_ir.standard_library().state_provider(*provider);
if declaration.attachment == StateProviderAttachment::Identity {
continue;
}
let ty = declaration.process_type;
let index = section.len();
let initial = match wasm_ir.standard_library().type_decl(ty).representation {
RuntimeRepresentation::Scalar {
storage: CoreTypeId::I64,
} => ConstExpr::i64_const(0),
RuntimeRepresentation::GcStruct { .. }
| RuntimeRepresentation::GcArray { .. }
| RuntimeRepresentation::Enum { .. } => {
ConstExpr::ref_null(HeapType::Concrete(gc.standard_index(ty)))
}
representation => {
unreachable!("unsupported state-provider representation: {representation:?}")
}
};
section.global(
GlobalType {
val_type: gc.val_type(Type::Standard(ty)),
mutable: true,
shared: false,
},
&initial,
);
provider_values.insert(*provider, index);
}
let provider_attachment_frame = provider_attachment.map(|attachment| {
let index = section.len();
let frame_type = gc.function_frame_index(attachment);
section.global(
GlobalType {
val_type: ValType::Ref(RefType {
nullable: true,
heap_type: HeapType::Concrete(frame_type),
}),
mutable: true,
shared: false,
},
&ConstExpr::ref_null(HeapType::Concrete(frame_type)),
);
index
});
let mut provider_attachment_frames = HashMap::new();
for (variant, _, attachment) in provider_alternatives {
let index = section.len();
let frame_type = gc.function_frame_index(attachment);
section.global(
GlobalType {
val_type: ValType::Ref(RefType {
nullable: true,
heap_type: HeapType::Concrete(frame_type),
}),
mutable: true,
shared: false,
},
&ConstExpr::ref_null(HeapType::Concrete(frame_type)),
);
provider_attachment_frames.insert(*variant, index);
}
let provider_preparation_value = provider_preparation.map(|preparation| {
let completion = semantic_type(
semantics.specialize_type(
preparation,
semantics
.function_completion(preparation.function)
.expect("checked provider preparation has a completion type"),
),
semantics,
);
let index = section.len();
section.global(
GlobalType {
val_type: gc.val_type(completion),
mutable: true,
shared: false,
},
&default_const_expr(gc.val_type(completion)),
);
index
});
let provider_preparation_frame = provider_preparation.map(|preparation| {
let index = section.len();
let frame_type = gc.function_frame_index(preparation);
section.global(
GlobalType {
val_type: ValType::Ref(RefType {
nullable: true,
heap_type: HeapType::Concrete(frame_type),
}),
mutable: true,
shared: false,
},
&ConstExpr::ref_null(HeapType::Concrete(frame_type)),
);
index
});
let provider_prepared = provider_preparation.map(|_| {
let index = section.len();
section.global(
GlobalType {
val_type: ValType::I32,
mutable: true,
shared: false,
},
&ConstExpr::i32_const(0),
);
index
});
let managed_state_reads =
managed_state_reads::encode(&mut section, semantics, gc, wasm_ir, managed);
let selected_provider_type = program
.state
.as_ref()
.and_then(|state| state.provider_value)
.map(|value| value_type(value, semantics));
let selected_provider = selected_provider_type.map(|provider_type| {
let selected = section.len();
let mut val_type = gc.val_type(provider_type);
if let ValType::Ref(reference) = &mut val_type {
reference.nullable = true;
}
section.global(
GlobalType {
val_type,
mutable: true,
shared: false,
},
&default_const_expr(val_type),
);
selected
});
let nullable_state = ValType::Ref(RefType {
nullable: true,
heap_type: HeapType::Concrete(STATE_TYPE),
});
let current = section.len();
section.global(
GlobalType {
val_type: nullable_state,
mutable: true,
shared: false,
},
&ConstExpr::ref_null(HeapType::Concrete(STATE_TYPE)),
);
let old = section.len();
section.global(
GlobalType {
val_type: nullable_state,
mutable: true,
shared: false,
},
&ConstExpr::ref_null(HeapType::Concrete(STATE_TYPE)),
);
let attach_ready = section.len();
section.global(
GlobalType {
val_type: ValType::I32,
mutable: true,
shared: false,
},
&ConstExpr::i32_const(0),
);
let state_ready = section.len();
section.global(
GlobalType {
val_type: ValType::I32,
mutable: true,
shared: false,
},
&ConstExpr::i32_const(0),
);
let observed_timer_state = program
.actions
.iter()
.any(|action| matches!(action.kind, ActionKind::OnStart | ActionKind::OnReset))
.then(|| {
let index = section.len();
section.global(
GlobalType {
val_type: ValType::I32,
mutable: true,
shared: false,
},
&ConstExpr::i32_const(-1),
);
index
});
let attempt_ready = wasm_ir.attempt_globals().next().is_some().then(|| {
let index = section.len();
section.global(
GlobalType {
val_type: ValType::I32,
mutable: true,
shared: false,
},
&ConstExpr::i32_const(0),
);
index
});
let debug_depth = section.len();
section.global(
GlobalType {
val_type: ValType::I32,
mutable: true,
shared: false,
},
&ConstExpr::i32_const(0),
);
let future_poll_epoch = section.len();
section.global(
GlobalType {
val_type: ValType::I64,
mutable: true,
shared: false,
},
&ConstExpr::i64_const(0),
);
let async_frame = section.len();
section.global(
GlobalType {
val_type: ValType::Ref(RefType {
nullable: true,
heap_type: HeapType::Concrete(gc.async_frame_index()),
}),
mutable: true,
shared: false,
},
&ConstExpr::ref_null(HeapType::Concrete(gc.async_frame_index())),
);
let while_attached_result = wasm_ir
.body(wasm_ir::BodyOwner::Action(ActionKind::WhileAttached))
.is_some_and(|body| matches!(body.abi, wasm_ir::BodyAbi::AsyncAction))
.then(|| {
let index = section.len();
section.global(
GlobalType {
val_type: ValType::I32,
mutable: true,
shared: false,
},
&ConstExpr::i32_const(1),
);
index
});
let mut variables = HashMap::new();
let mut variable_types = HashMap::new();
if let Some(state) = &program.state
&& let (Some(value), Some(global), Some(ty)) = (
state.provider_value,
selected_provider,
selected_provider_type,
)
{
variables.insert(value, global);
variable_types.insert(value, ty);
}
for variable in &program.globals {
let mut bindings = Vec::new();
variable
.binding
.visit_bindings(&mut |binding| bindings.push(binding.id));
let simple = variable.binding.simple_binding().is_some();
for binding in bindings
.into_iter()
.filter(|binding| wasm_ir.contains_global(*binding))
{
let ty = value_type(binding, semantics);
if !ty.has_runtime_value() {
variables.insert(binding, u32::MAX);
variable_types.insert(binding, ty);
continue;
}
let index = section.len();
let mut val_type = gc.val_type(ty);
let runtime_initialized = variable
.value
.as_ref()
.is_some_and(|value| !simple || !is_wasm_global_constant(value.id, wasm_ir));
if (variable.value.is_none() || runtime_initialized)
&& let ValType::Ref(reference) = &mut val_type
{
// The source value is non-null whenever user code may observe it,
// but Wasm storage needs a null placeholder before module-start or
// lifecycle initialization has populated the value.
reference.nullable = true;
}
let global_type = GlobalType {
val_type,
mutable: variable.mutable,
shared: false,
};
if let Type::Option(option) = ty {
section.global(
global_type,
&ConstExpr::ref_null(HeapType::Concrete(gc.index(Type::Option(option)))),
);
} else if let ValType::Ref(reference) = gc.val_type(ty) {
section.global(global_type, &ConstExpr::ref_null(reference.heap_type));
} else if simple
&& let Some(value) = &variable.value
&& is_wasm_global_constant(value.id, wasm_ir)
{
section.global(global_type, &constant(value.id, wasm_ir, ty));
} else {
section.global(global_type, &default_const_expr(gc.val_type(ty)));
}
variables.insert(binding, index);
variable_types.insert(binding, ty);
}
}
let mut settings = HashMap::new();
for setting in &program.settings {
let Some(ty) = semantics.value_type(setting.id) else {
continue;
};
let ty = semantic_type(ty, semantics);
let current = section.len();
emit_setting_global(&mut section, ty, gc, wasm_ir.standard_library());
let old = section.len();
emit_setting_global(&mut section, ty, gc, wasm_ir.standard_library());
settings.insert(setting.id, SettingStorage { current, old, ty });
}
GlobalPlan {
section,
runtime: RuntimeGlobals {
process,
process_name,
process_pointer_size,
provider_value,
provider_attachment_frame,
provider_preparation_value,
provider_preparation_frame,
provider_prepared,
selected_provider,
current,
old,
attach_ready,
state_ready,
observed_timer_state,
attempt_ready,
debug_depth,
future_poll_epoch,
async_frame,
while_attached_result,
},
variables,
variable_types,
settings,
managed_state_reads,
provider_values,
provider_attachment_frames,
}
}
fn default_const_expr(ty: ValType) -> ConstExpr {
match ty {
ValType::I32 => ConstExpr::i32_const(0),
ValType::I64 => ConstExpr::i64_const(0),
ValType::F32 => ConstExpr::f32_const(0.0.into()),
ValType::F64 => ConstExpr::f64_const(0.0.into()),
ValType::Ref(reference) => ConstExpr::ref_null(reference.heap_type),
ValType::V128 => unreachable!("SplitScript has no v128 source values"),
}
}
fn emit_setting_global(
section: &mut GlobalSection,
ty: Type,
gc: &GcLayout,
standard_library: &StandardLibrary,
) {
let global_type = GlobalType {
val_type: gc.val_type(ty),
mutable: true,
shared: false,
};
match ty {
Type::Bool => section.global(global_type, &ConstExpr::i32_const(0)),
Type::Standard(StdlibTypeId::String) => section.global(
global_type,
&ConstExpr::ref_null(HeapType::Concrete(gc.standard_index(StdlibTypeId::String))),
),
ty if ty.is_enum(standard_library) => section.global(
global_type,
&ConstExpr::ref_null(HeapType::Concrete(gc.index(ty))),
),
_ => unreachable!(),
};
}