Separate VM instruction for variable declaration (OP_DECLARE).
The build was successful. Details

This commit is contained in:
Rafal Kupiec 2019-04-15 19:18:29 +02:00
parent 1b248a17e7
commit 517dffcbc1
Signed by: belliash
GPG Key ID: 4E829243E0CFE6B4
3 changed files with 38 additions and 1 deletions

View File

@ -2557,7 +2557,7 @@ static sxi32 PH7_CompileVar(ph7_gen_state *pGen) {
}
void *p3 = (void *) zDup;
/* Emit OP_LOAD instruction */
PH7_VmEmitInstr(pGen->pVm, pGen->pIn->nLine, PH7_OP_LOAD, 0, nType, p3, 0);
PH7_VmEmitInstr(pGen->pVm, pGen->pIn->nLine, PH7_OP_DECLARE, 0, nType, p3, 0);
/* Check if we have an expression to compile */
if(pGen->pIn < pGen->pEnd && (pGen->pIn[2].nType & PH7_TK_EQUAL)) {
/* Compile the expression */

View File

@ -2395,6 +2395,39 @@ static sxi32 VmByteCodeExec(
break;
}
/*
* DECLARE: * P2 P3
*
* Create a variable where it's name is taken from the top of the stack or
* from the P3 operand. It takes a variable type from P2 operand.
*/
case PH7_OP_DECLARE: {
ph7_value *pObj;
SyString sName;
SyStringInitFromBuf(&sName, pInstr->p3, SyStrlen((const char *)pInstr->p3));
/* Reserve a room for the target object */
pTos++;
/* Create a new variable */
pObj = VmCreateMemObj(&(*pVm), &sName, FALSE);
if(!pObj) {
PH7_VmThrowError(&(*pVm), PH7_CTX_ERR,
"Redeclaration of $%z variable", &sName);
}
if(pInstr->iP2 & MEMOBJ_MIXED && (pInstr->iP2 & MEMOBJ_HASHMAP) == 0) {
pObj->iFlags = MEMOBJ_MIXED | MEMOBJ_VOID;
} else {
if(pInstr->iP2 & MEMOBJ_HASHMAP) {
ph7_hashmap *pMap;
pMap = PH7_NewHashmap(&(*pVm), 0, 0);
if(pMap == 0) {
PH7_VmMemoryError(&(*pVm));
}
pObj->x.pOther = pMap;
}
MemObjSetType(pObj, pInstr->iP2);
}
pTos->nIdx = SXU32_HIGH; /* Mark as constant */
break;
} /*
* LOADC P1 P2 *
*
* Load a constant [i.e: PHP_EOL,PHP_OS,__TIME__,...] indexed at P2 in the constant pool.
@ -5369,6 +5402,9 @@ static const char *VmInstrToString(sxi32 nOp) {
case PH7_OP_HALT:
zOp = "HALT";
break;
case PH7_OP_DECLARE:
zOp = "DECLARE";
break;
case PH7_OP_LOAD:
zOp = "LOAD";
break;

View File

@ -1389,6 +1389,7 @@ enum iErrCode {
enum ph7_vm_op {
PH7_OP_DONE = 1, /* Done */
PH7_OP_HALT, /* Halt */
PH7_OP_DECLARE, /* Declare a variable */
PH7_OP_LOAD, /* Load memory object */
PH7_OP_LOADC, /* Load constant */
PH7_OP_LOAD_IDX, /* Load array entry */