// File: /workspace/github_cpp_makefile_star2_O2/sangwoojun[P]rv32emulator/emulator.cpp
// Function: parse(_IO_FILE*, unsigned char*, instr*, int&, label_loc*, int&, source*)
void parse(FILE* fin, uint8_t* mem, instr* imem, int& memoff, label_loc* labels, int& label_count, source* src) {
	int line = 0;

	printf( "Parsing input file\n" );

	//sectionType cur_section = SECTION_NONE;

	char rbuf[1024];
	while(!feof(fin)) {
		if ( !fgets(rbuf, 1024, fin) ) break;
		for (char* p = rbuf; *p; ++p) *p = tolower(*p);
		line++;

		char* ftok = strtok(rbuf, " \t\r\n");
		if ( !ftok ) continue;

		if ( ftok[0] == '#' ) continue;

		if ( ftok[strlen(ftok)-1] == ':' ) {
			ftok[strlen(ftok)-1] = 0;
			if ( strlen(ftok) >= MAX_LABEL_LEN ) {
				printf( "Exceeded maximum length of label: %s\n", ftok );
				exit(3);
			}
			if ( label_count >= MAX_LABEL_COUNT ) {
				printf( "Exceeded maximum number of supported labels" );
				exit(3);
			}
			strncpy(labels[label_count].label, ftok, MAX_LABEL_LEN);
			labels[label_count].loc = memoff;
			label_count++;
			//printf( "Parsing label %s at mem %x\n", ftok, memoff );

			char* ntok = strtok(NULL, " \t\r\n");
			// there is more code after label
			if ( ntok ) {
				if ( ntok[0] == '.' ) {
					memoff = parse_assembler_directive(line, ntok, mem, memoff);
				} else {
					int count = parse_instr(line, ntok, imem, memoff, labels, src);
					for ( int i = 0; i < count; i++ ) *(uint32_t*)&mem[memoff+(i*4)] = 0xcccccccc;
					memoff += count*4;
				}
			}
		} else if ( ftok[0] == '.' ) {
			memoff = parse_assembler_directive(line, ftok, mem, memoff);
		} else {
			int count = parse_instr(line, ftok, imem, memoff, labels, src);
			for ( int i = 0; i < count; i++ ) *(uint32_t*)&mem[memoff+(i*4)] = 0xcccccccc;
			memoff += count*4;
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x428, %rsp            # imm = 0x428
movq %r9, -0x50(%rbp)
movq %r8, -0x48(%rbp)
movq %rcx, %r15
movq %rdx, -0x38(%rbp)
movq %rsi, %r13
movq %rdi, %r14
leaq 0x6672(%rip), %rdi      # 0xaa45
callq 0x2300
movq $0x0, -0x30(%rbp)
movq %r14, -0x40(%rbp)
movq %r14, %rdi
callq 0x2310
testl %eax, %eax
leaq -0x450(%rbp), %r12
jne 0x4576
movq %r12, %rdi
movl $0x400, %esi            # imm = 0x400
movq %r14, %rdx
callq 0x2340
testq %rax, %rax
je 0x4576
movq %r12, %rbx
movsbl (%rbx), %edi
testl %edi, %edi
je 0x442a
callq 0x22e0
movb %al, (%rbx)
incq %rbx
jmp 0x4417
movq -0x30(%rbp), %rax
incl %eax
movq %rax, -0x30(%rbp)
movq %r12, %rdi
leaq 0x62c4(%rip), %rsi      # 0xa702
callq 0x2330
testq %rax, %rax
je 0x43e4
movq %rax, %rbx
movb (%rax), %r12b
cmpb $0x23, %r12b
je 0x43e4
movq %rbx, %rdi
callq 0x20f0
cmpb $0x3a, -0x1(%rbx,%rax)
jne 0x44e8
movb $0x0, -0x1(%rbx,%rax)
movq %rbx, %rdi
callq 0x20f0
cmpq $0x20, %rax
jae 0x4598
movq -0x50(%rbp), %r12
movslq (%r12), %rax
cmpq $0x80, %rax
jge 0x4588
imulq $0x24, %rax, %rdi
movq -0x48(%rbp), %r14
addq %r14, %rdi
movq %rbx, %rsi
pushq $0x20
popq %rdx
callq 0x2110
movl (%r15), %eax
movslq (%r12), %rcx
imulq $0x24, %rcx, %rcx
movl %eax, 0x20(%r14,%rcx)
incl (%r12)
xorl %edi, %edi
leaq 0x623d(%rip), %rsi      # 0xa702
callq 0x2330
testq %rax, %rax
movq -0x40(%rbp), %r14
je 0x43e4
cmpb $0x2e, (%rax)
movl (%r15), %ecx
jne 0x4535
movq -0x30(%rbp), %rdi
movq %rax, %rsi
jmp 0x44f8
movl (%r15), %ecx
cmpb $0x2e, %r12b
jne 0x4502
movq -0x30(%rbp), %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x34dd
jmp 0x456e
movq -0x30(%rbp), %rdi
movq %rbx, %rsi
movq -0x38(%rbp), %rdx
movq 0x10(%rbp), %r9
callq 0x3fa2
xorl %ecx, %ecx
movl %eax, %edx
subl $0x1, %edx
jb 0x4568
movl (%r15), %esi
addl %ecx, %esi
movslq %esi, %rsi
movl $0xcccccccc, (%r13,%rsi) # imm = 0xCCCCCCCC
addl $0x4, %ecx
jmp 0x451a
movq -0x30(%rbp), %rdi
movq %rax, %rsi
movq -0x38(%rbp), %rdx
movq 0x10(%rbp), %r9
callq 0x3fa2
xorl %ecx, %ecx
movl %eax, %edx
subl $0x1, %edx
jb 0x4568
movl (%r15), %esi
addl %ecx, %esi
movslq %esi, %rsi
movl $0xcccccccc, (%r13,%rsi) # imm = 0xCCCCCCCC
addl $0x4, %ecx
jmp 0x454d
shll $0x2, %eax
addl (%r15), %eax
movl %eax, (%r15)
jmp 0x43e4
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x62d4(%rip), %rdi      # 0xa863
xorl %eax, %eax
callq 0x2040
jmp 0x45a9
leaq 0x629e(%rip), %rdi      # 0xa83d
movq %rbx, %rsi
xorl %eax, %eax
callq 0x2040
pushq $0x3
popq %rdi
callq 0x2200