// File: /workspace/github_c_makefile_star2_O2/Ihsoh[P]ilang/compiler/assembler/ins_enc.c
// Function: ins_enc_Ap
void ins_enc_Ap(
	Instruction *ins,
	InstructionEncoderData *data
) {
	_CHECK_PARAMS(1);

	ParserASTNode *ins_node = data->ins_node;

	ParserASTNode *oprd = ins_node->childs[0];

	EncoderInstruction enc_ins;
	
	ins_init(data->ctx, ins, ins_node, &enc_ins);

	int addr_size = ASM_DIRECT_ADDRESS_AST_NODE_GET_ADDR_SIZE(oprd);

	uint64_t base = ASM_DIRECT_ADDRESS_AST_NODE_GET_BASE(oprd);
	uint64_t offset = ASM_DIRECT_ADDRESS_AST_NODE_GET_OFFSET(oprd);

	switch (ASM_PARSER_CONTEXT_DATA_GET_ARCH(data->ctx)) {
		case ASM_ARCH_BIT16: {
			if (addr_size == ASM_MEM_ADDR_SIZE_UNKNOWN
					|| addr_size == ASM_MEM_ADDR_SIZE_16) {
				ins_add_uint16_to_ext(
					data->ctx,
					ins,
					&enc_ins,
					(uint16_t) offset
				);

				ins_add_uint16_to_ext(
					data->ctx,
					ins,
					&enc_ins,
					(uint16_t) base
				);
			} else if (addr_size == ASM_MEM_ADDR_SIZE_32) {
				enc_ins.legacy_prefix.operand_size_override = true;

				ins_add_uint32_to_ext(
					data->ctx,
					ins,
					&enc_ins,
					(uint32_t) offset
				);

				ins_add_uint16_to_ext(
					data->ctx,
					ins,
					&enc_ins,
					(uint16_t) base
				);
			} else {
				assert(0);
			}
			break;
		}
		case ASM_ARCH_BIT32: {
			if (addr_size == ASM_MEM_ADDR_SIZE_16) {
				enc_ins.legacy_prefix.operand_size_override = true;

				ins_add_uint16_to_ext(
					data->ctx,
					ins,
					&enc_ins,
					(uint16_t) offset
				);

				ins_add_uint16_to_ext(
					data->ctx,
					ins,
					&enc_ins,
					(uint16_t) base
				);
			} else if (addr_size == ASM_MEM_ADDR_SIZE_32
					|| addr_size == ASM_MEM_ADDR_SIZE_UNKNOWN) {
				ins_add_uint32_to_ext(
					data->ctx,
					ins,
					&enc_ins,
					(uint32_t) offset
				);

				ins_add_uint16_to_ext(
					data->ctx,
					ins,
					&enc_ins,
					(uint16_t) base
				);
			} else {
				assert(0);
			}
			break;
		}
		case ASM_ARCH_BIT64: {
			data->ctx->syntax_error_node_msg(
				data->ctx,
				ins_node,
				"instruction not supported in 64-bit mode."
			);
			break;
		}
		default: {
			assert(0);
			break;
		}
	}

	_output(data, &enc_ins);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
testq %rdi, %rdi
je 0x1a7da
movq %rsi, %rbx
testq %rsi, %rsi
je 0x1a7e3
movq (%rbx), %r15
testq %r15, %r15
je 0x1a7ec
cmpl $0x1, 0x44(%r15)
jne 0x1a7f5
movq %rdi, %r14
movq 0x48(%r15), %rax
movq (%rax), %r12
movq 0x8(%rbx), %rdi
leaq -0x88(%rbp), %rcx
movq %r14, %rsi
movq %r15, %rdx
callq 0x1470f
movq 0x8(%rbx), %rdi
movl 0xb0(%rdi), %edx
cmpl $0x4, %edx
je 0x1a765
movl 0x60(%r12), %eax
movq 0x70(%r12), %r15
movq 0x80(%r12), %rcx
cmpl $0x2, %edx
je 0x1a72d
cmpl $0x1, %edx
jne 0x1a814
cmpl $0x1, %eax
ja 0x1a788
movl $0xffff, %r13d          # imm = 0xFFFF
andl %r13d, %ecx
leaq -0x88(%rbp), %r12
jmp 0x1a748
cmpl $0x1, %eax
jne 0x1a774
leaq -0x88(%rbp), %r12
movb $0x1, 0xb(%r12)
movl $0xffff, %r13d          # imm = 0xFFFF
andl %r13d, %ecx
movq %r14, %rsi
movq %r12, %rdx
callq 0x1557f
movq 0x8(%rbx), %rdi
andl %r13d, %r15d
movq %r14, %rsi
movq %r12, %rdx
movl %r15d, %ecx
jmp 0x1a7b7
leaq 0x16826(%rip), %rdx     # 0x30f92
movq %r15, %rsi
callq *0x58(%rdi)
jmp 0x1a7bc
testl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
jne 0x1a833
leaq -0x88(%rbp), %r12
jmp 0x1a79e
cmpl $0x2, %eax
jne 0x1a852
leaq -0x88(%rbp), %r12
movb $0x1, 0xb(%r12)
movq %r14, %rsi
movq %r12, %rdx
callq 0x155ca
movq 0x8(%rbx), %rdi
movzwl %r15w, %ecx
movq %r14, %rsi
movq %r12, %rdx
callq 0x1557f
leaq -0x88(%rbp), %rsi
movq %rbx, %rdi
callq 0x17180
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x14f59(%rip), %rdi     # 0x2f73a
jmp 0x1a7fc
leaq 0x1ae01(%rip), %rdi     # 0x355eb
jmp 0x1a7fc
leaq 0x164be(%rip), %rdi     # 0x30cb1
jmp 0x1a7fc
leaq 0x16736(%rip), %rdi     # 0x30f32
leaq 0x16451(%rip), %rsi     # 0x30c54
leaq 0x17367(%rip), %rcx     # 0x31b71
movl $0x837, %edx            # imm = 0x837
callq 0xa0b0
leaq 0x12b55(%rip), %rdi     # 0x2d370
leaq 0x16432(%rip), %rsi     # 0x30c54
leaq 0x17348(%rip), %rcx     # 0x31b71
movl $0x89a, %edx            # imm = 0x89A
callq 0xa0b0
leaq 0x12b36(%rip), %rdi     # 0x2d370
leaq 0x16413(%rip), %rsi     # 0x30c54
leaq 0x17329(%rip), %rcx     # 0x31b71
movl $0x88d, %edx            # imm = 0x88D
callq 0xa0b0
leaq 0x12b17(%rip), %rdi     # 0x2d370
leaq 0x163f4(%rip), %rsi     # 0x30c54
leaq 0x1730a(%rip), %rcx     # 0x31b71
movl $0x868, %edx            # imm = 0x868
callq 0xa0b0