use of org.wso2.ballerinalang.programfile.Instruction in project ballerina by ballerina-lang.
the class PackageInfoWriter method writeInstructions.
private static byte[] writeInstructions(Instruction[] instructions) throws IOException {
ByteArrayOutputStream byteAOS = new ByteArrayOutputStream();
DataOutputStream dataOutStream = new DataOutputStream(byteAOS);
for (Instruction instruction : instructions) {
dataOutStream.write(instruction.opcode);
for (Operand operand : instruction.ops) {
dataOutStream.writeInt(operand.value);
}
}
return byteAOS.toByteArray();
}
use of org.wso2.ballerinalang.programfile.Instruction in project ballerina by ballerina-lang.
the class CodeGenerator method processTimeoutBlock.
/* generate code for timeout block */
private void processTimeoutBlock(BLangForkJoin forkJoin, SymbolEnv forkJoinEnv, RegIndex timeoutVarRegIndex, Operand timeoutBlockAddr) {
/* emit a GOTO instruction to jump out of the timeout block */
Operand gotoAddr = getOperand(-1);
this.emit(InstructionCodes.GOTO, gotoAddr);
timeoutBlockAddr.value = nextIP();
if (forkJoin.timeoutVariable != null) {
visitForkJoinParameterDefs(forkJoin.timeoutVariable, forkJoinEnv);
timeoutVarRegIndex.value = forkJoin.timeoutVariable.symbol.varIndex.value;
}
if (forkJoin.timeoutBody != null) {
this.genNode(forkJoin.timeoutBody, forkJoinEnv);
}
gotoAddr.value = nextIP();
}
use of org.wso2.ballerinalang.programfile.Instruction in project ballerina by ballerina-lang.
the class CodeGenerator method visit.
public void visit(BLangLock lockNode) {
if (lockNode.lockVariables.isEmpty()) {
this.genNode(lockNode.body, this.env);
return;
}
Operand gotoLockEndAddr = getOperand(-1);
Instruction instructGotoLockEnd = InstructionFactory.get(InstructionCodes.GOTO, gotoLockEndAddr);
Operand[] operands = getOperands(lockNode);
ErrorTableAttributeInfo errorTable = createErrorTableIfAbsent(currentPkgInfo);
int fromIP = nextIP();
emit((InstructionCodes.LOCK), operands);
this.genNode(lockNode.body, this.env);
int toIP = nextIP() - 1;
emit((InstructionCodes.UNLOCK), operands);
emit(instructGotoLockEnd);
ErrorTableEntry errorTableEntry = new ErrorTableEntry(fromIP, toIP, nextIP(), 0, -1);
errorTable.addErrorTableEntry(errorTableEntry);
emit((InstructionCodes.UNLOCK), operands);
emit(InstructionFactory.get(InstructionCodes.THROW, getOperand(-1)));
gotoLockEndAddr.value = nextIP();
}
use of org.wso2.ballerinalang.programfile.Instruction in project ballerina by ballerina-lang.
the class CodeGenerator method visit.
public void visit(BLangTransaction transactionNode) {
++transactionIndex;
Operand transactionIndexOperand = getOperand(transactionIndex);
Operand retryCountRegIndex = new RegIndex(-1, TypeTags.INT);
if (transactionNode.retryCount != null) {
this.genNode(transactionNode.retryCount, this.env);
retryCountRegIndex = transactionNode.retryCount.regIndex;
}
Operand committedFuncRegIndex = new RegIndex(-1, TypeTags.INVOKABLE);
if (transactionNode.onCommitFunction != null) {
committedFuncRegIndex.value = getFuncRefCPIndex((BInvokableSymbol) ((BLangFunctionVarRef) transactionNode.onCommitFunction).symbol);
}
Operand abortedFuncRegIndex = new RegIndex(-1, TypeTags.INVOKABLE);
if (transactionNode.onAbortFunction != null) {
abortedFuncRegIndex.value = getFuncRefCPIndex((BInvokableSymbol) ((BLangFunctionVarRef) transactionNode.onAbortFunction).symbol);
}
ErrorTableAttributeInfo errorTable = createErrorTableIfAbsent(currentPkgInfo);
Operand transStmtEndAddr = getOperand(-1);
Operand transStmtAbortEndAddr = getOperand(-1);
Operand transStmtFailEndAddr = getOperand(-1);
Instruction gotoAbortTransBlockEnd = InstructionFactory.get(InstructionCodes.GOTO, transStmtAbortEndAddr);
Instruction gotoFailTransBlockEnd = InstructionFactory.get(InstructionCodes.GOTO, transStmtFailEndAddr);
abortInstructions.push(gotoAbortTransBlockEnd);
failInstructions.push(gotoFailTransBlockEnd);
// start transaction
this.emit(InstructionCodes.TR_BEGIN, transactionIndexOperand, retryCountRegIndex, committedFuncRegIndex, abortedFuncRegIndex);
Operand transBlockStartAddr = getOperand(nextIP());
// retry transaction;
Operand retryEndWithThrowAddr = getOperand(-1);
Operand retryEndWithNoThrowAddr = getOperand(-1);
this.emit(InstructionCodes.TR_RETRY, transactionIndexOperand, retryEndWithThrowAddr, retryEndWithNoThrowAddr);
// process transaction statements
this.genNode(transactionNode.transactionBody, this.env);
// end the transaction
int transBlockEndAddr = nextIP();
this.emit(InstructionCodes.TR_END, transactionIndexOperand, getOperand(TransactionStatus.SUCCESS.value()));
abortInstructions.pop();
failInstructions.pop();
emit(InstructionCodes.GOTO, transStmtEndAddr);
// CodeGen for error handling.
int errorTargetIP = nextIP();
transStmtFailEndAddr.value = errorTargetIP;
emit(InstructionCodes.TR_END, transactionIndexOperand, getOperand(TransactionStatus.FAILED.value()));
if (transactionNode.onRetryBody != null) {
this.genNode(transactionNode.onRetryBody, this.env);
}
emit(InstructionCodes.GOTO, transBlockStartAddr);
retryEndWithThrowAddr.value = nextIP();
emit(InstructionCodes.TR_END, transactionIndexOperand, getOperand(TransactionStatus.END.value()));
emit(InstructionCodes.THROW, getOperand(-1));
ErrorTableEntry errorTableEntry = new ErrorTableEntry(transBlockStartAddr.value, transBlockEndAddr, errorTargetIP, 0, -1);
errorTable.addErrorTableEntry(errorTableEntry);
transStmtAbortEndAddr.value = nextIP();
emit(InstructionCodes.TR_END, transactionIndexOperand, getOperand(TransactionStatus.ABORTED.value()));
int transactionEndIp = nextIP();
transStmtEndAddr.value = transactionEndIp;
retryEndWithNoThrowAddr.value = transactionEndIp;
emit(InstructionCodes.TR_END, transactionIndexOperand, getOperand(TransactionStatus.END.value()));
}
use of org.wso2.ballerinalang.programfile.Instruction in project ballerina by ballerina-lang.
the class CodeGenerator method visit.
public void visit(BLangTryCatchFinally tryNode) {
Operand gotoTryCatchEndAddr = getOperand(-1);
Instruction instructGotoTryCatchEnd = InstructionFactory.get(InstructionCodes.GOTO, gotoTryCatchEndAddr);
List<int[]> unhandledErrorRangeList = new ArrayList<>();
ErrorTableAttributeInfo errorTable = createErrorTableIfAbsent(currentPkgInfo);
// Handle try block.
int fromIP = nextIP();
genNode(tryNode.tryBody, env);
int toIP = nextIP() - 1;
// Append finally block instructions.
if (tryNode.finallyBody != null) {
genNode(tryNode.finallyBody, env);
}
emit(instructGotoTryCatchEnd);
unhandledErrorRangeList.add(new int[] { fromIP, toIP });
// Handle catch blocks.
int order = 0;
for (BLangCatch bLangCatch : tryNode.getCatchBlocks()) {
addLineNumberInfo(bLangCatch.pos);
int targetIP = nextIP();
genNode(bLangCatch, env);
unhandledErrorRangeList.add(new int[] { targetIP, nextIP() - 1 });
// Append finally block instructions.
if (tryNode.finallyBody != null) {
genNode(tryNode.finallyBody, env);
}
emit(instructGotoTryCatchEnd);
// Create Error table entry for this catch block
BTypeSymbol structSymbol = bLangCatch.param.symbol.type.tsymbol;
BPackageSymbol packageSymbol = (BPackageSymbol) bLangCatch.param.symbol.type.tsymbol.owner;
int pkgCPIndex = addPackageRefCPEntry(currentPkgInfo, packageSymbol.pkgID);
int structNameCPIndex = addUTF8CPEntry(currentPkgInfo, structSymbol.name.value);
StructureRefCPEntry structureRefCPEntry = new StructureRefCPEntry(pkgCPIndex, structNameCPIndex);
int structCPEntryIndex = currentPkgInfo.addCPEntry(structureRefCPEntry);
StructInfo errorStructInfo = this.programFile.packageInfoMap.get(packageSymbol.pkgID.bvmAlias()).getStructInfo(structSymbol.name.value);
ErrorTableEntry errorTableEntry = new ErrorTableEntry(fromIP, toIP, targetIP, order++, structCPEntryIndex);
errorTableEntry.setError(errorStructInfo);
errorTable.addErrorTableEntry(errorTableEntry);
}
if (tryNode.finallyBody != null) {
// Create Error table entry for unhandled errors in try and catch(s) blocks
for (int[] range : unhandledErrorRangeList) {
ErrorTableEntry errorTableEntry = new ErrorTableEntry(range[0], range[1], nextIP(), order++, -1);
errorTable.addErrorTableEntry(errorTableEntry);
}
// Append finally block instruction.
genNode(tryNode.finallyBody, env);
emit(InstructionFactory.get(InstructionCodes.THROW, getOperand(-1)));
}
gotoTryCatchEndAddr.value = nextIP();
}
Aggregations