Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
199 changes: 72 additions & 127 deletions src/passes/Print.cpp

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ set(support_SOURCES
file.cpp
istring.cpp
json.cpp
name.cpp
path.cpp
safe_integer.cpp
string.cpp
threads.cpp
utilities.cpp
${support_HEADERS}
Expand Down
56 changes: 56 additions & 0 deletions src/support/name.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2024 WebAssembly Community Group participants
*
* 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.
*/

#include <algorithm>
#include <array>

#include "support/name.h"
#include "support/string.h"

namespace wasm {

// TODO: Use unicode rather than char.
bool Name::isIDChar(char c) {
if ('0' <= c && c <= '9') {
return true;
}
if ('A' <= c && c <= 'Z') {
return true;
}
if ('a' <= c && c <= 'z') {
return true;
}
static std::array<char, 23> otherIDChars = {
{'!', '#', '$', '%', '&', '\'', '*', '+', '-', '.', '/', ':',
'<', '=', '>', '?', '@', '\\', '^', '_', '`', '|', '~'}};
return std::find(otherIDChars.begin(), otherIDChars.end(), c) !=
otherIDChars.end();
}

std::ostream& Name::print(std::ostream& o) const {
assert(*this && "Cannot print an empty name");
// We need to quote names if they have tricky chars.
// TODO: This is not spec-compliant since the spec does not yet support
// quoted identifiers and has a limited set of valid idchars.
o << '$';
if (std::all_of(str.begin(), str.end(), isIDChar)) {
return o << str;
} else {
return String::printEscaped(o, str);
}
}

} // namespace wasm
5 changes: 5 additions & 0 deletions src/support/name.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ struct Name : public IString {
// TODO: Use C++23 `contains`.
return str.find(substring.str) != std::string_view::npos;
}

std::ostream& print(std::ostream& o) const;

private:
static bool isIDChar(char c);
};

} // namespace wasm
Expand Down
57 changes: 57 additions & 0 deletions src/support/string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2024 WebAssembly Community Group participants
*
* 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.
*/

#include <ostream>

#include "support/string.h"

namespace wasm::String {

std::ostream& printEscaped(std::ostream& os, std::string_view str) {
os << '"';
for (unsigned char c : str) {
switch (c) {
case '\t':
os << "\\t";
break;
case '\n':
os << "\\n";
break;
case '\r':
os << "\\r";
break;
case '"':
os << "\\\"";
break;
case '\'':
os << "\\'";
break;
case '\\':
os << "\\\\";
break;
default: {
if (c >= 32 && c < 127) {
os << c;
} else {
os << std::hex << '\\' << (c / 16) << (c % 16) << std::dec;
}
}
}
}
return os << '"';
}

} // namespace wasm::String
3 changes: 3 additions & 0 deletions src/support/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "support/utilities.h"
#include <algorithm>
#include <cctype>
#include <ostream>
#include <string>
#include <vector>

Expand Down Expand Up @@ -152,6 +153,8 @@ inline bool isNumber(const std::string& str) {
return !str.empty() && std::all_of(str.begin(), str.end(), ::isdigit);
}

std::ostream& printEscaped(std::ostream& os, std::string_view str);

} // namespace wasm::String

#endif // wasm_support_string_h
7 changes: 4 additions & 3 deletions src/wasm/wasm-type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1788,7 +1788,7 @@ void TypePrinter::printHeapTypeName(HeapType type) {
print(type);
return;
}
os << '$' << generator(type).name;
generator(type).name.print(os);
#if TRACE_CANONICALIZATION
os << "(;" << ((type.getID() >> 4) % 1000) << ";) ";
#endif
Expand Down Expand Up @@ -1915,7 +1915,8 @@ std::ostream& TypePrinter::print(HeapType type) {

auto names = generator(type);

os << "(type $" << names.name << ' ';
os << "(type ";
names.name.print(os) << ' ';

if (isTemp(type)) {
os << "(; temp ;) ";
Expand Down Expand Up @@ -2018,7 +2019,7 @@ TypePrinter::print(const Struct& struct_,
// TODO: move this to the function for printing fields.
os << " (field ";
if (auto it = fieldNames.find(i); it != fieldNames.end()) {
os << '$' << it->second << ' ';
it->second.print(os) << ' ';
}
print(struct_.fields[i]);
os << ')';
Expand Down
8 changes: 4 additions & 4 deletions test/lit/ctor-eval/array_new_data.wast
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
(module
;; CHECK: (type $0 (func))

;; CHECK: (type $[i8] (array i8))
(type $[i8] (array i8))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This moved away because the names no longer match, I guess? Should we update both at once?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, and it looks like the follow-up hasn't fixed it fully. Updating both at once makes sense.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kripken PTAL. The commits on this PR should be separately reviewable, but we can land them all together to avoid intermediate test regressions.

;; CHECK: (type $"[i8]" (array i8))
(type $"[i8]" (array i8))

;; CHECK: (memory $0 16 17 shared)
(memory $0 16 17 shared)
Expand All @@ -18,7 +18,7 @@

;; CHECK: (func $test (type $0)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (array.new_data $[i8] $1
;; CHECK-NEXT: (array.new_data $"[i8]" $1
;; CHECK-NEXT: (i32.const 16)
;; CHECK-NEXT: (i32.const 8)
;; CHECK-NEXT: )
Expand All @@ -29,7 +29,7 @@
;; atm. In fact the module would not validate as we refer to segment 1 here
;; but after flattening only segment 0 exists.
(drop
(array.new_data $[i8] $1
(array.new_data $"[i8]" $1
(i32.const 16)
(i32.const 8)
)
Expand Down
Loading