// File: rossy[P]crossc/SPIRV-Cross/spirv_glsl.cpp
// Function: spirv_cross::CompilerGLSL::emit_function(spirv_cross::SPIRFunction&, spirv_cross::Bitset const&)
void CompilerGLSL::emit_function(SPIRFunction &func, const Bitset &return_flags)
{
	// Avoid potential cycles.
	if (func.active)
		return;
	func.active = true;

	// If we depend on a function, emit that function before we emit our own function.
	for (auto block : func.blocks)
	{
		auto &b = get<SPIRBlock>(block);
		for (auto &i : b.ops)
		{
			auto ops = stream(i);
			auto op = static_cast<Op>(i.op);

			if (op == OpFunctionCall)
			{
				// Recursively emit functions which are called.
				uint32_t id = ops[2];
				emit_function(get<SPIRFunction>(id), ir.meta[ops[1]].decoration.decoration_flags);
			}
		}
	}

	emit_function_prototype(func, return_flags);
	begin_scope();

	if (func.self == ir.default_entry_point)
		emit_entry_point_declarations();

	current_function = &func;
	auto &entry_block = get<SPIRBlock>(func.entry_block);

	for (auto &v : func.local_variables)
	{
		auto &var = get<SPIRVariable>(v);
		if (var.storage == StorageClassWorkgroup)
		{
			// Special variable type which cannot have initializer,
			// need to be declared as standalone variables.
			// Comes from MSL which can push global variables as local variables in main function.
			add_local_variable_name(var.self);
			statement(variable_decl(var), ";");
			var.deferred_declaration = false;
		}
		else if (var.storage == StorageClassPrivate)
		{
			// These variables will not have had their CFG usage analyzed, so move it to the entry block.
			// Comes from MSL which can push global variables as local variables in main function.
			// We could just declare them right now, but we would miss out on an important initialization case which is
			// LUT declaration in MSL.
			// If we don't declare the variable when it is assigned we're forced to go through a helper function
			// which copies elements one by one.
			add_local_variable_name(var.self);
			auto &dominated = entry_block.dominated_variables;
			if (find(begin(dominated), end(dominated), var.self) == end(dominated))
				entry_block.dominated_variables.push_back(var.self);
			var.deferred_declaration = true;
		}
		else if (var.storage == StorageClassFunction && var.remapped_variable && var.static_expression)
		{
			// No need to declare this variable, it has a static expression.
			var.deferred_declaration = false;
		}
		else if (expression_is_lvalue(v))
		{
			add_local_variable_name(var.self);

			if (var.initializer)
				statement(variable_decl_function_local(var), ";");
			else
			{
				// Don't declare variable until first use to declutter the GLSL output quite a lot.
				// If we don't touch the variable before first branch,
				// declare it then since we need variable declaration to be in top scope.
				var.deferred_declaration = true;
			}
		}
		else
		{
			// HACK: SPIR-V in older glslang output likes to use samplers and images as local variables, but GLSL does not allow this.
			// For these types (non-lvalue), we enforce forwarding through a shadowed variable.
			// This means that when we OpStore to these variables, we just write in the expression ID directly.
			// This breaks any kind of branching, since the variable must be statically assigned.
			// Branching on samplers and images would be pretty much impossible to fake in GLSL.
			var.statically_assigned = true;
		}

		var.loop_variable_enable = false;

		// Loop variables are never declared outside their for-loop, so block any implicit declaration.
		if (var.loop_variable)
			var.deferred_declaration = false;
	}

	for (auto &line : current_function->fixup_hooks_in)
		line();

	entry_block.loop_dominator = SPIRBlock::NoDominator;
	emit_block_chain(entry_block);

	end_scope();
	processing_entry_point = false;
	statement("");
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x160, %rsp            # imm = 0x160
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x140(%rbp)
movq -0x10(%rbp), %rax
testb $0x1, 0xc8(%rax)
je 0x59bd4
jmp 0x5a182
movq -0x10(%rbp), %rax
movb $0x1, 0xc8(%rax)
movq -0x10(%rbp), %rax
addq $0x68, %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0x86d0
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rdi
callq 0x8700
movq %rax, -0x30(%rbp)
leaq -0x28(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x8730
testb $0x1, %al
jne 0x59c1b
jmp 0x59d20
leaq -0x28(%rbp), %rdi
callq 0x8770
movq -0x140(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %esi
callq 0x87d0
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
addq $0x38, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
callq 0xa4cc0
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rdi
callq 0xa4cf0
movq %rax, -0x58(%rbp)
leaq -0x50(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0xa4d20
testb $0x1, %al
jne 0x59c78
jmp 0x59d10
leaq -0x50(%rbp), %rdi
callq 0xa4d60
movq -0x140(%rbp), %rdi
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rsi
callq 0x2e560
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rax
movzwl (%rax), %eax
movl %eax, -0x6c(%rbp)
cmpl $0x39, -0x6c(%rbp)
jne 0x59d00
movq -0x140(%rbp), %rdi
movq -0x68(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x70(%rbp)
movl -0x70(%rbp), %esi
callq 0x106d0
movq -0x140(%rbp), %rdi
movq %rax, -0x148(%rbp)
addq $0x8, %rdi
addq $0x30, %rdi
movq -0x68(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, %esi
callq 0xead0
movq -0x140(%rbp), %rdi
movq -0x148(%rbp), %rsi
movq %rax, %rdx
addq $0x60, %rdx
callq 0x59ba0
jmp 0x59d02
leaq -0x50(%rbp), %rdi
callq 0xa4d80
jmp 0x59c62
jmp 0x59d12
leaq -0x28(%rbp), %rdi
callq 0x8790
jmp 0x59c05
movq -0x140(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq (%rdi), %rax
callq *0x40(%rax)
movq -0x140(%rbp), %rdi
callq 0x5c200
movq -0x140(%rbp), %rcx
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
cmpl 0x108(%rcx), %eax
jne 0x59d67
movq -0x140(%rbp), %rdi
movq (%rdi), %rax
callq *0x178(%rax)
movq -0x140(%rbp), %rdi
movq -0x10(%rbp), %rax
movq %rax, 0x148(%rdi)
movq -0x10(%rbp), %rax
movl 0x60(%rax), %esi
callq 0x87d0
movq %rax, -0x78(%rbp)
movq -0x10(%rbp), %rax
addq $0x48, %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rdi
callq 0x86d0
movq %rax, -0x88(%rbp)
movq -0x80(%rbp), %rdi
callq 0x8700
movq %rax, -0x90(%rbp)
leaq -0x88(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x8730
testb $0x1, %al
jne 0x59dd1
jmp 0x5a0b7
leaq -0x88(%rbp), %rdi
callq 0x8770
movq -0x140(%rbp), %rdi
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movl (%rax), %esi
callq 0xfec0
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
cmpl $0x4, 0x10(%rax)
jne 0x59ea3
movq -0x140(%rbp), %rdi
movq -0xa0(%rbp), %rax
movl 0x8(%rax), %esi
callq 0x78460
movq -0x140(%rbp), %rsi
movq -0xa0(%rbp), %rdx
leaq -0xc0(%rbp), %rdi
movq %rdi, -0x150(%rbp)
callq 0x62d60
movq -0x140(%rbp), %rdi
movq -0x150(%rbp), %rsi
leaq 0xef249(%rip), %rdx     # 0x1490a6
callq 0xf270
jmp 0x59e64
leaq -0xc0(%rbp), %rdi
callq 0x7708
movq -0xa0(%rbp), %rax
movb $0x0, 0x59(%rax)
jmp 0x5a081
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x7708
jmp 0x5a18b
movq -0xa0(%rbp), %rax
cmpl $0x6, 0x10(%rax)
jne 0x59f7d
movq -0x140(%rbp), %rdi
movq -0xa0(%rbp), %rax
movl 0x8(%rax), %esi
callq 0x78460
movq -0x78(%rbp), %rax
addq $0xb8, %rax
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rdi
callq 0x85d0
movq %rax, -0xe8(%rbp)
movq -0xd8(%rbp), %rdi
callq 0x8600
movq %rax, -0xf0(%rbp)
movq -0xa0(%rbp), %rdx
addq $0x8, %rdx
movq -0xe8(%rbp), %rdi
movq -0xf0(%rbp), %rsi
callq 0x8e40
movq %rax, -0xe0(%rbp)
movq -0xd8(%rbp), %rdi
callq 0x8600
movq %rax, -0xf8(%rbp)
leaq -0xe0(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x8e90
testb $0x1, %al
jne 0x59f52
jmp 0x59f6d
movq -0x78(%rbp), %rdi
addq $0xb8, %rdi
movq -0xa0(%rbp), %rsi
addq $0x8, %rsi
callq 0x88f0
movq -0xa0(%rbp), %rax
movb $0x1, 0x59(%rax)
jmp 0x5a07f
movq -0xa0(%rbp), %rax
cmpl $0x7, 0x10(%rax)
jne 0x59fb4
movq -0xa0(%rbp), %rax
testb $0x1, 0x5b(%rax)
je 0x59fb4
movq -0xa0(%rbp), %rax
cmpl $0x0, 0x3c(%rax)
je 0x59fb4
movq -0xa0(%rbp), %rax
movb $0x0, 0x59(%rax)
jmp 0x5a07d
movq -0x140(%rbp), %rdi
movq -0x98(%rbp), %rax
movl (%rax), %esi
callq 0x20410
testb $0x1, %al
jne 0x59fd2
jmp 0x5a070
movq -0x140(%rbp), %rdi
movq -0xa0(%rbp), %rax
movl 0x8(%rax), %esi
callq 0x78460
movq -0xa0(%rbp), %rax
cmpl $0x0, 0x18(%rax)
je 0x5a063
movq -0x140(%rbp), %rsi
movq -0xa0(%rbp), %rdx
leaq -0x118(%rbp), %rdi
movq %rdi, -0x158(%rbp)
callq 0x87650
movq -0x140(%rbp), %rdi
movq -0x158(%rbp), %rsi
leaq 0xef07b(%rip), %rdx     # 0x1490a6
callq 0xf270
jmp 0x5a032
leaq -0x118(%rbp), %rdi
callq 0x7708
jmp 0x5a06e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
leaq -0x118(%rbp), %rdi
callq 0x7708
jmp 0x5a18b
movq -0xa0(%rbp), %rax
movb $0x1, 0x59(%rax)
jmp 0x5a07b
movq -0xa0(%rbp), %rax
movb $0x1, 0x39(%rax)
jmp 0x5a07d
jmp 0x5a07f
jmp 0x5a081
movq -0xa0(%rbp), %rax
movb $0x0, 0x65(%rax)
movq -0xa0(%rbp), %rax
testb $0x1, 0x64(%rax)
je 0x5a0a4
movq -0xa0(%rbp), %rax
movb $0x0, 0x59(%rax)
jmp 0x5a0a6
leaq -0x88(%rbp), %rdi
callq 0x8790
jmp 0x59db5
movq -0x140(%rbp), %rax
movq 0x148(%rax), %rax
addq $0xb0, %rax
movq %rax, -0x120(%rbp)
movq -0x120(%rbp), %rdi
callq 0xa7e40
movq %rax, -0x128(%rbp)
movq -0x120(%rbp), %rdi
callq 0xa7e70
movq %rax, -0x130(%rbp)
leaq -0x128(%rbp), %rdi
leaq -0x130(%rbp), %rsi
callq 0xa7ea0
testb $0x1, %al
jne 0x5a111
jmp 0x5a13e
leaq -0x128(%rbp), %rdi
callq 0xa7ee0
movq %rax, -0x138(%rbp)
movq -0x138(%rbp), %rdi
callq 0xa7f00
leaq -0x128(%rbp), %rdi
callq 0xa7f40
jmp 0x5a0f8
movq -0x140(%rbp), %rdi
movq -0x78(%rbp), %rax
movl $0xffffffff, 0xb4(%rax) # imm = 0xFFFFFFFF
movq -0x78(%rbp), %rsi
callq 0x982d0
movq -0x140(%rbp), %rdi
callq 0x876c0
movq -0x140(%rbp), %rdi
movb $0x0, 0x5d8(%rdi)
leaq 0xe4721(%rip), %rsi     # 0x13e89e
callq 0xedf0
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
movq -0xc8(%rbp), %rdi
callq 0x7640
nopw (%rax,%rax)