// File: rossy[P]crossc/SPIRV-Cross/spirv_hlsl.cpp
// Function: spirv_cross::CompilerHLSL::emit_access_chain(spirv_cross::Instruction const&)
void CompilerHLSL::emit_access_chain(const Instruction &instruction)
{
	auto ops = stream(instruction);
	uint32_t length = instruction.length;

	bool need_byte_access_chain = false;
	auto &type = expression_type(ops[2]);
	const SPIRAccessChain *chain = nullptr;
	if (type.storage == StorageClassStorageBuffer || has_decoration(type.self, DecorationBufferBlock))
	{
		// If we are starting to poke into an SSBO, we are dealing with ByteAddressBuffers, and we need
		// to emit SPIRAccessChain rather than a plain SPIRExpression.
		uint32_t chain_arguments = length - 3;
		if (chain_arguments > type.array.size())
			need_byte_access_chain = true;
	}
	else
	{
		// Keep tacking on an existing access chain.
		chain = maybe_get<SPIRAccessChain>(ops[2]);
		if (chain)
			need_byte_access_chain = true;
	}

	if (need_byte_access_chain)
	{
		uint32_t to_plain_buffer_length = static_cast<uint32_t>(type.array.size());
		auto *backing_variable = maybe_get_backing_variable(ops[2]);

		string base;
		if (to_plain_buffer_length != 0)
		{
			bool need_transpose;
			base = access_chain(ops[2], &ops[3], to_plain_buffer_length, get<SPIRType>(ops[0]), &need_transpose);
		}
		else if (chain)
			base = chain->base;
		else
			base = to_expression(ops[2]);

		// Start traversing type hierarchy at the proper non-pointer types.
		auto *basetype = &get_non_pointer_type(type);

		// Traverse the type hierarchy down to the actual buffer types.
		for (uint32_t i = 0; i < to_plain_buffer_length; i++)
		{
			assert(basetype->parent_type);
			basetype = &get<SPIRType>(basetype->parent_type);
		}

		uint32_t matrix_stride = 0;
		bool row_major_matrix = false;

		// Inherit matrix information.
		if (chain)
		{
			matrix_stride = chain->matrix_stride;
			row_major_matrix = chain->row_major_matrix;
		}

		auto offsets =
		    flattened_access_chain_offset(*basetype, &ops[3 + to_plain_buffer_length],
		                                  length - 3 - to_plain_buffer_length, 0, 1, &row_major_matrix, &matrix_stride);

		auto &e = set<SPIRAccessChain>(ops[1], ops[0], type.storage, base, offsets.first, offsets.second);
		e.row_major_matrix = row_major_matrix;
		e.matrix_stride = matrix_stride;
		e.immutable = should_forward(ops[2]);
		e.loaded_from = backing_variable ? backing_variable->self : 0;

		if (chain)
		{
			e.dynamic_index += chain->dynamic_index;
			e.static_index += chain->static_index;
		}
	}
	else
	{
		CompilerGLSL::emit_instruction(instruction);
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x108(%rbp)
movq -0x10(%rbp), %rsi
callq 0x2e560
movq -0x108(%rbp), %rdi
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x1c(%rbp)
movb $0x0, -0x1d(%rbp)
movq -0x18(%rbp), %rax
movl 0x8(%rax), %esi
callq 0x1f760
movq %rax, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x28(%rbp), %rax
cmpl $0xc, 0x64(%rax)
je 0xcbbc0
movq -0x108(%rbp), %rdi
movq -0x28(%rbp), %rax
movl 0x8(%rax), %esi
movl $0x3, %edx
callq 0x1f560
testb $0x1, %al
jne 0xcbbc0
jmp 0xcbbf5
movl -0x1c(%rbp), %eax
subl $0x3, %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq %rax, -0x110(%rbp)
movq -0x28(%rbp), %rdi
addq $0x20, %rdi
callq 0x8650
movq %rax, %rcx
movq -0x110(%rbp), %rax
cmpq %rcx, %rax
jbe 0xcbbf3
movb $0x1, -0x1d(%rbp)
jmp 0xcbc19
movq -0x108(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x8(%rax), %esi
callq 0x2e7f0
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0xcbc17
movb $0x1, -0x1d(%rbp)
jmp 0xcbc19
testb $0x1, -0x1d(%rbp)
je 0xcbfe1
movq -0x28(%rbp), %rdi
addq $0x20, %rdi
callq 0x8650
movq -0x108(%rbp), %rdi
movl %eax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movl 0x8(%rax), %esi
callq 0x1fa90
movq %rax, -0x40(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x7530
cmpl $0x0, -0x38(%rbp)
je 0xcbd0c
movq -0x108(%rbp), %rdi
movq -0x18(%rbp), %rax
movl (%rax), %esi
movl 0x8(%rax), %ecx
movl %ecx, -0x12c(%rbp)
addq $0xc, %rax
movq %rax, -0x128(%rbp)
movl -0x38(%rbp), %eax
movl %eax, -0x11c(%rbp)
callq 0xea90
movq %rax, -0x118(%rbp)
jmp 0xcbc95
movq -0x118(%rbp), %r9
movl -0x11c(%rbp), %r8d
movq -0x128(%rbp), %rcx
movl -0x12c(%rbp), %edx
movq -0x108(%rbp), %rsi
movq %rsp, %rax
leaq -0x61(%rbp), %rdi
movq %rdi, (%rax)
movq $0x0, 0x8(%rax)
leaq -0x88(%rbp), %rdi
callq 0x85f90
jmp 0xcbcd7
leaq -0x60(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x7430
leaq -0x88(%rbp), %rdi
callq 0x7708
jmp 0xcbd62
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
jmp 0xcbfd6
cmpq $0x0, -0x30(%rbp)
je 0xcbd28
movq -0x30(%rbp), %rsi
addq $0x18, %rsi
leaq -0x60(%rbp), %rdi
callq 0x7050
jmp 0xcbd26
jmp 0xcbd60
movq -0x108(%rbp), %rsi
movq -0x18(%rbp), %rax
movl 0x8(%rax), %edx
leaq -0xb8(%rbp), %rdi
callq 0x71b50
jmp 0xcbd44
leaq -0x60(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x7430
leaq -0xb8(%rbp), %rdi
callq 0x7708
jmp 0xcbd62
movq -0x108(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x22e00
movq %rax, -0x138(%rbp)
jmp 0xcbd7b
movq -0x138(%rbp), %rax
movq %rax, -0xc0(%rbp)
movl $0x0, -0xc4(%rbp)
movl -0xc4(%rbp), %eax
cmpl -0x38(%rbp), %eax
jae 0xcbe10
movq -0xc0(%rbp), %rax
cmpl $0x0, 0x9c(%rax)
je 0xcbdb0
jmp 0xcbdcf
leaq 0x7cc1b(%rip), %rdi     # 0x1489d2
leaq 0x7cc2a(%rip), %rsi     # 0x1489e8
movl $0xdf1, %edx            # imm = 0xDF1
leaq 0x7cc39(%rip), %rcx     # 0x148a03
callq 0x7260
movq -0x108(%rbp), %rdi
movq -0xc0(%rbp), %rax
movl 0x9c(%rax), %esi
callq 0xea90
movq %rax, -0x140(%rbp)
jmp 0xcbdf1
movq -0x140(%rbp), %rax
movq %rax, -0xc0(%rbp)
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc4(%rbp)
jmp 0xcbd93
movl $0x0, -0xc8(%rbp)
movb $0x0, -0xc9(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0xcbe44
movq -0x30(%rbp), %rax
movl 0x60(%rax), %eax
movl %eax, -0xc8(%rbp)
movq -0x30(%rbp), %rax
movb 0x64(%rax), %al
andb $0x1, %al
movb %al, -0xc9(%rbp)
movq -0x108(%rbp), %rsi
movq -0xc0(%rbp), %rdx
movq -0x18(%rbp), %rcx
movl -0x38(%rbp), %eax
movl %eax, %edi
addl $0x3, %edi
leaq (%rcx,%rdi,4), %rcx
movl -0x1c(%rbp), %r8d
subl %eax, %r8d
addl $-0x3, %r8d
movq %rsp, %rax
leaq -0xc8(%rbp), %rdi
movq %rdi, 0x10(%rax)
leaq -0xc9(%rbp), %rdi
movq %rdi, 0x8(%rax)
movl $0x1, (%rax)
leaq -0xf8(%rbp), %rdi
xorl %r9d, %r9d
callq 0x862e0
jmp 0xcbe9d
movq -0x108(%rbp), %rdi
movq -0x18(%rbp), %rdx
movl 0x4(%rdx), %esi
movq -0x28(%rbp), %rcx
addq $0x64, %rcx
leaq -0xd8(%rbp), %r8
movq %rsp, %rax
movq %r8, (%rax)
leaq -0x60(%rbp), %r8
leaq -0xf8(%rbp), %r9
callq 0xdda30
movq %rax, -0x148(%rbp)
jmp 0xcbed9
movq -0x108(%rbp), %rdi
movq -0x148(%rbp), %rax
movq %rax, -0x100(%rbp)
movb -0xc9(%rbp), %cl
movq -0x100(%rbp), %rax
andb $0x1, %cl
movb %cl, 0x64(%rax)
movl -0xc8(%rbp), %ecx
movq -0x100(%rbp), %rax
movl %ecx, 0x60(%rax)
movq -0x18(%rbp), %rax
movl 0x8(%rax), %esi
callq 0x78810
movb %al, -0x149(%rbp)
jmp 0xcbf25
movb -0x149(%rbp), %cl
movq -0x100(%rbp), %rax
andb $0x1, %cl
movb %cl, 0x65(%rax)
cmpq $0x0, -0x40(%rbp)
je 0xcbf4e
movq -0x40(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x150(%rbp)
jmp 0xcbf58
xorl %eax, %eax
movl %eax, -0x150(%rbp)
jmp 0xcbf58
movl -0x150(%rbp), %ecx
movq -0x100(%rbp), %rax
movl %ecx, 0x5c(%rax)
cmpq $0x0, -0x30(%rbp)
je 0xcbfbf
movq -0x30(%rbp), %rsi
addq $0x38, %rsi
movq -0x100(%rbp), %rdi
addq $0x38, %rdi
callq 0x7470
jmp 0xcbf89
movq -0x30(%rbp), %rax
movl 0x58(%rax), %ecx
movq -0x100(%rbp), %rax
addl 0x58(%rax), %ecx
movl %ecx, 0x58(%rax)
jmp 0xcbfbf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0xa4730
jmp 0xcbfd6
leaq -0xf8(%rbp), %rdi
callq 0xa4730
leaq -0x60(%rbp), %rdi
callq 0x7708
jmp 0xcbff1
leaq -0x60(%rbp), %rdi
callq 0x7708
jmp 0xcbffa
movq -0x108(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x89270
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
movq -0x90(%rbp), %rdi
callq 0x7640
nopw %cs:(%rax,%rax)