Skip to main content

luaur_code_gen/functions/
to_string_ir_dump_alt_d.rs

1use crate::enums::ir_condition::IrCondition;
2use crate::enums::ir_op_kind::IrOpKind;
3use crate::functions::append::append;
4use crate::functions::append_vm_constant::append_vm_constant;
5use crate::functions::get_block_kind_name::get_block_kind_name;
6use crate::functions::to_string_ir_dump_alt_e::to_string as to_string_const;
7use crate::functions::vm_const_op::vm_const_op;
8use crate::functions::vm_exit_op::vm_exit_op;
9use crate::functions::vm_reg_op::vm_reg_op;
10use crate::functions::vm_upvalue_op::vm_upvalue_op;
11use crate::macros::codegen_assert::CODEGEN_ASSERT;
12use crate::records::ir_op::IrOp;
13use crate::records::ir_to_string_context::IrToStringContext;
14use luaur_vm::type_aliases::proto::Proto;
15
16static TEXT_FOR_CONDITION: [&str; IrCondition::Count as usize] = [
17    "eq", "not_eq", "lt", "not_lt", "le", "not_le", "gt", "not_gt", "ge", "not_ge", "u_lt", "u_le",
18    "u_gt", "u_ge",
19];
20
21const K_VM_EXIT_ENTRY_GUARD_PC: u32 = (1u32 << 28) - 1;
22
23pub fn to_string(ctx: &mut IrToStringContext, op: IrOp) {
24    match op.kind() {
25        IrOpKind::None => {}
26        IrOpKind::Undef => append(&mut ctx.result, format_args!("undef")),
27        IrOpKind::Constant => {
28            let proto = ctx.proto as *mut Proto;
29            let constant = ctx.constants[op.index() as usize];
30            to_string_const(ctx.result, proto, constant);
31        }
32        IrOpKind::Condition => {
33            CODEGEN_ASSERT!(op.index() < IrCondition::Count as u32);
34            ctx.result.push_str(TEXT_FOR_CONDITION[op.index() as usize]);
35        }
36        IrOpKind::Inst => append(&mut ctx.result, format_args!("%{}", op.index())),
37        IrOpKind::Block => {
38            let name = get_block_kind_name(ctx.blocks[op.index() as usize].kind);
39            append(&mut ctx.result, format_args!("{}_{}", name, op.index()));
40        }
41        IrOpKind::VmReg => append(&mut ctx.result, format_args!("R{}", vm_reg_op(op))),
42        IrOpKind::VmConst => {
43            append(&mut ctx.result, format_args!("K{}", vm_const_op(op)));
44
45            if !ctx.proto.is_null() {
46                let proto = ctx.proto as *mut Proto;
47                append(&mut ctx.result, format_args!(" ("));
48                append_vm_constant(ctx.result, proto, vm_const_op(op));
49                append(&mut ctx.result, format_args!(")"));
50            }
51        }
52        IrOpKind::VmUpvalue => append(&mut ctx.result, format_args!("U{}", vm_upvalue_op(op))),
53        IrOpKind::VmExit => {
54            if vm_exit_op(op) == K_VM_EXIT_ENTRY_GUARD_PC {
55                append(&mut ctx.result, format_args!("exit(entry)"));
56            } else {
57                append(&mut ctx.result, format_args!("exit({})", vm_exit_op(op)));
58            }
59        }
60    }
61}