// File: /workspace/github_c_makefile_star2_O2/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 $0x88, %rsp
movl %r9d, 0x24(%rsp)
movq %r8, %rbp
movq %rcx, 0x10(%rsp)
movq %rdx, 0x8(%rsp)
movq %rsi, %r12
movq %rdi, %r14
movq 0xc0(%rsp), %rbx
movq %rsi, %rdi
callq 0x12a25
cmpl $0x2, (%r12)
jne 0x28aa9
cmpb $0x28, (%rax)
jne 0x28aa9
cmpb $0x0, 0x1(%rax)
je 0x28b5e
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x1dccf
cmpl $0x0, (%rbx)
jne 0x28fb6
movq %r14, %rdi
movq %rax, %rsi
movq 0x8(%rsp), %rdx
movq %rbx, %rcx
callq 0x35977
cmpl $0x0, (%rbx)
jne 0x28fb6
movq %rax, %r15
movq 0x148(%rax), %r8
subq $0x8, %rsp
pushq $0x1
popq %r9
movq %r14, %rdi
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %rbp, %rcx
pushq %rbx
callq 0x4376f
popq %rax
popq %rcx
movq %r15, %rdi
callq 0x29d88
cmpl $0x0, (%rbx)
setne %al
movl 0x24(%rsp), %ecx
xorb $0x1, %cl
orb %al, %cl
jne 0x28fb6
movq 0x8(%rsp), %rdi
callq 0x2e7c9
testb %al, %al
je 0x28fb6
movq %r14, %rdi
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq %rbp, %rcx
movq %rbx, %r8
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4a8be
movq (%r14), %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x12e0e
cmpl $0x0, (%rbx)
jne 0x28fb6
movq %r12, %rdi
callq 0x12a25
movq %rax, %r15
cmpl $0x2, (%r12)
jne 0x28b98
cmpb $0x29, (%r15)
jne 0x28b98
cmpb $0x0, 0x1(%r15)
je 0x28f34
movq 0x8(%rsp), %rdi
leaq 0x8(%rdi), %rax
movq %rax, 0x50(%rsp)
pushq $0x1
popq %rax
pushq $0x18
popq %rcx
xorl %edx, %edx
movq %rbp, 0x30(%rsp)
leaq -0x1(%rax), %rsi
cmpq 0x28(%rdi), %rsi
movq %rcx, 0x28(%rsp)
jae 0x28f2c
movq %rax, 0x40(%rsp)
movq 0x18(%rdi), %r15
movq %rdx, 0x38(%rsp)
addq %rdx, %r15
movq 0x50(%rsp), %rdi
movq %rsi, 0x48(%rsp)
callq 0x2e515
movl %eax, %ebp
movq %r15, %rdi
callq 0x35655
movl %ebp, %r13d
movq 0x30(%rsp), %rbp
testb %al, %al
jne 0x28c0d
movl (%r15), %eax
cmpl $0x7, %eax
je 0x28c5c
cmpl $0x9, %eax
jne 0x28c94
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x1dccf
cmpl $0x0, (%rbx)
jne 0x28fb6
movq %r14, %rdi
movq %rax, %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x35977
cmpl $0x0, (%rbx)
jne 0x28fb6
addq %rbp, %r13
movq %r14, %rdi
movq 0x10(%rsp), %rsi
movq %r13, %rdx
movq %rax, %rcx
movq %rbx, %r8
callq 0x34300
jmp 0x28d50
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x1dccf
cmpl $0x0, (%rbx)
jne 0x28fb6
addq %rbp, %r13
movq %r14, %rdi
movq %r15, %rsi
movq 0x10(%rsp), %rdx
movq %r13, %rcx
movq %rax, %r8
movq %rbx, %r9
callq 0x46584
jmp 0x28d50
movq %r13, 0x18(%rsp)
movq %r15, %rdi
callq 0x357cc
testb %al, %al
jne 0x28cb1
movq %r15, %rdi
callq 0x357ea
testb %al, %al
je 0x28d1c
movq 0x8(%rsp), %rax
movq 0x20(%rax), %rbp
movq 0x18(%rsp), %rax
addq 0x30(%rsp), %rax
movq %rax, 0x18(%rsp)
movq %r12, %rdi
callq 0x12a25
cmpl $0x2, (%r12)
jne 0x28ce7
cmpb $0x28, (%rax)
jne 0x28ce7
cmpb $0x0, 0x1(%rax)
je 0x28ddb
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x1dccf
cmpl $0x0, (%rbx)
jne 0x28d15
movq %r14, %rdi
movq %r15, %rsi
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rcx
movq %rax, %r8
movq %rbx, %r9
callq 0x4092c
movq 0x30(%rsp), %rbp
jmp 0x28d50
cmpl $0x16, (%r15)
jne 0x2909e
movq 0x30(%rsp), %rbp
movq 0x18(%rsp), %r8
addq %rbp, %r8
subq $0x8, %rsp
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rdx
movq 0x18(%rsp), %rcx
xorl %r9d, %r9d
pushq %rbx
callq 0x28a5a
popq %rax
popq %rcx
cmpl $0x0, (%rbx)
jne 0x28fb6
movq %r12, %rdi
callq 0x12a25
movq %rax, %r15
cmpl $0x2, (%r12)
movq 0x48(%rsp), %rcx
movq 0x8(%rsp), %rdx
jne 0x28f0a
movzbl (%r15), %eax
cmpl $0x2c, %eax
jne 0x28efe
cmpb $0x0, 0x1(%r15)
jne 0x28f0a
movq 0x28(%rdx), %rax
decq %rax
cmpq %rax, %rcx
jae 0x28db4
movq (%r14), %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x12e0e
cmpl $0x0, (%rbx)
jne 0x28fb6
movq 0x40(%rsp), %rax
incq %rax
movq 0x28(%rsp), %rcx
addq $0x18, %rcx
movq 0x38(%rsp), %rdx
addq $0x148, %rdx            # imm = 0x148
movq 0x8(%rsp), %rdi
jmp 0x28bb3
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x1dfd5
cmpl $0x0, (%rbx)
jne 0x28d15
movq %rax, 0x60(%rsp)
movq 0x28(%rsp), %rax
movq -0x18(%rbp,%rax), %rax
movq %rax, 0x58(%rsp)
movq %rax, 0x68(%rsp)
movq %r15, 0x70(%rsp)
movq 0x10(%rsp), %rax
movq %rax, %r13
movq %rax, 0x78(%rsp)
movq 0x18(%rsp), %rax
movq %rax, 0x80(%rsp)
movq %r15, %rdi
callq 0x357cc
testb %al, %al
je 0x28e53
movq %r14, %rdi
movq %r15, %rsi
movq 0x58(%rsp), %rdx
movq %r13, %rcx
movq 0x18(%rsp), %r8
movq %rbx, %r9
callq 0x42fe0
jmp 0x28e73
movq 0x28(%rsp), %rax
movl -0x8(%rbp,%rax), %r8d
movq %r14, %rdi
movq %r15, %rsi
movq %r13, %rdx
movq 0x18(%rsp), %rcx
movq %rbx, %r9
callq 0x43092
cmpl $0x0, (%rbx)
movq 0x60(%rsp), %rcx
jne 0x28d15
movq %r14, %rdi
movq %r12, %rsi
leaq 0x68(%rsp), %rdx
movq %rbx, %r8
callq 0x161d0
cmpl $0x0, (%rbx)
jne 0x28d15
movq %r12, %rdi
callq 0x12a25
cmpl $0x2, (%r12)
jne 0x28eb7
cmpb $0x29, (%rax)
jne 0x28eb7
cmpb $0x0, 0x1(%rax)
je 0x28eeb
movq (%r14), %rcx
movq 0x30(%rcx), %r8
movl 0x38(%rcx), %r9d
movq %rbx, %rdi
pushq $0x1d
popq %rsi
leaq 0x44bee(%rip), %rdx     # 0x6dabd
movq %rax, %rcx
pushq $0x1db                  # imm = 0x1DB
leaq 0x44bc4(%rip), %rax     # 0x6daa2
pushq %rax
callq 0x140cd
popq %rax
popq %rcx
jmp 0x28d15
movq (%r14), %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x12e0e
jmp 0x28d15
cmpl $0x29, %eax
jne 0x28f0a
cmpb $0x0, 0x1(%r15)
je 0x28f76
movq (%r14), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x44b9c(%rip), %rdx     # 0x6dab8
pushq $0x1d
popq %rsi
movq %rbx, %rdi
movq %r15, %rcx
pushq $0x281                  # imm = 0x281
jmp 0x28fa7
cmpb $0x29, (%r15)
je 0x28f80
jmp 0x28f87
movzbl 0x24(%rsp), %r8d
movq %r14, %rdi
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq %rbp, %rcx
movq %rbx, %r9
callq 0x433f5
cmpl $0x0, (%rbx)
jne 0x28fb6
movq (%r14), %rdi
movq %r12, %rsi
movq %rbx, %rdx
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x12e0e
movq 0x40(%rsp), %rax
movq 0x38(%rsp), %rdx
cmpb $0x0, 0x1(%r15)
je 0x28fc8
movq (%r14), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x44b24(%rip), %rdx     # 0x6dabd
pushq $0x1d
popq %rsi
movq %rbx, %rdi
movq %r15, %rcx
pushq $0x287                  # imm = 0x287
leaq 0x44af4(%rip), %rax     # 0x6daa2
pushq %rax
callq 0x140cd
popq %rax
popq %rcx
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq $0x148, %rdx            # imm = 0x148
movq 0x8(%rsp), %r13
cmpq 0x28(%r13), %rax
jae 0x29055
movq %rax, %r15
movq 0x18(%r13), %rax
movq %rdx, 0x38(%rsp)
addq %rdx, %rax
movq %rax, 0x48(%rsp)
movq 0x50(%rsp), %rdi
movq %r15, %rsi
movq %r15, 0x40(%rsp)
callq 0x2e515
movq 0x20(%r13), %rdx
movq 0x28(%rsp), %r15
addq %r15, %rdx
movl %eax, %r8d
addq %rbp, %r8
subq $0x8, %rsp
movq %r14, %rdi
movq 0x50(%rsp), %rsi
movq 0x18(%rsp), %rcx
xorl %r9d, %r9d
pushq %rbx
callq 0x43100
popq %rax
popq %rcx
movq 0x40(%rsp), %rax
incq %rax
addq $0x18, %r15
movq %r15, 0x28(%rsp)
movq 0x38(%rsp), %rdx
addq $0x148, %rdx            # imm = 0x148
cmpl $0x0, (%rbx)
je 0x28fd4
jmp 0x28fb6
cmpb $0x0, 0x24(%rsp)
je 0x2908b
movq 0x8(%rsp), %rdi
callq 0x2e7c9
testb %al, %al
je 0x2908b
movq %r14, %rdi
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq %rbp, %rcx
movq %rbx, %r8
callq 0x4a8be
cmpl $0x0, (%rbx)
jne 0x28fb6
movq (%r14), %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x12e0e
jmp 0x28fb6
leaq 0x449fd(%rip), %rdx     # 0x6daa2
pushq $0xf
popq %rsi
movq %rbx, %rdi
movl $0x26c, %ecx            # imm = 0x26C
callq 0x1429e
jmp 0x28fb6