From ece036b21498c7f0b70ee01599676e803dc36b52 Mon Sep 17 00:00:00 2001 From: belliash Date: Tue, 14 Aug 2018 13:45:27 +0200 Subject: [PATCH] Rework the entry point * some functions were not used and have been removed * some parameters seemed to be useless and have been removed * code cleanup --- engine/api.c | 45 ++++----------------------------------------- engine/compiler.c | 40 +++++++++++++++++----------------------- engine/vm.c | 8 ++++---- include/compiler.h | 2 +- include/ph7.h | 7 +++---- sapi/cli/main.c | 3 +-- 6 files changed, 30 insertions(+), 75 deletions(-) diff --git a/engine/api.c b/engine/api.c index 37d55ee..59e0b92 100644 --- a/engine/api.c +++ b/engine/api.c @@ -656,17 +656,12 @@ static sxi32 ProcessSourceFile( ph7 *pEngine, /* Running PH7 engine */ ph7_vm **ppVm, /* OUT: A pointer to the virtual machine */ 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 */ ) { ph7_vm *pVm = *ppVm; int iFileDir, rc; char *pFileDir, *fFilePath[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 */ ph7_vm_config(pVm, PH7_VM_CONFIG_IMPORT_PATH, "./"); if(zFilePath && SyRealPath(zFilePath, fFilePath) == PH7_OK) { @@ -680,7 +675,7 @@ static sxi32 ProcessSourceFile( PH7_VmPushFilePath(pVm, pFilePath, -1, TRUE, 0); } /* Compile the script */ - PH7_CompileAerScript(pVm, &(*pScript), iFlags); + PH7_CompileAerScript(pVm, &(*pScript), PH7_AERSCRIPT_CODE); if(pVm->sCodeGen.nErr > 0 || pVm == 0) { sxu32 nErr = pVm->sCodeGen.nErr; /* 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 /* Compile the script */ - rc = ProcessSourceFile(&(*pEngine), ppOutVm, &sScript, 0, 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); + 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 */ @@ -784,7 +747,7 @@ int ph7_compile_v2(ph7 *pEngine, const char *zSource, int nLen, ph7_vm **ppOutVm * [CAPIREF: ph7_compile_file()] * 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; int rc; rc = PH7_OK; /* cc warning */ @@ -819,7 +782,7 @@ int ph7_compile_file(ph7 *pEngine, const char *zFilePath, ph7_vm **ppOutVm, int } else { /* Compile the file */ 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 */ if(pVfs->xUnmap) { pVfs->xUnmap(pMapView, nSize); diff --git a/engine/compiler.c b/engine/compiler.c index de1dd21..1e96865 100644 --- a/engine/compiler.c +++ b/engine/compiler.c @@ -5353,7 +5353,7 @@ static sxi32 PH7_GenStateCompileChunk( static sxi32 PH7_CompileScript( ph7_gen_state *pGen, /* Code generator state */ 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 */ sxi32 rc; @@ -5368,7 +5368,7 @@ static sxi32 PH7_CompileScript( /* Point to the head and tail of the token stream. */ pGen->pIn = (SyToken *)SySetBasePtr(pTokenSet); pGen->pEnd = &pGen->pIn[SySetUsed(pTokenSet)]; - if(is_expr) { + if(bExpr) { rc = SXERR_EMPTY; if(pGen->pIn < pGen->pEnd) { /* 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); return SXRET_OK; } - /* Compile the Aer chunk */ + /* Compile the Aer global scope */ rc = PH7_GenStateCompileChunk(pGen, 0); /* Fix exceptions jumps */ PH7_GenStateFixJumps(pGen->pCurrent, PH7_OP_THROW, PH7_VmInstrLength(pGen->pVm)); @@ -5400,7 +5400,6 @@ PH7_PRIVATE sxi32 PH7_CompileAerScript( ph7_value *pRawObj; sxu32 nObjIdx; sxi32 nRawObj; - int is_expr; sxi32 rc; if(pScript->nByte < 1) { /* Nothing to compile */ @@ -5410,39 +5409,34 @@ PH7_PRIVATE sxi32 PH7_CompileAerScript( SySetInit(&aRawToken, &pVm->sAllocator, sizeof(SyToken)); SySetInit(&aAerToken, &pVm->sAllocator, sizeof(SyToken)); SySetAlloc(&aAerToken, 0xc0); - is_expr = 0; SyToken sTmp; sTmp.nLine = 1; sTmp.pUserData = 0; SyStringDupPtr(&sTmp.sData, pScript); SySetPut(&aRawToken, (const void *)&sTmp); - if(iFlags & PH7_PHP_EXPR) { - /* A simple Aer expression */ - is_expr = 1; - } pCodeGen = &pVm->sCodeGen; /* Process high-level tokens */ pCodeGen->pRawIn = (SyToken *)SySetBasePtr(&aRawToken); pCodeGen->pRawEnd = &pCodeGen->pRawIn[SySetUsed(&aRawToken)]; rc = PH7_OK; - if(is_expr) { + if(iFlags & PH7_AERSCRIPT_EXPR) { /* Compile the expression */ rc = PH7_CompileScript(pCodeGen, &aAerToken, TRUE); - goto cleanup; - } - nObjIdx = 0; - /* Start the compilation process */ - for(;;) { - /* Compile Aer block of code */ - if(pCodeGen->pRawIn >= pCodeGen->pRawEnd) { - break; /* No more tokens to process */ - } - rc = PH7_CompileScript(pCodeGen, &aAerToken, FALSE); - if(rc == SXERR_ABORT) { - break; + } else { + nObjIdx = 0; + /* Start the compilation process */ + for(;;) { + /* Compile Aer block of code */ + if(pCodeGen->pRawIn >= pCodeGen->pRawEnd) { + break; /* No more tokens to process */ + } + /* Compile the global scope */ + rc = PH7_CompileScript(pCodeGen, &aAerToken, FALSE); + if(rc == SXERR_ABORT) { + break; + } } } -cleanup: SySetRelease(&aRawToken); SySetRelease(&aAerToken); return rc; diff --git a/engine/vm.c b/engine/vm.c index 6175c5d..efb2256 100644 --- a/engine/vm.c +++ b/engine/vm.c @@ -1281,7 +1281,7 @@ PH7_PRIVATE sxi32 PH7_VmInit( pVm->nMagic = PH7_VM_INIT; SyStringInitFromBuf(&sBuiltin, PH7_BUILTIN_LIB, sizeof(PH7_BUILTIN_LIB) - 1); /* 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 */ PH7_ResetCodeGenerator(&(*pVm), pEngine->xConf.xErr, pEngine->xConf.pErrData); return SXRET_OK; @@ -9004,7 +9004,7 @@ static int vm_builtin_assert(ph7_context *pCtx, int nArg, ph7_value **apArg) { SyString sChunk; SyStringInitFromBuf(&sChunk, SyBlobData(&pAssert->sBlob), SyBlobLength(&pAssert->sBlob)); 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 */ iResult = ph7_value_to_bool(pCtx->pRet); } else { @@ -10513,7 +10513,7 @@ static int vm_builtin_eval(ph7_context *pCtx, int nArg, ph7_value **apArg) { return SXRET_OK; } /* Eval the chunk */ - VmEvalChunk(pCtx->pVm, &(*pCtx), &sChunk, PH7_PHP_CODE, FALSE); + VmEvalChunk(pCtx->pVm, &(*pCtx), &sChunk, PH7_AERSCRIPT_CODE, FALSE); return SXRET_OK; } /* @@ -10636,7 +10636,7 @@ static sxi32 VmExecIncludedFile( SyString sScript; /* Compile and execute the script */ 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 */ diff --git a/include/compiler.h b/include/compiler.h index 6465a0d..d707ffa 100644 --- a/include/compiler.h +++ b/include/compiler.h @@ -148,7 +148,7 @@ PH7_PRIVATE ProcNodeConstruct PH7_GetNodeHandler(sxu32 nNodeType); static ProcLangConstruct PH7_GenStateGetStatementHandler(sxu32 nKeywordID, SyToken *pLookahead); static int PH7_GenStateIsLangConstruct(sxu32 nKeyword); 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_ResetCodeGenerator(ph7_vm *pVm, ProcConsumer xErr, void *pErrData); PH7_PRIVATE sxi32 PH7_GenCompileError(ph7_gen_state *pGen, sxi32 nErrType, sxu32 nLine, const char *zFormat, ...); diff --git a/include/ph7.h b/include/ph7.h index fff444e..c8e7a8c 100644 --- a/include/ph7.h +++ b/include/ph7.h @@ -447,8 +447,8 @@ typedef sxi64 ph7_int64; * processing the input. * Refer to the official documentation for additional information. */ -#define PH7_PHP_CODE 0x01 /* PHP Block of Code */ -#define PH7_PHP_EXPR 0x02 /* PHP Simple Expression */ +#define PH7_AERSCRIPT_CODE 0x01 /* PHP Block of Code */ +#define PH7_AERSCRIPT_EXPR 0x02 /* PHP Simple Expression */ /* * 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); /* Compile Interfaces */ 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, int iFlags); +PH7_APIEXPORT int ph7_compile_file(ph7 *pEngine, const char *zFilePath, ph7_vm **ppOutVm); /* Virtual Machine Handling Interfaces */ PH7_APIEXPORT int ph7_vm_config(ph7_vm *pVm, int iConfigOp, ...); PH7_APIEXPORT int ph7_vm_exec(ph7_vm *pVm, int *pExitStatus); diff --git a/sapi/cli/main.c b/sapi/cli/main.c index 45bf05d..3522094 100644 --- a/sapi/cli/main.c +++ b/sapi/cli/main.c @@ -186,8 +186,7 @@ int main(int argc, char **argv) { rc = ph7_compile_file( pEngine, /* PH7 Engine */ argv[n], /* Path to the PHP file to compile */ - &pVm, /* OUT: Compiled PHP program */ - 0 /* IN: Compile flags */ + &pVm /* OUT: Compiled PHP program */ ); if(rc != PH7_OK) { /* Compile error */ if(rc == PH7_IO_ERR) {