// File: /workspace/github_c_makefile_star2_O2/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
andq $0x0, -0x48(%rbp)
andq $0x0, -0x30(%rbp)
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 0x40d75
testb $0x2, 0x31(%rax)
jne 0x40dbd
leaq 0x24adc(%rip), %rcx     # 0x65858
pushq $0x1
popq %rsi
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0x3eed6
cmpl $-0xa, %eax
je 0x40dea
movq 0x158(%rbx), %rax
movq 0x160(%rbx), %rcx
xorl %r14d, %r14d
cmpq %rcx, %rax
jae 0x40dea
testl $0x40040, 0x10(%rax)    # imm = 0x40040
jne 0x40dea
addq $0x20, %rax
movq %rax, 0x158(%rbx)
jmp 0x40da2
addq $0x40, %rax
movq %rax, 0x158(%rbx)
movq (%rbx), %rax
movq 0xf0(%rax), %rax
movl 0x10(%rax), %edx
pushq $0x1
popq %rsi
leaq -0x30(%rbp), %r8
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x4246c
testl %eax, %eax
je 0x40dfc
movl %r14d, %eax
addq $0x28, %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 0x158(%rbx), %rax
movq (%r12), %r13
cmpq %r13, %rax
je 0x40e5d
movq 0x160(%rbx), %rcx
cmpq %rcx, %r13
jae 0x40e5d
leaq 0x20(%rax), %r15
leaq -0x20(%r15), %r12
cmpq %r13, %r12
jae 0x40ea0
testb $0x4, -0x10(%r15)
je 0x40e57
cmpl $0xb, -0x8(%r15)
je 0x40ea0
addq $0x20, %r15
jmp 0x40e40
leaq 0x24a0a(%rip), %rcx     # 0x6586e
pushq $0x1
popq %rsi
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0x3eed6
cmpl $-0xa, %eax
je 0x40dea
movq %r13, 0x158(%rbx)
cmpq 0x160(%rbx), %r13
jae 0x40e98
addq $0x20, %r13
movq %r13, 0x158(%rbx)
xorl %r14d, %r14d
jmp 0x40dea
cmpq %rax, %r12
jbe 0x40f0e
movq %rcx, -0x40(%rbp)
movq %r12, 0x160(%rbx)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4012f
cmpl $-0xa, %eax
je 0x40dea
leaq 0x20(%r15), %rax
movq %rax, -0x38(%rbp)
movq 0x158(%rbx), %r8
cmpq %r12, %r8
jae 0x40f23
movl 0x14(%r8), %edx
movq %rbx, %rdi
pushq $0x1
popq %rsi
leaq 0x249c9(%rip), %rcx     # 0x658b3
xorl %eax, %eax
callq 0x3eed6
cmpl $-0xa, %eax
je 0x40dea
movq 0x158(%rbx), %r8
addq $0x20, %r8
movq %r8, 0x158(%rbx)
jmp 0x40ed4
movl 0x14(%rax), %edx
leaq 0x24972(%rip), %rcx     # 0x6588a
pushq $0x1
popq %rsi
movq %rbx, %rdi
jmp 0x40d85
movq %r15, 0x158(%rbx)
cmpq %r13, %r15
jb 0x40f50
movl 0x14(%r15), %edx
leaq 0x24998(%rip), %rcx     # 0x658d2
pushq $0x1
popq %rsi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3eed6
cmpl $-0xa, %eax
je 0x40dea
movq (%rbx), %rdi
pushq $0x50
popq %rsi
callq 0x2ca19
testq %rax, %rax
je 0x40fb3
movq %rax, %r15
pushq $0x50
popq %rsi
movq %rax, %rdi
callq 0x27aea
movq (%rbx), %rax
movabsq $0x800000000, %rcx      # imm = 0x800000000
movq %rcx, 0x3c(%r15)
andl $0x0, 0x38(%r15)
andl $0x0, 0x44(%r15)
movq %rax, 0x28(%r15)
andq $0x0, 0x30(%r15)
andq $0x0, 0x48(%r15)
movq -0x38(%rbp), %r12
leaq -0x20(%r12), %rax
cmpq %r13, %rax
jae 0x40fd6
testb $0x2, -0xe(%r12)
jne 0x40fdf
addq $0x20, %r12
jmp 0x40f9b
movq 0x158(%rbx), %rax
movl 0x14(%rax), %edx
leaq 0x24933(%rip), %rcx     # 0x658f7
pushq $0x1
popq %rsi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3eed6
jmp 0x40dea
movq 0x158(%rbx), %r12
jmp 0x41057
movq 0x158(%rbx), %rcx
cmpq %rax, %rcx
jae 0x41030
movq %rax, 0x160(%rbx)
leaq 0x1d03(%rip), %rdx      # 0x42cfc
movq %rbx, %rdi
xorl %esi, %esi
callq 0x4012f
cmpl $-0xa, %eax
je 0x40dea
movq (%rbx), %rdi
callq 0x42d40
movq 0x10(%rax), %rdi
testq %rdi, %rdi
je 0x41029
movq %rdi, (%r15)
callq 0x28e24
movl %eax, 0x8(%r15)
orb $0x1, 0x20(%r15)
jmp 0x41050
movl 0x14(%rcx), %edx
leaq 0x248e8(%rip), %rcx     # 0x65922
pushq $0x1
popq %rsi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3eed6
cmpl $-0xa, %eax
je 0x40dea
movq %r12, 0x158(%rbx)
movq %r13, 0x160(%rbx)
cmpq %r13, %r12
jae 0x41165
leaq 0x1c8e(%rip), %rdx      # 0x42cfc
movq %rbx, %rdi
xorl %esi, %esi
callq 0x4012f
cmpl $-0xa, %eax
je 0x40dea
movq (%rbx), %rdi
callq 0x42d40
movq 0x10(%rax), %rdi
testq %rdi, %rdi
je 0x4109f
movq %rdi, 0x10(%r15)
callq 0x28e24
movl %eax, 0x18(%r15)
movq (%rbx), %rdi
pushq $0x43
popq %rsi
leaq -0x4c(%rbp), %r12
xorl %edx, %edx
xorl %ecx, %ecx
movq %r15, %r8
movq %r12, %r9
callq 0x3ee6f
movq -0x30(%rbp), %rdi
movl (%r12), %edx
pushq $0x43
popq %rsi
callq 0x42528
movq (%rbx), %rdi
movq 0xf0(%rdi), %rax
movl 0x10(%rax), %eax
movq -0x30(%rbp), %rcx
movl %eax, 0x10(%rcx)
pushq $0x44
popq %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r15, %r8
movq %r12, %r9
callq 0x3ee6f
movq -0x30(%rbp), %rdi
movl (%r12), %edx
pushq $0x44
popq %rsi
callq 0x42528
addq $0x20, %r13
movq %r13, 0x158(%rbx)
movq -0x40(%rbp), %rax
movq %rax, 0x160(%rbx)
movq %rbx, %rdi
callq 0x4254b
cmpl $-0xa, %eax
je 0x40dea
movq (%rbx), %rdi
movq -0x30(%rbp), %rax
movl 0x10(%rax), %ecx
pushq $0x8
popq %rsi
xorl %edx, %edx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x3ee6f
movq -0x30(%rbp), %rdi
movq (%rbx), %rax
movq 0xf0(%rax), %rax
movl 0x10(%rax), %edx
pushq $-0x1
popq %rsi
callq 0x42648
movq %rbx, %rdi
callq 0x426b3
jmp 0x40e98
movl 0x14(%r12), %edx
leaq 0x247c7(%rip), %rcx     # 0x65938
jmp 0x40f18