Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into fuzz.moar
Browse files Browse the repository at this point in the history
  • Loading branch information
kripken committed Feb 22, 2024
2 parents bb435ca + 4969f93 commit 097d2e2
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 155 deletions.
4 changes: 4 additions & 0 deletions scripts/fuzz_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,10 @@ def fix_double(x):
# change that index, so ignore it
out = re.sub(r'funcref\([\d\w$+-_:]+\)', 'funcref()', out)

# JS prints i31 as just a number, so change "i31ref(N)" (which C++ emits)
# to "N".
out = re.sub(r'i31ref\((\d+)\)', r'\1', out)

lines = out.splitlines()
for i in range(len(lines)):
line = lines[i]
Expand Down
2 changes: 1 addition & 1 deletion src/parser/context-decls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void applyImportNames(Importable& item, ImportNames* names) {
}
}

Result<> addExports(ParseInput& in,
Result<> addExports(Lexer& in,
Module& wasm,
const Named* item,
const std::vector<Name>& exports,
Expand Down
16 changes: 8 additions & 8 deletions src/parser/contexts.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
#define parser_context_h

#include "common.h"
#include "input.h"
#include "ir/names.h"
#include "lexer.h"
#include "support/name.h"
#include "support/result.h"
#include "wasm-builder.h"
Expand Down Expand Up @@ -577,8 +577,8 @@ struct NullInstrParserCtx {
};

struct NullCtx : NullTypeParserCtx, NullInstrParserCtx {
ParseInput in;
NullCtx(const ParseInput& in) : in(in) {}
Lexer in;
NullCtx(const Lexer& in) : in(in) {}
Result<> makeTypeUse(Index, std::optional<HeapTypeT>, ParamsT*, ResultsT*) {
return Ok{};
}
Expand All @@ -594,7 +594,7 @@ struct ParseDeclsCtx : NullTypeParserCtx, NullInstrParserCtx {
using TableTypeT = Limits;
using MemTypeT = MemType;

ParseInput in;
Lexer in;

// At this stage we only look at types to find implicit type definitions,
// which are inserted directly into the context. We cannot materialize or
Expand Down Expand Up @@ -772,7 +772,7 @@ struct ParseDeclsCtx : NullTypeParserCtx, NullInstrParserCtx {

// Phase 2: Parse type definitions into a TypeBuilder.
struct ParseTypeDefsCtx : TypeParserCtx<ParseTypeDefsCtx> {
ParseInput in;
Lexer in;

// We update slots in this builder as we parse type definitions.
TypeBuilder& builder;
Expand Down Expand Up @@ -844,7 +844,7 @@ struct ParseTypeDefsCtx : TypeParserCtx<ParseTypeDefsCtx> {
struct ParseImplicitTypeDefsCtx : TypeParserCtx<ParseImplicitTypeDefsCtx> {
using TypeUseT = Ok;

ParseInput in;
Lexer in;

// Types parsed so far.
std::vector<HeapType>& types;
Expand Down Expand Up @@ -914,7 +914,7 @@ struct ParseModuleTypesCtx : TypeParserCtx<ParseModuleTypesCtx>,

using ElemListT = Type;

ParseInput in;
Lexer in;

Module& wasm;

Expand Down Expand Up @@ -1099,7 +1099,7 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx> {

using TagLabelListT = std::vector<std::pair<TagIdxT, LabelIdxT>>;

ParseInput in;
Lexer in;

Module& wasm;
Builder builder;
Expand Down
88 changes: 0 additions & 88 deletions src/parser/input.h

This file was deleted.

81 changes: 47 additions & 34 deletions src/parser/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <variant>

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

#ifndef parser_lexer_h
#define parser_lexer_h
Expand Down Expand Up @@ -167,42 +168,41 @@ struct Lexer {
advance();
}

std::optional<Token> peek() const { return curr; }

bool takeLParen() {
auto t = peek();
if (!t || !t->isLParen()) {
if (!curr || !curr->isLParen()) {
return false;
}
advance();
return true;
}

bool peekLParen() { return Lexer(*this).takeLParen(); }

bool takeRParen() {
auto t = peek();
if (!t || !t->isRParen()) {
if (!curr || !curr->isRParen()) {
return false;
}
advance();
return true;
}

bool peekRParen() { return Lexer(*this).takeRParen(); }

bool takeUntilParen() {
while (true) {
auto t = peek();
if (!t) {
if (!curr) {
return false;
}
if (t->isLParen() || t->isRParen()) {
if (curr->isLParen() || curr->isRParen()) {
return true;
}
advance();
}
}

std::optional<Name> takeID() {
if (auto t = peek()) {
if (auto id = t->getID()) {
if (curr) {
if (auto id = curr->getID()) {
advance();
// See comment on takeName.
return Name(std::string(*id));
Expand All @@ -212,18 +212,22 @@ struct Lexer {
}

std::optional<std::string_view> takeKeyword() {
if (auto t = peek()) {
if (auto keyword = t->getKeyword()) {
if (curr) {
if (auto keyword = curr->getKeyword()) {
advance();
return *keyword;
}
}
return {};
}

std::optional<std::string_view> peekKeyword() {
return Lexer(*this).takeKeyword();
}

bool takeKeyword(std::string_view expected) {
if (auto t = peek()) {
if (auto keyword = t->getKeyword()) {
if (curr) {
if (auto keyword = curr->getKeyword()) {
if (*keyword == expected) {
advance();
return true;
Expand All @@ -235,16 +239,16 @@ struct Lexer {

std::optional<uint64_t> takeOffset() {
using namespace std::string_view_literals;
if (auto t = peek()) {
if (auto keyword = t->getKeyword()) {
if (curr) {
if (auto keyword = curr->getKeyword()) {
if (keyword->substr(0, 7) != "offset="sv) {
return {};
}
Lexer subLexer(keyword->substr(7));
if (subLexer.empty()) {
return {};
}
if (auto o = subLexer.peek()->getU<uint64_t>()) {
if (auto o = subLexer.curr->getU<uint64_t>()) {
subLexer.advance();
if (subLexer.empty()) {
advance();
Expand All @@ -258,16 +262,16 @@ struct Lexer {

std::optional<uint32_t> takeAlign() {
using namespace std::string_view_literals;
if (auto t = peek()) {
if (auto keyword = t->getKeyword()) {
if (curr) {
if (auto keyword = curr->getKeyword()) {
if (keyword->substr(0, 6) != "align="sv) {
return {};
}
Lexer subLexer(keyword->substr(6));
if (subLexer.empty()) {
return {};
}
if (auto a = subLexer.peek()->getU<uint32_t>()) {
if (auto a = subLexer.curr->getU<uint32_t>()) {
subLexer.advance();
if (subLexer.empty()) {
advance();
Expand All @@ -280,8 +284,8 @@ struct Lexer {
}

template<typename T> std::optional<T> takeU() {
if (auto t = peek()) {
if (auto n = t->getU<T>()) {
if (curr) {
if (auto n = curr->getU<T>()) {
advance();
return n;
}
Expand All @@ -290,8 +294,8 @@ struct Lexer {
}

template<typename T> std::optional<T> takeI() {
if (auto t = peek()) {
if (auto n = t->getI<T>()) {
if (curr) {
if (auto n = curr->getI<T>()) {
advance();
return n;
}
Expand All @@ -314,8 +318,8 @@ struct Lexer {
std::optional<uint8_t> takeI8() { return takeI<uint8_t>(); }

std::optional<double> takeF64() {
if (auto t = peek()) {
if (auto d = t->getF64()) {
if (curr) {
if (auto d = curr->getF64()) {
advance();
return d;
}
Expand All @@ -324,8 +328,8 @@ struct Lexer {
}

std::optional<float> takeF32() {
if (auto t = peek()) {
if (auto f = t->getF32()) {
if (curr) {
if (auto f = curr->getF32()) {
advance();
return f;
}
Expand All @@ -334,10 +338,11 @@ struct Lexer {
}

std::optional<std::string> takeString() {
if (auto t = peek()) {
if (auto s = t->getString()) {
if (curr) {
if (auto s = curr->getString()) {
std::string ret(*s);
advance();
return std::string(*s);
return ret;
}
}
return {};
Expand Down Expand Up @@ -391,12 +396,20 @@ struct Lexer {
TextPos position() const { return position(getPos()); }

size_t getPos() const {
if (auto t = peek()) {
return getIndex() - t->span.size();
if (curr) {
return getIndex() - curr->span.size();
}
return getIndex();
}

[[nodiscard]] Err err(size_t pos, std::string reason) {
std::stringstream msg;
msg << position(pos) << ": error: " << reason;
return Err{msg.str()};
}

[[nodiscard]] Err err(std::string reason) { return err(getPos(), reason); }

private:
void skipSpace();
void lexToken();
Expand Down
Loading

0 comments on commit 097d2e2

Please sign in to comment.