// File: /workspace/github_c_makefile_star2_O1/Ihsoh[P]ilang/compiler/assembler/main.c
// Function: main
int main(int argc, char *argv[]) {
	ParserContext *ctx = NULL;

	char *opt_file = NULL;
	char *opt_incpath = NULL;
	char *opt_action = NULL;
	char *opt_target = NULL;
	char *opt_output = NULL;

	// 跑测试。
	_test();

	// 没有任何参数时直接打印帮助信息。
	if (argc == 1) {
		_help();
		exit(0);
	}

	Map defs;
	map_init(
		&defs,
		map_rstr_comparer, map_rstr_comparer,
		(MapReleaser) rstr_delete, (MapReleaser) rstr_delete
	);

	for (int i = 1; i < argc; i++) {
		#define	_NEXT_ARG(opt_name) { if (i + 1 < argc) { opt = argv[++i]; } else { _error("the option '%s' expects a parameter.", opt_name); } }

		char *opt = argv[i];

		if (*opt == '-') {
			if (strcmp(opt, _OPT_INCPATH) == 0) {
				if (opt_incpath != NULL) {
					_error("the include path option '-incpath' cannot be specified multi-times.");
				}
				_NEXT_ARG(_OPT_INCPATH)
				opt_incpath = opt;
			} else if (strcmp(opt, _OPT_ACTION) == 0) {
				if (opt_action != NULL) {
					_error("the action option '-action' cannot be specified multi-times.");
				}
				_NEXT_ARG(_OPT_ACTION)
				opt_action = opt;
				if (strcmp(opt_action, _OPT_ACTION_COMPILE) != 0
						&& strcmp(opt_action, _OPT_ACTION_PRINTAST) != 0
						&& strcmp(opt_action, _OPT_ACTION_PREPROCESS) != 0
						&& strcmp(opt_action, _OPT_ACTION_PRINTTOKENS) != 0) {
					_error(
						"the option '-action' is specified a invalid value '%s', maybe specify a 'compile' or 'printast' or 'preprocess' or 'printtokens'.",
						opt_action
					);
				}
			} else if (strcmp(opt, _OPT_TARGET) == 0) {
				if (opt_target != NULL) {
					_error("the target option '-target' cannot be specified multi-times.");
				}
				_NEXT_ARG(_OPT_TARGET)
				opt_target = opt;
				if (strcmp(opt_target, _OPT_TARGET_FLAT) != 0) {
					_error(
						"the option '-target' is specified a invalid value '%s', maybe specify a 'flat'.",
						opt_target
					);
				}
			} else if (strcmp(opt, _OPT_DEF) == 0) {
				_NEXT_ARG(_OPT_DEF)
				const char *name = opt;
				_NEXT_ARG(_OPT_DEF)
				const char *value = opt;

				ResizableString *rstr_name = rstr_new_with_cstr(name);
				ResizableString *rstr_value = rstr_new_with_cstr(value);

				map_remove(&defs, rstr_name, true, true);
				map_set(&defs, rstr_name, rstr_value, true);
			} else if (strcmp(opt, _OPT_OUTPUT) == 0) {
				if (opt_output != NULL) {
					_error("the output option '-output' cannot be specified multi-times.");
				}
				_NEXT_ARG(_OPT_OUTPUT)
				opt_output = opt;
			} else if (strcmp(opt, _OPT_HELP) == 0) {
				_help();
				exit(0);
			} else {
				_error(
					"the option '%s' is invalid.",
					opt
				);
			}
		} else {
			if (opt_file != NULL) {
				_error(
					"the source file cannot be specified multi-times, '%s' and '%s'.",
					opt_file,
					opt
				);
			}
			opt_file = opt;
		}

		#undef	_NEXT_ARG
	}

	if (opt_file == NULL) {
		_error("the source file not be specified.");
	}
	if (opt_incpath == NULL) {
		opt_incpath = "";
	}
	if (opt_action == NULL) {
		opt_action = _OPT_ACTION_COMPILE;
	}
	if (opt_target == NULL) {
		opt_target = _OPT_TARGET_FLAT;
	}

	// 读取源文件。
	const char *source = NULL;
	int len = 0;
	source = (const char *)util_get_file_content(opt_file, &len);
	if (source == NULL) {
		_error("cannot open source file.");
	}

	// 打开输出文件。
	FILE *output = stdout;
	if (opt_output != NULL) {
		output = fopen(opt_output, "w");
		if (output == NULL) {
			_error("cannot write target code to target file '%s'.", opt_output);
		}
	}

	// 预处理。
	ResizableString preprocessed_source;
	rstr_init(&preprocessed_source);
	pp_process(
		opt_file, opt_incpath, source, len, &defs, NULL, &preprocessed_source
	);
	free((void *)source);

	map_free(&defs, true, true);

	if (strcmp(opt_action, _OPT_ACTION_PREPROCESS) == 0) {
		fputs(RSTR_CSTR(&preprocessed_source), output);
		goto _end;
	}

	if (strcmp(opt_action, _OPT_ACTION_PRINTTOKENS) == 0) {
		// 词法分析。
		LexerContext *lexctx = asm_lexer_new_context(
			opt_file, RSTR_CSTR(&preprocessed_source), RSTR_LEN(&preprocessed_source)
		);
		lexer_parse(lexctx);
		lexer_print_tokens(lexctx, output);
		lexer_free_context(lexctx);
	} else {
		// 语法解析。
		ctx = asm_parser_new_context(
			opt_file, RSTR_CSTR(&preprocessed_source), RSTR_LEN(&preprocessed_source)
		);
		ASM_PARSER_CONTEXT_DATA_SET_OUT(ctx, output);
		asm_parser_parse(ctx);

		if (strcmp(opt_action, _OPT_ACTION_COMPILE) == 0) {
			enc_encode(ctx);
		} else if (strcmp(opt_action, _OPT_ACTION_PRINTAST) == 0) {
			// 打印语法树。
			asm_parser_print_ast(ctx, output);
		} else {
			assert(0);
		}
	}

// 释放资源。
_end:
	rstr_free(&preprocessed_source);
	if (opt_output != NULL) {
		fclose(output);
	}
	if (ctx != NULL) {
		asm_parser_free_context(ctx);
	}

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rsi, %r12
movl %edi, %r13d
callq 0xfadb
callq 0xa729
callq 0xc15d
callq 0xcd62
cmpl $0x1, %r13d
je 0x1e60d
leaq -0x113b7(%rip), %rdx    # 0xcd58
leaq -0xf776(%rip), %rcx     # 0xe9a0
leaq -0xb0(%rbp), %rdi
movq %rdx, %rsi
movq %rcx, %r8
callq 0xc4ec
cmpl $0x2, %r13d
jl 0x1e386
movl $0x1, %r15d
xorl %ecx, %ecx
movq $0x0, -0x40(%rbp)
xorl %ebx, %ebx
movq $0x0, -0x50(%rbp)
movq $0x0, -0x38(%rbp)
movslq %r15d, %rax
movq (%r12,%rax,8), %r14
cmpb $0x2d, (%r14)
jne 0x1e207
movq %rcx, -0x30(%rbp)
movq %r14, %rdi
leaq 0x16e01(%rip), %rsi     # 0x34f74
callq 0xa0e0
testl %eax, %eax
je 0x1e218
movq %r14, %rdi
leaq 0x16e5c(%rip), %rsi     # 0x34fe6
callq 0xa0e0
testl %eax, %eax
je 0x1e243
movq %r14, %rdi
leaq 0x16f34(%rip), %rsi     # 0x350d5
callq 0xa0e0
testl %eax, %eax
je 0x1e2cc
movq %r14, %rdi
leaq 0x16fb7(%rip), %rsi     # 0x3516f
callq 0xa0e0
testl %eax, %eax
je 0x1e303
movq %r14, %rdi
leaq 0x16fa5(%rip), %rsi     # 0x35174
callq 0xa0e0
testl %eax, %eax
jne 0x1e5fa
cmpq $0x0, -0x38(%rbp)
movq -0x30(%rbp), %rcx
jne 0x1e619
incl %r15d
cmpl %r13d, %r15d
jge 0x1e622
movslq %r15d, %rax
movq (%r12,%rax,8), %rax
movq %rax, -0x38(%rbp)
jmp 0x1e378
testq %rcx, %rcx
jne 0x1e595
movq %r14, %rcx
jmp 0x1e378
cmpq $0x0, -0x40(%rbp)
jne 0x1e5a9
incl %r15d
cmpl %r13d, %r15d
movq -0x30(%rbp), %rcx
jge 0x1e5b5
movslq %r15d, %rax
movq (%r12,%rax,8), %rax
movq %rax, -0x40(%rbp)
jmp 0x1e378
testq %rbx, %rbx
jne 0x1e5c8
incl %r15d
cmpl %r13d, %r15d
jge 0x1e5d4
movslq %r15d, %rax
movq (%r12,%rax,8), %rbx
movq %rbx, %rdi
leaq 0x16dc2(%rip), %rsi     # 0x3502b
callq 0xa0e0
testl %eax, %eax
je 0x1e374
movq %rbx, %rdi
leaq 0x16db3(%rip), %rsi     # 0x35033
callq 0xa0e0
testl %eax, %eax
je 0x1e374
movq %rbx, %rdi
leaq 0x16da5(%rip), %rsi     # 0x3503c
callq 0xa0e0
testl %eax, %eax
je 0x1e374
movq %rbx, -0x48(%rbp)
movq %rbx, %rdi
leaq 0x16d95(%rip), %rsi     # 0x35047
callq 0xa0e0
testl %eax, %eax
jne 0x1e67b
movq -0x30(%rbp), %rcx
movq -0x48(%rbp), %rbx
jmp 0x1e378
cmpq $0x0, -0x50(%rbp)
jne 0x1e632
incl %r15d
cmpl %r13d, %r15d
jge 0x1e63b
movslq %r15d, %rax
movq (%r12,%rax,8), %rdi
movq %rdi, -0x50(%rbp)
leaq 0x16e25(%rip), %rsi     # 0x3511a
callq 0xa0e0
testl %eax, %eax
je 0x1e374
jmp 0x1e64b
leal 0x1(%r15), %eax
cmpl %r13d, %eax
jge 0x1e5e7
addl $0x2, %r15d
cmpl %r13d, %r15d
jge 0x1e5e7
cltq
movq (%r12,%rax,8), %rdi
movslq %r15d, %rax
movq (%r12,%rax,8), %rax
movq %rax, -0x48(%rbp)
callq 0xe884
movq %rax, %r14
movq -0x48(%rbp), %rdi
callq 0xe884
movq %rax, -0x48(%rbp)
leaq -0xb0(%rbp), %rdi
movq %r14, %rsi
movl $0x1, %edx
movl $0x1, %ecx
callq 0xc855
leaq -0xb0(%rbp), %rdi
movq %r14, %rsi
movq -0x48(%rbp), %rdx
movl $0x1, %ecx
callq 0xc658
movq -0x30(%rbp), %rcx
incl %r15d
cmpl %r13d, %r15d
jl 0x1e154
jmp 0x1e39a
movq $0x0, -0x38(%rbp)
xorl %ebx, %ebx
movq $0x0, -0x40(%rbp)
xorl %ecx, %ecx
testq %rcx, %rcx
je 0x1e658
movq -0x40(%rbp), %r15
testq %r15, %r15
leaq 0x11405(%rip), %r13     # 0x2f7b6
cmoveq %r13, %r15
testq %rbx, %rbx
leaq 0x16c6c(%rip), %r12     # 0x3502b
cmovneq %rbx, %r12
leaq -0x54(%rbp), %rsi
movl $0x0, (%rsi)
movq %rcx, -0x30(%rbp)
movq %rcx, %rdi
callq 0x106d5
testq %rax, %rax
je 0x1e661
movq %rax, %r14
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
je 0x1e413
leaq 0x16e61(%rip), %rsi     # 0x35256
callq 0xa140
movq %rax, -0x40(%rbp)
testq %rax, %rax
jne 0x1e421
leaq 0x16e4e(%rip), %rdi     # 0x35258
movq -0x38(%rbp), %rsi
jmp 0x1e686
movq 0x21bbe(%rip), %rax     # 0x3ffd8
movq (%rax), %rax
movq %rax, -0x40(%rbp)
leaq -0x68(%rbp), %rbx
movq %rbx, %rdi
callq 0xe468
movl -0x54(%rbp), %ecx
movq %rbx, (%rsp)
leaq -0xb0(%rbp), %rbx
movq -0x30(%rbp), %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %rbx, %r8
xorl %r9d, %r9d
callq 0x2d45c
movq %r14, %rdi
callq 0xa190
movq %rbx, %rdi
movl $0x1, %esi
movl $0x1, %edx
callq 0xc5ca
leaq 0x16bcb(%rip), %rsi     # 0x3503c
movq %r12, %rdi
callq 0xa0e0
testl %eax, %eax
je 0x1e503
leaq 0x16bbf(%rip), %rsi     # 0x35047
movq %r12, %rdi
callq 0xa0e0
movq -0x60(%rbp), %rcx
testq %rcx, %rcx
cmovneq %rcx, %r13
movl -0x64(%rbp), %edx
testl %eax, %eax
je 0x1e51f
movq -0x30(%rbp), %rdi
movq %r13, %rsi
callq 0x1e77c
movq %rax, %r14
movq -0x40(%rbp), %r13
movq %r13, 0xb8(%rax)
movq %rax, %rdi
callq 0x1e88b
leaq 0x16b60(%rip), %rsi     # 0x3502b
movq %r12, %rdi
callq 0xa0e0
testl %eax, %eax
je 0x1e58b
leaq 0x16b51(%rip), %rsi     # 0x35033
movq %r12, %rdi
callq 0xa0e0
testl %eax, %eax
movq -0x38(%rbp), %r15
jne 0x1e68d
movq %r14, %rdi
movq %r13, %rsi
callq 0x1ec74
jmp 0x1e554
movq -0x60(%rbp), %rax
testq %rax, %rax
cmovneq %rax, %r13
movq %r13, %rdi
movq -0x40(%rbp), %r13
movq %r13, %rsi
callq 0xa0c0
jmp 0x1e54d
movq -0x30(%rbp), %rdi
movq %r13, %rsi
callq 0x1c644
movq %rax, %rbx
movq %rax, %rdi
callq 0xb6e0
movq %rbx, %rdi
movq -0x40(%rbp), %r13
movq %r13, %rsi
callq 0xbc4b
movq %rbx, %rdi
callq 0xb119
xorl %r14d, %r14d
movq -0x38(%rbp), %r15
leaq -0x68(%rbp), %rdi
callq 0xe958
testq %r15, %r15
je 0x1e56a
movq %r13, %rdi
callq 0xa070
testq %r14, %r14
je 0x1e577
movq %r14, %rdi
callq 0x1e862
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x1297c
jmp 0x1e550
leaq 0x16c3f(%rip), %rdi     # 0x351db
movq %rcx, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x1e6c7
leaq 0x169cd(%rip), %rdi     # 0x34f7d
jmp 0x1e668
leaq 0x16a05(%rip), %rdi     # 0x34fc1
leaq 0x169b1(%rip), %rsi     # 0x34f74
jmp 0x1e686
leaq 0x16a1f(%rip), %rdi     # 0x34fee
jmp 0x1e668
leaq 0x169e6(%rip), %rdi     # 0x34fc1
leaq 0x16a04(%rip), %rsi     # 0x34fe6
jmp 0x1e686
leaq 0x169d3(%rip), %rdi     # 0x34fc1
leaq 0x16b7a(%rip), %rsi     # 0x3516f
jmp 0x1e686
leaq 0x16bb8(%rip), %rsi     # 0x351b9
movq %r14, %rdi
callq 0xa0e0
testl %eax, %eax
jne 0x1e66f
callq 0x1e6ac
xorl %edi, %edi
callq 0xa160
leaq 0x16b5c(%rip), %rdi     # 0x3517c
jmp 0x1e668
leaq 0x16998(%rip), %rdi     # 0x34fc1
leaq 0x16b44(%rip), %rsi     # 0x35174
jmp 0x1e686
leaq 0x16aa4(%rip), %rdi     # 0x350dd
jmp 0x1e668
leaq 0x1697f(%rip), %rdi     # 0x34fc1
leaq 0x16a8c(%rip), %rsi     # 0x350d5
jmp 0x1e686
leaq 0x16acd(%rip), %rdi     # 0x3511f
movq -0x50(%rbp), %rsi
jmp 0x1e686
leaq 0x16bbc(%rip), %rdi     # 0x3521b
jmp 0x1e668
leaq 0x16bd5(%rip), %rdi     # 0x3523d
xorl %eax, %eax
callq 0x1e6c7
leaq 0x16b49(%rip), %rdi     # 0x351bf
movq %r14, %rsi
jmp 0x1e686
leaq 0x169d1(%rip), %rdi     # 0x35053
movq -0x48(%rbp), %rsi
xorl %eax, %eax
callq 0x1e6c7
leaq 0x10d00(%rip), %rdi     # 0x2f394
leaq 0x16beb(%rip), %rsi     # 0x35286
leaq 0x16bfe(%rip), %rcx     # 0x352a0
movl $0xf2, %edx
callq 0xa0b0