// File: /workspace/github_cpp_makefile_star2_O3/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, %r12
movq %rdi, %r13
leaq 0x7311(%rip), %rdi      # 0xba03
callq 0x22e0
movq %r13, %rdi
callq 0x2300
testl %eax, %eax
jne 0x48b4
movq $0x0, -0x30(%rbp)
leaq -0x450(%rbp), %r14
movq %r13, -0x40(%rbp)
movq %r14, %rdi
movl $0x400, %esi            # imm = 0x400
movq %r13, %rdx
callq 0x2330
testq %rax, %rax
je 0x48b4
movb -0x450(%rbp), %al
leaq -0x44f(%rbp), %rbx
testb %al, %al
je 0x4756
movsbl %al, %edi
callq 0x22c0
movb %al, -0x1(%rbx)
movb (%rbx), %al
incq %rbx
jmp 0x4740
movq -0x30(%rbp), %rax
incl %eax
movq %rax, -0x30(%rbp)
movq %r14, %rdi
leaq 0x6f32(%rip), %rsi      # 0xb69c
callq 0x2320
testq %rax, %rax
je 0x489d
movq %rax, %rbx
movb (%rax), %r14b
cmpb $0x23, %r14b
je 0x489d
movq %rbx, %rdi
callq 0x20f0
cmpb $0x3a, -0x1(%rbx,%rax)
jne 0x4822
movb $0x0, -0x1(%rbx,%rax)
movq %rbx, %rdi
callq 0x20f0
cmpq $0x20, %rax
jae 0x48d6
movq -0x50(%rbp), %r14
movslq (%r14), %rax
cmpq $0x80, %rax
jge 0x48c6
movq %r12, %r13
leaq (%rax,%rax,8), %rax
movq -0x48(%rbp), %r12
leaq (%r12,%rax,4), %rdi
movl $0x20, %edx
movq %rbx, %rsi
callq 0x2100
movl (%r15), %eax
movslq (%r14), %rcx
leaq (%rcx,%rcx,8), %rcx
movl %eax, 0x20(%r12,%rcx,4)
incl (%r14)
xorl %edi, %edi
leaq 0x6ea0(%rip), %rsi      # 0xb69c
callq 0x2320
testq %rax, %rax
movq %r13, %r12
movq -0x40(%rbp), %r13
je 0x489d
cmpb $0x2e, (%rax)
movl (%r15), %ecx
jne 0x4869
movq -0x30(%rbp), %rdi
movq %rax, %rsi
jmp 0x4832
movl (%r15), %ecx
cmpb $0x2e, %r14b
jne 0x483c
movq -0x30(%rbp), %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x36ea
jmp 0x489a
movq -0x30(%rbp), %rdi
movq %rbx, %rsi
movq -0x38(%rbp), %rdx
movq 0x10(%rbp), %r9
callq 0x4288
movl %eax, %ecx
movq %r12, %rdx
movslq (%r15), %rsi
movl $0xcccccccc, (%rdx,%rsi) # imm = 0xCCCCCCCC
addq $0x4, %rdx
decl %ecx
jne 0x4855
jmp 0x4894
movq -0x30(%rbp), %rdi
movq %rax, %rsi
movq -0x38(%rbp), %rdx
movq 0x10(%rbp), %r9
callq 0x4288
movl %eax, %ecx
movq %r12, %rdx
movslq (%r15), %rsi
movl $0xcccccccc, (%rdx,%rsi) # imm = 0xCCCCCCCC
addq $0x4, %rdx
decl %ecx
jne 0x4882
shll $0x2, %eax
addl (%r15), %eax
movl %eax, (%r15)
movq %r13, %rdi
callq 0x2300
testl %eax, %eax
leaq -0x450(%rbp), %r14
je 0x471a
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6f2a(%rip), %rdi      # 0xb7f7
xorl %eax, %eax
callq 0x2040
jmp 0x48e7
leaq 0x6ef4(%rip), %rdi      # 0xb7d1
movq %rbx, %rsi
xorl %eax, %eax
callq 0x2040
movl $0x3, %edi
callq 0x2210