// File: kiven-li[P]xbytes/xbytes.cpp
// Function: xbytes::xbytes::output_reduce_code(std::basic_ofstream<char, std::char_traits<char>>&)
void xbytes::output_reduce_code(std::ofstream& ss)
{
	for(int i = 0; i < rules.size(); ++i)
	{
		rule* r = rules[i];
		if(r->code.size() == 0) continue;
		ss << "	case " << i << ":/* "  << r->str_rule << " */" << std::endl;

		string& code = r->code;

		//生成左边符号代码
		string& la = r->left_alias;
		if(la.size() > 0)
		{
			std::size_t spos = 0;
			while(spos < code.size())
			{
				std::size_t pos = code.find(la, spos);
				
				if(pos == std::string::npos) break;
				if(spos == pos) break;
				if(pos > 0 && isalpha(code[pos-1]))
				{
					spos = pos + la.size() - 1;
					continue;
				}
				if(pos < code.size() - 1 && isalpha(code[pos + la.size()]))
				{
					spos = pos + la.size() - 1;
					continue;
				}

				code.replace(pos, la.size(), "tt");
			}
		}

		//生成右边符号代码
		for(int m = 0; m < r->right_alias.size(); ++m)
		{
			string& ra = r->right_alias[m];
			if(ra.size() == 0) continue;

			std::stringstream ss;
			ss << "tv[" << m << "]";

			std::size_t spos = 0;
			while(spos < code.size())
			{
				std::size_t pos = code.find(ra, spos);

				if(pos == std::string::npos) break;
				if(spos == pos) break;
				if(pos > 0 && isalpha(code[pos-1]))
				{
					spos = pos + ra.size() - 1;
					continue;
				}
				if(pos < code.size() - 1 && isalpha(code[pos + ra.size()]))
				{
					spos = pos + ra.size() - 1;
					continue;
				}

				code.replace(pos, ra.size(), ss.str());
			}
		}

		ss << r->code << std::endl;
		ss << "	break;\n" << std::endl;
	}	
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2e0, %rsp            # imm = 0x2E0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x220(%rbp)
movl $0x0, -0x14(%rbp)
movq -0x220(%rbp), %rdi
movslq -0x14(%rbp), %rax
movq %rax, -0x228(%rbp)
addq $0x20, %rdi
callq 0xc510
movq %rax, %rcx
movq -0x228(%rbp), %rax
cmpq %rcx, %rax
jae 0xc16f
movq -0x220(%rbp), %rdi
addq $0x20, %rdi
movslq -0x14(%rbp), %rsi
callq 0xc530
movq (%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
addq $0x60, %rdi
callq 0x3200
cmpq $0x0, %rax
jne 0xbbf6
jmp 0xc161
movq -0x10(%rbp), %rdi
leaq 0xdc52(%rip), %rsi      # 0x19853
callq 0x32e0
movq %rax, %rdi
movl -0x14(%rbp), %esi
callq 0x34d0
movq %rax, %rdi
leaq 0xdc3f(%rip), %rsi      # 0x1985a
callq 0x32e0
movq %rax, %rdi
movq -0x20(%rbp), %rsi
addq $0x80, %rsi
callq 0x3290
movq %rax, %rdi
leaq 0xdc22(%rip), %rsi      # 0x1985f
callq 0x32e0
movq %rax, %rdi
movq 0x1837c(%rip), %rsi     # 0x23fc8
callq 0x3320
movq -0x20(%rbp), %rax
addq $0x60, %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0x3200
cmpq $0x0, %rax
jbe 0xbe08
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x230(%rbp)
movq -0x28(%rbp), %rdi
callq 0x3200
movq %rax, %rcx
movq -0x230(%rbp), %rax
cmpq %rcx, %rax
jae 0xbe06
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rdx
callq 0x3560
movq %rax, -0x40(%rbp)
cmpq $-0x1, -0x40(%rbp)
jne 0xbccc
jmp 0xbe06
movq -0x38(%rbp), %rax
cmpq -0x40(%rbp), %rax
jne 0xbcdb
jmp 0xbe06
cmpq $0x0, -0x40(%rbp)
jbe 0xbd2e
movq -0x28(%rbp), %rdi
movq -0x40(%rbp), %rsi
subq $0x1, %rsi
callq 0x3550
movsbl (%rax), %edi
callq 0x3470
cmpl $0x0, %eax
je 0xbd2e
movq -0x40(%rbp), %rax
movq %rax, -0x238(%rbp)
movq -0x30(%rbp), %rdi
callq 0x3200
movq %rax, %rcx
movq -0x238(%rbp), %rax
addq %rcx, %rax
subq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xbc84
movq -0x40(%rbp), %rax
movq %rax, -0x240(%rbp)
movq -0x28(%rbp), %rdi
callq 0x3200
movq %rax, %rcx
movq -0x240(%rbp), %rax
subq $0x1, %rcx
cmpq %rcx, %rax
jae 0xbdc5
movq -0x28(%rbp), %rax
movq %rax, -0x248(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x250(%rbp)
movq -0x30(%rbp), %rdi
callq 0x3200
movq -0x250(%rbp), %rsi
movq -0x248(%rbp), %rdi
addq %rax, %rsi
callq 0x3550
movsbl (%rax), %edi
callq 0x3470
cmpl $0x0, %eax
je 0xbdc5
movq -0x40(%rbp), %rax
movq %rax, -0x258(%rbp)
movq -0x30(%rbp), %rdi
callq 0x3200
movq %rax, %rcx
movq -0x258(%rbp), %rax
addq %rcx, %rax
subq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xbc84
movq -0x28(%rbp), %rax
movq %rax, -0x268(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x260(%rbp)
movq -0x30(%rbp), %rdi
callq 0x3200
movq -0x268(%rbp), %rdi
movq -0x260(%rbp), %rsi
movq %rax, %rdx
leaq 0xda67(%rip), %rcx      # 0x19863
callq 0x31a0
jmp 0xbc84
jmp 0xbe08
movl $0x0, -0x44(%rbp)
movslq -0x44(%rbp), %rax
movq %rax, -0x270(%rbp)
movq -0x20(%rbp), %rdi
addq $0x48, %rdi
callq 0xd540
movq %rax, %rcx
movq -0x270(%rbp), %rax
cmpq %rcx, %rax
jae 0xc122
movq -0x20(%rbp), %rdi
addq $0x48, %rdi
movslq -0x44(%rbp), %rsi
callq 0xd560
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
callq 0x3200
cmpq $0x0, %rax
jne 0xbe63
jmp 0xc106
leaq -0x1d8(%rbp), %rdi
callq 0x3210
leaq -0x1c8(%rbp), %rdi
leaq 0xd9e9(%rip), %rsi      # 0x19866
callq 0x32e0
movq %rax, -0x278(%rbp)
jmp 0xbe8b
movq -0x278(%rbp), %rdi
movl -0x44(%rbp), %esi
callq 0x34d0
movq %rax, -0x280(%rbp)
jmp 0xbea3
movq -0x280(%rbp), %rdi
leaq 0xd717(%rip), %rsi      # 0x195c8
callq 0x32e0
jmp 0xbeb8
movq $0x0, -0x1f0(%rbp)
movq -0x1f0(%rbp), %rax
movq %rax, -0x288(%rbp)
movq -0x28(%rbp), %rdi
callq 0x3200
movq %rax, %rcx
movq -0x288(%rbp), %rax
cmpq %rcx, %rax
jae 0xc0fa
movq -0x28(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x1f0(%rbp), %rdx
callq 0x3560
movq %rax, -0x1f8(%rbp)
cmpq $-0x1, -0x1f8(%rbp)
jne 0xbf2e
jmp 0xc0fa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1e0(%rbp)
movl %eax, -0x1e4(%rbp)
jmp 0xc114
movq -0x1f0(%rbp), %rax
cmpq -0x1f8(%rbp), %rax
jne 0xbf43
jmp 0xc0fa
cmpq $0x0, -0x1f8(%rbp)
jbe 0xbfb1
movq -0x28(%rbp), %rdi
movq -0x1f8(%rbp), %rsi
decq %rsi
callq 0x3550
movq %rax, -0x290(%rbp)
jmp 0xbf69
movq -0x290(%rbp), %rax
movsbl (%rax), %edi
callq 0x3470
cmpl $0x0, %eax
je 0xbfb1
movq -0x1f8(%rbp), %rax
movq %rax, -0x298(%rbp)
movq -0x50(%rbp), %rdi
callq 0x3200
movq %rax, %rcx
movq -0x298(%rbp), %rax
addq %rcx, %rax
subq $0x1, %rax
movq %rax, -0x1f0(%rbp)
jmp 0xbec3
movq -0x1f8(%rbp), %rax
movq %rax, -0x2a0(%rbp)
movq -0x28(%rbp), %rdi
callq 0x3200
movq %rax, %rcx
movq -0x2a0(%rbp), %rax
subq $0x1, %rcx
cmpq %rcx, %rax
jae 0xc068
movq -0x28(%rbp), %rax
movq %rax, -0x2b0(%rbp)
movq -0x1f8(%rbp), %rax
movq %rax, -0x2b8(%rbp)
movq -0x50(%rbp), %rdi
callq 0x3200
movq -0x2b8(%rbp), %rsi
movq -0x2b0(%rbp), %rdi
addq %rax, %rsi
callq 0x3550
movq %rax, -0x2a8(%rbp)
jmp 0xc020
movq -0x2a8(%rbp), %rax
movsbl (%rax), %edi
callq 0x3470
cmpl $0x0, %eax
je 0xc068
movq -0x1f8(%rbp), %rax
movq %rax, -0x2c0(%rbp)
movq -0x50(%rbp), %rdi
callq 0x3200
movq %rax, %rcx
movq -0x2c0(%rbp), %rax
addq %rcx, %rax
subq $0x1, %rax
movq %rax, -0x1f0(%rbp)
jmp 0xbec3
movq -0x28(%rbp), %rax
movq %rax, -0x2d8(%rbp)
movq -0x1f8(%rbp), %rax
movq %rax, -0x2d0(%rbp)
movq -0x50(%rbp), %rdi
callq 0x3200
movq %rax, -0x2c8(%rbp)
leaq -0x218(%rbp), %rdi
leaq -0x1d8(%rbp), %rsi
callq 0x32d0
jmp 0xc0a6
movq -0x2c8(%rbp), %rdx
movq -0x2d0(%rbp), %rsi
movq -0x2d8(%rbp), %rdi
leaq -0x218(%rbp), %rcx
callq 0x30f0
jmp 0xc0c9
leaq -0x218(%rbp), %rdi
callq 0x31b0
jmp 0xbec3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1e0(%rbp)
movl %eax, -0x1e4(%rbp)
leaq -0x218(%rbp), %rdi
callq 0x31b0
jmp 0xc114
leaq -0x1d8(%rbp), %rdi
callq 0x3230
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0xbe0f
leaq -0x1d8(%rbp), %rdi
callq 0x3230
jmp 0xc178
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
addq $0x60, %rsi
callq 0x3290
movq %rax, %rdi
movq 0x17e8b(%rip), %rsi     # 0x23fc8
callq 0x3320
movq -0x10(%rbp), %rdi
leaq 0xd71d(%rip), %rsi      # 0x1986a
callq 0x32e0
movq %rax, %rdi
movq 0x17e6c(%rip), %rsi     # 0x23fc8
callq 0x3320
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0xbb95
addq $0x2e0, %rsp            # imm = 0x2E0
popq %rbp
retq
movq -0x1e0(%rbp), %rdi
callq 0x34e0
nopw %cs:(%rax,%rax)
nop