// File: /workspace/github_cpp_makefile_star2_O3/kiven-li[P]xbytes/xbytes.cpp
// Function: xbytes::xbytes::make_action_table()
void xbytes::make_action_table()
{
	for(int i = 0; i < states.size(); ++i)
	{
		lr_state* s = states[i];
		s->actions.resize(symbols.size());

		//goto & shift action
		for(goto_map::iterator it = s->gotomap.begin();it != s->gotomap.end(); ++it)
		{
			symbol* ss = it->first;

			action act;
			act.next_state = it->second;
			act.r = NULL;
			act.lookahead = ss;

			if(ss->type == ST_TERMINAL)
			{
				act.type = LR_ACTION_SHIFT;				
			}
			else if(ss->type == ST_NONTERMINAL)
			{
				act.type = LR_ACTION_GOTO;
			}

			s->actions[ss->index].push_back(act);
		}

		//reduce action
		for(int m = 0; m < s->terms.size(); ++m)
		{
			term* t = s->terms[m];
			if(t->dot != t->r->rights.size()) continue;

			for(int n = 0; n < s->actions.size(); ++n)
			{
				if(symbols[n]->type != ST_TERMINAL) continue;
				if(!is_symbol_in_set(symbols[n], t->forwards)) continue;//FOR LALR
				//if(!is_symbol_in_set(symbols[m], t->r->left->follow_set)) continue; //for SLR

				action act;
				act.type = LR_ACTION_REDUCE;
				act.next_state = 0;
				act.r = t->r;
				act.lookahead = symbols[n];

				s->actions[n].push_back(act);
			}
		}

		//accept action
		for(int m = 0; m < s->terms.size(); ++m)
		{
			term* t = s->terms[m];
			if(t->dot != t->r->rights.size() - 1) continue;

			if(t->r->rights[t->dot]->name == ACCEPT_TERMINAL)
			{
				action act;
				act.r = t->r;
				act.type = LR_ACTION_ACCEPT;
				act.next_state = 0;
				act.lookahead = t->r->rights[t->dot];

				s->actions[t->r->rights[t->dot]->index].push_back(act);
			}
		}
	}

	//error action
	for(int i = 0; i < states.size(); ++i)
	{
		lr_state* s = states[i];
		for(int m = 0; m < s->actions.size(); ++m)
		{
			action_array& aa = s->actions[m];
			if(aa.size() == 0)
			{
				action act;
				act.r = NULL;
				act.type = LR_ACTION_ERROR;
				act.next_state = 0;
				act.lookahead = NULL;

				aa.push_back(act);
			}
		}
	}

	//resolve conflict
	int conflict = 0;
	for(int i = 0; i < states.size(); ++i)
	{
		lr_state* s = states[i];
		for(int m = 0; m < s->actions.size(); ++m)
		{
			action_array& aa = s->actions[m];
			if(aa.size() < 2) continue;

			for(int n = 1; n < aa.size(); ++n)
			{
				conflict += resolve_conflict(aa[n-1], aa[n]);
			}
		}
	}

	std::cout << "conflict: " << conflict << std::endl;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %r15
movq 0x38(%rdi), %rax
movq 0x40(%rdi), %rcx
movabsq $-0x5555555555555555, %r10 # imm = 0xAAAAAAAAAAAAAAAB
cmpq %rax, %rcx
movq %rdi, -0x58(%rbp)
je 0x5ab1
xorl %esi, %esi
movq %rsi, -0x48(%rbp)
movq (%rax,%rsi,8), %r14
leaq 0x50(%r14), %r12
movq 0x10(%r15), %rsi
subq 0x8(%r15), %rsi
sarq $0x3, %rsi
movq %r12, %rdi
callq 0xa280
movq 0x38(%r14), %r13
leaq 0x28(%r14), %rbx
cmpq %rbx, %r13
je 0x5860
movq 0x20(%r13), %rax
movl 0x28(%r13), %ecx
movl %ecx, -0x3c(%rbp)
movq $0x0, -0x38(%rbp)
movq %rax, -0x30(%rbp)
movl 0x48(%rax), %ecx
cmpl $0x2, %ecx
je 0x5811
cmpl $0x1, %ecx
jne 0x5818
movl $0x2, -0x40(%rbp)
jmp 0x5818
movl $0x1, -0x40(%rbp)
movslq (%rax), %rax
movq (%r12), %rcx
leaq (%rax,%rax,2), %rax
leaq (%rcx,%rax,8), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x5847
movq -0x30(%rbp), %rax
movq %rax, 0x10(%rsi)
movups -0x40(%rbp), %xmm0
movups %xmm0, (%rsi)
addq $0x18, 0x8(%rdi)
jmp 0x5850
leaq -0x40(%rbp), %rdx
callq 0xb828
movq %r13, %rdi
callq 0x3180
movq %rax, %r13
cmpq %rbx, %rax
jne 0x57e4
movq 0x8(%r14), %rcx
movq 0x10(%r14), %rsi
cmpq %rcx, %rsi
movq %r12, -0x50(%rbp)
je 0x59ac
xorl %r13d, %r13d
movabsq $-0x5555555555555555, %r10 # imm = 0xAAAAAAAAAAAAAAAB
movq (%rcx,%r13,8), %r12
movslq 0x8(%r12), %rax
movq (%r12), %rdx
movq 0x38(%rdx), %rdi
subq 0x30(%rdx), %rdi
sarq $0x3, %rdi
cmpq %rax, %rdi
jne 0x5994
movq 0x50(%r14), %rax
movq 0x58(%r14), %rdx
cmpq %rax, %rdx
je 0x5994
xorl %ebx, %ebx
movq 0x8(%r15), %rcx
movq (%rcx,%rbx,8), %rsi
cmpl $0x1, 0x48(%rsi)
jne 0x5972
movq 0x10(%r12), %r8
movq 0x18(%r12), %rdi
subq %r8, %rdi
je 0x5972
sarq $0x3, %rdi
cmpq $0x1, %rdi
movq %rdi, %r9
adcq $0x0, %r9
cmpq %rsi, (%r8)
je 0x5917
movl $0x1, %r11d
movq %r11, %r10
cmpq %r11, %r9
je 0x5908
leaq 0x1(%r10), %r11
cmpq %rsi, (%r8,%r10,8)
jne 0x58f6
cmpq %r10, %rdi
movabsq $-0x5555555555555555, %r10 # imm = 0xAAAAAAAAAAAAAAAB
jbe 0x5972
movq $0x3, -0x40(%rbp)
movq (%r12), %rdx
movq %rdx, -0x38(%rbp)
movq (%rcx,%rbx,8), %rcx
movq %rcx, -0x30(%rbp)
leaq (%rbx,%rbx,2), %rcx
leaq (%rax,%rcx,8), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x5957
movq -0x30(%rbp), %rax
movq %rax, 0x10(%rsi)
movups -0x40(%rbp), %xmm0
movups %xmm0, (%rsi)
addq $0x18, 0x8(%rdi)
jmp 0x596a
leaq -0x40(%rbp), %rdx
callq 0xb828
movabsq $-0x5555555555555555, %r10 # imm = 0xAAAAAAAAAAAAAAAB
movq 0x50(%r14), %rax
movq 0x58(%r14), %rdx
incq %rbx
movq %rdx, %rcx
subq %rax, %rcx
sarq $0x3, %rcx
imulq %r10, %rcx
cmpq %rbx, %rcx
ja 0x58b7
movq 0x8(%r14), %rcx
movq 0x10(%r14), %rsi
incq %r13
movq %rsi, %rax
subq %rcx, %rax
sarq $0x3, %rax
cmpq %r13, %rax
ja 0x5882
jmp 0x59b6
movabsq $-0x5555555555555555, %r10 # imm = 0xAAAAAAAAAAAAAAAB
cmpq %rcx, %rsi
leaq 0x77d9(%rip), %r12      # 0xd199
je 0x5a8b
xorl %ebx, %ebx
movq (%rcx,%rbx,8), %r15
movslq 0x8(%r15), %rax
movq (%r15), %rdx
movq 0x30(%rdx), %rcx
movq 0x38(%rdx), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
decq %rdx
cmpq %rax, %rdx
jne 0x5a70
movq (%rcx,%rax,8), %rdi
addq $0x8, %rdi
movq %r12, %rsi
callq 0x30a0
movabsq $-0x5555555555555555, %r10 # imm = 0xAAAAAAAAAAAAAAAB
testl %eax, %eax
jne 0x5a70
movq (%r15), %rax
movq %rax, -0x38(%rbp)
movq $0x4, -0x40(%rbp)
movslq 0x8(%r15), %rcx
movq 0x30(%rax), %rax
movq (%rax,%rcx,8), %rax
movq %rax, -0x30(%rbp)
movslq (%rax), %rax
movq -0x50(%rbp), %rcx
movq (%rcx), %rcx
leaq (%rax,%rax,2), %rax
leaq (%rcx,%rax,8), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x5a5d
movq -0x30(%rbp), %rax
movq %rax, 0x10(%rsi)
movups -0x40(%rbp), %xmm0
movups %xmm0, (%rsi)
addq $0x18, 0x8(%rdi)
jmp 0x5a70
leaq -0x40(%rbp), %rdx
callq 0xb828
movabsq $-0x5555555555555555, %r10 # imm = 0xAAAAAAAAAAAAAAAB
incq %rbx
movq 0x8(%r14), %rcx
movq 0x10(%r14), %rax
subq %rcx, %rax
sarq $0x3, %rax
cmpq %rbx, %rax
ja 0x59c8
movq -0x48(%rbp), %rsi
incq %rsi
movq -0x58(%rbp), %r15
movq 0x38(%r15), %rax
movq 0x40(%r15), %rcx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rsi, %rdx
ja 0x57b7
cmpq %rax, %rcx
je 0x5c4c
xorl %r12d, %r12d
xorps %xmm1, %xmm1
movq (%rax,%r12,8), %rbx
movq 0x50(%rbx), %rdx
movq 0x58(%rbx), %rdi
cmpq %rdx, %rdi
je 0x5b4f
xorl %r13d, %r13d
xorl %r14d, %r14d
movq 0x8(%rdx,%r13), %rsi
cmpq (%rdx,%r13), %rsi
jne 0x5b2d
leaq (%rdx,%r13), %rdi
movups %xmm1, -0x38(%rbp)
movq $0x5, -0x40(%rbp)
cmpq 0x10(%rdx,%r13), %rsi
je 0x5b0f
movq -0x30(%rbp), %rax
movq %rax, 0x10(%rsi)
movups -0x40(%rbp), %xmm0
movups %xmm0, (%rsi)
addq $0x18, 0x8(%rdi)
jmp 0x5b25
leaq -0x40(%rbp), %rdx
callq 0xb828
xorps %xmm1, %xmm1
movabsq $-0x5555555555555555, %r10 # imm = 0xAAAAAAAAAAAAAAAB
movq 0x50(%rbx), %rdx
movq 0x58(%rbx), %rdi
incq %r14
movq %rdi, %rax
subq %rdx, %rax
sarq $0x3, %rax
imulq %r10, %rax
addq $0x18, %r13
cmpq %r14, %rax
ja 0x5ad7
movq 0x38(%r15), %rax
movq 0x40(%r15), %rcx
incq %r12
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r12, %rdx
ja 0x5ac0
cmpq %rax, %rcx
je 0x5c4c
xorl %r8d, %r8d
xorl %r14d, %r14d
movq (%rax,%r8,8), %rdx
movq 0x50(%rdx), %rsi
movq %rdx, -0x50(%rbp)
movq 0x58(%rdx), %rdi
cmpq %rsi, %rdi
je 0x5c34
movq %r8, -0x48(%rbp)
xorl %ebx, %ebx
leaq (%rbx,%rbx,2), %rax
movq (%rsi,%rax,8), %rdx
movq 0x8(%rsi,%rax,8), %rcx
subq %rdx, %rcx
sarq $0x3, %rcx
imulq %r10, %rcx
cmpq $0x2, %rcx
jb 0x5c0e
leaq (%rsi,%rax,8), %r12
movl $0x1, %r13d
movl $0x18, %r15d
leaq (%rdx,%r15), %rsi
addq $-0x18, %rsi
addq %r15, %rdx
callq 0x764c
movabsq $-0x5555555555555555, %r10 # imm = 0xAAAAAAAAAAAAAAAB
addl %eax, %r14d
incq %r13
movq (%r12), %rdx
movq 0x8(%r12), %rax
subq %rdx, %rax
sarq $0x3, %rax
imulq %r10, %rax
addq $0x18, %r15
cmpq %r13, %rax
ja 0x5bc1
movq -0x50(%rbp), %rax
movq 0x50(%rax), %rsi
movq 0x58(%rax), %rdi
movq -0x58(%rbp), %r15
incq %rbx
movq %rdi, %rax
subq %rsi, %rax
sarq $0x3, %rax
imulq %r10, %rax
cmpq %rbx, %rax
ja 0x5b93
movq 0x38(%r15), %rax
movq 0x40(%r15), %rcx
movq -0x48(%rbp), %r8
incq %r8
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r8, %rdx
ja 0x5b74
jmp 0x5c4f
xorl %r14d, %r14d
movq 0xb382(%rip), %rbx      # 0x10fd8
leaq 0x758d(%rip), %rsi      # 0xd1ea
movl $0xa, %edx
movq %rbx, %rdi
callq 0x32b0
movq %rbx, %rdi
movl %r14d, %esi
callq 0x33b0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3230
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3170
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq