// File: /workspace/github_c_makefile_star2_O1/coyove[P]chann/./lib/unqlite/unqlite.c
// Function: jx9CompileForeach
static sxi32 jx9CompileForeach(jx9_gen_state *pGen)
{ 
	SyToken *pCur, *pTmp, *pEnd = 0;
	GenBlock *pForeachBlock = 0;
	jx9_foreach_info *pInfo;
	sxu32 nFalseJump;
	VmInstr *pInstr;
	sxu32 nLine;
	sxi32 rc;
	nLine = pGen->pIn->nLine;
	/* Jump the 'foreach' keyword */
	pGen->pIn++;    
	if( pGen->pIn >= pGen->pEnd || (pGen->pIn->nType & JX9_TK_LPAREN) == 0 ){
		/* Syntax error */
		rc = jx9GenCompileError(pGen, E_ERROR, nLine, "foreach: Expected '('");
		if( rc == SXERR_ABORT ){
			/* Error count limit reached, abort immediately */
			return SXERR_ABORT;
		}
		goto Synchronize;
	}
	/* Jump the left parenthesis '(' */
	pGen->pIn++; 
	/* Create the loop block */
	rc = GenStateEnterBlock(&(*pGen), GEN_BLOCK_LOOP, jx9VmInstrLength(pGen->pVm), 0, &pForeachBlock);
	if( rc != SXRET_OK ){
		return SXERR_ABORT;
	}
	/* Delimit the expression */
	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, "foreach: Missing expression");
		if( rc == SXERR_ABORT ){
			/* Error count limit reached, abort immediately */
			return SXERR_ABORT;
		}
		/* Synchronize */
		pGen->pIn = pEnd;
		if( pGen->pIn < pGen->pEnd ){
			pGen->pIn++;
		}
		return SXRET_OK;
	}
	/* Compile the array expression */
	pCur = pGen->pIn;
	while( pCur < pEnd ){
		if( pCur->nType & JX9_TK_KEYWORD ){
			sxi32 nKeywrd = SX_PTR_TO_INT(pCur->pUserData);
			if( nKeywrd == JX9_TKWRD_AS ){
				/* Break with the first 'as' found */
				break;
			}
		}
		/* Advance the stream cursor */
		pCur++;
	}
	if( pCur <= pGen->pIn ){
		rc = jx9GenCompileError(&(*pGen), E_ERROR, pGen->pIn->nLine, 
			"foreach: Missing array/object expression");
		if( rc == SXERR_ABORT ){
			/* Don't worry about freeing memory, everything will be released shortly */
			return SXERR_ABORT;
		}
		goto Synchronize;
	}
	/* Swap token streams */
	pTmp = pGen->pEnd;
	pGen->pEnd = pCur;
	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 < pCur ){
		rc = jx9GenCompileError(&(*pGen), E_ERROR, pGen->pIn->nLine, "foreach: Unexpected token '%z'", &pGen->pIn->sData);
		if( rc == SXERR_ABORT ){
			/* Don't worry about freeing memory, everything will be released shortly */
			return SXERR_ABORT;
		}
		pGen->pIn++;
	}
	pCur++; /* Jump the 'as' keyword */
	pGen->pIn = pCur; 
	if( pGen->pIn >= pEnd ){
		rc = jx9GenCompileError(&(*pGen), E_ERROR, pGen->pIn->nLine, "foreach: Missing $key => $value pair");
		if( rc == SXERR_ABORT ){
			return SXERR_ABORT;
		}
	}
	/* Create the foreach context */
	pInfo = (jx9_foreach_info *)SyMemBackendAlloc(&pGen->pVm->sAllocator, sizeof(jx9_foreach_info));
	if( pInfo == 0 ){
		jx9GenCompileError(&(*pGen), E_ERROR, pGen->pIn->nLine, "Fatal, JX9 engine is running out-of-memory");
		return SXERR_ABORT;
	}
	/* Zero the structure */
	SyZero(pInfo, sizeof(jx9_foreach_info));
	/* Initialize structure fields */
	SySetInit(&pInfo->aStep, &pGen->pVm->sAllocator, sizeof(jx9_foreach_step *));
	/* Check if we have a key field */
	while( pCur < pEnd && (pCur->nType & JX9_TK_COMMA) == 0 ){
		pCur++;
	}
	if( pCur < pEnd ){
		/* Compile the expression holding the key name */
		if( pGen->pIn >= pCur ){
			rc = jx9GenCompileError(&(*pGen), E_ERROR, pGen->pIn->nLine, "foreach: Missing $key");
			if( rc == SXERR_ABORT ){
				/* Don't worry about freeing memory, everything will be released shortly */
				return SXERR_ABORT;
			}
		}else{
			pGen->pEnd = pCur;
			rc = jx9CompileExpr(&(*pGen), 0, GenStateForEachNodeValidator);
			if( rc == SXERR_ABORT ){
				/* Don't worry about freeing memory, everything will be released shortly */
				return SXERR_ABORT;
			}
			pInstr = jx9VmPopInstr(pGen->pVm);
			if( pInstr->p3 ){
				/* Record key name */
				SyStringInitFromBuf(&pInfo->sKey, pInstr->p3, SyStrlen((const char *)pInstr->p3));
			}
			pInfo->iFlags |= JX9_4EACH_STEP_KEY;
		}
		pGen->pIn = &pCur[1]; /* Jump the arrow */
	}
	pGen->pEnd = pEnd;
	if( pGen->pIn >= pEnd ){
		rc = jx9GenCompileError(&(*pGen), E_ERROR, pGen->pIn->nLine, "foreach: Missing $value");
		if( rc == SXERR_ABORT ){
			/* Don't worry about freeing memory, everything will be released shortly */
			return SXERR_ABORT;
		}
		goto Synchronize;
	}
	/* Compile the expression holding the value name */
	rc = jx9CompileExpr(&(*pGen), 0, GenStateForEachNodeValidator);
	if( rc == SXERR_ABORT ){
		/* Don't worry about freeing memory, everything will be released shortly */
		return SXERR_ABORT;
	}
	pInstr = jx9VmPopInstr(pGen->pVm);
	if( pInstr->p3 ){
		/* Record value name */
		SyStringInitFromBuf(&pInfo->sValue, pInstr->p3, SyStrlen((const char *)pInstr->p3));
	}
	/* Emit the 'FOREACH_INIT' instruction */
	jx9VmEmitInstr(pGen->pVm, JX9_OP_FOREACH_INIT, 0, 0, pInfo, &nFalseJump);
	/* Save the instruction index so we can fix it later when the jump destination is resolved */
	GenStateNewJumpFixup(pForeachBlock, JX9_OP_FOREACH_INIT, nFalseJump);
	/* Record the first instruction to execute */
	pForeachBlock->nFirstInstr = jx9VmInstrLength(pGen->pVm);
	/* Emit the FOREACH_STEP instruction */
    jx9VmEmitInstr(pGen->pVm, JX9_OP_FOREACH_STEP, 0, 0, pInfo, &nFalseJump);
	/* Save the instruction index so we can fix it later when the jump destination is resolved */
	GenStateNewJumpFixup(pForeachBlock, JX9_OP_FOREACH_STEP, nFalseJump);
	/* Compile the loop body */
	pGen->pIn = &pEnd[1];
	pGen->pEnd = pTmp;
	rc = jx9CompileBlock(&(*pGen));
	if( rc == SXERR_ABORT ){
		/* Don't worry about freeing memory, everything will be released shortly */
		return SXERR_ABORT;
	}
	/* Emit the unconditional jump to the start of the loop */
	jx9VmEmitInstr(pGen->pVm, JX9_OP_JMP, 0, pForeachBlock->nFirstInstr, 0, 0);
	/* Fix all jumps now the destination is resolved */
	GenStateFixJumps(pForeachBlock, -1,jx9VmInstrLength(pGen->pVm));
	/* Release the loop block */
	GenStateLeaveBlock(pGen, 0);
	/* Statement successfully compiled */
	return SXRET_OK;
Synchronize:
	/* Synchronize with the first semi-colon ';' so we can avoid 
	 * compiling this erroneous block.
	 */
	while( pGen->pIn < pGen->pEnd && (pGen->pIn->nType & (JX9_TK_SEMI|JX9_TK_OCB)) == 0 ){
		pGen->pIn++;
	}
	return SXRET_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
movq $0x0, -0x38(%rbp)
movq 0x158(%rdi), %rax
movl 0x14(%rax), %r15d
leaq 0x20(%rax), %rcx
movq %rcx, 0x158(%rdi)
cmpq 0x160(%rdi), %rcx
jae 0x4b5ef
testb $0x2, 0x31(%rax)
jne 0x4b64b
leaq 0x2c277(%rip), %rcx     # 0x7786d
movq %rbx, %rdi
movl $0x1, %esi
movl %r15d, %edx
xorl %eax, %eax
callq 0x49434
movl $0xfffffff6, %r14d      # imm = 0xFFFFFFF6
cmpl $-0xa, %eax
je 0x4b680
movq 0x158(%rbx), %rcx
movq 0x160(%rbx), %rax
xorl %r14d, %r14d
cmpq %rax, %rcx
jae 0x4b680
addq $0x20, %rcx
testl $0x40040, -0x10(%rcx)   # imm = 0x40040
jne 0x4b680
movq %rcx, 0x158(%rbx)
leaq 0x20(%rcx), %rdx
cmpq %rax, %rcx
movq %rdx, %rcx
jb 0x4b62d
jmp 0x4b680
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 $0x1, %esi
xorl %ecx, %ecx
callq 0x4cf52
movl $0xfffffff6, %r14d      # imm = 0xFFFFFFF6
testl %eax, %eax
je 0x4b692
movl %r14d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x158(%rbx), %rax
movq 0x160(%rbx), %r13
movq %rax, %r12
cmpq %r13, %rax
jae 0x4b6d5
movl $0x1, %ecx
movq %rax, %r12
movl 0x10(%r12), %edx
btl $0x9, %edx
jb 0x4b6ca
btl $0xa, %edx
jae 0x4b6cc
cmpl $0x2, %ecx
jl 0x4b6d5
decl %ecx
jmp 0x4b6cc
incl %ecx
addq $0x20, %r12
cmpq %r13, %r12
jb 0x4b6b0
cmpq %r12, %rax
sete %cl
cmpq %r13, %r12
setae %dl
orb %cl, %dl
jne 0x4b777
movq %rax, %r15
cmpq %r12, %rax
jae 0x4b70b
movq %rax, %r15
testb $0x4, 0x10(%r15)
je 0x4b702
cmpl $0xb, 0x18(%r15)
je 0x4b70b
addq $0x20, %r15
cmpq %r12, %r15
jb 0x4b6f4
cmpq %rax, %r15
jbe 0x4b7bc
movq %r15, 0x160(%rbx)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4a7a9
cmpl $-0xa, %eax
je 0x4b680
movq 0x158(%rbx), %r8
cmpq %r15, %r8
jae 0x4b7da
movl 0x14(%r8), %edx
movq %rbx, %rdi
movl $0x1, %esi
leaq 0x2c175(%rip), %rcx     # 0x778c8
xorl %eax, %eax
callq 0x49434
cmpl $-0xa, %eax
je 0x4b680
movq 0x158(%rbx), %r8
addq $0x20, %r8
movq %r8, 0x158(%rbx)
jmp 0x4b737
leaq 0x2c105(%rip), %rcx     # 0x77883
movq %rbx, %rdi
movl $0x1, %esi
movl %r15d, %edx
xorl %eax, %eax
callq 0x49434
cmpl $-0xa, %eax
je 0x4b680
movq %r12, 0x158(%rbx)
cmpq 0x160(%rbx), %r12
jae 0x4b7b4
addq $0x20, %r12
movq %r12, 0x158(%rbx)
xorl %r14d, %r14d
jmp 0x4b680
movl 0x14(%rax), %edx
leaq 0x2c0d9(%rip), %rcx     # 0x7789f
movq %rbx, %rdi
movl $0x1, %esi
xorl %eax, %eax
callq 0x49434
jmp 0x4b60e
leaq 0x20(%r15), %rax
movq %rax, 0x158(%rbx)
movq %rax, -0x40(%rbp)
cmpq %r12, %rax
jb 0x4b811
movl 0x34(%r15), %edx
leaq 0x2c0ee(%rip), %rcx     # 0x778e7
movq %rbx, %rdi
movl $0x1, %esi
xorl %eax, %eax
callq 0x49434
cmpl $-0xa, %eax
je 0x4b680
movq (%rbx), %rdi
movl $0x50, %esi
callq 0x343ed
testq %rax, %rax
je 0x4b91f
movq $-0x4, %rcx
movl $0x0, 0x4(%rax,%rcx)
addq $0x4, %rcx
cmpq $0x4c, %rcx
jb 0x4b82e
movq (%rbx), %rdx
movabsq $0x800000000, %rcx      # imm = 0x800000000
movq %rcx, 0x3c(%rax)
xorl %ecx, %ecx
movl %ecx, 0x38(%rax)
movl %ecx, 0x44(%rax)
movq %rdx, 0x28(%rax)
movq %rcx, 0x30(%rax)
movq %rcx, 0x48(%rax)
movq -0x40(%rbp), %rdx
cmpq %r12, %rdx
setb %cl
jae 0x4b978
testb $0x2, 0x32(%r15)
jne 0x4b89e
addq $0x40, %r15
cmpq %r12, %r15
setb %cl
jae 0x4b978
leaq 0x20(%r15), %rdx
testb $0x2, 0x12(%r15)
movq %rdx, %r15
je 0x4b880
addq $-0x20, %rdx
testb %cl, %cl
je 0x4b978
movq %rax, %r15
movq 0x158(%rbx), %rax
cmpq %rdx, %rax
movq %rdx, -0x40(%rbp)
jae 0x4b944
movq %rdx, 0x160(%rbx)
leaq 0x1ff7(%rip), %rdx      # 0x4d8c2
movq %rbx, %rdi
xorl %esi, %esi
callq 0x4a7a9
cmpl $-0xa, %eax
je 0x4b680
movq (%rbx), %rax
movq 0xf0(%rax), %rax
movl 0x10(%rax), %ecx
movq 0x8(%rax), %rdx
decl %ecx
movl %ecx, 0x10(%rax)
imull 0x18(%rax), %ecx
movq 0x10(%rdx,%rcx), %rdi
testq %rdi, %rdi
je 0x4b915
movq %r15, -0x50(%rbp)
movq %rdi, (%r15)
callq 0x2ff3c
movq -0x50(%rbp), %rcx
movl %eax, 0x8(%rcx)
movq %r15, %rax
orb $0x1, 0x20(%r15)
jmp 0x4b969
movq 0x158(%rbx), %rax
movl 0x14(%rax), %edx
leaq 0x2bfdc(%rip), %rcx     # 0x7790c
movq %rbx, %rdi
movl $0x1, %esi
xorl %eax, %eax
callq 0x49434
jmp 0x4b680
movl 0x14(%rax), %edx
leaq 0x2bfe9(%rip), %rcx     # 0x77937
movq %rbx, %rdi
movl $0x1, %esi
xorl %eax, %eax
callq 0x49434
cmpl $-0xa, %eax
movq %r15, %rax
je 0x4b680
movq -0x40(%rbp), %rcx
addq $0x20, %rcx
movq %rcx, 0x158(%rbx)
movq %r12, 0x160(%rbx)
movq 0x158(%rbx), %rcx
cmpq %r12, %rcx
jae 0x4bae9
movq %rax, %r15
leaq 0x1f29(%rip), %rdx      # 0x4d8c2
movq %rbx, %rdi
xorl %esi, %esi
callq 0x4a7a9
cmpl $-0xa, %eax
je 0x4b680
movq (%rbx), %rsi
movq 0xf0(%rsi), %rax
movl 0x10(%rax), %ecx
movq 0x8(%rax), %rdx
decl %ecx
movl %ecx, 0x10(%rax)
imull 0x18(%rax), %ecx
movq 0x10(%rdx,%rcx), %rdi
testq %rdi, %rdi
je 0x4b9e5
movq %rdi, 0x10(%r15)
movq %rsi, -0x40(%rbp)
callq 0x2ff3c
movq -0x40(%rbp), %rsi
movl %eax, 0x18(%r15)
leaq -0x2c(%rbp), %r9
movq %rsi, %rdi
movl $0x43, %esi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r15, %r8
callq 0x493c1
movq -0x38(%rbp), %rdi
leaq -0x2c(%rbp), %rax
movl (%rax), %eax
leaq -0x48(%rbp), %rsi
movl $0x43, (%rsi)
movl %eax, 0x4(%rsi)
addq $0x18, %rdi
callq 0x336fc
movq (%rbx), %rdi
movq 0xf0(%rdi), %rax
movl 0x10(%rax), %eax
movq -0x38(%rbp), %rcx
movl %eax, 0x10(%rcx)
movl $0x44, %esi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r15, %r8
leaq -0x2c(%rbp), %r9
callq 0x493c1
movq -0x38(%rbp), %rdi
leaq -0x2c(%rbp), %rax
movl (%rax), %eax
leaq -0x48(%rbp), %rcx
movl $0x44, (%rcx)
movl %eax, 0x4(%rcx)
addq $0x18, %rdi
leaq -0x48(%rbp), %rsi
callq 0x336fc
addq $0x20, %r12
movq %r12, 0x158(%rbx)
movq %r13, 0x160(%rbx)
movq %rbx, %rdi
callq 0x4d031
cmpl $-0xa, %eax
je 0x4b680
movq (%rbx), %rdi
movq -0x38(%rbp), %rax
movl 0x10(%rax), %ecx
movl $0x8, %esi
xorl %edx, %edx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x493c1
movq -0x38(%rbp), %rdi
movq (%rbx), %rax
movq 0xf0(%rax), %rax
movl 0x10(%rax), %edx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x4d152
movq 0xc8(%rbx), %rdi
testq %rdi, %rdi
je 0x4b7b4
movq 0x8(%rdi), %rax
movq %rax, 0xc8(%rbx)
callq 0x4955d
jmp 0x4b7b4
movl 0x14(%rcx), %edx
leaq 0x2be5a(%rip), %rcx     # 0x7794d
jmp 0x4b7c6