Commit 5930e0ab authored by Michael Starzinger's avatar Michael Starzinger Committed by Commit Bot

[asm.js] Use token position instead of stream position.

This switches the parser to use token positions (i.e. {Position})
instead of stream positions (i.e. {GetPosition}) everywhere. Access to
the latter is being removed as it is unsupported when the scanner is in
rewind state anyways. This prevents "skipping" a token when seeking.

R=bradnelson@chromium.org
BUG=v8:6127

Change-Id: I9c13dd20a981061a2bccc4fb57e5c57d2a64ac5c
Reviewed-on: https://chromium-review.googlesource.com/480300Reviewed-by: 's avatarBrad Nelson <bradnelson@chromium.org>
Commit-Queue: Brad Nelson <bradnelson@chromium.org>
Cr-Commit-Position: refs/heads/master@{#44699}
parent f4a57545
...@@ -29,17 +29,17 @@ namespace wasm { ...@@ -29,17 +29,17 @@ namespace wasm {
#define FAIL_AND_RETURN(ret, msg) \ #define FAIL_AND_RETURN(ret, msg) \
failed_ = true; \ failed_ = true; \
failure_message_ = msg; \ failure_message_ = msg; \
failure_location_ = scanner_.GetPosition(); \ failure_location_ = static_cast<int>(scanner_.Position()); \
if (FLAG_trace_asm_parser) { \ if (FLAG_trace_asm_parser) { \
PrintF("[asm.js failure: %s, token: '%s', see: %s:%d]\n", msg, \ PrintF("[asm.js failure: %s, token: '%s', see: %s:%d]\n", msg, \
scanner_.Name(scanner_.Token()).c_str(), __FILE__, __LINE__); \ scanner_.Name(scanner_.Token()).c_str(), __FILE__, __LINE__); \
} \ } \
return ret; return ret;
#else #else
#define FAIL_AND_RETURN(ret, msg) \ #define FAIL_AND_RETURN(ret, msg) \
failed_ = true; \ failed_ = true; \
failure_message_ = msg; \ failure_message_ = msg; \
failure_location_ = scanner_.GetPosition(); \ failure_location_ = static_cast<int>(scanner_.Position()); \
return ret; return ret;
#endif #endif
...@@ -1302,7 +1302,8 @@ void AsmJsParser::SwitchStatement() { ...@@ -1302,7 +1302,8 @@ void AsmJsParser::SwitchStatement() {
pending_label_ = 0; pending_label_ = 0;
// TODO(bradnelson): Make less weird. // TODO(bradnelson): Make less weird.
std::vector<int32_t> cases; std::vector<int32_t> cases;
GatherCases(&cases); // Skips { implicitly. GatherCases(&cases);
EXPECT_TOKEN('{');
size_t count = cases.size() + 1; size_t count = cases.size() + 1;
for (size_t i = 0; i < count; ++i) { for (size_t i = 0; i < count; ++i) {
BareBegin(BlockKind::kOther); BareBegin(BlockKind::kOther);
...@@ -1958,17 +1959,17 @@ AsmType* AsmJsParser::BitwiseORExpression() { ...@@ -1958,17 +1959,17 @@ AsmType* AsmJsParser::BitwiseORExpression() {
call_coercion_deferred_ = nullptr; call_coercion_deferred_ = nullptr;
// TODO(bradnelson): Make it prettier. // TODO(bradnelson): Make it prettier.
bool zero = false; bool zero = false;
int old_pos; size_t old_pos;
size_t old_code; size_t old_code;
if (a->IsA(AsmType::Intish()) && CheckForZero()) { if (a->IsA(AsmType::Intish()) && CheckForZero()) {
old_pos = scanner_.GetPosition(); old_pos = scanner_.Position();
old_code = current_function_builder_->GetPosition(); old_code = current_function_builder_->GetPosition();
scanner_.Rewind(); scanner_.Rewind();
zero = true; zero = true;
} }
RECURSEn(b = BitwiseXORExpression()); RECURSEn(b = BitwiseXORExpression());
// Handle |0 specially. // Handle |0 specially.
if (zero && old_pos == scanner_.GetPosition()) { if (zero && old_pos == scanner_.Position()) {
current_function_builder_->StashCode(nullptr, old_code); current_function_builder_->StashCode(nullptr, old_code);
a = AsmType::Signed(); a = AsmType::Signed();
continue; continue;
...@@ -2440,7 +2441,7 @@ void AsmJsParser::ValidateFloatCoercion() { ...@@ -2440,7 +2441,7 @@ void AsmJsParser::ValidateFloatCoercion() {
} }
void AsmJsParser::GatherCases(std::vector<int32_t>* cases) { void AsmJsParser::GatherCases(std::vector<int32_t>* cases) {
int start = scanner_.GetPosition(); size_t start = scanner_.Position();
int depth = 0; int depth = 0;
for (;;) { for (;;) {
if (Peek('{')) { if (Peek('{')) {
......
...@@ -34,7 +34,7 @@ class AsmJsParser { ...@@ -34,7 +34,7 @@ class AsmJsParser {
explicit AsmJsParser(Isolate* isolate, Zone* zone, Handle<Script> script, explicit AsmJsParser(Isolate* isolate, Zone* zone, Handle<Script> script,
int start, int end); int start, int end);
bool Run(); bool Run();
const char* failure_message() const { return failure_message_.c_str(); } const char* failure_message() const { return failure_message_; }
int failure_location() const { return failure_location_; } int failure_location() const { return failure_location_; }
WasmModuleBuilder* module_builder() { return module_builder_; } WasmModuleBuilder* module_builder() { return module_builder_; }
const AsmTyper::StdlibSet* stdlib_uses() const { return &stdlib_uses_; } const AsmTyper::StdlibSet* stdlib_uses() const { return &stdlib_uses_; }
...@@ -115,7 +115,7 @@ class AsmJsParser { ...@@ -115,7 +115,7 @@ class AsmJsParser {
// Error Handling related // Error Handling related
bool failed_; bool failed_;
std::string failure_message_; const char* failure_message_;
int failure_location_; int failure_location_;
// Module Related. // Module Related.
......
...@@ -210,12 +210,7 @@ std::string AsmJsScanner::Name(token_t token) const { ...@@ -210,12 +210,7 @@ std::string AsmJsScanner::Name(token_t token) const {
} }
#endif #endif
int AsmJsScanner::GetPosition() const { void AsmJsScanner::Seek(size_t pos) {
DCHECK(!rewind_);
return static_cast<int>(stream_->pos());
}
void AsmJsScanner::Seek(int pos) {
stream_->Seek(pos); stream_->Seek(pos);
preceding_token_ = kUninitialized; preceding_token_ = kUninitialized;
token_ = kUninitialized; token_ = kUninitialized;
......
...@@ -62,10 +62,9 @@ class V8_EXPORT_PRIVATE AsmJsScanner { ...@@ -62,10 +62,9 @@ class V8_EXPORT_PRIVATE AsmJsScanner {
std::string Name(token_t token) const; std::string Name(token_t token) const;
#endif #endif
// Get current position (to use with Seek). // Restores old position (token after that position). Note that it is not
int GetPosition() const; // allowed to rewind right after a seek, because previous tokens are unknown.
// Restores old position (token after that position). void Seek(size_t pos);
void Seek(int pos);
// Select whether identifiers are resolved in global or local scope, // Select whether identifiers are resolved in global or local scope,
// and which scope new identifiers are added to. // and which scope new identifiers are added to.
......
...@@ -254,7 +254,7 @@ TEST_F(AsmJsScannerTest, TrailingCComment) { ...@@ -254,7 +254,7 @@ TEST_F(AsmJsScannerTest, TrailingCComment) {
TEST_F(AsmJsScannerTest, Seeking) { TEST_F(AsmJsScannerTest, Seeking) {
SetupSource("var eval do arguments function break\n"); SetupSource("var eval do arguments function break\n");
Skip(TOK(var)); Skip(TOK(var));
int old_pos = scanner.GetPosition(); size_t old_pos = scanner.Position();
Skip(TOK(eval)); Skip(TOK(eval));
Skip(TOK(do)); Skip(TOK(do));
Skip(TOK(arguments)); Skip(TOK(arguments));
...@@ -262,6 +262,7 @@ TEST_F(AsmJsScannerTest, Seeking) { ...@@ -262,6 +262,7 @@ TEST_F(AsmJsScannerTest, Seeking) {
Skip(TOK(arguments)); Skip(TOK(arguments));
scanner.Rewind(); scanner.Rewind();
scanner.Seek(old_pos); scanner.Seek(old_pos);
Skip(TOK(eval));
Skip(TOK(do)); Skip(TOK(do));
Skip(TOK(arguments)); Skip(TOK(arguments));
Skip(TOK(function)); Skip(TOK(function));
......
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