// File: /workspace/github_c_makefile_star2_O2/tstword[P]MIPSAssemblerC/src/assembler.c
// Function: check_directive
int check_directive(struct instruction_node *instr) {
    struct reserved_entry *directive = instr->mnemonic;
    struct operand_node *operand_list = instr->operand_list;

    /* Check if entry is directive */
    if(directive->token != TOK_DIRECTIVE) return 0;

    struct opcode_entry *entry = (struct opcode_entry *)directive->attrptr;

    int assemble_status = 1; /* Assume assembled directive */

    if(!verify_operand_list(directive, operand_list)) {
        destroy_instruction(instr);
        return 0;
    }

    /* Check if segment is text before handling directive */
    switch(entry - opcode_table) {
        case DIRECTIVE_ASCII:
        case DIRECTIVE_ASCIIZ:
        case DIRECTIVE_HALF:
        case DIRECTIVE_BYTE:
            if(cfg_assembler->segment != SEGMENT_DATA) {
                fprintf(stderr, "Directive '%s' is not allowed in the .text segment on line %ld\n", directive->id, cfg_assembler->lineno);
                cfg_assembler->status = ASSEMBLER_STATUS_FAIL;
                destroy_instruction(instr);
                return 0;
            }
            break;
    }

    switch(entry->opcode) {
        case DIRECTIVE_INCLUDE: {
            /* Create tokenizer structure */
            struct tokenizer *tokenizer = create_tokenizer(operand_list->identifier);
            if(tokenizer == NULL) {
                fprintf(stderr, "Failed to include file '%s' on line %ld : ", operand_list->identifier, cfg_assembler->lineno);
                perror(NULL);
                cfg_assembler->status = ASSEMBLER_STATUS_FAIL;
                destroy_instruction(instr);
                assemble_status = 0;
            } 
            else {
                insert_front(cfg_assembler->tokenizer_list, (void *)tokenizer);
                cfg_assembler->tokenizer = tokenizer;
                cfg_assembler->lookahead = get_next_token(tokenizer);
            }
            break;
        }
        case DIRECTIVE_TEXT: 
            cfg_assembler->segment = SEGMENT_TEXT;
            break;
        case DIRECTIVE_DATA:
            cfg_assembler->segment = SEGMENT_DATA;
            cfg_assembler->auto_align = 1;
            break;
        case DIRECTIVE_KTEXT:
            cfg_assembler->segment = SEGMENT_KTEXT;
            break;
        case DIRECTIVE_KDATA:
            cfg_assembler->segment = SEGMENT_KDATA;
            break;
        case DIRECTIVE_ALIGN: {
            if(operand_list->value.integer > 31) {
                fprintf(stderr, "Directive '.align n' expects n to be within the range of [0, 31] on line %ld\n", cfg_assembler->lineno);
                cfg_assembler->status = ASSEMBLER_STATUS_FAIL;
                destroy_instruction(instr);
                assemble_status = 0;
            }
            else if(operand_list->value.integer == 0) {
                /* Disable automatic alignment of .half, .word, directives until next .data segment */
                cfg_assembler->auto_align = 0;
            }
            else {
                align_segment_offset(operand_list->value.integer);
            }
            break;
        }
        case DIRECTIVE_WORD: {
            struct operand_node *current_operand = operand_list;
            while(current_operand != NULL) {
                if(current_operand->operand & OPERAND_LABEL) {
                    /* Check if label has been defined */
                    struct symbol_table_entry *sym_entry = get_symbol_table(cfg_assembler->symbol_table, current_operand->identifier);
                    if(sym_entry->status == SYMBOL_UNDEFINED) {
                        /* Special case, this directive can take multiple undefined labels
                         * We must ensure that this instruction is appended only once */
                        if(assemble_status) insert_front(sym_entry->instr_list, (void *)instr);
                        incr_segment_offset(0x4);
                        assemble_status = 0;
                    }
                    else {
                        offset_t sym_offset = sym_entry->offset;
                        write_segment_memory((void *)&sym_offset, 0x4);
                        incr_segment_offset(0x4);
                    }
                }
                else {
                    int value = current_operand->value.integer;
                    write_segment_memory((void *)&value, 0x4);
                    incr_segment_offset(0x4);
                }
                current_operand = current_operand->next;
            }
            break;
        }
        case DIRECTIVE_HALF: {
            struct operand_node *current_operand = operand_list;
            while(current_operand != NULL) {
                int value = current_operand->value.integer;
                write_segment_memory((void *)&value, 0x2);
                incr_segment_offset(0x2);
                current_operand = current_operand->next;
            }
            break;
        }
        case DIRECTIVE_BYTE: {
            struct operand_node *current_operand = operand_list;
            while(current_operand != NULL) {
                int value = current_operand->value.integer;
                write_segment_memory((void *)&value, 0x1);
                incr_segment_offset(0x1);
                current_operand = current_operand->next;
            }
            break;
        }
        case DIRECTIVE_ASCII: {
            write_escaped_string(operand_list->identifier);
            break;
        }
        case DIRECTIVE_ASCIIZ: {
            int nullterm = '\0';
            write_escaped_string(operand_list->identifier);
            write_segment_memory((void *)&nullterm, 0x1);
            incr_segment_offset(0x1);
            break;
        }
        case DIRECTIVE_SPACE: {
            alloc_segment_memory(operand_list->value.integer);
            incr_segment_offset(operand_list->value.integer);
            break;
        }
    }

    return assemble_status;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq (%rdi), %r12
xorl %ebx, %ebx
cmpl $0xc, 0x8(%r12)
jne 0x4f1d
movq %rdi, %r14
movq 0x8(%rdi), %r15
movq 0x10(%r12), %r13
movq %r12, %rdi
movq %r15, %rsi
callq 0x3bb3
testl %eax, %eax
je 0x4f15
movq %r13, %rax
subq 0x62f0(%rip), %rax      # 0xafb8
sarq $0x3, %rax
addq $-0x3c, %rax
cmpq $0x4, %rax
ja 0x4ced
cmpq $0x3, %rax
je 0x4ced
movq 0x78d5(%rip), %rax      # 0xc5b8
cmpb $0x1, 0x45(%rax)
jne 0x4ee5
movzbl (%r13), %eax
pushq $0x1
popq %rbx
addl $-0x39, %eax
cmpl $0xb, %eax
ja 0x4f1d
leaq 0x2540(%rip), %rcx      # 0x7248
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x8(%r15), %rdi
callq 0x64bd
testq %rax, %rax
je 0x4f3b
movq %rax, %r12
movq 0x788b(%rip), %rax      # 0xc5b8
movq 0x8(%rax), %rdi
movq %r12, %rsi
callq 0x547b
movq 0x7878(%rip), %rax      # 0xc5b8
movq %r12, (%rax)
movq %r12, %rdi
callq 0x654d
movq 0x7866(%rip), %rcx      # 0xc5b8
movl %eax, 0x40(%rcx)
jmp 0x4f1d
movl 0x8(%r15), %edi
callq 0x34b3
movl 0x8(%r15), %edi
jmp 0x4d8a
leaq 0x4(%rsp), %rbx
andl $0x0, (%rbx)
movq 0x8(%r15), %rdi
callq 0x35fc
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x3581
pushq $0x1
popq %rbx
movl %ebx, %edi
callq 0x3411
jmp 0x4f1d
movq 0x781d(%rip), %rax      # 0xc5b8
movb $0x2, 0x45(%rax)
jmp 0x4f1d
movq 0x780d(%rip), %rax      # 0xc5b8
movw $0x101, 0x45(%rax)      # imm = 0x101
jmp 0x4f1d
movq 0x8(%r15), %rdi
callq 0x35fc
jmp 0x4f1d
leaq 0x4(%rsp), %r14
pushq $0x2
popq %r12
pushq $0x2
popq %rbp
testq %r15, %r15
je 0x4f1d
movl 0x8(%r15), %eax
movl %eax, 0x4(%rsp)
movq %r14, %rdi
movq %r12, %rsi
callq 0x3581
movl %ebp, %edi
callq 0x3411
movq 0x10(%r15), %r15
jmp 0x4dd0
movq 0x77b8(%rip), %rax      # 0xc5b8
movb $0x0, 0x45(%rax)
jmp 0x4f1d
pushq $0x1
popq %rbx
leaq 0x4(%rsp), %r14
pushq $0x1
popq %r12
testq %r15, %r15
je 0x4f1d
movl 0x8(%r15), %eax
movl %eax, 0x4(%rsp)
movq %r14, %rdi
movq %r12, %rsi
callq 0x3581
movl %ebx, %edi
callq 0x3411
movq 0x10(%r15), %r15
jmp 0x4e15
movl 0x8(%r15), %edi
cmpl $0x20, %edi
jae 0x4f70
testl %edi, %edi
je 0x4f2e
callq 0x3488
jmp 0x4f1d
movq 0x7754(%rip), %rax      # 0xc5b8
movb $0x3, 0x45(%rax)
jmp 0x4f1d
pushq $0x1
popq %rbx
leaq 0x4(%rsp), %r12
pushq $0x4
popq %r13
pushq $0x4
popq %rbp
testq %r15, %r15
je 0x4f1d
testb $0x1, (%r15)
jne 0x4e91
movl 0x8(%r15), %eax
jmp 0x4eae
movq 0x7720(%rip), %rax      # 0xc5b8
movq 0x10(%rax), %rdi
movq 0x8(%r15), %rsi
callq 0x5c1a
cmpb $0x0, 0x8(%rax)
je 0x4eca
movl 0xc(%rax), %eax
movl %eax, 0x4(%rsp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x3581
movl %ebp, %edi
callq 0x3411
movq 0x10(%r15), %r15
jmp 0x4e7c
testl %ebx, %ebx
je 0x4eda
movq 0x18(%rax), %rdi
movq %r14, %rsi
callq 0x547b
movl %ebp, %edi
callq 0x3411
xorl %ebx, %ebx
jmp 0x4ec4
movq 0x610c(%rip), %rcx      # 0xaff8
movq (%rcx), %rdi
movq (%r12), %rdx
movq 0x98(%rax), %rcx
leaq 0x2673(%rip), %rsi      # 0x7574
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x30d0
movq 0x76a7(%rip), %rax      # 0xc5b8
movb $0x2, 0x44(%rax)
movq %r14, %rdi
callq 0x3d45
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x7683(%rip), %rax      # 0xc5b8
movb $0x0, 0x46(%rax)
jmp 0x4f1d
movq 0x60b6(%rip), %rax      # 0xaff8
movq (%rax), %rdi
movq 0x8(%r15), %rdx
movq 0x7668(%rip), %rax      # 0xc5b8
movq 0x98(%rax), %rcx
leaq 0x2656(%rip), %rsi      # 0x75b4
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x30d0
xorl %edi, %edi
callq 0x3130
jmp 0x4f0a
movq 0x6081(%rip), %rax      # 0xaff8
movq (%rax), %rdi
movq 0x7637(%rip), %rax      # 0xc5b8
movq 0x98(%rax), %rdx
leaq 0x2650(%rip), %rsi      # 0x75df
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x30d0
jmp 0x4f0a