|
| 1 | +mod consts; |
| 2 | +mod locals; |
| 3 | + |
| 4 | +use self::{consts::ConstRegistry, locals::LocalsRegistry}; |
| 5 | +use crate::core::{TypedVal, ValType}; |
| 6 | +use alloc::vec::Vec; |
| 7 | +use core::num::NonZeroUsize; |
| 8 | + |
| 9 | +#[derive(Debug, Clone)] |
| 10 | +pub struct ValueStack { |
| 11 | + operands: Vec<StackOperand>, |
| 12 | + consts: ConstRegistry, |
| 13 | + locals: LocalsRegistry, |
| 14 | + first_local: Option<OperandIdx>, |
| 15 | + last_local: Option<OperandIdx>, |
| 16 | + max_stack_height: usize, |
| 17 | +} |
| 18 | + |
| 19 | +#[derive(Debug, Copy, Clone)] |
| 20 | +pub struct OperandIdx(NonZeroUsize); |
| 21 | + |
| 22 | +impl From<OperandIdx> for usize { |
| 23 | + fn from(value: OperandIdx) -> Self { |
| 24 | + value.0.get() |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +impl From<usize> for OperandIdx { |
| 29 | + fn from(value: usize) -> Self { |
| 30 | + let Some(operand_idx) = NonZeroUsize::new(value.wrapping_add(1)) else { |
| 31 | + panic!("out of bounds `OperandIdx`: {value}") |
| 32 | + }; |
| 33 | + Self(operand_idx) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +#[derive(Debug, Copy, Clone)] |
| 38 | +enum StackOperand { |
| 39 | + Local { |
| 40 | + index: LocalIdx, |
| 41 | + prev_local: Option<OperandIdx>, |
| 42 | + next_local: Option<OperandIdx>, |
| 43 | + }, |
| 44 | + Temp { |
| 45 | + ty: ValType, |
| 46 | + }, |
| 47 | + Immediate { |
| 48 | + val: TypedVal, |
| 49 | + }, |
| 50 | +} |
| 51 | + |
| 52 | +#[derive(Debug, Copy, Clone)] |
| 53 | +pub enum Operand { |
| 54 | + Local(LocalOperand), |
| 55 | + Temp(TempOperand), |
| 56 | + Immediate(ImmediateOperand), |
| 57 | +} |
| 58 | + |
| 59 | +impl Operand { |
| 60 | + pub fn is_local(self) -> bool { |
| 61 | + matches!(self, Self::Local(_)) |
| 62 | + } |
| 63 | + |
| 64 | + pub fn is_temp(self) -> bool { |
| 65 | + matches!(self, Self::Temp(_)) |
| 66 | + } |
| 67 | + |
| 68 | + pub fn is_immediate(self) -> bool { |
| 69 | + matches!(self, Self::Immediate(_)) |
| 70 | + } |
| 71 | + |
| 72 | + pub fn ty(self) -> ValType { |
| 73 | + match self { |
| 74 | + Self::Local(local_operand) => local_operand.ty(), |
| 75 | + Self::Temp(temp_operand) => temp_operand.ty(), |
| 76 | + Self::Immediate(immediate_operand) => immediate_operand.ty(), |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +#[derive(Debug, Copy, Clone)] |
| 82 | +pub struct LocalOperand { |
| 83 | + index: LocalIdx, |
| 84 | + ty: ValType, |
| 85 | +} |
| 86 | + |
| 87 | +impl LocalOperand { |
| 88 | + pub fn index(self) -> LocalIdx { |
| 89 | + self.index |
| 90 | + } |
| 91 | + |
| 92 | + pub fn ty(self) -> ValType { |
| 93 | + self.ty |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +#[derive(Debug, Copy, Clone)] |
| 98 | +pub struct TempOperand { |
| 99 | + ty: ValType, |
| 100 | +} |
| 101 | + |
| 102 | +impl TempOperand { |
| 103 | + pub fn ty(self) -> ValType { |
| 104 | + self.ty |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +#[derive(Debug, Copy, Clone)] |
| 109 | +pub struct ImmediateOperand { |
| 110 | + val: TypedVal, |
| 111 | +} |
| 112 | + |
| 113 | +impl ImmediateOperand { |
| 114 | + pub fn val(self) -> TypedVal { |
| 115 | + self.val |
| 116 | + } |
| 117 | + |
| 118 | + pub fn ty(self) -> ValType { |
| 119 | + self.val.ty() |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +#[derive(Debug, Copy, Clone)] |
| 124 | +pub struct LocalIdx(usize); |
0 commit comments