// File: 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
subq $0x180, %rsp            # imm = 0x180
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x108(%rbp)
movl $0x0, -0xc(%rbp)
movq -0x108(%rbp), %rdi
movslq -0xc(%rbp), %rax
movq %rax, -0x110(%rbp)
addq $0x38, %rdi
callq 0xc550
movq %rax, %rcx
movq -0x110(%rbp), %rax
cmpq %rcx, %rax
jae 0x6dd3
movq -0x108(%rbp), %rdi
addq $0x38, %rdi
movslq -0xc(%rbp), %rsi
callq 0xc570
movq -0x108(%rbp), %rdi
movq (%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
addq $0x50, %rax
movq %rax, -0x118(%rbp)
addq $0x8, %rdi
callq 0xc4d0
movq -0x118(%rbp), %rdi
movq %rax, %rsi
callq 0xccf0
movq -0x18(%rbp), %rdi
addq $0x20, %rdi
callq 0xcd80
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rdi
addq $0x20, %rdi
callq 0xcde0
movq %rax, -0x28(%rbp)
leaq -0x20(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0xcdb0
testb $0x1, %al
jne 0x6a60
jmp 0x6ae3
leaq -0x20(%rbp), %rdi
callq 0xce10
movq (%rax), %rax
movq %rax, -0x30(%rbp)
leaq -0x20(%rbp), %rdi
callq 0xce10
movl 0x8(%rax), %eax
movl %eax, -0x44(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
cmpl $0x1, 0x48(%rax)
jne 0x6aa2
movl $0x2, -0x48(%rbp)
jmp 0x6ab5
movq -0x30(%rbp), %rax
cmpl $0x2, 0x48(%rax)
jne 0x6ab3
movl $0x1, -0x48(%rbp)
jmp 0x6ab5
movq -0x18(%rbp), %rdi
addq $0x50, %rdi
movq -0x30(%rbp), %rax
movslq (%rax), %rsi
callq 0xce30
movq %rax, %rdi
leaq -0x48(%rbp), %rsi
callq 0xce50
leaq -0x20(%rbp), %rdi
callq 0xcec0
jmp 0x6a39
movl $0x0, -0x4c(%rbp)
movslq -0x4c(%rbp), %rax
movq %rax, -0x120(%rbp)
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
callq 0xc590
movq %rax, %rcx
movq -0x120(%rbp), %rax
cmpq %rcx, %rax
jae 0x6c54
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
movslq -0x4c(%rbp), %rsi
callq 0xc5b0
movq (%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movslq 0x8(%rax), %rax
movq %rax, -0x128(%rbp)
movq -0x58(%rbp), %rax
movq (%rax), %rdi
addq $0x30, %rdi
callq 0xc4d0
movq %rax, %rcx
movq -0x128(%rbp), %rax
cmpq %rcx, %rax
je 0x6b60
jmp 0x6c46
movl $0x0, -0x5c(%rbp)
movslq -0x5c(%rbp), %rax
movq %rax, -0x130(%rbp)
movq -0x18(%rbp), %rdi
addq $0x50, %rdi
callq 0xcef0
movq %rax, %rcx
movq -0x130(%rbp), %rax
cmpq %rcx, %rax
jae 0x6c44
movq -0x108(%rbp), %rdi
addq $0x8, %rdi
movslq -0x5c(%rbp), %rsi
callq 0xc4f0
movq (%rax), %rax
cmpl $0x1, 0x48(%rax)
je 0x6bb4
jmp 0x6c36
movq -0x108(%rbp), %rdi
addq $0x8, %rdi
movslq -0x5c(%rbp), %rsi
callq 0xc4f0
movq -0x108(%rbp), %rdi
movq (%rax), %rsi
movq -0x58(%rbp), %rdx
addq $0x10, %rdx
callq 0x8550
testb $0x1, %al
jne 0x6be5
jmp 0x6c36
movq -0x108(%rbp), %rdi
movl $0x3, -0x78(%rbp)
movl $0x0, -0x74(%rbp)
movq -0x58(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x70(%rbp)
addq $0x8, %rdi
movslq -0x5c(%rbp), %rsi
callq 0xc4f0
movq (%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x18(%rbp), %rdi
addq $0x50, %rdi
movslq -0x5c(%rbp), %rsi
callq 0xce30
movq %rax, %rdi
leaq -0x78(%rbp), %rsi
callq 0xce50
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x6b67
jmp 0x6c46
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x6aea
movl $0x0, -0x7c(%rbp)
movslq -0x7c(%rbp), %rax
movq %rax, -0x138(%rbp)
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
callq 0xc590
movq %rax, %rcx
movq -0x138(%rbp), %rax
cmpq %rcx, %rax
jae 0x6dc3
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
movslq -0x7c(%rbp), %rsi
callq 0xc5b0
movq (%rax), %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
movslq 0x8(%rax), %rax
movq %rax, -0x140(%rbp)
movq -0x88(%rbp), %rax
movq (%rax), %rdi
addq $0x30, %rdi
callq 0xc4d0
movq %rax, %rcx
movq -0x140(%rbp), %rax
subq $0x1, %rcx
cmpq %rcx, %rax
je 0x6cde
jmp 0x6db5
movq -0x88(%rbp), %rax
movq (%rax), %rdi
addq $0x30, %rdi
movq -0x88(%rbp), %rax
movslq 0x8(%rax), %rsi
callq 0xc4f0
movq (%rax), %rdi
addq $0x8, %rdi
leaq 0x1247f(%rip), %rsi     # 0x19189
callq 0xc700
testb $0x1, %al
jne 0x6d18
jmp 0x6db3
movq -0x88(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x98(%rbp)
movl $0x4, -0xa0(%rbp)
movl $0x0, -0x9c(%rbp)
movq -0x88(%rbp), %rax
movq (%rax), %rdi
addq $0x30, %rdi
movq -0x88(%rbp), %rax
movslq 0x8(%rax), %rsi
callq 0xc4f0
movq (%rax), %rax
movq %rax, -0x90(%rbp)
movq -0x18(%rbp), %rax
addq $0x50, %rax
movq %rax, -0x148(%rbp)
movq -0x88(%rbp), %rax
movq (%rax), %rdi
addq $0x30, %rdi
movq -0x88(%rbp), %rax
movslq 0x8(%rax), %rsi
callq 0xc4f0
movq -0x148(%rbp), %rdi
movq (%rax), %rax
movslq (%rax), %rsi
callq 0xce30
movq %rax, %rdi
leaq -0xa0(%rbp), %rsi
callq 0xce50
jmp 0x6db5
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x6c5b
jmp 0x6dc5
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x69b1
movl $0x0, -0xa4(%rbp)
movq -0x108(%rbp), %rdi
movslq -0xa4(%rbp), %rax
movq %rax, -0x150(%rbp)
addq $0x38, %rdi
callq 0xc550
movq %rax, %rcx
movq -0x150(%rbp), %rax
cmpq %rcx, %rax
jae 0x6f03
movq -0x108(%rbp), %rdi
addq $0x38, %rdi
movslq -0xa4(%rbp), %rsi
callq 0xc570
movq (%rax), %rax
movq %rax, -0xb0(%rbp)
movl $0x0, -0xb4(%rbp)
movslq -0xb4(%rbp), %rax
movq %rax, -0x158(%rbp)
movq -0xb0(%rbp), %rdi
addq $0x50, %rdi
callq 0xcef0
movq %rax, %rcx
movq -0x158(%rbp), %rax
cmpq %rcx, %rax
jae 0x6eed
movq -0xb0(%rbp), %rdi
addq $0x50, %rdi
movslq -0xb4(%rbp), %rsi
callq 0xce30
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rdi
callq 0xcf20
cmpq $0x0, %rax
jne 0x6ed7
movq $0x0, -0xd0(%rbp)
movl $0x5, -0xd8(%rbp)
movl $0x0, -0xd4(%rbp)
movq $0x0, -0xc8(%rbp)
movq -0xc0(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0xce50
jmp 0x6ed9
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0x6e39
jmp 0x6eef
movl -0xa4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa4(%rbp)
jmp 0x6ddd
movl $0x0, -0xdc(%rbp)
movl $0x0, -0xe0(%rbp)
movq -0x108(%rbp), %rdi
movslq -0xe0(%rbp), %rax
movq %rax, -0x160(%rbp)
addq $0x38, %rdi
callq 0xc550
movq %rax, %rcx
movq -0x160(%rbp), %rax
cmpq %rcx, %rax
jae 0x70a0
movq -0x108(%rbp), %rdi
addq $0x38, %rdi
movslq -0xe0(%rbp), %rsi
callq 0xc570
movq (%rax), %rax
movq %rax, -0xe8(%rbp)
movl $0x0, -0xec(%rbp)
movslq -0xec(%rbp), %rax
movq %rax, -0x168(%rbp)
movq -0xe8(%rbp), %rdi
addq $0x50, %rdi
callq 0xcef0
movq %rax, %rcx
movq -0x168(%rbp), %rax
cmpq %rcx, %rax
jae 0x708a
movq -0xe8(%rbp), %rdi
addq $0x50, %rdi
movslq -0xec(%rbp), %rsi
callq 0xce30
movq %rax, -0xf8(%rbp)
movq -0xf8(%rbp), %rdi
callq 0xcf20
cmpq $0x2, %rax
jae 0x6fd9
jmp 0x7076
movl $0x1, -0xfc(%rbp)
movslq -0xfc(%rbp), %rax
movq %rax, -0x170(%rbp)
movq -0xf8(%rbp), %rdi
callq 0xcf20
movq %rax, %rcx
movq -0x170(%rbp), %rax
cmpq %rcx, %rax
jae 0x7074
movq -0xf8(%rbp), %rdi
movl -0xfc(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0xcf50
movq %rax, -0x178(%rbp)
movq -0xf8(%rbp), %rdi
movslq -0xfc(%rbp), %rsi
callq 0xcf50
movq -0x108(%rbp), %rdi
movq -0x178(%rbp), %rsi
movq %rax, %rdx
callq 0x92f0
addl -0xdc(%rbp), %eax
movl %eax, -0xdc(%rbp)
movl -0xfc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xfc(%rbp)
jmp 0x6fe3
jmp 0x7076
movl -0xec(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xec(%rbp)
jmp 0x6f73
jmp 0x708c
movl -0xe0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xe0(%rbp)
jmp 0x6f17
movq 0x1cf31(%rip), %rdi     # 0x23fd8
leaq 0x1212c(%rip), %rsi     # 0x191da
callq 0x32e0
movq %rax, %rdi
movl -0xdc(%rbp), %esi
callq 0x34d0
movq %rax, %rdi
movq 0x1cefd(%rip), %rsi     # 0x23fc8
callq 0x3320
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq
nopl (%rax)