// File: /workspace/github_c_makefile_star2_O1/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 0xc826
cmpl $0x0, 0x44(%rsi)
jle 0xc845
cmpl $0x0, 0x44(%rsi)
jle 0xc762
movq %rdi, %rbx
movl 0x70(%rsi), %r13d
xorl %edx, %edx
movl %r13d, -0x44(%rbp)
movq %rsi, -0x38(%rbp)
movq 0x48(%rsi), %rax
movq (%rax,%rdx,8), %r15
cmpl $0xc01, (%r15)          # imm = 0xC01
jne 0xc7d7
movl 0x44(%r15), %ecx
movl %ecx, %eax
andl $-0x2, %eax
cmpl $0x2, %eax
jne 0xc7b8
movq 0x48(%r15), %rax
movq (%rax), %r12
cmpl $0x200, (%r12)          # imm = 0x200
jne 0xc799
cmpl $0x3, %ecx
jne 0xc451
movq 0x10(%rax), %rdi
jmp 0xc453
xorl %edi, %edi
movzbl 0x60(%rsi), %ecx
cmpl $0x1, %ecx
movq %rdx, -0x50(%rbp)
je 0xc549
cmpl $0x2, %ecx
jne 0xc771
movq %r12, -0x40(%rbp)
movq 0x60(%r15), %r14
movslq 0x60(%r14), %r13
testq %r13, %r13
je 0xc7f6
movq 0x70(%r14), %r12
movq 0x38(%rbx), %rcx
movq %rcx, %rax
xorl %edx, %edx
divq %r13
testq %rdx, %rdx
je 0xc4a8
movq %r13, %rax
subq %rdx, %rax
addq %rax, %rcx
movq %rcx, 0x38(%rbx)
addq %rax, 0x40(%rbx)
movq 0x38(%rbx), %rax
movq %rax, 0x68(%r14)
movb $0x1, 0x48(%r14)
leaq 0x50(%r14), %r15
movq %r15, %rdi
callq 0x7508
movq 0x38(%rbx), %rdx
addq %r12, %rdx
negq %rdx
movq %r15, %rdi
leaq 0x36336(%rip), %rsi     # 0x4280b
xorl %eax, %eax
callq 0x7af1
movq 0x48(%rbx), %rdx
movq 0x30(%rbx), %rdi
movq 0x38(%rbx), %rcx
movq 0x58(%r14), %rax
testq %rax, %rax
leaq 0x36565(%rip), %rsi     # 0x42a5b
cmoveq %rsi, %rax
movq %rax, (%rsp)
leaq 0x36312(%rip), %rsi     # 0x42817
movl %r13d, %r8d
movq %r12, %r9
xorl %eax, %eax
callq 0x7af1
movq 0x30(%rbx), %rdi
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rsi
movl 0x4(%rax), %edx
callq 0x7728
movq 0x30(%rbx), %rdi
movl $0xa, %esi
callq 0x768b
addq %r12, 0x38(%rbx)
addq %r12, 0x40(%rbx)
incq 0x48(%rbx)
jmp 0xc746
movq %rdi, -0x30(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
movq 0x20(%rbx), %rdi
leaq 0x36242(%rip), %rsi     # 0x427a2
callq 0x758f
leaq -0x60(%rbp), %r14
movq %r14, %rdi
callq 0x7508
movq 0x10(%r12), %rax
movq 0x8(%rax), %rsi
movl 0x4(%rax), %edx
movq %r14, %rdi
callq 0x7728
movq 0x20(%rbx), %rdi
leaq 0x36219(%rip), %rsi     # 0x427a9
callq 0x758f
movq 0x20(%rbx), %rdi
movq 0x10(%r12), %rax
movq 0x8(%rax), %rsi
movl 0x4(%rax), %edx
callq 0x7728
movq 0x20(%rbx), %rdi
movl $0xa, %esi
callq 0x768b
testl %r13d, %r13d
jle 0xc5d2
movq 0x20(%rbx), %rdi
leaq 0x361e9(%rip), %rsi     # 0x427b1
movl %r13d, %edx
xorl %eax, %eax
callq 0x7af1
movq 0x20(%rbx), %rdi
movq 0x10(%r12), %rax
movq 0x8(%rax), %rsi
movl 0x4(%rax), %edx
callq 0x7728
movq 0x20(%rbx), %rdi
movl $0x3a, %esi
callq 0x768b
movq 0x20(%rbx), %rdi
movl $0xa, %esi
callq 0x768b
movq -0x40(%rbp), %r14
movl (%r14), %eax
leal -0x400(%rax), %ecx
cmpl $0x20, %ecx
ja 0xc675
leaq 0x35bc4(%rip), %rdx     # 0x421e0
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
movq 0x20(%rbx), %rdi
leaq 0x3618d(%rip), %rsi     # 0x427bd
jmp 0xc6a5
movq (%rbx), %rdi
movq 0x20(%rbx), %r12
movq -0x38(%rbp), %rsi
movq %r14, %rdx
callq 0x2ad41
movq %r12, %rdi
leaq 0x3619e(%rip), %rsi     # 0x427ed
movq %rax, %rdx
xorl %eax, %eax
callq 0x7af1
jmp 0xc6bd
movq 0x20(%rbx), %rdi
leaq 0x3616e(%rip), %rsi     # 0x427d4
jmp 0xc6a5
movq 0x20(%rbx), %rdi
leaq 0x36151(%rip), %rsi     # 0x427c4
jmp 0xc6a5
cmpl $0x440, %eax            # imm = 0x440
jne 0xc864
movq 0x20(%rbx), %rdi
leaq 0x36141(%rip), %rsi     # 0x427cc
jmp 0xc6a5
movq 0x20(%rbx), %rdi
leaq 0x3614c(%rip), %rsi     # 0x427e4
jmp 0xc6a5
movq 0x20(%rbx), %rdi
leaq 0x36137(%rip), %rsi     # 0x427dc
callq 0x758f
movq 0x20(%rbx), %rsi
movq %rbx, %rdi
movq -0x30(%rbp), %rdx
movq %r14, %rcx
callq 0xc883
movq 0x60(%r15), %r12
movb $0x1, 0x48(%r12)
addq $0x50, %r12
leaq -0x70(%rbp), %r14
movq %r14, %rdi
callq 0x7508
movq -0x58(%rbp), %rdx
testq %rdx, %rdx
leaq 0x36376(%rip), %r15     # 0x42a5b
cmoveq %r15, %rdx
movq %r14, %rdi
leaq 0x36105(%rip), %rsi     # 0x427f8
xorl %eax, %eax
callq 0x7af1
movq %r12, %rdi
callq 0x7508
movq -0x68(%rbp), %rdx
testq %rdx, %rdx
cmoveq %r15, %rdx
movq %r12, %rdi
leaq 0x363a4(%rip), %rsi     # 0x42abb
leaq 0x36add(%rip), %rcx     # 0x431fb
xorl %eax, %eax
callq 0x7af1
leaq -0x60(%rbp), %rdi
callq 0x79f8
movq %r14, %rdi
callq 0x79f8
movq 0x20(%rbx), %rdi
leaq 0x36318(%rip), %rsi     # 0x42a59
callq 0x758f
movq -0x38(%rbp), %rsi
movq -0x50(%rbp), %rdx
incq %rdx
movslq 0x44(%rsi), %rax
cmpq %rax, %rdx
movl -0x44(%rbp), %r13d
jl 0xc40a
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x33c15(%rip), %rdi     # 0x4038d
leaq 0x35cf9(%rip), %rsi     # 0x42478
cmpl $0x4, %ecx
jne 0xc815
leaq 0x35f37(%rip), %rcx     # 0x426c6
movl $0xe1b, %edx            # imm = 0xE1B
callq 0x30a0
leaq 0x35fd6(%rip), %rdi     # 0x42776
leaq 0x35cd1(%rip), %rsi     # 0x42478
leaq 0x35f18(%rip), %rcx     # 0x426c6
movl $0xd4c, %edx            # imm = 0xD4C
callq 0x30a0
leaq 0x35f7c(%rip), %rdi     # 0x4273b
leaq 0x35cb2(%rip), %rsi     # 0x42478
leaq 0x35ef9(%rip), %rcx     # 0x426c6
movl $0xd49, %edx            # imm = 0xD49
callq 0x30a0
leaq 0x35f35(%rip), %rdi     # 0x42713
leaq 0x35c93(%rip), %rsi     # 0x42478
leaq 0x35eda(%rip), %rcx     # 0x426c6
movl $0xd48, %edx            # imm = 0xD48
callq 0x30a0
leaq 0x36008(%rip), %rdi     # 0x42805
leaq 0x35c74(%rip), %rsi     # 0x42478
leaq 0x35ebb(%rip), %rcx     # 0x426c6
movl $0xdec, %edx            # imm = 0xDEC
callq 0x30a0
leaq 0x35eaa(%rip), %rcx     # 0x426c6
movl $0xe1f, %edx            # imm = 0xE1F
callq 0x30a0
leaq 0x41f7b(%rip), %rdi     # 0x4e7a8
leaq 0x35c44(%rip), %rsi     # 0x42478
leaq 0x35e8b(%rip), %rcx     # 0x426c6
movl $0xd41, %edx            # imm = 0xD41
callq 0x30a0
leaq 0x35eb5(%rip), %rdi     # 0x42701
leaq 0x35c25(%rip), %rsi     # 0x42478
leaq 0x35e6c(%rip), %rcx     # 0x426c6
movl $0xd42, %edx            # imm = 0xD42
callq 0x30a0
leaq 0x33b22(%rip), %rdi     # 0x4038d
leaq 0x35c06(%rip), %rsi     # 0x42478
leaq 0x35e4d(%rip), %rcx     # 0x426c6
movl $0xdc7, %edx            # imm = 0xDC7
callq 0x30a0