// File: /workspace/github_cpp_makefile_star2_O2/sangwoojun[P]rv32emulator/emulator.cpp
// Function: parse_instr(int, char*, instr*, int, label_loc*, source*)
int parse_instr(int line, char* ftok, instr* imem, int memoff, label_loc* labels, source* src) {
	if ( memoff +4 > DATA_OFFSET ) {
		printf( "Instructions in data segment! Line %d\n", line );
		exit(1);
	}
	char* o1 = strtok(NULL, " \t\r\n,");
	char* o2 = strtok(NULL, " \t\r\n,");
	char* o3 = strtok(NULL, " \t\r\n,");
	char* o4 = strtok(NULL, " \t\r\n,");

	int ioff = memoff/4;
	int pscnt = parse_pseudoinstructions(line, ftok, imem, ioff, labels, o1, o2, o3, o4, src);
	if ( pscnt > 0 ) {
		return pscnt;
	} else {
		instr* i = &imem[ioff];
		instr_type op = parse_instr(ftok);
		i->op = op;
		i->orig_line = line;
		append_source(ftok, o1, o2, o3, src, i);

		switch( op ) {
			case UNIMPL: return 1;
			case JAL:
				if ( o2 ) { // two operands, reg, label
					if ( !o1 || !o2 || o3 || o4 ) print_syntax_error( line, "Invalid format" );
					i->a1.type = OPTYPE_REG; i->a1.reg = parse_reg(o1, line);
					i->a2.type = OPTYPE_LABEL; strncpy(i->a2.label, o2, MAX_LABEL_LEN);
				} else { // one operand, label
					if ( !o1 || o2 || o3 || o4 ) print_syntax_error( line, "Invalid format" );

					i->a1.type = OPTYPE_REG; i->a1.reg = 1;
					i->a2.type = OPTYPE_LABEL; strncpy(i->a2.label, o1, MAX_LABEL_LEN);
				}
				return 1;
			case JALR:
				if ( !o1 || !o2 || o3 || o4 ) print_syntax_error( line, "Invalid format" );
				i->a1.reg = parse_reg(o1, line);
				parse_mem(o2, &i->a2.reg, &i->a3.imm, 12, line);
				return 1;
			case ADD: case SUB: case SLT: case SLTU: case AND: case OR: case XOR: case SLL: case SRL: case SRA:
				if ( !o1 || !o2 || !o3 || o4 ) print_syntax_error( line, "Invalid format" );
				i->a1.reg = parse_reg(o1, line);
				i->a2.reg = parse_reg(o2, line);
				i->a3.reg = parse_reg(o3, line);
				return 1;
			case LB: case LBU: case LH: case LHU: case LW: case SB: case SH: case SW:
				if ( !o1 || !o2 || o3 || o4 ) print_syntax_error( line, "Invalid format" );
				i->a1.reg = parse_reg(o1, line);
				parse_mem(o2, &i->a2.reg, &i->a3.imm, 12, line);
				return 1;
			case ADDI: case SLTI: case SLTIU: case ANDI: case ORI: case XORI: case SLLI: case SRLI: case SRAI:
				if ( !o1 || !o2 || !o3 || o4 ) print_syntax_error( line, "Invalid format" );

				i->a1.reg = parse_reg(o1, line);
				i->a2.reg = parse_reg(o2, line);
				i->a3.imm = signextend(parse_imm(o3, 12, line), 12);
				return 1;
			case BEQ: case BGE: case BGEU: case BLT: case BLTU: case BNE:
				if ( !o1 || !o2 || !o3 || o4 ) print_syntax_error( line, "Invalid format" );
				i->a1.reg = parse_reg(o1, line);
				i->a2.reg = parse_reg(o2, line);
				i->a3.type = OPTYPE_LABEL; strncpy(i->a3.label, o3, MAX_LABEL_LEN);
				return 1;
			case LUI: 
			case AUIPC: // how to deal with LSB correctly? FIXME
				if ( !o1 || !o2 || o3 || o4 ) print_syntax_error( line, "Invalid format" );
				i->a1.reg = parse_reg(o1, line);
				i->a2.imm = (parse_imm(o2, 20, line));
				return 1;
			case HCF: return 1;
		}

	}
	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, -0x58(%rbp)
movq %rdx, -0x48(%rbp)
movq %rsi, -0x50(%rbp)
movl %edi, -0x3c(%rbp)
cmpl $0x7ffd, %ecx           # imm = 0x7FFD
jge 0x437c
movl %ecx, %r14d
leaq 0x685f(%rip), %rbx      # 0xa837
xorl %edi, %edi
movq %rbx, %rsi
callq 0x2330
movq %rax, %r12
xorl %edi, %edi
movq %rbx, %rsi
callq 0x2330
movq %rax, %r13
xorl %edi, %edi
movq %rbx, %rsi
callq 0x2330
movq %rax, %r15
xorl %edi, %edi
movq %rbx, %rsi
callq 0x2330
movq %rax, %r8
pushq $0x4
popq %rcx
movl %r14d, %eax
cltd
idivl %ecx
movl %eax, %ebx
movl -0x3c(%rbp), %edi
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
movl %eax, %ecx
movq %r12, -0x70(%rbp)
movq %r12, %r9
movq %r8, %r12
pushq -0x58(%rbp)
pushq %r8
movq %r15, -0x60(%rbp)
pushq %r15
movq %r13, -0x68(%rbp)
pushq %r13
callq 0x3623
addq $0x20, %rsp
movl %eax, %r14d
testl %eax, %eax
je 0x4061
movl %r14d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, -0x38(%rbp)
movslq %ebx, %rax
imulq $0x98, %rax, %r14
movq -0x48(%rbp), %r13
leaq (%r14,%r13), %rax
movq %rax, -0x30(%rbp)
movq -0x50(%rbp), %r12
movq %r12, %rdi
callq 0x2860
movl %eax, %ebx
movl %eax, (%r13,%r14)
movl -0x3c(%rbp), %r15d
movl %r15d, 0x90(%r13,%r14)
movq %r12, %rdi
movq -0x70(%rbp), %r14
movq %r14, %rsi
movq -0x68(%rbp), %r13
movq %r13, %rdx
movq -0x60(%rbp), %r12
movq %r12, %rcx
movq -0x58(%rbp), %r8
movq -0x30(%rbp), %r9
callq 0x3280
movl %ebx, %eax
leaq 0x2f80(%rip), %rcx      # 0x7048
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
movl %r15d, %edx
movq %r13, %rbx
movq %r14, %rsi
jmpq *%rax
testq %rsi, %rsi
je 0x4395
testq %rbx, %rbx
je 0x4395
testq %r12, %r12
je 0x4395
cmpq $0x0, -0x38(%rbp)
jne 0x4395
pushq $0x1
popq %r14
movq %rsi, %rdi
movl %edx, %esi
movl %edx, %r15d
movl %r14d, %edx
callq 0x2c37
movq -0x30(%rbp), %r13
movl %eax, 0x28(%r13)
movq %rbx, %rdi
movl %r15d, %esi
movl %r14d, %edx
callq 0x2c37
movl %eax, 0x54(%r13)
movq %r12, %rdi
movl %r15d, %esi
movl %r14d, %edx
callq 0x2c37
movl %eax, 0x80(%r13)
jmp 0x404f
testq %rsi, %rsi
je 0x4395
testq %rbx, %rbx
je 0x4395
testq %r12, %r12
je 0x4395
cmpq $0x0, -0x38(%rbp)
jne 0x4395
pushq $0x1
popq %r14
movq %rsi, %rdi
movl %edx, %esi
movl %edx, %r15d
movl %r14d, %edx
callq 0x2c37
movq -0x30(%rbp), %r13
movl %eax, 0x28(%r13)
movq %rbx, %rdi
movl %r15d, %esi
movl %r14d, %edx
callq 0x2c37
movl %eax, 0x54(%r13)
pushq $0xc
popq %rsi
movq %r12, %rdi
movl %r15d, %edx
movl %r14d, %ecx
callq 0x2fc6
movl %eax, %ecx
shll $0x14, %ecx
sarl $0x1f, %ecx
andl $0xfffff000, %ecx       # imm = 0xFFFFF000
orl %eax, %ecx
movl %ecx, 0x84(%r13)
jmp 0x404f
testq %rsi, %rsi
je 0x4395
testq %rbx, %rbx
je 0x4395
testq %r12, %r12
jne 0x4395
cmpq $0x0, -0x38(%rbp)
jne 0x4395
pushq $0x1
popq %r14
movq %rsi, %rdi
movl %edx, %esi
movl %edx, %r15d
movl %r14d, %edx
callq 0x2c37
movq -0x30(%rbp), %rdx
movl %eax, 0x28(%rdx)
leaq 0x54(%rdx), %rsi
addq $0x84, %rdx
pushq $0xc
popq %rcx
movq %rbx, %rdi
movl %r15d, %r8d
callq 0x3045
jmp 0x404f
testq %rsi, %rsi
je 0x4395
testq %rbx, %rbx
je 0x4395
testq %r12, %r12
je 0x4395
cmpq $0x0, -0x38(%rbp)
jne 0x4395
pushq $0x1
popq %r14
movq %rsi, %rdi
movl %edx, %esi
movl %edx, %r15d
movl %r14d, %edx
callq 0x2c37
movq -0x30(%rbp), %r13
movl %eax, 0x28(%r13)
movq %rbx, %rdi
movl %r15d, %esi
movl %r14d, %edx
callq 0x2c37
movl %eax, 0x54(%r13)
movl $0x3, 0x5c(%r13)
addq $0x60, %r13
pushq $0x20
popq %rdx
movq %r13, %rdi
movq %r12, %rsi
callq 0x2110
jmp 0x404f
testq %rsi, %rsi
je 0x4395
testq %rbx, %rbx
je 0x4395
testq %r12, %r12
jne 0x4395
cmpq $0x0, -0x38(%rbp)
jne 0x4395
pushq $0x1
popq %r14
movq %rsi, %rdi
movl %edx, %esi
movl %edx, %r15d
movl %r14d, %edx
callq 0x2c37
movq -0x30(%rbp), %r12
movl %eax, 0x28(%r12)
pushq $0x14
popq %rsi
movq %rbx, %rdi
movl %r15d, %edx
movl %r14d, %ecx
callq 0x2fc6
movl %eax, 0x58(%r12)
jmp 0x404f
pushq $0x1
popq %r14
jmp 0x404f
testq %rsi, %rsi
sete %cl
orq -0x38(%rbp), %r12
setne %al
orb %cl, %al
testq %rbx, %rbx
je 0x4355
testb %al, %al
jne 0x4395
movq -0x30(%rbp), %r15
movl $0x1, 0x4(%r15)
pushq $0x1
popq %r14
movq %rsi, %rdi
movl %edx, %esi
movl %r14d, %edx
callq 0x2c37
movl %eax, 0x28(%r15)
movl $0x3, 0x30(%r15)
addq $0x34, %r15
pushq $0x20
popq %rdx
movq %r15, %rdi
movq %rbx, %rsi
jmp 0x4291
testb %al, %al
jne 0x4395
pushq $0x1
popq %r14
movq -0x30(%rbp), %rdi
movl %r14d, 0x4(%rdi)
movl %r14d, 0x28(%rdi)
movl $0x3, 0x30(%rdi)
addq $0x34, %rdi
pushq $0x20
popq %rdx
jmp 0x4291
leaq 0x648d(%rip), %rdi      # 0xa810
movl -0x3c(%rbp), %esi
xorl %eax, %eax
callq 0x2040
pushq $0x1
popq %rdi
callq 0x2200
leaq 0x6403(%rip), %rsi      # 0xa79f
movl %edx, %edi
callq 0x27ef