// File: /workspace/github_c_makefile_star2_O3/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 0xc5a4
cmpl $0x0, 0x44(%rsi)
jle 0xc5c3
movq %rdi, %rbx
movl 0x70(%rsi), %r13d
xorl %edi, %edi
movl %r13d, -0x44(%rbp)
movq %rsi, -0x38(%rbp)
movq 0x48(%rsi), %rax
movq (%rax,%rdi,8), %r15
cmpl $0xc01, (%r15)          # imm = 0xC01
jne 0xc536
movl 0x44(%r15), %ecx
movl %ecx, %eax
andl $-0x2, %eax
cmpl $0x2, %eax
jne 0xc517
movq 0x48(%r15), %rax
movq (%rax), %r12
cmpl $0x200, (%r12)          # imm = 0x200
jne 0xc4f8
cmpl $0x3, %ecx
jne 0xc1b4
movq 0x10(%rax), %rdx
jmp 0xc1b6
xorl %edx, %edx
movzbl 0x60(%rsi), %ecx
cmpl $0x1, %ecx
movq %rdi, -0x50(%rbp)
je 0xc2a8
cmpl $0x2, %ecx
jne 0xc4d0
movq %r12, -0x40(%rbp)
movq 0x60(%r15), %r14
movslq 0x60(%r14), %r13
testq %r13, %r13
je 0xc555
movq 0x70(%r14), %r12
movq 0x38(%rbx), %rcx
movq %rcx, %rax
xorl %edx, %edx
divq %r13
testq %rdx, %rdx
je 0xc20b
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 0x7314
movq 0x38(%rbx), %rdx
addq %r12, %rdx
negq %rdx
movq %r15, %rdi
leaq 0x34597(%rip), %rsi     # 0x407cb
xorl %eax, %eax
callq 0x7905
movq 0x48(%rbx), %rdx
movq 0x30(%rbx), %rdi
movq 0x38(%rbx), %rcx
movq 0x58(%r14), %rax
testq %rax, %rax
leaq 0x347c6(%rip), %rsi     # 0x40a1b
cmoveq %rsi, %rax
movq %rax, (%rsp)
leaq 0x34573(%rip), %rsi     # 0x407d7
movl %r13d, %r8d
movq %r12, %r9
xorl %eax, %eax
callq 0x7905
movq 0x30(%rbx), %rdi
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rsi
movl 0x4(%rax), %edx
callq 0x7537
movq 0x30(%rbx), %rdi
movl $0xa, %esi
callq 0x749a
addq %r12, 0x38(%rbx)
addq %r12, 0x40(%rbx)
incq 0x48(%rbx)
jmp 0xc4a5
movq %rdx, -0x30(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
movq 0x20(%rbx), %rdi
leaq 0x344a3(%rip), %rsi     # 0x40762
callq 0x739b
leaq -0x60(%rbp), %r14
movq %r14, %rdi
callq 0x7314
movq 0x10(%r12), %rax
movq 0x8(%rax), %rsi
movl 0x4(%rax), %edx
movq %r14, %rdi
callq 0x7537
movq 0x20(%rbx), %rdi
leaq 0x3447a(%rip), %rsi     # 0x40769
callq 0x739b
movq 0x20(%rbx), %rdi
movq 0x10(%r12), %rax
movq 0x8(%rax), %rsi
movl 0x4(%rax), %edx
callq 0x7537
movq 0x20(%rbx), %rdi
movl $0xa, %esi
callq 0x749a
testl %r13d, %r13d
jle 0xc331
movq 0x20(%rbx), %rdi
leaq 0x3444a(%rip), %rsi     # 0x40771
movl %r13d, %edx
xorl %eax, %eax
callq 0x7905
movq 0x20(%rbx), %rdi
movq 0x10(%r12), %rax
movq 0x8(%rax), %rsi
movl 0x4(%rax), %edx
callq 0x7537
movq 0x20(%rbx), %rdi
movl $0x3a, %esi
callq 0x749a
movq 0x20(%rbx), %rdi
movl $0xa, %esi
callq 0x749a
movq -0x40(%rbp), %r14
movl (%r14), %eax
leal -0x400(%rax), %ecx
cmpl $0x20, %ecx
ja 0xc3d4
leaq 0x33e25(%rip), %rdx     # 0x401a0
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
movq 0x20(%rbx), %rdi
leaq 0x343ee(%rip), %rsi     # 0x4077d
jmp 0xc404
movq (%rbx), %rdi
movq 0x20(%rbx), %r12
movq -0x38(%rbp), %rsi
movq %r14, %rdx
callq 0x2a367
movq %r12, %rdi
leaq 0x343ff(%rip), %rsi     # 0x407ad
movq %rax, %rdx
xorl %eax, %eax
callq 0x7905
jmp 0xc41c
movq 0x20(%rbx), %rdi
leaq 0x343cf(%rip), %rsi     # 0x40794
jmp 0xc404
movq 0x20(%rbx), %rdi
leaq 0x343b2(%rip), %rsi     # 0x40784
jmp 0xc404
cmpl $0x440, %eax            # imm = 0x440
jne 0xc585
movq 0x20(%rbx), %rdi
leaq 0x343a2(%rip), %rsi     # 0x4078c
jmp 0xc404
movq 0x20(%rbx), %rdi
leaq 0x343ad(%rip), %rsi     # 0x407a4
jmp 0xc404
movq 0x20(%rbx), %rdi
leaq 0x34398(%rip), %rsi     # 0x4079c
callq 0x739b
movq 0x20(%rbx), %rsi
movq %rbx, %rdi
movq -0x30(%rbp), %rdx
movq %r14, %rcx
callq 0xc5e2
movq 0x60(%r15), %r12
movb $0x1, 0x48(%r12)
addq $0x50, %r12
leaq -0x70(%rbp), %r14
movq %r14, %rdi
callq 0x7314
movq -0x58(%rbp), %rdx
testq %rdx, %rdx
leaq 0x345d7(%rip), %r15     # 0x40a1b
cmoveq %r15, %rdx
movq %r14, %rdi
leaq 0x34366(%rip), %rsi     # 0x407b8
xorl %eax, %eax
callq 0x7905
movq %r12, %rdi
callq 0x7314
movq -0x68(%rbp), %rdx
testq %rdx, %rdx
cmoveq %r15, %rdx
movq %r12, %rdi
leaq 0x34605(%rip), %rsi     # 0x40a7b
leaq 0x34d3e(%rip), %rcx     # 0x411bb
xorl %eax, %eax
callq 0x7905
leaq -0x60(%rbp), %rdi
callq 0x7812
movq %r14, %rdi
callq 0x7812
movq 0x20(%rbx), %rdi
leaq 0x34579(%rip), %rsi     # 0x40a19
callq 0x739b
movq -0x38(%rbp), %rsi
movq -0x50(%rbp), %rdi
incq %rdi
movslq 0x44(%rsi), %rax
cmpq %rax, %rdi
movl -0x44(%rbp), %r13d
jl 0xc16d
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x31e92(%rip), %rdi     # 0x3e369
leaq 0x33f5a(%rip), %rsi     # 0x40438
cmpl $0x4, %ecx
jne 0xc574
leaq 0x34198(%rip), %rcx     # 0x40686
movl $0xe1b, %edx            # imm = 0xE1B
callq 0x30a0
leaq 0x34237(%rip), %rdi     # 0x40736
leaq 0x33f32(%rip), %rsi     # 0x40438
leaq 0x34179(%rip), %rcx     # 0x40686
movl $0xd4c, %edx            # imm = 0xD4C
callq 0x30a0
leaq 0x341dd(%rip), %rdi     # 0x406fb
leaq 0x33f13(%rip), %rsi     # 0x40438
leaq 0x3415a(%rip), %rcx     # 0x40686
movl $0xd49, %edx            # imm = 0xD49
callq 0x30a0
leaq 0x34196(%rip), %rdi     # 0x406d3
leaq 0x33ef4(%rip), %rsi     # 0x40438
leaq 0x3413b(%rip), %rcx     # 0x40686
movl $0xd48, %edx            # imm = 0xD48
callq 0x30a0
leaq 0x34269(%rip), %rdi     # 0x407c5
leaq 0x33ed5(%rip), %rsi     # 0x40438
leaq 0x3411c(%rip), %rcx     # 0x40686
movl $0xdec, %edx            # imm = 0xDEC
callq 0x30a0
leaq 0x3410b(%rip), %rcx     # 0x40686
movl $0xe1f, %edx            # imm = 0xE1F
callq 0x30a0
leaq 0x31ddd(%rip), %rdi     # 0x3e369
leaq 0x33ea5(%rip), %rsi     # 0x40438
leaq 0x340ec(%rip), %rcx     # 0x40686
movl $0xdc7, %edx            # imm = 0xDC7
callq 0x30a0
leaq 0x401cd(%rip), %rdi     # 0x4c778
leaq 0x33e86(%rip), %rsi     # 0x40438
leaq 0x340cd(%rip), %rcx     # 0x40686
movl $0xd41, %edx            # imm = 0xD41
callq 0x30a0
leaq 0x340f7(%rip), %rdi     # 0x406c1
leaq 0x33e67(%rip), %rsi     # 0x40438
leaq 0x340ae(%rip), %rcx     # 0x40686
movl $0xd42, %edx            # imm = 0xD42
callq 0x30a0