sleigh_rs/semantic/
disassembly.rs1use crate::semantic::{
2 ContextId, InstNext, InstStart, Span, TableId, TokenFieldId,
3};
4use crate::{Number, NumberNonZeroUnsigned, NumberUnsigned, SpaceId};
5
6#[derive(Clone, Debug, Copy)]
7pub enum OpUnary {
8 Negation,
9 Negative,
10}
11
12#[derive(Clone, Debug, Copy, PartialEq, Eq, PartialOrd, Ord)]
13pub enum Op {
14 Add,
15 Sub,
16 Mul,
17 Div,
18 And,
19 Or,
20 Xor,
21 Asr,
22 Lsl,
23}
24
25#[derive(Clone, Debug)]
26pub enum ReadScope {
27 Integer(Number),
30 Context(ContextId),
31 TokenField(TokenFieldId),
32 InstStart(InstStart),
33 InstNext(InstNext),
34 Local(VariableId),
35}
36
37#[derive(Clone, Debug)]
38pub enum WriteScope {
39 Context(ContextId),
40 Local(VariableId),
41}
42
43#[derive(Clone, Debug)]
44pub enum AddrScope {
45 Integer(NumberUnsigned),
46 Table(TableId),
47 InstStart(InstStart),
50 InstNext(InstNext),
51 Local(VariableId),
52}
53
54#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
55pub struct VariableId(pub usize);
56
57#[derive(Clone, Copy, Debug)]
58pub enum VariableType {
59 Reference(SpaceId),
60 Value(Option<NumberNonZeroUnsigned>),
61}
62
63impl VariableType {
64 pub(crate) fn set_len(self, len: NumberNonZeroUnsigned) -> Option<Self> {
65 match self {
66 VariableType::Reference(_) => None,
67 VariableType::Value(None) => Some(Self::Value(Some(len))),
68 VariableType::Value(Some(current_len)) if current_len == len => {
69 Some(self)
70 }
71 VariableType::Value(Some(_)) => None,
72 }
73 }
74 pub(crate) fn set_space(self, id: SpaceId) -> Option<Self> {
75 match self {
76 VariableType::Reference(current_id) if current_id == id => {
77 Some(self)
78 }
79 VariableType::Value(None) => Some(Self::Reference(id)),
80 VariableType::Reference(_) | VariableType::Value(Some(_)) => None,
81 }
82 }
83}
84
85#[derive(Clone, Debug)]
86pub struct Variable {
87 pub(crate) name: Box<str>,
88 pub location: Span,
89 pub value_type: VariableType,
91}
92
93impl Variable {
94 pub fn name(&self) -> &str {
95 &self.name
96 }
97}
98
99#[derive(Clone, Debug)]
100pub struct Expr {
101 pub(crate) rpn: Box<[ExprElement]>,
102}
103
104impl Expr {
105 pub fn elements(&self) -> &[ExprElement] {
106 &self.rpn
107 }
108}
109
110#[derive(Clone, Debug)]
111pub enum ExprElement {
112 Value { value: ReadScope, location: Span },
113 Op(Op),
114 OpUnary(OpUnary),
115}
116
117#[derive(Clone, Debug)]
118pub struct GlobalSet {
119 pub location: Span,
120 pub address: AddrScope,
121 pub context: ContextId,
122}
123
124#[derive(Clone, Debug)]
125pub struct Assignment {
126 pub left: WriteScope,
127 pub right: Expr,
128}
129
130#[derive(Clone, Debug)]
131pub enum Assertation {
132 GlobalSet(GlobalSet),
133 Assignment(Assignment),
134}