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
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-Exception
// Copyright 2024 Daniel Fox Franke.

//! Definition and impls for [`DecodeNode`] and related types.

use crate::cast::{CastSign, Overflow};
use crate::error::AssemblerError;
use crate::items::LabelRef;
use crate::resolver::Resolver;
use crate::strings::{MysteryString, Utf32String};
use bytes::BufMut;

/// A node in a decoding table.
#[derive(Debug, Clone)]
pub enum DecodeNode<L> {
    /// Branch left on 0, right on 1.
    Branch(Box<DecodeNode<L>>, Box<DecodeNode<L>>),
    /// Terminate decoding.
    StringTerminator,
    /// Emit a character whose encoding is unspecified and determined by the IO
    /// system (but probably Latin-1).
    MysteryChar(u8),
    /// Emit a string whose encoding is unspecified and determined by the IO
    /// system (but probably Latin-1).
    MysteryString(MysteryString),
    /// Emit a Unicode character.
    UnicodeChar(char),
    /// Emit a Unicode string.
    Utf32String(Utf32String),
    /// Emit the string or call the function found by dereferencing the given
    /// address.
    IndirectRef(LabelRef<L>),
    /// Emit the string or call the function found by doubly dereferencing the
    /// given address.
    DoubleIndirectRef(LabelRef<L>),
    /// Call the function found by derefencing the given address, passing it the
    /// given arguments.
    IndirectRefWithArgs(LabelRef<L>, Vec<DecodeArg<L>>),
    /// Call the function found by doubly derefencing the given address, passing
    /// it the given arguments.
    DoubleIndirectRefWithArgs(LabelRef<L>, Vec<DecodeArg<L>>),
}

pub(crate) enum ResolvedDecodeNode {
    Branch(Box<ResolvedDecodeNode>, Box<ResolvedDecodeNode>),
    StringTerminator,
    MysteryChar(u8),
    MysteryString(MysteryString),
    UnicodeChar(char),
    Utf32String(Utf32String),
    IndirectRef(u32),
    DoubleIndirectRef(u32),
    IndirectRefWithArgs(u32, Vec<i32>),
    DoubleIndirectRefWithArgs(u32, Vec<i32>),
}

/// Argument to a function invoked from a decoding table.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum DecodeArg<L> {
    /// Argument is the absolute address of the given label+offset.
    Label(LabelRef<L>),
    /// Argument is the given literal.
    Literal(i32),
}

impl<L> DecodeArg<L> {
    /// Applies the given mapping function to the label within the argument, if any.
    pub fn map<F, M>(self, f: F) -> DecodeArg<M>
    where
        F: FnMut(L) -> M,
    {
        match self {
            DecodeArg::Label(l) => DecodeArg::Label(l.map(f)),
            DecodeArg::Literal(x) => DecodeArg::Literal(x),
        }
    }

    pub(crate) fn resolve<R>(&self, resolver: &R) -> Result<i32, AssemblerError<L>>
    where
        R: Resolver<Label = L>,
    {
        Ok(match self {
            DecodeArg::Label(l) => l.resolve_absolute(resolver)?.cast_sign(),
            DecodeArg::Literal(x) => *x,
        })
    }
}

impl<L> DecodeNode<L> {
    /// Applies the given mapping function to all labels within the node.
    pub fn map<F, M>(self, mut f: F) -> DecodeNode<M>
    where
        F: FnMut(L) -> M,
    {
        self.map_inner(&mut f)
    }

    // This method is recursive. This hack of taking a &mut F instead of an F is
    // necessary in order have a type we can reborrow and avoid infinite
    // recursion during trait resolution.
    fn map_inner<F, M>(self, f: &mut F) -> DecodeNode<M>
    where
        F: FnMut(L) -> M,
    {
        match self {
            DecodeNode::Branch(left, right) => DecodeNode::Branch(
                Box::new(left.map_inner(&mut *f)),
                Box::new(right.map_inner(&mut *f)),
            ),
            DecodeNode::StringTerminator => DecodeNode::StringTerminator,
            DecodeNode::MysteryChar(x) => DecodeNode::MysteryChar(x),
            DecodeNode::MysteryString(x) => DecodeNode::MysteryString(x),
            DecodeNode::UnicodeChar(x) => DecodeNode::UnicodeChar(x),
            DecodeNode::Utf32String(x) => DecodeNode::Utf32String(x),
            DecodeNode::IndirectRef(r) => DecodeNode::IndirectRef(r.map(f)),
            DecodeNode::DoubleIndirectRef(r) => DecodeNode::DoubleIndirectRef(r.map(f)),
            DecodeNode::IndirectRefWithArgs(r, args) => DecodeNode::IndirectRefWithArgs(
                r.map(&mut *f),
                args.into_iter().map(|arg| arg.map(&mut *f)).collect(),
            ),
            DecodeNode::DoubleIndirectRefWithArgs(r, args) => {
                DecodeNode::DoubleIndirectRefWithArgs(
                    r.map(&mut *f),
                    args.into_iter().map(|arg| arg.map(&mut *f)).collect(),
                )
            }
        }
    }

    pub(crate) fn len(&self) -> usize {
        match self {
            DecodeNode::Branch(left, right) => left.len() + right.len() + 9,
            DecodeNode::StringTerminator => 1,
            DecodeNode::MysteryChar(_) => 2,
            DecodeNode::MysteryString(s) => s.len() + 2,
            DecodeNode::UnicodeChar(_) => 5,
            DecodeNode::Utf32String(s) => s.byte_len() + 5,
            DecodeNode::IndirectRef(_) => 5,
            DecodeNode::DoubleIndirectRef(_) => 5,
            DecodeNode::IndirectRefWithArgs(_, args) => 4 * args.len() + 9,
            DecodeNode::DoubleIndirectRefWithArgs(_, args) => 4 * args.len() + 9,
        }
    }

    pub(crate) fn resolve<R>(&self, resolver: &R) -> Result<ResolvedDecodeNode, AssemblerError<L>>
    where
        R: Resolver<Label = L>,
    {
        Ok(match self {
            DecodeNode::Branch(left, right) => ResolvedDecodeNode::Branch(
                Box::new(left.resolve(resolver)?),
                Box::new(right.resolve(resolver)?),
            ),
            DecodeNode::StringTerminator => ResolvedDecodeNode::StringTerminator,
            DecodeNode::MysteryChar(c) => ResolvedDecodeNode::MysteryChar(*c),
            DecodeNode::MysteryString(s) => ResolvedDecodeNode::MysteryString(s.clone()),
            DecodeNode::UnicodeChar(c) => ResolvedDecodeNode::UnicodeChar(*c),
            DecodeNode::Utf32String(s) => ResolvedDecodeNode::Utf32String(s.clone()),
            DecodeNode::IndirectRef(r) => {
                ResolvedDecodeNode::IndirectRef(r.resolve_absolute(resolver)?)
            }
            DecodeNode::DoubleIndirectRef(r) => {
                ResolvedDecodeNode::DoubleIndirectRef(r.resolve_absolute(resolver)?)
            }
            DecodeNode::IndirectRefWithArgs(r, args) => {
                u32::try_from(args.len()).overflow()?; // We can't serialize this. Serialization is infallible, so check here instead.
                let mut newargs = Vec::with_capacity(args.len());
                for arg in args {
                    newargs.push(arg.resolve(resolver)?);
                }

                ResolvedDecodeNode::IndirectRefWithArgs(r.resolve_absolute(resolver)?, newargs)
            }
            DecodeNode::DoubleIndirectRefWithArgs(r, args) => {
                u32::try_from(args.len()).overflow()?;
                let mut newargs = Vec::with_capacity(args.len());
                for arg in args {
                    newargs.push(arg.resolve(resolver)?);
                }

                ResolvedDecodeNode::DoubleIndirectRefWithArgs(
                    r.resolve_absolute(resolver)?,
                    newargs,
                )
            }
        })
    }
}

