// File: /workspace/github_c_makefile_star2_O2/endeav0r[P]hsvm/src/assembler.c
// Function: assemble
uint16_t assemble (struct _token * tokens, unsigned char * mem)
{
    struct _labels labels;
    struct _label * label;
    struct _instruction ins;
    struct _token * next;
    struct _token * peek;
    int rule;
    int mem_i = 0;
    int copy_instruction = 0;
    uint16_t lval;

    labels.first = NULL;
    labels.last = NULL;

    // first pass to assemble
    next = tokens;
    while (next != NULL) {
        memset(&ins, 0, sizeof(struct _instruction));
        rule = match_rule(next);
        copy_instruction = 0;

        if (rule == -1) {
            fprintf(stderr, "couldn't match rule, line %d\n", next->line);
            for (peek = next;
                 (peek != NULL) && (peek->type != TOK_NEWLINE);
                 peek = peek->next) {
                fprintf(stderr, "%s ", token_str(peek));
            }
            fprintf(stderr, "\n");
            exit(-1);
        }

        switch (rule) {
        case RULE_MOV :
        case RULE_CMP :
            ins.opcode = next->type;
            peek = token_peek(next, 1);
            ins.operand_0 = peek->type;
            peek = token_peek(next, 3);
            ins.operand_1 = peek->type;
            copy_instruction = 1;
            break;

        case RULE_MOVLVAL    :
        case RULE_MOVLABEL   :
        case RULE_CMPLVAL    :
        case RULE_ADDLVAL    :
        case RULE_SUBLVAL    :
        case RULE_MULLVAL    :
        case RULE_DIVLVAL    :
        case RULE_MODLVAL    :
        case RULE_ANDLVAL    :
        case RULE_ORLVAL     :
        case RULE_XORLVAL    :
            if (next->type == OP_MOV) ins.opcode = OP_MOVLVAL;
            else if (next->type == OP_CMP) ins.opcode = OP_CMPLVAL;
            else if (next->type == OP_ADD) ins.opcode = OP_ADDLVAL;
            else if (next->type == OP_SUB) ins.opcode = OP_SUBLVAL;
            else if (next->type == OP_MUL) ins.opcode = OP_MULLVAL;
            else if (next->type == OP_DIV) ins.opcode = OP_DIVLVAL;
            else if (next->type == OP_MOD) ins.opcode = OP_MODLVAL;
            else if (next->type == OP_AND) ins.opcode = OP_ANDLVAL;
            else if (next->type == OP_OR ) ins.opcode = OP_ORLVAL;
            else if (next->type == OP_XOR) ins.opcode = OP_XORLVAL;
            peek = token_peek(next, 1);
            ins.operand_0 = peek->type;
            peek = token_peek(next, 3);
            lval = strtoul(peek->str, NULL, 0);
            lval = htons(lval);
            ins.lval = lval;
            copy_instruction = 1;
            break;

        case RULE_LOAD  :
        case RULE_STOR  :
        case RULE_LOADB :
        case RULE_STORB :
        case RULE_IN    :
        case RULE_OUT   :
        case RULE_PUSH  :
        case RULE_POP   :
            ins.opcode = next->type;
            if ((rule == RULE_STOR) || (rule == RULE_STORB))
                peek = token_peek(next, 3);
            else
                peek = token_peek(next, 1);
            ins.operand_0 = peek->type;
            copy_instruction = 1;
            break;

        case RULE_ADD :
        case RULE_SUB :
        case RULE_MUL :
        case RULE_DIV :
        case RULE_MOD :
        case RULE_AND :
        case RULE_OR  :
        case RULE_XOR :
            ins.opcode = next->type;
            peek = token_peek(next, 1);
            ins.operand_0 = peek->type;
            peek = token_peek(next, 3);
            ins.operand_1 = peek->type;
            peek = token_peek(next, 5);
            ins.operand_2 = peek->type;
            copy_instruction = 1;
            break;

        case RULE_JMP     :
        case RULE_JE      :
        case RULE_JNE     :
        case RULE_JL      :
        case RULE_JLE     :
        case RULE_JG      :
        case RULE_JGE     :
        case RULE_CALL    :
        case RULE_RET     :
        case RULE_HLT     :
        case RULE_SYSCALL :
        case RULE_NOP     :
        case RULE_PUSHLABEL :
            if (rule == RULE_PUSHLABEL)
                ins.opcode = OP_PUSHLVAL;
            else
                ins.opcode = next->type;
            copy_instruction = 1;
            break;

        case RULE_LOADR  :
        case RULE_STORR  :
        case RULE_LOADBR :
        case RULE_STORBR :
            if (rule == RULE_LOADR)
                ins.opcode = OP_LOADR;
            else if (rule == RULE_STORR)
                ins.opcode = OP_STORR;
            else if (rule == RULE_LOADBR)
                ins.opcode = OP_LOADBR;
            else if (rule == RULE_STORBR)
                ins.opcode = OP_STORBR;
            peek = token_peek(next, 1);
            ins.operand_0 = peek->type;
            peek = token_peek(next, 3);
            ins.operand_1 = peek->type;
            copy_instruction = 1;
            break;

        case RULE_PUSHLVAL :
        case RULE_JMPLVAL  :
        case RULE_JELVAL   :
        case RULE_JNELVAL  :
        case RULE_JLLVAL   :
        case RULE_JLELVAL  :
        case RULE_JGLVAL   :
        case RULE_JGELVAL  :
            if (rule == RULE_PUSHLVAL) ins.opcode = OP_PUSHLVAL;
            else ins.opcode = next->type;
            peek = token_peek(next, 1);
            lval = strtoul(peek->str, NULL, 0);
            lval = htons(lval);
            ins.lval = lval;
            copy_instruction = 1;
            break;

        case RULE_LABELNUM :
            labels_append(&labels, label_create(next->str, mem_i));

            peek = token_peek(next, 1);
            lval = strtoul(peek->str, NULL, 0);
            lval = htons(lval);
            memcpy(&(mem[mem_i]), &lval, 2);
            mem_i += 2;
            break;

        case RULE_LABELSTR :
            labels_append(&labels, label_create(next->str, mem_i));

            peek = token_peek(next, 1);
            memcpy(&(mem[mem_i]), peek->str, strlen(peek->str) + 1);
            mem_i += strlen(peek->str) + 1;
            break;

        case RULE_LABELLABEL :
            labels_append(&labels, label_create(next->str, mem_i));
            mem_i += 2;
            break;

        case RULE_LABEL :
            labels_append(&labels, label_create(next->str, mem_i));
            break;
        }

        if (copy_instruction) {
            memcpy(&(mem[mem_i]), &ins, sizeof(struct _instruction));
            mem_i += sizeof(struct _instruction);
        }
        next = advance_token(next, rule_size(rule));
    }

    // second pass to patch labels
    mem_i = 0;
    next = tokens;
    while (next != NULL) {
        rule = match_rule(next);
        switch (rule) {
        case RULE_JMP       :
        case RULE_JE        :
        case RULE_JNE       :
        case RULE_JL        :
        case RULE_JLE       :
        case RULE_JG        :
        case RULE_JGE       :
        case RULE_CALL      :
        case RULE_LOAD      :
        case RULE_LOADB     :
        case RULE_STOR      :
        case RULE_STORB     :
        case RULE_MOVLABEL  :
        case RULE_PUSHLABEL :

            if ((rule == RULE_LOAD) || (rule == RULE_MOVLABEL))
                peek = token_peek(next, 3);
            else
                peek = token_peek(next, 1);

            label = labels_search(&labels, peek->str);
            if (label == NULL) {
                fprintf(stderr, "could not find label: %s\n", peek->str);
                exit(-1);
            }

            memcpy(&ins, &(mem[mem_i]), sizeof(struct _instruction));

            ins.lval = label->location;
            if (    (rule == RULE_JMP)   || (rule == RULE_JE)  || (rule == RULE_JNE)
                 || (rule == RULE_JL)    || (rule == RULE_JLE)
                 || (rule == RULE_JG)    || (rule == RULE_JGE)
                 || (rule == RULE_CALL)) {
                ins.lval = ins.lval - mem_i - 4;
            }
            ins.lval = htons(ins.lval);

            memcpy(&(mem[mem_i]), &ins, sizeof(struct _instruction));

        case RULE_MOV       :
        case RULE_MOVLVAL   :
        case RULE_CMP       :
        case RULE_CMPLVAL   :
        case RULE_ADD       :
        case RULE_ADDLVAL   :
        case RULE_SUB       :
        case RULE_SUBLVAL   :
        case RULE_MUL       :
        case RULE_MULLVAL   :
        case RULE_DIV       :
        case RULE_DIVLVAL   :
        case RULE_MOD       :
        case RULE_MODLVAL   :
        case RULE_AND       :
        case RULE_ANDLVAL   :
        case RULE_OR        :
        case RULE_ORLVAL    :
        case RULE_XOR       :
        case RULE_XORLVAL   :
        case RULE_JMPLVAL   :
        case RULE_JELVAL    :
        case RULE_JNELVAL   :
        case RULE_JLLVAL    :
        case RULE_JLELVAL   :
        case RULE_JGLVAL    :
        case RULE_JGELVAL   :
        case RULE_CALLR     :
        case RULE_RET       :
        case RULE_LOADR     :
        case RULE_LOADBR    :
        case RULE_STORR     :
        case RULE_STORBR    :
        case RULE_IN        :
        case RULE_OUT       :
        case RULE_PUSH      :
        case RULE_PUSHLVAL  :
        case RULE_POP       :
        case RULE_HLT       :
        case RULE_SYSCALL   :
        case RULE_NOP       :
            mem_i += sizeof(struct _instruction);
            break;

        case RULE_LABELLABEL :
            peek = token_peek(next, 1);

            label = labels_search(&labels, peek->str);
            if (label == NULL) {
                fprintf(stderr, "could not find label: %s\n", peek->str);
                exit(-1);
            }
            ins.lval = htons(label->location);
            memcpy(&(mem[mem_i]), &(ins.lval), 2);

        case RULE_LABELNUM :
            mem_i += 2;
            break;

        case RULE_LABELSTR :
            peek = token_peek(next, 1);
            mem_i += strlen(peek->str) + 1;
            break;
        }
        next = advance_token(next, rule_size(rule));
    }

    return mem_i;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, 0x18(%rsp)
movq %rdi, %r12
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x10(%rsp)
pushq $0x3
popq %r14
movq $0x0, 0x8(%rsp)
movq %rdi, 0x30(%rsp)
xorl %r15d, %r15d
testq %r12, %r12
je 0x2934
movq %r12, %rdi
callq 0x23ea
movl %eax, %edi
leal 0x1(%rdi), %eax
cmpl $0x3b, %eax
ja 0x290e
movb $0x43, %bl
leaq 0x3a80(%rip), %rcx      # 0x6004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
movslq %r15d, %rbp
jmpq *%rax
movq %r15, %r13
movb (%r12), %bl
xorl %r15d, %r15d
jmp 0x25f9
movl (%r12), %eax
leal -0x10(%rax), %ecx
cmpl $0xe, %ecx
movq %rdi, (%rsp)
ja 0x266a
leaq 0x3b3d(%rip), %rdx      # 0x60f4
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
movq %r15, %r13
movb $0x11, %bl
jmp 0x28c7
movq %r15, %r13
movb (%r12), %bl
leal -0x2a(%rdi), %eax
xorl %ecx, %ecx
testl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
sete %cl
leal 0x1(%rcx,%rcx), %esi
movl %r14d, %r15d
movq %rdi, %r14
movq %r12, %rdi
callq 0x2472
movq %r14, %rdi
movl %r15d, %r14d
movb (%rax), %r15b
xorl %eax, %eax
jmp 0x28f5
movq %r15, %r13
movb (%r12), %bl
movq %rdi, (%rsp)
movq %r12, %rdi
pushq $0x1
popq %rsi
callq 0x2472
movb (%rax), %r15b
movq %r12, %rdi
movl %r14d, %esi
callq 0x2472
movzbl (%rax), %r14d
movq %r12, %rdi
pushq $0x5
popq %rsi
callq 0x2472
movq (%rsp), %rdi
movl (%rax), %eax
shll $0x8, %eax
orl %r14d, %eax
pushq $0x3
popq %r14
jmp 0x28f5
movq %rdi, (%rsp)
movq %r15, %r13
movb (%r12), %bl
movq %r12, %rdi
pushq $0x1
popq %rsi
callq 0x2472
movq 0x8(%rax), %rdi
xorl %r15d, %r15d
jmp 0x28e4
cmpl $0x51, %eax
je 0x28c2
cmpl $0x53, %eax
jne 0x26b2
movq %r15, %r13
movb $0x54, %bl
jmp 0x28c7
movq %r15, %r13
movb (%r12), %bl
movq %rdi, (%rsp)
movq %r12, %rdi
pushq $0x1
popq %rsi
callq 0x2472
movb (%rax), %r15b
movq %r12, %rdi
movl %r14d, %esi
callq 0x2472
movq (%rsp), %rdi
movzbl (%rax), %eax
jmp 0x28f5
movq %r15, %r13
xorl %ebx, %ebx
jmp 0x28c7
movq %r15, %r13
movb $0x1b, %bl
jmp 0x28c7
movq %r15, %r13
movb $0x1d, %bl
jmp 0x28c7
movq %r15, %r13
movb $0x1f, %bl
jmp 0x28c7
movq %r15, %r13
movb $0x13, %bl
jmp 0x28c7
movq %r15, %r13
movb $0x19, %bl
jmp 0x28c7
movq %r15, %r13
movb $0x15, %bl
jmp 0x28c7
movq %r15, %r13
movb $0x17, %bl
jmp 0x28c7
movq %rdi, (%rsp)
movq %r15, %r13
jmp 0x2653
movq %rdi, (%rsp)
movq %r15, %r13
movb $0x37, %bl
jmp 0x27fb
movq %rdi, (%rsp)
movq 0x8(%r12), %rdi
movzwl %r15w, %esi
callq 0x2488
movq %rax, %rdx
movq %rax, %rbx
movq 0x8(%rsp), %rcx
testq %rcx, %rcx
je 0x274a
movq 0x10(%rsp), %rax
movq %rdx, 0x10(%rax)
movq %rcx, %rbx
movq %r12, %rdi
pushq $0x1
popq %rsi
movq %rdx, 0x8(%rsp)
callq 0x2472
movq %rax, %r14
addq 0x18(%rsp), %rbp
movq 0x8(%rax), %r13
movq %r13, %rdi
callq 0x2100
leaq 0x1(%rax), %rdx
movq %rbp, %rdi
movq %r13, %rsi
callq 0x21e0
movq 0x8(%r14), %rdi
callq 0x2100
addl %eax, %r15d
incl %r15d
movq 0x8(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %rbx, 0x8(%rsp)
movq (%rsp), %rdi
jmp 0x28bc
movq %rdi, (%rsp)
movq %r15, %r13
movb $0x33, %bl
jmp 0x27fb
movq %r15, %r13
jmp 0x2597
movq %rdi, (%rsp)
movq %r15, %r13
movb $0x31, %bl
jmp 0x27fb
movq %rdi, %rbx
movq 0x8(%r12), %rdi
movzwl %r15w, %esi
callq 0x2488
cmpq $0x0, 0x8(%rsp)
je 0x2925
movq 0x10(%rsp), %rcx
movq %rax, 0x10(%rcx)
movq %rax, 0x10(%rsp)
jmp 0x292f
movq %rdi, (%rsp)
movq %r15, %r13
movb $0x35, %bl
movq %r12, %rdi
pushq $0x1
popq %rsi
callq 0x2472
movb (%rax), %r15b
movq %r12, %rdi
movl %r14d, %esi
callq 0x2472
movzbl (%rax), %eax
jmp 0x28f1
movq %rdi, %rbx
movq 0x8(%r12), %rdi
movzwl %r15w, %esi
callq 0x2488
movq %rax, %rcx
movq 0x8(%rsp), %rdx
testq %rdx, %rdx
je 0x2846
movq 0x10(%rsp), %rcx
movq %rax, 0x10(%rcx)
movq %rdx, %rcx
addl $0x2, %r15d
movq %rax, 0x10(%rsp)
movq %rcx, 0x8(%rsp)
jmp 0x292f
movq %rdi, %r14
movq 0x8(%r12), %rdi
movzwl %r15w, %esi
callq 0x2488
movq %rax, %r13
movq %rax, %rbx
movq 0x8(%rsp), %rcx
testq %rcx, %rcx
je 0x2886
movq 0x10(%rsp), %rax
movq %r13, 0x10(%rax)
movq %rcx, %rbx
movq %r12, %rdi
pushq $0x1
popq %rsi
callq 0x2472
movq 0x8(%rax), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2290
rolw $0x8, %ax
movq 0x18(%rsp), %rcx
movw %ax, (%rcx,%rbp)
addl $0x2, %r15d
movq %r13, 0x10(%rsp)
movq %rbx, 0x8(%rsp)
movq %r14, %rdi
pushq $0x3
popq %r14
jmp 0x290e
movq %r15, %r13
movb $0x52, %bl
movq %r12, %rdi
pushq $0x1
popq %rsi
callq 0x2472
movb (%rax), %r15b
movq %r12, %rdi
movl %r14d, %esi
callq 0x2472
movq 0x8(%rax), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2290
rolw $0x8, %ax
movq (%rsp), %rdi
movq 0x18(%rsp), %rcx
movb %bl, (%rcx,%rbp)
movb %r15b, 0x1(%rcx,%rbp)
movw %ax, 0x2(%rcx,%rbp)
movq %r13, %r15
addl $0x4, %r15d
callq 0x243f
movq %r12, %rdi
movl %eax, %esi
callq 0x245c
movq %rax, %r12
jmp 0x255c
movq %rax, 0x10(%rsp)
movq %rax, 0x8(%rsp)
movq %rbx, %rdi
jmp 0x290e
leaq 0x20(%rsp), %rcx
movq 0x8(%rsp), %rax
movq %rax, (%rcx)
movq 0x10(%rsp), %rax
movq %rax, 0x8(%rcx)
xorl %r13d, %r13d
movq 0x30(%rsp), %r12
testq %r12, %r12
je 0x2a67
movq %r12, %rdi
callq 0x23ea
movl %eax, %ebp
cmpl $0x39, %eax
ja 0x29e7
movl %ebp, %ebx
leaq 0x37bd(%rip), %rcx      # 0x6130
movslq (%rcx,%rbx,4), %rax
addq %rcx, %rax
movslq %r13d, %r15
movl %r14d, %esi
jmpq *%rax
pushq $0x1
popq %rsi
movq %r12, %r14
movq %r12, %rdi
callq 0x2472
movq 0x8(%rax), %r12
leaq 0x20(%rsp), %rdi
movq %r12, %rsi
callq 0x24ee
testq %rax, %rax
je 0x2ade
movzwl 0x8(%rax), %eax
cmpl $0x23, %ebp
ja 0x2a5f
movabsq $0xaaaa00000, %rcx      # imm = 0xAAAA00000
btq %rbx, %rcx
movq %r14, %r12
jae 0x29d0
subl %r13d, %eax
addl $-0x4, %eax
pushq $0x3
popq %r14
rolw $0x8, %ax
movq 0x18(%rsp), %rcx
movw %ax, 0x2(%rcx,%r15)
addl $0x4, %r13d
movl %ebp, %edi
callq 0x243f
movq %r12, %rdi
movl %eax, %esi
callq 0x245c
movq %rax, %r12
jmp 0x2952
movq %r12, %rdi
pushq $0x1
popq %rsi
callq 0x2472
movq 0x8(%rax), %rdi
callq 0x2100
addl %eax, %r13d
incl %r13d
jmp 0x29e7
movq %r12, %rbx
movq %r12, %rdi
pushq $0x1
popq %rsi
callq 0x2472
movq 0x8(%rax), %r12
leaq 0x20(%rsp), %rdi
movq %r12, %rsi
callq 0x24ee
testq %rax, %rax
je 0x2ade
movzwl 0x8(%rax), %eax
rolw $0x8, %ax
movq 0x18(%rsp), %rcx
movw %ax, (%rcx,%r15)
movq %rbx, %r12
addl $0x2, %r13d
jmp 0x29e7
movq %r14, %r12
jmp 0x29d0
movl %r13d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x6578(%rip), %r15      # 0x8ff8
movq (%r15), %rdi
movl 0x4(%r12), %edx
leaq 0x3789(%rip), %rsi      # 0x6218
xorl %eax, %eax
callq 0x21c0
leaq 0x3799(%rip), %rbx      # 0x6236
testq %r12, %r12
je 0x2acb
cmpl $0x70, (%r12)
je 0x2acb
movq (%r15), %r14
movq %r12, %rdi
callq 0x416d
movq %r14, %rdi
movq %rbx, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x21c0
movq 0x10(%r12), %r12
jmp 0x2a9d
movq (%r15), %rsi
pushq $0xa
popq %rdi
callq 0x2160
pushq $-0x1
popq %rdi
callq 0x22b0
movq 0x6513(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
leaq 0x374b(%rip), %rsi      # 0x623a
movq %r12, %rdx
xorl %eax, %eax
callq 0x21c0
jmp 0x2ad6