// File: /workspace/github_c_makefile_star2_O3/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 0x4b036
testb $0x2, 0x31(%rax)
jne 0x4b08f
leaq 0x2aa7f(%rip), %rcx     # 0x75abc
movq %rbx, %rdi
movl $0x1, %esi
movl %r15d, %edx
xorl %eax, %eax
callq 0x483d1
movl $0xfffffff6, %r14d      # imm = 0xFFFFFFF6
cmpl $-0xa, %eax
je 0x4b0c4
movq 0x158(%rbx), %rcx
movq 0x160(%rbx), %rax
xorl %r14d, %r14d
cmpq %rax, %rcx
jae 0x4b0c4
addq $0x20, %rcx
testb $0x4, -0xe(%rcx)
jne 0x4b0c4
movq %rcx, 0x158(%rbx)
leaq 0x20(%rcx), %rdx
cmpq %rax, %rcx
movq %rdx, %rcx
jb 0x4b074
jmp 0x4b0c4
addq $0x40, %rax
movq %rax, 0x158(%rbx)
movq (%rbx), %rax
movq 0xf0(%rax), %rax
movl 0x10(%rax), %edx
leaq -0x40(%rbp), %r8
movq %rbx, %rdi
movl $0x101, %esi            # imm = 0x101
xorl %ecx, %ecx
callq 0x4bdf9
movl $0xfffffff6, %r14d      # imm = 0xFFFFFFF6
testl %eax, %eax
je 0x4b0d6
movl %r14d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x158(%rbx), %r12
movq 0x160(%rbx), %r13
cmpq %r13, %r12
jae 0x4b129
movl $0x1, %ecx
xorl %eax, %eax
movl 0x10(%r12,%rax), %edx
btl $0x9, %edx
jb 0x4b10e
btl $0xa, %edx
jae 0x4b110
cmpl $0x2, %ecx
jl 0x4b3ca
decl %ecx
jmp 0x4b110
incl %ecx
leaq (%r12,%rax), %rdx
addq $0x20, %rdx
addq $0x20, %rax
cmpq %r13, %rdx
jb 0x4b0f0
addq %rax, %r12
cmpq %r13, %r12
jb 0x4b152
leaq 0x2a9b0(%rip), %rcx     # 0x75ae0
movq %rbx, %rdi
movl $0x1, %esi
movl %r15d, %edx
xorl %eax, %eax
callq 0x483d1
cmpl $-0xa, %eax
je 0x4b0c4
movq 0x160(%rbx), %r13
movq %r12, 0x160(%rbx)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x49789
cmpl $-0xa, %eax
je 0x4b0c4
movq 0x158(%rbx), %r8
cmpq %r12, %r8
jae 0x4b1b7
leaq 0x2a98a(%rip), %r15     # 0x75b0b
movl 0x14(%r8), %edx
movq %rbx, %rdi
movl $0x1, %esi
movq %r15, %rcx
xorl %eax, %eax
callq 0x483d1
cmpl $-0xa, %eax
je 0x4b0c4
movq 0x158(%rbx), %r8
addq $0x20, %r8
movq %r8, 0x158(%rbx)
cmpq %r12, %r8
jb 0x4b181
leaq 0x20(%r12), %r8
movq %r8, 0x158(%rbx)
movq %r13, 0x160(%rbx)
cmpq %r13, %r8
jae 0x4b3a8
leaq 0x40(%r12), %rax
cmpq %r13, %rax
jae 0x4b3ab
testl $0x100040, 0x30(%r12)   # imm = 0x100040
je 0x4b3ab
movq %rax, 0x158(%rbx)
movq (%rbx), %rdi
movl $0x30, %esi
callq 0x33d86
testq %rax, %rax
je 0x4b3d8
movq %rax, %r15
movq $-0x4, %rax
movl $0x0, 0x4(%r15,%rax)
addq $0x4, %rax
cmpq $0x2c, %rax
jb 0x4b217
movq (%rbx), %rdi
movabsq $0x3000000000, %rax     # imm = 0x3000000000
movq %rax, 0x14(%r15)
xorl %eax, %eax
movl %eax, 0x10(%r15)
movl %eax, 0x1c(%r15)
movq %rdi, (%r15)
movq %rax, 0x8(%r15)
movq %rax, 0x20(%r15)
movl $0x45, %esi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r15, %r8
xorl %r9d, %r9d
callq 0x48365
movq %r15, %r12
addq $0x2c, %r12
leaq -0x70(%rbp), %rdx
movq %r12, -0x38(%rbp)
movq 0x158(%rbx), %r8
cmpq 0x160(%rbx), %r8
jae 0x4b41f
movl 0x10(%r8), %eax
testb $0x4, %al
je 0x4b3f8
movl 0x18(%r8), %eax
cmpl $0x5, %eax
je 0x4b30c
cmpl $0xa, %eax
jne 0x4b3fc
cmpl $0x0, (%r12)
je 0x4b2d6
movl 0x14(%r8), %edx
movq %rbx, %rdi
movl $0x2, %esi
leaq 0x2a86a(%rip), %rcx     # 0x75b29
xorl %eax, %eax
callq 0x483d1
cmpl $-0xa, %eax
je 0x4b0c4
movq 0x158(%rbx), %r8
addq $0x20, %r8
movq %r8, 0x158(%rbx)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4c819
xorl %ecx, %ecx
cmpl $-0x12, %eax
sete %dl
cmpl $-0xa, %eax
je 0x4b0c4
movb %dl, %cl
leal (%rcx,%rcx,4), %r13d
leaq -0x70(%rbp), %rdx
jmp 0x4b393
addq $0x20, %r8
movq %r8, 0x158(%rbx)
movq (%rbx), %rax
movabsq $0x1800000000, %rcx     # imm = 0x1800000000
movq %rcx, -0x5c(%rbp)
xorl %ecx, %ecx
movl %ecx, -0x60(%rbp)
movl %ecx, -0x54(%rbp)
movq %rax, -0x70(%rbp)
movq %rcx, -0x68(%rbp)
movq %rcx, -0x50(%rbp)
movq %rbx, %rdi
movq %rdx, %r12
movq %rdx, %rsi
callq 0x4c90f
movl $0x1, %r13d
cmpl $-0xa, %eax
jne 0x4b35a
movq %r12, %rdx
jmp 0x4b38f
movq %rbx, %rdi
leaq -0x48(%rbp), %rsi
callq 0x4c819
movl %eax, -0x2c(%rbp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x33094
movl -0x2c(%rbp), %eax
cmpl $-0xa, %eax
movq %r12, %rdx
je 0x4b38f
cmpl $-0x12, %eax
jne 0x4b38c
movl $0x5, %r13d
jmp 0x4b38f
xorl %r13d, %r13d
movq -0x38(%rbp), %r12
testl %r13d, %r13d
je 0x4b273
cmpl $0x5, %r13d
jne 0x4b0c4
jmp 0x4b41f
movq %r12, %r8
movl 0x14(%r8), %edx
leaq 0x2a755(%rip), %rcx     # 0x75b0b
movq %rbx, %rdi
movl $0x1, %esi
xorl %eax, %eax
callq 0x483d1
jmp 0x4b055
testq %rax, %rax
jne 0x4b121
jmp 0x4b129
leaq 0x2a37a(%rip), %rcx     # 0x75759
movq %rbx, %rdi
movl $0x1, %esi
movl $0x1, %edx
xorl %eax, %eax
callq 0x483d1
jmp 0x4b0c4
testb %al, %al
js 0x4b41f
movl 0x14(%r8), %edx
leaq 0x2a704(%rip), %rcx     # 0x75b0b
movq %rbx, %rdi
movl $0x1, %esi
xorl %eax, %eax
callq 0x483d1
cmpl $-0xa, %eax
je 0x4b0c4
movq (%rbx), %rax
movq 0xf0(%rax), %rax
movl 0x10(%rax), %edx
movl %edx, 0x28(%r15)
movq -0x40(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x4bff5
movq 0xc8(%rbx), %rdi
testq %rdi, %rdi
je 0x4b45a
movq 0x8(%rdi), %rax
movq %rax, 0xc8(%rbx)
callq 0x484ee
movq 0x158(%rbx), %rax
xorl %r14d, %r14d
cmpq 0x160(%rbx), %rax
jae 0x4b0c4
addq $0x20, %rax
movq %rax, 0x158(%rbx)
jmp 0x4b0c4