v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
type-parser.h
Go to the documentation of this file.
1// Copyright 2022 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_COMPILER_TURBOSHAFT_TYPE_PARSER_H_
6#define V8_COMPILER_TURBOSHAFT_TYPE_PARSER_H_
7
8#include <optional>
9
11
13
14// TypeParser is used to construct a Type from a string literal.
15// It's primary use is the %CheckTurboshaftTypeOf intrinsic, which allows
16// mjsunit tests to check the static type of expressions. Typically the string
17// has to have the format that Type::ToString() would produce.
18//
19// Examples: "Word32", "Word64[30, 100]", "Float32{-1.02}", "Float64{3.2, 17.8}"
21 public:
22 explicit TypeParser(const std::string_view& str, Zone* zone)
23 : str_(str), zone_(zone) {}
24
25 std::optional<Type> Parse() {
26 std::optional<Type> type = ParseType();
27 // Skip trailing whitespace.
28 while (pos_ < str_.length() && str_[pos_] == ' ') ++pos_;
29 if (pos_ < str_.length()) return std::nullopt;
30 return type;
31 }
32
33 private:
34 std::optional<Type> ParseType();
35
36 template <typename T>
37 std::optional<T> ParseRange() {
38 if (!ConsumeIf("[")) return std::nullopt;
40 if (!from) return std::nullopt;
41 if (!ConsumeIf(",")) return std::nullopt;
43 if (!to) return std::nullopt;
44 if (!ConsumeIf("]")) return std::nullopt;
45 if constexpr (!std::is_same_v<T, Word32Type> &&
46 !std::is_same_v<T, Word64Type>) {
47 CHECK_LE(*from, *to);
48 }
49 return T::Range(*from, *to, zone_);
50 }
51
52 template <typename T>
53 std::optional<T> ParseSet() {
54 if (!ConsumeIf("{")) return std::nullopt;
56 if (!elements) return std::nullopt;
57 if (!ConsumeIf("}")) return std::nullopt;
58 CHECK_LT(0, elements->size());
59 CHECK_LE(elements->size(), T::kMaxSetSize);
60 return T::Set(*elements, zone_);
61 }
62
63 template <typename T>
64 std::optional<std::vector<T>> ParseSetElements() {
65 std::vector<T> elements;
66 if (IsNext("}")) return elements;
67 while (true) {
68 auto element_opt = ReadValue<T>();
69 if (!element_opt) return std::nullopt;
70 elements.push_back(*element_opt);
71
72 if (IsNext("}")) break;
73 if (!ConsumeIf(",")) return std::nullopt;
74 }
75 base::sort(elements);
76 elements.erase(std::unique(elements.begin(), elements.end()),
77 elements.end());
78 return elements;
79 }
80
81 bool ConsumeIf(const std::string_view& prefix) {
82 if (IsNext(prefix)) {
83 pos_ += prefix.length();
84 return true;
85 }
86 return false;
87 }
88
89 bool IsNext(const std::string_view& prefix) {
90 // Skip leading whitespace.
91 while (pos_ < str_.length() && str_[pos_] == ' ') ++pos_;
92 if (pos_ >= str_.length()) return false;
93 size_t remaining_length = str_.length() - pos_;
94 if (prefix.length() > remaining_length) return false;
95 return str_.compare(pos_, prefix.length(), prefix, 0, prefix.length()) == 0;
96 }
97
98 template <typename T>
99 std::optional<T> ReadValue() {
100 T result;
101 size_t read = 0;
102 // TODO(nicohartmann@): Ideally we want to avoid this string construction
103 // (e.g. using std::from_chars).
104 std::string s(str_.cbegin() + pos_, str_.cend());
105 if constexpr (std::is_same_v<T, uint32_t>) {
106 result = static_cast<uint32_t>(std::stoul(s, &read));
107 } else if constexpr (std::is_same_v<T, uint64_t>) {
108 result = std::stoull(s, &read);
109 } else if constexpr (std::is_same_v<T, float>) {
110 result = std::stof(s, &read);
111 } else if constexpr (std::is_same_v<T, double>) {
112 result = std::stod(s, &read);
113 }
114 if (read == 0) return std::nullopt;
115 pos_ += read;
116 return result;
117 }
118
119 std::string_view str_;
121 size_t pos_ = 0;
122};
123
124} // namespace v8::internal::compiler::turboshaft
125
126#endif // V8_COMPILER_TURBOSHAFT_TYPE_PARSER_H_
bool IsNext(const std::string_view &prefix)
Definition type-parser.h:89
std::optional< std::vector< T > > ParseSetElements()
Definition type-parser.h:64
bool ConsumeIf(const std::string_view &prefix)
Definition type-parser.h:81
TypeParser(const std::string_view &str, Zone *zone)
Definition type-parser.h:22
ZoneVector< RpoNumber > & result
int s
Definition mul-fft.cc:297
void sort(C &container)
#define CHECK_LT(lhs, rhs)
#define CHECK_LE(lhs, rhs)
wasm::ValueType type