Add Type Infer Test Case

Description:
1.Add type infer test cases for bitwise_op, callfunctions, ldobjbyvalue and logic_op

Issue:
https://gitee.com/openharmony/ark_js_runtime/issues/I5B78L

Signed-off-by: huoqingyi <huoqingyi@huawei.com>
Change-Id: I486f6f91a32e1c7caa2446b12b8e6b33db8d47fe
This commit is contained in:
huoqingyi
2022-06-08 01:02:53 +08:00
parent 7685b11b6b
commit 1da170f1bd
35 changed files with 653 additions and 18 deletions
@@ -2202,7 +2202,7 @@ void BytecodeCircuitBuilder::NewReturn(BytecodeRegion &bb, const uint8_t *pc, Ga
auto gate = circuit_.NewGate(OpCode(OpCode::RETURN), 0,
{ state, depend, Circuit::NullGate(),
Circuit::GetCircuitRoot(OpCode(OpCode::RETURN_LIST)) },
GateType::Empty());
GateType::AnyType());
jsgateToBytecode_[gate] = {bb.id, pc};
} else if (static_cast<EcmaOpcode>(bytecodeInfo.opcode) == EcmaOpcode::RETURNUNDEFINED_PREF) {
// handle returnundefined bytecode
@@ -2213,7 +2213,7 @@ void BytecodeCircuitBuilder::NewReturn(BytecodeRegion &bb, const uint8_t *pc, Ga
auto gate = circuit_.NewGate(OpCode(OpCode::RETURN), 0,
{ state, depend, constant,
Circuit::GetCircuitRoot(OpCode(OpCode::RETURN_LIST)) },
GateType::Empty());
GateType::AnyType());
jsgateToBytecode_[gate] = {bb.id, pc};
}
}
+55
View File
@@ -24,4 +24,59 @@ bool Type::IsBitset() const
}
Type::~Type() {}
std::string GateType::GetTypeStr() const
{
GlobalTSTypeRef gt = GlobalTSTypeRef(GetType());
ASSERT(gt.GetFlag() == 0);
if (IsPrimitiveTypeKind()) {
auto primitive = static_cast<TSPrimitiveType>(gt.GetLocalId());
switch (primitive) {
case TSPrimitiveType::ANY:
return "any";
case TSPrimitiveType::NUMBER:
return "number";
case TSPrimitiveType::BOOLEAN:
return "boolean";
case TSPrimitiveType::VOID_TYPE:
return "void";
case TSPrimitiveType::STRING:
return "string";
case TSPrimitiveType::SYMBOL:
return "symbol";
case TSPrimitiveType::NULL_TYPE:
return "null";
case TSPrimitiveType::UNDEFINED:
return "undefined";
case TSPrimitiveType::INT:
return "int";
case TSPrimitiveType::BIG_INT:
return "big_int";
default:
break;
}
}
auto typeKind = static_cast<TSTypeKind>(gt.GetKind());
switch (typeKind) {
case TSTypeKind::CLASS:
return "class";
case TSTypeKind::CLASS_INSTANCE:
return "class_instance";
case TSTypeKind::FUNCTION:
return "function";
case TSTypeKind::UNION:
return "union";
case TSTypeKind::ARRAY:
return "array";
case TSTypeKind::OBJECT:
return "object";
case TSTypeKind::IMPORT:
return "import";
case TSTypeKind::INTERFACE_KIND:
return "interface";
default:
break;
}
return "gatetype:" + std::to_string(gt.GetType());
}
}; // namespace panda::ecmascript::kungfu
+9
View File
@@ -124,6 +124,13 @@ public:
return GateType(TSPrimitiveType::BIG_INT);
}
bool inline IsTSType() const
{
GlobalTSTypeRef gt = GlobalTSTypeRef(GetType());
// 0: TS type
return gt.GetFlag() == 0;
}
bool inline IsAnyType() const
{
return type_ == static_cast<uint32_t>(TSPrimitiveType::ANY);
@@ -267,6 +274,8 @@ public:
return type_ >= other.type_;
}
std::string GetTypeStr() const;
private:
uint32_t type_ {0};
};
@@ -65,13 +65,15 @@ void TypeInfer::TypeInferPrint() const
const auto &gateList = circuit_->GetAllGates();
std::string log("TestInfer:");
for (const auto &gate : gateList) {
auto op = gateAccessor_.GetOpCode(gate);
if (op == OpCode::JS_BYTECODE) {
log += "&" + builder_->GetBytecodeStr(gate) + ":";
auto gateType = gateAccessor_.GetGateType(gate);
auto type = gateType.GetType() > GlobalTSTypeRef::TS_TYPE_RESERVED_COUNT ? "INF"
: std::to_string(gateType.GetType());
log += type;
auto type = gateAccessor_.GetGateType(gate);
if (ShouldInfer(gate) && type.IsTSType() && !type.IsAnyType()) {
auto op = gateAccessor_.GetOpCode(gate);
if (op == OpCode::VALUE_SELECTOR) {
log += "&" + op.Str() + ":";
} else {
log += "&" + builder_->GetBytecodeStr(gate) + ":";
}
log += type.GetTypeStr();
}
}
COMPILER_LOG(INFO) << std::dec << log;
@@ -93,7 +95,7 @@ bool TypeInfer::UpdateType(GateRef gate, const GlobalTSTypeRef &typeRef)
return UpdateType(gate, type);
}
bool TypeInfer::ShouldInfer(const GateRef gate)
bool TypeInfer::ShouldInfer(const GateRef gate) const
{
auto op = gateAccessor_.GetOpCode(gate);
// handle phi gates
@@ -104,9 +106,9 @@ bool TypeInfer::ShouldInfer(const GateRef gate)
op == OpCode::CONSTANT ||
op == OpCode::RETURN) {
auto gateToBytecode = builder_->GetGateToBytecode();
// handle gates generated by ecma.* bytecodes
// handle gates generated by ecma.* bytecodes (not including Jump)
if (gateToBytecode.find(gate) != gateToBytecode.end()) {
return true;
return !builder_->GetByteCodeInfo(gate).IsJump();
}
}
return false;
@@ -40,7 +40,7 @@ public:
private:
bool UpdateType(GateRef gate, const GateType type);
bool UpdateType(GateRef gate, const GlobalTSTypeRef &typeRef);
bool ShouldInfer(const GateRef gate);
bool ShouldInfer(const GateRef gate) const;
bool Infer(GateRef gate);
bool InferPhiGate(GateRef gate);
bool SetNumberType(GateRef gate);