Commit e1cf815d authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[wasm] [cleanup] Use consistent types in decoder

For stack sizes and control depths, we were sometimes using uint32_t
and sometimes size_t.
This CL switches to uint32_t consistently.

R=titzer@chromium.org

Change-Id: I5ce3d63832bc926584b153cf248006cd78d77b97
Reviewed-on: https://chromium-review.googlesource.com/645861Reviewed-by: 's avatarBen Titzer <titzer@chromium.org>
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#47769}
parent f1ec44e2
...@@ -393,7 +393,7 @@ struct Merge { ...@@ -393,7 +393,7 @@ struct Merge {
Value first; Value first;
} vals; // Either multiple values or a single value. } vals; // Either multiple values or a single value.
Value& operator[](size_t i) { Value& operator[](uint32_t i) {
DCHECK_GT(arity, i); DCHECK_GT(arity, i);
return arity == 1 ? vals.first : vals.array[i]; return arity == 1 ? vals.first : vals.array[i];
} }
...@@ -413,7 +413,7 @@ template <typename Value> ...@@ -413,7 +413,7 @@ template <typename Value>
struct ControlBase { struct ControlBase {
const byte* pc; const byte* pc;
ControlKind kind; ControlKind kind;
size_t stack_depth; // stack height at the beginning of the construct. uint32_t stack_depth; // stack height at the beginning of the construct.
bool unreachable; // The current block has been ended. bool unreachable; // The current block has been ended.
// Values merged into the end of this control construct. // Values merged into the end of this control construct.
...@@ -430,19 +430,19 @@ struct ControlBase { ...@@ -430,19 +430,19 @@ struct ControlBase {
// Named constructors. // Named constructors.
static ControlBase Block(const byte* pc, size_t stack_depth) { static ControlBase Block(const byte* pc, size_t stack_depth) {
return {pc, kControlBlock, stack_depth, false, {}}; return {pc, kControlBlock, static_cast<uint32_t>(stack_depth), false, {}};
} }
static ControlBase If(const byte* pc, size_t stack_depth) { static ControlBase If(const byte* pc, size_t stack_depth) {
return {pc, kControlIf, stack_depth, false, {}}; return {pc, kControlIf, static_cast<uint32_t>(stack_depth), false, {}};
} }
static ControlBase Loop(const byte* pc, size_t stack_depth) { static ControlBase Loop(const byte* pc, size_t stack_depth) {
return {pc, kControlLoop, stack_depth, false, {}}; return {pc, kControlLoop, static_cast<uint32_t>(stack_depth), false, {}};
} }
static ControlBase Try(const byte* pc, size_t stack_depth) { static ControlBase Try(const byte* pc, size_t stack_depth) {
return {pc, kControlTry, stack_depth, false, {}}; return {pc, kControlTry, static_cast<uint32_t>(stack_depth), false, {}};
} }
}; };
...@@ -561,8 +561,10 @@ class WasmDecoder : public Decoder { ...@@ -561,8 +561,10 @@ class WasmDecoder : public Decoder {
ZoneVector<ValueType>* local_types_; ZoneVector<ValueType>* local_types_;
size_t total_locals() const { uint32_t total_locals() const {
return local_types_ == nullptr ? 0 : local_types_->size(); return local_types_ == nullptr
? 0
: static_cast<uint32_t>(local_types_->size());
} }
static bool DecodeLocals(Decoder* decoder, const FunctionSig* sig, static bool DecodeLocals(Decoder* decoder, const FunctionSig* sig,
...@@ -617,7 +619,7 @@ class WasmDecoder : public Decoder { ...@@ -617,7 +619,7 @@ class WasmDecoder : public Decoder {
} }
static BitVector* AnalyzeLoopAssignment(Decoder* decoder, const byte* pc, static BitVector* AnalyzeLoopAssignment(Decoder* decoder, const byte* pc,
int locals_count, Zone* zone) { uint32_t locals_count, Zone* zone) {
if (pc >= decoder->end()) return nullptr; if (pc >= decoder->end()) return nullptr;
if (*pc != kExprLoop) return nullptr; if (*pc != kExprLoop) return nullptr;
...@@ -1123,7 +1125,7 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -1123,7 +1125,7 @@ class WasmFullDecoder : public WasmDecoder<validate> {
return &stack_[stack_.size() - depth - 1]; return &stack_[stack_.size() - depth - 1];
} }
inline Value& GetMergeValueFromStack(Control* c, size_t i) { inline Value& GetMergeValueFromStack(Control* c, uint32_t i) {
DCHECK_GT(c->merge.arity, i); DCHECK_GT(c->merge.arity, i);
DCHECK_GE(stack_.size(), c->stack_depth + c->merge.arity); DCHECK_GE(stack_.size(), c->stack_depth + c->merge.arity);
return stack_[stack_.size() - c->merge.arity + i]; return stack_[stack_.size() - c->merge.arity + i];
...@@ -2022,7 +2024,7 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -2022,7 +2024,7 @@ class WasmFullDecoder : public WasmDecoder<validate> {
Value Pop() { Value Pop() {
DCHECK(!control_.empty()); DCHECK(!control_.empty());
size_t limit = control_.back().stack_depth; uint32_t limit = control_.back().stack_depth;
if (stack_.size() <= limit) { if (stack_.size() <= limit) {
// Popping past the current control start in reachable code. // Popping past the current control start in reachable code.
if (!VALIDATE(control_.back().unreachable)) { if (!VALIDATE(control_.back().unreachable)) {
...@@ -2049,7 +2051,7 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -2049,7 +2051,7 @@ class WasmFullDecoder : public WasmDecoder<validate> {
bool TypeCheckMergeValues(Control* c) { bool TypeCheckMergeValues(Control* c) {
DCHECK_GE(stack_.size(), c->stack_depth + c->merge.arity); DCHECK_GE(stack_.size(), c->stack_depth + c->merge.arity);
// Typecheck the topmost {c->merge.arity} values on the stack. // Typecheck the topmost {c->merge.arity} values on the stack.
for (size_t i = 0; i < c->merge.arity; ++i) { for (uint32_t i = 0; i < c->merge.arity; ++i) {
auto& val = GetMergeValueFromStack(c, i); auto& val = GetMergeValueFromStack(c, i);
auto& old = c->merge[i]; auto& old = c->merge[i];
if (val.type != old.type) { if (val.type != old.type) {
...@@ -2058,7 +2060,7 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -2058,7 +2060,7 @@ class WasmFullDecoder : public WasmDecoder<validate> {
// If it is not polymorphic, this is a type error. // If it is not polymorphic, this is a type error.
if (!VALIDATE(val.type == kWasmVar)) { if (!VALIDATE(val.type == kWasmVar)) {
this->errorf( this->errorf(
this->pc_, "type error in merge[%zu] (expected %s, got %s)", i, this->pc_, "type error in merge[%u] (expected %s, got %s)", i,
WasmOpcodes::TypeName(old.type), WasmOpcodes::TypeName(val.type)); WasmOpcodes::TypeName(old.type), WasmOpcodes::TypeName(val.type));
return false; return false;
} }
...@@ -2072,13 +2074,15 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -2072,13 +2074,15 @@ class WasmFullDecoder : public WasmDecoder<validate> {
bool TypeCheckFallThru(Control* c) { bool TypeCheckFallThru(Control* c) {
DCHECK_EQ(c, &control_.back()); DCHECK_EQ(c, &control_.back());
if (!validate) return true; if (!validate) return true;
size_t expected = c->merge.arity; uint32_t expected = c->merge.arity;
size_t actual = stack_.size() - c->stack_depth; DCHECK_GE(stack_.size(), c->stack_depth);
uint32_t actual = static_cast<uint32_t>(stack_.size()) - c->stack_depth;
// Fallthrus must match the arity of the control exactly. // Fallthrus must match the arity of the control exactly.
if (!InsertUnreachablesIfNecessary(expected, actual) || actual > expected) { if (!InsertUnreachablesIfNecessary(expected, actual) || actual > expected) {
this->errorf(this->pc_, this->errorf(
"expected %u elements on the stack for fallthru to @%d", this->pc_,
c->merge.arity, startrel(c->pc)); "expected %u elements on the stack for fallthru to @%d, found %u",
expected, startrel(c->pc), actual);
return false; return false;
} }
...@@ -2092,17 +2096,21 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -2092,17 +2096,21 @@ class WasmFullDecoder : public WasmDecoder<validate> {
return true; return true;
} }
// Breaks must have at least the number of values expected; can have more. // Breaks must have at least the number of values expected; can have more.
size_t expected = c->merge.arity; uint32_t expected = c->merge.arity;
size_t actual = stack_.size() - control_.back().stack_depth; DCHECK_GE(stack_.size(), control_.back().stack_depth);
uint32_t actual =
static_cast<uint32_t>(stack_.size()) - control_.back().stack_depth;
if (!InsertUnreachablesIfNecessary(expected, actual)) { if (!InsertUnreachablesIfNecessary(expected, actual)) {
this->errorf(this->pc_, "expected %u elements on the stack for br to @%d", this->errorf(this->pc_,
c->merge.arity, startrel(c->pc)); "expected %u elements on the stack for br to @%d, found %u",
expected, startrel(c->pc), actual);
return false; return false;
} }
return TypeCheckMergeValues(c); return TypeCheckMergeValues(c);
} }
inline bool InsertUnreachablesIfNecessary(size_t expected, size_t actual) { inline bool InsertUnreachablesIfNecessary(uint32_t expected,
uint32_t actual) {
if (V8_LIKELY(actual >= expected)) { if (V8_LIKELY(actual >= expected)) {
return true; // enough actual values are there. return true; // enough actual values are there.
} }
......
...@@ -528,9 +528,10 @@ class WasmGraphBuildingInterface { ...@@ -528,9 +528,10 @@ class WasmGraphBuildingInterface {
const bool first = target->state == SsaEnv::kUnreachable; const bool first = target->state == SsaEnv::kUnreachable;
Goto(decoder, ssa_env_, target); Goto(decoder, ssa_env_, target);
size_t avail = decoder->stack_size() - decoder->control_at(0)->stack_depth; uint32_t avail =
size_t start = avail >= c->merge.arity ? 0 : c->merge.arity - avail; decoder->stack_size() - decoder->control_at(0)->stack_depth;
for (size_t i = start; i < c->merge.arity; ++i) { uint32_t start = avail >= c->merge.arity ? 0 : c->merge.arity - avail;
for (uint32_t i = start; i < c->merge.arity; ++i) {
auto& val = decoder->GetMergeValueFromStack(c, i); auto& val = decoder->GetMergeValueFromStack(c, i);
auto& old = c->merge[i]; auto& old = c->merge[i];
DCHECK_NOT_NULL(val.node); DCHECK_NOT_NULL(val.node);
...@@ -638,8 +639,7 @@ class WasmGraphBuildingInterface { ...@@ -638,8 +639,7 @@ class WasmGraphBuildingInterface {
env->effect = builder_->EffectPhi(1, &env->effect, env->control); env->effect = builder_->EffectPhi(1, &env->effect, env->control);
builder_->Terminate(env->effect, env->control); builder_->Terminate(env->effect, env->control);
BitVector* assigned = WasmDecoder<true>::AnalyzeLoopAssignment( BitVector* assigned = WasmDecoder<true>::AnalyzeLoopAssignment(
decoder, decoder->pc(), static_cast<int>(decoder->total_locals()), decoder, decoder->pc(), decoder->total_locals(), decoder->zone());
decoder->zone());
if (decoder->failed()) return env; if (decoder->failed()) return env;
if (assigned != nullptr) { if (assigned != nullptr) {
// Only introduce phis for variables assigned in this loop. // Only introduce phis for variables assigned in this loop.
...@@ -887,7 +887,7 @@ bool PrintRawWasmCode(AccountingAllocator* allocator, const FunctionBody& body, ...@@ -887,7 +887,7 @@ bool PrintRawWasmCode(AccountingAllocator* allocator, const FunctionBody& body,
os << RawOpcodeName(opcode) << ","; os << RawOpcodeName(opcode) << ",";
for (size_t j = 1; j < length; ++j) { for (unsigned j = 1; j < length; ++j) {
os << " 0x" << AsHex(i.pc()[j], 2) << ","; os << " 0x" << AsHex(i.pc()[j], 2) << ",";
} }
...@@ -957,7 +957,7 @@ BitVector* AnalyzeLoopAssignmentForTesting(Zone* zone, size_t num_locals, ...@@ -957,7 +957,7 @@ BitVector* AnalyzeLoopAssignmentForTesting(Zone* zone, size_t num_locals,
const byte* start, const byte* end) { const byte* start, const byte* end) {
Decoder decoder(start, end); Decoder decoder(start, end);
return WasmDecoder<true>::AnalyzeLoopAssignment( return WasmDecoder<true>::AnalyzeLoopAssignment(
&decoder, start, static_cast<int>(num_locals), zone); &decoder, start, static_cast<uint32_t>(num_locals), zone);
} }
#undef TRACE #undef TRACE
......
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