Skip to main content

luaur_bytecode/records/
bytecode_builder.rs

1use crate::enums::dump_flags::DumpFlags;
2use crate::enums::r#type::Type;
3use crate::records::bytecode_encoder::BytecodeEncoder;
4use crate::records::class_shape::ClassShape;
5use crate::records::constant::Constant;
6use crate::records::constant_key::ConstantKey;
7use crate::records::constant_key_hash::ConstantKeyHash;
8use crate::records::debug_local_bytecode_builder::DebugLocal;
9use crate::records::debug_upval::DebugUpval;
10use crate::records::function::Function;
11use crate::records::jump::Jump;
12use crate::records::string_ref::StringRef;
13use crate::records::string_ref_hash::StringRefHash;
14use crate::records::table_shape::TableShape;
15use crate::records::table_shape_hash::TableShapeHash;
16use crate::records::typed_local_bytecode_builder::TypedLocal;
17use crate::records::typed_upval::TypedUpval;
18use crate::records::userdata_type::UserdataType;
19use luaur_common::records::dense_hash_map::DenseHashMap;
20use luaur_common::records::dense_hash_table::DenseEqDefault;
21use luaur_common::type_aliases::dense_hash_default::DenseHashDefault;
22
23/// An owned bytecode encoder. A newtype so `BytecodeBuilder` can stay `Debug`
24/// without requiring `BytecodeEncoder: Debug`.
25pub struct EncoderBox(pub alloc::boxed::Box<dyn BytecodeEncoder>);
26
27impl core::fmt::Debug for EncoderBox {
28    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
29        f.write_str("EncoderBox(..)")
30    }
31}
32
33#[derive(Debug)]
34pub struct BytecodeBuilder {
35    pub(crate) functions: Vec<Function>,
36    pub(crate) current_function: u32,
37    pub(crate) main_function: u32,
38
39    pub(crate) total_instruction_count: usize,
40    pub(crate) insns: Vec<u32>,
41    pub(crate) lines: Vec<i32>,
42    pub(crate) constants: Vec<Constant>,
43    pub(crate) protos: Vec<u32>,
44    pub(crate) jumps: Vec<Jump>,
45
46    pub(crate) table_shapes: Vec<TableShape>,
47    pub(crate) class_shapes: Vec<ClassShape>,
48
49    pub(crate) fb_slots: Vec<u32>,
50
51    pub(crate) has_long_jumps: bool,
52
53    pub(crate) constant_map:
54        DenseHashMap<ConstantKey, i32, DenseHashDefault<ConstantKey>, DenseEqDefault<ConstantKey>>,
55    pub(crate) table_shape_map:
56        DenseHashMap<TableShape, i32, DenseHashDefault<TableShape>, DenseEqDefault<TableShape>>,
57    pub(crate) proto_map: DenseHashMap<u32, i16>,
58
59    pub(crate) debug_line: i32,
60
61    pub(crate) debug_locals: Vec<DebugLocal>,
62    pub(crate) debug_upvals: Vec<DebugUpval>,
63
64    pub(crate) typed_locals: Vec<TypedLocal>,
65    pub(crate) typed_upvals: Vec<TypedUpval>,
66
67    pub(crate) userdata_types: Vec<UserdataType>,
68
69    pub(crate) string_table:
70        DenseHashMap<StringRef, u32, DenseHashDefault<StringRef>, DenseEqDefault<StringRef>>,
71    pub(crate) debug_strings: Vec<StringRef>,
72
73    pub(crate) debug_remarks: Vec<(u32, u32)>,
74    pub(crate) debug_remark_buffer: String,
75
76    /// C++ holds a borrowed `BytecodeEncoder*`; the port owns the encoder so the
77    /// builder can never call through a dangling or null pointer (issue #43).
78    pub(crate) encoder: Option<EncoderBox>,
79    pub(crate) bytecode: String,
80
81    pub(crate) dump_flags: u32,
82    pub(crate) dump_source: Vec<String>,
83    pub(crate) dump_remarks: Vec<(i32, String)>,
84
85    pub(crate) temp_type_info: String,
86
87    pub(crate) dump_function_ptr: Option<fn(&BytecodeBuilder, &mut Vec<i32>) -> String>,
88}
89
90impl BytecodeBuilder {
91    pub fn get_string_hash(key: StringRef) -> u32 {
92        crate::methods::bytecode_builder_get_string_hash::bytecode_builder_get_string_hash(key)
93    }
94
95    pub const DUMP_CODE: u32 = DumpFlags::Dump_Code as u32;
96    pub const DUMP_LINES: u32 = DumpFlags::Dump_Lines as u32;
97    pub const DUMP_SOURCE: u32 = DumpFlags::Dump_Source as u32;
98    pub const DUMP_LOCALS: u32 = DumpFlags::Dump_Locals as u32;
99    pub const DUMP_REMARKS: u32 = DumpFlags::Dump_Remarks as u32;
100    pub const DUMP_TYPES: u32 = DumpFlags::Dump_Types as u32;
101    pub const DUMP_CONSTANTS: u32 = DumpFlags::Dump_Constants as u32;
102}
103
104struct DummyEncoder;
105
106impl BytecodeEncoder for DummyEncoder {
107    fn encode(&mut self, _data: &mut [u32]) {}
108}
109
110impl Default for ConstantKey {
111    fn default() -> Self {
112        Self {
113            r#type: Type::Type_Nil,
114            value: 0,
115            extra: 0,
116        }
117    }
118}
119
120impl Default for BytecodeBuilder {
121    fn default() -> Self {
122        Self {
123            functions: Vec::new(),
124            current_function: !0u32,
125            main_function: !0u32,
126            total_instruction_count: 0,
127            insns: Vec::new(),
128            lines: Vec::new(),
129            constants: Vec::new(),
130            protos: Vec::new(),
131            jumps: Vec::new(),
132            table_shapes: Vec::new(),
133            class_shapes: Vec::new(),
134            fb_slots: Vec::new(),
135            has_long_jumps: false,
136            constant_map: DenseHashMap::new(ConstantKey {
137                r#type: Type::Type_Nil,
138                value: 0,
139                extra: 0,
140            }),
141            table_shape_map: DenseHashMap::new(TableShape {
142                keys: [0; 32],
143                constants: [-1; 32],
144                length: 0,
145                hasConstants: false,
146            }),
147            proto_map: DenseHashMap::new(!0u32),
148            debug_line: 0,
149            debug_locals: Vec::new(),
150            debug_upvals: Vec::new(),
151            typed_locals: Vec::new(),
152            typed_upvals: Vec::new(),
153            userdata_types: Vec::new(),
154            string_table: DenseHashMap::new(StringRef::default()),
155            debug_strings: Vec::new(),
156            debug_remarks: Vec::new(),
157            debug_remark_buffer: String::new(),
158            encoder: None,
159            bytecode: String::new(),
160            dump_flags: 0,
161            dump_source: Vec::new(),
162            dump_remarks: Vec::new(),
163            temp_type_info: String::new(),
164            dump_function_ptr: None,
165        }
166    }
167}