// File: /workspace/github_c_makefile_star2_O2/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 %rsi, %r14
movq %rdi, %r13
leaq 0xcfb5(%rip), %r15      # 0x13568
movq (%r15), %r15
testq %r15, %r15
je 0x687e
movq 0x18(%r15), %rdi
movq %r13, %rsi
callq 0x1100
testl %eax, %eax
je 0x65d5
addq $0x8, %r15
jmp 0x65b3
pushq $0x1
popq %rbx
movl $0x400, %edi            # imm = 0x400
movq %rbx, %rsi
callq 0x10f0
movq %rax, %r12
movl $0x400, %edi            # imm = 0x400
movq %rbx, %rsi
callq 0x10f0
movq %rax, 0x8(%rsp)
movq 0x20(%r15), %r15
movq %r15, %rdi
callq 0x1090
leaq 0x1(%rax), %rdi
movq %rbx, %rsi
callq 0x10f0
movq %rax, 0x18(%rsp)
movq %rax, %rdi
movq %r15, %rsi
callq 0x1060
testq %r14, %r14
movq %r13, 0x20(%rsp)
je 0x6657
movq %r14, %rdi
callq 0x1090
testq %rax, %rax
je 0x6657
incq %rax
pushq $0x1
popq %rsi
movq %rax, %rdi
callq 0x10f0
movq %rax, %rbx
movq %rax, %rdi
movq %r14, %rsi
callq 0x1060
jmp 0x6659
xorl %ebx, %ebx
xorl %r13d, %r13d
xorl %ebp, %ebp
xorl %r14d, %r14d
testq %rbx, %rbx
je 0x66a5
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1ec1
movq %rbx, %rdi
movq %r12, %rsi
callq 0xc4ad
movq %r12, %rdi
callq 0x7706
movq %rax, %r15
movq %rax, %rdi
callq 0x1372
movq %rax, %r14
movq %r15, %rdi
callq 0x1030
movq %rbx, %rdi
movq %r12, %rsi
callq 0xc4ad
movq 0x18(%rsp), %rdi
movq %r12, %rsi
callq 0x1ec1
movzbl (%r12), %eax
cmpl $0x5b, %eax
je 0x6726
cmpl $0x7b, %eax
je 0x6714
testl %eax, %eax
je 0x679b
movq %r12, %rdi
leaq 0x7206(%rip), %rsi      # 0xd8d9
callq 0x1100
pushq $0x1
popq %r15
testl %eax, %eax
je 0x6739
movq %r12, %rdi
leaq 0x691f(%rip), %rsi      # 0xd009
callq 0x1100
pushq $0x2
popq %r15
testl %eax, %eax
je 0x6739
movq %r12, %rdi
leaq 0x7b30(%rip), %rsi      # 0xe231
callq 0x1100
testl %eax, %eax
pushq $0x4
popq %rax
cmovel %eax, %r13d
movl %r13d, %r15d
jmp 0x6739
movq %r12, %rdi
movq %rbx, %rsi
movq 0x8(%rsp), %rdx
callq 0x61eb
jmp 0x6736
movq %r12, %rdi
movq %rbx, %rsi
movq 0x8(%rsp), %rdx
callq 0x63e2
movl %eax, %r15d
testq %r14, %r14
je 0x6785
cmpq $0x0, 0x10(%rsp)
je 0x677d
movq 0x8(%rsp), %rdi
cmpb $0x0, (%rdi)
je 0x6769
leaq 0x718f(%rip), %rsi      # 0xd8e6
movq %rdi, %rdx
movl %r15d, %ecx
movq %r14, %r8
xorl %eax, %eax
callq 0x11a0
jmp 0x677d
leaq 0x717b(%rip), %rsi      # 0xd8eb
movl %r15d, %edx
movq %r14, %rcx
xorl %eax, %eax
callq 0x11a0
movq %r14, %rdi
callq 0x1030
addl %r15d, %ebp
movq 0x18(%rsp), %rax
cmpb $0x0, (%rax)
movl %r15d, %r13d
jne 0x6661
jmp 0x679e
movl %r13d, %r15d
movq %r12, %rdi
callq 0x1030
movq 0x18(%rsp), %rdi
callq 0x1030
cmpq $0x0, 0x10(%rsp)
je 0x686d
movq 0x20(%rsp), %r12
movzbl (%r12), %eax
cmpl $0x25, %eax
je 0x67fb
xorl %ebx, %ebx
cmpl $0x40, %eax
movq 0x8(%rsp), %r13
jne 0x6822
leaq 0x6f92(%rip), %r14      # 0xd770
movq %r14, %rdi
movq %r12, %rsi
callq 0x53de
movl %eax, %ebx
movq %r14, %rdi
movq %r12, %rsi
callq 0x5442
movl %eax, %r15d
jmp 0x6822
leaq 0xc946(%rip), %r14      # 0x13148
movq (%r14), %rdi
movq %r12, %rsi
callq 0x53de
movl %eax, %ebx
movq (%r14), %rdi
movq %r12, %rsi
callq 0x5442
movl %eax, %r15d
movq 0x8(%rsp), %r13
testl %r15d, %r15d
jle 0x6841
leaq 0x70f9(%rip), %rsi      # 0xd927
movq 0x10(%rsp), %rdi
movl %ebx, %edx
movq %r13, %rcx
xorl %eax, %eax
callq 0x11a0
jmp 0x686d
movq %r12, %rdi
callq 0x7706
movq %rax, %rbx
leaq 0x70e8(%rip), %rsi      # 0xd93b
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
leaq 0x7070(%rip), %rdi      # 0xd8f5
movq %r13, %rsi
xorl %eax, %eax
callq 0x10b0
xorl %edi, %edi
callq 0x11b0