// File: rossy[P]crossc/SPIRV-Cross/spirv_msl.cpp
// Function: spirv_cross::CompilerMSL::emit_specialization_constants()
void CompilerMSL::emit_specialization_constants()
{
	SpecializationConstant wg_x, wg_y, wg_z;
	uint32_t workgroup_size_id = get_work_group_size_specialization_constants(wg_x, wg_y, wg_z);
	bool emitted = false;

	for (auto &id : ir.ids)
	{
		if (id.get_type() == TypeConstant)
		{
			auto &c = id.get<SPIRConstant>();
			if (!c.specialization)
				continue;

			// If WorkGroupSize is a specialization constant, it will be declared explicitly below.
			if (c.self == workgroup_size_id)
				continue;

			auto &type = get<SPIRType>(c.constant_type);
			string sc_type_name = type_to_glsl(type);
			string sc_name = to_name(c.self);
			string sc_tmp_name = sc_name + "_tmp";

			// Function constants are only supported in MSL 1.2 and later.
			// If we don't support it just declare the "default" directly.
			// This "default" value can be overridden to the true specialization constant by the API user.
			if (msl_options.supports_msl_version(1, 2) && has_decoration(c.self, DecorationSpecId))
			{
				uint32_t constant_id = get_decoration(c.self, DecorationSpecId);
				// Only scalar, non-composite values can be function constants.
				statement("constant ", sc_type_name, " ", sc_tmp_name, " [[function_constant(", constant_id, ")]];");
				statement("constant ", sc_type_name, " ", sc_name, " = is_function_constant_defined(", sc_tmp_name,
				          ") ? ", sc_tmp_name, " : ", constant_expression(c), ";");
			}
			else
			{
				// Composite specialization constants must be built from other specialization constants.
				statement("constant ", sc_type_name, " ", sc_name, " = ", constant_expression(c), ";");
			}
			emitted = true;
		}
		else if (id.get_type() == TypeConstantOp)
		{
			auto &c = id.get<SPIRConstantOp>();
			auto &type = get<SPIRType>(c.basetype);
			auto name = to_name(c.self);
			statement("constant ", variable_decl(type, name), " = ", constant_op_expression(c), ";");
			emitted = true;
		}
	}

	// TODO: This can be expressed as a [[threads_per_threadgroup]] input semantic, but we need to know
	// the work group size at compile time in SPIR-V, and [[threads_per_threadgroup]] would need to be passed around as a global.
	// The work group size may be a specialization constant.
	if (workgroup_size_id)
	{
		statement("constant uint3 ", builtin_to_glsl(BuiltInWorkgroupSize, StorageClassWorkgroup), " = ",
		          constant_expression(get<SPIRConstant>(workgroup_size_id)), ";");
		emitted = true;
	}

	if (emitted)
		statement("");
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x210, %rsp            # imm = 0x210
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x1c8(%rbp)
leaq -0x10(%rbp), %rsi
leaq -0x18(%rbp), %rdx
leaq -0x20(%rbp), %rcx
callq 0x251e0
movl %eax, %ecx
movq -0x1c8(%rbp), %rax
movl %ecx, -0x24(%rbp)
movb $0x0, -0x25(%rbp)
addq $0x8, %rax
addq $0x18, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0xf940
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rdi
callq 0xf970
movq %rax, -0x40(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0xf9a0
testb $0x1, %al
jne 0xf6bf7
jmp 0xf70f1
leaq -0x38(%rbp), %rdi
callq 0xf9e0
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
callq 0xfa00
cmpl $0x3, %eax
jne 0xf6f6c
movq -0x48(%rbp), %rdi
callq 0x2f2f0
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
testb $0x1, 0x108(%rax)
jne 0xf6c35
jmp 0xf70e3
movq -0x50(%rbp), %rax
movl 0x8(%rax), %eax
cmpl -0x24(%rbp), %eax
jne 0xf6c46
jmp 0xf70e3
movq -0x1c8(%rbp), %rdi
movq -0x50(%rbp), %rax
movl 0xc(%rax), %esi
callq 0xea90
movq -0x1c8(%rbp), %rsi
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rdx
movq (%rsi), %rax
movq 0x98(%rax), %rax
leaq -0x78(%rbp), %rdi
xorl %ecx, %ecx
callq *%rax
movq -0x1c8(%rbp), %rsi
movq -0x50(%rbp), %rax
movl 0x8(%rax), %edx
movq (%rsi), %rax
movq 0x38(%rax), %rax
leaq -0x98(%rbp), %rdi
movl $0x1, %ecx
callq *%rax
jmp 0xf6c9f
leaq 0x55c54(%rip), %rdx     # 0x14c8fa
leaq -0xc8(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x2f8c0
jmp 0xf6cbb
movq -0x1c8(%rbp), %rdi
addq $0x7cc, %rdi            # imm = 0x7CC
movl $0x1, %esi
movl $0x2, %edx
xorl %ecx, %ecx
callq 0x1160d0
movb %al, -0x1c9(%rbp)
jmp 0xf6ce2
movb -0x1c9(%rbp), %al
testb $0x1, %al
jne 0xf6cf1
jmp 0xf6e8f
movq -0x1c8(%rbp), %rdi
movq -0x50(%rbp), %rax
movl 0x8(%rax), %esi
movl $0x1, %edx
callq 0x1f560
movb %al, -0x1ca(%rbp)
jmp 0xf6d11
movb -0x1ca(%rbp), %al
testb $0x1, %al
jne 0xf6d20
jmp 0xf6e8f
movq -0x1c8(%rbp), %rdi
movq -0x50(%rbp), %rax
movl 0x8(%rax), %esi
movl $0x1, %edx
callq 0x23520
movl %eax, -0x1d0(%rbp)
jmp 0xf6d40
movq -0x1c8(%rbp), %rdi
movl -0x1d0(%rbp), %eax
movl %eax, -0xcc(%rbp)
leaq 0x55bbb(%rip), %rcx     # 0x14c915
movq %rsp, %rax
movq %rcx, 0x8(%rax)
leaq -0xcc(%rbp), %rcx
movq %rcx, (%rax)
leaq 0x55b77(%rip), %rsi     # 0x14c8e9
leaq 0x47b24(%rip), %rcx     # 0x13e89d
leaq 0x55b7f(%rip), %r9      # 0x14c8ff
leaq -0x78(%rbp), %rdx
leaq -0xc8(%rbp), %r8
callq 0x116110
jmp 0xf6d92
movq -0x1c8(%rbp), %rsi
movq -0x50(%rbp), %rdx
leaq -0xf0(%rbp), %rdi
callq 0x64bc0
jmp 0xf6dab
movq -0x1c8(%rbp), %rdi
leaq 0x522ed(%rip), %rcx     # 0x1490a6
movq %rsp, %rax
movq %rcx, 0x28(%rax)
leaq -0xf0(%rbp), %rcx
movq %rcx, 0x20(%rax)
leaq 0x47587(%rip), %rcx     # 0x13e359
movq %rcx, 0x18(%rax)
leaq -0xc8(%rbp), %rcx
movq %rcx, 0x10(%rax)
leaq 0x55b53(%rip), %rdx     # 0x14c93b
movq %rdx, 0x8(%rax)
movq %rcx, (%rax)
leaq 0x55af3(%rip), %rsi     # 0x14c8e9
leaq 0x47aa0(%rip), %rcx     # 0x13e89d
leaq 0x55b16(%rip), %r9      # 0x14c91a
leaq -0x78(%rbp), %rdx
leaq -0x98(%rbp), %r8
callq 0x116290
jmp 0xf6e16
leaq -0xf0(%rbp), %rdi
callq 0x7708
jmp 0xf6f1c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
jmp 0xf6f5e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
jmp 0xf6f52
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
jmp 0xf6f46
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x7708
jmp 0xf6f46
movq -0x1c8(%rbp), %rsi
movq -0x50(%rbp), %rdx
leaq -0x110(%rbp), %rdi
callq 0x64bc0
jmp 0xf6ea8
movq -0x1c8(%rbp), %rdi
leaq 0x521f0(%rip), %rcx     # 0x1490a6
movq %rsp, %rax
movq %rcx, 0x8(%rax)
leaq -0x110(%rbp), %rcx
movq %rcx, (%rax)
leaq 0x55a1b(%rip), %rsi     # 0x14c8e9
leaq 0x479c8(%rip), %rcx     # 0x13e89d
leaq 0x52133(%rip), %r9      # 0x14900f
leaq -0x78(%rbp), %rdx
leaq -0x98(%rbp), %r8
callq 0x116480
jmp 0xf6eee
leaq -0x110(%rbp), %rdi
callq 0x7708
jmp 0xf6f1c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
leaq -0x110(%rbp), %rdi
callq 0x7708
jmp 0xf6f46
movb $0x1, -0x25(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x7708
leaq -0x98(%rbp), %rdi
callq 0x7708
leaq -0x78(%rbp), %rdi
callq 0x7708
jmp 0xf70e1
leaq -0xc8(%rbp), %rdi
callq 0x7708
leaq -0x98(%rbp), %rdi
callq 0x7708
leaq -0x78(%rbp), %rdi
callq 0x7708
jmp 0xf7204
movq -0x48(%rbp), %rdi
callq 0xfa00
cmpl $0xa, %eax
jne 0xf70df
movq -0x48(%rbp), %rdi
callq 0x57610
movq -0x1c8(%rbp), %rdi
movq %rax, -0x118(%rbp)
movq -0x118(%rbp), %rax
movl 0x28(%rax), %esi
callq 0xea90
movq -0x1c8(%rbp), %rsi
movq %rax, -0x120(%rbp)
movq -0x118(%rbp), %rax
movl 0x8(%rax), %edx
movq (%rsi), %rax
movq 0x38(%rax), %rax
leaq -0x140(%rbp), %rdi
movq %rdi, -0x1d8(%rbp)
movl $0x1, %ecx
callq *%rax
movq -0x1c8(%rbp), %rsi
movq -0x1d8(%rbp), %rcx
movq -0x120(%rbp), %rdx
movq (%rsi), %rax
movq 0xc8(%rax), %rax
leaq -0x160(%rbp), %rdi
xorl %r8d, %r8d
callq *%rax
jmp 0xf7005
movq -0x1c8(%rbp), %rsi
movq -0x118(%rbp), %rdx
leaq -0x180(%rbp), %rdi
callq 0x63510
jmp 0xf7021
movq -0x1c8(%rbp), %rdi
leaq 0x558ba(%rip), %rsi     # 0x14c8e9
leaq 0x51fd9(%rip), %rcx     # 0x14900f
leaq 0x52069(%rip), %r9      # 0x1490a6
leaq -0x160(%rbp), %rdx
leaq -0x180(%rbp), %r8
callq 0x115f80
jmp 0xf7052
leaq -0x180(%rbp), %rdi
callq 0x7708
leaq -0x160(%rbp), %rdi
callq 0x7708
movb $0x1, -0x25(%rbp)
leaq -0x140(%rbp), %rdi
callq 0x7708
jmp 0xf70df
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
jmp 0xf70ce
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
jmp 0xf70c2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
leaq -0x180(%rbp), %rdi
callq 0x7708
leaq -0x160(%rbp), %rdi
callq 0x7708
leaq -0x140(%rbp), %rdi
callq 0x7708
jmp 0xf7204
jmp 0xf70e1
jmp 0xf70e3
leaq -0x38(%rbp), %rdi
callq 0xfbd0
jmp 0xf6be1
cmpl $0x0, -0x24(%rbp)
je 0xf71e2
movq -0x1c8(%rbp), %rsi
movq (%rsi), %rax
movq 0xa0(%rax), %rax
leaq -0x1a0(%rbp), %rdi
movl $0x19, %edx
movl $0x4, %ecx
callq *%rax
movq -0x1c8(%rbp), %rdi
movl -0x24(%rbp), %esi
callq 0x31910
movq %rax, -0x1e0(%rbp)
jmp 0xf7137
movq -0x1e0(%rbp), %rdx
movq -0x1c8(%rbp), %rsi
leaq -0x1c0(%rbp), %rdi
callq 0x64bc0
jmp 0xf7153
movq -0x1c8(%rbp), %rdi
leaq 0x557df(%rip), %rsi     # 0x14c940
leaq 0x51ea7(%rip), %rcx     # 0x14900f
leaq 0x51f37(%rip), %r9      # 0x1490a6
leaq -0x1a0(%rbp), %rdx
leaq -0x1c0(%rbp), %r8
callq 0x116600
jmp 0xf7184
leaq -0x1c0(%rbp), %rdi
callq 0x7708
leaq -0x1a0(%rbp), %rdi
callq 0x7708
movb $0x1, -0x25(%rbp)
jmp 0xf71e2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
jmp 0xf71d4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
leaq -0x1c0(%rbp), %rdi
callq 0x7708
leaq -0x1a0(%rbp), %rdi
callq 0x7708
jmp 0xf7204
testb $0x1, -0x25(%rbp)
je 0xf71fb
movq -0x1c8(%rbp), %rdi
leaq 0x476a8(%rip), %rsi     # 0x13e89e
callq 0xedf0
addq $0x210, %rsp            # imm = 0x210
popq %rbp
retq
movq -0xa0(%rbp), %rdi
callq 0x7640