// File: /workspace/github_cpp_makefile_star2_O2/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 $0x48, %rsp
movq %rdi, %r8
xorl %esi, %esi
pushq $0x18
popq %r9
movq %rdi, -0x30(%rbp)
movq 0x38(%r8), %rax
movq 0x40(%r8), %rcx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rsi, %rdx
jbe 0x3fb7
movq %rsi, -0x68(%rbp)
movq (%rax,%rsi,8), %r14
leaq 0x50(%r14), %rdi
movq 0x10(%r8), %rsi
subq 0x8(%r8), %rsi
sarq $0x3, %rsi
movq %rdi, -0x60(%rbp)
callq 0x7294
movq 0x38(%r14), %r13
leaq 0x28(%r14), %rbx
cmpq %rbx, %r13
je 0x3e2a
movq 0x20(%r13), %rax
movl 0x28(%r13), %ecx
movl %ecx, -0x44(%rbp)
andq $0x0, -0x40(%rbp)
movq %rax, -0x38(%rbp)
movl 0x48(%rax), %ecx
cmpl $0x2, %ecx
je 0x3dff
cmpl $0x1, %ecx
jne 0x3e06
movl $0x2, -0x48(%rbp)
jmp 0x3e06
movl $0x1, -0x48(%rbp)
movslq (%rax), %rax
imulq $0x18, %rax, %rdi
movq -0x60(%rbp), %rax
addq (%rax), %rdi
leaq -0x48(%rbp), %rsi
callq 0x72d6
movq %r13, %rdi
callq 0x2180
movq %rax, %r13
jmp 0x3dd0
xorl %esi, %esi
movq -0x30(%rbp), %r8
pushq $0x18
popq %r9
movq 0x8(%r14), %rax
movq 0x10(%r14), %rcx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rsi, %rdx
jbe 0x3f09
movq %rsi, -0x70(%rbp)
movq (%rax,%rsi,8), %rcx
movslq 0x8(%rcx), %rax
movq %rcx, -0x50(%rbp)
movq (%rcx), %rcx
movq 0x38(%rcx), %rdx
subq 0x30(%rcx), %rdx
sarq $0x3, %rdx
cmpq %rax, %rdx
jne 0x3efd
movq -0x50(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x58(%rbp)
xorl %r12d, %r12d
xorl %ebx, %ebx
movq 0x50(%r14), %r15
movq 0x58(%r14), %rax
subq %r15, %rax
cqto
idivq %r9
cmpq %rbx, %rax
jbe 0x3efd
movq 0x8(%r8), %r13
movq (%r13,%rbx,8), %rsi
cmpl $0x1, 0x48(%rsi)
jne 0x3ef4
movq -0x58(%rbp), %rdx
callq 0x4b38
pushq $0x18
popq %r9
movq -0x30(%rbp), %r8
testb %al, %al
je 0x3ef4
movq $0x3, -0x48(%rbp)
movq -0x50(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
movq (%r13,%rbx,8), %rax
movq %rax, -0x38(%rbp)
addq %r12, %r15
movq %r15, %rdi
leaq -0x48(%rbp), %rsi
callq 0x72d6
pushq $0x18
popq %r9
movq -0x30(%rbp), %r8
incq %rbx
addq $0x18, %r12
jmp 0x3e88
movq -0x70(%rbp), %rsi
incq %rsi
jmp 0x3e34
xorl %ebx, %ebx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rbx, %rcx
jbe 0x3fab
movq (%rax,%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 0x3f9b
movq (%rcx,%rax,8), %rdi
addq $0x8, %rdi
leaq 0x624d(%rip), %rsi      # 0xa199
callq 0x7074
pushq $0x18
popq %r9
movq -0x30(%rbp), %r8
testb %al, %al
je 0x3f9b
movq (%r15), %rax
movq %rax, -0x40(%rbp)
movq $0x4, -0x48(%rbp)
movslq 0x8(%r15), %rcx
movq 0x30(%rax), %rax
movq (%rax,%rcx,8), %rax
movq %rax, -0x38(%rbp)
movslq (%rax), %rax
imulq $0x18, %rax, %rdi
movq -0x60(%rbp), %rax
addq (%rax), %rdi
leaq -0x48(%rbp), %rsi
callq 0x72d6
pushq $0x18
popq %r9
movq -0x30(%rbp), %r8
incq %rbx
movq 0x8(%r14), %rax
movq 0x10(%r14), %rcx
jmp 0x3f0b
movq -0x68(%rbp), %rsi
incq %rsi
jmp 0x3d8c
xorl %ebx, %ebx
leaq -0x48(%rbp), %r14
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rbx, %rdx
jbe 0x402e
movq (%rax,%rbx,8), %r15
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x50(%r15), %rdi
movq 0x58(%r15), %rax
subq %rdi, %rax
cqto
idivq %r9
cmpq %r13, %rax
jbe 0x4021
movq 0x8(%rdi,%r12), %rax
cmpq (%rdi,%r12), %rax
jne 0x4018
addq %r12, %rdi
xorps %xmm0, %xmm0
movups %xmm0, -0x40(%rbp)
movq $0x5, -0x48(%rbp)
movq %r14, %rsi
callq 0x72d6
pushq $0x18
popq %r9
movq -0x30(%rbp), %r8
incq %r13
addq $0x18, %r12
jmp 0x3fd6
incq %rbx
movq 0x38(%r8), %rax
movq 0x40(%r8), %rcx
jmp 0x3fbd
xorl %edx, %edx
xorl %r14d, %r14d
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rdx, %rcx
jbe 0x40ed
movq %rdx, -0x50(%rbp)
movq (%rax,%rdx,8), %r10
xorl %r13d, %r13d
movq %r10, -0x58(%rbp)
movq 0x50(%r10), %rbx
movq 0x58(%r10), %rax
subq %rbx, %rax
cqto
idivq %r9
cmpq %r13, %rax
jbe 0x40d9
imulq $0x18, %r13, %rdi
movq (%rbx,%rdi), %rcx
movq 0x8(%rbx,%rdi), %rsi
movq %rsi, %rax
subq %rcx, %rax
cqto
idivq %r9
cmpq $0x2, %rax
jb 0x40d1
addq %rdi, %rbx
movq %r9, %r12
pushq $0x1
popq %r15
subq %rcx, %rsi
movq %rsi, %rax
cqto
idivq %r9
cmpq %r15, %rax
jbe 0x40c9
leaq (%rcx,%r12), %rsi
addq $-0x18, %rsi
addq %r12, %rcx
movq %rcx, %rdx
callq 0x52de
pushq $0x18
popq %r9
addl %eax, %r14d
incq %r15
movq (%rbx), %rcx
movq 0x8(%rbx), %rsi
addq $0x18, %r12
jmp 0x408f
movq -0x30(%rbp), %r8
movq -0x58(%rbp), %r10
incq %r13
jmp 0x4052
movq -0x50(%rbp), %rdx
incq %rdx
movq 0x38(%r8), %rax
movq 0x40(%r8), %rcx
jmp 0x4033
movq 0xaee4(%rip), %rdi      # 0xefd8
leaq 0x60ef(%rip), %rsi      # 0xa1ea
callq 0x2220
movq %rax, %rdi
movl %r14d, %esi
callq 0x2370
movq %rax, %rdi
callq 0x2110
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq