// File: /workspace/github_c_makefile_star2_O2/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
movl %ecx, %r15d
movq %rsi, %r14
movq %rdi, %rbx
testq %r14, %r14
je 0x2b5e0
movl (%r14), %eax
cmpl $0x430, %eax            # imm = 0x430
je 0x2b261
cmpl $0x440, %eax            # imm = 0x440
jne 0x2b2ec
cmpl $0x1, 0x44(%r14)
jne 0x2b63d
testq %rdx, %rdx
je 0x2b25b
movb $0x10, (%rdx)
movq 0x48(%r14), %rax
jmp 0x2b2df
movl 0x44(%r14), %eax
decl %eax
cmpl $0x2, %eax
jae 0x2b61e
testq %rdx, %rdx
je 0x2b278
movb $0xf, (%rdx)
movq 0x48(%r14), %rax
movq (%rax), %r15
xorl %r12d, %r12d
movslq 0x44(%r15), %rax
cmpq %rax, %r12
jge 0x2b2cc
movq 0x48(%r15), %rax
movq (%rax,%r12,8), %rax
movl (%rax), %ecx
cmpl $0xa03, %ecx            # imm = 0xA03
je 0x2b2c7
cmpl $0xa02, %ecx            # imm = 0xA02
jne 0x2b5a2
cmpl $0x2, 0x44(%rax)
jne 0x2b5c1
movq 0x48(%rax), %rax
movq 0x8(%rax), %rsi
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x2b20d
incq %r12
jmp 0x2b282
cmpl $0x2, 0x44(%r14)
jne 0x2b56c
movq 0x48(%r14), %rax
addq $0x8, %rax
movq (%rax), %r14
xorl %edx, %edx
xorl %r15d, %r15d
jmp 0x2b22a
leal -0x400(%rax), %ecx
cmpl $0x20, %ecx
ja 0x2b323
leaq 0x1d4d6(%rip), %rax     # 0x487d4
movslq (%rax,%rcx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
cmpl $0x0, 0x44(%r14)
jne 0x2b67b
testq %rdx, %rdx
je 0x2b56c
movb $0x1, (%rdx)
jmp 0x2b56c
cmpl $0x4ff, %eax            # imm = 0x4FF
jne 0x2b5ff
cmpl $0x0, 0x44(%r14)
jne 0x2b86b
testq %rdx, %rdx
je 0x2b56c
movb $-0x1, (%rdx)
jmp 0x2b56c
cmpl $0x0, 0x44(%r14)
jne 0x2b69a
testq %rdx, %rdx
je 0x2b56c
movb $0xb, (%rdx)
jmp 0x2b56c
cmpl $0x0, 0x44(%r14)
jne 0x2b6b9
testq %rdx, %rdx
je 0x2b56c
movb $0xc, (%rdx)
jmp 0x2b56c
cmpl $0x2, 0x44(%r14)
jne 0x2b6d8
testq %rdx, %rdx
je 0x2b395
movb $0xe, (%rdx)
movq 0x48(%r14), %rax
movq (%rax), %r13
cmpl $0x421, (%r13)          # imm = 0x421
jne 0x2b82d
xorl %r15d, %r15d
movslq 0x44(%r13), %rax
cmpq %rax, %r15
jge 0x2b558
movq 0x48(%r13), %rax
movq (%rax,%r15,8), %r12
cmpl $0x300, (%r12)          # imm = 0x300
jne 0x2b65c
movq %rbx, %rdi
movq %r12, %rsi
callq 0x20c3e
testq %rax, %rax
jne 0x2b415
movups 0x1e1f6(%rip), %xmm0    # 0x495dd
movaps %xmm0, -0x420(%rbp)
movups 0x1e1d8(%rip), %xmm0    # 0x495cd
movaps %xmm0, -0x430(%rbp)
movw $0x2e, -0x410(%rbp)
movq %rbx, %rdi
movq %r12, %rsi
leaq -0x430(%rbp), %rdx
callq *0x58(%rbx)
incq %r15
jmp 0x2b3ad
cmpl $0x1, 0x44(%r14)
jne 0x2b6f7
testq %rdx, %rdx
je 0x2b42d
movb $0xd, (%rdx)
movq 0x48(%r14), %rax
movq (%rax), %rax
cmpl $0x200, (%rax)          # imm = 0x200
jne 0x2b84c
movq 0x10(%rax), %r12
pushq $0x2
popq %rdx
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rcx
callq 0x6779
testq %rax, %rax
je 0x2b57e
testb $0x1, %r15b
je 0x2b56c
cmpb $0x1, 0x40(%rax)
jne 0x2b56c
movq 0x50(%rbx), %rax
leaq 0x1e0da(%rip), %rdx     # 0x49557
jmp 0x2b589
cmpl $0x0, 0x44(%r14)
jne 0x2b716
testq %rdx, %rdx
je 0x2b56c
movb $0x7, (%rdx)
jmp 0x2b56c
cmpl $0x0, 0x44(%r14)
jne 0x2b735
testq %rdx, %rdx
je 0x2b56c
movb $0x4, (%rdx)
jmp 0x2b56c
cmpl $0x0, 0x44(%r14)
jne 0x2b754
testq %rdx, %rdx
je 0x2b56c
movb $0x8, (%rdx)
jmp 0x2b56c
cmpl $0x0, 0x44(%r14)
jne 0x2b773
testq %rdx, %rdx
je 0x2b56c
movb $0x5, (%rdx)
jmp 0x2b56c
cmpl $0x0, 0x44(%r14)
jne 0x2b792
testq %rdx, %rdx
je 0x2b56c
movb $0x9, (%rdx)
jmp 0x2b56c
cmpl $0x0, 0x44(%r14)
jne 0x2b7b1
testq %rdx, %rdx
je 0x2b56c
movb $0x3, (%rdx)
jmp 0x2b56c
cmpl $0x0, 0x44(%r14)
jne 0x2b7d0
testq %rdx, %rdx
je 0x2b56c
movb $0x6, (%rdx)
jmp 0x2b56c
cmpl $0x0, 0x44(%r14)
jne 0x2b7ef
testq %rdx, %rdx
je 0x2b56c
movb $0xa, (%rdx)
jmp 0x2b56c
cmpl $0x0, 0x44(%r14)
jne 0x2b80e
testq %rdx, %rdx
je 0x2b56c
movb $0x2, (%rdx)
jmp 0x2b56c
movq 0x48(%r14), %rax
movq 0x8(%rax), %rsi
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x2b20d
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x50(%rbx), %rax
leaq 0x1dfbc(%rip), %rdx     # 0x49545
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 0xfdc0(%rip), %rdi      # 0x3b369
leaq 0x1dbc0(%rip), %rsi     # 0x49170
leaq 0x1df3b(%rip), %rcx     # 0x494f2
movl $0x3d8, %edx            # imm = 0x3D8
callq 0x30a0
leaq 0x1e050(%rip), %rdi     # 0x49618
leaq 0x1dba1(%rip), %rsi     # 0x49170
leaq 0x1df1c(%rip), %rcx     # 0x494f2
movl $0x3ce, %edx            # imm = 0x3CE
callq 0x30a0
leaq 0x12923(%rip), %rdi     # 0x3df0a
leaq 0x1db82(%rip), %rsi     # 0x49170
leaq 0x1defd(%rip), %rcx     # 0x494f2
movl $0x34d, %edx            # imm = 0x34D
callq 0x30a0
leaq 0xfd63(%rip), %rdi      # 0x3b369
leaq 0x1db63(%rip), %rsi     # 0x49170
leaq 0x1dede(%rip), %rcx     # 0x494f2
movl $0x3f4, %edx            # imm = 0x3F4
callq 0x30a0
leaq 0x1dfca(%rip), %rdi     # 0x495ef
leaq 0x1db44(%rip), %rsi     # 0x49170
leaq 0x1debf(%rip), %rcx     # 0x494f2
movl $0x3be, %edx            # imm = 0x3BE
callq 0x30a0
leaq 0x1358f(%rip), %rdi     # 0x3ebd3
leaq 0x1db25(%rip), %rsi     # 0x49170
leaq 0x1dea0(%rip), %rcx     # 0x494f2
movl $0x3e5, %edx            # imm = 0x3E5
callq 0x30a0
leaq 0x1df3d(%rip), %rdi     # 0x495a0
leaq 0x1db06(%rip), %rsi     # 0x49170
leaq 0x1de81(%rip), %rcx     # 0x494f2
movl $0x3ac, %edx            # imm = 0x3AC
callq 0x30a0
leaq 0x1deb0(%rip), %rdi     # 0x49532
leaq 0x1dae7(%rip), %rsi     # 0x49170
leaq 0x1de62(%rip), %rcx     # 0x494f2
movl $0x353, %edx            # imm = 0x353
callq 0x30a0
leaq 0x1de91(%rip), %rdi     # 0x49532
leaq 0x1dac8(%rip), %rsi     # 0x49170
leaq 0x1de43(%rip), %rcx     # 0x494f2
movl $0x385, %edx            # imm = 0x385
callq 0x30a0
leaq 0x1de72(%rip), %rdi     # 0x49532
leaq 0x1daa9(%rip), %rsi     # 0x49170
leaq 0x1de24(%rip), %rcx     # 0x494f2
movl $0x38a, %edx            # imm = 0x38A
callq 0x30a0
leaq 0x1df26(%rip), %rdi     # 0x49605
leaq 0x1da8a(%rip), %rsi     # 0x49170
leaq 0x1de05(%rip), %rcx     # 0x494f2
movl $0x3a4, %edx            # imm = 0x3A4
callq 0x30a0
leaq 0x134d5(%rip), %rdi     # 0x3ebd3
leaq 0x1da6b(%rip), %rsi     # 0x49170
leaq 0x1dde6(%rip), %rcx     # 0x494f2
movl $0x38f, %edx            # imm = 0x38F
callq 0x30a0
leaq 0x1de15(%rip), %rdi     # 0x49532
leaq 0x1da4c(%rip), %rsi     # 0x49170
leaq 0x1ddc7(%rip), %rcx     # 0x494f2
movl $0x367, %edx            # imm = 0x367
callq 0x30a0
leaq 0x1ddf6(%rip), %rdi     # 0x49532
leaq 0x1da2d(%rip), %rsi     # 0x49170
leaq 0x1dda8(%rip), %rcx     # 0x494f2
movl $0x36c, %edx            # imm = 0x36C
callq 0x30a0
leaq 0x1ddd7(%rip), %rdi     # 0x49532
leaq 0x1da0e(%rip), %rsi     # 0x49170
leaq 0x1dd89(%rip), %rcx     # 0x494f2
movl $0x371, %edx            # imm = 0x371
callq 0x30a0
leaq 0x1ddb8(%rip), %rdi     # 0x49532
leaq 0x1d9ef(%rip), %rsi     # 0x49170
leaq 0x1dd6a(%rip), %rcx     # 0x494f2
movl $0x376, %edx            # imm = 0x376
callq 0x30a0
leaq 0x1dd99(%rip), %rdi     # 0x49532
leaq 0x1d9d0(%rip), %rsi     # 0x49170
leaq 0x1dd4b(%rip), %rcx     # 0x494f2
movl $0x37b, %edx            # imm = 0x37B
callq 0x30a0
leaq 0x1dd7a(%rip), %rdi     # 0x49532
leaq 0x1d9b1(%rip), %rsi     # 0x49170
leaq 0x1dd2c(%rip), %rcx     # 0x494f2
movl $0x362, %edx            # imm = 0x362
callq 0x30a0
leaq 0x1dd5b(%rip), %rdi     # 0x49532
leaq 0x1d992(%rip), %rsi     # 0x49170
leaq 0x1dd0d(%rip), %rcx     # 0x494f2
movl $0x35d, %edx            # imm = 0x35D
callq 0x30a0
leaq 0x1dd3c(%rip), %rdi     # 0x49532
leaq 0x1d973(%rip), %rsi     # 0x49170
leaq 0x1dcee(%rip), %rcx     # 0x494f2
movl $0x380, %edx            # imm = 0x380
callq 0x30a0
leaq 0x1dd1d(%rip), %rdi     # 0x49532
leaq 0x1d954(%rip), %rsi     # 0x49170
leaq 0x1dccf(%rip), %rcx     # 0x494f2
movl $0x358, %edx            # imm = 0x358
callq 0x30a0
leaq 0x1dd3b(%rip), %rdi     # 0x4956f
leaq 0x1d935(%rip), %rsi     # 0x49170
leaq 0x1dcb0(%rip), %rcx     # 0x494f2
movl $0x3a9, %edx            # imm = 0x3A9
callq 0x30a0
leaq 0x12f73(%rip), %rdi     # 0x3e7c6
leaq 0x1d916(%rip), %rsi     # 0x49170
leaq 0x1dc91(%rip), %rcx     # 0x494f2
movl $0x394, %edx            # imm = 0x394
callq 0x30a0
leaq 0x1dcc0(%rip), %rdi     # 0x49532
leaq 0x1d8f7(%rip), %rsi     # 0x49170
leaq 0x1dc72(%rip), %rcx     # 0x494f2
movl $0x3ef, %edx            # imm = 0x3EF
callq 0x30a0