// File: /workspace/github_cpp_makefile_star2_O2/AndrewAlbert[P]Compiler-Theory/SourceCode/parser.cpp
// Function: Parser::ProcedureBody()
bool Parser::ProcedureBody(){
	bool resyncEnabled = true;
	bool procDec = false;
	//get symbol declarations for next procedure
	while(true){
		while( Declaration(procDec) ){
			if(procDec){
				if( !CheckToken(T_SEMICOLON) ) ReportWarning("expected ';' after procedure declaration in procedure");
			}
			else if( !CheckToken(T_SEMICOLON) ) ReportLineError("expected ';' after variable declaration in procedure", true);
		}
		
		// GEN: set the procedure's FP and SP in the program body after all declarations have been made
		// Each procedure declaration added its bytes to the frame size so this is the max size
		int frameSize = Scopes->getFrameSize();
		generator->setProcedurePointers(frameSize);
		
		//get statements for procedure body
		if( CheckToken(T_BEGIN) ){
			resyncEnabled = true;
			while(true){
				while( Statement() ){
					if( !CheckToken(T_SEMICOLON) ) ReportLineError("expected ';' after statement in procedure",true);
				}
				if( CheckToken(T_END) ){

					// Add label for procedure end and return
					string labelEnd;
					labelEnd = generator->newLabel("Procedure_End");
					generator->placeLabel( labelEnd );
					generator->ProcedureReturn();

					if( CheckToken(T_PROCEDURE) ) return true;
					else{
						ReportError("expected 'end procedure' at end of procedure declaration");
						return true;
					}
				}
				else if(resyncEnabled){
					resyncEnabled = false;
					ReportLineError("Bad line. Expected Statement or 'end' reserved keyword in procedure body.");
				}
				else{
					ReportFatalError("expected 'end procedure' at end of procedure declaration");
					return false;
				}
			}
		}
		else if(resyncEnabled){
			resyncEnabled = false;
			ReportLineError("Bad line. Expected Declaration or 'begin' reserved keyword in procedure body.");
		}
		else{
			ReportFatalError("Parser resync failed. Couldn't find a valid declaration or the 'begin reserved keyword in procedure body.");
			return false;
		}
		
	}

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x188, %rsp            # imm = 0x188
movq %rdi, %rbx
leaq 0x2(%rsp), %r14
movb $0x0, (%r14)
movb $0x1, %al
movl %eax, 0x4(%rsp)
leaq 0x168(%rsp), %r12
leaq 0x148(%rsp), %r15
leaq 0xe8(%rsp), %r13
movq %rbx, %rdi
movq %r14, %rsi
callq 0x534e
testb %al, %al
je 0x5bd4
movb 0x2(%rsp), %bpl
movq %rbx, %rdi
movl $0x12c, %esi            # imm = 0x12C
callq 0x49b0
cmpb $0x1, %bpl
jne 0x5ba1
testb %al, %al
jne 0x5b4d
movq %r12, %rdi
leaq 0xb8b9(%rip), %rsi      # 0x1143b
leaq 0x8(%rsp), %rdx
callq 0x36b8
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4020
movq %r12, %rdi
callq 0x20f0
jmp 0x5b4d
testb %al, %al
jne 0x5b4d
movq %r15, %rdi
leaq 0xb8c2(%rip), %rsi      # 0x11471
leaq 0x8(%rsp), %rdx
callq 0x36b8
movq %rbx, %rdi
movq %r15, %rsi
pushq $0x1
popq %rdx
callq 0x444c
movq %r15, %rdi
callq 0x20f0
jmp 0x5b4d
movq 0x90(%rbx), %rdi
callq 0xb4d6
movq 0x98(%rbx), %rdi
movl %eax, %esi
callq 0xeba0
movq %rbx, %rdi
movl $0x103, %esi            # imm = 0x103
callq 0x49b0
testb %al, %al
jne 0x5c41
testb $0x1, 0x4(%rsp)
je 0x5def
movq %r13, %rdi
leaq 0xbca1(%rip), %rsi      # 0x118b5
leaq 0x8(%rsp), %rdx
callq 0x36b8
movq %rbx, %rdi
movq %r13, %rsi
pushq $0x1
popq %rdx
callq 0x444c
movq %r13, %rdi
callq 0x20f0
movl $0x0, 0x4(%rsp)
jmp 0x5b4d
movb $0x1, %r13b
leaq 0xbbaf(%rip), %r14      # 0x117fa
leaq 0x128(%rsp), %r15
leaq 0x8(%rsp), %r12
pushq $0x1
popq %rbp
movq %rbx, %rdi
callq 0x557c
movq %rbx, %rdi
testb %al, %al
je 0x5c9d
movl $0x12c, %esi            # imm = 0x12C
callq 0x49b0
testb %al, %al
jne 0x5c5b
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x36b8
movq %rbx, %rdi
movq %r15, %rsi
movl %ebp, %edx
callq 0x444c
movq %r15, %rdi
callq 0x20f0
jmp 0x5c5b
movl $0x104, %esi            # imm = 0x104
callq 0x49b0
testb %al, %al
jne 0x5cec
testb $0x1, %r13b
je 0x5e18
leaq 0x108(%rsp), %r13
movq %r13, %rdi
leaq 0xbba4(%rip), %rsi      # 0x1186b
movq %r12, %rdx
callq 0x36b8
movq %rbx, %rdi
movq %r13, %rsi
movl %ebp, %edx
callq 0x444c
movq %r13, %rdi
callq 0x20f0
xorl %r13d, %r13d
jmp 0x5c5b
leaq 0x18(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq 0x98(%rbx), %r14
leaq 0xbb19(%rip), %rsi      # 0x11824
leaq 0x68(%rsp), %rdi
leaq 0x3(%rsp), %rdx
callq 0x36b8
leaq 0xc8(%rsp), %rdi
leaq 0x68(%rsp), %rdx
movq %r14, %rsi
callq 0xeaa4
leaq 0x8(%rsp), %r15
leaq 0xc8(%rsp), %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x2240
movq %r14, %rdi
callq 0x20f0
leaq 0x68(%rsp), %rdi
callq 0x20f0
movq 0x98(%rbx), %r14
leaq 0x48(%rsp), %rdi
movq %r15, %rsi
callq 0x2270
leaq 0x48(%rsp), %rsi
movq %r14, %rdi
callq 0xeb7e
leaq 0x48(%rsp), %rdi
callq 0x20f0
movq 0x98(%rbx), %rdi
callq 0xee32
movq %rbx, %rdi
movl $0x106, %esi            # imm = 0x106
callq 0x49b0
testb %al, %al
jne 0x5dd1
leaq 0xba8a(%rip), %rsi      # 0x11832
leaq 0x28(%rsp), %rdi
leaq 0xc8(%rsp), %rdx
callq 0x36b8
leaq 0x28(%rsp), %rsi
movq %rbx, %rdi
callq 0x47e0
leaq 0x28(%rsp), %rdi
callq 0x20f0
leaq 0x8(%rsp), %rdi
callq 0x20f0
movb $0x1, %al
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xbb0d(%rip), %rsi      # 0x11903
leaq 0x88(%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0x36b8
leaq 0x88(%rsp), %rsi
movq %rbx, %rdi
callq 0x4278
leaq 0xba13(%rip), %rsi      # 0x11832
leaq 0xa8(%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0x36b8
leaq 0xa8(%rsp), %rsi
movq %rbx, %rdi
callq 0x4278
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
jmp 0x5e5f
jmp 0x5e68
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
jmp 0x5e5f
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x20f0
jmp 0x5e6b
jmp 0x5e68
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
jmp 0x5ed7
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
jmp 0x5ed7
jmp 0x5ede
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
jmp 0x5ed7
jmp 0x5ede
jmp 0x5ede
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
jmp 0x5ed7
jmp 0x5ede
movq %rax, %rbx
leaq 0xe8(%rsp), %rdi
jmp 0x5ed7
movq %rax, %rbx
leaq 0x168(%rsp), %rdi
jmp 0x5ed7
jmp 0x5ede
jmp 0x5ede
movq %rax, %rbx
leaq 0x148(%rsp), %rdi
jmp 0x5ed7
movq %rax, %rbx
leaq 0x128(%rsp), %rdi
callq 0x20f0
jmp 0x5ee1
movq %rax, %rbx
movq %rbx, %rdi
callq 0x2310
nop