// File: /workspace/github_c_makefile_star2_O2/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 $0xa8, %rsp
movq %rsi, %r13
movl %edi, %ebx
callq 0xf3e2
callq 0xa633
callq 0xbe10
callq 0xc949
movl %ebx, -0x2c(%rbp)
cmpl $0x1, %ebx
je 0x1d4a3
leaq -0x10675(%rip), %rdx    # 0xc93f
leaq -0xecc1(%rip), %rcx     # 0xe2fa
leaq -0xc0(%rbp), %rdi
movq %rdx, %rsi
movq %rcx, %r8
callq 0xc13c
pushq $0x1
popq %rax
movq $0x0, -0x40(%rbp)
leaq 0x1600c(%rip), %rcx     # 0x32feb
movq %rcx, -0x48(%rbp)
movl %eax, %r12d
movq $0x0, -0x50(%rbp)
xorl %r14d, %r14d
movq $0x0, -0x58(%rbp)
xorl %ecx, %ecx
movl -0x2c(%rbp), %edx
cmpl %edx, %r12d
jge 0x1d248
movslq %r12d, %rax
movq (%r13,%rax,8), %r15
cmpb $0x2d, (%r15)
jne 0x1d0c5
movq %r14, %rbx
movq %rcx, -0x38(%rbp)
movq %r15, %rdi
leaq 0x15f0a(%rip), %rsi     # 0x32f34
callq 0xa0e0
testl %eax, %eax
je 0x1d0d6
movq %r15, %rdi
leaq 0x15f65(%rip), %rsi     # 0x32fa6
callq 0xa0e0
testl %eax, %eax
je 0x1d108
movq %r15, %rdi
leaq 0x1603d(%rip), %rsi     # 0x33095
callq 0xa0e0
testl %eax, %eax
je 0x1d18e
movq %r15, %rdi
leaq 0x160c0(%rip), %rsi     # 0x3312f
callq 0xa0e0
testl %eax, %eax
je 0x1d1c7
movq %r15, %rdi
leaq 0x160ae(%rip), %rsi     # 0x33134
callq 0xa0e0
testl %eax, %eax
jne 0x1d490
cmpq $0x0, -0x40(%rbp)
movq -0x38(%rbp), %rcx
movl -0x2c(%rbp), %edx
jne 0x1d4bf
incl %r12d
cmpl %edx, %r12d
jge 0x1d4af
movq %rbx, %r14
movslq %r12d, %rax
movq (%r13,%rax,8), %rax
movq %rax, -0x40(%rbp)
jmp 0x1d240
testq %rcx, %rcx
jne 0x1d42b
movq %r15, %rcx
jmp 0x1d240
cmpq $0x0, -0x58(%rbp)
jne 0x1d43f
incl %r12d
movl -0x2c(%rbp), %edx
cmpl %edx, %r12d
movq -0x38(%rbp), %rcx
jge 0x1d44b
movq %rbx, %r14
movslq %r12d, %rax
movq (%r13,%rax,8), %rax
movq %rax, -0x58(%rbp)
jmp 0x1d240
testq %rbx, %rbx
jne 0x1d45e
incl %r12d
cmpl -0x2c(%rbp), %r12d
jge 0x1d46a
movslq %r12d, %rax
movq (%r13,%rax,8), %r15
movq %r15, %rdi
leaq 0x15ebb(%rip), %rsi     # 0x32feb
callq 0xa0e0
testl %eax, %eax
je 0x1d182
movq %r15, %rdi
leaq 0x15eb0(%rip), %rsi     # 0x32ff3
callq 0xa0e0
testl %eax, %eax
je 0x1d182
movq %r15, %rdi
leaq 0x15ea6(%rip), %rsi     # 0x32ffc
callq 0xa0e0
testl %eax, %eax
je 0x1d182
movq %r15, %r14
movq %r15, %rdi
leaq 0x15e9b(%rip), %rsi     # 0x33007
callq 0xa0e0
testl %eax, %eax
jne 0x1d511
movq -0x38(%rbp), %rcx
jmp 0x1d23d
movq -0x38(%rbp), %rcx
movq %r15, %r14
jmp 0x1d23d
cmpq $0x0, -0x50(%rbp)
jne 0x1d4d5
incl %r12d
cmpl -0x2c(%rbp), %r12d
jge 0x1d4de
movslq %r12d, %rax
movq (%r13,%rax,8), %rdi
movq %rdi, -0x50(%rbp)
leaq 0x15f21(%rip), %rsi     # 0x330da
callq 0xa0e0
testl %eax, %eax
je 0x1d236
jmp 0x1d4c8
leal 0x1(%r12), %eax
movl -0x2c(%rbp), %ecx
cmpl %ecx, %eax
jge 0x1d47d
addl $0x2, %r12d
cmpl %ecx, %r12d
jge 0x1d47d
cltq
movq (%r13,%rax,8), %rdi
movslq %r12d, %rax
movq (%r13,%rax,8), %r15
callq 0xe1f8
movq %rax, %r14
movq %r15, %rdi
callq 0xe1f8
movq %rax, -0x78(%rbp)
leaq -0xc0(%rbp), %rdi
movq %r14, %rsi
pushq $0x1
popq %r15
movl %r15d, %edx
movl %r15d, %ecx
callq 0xc477
leaq -0xc0(%rbp), %rdi
movq %r14, %rsi
movq -0x78(%rbp), %rdx
movl %r15d, %ecx
callq 0xc29d
movq -0x38(%rbp), %rcx
movq %rbx, %r14
movl -0x2c(%rbp), %edx
incl %r12d
jmp 0x1cffe
testq %rcx, %rcx
je 0x1d4fa
movq -0x58(%rbp), %rbx
testq %rbx, %rbx
leaq 0x10533(%rip), %r13     # 0x2d792
cmoveq %r13, %rbx
testq %r14, %r14
movq -0x48(%rbp), %rax
cmovneq %r14, %rax
movq %rax, -0x48(%rbp)
leaq -0x5c(%rbp), %rsi
andl $0x0, (%rsi)
movq %rcx, -0x38(%rbp)
movq %rcx, %rdi
callq 0xfe3a
testq %rax, %rax
movq -0x40(%rbp), %rdi
je 0x1d503
movq %rax, %r14
testq %rdi, %rdi
movq %rdi, -0x40(%rbp)
je 0x1d2c2
leaq 0x15f71(%rip), %rsi     # 0x33216
callq 0xa140
movq %rax, %r12
testq %rax, %rax
jne 0x1d2cc
leaq 0x15f5f(%rip), %rdi     # 0x33218
movq -0x40(%rbp), %rsi
jmp 0x1d51b
movq 0x20d0f(%rip), %rax     # 0x3dfd8
movq (%rax), %r12
leaq -0x70(%rbp), %r15
movq %r15, %rdi
callq 0xde1c
movl -0x5c(%rbp), %ecx
movq %r15, (%rsp)
leaq -0xc0(%rbp), %r15
movq -0x38(%rbp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
movq %r15, %r8
xorl %r9d, %r9d
callq 0x2b2d8
movq %r14, %rdi
callq 0xa190
pushq $0x1
popq %rdx
movq %r15, %rdi
movl %edx, %esi
callq 0xc214
leaq 0x15ce5(%rip), %rsi     # 0x32ffc
movq -0x48(%rbp), %r15
movq %r15, %rdi
callq 0xa0e0
testl %eax, %eax
je 0x1d3a1
leaq 0x15cd9(%rip), %rsi     # 0x33007
movq %r15, %rdi
callq 0xa0e0
movq -0x68(%rbp), %rcx
testq %rcx, %rcx
cmovneq %rcx, %r13
movl -0x6c(%rbp), %edx
testl %eax, %eax
je 0x1d3b9
movq -0x38(%rbp), %rdi
movq %r13, %rsi
callq 0x1d60c
movq %rax, %r14
movq %r12, 0xb8(%rax)
movq %rax, %rdi
callq 0x1d716
leaq 0x15c7e(%rip), %rsi     # 0x32feb
movq %r15, %rdi
callq 0xa0e0
testl %eax, %eax
je 0x1d421
leaq 0x15c6f(%rip), %rsi     # 0x32ff3
movq %r15, %rdi
callq 0xa0e0
testl %eax, %eax
jne 0x1d522
movq %r14, %rdi
movq %r12, %rsi
callq 0x1f72c
jmp 0x1d3e6
movq -0x68(%rbp), %rax
testq %rax, %rax
cmovneq %rax, %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0xa0c0
jmp 0x1d3e3
movq -0x38(%rbp), %rdi
movq %r13, %rsi
callq 0x1b5b0
movq %rax, %r14
movq %rax, %rdi
callq 0xb4d1
movq %r14, %rdi
movq %r12, %rsi
callq 0xb9a8
movq %r14, %rdi
callq 0xaf21
xorl %r14d, %r14d
movq -0x40(%rbp), %rbx
leaq -0x70(%rbp), %rdi
callq 0xe2b9
testq %rbx, %rbx
je 0x1d400
movq %r12, %rdi
callq 0xa070
testq %r14, %r14
je 0x1d40d
movq %r14, %rdi
callq 0x1d6ed
xorl %eax, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x11d20
jmp 0x1d3e6
leaq 0x15d69(%rip), %rdi     # 0x3319b
movq %rcx, %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x1d55c
leaq 0x15af7(%rip), %rdi     # 0x32f3d
jmp 0x1d50a
leaq 0x15b2f(%rip), %rdi     # 0x32f81
leaq 0x15adb(%rip), %rsi     # 0x32f34
jmp 0x1d51b
leaq 0x15b49(%rip), %rdi     # 0x32fae
jmp 0x1d50a
leaq 0x15b10(%rip), %rdi     # 0x32f81
leaq 0x15b2e(%rip), %rsi     # 0x32fa6
jmp 0x1d51b
leaq 0x15afd(%rip), %rdi     # 0x32f81
leaq 0x15ca4(%rip), %rsi     # 0x3312f
jmp 0x1d51b
leaq 0x15ce2(%rip), %rsi     # 0x33179
movq %r15, %rdi
callq 0xa0e0
testl %eax, %eax
jne 0x1d4ee
callq 0x1d541
xorl %edi, %edi
callq 0xa160
leaq 0x15acb(%rip), %rdi     # 0x32f81
leaq 0x15c77(%rip), %rsi     # 0x33134
jmp 0x1d51b
leaq 0x15c76(%rip), %rdi     # 0x3313c
jmp 0x1d50a
leaq 0x15c10(%rip), %rdi     # 0x330df
movq -0x50(%rbp), %rsi
jmp 0x1d51b
leaq 0x15bc1(%rip), %rdi     # 0x3309d
jmp 0x1d50a
leaq 0x15a9c(%rip), %rdi     # 0x32f81
leaq 0x15ba9(%rip), %rsi     # 0x33095
jmp 0x1d51b
leaq 0x15c8a(%rip), %rdi     # 0x3317f
movq %r15, %rsi
jmp 0x1d51b
leaq 0x15cda(%rip), %rdi     # 0x331db
jmp 0x1d50a
leaq 0x15cf3(%rip), %rdi     # 0x331fd
xorl %eax, %eax
callq 0x1d55c
leaq 0x15afb(%rip), %rdi     # 0x33013
movq %r14, %rsi
xorl %eax, %eax
callq 0x1d55c
leaq 0xfe47(%rip), %rdi      # 0x2d370
leaq 0x15d16(%rip), %rsi     # 0x33246
leaq 0x15d29(%rip), %rcx     # 0x33260
movl $0xf2, %edx
callq 0xa0b0