// File: /workspace/github_c_makefile_star2_O1/tyabu12[P]pl0compiler/codegen.c
// Function: execute
void execute(const int *secretValue) {    /* 目的コード（命令語）の実行           */
#if 0
  static Mem m;
  initMemory(&m, secretValue);
  while (!m.exitCode) stepForward(&m);
#else
    int stack[MAXMEM];          /* 実行時スタック                       */
    int display[MAXLEVEL];      /* 現在見える各ブロックの先頭番地のディスプレイ */
    int pc, top, lev, temp;
    Inst i;                     /* 実行する命令語                       */
    /* printf("start execution\n"); */
    top = 0;  pc = 0;           /* top:次にスタックに入れる場所、pc:命令語のカウンタ */
    stack[0] = 0;  stack[1] = 0; /* stack[top]はcalleeで壊すディスプレイの退避場所 */
                                /* stack[top+1]はcallerへの戻り番地     */
    if (secretValue) stack[2] = *secretValue; /* シークレットモード (第3セルに値を格納) */
    display[0] = 0;             /* 主ブロックの先頭番地は 0             */
    do {
        i = code[pc++];         /* これから実行する命令語               */
        switch (i.opCode) {
        case lit: stack[top++] = i.u.value;
            break;
        case lod: stack[top++] = stack[display[i.u.addr.level] + i.u.addr.addr];
            break;
        case sto: stack[display[i.u.addr.level] + i.u.addr.addr] = stack[--top];
            break;
        case cal: lev = i.u.addr.level +1; /* i.u.addr.levelはcalleeの名前のレベル */
                                /* calleeのブロックのレベルlevはそれに＋１したもの */
            stack[top] = display[lev]; /* display[lev]の退避            */
            stack[top+1] = pc; display[lev] = top;
                                /* 現在のtopがcalleeのブロックの先頭番地 */
            pc = i.u.addr.addr;
            break;
        case ret: temp = stack[--top]; /* スタックのトップにあるものが返す値 */
            top = display[i.u.addr.level]; /* topを呼ばれたときの値に戻す */
            display[i.u.addr.level] = stack[top]; /* 壊したディスプレイの回復 */
            pc = stack[top+1];
            top -= i.u.addr.addr; /* 実引数の分だけトップを戻す         */
            stack[top++] = temp; /* 返す値をスタックのトップへ          */
            break;
        case ict: top += i.u.value;
            if (top >= MAXMEM-MAXREG)
                errorF("stack overflow");
            break;
        case jmp: pc = i.u.value; break;
        case jpc: if (stack[--top] == 0) pc = i.u.value;
            break;
        case opr:
            switch (i.u.optr) {
            case neg: stack[top-1] = -stack[top-1]; continue;
            case add: --top;  stack[top-1] += stack[top]; continue;
            case sub: --top; stack[top-1] -= stack[top]; continue;
            case mul: --top;  stack[top-1] *= stack[top];  continue;
            case div: --top;  stack[top-1] /= stack[top]; continue;
            case odd: stack[top-1] = stack[top-1] & 1; continue;
            case eq: --top;  stack[top-1] = (stack[top-1] == stack[top]); continue;
            case ls: --top;  stack[top-1] = (stack[top-1] < stack[top]); continue;
            case gr: --top;  stack[top-1] = (stack[top-1] > stack[top]); continue;
            case neq: --top;  stack[top-1] = (stack[top-1] != stack[top]); continue;
            case lseq: --top;  stack[top-1] = (stack[top-1] <= stack[top]); continue;
            case greq: --top;  stack[top-1] = (stack[top-1] >= stack[top]); continue;
            case rd: scanf("%d", &stack[top++]); continue;
            case wrt: printf("%d", stack[--top]); continue;
            case wrl: printf("\n"); continue;
            }
        }
    } while (pc != 0);
#endif
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1f68, %rsp           # imm = 0x1F68
movq $0x0, 0x20(%rsp)
testq %rdi, %rdi
je 0x2f50
movl (%rdi), %eax
movl %eax, 0x28(%rsp)
movl $0x0, (%rsp)
leaq 0x5192(%rip), %r15      # 0x80f0
leaq 0x213b(%rip), %r12      # 0x50a0
leaq 0x2158(%rip), %r13      # 0x50c4
leaq 0x228d(%rip), %r14      # 0x5200
xorl %ebp, %ebp
xorl %ebx, %ebx
movslq %ebx, %rax
incl %ebx
leaq (%rax,%rax,2), %rdx
movl (%r15,%rdx,4), %edi
cmpq $0x8, %rdi
ja 0x3195
movl 0x4(%r15,%rdx,4), %ecx
movslq %ecx, %rax
movl 0x8(%r15,%rdx,4), %esi
movslq %esi, %rdx
movslq (%r12,%rdi,4), %rdi
addq %r12, %rdi
jmpq *%rdi
movslq %ebp, %rax
incl %ebp
jmp 0x2ff1
movl 0x4(%rsp,%rax,4), %ecx
movslq %ebp, %rdx
movl %ecx, 0x20(%rsp,%rdx,4)
movl %ebx, 0x24(%rsp,%rdx,4)
movl %ebp, 0x4(%rsp,%rax,4)
movl %esi, %ebx
jmp 0x3195
movslq (%rsp,%rax,4), %rax
addq %rdx, %rax
movl 0x20(%rsp,%rax,4), %eax
movslq %ebp, %rcx
incl %ebp
movl %eax, 0x20(%rsp,%rcx,4)
jmp 0x3195
movslq %ebp, %rcx
decl %ebp
movl 0x1c(%rsp,%rcx,4), %ecx
movslq (%rsp,%rax,4), %rax
addq %rdx, %rax
movl %ecx, 0x20(%rsp,%rax,4)
jmp 0x3195
movl %ecx, %ebx
jmp 0x3195
cmpl $0xe, %ecx
ja 0x3195
movl %ecx, %eax
movslq (%r13,%rax,4), %rax
addq %r13, %rax
jmpq *%rax
movslq %ebp, %rax
negl 0x1c(%rsp,%rax,4)
jmp 0x3195
movslq %ebp, %rcx
movl 0x1c(%rsp,%rcx,4), %ecx
movslq (%rsp,%rax,4), %rsi
movl 0x20(%rsp,%rsi,4), %edi
movl 0x24(%rsp,%rsi,4), %ebx
movl %edi, (%rsp,%rax,4)
subq %rdx, %rsi
leal 0x1(%rsi), %ebp
movl %ecx, 0x20(%rsp,%rsi,4)
jmp 0x3195
addl %ecx, %ebp
cmpl $0x7bc, %ebp            # imm = 0x7BC
jl 0x3195
movq %r14, %rdi
callq 0x3953
jmp 0x3195
movslq %ebp, %rax
decl %ebp
cmpl $0x0, 0x1c(%rsp,%rax,4)
cmovel %ecx, %ebx
jmp 0x3195
movl $0xa, %edi
callq 0x2030
jmp 0x3195
movslq %ebp, %rax
decl %ebp
movl 0x18(%rsp,%rax,4), %ecx
xorl %edx, %edx
cmpl 0x1c(%rsp,%rax,4), %ecx
setge %dl
jmp 0x3191
movslq %ebp, %rcx
decl %ebp
movl 0x18(%rsp,%rcx,4), %eax
cltd
idivl 0x1c(%rsp,%rcx,4)
movl %eax, 0x18(%rsp,%rcx,4)
jmp 0x3195
movslq %ebp, %rax
incl %ebp
leaq (%rsp,%rax,4), %rsi
addq $0x20, %rsi
leaq 0x20c7(%rip), %rdi      # 0x518c
xorl %eax, %eax
callq 0x2160
jmp 0x3195
movslq %ebp, %rax
decl %ebp
movl 0x18(%rsp,%rax,4), %ecx
xorl %edx, %edx
cmpl 0x1c(%rsp,%rax,4), %ecx
setne %dl
jmp 0x3191
movslq %ebp, %rax
decl %ebp
movl 0x1c(%rsp,%rax,4), %ecx
subl %ecx, 0x18(%rsp,%rax,4)
jmp 0x3195
movslq %ebp, %rax
decl %ebp
movl 0x18(%rsp,%rax,4), %ecx
imull 0x1c(%rsp,%rax,4), %ecx
movl %ecx, 0x18(%rsp,%rax,4)
jmp 0x3195
movslq %ebp, %rax
decl %ebp
movl 0x18(%rsp,%rax,4), %ecx
xorl %edx, %edx
cmpl 0x1c(%rsp,%rax,4), %ecx
setl %dl
jmp 0x3191
movslq %ebp, %rax
decl %ebp
movl 0x1c(%rsp,%rax,4), %ecx
addl %ecx, 0x18(%rsp,%rax,4)
jmp 0x3195
movslq %ebp, %rax
andl $0x1, 0x1c(%rsp,%rax,4)
jmp 0x3195
movslq %ebp, %rax
decl %ebp
movl 0x1c(%rsp,%rax,4), %esi
leaq 0x203e(%rip), %rdi      # 0x518c
xorl %eax, %eax
callq 0x2090
jmp 0x3195
movslq %ebp, %rax
decl %ebp
movl 0x18(%rsp,%rax,4), %ecx
xorl %edx, %edx
cmpl 0x1c(%rsp,%rax,4), %ecx
sete %dl
jmp 0x3191
movslq %ebp, %rax
decl %ebp
movl 0x18(%rsp,%rax,4), %ecx
xorl %edx, %edx
cmpl 0x1c(%rsp,%rax,4), %ecx
setle %dl
jmp 0x3191
movslq %ebp, %rax
decl %ebp
movl 0x18(%rsp,%rax,4), %ecx
xorl %edx, %edx
cmpl 0x1c(%rsp,%rax,4), %ecx
setg %dl
movl %edx, 0x18(%rsp,%rax,4)
testl %ebx, %ebx
jne 0x2f77
addq $0x1f68, %rsp           # imm = 0x1F68
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop