lua2hcb_compiler/
ir.rs

1use std::fmt;
2
3#[derive(Clone, Debug)]
4pub struct Label {
5    pub name: String,
6}
7
8impl Label {
9    pub fn new(name: impl Into<String>) -> Self {
10        Self { name: name.into() }
11    }
12}
13
14#[derive(Clone, Debug)]
15pub enum Item {
16    Label(Label),
17    Op(OpKind),
18}
19
20#[derive(Clone, Debug)]
21pub enum OpKind {
22    // Simple one-byte ops
23    Nop,
24    Ret,
25    Retv,
26    PushNil,
27    PushTrue,
28    PushTop,
29    PushReturn,
30    Neg,
31    Add,
32    Sub,
33    Mul,
34    Div,
35    Mod,
36    BitTest,
37    And,
38    Or,
39    SetE,
40    SetNe,
41    SetG,
42    SetLe,
43    SetL,
44    SetGe,
45
46    // Ops with immediates
47    InitStack { args: i8, locals: i8 },
48    CallFn { name: String },
49    Syscall { id: u16 },
50    JmpAbs { target: u32 },
51    JzAbs { target: u32 },
52    JmpLabel { label: String },
53    JzLabel { label: String },
54
55    PushI8(i8),
56    PushI16(i16),
57    PushI32(i32),
58    PushF32(f32),
59    PushString(String),
60
61    PushGlobal(u16),
62    PushStack(i8),
63    PushGlobalTable(u16),
64    PushLocalTable(i8),
65
66    PopGlobal(u16),
67    PopStack(i8),
68    PopGlobalTable(u16),
69    PopLocalTable(i8),
70}
71
72impl fmt::Display for OpKind {
73    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
74        write!(f, "{:?}", self)
75    }
76}