// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
movq %rdi, %rbx
movq 0x158(%rdi), %rax
movl 0x14(%rax), %r15d
leaq 0x20(%rax), %rcx
movq %rcx, 0x158(%rdi)
cmpq 0x160(%rdi), %rcx
jae 0x4c180
testb $0x2, 0x31(%rax)
jne 0x4c1d9
leaq 0x2b92a(%rip), %rcx     # 0x77ab1
movq %rbx, %rdi
movl $0x1, %esi
movl %r15d, %edx
xorl %eax, %eax
callq 0x49434
movl $0xfffffff6, %r14d      # imm = 0xFFFFFFF6
cmpl $-0xa, %eax
je 0x4c20e
movq 0x158(%rbx), %rcx
movq 0x160(%rbx), %rax
xorl %r14d, %r14d
cmpq %rax, %rcx
jae 0x4c20e
addq $0x20, %rcx
testb $0x4, -0xe(%rcx)
jne 0x4c20e
movq %rcx, 0x158(%rbx)
leaq 0x20(%rcx), %rdx
cmpq %rax, %rcx
movq %rdx, %rcx
jb 0x4c1be
jmp 0x4c20e
addq $0x40, %rax
movq %rax, 0x158(%rbx)
movq (%rbx), %rax
movq 0xf0(%rax), %rax
movl 0x10(%rax), %edx
leaq -0x38(%rbp), %r8
movq %rbx, %rdi
movl $0x101, %esi            # imm = 0x101
xorl %ecx, %ecx
callq 0x4cf52
movl $0xfffffff6, %r14d      # imm = 0xFFFFFFF6
testl %eax, %eax
je 0x4c220
movl %r14d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x158(%rbx), %rcx
movq 0x160(%rbx), %rax
movq %rcx, %r12
cmpq %rax, %rcx
jae 0x4c263
movl $0x1, %edx
movq %rcx, %r12
movl 0x10(%r12), %esi
btl $0x9, %esi
jb 0x4c258
btl $0xa, %esi
jae 0x4c25a
cmpl $0x2, %edx
jl 0x4c263
decl %edx
jmp 0x4c25a
incl %edx
addq $0x20, %r12
cmpq %rax, %r12
jb 0x4c23e
cmpq %r12, %rcx
setne %cl
cmpq %rax, %r12
setb %al
testb %al, %cl
jne 0x4c295
leaq 0x2b85b(%rip), %rcx     # 0x77ad5
movq %rbx, %rdi
movl $0x1, %esi
movl %r15d, %edx
xorl %eax, %eax
callq 0x49434
cmpl $-0xa, %eax
je 0x4c20e
movq 0x160(%rbx), %r13
movq %r12, 0x160(%rbx)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4a7a9
cmpl $-0xa, %eax
je 0x4c20e
movq 0x158(%rbx), %r8
cmpq %r12, %r8
jae 0x4c301
leaq 0x2b835(%rip), %r15     # 0x77b00
movl 0x14(%r8), %edx
movq %rbx, %rdi
movl $0x1, %esi
movq %r15, %rcx
xorl %eax, %eax
callq 0x49434
cmpl $-0xa, %eax
je 0x4c20e
movq 0x158(%rbx), %r8
addq $0x20, %r8
movq %r8, 0x158(%rbx)
cmpq %r12, %r8
jb 0x4c2cb
leaq 0x20(%r12), %r8
movq %r8, 0x158(%rbx)
movq %r13, 0x160(%rbx)
cmpq %r13, %r8
jae 0x4c5a0
leaq 0x40(%r12), %rax
cmpq %r13, %rax
jae 0x4c5a3
testl $0x100040, 0x30(%r12)   # imm = 0x100040
je 0x4c5a3
movq %rax, 0x158(%rbx)
movq (%rbx), %rdi
movl $0x30, %esi
callq 0x343ed
testq %rax, %rax
je 0x4c5c2
movq %rax, %r15
movq $-0x4, %rax
movl $0x0, 0x4(%r15,%rax)
addq $0x4, %rax
cmpq $0x2c, %rax
jb 0x4c361
movq (%rbx), %rdi
movabsq $0x3000000000, %rax     # imm = 0x3000000000
movq %rax, 0x14(%r15)
xorl %r12d, %r12d
movl %r12d, 0x10(%r15)
movl %r12d, 0x1c(%r15)
movq %rdi, (%r15)
movq %r12, 0x8(%r15)
movq %r12, 0x20(%r15)
movl $0x45, %esi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r15, %r8
xorl %r9d, %r9d
callq 0x493c1
movq %r15, %rax
addq $0x2c, %rax
movq %rax, -0x30(%rbp)
movq 0x158(%rbx), %r8
movl $0x5, %r13d
cmpq 0x160(%rbx), %r8
jae 0x4c52b
movl 0x10(%r8), %eax
testb $0x4, %al
jne 0x4c414
testb %al, %al
js 0x4c409
movl 0x14(%r8), %edx
movl $0x1, %r13d
movq %rbx, %rdi
movl $0x1, %esi
leaq 0x2b707(%rip), %rcx     # 0x77b00
xorl %eax, %eax
callq 0x49434
cmpl $-0xa, %eax
je 0x4c52b
movl $0x5, %r13d
jmp 0x4c52b
movl 0x18(%r8), %eax
cmpl $0x5, %eax
je 0x4c47d
cmpl $0xa, %eax
jne 0x4c501
movq -0x30(%rbp), %rax
cmpl $0x0, (%rax)
je 0x4c458
movl 0x14(%r8), %edx
movq %rbx, %rdi
movl $0x2, %esi
leaq 0x2b6dc(%rip), %rcx     # 0x77b1e
xorl %eax, %eax
callq 0x49434
movl $0x1, %r13d
cmpl $-0xa, %eax
je 0x4c52b
addq $0x20, 0x158(%rbx)
movq %rbx, %rdi
movq -0x30(%rbp), %rsi
callq 0x4da23
xorl %ecx, %ecx
cmpl $-0x12, %eax
sete %cl
leal (%rcx,%rcx,4), %r13d
cmpl $-0xa, %eax
jmp 0x4c4f6
addq $0x20, %r8
movq %r8, 0x158(%rbx)
movq (%rbx), %rax
movabsq $0x1800000000, %rcx     # imm = 0x1800000000
movq %rcx, -0x54(%rbp)
movl %r12d, -0x58(%rbp)
movl %r12d, -0x4c(%rbp)
movq %rax, -0x68(%rbp)
movq %r12, -0x60(%rbp)
movq %r12, -0x48(%rbp)
movq %rbx, %rdi
leaq -0x68(%rbp), %rsi
callq 0x4db07
movl $0x1, %r13d
cmpl $-0xa, %eax
je 0x4c52b
movq %rbx, %rdi
leaq -0x40(%rbp), %rsi
callq 0x4da23
movl %eax, %r12d
movq %r15, %rdi
leaq -0x68(%rbp), %rsi
callq 0x336fc
xorl %eax, %eax
cmpl $-0x12, %r12d
sete %al
leal (%rax,%rax,4), %r13d
cmpl $-0xa, %r12d
movl $0x0, %r12d
movl $0x1, %eax
cmovel %eax, %r13d
jmp 0x4c52b
movl 0x14(%r8), %edx
movq %rbx, %rdi
movl $0x1, %esi
leaq 0x2b5ec(%rip), %rcx     # 0x77b00
xorl %eax, %eax
callq 0x49434
xorl %ecx, %ecx
cmpl $-0xa, %eax
setne %cl
leal 0x1(,%rcx,4), %r13d
testl %r13d, %r13d
je 0x4c3ba
cmpl $0x5, %r13d
jne 0x4c20e
movq (%rbx), %rax
movq 0xf0(%rax), %rax
movl 0x10(%rax), %edx
movl %edx, 0x28(%r15)
movq -0x38(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x4d152
movq 0xc8(%rbx), %rdi
testq %rdi, %rdi
je 0x4c579
movq 0x8(%rdi), %rax
movq %rax, 0xc8(%rbx)
callq 0x4955d
movq 0x158(%rbx), %rax
xorl %r14d, %r14d
cmpq 0x160(%rbx), %rax
jae 0x4c20e
addq $0x20, %rax
movq %rax, 0x158(%rbx)
jmp 0x4c20e
movq %r12, %r8
movl 0x14(%r8), %edx
leaq 0x2b552(%rip), %rcx     # 0x77b00
movq %rbx, %rdi
movl $0x1, %esi
xorl %eax, %eax
callq 0x49434
jmp 0x4c19f
leaq 0x2b185(%rip), %rcx     # 0x7774e
movq %rbx, %rdi
movl $0x1, %esi
movl $0x1, %edx
xorl %eax, %eax
callq 0x49434
jmp 0x4c20e