// File: /workspace/github_c_makefile_star2_O2/Liutos[P]Eloquent/vm.c
// Function: vm_execute
void vm_execute(vm_t *vm, ins_t *ins)
{
    value_t *_fun_, *_val_, *_val2_;
    value_t **_addr_; // Used only by BC_ADDR
    vm->ip = 0;
    while (vm_hasnext(vm, ins)) {
        bytecode_t *bc = vm_nextins(vm, ins);
        switch (BC_OPCODE(bc)) {
            case BC_ADDR:
                _addr_ = env_getaddr(vm->env, BC_ADDR_NAME(bc));
                if (_addr_ == NULL)
                    _addr_ = env_getaddr(vm->global_env, BC_ADDR_NAME(bc));
                if (_addr_ == NULL) {
                    stack_push(vm->stack, value_error_newf("Undefined variable: %s", BC_ADDR_NAME(bc)));
                    goto __check_exception;
                }
                stack_push(vm->stack, value_ref_new(_addr_));
                break;
            case BC_ARGS:
                for (int i = BC_ARGS_ARITY(bc) - 1; i >= 0; i--) {
                    value_t *obj = (value_t *)stack_nth(vm->stack, i);
                    env_set(vm->env, NULL, obj);
                }
                stack_shrink(vm->stack, BC_ARGS_ARITY(bc));
                break;
            case BC_ASET:
                _val_ = (value_t *)stack_pop(vm->stack);
                _val2_ = (value_t *)stack_top(vm->stack);
                if (elo_type(_val_) != VALUE_REF) {
                    stack_push(vm->stack, value_error_new("Operand of `valof' must be a reference"));
                    goto __check_exception;
                }
                *VALUE_REF_ADDR(_val_) = _val2_;
                break;
            case BC_CALL:
                _fun_ = (value_t *)stack_pop(vm->stack);
                if (BC_CALL_NARGS(bc) != VALUE_FUNC_ARITY(_fun_)) {
                    stack_push(vm->stack, value_error_newf("Incorrect number of arguments. Expecting %d but get %d ones", VALUE_FUNC_ARITY(_fun_), BC_CALL_NARGS(bc)));
                    break;
                }

                if (VALUE_FUNC_ISBIF(_fun_))
                    vm_execute_bif(vm, _fun_);
                else {
                    vm_trace_callucf(vm, _fun_);
                    vm_save(vm, ins, _fun_);
                    vm->depth++;
                    /* 初始化环境、字节码序列和指令指针 */
                    vm->env = env_new(VALUE_FUNC_ENV(_fun_));
                    vm->denv = env_new(vm->denv);
                    vm->fun = _fun_;
                    ins = VALUE_UCF_CODE(_fun_);
                    vm_goto(vm, 0);
                    /* 开始执行新的字节码指令 */
                }
                break;
__check_exception:
            case BC_CHKEX:
                _val_ = (value_t *)stack_top(vm->stack);
                if (elo_ERRORP(_val_)) {
                    if (!stack_isempty(vm->sys_stack)) {
                        vm_restore(vm, &ins, &_fun_);
                        /* 将运行时错误压回参数栈 */
                        stack_push(vm->stack, _val_);
                    } else
                        return;
                }
                break;
            case BC_DGET:
                _val_ = env_get(vm->denv, BC_DGET_NAME(bc));
                if (_val_ == NULL) {
                    stack_push(vm->stack, value_error_newf("Undefined dynamic variable: %s", BC_DGET_NAME(bc)));
                    goto __check_exception;
                }

                stack_push(vm->stack, _val_);
                break;
            case BC_DSET:
                env_set(vm->denv, BC_DSET_NAME(bc), (value_t *)stack_top(vm->stack));
                break;
            case BC_FJUMP:
                _val_ = (value_t *)stack_pop(vm->stack);
                if (_val_->kind == VALUE_INT && VALUE_INT_VALUE(_val_) == 0)
                    vm_goto(vm, BC_FJUMP_INDEX(bc));
                break;
            case BC_GREF:
                _val_ = env_ref(vm->global_env, BC_REF_I(bc), BC_REF_J(bc));
                if (_val_ == NULL) {
                    stack_push(vm->stack, value_error_newf("Undefined variable: %s", BC_REF_NAME(bc)));
                    goto __check_exception;
                }

                stack_push(vm->stack, _val_);
                break;
            case BC_GSET:
                _val_ = (value_t *)stack_top(vm->stack);
                vm_env_set(vm->global_env, BC_SET_I(bc), BC_SET_J(bc), BC_SET_NAME(bc), _val_);
                break;
            case BC_FUNC:
                _val_ = (value_t *)stack_top(vm->stack);
                VALUE_FUNC_ENV(_val_) = vm->env;
                break;
            case BC_JUMP:
                vm_goto(vm, BC_JUMP_INDEX(bc));
                break;
            case BC_NOPE:
                break;
            case BC_POP:
                stack_pop(vm->stack);
                break;
            case BC_PRINT:
                _val_ = (value_t *)stack_top(vm->stack);
                vm_value_print(vm, _val_);
                break;
            case BC_PUSH:
                stack_push(vm->stack, BC_PUSH_PTR(bc));
                break;
            case BC_RETURN:
                vm->depth--;
                vm_restore(vm, &ins, &_fun_);
                vm_trace_retucf(vm, _fun_, (value_t *)stack_top(vm->stack));
                /* 开始执行旧的字节码指令 */
                break;
            case BC_REF:
                _val_ = env_ref(vm->env, BC_REF_I(bc), BC_REF_J(bc));
                if (_val_ == NULL) {
                    stack_push(vm->stack, value_error_newf("Undefined variable: %s", BC_REF_NAME(bc)));
                    goto __check_exception;
                }

                stack_push(vm->stack, _val_);
                break;
            case BC_SET:
                _val_ = (value_t *)stack_top(vm->stack);
                vm_env_set(vm->env, BC_SET_I(bc), BC_SET_J(bc), BC_SET_NAME(bc), _val_);
                break;
            case BC_TRACE: {
                _val_ = (value_t *)stack_top(vm->stack);
                ident_t *id = BC_TRACE_VAR(bc);
                if (id == NULL) {
                    char name[32] = {0};
                    snprintf(name, sizeof(name), "#<%p>", _val_);
                    id = ident_intern(name);
                }
                hash_table_set(vm->traced_objs, _val_, id);
            }
                break;
            case BC_VALOF:
                _val_ = (value_t *)stack_pop(vm->stack);
                if (elo_type(_val_) != VALUE_REF) {
                    stack_push(vm->stack, value_error_new("Argument of `valof' must be a reference"));
                    goto __check_exception;
                }
                stack_push(vm->stack, *VALUE_REF_ADDR(_val_));
                break;
            default :
                fprintf(stderr, "Not support: %s\n", bc_name(bc));
                exit(0);
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movq %rsp, %rax
movq %rsi, (%rax)
andl $0x0, 0x4(%rdi)
leaq 0x3809(%rip), %r13      # 0x6118
movq 0x76ba(%rip), %r15      # 0x9fd0
xorl %eax, %eax
movslq %eax, %rsi
cmpq %rsi, 0x10(%r14)
jbe 0x2ec2
movq %r14, %rdi
callq 0x5e9f
incl 0x4(%rbx)
movq %rax, %r12
movl (%rax), %eax
cmpq $0x15, %rax
ja 0x2ed1
movslq (%r13,%rax,4), %rax
addq %r13, %rax
jmpq *%rax
movq 0x10(%rbx), %rdi
movq 0x8(%r12), %rax
movq (%rax), %rsi
callq 0x54e7
testq %rax, %rax
jne 0x2979
movq 0x18(%rbx), %rdi
movq 0x8(%r12), %rax
movq (%rax), %rsi
callq 0x54e7
testq %rax, %rax
je 0x2db1
movq 0x30(%rbx), %r14
movq %rax, %rdi
callq 0x2572
jmp 0x2cc2
movq 0x18(%rbx), %rdi
jmp 0x2b13
movq 0x30(%rbx), %rdi
callq 0x5e8a
cmpl $0x6, (%rax)
movq 0x30(%rbx), %r14
jne 0x2ca7
movq 0x8(%rax), %rax
movq (%rax), %rsi
movq %r14, %rdi
jmp 0x2d1f
movq 0x30(%rbx), %rdi
callq 0x5e8a
cmpl $0x5, (%rax)
jne 0x2d24
cmpl $0x0, 0x8(%rax)
jne 0x2d24
movl 0x10(%r12), %eax
movl %eax, 0x4(%rbx)
jmp 0x2d24
movq 0x30(%rbx), %rdi
callq 0x5ea7
movq %rax, %r14
movq 0x8(%r12), %rdx
testq %rdx, %rdx
jne 0x2a2b
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
leaq 0x10(%rsp), %r12
movq %r12, %rdi
pushq $0x20
popq %rsi
leaq 0x36f8(%rip), %rdx      # 0x610e
movq %r14, %rcx
xorl %eax, %eax
callq 0x2050
movq %r12, %rdi
callq 0x27bc
movq %rax, %rdx
movq 0x28(%rbx), %rdi
movq %r14, %rsi
callq 0x5953
jmp 0x2d24
movq 0x30(%rbx), %rdi
callq 0x5ea7
movq 0x18(%rbx), %rdi
jmp 0x2c73
movq 0x30(%rbx), %rdi
callq 0x5ea7
movq 0x10(%rbx), %rcx
movq %rcx, 0x18(%rax)
jmp 0x2d24
movq 0x30(%rbx), %rdi
callq 0x5e8a
jmp 0x2d24
decl (%rbx)
movq %rbx, %rdi
movq %rsp, %rsi
leaq 0x8(%rsp), %rdx
callq 0x2f18
movq 0x8(%rsp), %r14
movq 0x30(%rbx), %rdi
callq 0x5ea7
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x3116
jmp 0x2d24
movq 0x30(%rbx), %rdi
callq 0x5ea7
cmpl $0x4, (%rax)
jne 0x2c8f
cmpl $0x0, 0xc(%rax)
je 0x2c8f
movq 0x20(%rax), %r14
xorl %r12d, %r12d
cmpq %r12, 0x10(%r14)
jbe 0x2d24
movq %r14, %rdi
movq %r12, %rsi
callq 0x5e9f
movq %rax, %rbp
movq (%r15), %rdi
leaq 0x37d7(%rip), %rsi      # 0x62c1
movl %r12d, %edx
xorl %eax, %eax
callq 0x20d0
movq (%r15), %rsi
movq %rbp, %rdi
callq 0x4583
movq (%r15), %rsi
pushq $0xa
popq %rdi
callq 0x20a0
incq %r12
jmp 0x2ac8
movq 0x10(%rbx), %rdi
movl 0x8(%r12), %esi
movl 0xc(%r12), %edx
callq 0x54b0
movq 0x30(%rbx), %r14
testq %rax, %rax
jne 0x2cc2
movq 0x10(%r12), %rsi
leaq 0x3635(%rip), %rdi      # 0x6170
jmp 0x2c33
movq 0x30(%rbx), %rdi
movq 0x8(%r12), %rsi
jmp 0x2d1f
movq 0x30(%rbx), %rdi
callq 0x5e8a
movq %rax, 0x8(%rsp)
movl 0x8(%r12), %edx
movl 0x10(%rax), %esi
cmpl %esi, %edx
jne 0x2cb0
movq %rax, %rbp
cmpl $0x0, 0x8(%rax)
je 0x2d30
cmpl $0x2, %edx
je 0x2e71
cmpl $0x1, %edx
je 0x2e47
testl %edx, %edx
jne 0x2efe
movq %rbx, %rdi
movq %rbp, %rsi
xorl %edx, %edx
xorl %eax, %eax
callq 0x2f9b
movq 0x8(%rbx), %rdi
callq *0x20(%rbp)
jmp 0x2eac
movl 0x8(%r12), %ebp
testl %ebp, %ebp
jle 0x2bd4
decl %ebp
movq 0x30(%rbx), %rdi
movl %ebp, %esi
callq 0x5e75
movq 0x10(%rbx), %rdi
xorl %esi, %esi
movq %rax, %rdx
callq 0x5321
jmp 0x2bb3
movq 0x30(%rbx), %rdi
movslq 0x8(%r12), %rsi
callq 0x5dde
jmp 0x2d24
movq 0x8(%r12), %r14
movq 0x8(%rbx), %r12
movq 0x30(%rbx), %rdi
callq 0x5ea7
movq %r12, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x5321
jmp 0x2d24
movq 0x8(%rbx), %rdi
movq 0x8(%r12), %rsi
callq 0x5453
movq 0x30(%rbx), %r14
testq %rax, %rax
jne 0x2cc2
movq 0x8(%r12), %rsi
leaq 0x35b7(%rip), %rdi      # 0x61ea
xorl %eax, %eax
callq 0x2486
jmp 0x2cda
movq 0x30(%rbx), %rdi
callq 0x5e8a
movq %rax, %r14
movq 0x30(%rbx), %rdi
callq 0x5ea7
cmpl $0x6, (%r14)
jne 0x2cca
movq 0x8(%r14), %rcx
movq %rax, (%rcx)
jmp 0x2d24
movq 0x30(%rbx), %rdi
callq 0x5ea7
movq 0x10(%rbx), %rdi
movl 0x8(%r12), %esi
movl 0xc(%r12), %edx
movq 0x10(%r12), %rcx
movq %rax, %r8
callq 0x2f6a
jmp 0x2d24
movq (%r15), %rsi
movq %rax, %rdi
callq 0x2612
movq (%r15), %rsi
pushq $0xa
popq %rdi
callq 0x20a0
jmp 0x2d24
leaq 0x355b(%rip), %rdi      # 0x6209
jmp 0x2cd5
movq 0x30(%rbx), %r14
leaq 0x34f3(%rip), %rdi      # 0x61ae
xorl %eax, %eax
callq 0x2486
movq %r14, %rdi
movq %rax, %rsi
jmp 0x2d1f
movq 0x30(%rbx), %r14
leaq 0x34b2(%rip), %rdi      # 0x6187
callq 0x2455
movq %r14, %rdi
movq %rax, %rsi
callq 0x5de3
movq 0x30(%rbx), %rdi
callq 0x5ea7
cmpl $0x2, (%rax)
jne 0x2d24
movq %rax, %r14
movq 0x38(%rbx), %rdi
callq 0x5eb4
testq %rax, %rax
je 0x2ec2
movq %rbx, %rdi
movq %rsp, %rsi
leaq 0x8(%rsp), %rdx
callq 0x2f18
movq 0x30(%rbx), %rdi
movq %r14, %rsi
callq 0x5de3
movq (%rsp), %r14
movl 0x4(%rbx), %eax
jmp 0x2918
andq $0x0, 0x10(%rsp)
movq %rbx, %rdi
movq %rbp, %rsi
leaq 0x10(%rsp), %rdx
callq 0x3182
testl %eax, %eax
je 0x2dd7
movq %rbx, %rdi
callq 0x31c2
movq (%r15), %rdi
movl (%rbx), %edx
movq 0x10(%rsp), %rax
movq (%rax), %rcx
leaq 0x34f6(%rip), %rsi      # 0x6260
xorl %eax, %eax
callq 0x20d0
movl 0x10(%rbp), %r12d
decl %r12d
testl %r12d, %r12d
js 0x2dc2
movq 0x30(%rbx), %rdi
movl %r12d, %esi
callq 0x5e75
movq (%r15), %rsi
movq %rax, %rdi
callq 0x2612
subl $0x1, %r12d
jb 0x2d78
movq (%r15), %rcx
leaq 0x34c5(%rip), %rdi      # 0x6269
pushq $0x2
popq %rsi
pushq $0x1
popq %rdx
callq 0x2140
jmp 0x2d78
movq 0x30(%rbx), %r14
movq 0x8(%r12), %rax
movq (%rax), %rsi
jmp 0x2b34
movq (%r15), %rcx
leaq 0x34a0(%rip), %rdi      # 0x626c
pushq $0x2
popq %rsi
pushq $0x1
popq %rdx
callq 0x2140
movq 0x8(%rbx), %rsi
movq 0x38(%rbx), %rdi
callq 0x5de3
movq 0x10(%rbx), %rsi
movq 0x38(%rbx), %rdi
callq 0x5de3
movq 0x38(%rbx), %rdi
movq %r14, %rsi
callq 0x5de3
movq 0x38(%rbx), %rdi
movslq 0x4(%rbx), %rsi
callq 0x5de3
movq 0x38(%rbx), %rdi
movq %rbp, %rsi
callq 0x5de3
incl (%rbx)
movq 0x18(%rbp), %rdi
callq 0x5297
movq %rax, 0x10(%rbx)
movq 0x8(%rbx), %rdi
callq 0x5297
movq %rax, 0x8(%rbx)
movq %rbp, 0x40(%rbx)
movq 0x20(%rbp), %rax
movq %rax, (%rsp)
andl $0x0, 0x4(%rbx)
jmp 0x2d24
movq 0x30(%rbx), %rdi
callq 0x5e8a
movq %rax, %r14
movq %rbx, %rdi
movq %rbp, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x2f9b
movq 0x8(%rbx), %rdi
movq %r14, %rsi
callq *0x20(%rbp)
jmp 0x2eac
movq 0x30(%rbx), %rdi
callq 0x5e8a
movq %rax, %r14
movq 0x30(%rbx), %rdi
callq 0x5e8a
movq %rax, %r12
movq %rbx, %rdi
movq %rbp, %rsi
movq %rax, %rdx
movq %r14, %rcx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x2f9b
movq 0x8(%rbx), %rdi
movq %r12, %rsi
movq %r14, %rdx
callq *0x20(%rbp)
movq %rax, %r14
movq %rbx, %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x3116
jmp 0x2d18
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x7120(%rip), %rax      # 0x9ff8
movq (%rax), %rbx
movq %r12, %rdi
callq 0x464d
leaq 0x3347(%rip), %rsi      # 0x6231
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x20d0
xorl %edi, %edi
callq 0x2130
movq 0x70f3(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
leaq 0x3333(%rip), %rsi      # 0x6242
xorl %eax, %eax
callq 0x20d0
jmp 0x2ef7