// File: /workspace/github_c_makefile_star2_O2/markdryan[P]subtilis/frontend/array_type.c
// Function: subtilis_array_size_calc
subtilis_exp_t *subtilis_array_size_calc(subtilis_parser_t *p,
					 const char *var_name,
					 subtilis_exp_t **e, size_t index_count,
					 subtilis_error_t *err)
{
	size_t i;
	bool non_const_dim = false;
	subtilis_exp_t *sizee = NULL;
	subtilis_exp_t *edup = NULL;
	subtilis_exp_t *one = NULL;
	int64_t array_size = 1;

	/*
	 * TODO: This whole thing is 32 bit specific.  At least the
	 * pointer needs to be platform specific although we may still
	 * keep maximum array sizes to 32 bits.
	 */

	/* TODO:
	 * I don't see any way to detect whether the array dimensions
	 * overflow 32 bits. For constants we can use 64 bit integers to
	 * store the size. This would then limit us to using 32 bits for
	 * the array size, even on 64 bit builds (unless we resort to
	 * instrinsics) as there are no 128 bit integers in C.  For the
	 * ARM2 backend, there is no way to detect overflow of dynamic
	 * vars.  This could be done on StrongARM using UMULL.  Perhaps
	 * we could execute different code on SA here.  For now, be
	 * careful with those array bounds!
	 */

	for (i = 0; i < index_count; i++) {
		if (e[i]->type.type != SUBTILIS_TYPE_CONST_INTEGER) {
			non_const_dim = true;
			continue;
		}
		if (e[i]->exp.ir_op.integer < 0) {
			subtilis_error_bad_dim(err, var_name,
					       p->l->stream->name, p->l->line);
			return NULL;
		}
		array_size = array_size * (e[i]->exp.ir_op.integer + 1);
	}

	if (array_size > (int64_t)0x7ffffff) {
		subtilis_error_bad_dim(err, var_name, p->l->stream->name,
				       p->l->line);
		return NULL;
	}

	sizee = subtilis_exp_new_int32((int32_t)array_size, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return NULL;

	if (non_const_dim) {
		sizee = subtilis_type_if_exp_to_var(p, sizee, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;

		for (i = 0; i < index_count; i++) {
			if (e[i]->type.type != SUBTILIS_TYPE_CONST_INTEGER) {
				prv_check_dynamic_dim(p, e[i], 0, NULL, err);
				if (err->type != SUBTILIS_ERROR_OK)
					goto cleanup;
				edup = subtilis_type_if_dup(e[i], err);
				if (err->type != SUBTILIS_ERROR_OK)
					goto cleanup;
				one = subtilis_exp_new_int32(1, err);
				if (err->type != SUBTILIS_ERROR_OK)
					goto cleanup;
				edup = subtilis_type_if_add(p, edup, one, err);
				if (err->type != SUBTILIS_ERROR_OK)
					goto cleanup;
				sizee =
				    subtilis_type_if_mul(p, sizee, edup, err);
				if (err->type != SUBTILIS_ERROR_OK)
					goto cleanup;
			}
		}
	}

	return sizee;

cleanup:

	subtilis_exp_delete(sizee);

	return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %r8, %rbx
movq %rcx, %r14
movq %rdx, %r12
movq %rsi, %rdx
movq %rdi, %rbp
pushq $0x1
popq %rdi
xorl %eax, %eax
xorl %r15d, %r15d
cmpq %rax, %r14
je 0x3fa34
movq (%r12,%rax,8), %rcx
cmpl $0x1, (%rcx)
jne 0x3fa2c
movl 0x148(%rcx), %ecx
testl %ecx, %ecx
js 0x3faa1
incq %rcx
imulq %rcx, %rdi
jmp 0x3fa2f
movb $0x1, %r15b
incq %rax
jmp 0x3fa0b
cmpq $0x8000000, %rdi        # imm = 0x8000000
jl 0x3fa64
movq (%rbp), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x2ff42(%rip), %rax     # 0x6f992
leaq 0x2e36e(%rip), %rcx     # 0x6ddc5
pushq $0x38
popq %rsi
movq %rbx, %rdi
pushq $0x62d                  # imm = 0x62D
jmp 0x3fac6
movq %rbx, %rsi
callq 0x2a369
xorl %r13d, %r13d
cmpl $0x0, (%rbx)
sete %cl
cmoveq %rax, %r13
andb %r15b, %cl
cmpb $0x1, %cl
jne 0x3fad1
movq %rbp, %rdi
movq %rax, %rsi
movq %rbx, %rdx
callq 0x3415f
movq %rax, %r13
cmpl $0x0, (%rbx)
je 0x3fae3
movq %r13, %rdi
callq 0x29d88
jmp 0x3face
movq (%rbp), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x2fede(%rip), %rax     # 0x6f992
leaq 0x2e30a(%rip), %rcx     # 0x6ddc5
pushq $0x38
popq %rsi
movq %rbx, %rdi
pushq $0x625                  # imm = 0x625
pushq %rax
callq 0x140cd
popq %rax
popq %rcx
xorl %r13d, %r13d
movq %r13, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r15d, %r15d
cmpq %r15, %r14
je 0x3fad1
movq (%r12,%r15,8), %rsi
cmpl $0x1, (%rsi)
je 0x3fb73
movq %rbp, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
movq %rbx, %r8
callq 0x40ee4
cmpl $0x0, (%rbx)
jne 0x3fa97
movq %r15, (%rsp)
movq (%r12,%r15,8), %rdi
movq %rbx, %rsi
callq 0x341c4
cmpl $0x0, (%rbx)
jne 0x3fa97
movq %rax, %r15
pushq $0x1
popq %rdi
movq %rbx, %rsi
callq 0x2a369
cmpl $0x0, (%rbx)
jne 0x3fa97
movq %rbp, %rdi
movq %r15, %rsi
movq %rax, %rdx
movq %rbx, %rcx
callq 0x34b18
cmpl $0x0, (%rbx)
jne 0x3fa97
movq %rbp, %rdi
movq %r13, %rsi
movq %rax, %rdx
movq %rbx, %rcx
callq 0x34bfb
movq %rax, %r13
cmpl $0x0, (%rbx)
movq (%rsp), %r15
jne 0x3fa97
incq %r15
jmp 0x3fae6