// File: /workspace/github_c_makefile_star2_O2/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 %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %esi, %ebp
movq %rdi, %rbx
movb 0x58(%rdi), %al
incb %al
movsbl %al, %edx
leaq 0x8(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movl %ebp, %ecx
callq 0x2f0e
leaq 0x5868(%rip), %rdx      # 0x91f6
pushq $0x7
popq %rsi
movq %r14, %rdi
callq 0x377a
movq 0x8(%r14), %rax
cmpl $0xa, 0x40(%rax)
je 0x39fc
leaq 0x586c(%rip), %r14      # 0x9216
leaq 0x8(%rsp), %r15
leaq 0x5877(%rip), %r12      # 0x922d
pushq $0x3
popq %r13
movq %r15, %rdi
movq %r14, %rsi
callq 0x3be1
movzwl %ax, %esi
movq %r15, %rdi
callq 0x3ca8
movq 0x30(%rsp), %rax
movb 0x20(%rax), %cl
incb %cl
movb %cl, 0x20(%rax)
cmpb $0x9, %cl
jl 0x39ed
movq %r15, %rdi
movq %r12, %rsi
callq 0x350f
movq %r15, %rdi
movl %r13d, %esi
callq 0x30a5
testb %al, %al
jne 0x39ba
leaq 0x584e(%rip), %rdx      # 0x9251
leaq 0x8(%rsp), %rdi
pushq $0xa
popq %rsi
callq 0x377a
cmpl $0x4, %ebp
je 0x3a69
cmpl $0x2, %ebp
jne 0x3a38
pushq $0x2
popq %rsi
movq %rbx, %rdi
callq 0x30a5
testb %al, %al
je 0x3a38
leaq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x38a1
jmp 0x3a5c
pushq $0x2
popq %rsi
movq %rbx, %rdi
callq 0x30a5
testb %al, %al
je 0x3a69
leaq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x38a1
movq %r14, %rdi
callq 0x3cea
pushq $0x30
popq %rsi
movq %r14, %rdi
callq 0x37c2
jmp 0x3a94
leaq 0x5807(%rip), %rdx      # 0x9277
leaq 0x8(%rsp), %r14
pushq $0x5
popq %rsi
movq %r14, %rdi
callq 0x377a
incb 0x58(%r14)
movq %r14, %rdi
callq 0x3d1f
movq %r14, %rdi
callq 0x3d51
leaq 0x8(%rsp), %rdi
callq 0x34cb
movq %rax, %r14
movq 0x20(%rbx), %rax
movq %rax, 0x28(%r14)
movq 0x78(%r14), %rax
leaq 0x28(%rax), %rcx
testq %rax, %rax
leaq 0x57dd(%rip), %rax      # 0x9298
cmovneq %rcx, %rax
movq %rax, 0x30(%r14)
pushq $0x28
popq %rsi
movq %rbx, %rdi
callq 0x37c2
pushq $0x3
popq %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x374f
movzwl %ax, %esi
movq %rbx, %rdi
callq 0x372f
xorl %r15d, %r15d
movzwl 0x22(%r14), %eax
cmpq %rax, %r15
jae 0x3b1e
movq 0x50(%rsp), %rax
movzbl 0x2(%rax,%r15,4), %esi
movq %rbx, %rdi
callq 0x37c2
movq 0x50(%rsp), %rax
movzwl (%rax,%r15,4), %esi
movq %rbx, %rdi
callq 0x372f
incq %r15
jmp 0x3aea
leaq 0x8(%rsp), %rdi
callq 0x34f5
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq