// File: /workspace/github_c_makefile_star2_O1/adamjedlicka[P]blu/src/vm/compiler/compiler.c
// Function: function
static void function(bluCompiler* compiler, bluFunctionType type) {
	bluCompiler fnCompiler;
	initCompiler(&fnCompiler, compiler, compiler->scopeDepth + 1, type);

	// Compile the parameter list.
	consume(&fnCompiler, TOKEN_LEFT_PAREN, "Expect '(' after function name.");

	if (!check(&fnCompiler, TOKEN_RIGHT_PAREN)) {
		do {
			uint16_t parameter = parseVariable(&fnCompiler, "Expect parameter name.");
			defineVariable(&fnCompiler, parameter);

			fnCompiler.function->arity++;
			if (fnCompiler.function->arity > 8) {
				error(&fnCompiler, "Cannot have more than 8 parameters.");
			}
		} while (match(&fnCompiler, TOKEN_COMMA));
	}

	consume(&fnCompiler, TOKEN_RIGHT_PAREN, "Expect ')' after function parameters.");

	// The body
	if (type == TYPE_ANONYMOUS && match(compiler, TOKEN_COLON)) {
		expression(&fnCompiler);
		emitByte(&fnCompiler, OP_RETURN);
	} else if (type != TYPE_INITIALIZER && match(compiler, TOKEN_COLON)) {
		expression(&fnCompiler);
		expectNewlineOrSemicolon(&fnCompiler);
		emitByte(&fnCompiler, OP_RETURN);
	} else {
		consume(&fnCompiler, TOKEN_LEFT_BRACE, "Expect '{' before function body.");
		beginScope(&fnCompiler);
		block(&fnCompiler);
		endScope(&fnCompiler);
	}

	// Create the function object.
	bluObjFunction* function = endCompiler(&fnCompiler);
	function->chunk.file = compiler->file;
	function->chunk.name = function->name != NULL ? function->name->chars : "__anonymous";

#ifdef DEBUG_COMPILER_DISASSEMBLE
	bluDisassembleChunk(&function->chunk);
#endif

	// Capture the upvalues in the new closure object.
	emitByte(compiler, OP_CLOSURE);
	emitShort(compiler, makeConstant(compiler, OBJ_VAL(function)));

	// Emit arguments for each upvalue to know whether to capture a local or an upvalue.
	for (uint16_t i = 0; i < function->upvalueCount; i++) {
		emitByte(compiler, fnCompiler.upvalues.data[i].isLocal ? 1 : 0);
		emitShort(compiler, fnCompiler.upvalues.data[i].index);
	}

	freeCompiler(&fnCompiler);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x60, %rsp
movl %esi, %ebp
movq %rdi, %rbx
movb 0x58(%rdi), %al
incb %al
movsbl %al, %edx
movq %rsp, %r14
movq %r14, %rdi
movq %rbx, %rsi
movl %ebp, %ecx
callq 0x544f
leaq 0x4639(%rip), %rdx      # 0xa580
movq %r14, %rdi
movl $0x7, %esi
callq 0x5d2c
movq 0x8(%r14), %rax
cmpl $0xa, 0x40(%rax)
jne 0x5fa6
leaq 0x4676(%rip), %rdx      # 0xa5db
movq %rsp, %rdi
movl $0xa, %esi
callq 0x5d2c
cmpl $0x4, %ebp
je 0x6054
cmpl $0x2, %ebp
jne 0x6011
movq 0x8(%rbx), %rax
cmpl $0x2, 0x40(%rax)
jne 0x600c
movq %rbx, %rdi
callq 0x553e
movq %rsp, %r14
movq %r14, %rdi
callq 0x5e4c
jmp 0x6036
leaq 0x45f3(%rip), %r14      # 0xa5a0
movq %rsp, %r15
leaq 0x4600(%rip), %r12      # 0xa5b7
movq %r15, %rdi
movq %r14, %rsi
callq 0x6221
movzwl %ax, %esi
movq %r15, %rdi
callq 0x632b
movq 0x28(%rsp), %rax
movb 0x20(%rax), %cl
incb %cl
movb %cl, 0x20(%rax)
cmpb $0x9, %cl
jl 0x5ff3
movq 0x8(%rsp), %rsi
addq $0x20, %rsi
movq %r15, %rdi
movq %r12, %rdx
callq 0x5b1c
movq 0x8(%rsp), %rax
cmpl $0x3, 0x40(%rax)
jne 0x5f5e
movq %r15, %rdi
callq 0x553e
jmp 0x5fb7
cmpl $0x4, %ebp
je 0x6054
movq 0x8(%rbx), %rax
cmpl $0x2, 0x40(%rax)
jne 0x6054
movq %rbx, %rdi
callq 0x553e
movq %rsp, %r14
movq %r14, %rdi
callq 0x5e4c
movq %r14, %rdi
callq 0x637e
movq 0x8(%r14), %rax
movq 0x28(%r14), %rdi
addq $0x28, %rdi
movl 0x54(%rax), %edx
movl 0x58(%rax), %ecx
movl $0x30, %esi
callq 0x4b23
jmp 0x607f
leaq 0x45a6(%rip), %rdx      # 0xa601
movq %rsp, %r14
movq %r14, %rdi
movl $0x5, %esi
callq 0x5d2c
incb 0x58(%r14)
movq %r14, %rdi
callq 0x63ab
movq %r14, %rdi
callq 0x63df
movq %rsp, %rdi
callq 0x5ab2
movq %rax, %r14
movq 0x20(%rbx), %rax
movq %rax, 0x28(%r14)
movq 0x78(%r14), %rax
leaq 0x28(%rax), %rcx
testq %rax, %rax
leaq 0x457e(%rip), %rax      # 0xa622
cmovneq %rcx, %rax
movq %rax, 0x30(%r14)
movq 0x8(%rbx), %rax
movq 0x28(%rbx), %rdi
addq $0x28, %rdi
movl 0x54(%rax), %edx
movl 0x58(%rax), %ecx
movl $0x28, %esi
callq 0x4b23
movq %rbx, %rdi
movl $0x3, %esi
movq %r14, %rdx
callq 0x5cf9
movzwl %ax, %esi
movq %rbx, %rdi
callq 0x5cb5
cmpw $0x0, 0x22(%r14)
je 0x612f
xorl %r15d, %r15d
movq 0x48(%rsp), %rax
movzbl 0x2(%rax,%r15,4), %esi
movq 0x8(%rbx), %rax
movq 0x28(%rbx), %rdi
addq $0x28, %rdi
movl 0x54(%rax), %edx
movl 0x58(%rax), %ecx
callq 0x4b23
movq 0x48(%rsp), %rax
movzwl (%rax,%r15,4), %esi
movq %rbx, %rdi
callq 0x5cb5
incq %r15
movzwl 0x22(%r14), %eax
cmpq %rax, %r15
jb 0x60ee
movq %rsp, %rdi
callq 0x5adc
addq $0x60, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq