// File: /workspace/github_c_makefile_star2_O1/aquaxis[P]synverll/src/parser_ir_memory.c
// Function: get_memory_size
int get_memory_size(char *label, char *ptr, char *rslt_ptr)
{
	MEMORY_TREE *now_memory_tree;
	int size = 0;
	int total = 0;
	int base_adrs = 0;
	char *buf = NULL;
	char *token = NULL;
	char *buf_ptr = NULL;
	char *ptr_cnt = NULL;
	char *temp = NULL;
	char *str = NULL;

	now_memory_tree = memory_tree_top;
    while(now_memory_tree != NULL){
		if(!strcmp(now_memory_tree->label, label)) break;
        now_memory_tree = now_memory_tree->next_ptr;
    }
	if(now_memory_tree == NULL){
		printf("[ABORT] get_memory_size(): unknown memory = \"%s\"\n", label);
		exit(0);
	}

	token		= calloc(STR_MAX,1);
	temp		= calloc(STR_MAX,1);
	buf			= calloc(strlen(now_memory_tree->type)+1,1);
	strcpy(buf, now_memory_tree->type);

	if(ptr != NULL){
		if(strlen(ptr) > 0){
			buf_ptr = calloc(strlen(ptr)+1,1);
			strcpy(buf_ptr, ptr);
		}
	}

	size = 0;
	total = 0;
	do{
		/*
		 * ptrからアドレスポインタを取得する
		 */
		if(buf_ptr != NULL){
			// Type
			get_irtype_llvm(buf_ptr, token);
			// Name
			get_token_llvm(buf_ptr, token);
			str = regalloc(token);
			ptr_cnt = charalloc(str);
			free(str);
			// ,
			get_token_llvm(buf_ptr, token);
		}

		get_irtype_llvm(buf, token);
		if(strlen(token) == 0) break;

		if(token[0] == '['){
			size = __get_array_size(token, buf_ptr, temp);
		}else if(token[0] == '{'){
			size = __get_struct_size(token, buf_ptr, temp);
		}else if(!strcmp(token, "i8")){
			size = 1;
		}else if(!strcmp(token, "i16")){
			size = 2;
		}else if(!strcmp(token, "i32")){
			size = 4;
		}
		total += size;

		if(ptr_cnt != NULL){
			if(rslt_ptr != NULL){
				if(strlen(temp) > 0){
					sprintf(temp, "%s + (%d * %s)", temp, size, ptr_cnt);
				}else{
					sprintf(temp, "(%d * %s)", size, ptr_cnt);
				}
			}
			free(ptr_cnt);
		}

	}while(strlen(buf) > 0);
	if(token != NULL) free(token);
	if(buf != NULL) free(buf);

	if(rslt_ptr != NULL){
		if(label[0] == '@'){
			// グローバルメモリ
			base_adrs = get_adrs_memmap("GLOBAL", label);
			size = get_size_memmap("GLOBAL", label);
		}else if(label[0] == '%'){
			// ローカルメモリ
			base_adrs = get_adrs_memmap(module_name, label);
			size = get_size_memmap(module_name, label);
		}

		if(size > 0){
			sprintf(rslt_ptr, "__gm_base + %d + %s", base_adrs, temp);
		}else{
			buf = regalloc(label);
			sprintf(rslt_ptr, "%s + %s", buf, temp);
			free(buf);
		}
	}

	return total;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, 0x10(%rsp)
movq %rdi, %r15
movq 0xdbc2(%rip), %r14      # 0x14578
testq %r14, %r14
je 0x69d7
movq %rsi, %rbx
movq 0x18(%r14), %rdi
movq %r15, %rsi
callq 0x1110
testl %eax, %eax
je 0x69ef
movq 0x8(%r14), %r14
testq %r14, %r14
jne 0x69be
leaq 0x7f44(%rip), %rdi      # 0xe922
movq %r15, %rsi
xorl %eax, %eax
callq 0x10b0
xorl %edi, %edi
callq 0x11c0
movl $0x400, %edi            # imm = 0x400
movl $0x1, %esi
callq 0x1100
movq %rax, %r12
movl $0x400, %edi            # imm = 0x400
movl $0x1, %esi
callq 0x1100
movq %rax, %r13
movq 0x20(%r14), %r14
movq %r14, %rdi
callq 0x1090
leaq 0x1(%rax), %rdi
movl $0x1, %esi
callq 0x1100
movq %rax, 0x18(%rsp)
movq %rax, %rdi
movq %r14, %rsi
callq 0x1060
testq %rbx, %rbx
movq %r15, 0x20(%rsp)
je 0x6a74
movq %rbx, %rdi
callq 0x1090
testq %rax, %rax
je 0x6a74
incq %rax
movl $0x1, %esi
movq %rax, %rdi
callq 0x1100
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1060
jmp 0x6a77
xorl %r14d, %r14d
xorl %ebx, %ebx
xorl %ebp, %ebp
xorl %r15d, %r15d
movl %ebx, 0xc(%rsp)
movq %r13, %rbx
testq %r14, %r14
je 0x6ac9
movq %r14, %rdi
movq %r12, %rsi
callq 0x201a
movq %r14, %rdi
movq %r12, %rsi
callq 0xcc26
movq %r12, %rdi
callq 0x7c87
movq %rax, %r13
movq %rax, %rdi
callq 0x1395
movq %rax, %r15
movq %r13, %rdi
callq 0x1030
movq %r14, %rdi
movq %r12, %rsi
callq 0xcc26
movq 0x18(%rsp), %rdi
movq %r12, %rsi
callq 0x201a
movzbl (%r12), %eax
cmpl $0x5b, %eax
je 0x6b4d
cmpl $0x7b, %eax
movq %rbx, %r13
je 0x6b3d
testl %eax, %eax
je 0x6bcd
movq %r12, %rdi
leaq 0x7512(%rip), %rsi      # 0xe00c
callq 0x1110
testl %eax, %eax
movl 0xc(%rsp), %ebx
je 0x6bbf
movq %r12, %rdi
leaq 0x74fa(%rip), %rsi      # 0xe00f
callq 0x1110
testl %eax, %eax
je 0x6bc6
movq %r12, %rdi
leaq 0x8731(%rip), %rsi      # 0xf25d
callq 0x1110
testl %eax, %eax
movl $0x4, %eax
cmovel %eax, %ebx
jmp 0x6b60
movq %r12, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x65b8
jmp 0x6b5e
movq %r12, %rdi
movq %r14, %rsi
movq %rbx, %r13
movq %rbx, %rdx
callq 0x67d5
movl %eax, %ebx
testq %r15, %r15
je 0x6bad
cmpq $0x0, 0x10(%rsp)
je 0x6ba5
cmpb $0x0, (%r13)
je 0x6b8f
movq %r13, %rdi
leaq 0x7d95(%rip), %rsi      # 0xe913
movq %r13, %rdx
movl %ebx, %ecx
movq %r15, %r8
xorl %eax, %eax
callq 0x11b0
jmp 0x6ba5
movq %r13, %rdi
leaq 0x7d7f(%rip), %rsi      # 0xe918
movl %ebx, %edx
movq %r15, %rcx
xorl %eax, %eax
callq 0x11b0
movq %r15, %rdi
callq 0x1030
addl %ebx, %ebp
movq 0x18(%rsp), %rax
cmpb $0x0, (%rax)
jne 0x6a7e
jmp 0x6bd1
movl $0x1, %ebx
jmp 0x6b60
movl $0x2, %ebx
jmp 0x6b60
movl 0xc(%rsp), %ebx
movq %r12, %rdi
callq 0x1030
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x6be8
callq 0x1030
cmpq $0x0, 0x10(%rsp)
movq 0x20(%rsp), %r15
je 0x6c91
movzbl (%r15), %eax
cmpl $0x25, %eax
je 0x6c24
xorl %r14d, %r14d
cmpl $0x40, %eax
jne 0x6c46
leaq 0x7b8f(%rip), %rbx      # 0xe7a0
movq %rbx, %rdi
movq %r15, %rsi
callq 0x5725
movl %eax, %r14d
movq %rbx, %rdi
jmp 0x6c3c
leaq 0xd52d(%rip), %rbx      # 0x14158
movq (%rbx), %rdi
movq %r15, %rsi
callq 0x5725
movl %eax, %r14d
movq (%rbx), %rdi
movq %r15, %rsi
callq 0x578d
movl %eax, %ebx
testl %ebx, %ebx
jle 0x6c65
leaq 0x7d03(%rip), %rsi      # 0xe954
movq 0x10(%rsp), %rdi
movl %r14d, %edx
movq %r13, %rcx
xorl %eax, %eax
callq 0x11b0
jmp 0x6c91
movq %r15, %rdi
callq 0x7c87
movq %rax, %rbx
leaq 0x7cf1(%rip), %rsi      # 0xe968
movq 0x10(%rsp), %rdi
movq %rax, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x11b0
movq %rbx, %rdi
callq 0x1030
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq