msg_tool\scripts\softpal\scr/
disasm.rs

1use crate::ext::io::*;
2use anyhow::Result;
3use int_enum::IntEnum;
4use std::collections::HashMap;
5use std::io::{Read, Write};
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8enum Oper {
9    P,
10    I,
11    L,
12}
13
14use Oper::*;
15
16const OPS: [(u16, (Option<&'static str>, &'static [Oper])); 210] = [
17    (0x0001, (Some("mov"), &[P, P])),
18    (0x0002, (Some("add"), &[P, P])),
19    (0x0003, (Some("sub"), &[P, P])),
20    (0x0004, (Some("mul"), &[P, P])),
21    (0x0005, (Some("div"), &[P, P])),
22    (0x0006, (Some("binand"), &[P, P])),
23    (0x0007, (Some("binor"), &[P, P])),
24    (0x0008, (Some("binxor"), &[P, P])),
25    (0x0009, (Some("jmp"), &[L])),
26    (0x000A, (Some("jz"), &[L, P])),
27    (0x000B, (Some("call"), &[L])),
28    (0x000C, (Some("eq"), &[P, P])),
29    (0x000D, (Some("neq"), &[P, P])),
30    (0x000E, (Some("le"), &[P, P])),
31    (0x000F, (Some("ge"), &[P, P])),
32    (0x0010, (Some("lt"), &[P, P])),
33    (0x0011, (Some("gt"), &[P, P])),
34    (0x0012, (Some("logor"), &[P, P])),
35    (0x0013, (Some("logand"), &[P, P])),
36    (0x0014, (Some("not"), &[I])),
37    (0x0015, (Some("exit"), &[])),
38    (0x0016, (Some("nop"), &[])),
39    (0x0017, (Some("syscall"), &[I, I])),
40    (0x0018, (Some("ret"), &[])),
41    (0x0019, (None, &[])),
42    (0x001A, (Some("mod"), &[P, P])),
43    (0x001B, (Some("shl"), &[P, P])),
44    (0x001C, (Some("sar"), &[P, P])),
45    (0x001D, (Some("neg"), &[I])),
46    (0x001E, (Some("pop"), &[P])),
47    (0x001F, (Some("push"), &[P])),
48    (0x0020, (Some("enter"), &[P])),
49    (0x0021, (Some("leave"), &[P])),
50    (0x0023, (Some("create_message"), &[])),
51    (0x0024, (Some("get_message"), &[])),
52    (0x0025, (Some("get_message_param"), &[])),
53    (0x0028, (Some("se_load"), &[])),
54    (0x0029, (Some("se_play"), &[])),
55    (0x002A, (Some("se_play_ex"), &[])),
56    (0x002B, (Some("se_stop"), &[])),
57    (0x002C, (Some("se_set_volume"), &[])),
58    (0x002D, (Some("se_get_volume"), &[])),
59    (0x002E, (Some("se_unload"), &[])),
60    (0x002F, (Some("se_wait"), &[])),
61    (0x0030, (Some("set_se_info"), &[])),
62    (0x0031, (Some("get_se_ex_volume"), &[])),
63    (0x0032, (Some("set_se_ex_volume"), &[])),
64    (0x0033, (Some("se_enable"), &[])),
65    (0x0034, (Some("is_se_enable"), &[])),
66    (0x0035, (Some("se_set_pan"), &[])),
67    (0x0036, (Some("se_mute"), &[])),
68    (0x0038, (Some("select_init"), &[])),
69    (0x0039, (Some("select"), &[])),
70    (0x003A, (Some("select_add_choice"), &[])),
71    (0x003B, (Some("end_select"), &[])),
72    (0x003C, (Some("select_clear"), &[])),
73    (0x003D, (Some("select_set_offset"), &[])),
74    (0x003E, (Some("select_set_process"), &[])),
75    (0x003F, (Some("select_lock"), &[])),
76    (0x0040, (Some("get_select_on_key"), &[])),
77    (0x0041, (Some("get_select_pull_key"), &[])),
78    (0x0042, (Some("get_select_push_key"), &[])),
79    (0x0044, (Some("skip_set"), &[])),
80    (0x0045, (Some("skip_is"), &[])),
81    (0x0046, (Some("auto_set"), &[])),
82    (0x0047, (Some("auto_is"), &[])),
83    (0x0048, (Some("auto_set_time"), &[])),
84    (0x0049, (Some("auto_get_time"), &[])),
85    (0x004A, (Some("window_set_mode"), &[])),
86    (0x004B, (None, &[])),
87    (0x004C, (None, &[])),
88    (0x004D, (None, &[])),
89    (0x004E, (None, &[])),
90    (0x004F, (Some("effect_enable_is"), &[])),
91    (0x0050, (Some("cursor_pos_get"), &[])),
92    (0x0051, (Some("time_get"), &[])),
93    (0x0052, (None, &[])),
94    (0x0053, (Some("load_font"), &[])),
95    (0x0054, (Some("unload_font"), &[])),
96    (0x0055, (Some("set_font_type"), &[])),
97    (0x0056, (Some("key_cancel"), &[])),
98    (0x0057, (Some("set_font_color"), &[])),
99    (0x0058, (Some("load_font_ex"), &[])),
100    (0x0059, (None, &[])),
101    (0x005A, (None, &[])),
102    (0x005B, (Some("lpush"), &[])),
103    (0x005C, (Some("lpop"), &[])),
104    (0x005D, (None, &[])),
105    (0x005E, (None, &[])),
106    (0x005F, (Some("set_font_size"), &[])),
107    (0x0060, (Some("get_font_size"), &[])),
108    (0x0061, (Some("get_font_type"), &[])),
109    (0x0062, (Some("set_font_effect"), &[])),
110    (0x0063, (Some("get_font_effect"), &[])),
111    (0x0064, (Some("get_pull_key"), &[])),
112    (0x0065, (Some("get_on_key"), &[])),
113    (0x0066, (Some("get_push_key"), &[])),
114    (0x0067, (Some("input_clear"), &[])),
115    (0x0068, (Some("change_window_size"), &[])),
116    (0x0069, (Some("change_aspect_mode"), &[])),
117    (0x006A, (Some("aspect_position_enable"), &[])),
118    (0x006B, (None, &[])),
119    (0x006C, (Some("get_aspect_mode"), &[])),
120    (0x006D, (Some("get_monitor_size"), &[])),
121    (0x006E, (Some("get_window_pos"), &[])),
122    (0x006F, (Some("get_system_metrics"), &[])),
123    (0x0070, (Some("set_system_path"), &[])),
124    (0x0071, (Some("set_allmosaicthumbnail"), &[])),
125    (0x0072, (Some("enable_window_change"), &[])),
126    (0x0073, (Some("is_enable_window_change"), &[])),
127    (0x0074, (Some("set_cursor"), &[])),
128    (0x0075, (Some("set_hide_cursor_time"), &[])),
129    (0x0076, (Some("get_hide_cursor_time"), &[])),
130    (0x0077, (Some("scene_skip"), &[])),
131    (0x0078, (Some("cancel_scene_skip"), &[])),
132    (0x0079, (Some("lsize"), &[])),
133    (0x007A, (Some("get_async_key"), &[])),
134    (0x007B, (Some("get_font_color"), &[])),
135    (0x007C, (Some("get_current_date"), &[])),
136    (0x007D, (Some("history_skip"), &[])),
137    (0x007E, (Some("cancel_history_skip"), &[])),
138    (0x007F, (None, &[])),
139    (0x0081, (Some("system_btn_set"), &[])),
140    (0x0082, (Some("system_btn_release"), &[])),
141    (0x0083, (Some("system_btn_enable"), &[])),
142    (0x0086, (Some("text_init"), &[])),
143    (0x0087, (Some("text_set_icon"), &[])),
144    (0x0088, (Some("text"), &[])),
145    (0x0089, (Some("text_hide"), &[])),
146    (0x008A, (Some("text_show"), &[])),
147    (0x008B, (Some("text_set_btn"), &[])),
148    (0x008C, (Some("text_uninit"), &[])),
149    (0x008D, (Some("text_set_rect"), &[])),
150    (0x008E, (Some("text_clear"), &[])),
151    (0x008F, (None, &[])),
152    (0x0090, (Some("text_get_time"), &[])),
153    (0x0091, (Some("text_window_set_alpha"), &[])),
154    (0x0092, (Some("text_voice_play"), &[])),
155    (0x0093, (None, &[])),
156    (0x0094, (Some("text_set_icon_animation_time"), &[])),
157    (0x0095, (Some("text_w"), &[])),
158    (0x0096, (Some("text_a"), &[])),
159    (0x0097, (Some("text_wa"), &[])),
160    (0x0098, (Some("text_n"), &[])),
161    (0x0099, (Some("text_cat"), &[])),
162    (0x009A, (Some("set_history"), &[])),
163    (0x009B, (Some("is_text_visible"), &[])),
164    (0x009C, (Some("text_set_base"), &[])),
165    (0x009D, (Some("enable_voice_cut"), &[])),
166    (0x009E, (Some("is_voice_cut"), &[])),
167    (0x009F, (None, &[])),
168    (0x00A0, (None, &[])),
169    (0x00A1, (None, &[])),
170    (0x00A2, (Some("text_set_color"), &[])),
171    (0x00A3, (Some("text_redraw"), &[])),
172    (0x00A4, (Some("set_text_mode"), &[])),
173    (0x00A5, (Some("text_init_visualnovelmode"), &[])),
174    (0x00A6, (Some("text_set_icon_mode"), &[])),
175    (0x00A7, (Some("text_vn_br"), &[])),
176    (0x00A8, (None, &[])),
177    (0x00A9, (None, &[])),
178    (0x00AA, (None, &[])),
179    (0x00AB, (None, &[])),
180    (0x00AC, (Some("tips_get_str"), &[])),
181    (0x00AD, (Some("tips_get_param"), &[])),
182    (0x00AE, (Some("tips_reset"), &[])),
183    (0x00AF, (Some("tips_search"), &[])),
184    (0x00B0, (Some("tips_set_color"), &[])),
185    (0x00B1, (Some("tips_stop"), &[])),
186    (0x00B2, (Some("tips_get_flag"), &[])),
187    (0x00B3, (Some("tips_init"), &[])),
188    (0x00B4, (Some("tips_pause"), &[])),
189    (0x00B6, (Some("voice_play"), &[])),
190    (0x00B7, (Some("voice_stop"), &[])),
191    (0x00B8, (Some("voice_set_volume"), &[])),
192    (0x00B9, (Some("voice_get_volume"), &[])),
193    (0x00BA, (Some("set_voice_info"), &[])),
194    (0x00BB, (Some("voice_enable"), &[])),
195    (0x00BC, (Some("is_voice_enable"), &[])),
196    (0x00BD, (None, &[])),
197    (0x00BE, (Some("bgv_play"), &[])),
198    (0x00BF, (Some("bgv_stop"), &[])),
199    (0x00C0, (Some("bgv_enable"), &[])),
200    (0x00C1, (Some("get_voice_ex_volume"), &[])),
201    (0x00C2, (Some("set_voice_ex_volume"), &[])),
202    (0x00C3, (Some("voice_check_enable"), &[])),
203    (0x00C4, (Some("voice_autopan_initialize"), &[])),
204    (0x00C5, (Some("voice_autopan_enable"), &[])),
205    (0x00C6, (Some("set_voice_autopan"), &[])),
206    (0x00C7, (Some("is_voice_autopan_enable"), &[])),
207    (0x00C8, (Some("voice_wait"), &[])),
208    (0x00C9, (Some("bgv_pause"), &[])),
209    (0x00CA, (Some("bgv_mute"), &[])),
210    (0x00CB, (Some("set_bgv_volume"), &[])),
211    (0x00CC, (Some("get_bgv_volume"), &[])),
212    (0x00CD, (Some("set_bgv_auto_volume"), &[])),
213    (0x00CE, (Some("voice_mute"), &[])),
214    (0x00CF, (Some("voice_call"), &[])),
215    (0x00D0, (Some("voice_call_clear"), &[])),
216    (0x00D2, (Some("wait"), &[])),
217    (0x00D3, (Some("wait_click"), &[])),
218    (0x00D4, (Some("wait_sync_begin"), &[])),
219    (0x00D5, (Some("wait_sync"), &[])),
220    (0x00D6, (Some("wait_sync_end"), &[])),
221    (0x00D7, (None, &[])),
222    (0x00D8, (Some("wait_clear"), &[])),
223    (0x00D9, (Some("wait_click_no_anim"), &[])),
224    (0x00DA, (Some("wait_sync_get_time"), &[])),
225    (0x00DB, (Some("wait_time_push"), &[])),
226    (0x00DC, (Some("wait_time_pop"), &[])),
227];
228const MOV: u16 = 0x0001;
229const CALL: u16 = 0x000B;
230const SYSCALL: u16 = 0x0017;
231const RET: u16 = 0x0018;
232const PUSH: u16 = 0x001F;
233const ENTER: u16 = 0x0020;
234const SELECT_ADD_CHOICE: u16 = 0x003A;
235const TEXT: u16 = 0x0088;
236const TEXT_W: u16 = 0x0095;
237const TEXT_A: u16 = 0x0096;
238const TEXT_WA: u16 = 0x0097;
239const TEXT_N: u16 = 0x0098;
240const TEXT_CAT: u16 = 0x0099;
241pub const CODE_OFFSET: u32 = 0xC;
242
243#[derive(Clone, Copy)]
244struct Operand {
245    offset: u32,
246    raw_value: u32,
247}
248
249#[derive(Debug, Clone, Copy, PartialEq, Eq, IntEnum)]
250#[repr(u32)]
251enum OperandType {
252    Literal = 0,
253    Variable = 4,
254    Argument = 8,
255    UNK = 0xFF,
256}
257
258impl Operand {
259    pub fn typ(&self) -> OperandType {
260        let typ = (self.raw_value >> 28) & 0xF;
261        OperandType::try_from(typ).unwrap_or(OperandType::UNK)
262    }
263
264    pub fn raw_type(&self) -> u32 {
265        (self.raw_value >> 28) & 0xF
266    }
267
268    pub fn value(&self) -> u32 {
269        self.raw_value & 0x0FFFFFFF
270    }
271}
272
273impl std::fmt::Display for Operand {
274    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
275        write!(f, "0x{:08X}", self.raw_value)
276    }
277}
278
279struct Instruction {
280    offset: u32,
281    opcode: u16,
282    operands: Vec<Operand>,
283}
284
285impl Instruction {
286    pub fn is_message(&self) -> bool {
287        match self.opcode {
288            TEXT | TEXT_W | TEXT_A | TEXT_WA | TEXT_N | TEXT_CAT => true,
289            SYSCALL => {
290                if self.operands.is_empty() {
291                    false
292                } else {
293                    let raw_value = self.operands[0].raw_value;
294                    match raw_value {
295                        0x20002 | 0x2000F | 0x20010 | 0x20011 | 0x20012 | 0x20013 => true,
296                        _ => false,
297                    }
298                }
299            }
300            _ => false,
301        }
302    }
303}
304
305struct UserMessageFunction {
306    num_args: u32,
307    name_arg_index: u32,
308    message_arg_index: u32,
309}
310
311pub struct Disasm<'a> {
312    reader: MemReaderRef<'a>,
313    label_offsets: Vec<u32>,
314    user_message_functions: HashMap<u32, UserMessageFunction>,
315    variables: HashMap<u32, Operand>,
316    stack: Vec<Operand>,
317    strs: Vec<PalString>,
318}
319
320#[derive(Debug)]
321pub enum StringType {
322    Name,
323    Message,
324}
325
326#[derive(Debug)]
327pub struct PalString {
328    pub offset: u32,
329    pub typ: StringType,
330}
331
332impl<'a> Disasm<'a> {
333    pub fn new(data: &'a [u8], label_offsets: &[u32]) -> Result<Self> {
334        let mut reader = MemReaderRef::new(data);
335        let mut magic = [0; 4];
336        reader.read_exact(&mut magic)?;
337        if magic != *b"Sv20" {
338            return Err(anyhow::anyhow!(
339                "Invalid magic number for Softpal script: {:?}",
340                magic
341            ));
342        }
343        Ok(Self {
344            reader,
345            label_offsets: label_offsets.to_vec(),
346            user_message_functions: HashMap::new(),
347            variables: HashMap::new(),
348            stack: Vec::new(),
349            strs: Vec::new(),
350        })
351    }
352
353    pub fn disassemble<W: Write + ?Sized>(
354        mut self,
355        mut writer: Option<&mut W>,
356    ) -> Result<Vec<PalString>> {
357        self.find_user_message_functions()?;
358        self.reader.pos = CODE_OFFSET as usize;
359        let len = self.reader.data.len();
360        while self.reader.pos < len {
361            let instr = self.read_instruction()?;
362            if let Some(writer) = writer.as_mut() {
363                self.write_instruction_to(&instr, writer)?;
364            }
365            if instr.is_message() {
366                self.handle_message_instruction()?;
367            } else if instr.opcode == MOV {
368                self.handle_mov_instruction(instr)?;
369            } else if instr.opcode == PUSH {
370                self.handle_push_instruction(instr)?;
371            } else if instr.opcode == CALL {
372                self.handle_call_instruction(instr)?;
373            } else if instr.opcode == SYSCALL {
374                self.handle_syscall_instruction(instr)?;
375            } else if instr.opcode == SELECT_ADD_CHOICE {
376                self.handle_select_choice_instruction()?;
377            } else {
378                self.stack.clear();
379                self.variables.clear();
380            }
381        }
382        Ok(self.strs)
383    }
384
385    fn read_instruction(&mut self) -> Result<Instruction> {
386        let offset = self.reader.pos as u32;
387        let opcode = self.reader.read_u32()?;
388        if (opcode >> 16) != 1 {
389            return Err(anyhow::anyhow!(
390                "Invalid opcode format: 0x{:08X} at offset 0x{:08X}",
391                opcode,
392                offset
393            ));
394        }
395        let opcode = (opcode & 0xFFFF) as u16;
396        let (_, (_, opers)) = OPS.iter().find(|(op, _)| *op == opcode).ok_or_else(|| {
397            anyhow::anyhow!(
398                "Unknown opcode: 0x{:04X} at offset 0x{:08X}",
399                opcode,
400                offset
401            )
402        })?;
403        let mut operands = Vec::new();
404        for _ in *opers {
405            let offset = self.reader.pos as u32;
406            let raw_value = self.reader.read_u32()?;
407            operands.push(Operand { offset, raw_value });
408        }
409        Ok(Instruction {
410            offset,
411            opcode,
412            operands,
413        })
414    }
415
416    fn write_instruction_to(&self, instr: &Instruction, writer: &mut dyn Write) -> Result<()> {
417        let (_, (name, opers)) =
418            OPS.iter()
419                .find(|(op, _)| *op == instr.opcode)
420                .ok_or_else(|| {
421                    anyhow::anyhow!(
422                        "Unknown opcode: 0x{:04X} at offset 0x{:08X}",
423                        instr.opcode,
424                        instr.offset
425                    )
426                })?;
427        if let Some(name) = name {
428            write!(writer, "0x{:08X} {}", instr.offset, name)?;
429        } else {
430            write!(writer, "0x{:08X} 0x{:04X}", instr.offset, instr.opcode)?;
431        }
432        for i in 0..instr.operands.len() {
433            writer.write_all(if i == 0 { b" " } else { b", " })?;
434            let value = instr.operands[i].value();
435            let mut typ = opers[i];
436            if typ == L {
437                if instr.operands[i].typ() == OperandType::Literal {
438                    write!(writer, "#0x{:08X}", self.label_offsets[value as usize - 1])?;
439                } else {
440                    typ = P;
441                }
442            }
443            if typ == P {
444                match instr.operands[i].typ() {
445                    OperandType::Literal => write!(writer, "0x{:08X}", value)?,
446                    OperandType::Variable => write!(writer, "var_{}", value)?,
447                    OperandType::Argument => write!(writer, "arg_{}", value)?,
448                    OperandType::UNK => {
449                        write!(writer, "{}:[0x{:08X}]", instr.operands[i].raw_type(), value)?
450                    }
451                }
452            } else if typ == I {
453                write!(writer, "0x{:08X}", value)?;
454            }
455        }
456        writeln!(writer)?;
457        if instr.opcode == RET {
458            writeln!(writer)?;
459        }
460        Ok(())
461    }
462
463    fn find_user_message_functions(&mut self) -> Result<()> {
464        let mut current_func_args = None;
465        self.reader.pos = CODE_OFFSET as usize;
466        let len = self.reader.data.len();
467        while self.reader.pos < len {
468            let instr = self.read_instruction()?;
469            if instr.is_message() {
470                if let Some((func_offset, func_num_args)) = current_func_args {
471                    if self.stack.len() >= 4 {
472                        let _number = self.stack.pop().unwrap();
473                        let name = self.stack.pop().unwrap();
474                        let message = self.stack.pop().unwrap();
475                        if name.typ() == OperandType::Argument
476                            && message.typ() == OperandType::Argument
477                        {
478                            self.user_message_functions.insert(
479                                func_offset,
480                                UserMessageFunction {
481                                    num_args: func_num_args,
482                                    name_arg_index: name.value() - 1,
483                                    message_arg_index: message.value() - 1,
484                                },
485                            );
486                            current_func_args = None;
487                        }
488                    }
489                }
490                self.stack.clear();
491                self.variables.clear();
492                continue;
493            }
494            match instr.opcode {
495                ENTER => {
496                    current_func_args = Some((instr.offset, instr.operands[0].value()));
497                    self.stack.clear();
498                    self.variables.clear();
499                }
500                MOV if instr.operands[0].typ() == OperandType::Variable => {
501                    self.variables
502                        .insert(instr.operands[0].value(), instr.operands[1]);
503                }
504                PUSH => {
505                    if instr.operands[0].typ() == OperandType::Variable
506                        && self.variables.contains_key(&instr.operands[0].value())
507                    {
508                        let var = self.variables.get(&instr.operands[0].value()).unwrap();
509                        self.stack.push(*var);
510                    } else {
511                        self.stack.push(instr.operands[0]);
512                    }
513                }
514                RET => {
515                    current_func_args = None;
516                    self.stack.clear();
517                    self.variables.clear();
518                }
519                _ => {
520                    self.stack.clear();
521                    self.variables.clear();
522                }
523            }
524        }
525        Ok(())
526    }
527
528    fn handle_mov_instruction(&mut self, instr: Instruction) -> Result<()> {
529        if instr.operands[0].typ() == OperandType::Variable {
530            self.variables
531                .insert(instr.operands[0].value(), instr.operands[1]);
532        }
533        Ok(())
534    }
535
536    fn handle_push_instruction(&mut self, instr: Instruction) -> Result<()> {
537        if instr.operands[0].typ() == OperandType::Variable
538            && self.variables.contains_key(&instr.operands[0].value())
539        {
540            let var = self.variables.get(&instr.operands[0].value()).unwrap();
541            self.stack.push(*var);
542        } else {
543            self.stack.push(instr.operands[0]);
544        }
545        Ok(())
546    }
547
548    fn handle_call_instruction(&mut self, instr: Instruction) -> Result<()> {
549        self.handle_call_instruction_internal(instr)?;
550        self.stack.clear();
551        self.variables.clear();
552        Ok(())
553    }
554
555    fn handle_call_instruction_internal(&mut self, instr: Instruction) -> Result<()> {
556        if self.label_offsets.is_empty() || instr.operands[0].typ() != OperandType::Literal {
557            return Ok(());
558        }
559        let target_offset = self.label_offsets[instr.operands[0].value() as usize - 1];
560        let message_func = match self.user_message_functions.get(&target_offset) {
561            Some(func) => func,
562            None => return Ok(()),
563        };
564        if self.stack.len() < message_func.num_args as usize {
565            return Ok(());
566        }
567        let mut args = Vec::new();
568        for _ in 0..message_func.num_args {
569            args.push(self.stack.pop().unwrap());
570        }
571        args.reverse();
572        let name = args[message_func.name_arg_index as usize];
573        let message = args[message_func.message_arg_index as usize];
574        if name.typ() == OperandType::Literal && message.typ() == OperandType::Literal {
575            self.strs.push(PalString {
576                offset: name.offset,
577                typ: StringType::Name,
578            });
579            self.strs.push(PalString {
580                offset: message.offset,
581                typ: StringType::Message,
582            });
583        }
584        Ok(())
585    }
586
587    fn handle_syscall_instruction(&mut self, instr: Instruction) -> Result<()> {
588        match instr.operands[0].raw_value {
589            0x60002 => {
590                self.handle_select_choice_instruction()?;
591            }
592            _ => {
593                self.stack.clear();
594            }
595        }
596        Ok(())
597    }
598
599    fn handle_message_instruction(&mut self) -> Result<()> {
600        self.handle_message_instruction_internal()?;
601        self.stack.clear();
602        self.variables.clear();
603        Ok(())
604    }
605
606    fn handle_message_instruction_internal(&mut self) -> Result<()> {
607        if self.stack.len() < 4 {
608            return Ok(());
609        }
610        let _number = self.stack.pop().unwrap();
611        let name = self.stack.pop().unwrap();
612        let message = self.stack.pop().unwrap();
613        if name.typ() != OperandType::Literal || message.typ() != OperandType::Literal {
614            return Ok(());
615        }
616        self.strs.push(PalString {
617            offset: name.offset,
618            typ: StringType::Name,
619        });
620        self.strs.push(PalString {
621            offset: message.offset,
622            typ: StringType::Message,
623        });
624        Ok(())
625    }
626
627    fn handle_select_choice_instruction(&mut self) -> Result<()> {
628        self.handle_select_choice_instruction_internal()?;
629        self.stack.clear();
630        self.variables.clear();
631        Ok(())
632    }
633
634    fn handle_select_choice_instruction_internal(&mut self) -> Result<()> {
635        if self.stack.len() < 1 {
636            return Ok(());
637        }
638        let choice = self.stack.pop().unwrap();
639        self.strs.push(PalString {
640            offset: choice.offset,
641            typ: StringType::Message,
642        });
643        Ok(())
644    }
645}