use of org.develnext.jphp.core.compiler.jvm.misc.LocalVariable in project jphp by jphp-compiler.
the class ExpressionStmtCompiler method writeVariableAssign.
void writeVariableAssign(VariableExprToken variable, StackItem R, AssignExprToken operator, boolean returnValue) {
LocalVariable local = method.getLocalVariable(variable.getName());
checkAssignableVar(variable);
Memory value = R.getMemory();
writeLineNumber(variable);
if (local.isReference()) {
String name = "assign";
if (operator.isAsReference())
name = "assignRef";
if (!R.isKnown()) {
stackPush(R);
writePopBoxing();
writePopImmutable();
writePushVariable(variable);
writeSysStaticCall(Memory.class, name + "Right", Memory.class, stackPeek().type.toClass(), Memory.class);
} else {
writePushVariable(variable);
Memory tmp = tryWritePush(R);
if (tmp != null) {
value = tmp;
writePushMemory(value);
}
writePopBoxing();
writePopImmutable();
writeSysDynamicCall(Memory.class, name, Memory.class, stackPeek().type.toClass());
}
if (!returnValue)
writePopAll(1);
} else {
Memory result = tryWritePush(R);
if (result != null) {
writePushMemory(result);
value = result;
}
writePopBoxing();
writeVarStore(local, returnValue, true);
}
/*if (!methodStatement.variable(variable).isPassed())
local.setValue(value);
if (methodStatement.isDynamicLocal()) */
local.setValue(null);
}
use of org.develnext.jphp.core.compiler.jvm.misc.LocalVariable in project jphp by jphp-compiler.
the class TryCatchCompiler method write.
@Override
public void write(TryStmtToken token) {
if (token.getBody() == null || token.getBody().getInstructions().isEmpty()) {
if (token.getFinally() != null)
expr.write(BodyStmtToken.class, token.getFinally());
return;
}
expr.writeDefineVariables(token.getLocal());
LabelNode tryStart = expr.writeLabel(node, token.getMeta().getStartLine());
LabelNode tryEnd = new LabelNode();
LabelNode catchStart = new LabelNode();
LabelNode catchEnd = new LabelNode();
LabelNode returnLabel = new LabelNode();
method.node.tryCatchBlocks.add(0, new TryCatchBlockNode(tryStart, tryEnd, catchStart, Type.getInternalName(BaseBaseException.class)));
if (token.getFinally() != null) {
method.getTryStack().push(new MethodStmtCompiler.TryCatchItem(token, returnLabel));
}
expr.write(BodyStmtToken.class, token.getBody());
if (token.getFinally() != null) {
method.getTryStack().pop();
}
add(tryEnd);
add(new JumpInsnNode(GOTO, catchEnd));
add(catchStart);
LocalVariable exception = method.addLocalVariable("~catch~" + method.nextStatementIndex(BaseException.class), catchStart, BaseBaseException.class);
exception.setEndLabel(catchEnd);
expr.makeVarStore(exception);
LabelNode nextCatch = null;
int i = 0, size = token.getCatches().size();
LocalVariable local = null;
LabelNode catchFail = new LabelNode();
for (CatchStmtToken _catch : token.getCatches()) {
if (nextCatch != null) {
add(nextCatch);
}
if (i == size - 1) {
nextCatch = catchFail;
} else {
nextCatch = new LabelNode();
}
local = method.getLocalVariable(_catch.getVariable().getName());
expr.writePushEnv();
expr.writeVarLoad(exception);
expr.writePushConstString(_catch.getException().toName());
expr.writePushConstString(_catch.getException().toName().toLowerCase());
expr.writeSysDynamicCall(Environment.class, "__throwCatch", Memory.class, BaseBaseException.class, String.class, String.class);
expr.writeVarAssign(local, _catch.getVariable(), true, false);
expr.writePopBoolean();
add(new JumpInsnNode(IFEQ, nextCatch));
expr.stackPop();
expr.write(BodyStmtToken.class, _catch.getBody());
add(new JumpInsnNode(GOTO, catchEnd));
i++;
}
add(catchFail);
if (token.getFinally() != null) {
expr.write(BodyStmtToken.class, token.getFinally());
}
/*if (method.getTryStack().empty()) {
expr.writePushEnv();
expr.writeVarLoad(exception);
expr.writeSysDynamicCall(Environment.class, "__throwFailedCatch", void.class, BaseException.class);
} else {*/
expr.makeVarLoad(exception);
add(new InsnNode(ATHROW));
//}
add(catchEnd);
if (token.getFinally() != null) {
LabelNode skip = new LabelNode();
add(new JumpInsnNode(GOTO, skip));
// finally for return
add(returnLabel);
expr.write(BodyStmtToken.class, token.getFinally());
if (method.getTryStack().empty()) {
// all finally blocks are done
LocalVariable retVar = method.getOrAddLocalVariable("~result~", null, Memory.class);
expr.writeVarLoad(retVar);
add(new InsnNode(ARETURN));
expr.stackPop();
} else {
// goto next finally block
add(new JumpInsnNode(GOTO, method.getTryStack().peek().getReturnLabel()));
}
add(skip);
// other finally
expr.write(BodyStmtToken.class, token.getFinally());
}
expr.writeUndefineVariables(token.getLocal());
method.prevStatementIndex(BaseBaseException.class);
}
use of org.develnext.jphp.core.compiler.jvm.misc.LocalVariable in project jphp by jphp-compiler.
the class ClosureValueCompiler method writePushUses.
protected void writePushUses(Collection<ArgumentStmtToken> parameters) {
if (parameters.isEmpty()) {
add(new InsnNode(ACONST_NULL));
expr.stackPush(Memory.Type.REFERENCE);
return;
}
expr.writePushSmallInt(parameters.size());
add(new TypeInsnNode(ANEWARRAY, Type.getInternalName(Memory.class)));
expr.stackPop();
expr.stackPush(Memory.Type.REFERENCE);
int i = 0;
for (ArgumentStmtToken param : parameters) {
expr.writePushDup();
expr.writePushSmallInt(i);
LocalVariable local = method.getLocalVariable(param.getName().getName());
if (local == null)
expr.writePushNull();
else
expr.writeVarLoad(local);
if (!param.isReference())
expr.writePopBoxing(true);
add(new InsnNode(AASTORE));
expr.stackPop();
expr.stackPop();
expr.stackPop();
i++;
}
}
use of org.develnext.jphp.core.compiler.jvm.misc.LocalVariable in project jphp by jphp-compiler.
the class ExpressionStmtCompiler method writePushEnv.
public void writePushEnv() {
method.entity.setUsesStackTrace(true);
LocalVariable variable = method.getLocalVariable("~env");
if (variable == null) {
if (!methodStatement.isStatic())
writePushEnvFromSelf();
else
throw new RuntimeException("Cannot find `~env` variable");
return;
}
stackPush(Memory.Type.REFERENCE);
makeVarLoad(variable);
}
use of org.develnext.jphp.core.compiler.jvm.misc.LocalVariable in project jphp by jphp-compiler.
the class MethodStmtCompiler method addLocalVariable.
public LocalVariable addLocalVariable(String variable, LabelNode label, Class clazz) {
LocalVariable result;
localVariables.put(variable, result = new LocalVariable(variable, localVariables.size(), label, clazz));
return result;
}
Aggregations