// File: /workspace/github_cpp_makefile_star2_O1/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 0x467e
movl %ecx, %r14d
leaq 0x758b(%rip), %rbx      # 0xb837
xorl %edi, %edi
movq %rbx, %rsi
callq 0x2320
movq %rax, %r12
xorl %edi, %edi
movq %rbx, %rsi
callq 0x2320
movq %rax, %r13
xorl %edi, %edi
movq %rbx, %rsi
callq 0x2320
movq %rax, %r15
xorl %edi, %edi
movq %rbx, %rsi
callq 0x2320
leal 0x3(%r14), %ebx
testl %r14d, %r14d
cmovnsl %r14d, %ebx
sarl $0x2, %ebx
movl -0x3c(%rbp), %edi
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
movl %ebx, %ecx
movq %r12, -0x70(%rbp)
movq %r12, %r9
movq %rax, %r12
pushq -0x58(%rbp)
pushq %rax
movq %r15, -0x60(%rbp)
pushq %r15
movq %r13, -0x68(%rbp)
pushq %r13
callq 0x386c
addq $0x20, %rsp
movl %eax, %r14d
testl %eax, %eax
je 0x4334
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 0x3446
movl %ebx, %eax
leaq 0x3cad(%rip), %rcx      # 0x8048
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
movl %r15d, %esi
movq %r13, %rbx
movq %r14, %r8
jmpq *%rax
testq %r8, %r8
je 0x4691
testq %rbx, %rbx
je 0x4691
testq %r12, %r12
je 0x4691
cmpq $0x0, -0x38(%rbp)
jne 0x4691
movl $0x1, %r14d
movq %r8, %rdi
movl $0x1, %edx
movl %esi, %r15d
callq 0x2d17
movq -0x30(%rbp), %r13
movl %eax, 0x28(%r13)
movq %rbx, %rdi
movl %r15d, %esi
movl $0x1, %edx
callq 0x2d17
movl %eax, 0x54(%r13)
movq %r12, %rdi
movl %r15d, %esi
movl $0x1, %edx
callq 0x2d17
movl %eax, 0x80(%r13)
jmp 0x4322
testq %r8, %r8
je 0x4691
testq %rbx, %rbx
je 0x4691
testq %r12, %r12
je 0x4691
cmpq $0x0, -0x38(%rbp)
jne 0x4691
movl $0x1, %r14d
movq %r8, %rdi
movl $0x1, %edx
movl %esi, %r15d
callq 0x2d17
movq -0x30(%rbp), %r13
movl %eax, 0x28(%r13)
movq %rbx, %rdi
movl %r15d, %esi
movl $0x1, %edx
callq 0x2d17
movl %eax, 0x54(%r13)
movq %r12, %rdi
movl $0xc, %esi
movl %r15d, %edx
movl $0x1, %ecx
callq 0x3173
movl %eax, %ecx
shll $0x14, %ecx
sarl $0x1f, %ecx
andl $0xfffff000, %ecx       # imm = 0xFFFFF000
orl %eax, %ecx
movl %ecx, 0x84(%r13)
jmp 0x4322
testq %r8, %r8
je 0x4691
testq %rbx, %rbx
je 0x4691
testq %r12, %r12
jne 0x4691
cmpq $0x0, -0x38(%rbp)
jne 0x4691
movl $0x1, %r14d
movq %r8, %rdi
movl $0x1, %edx
movl %esi, %r15d
callq 0x2d17
movq -0x30(%rbp), %rdx
movl %eax, 0x28(%rdx)
leaq 0x54(%rdx), %rsi
addq $0x84, %rdx
movq %rbx, %rdi
movl $0xc, %ecx
movl %r15d, %r8d
callq 0x3207
jmp 0x4322
testq %r8, %r8
je 0x4691
testq %rbx, %rbx
je 0x4691
testq %r12, %r12
je 0x4691
cmpq $0x0, -0x38(%rbp)
jne 0x4691
movl $0x1, %r14d
movq %r8, %rdi
movl $0x1, %edx
movl %esi, %r15d
callq 0x2d17
movq -0x30(%rbp), %r13
movl %eax, 0x28(%r13)
movq %rbx, %rdi
movl %r15d, %esi
movl $0x1, %edx
callq 0x2d17
movl %eax, 0x54(%r13)
movl $0x3, 0x5c(%r13)
addq $0x60, %r13
movl $0x20, %edx
movq %r13, %rdi
movq %r12, %rsi
callq 0x2100
jmp 0x4322
testq %r8, %r8
je 0x4691
testq %rbx, %rbx
je 0x4691
testq %r12, %r12
jne 0x4691
cmpq $0x0, -0x38(%rbp)
jne 0x4691
movl $0x1, %r14d
movq %r8, %rdi
movl $0x1, %edx
movl %esi, %r15d
callq 0x2d17
movq -0x30(%rbp), %r12
movl %eax, 0x28(%r12)
movq %rbx, %rdi
movl $0x14, %esi
movl %r15d, %edx
movl $0x1, %ecx
callq 0x3173
movl %eax, 0x58(%r12)
jmp 0x4322
movl $0x1, %r14d
jmp 0x4322
testq %r8, %r8
sete %cl
orq -0x38(%rbp), %r12
setne %al
orb %cl, %al
testq %rbx, %rbx
je 0x4650
testb %al, %al
jne 0x4691
movq -0x30(%rbp), %r15
movl $0x1, 0x4(%r15)
movl $0x1, %r14d
movq %r8, %rdi
movl $0x1, %edx
callq 0x2d17
movl %eax, 0x28(%r15)
movl $0x3, 0x30(%r15)
addq $0x34, %r15
movl $0x20, %edx
movq %r15, %rdi
movq %rbx, %rsi
jmp 0x457c
testb %al, %al
jne 0x4691
movl $0x1, %r14d
movq -0x30(%rbp), %rdi
movl %r14d, 0x4(%rdi)
movl %r14d, 0x28(%rdi)
movl $0x3, 0x30(%rdi)
addq $0x34, %rdi
movl $0x20, %edx
movq %r8, %rsi
jmp 0x457c
leaq 0x718b(%rip), %rdi      # 0xb810
movl -0x3c(%rbp), %esi
xorl %eax, %eax
callq 0x2040
jmp 0x46a6
leaq 0x6ea8(%rip), %rdi      # 0xb540
leaq 0x7100(%rip), %rdx      # 0xb79f
xorl %eax, %eax
callq 0x2040
movl $0x1, %edi
callq 0x2210