// File: /workspace/github_c_makefile_star2_O1/coyove[P]chann/./lib/unqlite/unqlite.c
// Function: jx9CompileStatic
static sxi32 jx9CompileStatic(jx9_gen_state *pGen)
{
	jx9_vm_func_static_var sStatic; /* Structure describing the static variable */
	jx9_vm_func *pFunc;             /* Enclosing function */
	GenBlock *pBlock;
	SyString *pName;
	char *zDup;
	sxu32 nLine;
	sxi32 rc;
	/* Jump the static keyword */
	nLine = pGen->pIn->nLine;
	pGen->pIn++;
	/* Extract the enclosing function if any */
	pBlock = pGen->pCurrent;
	while( pBlock ){
		if( pBlock->iFlags & GEN_BLOCK_FUNC){
			break;
		}
		/* Point to the upper block */
		pBlock = pBlock->pParent;
	}
	if( pBlock == 0 ){
		/* Static statement, called outside of a function body, treat it as a simple variable. */
		if( pGen->pIn >= pGen->pEnd || (pGen->pIn->nType & JX9_TK_DOLLAR) == 0 ){
			rc = jx9GenCompileError(&(*pGen), E_ERROR, nLine, "Expected variable after 'static' keyword");
			if( rc == SXERR_ABORT ){
				return SXERR_ABORT;
			}
			goto Synchronize;
		}
		/* Compile the expression holding the variable */
		rc = jx9CompileExpr(&(*pGen), 0, 0);
		if( rc == SXERR_ABORT ){
			return SXERR_ABORT;
		}else if( rc != SXERR_EMPTY ){
			/* Emit the POP instruction */
			jx9VmEmitInstr(pGen->pVm, JX9_OP_POP, 1, 0, 0, 0);
		}
		return SXRET_OK;
	}
	pFunc = (jx9_vm_func *)pBlock->pUserData;
	/* Make sure we are dealing with a valid statement */
	if( pGen->pIn >= pGen->pEnd || (pGen->pIn->nType & JX9_TK_DOLLAR) == 0 || &pGen->pIn[1] >= pGen->pEnd ||
		(pGen->pIn[1].nType & (JX9_TK_ID|JX9_TK_KEYWORD)) == 0 ){
			rc = jx9GenCompileError(&(*pGen), E_ERROR, nLine, "Expected variable after 'static' keyword");
			if( rc == SXERR_ABORT ){
				return SXERR_ABORT;
			}
			goto Synchronize;
	}
	pGen->pIn++;
	/* Extract variable name */
	pName = &pGen->pIn->sData;
	pGen->pIn++; /* Jump the var name */
	if( pGen->pIn < pGen->pEnd && (pGen->pIn->nType & (JX9_TK_SEMI/*';'*/|JX9_TK_EQUAL/*'='*/)) == 0 ){
		rc = jx9GenCompileError(&(*pGen), E_ERROR, pGen->pIn->nLine, "static: Unexpected token '%z'", &pGen->pIn->sData);
		goto Synchronize;
	}
	/* Initialize the structure describing the static variable */
	SySetInit(&sStatic.aByteCode, &pGen->pVm->sAllocator, sizeof(VmInstr));
	sStatic.nIdx = SXU32_HIGH; /* Not yet created */
	/* Duplicate variable name */
	zDup = SyMemBackendStrDup(&pGen->pVm->sAllocator, pName->zString, pName->nByte);
	if( zDup == 0 ){
		jx9GenCompileError(&(*pGen), E_ERROR, nLine, "Fatal, JX9 engine is running out of memory");
		return SXERR_ABORT;
	}
	SyStringInitFromBuf(&sStatic.sName, zDup, pName->nByte);
	/* Check if we have an expression to compile */
	if( pGen->pIn < pGen->pEnd && (pGen->pIn->nType & JX9_TK_EQUAL) ){
		SySet *pInstrContainer;
		pGen->pIn++; /* Jump the equal '=' sign */
		/* Swap bytecode container */
		pInstrContainer = jx9VmGetByteCodeContainer(pGen->pVm);
		jx9VmSetByteCodeContainer(pGen->pVm, &sStatic.aByteCode);
		/* Compile the expression */
		rc = jx9CompileExpr(&(*pGen), 0, 0);
		/* Emit the done instruction */
		jx9VmEmitInstr(pGen->pVm, JX9_OP_DONE, (rc != SXERR_EMPTY ? 1 : 0), 0, 0, 0);
		/* Restore default bytecode container */
		jx9VmSetByteCodeContainer(pGen->pVm, pInstrContainer);
	}
	/* Finally save the compiled static variable in the appropriate container */
	SySetPut(&pFunc->aStatic, (const void *)&sStatic);
	return SXRET_OK;
Synchronize:
	/* Synchronize with the first semi-colon ';', so we can avoid compiling this erroneous
	 * statement. 
	 */
	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 %r12
pushq %rbx
subq $0x40, %rsp
movq %rdi, %rbx
movq 0xc8(%rdi), %rax
movq 0x158(%rdi), %r12
movl 0x14(%r12), %r14d
leaq 0x20(%r12), %rcx
movq %rcx, 0x158(%rdi)
testq %rax, %rax
je 0x4c96b
testb $0x8, 0x14(%rax)
jne 0x4c9bf
movq 0x8(%rax), %rax
jmp 0x4c95a
cmpq 0x160(%rbx), %rcx
jae 0x4caf2
testb $0x10, 0x30(%r12)
je 0x4caf2
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4a7a9
cmpl $-0xa, %eax
je 0x4cb49
cmpl $-0x3, %eax
je 0x4c9b8
movq (%rbx), %rdi
movl $0xb, %esi
movl $0x1, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x493c1
xorl %eax, %eax
jmp 0x4cb49
movq 0x160(%rbx), %rdx
cmpq %rdx, %rcx
jae 0x4caf2
testb $0x10, 0x30(%r12)
je 0x4caf2
leaq 0x40(%r12), %rcx
cmpq %rdx, %rcx
jae 0x4caf2
testb $0xc, 0x50(%r12)
je 0x4caf2
movq 0x40(%rax), %r15
leaq 0x60(%r12), %r8
movq %r8, 0x158(%rbx)
cmpq %rdx, %r8
jae 0x4ca16
testb $0x44, 0x72(%r12)
je 0x4cb76
movq (%rbx), %rdi
movabsq $0x1800000000, %rax     # imm = 0x1800000000
movq %rax, -0x3c(%rbp)
xorl %eax, %eax
movl %eax, -0x40(%rbp)
movl %eax, -0x34(%rbp)
movq %rdi, -0x50(%rbp)
movq %rax, -0x48(%rbp)
movq %rax, -0x30(%rbp)
movl $0xffffffff, -0x28(%rbp) # imm = 0xFFFFFFFF
movq 0x40(%r12), %rsi
movl 0x48(%r12), %edx
callq 0x3e425
testq %rax, %rax
je 0x4cb56
movq %rax, -0x60(%rbp)
movl 0x48(%r12), %eax
movl %eax, -0x58(%rbp)
movq 0x158(%rbx), %rax
cmpq 0x160(%rbx), %rax
jae 0x4cadd
testb $0x40, 0x12(%rax)
je 0x4cadd
leaq -0x50(%rbp), %rcx
addq $0x20, %rax
movq %rax, 0x158(%rbx)
movq (%rbx), %rax
movq 0xf0(%rax), %r14
movq %rcx, 0xf0(%rax)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4a7a9
movq (%rbx), %rdi
xorl %edx, %edx
cmpl $-0x3, %eax
setne %dl
movl $0x1, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x493c1
movq (%rbx), %rax
leaq 0xc8(%rax), %rcx
testq %r14, %r14
cmovneq %r14, %rcx
movq %rcx, 0xf0(%rax)
addq $0x28, %r15
leaq -0x60(%rbp), %rsi
movq %r15, %rdi
callq 0x336fc
jmp 0x4c9b8
leaq 0x2b143(%rip), %rcx     # 0x77c3c
movq %rbx, %rdi
movl $0x1, %esi
movl %r14d, %edx
xorl %eax, %eax
callq 0x49434
movl %eax, %ecx
movl $0xfffffff6, %eax       # imm = 0xFFFFFFF6
cmpl $-0xa, %ecx
je 0x4cb49
movq 0x158(%rbx), %rdx
movq 0x160(%rbx), %rcx
xorl %eax, %eax
cmpq %rcx, %rdx
jae 0x4cb49
addq $0x20, %rdx
testb $0x4, -0xe(%rdx)
jne 0x4cb49
movq %rdx, 0x158(%rbx)
leaq 0x20(%rdx), %rsi
cmpq %rcx, %rdx
movq %rsi, %rdx
jb 0x4cb30
addq $0x40, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2b126(%rip), %rcx     # 0x77c83
movq %rbx, %rdi
movl $0x1, %esi
movl %r14d, %edx
xorl %eax, %eax
callq 0x49434
movl $0xfffffff6, %eax       # imm = 0xFFFFFFF6
jmp 0x4cb49
movl 0x74(%r12), %edx
leaq 0x2b0e3(%rip), %rcx     # 0x77c65
movq %rbx, %rdi
movl $0x1, %esi
xorl %eax, %eax
callq 0x49434
jmp 0x4cb17