Migrate built-in classes and interfaces to native C
Build / AerScript (push) Successful in 16s

This commit is contained in:
2026-11-19 16:15:05 +01:00
parent 58f604aea5
commit bcfa4c6163
8 changed files with 574 additions and 174 deletions
+70 -14
View File
@@ -107,6 +107,7 @@ static sxi32 PH7_GenStateEnterBlock(
* a tiny chunk of memory, there is no much we can do here.
*/
PH7_GenCompileError(&(*pGen), E_ERROR, 1, "PH7 engine is running out-of-memory");
return SXERR_ABORT;
}
/* Zero the structure */
SyZero(pBlock, sizeof(GenBlock));
@@ -284,6 +285,7 @@ static ph7_value *PH7_GenStateInstallNumLiteral(ph7_gen_state *pGen, sxu32 *pIdx
pObj = PH7_ReserveConstObj(pGen->pVm, &nIdx);
if(pObj == 0) {
PH7_GenCompileError(&(*pGen), E_ERROR, 1, "PH7 engine is running out-of-memory");
return 0;
}
*pIdx = nIdx;
return pObj;
@@ -442,6 +444,7 @@ PH7_PRIVATE sxi32 PH7_CompileSimpleString(ph7_gen_state *pGen, sxi32 iCompileFla
pObj = PH7_ReserveConstObj(pGen->pVm, &nIdx);
if(pObj == 0) {
PH7_GenCompileError(&(*pGen), E_ERROR, 1, "PH7 engine is running out-of-memory");
return SXERR_ABORT;
}
PH7_MemObjInitFromString(pGen->pVm, pObj, 0);
/* Compile the node */
@@ -1980,6 +1983,10 @@ static void GenStateCloseScope(ph7_gen_state *pGen, sxu32 nFirst) {
sxu32 n, nEnd = PH7_VmInstrLength(pGen->pVm);
pScope = (VmScopeInfo *)SyMemBackendAlloc(&pGen->pVm->sAllocator, sizeof(VmScopeInfo));
if(pScope == 0) {
if(pGen->pVm->bInit) {
PH7_GenCompileError(pGen, E_ERROR, 1, "PH7 engine is running out-of-memory");
return;
}
PH7_VmMemoryError(pGen->pVm);
}
pScope->pParent = 0;
@@ -3371,6 +3378,7 @@ static sxi32 PH7_GenStateCollectFuncArgs(ph7_vm_func *pFunc, ph7_gen_state *pGen
SyStringInitFromBuf(&sArg.sClass, zDup, pName->nByte);
} else {
PH7_GenCompileError(pGen, E_ERROR, pIn->nLine, "PH7 engine is running out-of-memory");
return SXERR_ABORT;
}
}
pIn++;
@@ -3399,6 +3407,7 @@ static sxi32 PH7_GenStateCollectFuncArgs(ph7_vm_func *pFunc, ph7_gen_state *pGen
zDup = SyMemBackendStrDup(&pGen->pVm->sAllocator, SyStringData(&pIn->sData), SyStringLength(&pIn->sData));
if(zDup == 0) {
PH7_GenCompileError(&(*pGen), E_ERROR, pIn->nLine, "PH7 engine is running out-of-memory");
return SXERR_ABORT;
}
SyStringInitFromBuf(&sArg.sName, zDup, SyStringLength(&pIn->sData));
pIn++;
@@ -3489,7 +3498,10 @@ static sxi32 PH7_GenStateCollectFuncArgs(ph7_vm_func *pFunc, ph7_gen_state *pGen
SyBlobRelease(&sSig);
}
/* Save in the argument set */
SySetPut(&pFunc->aArgs, (const void *)&sArg);
rc = SySetPut(&pFunc->aArgs, (const void *)&sArg);
if(rc != SXRET_OK && pGen->pVm->bInit) {
return SXERR_ABORT;
}
}
if(SyBlobLength(&sSig) > 0) {
/* Save function signature */
@@ -3514,6 +3526,7 @@ static sxi32 PH7_GenStateCompileFuncBody(
rc = PH7_GenStateEnterBlock(&(*pGen), GEN_BLOCK_PROTECTED | GEN_BLOCK_FUNC, PH7_VmInstrLength(pGen->pVm), pFunc, &pBlock);
if(rc != SXRET_OK) {
PH7_GenCompileError(&(*pGen), E_ERROR, 1, "PH7 engine is running out-of-memory");
return SXERR_ABORT;
}
/* The formal arguments are declared for the whole body: hand their pinned
* categories to the static comparison checker, owned by this body block.
@@ -3541,7 +3554,11 @@ static sxi32 PH7_GenStateCompileFuncBody(
pInstrContainer = PH7_VmGetByteCodeContainer(pGen->pVm);
PH7_VmSetByteCodeContainer(pGen->pVm, &pFunc->aByteCode);
/* Compile the body */
PH7_CompileBlock(&(*pGen));
rc = PH7_CompileBlock(&(*pGen));
if(rc == SXERR_ABORT || pGen->pVm->iInitError) {
PH7_VmSetByteCodeContainer(pGen->pVm, pInstrContainer);
return SXERR_ABORT;
}
/* Fix exception jumps now the destination is resolved */
PH7_GenStateFixJumps(pGen->pCurrent, PH7_OP_THROW, PH7_VmInstrLength(pGen->pVm));
/* Mark the implicit function epilogue, distinct from other DONE instructions. */
@@ -3885,6 +3902,7 @@ loop:
pAttr = PH7_NewClassAttr(pGen->pVm, pClass, pName, nLine, iProtection, iFlags, nType);
if(pAttr == 0) {
PH7_GenCompileError(pGen, E_ERROR, nLine, "PH7 engine is running out-of-memory");
return SXERR_ABORT;
}
if(pGen->pIn->nType & PH7_TK_EQUAL /*'='*/) {
SySet *pInstrContainer;
@@ -3895,6 +3913,10 @@ loop:
/* Compile attribute value.
*/
rc = PH7_CompileExpr(&(*pGen), EXPR_FLAG_COMMA_STATEMENT, 0);
if(pGen->pVm->iInitError || rc == SXERR_ABORT) {
PH7_VmSetByteCodeContainer(pGen->pVm, pInstrContainer);
return SXERR_ABORT;
}
if(rc == SXERR_EMPTY) {
PH7_GenCompileError(pGen, E_ERROR, nLine, "Attribute '%z': Missing default value", pName);
}
@@ -3987,6 +4009,7 @@ static sxi32 PH7_GenStateCompileClassMethod(
pMeth = PH7_NewClassMethod(pGen->pVm, pClass, pName, nLine, iProtection, iFlags);
if(pMeth == 0) {
PH7_GenCompileError(&(*pGen), E_ERROR, nLine, "PH7 is running out-of-memory");
return SXERR_ABORT;
}
/* Jump the left parenthesis '(' */
pGen->pIn++;
@@ -4078,13 +4101,16 @@ static sxi32 PH7_CompileClassInterface(ph7_gen_state *pGen) {
pGen->pIn++;
/* Obtain a raw class */
pClass = PH7_NewRawClass(pGen->pVm, pName);
if(pClass == 0) {
if(pClass == 0 || (pGen->pVm->bInit &&
(!pClass->hMethod.apBucket || !pClass->hAttr.apBucket || !pClass->hDerived.apBucket))) {
PH7_GenCompileError(pGen, E_ERROR, nLine, "PH7 is running out-of-memory");
return SXERR_ABORT;
}
/* Obtain a raw class inheritance storage */
pClassInfo = PH7_NewClassInfo(pGen->pVm, pName);
if(pClassInfo == 0) {
PH7_GenCompileError(pGen, E_ERROR, nLine, "PH7 is running out-of-memory");
return SXERR_ABORT;
}
/* Mark as an interface */
pClass->pInfo = pClassInfo;
@@ -4288,13 +4314,16 @@ static sxi32 PH7_GenStateCompileClass(ph7_gen_state *pGen, sxi32 iFlags) {
pGen->pIn++;
/* Obtain a raw class */
pClass = PH7_NewRawClass(pGen->pVm, pName);
if(pClass == 0) {
if(pClass == 0 || (pGen->pVm->bInit &&
(!pClass->hMethod.apBucket || !pClass->hAttr.apBucket || !pClass->hDerived.apBucket))) {
PH7_GenCompileError(pGen, E_ERROR, nLine, "PH7 is running out-of-memory");
return SXERR_ABORT;
}
/* Obtain a raw class inheritance storage */
pClassInfo = PH7_NewClassInfo(pGen->pVm, pName);
if(pClassInfo == 0) {
PH7_GenCompileError(pGen, E_ERROR, nLine, "PH7 is running out-of-memory");
return SXERR_ABORT;
}
pClass->pInfo = pClassInfo;
/* Assume a standalone class */
@@ -5598,7 +5627,9 @@ static sxi32 PH7_CompileExpr(
nExpr = 0;
pRoot = 0;
SySetInit(&sExprNode, &pGen->pVm->sAllocator, sizeof(ph7_expr_node *));
SySetAlloc(&sExprNode, 0x10);
if(SySetAlloc(&sExprNode, 0x10) != SXRET_OK && pGen->pVm->bInit) {
return SXERR_ABORT;
}
rc = SXRET_OK;
/* Delimit the expression */
pEnd = pGen->pIn;
@@ -5673,6 +5704,9 @@ static sxi32 PH7_CompileExpr(
pGen->pEnd = pEnd;
/* Try to get an expression tree */
rc = PH7_ExprMakeTree(&(*pGen), &sExprNode, &pRoot);
if(rc != SXRET_OK && pGen->pVm->bInit) {
rc = SXERR_ABORT;
}
if(rc == SXRET_OK && pRoot) {
rc = SXRET_OK;
if(xTreeValidator) {
@@ -5859,8 +5893,9 @@ static sxi32 PH7_GenStateCompileChunk(
/* Compile the statement */
rc = xCons(&(*pGen));
}
if(rc == SXERR_ABORT) {
if(rc == SXERR_ABORT || pGen->pVm->iInitError) {
/* Request to abort compilation */
rc = SXERR_ABORT;
break;
}
}
@@ -5902,8 +5937,9 @@ static sxi32 PH7_GenStateCompileGlobalScope(
}
/* Compile the statement */
rc = xCons(&(*pGen));
if(rc == SXERR_ABORT) {
if(rc == SXERR_ABORT || pGen->pVm->iInitError) {
/* Request to abort compilation */
rc = SXERR_ABORT;
break;
}
} else {
@@ -5936,7 +5972,10 @@ static sxi32 PH7_CompileScript(
/* Advance the stream cursor */
pGen->pRawIn++;
/* Tokenize the Aer chunk first */
PH7_TokenizeAerScript(SyStringData(&pScript->sData), SyStringLength(&pScript->sData), pScript->nLine, &(*pTokenSet));
rc = PH7_TokenizeAerScript(SyStringData(&pScript->sData), SyStringLength(&pScript->sData), pScript->nLine, &(*pTokenSet));
if(rc != SXRET_OK && pGen->pVm->bInit) {
return SXERR_ABORT;
}
/* Point to the head and tail of the token stream. */
pGen->pIn = (SyToken *)SySetBasePtr(pTokenSet);
pGen->pEnd = &pGen->pIn[SySetUsed(pTokenSet)];
@@ -5989,12 +6028,19 @@ PH7_PRIVATE sxi32 PH7_CompileAerScript(
/* Initialize the tokens containers */
SySetInit(&aRawToken, &pVm->sAllocator, sizeof(SyToken));
SySetInit(&aAerToken, &pVm->sAllocator, sizeof(SyToken));
SySetAlloc(&aAerToken, 0xc0);
rc = SySetAlloc(&aAerToken, 0xc0);
if(rc != SXRET_OK && pVm->bInit) {
return rc;
}
SyToken sTmp;
sTmp.nLine = 1;
sTmp.pUserData = 0;
SyStringDupPtr(&sTmp.sData, pScript);
SySetPut(&aRawToken, (const void *)&sTmp);
rc = SySetPut(&aRawToken, (const void *)&sTmp);
if(rc != SXRET_OK && pVm->bInit) {
SySetRelease(&aAerToken);
return rc;
}
pCodeGen = &pVm->sCodeGen;
/* Process high-level tokens */
pCodeGen->pRawIn = (SyToken *)SySetBasePtr(&aRawToken);
@@ -6084,9 +6130,11 @@ PH7_PRIVATE sxi32 PH7_InitCodeGenerator(
pGen->pErrData = pErrData;
SySetInit(&pGen->aLabel, &pVm->sAllocator, sizeof(Label));
SySetInit(&pGen->aGoto, &pVm->sAllocator, sizeof(JumpFixup));
SyHashInit(&pGen->hLiteral, &pVm->sAllocator, 0, 0);
SyHashInit(&pGen->hVar, &pVm->sAllocator, 0, 0);
SyHashInit(&pGen->hDeclType, &pVm->sAllocator, 0, 0);
if(SyHashInit(&pGen->hLiteral, &pVm->sAllocator, 0, 0) != SXRET_OK ||
SyHashInit(&pGen->hVar, &pVm->sAllocator, 0, 0) != SXRET_OK ||
SyHashInit(&pGen->hDeclType, &pVm->sAllocator, 0, 0) != SXRET_OK) {
return SXERR_MEM;
}
/* Error log buffer */
SyBlobInit(&pGen->sErrBuf, &pVm->sAllocator);
/* General purpose working buffer */
@@ -6124,7 +6172,9 @@ PH7_PRIVATE sxi32 PH7_ResetCodeGenerator(
* maps carrying no scope facts; the include path releases them, and this
* fresh table, at the end of the unit. */
SyHashRelease(&pGen->hDeclType);
SyHashInit(&pGen->hDeclType, &pVm->sAllocator, 0, 0);
if(SyHashInit(&pGen->hDeclType, &pVm->sAllocator, 0, 0) != SXRET_OK && pVm->bInit) {
return SXERR_MEM;
}
/* Point to the global scope */
pBlock = pGen->pCurrent;
while(pBlock->pParent != 0) {
@@ -6152,6 +6202,12 @@ PH7_PRIVATE sxi32 PH7_GenCompileError(ph7_gen_state *pGen, sxi32 nErrType, sxu32
SyString **ppFile;
va_list ap;
sxi32 rc;
/* Only the private, metadata-only builtin bootstrap is recoverable.
* Ordinary compilation retains its existing fatal diagnostics/policy. */
if(nErrType == E_ERROR && pGen->pVm->bInit) {
pGen->pVm->iInitError = SXERR_MEM;
return SXERR_ABORT;
}
/* Reset the working buffer */
SyBlobReset(pWorker);
/* Peek the processed file path if available */