// File: Eronana[P]strawberry/src/assembler.cpp
// Function: gen_code()
bool gen_code()
{
    int op_code_size=0;
    for(auto &x:s_code)
    {
        op_pos[op_pos.size()]=op_code_size;
        if(!opcode.count(x.oper))
        {
            fprintf(stderr,"Error(line %d): unknown instruction \"%s\"\n",x.line,x.oper.c_str());
            return false;
        }
        int op=opcode[x.oper];
        int int_opnd=-1;
        if(op==OP_PUSH)
        {
            switch(get_opnd_type(x.opnd))
            {
                case OT_NULL:
                    op=OP_NPUSH;
                    break;
                case OT_BOOL:
                    op=OP_BPUSH;
                    break;
                case OT_INT:
                    op=OP_IPUSH;
                    break;
                case OT_FLOAT:
                    op=OP_FPUSH;
                    break;
                case OT_STRING:
                    op=OP_SPUSH;
                    break;
                default:
                    fprintf(stderr,"Error(line %d): incorrect operand\n",x.line);
                    return false;
            }
            get_opnd(x.opnd,int_opnd);
        }
        code.push_back(op);
        switch(op)
        {
            case OP_IPUSH:
            case OP_FPUSH:
            case OP_SPUSH:
            case OP_LOAD_GLOBAL:
            case OP_STORE_GLOBAL:
            case OP_LOAD:
            case OP_STORE:
            case OP_LOAD_EXTERNAL:
            case OP_STORE_EXTERNAL:
            case OP_ADDSP:
            case OP_SUBSP:
            case OP_CALL:
                if(int_opnd==-1)int_opnd=atoi(x.opnd.c_str());
                add_int32(int_opnd);
                break;
            case OP_CREATE_FUNCTION:
            case OP_JMP:
                jmp_table.push_back({code.size(),labels[x.opnd]});
                add_int32(0);
                break;
            case OP_BPUSH:
                add_int8(x.opnd[0]=='T'?1:0);
                break;
        }
        op_code_size+=op_length[op];
    }
    for(auto &x:jmp_table)
    {
        *(int32_t*)&code[x.first]=op_pos[x.second];
    }
    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movl $0x0, -0x8(%rbp)
leaq 0x479d7(%rip), %rax     # 0x8ab60
movq %rax, -0x10(%rbp)
leaq 0x479cc(%rip), %rdi     # 0x8ab60
callq 0x44950
movq %rax, -0x18(%rbp)
leaq 0x479bc(%rip), %rdi     # 0x8ab60
callq 0x44980
movq %rax, -0x20(%rbp)
leaq -0x18(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0x449b0
testb $0x1, %al
jne 0x431c3
jmp 0x4345e
leaq -0x18(%rbp), %rdi
callq 0x449f0
movq %rax, -0x28(%rbp)
movl -0x8(%rbp), %eax
movl %eax, -0x6c(%rbp)
leaq 0x47953(%rip), %rdi     # 0x8ab30
callq 0x442c0
movl %eax, -0x2c(%rbp)
leaq 0x47944(%rip), %rdi     # 0x8ab30
leaq -0x2c(%rbp), %rsi
callq 0x44a10
movl -0x6c(%rbp), %ecx
movl %ecx, (%rax)
movq -0x28(%rbp), %rsi
addq $0x8, %rsi
leaq 0x477d7(%rip), %rdi     # 0x8a9e0
callq 0x18850
cmpq $0x0, %rax
jne 0x43259
movq 0x46dbd(%rip), %rax     # 0x89fd8
movq (%rax), %rax
movq %rax, -0x78(%rbp)
movq -0x28(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x70(%rbp)
movq -0x28(%rbp), %rdi
addq $0x8, %rdi
callq 0x7120
movq -0x78(%rbp), %rdi
movl -0x70(%rbp), %edx
movq %rax, %rcx
leaq 0x15ce9(%rip), %rsi     # 0x58f32
movb $0x0, %al
callq 0x74c0
movb $0x0, -0x1(%rbp)
jmp 0x434ef
movq -0x28(%rbp), %rsi
addq $0x8, %rsi
leaq 0x47778(%rip), %rdi     # 0x8a9e0
callq 0x188b0
movl (%rax), %eax
movl %eax, -0x30(%rbp)
movl $0xffffffff, -0x34(%rbp) # imm = 0xFFFFFFFF
cmpl $0x12345678, -0x30(%rbp) # imm = 0x12345678
jne 0x43317
movq -0x28(%rbp), %rdi
addq $0x28, %rdi
callq 0x42310
movl %eax, %ecx
movq %rcx, -0x80(%rbp)
subl $0x4, %eax
ja 0x432df
movq -0x80(%rbp), %rax
leaq 0x1593f(%rip), %rcx     # 0x58be8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x6, -0x30(%rbp)
jmp 0x43306
movl $0x7, -0x30(%rbp)
jmp 0x43306
movl $0x8, -0x30(%rbp)
jmp 0x43306
movl $0x9, -0x30(%rbp)
jmp 0x43306
movl $0xa, -0x30(%rbp)
jmp 0x43306
movq 0x46cf2(%rip), %rax     # 0x89fd8
movq (%rax), %rdi
movq -0x28(%rbp), %rax
movl (%rax), %edx
leaq 0x15c66(%rip), %rsi     # 0x58f5c
movb $0x0, %al
callq 0x74c0
movb $0x0, -0x1(%rbp)
jmp 0x434ef
movq -0x28(%rbp), %rdi
addq $0x28, %rdi
leaq -0x34(%rbp), %rsi
callq 0x42690
movb -0x30(%rbp), %al
movb %al, -0x35(%rbp)
leaq 0x47854(%rip), %rdi     # 0x8ab78
leaq -0x35(%rbp), %rsi
callq 0x44850
movl -0x30(%rbp), %eax
movl %eax, -0x84(%rbp)
subl $0x6, %eax
jb 0x43399
jmp 0x4333d
movl -0x84(%rbp), %eax
subl $0x7, %eax
je 0x4340e
jmp 0x4334e
movl -0x84(%rbp), %eax
addl $-0x8, %eax
subl $0x3, %eax
jb 0x43399
jmp 0x4335e
movl -0x84(%rbp), %eax
subl $0xe, %eax
je 0x433c1
jmp 0x4336b
movl -0x84(%rbp), %eax
addl $-0x33, %eax
subl $0x2, %eax
jb 0x43399
jmp 0x4337b
movl -0x84(%rbp), %eax
subl $0x35, %eax
je 0x433c1
jmp 0x43388
movl -0x84(%rbp), %eax
subl $0x36, %eax
jne 0x43437
jmp 0x43399
cmpl $-0x1, -0x34(%rbp)
jne 0x433b7
movq -0x28(%rbp), %rdi
addq $0x28, %rdi
callq 0x7120
movq %rax, %rdi
callq 0x7530
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %edi
callq 0x43120
jmp 0x43437
leaq 0x477b0(%rip), %rdi     # 0x8ab78
callq 0x44880
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rsi
addq $0x28, %rsi
leaq 0x47630(%rip), %rdi     # 0x8aa10
callq 0x188b0
movq %rax, %rdx
leaq -0x40(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x44b20
leaq 0x4771c(%rip), %rdi     # 0x8ab18
leaq -0x40(%rbp), %rsi
callq 0x44af0
xorl %edi, %edi
callq 0x43120
jmp 0x43437
movq -0x28(%rbp), %rdi
addq $0x28, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x7660
movsbl (%rax), %edx
xorl %eax, %eax
movl $0x1, %ecx
cmpl $0x54, %edx
cmovel %ecx, %eax
movsbl %al, %edi
callq 0x430f0
movslq -0x30(%rbp), %rcx
leaq 0x157be(%rip), %rax     # 0x58c00
movq (%rax,%rcx,8), %rcx
movslq -0x8(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x8(%rbp)
leaq -0x18(%rbp), %rdi
callq 0x44b50
jmp 0x431ad
leaq 0x476b3(%rip), %rax     # 0x8ab18
movq %rax, -0x50(%rbp)
leaq 0x476a8(%rip), %rdi     # 0x8ab18
callq 0x44b70
movq %rax, -0x58(%rbp)
leaq 0x47698(%rip), %rdi     # 0x8ab18
callq 0x44ba0
movq %rax, -0x60(%rbp)
leaq -0x58(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x44bd0
testb $0x1, %al
jne 0x4349c
jmp 0x434eb
leaq -0x58(%rbp), %rdi
callq 0x44c10
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rsi
addq $0x4, %rsi
leaq 0x47678(%rip), %rdi     # 0x8ab30
callq 0x442e0
movl (%rax), %eax
movl %eax, -0x88(%rbp)
movq -0x68(%rbp), %rax
movslq (%rax), %rsi
leaq 0x476a5(%rip), %rdi     # 0x8ab78
callq 0x44930
movl -0x88(%rbp), %ecx
movl %ecx, (%rax)
leaq -0x58(%rbp), %rdi
callq 0x44c30
jmp 0x43489
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax)