|
1 | 1 | //! Instruction IR with symbolic labels. |
2 | 2 | //! |
3 | 3 | //! Pre-layout instructions use `Label` for symbolic references. |
4 | | -//! After layout, labels are resolved to `StepId` for serialization. |
| 4 | +//! After layout, labels are resolved to step addresses (u16) for serialization. |
5 | 5 | //! Member indices use deferred resolution via `MemberRef`. |
6 | 6 |
|
7 | 7 | use std::collections::BTreeMap; |
8 | 8 | use std::num::NonZeroU16; |
9 | 9 |
|
10 | 10 | use super::effects::{EffectOp, EffectOpcode}; |
11 | | -use super::ids::StepId; |
12 | | -use super::instructions::{Call, Match, Return, Trampoline, select_match_opcode}; |
| 11 | +use super::instructions::{Call, Match, Return, StepAddr, StepId, Trampoline, select_match_opcode}; |
13 | 12 | use super::nav::Nav; |
14 | 13 | use crate::analyze::type_check::TypeId; |
15 | 14 |
|
16 | | -/// Symbolic reference, resolved to StepId at layout time. |
| 15 | +/// Symbolic reference, resolved to step address at layout time. |
17 | 16 | #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] |
18 | 17 | pub struct Label(pub u32); |
19 | 18 |
|
20 | 19 | impl Label { |
21 | | - /// Resolve this label to a StepId using the layout mapping. |
| 20 | + /// Resolve this label to a step address using the layout mapping. |
22 | 21 | #[inline] |
23 | | - pub fn resolve(self, map: &BTreeMap<Label, StepId>) -> StepId { |
| 22 | + pub fn resolve(self, map: &BTreeMap<Label, StepAddr>) -> StepAddr { |
24 | 23 | *map.get(&self).expect("label not in layout") |
25 | 24 | } |
26 | 25 | } |
@@ -197,7 +196,7 @@ impl Instruction { |
197 | 196 | /// - `get_member_base`: maps parent TypeId to member base index |
198 | 197 | pub fn resolve<F, G>( |
199 | 198 | &self, |
200 | | - map: &BTreeMap<Label, StepId>, |
| 199 | + map: &BTreeMap<Label, StepAddr>, |
201 | 200 | lookup_member: F, |
202 | 201 | get_member_base: G, |
203 | 202 | ) -> Vec<u8> |
@@ -263,15 +262,19 @@ impl MatchIR { |
263 | 262 | /// - `get_member_base`: maps parent TypeId to member base index |
264 | 263 | pub fn resolve<F, G>( |
265 | 264 | &self, |
266 | | - map: &BTreeMap<Label, StepId>, |
| 265 | + map: &BTreeMap<Label, StepAddr>, |
267 | 266 | lookup_member: F, |
268 | 267 | get_member_base: G, |
269 | 268 | ) -> Vec<u8> |
270 | 269 | where |
271 | 270 | F: Fn(plotnik_core::Symbol, TypeId) -> Option<u16>, |
272 | 271 | G: Fn(TypeId) -> Option<u16>, |
273 | 272 | { |
274 | | - let successors: Vec<StepId> = self.successors.iter().map(|&l| l.resolve(map)).collect(); |
| 273 | + let successors: Vec<StepId> = self |
| 274 | + .successors |
| 275 | + .iter() |
| 276 | + .map(|&l| StepId::new(l.resolve(map))) |
| 277 | + .collect(); |
275 | 278 |
|
276 | 279 | // Resolve effect member references to absolute indices |
277 | 280 | let pre_effects: Vec<EffectOp> = self |
@@ -323,13 +326,13 @@ pub struct CallIR { |
323 | 326 |
|
324 | 327 | impl CallIR { |
325 | 328 | /// Resolve labels and serialize to bytecode bytes. |
326 | | - pub fn resolve(&self, map: &BTreeMap<Label, StepId>) -> [u8; 8] { |
| 329 | + pub fn resolve(&self, map: &BTreeMap<Label, StepAddr>) -> [u8; 8] { |
327 | 330 | let c = Call { |
328 | 331 | segment: 0, |
329 | 332 | nav: self.nav, |
330 | 333 | node_field: self.node_field, |
331 | | - next: self.next.resolve(map), |
332 | | - target: self.target.resolve(map), |
| 334 | + next: StepId::new(self.next.resolve(map)), |
| 335 | + target: StepId::new(self.target.resolve(map)), |
333 | 336 | }; |
334 | 337 | c.to_bytes() |
335 | 338 | } |
@@ -364,20 +367,20 @@ pub struct TrampolineIR { |
364 | 367 |
|
365 | 368 | impl TrampolineIR { |
366 | 369 | /// Resolve labels and serialize to bytecode bytes. |
367 | | - pub fn resolve(&self, map: &BTreeMap<Label, StepId>) -> [u8; 8] { |
| 370 | + pub fn resolve(&self, map: &BTreeMap<Label, StepAddr>) -> [u8; 8] { |
368 | 371 | let t = Trampoline { |
369 | 372 | segment: 0, |
370 | | - next: self.next.resolve(map), |
| 373 | + next: StepId::new(self.next.resolve(map)), |
371 | 374 | }; |
372 | 375 | t.to_bytes() |
373 | 376 | } |
374 | 377 | } |
375 | 378 |
|
376 | | -/// Result of layout: maps labels to step IDs. |
| 379 | +/// Result of layout: maps labels to step addresses. |
377 | 380 | #[derive(Clone, Debug)] |
378 | 381 | pub struct LayoutResult { |
379 | | - /// Mapping from symbolic labels to concrete step IDs. |
380 | | - pub label_to_step: BTreeMap<Label, StepId>, |
| 382 | + /// Mapping from symbolic labels to concrete step addresses (raw u16). |
| 383 | + pub label_to_step: BTreeMap<Label, StepAddr>, |
381 | 384 | /// Total number of steps (for header). |
382 | 385 | pub total_steps: u16, |
383 | 386 | } |
@@ -464,7 +467,7 @@ mod tests { |
464 | 467 | }; |
465 | 468 |
|
466 | 469 | let mut map = BTreeMap::new(); |
467 | | - map.insert(Label(0), StepId::new(1)); |
| 470 | + map.insert(Label(0), 1u16); |
468 | 471 |
|
469 | 472 | let bytes = m.resolve(&map, |_, _| None, |_| None); |
470 | 473 | assert_eq!(bytes.len(), 8); |
|
0 commit comments