// File: /workspace/github_c_makefile_star2_O1/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
movl $0x1, %edi
testq %rcx, %rcx
je 0x420ed
xorl %r15d, %r15d
xorl %eax, %eax
movq (%r12,%rax,8), %rcx
cmpl $0x1, (%rcx)
jne 0x420dc
movl 0x148(%rcx), %ecx
testl %ecx, %ecx
js 0x4215f
incq %rcx
imulq %rcx, %rdi
jmp 0x420df
movb $0x1, %r15b
incq %rax
cmpq %rax, %r14
jne 0x420bc
xorb $0x1, %r15b
jmp 0x420f0
movb $0x1, %r15b
cmpq $0x8000000, %rdi        # imm = 0x8000000
jl 0x42122
movq (%rbp), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x338b6(%rip), %rax     # 0x759c2
leaq 0x31ce2(%rip), %rcx     # 0x73df5
movq %rbx, %rdi
movl $0x38, %esi
pushq $0x62d                  # imm = 0x62D
jmp 0x42186
movq %rbx, %rsi
callq 0x2bb29
xorl %r13d, %r13d
cmpl $0x0, (%rbx)
setne %cl
cmoveq %rax, %r13
orb %r15b, %cl
testb $0x1, %cl
jne 0x42193
movq %rbp, %rdi
movq %rax, %rsi
movq %rbx, %rdx
callq 0x36269
movq %rax, %r13
cmpl $0x0, (%rbx)
je 0x421a5
movq %r13, %rdi
callq 0x2b4c3
jmp 0x42190
movq (%rbp), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x33850(%rip), %rax     # 0x759c2
leaq 0x31c7c(%rip), %rcx     # 0x73df5
movq %rbx, %rdi
movl $0x38, %esi
pushq $0x625                  # imm = 0x625
pushq %rax
callq 0x1442a
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 0x42193
xorl %r15d, %r15d
movq (%r12,%r15,8), %rsi
cmpl $0x1, (%rsi)
je 0x42231
movq %rbp, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
movq %rbx, %r8
callq 0x435d4
cmpl $0x0, (%rbx)
jne 0x42155
movq (%r12,%r15,8), %rdi
movq %rbx, %rsi
callq 0x362d2
cmpl $0x0, (%rbx)
jne 0x42155
movq %rax, (%rsp)
movl $0x1, %edi
movq %rbx, %rsi
callq 0x2bb29
cmpl $0x0, (%rbx)
jne 0x42155
movq %rbp, %rdi
movq (%rsp), %rsi
movq %rax, %rdx
movq %rbx, %rcx
callq 0x36c85
cmpl $0x0, (%rbx)
jne 0x42155
movq %rbp, %rdi
movq %r13, %rsi
movq %rax, %rdx
movq %rbx, %rcx
callq 0x36d24
movq %rax, %r13
cmpl $0x0, (%rbx)
jne 0x42155
incq %r15
cmpq %r15, %r14
jne 0x421ad
jmp 0x42193