Commit 98628ab9 authored by Sigurd Schneider's avatar Sigurd Schneider Committed by Commit Bot

[torque] Generate class templates from Torque

Bug: v8:7793
Change-Id: I390b4b5e1f486c53c122f7ebf8dd4106c9bbd10e
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1607650
Commit-Queue: Sigurd Schneider <sigurds@chromium.org>
Reviewed-by: 's avatarTobias Tebbi <tebbi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#61564}
parent 0e5ed533
......@@ -1069,6 +1069,9 @@ action("run_torque") {
"$target_gen_dir/torque-generated/class-verifiers-tq.cc",
"$target_gen_dir/torque-generated/class-verifiers-tq.h",
"$target_gen_dir/torque-generated/objects-printer-tq.cc",
"$target_gen_dir/torque-generated/class-definitions-tq.cc",
"$target_gen_dir/torque-generated/class-definitions-tq-inl.h",
"$target_gen_dir/torque-generated/class-definitions-tq.h",
]
foreach(namespace, torque_namespaces) {
outputs += [
......@@ -1134,6 +1137,7 @@ v8_source_set("torque_generated_definitions") {
]
sources = [
"$target_gen_dir/torque-generated/class-definitions-tq.cc",
"$target_gen_dir/torque-generated/class-verifiers-tq.cc",
"$target_gen_dir/torque-generated/class-verifiers-tq.h",
"$target_gen_dir/torque-generated/objects-printer-tq.cc",
......
......@@ -71,6 +71,7 @@ extern class Context extends HeapObject {
native_context: Object;
}
type NativeContext extends Context;
extern class Oddball extends HeapObject {
to_number_raw: float64;
to_string: String;
......@@ -78,6 +79,7 @@ extern class Oddball extends HeapObject {
type_of: String;
kind: Smi;
}
extern class HeapNumber extends HeapObject { value: float64; }
type Number = Smi | HeapNumber;
type BigInt extends HeapObject generates 'TNode<BigInt>';
......@@ -136,12 +138,15 @@ type NaN extends HeapNumber;
@abstract
@noVerifier
@generatePrint
@generateCppClass
extern class Struct extends HeapObject {
}
@abstract
@dirtyInstantiatedAbstractClass
@generatePrint
@generateCppClass
extern class Tuple2 extends Struct {
value1: Object;
value2: Object;
......@@ -150,6 +155,7 @@ extern class Tuple2 extends Struct {
@abstract
@dirtyInstantiatedAbstractClass
@generatePrint
@generateCppClass
extern class Tuple3 extends Tuple2 {
value3: Object;
}
......@@ -448,7 +454,6 @@ extern class SharedFunctionInfo extends HeapObject {
expected_nof_properties: uint16;
function_token_offset: int16;
flags: int32;
@ifdef(V8_SFI_HAS_UNIQUE_ID) unique_id: int32;
}
......
......@@ -1661,8 +1661,6 @@ void PrototypeUsers::Verify(WeakArrayList array) {
CHECK_EQ(weak_maps_count + empty_slots_count + 1, array->length());
}
USE_TORQUE_VERIFIER(Tuple2)
void EnumCache::EnumCacheVerify(Isolate* isolate) {
TorqueGeneratedClassVerifiers::EnumCacheVerify(*this, isolate);
Heap* heap = isolate->heap();
......@@ -1674,15 +1672,6 @@ void EnumCache::EnumCacheVerify(Isolate* isolate) {
USE_TORQUE_VERIFIER(SourcePositionTableWithFrameCache)
void Tuple3::Tuple3Verify(Isolate* isolate) {
// Not using the Torque-generated verifier because IsTuple2 incorrectly
// returns false for instances of Tuple3.
CHECK(IsTuple3());
VerifyObjectField(isolate, kValue1Offset);
VerifyObjectField(isolate, kValue2Offset);
VerifyObjectField(isolate, kValue3Offset);
}
USE_TORQUE_VERIFIER(ClassPositions)
void ObjectBoilerplateDescription::ObjectBoilerplateDescriptionVerify(
......
......@@ -40,6 +40,7 @@
#include "src/property-details.h"
#include "src/property.h"
#include "src/v8memory.h"
#include "torque-generated/class-definitions-tq-inl.h"
// Has to be the last include (doesn't have include guards):
#include "src/objects/object-macros.h"
......
......@@ -104,3 +104,5 @@
#undef EXPORT_DECL_VERIFIER
#undef DEFINE_DEOPT_ELEMENT_ACCESSORS
#undef DEFINE_DEOPT_ENTRY_ACCESSORS
#undef TQ_OBJECT_CONSTRUCTORS
#undef TQ_OBJECT_CONSTRUCTORS_IMPL
......@@ -521,3 +521,15 @@
void DeoptimizationData::Set##name(int i, type value) { \
set(IndexForEntry(i) + k##name##Offset, value); \
}
#define TQ_OBJECT_CONSTRUCTORS(Type) \
public: \
constexpr Type() = default; \
\
protected: \
inline explicit Type(Address ptr); \
friend class TorqueGenerated##Type<Type, Super>;
#define TQ_OBJECT_CONSTRUCTORS_IMPL(Type) \
inline Type::Type(Address ptr) \
: TorqueGenerated##Type<Type, Type::Super>(ptr) {}
......@@ -11,6 +11,7 @@
#include "src/objects-inl.h"
#include "src/objects/oddball.h"
#include "src/roots-inl.h"
#include "torque-generated/class-definitions-tq-inl.h"
// Has to be the last include (doesn't have include guards):
#include "src/objects/object-macros.h"
......@@ -18,20 +19,14 @@
namespace v8 {
namespace internal {
OBJECT_CONSTRUCTORS_IMPL(Struct, HeapObject)
// TODO(jkummerow): Fix IsTuple2() and IsTuple3() to be subclassing-aware,
// or rethink this more generally (see crbug.com/v8/8516).
Tuple2::Tuple2(Address ptr) : Struct(ptr) {}
Tuple3::Tuple3(Address ptr) : Tuple2(ptr) {}
TQ_OBJECT_CONSTRUCTORS_IMPL(Struct)
TQ_OBJECT_CONSTRUCTORS_IMPL(Tuple2)
TQ_OBJECT_CONSTRUCTORS_IMPL(Tuple3)
OBJECT_CONSTRUCTORS_IMPL(AccessorPair, Struct)
OBJECT_CONSTRUCTORS_IMPL(ClassPositions, Struct)
CAST_ACCESSOR(AccessorPair)
CAST_ACCESSOR(Struct)
CAST_ACCESSOR(Tuple2)
CAST_ACCESSOR(Tuple3)
CAST_ACCESSOR(ClassPositions)
void Struct::InitializeBody(int object_size) {
......@@ -41,10 +36,6 @@ void Struct::InitializeBody(int object_size) {
}
}
ACCESSORS(Tuple2, value1, Object, kValue1Offset)
ACCESSORS(Tuple2, value2, Object, kValue2Offset)
ACCESSORS(Tuple3, value3, Object, kValue3Offset)
ACCESSORS(AccessorPair, getter, Object, kGetterOffset)
ACCESSORS(AccessorPair, setter, Object, kSetterOffset)
......
......@@ -7,7 +7,7 @@
#include "src/objects.h"
#include "src/objects/heap-object.h"
#include "torque-generated/field-offsets-tq.h"
#include "torque-generated/class-definitions-tq.h"
// Has to be the last include (doesn't have include guards):
#include "src/objects/object-macros.h"
......@@ -18,47 +18,26 @@ namespace internal {
// An abstract superclass, a marker class really, for simple structure classes.
// It doesn't carry much functionality but allows struct classes to be
// identified in the type system.
class Struct : public HeapObject {
class Struct : public TorqueGeneratedStruct<Struct, HeapObject> {
public:
inline void InitializeBody(int object_size);
DECL_CAST(Struct)
void BriefPrintDetails(std::ostream& os);
OBJECT_CONSTRUCTORS(Struct, HeapObject);
TQ_OBJECT_CONSTRUCTORS(Struct)
};
class Tuple2 : public Struct {
class Tuple2 : public TorqueGeneratedTuple2<Tuple2, Struct> {
public:
DECL_ACCESSORS(value1, Object)
DECL_ACCESSORS(value2, Object)
DECL_CAST(Tuple2)
// Dispatched behavior.
DECL_PRINTER(Tuple2)
DECL_VERIFIER(Tuple2)
void BriefPrintDetails(std::ostream& os);
DEFINE_FIELD_OFFSET_CONSTANTS(HeapObject::kHeaderSize,
TORQUE_GENERATED_TUPLE2_FIELDS)
OBJECT_CONSTRUCTORS(Tuple2, Struct);
TQ_OBJECT_CONSTRUCTORS(Tuple2)
};
class Tuple3 : public Tuple2 {
class Tuple3 : public TorqueGeneratedTuple3<Tuple3, Tuple2> {
public:
DECL_ACCESSORS(value3, Object)
DECL_CAST(Tuple3)
// Dispatched behavior.
DECL_PRINTER(Tuple3)
DECL_VERIFIER(Tuple3)
void BriefPrintDetails(std::ostream& os);
DEFINE_FIELD_OFFSET_CONSTANTS(Tuple2::kSize, TORQUE_GENERATED_TUPLE3_FIELDS)
OBJECT_CONSTRUCTORS(Tuple3, Tuple2);
TQ_OBJECT_CONSTRUCTORS(Tuple3)
};
// Support for JavaScript accessors: A pair of a getter and a setter. Each
......
......@@ -70,7 +70,8 @@ enum class ClassFlag {
kAbstract = 1 << 4,
kInstantiatedAbstractClass = 1 << 5,
kHasSameInstanceTypeAsParent = 1 << 6,
kHasIndexedField = 1 << 7
kGenerateCppClassDefinitions = 1 << 7,
kHasIndexedField = 1 << 8
};
using ClassFlags = base::Flags<ClassFlag>;
......
This diff is collapsed.
......@@ -278,6 +278,7 @@ class ImplementationVisitor {
void GenerateBuiltinDefinitions(const std::string& output_directory);
void GenerateClassFieldOffsets(const std::string& output_directory);
void GeneratePrintDefinitions(const std::string& output_directory);
void GenerateClassDefinitions(const std::string& output_directory);
void GenerateClassVerifiers(const std::string& output_directory);
VisitResult Visit(Expression* expr);
......
......@@ -79,6 +79,7 @@ void CompileCurrentAst(TorqueCompilerOptions options) {
implementation_visitor.GenerateBuiltinDefinitions(output_directory);
implementation_visitor.GenerateClassFieldOffsets(output_directory);
implementation_visitor.GeneratePrintDefinitions(output_directory);
implementation_visitor.GenerateClassDefinitions(output_directory);
implementation_visitor.GenerateClassVerifiers(output_directory);
for (Namespace* n : GlobalContext::Get().GetNamespaces()) {
......
......@@ -657,9 +657,9 @@ class AnnotationSet {
base::Optional<ParseResult> MakeClassDeclaration(
ParseResultIterator* child_results) {
AnnotationSet annotations(
child_results,
{"@generatePrint", "@noVerifier", "@abstract",
"@dirtyInstantiatedAbstractClass", "@hasSameInstanceTypeAsParent"});
child_results, {"@generatePrint", "@noVerifier", "@abstract",
"@dirtyInstantiatedAbstractClass",
"@hasSameInstanceTypeAsParent", "@generateCppClass"});
ClassFlags flags = ClassFlag::kNone;
bool generate_print = annotations.Contains("@generatePrint");
if (generate_print) flags |= ClassFlag::kGeneratePrint;
......@@ -674,6 +674,9 @@ base::Optional<ParseResult> MakeClassDeclaration(
if (annotations.Contains("@hasSameInstanceTypeAsParent")) {
flags |= ClassFlag::kHasSameInstanceTypeAsParent;
}
if (annotations.Contains("@generateCppClass")) {
flags |= ClassFlag::kGenerateCppClassDefinitions;
}
auto is_extern = child_results->NextAs<bool>();
if (is_extern) flags |= ClassFlag::kExtern;
auto transient = child_results->NextAs<bool>();
......
......@@ -353,6 +353,20 @@ void ClassType::Finalize() const {
TypeVisitor::VisitClassFieldsAndMethods(const_cast<ClassType*>(this),
this->decl_);
is_finalized_ = true;
if (GenerateCppClassDefinitions()) {
for (const Field& f : fields()) {
const Type* field_type = f.name_and_type.type;
if (!field_type->IsSubtypeOf(TypeOracle::GetObjectType()) ||
field_type->IsSubtypeOf(TypeOracle::GetSmiType()) ||
field_type->IsSubtypeOf(TypeOracle::GetNumberType())) {
std::stringstream s;
s << "Generation of C++ class for Torque class " << name()
<< " is not supported yet, because the type of field "
<< f.name_and_type.name << " cannot be handled yet";
ReportLintError(s.str(), f.pos);
}
}
}
CheckForDuplicateFields();
}
......
......@@ -524,6 +524,9 @@ class ClassType final : public AggregateType {
bool HasSameInstanceTypeAsParent() const {
return flags_ & ClassFlag::kHasSameInstanceTypeAsParent;
}
bool GenerateCppClassDefinitions() const {
return flags_ & ClassFlag::kGenerateCppClassDefinitions;
}
bool HasIndexedField() const override;
size_t size() const { return size_; }
const ClassType* GetSuperClass() const {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment