// File: /workspace/github_c_makefile_star2_O2/markdryan[P]subtilis/frontend/parser_assignment.c
// Function: subtilis_parser_assignment
void subtilis_parser_assignment(subtilis_parser_t *p, subtilis_token_t *t,
				subtilis_error_t *err)
{
	const char *tbuf;
	subtilis_ir_operand_t op1;
	const subtilis_symbol_t *s;
	subtilis_assign_type_t at;
	subtilis_type_t type;
	size_t mem_reg = SUBTILIS_IR_REG_LOCAL;
	bool new_global = false;
	char *var_name = NULL;
	subtilis_exp_t *e = NULL;

	type.type = SUBTILIS_TYPE_VOID;

	var_name = subtilis_parser_get_assignment_var(p, t, &type, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	tbuf = subtilis_token_get_text(t);
	if (t->type != SUBTILIS_TOKEN_OPERATOR) {
		prv_missing_operator_error(p, tbuf, var_name, err);
		goto cleanup;
	}

	if (!strcmp(tbuf, "(")) {
		s = subtilis_symbol_table_lookup(p->local_st, var_name);
		if (!s) {
			s = subtilis_symbol_table_lookup(p->st, var_name);
			mem_reg = SUBTILIS_IR_REG_GLOBAL;
		}
		if (s && s->t.type == SUBTILIS_TYPE_FN)
			subtilis_parser_call_ptr(p, t, s, var_name, mem_reg,
						 err);
		else
			prv_assign_array(p, t, var_name, &type, err);
		goto cleanup;
	}

	if (!strcmp(tbuf, "{")) {
		prv_assign_vector(p, t, var_name, &type, err);
		goto cleanup;
	}

	if (!strcmp(tbuf, ":=")) {
		prv_assignment_local(p, t, var_name, &type, err);
		goto cleanup;
	}

	subtilis_parser_lookup_assignment_var(p, t, var_name, &s, &op1.reg,
					      &new_global, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	if (!new_global && (type.type == SUBTILIS_TYPE_REC) &&
	    !strcmp(tbuf, ".")) {
		prv_assign_field(p, t, &type, op1.reg, s->loc, err);
		goto cleanup;
	} else if (!strcmp(tbuf, "=") || !strcmp(tbuf, ":=")) {
		at = SUBTILIS_ASSIGN_TYPE_EQUAL;
	} else if (new_global) {
		prv_missing_operator_error(p, tbuf, var_name, err);
		goto cleanup;
	} else if (!strcmp(tbuf, "+=")) {
		at = SUBTILIS_ASSIGN_TYPE_PLUS_EQUAL;
	} else if (!strcmp(tbuf, "-=") && subtilis_type_if_is_numeric(&type)) {
		at = SUBTILIS_ASSIGN_TYPE_MINUS_EQUAL;
	} else {
		subtilis_error_set_assignment_op_expected(
		    err, tbuf, p->l->stream->name, p->l->line);
		goto cleanup;
	}

	if (new_global) {
		if (p->current->proc_called) {
			subtilis_error_set_global_after_proc(
			    err, var_name, p->l->stream->name, p->l->line);
			goto cleanup;
		}
		if ((type.type == SUBTILIS_TYPE_REC) &&
		    (at == SUBTILIS_ASSIGN_TYPE_EQUAL)) {
			subtilis_parser_rec_init(p, t, &type, var_name, false,
						 true, err);
			goto cleanup;
		}
		s = subtilis_symbol_table_insert(p->st, var_name, &type, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;
	} else {
		/*
		 * We need to check that the id type matches the type in the
		 * symbol table.  The compiler can still generate correct code
		 * because it knows the correct type, but the code still looks
		 * weird.
		 */

		subtilis_type_compare_diag(p, &type, &s->t, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;
	}

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

	tbuf = subtilis_token_get_text(t);
	if ((type.type == SUBTILIS_TYPE_REC) &&
	    (at == SUBTILIS_ASSIGN_TYPE_EQUAL) && !strcmp(tbuf, "(")) {
		subtilis_parser_rec_reset(p, t, &type, op1.reg, s->loc, err);
		goto cleanup;
	}

	e = subtilis_parser_priority7(p, t, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	/* Ownership of e is passed to the following functions. */

	if (subtilis_type_if_is_reference(&s->t)) {
		if (at == SUBTILIS_ASSIGN_TYPE_EQUAL) {
			if (new_global)
				subtilis_type_if_new_ref(p, &s->t, op1.reg,
							 s->loc, e, err);
			else
				subtilis_type_if_assign_ref(p, &s->t, op1.reg,
							    s->loc, e, err);
		} else if (s->t.type == SUBTILIS_TYPE_STRING) {
			subtilis_string_type_add_eq(p, op1.reg, s->loc, e, err);
		} else {
			subtilis_error_set_assertion_failed(err);
		}
		goto cleanup;
	}

	if ((type.type == SUBTILIS_TYPE_FN) && (e->partial_name)) {
		if (e->type.type != SUBTILIS_TYPE_FN) {
			subtilis_error_set_expected(
			    err, subtilis_type_name(&type),
			    subtilis_type_name(&e->type), p->l->stream->name,
			    p->l->line);
			goto cleanup;
		}
		subtilis_parser_call_add_addr(p, &type, e, err);
	} else {
		e = subtilis_type_if_coerce_type(p, e, &type, err);
	}
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	e = prv_assignment_operator(p, var_name, e, at, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	if (s->is_reg)
		subtilis_type_if_assign_to_reg(p, s->loc, e, err);
	else
		subtilis_type_if_assign_to_mem(p, op1.reg, s->loc, e, err);

cleanup:

	subtilis_type_free(&type);
	free(var_name);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x308, %rsp            # imm = 0x308
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, %r15
movb $0x0, 0xf(%rsp)
leaq 0x78(%rsp), %rdx
movl $0x8, (%rdx)
movq %r14, %rcx
callq 0x17cc2
movq %rax, %rbx
cmpl $0x0, (%r14)
jne 0x180ab
movq %r12, %rdi
callq 0x12a25
movq %rax, %r13
cmpl $0x2, (%r12)
jne 0x1809a
movzbl (%r13), %eax
cmpl $0x7b, %eax
je 0x17f5c
cmpl $0x3a, %eax
je 0x17ef8
cmpl $0x28, %eax
jne 0x17f67
cmpb $0x0, 0x1(%r13)
jne 0x17f67
movq 0x68(%r15), %rdi
movq %rbx, %rsi
callq 0x2d376
testq %rax, %rax
je 0x180fe
pushq $0x2
jmp 0x18111
cmpb $0x3d, 0x1(%r13)
jne 0x17f67
cmpb $0x0, 0x2(%r13)
jne 0x17f67
movq 0x48(%r15), %rax
cmpq 0x50(%r15), %rax
jne 0x17f1b
cmpq $0x0, 0x78(%r15)
je 0x183f3
movq 0x68(%r15), %rdi
movq %rbx, %rsi
callq 0x2d376
testq %rax, %rax
je 0x18231
movq (%r15), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x55662(%rip), %rax     # 0x6d5a4
leaq 0x55e7c(%rip), %rcx     # 0x6ddc5
pushq $0x20
popq %rsi
movq %r14, %rdi
movq %rbx, %rdx
pushq $0x260                  # imm = 0x260
jmp 0x181e1
cmpb $0x0, 0x1(%r13)
je 0x18013
subq $0x8, %rsp
leaq 0x28(%rsp), %rcx
leaq 0x1c8(%rsp), %r8
leaq 0x17(%rsp), %r9
movq %r15, %rdi
movq %rbx, %rdx
pushq %r14
callq 0x17d87
popq %rax
popq %rcx
cmpl $0x0, (%r14)
jne 0x180ab
movb 0xf(%rsp), %bpl
movl 0x78(%rsp), %eax
cmpl $0x16, %eax
sete %cl
movl %ebp, %edx
notb %dl
andb %cl, %dl
movb (%r13), %cl
movzbl %cl, %ecx
cmpb $0x2e, %cl
sete %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x17ff5
cmpb $0x0, 0x1(%r13)
jne 0x18095
movq 0x1c0(%rsp), %rcx
movq 0x20(%rsp), %rax
movq (%rax), %r8
leaq 0x78(%rsp), %rdx
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %r9
callq 0x1874a
jmp 0x180ab
cmpl $0x3a, %ecx
je 0x18078
cmpl $0x3d, %ecx
jne 0x18095
cmpb $0x0, 0x1(%r13)
jne 0x18095
jmp 0x181ee
leaq 0x1c0(%rsp), %rsi
movl $0x8, (%rsi)
leaq 0x78(%rsp), %rdi
movq %r14, %rdx
callq 0x34066
cmpl $0x0, (%r14)
jne 0x180ab
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x1e894
movq %rax, 0x20(%rsp)
cmpl $0x0, (%r14)
jne 0x183b4
xorl %edx, %edx
testq %rax, %rax
setne %dl
leaq 0x20(%rsp), %rcx
leaq 0x1c0(%rsp), %r9
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %r8
pushq %r14
pushq $0x0
jmp 0x183ad
cmpb $0x3d, 0x1(%r13)
jne 0x18095
cmpb $0x0, 0x2(%r13)
je 0x181ee
testb %bpl, %bpl
je 0x181ba
jmp 0x1809a
testb %bpl, %bpl
je 0x180cf
movq %r15, %rdi
movq %r13, %rsi
movq %rbx, %rdx
movq %r14, %rcx
callq 0x186fa
leaq 0x78(%rsp), %rdi
callq 0x2dc7c
movq %rbx, %rdi
callq 0x122c0
addq $0x308, %rsp            # imm = 0x308
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x2d, %ecx
je 0x1818a
cmpl $0x2b, %ecx
jne 0x181ba
cmpb $0x3d, 0x1(%r13)
jne 0x181ba
cmpb $0x0, 0x2(%r13)
jne 0x181ba
pushq $0x1
jmp 0x181a8
movq 0x60(%r15), %rdi
movq %rbx, %rsi
callq 0x2d376
testq %rax, %rax
je 0x18132
pushq $0x1
popq %r8
cmpl $0x9, 0x8(%rax)
jne 0x18132
movq %r15, %rdi
movq %r12, %rsi
movq %rax, %rdx
movq %rbx, %rcx
movq %r14, %r9
callq 0x1bd5f
jmp 0x180ab
leaq 0x1c0(%rsp), %rsi
movl $0x8, (%rsi)
leaq 0x78(%rsp), %rdi
movq %r14, %rdx
callq 0x34015
cmpl $0x0, (%r14)
jne 0x180ab
leaq 0x20(%rsp), %rdx
pushq $0xa
popq %rcx
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %r8
callq 0x1e437
cmpl $0x0, (%r14)
je 0x18390
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x19074
jmp 0x183b4
cmpb $0x3d, 0x1(%r13)
jne 0x181ba
cmpb $0x0, 0x2(%r13)
jne 0x181ba
leaq 0x78(%rsp), %rdi
callq 0x35655
testb %al, %al
je 0x181ba
pushq $0x2
popq %rax
movl %eax, 0x10(%rsp)
movl $0x0, 0x14(%rsp)
jmp 0x18299
movq (%r15), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x553d8(%rip), %rax     # 0x6d5a4
leaq 0x55bf2(%rip), %rcx     # 0x6ddc5
pushq $0x14
popq %rsi
movq %r14, %rdi
movq %r13, %rdx
pushq $0x461                  # imm = 0x461
pushq %rax
callq 0x140cd
popq %rax
popq %rcx
jmp 0x180ab
testb %bpl, %bpl
je 0x1828b
movq 0x48(%r15), %rcx
cmpb $0x1, 0xe0(%rcx)
jne 0x183c6
movq (%r15), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x5538a(%rip), %rax     # 0x6d5a4
leaq 0x55ba4(%rip), %rcx     # 0x6ddc5
pushq $0x6c
popq %rsi
movq %r14, %rdi
movq %rbx, %rdx
pushq $0x468                  # imm = 0x468
jmp 0x181e1
leaq 0x78(%rsp), %rdi
callq 0x35655
testb %al, %al
je 0x1835f
leaq 0x78(%rsp), %rcx
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %r8
callq 0x179c7
cmpl $0x0, (%r14)
jne 0x180ab
movq %rax, %r12
movq 0x68(%r15), %rdi
movq 0x148(%rax), %rcx
leaq 0x78(%rsp), %rdx
movq %rbx, %rsi
movq %r14, %r8
callq 0x2d0fe
movq %r12, %rdi
callq 0x29d88
jmp 0x180ab
movb $0x1, %al
movl %eax, 0x14(%rsp)
movl $0x0, 0x10(%rsp)
movq 0x20(%rsp), %r13
leaq 0x8(%r13), %rdx
leaq 0x78(%rsp), %rsi
movq %r15, %rdi
movq %r14, %rcx
callq 0x35d40
cmpl $0x0, (%r14)
jne 0x180ab
movq (%r15), %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x12e0e
cmpl $0x0, (%r14)
jne 0x180ab
movq %r12, %rdi
callq 0x12a25
cmpl $0x16, 0x78(%rsp)
setne %cl
movl 0x14(%rsp), %edx
xorb $0x1, %dl
orb %cl, %dl
jne 0x182fe
cmpb $0x28, (%rax)
jne 0x182fe
cmpb $0x0, 0x1(%rax)
je 0x18591
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x1dccf
cmpl $0x0, (%r14)
jne 0x180ab
movq %rax, 0x18(%rsp)
leaq 0x8(%r13), %r12
movq %r12, %rdi
callq 0x35808
testb %al, %al
je 0x184f4
cmpb $0x0, 0x14(%rsp)
je 0x185b5
movq 0x1c0(%rsp), %rdx
movq (%r13), %rcx
movq %r15, %rdi
movq %r12, %rsi
testb %bpl, %bpl
je 0x18615
movq 0x18(%rsp), %r8
jmp 0x18608
movl 0x78(%rsp), %eax
cmpl $0x16, %eax
je 0x1846d
cmpl $0x9, %eax
jne 0x18488
leaq 0x78(%rsp), %rcx
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rdx
movq %r14, %r8
callq 0x17b1f
jmp 0x180ab
leaq 0x20(%rsp), %rcx
leaq 0x1c0(%rsp), %r9
movq %r15, %rdi
movq %r12, %rsi
movq %rax, %rdx
movq %rbx, %r8
pushq %r14
pushq $0x1
callq 0x19126
popq %rax
popq %rcx
leaq 0x1c0(%rsp), %rdi
callq 0x2dc7c
jmp 0x180ab
cmpl $0x16, %eax
jne 0x18434
subq $0x8, %rsp
leaq 0x80(%rsp), %rdx
pushq $0x1
popq %r9
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rcx
xorl %r8d, %r8d
pushq %r14
callq 0x288d1
jmp 0x181e7
movq 0x60(%r15), %rdi
movq %rbx, %rsi
callq 0x2d376
testq %rax, %rax
je 0x17f1b
movq (%r15), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x5518a(%rip), %rax     # 0x6d5a4
leaq 0x559a4(%rip), %rcx     # 0x6ddc5
pushq $0x52
popq %rsi
movq %r14, %rdi
movq %rbx, %rdx
pushq $0x259                  # imm = 0x259
jmp 0x181e1
movq 0x60(%r15), %rdi
leaq 0x78(%rsp), %rdx
movq %rbx, %rsi
movq %r14, %rcx
callq 0x2cbb7
movq %rax, 0x20(%rsp)
cmpl $0x0, (%r14)
jne 0x180ab
movq %rax, %r13
movb $0x1, %al
movl %eax, 0x14(%rsp)
movl $0x0, 0x10(%rsp)
jmp 0x182bc
leaq 0x78(%rsp), %rcx
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rdx
movq %r14, %r8
callq 0x17c42
jmp 0x180ab
leaq 0x78(%rsp), %rdi
callq 0x35808
testb %al, %al
je 0x184d8
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x1dfd5
cmpl $0x0, (%r14)
jne 0x180ab
movq %rax, %r12
movq 0x158(%rax), %rdx
movq 0x68(%r15), %rdi
testq %rdx, %rdx
je 0x185dd
movq %r12, %rsi
movq %rbx, %rcx
movq %r14, %r8
callq 0x2d01c
jmp 0x1827e
leaq 0x550c5(%rip), %rdx     # 0x6d5a4
pushq $0xf
popq %rsi
movq %r14, %rdi
movl $0x274, %ecx            # imm = 0x274
callq 0x1429e
jmp 0x180ab
cmpl $0x9, 0x78(%rsp)
movq 0x18(%rsp), %rsi
jne 0x18530
cmpq $0x0, 0x160(%rsi)
je 0x18530
cmpl $0x9, (%rsi)
jne 0x1863e
leaq 0x78(%rsp), %rsi
movq %r15, %rdi
movq 0x18(%rsp), %r12
movq %r12, %rdx
movq %r14, %rcx
callq 0x1cc3c
movq %r12, %rax
jmp 0x18540
leaq 0x78(%rsp), %rdx
movq %r15, %rdi
movq %r14, %rcx
callq 0x35977
cmpl $0x0, (%r14)
jne 0x180ab
movq %rax, %r12
cmpl $0x0, 0x10(%rsp)
je 0x186b8
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x33cb3
cmpl $0x0, (%r14)
jne 0x1869b
cmpl $0x1, 0x10(%rsp)
jne 0x1867f
movq %r15, %rdi
movq %rax, %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0x2a5db
jmp 0x18690
movq 0x1c0(%rsp), %rcx
movq (%r13), %r8
leaq 0x78(%rsp), %rdx
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %r9
callq 0x290ba
jmp 0x180ab
cmpl $0x7, (%r12)
jne 0x18627
movq 0x1c0(%rsp), %rsi
movq (%r13), %rdx
movq %r15, %rdi
movq 0x18(%rsp), %rcx
movq %r14, %r8
callq 0x493b7
jmp 0x180ab
leaq 0x78(%rsp), %rdx
movq %rbx, %rsi
movq %r14, %rcx
callq 0x2cbb7
cmpl $0x0, (%r14)
jne 0x180ab
movq (%rax), %rcx
leaq 0x78(%rsp), %rsi
pushq $0x2
popq %rdx
movq %r15, %rdi
movq %r12, %r8
movq %r14, %r9
callq 0x33f1f
jmp 0x180ab
movq 0x18(%rsp), %r8
movq %r14, %r9
callq 0x33f4e
jmp 0x180ab
leaq 0x54f76(%rip), %rdx     # 0x6d5a4
pushq $0xf
popq %rsi
movq %r14, %rdi
movl $0x49d, %ecx            # imm = 0x49D
jmp 0x184ea
leaq 0x78(%rsp), %rdi
callq 0x2dcbb
movq %rax, %r13
movq 0x18(%rsp), %rdi
callq 0x2dcbb
movq (%r15), %rcx
movq 0x30(%rcx), %r8
movl 0x38(%rcx), %r9d
leaq 0x54f3d(%rip), %r10     # 0x6d5a4
pushq $0x1d
popq %rsi
movq %r14, %rdi
movq %r13, %rdx
movq %rax, %rcx
pushq $0x4a7                  # imm = 0x4A7
pushq %r10
jmp 0x181e2
movq %r15, %rdi
movq %rax, %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0x34e98
movq %rax, %r12
cmpl $0x0, (%r14)
je 0x186b8
xorl %eax, %eax
movq %rax, %rdi
callq 0x29d88
movq %r12, %rdi
callq 0x29d88
cmpl $0x0, (%r14)
jne 0x180ab
xorl %r12d, %r12d
movq 0x20(%rsp), %rax
cmpb $0x1, 0x2a8(%rax)
jne 0x186dc
movq (%rax), %rsi
movq %r15, %rdi
movq %r12, %rdx
movq %r14, %rcx
callq 0x342ce
jmp 0x180ab
movq 0x1c0(%rsp), %rsi
movq (%rax), %rdx
movq %r15, %rdi
movq %r12, %rcx
movq %r14, %r8
callq 0x34300
jmp 0x180ab