// File: /workspace/github_c_makefile_star2_O2/Ihsoh[P]ilang/compiler/backend/asmgen_gas32.c
// Function: asm_var
static void _asm_var(
	ASMGeneratorGas32Context *ctx,
	ParserASTNode *node
) {
	assert(ctx);
	assert(node);
	assert(node->type == BE_NODE_VAR);
	assert(node->nchilds > 0);

	int align = BE_VAR_AST_NODE_GET_ALIGN(node);

	for (int i = 0; i < node->nchilds; i++) {
		ParserASTNode *node_var_item = node->childs[i];
		assert(node_var_item->type == BE_NODE_VAR_ITEM);
		assert(node_var_item->nchilds == 2 || node_var_item->nchilds == 3);

		ParserASTNode *node_identifier = node_var_item->childs[0];
		assert(node_identifier->type == BE_NODE_IDENTIFIER);
		ParserASTNode *node_type = node_var_item->childs[1];
		ParserASTNode *node_expr = NULL;

		if (node_var_item->nchilds == 3) {
			node_expr = node_var_item->childs[2];
		}

		switch (BE_VAR_AST_NODE_GET_TYPE(node)) {
			case BE_VAR_TYPE_GLOBAL: {
				// .section __DATA,__data
				// .globl [VAR_NAME]
				// .balign	[?]
				// [VAR_NAME]:
				// .[TYPE] [EXPR]

				// rstr_append_with_cstr(ctx->body, ".section __DATA,__data\n");
				rstr_append_with_cstr(ctx->body, ".data\n");
				
				ResizableString rstr_var_name;
				rstr_init(&rstr_var_name);
				rstr_append_with_raw(
					&rstr_var_name,
					node_identifier->token->content,
					node_identifier->token->len
				);

				rstr_append_with_cstr(ctx->body, ".globl ");
				rstr_append_with_raw(ctx->body, node_identifier->token->content, node_identifier->token->len);
				rstr_append_with_char(ctx->body, '\n');

				// .balign	[?]
				if (align > 0) {
					rstr_appendf(ctx->body, ".balign %d\n", align);
				}

				rstr_append_with_raw(ctx->body, node_identifier->token->content, node_identifier->token->len);
				rstr_append_with_char(ctx->body, ':');
				rstr_append_with_char(ctx->body, '\n');

				switch (node_type->type) {
					case BE_NODE_TYPE_CHAR: {
						rstr_append_with_cstr(ctx->body, ".byte ");
						_asm_global_var_initializer(ctx, ctx->body, node_expr, node_type);
						break;
					}
					case BE_NODE_TYPE_INT8: {
						rstr_append_with_cstr(ctx->body, ".byte ");
						_asm_global_var_initializer(ctx, ctx->body, node_expr, node_type);
						break;
					}
					case BE_NODE_TYPE_UINT8: {
						rstr_append_with_cstr(ctx->body, ".byte ");
						_asm_global_var_initializer(ctx, ctx->body, node_expr, node_type);
						break;
					}
					case BE_NODE_TYPE_INT16: {
						rstr_append_with_cstr(ctx->body, ".2byte ");
						_asm_global_var_initializer(ctx, ctx->body, node_expr, node_type);
						break;
					}
					case BE_NODE_TYPE_UINT16: {
						rstr_append_with_cstr(ctx->body, ".2byte ");
						_asm_global_var_initializer(ctx, ctx->body, node_expr, node_type);
						break;
					}
					case BE_NODE_TYPE_INT32: {
						rstr_append_with_cstr(ctx->body, ".4byte ");
						_asm_global_var_initializer(ctx, ctx->body, node_expr, node_type);
						break;
					}
					case BE_NODE_TYPE_UINT32: {
						rstr_append_with_cstr(ctx->body, ".4byte ");
						_asm_global_var_initializer(ctx, ctx->body, node_expr, node_type);
						break;
					}
					case BE_NODE_TYPE_INT64: {
						rstr_append_with_cstr(ctx->body, ".8byte ");
						_asm_global_var_initializer(ctx, ctx->body, node_expr, node_type);
						break;
					}
					case BE_NODE_TYPE_UINT64: {
						rstr_append_with_cstr(ctx->body, ".8byte ");
						_asm_global_var_initializer(ctx, ctx->body, node_expr, node_type);
						break;
					}
					case BE_NODE_TYPE_FLOAT: {
						rstr_append_with_cstr(ctx->body, ".float ");
						_asm_global_var_initializer(ctx, ctx->body, node_expr, node_type);
						break;
					}
					case BE_NODE_TYPE_DOUBLE: {
						rstr_append_with_cstr(ctx->body, ".double ");
						_asm_global_var_initializer(ctx, ctx->body, node_expr, node_type);
						break;
					}
					case BE_NODE_TYPE_STRUCT: {
						rstr_appendf(
							ctx->body,
							".zero %zu\n",
							be_sem_calc_type_size(ctx->psrctx, node, node_type)
						);
						break;
					}
					case BE_NODE_TYPE_ARRAY: {
						rstr_appendf(
							ctx->body,
							".zero %zu\n",
							be_sem_calc_type_size(ctx->psrctx, node, node_type)
						);
						break;
					}
					case BE_NODE_TYPE_POINTER: {
						rstr_append_with_cstr(ctx->body, ".4byte ");
						_asm_global_var_initializer(ctx, ctx->body, node_expr, node_type);
						break;
					}
					case BE_NODE_TYPE_VA_LIST: {
						rstr_append_with_cstr(ctx->body, ".4byte ");
						_asm_global_var_initializer(ctx, ctx->body, node_expr, node_type);
						break;
					}
					default: {
						assert(0);
						break;
					}
				}

				// 更新全局变量符号在代码生成时的名字。
				ParserSymbol *symbol = BE_VAR_ITEM_AST_NODE_GET_SYMBOL(node_var_item);
				BE_VAR_SYMBOL_SET_HAS_CODE_GEN_NAME(symbol, true);
				ResizableString *code_gen_name = BE_VAR_SYMBOL_GET_CODE_GEN_NAME(symbol);

				ResizableString rstr_disp;
				rstr_init(&rstr_disp);
				rstr_appendf(
					&rstr_disp,
					"(%s-%%s)+%%d",
					RSTR_CSTR(&rstr_var_name)
				);

				rstr_init(code_gen_name);
				_asm_inst_memref_base_disp(
					ctx,
					code_gen_name,
					"%s",
					RSTR_CSTR(&rstr_disp)
				);

				rstr_free(&rstr_var_name);
				rstr_free(&rstr_disp);

				rstr_append_with_cstr(ctx->body, "\n\n");

				break;
			}
			case BE_VAR_TYPE_LOCAL: {
				ParserSymbol *symbol = BE_VAR_ITEM_AST_NODE_GET_SYMBOL(node_var_item);
				
				int align = BE_VAR_SYMBOL_GET_ALIGN(symbol);
				assert(align);

				size_t type_size = BE_VAR_SYMBOL_GET_TYPE_SIZE(symbol);

				if (ctx->local_var_address_counter % align > 0) {
					size_t align_padding = align - ctx->local_var_address_counter % align;
					ctx->local_var_address_counter += align_padding;
					ctx->local_var_size += align_padding;
				}

				BE_VAR_SYMBOL_SET_ADDRESS(symbol, ctx->local_var_address_counter);

				BE_VAR_SYMBOL_SET_HAS_CODE_GEN_NAME(symbol, true);	
				ResizableString *code_gen_name = BE_VAR_SYMBOL_GET_CODE_GEN_NAME(symbol);
				rstr_init(code_gen_name);
				rstr_appendf(
					code_gen_name,
					"%d+%%d(%%s)",
					-(ctx->local_var_address_counter + type_size)
				);

				rstr_appendf(
					ctx->local_var_defs,
					"# local-var: type=local, index=%zu, address=%zu, align=%d, size=%zu, value=%s, name=",
					ctx->local_var_index_counter,
					ctx->local_var_address_counter,
					align,
					type_size,
					RSTR_CSTR(code_gen_name)
				);
				rstr_append_with_raw(
					ctx->local_var_defs,
					node_identifier->token->content,
					node_identifier->token->len
				);
				rstr_append_with_char(
					ctx->local_var_defs,
					'\n'
				);

				ctx->local_var_address_counter += type_size;
				ctx->local_var_size += type_size;
				ctx->local_var_index_counter++;

				break;
			}
			case BE_VAR_TYPE_STRUCT_MEMBER: {
				assert(0);
				break;
			}
			default: {
				assert(0);
			}
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
cmpl $0xc00, (%rsi)          # imm = 0xC00
jne 0xbb29
movl 0x44(%rsi), %eax
testl %eax, %eax
jle 0xbb48
movq %rdi, %rbx
movl 0x70(%rsi), %r13d
xorl %edi, %edi
movl %r13d, -0x44(%rbp)
movq %rsi, -0x38(%rbp)
cltq
cmpq %rax, %rdi
jge 0xba4a
movq 0x48(%rsi), %rax
movq (%rax,%rdi,8), %r15
cmpl $0xc01, (%r15)          # imm = 0xC01
jne 0xba59
movl 0x44(%r15), %ecx
movl %ecx, %eax
andl $-0x2, %eax
cmpl $0x2, %eax
jne 0xbabb
movq 0x48(%r15), %rax
movq (%rax), %r12
cmpl $0x200, (%r12)          # imm = 0x200
jne 0xba78
cmpl $0x3, %ecx
jne 0xb74a
movq 0x10(%rax), %rdx
jmp 0xb74c
xorl %edx, %edx
movzbl 0x60(%rsi), %ecx
cmpl $0x1, %ecx
movq %rdi, -0x50(%rbp)
je 0xb83c
cmpl $0x2, %ecx
jne 0xba97
movq %r12, -0x40(%rbp)
movq 0x60(%r15), %r14
movslq 0x60(%r14), %r13
testq %r13, %r13
je 0xbada
movq 0x70(%r14), %r12
movq 0x38(%rbx), %rcx
movq %rcx, %rax
xorl %edx, %edx
divq %r13
testq %rdx, %rdx
je 0xb7a1
movq %r13, %rax
subq %rdx, %rax
addq %rax, %rcx
movq %rcx, 0x38(%rbx)
addq %rax, 0x40(%rbx)
movq %rcx, 0x68(%r14)
movb $0x1, 0x48(%r14)
leaq 0x50(%r14), %r15
movq %r15, %rdi
callq 0x6e1c
movq 0x38(%rbx), %rdx
addq %r12, %rdx
negq %rdx
movq %r15, %rdi
leaq 0x32011(%rip), %rsi     # 0x3d7db
xorl %eax, %eax
callq 0x739a
movq 0x48(%rbx), %rdx
movq 0x30(%rbx), %rdi
movq 0x38(%rbx), %rcx
movq 0x58(%r14), %rax
testq %rax, %rax
leaq 0x32240(%rip), %rsi     # 0x3da2b
cmoveq %rsi, %rax
movq %rax, (%rsp)
leaq 0x31fed(%rip), %rsi     # 0x3d7e7
movl %r13d, %r8d
movq %r12, %r9
xorl %eax, %eax
callq 0x739a
movq 0x30(%rbx), %rdi
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rsi
movl 0x4(%rax), %edx
callq 0x700f
movq 0x30(%rbx), %rdi
pushq $0xa
popq %rsi
callq 0x6f81
addq %r12, 0x38(%rbx)
addq %r12, 0x40(%rbx)
incq 0x48(%rbx)
jmp 0xba33
movq %rdx, -0x30(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
movq 0x20(%rbx), %rdi
leaq 0x31f1f(%rip), %rsi     # 0x3d772
callq 0x6e99
leaq -0x60(%rbp), %r14
movq %r14, %rdi
callq 0x6e1c
movq 0x10(%r12), %rax
movq 0x8(%rax), %rsi
movl 0x4(%rax), %edx
movq %r14, %rdi
callq 0x700f
movq 0x20(%rbx), %rdi
leaq 0x31ef6(%rip), %rsi     # 0x3d779
callq 0x6e99
movq 0x20(%rbx), %rdi
movq 0x10(%r12), %rax
movq 0x8(%rax), %rsi
movl 0x4(%rax), %edx
callq 0x700f
movq 0x20(%rbx), %rdi
pushq $0xa
popq %rsi
callq 0x6f81
testl %r13d, %r13d
jle 0xb8c3
movq 0x20(%rbx), %rdi
leaq 0x31ec8(%rip), %rsi     # 0x3d781
movl %r13d, %edx
xorl %eax, %eax
callq 0x739a
movq 0x20(%rbx), %rdi
movq 0x10(%r12), %rax
movq 0x8(%rax), %rsi
movl 0x4(%rax), %edx
callq 0x700f
movq 0x20(%rbx), %rdi
pushq $0x3a
popq %rsi
callq 0x6f81
movq 0x20(%rbx), %rdi
pushq $0xa
popq %rsi
callq 0x6f81
movq -0x40(%rbp), %r14
movl (%r14), %eax
leal -0x400(%rax), %ecx
cmpl $0x20, %ecx
ja 0xb962
leaq 0x318a7(%rip), %rdx     # 0x3d1b0
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
movq 0x20(%rbx), %rdi
leaq 0x31e70(%rip), %rsi     # 0x3d78d
jmp 0xb992
movq (%rbx), %rdi
movq 0x20(%rbx), %r13
movq -0x38(%rbp), %rsi
movq %r14, %rdx
callq 0x27a58
movq %r13, %rdi
leaq 0x31e81(%rip), %rsi     # 0x3d7bd
movq %rax, %rdx
xorl %eax, %eax
callq 0x739a
jmp 0xb9aa
movq 0x20(%rbx), %rdi
leaq 0x31e51(%rip), %rsi     # 0x3d7a4
jmp 0xb992
movq 0x20(%rbx), %rdi
leaq 0x31e34(%rip), %rsi     # 0x3d794
jmp 0xb992
cmpl $0x440, %eax            # imm = 0x440
jne 0xbb0a
movq 0x20(%rbx), %rdi
leaq 0x31e24(%rip), %rsi     # 0x3d79c
jmp 0xb992
movq 0x20(%rbx), %rdi
leaq 0x31e2f(%rip), %rsi     # 0x3d7b4
jmp 0xb992
movq 0x20(%rbx), %rdi
leaq 0x31e1a(%rip), %rsi     # 0x3d7ac
callq 0x6e99
movq 0x20(%rbx), %rsi
movq %rbx, %rdi
movq -0x30(%rbp), %rdx
movq %r14, %rcx
callq 0xbb67
movq 0x60(%r15), %r12
movb $0x1, 0x48(%r12)
addq $0x50, %r12
leaq -0x70(%rbp), %r14
movq %r14, %rdi
callq 0x6e1c
movq -0x58(%rbp), %rdx
testq %rdx, %rdx
leaq 0x32059(%rip), %r15     # 0x3da2b
cmoveq %r15, %rdx
movq %r14, %rdi
leaq 0x31de8(%rip), %rsi     # 0x3d7c8
xorl %eax, %eax
callq 0x739a
movq %r12, %rdi
callq 0x6e1c
movq -0x68(%rbp), %rdx
testq %rdx, %rdx
cmoveq %r15, %rdx
movq %r12, %rdi
leaq 0x32087(%rip), %rsi     # 0x3da8b
leaq 0x327af(%rip), %rcx     # 0x3e1ba
xorl %eax, %eax
callq 0x739a
leaq -0x60(%rbp), %rdi
callq 0x72b9
movq %r14, %rdi
callq 0x72b9
movq 0x20(%rbx), %rdi
leaq 0x31ffb(%rip), %rsi     # 0x3da29
callq 0x6e99
movq -0x38(%rbp), %rsi
movq -0x50(%rbp), %rdi
incq %rdi
movl 0x44(%rsi), %eax
movl -0x44(%rbp), %r13d
jmp 0xb6f8
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x31c83(%rip), %rdi     # 0x3d6e3
leaq 0x319e1(%rip), %rsi     # 0x3d448
leaq 0x31c28(%rip), %rcx     # 0x3d696
movl $0xd48, %edx            # imm = 0xD48
callq 0x30a0
leaq 0x31cc7(%rip), %rdi     # 0x3d746
leaq 0x319c2(%rip), %rsi     # 0x3d448
leaq 0x31c09(%rip), %rcx     # 0x3d696
movl $0xd4c, %edx            # imm = 0xD4C
callq 0x30a0
leaq 0x2f8cb(%rip), %rdi     # 0x3b369
leaq 0x319a3(%rip), %rsi     # 0x3d448
cmpl $0x4, %ecx
jne 0xbaf9
leaq 0x31be5(%rip), %rcx     # 0x3d696
movl $0xe1b, %edx            # imm = 0xE1B
callq 0x30a0
leaq 0x31c49(%rip), %rdi     # 0x3d70b
leaq 0x3197f(%rip), %rsi     # 0x3d448
leaq 0x31bc6(%rip), %rcx     # 0x3d696
movl $0xd49, %edx            # imm = 0xD49
callq 0x30a0
leaq 0x31cf4(%rip), %rdi     # 0x3d7d5
leaq 0x31960(%rip), %rsi     # 0x3d448
leaq 0x31ba7(%rip), %rcx     # 0x3d696
movl $0xdec, %edx            # imm = 0xDEC
callq 0x30a0
leaq 0x31b96(%rip), %rcx     # 0x3d696
movl $0xe1f, %edx            # imm = 0xE1F
callq 0x30a0
leaq 0x2f858(%rip), %rdi     # 0x3b369
leaq 0x31930(%rip), %rsi     # 0x3d448
leaq 0x31b77(%rip), %rcx     # 0x3d696
movl $0xdc7, %edx            # imm = 0xDC7
callq 0x30a0
leaq 0x3dc78(%rip), %rdi     # 0x497a8
leaq 0x31911(%rip), %rsi     # 0x3d448
leaq 0x31b58(%rip), %rcx     # 0x3d696
movl $0xd41, %edx            # imm = 0xD41
callq 0x30a0
leaq 0x31b82(%rip), %rdi     # 0x3d6d1
leaq 0x318f2(%rip), %rsi     # 0x3d448
leaq 0x31b39(%rip), %rcx     # 0x3d696
movl $0xd42, %edx            # imm = 0xD42
callq 0x30a0