// File: /workspace/github_c_makefile_star2_O3/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 %rdi, %rbp
testq %rcx, %rcx
je 0x41fec
movq %rsi, %rdx
movl $0x1, %edi
xorl %r15d, %r15d
xorl %eax, %eax
movq (%r12,%rax,8), %rcx
cmpl $0x1, (%rcx)
jne 0x41faf
movl 0x148(%rcx), %ecx
testl %ecx, %ecx
js 0x42031
incq %rcx
imulq %rcx, %rdi
jmp 0x41fb2
movb $0x1, %r15b
incq %rax
cmpq %rax, %r14
jne 0x41f8f
cmpq $0x8000000, %rdi        # imm = 0x8000000
jl 0x41ff4
movq (%rbp), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x33a30(%rip), %rax     # 0x75a06
leaq 0x31e38(%rip), %rcx     # 0x73e15
movq %rbx, %rdi
movl $0x38, %esi
pushq $0x62d                  # imm = 0x62D
jmp 0x42058
movl $0x1, %edi
xorl %r15d, %r15d
movq %rbx, %rsi
callq 0x2b7af
xorl %r13d, %r13d
cmpl $0x0, (%rbx)
sete %cl
cmoveq %rax, %r13
andb %r15b, %cl
cmpb $0x1, %cl
jne 0x42065
movq %rbp, %rdi
movq %rax, %rsi
movq %rbx, %rdx
callq 0x36119
movq %rax, %r13
cmpl $0x0, (%rbx)
je 0x42077
movq %r13, %rdi
callq 0x2b166
jmp 0x42062
movq (%rbp), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x339c2(%rip), %rax     # 0x75a06
leaq 0x31dca(%rip), %rcx     # 0x73e15
movq %rbx, %rdi
movl $0x38, %esi
pushq $0x625                  # imm = 0x625
pushq %rax
callq 0x143da
addq $0x10, %rsp
xorl %r13d, %r13d
movq %r13, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %r14, %r14
je 0x42065
xorl %r15d, %r15d
movq (%r12,%r15,8), %rsi
cmpl $0x1, (%rsi)
je 0x42103
movq %rbp, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
movq %rbx, %r8
callq 0x4352b
cmpl $0x0, (%rbx)
jne 0x42027
movq (%r12,%r15,8), %rdi
movq %rbx, %rsi
callq 0x36182
cmpl $0x0, (%rbx)
jne 0x42027
movq %rax, (%rsp)
movl $0x1, %edi
movq %rbx, %rsi
callq 0x2b7af
cmpl $0x0, (%rbx)
jne 0x42027
movq %rbp, %rdi
movq (%rsp), %rsi
movq %rax, %rdx
movq %rbx, %rcx
callq 0x36b28
cmpl $0x0, (%rbx)
jne 0x42027
movq %rbp, %rdi
movq %r13, %rsi
movq %rax, %rdx
movq %rbx, %rcx
callq 0x36bc7
movq %rax, %r13
cmpl $0x0, (%rbx)
jne 0x42027
incq %r15
cmpq %r15, %r14
jne 0x4207f
jmp 0x42065