Compiler rework merge #43

Merged
belliash merged 55 commits from compiler_rework into master 2018-08-16 21:28:49 +02:00
6 changed files with 30 additions and 75 deletions
Showing only changes of commit ece036b214 - Show all commits

View File

@ -656,17 +656,12 @@ static sxi32 ProcessSourceFile(
ph7 *pEngine, /* Running PH7 engine */ ph7 *pEngine, /* Running PH7 engine */
ph7_vm **ppVm, /* OUT: A pointer to the virtual machine */ ph7_vm **ppVm, /* OUT: A pointer to the virtual machine */
SyString *pScript, /* Raw PHP script to compile */ SyString *pScript, /* Raw PHP script to compile */
sxi32 iFlags, /* Compile-time flags */
const char *zFilePath /* File path if script come from a file. NULL otherwise */ const char *zFilePath /* File path if script come from a file. NULL otherwise */
) { ) {
ph7_vm *pVm = *ppVm; ph7_vm *pVm = *ppVm;
int iFileDir, rc; int iFileDir, rc;
char *pFileDir, *fFilePath[PATH_MAX + 1]; char *pFileDir, *fFilePath[PATH_MAX + 1];
char *pFilePath[PATH_MAX + 1]; char *pFilePath[PATH_MAX + 1];
if(iFlags < 0) {
/* Default compile-time flags */
iFlags = 0;
}
/* Install local import path which is the current directory */ /* Install local import path which is the current directory */
ph7_vm_config(pVm, PH7_VM_CONFIG_IMPORT_PATH, "./"); ph7_vm_config(pVm, PH7_VM_CONFIG_IMPORT_PATH, "./");
if(zFilePath && SyRealPath(zFilePath, fFilePath) == PH7_OK) { if(zFilePath && SyRealPath(zFilePath, fFilePath) == PH7_OK) {
@ -680,7 +675,7 @@ static sxi32 ProcessSourceFile(
PH7_VmPushFilePath(pVm, pFilePath, -1, TRUE, 0); PH7_VmPushFilePath(pVm, pFilePath, -1, TRUE, 0);
} }
/* Compile the script */ /* Compile the script */
PH7_CompileAerScript(pVm, &(*pScript), iFlags); PH7_CompileAerScript(pVm, &(*pScript), PH7_AERSCRIPT_CODE);
if(pVm->sCodeGen.nErr > 0 || pVm == 0) { if(pVm->sCodeGen.nErr > 0 || pVm == 0) {
sxu32 nErr = pVm->sCodeGen.nErr; sxu32 nErr = pVm->sCodeGen.nErr;
/* Compilation error or null ppVm pointer,release this VM */ /* Compilation error or null ppVm pointer,release this VM */
@ -740,39 +735,7 @@ int ph7_compile(ph7 *pEngine, const char *zSource, int nLen, ph7_vm **ppOutVm) {
} }
#endif #endif
/* Compile the script */ /* Compile the script */
rc = ProcessSourceFile(&(*pEngine), ppOutVm, &sScript, 0, 0); rc = ProcessSourceFile(&(*pEngine), ppOutVm, &sScript, 0);
#if defined(PH7_ENABLE_THREADS)
/* Leave engine mutex */
SyMutexLeave(sMPGlobal.pMutexMethods, pEngine->pMutex); /* NO-OP if sMPGlobal.nThreadingLevel != PH7_THREAD_LEVEL_MULTI */
#endif
/* Compilation result */
return rc;
}
/*
* [CAPIREF: ph7_compile_v2()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
int ph7_compile_v2(ph7 *pEngine, const char *zSource, int nLen, ph7_vm **ppOutVm, int iFlags) {
SyString sScript;
int rc;
if(PH7_ENGINE_MISUSE(pEngine) || zSource == 0) {
return PH7_CORRUPT;
}
if(nLen < 0) {
/* Compute input length automatically */
nLen = (int)SyStrlen(zSource);
}
SyStringInitFromBuf(&sScript, zSource, nLen);
#if defined(PH7_ENABLE_THREADS)
/* Acquire engine mutex */
SyMutexEnter(sMPGlobal.pMutexMethods, pEngine->pMutex); /* NO-OP if sMPGlobal.nThreadingLevel != PH7_THREAD_LEVEL_MULTI */
if(sMPGlobal.nThreadingLevel > PH7_THREAD_LEVEL_SINGLE &&
PH7_THRD_ENGINE_RELEASE(pEngine)) {
return PH7_ABORT; /* Another thread have released this instance */
}
#endif
/* Compile the script */
rc = ProcessSourceFile(&(*pEngine), ppOutVm, &sScript, iFlags, 0);
#if defined(PH7_ENABLE_THREADS) #if defined(PH7_ENABLE_THREADS)
/* Leave engine mutex */ /* Leave engine mutex */
SyMutexLeave(sMPGlobal.pMutexMethods, pEngine->pMutex); /* NO-OP if sMPGlobal.nThreadingLevel != PH7_THREAD_LEVEL_MULTI */ SyMutexLeave(sMPGlobal.pMutexMethods, pEngine->pMutex); /* NO-OP if sMPGlobal.nThreadingLevel != PH7_THREAD_LEVEL_MULTI */
@ -784,7 +747,7 @@ int ph7_compile_v2(ph7 *pEngine, const char *zSource, int nLen, ph7_vm **ppOutVm
* [CAPIREF: ph7_compile_file()] * [CAPIREF: ph7_compile_file()]
* Please refer to the official documentation for function purpose and expected parameters. * Please refer to the official documentation for function purpose and expected parameters.
*/ */
int ph7_compile_file(ph7 *pEngine, const char *zFilePath, ph7_vm **ppOutVm, int iFlags) { int ph7_compile_file(ph7 *pEngine, const char *zFilePath, ph7_vm **ppOutVm) {
const ph7_vfs *pVfs; const ph7_vfs *pVfs;
int rc; int rc;
rc = PH7_OK; /* cc warning */ rc = PH7_OK; /* cc warning */
@ -819,7 +782,7 @@ int ph7_compile_file(ph7 *pEngine, const char *zFilePath, ph7_vm **ppOutVm, int
} else { } else {
/* Compile the file */ /* Compile the file */
SyStringInitFromBuf(&sScript, pMapView, nSize); SyStringInitFromBuf(&sScript, pMapView, nSize);
rc = ProcessSourceFile(&(*pEngine), ppOutVm, &sScript, iFlags, zFilePath); rc = ProcessSourceFile(&(*pEngine), ppOutVm, &sScript, zFilePath);
/* Release the memory view of the whole file */ /* Release the memory view of the whole file */
if(pVfs->xUnmap) { if(pVfs->xUnmap) {
pVfs->xUnmap(pMapView, nSize); pVfs->xUnmap(pMapView, nSize);

View File

@ -5353,7 +5353,7 @@ static sxi32 PH7_GenStateCompileChunk(
static sxi32 PH7_CompileScript( static sxi32 PH7_CompileScript(
ph7_gen_state *pGen, /* Code generator state */ ph7_gen_state *pGen, /* Code generator state */
SySet *pTokenSet, /* Token set */ SySet *pTokenSet, /* Token set */
int is_expr /* TRUE if we are dealing with a simple expression */ sxbool bExpr /* TRUE if we are dealing with a simple expression */
) { ) {
SyToken *pScript = pGen->pRawIn; /* Script to compile */ SyToken *pScript = pGen->pRawIn; /* Script to compile */
sxi32 rc; sxi32 rc;
@ -5368,7 +5368,7 @@ static sxi32 PH7_CompileScript(
/* Point to the head and tail of the token stream. */ /* Point to the head and tail of the token stream. */
pGen->pIn = (SyToken *)SySetBasePtr(pTokenSet); pGen->pIn = (SyToken *)SySetBasePtr(pTokenSet);
pGen->pEnd = &pGen->pIn[SySetUsed(pTokenSet)]; pGen->pEnd = &pGen->pIn[SySetUsed(pTokenSet)];
if(is_expr) { if(bExpr) {
rc = SXERR_EMPTY; rc = SXERR_EMPTY;
if(pGen->pIn < pGen->pEnd) { if(pGen->pIn < pGen->pEnd) {
/* A simple expression,compile it */ /* A simple expression,compile it */
@ -5378,7 +5378,7 @@ static sxi32 PH7_CompileScript(
PH7_VmEmitInstr(pGen->pVm, PH7_OP_DONE, (rc != SXERR_EMPTY ? 1 : 0), 0, 0, 0); PH7_VmEmitInstr(pGen->pVm, PH7_OP_DONE, (rc != SXERR_EMPTY ? 1 : 0), 0, 0, 0);
return SXRET_OK; return SXRET_OK;
} }
/* Compile the Aer chunk */ /* Compile the Aer global scope */
rc = PH7_GenStateCompileChunk(pGen, 0); rc = PH7_GenStateCompileChunk(pGen, 0);
/* Fix exceptions jumps */ /* Fix exceptions jumps */
PH7_GenStateFixJumps(pGen->pCurrent, PH7_OP_THROW, PH7_VmInstrLength(pGen->pVm)); PH7_GenStateFixJumps(pGen->pCurrent, PH7_OP_THROW, PH7_VmInstrLength(pGen->pVm));
@ -5400,7 +5400,6 @@ PH7_PRIVATE sxi32 PH7_CompileAerScript(
ph7_value *pRawObj; ph7_value *pRawObj;
sxu32 nObjIdx; sxu32 nObjIdx;
sxi32 nRawObj; sxi32 nRawObj;
int is_expr;
sxi32 rc; sxi32 rc;
if(pScript->nByte < 1) { if(pScript->nByte < 1) {
/* Nothing to compile */ /* Nothing to compile */
@ -5410,39 +5409,34 @@ PH7_PRIVATE sxi32 PH7_CompileAerScript(
SySetInit(&aRawToken, &pVm->sAllocator, sizeof(SyToken)); SySetInit(&aRawToken, &pVm->sAllocator, sizeof(SyToken));
SySetInit(&aAerToken, &pVm->sAllocator, sizeof(SyToken)); SySetInit(&aAerToken, &pVm->sAllocator, sizeof(SyToken));
SySetAlloc(&aAerToken, 0xc0); SySetAlloc(&aAerToken, 0xc0);
is_expr = 0;
SyToken sTmp; SyToken sTmp;
sTmp.nLine = 1; sTmp.nLine = 1;
sTmp.pUserData = 0; sTmp.pUserData = 0;
SyStringDupPtr(&sTmp.sData, pScript); SyStringDupPtr(&sTmp.sData, pScript);
SySetPut(&aRawToken, (const void *)&sTmp); SySetPut(&aRawToken, (const void *)&sTmp);
if(iFlags & PH7_PHP_EXPR) {
/* A simple Aer expression */
is_expr = 1;
}
pCodeGen = &pVm->sCodeGen; pCodeGen = &pVm->sCodeGen;
/* Process high-level tokens */ /* Process high-level tokens */
pCodeGen->pRawIn = (SyToken *)SySetBasePtr(&aRawToken); pCodeGen->pRawIn = (SyToken *)SySetBasePtr(&aRawToken);
pCodeGen->pRawEnd = &pCodeGen->pRawIn[SySetUsed(&aRawToken)]; pCodeGen->pRawEnd = &pCodeGen->pRawIn[SySetUsed(&aRawToken)];
rc = PH7_OK; rc = PH7_OK;
if(is_expr) { if(iFlags & PH7_AERSCRIPT_EXPR) {
/* Compile the expression */ /* Compile the expression */
rc = PH7_CompileScript(pCodeGen, &aAerToken, TRUE); rc = PH7_CompileScript(pCodeGen, &aAerToken, TRUE);
goto cleanup; } else {
} nObjIdx = 0;
nObjIdx = 0; /* Start the compilation process */
/* Start the compilation process */ for(;;) {
for(;;) { /* Compile Aer block of code */
/* Compile Aer block of code */ if(pCodeGen->pRawIn >= pCodeGen->pRawEnd) {
if(pCodeGen->pRawIn >= pCodeGen->pRawEnd) { break; /* No more tokens to process */
break; /* No more tokens to process */ }
} /* Compile the global scope */
rc = PH7_CompileScript(pCodeGen, &aAerToken, FALSE); rc = PH7_CompileScript(pCodeGen, &aAerToken, FALSE);
if(rc == SXERR_ABORT) { if(rc == SXERR_ABORT) {
break; break;
}
} }
} }
cleanup:
SySetRelease(&aRawToken); SySetRelease(&aRawToken);
SySetRelease(&aAerToken); SySetRelease(&aAerToken);
return rc; return rc;

View File

@ -1281,7 +1281,7 @@ PH7_PRIVATE sxi32 PH7_VmInit(
pVm->nMagic = PH7_VM_INIT; pVm->nMagic = PH7_VM_INIT;
SyStringInitFromBuf(&sBuiltin, PH7_BUILTIN_LIB, sizeof(PH7_BUILTIN_LIB) - 1); SyStringInitFromBuf(&sBuiltin, PH7_BUILTIN_LIB, sizeof(PH7_BUILTIN_LIB) - 1);
/* Compile the built-in library */ /* Compile the built-in library */
VmEvalChunk(&(*pVm), 0, &sBuiltin, PH7_PHP_CODE, FALSE); VmEvalChunk(&(*pVm), 0, &sBuiltin, PH7_AERSCRIPT_CODE, FALSE);
/* Reset the code generator */ /* Reset the code generator */
PH7_ResetCodeGenerator(&(*pVm), pEngine->xConf.xErr, pEngine->xConf.pErrData); PH7_ResetCodeGenerator(&(*pVm), pEngine->xConf.xErr, pEngine->xConf.pErrData);
return SXRET_OK; return SXRET_OK;
@ -9004,7 +9004,7 @@ static int vm_builtin_assert(ph7_context *pCtx, int nArg, ph7_value **apArg) {
SyString sChunk; SyString sChunk;
SyStringInitFromBuf(&sChunk, SyBlobData(&pAssert->sBlob), SyBlobLength(&pAssert->sBlob)); SyStringInitFromBuf(&sChunk, SyBlobData(&pAssert->sBlob), SyBlobLength(&pAssert->sBlob));
if(sChunk.nByte > 0) { if(sChunk.nByte > 0) {
VmEvalChunk(pVm, pCtx, &sChunk, PH7_PHP_CODE | PH7_PHP_EXPR, FALSE); VmEvalChunk(pVm, pCtx, &sChunk, PH7_AERSCRIPT_CODE | PH7_AERSCRIPT_EXPR, FALSE);
/* Extract evaluation result */ /* Extract evaluation result */
iResult = ph7_value_to_bool(pCtx->pRet); iResult = ph7_value_to_bool(pCtx->pRet);
} else { } else {
@ -10513,7 +10513,7 @@ static int vm_builtin_eval(ph7_context *pCtx, int nArg, ph7_value **apArg) {
return SXRET_OK; return SXRET_OK;
} }
/* Eval the chunk */ /* Eval the chunk */
VmEvalChunk(pCtx->pVm, &(*pCtx), &sChunk, PH7_PHP_CODE, FALSE); VmEvalChunk(pCtx->pVm, &(*pCtx), &sChunk, PH7_AERSCRIPT_CODE, FALSE);
return SXRET_OK; return SXRET_OK;
} }
/* /*
@ -10636,7 +10636,7 @@ static sxi32 VmExecIncludedFile(
SyString sScript; SyString sScript;
/* Compile and execute the script */ /* Compile and execute the script */
SyStringInitFromBuf(&sScript, SyBlobData(&sContents), SyBlobLength(&sContents)); SyStringInitFromBuf(&sScript, SyBlobData(&sContents), SyBlobLength(&sContents));
VmEvalChunk(pCtx->pVm, &(*pCtx), &sScript, PH7_PHP_CODE, TRUE); VmEvalChunk(pCtx->pVm, &(*pCtx), &sScript, PH7_AERSCRIPT_CODE, TRUE);
} }
} }
/* Pop from the set of included file */ /* Pop from the set of included file */

View File

@ -148,7 +148,7 @@ PH7_PRIVATE ProcNodeConstruct PH7_GetNodeHandler(sxu32 nNodeType);
static ProcLangConstruct PH7_GenStateGetStatementHandler(sxu32 nKeywordID, SyToken *pLookahead); static ProcLangConstruct PH7_GenStateGetStatementHandler(sxu32 nKeywordID, SyToken *pLookahead);
static int PH7_GenStateIsLangConstruct(sxu32 nKeyword); static int PH7_GenStateIsLangConstruct(sxu32 nKeyword);
static sxi32 PH7_GenStateCompileChunk(ph7_gen_state *pGen, sxi32 iFlags); static sxi32 PH7_GenStateCompileChunk(ph7_gen_state *pGen, sxi32 iFlags);
static sxi32 PH7_CompileScript(ph7_gen_state *pGen, SySet *pTokenSet, int is_expr); static sxi32 PH7_CompileScript(ph7_gen_state *pGen, SySet *pTokenSet, sxbool bExpr);
PH7_PRIVATE sxi32 PH7_InitCodeGenerator(ph7_vm *pVm, ProcConsumer xErr, void *pErrData); PH7_PRIVATE sxi32 PH7_InitCodeGenerator(ph7_vm *pVm, ProcConsumer xErr, void *pErrData);
PH7_PRIVATE sxi32 PH7_ResetCodeGenerator(ph7_vm *pVm, ProcConsumer xErr, void *pErrData); PH7_PRIVATE sxi32 PH7_ResetCodeGenerator(ph7_vm *pVm, ProcConsumer xErr, void *pErrData);
PH7_PRIVATE sxi32 PH7_GenCompileError(ph7_gen_state *pGen, sxi32 nErrType, sxu32 nLine, const char *zFormat, ...); PH7_PRIVATE sxi32 PH7_GenCompileError(ph7_gen_state *pGen, sxi32 nErrType, sxu32 nLine, const char *zFormat, ...);

View File

@ -447,8 +447,8 @@ typedef sxi64 ph7_int64;
* processing the input. * processing the input.
* Refer to the official documentation for additional information. * Refer to the official documentation for additional information.
*/ */
#define PH7_PHP_CODE 0x01 /* PHP Block of Code */ #define PH7_AERSCRIPT_CODE 0x01 /* PHP Block of Code */
#define PH7_PHP_EXPR 0x02 /* PHP Simple Expression */ #define PH7_AERSCRIPT_EXPR 0x02 /* PHP Simple Expression */
/* /*
* Call Context Error Message Severity Level. * Call Context Error Message Severity Level.
* *
@ -607,8 +607,7 @@ PH7_APIEXPORT int ph7_config(ph7 *pEngine, int nConfigOp, ...);
PH7_APIEXPORT int ph7_release(ph7 *pEngine); PH7_APIEXPORT int ph7_release(ph7 *pEngine);
/* Compile Interfaces */ /* Compile Interfaces */
PH7_APIEXPORT int ph7_compile(ph7 *pEngine, const char *zSource, int nLen, ph7_vm **ppOutVm); PH7_APIEXPORT int ph7_compile(ph7 *pEngine, const char *zSource, int nLen, ph7_vm **ppOutVm);
PH7_APIEXPORT int ph7_compile_v2(ph7 *pEngine, const char *zSource, int nLen, ph7_vm **ppOutVm, int iFlags); PH7_APIEXPORT int ph7_compile_file(ph7 *pEngine, const char *zFilePath, ph7_vm **ppOutVm);
PH7_APIEXPORT int ph7_compile_file(ph7 *pEngine, const char *zFilePath, ph7_vm **ppOutVm, int iFlags);
/* Virtual Machine Handling Interfaces */ /* Virtual Machine Handling Interfaces */
PH7_APIEXPORT int ph7_vm_config(ph7_vm *pVm, int iConfigOp, ...); PH7_APIEXPORT int ph7_vm_config(ph7_vm *pVm, int iConfigOp, ...);
PH7_APIEXPORT int ph7_vm_exec(ph7_vm *pVm, int *pExitStatus); PH7_APIEXPORT int ph7_vm_exec(ph7_vm *pVm, int *pExitStatus);

View File

@ -186,8 +186,7 @@ int main(int argc, char **argv) {
rc = ph7_compile_file( rc = ph7_compile_file(
pEngine, /* PH7 Engine */ pEngine, /* PH7 Engine */
argv[n], /* Path to the PHP file to compile */ argv[n], /* Path to the PHP file to compile */
&pVm, /* OUT: Compiled PHP program */ &pVm /* OUT: Compiled PHP program */
0 /* IN: Compile flags */
); );
if(rc != PH7_OK) { /* Compile error */ if(rc != PH7_OK) { /* Compile error */
if(rc == PH7_IO_ERR) { if(rc == PH7_IO_ERR) {