// File: /workspace/github_c_makefile_star2_O0/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
subq $0x120, %rsp            # imm = 0x120
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
callq 0x29d50
cmpl $0x1, -0x8(%rbp)
jne 0x29560
callq 0x29d70
xorl %edi, %edi
callq 0xa180
leaq -0x88(%rbp), %rdi
leaq -0x1b60e(%rip), %rdx    # 0xdf60
leaq -0x191d5(%rip), %r8     # 0x103a0
movq %rdx, %rsi
movq %r8, %rcx
callq 0xd210
movl $0x1, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x29a0e
movq -0x10(%rbp), %rax
movslq -0x8c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2d, %eax
jne 0x299cd
movq -0x98(%rbp), %rdi
leaq 0x23d60(%rip), %rsi     # 0x4d330
callq 0xa0f0
cmpl $0x0, %eax
jne 0x29645
cmpq $0x0, -0x28(%rbp)
je 0x295ef
leaq 0x23d51(%rip), %rdi     # 0x4d339
movb $0x0, %al
callq 0x29da0
movl -0x8c(%rbp), %eax
addl $0x1, %eax
cmpl -0x8(%rbp), %eax
jge 0x29620
movq -0x10(%rbp), %rax
movl -0x8c(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x8c(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x98(%rbp)
jmp 0x29635
leaq 0x23d56(%rip), %rdi     # 0x4d37d
leaq 0x23d02(%rip), %rsi     # 0x4d330
movb $0x0, %al
callq 0x29da0
movq -0x98(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0x299cb
movq -0x98(%rbp), %rdi
leaq 0x23d4f(%rip), %rsi     # 0x4d3a2
callq 0xa0f0
cmpl $0x0, %eax
jne 0x29732
cmpq $0x0, -0x30(%rbp)
je 0x29676
leaq 0x23d3b(%rip), %rdi     # 0x4d3aa
movb $0x0, %al
callq 0x29da0
movl -0x8c(%rbp), %eax
addl $0x1, %eax
cmpl -0x8(%rbp), %eax
jge 0x296a7
movq -0x10(%rbp), %rax
movl -0x8c(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x8c(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x98(%rbp)
jmp 0x296bc
leaq 0x23ccf(%rip), %rdi     # 0x4d37d
leaq 0x23ced(%rip), %rsi     # 0x4d3a2
movb $0x0, %al
callq 0x29da0
movq -0x98(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
leaq 0x23d15(%rip), %rsi     # 0x4d3e7
callq 0xa0f0
cmpl $0x0, %eax
je 0x2972d
movq -0x30(%rbp), %rdi
leaq 0x23d08(%rip), %rsi     # 0x4d3ef
callq 0xa0f0
cmpl $0x0, %eax
je 0x2972d
movq -0x30(%rbp), %rdi
leaq 0x23cfc(%rip), %rsi     # 0x4d3f8
callq 0xa0f0
cmpl $0x0, %eax
je 0x2972d
movq -0x30(%rbp), %rdi
leaq 0x23cf2(%rip), %rsi     # 0x4d403
callq 0xa0f0
cmpl $0x0, %eax
je 0x2972d
movq -0x30(%rbp), %rsi
leaq 0x23ce9(%rip), %rdi     # 0x4d40f
movb $0x0, %al
callq 0x29da0
jmp 0x299c9
movq -0x98(%rbp), %rdi
leaq 0x23d51(%rip), %rsi     # 0x4d491
callq 0xa0f0
cmpl $0x0, %eax
jne 0x297e0
cmpq $0x0, -0x38(%rbp)
je 0x29763
leaq 0x23d3d(%rip), %rdi     # 0x4d499
movb $0x0, %al
callq 0x29da0
movl -0x8c(%rbp), %eax
addl $0x1, %eax
cmpl -0x8(%rbp), %eax
jge 0x29794
movq -0x10(%rbp), %rax
movl -0x8c(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x8c(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x98(%rbp)
jmp 0x297a9
leaq 0x23be2(%rip), %rdi     # 0x4d37d
leaq 0x23cef(%rip), %rsi     # 0x4d491
movb $0x0, %al
callq 0x29da0
movq -0x98(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
leaq 0x23d17(%rip), %rsi     # 0x4d4d6
callq 0xa0f0
cmpl $0x0, %eax
je 0x297db
movq -0x38(%rbp), %rsi
leaq 0x23d07(%rip), %rdi     # 0x4d4db
movb $0x0, %al
callq 0x29da0
jmp 0x299c7
movq -0x98(%rbp), %rdi
leaq 0x23d3d(%rip), %rsi     # 0x4d52b
callq 0xa0f0
cmpl $0x0, %eax
jne 0x29908
movl -0x8c(%rbp), %eax
addl $0x1, %eax
cmpl -0x8(%rbp), %eax
jge 0x2982d
movq -0x10(%rbp), %rax
movl -0x8c(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x8c(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x98(%rbp)
jmp 0x29842
leaq 0x23b49(%rip), %rdi     # 0x4d37d
leaq 0x23cf0(%rip), %rsi     # 0x4d52b
movb $0x0, %al
callq 0x29da0
movq -0x98(%rbp), %rax
movq %rax, -0xa0(%rbp)
movl -0x8c(%rbp), %eax
addl $0x1, %eax
cmpl -0x8(%rbp), %eax
jge 0x29881
movq -0x10(%rbp), %rax
movl -0x8c(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x8c(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x98(%rbp)
jmp 0x29896
leaq 0x23af5(%rip), %rdi     # 0x4d37d
leaq 0x23c9c(%rip), %rsi     # 0x4d52b
movb $0x0, %al
callq 0x29da0
movq -0x98(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0xa0(%rbp), %rdi
callq 0x10220
movq %rax, -0xb0(%rbp)
movq -0xa8(%rbp), %rdi
callq 0x10220
movq %rax, -0xb8(%rbp)
movq -0xb0(%rbp), %rsi
leaq -0x88(%rbp), %rdi
movl $0x1, %ecx
movl %ecx, %edx
callq 0xd7f0
movq -0xb0(%rbp), %rsi
movq -0xb8(%rbp), %rdx
leaq -0x88(%rbp), %rdi
movl $0x1, %ecx
callq 0xd460
jmp 0x299c5
movq -0x98(%rbp), %rdi
leaq 0x23c1a(%rip), %rsi     # 0x4d530
callq 0xa0f0
cmpl $0x0, %eax
jne 0x29988
cmpq $0x0, -0x40(%rbp)
je 0x29935
leaq 0x23c0a(%rip), %rdi     # 0x4d538
movb $0x0, %al
callq 0x29da0
movl -0x8c(%rbp), %eax
addl $0x1, %eax
cmpl -0x8(%rbp), %eax
jge 0x29966
movq -0x10(%rbp), %rax
movl -0x8c(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x8c(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x98(%rbp)
jmp 0x2997b
leaq 0x23a10(%rip), %rdi     # 0x4d37d
leaq 0x23bbc(%rip), %rsi     # 0x4d530
movb $0x0, %al
callq 0x29da0
movq -0x98(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0x299c3
movq -0x98(%rbp), %rdi
leaq 0x23bdf(%rip), %rsi     # 0x4d575
callq 0xa0f0
cmpl $0x0, %eax
jne 0x299ac
callq 0x29d70
xorl %edi, %edi
callq 0xa180
movq -0x98(%rbp), %rsi
leaq 0x23bc1(%rip), %rdi     # 0x4d57b
movb $0x0, %al
callq 0x29da0
jmp 0x299c3
jmp 0x299c5
jmp 0x299c7
jmp 0x299c9
jmp 0x299cb
jmp 0x299f8
cmpq $0x0, -0x20(%rbp)
je 0x299ed
movq -0x20(%rbp), %rsi
movq -0x98(%rbp), %rdx
leaq 0x23bb1(%rip), %rdi     # 0x4d597
movb $0x0, %al
callq 0x29da0
movq -0x98(%rbp), %rax
movq %rax, -0x20(%rbp)
jmp 0x299fa
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0x2958a
cmpq $0x0, -0x20(%rbp)
jne 0x29a23
leaq 0x23bbb(%rip), %rdi     # 0x4d5d7
movb $0x0, %al
callq 0x29da0
cmpq $0x0, -0x28(%rbp)
jne 0x29a35
leaq 0x1de56(%rip), %rax     # 0x47887
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x29a47
leaq 0x239a4(%rip), %rax     # 0x4d3e7
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x29a59
leaq 0x23a81(%rip), %rax     # 0x4d4d6
movq %rax, -0x38(%rbp)
movq $0x0, -0xc0(%rbp)
movl $0x0, -0xc4(%rbp)
movq -0x20(%rbp), %rdi
leaq -0xc4(%rbp), %rsi
callq 0x129c0
movq %rax, -0xc0(%rbp)
cmpq $0x0, -0xc0(%rbp)
jne 0x29a9d
leaq 0x23b63(%rip), %rdi     # 0x4d5f9
movb $0x0, %al
callq 0x29da0
movq 0x2e534(%rip), %rax     # 0x57fd8
movq (%rax), %rax
movq %rax, -0xd0(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x29aea
movq -0x40(%rbp), %rdi
leaq 0x23b52(%rip), %rsi     # 0x4d612
callq 0xa150
movq %rax, -0xd0(%rbp)
cmpq $0x0, -0xd0(%rbp)
jne 0x29ae8
movq -0x40(%rbp), %rsi
leaq 0x23b33(%rip), %rdi     # 0x4d614
movb $0x0, %al
callq 0x29da0
jmp 0x29aea
leaq -0xe0(%rbp), %rdi
callq 0xfca0
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0xc0(%rbp), %rdx
movl -0xc4(%rbp), %ecx
leaq -0x88(%rbp), %r8
xorl %eax, %eax
movl %eax, %r9d
leaq -0xe0(%rbp), %rax
movq %rax, (%rsp)
callq 0x45820
movq -0xc0(%rbp), %rdi
callq 0xa1b0
leaq -0x88(%rbp), %rdi
movl $0x1, %edx
movl %edx, %esi
callq 0xd360
movq -0x30(%rbp), %rdi
leaq 0x238a7(%rip), %rsi     # 0x4d3f8
callq 0xa0f0
cmpl $0x0, %eax
jne 0x29b9b
cmpq $0x0, -0xd8(%rbp)
jne 0x29b75
leaq 0x1dd1b(%rip), %rax     # 0x47887
movq %rax, -0xf0(%rbp)
jmp 0x29b83
movq -0xd8(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0xf0(%rbp), %rdi
movq -0xd0(%rbp), %rsi
callq 0xa0c0
jmp 0x29d0e
movq -0x30(%rbp), %rdi
leaq 0x2385d(%rip), %rsi     # 0x4d403
callq 0xa0f0
cmpl $0x0, %eax
jne 0x29c37
movq -0x20(%rbp), %rax
movq %rax, -0xf8(%rbp)
cmpq $0x0, -0xd8(%rbp)
jne 0x29bd9
leaq 0x1dcb7(%rip), %rax     # 0x47887
movq %rax, -0x100(%rbp)
jmp 0x29be7
movq -0xd8(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0xf8(%rbp), %rdi
movq -0x100(%rbp), %rsi
movl -0xdc(%rbp), %edx
callq 0x25bd0
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rdi
callq 0xbe70
movq -0xe8(%rbp), %rdi
movq -0xd0(%rbp), %rsi
callq 0xc6b0
movq -0xe8(%rbp), %rdi
callq 0xb590
jmp 0x29d0c
movq -0x20(%rbp), %rax
movq %rax, -0x108(%rbp)
cmpq $0x0, -0xd8(%rbp)
jne 0x29c5c
leaq 0x1dc34(%rip), %rax     # 0x47887
movq %rax, -0x110(%rbp)
jmp 0x29c6a
movq -0xd8(%rbp), %rax
movq %rax, -0x110(%rbp)
movq -0x108(%rbp), %rdi
movq -0x110(%rbp), %rsi
movl -0xdc(%rbp), %edx
callq 0x29e70
movq %rax, -0x18(%rbp)
movq -0xd0(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0xb8(%rax)
movq -0x18(%rbp), %rdi
callq 0x29fe0
movq -0x30(%rbp), %rdi
leaq 0x2373a(%rip), %rsi     # 0x4d3e7
callq 0xa0f0
cmpl $0x0, %eax
jne 0x29cc2
movq -0x18(%rbp), %rdi
callq 0x162a0
jmp 0x29d0a
movq -0x30(%rbp), %rdi
leaq 0x23722(%rip), %rsi     # 0x4d3ef
callq 0xa0f0
cmpl $0x0, %eax
jne 0x29ce9
movq -0x18(%rbp), %rdi
movq -0xd0(%rbp), %rsi
callq 0x2a190
jmp 0x29d08
leaq 0x1d775(%rip), %rdi     # 0x47465
leaq 0x2394b(%rip), %rsi     # 0x4d642
movl $0xf2, %edx
leaq 0x23959(%rip), %rcx     # 0x4d65c
callq 0xa0b0
jmp 0x29d0a
jmp 0x29d0c
jmp 0x29d0e
leaq -0xe0(%rbp), %rdi
callq 0x10320
cmpq $0x0, -0x40(%rbp)
je 0x29d2d
movq -0xd0(%rbp), %rdi
callq 0xa070
cmpq $0x0, -0x18(%rbp)
je 0x29d3d
movq -0x18(%rbp), %rdi
callq 0x29fa0
xorl %eax, %eax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nopl (%rax,%rax)