2022-01-06 02:26:19 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-06-16 09:27:35 +00:00
|
|
|
#include "ecmascript/compiler/bytecode_circuit_builder.h"
|
|
|
|
|
2022-04-24 07:14:09 +00:00
|
|
|
#include "ecmascript/base/number_helper.h"
|
2022-06-10 07:22:21 +00:00
|
|
|
#include "ecmascript/compiler/gate_accessor.h"
|
2022-07-21 17:37:55 +00:00
|
|
|
#include "ecmascript/ts_types/ts_manager.h"
|
2022-09-20 15:17:00 +00:00
|
|
|
#include "libpandafile/bytecode_instruction-inl.h"
|
2022-08-26 01:06:59 +00:00
|
|
|
|
2022-01-19 01:11:02 +00:00
|
|
|
namespace panda::ecmascript::kungfu {
|
2022-03-07 02:58:13 +00:00
|
|
|
void BytecodeCircuitBuilder::BytecodeToCircuit()
|
2022-01-06 02:26:19 +00:00
|
|
|
{
|
2022-08-02 08:38:18 +00:00
|
|
|
std::map<std::pair<uint8_t *, uint8_t *>, std::vector<uint8_t *>> exceptionInfo;
|
2022-01-06 02:26:19 +00:00
|
|
|
|
|
|
|
// collect try catch block info
|
2022-08-02 08:38:18 +00:00
|
|
|
CollectTryCatchBlockInfo(exceptionInfo);
|
2022-01-06 02:26:19 +00:00
|
|
|
|
2022-03-07 02:58:13 +00:00
|
|
|
// Complete bytecode block Information
|
2022-08-02 08:38:18 +00:00
|
|
|
CompleteBytecodeBlockInfo();
|
2022-01-06 02:26:19 +00:00
|
|
|
|
|
|
|
// Building the basic block diagram of bytecode
|
2022-08-02 08:38:18 +00:00
|
|
|
BuildBasicBlocks(exceptionInfo);
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
|
|
|
|
2022-01-22 11:09:02 +00:00
|
|
|
void BytecodeCircuitBuilder::CollectBytecodeBlockInfo(uint8_t *pc, std::vector<CfgInfo> &bytecodeBlockInfos)
|
2022-01-06 02:26:19 +00:00
|
|
|
{
|
2022-09-19 07:19:07 +00:00
|
|
|
BytecodeInstruction inst(pc);
|
|
|
|
auto opcode = inst.GetOpcode();
|
2022-09-08 01:37:53 +00:00
|
|
|
auto bytecodeOffset = BytecodeInstruction::Size(opcode);
|
2022-09-21 02:26:33 +00:00
|
|
|
switch (static_cast<EcmaOpcode>(opcode)) {
|
2022-09-08 01:37:53 +00:00
|
|
|
case EcmaOpcode::JMP_IMM8: {
|
2022-04-15 06:42:30 +00:00
|
|
|
int8_t offset = static_cast<int8_t>(READ_INST_8_0());
|
2022-01-06 02:26:19 +00:00
|
|
|
std::vector<uint8_t *> temp;
|
|
|
|
temp.emplace_back(pc + offset);
|
|
|
|
// current basic block end
|
2022-01-19 01:11:02 +00:00
|
|
|
bytecodeBlockInfos.emplace_back(pc, SplitKind::END, temp);
|
2022-09-08 01:37:53 +00:00
|
|
|
bytecodeBlockInfos.emplace_back(pc + bytecodeOffset, SplitKind::START,
|
|
|
|
std::vector<uint8_t *>(1, pc + bytecodeOffset));
|
2022-01-06 02:26:19 +00:00
|
|
|
// jump basic block start
|
2022-01-22 11:09:02 +00:00
|
|
|
bytecodeBlockInfos.emplace_back(pc + offset, SplitKind::START, std::vector<uint8_t *>(1, pc + offset));
|
2022-08-16 15:48:09 +00:00
|
|
|
}
|
2022-09-06 05:27:19 +00:00
|
|
|
break;
|
2022-09-08 01:37:53 +00:00
|
|
|
case EcmaOpcode::JMP_IMM16: {
|
2022-04-15 06:42:30 +00:00
|
|
|
int16_t offset = static_cast<int16_t>(READ_INST_16_0());
|
2022-01-06 02:26:19 +00:00
|
|
|
std::vector<uint8_t *> temp;
|
|
|
|
temp.emplace_back(pc + offset);
|
2022-01-19 01:11:02 +00:00
|
|
|
bytecodeBlockInfos.emplace_back(pc, SplitKind::END, temp);
|
2022-09-08 01:37:53 +00:00
|
|
|
bytecodeBlockInfos.emplace_back(pc + bytecodeOffset, SplitKind::START,
|
|
|
|
std::vector<uint8_t *>(1, pc + bytecodeOffset));
|
|
|
|
bytecodeBlockInfos.emplace_back(pc + offset, SplitKind::START, std::vector<uint8_t *>(1, pc + offset));
|
2022-08-16 15:48:09 +00:00
|
|
|
}
|
2022-09-06 05:27:19 +00:00
|
|
|
break;
|
2022-09-08 01:37:53 +00:00
|
|
|
case EcmaOpcode::JMP_IMM32: {
|
2022-04-15 06:42:30 +00:00
|
|
|
int32_t offset = static_cast<int32_t>(READ_INST_32_0());
|
2022-01-06 02:26:19 +00:00
|
|
|
std::vector<uint8_t *> temp;
|
|
|
|
temp.emplace_back(pc + offset);
|
2022-01-19 01:11:02 +00:00
|
|
|
bytecodeBlockInfos.emplace_back(pc, SplitKind::END, temp);
|
2022-09-08 01:37:53 +00:00
|
|
|
bytecodeBlockInfos.emplace_back(pc + bytecodeOffset, SplitKind::START,
|
|
|
|
std::vector<uint8_t *>(1, pc + bytecodeOffset));
|
2022-01-22 11:09:02 +00:00
|
|
|
bytecodeBlockInfos.emplace_back(pc + offset, SplitKind::START, std::vector<uint8_t *>(1, pc + offset));
|
2022-08-16 15:48:09 +00:00
|
|
|
}
|
2022-09-06 05:27:19 +00:00
|
|
|
break;
|
2022-09-08 01:37:53 +00:00
|
|
|
case EcmaOpcode::JEQZ_IMM8: {
|
2022-01-06 02:26:19 +00:00
|
|
|
std::vector<uint8_t *> temp;
|
2022-09-08 01:37:53 +00:00
|
|
|
temp.emplace_back(pc + bytecodeOffset); // first successor
|
2022-04-15 06:42:30 +00:00
|
|
|
int8_t offset = static_cast<int8_t>(READ_INST_8_0());
|
2022-01-06 02:26:19 +00:00
|
|
|
temp.emplace_back(pc + offset); // second successor
|
|
|
|
// condition branch current basic block end
|
2022-01-19 01:11:02 +00:00
|
|
|
bytecodeBlockInfos.emplace_back(pc, SplitKind::END, temp);
|
2022-01-06 02:26:19 +00:00
|
|
|
// first branch basic block start
|
2022-09-08 01:37:53 +00:00
|
|
|
bytecodeBlockInfos.emplace_back(pc + bytecodeOffset, SplitKind::START,
|
|
|
|
std::vector<uint8_t *>(1, pc + bytecodeOffset));
|
2022-01-06 02:26:19 +00:00
|
|
|
// second branch basic block start
|
2022-01-19 01:11:02 +00:00
|
|
|
bytecodeBlockInfos.emplace_back(pc + offset, SplitKind::START, std::vector<uint8_t *>(1, pc + offset));
|
2022-08-16 15:48:09 +00:00
|
|
|
}
|
2022-09-06 05:27:19 +00:00
|
|
|
break;
|
2022-09-08 01:37:53 +00:00
|
|
|
case EcmaOpcode::JEQZ_IMM16: {
|
2022-01-06 02:26:19 +00:00
|
|
|
std::vector<uint8_t *> temp;
|
2022-09-08 01:37:53 +00:00
|
|
|
temp.emplace_back(pc + bytecodeOffset); // first successor
|
2022-04-15 06:42:30 +00:00
|
|
|
int16_t offset = static_cast<int16_t>(READ_INST_16_0());
|
2022-01-06 02:26:19 +00:00
|
|
|
temp.emplace_back(pc + offset); // second successor
|
2022-01-19 01:11:02 +00:00
|
|
|
bytecodeBlockInfos.emplace_back(pc, SplitKind::END, temp); // end
|
2022-09-08 01:37:53 +00:00
|
|
|
bytecodeBlockInfos.emplace_back(pc + bytecodeOffset, SplitKind::START,
|
|
|
|
std::vector<uint8_t *>(1, pc + bytecodeOffset));
|
|
|
|
bytecodeBlockInfos.emplace_back(pc + offset, SplitKind::START, std::vector<uint8_t *>(1, pc + offset));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EcmaOpcode::JEQZ_IMM32: {
|
|
|
|
std::vector<uint8_t *> temp;
|
|
|
|
temp.emplace_back(pc + bytecodeOffset); // first successor
|
|
|
|
int16_t offset = static_cast<int16_t>(READ_INST_32_0());
|
|
|
|
temp.emplace_back(pc + offset); // second successor
|
|
|
|
bytecodeBlockInfos.emplace_back(pc, SplitKind::END, temp); // end
|
|
|
|
bytecodeBlockInfos.emplace_back(pc + bytecodeOffset, SplitKind::START,
|
|
|
|
std::vector<uint8_t *>(1, pc + bytecodeOffset));
|
2022-01-19 01:11:02 +00:00
|
|
|
bytecodeBlockInfos.emplace_back(pc + offset, SplitKind::START, std::vector<uint8_t *>(1, pc + offset));
|
2022-08-16 15:48:09 +00:00
|
|
|
}
|
2022-09-06 05:27:19 +00:00
|
|
|
break;
|
2022-09-08 01:37:53 +00:00
|
|
|
case EcmaOpcode::JNEZ_IMM8: {
|
2022-01-06 02:26:19 +00:00
|
|
|
std::vector<uint8_t *> temp;
|
2022-09-08 01:37:53 +00:00
|
|
|
temp.emplace_back(pc + bytecodeOffset); // first successor
|
2022-04-15 06:42:30 +00:00
|
|
|
int8_t offset = static_cast<int8_t>(READ_INST_8_0());
|
2022-01-19 01:11:02 +00:00
|
|
|
temp.emplace_back(pc + offset); // second successor
|
|
|
|
bytecodeBlockInfos.emplace_back(pc, SplitKind::END, temp);
|
2022-09-08 01:37:53 +00:00
|
|
|
bytecodeBlockInfos.emplace_back(pc + bytecodeOffset, SplitKind::START,
|
|
|
|
std::vector<uint8_t *>(1, pc + bytecodeOffset));
|
2022-01-19 01:11:02 +00:00
|
|
|
bytecodeBlockInfos.emplace_back(pc + offset, SplitKind::START, std::vector<uint8_t *>(1, pc + offset));
|
2022-08-16 15:48:09 +00:00
|
|
|
}
|
2022-09-06 05:27:19 +00:00
|
|
|
break;
|
2022-09-08 01:37:53 +00:00
|
|
|
case EcmaOpcode::JNEZ_IMM16: {
|
2022-01-06 02:26:19 +00:00
|
|
|
std::vector<uint8_t *> temp;
|
2022-09-08 01:37:53 +00:00
|
|
|
temp.emplace_back(pc + bytecodeOffset); // first successor
|
2022-06-06 09:41:17 +00:00
|
|
|
int16_t offset = static_cast<int16_t>(READ_INST_16_0());
|
2022-01-19 01:11:02 +00:00
|
|
|
temp.emplace_back(pc + offset); // second successor
|
|
|
|
bytecodeBlockInfos.emplace_back(pc, SplitKind::END, temp);
|
2022-09-08 01:37:53 +00:00
|
|
|
bytecodeBlockInfos.emplace_back(pc + bytecodeOffset, SplitKind::START,
|
|
|
|
std::vector<uint8_t *>(1, pc + bytecodeOffset));
|
|
|
|
bytecodeBlockInfos.emplace_back(pc + offset, SplitKind::START, std::vector<uint8_t *>(1, pc + offset));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EcmaOpcode::JNEZ_IMM32: {
|
|
|
|
std::vector<uint8_t *> temp;
|
|
|
|
temp.emplace_back(pc + bytecodeOffset); // first successor
|
|
|
|
int16_t offset = static_cast<int16_t>(READ_INST_32_0());
|
|
|
|
temp.emplace_back(pc + offset); // second successor
|
|
|
|
bytecodeBlockInfos.emplace_back(pc, SplitKind::END, temp);
|
|
|
|
bytecodeBlockInfos.emplace_back(pc + bytecodeOffset, SplitKind::START,
|
|
|
|
std::vector<uint8_t *>(1, pc + bytecodeOffset));
|
2022-01-19 01:11:02 +00:00
|
|
|
bytecodeBlockInfos.emplace_back(pc + offset, SplitKind::START, std::vector<uint8_t *>(1, pc + offset));
|
2022-08-16 15:48:09 +00:00
|
|
|
}
|
2022-09-06 05:27:19 +00:00
|
|
|
break;
|
2022-09-08 01:37:53 +00:00
|
|
|
case EcmaOpcode::RETURN:
|
|
|
|
case EcmaOpcode::RETURNUNDEFINED:
|
|
|
|
case EcmaOpcode::THROW_PREF_NONE:
|
|
|
|
case EcmaOpcode::THROW_CONSTASSIGNMENT_PREF_V8:
|
|
|
|
case EcmaOpcode::THROW_NOTEXISTS_PREF_NONE:
|
|
|
|
case EcmaOpcode::THROW_PATTERNNONCOERCIBLE_PREF_NONE:
|
2022-09-19 08:49:40 +00:00
|
|
|
case EcmaOpcode::THROW_DELETESUPERPROPERTY_PREF_NONE: {
|
2022-01-19 01:11:02 +00:00
|
|
|
bytecodeBlockInfos.emplace_back(pc, SplitKind::END, std::vector<uint8_t *>(1, pc));
|
2022-01-06 02:26:19 +00:00
|
|
|
break;
|
2022-04-01 06:33:39 +00:00
|
|
|
}
|
2022-01-06 02:26:19 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-02 08:38:18 +00:00
|
|
|
void BytecodeCircuitBuilder::CollectTryCatchBlockInfo(std::map<std::pair<uint8_t *, uint8_t *>,
|
|
|
|
std::vector<uint8_t *>> &byteCodeException)
|
2022-01-06 02:26:19 +00:00
|
|
|
{
|
|
|
|
// try contains many catch
|
2022-06-07 04:12:06 +00:00
|
|
|
panda_file::MethodDataAccessor mda(*pf_, method_->GetMethodId());
|
|
|
|
panda_file::CodeDataAccessor cda(*pf_, mda.GetCodeId().value());
|
2022-08-02 08:38:18 +00:00
|
|
|
|
|
|
|
cda.EnumerateTryBlocks([this, &byteCodeException](
|
2022-01-18 04:02:20 +00:00
|
|
|
panda_file::CodeDataAccessor::TryBlock &try_block) {
|
2022-01-19 01:11:02 +00:00
|
|
|
auto tryStartOffset = try_block.GetStartPc();
|
|
|
|
auto tryEndOffset = try_block.GetStartPc() + try_block.GetLength();
|
2022-03-07 02:58:13 +00:00
|
|
|
auto tryStartPc = const_cast<uint8_t *>(method_->GetBytecodeArray() + tryStartOffset);
|
|
|
|
auto tryEndPc = const_cast<uint8_t *>(method_->GetBytecodeArray() + tryEndOffset);
|
2022-08-23 02:33:21 +00:00
|
|
|
// skip try blocks with same pc in start and end label
|
|
|
|
if (tryStartPc == tryEndPc) {
|
|
|
|
return true;
|
|
|
|
}
|
2022-01-06 02:26:19 +00:00
|
|
|
byteCodeException[std::make_pair(tryStartPc, tryEndPc)] = {};
|
|
|
|
uint32_t pcOffset = panda_file::INVALID_OFFSET;
|
|
|
|
try_block.EnumerateCatchBlocks([&](panda_file::CodeDataAccessor::CatchBlock &catch_block) {
|
|
|
|
pcOffset = catch_block.GetHandlerPc();
|
2022-03-07 02:58:13 +00:00
|
|
|
auto catchBlockPc = const_cast<uint8_t *>(method_->GetBytecodeArray() + pcOffset);
|
2022-01-06 02:26:19 +00:00
|
|
|
// try block associate catch block
|
|
|
|
byteCodeException[std::make_pair(tryStartPc, tryEndPc)].emplace_back(catchBlockPc);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
// Check whether the previous block of the try block exists.
|
|
|
|
// If yes, add the current block; otherwise, create a new block.
|
|
|
|
bool flag = false;
|
2022-08-02 08:38:18 +00:00
|
|
|
for (size_t i = 0; i < bytecodeBlockInfos_.size(); i++) {
|
|
|
|
if (bytecodeBlockInfos_[i].splitKind == SplitKind::START) {
|
2022-01-06 02:26:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-08-02 08:38:18 +00:00
|
|
|
if (bytecodeBlockInfos_[i].pc == byteCodeCurPrePc_.at(tryStartPc)) {
|
2022-01-06 02:26:19 +00:00
|
|
|
flag = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!flag) {
|
|
|
|
// pre block
|
2022-09-26 13:28:03 +00:00
|
|
|
if (byteCodeCurPrePc_.at(tryStartPc) != tryStartPc) {
|
|
|
|
bytecodeBlockInfos_.emplace_back(byteCodeCurPrePc_.at(tryStartPc), SplitKind::END,
|
2022-10-13 09:42:17 +00:00
|
|
|
std::vector<uint8_t *>(1, tryStartPc));
|
2022-09-26 13:28:03 +00:00
|
|
|
}
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
2022-01-19 01:11:02 +00:00
|
|
|
// try block
|
2022-08-02 08:38:18 +00:00
|
|
|
bytecodeBlockInfos_.emplace_back(tryStartPc, SplitKind::START, std::vector<uint8_t *>(1, tryStartPc));
|
2022-01-06 02:26:19 +00:00
|
|
|
flag = false;
|
2022-08-02 08:38:18 +00:00
|
|
|
for (size_t i = 0; i < bytecodeBlockInfos_.size(); i++) {
|
|
|
|
if (bytecodeBlockInfos_[i].splitKind == SplitKind::START) {
|
2022-01-06 02:26:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-08-02 08:38:18 +00:00
|
|
|
if (bytecodeBlockInfos_[i].pc == byteCodeCurPrePc_.at(tryEndPc)) {
|
|
|
|
auto &succs = bytecodeBlockInfos_[i].succs;
|
|
|
|
auto iter = std::find(succs.cbegin(), succs.cend(), bytecodeBlockInfos_[i].pc);
|
2022-04-24 17:17:29 +00:00
|
|
|
if (iter == succs.cend()) {
|
2022-09-19 07:19:07 +00:00
|
|
|
auto opcode = PcToOpcode(bytecodeBlockInfos_[i].pc);
|
2022-01-06 02:26:19 +00:00
|
|
|
switch (opcode) {
|
2022-09-08 01:37:53 +00:00
|
|
|
case EcmaOpcode::JMP_IMM8:
|
|
|
|
case EcmaOpcode::JMP_IMM16:
|
|
|
|
case EcmaOpcode::JMP_IMM32:
|
|
|
|
case EcmaOpcode::JEQZ_IMM8:
|
|
|
|
case EcmaOpcode::JEQZ_IMM16:
|
|
|
|
case EcmaOpcode::JEQZ_IMM32:
|
|
|
|
case EcmaOpcode::JNEZ_IMM8:
|
|
|
|
case EcmaOpcode::JNEZ_IMM16:
|
|
|
|
case EcmaOpcode::JNEZ_IMM32:
|
|
|
|
case EcmaOpcode::RETURN:
|
|
|
|
case EcmaOpcode::RETURNUNDEFINED:
|
|
|
|
case EcmaOpcode::THROW_PREF_NONE: {
|
2022-01-06 02:26:19 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
succs.emplace_back(tryEndPc);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
flag = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!flag) {
|
2022-08-02 08:38:18 +00:00
|
|
|
bytecodeBlockInfos_.emplace_back(byteCodeCurPrePc_.at(tryEndPc), SplitKind::END,
|
|
|
|
std::vector<uint8_t *>(1, tryEndPc));
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
2022-08-02 08:38:18 +00:00
|
|
|
bytecodeBlockInfos_.emplace_back(tryEndPc, SplitKind::START, std::vector<uint8_t *>(1, tryEndPc)); // next block
|
2022-01-06 02:26:19 +00:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-02 08:38:18 +00:00
|
|
|
void BytecodeCircuitBuilder::CompleteBytecodeBlockInfo()
|
2022-01-06 02:26:19 +00:00
|
|
|
{
|
2022-08-02 08:38:18 +00:00
|
|
|
std::sort(bytecodeBlockInfos_.begin(), bytecodeBlockInfos_.end());
|
2022-01-06 02:26:19 +00:00
|
|
|
|
|
|
|
// Deduplicate
|
2022-08-02 08:38:18 +00:00
|
|
|
auto deduplicateIndex = std::unique(bytecodeBlockInfos_.begin(), bytecodeBlockInfos_.end());
|
|
|
|
bytecodeBlockInfos_.erase(deduplicateIndex, bytecodeBlockInfos_.end());
|
2022-01-06 02:26:19 +00:00
|
|
|
|
|
|
|
// Supplementary block information
|
2022-09-08 01:37:53 +00:00
|
|
|
// endBlockPc: Pairs occur, with odd indexes indicating endPc, and even indexes indicating startPc.
|
2022-01-06 02:26:19 +00:00
|
|
|
std::vector<uint8_t *> endBlockPc;
|
2022-09-08 01:37:53 +00:00
|
|
|
std::vector<uint8_t *> startBlockPc; //
|
2022-08-02 08:38:18 +00:00
|
|
|
for (size_t i = 0; i < bytecodeBlockInfos_.size() - 1; i++) {
|
|
|
|
if (bytecodeBlockInfos_[i].splitKind == bytecodeBlockInfos_[i + 1].splitKind &&
|
|
|
|
bytecodeBlockInfos_[i].splitKind == SplitKind::START) {
|
|
|
|
auto prePc = byteCodeCurPrePc_.at(bytecodeBlockInfos_[i + 1].pc);
|
2022-01-06 02:26:19 +00:00
|
|
|
endBlockPc.emplace_back(prePc); // Previous instruction of current instruction
|
2022-08-02 08:38:18 +00:00
|
|
|
endBlockPc.emplace_back(bytecodeBlockInfos_[i + 1].pc); // current instruction
|
2022-01-06 02:26:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-08-02 08:38:18 +00:00
|
|
|
if (bytecodeBlockInfos_[i].splitKind == bytecodeBlockInfos_[i + 1].splitKind &&
|
|
|
|
bytecodeBlockInfos_[i].splitKind == SplitKind::END) {
|
|
|
|
auto tempPc = bytecodeBlockInfos_[i].pc;
|
|
|
|
auto findItem = std::find_if(byteCodeCurPrePc_.cbegin(), byteCodeCurPrePc_.cend(),
|
2022-01-18 04:02:20 +00:00
|
|
|
[tempPc](const std::map<uint8_t *, uint8_t *>::value_type item) {
|
|
|
|
return item.second == tempPc;
|
|
|
|
});
|
2022-08-02 08:38:18 +00:00
|
|
|
if (findItem != byteCodeCurPrePc_.cend()) {
|
2022-01-06 02:26:19 +00:00
|
|
|
startBlockPc.emplace_back((*findItem).first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Supplementary end block info
|
2022-04-24 17:17:29 +00:00
|
|
|
for (auto iter = endBlockPc.cbegin(); iter != endBlockPc.cend(); iter += 2) { // 2: index
|
2022-09-08 01:37:53 +00:00
|
|
|
bytecodeBlockInfos_.emplace_back(*iter, SplitKind::END, std::vector<uint8_t *>(1, *(iter + 1)));
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
|
|
|
// Supplementary start block info
|
2022-04-24 17:17:29 +00:00
|
|
|
for (auto iter = startBlockPc.cbegin(); iter != startBlockPc.cend(); iter++) {
|
2022-08-02 08:38:18 +00:00
|
|
|
bytecodeBlockInfos_.emplace_back(*iter, SplitKind::START, std::vector<uint8_t *>(1, *iter));
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Deduplicate successor
|
2022-08-02 08:38:18 +00:00
|
|
|
for (size_t i = 0; i < bytecodeBlockInfos_.size(); i++) {
|
|
|
|
if (bytecodeBlockInfos_[i].splitKind == SplitKind::END) {
|
|
|
|
std::set<uint8_t *> tempSet(bytecodeBlockInfos_[i].succs.cbegin(),
|
|
|
|
bytecodeBlockInfos_[i].succs.cend());
|
|
|
|
bytecodeBlockInfos_[i].succs.assign(tempSet.cbegin(), tempSet.cend());
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-02 08:38:18 +00:00
|
|
|
std::sort(bytecodeBlockInfos_.begin(), bytecodeBlockInfos_.end());
|
2022-01-06 02:26:19 +00:00
|
|
|
|
|
|
|
// handling jumps to an empty block
|
2022-08-02 08:38:18 +00:00
|
|
|
auto endPc = bytecodeBlockInfos_[bytecodeBlockInfos_.size() - 1].pc;
|
|
|
|
auto iter = --byteCodeCurPrePc_.cend();
|
2022-01-06 02:26:19 +00:00
|
|
|
if (endPc == iter->first) {
|
2022-08-02 08:38:18 +00:00
|
|
|
bytecodeBlockInfos_.emplace_back(endPc, SplitKind::END, std::vector<uint8_t *>(1, endPc));
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
|
|
|
// Deduplicate
|
2022-08-02 08:38:18 +00:00
|
|
|
deduplicateIndex = std::unique(bytecodeBlockInfos_.begin(), bytecodeBlockInfos_.end());
|
|
|
|
bytecodeBlockInfos_.erase(deduplicateIndex, bytecodeBlockInfos_.end());
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
|
|
|
|
2022-03-07 02:58:13 +00:00
|
|
|
void BytecodeCircuitBuilder::BuildBasicBlocks(std::map<std::pair<uint8_t *, uint8_t *>,
|
2022-10-27 03:56:58 +00:00
|
|
|
std::vector<uint8_t *>> &exception)
|
2022-01-06 02:26:19 +00:00
|
|
|
{
|
2022-01-22 11:09:02 +00:00
|
|
|
std::map<uint8_t *, BytecodeRegion *> startPcToBB; // [start, bb]
|
|
|
|
std::map<uint8_t *, BytecodeRegion *> endPcToBB; // [end, bb]
|
2022-08-02 08:38:18 +00:00
|
|
|
graph_.resize(bytecodeBlockInfos_.size() / 2); // 2 : half size
|
2022-01-06 02:26:19 +00:00
|
|
|
// build basic block
|
|
|
|
int blockId = 0;
|
|
|
|
int index = 0;
|
2022-08-02 08:38:18 +00:00
|
|
|
for (size_t i = 0; i < bytecodeBlockInfos_.size() - 1; i += 2) { // 2:index
|
|
|
|
auto startPc = bytecodeBlockInfos_[i].pc;
|
|
|
|
auto endPc = bytecodeBlockInfos_[i + 1].pc;
|
2022-04-24 17:17:29 +00:00
|
|
|
auto block = &graph_[index++];
|
2022-01-06 02:26:19 +00:00
|
|
|
block->id = blockId++;
|
|
|
|
block->start = startPc;
|
|
|
|
block->end = endPc;
|
|
|
|
block->preds = {};
|
|
|
|
block->succs = {};
|
2022-01-19 01:11:02 +00:00
|
|
|
startPcToBB[startPc] = block;
|
|
|
|
endPcToBB[endPc] = block;
|
2022-10-27 03:56:58 +00:00
|
|
|
block->bytecodeIterator_.Reset(this, startPc, endPc);
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// add block associate
|
2022-08-02 08:38:18 +00:00
|
|
|
for (size_t i = 0; i < bytecodeBlockInfos_.size(); i++) {
|
|
|
|
if (bytecodeBlockInfos_[i].splitKind == SplitKind::START) {
|
2022-01-06 02:26:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-08-02 08:38:18 +00:00
|
|
|
auto curPc = bytecodeBlockInfos_[i].pc;
|
|
|
|
auto &successors = bytecodeBlockInfos_[i].succs;
|
2022-01-06 02:26:19 +00:00
|
|
|
for (size_t j = 0; j < successors.size(); j++) {
|
|
|
|
if (successors[j] == curPc) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-01-19 01:11:02 +00:00
|
|
|
auto curBlock = endPcToBB[curPc];
|
|
|
|
auto succsBlock = startPcToBB[successors[j]];
|
2022-01-06 02:26:19 +00:00
|
|
|
curBlock->succs.emplace_back(succsBlock);
|
|
|
|
succsBlock->preds.emplace_back(curBlock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// try catch block associate
|
2022-04-24 17:17:29 +00:00
|
|
|
for (size_t i = 0; i < graph_.size(); i++) {
|
|
|
|
const auto pc = graph_[i].start;
|
|
|
|
auto it = exception.cbegin();
|
|
|
|
for (; it != exception.cend(); it++) {
|
2022-01-06 02:26:19 +00:00
|
|
|
if (pc < it->first.first || pc >= it->first.second) { // try block interval
|
2022-01-18 04:02:20 +00:00
|
|
|
continue;
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
|
|
|
auto catchs = exception[it->first]; // catchs start pc
|
2022-04-24 17:17:29 +00:00
|
|
|
for (size_t j = i + 1; j < graph_.size(); j++) {
|
|
|
|
if (std::find(catchs.cbegin(), catchs.cend(), graph_[j].start) != catchs.cend()) {
|
|
|
|
graph_[i].catchs.insert(graph_[i].catchs.cbegin(), &graph_[j]);
|
|
|
|
graph_[i].succs.emplace_back(&graph_[j]);
|
|
|
|
graph_[j].preds.emplace_back(&graph_[i]);
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-21 02:14:57 +00:00
|
|
|
|
|
|
|
// When there are multiple catch blocks in the current block, the set of catch blocks
|
|
|
|
// needs to be sorted to satisfy the order of execution of catch blocks.
|
|
|
|
BytecodeRegion& bb = graph_[i];
|
2022-07-15 02:35:25 +00:00
|
|
|
bb.SortCatches();
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 08:14:34 +00:00
|
|
|
if (IsLogEnabled()) {
|
2022-10-03 04:31:39 +00:00
|
|
|
PrintGraph("Build Basic Block");
|
2022-04-14 08:14:34 +00:00
|
|
|
}
|
2022-04-24 17:17:29 +00:00
|
|
|
ComputeDominatorTree();
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
|
|
|
|
2022-04-24 17:17:29 +00:00
|
|
|
void BytecodeCircuitBuilder::ComputeDominatorTree()
|
2022-01-06 02:26:19 +00:00
|
|
|
{
|
|
|
|
// Construct graph backward order
|
2022-10-15 04:09:38 +00:00
|
|
|
std::map<size_t, size_t> bbIdToDfsTimestamp;
|
2022-08-01 09:13:57 +00:00
|
|
|
std::unordered_map<size_t, size_t> dfsFatherIdx;
|
|
|
|
std::unordered_map<size_t, size_t> bbDfsTimestampToIdx;
|
2022-10-15 04:09:38 +00:00
|
|
|
std::vector<size_t> basicBlockList;
|
2022-01-06 02:26:19 +00:00
|
|
|
size_t timestamp = 0;
|
|
|
|
std::deque<size_t> pendingList;
|
2022-04-24 17:17:29 +00:00
|
|
|
std::vector<size_t> visited(graph_.size(), 0);
|
|
|
|
auto basicBlockId = graph_[0].id;
|
2022-08-01 09:13:57 +00:00
|
|
|
visited[graph_[0].id] = 1;
|
2022-10-27 09:12:44 +00:00
|
|
|
pendingList.emplace_back(basicBlockId);
|
2022-01-06 02:26:19 +00:00
|
|
|
while (!pendingList.empty()) {
|
2022-08-01 09:13:57 +00:00
|
|
|
size_t curBlockId = pendingList.back();
|
2022-01-06 02:26:19 +00:00
|
|
|
pendingList.pop_back();
|
2022-10-27 09:12:44 +00:00
|
|
|
basicBlockList.emplace_back(curBlockId);
|
2022-10-15 04:09:38 +00:00
|
|
|
bbIdToDfsTimestamp[curBlockId] = timestamp++;
|
2022-08-01 09:13:57 +00:00
|
|
|
for (const auto &succBlock: graph_[curBlockId].succs) {
|
2022-01-06 02:26:19 +00:00
|
|
|
if (visited[succBlock->id] == 0) {
|
|
|
|
visited[succBlock->id] = 1;
|
2022-10-27 09:12:44 +00:00
|
|
|
pendingList.emplace_back(succBlock->id);
|
2022-10-15 04:09:38 +00:00
|
|
|
dfsFatherIdx[succBlock->id] = bbIdToDfsTimestamp[curBlockId];
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-26 01:06:59 +00:00
|
|
|
|
2022-10-15 04:09:38 +00:00
|
|
|
for (size_t idx = 0; idx < basicBlockList.size(); idx++) {
|
|
|
|
bbDfsTimestampToIdx[basicBlockList[idx]] = idx;
|
2022-08-01 09:13:57 +00:00
|
|
|
}
|
2022-10-15 04:09:38 +00:00
|
|
|
RemoveDeadRegions(bbIdToDfsTimestamp);
|
2022-01-06 02:26:19 +00:00
|
|
|
|
2022-10-15 04:09:38 +00:00
|
|
|
std::vector<size_t> immDom(basicBlockList.size()); // immediate dominator with dfs order index
|
|
|
|
std::vector<size_t> semiDom(basicBlockList.size());
|
2022-08-01 09:13:57 +00:00
|
|
|
std::vector<size_t> realImmDom(graph_.size()); // immediate dominator with real index
|
2022-10-15 04:09:38 +00:00
|
|
|
std::vector<std::vector<size_t> > semiDomTree(basicBlockList.size());
|
2022-08-01 09:13:57 +00:00
|
|
|
{
|
2022-10-15 04:09:38 +00:00
|
|
|
std::vector<size_t> parent(basicBlockList.size());
|
2022-08-01 09:13:57 +00:00
|
|
|
std::iota(parent.begin(), parent.end(), 0);
|
2022-10-15 04:09:38 +00:00
|
|
|
std::vector<size_t> minIdx(basicBlockList.size());
|
2022-08-01 09:13:57 +00:00
|
|
|
std::function<size_t(size_t)> unionFind = [&] (size_t idx) -> size_t {
|
|
|
|
if (parent[idx] == idx) return idx;
|
|
|
|
size_t unionFindSetRoot = unionFind(parent[idx]);
|
|
|
|
if (semiDom[minIdx[idx]] > semiDom[minIdx[parent[idx]]]) {
|
|
|
|
minIdx[idx] = minIdx[parent[idx]];
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
2022-08-01 09:13:57 +00:00
|
|
|
return parent[idx] = unionFindSetRoot;
|
|
|
|
};
|
|
|
|
auto merge = [&] (size_t fatherIdx, size_t sonIdx) -> void {
|
|
|
|
size_t parentFatherIdx = unionFind(fatherIdx);
|
|
|
|
size_t parentSonIdx = unionFind(sonIdx);
|
|
|
|
parent[parentSonIdx] = parentFatherIdx;
|
|
|
|
};
|
|
|
|
std::iota(semiDom.begin(), semiDom.end(), 0);
|
|
|
|
semiDom[0] = semiDom.size();
|
2022-10-15 04:09:38 +00:00
|
|
|
for (size_t idx = basicBlockList.size() - 1; idx >= 1; idx--) {
|
|
|
|
for (const auto &preBlock : graph_[basicBlockList[idx]].preds) {
|
2022-08-01 09:13:57 +00:00
|
|
|
if (bbDfsTimestampToIdx[preBlock->id] < idx) {
|
|
|
|
semiDom[idx] = std::min(semiDom[idx], bbDfsTimestampToIdx[preBlock->id]);
|
|
|
|
} else {
|
|
|
|
unionFind(bbDfsTimestampToIdx[preBlock->id]);
|
|
|
|
semiDom[idx] = std::min(semiDom[idx], semiDom[minIdx[bbDfsTimestampToIdx[preBlock->id]]]);
|
|
|
|
}
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
2022-08-01 09:13:57 +00:00
|
|
|
for (const auto & succDomIdx : semiDomTree[idx]) {
|
|
|
|
unionFind(succDomIdx);
|
|
|
|
if (idx == semiDom[minIdx[succDomIdx]]) {
|
|
|
|
immDom[succDomIdx] = idx;
|
|
|
|
} else {
|
|
|
|
immDom[succDomIdx] = minIdx[succDomIdx];
|
|
|
|
}
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
2022-08-01 09:13:57 +00:00
|
|
|
minIdx[idx] = idx;
|
2022-10-15 04:09:38 +00:00
|
|
|
merge(dfsFatherIdx[basicBlockList[idx]], idx);
|
2022-10-27 09:12:44 +00:00
|
|
|
semiDomTree[semiDom[idx]].emplace_back(idx);
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
2022-10-15 04:09:38 +00:00
|
|
|
for (size_t idx = 1; idx < basicBlockList.size(); idx++) {
|
2022-08-01 09:13:57 +00:00
|
|
|
if (immDom[idx] != semiDom[idx]) {
|
|
|
|
immDom[idx] = immDom[immDom[idx]];
|
2022-04-14 08:14:34 +00:00
|
|
|
}
|
2022-10-15 04:09:38 +00:00
|
|
|
realImmDom[basicBlockList[idx]] = basicBlockList[immDom[idx]];
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
2022-08-01 09:13:57 +00:00
|
|
|
semiDom[0] = 0;
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 08:14:34 +00:00
|
|
|
if (IsLogEnabled()) {
|
2022-10-03 04:31:39 +00:00
|
|
|
PrintGraph("Computed Dom Trees");
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
2022-04-14 08:14:34 +00:00
|
|
|
|
2022-08-01 09:13:57 +00:00
|
|
|
BuildImmediateDominator(realImmDom);
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
2022-09-29 06:18:49 +00:00
|
|
|
|
2022-04-24 17:17:29 +00:00
|
|
|
void BytecodeCircuitBuilder::BuildImmediateDominator(const std::vector<size_t> &immDom)
|
2022-01-06 02:26:19 +00:00
|
|
|
{
|
2022-04-24 17:17:29 +00:00
|
|
|
graph_[0].iDominator = &graph_[0];
|
2022-01-18 04:02:20 +00:00
|
|
|
for (size_t i = 1; i < immDom.size(); i++) {
|
2022-04-24 17:17:29 +00:00
|
|
|
auto dominatedBlock = &graph_[i];
|
2022-01-06 02:26:19 +00:00
|
|
|
if (dominatedBlock->isDead) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-04-24 17:17:29 +00:00
|
|
|
auto immDomBlock = &graph_[immDom[i]];
|
2022-01-06 02:26:19 +00:00
|
|
|
dominatedBlock->iDominator = immDomBlock;
|
|
|
|
}
|
|
|
|
|
2022-04-24 17:17:29 +00:00
|
|
|
for (auto &block : graph_) {
|
2022-01-06 02:26:19 +00:00
|
|
|
if (block.isDead) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (block.iDominator->id != block.id) {
|
|
|
|
block.iDominator->immDomBlocks.emplace_back(&block);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-24 17:17:29 +00:00
|
|
|
ComputeDomFrontiers(immDom);
|
|
|
|
InsertPhi();
|
|
|
|
UpdateCFG();
|
|
|
|
BuildCircuit();
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
|
|
|
|
2022-04-24 17:17:29 +00:00
|
|
|
void BytecodeCircuitBuilder::ComputeDomFrontiers(const std::vector<size_t> &immDom)
|
2022-01-06 02:26:19 +00:00
|
|
|
{
|
2022-01-22 11:09:02 +00:00
|
|
|
std::vector<std::set<BytecodeRegion *>> domFrontiers(immDom.size());
|
2022-04-24 17:17:29 +00:00
|
|
|
for (auto &bb : graph_) {
|
2022-01-06 02:26:19 +00:00
|
|
|
if (bb.isDead) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-01-22 11:09:02 +00:00
|
|
|
if (bb.preds.size() < 2) { // 2: pred num
|
2022-01-06 02:26:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < bb.preds.size(); i++) {
|
|
|
|
auto runner = bb.preds[i];
|
2022-01-18 04:02:20 +00:00
|
|
|
while (runner->id != immDom[bb.id]) {
|
2022-01-06 02:26:19 +00:00
|
|
|
domFrontiers[runner->id].insert(&bb);
|
2022-04-24 17:17:29 +00:00
|
|
|
runner = &graph_[immDom[runner->id]];
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < domFrontiers.size(); i++) {
|
2022-04-24 17:17:29 +00:00
|
|
|
for (auto iter = domFrontiers[i].cbegin(); iter != domFrontiers[i].cend(); iter++) {
|
|
|
|
graph_[i].domFrontiers.emplace_back(*iter);
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-15 04:09:38 +00:00
|
|
|
void BytecodeCircuitBuilder::RemoveDeadRegions(const std::map<size_t, size_t> &bbIdToDfsTimestamp)
|
2022-01-06 02:26:19 +00:00
|
|
|
{
|
2022-04-24 17:17:29 +00:00
|
|
|
for (auto &block: graph_) {
|
2022-01-22 11:09:02 +00:00
|
|
|
std::vector<BytecodeRegion *> newPreds;
|
2022-01-19 01:11:02 +00:00
|
|
|
for (auto &bb : block.preds) {
|
2022-10-15 04:09:38 +00:00
|
|
|
if (bbIdToDfsTimestamp.count(bb->id)) {
|
2022-01-06 02:26:19 +00:00
|
|
|
newPreds.emplace_back(bb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
block.preds = newPreds;
|
|
|
|
}
|
|
|
|
|
2022-04-24 17:17:29 +00:00
|
|
|
for (auto &block : graph_) {
|
2022-10-15 04:09:38 +00:00
|
|
|
block.isDead = !bbIdToDfsTimestamp.count(block.id);
|
2022-01-06 02:26:19 +00:00
|
|
|
if (block.isDead) {
|
|
|
|
block.succs.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-24 17:17:29 +00:00
|
|
|
void BytecodeCircuitBuilder::InsertPhi()
|
2022-01-06 02:26:19 +00:00
|
|
|
{
|
|
|
|
std::map<uint16_t, std::set<size_t>> defsitesInfo; // <vreg, bbs>
|
2022-04-24 17:17:29 +00:00
|
|
|
for (auto &bb : graph_) {
|
2022-01-06 02:26:19 +00:00
|
|
|
if (bb.isDead) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-06-30 02:22:52 +00:00
|
|
|
EnumerateBlock(bb, [this, &defsitesInfo, &bb]
|
2022-10-27 03:56:58 +00:00
|
|
|
(const BytecodeInfo &bytecodeInfo) -> bool {
|
2022-09-29 06:18:49 +00:00
|
|
|
if (bytecodeInfo.IsBc(EcmaOpcode::RESUMEGENERATOR)) {
|
2022-10-03 02:45:22 +00:00
|
|
|
auto numVRegs = method_->GetNumberVRegs();
|
2022-06-06 09:41:17 +00:00
|
|
|
for (size_t i = 0; i < numVRegs; i++) {
|
2022-10-27 03:56:58 +00:00
|
|
|
defsitesInfo[i].insert(bb.id);
|
2022-06-06 09:41:17 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-18 04:02:20 +00:00
|
|
|
for (const auto &vreg: bytecodeInfo.vregOut) {
|
2022-01-06 02:26:19 +00:00
|
|
|
defsitesInfo[vreg].insert(bb.id);
|
|
|
|
}
|
2022-06-30 02:22:52 +00:00
|
|
|
return true;
|
|
|
|
});
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 02:35:25 +00:00
|
|
|
// handle phi generated from multiple control flow in the same source block
|
|
|
|
InsertExceptionPhi(defsitesInfo);
|
|
|
|
|
2022-01-19 01:11:02 +00:00
|
|
|
for (const auto&[variable, defsites] : defsitesInfo) {
|
2022-01-06 02:26:19 +00:00
|
|
|
std::queue<uint16_t> workList;
|
2022-01-18 04:02:20 +00:00
|
|
|
for (auto blockId: defsites) {
|
2022-01-06 02:26:19 +00:00
|
|
|
workList.push(blockId);
|
|
|
|
}
|
|
|
|
while (!workList.empty()) {
|
|
|
|
auto currentId = workList.front();
|
|
|
|
workList.pop();
|
2022-04-24 17:17:29 +00:00
|
|
|
for (auto &block : graph_[currentId].domFrontiers) {
|
2022-01-06 02:26:19 +00:00
|
|
|
if (!block->phi.count(variable)) {
|
|
|
|
block->phi.insert(variable);
|
|
|
|
if (!defsitesInfo[variable].count(block->id)) {
|
|
|
|
workList.push(block->id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-14 08:14:34 +00:00
|
|
|
|
|
|
|
if (IsLogEnabled()) {
|
2022-10-03 04:31:39 +00:00
|
|
|
PrintGraph("Inserted Phis");
|
2022-04-14 08:14:34 +00:00
|
|
|
}
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 02:35:25 +00:00
|
|
|
void BytecodeCircuitBuilder::InsertExceptionPhi(std::map<uint16_t, std::set<size_t>> &defsitesInfo)
|
|
|
|
{
|
|
|
|
// handle try catch defsite
|
|
|
|
for (auto &bb : graph_) {
|
|
|
|
if (bb.isDead) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (bb.catchs.size() == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
std::set<size_t> vregs;
|
2022-08-05 09:00:14 +00:00
|
|
|
EnumerateBlock(bb, [this, &vregs]
|
2022-10-27 03:56:58 +00:00
|
|
|
(const BytecodeInfo &bytecodeInfo) -> bool {
|
2022-09-29 06:18:49 +00:00
|
|
|
if (bytecodeInfo.IsBc(EcmaOpcode::RESUMEGENERATOR)) {
|
2022-10-03 02:45:22 +00:00
|
|
|
auto numVRegs = method_->GetNumberVRegs();
|
2022-08-05 09:00:14 +00:00
|
|
|
for (size_t i = 0; i < numVRegs; i++) {
|
|
|
|
vregs.insert(i);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2022-07-15 02:35:25 +00:00
|
|
|
for (const auto &vreg: bytecodeInfo.vregOut) {
|
|
|
|
vregs.insert(vreg);
|
|
|
|
}
|
2022-06-30 02:22:52 +00:00
|
|
|
return true;
|
|
|
|
});
|
2022-07-15 02:35:25 +00:00
|
|
|
|
|
|
|
for (auto &vreg : vregs) {
|
|
|
|
defsitesInfo[vreg].insert(bb.catchs.at(0)->id);
|
|
|
|
bb.catchs.at(0)->phi.insert(vreg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-19 01:11:02 +00:00
|
|
|
// Update CFG's predecessor, successor and try catch associations
|
2022-04-24 17:17:29 +00:00
|
|
|
void BytecodeCircuitBuilder::UpdateCFG()
|
2022-01-19 01:11:02 +00:00
|
|
|
{
|
2022-04-24 17:17:29 +00:00
|
|
|
for (auto &bb: graph_) {
|
2022-01-19 01:11:02 +00:00
|
|
|
if (bb.isDead) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
bb.preds.clear();
|
|
|
|
bb.trys.clear();
|
2022-01-22 11:09:02 +00:00
|
|
|
std::vector<BytecodeRegion *> newSuccs;
|
2022-01-19 01:11:02 +00:00
|
|
|
for (const auto &succ: bb.succs) {
|
2022-04-24 17:17:29 +00:00
|
|
|
if (std::count(bb.catchs.cbegin(), bb.catchs.cend(), succ)) {
|
2022-01-19 01:11:02 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-10-27 09:12:44 +00:00
|
|
|
newSuccs.emplace_back(succ);
|
2022-01-19 01:11:02 +00:00
|
|
|
}
|
|
|
|
bb.succs = newSuccs;
|
|
|
|
}
|
2022-04-24 17:17:29 +00:00
|
|
|
for (auto &bb: graph_) {
|
2022-01-19 01:11:02 +00:00
|
|
|
if (bb.isDead) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (auto &succ: bb.succs) {
|
2022-10-27 09:12:44 +00:00
|
|
|
succ->preds.emplace_back(&bb);
|
2022-01-19 01:11:02 +00:00
|
|
|
}
|
|
|
|
for (auto &catchBlock: bb.catchs) {
|
2022-10-27 09:12:44 +00:00
|
|
|
catchBlock->trys.emplace_back(&bb);
|
2022-01-19 01:11:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-24 17:17:29 +00:00
|
|
|
// build circuit
|
|
|
|
void BytecodeCircuitBuilder::BuildCircuitArgs()
|
|
|
|
{
|
2022-06-16 09:27:35 +00:00
|
|
|
argAcc_.NewCommonArg(CommonArgIdx::GLUE, MachineType::I64, GateType::NJSValue());
|
|
|
|
argAcc_.NewCommonArg(CommonArgIdx::LEXENV, MachineType::I64, GateType::TaggedValue());
|
2022-09-07 09:46:52 +00:00
|
|
|
argAcc_.NewCommonArg(CommonArgIdx::ACTUAL_ARGC, MachineType::I64, GateType::NJSValue());
|
2022-06-16 09:27:35 +00:00
|
|
|
auto funcIdx = static_cast<size_t>(CommonArgIdx::FUNC);
|
|
|
|
const size_t actualNumArgs = argAcc_.GetActualNumArgs();
|
|
|
|
// new actual argument gates
|
|
|
|
for (size_t argIdx = funcIdx; argIdx < actualNumArgs; argIdx++) {
|
|
|
|
argAcc_.NewArg(argIdx);
|
2022-04-24 17:17:29 +00:00
|
|
|
}
|
2022-07-19 12:44:46 +00:00
|
|
|
argAcc_.CollectArgs();
|
2022-10-22 07:59:53 +00:00
|
|
|
if (HasTypes()) {
|
2022-07-05 07:24:00 +00:00
|
|
|
argAcc_.FillArgsGateType(&typeRecorder_);
|
|
|
|
}
|
2022-04-24 17:17:29 +00:00
|
|
|
}
|
|
|
|
|
2022-06-30 02:22:52 +00:00
|
|
|
bool BytecodeCircuitBuilder::ShouldBeDead(BytecodeRegion &curBlock)
|
|
|
|
{
|
|
|
|
auto isDead = false;
|
|
|
|
for (auto bbPred : curBlock.preds) {
|
|
|
|
if (!bbPred->isDead) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
isDead = true;
|
|
|
|
}
|
|
|
|
for (auto bbTry : curBlock.trys) {
|
|
|
|
if (!bbTry->isDead) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
isDead = true;
|
|
|
|
}
|
|
|
|
return isDead;
|
|
|
|
}
|
|
|
|
|
2022-04-24 17:17:29 +00:00
|
|
|
void BytecodeCircuitBuilder::CollectPredsInfo()
|
|
|
|
{
|
|
|
|
for (auto &bb: graph_) {
|
|
|
|
if (bb.isDead) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
bb.numOfStatePreds = 0;
|
|
|
|
}
|
|
|
|
// get number of expanded state predicates of each block
|
|
|
|
// one block-level try catch edge may correspond to multiple bytecode-level edges
|
|
|
|
for (auto &bb: graph_) {
|
|
|
|
if (bb.isDead) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-06-30 02:22:52 +00:00
|
|
|
if (ShouldBeDead(bb)) {
|
2022-07-22 09:12:45 +00:00
|
|
|
bb.UpdateTryCatchInfoForDeadBlock();
|
2022-06-30 02:22:52 +00:00
|
|
|
bb.isDead = true;
|
|
|
|
continue;
|
|
|
|
}
|
2022-07-22 09:12:45 +00:00
|
|
|
bool noThrow = true;
|
|
|
|
EnumerateBlock(bb, [&noThrow, &bb]
|
2022-10-27 03:56:58 +00:00
|
|
|
(const BytecodeInfo &bytecodeInfo) -> bool {
|
2022-02-17 03:48:40 +00:00
|
|
|
if (bytecodeInfo.IsGeneral()) {
|
2022-07-22 09:12:45 +00:00
|
|
|
noThrow = false;
|
2022-04-24 17:17:29 +00:00
|
|
|
if (!bb.catchs.empty()) {
|
|
|
|
bb.catchs.at(0)->numOfStatePreds++;
|
|
|
|
}
|
|
|
|
}
|
2022-06-30 02:22:52 +00:00
|
|
|
if (bytecodeInfo.IsCondJump() && bb.succs.size() == 1) {
|
|
|
|
ASSERT(bb.succs[0]->id == bb.id + 1);
|
|
|
|
bb.succs[0]->numOfStatePreds++;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
2022-07-22 09:12:45 +00:00
|
|
|
bb.UpdateRedundantTryCatchInfo(noThrow);
|
|
|
|
bb.UpdateTryCatchInfoIfNoThrow(noThrow);
|
2022-04-24 17:17:29 +00:00
|
|
|
for (auto &succ: bb.succs) {
|
|
|
|
succ->numOfStatePreds++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// collect loopback edges
|
|
|
|
std::vector<VisitState> visitState(graph_.size(), VisitState::UNVISITED);
|
|
|
|
std::function<void(size_t)> dfs = [&](size_t bbId) -> void {
|
|
|
|
visitState[bbId] = VisitState::PENDING;
|
2022-06-30 02:22:52 +00:00
|
|
|
std::vector<BytecodeRegion *> merge;
|
2022-09-07 09:46:52 +00:00
|
|
|
merge.insert(merge.end(), graph_[bbId].succs.begin(), graph_[bbId].succs.end());
|
|
|
|
merge.insert(merge.end(), graph_[bbId].catchs.begin(), graph_[bbId].catchs.end());
|
2022-06-30 02:22:52 +00:00
|
|
|
auto it = merge.crbegin();
|
|
|
|
while (it != merge.crend()) {
|
2022-04-24 17:17:29 +00:00
|
|
|
auto succBlock = *it;
|
|
|
|
it++;
|
|
|
|
if (visitState[succBlock->id] == VisitState::UNVISITED) {
|
|
|
|
dfs(succBlock->id);
|
|
|
|
} else {
|
|
|
|
if (visitState[succBlock->id] == VisitState::PENDING) {
|
2022-09-07 09:46:52 +00:00
|
|
|
graph_[succBlock->id].loopbackBlocks.insert(bbId);
|
2022-04-24 17:17:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
visitState[bbId] = VisitState::VISITED;
|
|
|
|
};
|
|
|
|
dfs(graph_[0].id);
|
|
|
|
for (auto &bb: graph_) {
|
|
|
|
if (bb.isDead) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
bb.phiAcc = (bb.numOfStatePreds > 1) || (!bb.trys.empty());
|
|
|
|
bb.numOfLoopBacks = bb.loopbackBlocks.size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BytecodeCircuitBuilder::NewMerge(GateRef &state, GateRef &depend, size_t numOfIns)
|
|
|
|
{
|
|
|
|
state = circuit_.NewGate(OpCode(OpCode::MERGE), numOfIns,
|
|
|
|
std::vector<GateRef>(numOfIns, Circuit::NullGate()),
|
2022-06-08 11:50:21 +00:00
|
|
|
GateType::Empty());
|
2022-04-24 17:17:29 +00:00
|
|
|
depend = circuit_.NewGate(OpCode(OpCode::DEPEND_SELECTOR), numOfIns,
|
|
|
|
std::vector<GateRef>(numOfIns + 1, Circuit::NullGate()),
|
2022-06-08 11:50:21 +00:00
|
|
|
GateType::Empty());
|
2022-07-19 12:44:46 +00:00
|
|
|
gateAcc_.NewIn(depend, 0, state);
|
2022-04-24 17:17:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BytecodeCircuitBuilder::NewLoopBegin(BytecodeRegion &bb)
|
|
|
|
{
|
2022-10-09 09:40:04 +00:00
|
|
|
if (bb.id == 0 && bb.numOfStatePreds == 1) {
|
|
|
|
bb.mergeForwardEdges = circuit_.NewGate(OpCode(OpCode::MERGE),
|
|
|
|
bb.numOfStatePreds, std::vector<GateRef>(bb.numOfStatePreds,
|
|
|
|
Circuit::GetCircuitRoot(OpCode(OpCode::STATE_ENTRY))),
|
|
|
|
GateType::Empty());
|
|
|
|
bb.depForward = circuit_.NewGate(OpCode(OpCode::DEPEND_SELECTOR),
|
|
|
|
bb.numOfStatePreds, std::vector<GateRef>(bb.numOfStatePreds + 1, Circuit::NullGate()), GateType::Empty());
|
|
|
|
gateAcc_.NewIn(bb.depForward, 0, bb.mergeForwardEdges);
|
|
|
|
gateAcc_.NewIn(bb.depForward, 1, Circuit::GetCircuitRoot(OpCode(OpCode::DEPEND_ENTRY)));
|
|
|
|
} else {
|
|
|
|
NewMerge(bb.mergeForwardEdges, bb.depForward, bb.numOfStatePreds - bb.numOfLoopBacks);
|
|
|
|
}
|
2022-04-24 17:17:29 +00:00
|
|
|
NewMerge(bb.mergeLoopBackEdges, bb.depLoopBack, bb.numOfLoopBacks);
|
|
|
|
auto loopBack = circuit_.NewGate(OpCode(OpCode::LOOP_BACK), 0,
|
2022-06-08 11:50:21 +00:00
|
|
|
{bb.mergeLoopBackEdges}, GateType::Empty());
|
2022-04-24 17:17:29 +00:00
|
|
|
bb.stateStart = circuit_.NewGate(OpCode(OpCode::LOOP_BEGIN), 0,
|
2022-06-08 11:50:21 +00:00
|
|
|
{bb.mergeForwardEdges, loopBack}, GateType::Empty());
|
2022-04-24 17:17:29 +00:00
|
|
|
// 2: the number of depend inputs and it is in accord with LOOP_BEGIN
|
|
|
|
bb.dependStart = circuit_.NewGate(OpCode(OpCode::DEPEND_SELECTOR), 2,
|
|
|
|
{bb.stateStart, bb.depForward, bb.depLoopBack},
|
2022-06-08 11:50:21 +00:00
|
|
|
GateType::Empty());
|
2022-04-24 17:17:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BytecodeCircuitBuilder::BuildBlockCircuitHead()
|
|
|
|
{
|
|
|
|
for (auto &bb: graph_) {
|
|
|
|
if (bb.isDead) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (bb.numOfStatePreds == 0) {
|
|
|
|
bb.stateStart = Circuit::GetCircuitRoot(OpCode(OpCode::STATE_ENTRY));
|
|
|
|
bb.dependStart = Circuit::GetCircuitRoot(OpCode(OpCode::DEPEND_ENTRY));
|
|
|
|
} else if (bb.numOfLoopBacks > 0) {
|
|
|
|
NewLoopBegin(bb);
|
|
|
|
} else {
|
|
|
|
NewMerge(bb.stateStart, bb.dependStart, bb.numOfStatePreds);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<GateRef> BytecodeCircuitBuilder::CreateGateInList(const BytecodeInfo &info)
|
|
|
|
{
|
2022-06-10 07:22:21 +00:00
|
|
|
size_t numValueInputs = info.ComputeValueInputCount();
|
2022-04-24 17:17:29 +00:00
|
|
|
const size_t length = 2; // 2: state and depend on input
|
2022-06-10 07:22:21 +00:00
|
|
|
const size_t numBCOffsetInput = info.ComputeBCOffsetInputCount();
|
|
|
|
std::vector<GateRef> inList(length + numValueInputs + numBCOffsetInput, Circuit::NullGate());
|
2022-04-24 17:17:29 +00:00
|
|
|
for (size_t i = 0; i < info.inputs.size(); i++) {
|
2022-10-27 03:56:58 +00:00
|
|
|
auto &input = info.inputs[i];
|
2022-10-17 11:41:53 +00:00
|
|
|
if (std::holds_alternative<ConstDataId>(input)) {
|
|
|
|
if (std::get<ConstDataId>(input).IsStringId()) {
|
2022-10-29 03:02:44 +00:00
|
|
|
tsManager_->AddIndexOrSkippedMethodID(TSManager::SnapshotInfoType::STRING,
|
2022-10-25 03:42:17 +00:00
|
|
|
std::get<ConstDataId>(input).GetId());
|
2022-10-19 01:53:32 +00:00
|
|
|
inList[i + length] = circuit_.GetConstantDataGate(std::get<ConstDataId>(input).CaculateBitField(),
|
|
|
|
GateType::StringType());
|
2022-10-25 03:42:17 +00:00
|
|
|
continue;
|
2022-10-19 01:53:32 +00:00
|
|
|
} else if (std::get<ConstDataId>(input).IsMethodId()) {
|
2022-10-29 03:02:44 +00:00
|
|
|
tsManager_->AddIndexOrSkippedMethodID(TSManager::SnapshotInfoType::METHOD,
|
2022-10-25 03:42:17 +00:00
|
|
|
std::get<ConstDataId>(input).GetId());
|
|
|
|
} else if (std::get<ConstDataId>(input).IsClassLiteraId()) {
|
2022-10-29 03:02:44 +00:00
|
|
|
tsManager_->AddIndexOrSkippedMethodID(TSManager::SnapshotInfoType::CLASS_LITERAL,
|
2022-10-25 03:42:17 +00:00
|
|
|
std::get<ConstDataId>(input).GetId(), recordName_);
|
|
|
|
} else if (std::get<ConstDataId>(input).IsObjectLiteralID()) {
|
2022-10-29 03:02:44 +00:00
|
|
|
tsManager_->AddIndexOrSkippedMethodID(TSManager::SnapshotInfoType::OBJECT_LITERAL,
|
2022-10-25 03:42:17 +00:00
|
|
|
std::get<ConstDataId>(input).GetId(), recordName_);
|
|
|
|
} else if (std::get<ConstDataId>(input).IsArrayLiteralID()) {
|
2022-10-29 03:02:44 +00:00
|
|
|
tsManager_->AddIndexOrSkippedMethodID(TSManager::SnapshotInfoType::ARRAY_LITERAL,
|
2022-10-25 03:42:17 +00:00
|
|
|
std::get<ConstDataId>(input).GetId(), recordName_);
|
|
|
|
}
|
|
|
|
inList[i + length] = circuit_.GetConstantGate(MachineType::I64,
|
2022-10-19 01:53:32 +00:00
|
|
|
std::get<ConstDataId>(input).GetId(),
|
|
|
|
GateType::NJSValue());
|
2022-04-24 17:17:29 +00:00
|
|
|
} else if (std::holds_alternative<Immediate>(input)) {
|
2022-09-26 13:32:52 +00:00
|
|
|
inList[i + length] = circuit_.GetConstantGate(MachineType::I64,
|
|
|
|
std::get<Immediate>(input).GetValue(),
|
|
|
|
GateType::NJSValue());
|
2022-10-24 11:32:00 +00:00
|
|
|
} else if (std::holds_alternative<ICSlotId>(input)) {
|
|
|
|
inList[i + length] = circuit_.GetConstantGate(MachineType::I16,
|
|
|
|
std::get<ICSlotId>(input).GetId(),
|
|
|
|
GateType::NJSValue());
|
2022-04-24 17:17:29 +00:00
|
|
|
} else {
|
|
|
|
ASSERT(std::holds_alternative<VirtualRegister>(input));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return inList;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BytecodeCircuitBuilder::SetBlockPred(BytecodeRegion &bbNext, const GateRef &state,
|
|
|
|
const GateRef &depend, bool isLoopBack)
|
|
|
|
{
|
|
|
|
if (bbNext.numOfLoopBacks == 0) {
|
2022-07-19 12:44:46 +00:00
|
|
|
gateAcc_.NewIn(bbNext.stateStart, bbNext.statePredIndex, state);
|
|
|
|
gateAcc_.NewIn(bbNext.dependStart, bbNext.statePredIndex + 1, depend);
|
2022-04-24 17:17:29 +00:00
|
|
|
} else {
|
|
|
|
if (isLoopBack) {
|
2022-07-19 12:44:46 +00:00
|
|
|
gateAcc_.NewIn(bbNext.mergeLoopBackEdges, bbNext.loopBackIndex, state);
|
|
|
|
gateAcc_.NewIn(bbNext.depLoopBack, bbNext.loopBackIndex + 1, depend);
|
2022-04-24 17:17:29 +00:00
|
|
|
bbNext.loopBackIndex++;
|
|
|
|
ASSERT(bbNext.loopBackIndex <= bbNext.numOfLoopBacks);
|
|
|
|
} else {
|
2022-07-19 12:44:46 +00:00
|
|
|
gateAcc_.NewIn(bbNext.mergeForwardEdges, bbNext.forwardIndex, state);
|
|
|
|
gateAcc_.NewIn(bbNext.depForward, bbNext.forwardIndex + 1, depend);
|
2022-04-24 17:17:29 +00:00
|
|
|
bbNext.forwardIndex++;
|
|
|
|
ASSERT(bbNext.forwardIndex <= bbNext.numOfStatePreds - bbNext.numOfLoopBacks);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bbNext.statePredIndex++;
|
|
|
|
ASSERT(bbNext.statePredIndex <= bbNext.numOfStatePreds);
|
|
|
|
}
|
|
|
|
|
|
|
|
GateRef BytecodeCircuitBuilder::NewConst(const BytecodeInfo &info)
|
Add information such as instruction immediate, method_id, string_id, etc.
The lowering slow path requires not only the vreg information of the
instruction, but also other information such as string id, method id, etc.
For each bytecode instruction, collect the various information stored in
the instruction, and use the information as the input of the gate, and
in the lowering stage, take out the required information from the gate.
issue:https://gitee.com/openharmony/ark_js_runtime/issues/I4WQR5
Signed-off-by: wanyanglan <wanyanglan1@huawei.com>
Change-Id: I42ef36554b0b88ce3e1cd0f593e8ce9e924b83e0
2022-03-04 07:52:07 +00:00
|
|
|
{
|
2022-10-27 03:56:58 +00:00
|
|
|
auto opcode = info.GetOpcode();
|
Add information such as instruction immediate, method_id, string_id, etc.
The lowering slow path requires not only the vreg information of the
instruction, but also other information such as string id, method id, etc.
For each bytecode instruction, collect the various information stored in
the instruction, and use the information as the input of the gate, and
in the lowering stage, take out the required information from the gate.
issue:https://gitee.com/openharmony/ark_js_runtime/issues/I4WQR5
Signed-off-by: wanyanglan <wanyanglan1@huawei.com>
Change-Id: I42ef36554b0b88ce3e1cd0f593e8ce9e924b83e0
2022-03-04 07:52:07 +00:00
|
|
|
GateRef gate = 0;
|
|
|
|
switch (opcode) {
|
2022-09-08 01:37:53 +00:00
|
|
|
case EcmaOpcode::LDNAN:
|
2022-09-26 13:32:52 +00:00
|
|
|
gate = circuit_.GetConstantGate(MachineType::I64,
|
|
|
|
base::NumberHelper::GetNaN(),
|
|
|
|
GateType::TaggedValue());
|
Add information such as instruction immediate, method_id, string_id, etc.
The lowering slow path requires not only the vreg information of the
instruction, but also other information such as string id, method id, etc.
For each bytecode instruction, collect the various information stored in
the instruction, and use the information as the input of the gate, and
in the lowering stage, take out the required information from the gate.
issue:https://gitee.com/openharmony/ark_js_runtime/issues/I4WQR5
Signed-off-by: wanyanglan <wanyanglan1@huawei.com>
Change-Id: I42ef36554b0b88ce3e1cd0f593e8ce9e924b83e0
2022-03-04 07:52:07 +00:00
|
|
|
break;
|
2022-09-08 01:37:53 +00:00
|
|
|
case EcmaOpcode::LDINFINITY:
|
2022-09-26 13:32:52 +00:00
|
|
|
gate = circuit_.GetConstantGate(MachineType::I64,
|
|
|
|
base::NumberHelper::GetPositiveInfinity(),
|
|
|
|
GateType::TaggedValue());
|
Add information such as instruction immediate, method_id, string_id, etc.
The lowering slow path requires not only the vreg information of the
instruction, but also other information such as string id, method id, etc.
For each bytecode instruction, collect the various information stored in
the instruction, and use the information as the input of the gate, and
in the lowering stage, take out the required information from the gate.
issue:https://gitee.com/openharmony/ark_js_runtime/issues/I4WQR5
Signed-off-by: wanyanglan <wanyanglan1@huawei.com>
Change-Id: I42ef36554b0b88ce3e1cd0f593e8ce9e924b83e0
2022-03-04 07:52:07 +00:00
|
|
|
break;
|
2022-09-08 01:37:53 +00:00
|
|
|
case EcmaOpcode::LDUNDEFINED:
|
2022-09-26 13:32:52 +00:00
|
|
|
gate = circuit_.GetConstantGate(MachineType::I64,
|
|
|
|
JSTaggedValue::VALUE_UNDEFINED,
|
|
|
|
GateType::TaggedValue());
|
Add information such as instruction immediate, method_id, string_id, etc.
The lowering slow path requires not only the vreg information of the
instruction, but also other information such as string id, method id, etc.
For each bytecode instruction, collect the various information stored in
the instruction, and use the information as the input of the gate, and
in the lowering stage, take out the required information from the gate.
issue:https://gitee.com/openharmony/ark_js_runtime/issues/I4WQR5
Signed-off-by: wanyanglan <wanyanglan1@huawei.com>
Change-Id: I42ef36554b0b88ce3e1cd0f593e8ce9e924b83e0
2022-03-04 07:52:07 +00:00
|
|
|
break;
|
2022-09-08 01:37:53 +00:00
|
|
|
case EcmaOpcode::LDNULL:
|
2022-09-26 13:32:52 +00:00
|
|
|
gate = circuit_.GetConstantGate(MachineType::I64,
|
|
|
|
JSTaggedValue::VALUE_NULL,
|
|
|
|
GateType::TaggedValue());
|
Add information such as instruction immediate, method_id, string_id, etc.
The lowering slow path requires not only the vreg information of the
instruction, but also other information such as string id, method id, etc.
For each bytecode instruction, collect the various information stored in
the instruction, and use the information as the input of the gate, and
in the lowering stage, take out the required information from the gate.
issue:https://gitee.com/openharmony/ark_js_runtime/issues/I4WQR5
Signed-off-by: wanyanglan <wanyanglan1@huawei.com>
Change-Id: I42ef36554b0b88ce3e1cd0f593e8ce9e924b83e0
2022-03-04 07:52:07 +00:00
|
|
|
break;
|
2022-09-08 01:37:53 +00:00
|
|
|
case EcmaOpcode::LDTRUE:
|
2022-09-26 13:32:52 +00:00
|
|
|
gate = circuit_.GetConstantGate(MachineType::I64,
|
|
|
|
JSTaggedValue::VALUE_TRUE,
|
|
|
|
GateType::TaggedValue());
|
Add information such as instruction immediate, method_id, string_id, etc.
The lowering slow path requires not only the vreg information of the
instruction, but also other information such as string id, method id, etc.
For each bytecode instruction, collect the various information stored in
the instruction, and use the information as the input of the gate, and
in the lowering stage, take out the required information from the gate.
issue:https://gitee.com/openharmony/ark_js_runtime/issues/I4WQR5
Signed-off-by: wanyanglan <wanyanglan1@huawei.com>
Change-Id: I42ef36554b0b88ce3e1cd0f593e8ce9e924b83e0
2022-03-04 07:52:07 +00:00
|
|
|
break;
|
2022-09-08 01:37:53 +00:00
|
|
|
case EcmaOpcode::LDFALSE:
|
2022-09-26 13:32:52 +00:00
|
|
|
gate = circuit_.GetConstantGate(MachineType::I64,
|
|
|
|
JSTaggedValue::VALUE_FALSE,
|
|
|
|
GateType::TaggedValue());
|
Add information such as instruction immediate, method_id, string_id, etc.
The lowering slow path requires not only the vreg information of the
instruction, but also other information such as string id, method id, etc.
For each bytecode instruction, collect the various information stored in
the instruction, and use the information as the input of the gate, and
in the lowering stage, take out the required information from the gate.
issue:https://gitee.com/openharmony/ark_js_runtime/issues/I4WQR5
Signed-off-by: wanyanglan <wanyanglan1@huawei.com>
Change-Id: I42ef36554b0b88ce3e1cd0f593e8ce9e924b83e0
2022-03-04 07:52:07 +00:00
|
|
|
break;
|
2022-09-08 01:37:53 +00:00
|
|
|
case EcmaOpcode::LDHOLE:
|
2022-09-26 13:32:52 +00:00
|
|
|
gate = circuit_.GetConstantGate(MachineType::I64,
|
|
|
|
JSTaggedValue::VALUE_HOLE,
|
|
|
|
GateType::TaggedValue());
|
Add information such as instruction immediate, method_id, string_id, etc.
The lowering slow path requires not only the vreg information of the
instruction, but also other information such as string id, method id, etc.
For each bytecode instruction, collect the various information stored in
the instruction, and use the information as the input of the gate, and
in the lowering stage, take out the required information from the gate.
issue:https://gitee.com/openharmony/ark_js_runtime/issues/I4WQR5
Signed-off-by: wanyanglan <wanyanglan1@huawei.com>
Change-Id: I42ef36554b0b88ce3e1cd0f593e8ce9e924b83e0
2022-03-04 07:52:07 +00:00
|
|
|
break;
|
2022-09-08 01:37:53 +00:00
|
|
|
case EcmaOpcode::LDAI_IMM32:
|
2022-09-26 13:32:52 +00:00
|
|
|
gate = circuit_.GetConstantGate(MachineType::I64,
|
|
|
|
std::get<Immediate>(info.inputs[0]).ToJSTaggedValueInt(),
|
|
|
|
GateType::TaggedValue());
|
Add information such as instruction immediate, method_id, string_id, etc.
The lowering slow path requires not only the vreg information of the
instruction, but also other information such as string id, method id, etc.
For each bytecode instruction, collect the various information stored in
the instruction, and use the information as the input of the gate, and
in the lowering stage, take out the required information from the gate.
issue:https://gitee.com/openharmony/ark_js_runtime/issues/I4WQR5
Signed-off-by: wanyanglan <wanyanglan1@huawei.com>
Change-Id: I42ef36554b0b88ce3e1cd0f593e8ce9e924b83e0
2022-03-04 07:52:07 +00:00
|
|
|
break;
|
2022-09-08 01:37:53 +00:00
|
|
|
case EcmaOpcode::FLDAI_IMM64:
|
2022-09-26 13:32:52 +00:00
|
|
|
gate = circuit_.GetConstantGate(MachineType::I64,
|
|
|
|
std::get<Immediate>(info.inputs.at(0)).ToJSTaggedValueDouble(),
|
|
|
|
GateType::TaggedValue());
|
Add information such as instruction immediate, method_id, string_id, etc.
The lowering slow path requires not only the vreg information of the
instruction, but also other information such as string id, method id, etc.
For each bytecode instruction, collect the various information stored in
the instruction, and use the information as the input of the gate, and
in the lowering stage, take out the required information from the gate.
issue:https://gitee.com/openharmony/ark_js_runtime/issues/I4WQR5
Signed-off-by: wanyanglan <wanyanglan1@huawei.com>
Change-Id: I42ef36554b0b88ce3e1cd0f593e8ce9e924b83e0
2022-03-04 07:52:07 +00:00
|
|
|
break;
|
2022-09-08 01:37:53 +00:00
|
|
|
case EcmaOpcode::LDFUNCTION:
|
2022-06-16 09:27:35 +00:00
|
|
|
gate = argAcc_.GetCommonArgGate(CommonArgIdx::FUNC);
|
2022-04-06 07:17:04 +00:00
|
|
|
break;
|
2022-09-09 07:35:02 +00:00
|
|
|
case EcmaOpcode::LDNEWTARGET:
|
|
|
|
gate = argAcc_.GetCommonArgGate(CommonArgIdx::NEW_TARGET);
|
|
|
|
break;
|
|
|
|
case EcmaOpcode::LDTHIS:
|
2022-10-15 04:09:38 +00:00
|
|
|
gate = argAcc_.GetCommonArgGate(CommonArgIdx::THIS_OBJECT);
|
2022-09-09 07:35:02 +00:00
|
|
|
break;
|
2022-10-17 11:41:53 +00:00
|
|
|
case EcmaOpcode::LDA_STR_ID16: {
|
|
|
|
auto input = std::get<ConstDataId>(info.inputs.at(0));
|
|
|
|
if (input.IsStringId()) {
|
2022-10-29 03:02:44 +00:00
|
|
|
tsManager_->AddIndexOrSkippedMethodID(TSManager::SnapshotInfoType::STRING, input.GetId());
|
2022-10-17 11:41:53 +00:00
|
|
|
}
|
|
|
|
gate = circuit_.GetConstantDataGate(input.CaculateBitField(), GateType::StringType());
|
|
|
|
break;
|
|
|
|
}
|
Add information such as instruction immediate, method_id, string_id, etc.
The lowering slow path requires not only the vreg information of the
instruction, but also other information such as string id, method id, etc.
For each bytecode instruction, collect the various information stored in
the instruction, and use the information as the input of the gate, and
in the lowering stage, take out the required information from the gate.
issue:https://gitee.com/openharmony/ark_js_runtime/issues/I4WQR5
Signed-off-by: wanyanglan <wanyanglan1@huawei.com>
Change-Id: I42ef36554b0b88ce3e1cd0f593e8ce9e924b83e0
2022-03-04 07:52:07 +00:00
|
|
|
default:
|
2022-04-11 02:41:49 +00:00
|
|
|
UNREACHABLE();
|
Add information such as instruction immediate, method_id, string_id, etc.
The lowering slow path requires not only the vreg information of the
instruction, but also other information such as string id, method id, etc.
For each bytecode instruction, collect the various information stored in
the instruction, and use the information as the input of the gate, and
in the lowering stage, take out the required information from the gate.
issue:https://gitee.com/openharmony/ark_js_runtime/issues/I4WQR5
Signed-off-by: wanyanglan <wanyanglan1@huawei.com>
Change-Id: I42ef36554b0b88ce3e1cd0f593e8ce9e924b83e0
2022-03-04 07:52:07 +00:00
|
|
|
}
|
|
|
|
return gate;
|
|
|
|
}
|
|
|
|
|
2022-10-27 03:56:58 +00:00
|
|
|
void BytecodeCircuitBuilder::NewJSGate(BytecodeRegion &bb, GateRef &state, GateRef &depend)
|
2022-01-18 04:02:20 +00:00
|
|
|
{
|
2022-10-27 03:56:58 +00:00
|
|
|
auto &iterator = bb.GetBytecodeIterator();
|
|
|
|
const BytecodeInfo& bytecodeInfo = iterator.GetBytecodeInfo();
|
2022-10-27 09:12:44 +00:00
|
|
|
auto pc = bytecodeInfo.GetPC();
|
2022-06-10 07:22:21 +00:00
|
|
|
size_t numValueInputs = bytecodeInfo.ComputeTotalValueCount();
|
2022-04-24 17:17:29 +00:00
|
|
|
GateRef gate = 0;
|
|
|
|
std::vector<GateRef> inList = CreateGateInList(bytecodeInfo);
|
2022-10-07 23:56:43 +00:00
|
|
|
if (bytecodeInfo.IsDef()) {
|
2022-04-24 17:17:29 +00:00
|
|
|
gate = circuit_.NewGate(OpCode(OpCode::JS_BYTECODE), MachineType::I64, numValueInputs,
|
2022-06-08 11:50:21 +00:00
|
|
|
inList, GateType::AnyType());
|
2022-04-24 17:17:29 +00:00
|
|
|
} else {
|
|
|
|
gate = circuit_.NewGate(OpCode(OpCode::JS_BYTECODE), MachineType::NOVALUE, numValueInputs,
|
2022-06-08 11:50:21 +00:00
|
|
|
inList, GateType::Empty());
|
2022-04-14 08:14:34 +00:00
|
|
|
}
|
2022-06-10 07:22:21 +00:00
|
|
|
// 1: store bcoffset in the end.
|
|
|
|
AddBytecodeOffsetInfo(gate, bytecodeInfo, numValueInputs + 1, const_cast<uint8_t *>(pc));
|
2022-07-19 12:44:46 +00:00
|
|
|
gateAcc_.NewIn(gate, 0, state);
|
|
|
|
gateAcc_.NewIn(gate, 1, depend);
|
2022-06-08 11:50:21 +00:00
|
|
|
auto ifSuccess = circuit_.NewGate(OpCode(OpCode::IF_SUCCESS), 0, {gate}, GateType::Empty());
|
|
|
|
auto ifException = circuit_.NewGate(OpCode(OpCode::IF_EXCEPTION), 0, {gate}, GateType::Empty());
|
2022-04-24 17:17:29 +00:00
|
|
|
if (!bb.catchs.empty()) {
|
|
|
|
auto &bbNext = bb.catchs.at(0);
|
|
|
|
auto isLoopBack = bbNext->loopbackBlocks.count(bb.id);
|
|
|
|
SetBlockPred(*bbNext, ifException, gate, isLoopBack);
|
2022-10-27 03:56:58 +00:00
|
|
|
if (bytecodeInfo.GetOpcode() == EcmaOpcode::CREATEASYNCGENERATOROBJ_V8) {
|
2022-10-27 09:12:44 +00:00
|
|
|
bbNext->expandedPreds.push_back({bb.id, iterator.Index() + 1, true}); // 1: next pc
|
2022-09-29 06:18:49 +00:00
|
|
|
} else {
|
2022-10-27 03:56:58 +00:00
|
|
|
bbNext->expandedPreds.push_back({bb.id, iterator.Index(), true});
|
2022-09-29 06:18:49 +00:00
|
|
|
}
|
2022-04-24 17:17:29 +00:00
|
|
|
} else {
|
2022-09-26 13:32:52 +00:00
|
|
|
auto constant = circuit_.GetConstantGate(MachineType::I64,
|
|
|
|
JSTaggedValue::VALUE_EXCEPTION,
|
|
|
|
GateType::TaggedValue());
|
2022-04-24 17:17:29 +00:00
|
|
|
circuit_.NewGate(OpCode(OpCode::RETURN), 0,
|
|
|
|
{ifException, gate, constant,
|
|
|
|
Circuit::GetCircuitRoot(OpCode(OpCode::RETURN_LIST))},
|
2022-09-26 13:32:52 +00:00
|
|
|
GateType::Empty());
|
2022-02-12 08:15:41 +00:00
|
|
|
}
|
2022-10-27 03:56:58 +00:00
|
|
|
jsgateToBytecode_[gate] = { bb.id, iterator.Index() };
|
2022-09-26 13:32:52 +00:00
|
|
|
byteCodeToJSGate_[pc] = gate;
|
2022-06-06 09:41:17 +00:00
|
|
|
if (bytecodeInfo.IsGeneratorRelative()) {
|
|
|
|
suspendAndResumeGates_.emplace_back(gate);
|
|
|
|
}
|
2022-02-17 03:48:40 +00:00
|
|
|
if (bytecodeInfo.IsThrow()) {
|
2022-09-26 13:32:52 +00:00
|
|
|
auto constant = circuit_.GetConstantGate(MachineType::I64,
|
|
|
|
JSTaggedValue::VALUE_EXCEPTION,
|
|
|
|
GateType::TaggedValue());
|
2022-04-24 17:17:29 +00:00
|
|
|
circuit_.NewGate(OpCode(OpCode::RETURN), 0,
|
|
|
|
{ifSuccess, gate, constant,
|
|
|
|
Circuit::GetCircuitRoot(OpCode(OpCode::RETURN_LIST))},
|
2022-09-26 13:32:52 +00:00
|
|
|
GateType::Empty());
|
2022-04-24 17:17:29 +00:00
|
|
|
return;
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
2022-04-24 17:17:29 +00:00
|
|
|
state = ifSuccess;
|
|
|
|
depend = gate;
|
|
|
|
if (pc == bb.end) {
|
|
|
|
auto &bbNext = graph_[bb.id + 1];
|
|
|
|
auto isLoopBack = bbNext.loopbackBlocks.count(bb.id);
|
|
|
|
SetBlockPred(bbNext, state, depend, isLoopBack);
|
2022-10-27 03:56:58 +00:00
|
|
|
bbNext.expandedPreds.push_back({bb.id, iterator.Index(), false});
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
2022-04-24 17:17:29 +00:00
|
|
|
}
|
|
|
|
|
2022-10-27 03:56:58 +00:00
|
|
|
void BytecodeCircuitBuilder::NewJump(BytecodeRegion &bb, GateRef &state, GateRef &depend)
|
2022-04-24 17:17:29 +00:00
|
|
|
{
|
2022-10-27 03:56:58 +00:00
|
|
|
auto &iterator = bb.GetBytecodeIterator();
|
|
|
|
const BytecodeInfo& bytecodeInfo = iterator.GetBytecodeInfo();
|
2022-10-27 09:12:44 +00:00
|
|
|
auto pc = bytecodeInfo.GetPC();
|
2022-06-10 07:22:21 +00:00
|
|
|
size_t numValueInputs = bytecodeInfo.ComputeValueInputCount();
|
2022-02-17 03:48:40 +00:00
|
|
|
if (bytecodeInfo.IsCondJump()) {
|
2022-04-24 17:17:29 +00:00
|
|
|
GateRef gate = 0;
|
|
|
|
gate = circuit_.NewGate(OpCode(OpCode::JS_BYTECODE), MachineType::NOVALUE, numValueInputs,
|
|
|
|
std::vector<GateRef>(2 + numValueInputs, // 2: state and depend input
|
|
|
|
Circuit::NullGate()),
|
2022-06-08 11:50:21 +00:00
|
|
|
GateType::Empty());
|
2022-07-19 12:44:46 +00:00
|
|
|
gateAcc_.NewIn(gate, 0, state);
|
|
|
|
gateAcc_.NewIn(gate, 1, depend);
|
2022-06-08 11:50:21 +00:00
|
|
|
auto ifTrue = circuit_.NewGate(OpCode(OpCode::IF_TRUE), 0, {gate}, GateType::Empty());
|
2022-10-17 11:26:13 +00:00
|
|
|
auto trueRelay = circuit_.NewGate(OpCode(OpCode::DEPEND_RELAY), 0, {ifTrue, gate}, GateType::Empty());
|
2022-06-08 11:50:21 +00:00
|
|
|
auto ifFalse = circuit_.NewGate(OpCode(OpCode::IF_FALSE), 0, {gate}, GateType::Empty());
|
2022-10-17 11:26:13 +00:00
|
|
|
auto falseRelay = circuit_.NewGate(OpCode(OpCode::DEPEND_RELAY), 0, {ifFalse, gate}, GateType::Empty());
|
2022-06-30 02:22:52 +00:00
|
|
|
if (bb.succs.size() == 1) {
|
|
|
|
auto &bbNext = bb.succs[0];
|
|
|
|
ASSERT(bbNext->id == bb.id + 1);
|
|
|
|
auto isLoopBack = bbNext->loopbackBlocks.count(bb.id);
|
2022-10-17 11:26:13 +00:00
|
|
|
SetBlockPred(*bbNext, ifFalse, trueRelay, isLoopBack);
|
|
|
|
SetBlockPred(*bbNext, ifTrue, falseRelay, isLoopBack);
|
2022-10-27 03:56:58 +00:00
|
|
|
bbNext->expandedPreds.push_back({bb.id, iterator.Index(), false});
|
2022-06-30 02:22:52 +00:00
|
|
|
} else {
|
|
|
|
ASSERT(bb.succs.size() == 2); // 2 : 2 num of successors
|
2022-08-04 07:37:10 +00:00
|
|
|
[[maybe_unused]] uint32_t bitSet = 0;
|
2022-06-30 02:22:52 +00:00
|
|
|
for (auto &bbNext: bb.succs) {
|
|
|
|
if (bbNext->id == bb.id + 1) {
|
|
|
|
auto isLoopBack = bbNext->loopbackBlocks.count(bb.id);
|
2022-10-17 11:26:13 +00:00
|
|
|
SetBlockPred(*bbNext, ifFalse, falseRelay, isLoopBack);
|
2022-10-27 03:56:58 +00:00
|
|
|
bbNext->expandedPreds.push_back({bb.id, iterator.Index(), false});
|
2022-06-30 02:22:52 +00:00
|
|
|
bitSet |= 1;
|
|
|
|
} else {
|
|
|
|
auto isLoopBack = bbNext->loopbackBlocks.count(bb.id);
|
2022-10-17 11:26:13 +00:00
|
|
|
SetBlockPred(*bbNext, ifTrue, trueRelay, isLoopBack);
|
2022-10-27 03:56:58 +00:00
|
|
|
bbNext->expandedPreds.push_back({bb.id, iterator.Index(), false});
|
2022-06-30 02:22:52 +00:00
|
|
|
bitSet |= 2; // 2:verify
|
|
|
|
}
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
2022-06-30 02:22:52 +00:00
|
|
|
ASSERT(bitSet == 3); // 3:Verify the number of successor blocks
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
2022-10-27 03:56:58 +00:00
|
|
|
jsgateToBytecode_[gate] = { bb.id, iterator.Index() };
|
2022-09-26 13:32:52 +00:00
|
|
|
byteCodeToJSGate_[pc] = gate;
|
2022-04-24 17:17:29 +00:00
|
|
|
} else {
|
|
|
|
ASSERT(bb.succs.size() == 1);
|
|
|
|
auto &bbNext = bb.succs.at(0);
|
|
|
|
auto isLoopBack = bbNext->loopbackBlocks.count(bb.id);
|
|
|
|
SetBlockPred(*bbNext, state, depend, isLoopBack);
|
2022-10-27 03:56:58 +00:00
|
|
|
bbNext->expandedPreds.push_back({bb.id, iterator.Index(), false});
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
2022-04-24 17:17:29 +00:00
|
|
|
}
|
2022-01-19 01:11:02 +00:00
|
|
|
|
2022-10-27 03:56:58 +00:00
|
|
|
void BytecodeCircuitBuilder::NewReturn(BytecodeRegion &bb, GateRef &state, GateRef &depend)
|
2022-04-24 17:17:29 +00:00
|
|
|
{
|
|
|
|
ASSERT(bb.succs.empty());
|
2022-10-27 03:56:58 +00:00
|
|
|
auto &iterator = bb.GetBytecodeIterator();
|
|
|
|
const BytecodeInfo& bytecodeInfo = iterator.GetBytecodeInfo();
|
2022-10-27 09:12:44 +00:00
|
|
|
auto pc = bytecodeInfo.GetPC();
|
2022-10-27 03:56:58 +00:00
|
|
|
if (bytecodeInfo.GetOpcode() == EcmaOpcode::RETURN) {
|
2022-09-06 05:27:19 +00:00
|
|
|
// handle return.dyn bytecode
|
2022-04-24 17:17:29 +00:00
|
|
|
auto gate = circuit_.NewGate(OpCode(OpCode::RETURN), 0,
|
|
|
|
{ state, depend, Circuit::NullGate(),
|
|
|
|
Circuit::GetCircuitRoot(OpCode(OpCode::RETURN_LIST)) },
|
2022-09-26 13:32:52 +00:00
|
|
|
GateType::Empty());
|
2022-10-27 03:56:58 +00:00
|
|
|
jsgateToBytecode_[gate] = { bb.id, iterator.Index() };
|
2022-09-26 13:32:52 +00:00
|
|
|
byteCodeToJSGate_[pc] = gate;
|
2022-10-27 03:56:58 +00:00
|
|
|
} else if (bytecodeInfo.GetOpcode() == EcmaOpcode::RETURNUNDEFINED) {
|
2022-04-24 17:17:29 +00:00
|
|
|
// handle returnundefined bytecode
|
2022-09-26 13:32:52 +00:00
|
|
|
auto constant = circuit_.GetConstantGate(MachineType::I64,
|
|
|
|
JSTaggedValue::VALUE_UNDEFINED,
|
|
|
|
GateType::TaggedValue());
|
2022-04-24 17:17:29 +00:00
|
|
|
auto gate = circuit_.NewGate(OpCode(OpCode::RETURN), 0,
|
|
|
|
{ state, depend, constant,
|
|
|
|
Circuit::GetCircuitRoot(OpCode(OpCode::RETURN_LIST)) },
|
2022-09-26 13:32:52 +00:00
|
|
|
GateType::Empty());
|
2022-10-27 03:56:58 +00:00
|
|
|
jsgateToBytecode_[gate] = { bb.id, iterator.Index() };
|
2022-09-26 13:32:52 +00:00
|
|
|
byteCodeToJSGate_[pc] = gate;
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
2022-04-24 17:17:29 +00:00
|
|
|
}
|
|
|
|
|
2022-10-27 03:56:58 +00:00
|
|
|
void BytecodeCircuitBuilder::NewByteCode(BytecodeRegion &bb, GateRef &state, GateRef &depend)
|
2022-04-24 17:17:29 +00:00
|
|
|
{
|
2022-10-27 03:56:58 +00:00
|
|
|
auto &iterator = bb.GetBytecodeIterator();
|
|
|
|
const BytecodeInfo& bytecodeInfo = iterator.GetBytecodeInfo();
|
2022-10-27 09:12:44 +00:00
|
|
|
auto pc = bytecodeInfo.GetPC();
|
2022-02-17 03:48:40 +00:00
|
|
|
if (bytecodeInfo.IsSetConstant()) {
|
2022-04-24 17:17:29 +00:00
|
|
|
// handle bytecode command to get constants
|
|
|
|
GateRef gate = NewConst(bytecodeInfo);
|
2022-10-27 03:56:58 +00:00
|
|
|
jsgateToBytecode_[gate] = { bb.id, iterator.Index() };
|
2022-09-26 13:32:52 +00:00
|
|
|
byteCodeToJSGate_[pc] = gate;
|
2022-06-30 02:22:52 +00:00
|
|
|
if (pc == bb.end) {
|
|
|
|
auto &bbNext = graph_[bb.id + 1];
|
|
|
|
auto isLoopBack = bbNext.loopbackBlocks.count(bb.id);
|
|
|
|
SetBlockPred(bbNext, state, depend, isLoopBack);
|
2022-10-27 03:56:58 +00:00
|
|
|
bbNext.expandedPreds.push_back({bb.id, iterator.Index(), false});
|
2022-06-30 02:22:52 +00:00
|
|
|
}
|
2022-02-17 03:48:40 +00:00
|
|
|
} else if (bytecodeInfo.IsGeneral()) {
|
2022-04-24 17:17:29 +00:00
|
|
|
// handle general ecma.* bytecodes
|
2022-10-27 03:56:58 +00:00
|
|
|
NewJSGate(bb, state, depend);
|
2022-02-17 03:48:40 +00:00
|
|
|
} else if (bytecodeInfo.IsJump()) {
|
2022-04-24 17:17:29 +00:00
|
|
|
// handle conditional jump and unconditional jump bytecodes
|
2022-10-27 03:56:58 +00:00
|
|
|
NewJump(bb, state, depend);
|
2022-02-17 03:48:40 +00:00
|
|
|
} else if (bytecodeInfo.IsReturn()) {
|
2022-09-06 05:27:19 +00:00
|
|
|
// handle return.dyn and returnundefined bytecodes
|
2022-10-27 03:56:58 +00:00
|
|
|
NewReturn(bb, state, depend);
|
2022-02-17 03:48:40 +00:00
|
|
|
} else if (bytecodeInfo.IsMov()) {
|
2022-09-06 05:27:19 +00:00
|
|
|
// handle mov.dyn lda.dyn sta.dyn bytecodes
|
2022-04-24 17:17:29 +00:00
|
|
|
if (pc == bb.end) {
|
|
|
|
auto &bbNext = graph_[bb.id + 1];
|
|
|
|
auto isLoopBack = bbNext.loopbackBlocks.count(bb.id);
|
|
|
|
SetBlockPred(bbNext, state, depend, isLoopBack);
|
2022-10-27 03:56:58 +00:00
|
|
|
bbNext.expandedPreds.push_back({bb.id, iterator.Index(), false});
|
2022-04-24 17:17:29 +00:00
|
|
|
}
|
2022-02-17 03:48:40 +00:00
|
|
|
} else if (bytecodeInfo.IsDiscarded()) {
|
2022-04-24 17:17:29 +00:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BytecodeCircuitBuilder::BuildSubCircuit()
|
|
|
|
{
|
|
|
|
for (auto &bb: graph_) {
|
2022-01-06 02:26:19 +00:00
|
|
|
if (bb.isDead) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto stateCur = bb.stateStart;
|
|
|
|
auto dependCur = bb.dependStart;
|
2022-01-19 01:11:02 +00:00
|
|
|
ASSERT(stateCur != Circuit::NullGate());
|
|
|
|
ASSERT(dependCur != Circuit::NullGate());
|
2022-02-10 02:03:48 +00:00
|
|
|
if (!bb.trys.empty()) {
|
2022-06-08 11:50:21 +00:00
|
|
|
dependCur = circuit_.NewGate(OpCode(OpCode::GET_EXCEPTION), 0, {dependCur}, GateType::Empty());
|
2022-02-10 02:03:48 +00:00
|
|
|
}
|
2022-10-27 03:56:58 +00:00
|
|
|
EnumerateBlock(bb, [this, &stateCur, &dependCur, &bb]
|
|
|
|
(const BytecodeInfo &bytecodeInfo) -> bool {
|
|
|
|
NewByteCode(bb, stateCur, dependCur);
|
2022-02-17 03:48:40 +00:00
|
|
|
if (bytecodeInfo.IsJump() || bytecodeInfo.IsThrow()) {
|
2022-06-30 02:22:52 +00:00
|
|
|
return false;
|
2022-04-24 17:17:29 +00:00
|
|
|
}
|
2022-06-30 02:22:52 +00:00
|
|
|
return true;
|
|
|
|
});
|
2022-04-24 17:17:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-11 19:13:23 +00:00
|
|
|
void BytecodeCircuitBuilder::NewPhi(BytecodeRegion &bb, uint16_t reg, bool acc, GateRef ¤tPhi)
|
2022-04-24 17:17:29 +00:00
|
|
|
{
|
|
|
|
if (bb.numOfLoopBacks == 0) {
|
2022-06-11 19:13:23 +00:00
|
|
|
currentPhi =
|
|
|
|
circuit_.NewGate(OpCode(OpCode::VALUE_SELECTOR), MachineType::I64, bb.numOfStatePreds,
|
|
|
|
std::vector<GateRef>(1 + bb.numOfStatePreds, Circuit::NullGate()), GateType::AnyType());
|
2022-07-19 12:44:46 +00:00
|
|
|
gateAcc_.NewIn(currentPhi, 0, bb.stateStart);
|
2022-04-24 17:17:29 +00:00
|
|
|
for (size_t i = 0; i < bb.numOfStatePreds; ++i) {
|
2022-10-27 03:56:58 +00:00
|
|
|
auto &[predId, predBcIdx, isException] = bb.expandedPreds.at(i);
|
2022-10-27 09:12:44 +00:00
|
|
|
gateAcc_.NewIn(currentPhi, i + 1, ResolveDef(predId, predBcIdx, reg, acc));
|
2022-04-24 17:17:29 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-06-11 19:13:23 +00:00
|
|
|
// 2: the number of value inputs and it is in accord with LOOP_BEGIN
|
|
|
|
currentPhi = circuit_.NewGate(OpCode(OpCode::VALUE_SELECTOR), MachineType::I64, 2,
|
|
|
|
{bb.stateStart, Circuit::NullGate(), Circuit::NullGate()}, GateType::AnyType());
|
|
|
|
auto loopBackValue =
|
|
|
|
circuit_.NewGate(OpCode(OpCode::VALUE_SELECTOR), MachineType::I64, bb.numOfLoopBacks,
|
|
|
|
std::vector<GateRef>(1 + bb.numOfLoopBacks, Circuit::NullGate()), GateType::AnyType());
|
2022-07-19 12:44:46 +00:00
|
|
|
gateAcc_.NewIn(loopBackValue, 0, bb.mergeLoopBackEdges);
|
2022-08-11 11:46:55 +00:00
|
|
|
size_t loopBackIndex = 1; // 1: start index of value inputs
|
2022-04-24 17:17:29 +00:00
|
|
|
for (size_t i = 0; i < bb.numOfStatePreds; ++i) {
|
2022-10-27 03:56:58 +00:00
|
|
|
auto &[predId, predBcIdx, isException] = bb.expandedPreds.at(i);
|
2022-04-24 17:17:29 +00:00
|
|
|
if (bb.loopbackBlocks.count(predId)) {
|
2022-10-27 09:12:44 +00:00
|
|
|
gateAcc_.NewIn(loopBackValue, loopBackIndex++, ResolveDef(predId, predBcIdx, reg, acc));
|
2022-04-24 17:17:29 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-11 19:13:23 +00:00
|
|
|
auto forwardValue = circuit_.NewGate(
|
|
|
|
OpCode(OpCode::VALUE_SELECTOR), MachineType::I64, bb.numOfStatePreds - bb.numOfLoopBacks,
|
|
|
|
std::vector<GateRef>(1 + bb.numOfStatePreds - bb.numOfLoopBacks, Circuit::NullGate()), GateType::AnyType());
|
2022-07-19 12:44:46 +00:00
|
|
|
gateAcc_.NewIn(forwardValue, 0, bb.mergeForwardEdges);
|
2022-08-11 11:46:55 +00:00
|
|
|
size_t forwardIndex = 1; // 1: start index of value inputs
|
2022-04-24 17:17:29 +00:00
|
|
|
for (size_t i = 0; i < bb.numOfStatePreds; ++i) {
|
2022-10-27 03:56:58 +00:00
|
|
|
auto &[predId, predBcIdx, isException] = bb.expandedPreds.at(i);
|
2022-04-24 17:17:29 +00:00
|
|
|
if (!bb.loopbackBlocks.count(predId)) {
|
2022-10-27 09:12:44 +00:00
|
|
|
gateAcc_.NewIn(forwardValue, forwardIndex++, ResolveDef(predId, predBcIdx, reg, acc));
|
2022-04-24 17:17:29 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-19 12:44:46 +00:00
|
|
|
gateAcc_.NewIn(currentPhi, 1, forwardValue); // 1: index of forward value input
|
|
|
|
gateAcc_.NewIn(currentPhi, 2, loopBackValue); // 2: index of loop-back value input
|
2022-04-24 17:17:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// recursive variables renaming algorithm
|
2022-10-27 09:12:44 +00:00
|
|
|
GateRef BytecodeCircuitBuilder::ResolveDef(const size_t bbId, int32_t bcId,
|
2022-10-29 00:58:50 +00:00
|
|
|
const uint16_t reg, const bool acc)
|
2022-04-24 17:17:29 +00:00
|
|
|
{
|
2022-02-17 03:48:40 +00:00
|
|
|
auto tmpReg = reg;
|
2022-04-24 17:17:29 +00:00
|
|
|
// find def-site in bytecodes of basic block
|
|
|
|
auto ans = Circuit::NullGate();
|
|
|
|
auto &bb = graph_.at(bbId);
|
2022-07-05 07:24:00 +00:00
|
|
|
GateType type = GateType::AnyType();
|
2022-02-17 03:48:40 +00:00
|
|
|
auto tmpAcc = acc;
|
2022-10-27 03:56:58 +00:00
|
|
|
auto &iterator = bb.GetBytecodeIterator();
|
|
|
|
for (iterator.Goto(bcId); !iterator.Done(); --iterator) {
|
|
|
|
const BytecodeInfo& curInfo = iterator.GetBytecodeInfo();
|
2022-10-27 09:12:44 +00:00
|
|
|
auto pcIter = curInfo.GetPC();
|
2022-05-26 04:54:21 +00:00
|
|
|
// original bc use acc as input && current bc use acc as output
|
2022-10-27 03:56:58 +00:00
|
|
|
bool isTransByAcc = tmpAcc && curInfo.AccOut();
|
2022-05-26 04:54:21 +00:00
|
|
|
// 0 : the index in vreg-out list
|
|
|
|
bool isTransByVreg = (!tmpAcc && curInfo.IsOut(tmpReg, 0));
|
|
|
|
if (isTransByAcc || isTransByVreg) {
|
2022-02-17 03:48:40 +00:00
|
|
|
if (curInfo.IsMov()) {
|
2022-10-27 03:56:58 +00:00
|
|
|
tmpAcc = curInfo.AccIn();
|
2022-02-17 03:48:40 +00:00
|
|
|
if (!curInfo.inputs.empty()) {
|
|
|
|
ASSERT(!tmpAcc);
|
|
|
|
ASSERT(curInfo.inputs.size() == 1);
|
|
|
|
tmpReg = std::get<VirtualRegister>(curInfo.inputs.at(0)).GetId();
|
2022-07-05 07:24:00 +00:00
|
|
|
}
|
2022-10-22 07:59:53 +00:00
|
|
|
if (HasTypes()) {
|
2022-10-27 03:56:58 +00:00
|
|
|
type = typeRecorder_.UpdateType(pcToBCOffset_.at(pcIter) - 1, type);
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
2022-02-17 03:48:40 +00:00
|
|
|
} else {
|
2022-10-27 03:56:58 +00:00
|
|
|
ans = byteCodeToJSGate_.at(pcIter);
|
2022-10-22 07:59:53 +00:00
|
|
|
if (HasTypes() && !type.IsAnyType()) {
|
2022-07-05 07:24:00 +00:00
|
|
|
gateAcc_.SetGateType(ans, type);
|
|
|
|
}
|
2022-02-17 03:48:40 +00:00
|
|
|
break;
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-27 03:56:58 +00:00
|
|
|
if (curInfo.GetOpcode() != EcmaOpcode::RESUMEGENERATOR) {
|
2022-06-06 09:41:17 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// New RESTORE_REGISTER HIR, used to restore the register content when processing resume instruction.
|
|
|
|
// New SAVE_REGISTER HIR, used to save register content when processing suspend instruction.
|
2022-10-27 03:56:58 +00:00
|
|
|
auto resumeGate = byteCodeToJSGate_.at(pcIter);
|
2022-09-19 13:08:34 +00:00
|
|
|
ans = GetExistingRestore(resumeGate, tmpReg);
|
|
|
|
if (ans != Circuit::NullGate()) {
|
|
|
|
break;
|
|
|
|
}
|
2022-07-19 12:44:46 +00:00
|
|
|
GateRef resumeDependGate = gateAcc_.GetDep(resumeGate);
|
2022-06-06 09:41:17 +00:00
|
|
|
ans = circuit_.NewGate(OpCode(OpCode::RESTORE_REGISTER), MachineType::I64, tmpReg,
|
2022-07-05 07:24:00 +00:00
|
|
|
{resumeDependGate}, GateType::AnyType());
|
2022-09-19 13:08:34 +00:00
|
|
|
SetExistingRestore(resumeGate, tmpReg, ans);
|
2022-07-19 12:44:46 +00:00
|
|
|
gateAcc_.SetDep(resumeGate, ans);
|
2022-10-27 03:56:58 +00:00
|
|
|
bcId = iterator.Index();
|
2022-10-27 09:12:44 +00:00
|
|
|
auto saveRegGate = ResolveDef(bbId, iterator.Index() - 1, tmpReg, tmpAcc);
|
2022-10-27 03:56:58 +00:00
|
|
|
iterator.Goto(bcId);
|
|
|
|
auto nextPcIter = iterator.PeekPrevPc(2); // 2: skip 2
|
|
|
|
ASSERT(Bytecodes::GetOpcode(nextPcIter) == EcmaOpcode::SUSPENDGENERATOR_V8);
|
|
|
|
GateRef suspendGate = byteCodeToJSGate_.at(nextPcIter);
|
2022-07-19 12:44:46 +00:00
|
|
|
auto dependGate = gateAcc_.GetDep(suspendGate);
|
2022-06-06 09:41:17 +00:00
|
|
|
auto newDependGate = circuit_.NewGate(OpCode(OpCode::SAVE_REGISTER), tmpReg, {dependGate, saveRegGate},
|
|
|
|
GateType::Empty());
|
2022-07-19 12:44:46 +00:00
|
|
|
gateAcc_.SetDep(suspendGate, newDependGate);
|
2022-06-06 09:41:17 +00:00
|
|
|
break;
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
2022-04-24 17:17:29 +00:00
|
|
|
// find GET_EXCEPTION gate if this is a catch block
|
2022-02-17 03:48:40 +00:00
|
|
|
if (ans == Circuit::NullGate() && tmpAcc) {
|
2022-04-24 17:17:29 +00:00
|
|
|
if (!bb.trys.empty()) {
|
2022-07-19 12:44:46 +00:00
|
|
|
std::vector<GateRef> outList;
|
|
|
|
gateAcc_.GetOutVector(bb.dependStart, outList);
|
2022-04-24 17:17:29 +00:00
|
|
|
ASSERT(outList.size() == 1);
|
|
|
|
const auto &getExceptionGate = outList.at(0);
|
2022-07-19 12:44:46 +00:00
|
|
|
ASSERT(gateAcc_.GetOpCode(getExceptionGate) == OpCode::GET_EXCEPTION);
|
2022-04-24 17:17:29 +00:00
|
|
|
ans = getExceptionGate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// find def-site in value selectors of vregs
|
2022-02-17 03:48:40 +00:00
|
|
|
if (ans == Circuit::NullGate() && !tmpAcc && bb.phi.count(tmpReg)) {
|
|
|
|
if (!bb.vregToValSelectorGate.count(tmpReg)) {
|
2022-06-11 19:13:23 +00:00
|
|
|
NewPhi(bb, tmpReg, tmpAcc, bb.vregToValSelectorGate[tmpReg]);
|
2022-04-24 17:17:29 +00:00
|
|
|
}
|
2022-02-17 03:48:40 +00:00
|
|
|
ans = bb.vregToValSelectorGate.at(tmpReg);
|
2022-04-24 17:17:29 +00:00
|
|
|
}
|
|
|
|
// find def-site in value selectors of acc
|
2022-02-17 03:48:40 +00:00
|
|
|
if (ans == Circuit::NullGate() && tmpAcc && bb.phiAcc) {
|
2022-04-24 17:17:29 +00:00
|
|
|
if (bb.valueSelectorAccGate == Circuit::NullGate()) {
|
2022-06-11 19:13:23 +00:00
|
|
|
NewPhi(bb, tmpReg, tmpAcc, bb.valueSelectorAccGate);
|
2022-04-24 17:17:29 +00:00
|
|
|
}
|
|
|
|
ans = bb.valueSelectorAccGate;
|
|
|
|
}
|
2022-06-16 09:27:35 +00:00
|
|
|
if (ans == Circuit::NullGate() && IsEntryBlock(bbId)) { // entry block
|
2022-04-24 17:17:29 +00:00
|
|
|
// find def-site in function args
|
2022-06-16 09:27:35 +00:00
|
|
|
ASSERT(!tmpAcc);
|
|
|
|
ans = argAcc_.GetArgGate(tmpReg);
|
2022-05-26 04:54:21 +00:00
|
|
|
return ans;
|
2022-04-24 17:17:29 +00:00
|
|
|
}
|
|
|
|
if (ans == Circuit::NullGate()) {
|
|
|
|
// recursively find def-site in dominator block
|
2022-10-27 03:56:58 +00:00
|
|
|
auto dom = bb.iDominator;
|
|
|
|
auto &domIterator = dom->GetBytecodeIterator();
|
2022-10-27 09:12:44 +00:00
|
|
|
return ResolveDef(dom->id, domIterator.GetEndBcIndex(), tmpReg, tmpAcc);
|
2022-04-24 17:17:29 +00:00
|
|
|
} else {
|
|
|
|
// def-site already found
|
|
|
|
return ans;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BytecodeCircuitBuilder::BuildCircuit()
|
|
|
|
{
|
|
|
|
// create arg gates array
|
|
|
|
BuildCircuitArgs();
|
|
|
|
CollectPredsInfo();
|
|
|
|
BuildBlockCircuitHead();
|
|
|
|
// build states sub-circuit of each block
|
|
|
|
BuildSubCircuit();
|
2022-01-19 01:11:02 +00:00
|
|
|
// verification of soundness of CFG
|
2022-04-24 17:17:29 +00:00
|
|
|
for (auto &bb: graph_) {
|
2022-01-06 02:26:19 +00:00
|
|
|
if (bb.isDead) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-01-19 01:11:02 +00:00
|
|
|
ASSERT(bb.statePredIndex == bb.numOfStatePreds);
|
2022-04-24 17:17:29 +00:00
|
|
|
ASSERT(bb.loopBackIndex == bb.numOfLoopBacks);
|
|
|
|
if (bb.numOfLoopBacks) {
|
|
|
|
ASSERT(bb.forwardIndex == bb.numOfStatePreds - bb.numOfLoopBacks);
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-19 01:11:02 +00:00
|
|
|
// resolve def-site of virtual regs and set all value inputs
|
2022-07-19 12:44:46 +00:00
|
|
|
std::vector<GateRef> gates;
|
|
|
|
circuit_.GetAllGates(gates);
|
|
|
|
for (auto gate: gates) {
|
|
|
|
auto valueCount = gateAcc_.GetInValueCount(gate);
|
2022-01-22 11:09:02 +00:00
|
|
|
auto it = jsgateToBytecode_.find(gate);
|
2022-04-24 17:17:29 +00:00
|
|
|
if (it == jsgateToBytecode_.cend()) {
|
2022-01-06 02:26:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-10-18 03:02:13 +00:00
|
|
|
if (gateAcc_.IsConstant(gate)) {
|
Add information such as instruction immediate, method_id, string_id, etc.
The lowering slow path requires not only the vreg information of the
instruction, but also other information such as string id, method id, etc.
For each bytecode instruction, collect the various information stored in
the instruction, and use the information as the input of the gate, and
in the lowering stage, take out the required information from the gate.
issue:https://gitee.com/openharmony/ark_js_runtime/issues/I4WQR5
Signed-off-by: wanyanglan <wanyanglan1@huawei.com>
Change-Id: I42ef36554b0b88ce3e1cd0f593e8ce9e924b83e0
2022-03-04 07:52:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-10-27 03:56:58 +00:00
|
|
|
const auto &[bbIndex, bcIndex] = it->second;
|
|
|
|
const BytecodeInfo& bytecodeInfo = GetBytecodeInfo(bbIndex, bcIndex);
|
2022-10-22 07:59:53 +00:00
|
|
|
if (HasTypes()) {
|
2022-10-27 09:12:44 +00:00
|
|
|
auto pc = bytecodeInfo.GetPC();
|
2022-07-05 07:24:00 +00:00
|
|
|
auto type = typeRecorder_.GetType(pcToBCOffset_.at(pc) - 1);
|
|
|
|
if (!type.IsAnyType()) {
|
|
|
|
gateAcc_.SetGateType(gate, type);
|
|
|
|
}
|
|
|
|
}
|
2022-06-10 07:22:21 +00:00
|
|
|
[[maybe_unused]] size_t numValueInputs = bytecodeInfo.ComputeTotalValueCount();
|
2022-10-27 03:56:58 +00:00
|
|
|
[[maybe_unused]] size_t numValueOutputs = bytecodeInfo.ComputeOutCount();
|
2022-03-02 08:06:19 +00:00
|
|
|
ASSERT(numValueInputs == valueCount);
|
2022-01-06 02:26:19 +00:00
|
|
|
ASSERT(numValueOutputs <= 1);
|
2022-07-19 12:44:46 +00:00
|
|
|
auto stateCount = gateAcc_.GetStateCount(gate);
|
|
|
|
auto dependCount = gateAcc_.GetDependCount(gate);
|
2022-03-02 08:06:19 +00:00
|
|
|
for (size_t valueIdx = 0; valueIdx < valueCount; valueIdx++) {
|
|
|
|
auto inIdx = valueIdx + stateCount + dependCount;
|
2022-07-19 12:44:46 +00:00
|
|
|
if (!gateAcc_.IsInGateNull(gate, inIdx)) {
|
2022-01-14 09:48:52 +00:00
|
|
|
continue;
|
|
|
|
}
|
Add information such as instruction immediate, method_id, string_id, etc.
The lowering slow path requires not only the vreg information of the
instruction, but also other information such as string id, method id, etc.
For each bytecode instruction, collect the various information stored in
the instruction, and use the information as the input of the gate, and
in the lowering stage, take out the required information from the gate.
issue:https://gitee.com/openharmony/ark_js_runtime/issues/I4WQR5
Signed-off-by: wanyanglan <wanyanglan1@huawei.com>
Change-Id: I42ef36554b0b88ce3e1cd0f593e8ce9e924b83e0
2022-03-04 07:52:07 +00:00
|
|
|
if (valueIdx < bytecodeInfo.inputs.size()) {
|
2022-10-07 23:56:43 +00:00
|
|
|
auto vregId = std::get<VirtualRegister>(bytecodeInfo.inputs.at(valueIdx)).GetId();
|
2022-10-27 09:12:44 +00:00
|
|
|
GateRef defVreg = ResolveDef(bbIndex, bcIndex - 1, vregId, false);
|
2022-10-07 23:56:43 +00:00
|
|
|
gateAcc_.NewIn(gate, inIdx, defVreg);
|
2022-01-06 02:26:19 +00:00
|
|
|
} else {
|
2022-10-27 09:12:44 +00:00
|
|
|
GateRef defAcc = ResolveDef(bbIndex, bcIndex - 1, 0, true);
|
2022-10-07 23:56:43 +00:00
|
|
|
gateAcc_.NewIn(gate, inIdx, defAcc);
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-22 07:59:53 +00:00
|
|
|
if (HasTypes() && IsTypeLoweringEnabled()) {
|
2022-10-21 13:02:05 +00:00
|
|
|
frameStateBuilder_.BuildFrameState();
|
2022-10-09 07:13:51 +00:00
|
|
|
}
|
2022-04-14 08:14:34 +00:00
|
|
|
|
|
|
|
if (IsLogEnabled()) {
|
2022-10-03 04:31:39 +00:00
|
|
|
PrintGraph("Bytecode2Gate");
|
2022-10-09 07:53:21 +00:00
|
|
|
LOG_COMPILER(INFO) << "\033[34m" << "============= "
|
|
|
|
<< "After bytecode2circuit lowering ["
|
|
|
|
<< methodName_ << "]"
|
|
|
|
<< " =============" << "\033[0m";
|
2022-04-14 08:14:34 +00:00
|
|
|
circuit_.PrintAllGates(*this);
|
2022-10-03 04:31:39 +00:00
|
|
|
LOG_COMPILER(INFO) << "\033[34m" << "=========================== End ===========================" << "\033[0m";
|
2022-04-14 08:14:34 +00:00
|
|
|
}
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
|
|
|
|
2022-06-10 07:22:21 +00:00
|
|
|
void BytecodeCircuitBuilder::AddBytecodeOffsetInfo(GateRef &gate, const BytecodeInfo &info, size_t bcOffsetIndex,
|
|
|
|
uint8_t *pc)
|
|
|
|
{
|
|
|
|
if (info.IsCall()) {
|
2022-09-26 13:32:52 +00:00
|
|
|
auto bcOffset = circuit_.GetConstantGate(MachineType::I64,
|
|
|
|
pcToBCOffset_.at(pc),
|
|
|
|
GateType::NJSValue());
|
2022-07-19 12:44:46 +00:00
|
|
|
gateAcc_.NewIn(gate, bcOffsetIndex, bcOffset);
|
2022-06-10 07:22:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-28 02:40:23 +00:00
|
|
|
GateRef BytecodeCircuitBuilder::GetExistingRestore(GateRef resumeGate, uint16_t tmpReg) const
|
2022-09-26 08:38:14 +00:00
|
|
|
{
|
2022-09-19 13:08:34 +00:00
|
|
|
auto pr = std::make_pair(resumeGate, tmpReg);
|
|
|
|
if (resumeRegToRestore_.count(pr)) {
|
|
|
|
return resumeRegToRestore_.at(pr);
|
|
|
|
}
|
|
|
|
return Circuit::NullGate();
|
|
|
|
}
|
|
|
|
|
2022-09-28 02:40:23 +00:00
|
|
|
void BytecodeCircuitBuilder::SetExistingRestore(GateRef resumeGate, uint16_t tmpReg, GateRef restoreGate)
|
2022-09-26 08:38:14 +00:00
|
|
|
{
|
2022-09-19 13:08:34 +00:00
|
|
|
auto pr = std::make_pair(resumeGate, tmpReg);
|
|
|
|
resumeRegToRestore_[pr] = restoreGate;
|
|
|
|
}
|
|
|
|
|
2022-10-03 04:31:39 +00:00
|
|
|
void BytecodeCircuitBuilder::PrintGraph(const char* title)
|
2022-01-06 02:26:19 +00:00
|
|
|
{
|
2022-10-03 04:31:39 +00:00
|
|
|
std::map<const uint8_t *, GateRef> bcToGate;
|
|
|
|
for (const auto &[key, value]: jsgateToBytecode_) {
|
2022-10-27 09:12:44 +00:00
|
|
|
auto pc = GetBytecodeInfo(value.first, value.second).GetPC();
|
2022-10-27 03:56:58 +00:00
|
|
|
bcToGate[pc] = key;
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
|
|
|
|
2022-10-03 04:31:39 +00:00
|
|
|
LOG_COMPILER(INFO) << "======================== " << title << " ========================";
|
2022-04-24 17:17:29 +00:00
|
|
|
for (size_t i = 0; i < graph_.size(); i++) {
|
2022-10-03 04:31:39 +00:00
|
|
|
BytecodeRegion& bb = graph_[i];
|
|
|
|
if (bb.isDead) {
|
|
|
|
LOG_COMPILER(INFO) << "B" << bb.id << ": ;preds= invalid BB";
|
|
|
|
LOG_COMPILER(INFO) << "\tBytecodePC: [" << reinterpret_cast<void*>(bb.start) << ", "
|
|
|
|
<< reinterpret_cast<void*>(bb.end) << ")";
|
2022-01-06 02:26:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-10-03 04:31:39 +00:00
|
|
|
std::string log("B" + std::to_string(bb.id) + ": ;preds= ");
|
|
|
|
for (size_t k = 0; k < bb.preds.size(); ++k) {
|
|
|
|
log += std::to_string(bb.preds[k]->id) + ", ";
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
2022-07-06 06:12:54 +00:00
|
|
|
LOG_COMPILER(INFO) << log;
|
2022-10-03 04:31:39 +00:00
|
|
|
LOG_COMPILER(INFO) << "\tBytecodePC: [" << reinterpret_cast<void*>(bb.start) << ", "
|
|
|
|
<< reinterpret_cast<void*>(bb.end) << ")";
|
2022-01-06 02:26:19 +00:00
|
|
|
|
2022-10-03 04:31:39 +00:00
|
|
|
std::string log1("\tSucces: ");
|
|
|
|
for (size_t j = 0; j < bb.succs.size(); j++) {
|
|
|
|
log1 += std::to_string(bb.succs[j]->id) + ", ";
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
2022-10-03 04:31:39 +00:00
|
|
|
LOG_COMPILER(INFO) << log1;
|
2022-01-06 02:26:19 +00:00
|
|
|
|
2022-10-03 04:31:39 +00:00
|
|
|
for (size_t j = 0; j < bb.catchs.size(); j++) {
|
|
|
|
LOG_COMPILER(INFO) << "\tcatch [: " << reinterpret_cast<void*>(bb.catchs[j]->start) << ", "
|
|
|
|
<< reinterpret_cast<void*>(bb.catchs[j]->end) << ")";
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
|
|
|
|
2022-10-03 04:31:39 +00:00
|
|
|
std::string log2("\tTrys: ");
|
|
|
|
for (auto tryBlock: bb.trys) {
|
|
|
|
log2 += std::to_string(tryBlock->id) + " , ";
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
2022-10-03 04:31:39 +00:00
|
|
|
LOG_COMPILER(INFO) << log2;
|
2022-01-06 02:26:19 +00:00
|
|
|
|
2022-10-03 04:31:39 +00:00
|
|
|
std::string log3 = "\tDom: ";
|
|
|
|
for (size_t j = 0; j < bb.immDomBlocks.size(); j++) {
|
|
|
|
log3 += "B" + std::to_string(bb.immDomBlocks[j]->id) + std::string(", ");
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
2022-10-03 04:31:39 +00:00
|
|
|
LOG_COMPILER(INFO) << log3;
|
2022-01-06 02:26:19 +00:00
|
|
|
|
2022-10-03 04:31:39 +00:00
|
|
|
if (bb.iDominator) {
|
|
|
|
LOG_COMPILER(INFO) << "\tIDom B" << bb.iDominator->id;
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
|
|
|
|
2022-10-03 04:31:39 +00:00
|
|
|
std::string log4("\tDom Frontiers: ");
|
|
|
|
for (const auto &frontier: bb.domFrontiers) {
|
|
|
|
log4 += std::to_string(frontier->id) + " , ";
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
2022-10-03 04:31:39 +00:00
|
|
|
LOG_COMPILER(INFO) << log4;
|
2022-01-06 02:26:19 +00:00
|
|
|
|
2022-10-03 04:31:39 +00:00
|
|
|
std::string log5("\tPhi: ");
|
|
|
|
for (auto variable: bb.phi) {
|
|
|
|
log5 += std::to_string(variable) + " , ";
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
2022-10-03 04:31:39 +00:00
|
|
|
LOG_COMPILER(INFO) << log5;
|
2022-01-06 02:26:19 +00:00
|
|
|
|
2022-10-03 04:31:39 +00:00
|
|
|
PrintBytecodeInfo(bb, bcToGate);
|
|
|
|
LOG_COMPILER(INFO) << "";
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-03 04:31:39 +00:00
|
|
|
void BytecodeCircuitBuilder::PrintBytecodeInfo(BytecodeRegion& bb, const std::map<const uint8_t *, GateRef>& bcToGate)
|
2022-01-06 02:26:19 +00:00
|
|
|
{
|
2022-10-03 04:31:39 +00:00
|
|
|
if (bb.isDead) {
|
|
|
|
return;
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
2022-10-03 04:31:39 +00:00
|
|
|
LOG_COMPILER(INFO) << "\tBytecode[] = ";
|
2022-10-27 03:56:58 +00:00
|
|
|
EnumerateBlock(bb, [&](const BytecodeInfo &bytecodeInfo) -> bool {
|
2022-10-27 09:12:44 +00:00
|
|
|
auto &iterator = bb.GetBytecodeIterator();
|
|
|
|
auto pc = bytecodeInfo.GetPC();
|
2022-10-03 04:31:39 +00:00
|
|
|
std::string log;
|
2022-10-27 09:12:44 +00:00
|
|
|
log += std::string("\t\t< ") + std::to_string(iterator.Index()) + ": ";
|
2022-10-21 13:02:05 +00:00
|
|
|
log += GetEcmaOpcodeStr(static_cast<EcmaOpcode>(*pc)) + ", " + "In=[";
|
2022-10-27 03:56:58 +00:00
|
|
|
if (bytecodeInfo.AccIn()) {
|
2022-10-03 04:31:39 +00:00
|
|
|
log += "acc,";
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
2022-10-03 04:31:39 +00:00
|
|
|
for (const auto &in: bytecodeInfo.inputs) {
|
|
|
|
if (std::holds_alternative<VirtualRegister>(in)) {
|
|
|
|
log += std::to_string(std::get<VirtualRegister>(in).GetId()) + ",";
|
|
|
|
}
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
2022-10-03 04:31:39 +00:00
|
|
|
log += "], Out=[";
|
2022-10-27 03:56:58 +00:00
|
|
|
if (bytecodeInfo.AccOut()) {
|
2022-10-03 04:31:39 +00:00
|
|
|
log += "acc,";
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
2022-10-03 04:31:39 +00:00
|
|
|
for (const auto &out: bytecodeInfo.vregOut) {
|
|
|
|
log += std::to_string(out) + ",";
|
2022-01-06 02:26:19 +00:00
|
|
|
}
|
2022-10-03 04:31:39 +00:00
|
|
|
log += "] >";
|
|
|
|
LOG_COMPILER(INFO) << log;
|
2022-07-15 02:35:25 +00:00
|
|
|
|
2022-10-03 04:31:39 +00:00
|
|
|
auto r = bcToGate.find(pc);
|
|
|
|
if (r != bcToGate.end()) {
|
|
|
|
this->gateAcc_.ShortPrint(r->second);
|
2022-07-15 02:35:25 +00:00
|
|
|
}
|
2022-10-03 04:31:39 +00:00
|
|
|
return true;
|
|
|
|
});
|
2022-07-15 02:35:25 +00:00
|
|
|
}
|
2022-04-22 03:03:38 +00:00
|
|
|
} // namespace panda::ecmascript::kungfu
|