// File: /workspace/github_c_makefile_star2_O3/markdryan[P]subtilis/frontend/parser_rec.c
// Function: prv_rec_init
static void prv_rec_init(subtilis_parser_t *p, subtilis_token_t *t,
			 const subtilis_type_t *type, size_t mem_reg,
			 size_t loc, bool push, subtilis_error_t *err)
{
	const char *tbuf;
	size_t id;
	const subtilis_type_rec_t *rec;
	const subtilis_type_t *field_type;
	uint32_t offset;
	subtilis_exp_t *e;

	tbuf = subtilis_token_get_text(t);
	if (!((t->type == SUBTILIS_TOKEN_OPERATOR) && !strcmp(tbuf, "("))) {
		/*
		 * We're copying one REC into another.  There's no
		 * initialisation list here.
		 */
		e = subtilis_parser_priority7(p, t, err);
		if (err->type != SUBTILIS_ERROR_OK)
			return;

		e = subtilis_type_if_coerce_type(p, e, type, err);
		if (err->type != SUBTILIS_ERROR_OK)
			return;

		subtilis_rec_type_copy(p, type, mem_reg, loc, e->exp.ir_op.reg,
				       true, err);
		subtilis_exp_delete(e);
		if (err->type != SUBTILIS_ERROR_OK)
			return;

		if (push && subtilis_type_rec_need_deref(type))
			subtilis_reference_type_push_reference(p, type, mem_reg,
							       loc, err);
		return;
	}

	subtilis_lexer_get(p->l, t, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	tbuf = subtilis_token_get_text(t);
	if ((t->type == SUBTILIS_TOKEN_OPERATOR) && !strcmp(tbuf, ")")) {
		subtilis_rec_type_zero(p, type, mem_reg, loc, push, err);
		if (err->type != SUBTILIS_ERROR_OK)
			return;

		subtilis_lexer_get(p->l, t, err);
		return;
	}

	rec = &type->params.rec;

	/*
	 * TODO: All of this will need to be re-written as will the existing
	 * code to initialise arrays.  Basically, we want parse the expressions
	 * build up some sort of tree, and then anaylse the tree to figure out
	 * the most efficient way of initialising things.  This sort of thing
	 * will be easier though when have a two stage parser.  Othwerwise,
	 * we'll end up loading using too many regs.  We could bomb out if
	 * we hit a variable.  Can I do this withouth the re-write?
	 */

	for (id = 0; id < rec->num_fields; id++) {
		field_type = &rec->field_types[id];
		offset = subtilis_type_rec_field_offset_id(rec, id);
		if (subtilis_type_if_is_numeric(field_type) ||
		    field_type->type == SUBTILIS_TYPE_FN) {
			e = subtilis_parser_priority7(p, t, err);
			if (err->type != SUBTILIS_ERROR_OK)
				return;
			e = subtilis_type_if_coerce_type(p, e, field_type, err);
			if (err->type != SUBTILIS_ERROR_OK)
				return;
			subtilis_type_if_assign_to_mem(p, mem_reg, loc + offset,
						       e, err);
		} else if (field_type->type == SUBTILIS_TYPE_STRING) {
			e = subtilis_parser_priority7(p, t, err);
			if (err->type != SUBTILIS_ERROR_OK)
				return;
			subtilis_string_type_new_field_ref(
			    p, field_type, mem_reg, loc + offset, e, err);
		} else if (subtilis_type_if_is_array(field_type) ||
			   subtilis_type_if_is_vector(field_type)) {
			prv_array_field_init(p, t, &rec->fields[id], field_type,
					     mem_reg, loc + offset, err);
		} else if (field_type->type == SUBTILIS_TYPE_REC) {
			prv_rec_init(p, t, field_type, mem_reg, loc + offset,
				     false, err);
		} else {
			subtilis_error_set_assertion_failed(err);
			return;
		}

		if (err->type != SUBTILIS_ERROR_OK)
			return;

		tbuf = subtilis_token_get_text(t);
		if (t->type == SUBTILIS_TOKEN_OPERATOR) {
			if (!strcmp(tbuf, ",")) {
				if (id < rec->num_fields - 1) {
					subtilis_lexer_get(p->l, t, err);
					if (err->type != SUBTILIS_ERROR_OK)
						return;
				}
				continue;
			}
			if (!strcmp(tbuf, ")"))
				break;
		}
		subtilis_error_set_expected(err, ", or )", tbuf,
					    p->l->stream->name, p->l->line);
		return;
	}

	if (strcmp(tbuf, ")")) {
		subtilis_error_set_expected(err, ")", tbuf, p->l->stream->name,
					    p->l->line);
		return;
	}

	for (id = id + 1; id < rec->num_fields; id++) {
		field_type = &rec->field_types[id];
		offset = subtilis_type_rec_field_offset_id(rec, id);
		subtilis_rec_type_init_field(p, field_type, &rec->fields[id],
					     mem_reg, offset + loc, false, err);
		if (err->type != SUBTILIS_ERROR_OK)
			return;
	}

	if (push && subtilis_type_rec_need_deref(type)) {
		subtilis_reference_type_push_reference(p, type, mem_reg, loc,
						       err);
		if (err->type != SUBTILIS_ERROR_OK)
			return;
	}

	subtilis_lexer_get(p->l, t, err);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %r9d, 0x2c(%rsp)
movq %r8, 0x18(%rsp)
movq %rcx, 0x8(%rsp)
movq %rdx, 0x10(%rsp)
movq %rsi, %r15
movq %rdi, %r14
movq 0xb0(%rsp), %rbx
movq %rsi, %rdi
callq 0x12ad0
cmpl $0x2, (%r15)
jne 0x29dda
cmpb $0x28, (%rax)
jne 0x29dda
cmpb $0x0, 0x1(%rax)
je 0x29e94
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x1e938
cmpl $0x0, (%rbx)
jne 0x2a2b0
movq %r14, %rdi
movq %rax, %rsi
movq 0x10(%rsp), %rdx
movq %rbx, %rcx
callq 0x37860
cmpl $0x0, (%rbx)
jne 0x2a2b0
movq %rax, %r15
movq 0x148(%rax), %r8
subq $0x8, %rsp
movq %r14, %rdi
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq 0x20(%rsp), %rcx
movl $0x1, %r9d
pushq %rbx
callq 0x45fae
addq $0x10, %rsp
movq %r15, %rdi
callq 0x2b166
cmpl $0x0, (%rbx)
setne %al
movl 0x2c(%rsp), %ecx
xorb $0x1, %cl
orb %al, %cl
jne 0x2a2b0
movq 0x10(%rsp), %rdi
callq 0x30171
testb %al, %al
je 0x2a2b0
movq %r14, %rdi
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdx
movq 0x18(%rsp), %rcx
movq %rbx, %r8
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4d4a6
movq (%r14), %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x12edc
cmpl $0x0, (%rbx)
jne 0x2a2b0
movq %r15, %rdi
callq 0x12ad0
movq %rax, %r12
cmpl $0x2, (%r15)
jne 0x29ecf
cmpb $0x29, (%r12)
jne 0x29ecf
cmpb $0x0, 0x1(%r12)
je 0x2a2bf
movq 0x10(%rsp), %rdx
leaq 0x8(%rdx), %rax
movq %rax, 0x48(%rsp)
cmpq $0x0, 0x28(%rdx)
movq 0x8(%rsp), %r13
je 0x2a22c
movl $0x10, %ecx
xorl %ebp, %ebp
xorl %esi, %esi
movq %rcx, 0x38(%rsp)
movq 0x18(%rdx), %r12
addq %rbp, %r12
movq 0x48(%rsp), %rdi
movq %rsi, 0x30(%rsp)
callq 0x2fe6a
movl %eax, 0x20(%rsp)
movq %r12, %rdi
callq 0x37534
testb %al, %al
jne 0x29f33
movl (%r12), %eax
cmpl $0x7, %eax
je 0x29f85
cmpl $0x9, %eax
jne 0x29fc0
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x1e938
cmpl $0x0, (%rbx)
jne 0x2a2b0
movq %r14, %rdi
movq %rax, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x37860
cmpl $0x0, (%rbx)
jne 0x2a2b0
movl 0x20(%rsp), %edx
addq 0x18(%rsp), %rdx
movq %r14, %rdi
movq 0x8(%rsp), %rsi
movq %rax, %rcx
movq %rbx, %r8
callq 0x362c6
jmp 0x2a06d
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x1e938
cmpl $0x0, (%rbx)
jne 0x2a2b0
movl 0x20(%rsp), %ecx
addq 0x18(%rsp), %rcx
movq %r14, %rdi
movq %r12, %rsi
movq 0x8(%rsp), %rdx
movq %rax, %r8
movq %rbx, %r9
callq 0x48ee0
jmp 0x2a06d
movq %r12, %rdi
callq 0x376b5
testb %al, %al
jne 0x29fd8
movq %r12, %rdi
callq 0x376d3
testb %al, %al
je 0x2a03b
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rax
movq %rax, 0x40(%rsp)
movl 0x20(%rsp), %r13d
addq 0x18(%rsp), %r13
movq %r15, %rdi
callq 0x12ad0
cmpl $0x2, (%r15)
jne 0x2a00d
cmpb $0x28, (%rax)
jne 0x2a00d
cmpb $0x0, 0x1(%rax)
je 0x2a10c
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x1e938
cmpl $0x0, (%rbx)
jne 0x2a06d
movq %r14, %rdi
movq %r12, %rsi
movq 0x8(%rsp), %rdx
movq %r13, %rcx
movq %rax, %r8
movq %rbx, %r9
callq 0x42f54
jmp 0x2a06d
cmpl $0x16, (%r12)
jne 0x2a3f1
movl 0x20(%rsp), %r8d
addq 0x18(%rsp), %r8
subq $0x8, %rsp
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %r13, %rcx
xorl %r9d, %r9d
pushq %rbx
callq 0x29d8d
addq $0x10, %rsp
cmpl $0x0, (%rbx)
jne 0x2a2b0
movq %r15, %rdi
callq 0x12ad0
movq %rax, %r12
cmpl $0x2, (%r15)
movq 0x8(%rsp), %r13
movq 0x10(%rsp), %rdx
jne 0x2a27d
movzbl (%r12), %eax
cmpl $0x2c, %eax
jne 0x2a26c
cmpb $0x0, 0x1(%r12)
jne 0x2a27d
movq 0x28(%rdx), %rax
leaq -0x1(%rax), %rcx
movq 0x30(%rsp), %rsi
cmpq %rcx, %rsi
jae 0x2a0eb
movq (%r14), %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x12edc
cmpl $0x0, (%rbx)
jne 0x2a2b0
movq 0x10(%rsp), %rdx
movq 0x28(%rdx), %rax
movq 0x8(%rsp), %r13
movq 0x30(%rsp), %rsi
incq %rsi
movq 0x38(%rsp), %rcx
addq $0x18, %rcx
addq $0x148, %rbp            # imm = 0x148
cmpq %rax, %rsi
jb 0x29ef6
jmp 0x2a22e
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x1ec4c
cmpl $0x0, (%rbx)
jne 0x2a06d
movq %rax, 0x20(%rsp)
movq 0x38(%rsp), %rax
movq 0x40(%rsp), %rcx
movq -0x10(%rcx,%rax), %rax
movq %rax, 0x50(%rsp)
movq %rax, 0x58(%rsp)
movq %r12, 0x60(%rsp)
movq 0x8(%rsp), %rax
movq %rax, 0x68(%rsp)
movq %r13, 0x70(%rsp)
movq %r12, %rdi
callq 0x376b5
testb %al, %al
je 0x2a17e
movq %r14, %rdi
movq %r12, %rsi
movq 0x50(%rsp), %rdx
movq 0x8(%rsp), %rcx
movq %r13, %r8
movq %rbx, %r9
callq 0x457ec
jmp 0x2a1a2
movq 0x38(%rsp), %rax
movq 0x40(%rsp), %rcx
movl (%rcx,%rax), %r8d
movq %r14, %rdi
movq %r12, %rsi
movq 0x8(%rsp), %rdx
movq %r13, %rcx
movq %rbx, %r9
callq 0x458a8
cmpl $0x0, (%rbx)
movq 0x20(%rsp), %rcx
jne 0x2a06d
movq %r14, %rdi
movq %r15, %rsi
leaq 0x58(%rsp), %rdx
movq %rbx, %r8
callq 0x16680
cmpl $0x0, (%rbx)
jne 0x2a06d
movq %r15, %rdi
callq 0x12ad0
cmpl $0x2, (%r15)
jne 0x2a1e5
cmpb $0x29, (%rax)
jne 0x2a1e5
cmpb $0x0, 0x1(%rax)
je 0x2a219
movq (%r14), %rcx
movq 0x30(%rcx), %r8
movl 0x38(%rcx), %r9d
movq %rbx, %rdi
movl $0x1d, %esi
leaq 0x49906(%rip), %rdx     # 0x73b05
movq %rax, %rcx
pushq $0x1db                  # imm = 0x1DB
leaq 0x498dc(%rip), %rax     # 0x73aea
pushq %rax
callq 0x143da
jmp 0x2a069
movq (%r14), %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x12edc
jmp 0x2a06d
xorl %esi, %esi
cmpb $0x29, (%r12)
jne 0x2a241
cmpb $0x0, 0x1(%r12)
je 0x2a300
movq (%r14), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x49897(%rip), %rax     # 0x73aea
leaq 0x498ab(%rip), %rdx     # 0x73b05
movq %rbx, %rdi
movl $0x1d, %esi
movq %r12, %rcx
pushq $0x287                  # imm = 0x287
jmp 0x2a2a6
cmpl $0x29, %eax
jne 0x2a27d
cmpb $0x0, 0x1(%r12)
je 0x2a3e7
movq (%r14), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x49871(%rip), %rdx     # 0x73b00
movq %rbx, %rdi
movl $0x1d, %esi
movq %r12, %rcx
pushq $0x281                  # imm = 0x281
leaq 0x49844(%rip), %rax     # 0x73aea
pushq %rax
callq 0x143da
addq $0x10, %rsp
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movzbl 0x2c(%rsp), %r8d
movq %r14, %rdi
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdx
movq 0x18(%rsp), %rcx
movq %rbx, %r9
callq 0x45c2d
cmpl $0x0, (%rbx)
jne 0x2a2b0
movq (%r14), %rdi
movq %r15, %rsi
movq %rbx, %rdx
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x12edc
leaq 0x1(%rsi), %r12
leaq (%rsi,%rsi,2), %rax
leaq 0x18(,%rax,8), %rax
movq %rax, 0x30(%rsp)
imulq $0x148, %rsi, %rcx      # imm = 0x148
addq $0x148, %rcx            # imm = 0x148
movq 0x10(%rsp), %rax
cmpq 0x28(%rax), %r12
jae 0x2a39c
movq 0x18(%rax), %rbp
addq %rcx, %rbp
movq 0x48(%rsp), %rdi
movq %r12, %rsi
movq %rax, %r13
movq %rcx, 0x20(%rsp)
callq 0x2fe6a
movq 0x20(%r13), %rdx
movq 0x30(%rsp), %r13
addq %r13, %rdx
movl %eax, %r8d
addq 0x18(%rsp), %r8
subq $0x8, %rsp
movq %r14, %rdi
movq %rbp, %rsi
movq 0x10(%rsp), %rcx
xorl %r9d, %r9d
pushq %rbx
callq 0x45916
movq 0x30(%rsp), %rcx
addq $0x10, %rsp
incq %r12
addq $0x18, %r13
movq %r13, 0x30(%rsp)
addq $0x148, %rcx            # imm = 0x148
cmpl $0x0, (%rbx)
je 0x2a323
jmp 0x2a2b0
cmpb $0x0, 0x2c(%rsp)
je 0x2a3d4
movq 0x10(%rsp), %rdi
callq 0x30171
testb %al, %al
je 0x2a3d4
movq %r14, %rdi
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdx
movq 0x18(%rsp), %rcx
movq %rbx, %r8
callq 0x4d4a6
cmpl $0x0, (%rbx)
jne 0x2a2b0
movq (%r14), %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x12edc
jmp 0x2a2b0
movq 0x30(%rsp), %rsi
jmp 0x2a22e
leaq 0x496f2(%rip), %rdx     # 0x73aea
movq %rbx, %rdi
movl $0xf, %esi
movl $0x26c, %ecx            # imm = 0x26C
callq 0x145a4
jmp 0x2a2b0