// File: /workspace/github_c_makefile_star2_O2/coyove[P]chann/./lib/unqlite/unqlite.c
// Function: jx9CompileSwitch
static sxi32 jx9CompileSwitch(jx9_gen_state *pGen)
{
	GenBlock *pSwitchBlock;
	SyToken *pTmp, *pEnd;
	jx9_switch *pSwitch;
	sxu32 nLine;
	sxi32 rc;
	nLine = pGen->pIn->nLine;
	/* Jump the 'switch' keyword */
	pGen->pIn++;    
	if( pGen->pIn >= pGen->pEnd || (pGen->pIn->nType & JX9_TK_LPAREN) == 0 ){
		/* Syntax error */
		rc = jx9GenCompileError(pGen, E_ERROR, nLine, "Expected '(' after 'switch' keyword");
		if( rc == SXERR_ABORT ){
			/* Error count limit reached, abort immediately */
			return SXERR_ABORT;
		}
		goto Synchronize;
	}
	/* Jump the left parenthesis '(' */
	pGen->pIn++; 
	pEnd = 0; /* cc warning */
	/* Create the loop block */
	rc = GenStateEnterBlock(&(*pGen), GEN_BLOCK_LOOP|GEN_BLOCK_SWITCH, 
		jx9VmInstrLength(pGen->pVm), 0, &pSwitchBlock);
	if( rc != SXRET_OK ){
		return SXERR_ABORT;
	}
	/* Delimit the condition */
	jx9DelimitNestedTokens(pGen->pIn, pGen->pEnd, JX9_TK_LPAREN /* '(' */, JX9_TK_RPAREN /* ')' */, &pEnd);
	if( pGen->pIn == pEnd || pEnd >= pGen->pEnd ){
		/* Empty expression */
		rc = jx9GenCompileError(pGen, E_ERROR, nLine, "Expected expression after 'switch' keyword");
		if( rc == SXERR_ABORT ){
			/* Error count limit reached, abort immediately */
			return SXERR_ABORT;
		}
	}
	/* Swap token streams */
	pTmp = pGen->pEnd;
	pGen->pEnd = pEnd;
	/* Compile the expression */
	rc = jx9CompileExpr(&(*pGen), 0, 0);
	if( rc == SXERR_ABORT ){
		/* Expression handler request an operation abort [i.e: Out-of-memory] */
		return SXERR_ABORT;
	}
	/* Update token stream */
	while(pGen->pIn < pEnd ){
		rc = jx9GenCompileError(&(*pGen), E_ERROR, pGen->pIn->nLine, 
			"Switch: Unexpected token '%z'", &pGen->pIn->sData);
		if( rc == SXERR_ABORT ){
			return SXERR_ABORT;
		}
		pGen->pIn++;
	}
	pGen->pIn  = &pEnd[1];
	pGen->pEnd = pTmp;
	if( pGen->pIn >= pGen->pEnd || &pGen->pIn[1] >= pGen->pEnd ||
		(pGen->pIn->nType & (JX9_TK_OCB/*'{'*/|JX9_TK_COLON/*:*/)) == 0 ){
			pTmp = pGen->pIn;
			if( pTmp >= pGen->pEnd ){
				pTmp--;
			}
			/* Unexpected token */
			rc = jx9GenCompileError(&(*pGen), E_ERROR, pTmp->nLine, "Switch: Unexpected token '%z'", &pTmp->sData);
			if( rc == SXERR_ABORT ){
				return SXERR_ABORT;
			}
			goto Synchronize;
	}
	pGen->pIn++; /* Jump the leading curly braces/colons */
	/* Create the switch blocks container */
	pSwitch = (jx9_switch *)SyMemBackendAlloc(&pGen->pVm->sAllocator, sizeof(jx9_switch));
	if( pSwitch == 0 ){
		/* Abort compilation */
		return GenStateOutOfMem(pGen);
	}
	/* Zero the structure */
	SyZero(pSwitch, sizeof(jx9_switch));
	/* Initialize fields */
	SySetInit(&pSwitch->aCaseExpr, &pGen->pVm->sAllocator, sizeof(jx9_case_expr));
	/* Emit the switch instruction */
	jx9VmEmitInstr(pGen->pVm, JX9_OP_SWITCH, 0, 0, pSwitch, 0);
	/* Compile case blocks */
	for(;;){
		sxu32 nKwrd;
		if( pGen->pIn >= pGen->pEnd ){
			/* No more input to process */
			break;
		}
		if( (pGen->pIn->nType & JX9_TK_KEYWORD) == 0 ){
			if(  (pGen->pIn->nType & JX9_TK_CCB /*}*/) == 0 ){
				/* Unexpected token */
				rc = jx9GenCompileError(&(*pGen), E_ERROR, pGen->pIn->nLine, "Switch: Unexpected token '%z'", 
					&pGen->pIn->sData);
				if( rc == SXERR_ABORT ){
					return SXERR_ABORT;
				}
				/* FALL THROUGH */
			}
			/* Block compiled */
			break;
		}
		/* Extract the keyword */
		nKwrd = SX_PTR_TO_INT(pGen->pIn->pUserData);
		if( nKwrd == JX9_TKWRD_DEFAULT ){
			/*
			 * Accroding to the JX9 language reference manual
			 *  A special case is the default case. This case matches anything
			 *  that wasn't matched by the other cases.
			 */
			if( pSwitch->nDefault > 0 ){
				/* Default case already compiled */ 
				rc = jx9GenCompileError(&(*pGen), E_WARNING, pGen->pIn->nLine, "Switch: 'default' case already compiled");
				if( rc == SXERR_ABORT ){
					return SXERR_ABORT;
				}
			}
			pGen->pIn++; /* Jump the 'default' keyword */
			/* Compile the default block */
			rc = GenStateCompileSwitchBlock(pGen,&pSwitch->nDefault);
			if( rc == SXERR_ABORT){
				return SXERR_ABORT;
			}else if( rc == SXERR_EOF ){
				break;
			}
		}else if( nKwrd == JX9_TKWRD_CASE ){
			jx9_case_expr sCase;
			/* Standard case block */
			pGen->pIn++; /* Jump the 'case' keyword */
			/* initialize the structure */
			SySetInit(&sCase.aByteCode, &pGen->pVm->sAllocator, sizeof(VmInstr));
			/* Compile the case expression */
			rc = GenStateCompileCaseExpr(pGen, &sCase);
			if( rc == SXERR_ABORT ){
				return SXERR_ABORT;
			}
			/* Compile the case block */
			rc = GenStateCompileSwitchBlock(pGen,&sCase.nStart);
			/* Insert in the switch container */
			SySetPut(&pSwitch->aCaseExpr, (const void *)&sCase);
			if( rc == SXERR_ABORT){
				return SXERR_ABORT;
			}else if( rc == SXERR_EOF ){
				break;
			}
		}else{
			/* Unexpected token */
			rc = jx9GenCompileError(&(*pGen), E_ERROR, pGen->pIn->nLine, "Switch: Unexpected token '%z'", 
				&pGen->pIn->sData);
			if( rc == SXERR_ABORT ){
				return SXERR_ABORT;
			}
			break;
		}
	}
	/* Fix all jumps now the destination is resolved */
	pSwitch->nOut = jx9VmInstrLength(pGen->pVm);
	GenStateFixJumps(pSwitchBlock, -1, jx9VmInstrLength(pGen->pVm));
	/* Release the loop block */
	GenStateLeaveBlock(pGen, 0);
	if( pGen->pIn < pGen->pEnd ){
		/* Jump the trailing curly braces */
		pGen->pIn++;
	}
	/* Statement successfully compiled */
	return SXRET_OK;
Synchronize:
	/* Synchronize with the first semi-colon */
	while( pGen->pIn < pGen->pEnd && (pGen->pIn->nType & JX9_TK_SEMI) == 0 ){
		pGen->pIn++;
	}
	return SXRET_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rbx
movq 0x158(%rdi), %rax
movl 0x14(%rax), %r15d
leaq 0x20(%rax), %rcx
movq %rcx, 0x158(%rdi)
pushq $-0xa
popq %r14
cmpq 0x160(%rdi), %rcx
jae 0x41728
testb $0x2, 0x31(%rax)
jne 0x4176d
leaq 0x2436d(%rip), %rcx     # 0x65a9c
pushq $0x1
popq %rsi
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0x3eed6
cmpl $-0xa, %eax
je 0x417a1
movq 0x158(%rbx), %rax
movq 0x160(%rbx), %rcx
xorl %r14d, %r14d
cmpq %rcx, %rax
jae 0x417a1
testb $0x4, 0x12(%rax)
jne 0x417a1
addq $0x20, %rax
movq %rax, 0x158(%rbx)
jmp 0x41755
addq $0x40, %rax
movq %rax, 0x158(%rbx)
andq $0x0, -0x48(%rbp)
movq (%rbx), %rax
movq 0xf0(%rax), %rax
movl 0x10(%rax), %edx
leaq -0x50(%rbp), %r8
movq %rbx, %rdi
movl $0x101, %esi            # imm = 0x101
xorl %ecx, %ecx
callq 0x4246c
testl %eax, %eax
je 0x417b3
movl %r14d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x158(%rbx), %rdi
movq 0x160(%rbx), %rsi
leaq -0x48(%rbp), %r12
movl $0x200, %edx            # imm = 0x200
movl $0x400, %ecx            # imm = 0x400
movq %r12, %r8
callq 0x424f6
movq (%r12), %r13
cmpq %r13, 0x158(%rbx)
je 0x417f0
movq 0x160(%rbx), %r12
cmpq %r12, %r13
jb 0x41813
leaq 0x242c9(%rip), %rcx     # 0x65ac0
pushq $0x1
popq %rsi
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0x3eed6
cmpl $-0xa, %eax
je 0x417a1
movq 0x160(%rbx), %r12
movq %r13, 0x160(%rbx)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4012f
cmpl $-0xa, %eax
je 0x417a1
movq 0x158(%rbx), %r8
leaq 0x242ae(%rip), %r15     # 0x65aeb
cmpq %r13, %r8
jae 0x41873
movl 0x14(%r8), %edx
movq %rbx, %rdi
pushq $0x1
popq %rsi
movq %r15, %rcx
xorl %eax, %eax
callq 0x3eed6
cmpl $-0xa, %eax
je 0x417a1
movq 0x158(%rbx), %r8
addq $0x20, %r8
movq %r8, 0x158(%rbx)
jmp 0x4183d
leaq 0x20(%r13), %r8
movq %r8, 0x158(%rbx)
movq %r12, 0x160(%rbx)
cmpq %r12, %r8
jae 0x41b17
leaq 0x40(%r13), %rax
cmpq %r12, %rax
jae 0x41b1a
testl $0x100040, 0x30(%r13)   # imm = 0x100040
je 0x41b1a
movq %rax, 0x158(%rbx)
movq (%rbx), %rdi
pushq $0x30
popq %rsi
callq 0x2ca19
testq %rax, %rax
je 0x41b37
movq %rax, %r12
pushq $0x30
popq %rsi
movq %rax, %rdi
callq 0x27aea
movq (%rbx), %rdi
movabsq $0x3000000000, %rax     # imm = 0x3000000000
movq %rax, 0x14(%r12)
andl $0x0, 0x10(%r12)
andl $0x0, 0x1c(%r12)
movq %rdi, (%r12)
andq $0x0, 0x8(%r12)
andq $0x0, 0x20(%r12)
pushq $0x45
popq %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r12, %r8
xorl %r9d, %r9d
callq 0x3ee6f
movq %r12, %r13
addq $0x2c, %r13
movq %r12, -0x40(%rbp)
movq %r13, -0x38(%rbp)
movq 0x158(%rbx), %r8
movq 0x160(%rbx), %r15
cmpq %r15, %r8
jae 0x41b69
movl 0x10(%r8), %eax
testb $0x4, %al
je 0x41b44
movl 0x18(%r8), %eax
cmpl $0x5, %eax
je 0x419b6
cmpl $0xa, %eax
jne 0x41b48
cmpl $0x0, (%r13)
je 0x41985
movl 0x14(%r8), %edx
movq %rbx, %rdi
pushq $0x2
popq %rsi
leaq 0x2419b(%rip), %rcx     # 0x65b09
xorl %eax, %eax
callq 0x3eed6
cmpl $-0xa, %eax
je 0x417a1
movq 0x158(%rbx), %r8
addq $0x20, %r8
movq %r8, 0x158(%rbx)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x42e45
xorl %ecx, %ecx
cmpl $-0x12, %eax
sete %dl
cmpl $-0xa, %eax
je 0x417a1
movb %dl, %cl
leal (%rcx,%rcx,4), %ecx
jmp 0x41b04
leaq 0x20(%r8), %rax
movq %rax, 0x158(%rbx)
movq (%rbx), %rax
movabsq $0x1800000000, %rcx     # imm = 0x1800000000
movq %rcx, -0x6c(%rbp)
andl $0x0, -0x70(%rbp)
andl $0x0, -0x64(%rbp)
movq %rax, -0x80(%rbp)
andq $0x0, -0x78(%rbp)
andq $0x0, -0x60(%rbp)
xorl %edx, %edx
pushq $0x20
popq %rcx
leaq (%r8,%rcx), %r13
cmpq %r15, %r13
jae 0x41a21
movl 0x10(%r8,%rcx), %esi
btl $0x9, %esi
jb 0x41a15
btl $0xa, %esi
jb 0x41a19
testl $0x140000, %esi         # imm = 0x140000
je 0x41a1b
testl %edx, %edx
jg 0x41a1b
jmp 0x41a21
incl %edx
jmp 0x41a1b
decl %edx
addq $0x20, %rcx
jmp 0x419ed
cmpq $0x20, %rcx
ja 0x41a59
movl 0x34(%r8), %edx
movq %rbx, %rdi
pushq $0x1
popq %r15
movl %r15d, %esi
leaq 0x240f5(%rip), %rcx     # 0x65b31
xorl %eax, %eax
callq 0x3eed6
movl %r15d, %ecx
cmpl $-0xa, %eax
je 0x41b00
movq (%rbx), %rax
movq 0x160(%rbx), %r15
movq %r13, 0x160(%rbx)
movq 0xf0(%rax), %r12
leaq -0x80(%rbp), %rcx
movq %rcx, 0xf0(%rax)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4012f
movl %eax, -0x2c(%rbp)
movq (%rbx), %rdi
xorl %edx, %edx
cmpl $-0x3, %eax
setne %dl
pushq $0x1
popq %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x3ee6f
movq (%rbx), %rax
leaq 0xc8(%rax), %rcx
testq %r12, %r12
cmovneq %r12, %rcx
movq -0x40(%rbp), %r12
movq %rcx, 0xf0(%rax)
movq %r13, 0x158(%rbx)
movq %r15, 0x160(%rbx)
pushq $0x1
popq %rcx
cmpl $-0xa, -0x2c(%rbp)
je 0x41b00
movq %rbx, %rdi
leaq -0x58(%rbp), %rsi
callq 0x42e45
movl %eax, %r13d
movq %r12, %rdi
leaq -0x80(%rbp), %rsi
callq 0x2be00
pushq $0x1
popq %rcx
cmpl $-0xa, %r13d
je 0x41b00
cmpl $-0x12, %r13d
jne 0x41afe
pushq $0x5
popq %rcx
jmp 0x41b00
xorl %ecx, %ecx
movq -0x38(%rbp), %r13
testl %ecx, %ecx
je 0x41921
cmpl $0x5, %ecx
jne 0x417a1
jmp 0x41b69
movq %r13, %r8
movl 0x14(%r8), %edx
leaq 0x23fc6(%rip), %rcx     # 0x65aeb
pushq $0x1
popq %rsi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3eed6
jmp 0x4173f
movq %rbx, %rdi
callq 0x426d9
jmp 0x417a1
testb %al, %al
js 0x41b69
movl 0x14(%r8), %edx
leaq 0x23f98(%rip), %rcx     # 0x65aeb
pushq $0x1
popq %rsi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3eed6
cmpl $-0xa, %eax
je 0x417a1
movq (%rbx), %rax
movq 0xf0(%rax), %rax
movl 0x10(%rax), %edx
movl %edx, 0x28(%r12)
movq -0x50(%rbp), %rdi
pushq $-0x1
popq %rsi
callq 0x42648
movq %rbx, %rdi
callq 0x426b3
movq 0x158(%rbx), %rax
xorl %r14d, %r14d
cmpq 0x160(%rbx), %rax
jae 0x417a1
addq $0x20, %rax
movq %rax, 0x158(%rbx)
jmp 0x417a1