// File: /workspace/github_c_makefile_star2_O3/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 0xdcd4(%rip), %r14      # 0x14568
testq %r14, %r14
je 0x68b5
movq %rsi, %rbx
movq 0x18(%r14), %rdi
movq %r15, %rsi
callq 0x1100
testl %eax, %eax
je 0x68cd
movq 0x8(%r14), %r14
testq %r14, %r14
jne 0x689c
leaq 0x8059(%rip), %rdi      # 0xe915
movq %r15, %rsi
xorl %eax, %eax
callq 0x10b0
xorl %edi, %edi
callq 0x11b0
movl $0x400, %edi            # imm = 0x400
movl $0x1, %esi
callq 0x10f0
movq %rax, %r12
movl $0x400, %edi            # imm = 0x400
movl $0x1, %esi
callq 0x10f0
movq %rax, %r13
movq 0x20(%r14), %r14
movq %r14, %rdi
callq 0x1090
leaq 0x1(%rax), %rdi
movl $0x1, %esi
callq 0x10f0
movq %rax, 0x18(%rsp)
movq %rax, %rdi
movq %r14, %rsi
callq 0x1060
testq %rbx, %rbx
movq %r15, 0x20(%rsp)
je 0x6952
movq %rbx, %rdi
callq 0x1090
testq %rax, %rax
je 0x6952
incq %rax
movl $0x1, %esi
movq %rax, %rdi
callq 0x10f0
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1060
jmp 0x6955
xorl %r14d, %r14d
xorl %ebx, %ebx
xorl %ebp, %ebp
xorl %r15d, %r15d
movl %ebx, 0xc(%rsp)
movq %r13, %rbx
testq %r14, %r14
je 0x69a7
movq %r14, %rdi
movq %r12, %rsi
callq 0x1fbb
movq %r14, %rdi
movq %r12, %rsi
callq 0xcaa7
movq %r12, %rdi
callq 0x7b63
movq %rax, %r13
movq %rax, %rdi
callq 0x137c
movq %rax, %r15
movq %r13, %rdi
callq 0x1030
movq %r14, %rdi
movq %r12, %rsi
callq 0xcaa7
movq 0x18(%rsp), %rdi
movq %r12, %rsi
callq 0x1fbb
movzbl (%r12), %eax
cmpl $0x5b, %eax
je 0x6a2b
cmpl $0x7b, %eax
movq %rbx, %r13
je 0x6a1b
testl %eax, %eax
je 0x6af9
movq %r12, %rdi
leaq 0x7f21(%rip), %rsi      # 0xe8f9
callq 0x1100
testl %eax, %eax
movl 0xc(%rsp), %ebx
je 0x6a9d
movq %r12, %rdi
leaq 0x7616(%rip), %rsi      # 0xe009
callq 0x1100
testl %eax, %eax
je 0x6aa4
movq %r12, %rdi
leaq 0x8847(%rip), %rsi      # 0xf251
callq 0x1100
testl %eax, %eax
movl $0x4, %eax
cmovel %eax, %ebx
jmp 0x6a3e
movq %r12, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x64b0
jmp 0x6a3c
movq %r12, %rdi
movq %r14, %rsi
movq %rbx, %r13
movq %rbx, %rdx
callq 0x66b5
movl %eax, %ebx
testq %r15, %r15
je 0x6a8b
cmpq $0x0, 0x10(%rsp)
je 0x6a83
cmpb $0x0, (%r13)
je 0x6a6d
movq %r13, %rdi
leaq 0x7eaa(%rip), %rsi      # 0xe906
movq %r13, %rdx
movl %ebx, %ecx
movq %r15, %r8
xorl %eax, %eax
callq 0x11a0
jmp 0x6a83
movq %r13, %rdi
leaq 0x7e94(%rip), %rsi      # 0xe90b
movl %ebx, %edx
movq %r15, %rcx
xorl %eax, %eax
callq 0x11a0
movq %r15, %rdi
callq 0x1030
addl %ebx, %ebp
movq 0x18(%rsp), %rax
cmpb $0x0, (%rax)
jne 0x695c
jmp 0x6aab
movl $0x1, %ebx
jmp 0x6a3e
movl $0x2, %ebx
jmp 0x6a3e
movq %r12, %rdi
callq 0x1030
movq 0x18(%rsp), %rdi
callq 0x1030
cmpq $0x0, 0x10(%rsp)
movq 0x20(%rsp), %r15
je 0x6b7c
movzbl (%r15), %eax
cmpl $0x25, %eax
je 0x6b0f
xorl %r14d, %r14d
cmpl $0x40, %eax
jne 0x6b31
leaq 0x7caa(%rip), %rbx      # 0xe790
movq %rbx, %rdi
movq %r15, %rsi
callq 0x562e
movl %eax, %r14d
movq %rbx, %rdi
jmp 0x6b27
movq %r12, %rdi
callq 0x1030
cmpq $0x0, 0x18(%rsp)
movl 0xc(%rsp), %ebx
jne 0x6ab3
jmp 0x6abd
leaq 0xd632(%rip), %rbx      # 0x14148
movq (%rbx), %rdi
movq %r15, %rsi
callq 0x562e
movl %eax, %r14d
movq (%rbx), %rdi
movq %r15, %rsi
callq 0x5696
movl %eax, %ebx
testl %ebx, %ebx
jle 0x6b50
leaq 0x7e0b(%rip), %rsi      # 0xe947
movq 0x10(%rsp), %rdi
movl %r14d, %edx
movq %r13, %rcx
xorl %eax, %eax
callq 0x11a0
jmp 0x6b7c
movq %r15, %rdi
callq 0x7b63
movq %rax, %rbx
leaq 0x7df9(%rip), %rsi      # 0xe95b
movq 0x10(%rsp), %rdi
movq %rax, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x11a0
movq %rbx, %rdi
callq 0x1030
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq