// File: /workspace/github_cpp_makefile_star2_O1/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 0x738f(%rip), %rdi      # 0xba6f
callq 0x22e0
movq %r13, %rdi
callq 0x2300
testl %eax, %eax
jne 0x48a2
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 0x48a2
movb -0x450(%rbp), %al
leaq -0x44f(%rbp), %rbx
testb %al, %al
je 0x4744
movsbl %al, %edi
callq 0x22c0
movb %al, -0x1(%rbx)
movb (%rbx), %al
incq %rbx
jmp 0x472e
movq -0x30(%rbp), %rax
incl %eax
movq %rax, -0x30(%rbp)
movq %r14, %rdi
leaq 0x6faa(%rip), %rsi      # 0xb702
callq 0x2320
testq %rax, %rax
je 0x488b
movq %rax, %rbx
movb (%rax), %r14b
cmpb $0x23, %r14b
je 0x488b
movq %rbx, %rdi
callq 0x20f0
cmpb $0x3a, -0x1(%rbx,%rax)
jne 0x4810
movb $0x0, -0x1(%rbx,%rax)
movq %rbx, %rdi
callq 0x20f0
cmpq $0x20, %rax
jae 0x48c4
movq -0x50(%rbp), %r14
movslq (%r14), %rax
cmpq $0x80, %rax
jge 0x48b4
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 0x6f18(%rip), %rsi      # 0xb702
callq 0x2320
testq %rax, %rax
movq %r13, %r12
movq -0x40(%rbp), %r13
je 0x488b
cmpb $0x2e, (%rax)
movl (%r15), %ecx
jne 0x4857
movq -0x30(%rbp), %rdi
movq %rax, %rsi
jmp 0x4820
movl (%r15), %ecx
cmpb $0x2e, %r14b
jne 0x482a
movq -0x30(%rbp), %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x36e1
jmp 0x4888
movq -0x30(%rbp), %rdi
movq %rbx, %rsi
movq -0x38(%rbp), %rdx
movq 0x10(%rbp), %r9
callq 0x4276
movl %eax, %ecx
movq %r12, %rdx
movslq (%r15), %rsi
movl $0xcccccccc, (%rdx,%rsi) # imm = 0xCCCCCCCC
addq $0x4, %rdx
decl %ecx
jne 0x4843
jmp 0x4882
movq -0x30(%rbp), %rdi
movq %rax, %rsi
movq -0x38(%rbp), %rdx
movq 0x10(%rbp), %r9
callq 0x4276
movl %eax, %ecx
movq %r12, %rdx
movslq (%r15), %rsi
movl $0xcccccccc, (%rdx,%rsi) # imm = 0xCCCCCCCC
addq $0x4, %rdx
decl %ecx
jne 0x4870
shll $0x2, %eax
addl (%r15), %eax
movl %eax, (%r15)
movq %r13, %rdi
callq 0x2300
testl %eax, %eax
leaq -0x450(%rbp), %r14
je 0x4708
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6fa8(%rip), %rdi      # 0xb863
xorl %eax, %eax
callq 0x2040
jmp 0x48d5
leaq 0x6f72(%rip), %rdi      # 0xb83d
movq %rbx, %rsi
xorl %eax, %eax
callq 0x2040
movl $0x3, %edi
callq 0x2210