// File: rossy[P]crossc/SPIRV-Cross/spirv_hlsl.cpp
// Function: spirv_cross::CompilerHLSL::emit_atomic(unsigned int const*, unsigned int, spv::Op)
void CompilerHLSL::emit_atomic(const uint32_t *ops, uint32_t length, spv::Op op)
{
	const char *atomic_op = nullptr;

	string value_expr;
	if (op != OpAtomicIDecrement && op != OpAtomicIIncrement)
		value_expr = to_expression(ops[op == OpAtomicCompareExchange ? 6 : 5]);

	switch (op)
	{
	case OpAtomicIIncrement:
		atomic_op = "InterlockedAdd";
		value_expr = "1";
		break;

	case OpAtomicIDecrement:
		atomic_op = "InterlockedAdd";
		value_expr = "-1";
		break;

	case OpAtomicISub:
		atomic_op = "InterlockedAdd";
		value_expr = join("-", enclose_expression(value_expr));
		break;

	case OpAtomicSMin:
	case OpAtomicUMin:
		atomic_op = "InterlockedMin";
		break;

	case OpAtomicSMax:
	case OpAtomicUMax:
		atomic_op = "InterlockedMax";
		break;

	case OpAtomicAnd:
		atomic_op = "InterlockedAnd";
		break;

	case OpAtomicOr:
		atomic_op = "InterlockedOr";
		break;

	case OpAtomicXor:
		atomic_op = "InterlockedXor";
		break;

	case OpAtomicIAdd:
		atomic_op = "InterlockedAdd";
		break;

	case OpAtomicExchange:
		atomic_op = "InterlockedExchange";
		break;

	case OpAtomicCompareExchange:
		if (length < 8)
			SPIRV_CROSS_THROW("Not enough data for opcode.");
		atomic_op = "InterlockedCompareExchange";
		value_expr = join(to_expression(ops[7]), ", ", value_expr);
		break;

	default:
		SPIRV_CROSS_THROW("Unknown atomic opcode.");
	}

	uint32_t result_type = ops[0];
	uint32_t id = ops[1];
	forced_temporaries.insert(ops[1]);

	auto &type = get<SPIRType>(result_type);
	statement(variable_decl(type, to_name(id)), ";");

	auto &data_type = expression_type(ops[2]);
	auto *chain = maybe_get<SPIRAccessChain>(ops[2]);
	SPIRType::BaseType expr_type;
	if (data_type.storage == StorageClassImage || !chain)
	{
		statement(atomic_op, "(", to_expression(ops[2]), ", ", value_expr, ", ", to_name(id), ");");
		expr_type = data_type.basetype;
	}
	else
	{
		// RWByteAddress buffer is always uint in its underlying type.
		expr_type = SPIRType::UInt;
		statement(chain->base, ".", atomic_op, "(", chain->dynamic_index, chain->static_index, ", ", value_expr, ", ",
		          to_name(id), ");");
	}

	auto expr = bitcast_expression(type, expr_type, to_name(id));
	set<SPIRExpression>(id, expr, result_type, true);
	flush_all_atomic_capable_variables();
	register_read(ops[1], ops[2], should_forward(ops[2]));
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x330, %rsp            # imm = 0x330
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x268(%rbp)
movq $0x0, -0x20(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x7530
cmpl $0xe9, -0x18(%rbp)
je 0xcc0a9
cmpl $0xe8, -0x18(%rbp)
je 0xcc0a9
movq -0x268(%rbp), %rsi
movq -0x10(%rbp), %rax
movl -0x18(%rbp), %ecx
subl $0xe6, %ecx
sete %cl
movzbl %cl, %ecx
movl 0x14(%rax,%rcx,4), %edx
leaq -0x60(%rbp), %rdi
callq 0x71b50
jmp 0xcc080
leaq -0x40(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x7430
leaq -0x60(%rbp), %rdi
callq 0x7708
jmp 0xcc0a9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0xcc93d
movl -0x18(%rbp), %eax
addl $0xffffff1b, %eax       # imm = 0xFFFFFF1B
movl %eax, %ecx
movq %rcx, -0x270(%rbp)
subl $0xd, %eax
ja 0xcc383
movq -0x270(%rbp), %rax
leaq 0x7979b(%rip), %rcx     # 0x14586c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x7c969(%rip), %rax     # 0x148a4a
movq %rax, -0x20(%rbp)
leaq 0x7826c(%rip), %rsi     # 0x144358
leaq -0x40(%rbp), %rdi
callq 0x7510
jmp 0xcc0f7
jmp 0xcc453
leaq 0x7c947(%rip), %rax     # 0x148a4a
movq %rax, -0x20(%rbp)
leaq 0x78249(%rip), %rsi     # 0x144357
leaq -0x40(%rbp), %rdi
callq 0x7510
jmp 0xcc119
jmp 0xcc453
movq -0x268(%rbp), %rsi
leaq 0x7c91e(%rip), %rax     # 0x148a4a
movq %rax, -0x20(%rbp)
leaq -0xb0(%rbp), %rdi
leaq -0x40(%rbp), %rdx
callq 0x6ff50
jmp 0xcc142
leaq 0x78213(%rip), %rsi     # 0x14435c
leaq -0x90(%rbp), %rdi
leaq -0xb0(%rbp), %rdx
callq 0xa6fd0
jmp 0xcc15e
leaq -0x40(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x7430
leaq -0x90(%rbp), %rdi
callq 0x7708
leaq -0xb0(%rbp), %rdi
callq 0x7708
jmp 0xcc453
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x7708
jmp 0xcc93d
leaq 0x7c8aa(%rip), %rax     # 0x148a59
movq %rax, -0x20(%rbp)
jmp 0xcc453
leaq 0x7c8a9(%rip), %rax     # 0x148a68
movq %rax, -0x20(%rbp)
jmp 0xcc453
leaq 0x7c8a8(%rip), %rax     # 0x148a77
movq %rax, -0x20(%rbp)
jmp 0xcc453
leaq 0x7c8a7(%rip), %rax     # 0x148a86
movq %rax, -0x20(%rbp)
jmp 0xcc453
leaq 0x7c8a5(%rip), %rax     # 0x148a94
movq %rax, -0x20(%rbp)
jmp 0xcc453
leaq 0x7c84b(%rip), %rax     # 0x148a4a
movq %rax, -0x20(%rbp)
jmp 0xcc453
leaq 0x7c894(%rip), %rax     # 0x148aa3
movq %rax, -0x20(%rbp)
jmp 0xcc453
cmpl $0x8, -0x14(%rbp)
jae 0xcc2f2
movb $0x1, -0xd2(%rbp)
movl $0x10, %edi
callq 0x71a0
movq %rax, -0x280(%rbp)
leaq -0xd1(%rbp), %rdi
movq %rdi, -0x278(%rbp)
callq 0x7650
movq -0x278(%rbp), %rdx
leaq 0x7c85c(%rip), %rsi     # 0x148ab7
leaq -0xd0(%rbp), %rdi
callq 0x7520
jmp 0xcc269
movq -0x280(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x9c50
jmp 0xcc27e
movq -0x280(%rbp), %rdi
movb $0x0, -0xd2(%rbp)
leaq 0xe1b45(%rip), %rsi     # 0x1addd8
leaq -0xc260a(%rip), %rdx    # 0x9c90
callq 0x7610
jmp 0xcc94f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0xcc2ca
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x7708
leaq -0xd1(%rbp), %rdi
callq 0x73d0
testb $0x1, -0xd2(%rbp)
jne 0xcc2e1
jmp 0xcc2ed
movq -0x280(%rbp), %rdi
callq 0x72c0
jmp 0xcc93d
movq -0x268(%rbp), %rsi
leaq 0x7c7d3(%rip), %rax     # 0x148ad3
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movl 0x1c(%rax), %edx
leaq -0x118(%rbp), %rdi
callq 0x71b50
jmp 0xcc319
leaq 0x7826f(%rip), %rdx     # 0x14458f
leaq -0xf8(%rbp), %rdi
leaq -0x118(%rbp), %rsi
leaq -0x40(%rbp), %rcx
callq 0xddaa0
jmp 0xcc339
leaq -0x40(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x7430
leaq -0xf8(%rbp), %rdi
callq 0x7708
leaq -0x118(%rbp), %rdi
callq 0x7708
jmp 0xcc453
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x118(%rbp), %rdi
callq 0x7708
jmp 0xcc93d
movb $0x1, -0x13a(%rbp)
movl $0x10, %edi
callq 0x71a0
movq %rax, -0x290(%rbp)
leaq -0x139(%rbp), %rdi
movq %rdi, -0x288(%rbp)
callq 0x7650
movq -0x288(%rbp), %rdx
leaq 0x7c732(%rip), %rsi     # 0x148aee
leaq -0x138(%rbp), %rdi
callq 0x7520
jmp 0xcc3ca
movq -0x290(%rbp), %rdi
leaq -0x138(%rbp), %rsi
callq 0x9c50
jmp 0xcc3df
movq -0x290(%rbp), %rdi
movb $0x0, -0x13a(%rbp)
leaq 0xe19e4(%rip), %rsi     # 0x1addd8
leaq -0xc276b(%rip), %rdx    # 0x9c90
callq 0x7610
jmp 0xcc94f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0xcc42b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x7708
leaq -0x139(%rbp), %rdi
callq 0x73d0
testb $0x1, -0x13a(%rbp)
jne 0xcc442
jmp 0xcc44e
movq -0x290(%rbp), %rdi
callq 0x72c0
jmp 0xcc93d
movq -0x268(%rbp), %rdi
movq -0x10(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x140(%rbp)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x144(%rbp)
addq $0x228, %rdi            # imm = 0x228
movq -0x10(%rbp), %rsi
addq $0x4, %rsi
callq 0x2e890
movb %dl, -0x299(%rbp)
movq %rax, -0x298(%rbp)
jmp 0xcc496
movq -0x268(%rbp), %rdi
movb -0x299(%rbp), %al
movq -0x298(%rbp), %rcx
movq %rcx, -0x158(%rbp)
movb %al, -0x150(%rbp)
movl -0x140(%rbp), %esi
callq 0xea90
movq %rax, -0x2a8(%rbp)
jmp 0xcc4cb
movq -0x268(%rbp), %rsi
movq -0x2a8(%rbp), %rax
movq %rax, -0x160(%rbp)
movq -0x160(%rbp), %rax
movq %rax, -0x2b0(%rbp)
movl -0x144(%rbp), %edx
movq (%rsi), %rax
movq 0x38(%rax), %rax
leaq -0x1a0(%rbp), %rdi
movl $0x1, %ecx
callq *%rax
jmp 0xcc50b
movq -0x2b0(%rbp), %rdx
movq -0x268(%rbp), %rsi
movq (%rsi), %rax
movq 0xc8(%rax), %rax
leaq -0x180(%rbp), %rdi
leaq -0x1a0(%rbp), %rcx
xorl %r8d, %r8d
callq *%rax
jmp 0xcc538
movq -0x268(%rbp), %rdi
leaq 0x7cb60(%rip), %rdx     # 0x1490a6
leaq -0x180(%rbp), %rsi
callq 0xf270
jmp 0xcc554
leaq -0x180(%rbp), %rdi
callq 0x7708
leaq -0x1a0(%rbp), %rdi
callq 0x7708
movq -0x268(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x8(%rax), %esi
callq 0x1f760
movq %rax, -0x2b8(%rbp)
jmp 0xcc588
movq -0x268(%rbp), %rdi
movq -0x2b8(%rbp), %rax
movq %rax, -0x1a8(%rbp)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %esi
callq 0x2e7f0
movq %rax, -0x2c0(%rbp)
jmp 0xcc5b2
movq -0x2c0(%rbp), %rax
movq %rax, -0x1b0(%rbp)
movq -0x1a8(%rbp), %rax
cmpl $0xb, 0x64(%rax)
je 0xcc5db
cmpq $0x0, -0x1b0(%rbp)
jne 0xcc6fd
movq -0x268(%rbp), %rsi
movq -0x10(%rbp), %rax
movl 0x8(%rax), %edx
leaq -0x1d8(%rbp), %rdi
callq 0x71b50
jmp 0xcc5f7
movq -0x268(%rbp), %rsi
movl -0x144(%rbp), %edx
movq (%rsi), %rax
movq 0x38(%rax), %rax
leaq -0x1f8(%rbp), %rdi
movl $0x1, %ecx
callq *%rax
jmp 0xcc61b
movq -0x268(%rbp), %rdi
leaq 0x7ca7c(%rip), %rcx     # 0x1490a5
movq %rsp, %rax
movq %rcx, 0x10(%rax)
leaq -0x1f8(%rbp), %rcx
movq %rcx, 0x8(%rax)
leaq 0x77f4d(%rip), %r8      # 0x14458f
movq %r8, (%rax)
leaq 0x80ce1(%rip), %rdx     # 0x14d32d
leaq -0x20(%rbp), %rsi
leaq -0x1d8(%rbp), %rcx
leaq -0x40(%rbp), %r9
callq 0xddb60
jmp 0xcc662
leaq -0x1f8(%rbp), %rdi
callq 0x7708
leaq -0x1d8(%rbp), %rdi
callq 0x7708
movq -0x1a8(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x1b4(%rbp)
jmp 0xcc7ec
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0xcc6b5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x180(%rbp), %rdi
callq 0x7708
leaq -0x1a0(%rbp), %rdi
callq 0x7708
jmp 0xcc93d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0xcc6ec
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x1f8(%rbp), %rdi
callq 0x7708
leaq -0x1d8(%rbp), %rdi
callq 0x7708
jmp 0xcc93d
movq -0x268(%rbp), %rsi
movl $0x5, -0x1b4(%rbp)
movq -0x1b0(%rbp), %rax
movq %rax, %rcx
addq $0x18, %rcx
movq %rcx, -0x2d8(%rbp)
movq %rax, %rcx
addq $0x38, %rcx
movq %rcx, -0x2d0(%rbp)
addq $0x58, %rax
movq %rax, -0x2c8(%rbp)
movl -0x144(%rbp), %edx
movq (%rsi), %rax
movq 0x38(%rax), %rax
leaq -0x218(%rbp), %rdi
movl $0x1, %ecx
callq *%rax
jmp 0xcc759
movq -0x2d0(%rbp), %r9
movq -0x2d8(%rbp), %rsi
movq -0x268(%rbp), %rdi
movq -0x2c8(%rbp), %rcx
leaq 0x7c929(%rip), %rdx     # 0x1490a5
movq %rsp, %rax
movq %rdx, 0x28(%rax)
leaq -0x218(%rbp), %rdx
movq %rdx, 0x20(%rax)
leaq 0x77dfa(%rip), %rdx     # 0x14458f
movq %rdx, 0x18(%rax)
leaq -0x40(%rbp), %r8
movq %r8, 0x10(%rax)
movq %rdx, 0x8(%rax)
movq %rcx, (%rax)
leaq 0x7857e(%rip), %rdx     # 0x144d2d
leaq 0x80b77(%rip), %r8      # 0x14d32d
leaq -0x20(%rbp), %rcx
callq 0xddd00
jmp 0xcc7c1
leaq -0x218(%rbp), %rdi
callq 0x7708
jmp 0xcc7ec
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x218(%rbp), %rdi
callq 0x7708
jmp 0xcc93d
movq -0x268(%rbp), %rsi
movq -0x160(%rbp), %rax
movq %rax, -0x2e8(%rbp)
movl -0x1b4(%rbp), %eax
movl %eax, -0x2dc(%rbp)
movl -0x144(%rbp), %edx
movq (%rsi), %rax
movq 0x38(%rax), %rax
leaq -0x258(%rbp), %rdi
movl $0x1, %ecx
callq *%rax
jmp 0xcc82a
movl -0x2dc(%rbp), %ecx
movq -0x2e8(%rbp), %rdx
movq -0x268(%rbp), %rsi
leaq -0x238(%rbp), %rdi
leaq -0x258(%rbp), %r8
callq 0x7dfd0
jmp 0xcc853
leaq -0x258(%rbp), %rdi
callq 0x7708
movq -0x268(%rbp), %rdi
movl -0x144(%rbp), %esi
movb $0x1, -0x259(%rbp)
leaq -0x238(%rbp), %rdx
leaq -0x140(%rbp), %rcx
leaq -0x259(%rbp), %r8
callq 0xddef0
jmp 0xcc88f
movq -0x268(%rbp), %rdi
callq 0x1ffa0
jmp 0xcc89d
movq -0x268(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %ecx
movl %ecx, -0x2f4(%rbp)
movl 0x8(%rax), %esi
movl %esi, -0x2f0(%rbp)
callq 0x78810
movb %al, -0x2e9(%rbp)
jmp 0xcc8c7
movl -0x2f0(%rbp), %edx
movl -0x2f4(%rbp), %esi
movq -0x268(%rbp), %rdi
movb -0x2e9(%rbp), %al
movzbl %al, %ecx
andl $0x1, %ecx
callq 0x1fb20
jmp 0xcc8ed
leaq -0x238(%rbp), %rdi
callq 0x7708
leaq -0x40(%rbp), %rdi
callq 0x7708
addq $0x330, %rsp            # imm = 0x330
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x258(%rbp), %rdi
callq 0x7708
jmp 0xcc93d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x238(%rbp), %rdi
callq 0x7708
leaq -0x40(%rbp), %rdi
callq 0x7708
movq -0x68(%rbp), %rdi
callq 0x7640
nop