// File: /workspace/github_c_makefile_star2_O3/Ihsoh[P]ilang/compiler/backend/semantics.c
// Function: _type
static void __type(
	ParserContext *ctx,
	ParserASTNode *node,
	uint8_t *type,
	bool first_type
) {
	assert(ctx);
	assert(node);
	
	#define _SET_TYPE(t) { if (type != NULL) { *type = (t); } }

	switch (node->type) {
		case BE_NODE_TYPE_CHAR: {
			assert(node->nchilds == 0);
			_SET_TYPE(BE_TYPE_CHAR)
			break;
		}
		case BE_NODE_TYPE_INT8: {
			assert(node->nchilds == 0);
			_SET_TYPE(BE_TYPE_INT8)
			break;
		}
		case BE_NODE_TYPE_UINT8: {
			assert(node->nchilds == 0);
			_SET_TYPE(BE_TYPE_UINT8)
			break;
		}
		case BE_NODE_TYPE_INT16: {
			assert(node->nchilds == 0);
			_SET_TYPE(BE_TYPE_INT16)
			break;
		}
		case BE_NODE_TYPE_UINT16: {
			assert(node->nchilds == 0);
			_SET_TYPE(BE_TYPE_UINT16)
			break;
		}
		case BE_NODE_TYPE_INT32: {
			assert(node->nchilds == 0);
			_SET_TYPE(BE_TYPE_INT32)
			break;
		}
		case BE_NODE_TYPE_UINT32: {
			assert(node->nchilds == 0);
			_SET_TYPE(BE_TYPE_UINT32)
			break;
		}
		case BE_NODE_TYPE_INT64: {
			assert(node->nchilds == 0);
			_SET_TYPE(BE_TYPE_INT64)
			break;
		}
		case BE_NODE_TYPE_UINT64: {
			assert(node->nchilds == 0);
			_SET_TYPE(BE_TYPE_UINT64)
			break;
		}
		case BE_NODE_TYPE_FLOAT: {
			assert(node->nchilds == 0);
			_SET_TYPE(BE_TYPE_FLOAT)
			break;
		}
		case BE_NODE_TYPE_DOUBLE: {
			assert(node->nchilds == 0);
			_SET_TYPE(BE_TYPE_DOUBLE)
			break;
		}
		case BE_NODE_TYPE_VA_LIST: {
			assert(node->nchilds == 0);
			_SET_TYPE(BE_TYPE_VA_LIST)
			break;
		}
		case BE_NODE_TYPE_STRUCT: {
			assert(node->nchilds == 1);

			_SET_TYPE(BE_TYPE_STRUCT)

			ParserASTNode *node_id = node->childs[0];
			assert(node_id->type == BE_NODE_IDENTIFIER);

			LexerToken *token = node_id->token;
			ParserSymbol *symbol_struct = parser_get_symbol_from_node(ctx, node, BE_SYM_STRUCT, token);
			if (symbol_struct == NULL) {
				_SYNERR_TOKEN(ctx, token, "undefined struct.");
			} else {
				if (first_type
						&& BE_STRUCT_SYMBOL_GET_DUMMY(symbol_struct)) {
					_SYNERR_TOKEN(ctx, token, "struct not implemented.");
				}
			}

			break;
		}
		case BE_NODE_TYPE_ARRAY: {
			assert(node->nchilds == 2);

			_SET_TYPE(BE_TYPE_ARRAY)

			ParserASTNode *node_array_dims = node->childs[0];
			assert(node_array_dims->type == BE_NODE_TYPE_ARRAY_DIMS);
			for (int i = 0; i < node_array_dims->nchilds; i++) {
				ParserASTNode *node_array_dim = node_array_dims->childs[i];
				assert(node_array_dim->type == BE_NODE_LITERAL_UINT);

				uint64_t dim = be_parser_get_uint_val(ctx, node_array_dim);
				if (dim == 0) {
					_SYNERR_NODE(
						ctx,
						node_array_dim,
						"array dimension size cannot be 0."
					);
				}
			}
			
			ParserASTNode *node_type = node->childs[1];
			__type(ctx, node_type, NULL, false);

			break;
		}
		case BE_NODE_TYPE_FUNC: {
			assert(node->nchilds == 1 || node->nchilds == 2);

			_SET_TYPE(BE_TYPE_FUNC)

			ParserASTNode *node_params = NULL;
			ParserASTNode *node_type = NULL;

			node_params = node->childs[0];
			for (int i = 0; i < node_params->nchilds; i++) {
				ParserASTNode *node_param = node_params->childs[i];
				switch (node_param->type) {
					case BE_NODE_FUNC_PARAMS_ELLIPSIS_ITEM: {

						break;
					}
					case BE_NODE_FUNC_PARAMS_ITEM: {
						assert(node_param->nchilds == 2);
						
						ParserASTNode *node_param_id = node_param->childs[0];

						ParserASTNode *node_param_type = node_param->childs[1];
						__type(ctx, node_param_type, NULL, false);

						break;
					}
					default: {
						assert(0);
					}
				}
			}
			
			if (node->nchilds == 2) {
				node_type = node->childs[1];
				__type(ctx, node_type, NULL, false);
			}

			break;
		}
		case BE_NODE_TYPE_POINTER: {
			assert(node->nchilds == 1);

			_SET_TYPE(BE_TYPE_POINTER)

			ParserASTNode *node_type = node->childs[0];
			__type(ctx, node_type, NULL, false);

			break;
		}
		case BE_NODE_TYPE_VOID: {
			assert(node->nchilds == 0);
			_SET_TYPE(BE_TYPE_VOID)
			break;
		}
		default: {
			assert(0);
		}
	}

	#undef	_SET_TYPE
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x408, %rsp            # imm = 0x408
testq %rsi, %rsi
je 0x2dd3c
movl %ecx, %r15d
movq %rsi, %r14
movq %rdi, %rbx
movl (%r14), %eax
cmpl $0x43f, %eax            # imm = 0x43F
jg 0x2dd06
cmpl $0x430, %eax            # imm = 0x430
jne 0x2dd5b
movl 0x44(%r14), %eax
decl %eax
cmpl $0x2, %eax
jae 0x2e07b
testq %rdx, %rdx
je 0x2dc94
movb $0xf, (%rdx)
movq 0x48(%r14), %rax
movq (%rax), %r15
movl 0x44(%r15), %eax
testl %eax, %eax
jle 0x2dcf1
xorl %r12d, %r12d
movq 0x48(%r15), %rcx
movq (%rcx,%r12,8), %rcx
movl (%rcx), %edx
cmpl $0xa03, %edx            # imm = 0xA03
je 0x2dce6
cmpl $0xa02, %edx            # imm = 0xA02
jne 0x2e05c
cmpl $0x2, 0x44(%rcx)
jne 0x2e03d
movq 0x48(%rcx), %rax
movq 0x8(%rax), %rsi
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x2dc3e
movl 0x44(%r15), %eax
incq %r12
movslq %eax, %rcx
cmpq %rcx, %r12
jl 0x2dca6
cmpl $0x2, 0x44(%r14)
jne 0x2e007
movq 0x48(%r14), %rax
addq $0x8, %rax
jmp 0x2dd28
cmpl $0x440, %eax            # imm = 0x440
jne 0x2dd95
cmpl $0x1, 0x44(%r14)
jne 0x2e09a
testq %rdx, %rdx
je 0x2dd24
movb $0x10, (%rdx)
movq 0x48(%r14), %rax
movq (%rax), %r14
xorl %r15d, %r15d
movl $0x0, %edx
testq %r14, %r14
jne 0x2dc64
leaq 0x131c8(%rip), %rdi     # 0x40f0b
leaq 0x1e3f6(%rip), %rsi     # 0x4c140
leaq 0x1e771(%rip), %rcx     # 0x4c4c2
movl $0x34d, %edx            # imm = 0x34D
callq 0x30a0
addl $0xfffffc00, %eax       # imm = 0xFFFFFC00
cmpl $0x20, %eax
ja 0x2e0b9
leaq 0x1da34(%rip), %rcx     # 0x4b7a4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpl $0x0, 0x44(%r14)
jne 0x2e0f7
testq %rdx, %rdx
je 0x2e007
movb $0x1, (%rdx)
jmp 0x2e007
cmpl $0x4ff, %eax            # imm = 0x4FF
jne 0x2e0b9
cmpl $0x0, 0x44(%r14)
jne 0x2e2a9
testq %rdx, %rdx
je 0x2e007
movb $-0x1, (%rdx)
jmp 0x2e007
cmpl $0x0, 0x44(%r14)
jne 0x2e116
testq %rdx, %rdx
je 0x2e007
movb $0xc, (%rdx)
jmp 0x2e007
cmpl $0x0, 0x44(%r14)
jne 0x2e135
testq %rdx, %rdx
je 0x2e007
movb $0x9, (%rdx)
jmp 0x2e007
cmpl $0x1, 0x44(%r14)
jne 0x2e154
testq %rdx, %rdx
je 0x2de07
movb $0xd, (%rdx)
movq 0x48(%r14), %rax
movq (%rax), %rax
cmpl $0x200, (%rax)          # imm = 0x200
jne 0x2e2c8
movq 0x10(%rax), %r12
movq %rbx, %rdi
movq %r14, %rsi
movl $0x2, %edx
movq %r12, %rcx
callq 0x6c49
testq %rax, %rax
je 0x2e019
testb $0x1, %r15b
je 0x2e007
cmpb $0x1, 0x40(%rax)
jne 0x2e007
movq 0x50(%rbx), %rax
leaq 0x1e6ce(%rip), %rdx     # 0x4c527
jmp 0x2e024
cmpl $0x0, 0x44(%r14)
jne 0x2e173
testq %rdx, %rdx
je 0x2e007
movb $0x4, (%rdx)
jmp 0x2e007
cmpl $0x0, 0x44(%r14)
jne 0x2e192
testq %rdx, %rdx
je 0x2e007
movb $0x2, (%rdx)
jmp 0x2e007
cmpl $0x0, 0x44(%r14)
jne 0x2e1b1
testq %rdx, %rdx
je 0x2e007
movb $0xa, (%rdx)
jmp 0x2e007
cmpl $0x0, 0x44(%r14)
jne 0x2e1d0
testq %rdx, %rdx
je 0x2e007
movb $0x8, (%rdx)
jmp 0x2e007
cmpl $0x0, 0x44(%r14)
jne 0x2e1ef
testq %rdx, %rdx
je 0x2e007
movb $0x3, (%rdx)
jmp 0x2e007
cmpl $0x0, 0x44(%r14)
jne 0x2e20e
testq %rdx, %rdx
je 0x2e007
movb $0x5, (%rdx)
jmp 0x2e007
cmpl $0x0, 0x44(%r14)
jne 0x2e22d
testq %rdx, %rdx
je 0x2e007
movb $0x6, (%rdx)
jmp 0x2e007
cmpl $0x0, 0x44(%r14)
jne 0x2e24c
testq %rdx, %rdx
je 0x2e007
movb $0x7, (%rdx)
jmp 0x2e007
cmpl $0x0, 0x44(%r14)
jne 0x2e26b
testq %rdx, %rdx
je 0x2e007
movb $0xb, (%rdx)
jmp 0x2e007
cmpl $0x2, 0x44(%r14)
jne 0x2e28a
testq %rdx, %rdx
je 0x2df6d
movb $0xe, (%rdx)
movq 0x48(%r14), %rax
movq (%rax), %r13
cmpl $0x421, (%r13)          # imm = 0x421
jne 0x2e2e7
cmpl $0x0, 0x44(%r13)
jle 0x2dff7
xorl %r15d, %r15d
movq 0x48(%r13), %rax
movq (%rax,%r15,8), %r12
cmpl $0x300, (%r12)          # imm = 0x300
jne 0x2e0d8
movq %rbx, %rdi
movq %r12, %rsi
callq 0x22d3f
testq %rax, %rax
jne 0x2dfe7
movups 0x1e5f4(%rip), %xmm0    # 0x4c5ad
movaps %xmm0, -0x420(%rbp)
movups 0x1e5d6(%rip), %xmm0    # 0x4c59d
movaps %xmm0, -0x430(%rbp)
movw $0x2e, -0x410(%rbp)
movq %rbx, %rdi
movq %r12, %rsi
leaq -0x430(%rbp), %rdx
callq *0x58(%rbx)
incq %r15
movslq 0x44(%r13), %rax
cmpq %rax, %r15
jl 0x2df8c
movq 0x48(%r14), %rax
movq 0x8(%rax), %rsi
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x2dc3e
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x50(%rbx), %rax
leaq 0x1e4f1(%rip), %rdx     # 0x4c515
movq %rbx, %rdi
movq %r12, %rsi
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *%rax
leaq 0x1e5a4(%rip), %rdi     # 0x4c5e8
leaq 0x1e0f5(%rip), %rsi     # 0x4c140
leaq 0x1e470(%rip), %rcx     # 0x4c4c2
movl $0x3ce, %edx            # imm = 0x3CE
callq 0x30a0
leaq 0x10306(%rip), %rdi     # 0x3e369
leaq 0x1e0d6(%rip), %rsi     # 0x4c140
leaq 0x1e451(%rip), %rcx     # 0x4c4c2
movl $0x3d8, %edx            # imm = 0x3D8
callq 0x30a0
leaq 0x1e53d(%rip), %rdi     # 0x4c5bf
leaq 0x1e0b7(%rip), %rsi     # 0x4c140
leaq 0x1e432(%rip), %rcx     # 0x4c4c2
movl $0x3be, %edx            # imm = 0x3BE
callq 0x30a0
leaq 0x13b00(%rip), %rdi     # 0x41ba1
leaq 0x1e098(%rip), %rsi     # 0x4c140
leaq 0x1e413(%rip), %rcx     # 0x4c4c2
movl $0x3e5, %edx            # imm = 0x3E5
callq 0x30a0
leaq 0x102a9(%rip), %rdi     # 0x3e369
leaq 0x1e079(%rip), %rsi     # 0x4c140
leaq 0x1e3f4(%rip), %rcx     # 0x4c4c2
movl $0x3f4, %edx            # imm = 0x3F4
callq 0x30a0
leaq 0x1e491(%rip), %rdi     # 0x4c570
leaq 0x1e05a(%rip), %rsi     # 0x4c140
leaq 0x1e3d5(%rip), %rcx     # 0x4c4c2
movl $0x3ac, %edx            # imm = 0x3AC
callq 0x30a0
leaq 0x1e404(%rip), %rdi     # 0x4c502
leaq 0x1e03b(%rip), %rsi     # 0x4c140
leaq 0x1e3b6(%rip), %rcx     # 0x4c4c2
movl $0x353, %edx            # imm = 0x353
callq 0x30a0
leaq 0x1e3e5(%rip), %rdi     # 0x4c502
leaq 0x1e01c(%rip), %rsi     # 0x4c140
leaq 0x1e397(%rip), %rcx     # 0x4c4c2
movl $0x38a, %edx            # imm = 0x38A
callq 0x30a0
leaq 0x1e3c6(%rip), %rdi     # 0x4c502
leaq 0x1dffd(%rip), %rsi     # 0x4c140
leaq 0x1e378(%rip), %rcx     # 0x4c4c2
movl $0x37b, %edx            # imm = 0x37B
callq 0x30a0
leaq 0x13a46(%rip), %rdi     # 0x41ba1
leaq 0x1dfde(%rip), %rsi     # 0x4c140
leaq 0x1e359(%rip), %rcx     # 0x4c4c2
movl $0x38f, %edx            # imm = 0x38F
callq 0x30a0
leaq 0x1e388(%rip), %rdi     # 0x4c502
leaq 0x1dfbf(%rip), %rsi     # 0x4c140
leaq 0x1e33a(%rip), %rcx     # 0x4c4c2
movl $0x36c, %edx            # imm = 0x36C
callq 0x30a0
leaq 0x1e369(%rip), %rdi     # 0x4c502
leaq 0x1dfa0(%rip), %rsi     # 0x4c140
leaq 0x1e31b(%rip), %rcx     # 0x4c4c2
movl $0x358, %edx            # imm = 0x358
callq 0x30a0
leaq 0x1e34a(%rip), %rdi     # 0x4c502
leaq 0x1df81(%rip), %rsi     # 0x4c140
leaq 0x1e2fc(%rip), %rcx     # 0x4c4c2
movl $0x380, %edx            # imm = 0x380
callq 0x30a0
leaq 0x1e32b(%rip), %rdi     # 0x4c502
leaq 0x1df62(%rip), %rsi     # 0x4c140
leaq 0x1e2dd(%rip), %rcx     # 0x4c4c2
movl $0x371, %edx            # imm = 0x371
callq 0x30a0
leaq 0x1e30c(%rip), %rdi     # 0x4c502
leaq 0x1df43(%rip), %rsi     # 0x4c140
leaq 0x1e2be(%rip), %rcx     # 0x4c4c2
movl $0x362, %edx            # imm = 0x362
callq 0x30a0
leaq 0x1e2ed(%rip), %rdi     # 0x4c502
leaq 0x1df24(%rip), %rsi     # 0x4c140
leaq 0x1e29f(%rip), %rcx     # 0x4c4c2
movl $0x376, %edx            # imm = 0x376
callq 0x30a0
leaq 0x1e2ce(%rip), %rdi     # 0x4c502
leaq 0x1df05(%rip), %rsi     # 0x4c140
leaq 0x1e280(%rip), %rcx     # 0x4c4c2
movl $0x35d, %edx            # imm = 0x35D
callq 0x30a0
leaq 0x1e2af(%rip), %rdi     # 0x4c502
leaq 0x1dee6(%rip), %rsi     # 0x4c140
leaq 0x1e261(%rip), %rcx     # 0x4c4c2
movl $0x367, %edx            # imm = 0x367
callq 0x30a0
leaq 0x1e290(%rip), %rdi     # 0x4c502
leaq 0x1dec7(%rip), %rsi     # 0x4c140
leaq 0x1e242(%rip), %rcx     # 0x4c4c2
movl $0x385, %edx            # imm = 0x385
callq 0x30a0
leaq 0x1e344(%rip), %rdi     # 0x4c5d5
leaq 0x1dea8(%rip), %rsi     # 0x4c140
leaq 0x1e223(%rip), %rcx     # 0x4c4c2
movl $0x3a4, %edx            # imm = 0x3A4
callq 0x30a0
leaq 0x1e252(%rip), %rdi     # 0x4c502
leaq 0x1de89(%rip), %rsi     # 0x4c140
leaq 0x1e204(%rip), %rcx     # 0x4c4c2
movl $0x3ef, %edx            # imm = 0x3EF
callq 0x30a0
leaq 0x134c5(%rip), %rdi     # 0x41794
leaq 0x1de6a(%rip), %rsi     # 0x4c140
leaq 0x1e1e5(%rip), %rcx     # 0x4c4c2
movl $0x394, %edx            # imm = 0x394
callq 0x30a0
leaq 0x1e251(%rip), %rdi     # 0x4c53f
leaq 0x1de4b(%rip), %rsi     # 0x4c140
leaq 0x1e1c6(%rip), %rcx     # 0x4c4c2
movl $0x3a9, %edx            # imm = 0x3A9
callq 0x30a0