// File: /workspace/github_c_makefile_star2_O3/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 0x338b
leaq 0x5416(%rip), %rdx      # 0x924a
movq %r14, %rdi
movl $0x7, %esi
callq 0x3c36
movq 0x8(%r14), %rax
cmpl $0xa, 0x40(%rax)
jne 0x3e97
leaq 0x5453(%rip), %rdx      # 0x92a5
movq %rsp, %rdi
movl $0xa, %esi
callq 0x3c36
cmpl $0x4, %ebp
je 0x3f40
cmpl $0x2, %ebp
jne 0x3efd
movq 0x8(%rbx), %rax
cmpl $0x2, 0x40(%rax)
jne 0x3f40
movq %rbx, %rdi
callq 0x34a2
movq %rsp, %r14
movq %r14, %rdi
callq 0x3d56
jmp 0x3f22
leaq 0x53cc(%rip), %r14      # 0x926a
movq %rsp, %r15
leaq 0x53d9(%rip), %r12      # 0x9281
movq %r15, %rdi
movq %r14, %rsi
callq 0x412a
movzwl %ax, %esi
movq %r15, %rdi
callq 0x4234
movq 0x28(%rsp), %rax
movb 0x20(%rax), %cl
incb %cl
movb %cl, 0x20(%rax)
cmpb $0x9, %cl
jl 0x3ee4
movq 0x8(%rsp), %rsi
addq $0x20, %rsi
movq %r15, %rdi
movq %r12, %rdx
callq 0x3a3b
movq 0x8(%rsp), %rax
cmpl $0x3, 0x40(%rax)
jne 0x3e4b
movq %r15, %rdi
callq 0x34a2
jmp 0x3ea8
movq 0x8(%rbx), %rax
cmpl $0x2, 0x40(%rax)
jne 0x3f40
movq %rbx, %rdi
callq 0x34a2
movq %rsp, %r14
movq %r14, %rdi
callq 0x3d56
movq %r14, %rdi
callq 0x4287
movq 0x8(%r14), %rax
movq 0x28(%r14), %rdi
movl 0x54(%rax), %edx
movl 0x58(%rax), %ecx
addq $0x28, %rdi
movl $0x30, %esi
callq 0x2a5f
jmp 0x3f6b
leaq 0x5384(%rip), %rdx      # 0x92cb
movq %rsp, %r14
movq %r14, %rdi
movl $0x5, %esi
callq 0x3c36
incb 0x58(%r14)
movq %r14, %rdi
callq 0x42b4
movq %r14, %rdi
callq 0x42e8
movq 0x28(%rsp), %r14
movslq 0x40(%r14), %rax
testq %rax, %rax
je 0x3f84
movq 0x38(%r14), %rcx
cmpb $0x30, -0x1(%rcx,%rax)
je 0x3f93
movq %rsp, %r14
movq %r14, %rdi
callq 0x4b68
movq 0x28(%r14), %r14
movq 0x20(%rbx), %rax
movq %rax, 0x28(%r14)
movq 0x78(%r14), %rax
leaq 0x28(%rax), %rcx
testq %rax, %rax
leaq 0x533f(%rip), %rax      # 0x92ec
cmovneq %rcx, %rax
movq %rax, 0x30(%r14)
movq 0x8(%rbx), %rax
movq 0x28(%rbx), %rdi
movl 0x54(%rax), %edx
movl 0x58(%rax), %ecx
addq $0x28, %rdi
movl $0x28, %esi
callq 0x2a5f
movq %rbx, %rdi
movl $0x3, %esi
movq %r14, %rdx
callq 0x3c03
movzwl %ax, %esi
movq %rbx, %rdi
callq 0x3bbf
cmpw $0x0, 0x22(%r14)
je 0x4038
xorl %r15d, %r15d
movq 0x48(%rsp), %rax
movzbl 0x2(%rax,%r15,4), %esi
movq 0x8(%rbx), %rax
movq 0x28(%rbx), %rdi
movl 0x54(%rax), %edx
movl 0x58(%rax), %ecx
addq $0x28, %rdi
callq 0x2a5f
movq 0x48(%rsp), %rax
movzwl (%rax,%r15,4), %esi
movq %rbx, %rdi
callq 0x3bbf
incq %r15
movzwl 0x22(%r14), %eax
cmpq %rax, %r15
jb 0x3ff7
movq %rsp, %rdi
callq 0x39fb
addq $0x60, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq