// File: /workspace/github_c_makefile_star2_O3/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, %r13
movl %edi, %ebx
callq 0xf8e7
callq 0xa73b
callq 0xc0c1
callq 0xcc72
cmpl $0x1, %ebx
je 0x1e24a
leaq -0x110e5(%rip), %rdx    # 0xcc68
leaq -0xf59a(%rip), %rcx     # 0xe7ba
leaq -0xb8(%rbp), %rdi
movq %rdx, %rsi
movq %rcx, %r8
callq 0xc400
cmpl $0x2, %ebx
jl 0x1e295
movl %ebx, %edx
movl $0x1, %r12d
xorl %ecx, %ecx
leaq 0x1625b(%rip), %rax     # 0x33fdb
movq %rax, -0x48(%rbp)
movq $0x0, -0x58(%rbp)
xorl %r14d, %r14d
movq $0x0, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
movl %ebx, -0x2c(%rbp)
movslq %r12d, %rax
movq (%r13,%rax,8), %r15
cmpb $0x2d, (%r15)
jne 0x1de60
movq %r14, %rbx
movq %rcx, -0x38(%rbp)
movq %r15, %rdi
leaq 0x1615f(%rip), %rsi     # 0x33f24
callq 0xa0f0
testl %eax, %eax
je 0x1de71
movq %r15, %rdi
leaq 0x161ba(%rip), %rsi     # 0x33f96
callq 0xa0f0
testl %eax, %eax
je 0x1dea3
movq %r15, %rdi
leaq 0x16292(%rip), %rsi     # 0x34085
callq 0xa0f0
testl %eax, %eax
je 0x1df29
movq %r15, %rdi
leaq 0x16315(%rip), %rsi     # 0x3411f
callq 0xa0f0
testl %eax, %eax
je 0x1df62
movq %r15, %rdi
leaq 0x16303(%rip), %rsi     # 0x34124
callq 0xa0f0
testl %eax, %eax
jne 0x1e237
cmpq $0x0, -0x40(%rbp)
movq -0x38(%rbp), %rcx
movl -0x2c(%rbp), %edx
jne 0x1e256
incl %r12d
cmpl %edx, %r12d
jge 0x1e25f
movq %rbx, %r14
movslq %r12d, %rax
movq (%r13,%rax,8), %rax
movq %rax, -0x40(%rbp)
jmp 0x1dfdb
testq %rcx, %rcx
jne 0x1e1d2
movq %r15, %rcx
jmp 0x1dfdb
cmpq $0x0, -0x58(%rbp)
jne 0x1e1e6
incl %r12d
movl -0x2c(%rbp), %edx
cmpl %edx, %r12d
movq -0x38(%rbp), %rcx
jge 0x1e1f2
movq %rbx, %r14
movslq %r12d, %rax
movq (%r13,%rax,8), %rax
movq %rax, -0x58(%rbp)
jmp 0x1dfdb
testq %rbx, %rbx
jne 0x1e205
incl %r12d
cmpl -0x2c(%rbp), %r12d
jge 0x1e211
movslq %r12d, %rax
movq (%r13,%rax,8), %r15
movq %r15, %rdi
leaq 0x16110(%rip), %rsi     # 0x33fdb
callq 0xa0f0
testl %eax, %eax
je 0x1df1d
movq %r15, %rdi
leaq 0x16105(%rip), %rsi     # 0x33fe3
callq 0xa0f0
testl %eax, %eax
je 0x1df1d
movq %r15, %rdi
leaq 0x160fb(%rip), %rsi     # 0x33fec
callq 0xa0f0
testl %eax, %eax
je 0x1df1d
movq %r15, %r14
movq %r15, %rdi
leaq 0x160f0(%rip), %rsi     # 0x33ff7
callq 0xa0f0
testl %eax, %eax
jne 0x1e2b8
movq -0x38(%rbp), %rcx
jmp 0x1dfd8
movq -0x38(%rbp), %rcx
movq %r15, %r14
jmp 0x1dfd8
cmpq $0x0, -0x50(%rbp)
jne 0x1e26f
incl %r12d
cmpl -0x2c(%rbp), %r12d
jge 0x1e278
movslq %r12d, %rax
movq (%r13,%rax,8), %rdi
movq %rdi, -0x50(%rbp)
leaq 0x16176(%rip), %rsi     # 0x340ca
callq 0xa0f0
testl %eax, %eax
je 0x1dfd1
jmp 0x1e288
leal 0x1(%r12), %eax
movl -0x2c(%rbp), %ecx
cmpl %ecx, %eax
jge 0x1e224
addl $0x2, %r12d
cmpl %ecx, %r12d
jge 0x1e224
cltq
movq (%r13,%rax,8), %rdi
movslq %r12d, %rax
movq (%r13,%rax,8), %r15
callq 0xe69e
movq %rax, %r14
movq %r15, %rdi
callq 0xe69e
movq %rax, %r15
leaq -0xb8(%rbp), %rdi
movq %r14, %rsi
movl $0x1, %edx
movl $0x1, %ecx
callq 0xc765
leaq -0xb8(%rbp), %rdi
movq %r14, %rsi
movq %r15, %rdx
movl $0x1, %ecx
callq 0xc56c
movq -0x38(%rbp), %rcx
movq %rbx, %r14
movl -0x2c(%rbp), %edx
incl %r12d
cmpl %edx, %r12d
jl 0x1dda2
testq %rcx, %rcx
je 0x1e295
movq -0x58(%rbp), %rbx
testq %rbx, %rbx
leaq 0x10794(%rip), %r13     # 0x2e792
cmoveq %r13, %rbx
testq %r14, %r14
movq -0x48(%rbp), %rax
cmovneq %r14, %rax
movq %rax, -0x48(%rbp)
leaq -0x5c(%rbp), %rsi
movl $0x0, (%rsi)
movq %rcx, -0x38(%rbp)
movq %rcx, %rdi
callq 0x104c5
testq %rax, %rax
movq -0x40(%rbp), %rdi
je 0x1e2aa
movq %rax, %r14
testq %rdi, %rdi
movq %rdi, -0x40(%rbp)
je 0x1e064
leaq 0x161bf(%rip), %rsi     # 0x34206
callq 0xa150
movq %rax, %r12
testq %rax, %rax
jne 0x1e06e
leaq 0x161ad(%rip), %rdi     # 0x34208
movq -0x40(%rbp), %rsi
jmp 0x1e2c2
movq 0x20f6d(%rip), %rax     # 0x3efd8
movq (%rax), %r12
leaq -0x70(%rbp), %r15
movq %r15, %rdi
callq 0xe274
movl -0x5c(%rbp), %ecx
movq %r15, (%rsp)
leaq -0xb8(%rbp), %r15
movq -0x38(%rbp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
movq %r15, %r8
xorl %r9d, %r9d
callq 0x2c9a4
movq %r14, %rdi
callq 0xa1a0
movq %r15, %rdi
movl $0x1, %esi
movl $0x1, %edx
callq 0xc4de
leaq 0x15f2e(%rip), %rsi     # 0x33fec
movq -0x48(%rbp), %r15
movq %r15, %rdi
callq 0xa0f0
testl %eax, %eax
je 0x1e148
leaq 0x15f22(%rip), %rsi     # 0x33ff7
movq %r15, %rdi
callq 0xa0f0
movq -0x68(%rbp), %rcx
testq %rcx, %rcx
cmovneq %rcx, %r13
movl -0x6c(%rbp), %edx
testl %eax, %eax
je 0x1e160
movq -0x38(%rbp), %rdi
movq %r13, %rsi
callq 0x1e3b8
movq %rax, %r14
movq %r12, 0xb8(%rax)
movq %rax, %rdi
callq 0x1e4c7
leaq 0x15ec7(%rip), %rsi     # 0x33fdb
movq %r15, %rdi
callq 0xa0f0
testl %eax, %eax
je 0x1e1c8
leaq 0x15eb8(%rip), %rsi     # 0x33fe3
movq %r15, %rdi
callq 0xa0f0
testl %eax, %eax
jne 0x1e2c9
movq %r14, %rdi
movq %r12, %rsi
callq 0x203c2
jmp 0x1e18d
movq -0x68(%rbp), %rax
testq %rax, %rax
cmovneq %rax, %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0xa0c0
jmp 0x1e18a
movq -0x38(%rbp), %rdi
movq %r13, %rsi
callq 0x1c2b8
movq %rax, %r14
movq %rax, %rdi
callq 0xb717
movq %r14, %rdi
movq %r12, %rsi
callq 0xbc06
movq %r14, %rdi
callq 0xb112
xorl %r14d, %r14d
movq -0x40(%rbp), %rbx
leaq -0x70(%rbp), %rdi
callq 0xe772
testq %rbx, %rbx
je 0x1e1a7
movq %r12, %rdi
callq 0xa070
testq %r14, %r14
je 0x1e1b4
movq %r14, %rdi
callq 0x1e49e
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x126f4
jmp 0x1e18d
leaq 0x15fb2(%rip), %rdi     # 0x3418b
movq %rcx, %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x1e303
leaq 0x15d40(%rip), %rdi     # 0x33f2d
jmp 0x1e2b1
leaq 0x15d78(%rip), %rdi     # 0x33f71
leaq 0x15d24(%rip), %rsi     # 0x33f24
jmp 0x1e2c2
leaq 0x15d92(%rip), %rdi     # 0x33f9e
jmp 0x1e2b1
leaq 0x15d59(%rip), %rdi     # 0x33f71
leaq 0x15d77(%rip), %rsi     # 0x33f96
jmp 0x1e2c2
leaq 0x15d46(%rip), %rdi     # 0x33f71
leaq 0x15eed(%rip), %rsi     # 0x3411f
jmp 0x1e2c2
leaq 0x15f2b(%rip), %rsi     # 0x34169
movq %r15, %rdi
callq 0xa0f0
testl %eax, %eax
jne 0x1e29e
callq 0x1e2e8
xorl %edi, %edi
callq 0xa170
leaq 0x15ecf(%rip), %rdi     # 0x3412c
jmp 0x1e2b1
leaq 0x15d0b(%rip), %rdi     # 0x33f71
leaq 0x15eb7(%rip), %rsi     # 0x34124
jmp 0x1e2c2
leaq 0x15e17(%rip), %rdi     # 0x3408d
jmp 0x1e2b1
leaq 0x15cf2(%rip), %rdi     # 0x33f71
leaq 0x15dff(%rip), %rsi     # 0x34085
jmp 0x1e2c2
leaq 0x15e40(%rip), %rdi     # 0x340cf
movq -0x50(%rbp), %rsi
jmp 0x1e2c2
leaq 0x15f2f(%rip), %rdi     # 0x341cb
jmp 0x1e2b1
leaq 0x15eca(%rip), %rdi     # 0x3416f
movq %r15, %rsi
jmp 0x1e2c2
leaq 0x15f3c(%rip), %rdi     # 0x341ed
xorl %eax, %eax
callq 0x1e303
leaq 0x15d44(%rip), %rdi     # 0x34003
movq %r14, %rsi
xorl %eax, %eax
callq 0x1e303
leaq 0x100a0(%rip), %rdi     # 0x2e370
leaq 0x15f5f(%rip), %rsi     # 0x34236
leaq 0x15f72(%rip), %rcx     # 0x34250
movl $0xf2, %edx
callq 0xa0b0