impl ResolvedDecodeNode {
    pub(crate) fn count_nodes(&self) -> usize {
        match self {
            ResolvedDecodeNode::Branch(left, right) => 1 + left.count_nodes() + right.count_nodes(),
            _ => 1,
        }
    }

    pub(crate) fn len(&self) -> usize {
        match self {
            ResolvedDecodeNode::Branch(left, right) => left.len() + right.len() + 9,
            ResolvedDecodeNode::StringTerminator => 1,
            ResolvedDecodeNode::MysteryChar(_) => 2,
            ResolvedDecodeNode::MysteryString(s) => s.len() + 2,
            ResolvedDecodeNode::UnicodeChar(_) => 5,
            ResolvedDecodeNode::Utf32String(s) => s.byte_len() + 5,
            ResolvedDecodeNode::IndirectRef(_) => 5,
            ResolvedDecodeNode::DoubleIndirectRef(_) => 5,
            ResolvedDecodeNode::IndirectRefWithArgs(_, args) => 4 * args.len() + 9,
            ResolvedDecodeNode::DoubleIndirectRefWithArgs(_, args) => 4 * args.len() + 9,
        }
    }

    pub(crate) fn serialize<B>(&self, num: u32, mut buf: B)
    where
        B: BufMut,
    {
        self.serialize_inner(num, &mut buf)
    }

    fn serialize_inner<B>(&self, num: u32, buf: &mut B)
    where
        B: BufMut,
    {
        match self {
            ResolvedDecodeNode::Branch(left, right) => {
                let panic_msg = "decode tables with >= 2**32 nodes should have been rejected before serialization";
                let left_num = num.checked_add(1).expect(panic_msg);
                let right_num = left_num
                    .checked_add(left.count_nodes().try_into().expect(panic_msg))
                    .expect(panic_msg);
                buf.put_u8(0);
                left.serialize_inner(left_num, &mut *buf);
                right.serialize_inner(right_num, &mut *buf);
            }
            ResolvedDecodeNode::StringTerminator => {
                buf.put_u8(1);
            }
            ResolvedDecodeNode::MysteryChar(x) => {
                buf.put_u8(2);
                buf.put_u8(*x);
            }
            ResolvedDecodeNode::MysteryString(s) => {
                buf.put_u8(3);
                buf.put(s.to_bytes());
                buf.put_u8(0);
            }
            ResolvedDecodeNode::UnicodeChar(c) => {
                buf.put_u8(4);
                buf.put_u32((*c).into());
            }
            ResolvedDecodeNode::Utf32String(s) => {
                buf.put_u8(5);
                buf.put(s.to_bytes());
                buf.put_u32(0);
            }
            ResolvedDecodeNode::IndirectRef(r) => {
                buf.put_u8(8);
                buf.put_u32(*r);
            }
            ResolvedDecodeNode::DoubleIndirectRef(r) => {
                buf.put_u8(9);
                buf.put_u32(*r);
            }
            ResolvedDecodeNode::IndirectRefWithArgs(r, args) => {
                buf.put_u8(0xa);
                buf.put_u32(*r);
                buf.put_u32(
                    args.len().try_into().expect(
                        "refs with >= 2**32 args should have been rejected during resolution",
                    ),
                );
                for arg in args {
                    buf.put_i32(*arg)
                }
            }
            ResolvedDecodeNode::DoubleIndirectRefWithArgs(r, args) => {
                buf.put_u8(0xb);
                buf.put_u32(*r);
                buf.put_u32(
                    args.len().try_into().expect(
                        "refs with >= 2**32 args should have been rejected during resolution",
                    ),
                );
                for arg in args {
                    buf.put_i32(*arg)
                }
            }
        }
    }
}