Commit 37ad3aa9 authored by titzer's avatar titzer Committed by Commit bot

[wasm] Introduce Encoder::GetLocal and Encoder::SetLocal utilities.

R=bradnelson@chromium.org,aseemgarg@chromium.org
BUG=

Review URL: https://codereview.chromium.org/1895013002

Cr-Commit-Position: refs/heads/master@{#35598}
parent 3afb3324
......@@ -510,30 +510,28 @@ class AsmWasmBuilderImpl : public AstVisitor {
void VisitVariableProxy(VariableProxy* expr) {
if (in_function_) {
Variable* var = expr->var();
LocalType var_type = TypeOf(expr);
if (is_set_op_) {
is_set_op_ = false;
if (var->IsContextSlot()) {
current_function_builder_->Emit(kExprStoreGlobal);
return current_function_builder_->EmitWithVarInt(
kExprStoreGlobal, LookupOrInsertGlobal(var, var_type));
} else {
current_function_builder_->Emit(kExprSetLocal);
return current_function_builder_->EmitSetLocal(
LookupOrInsertLocal(var, var_type));
}
is_set_op_ = false;
} else {
if (VisitStdlibConstant(var)) {
return;
}
if (var->IsContextSlot()) {
current_function_builder_->Emit(kExprLoadGlobal);
return current_function_builder_->EmitWithVarInt(
kExprLoadGlobal, LookupOrInsertGlobal(var, var_type));
} else {
current_function_builder_->Emit(kExprGetLocal);
return current_function_builder_->EmitGetLocal(
LookupOrInsertLocal(var, var_type));
}
}
LocalType var_type = TypeOf(expr);
DCHECK_NE(kAstStmt, var_type);
if (var->IsContextSlot()) {
AddLeb128(LookupOrInsertGlobal(var, var_type), false);
} else {
AddLeb128(LookupOrInsertLocal(var, var_type), true);
}
}
}
......@@ -1387,19 +1385,6 @@ class AsmWasmBuilderImpl : public AstVisitor {
}
}
void AddLeb128(uint32_t index, bool is_local) {
std::vector<uint8_t> index_vec = UnsignedLEB128From(index);
if (is_local) {
uint32_t pos_of_index[1] = {0};
current_function_builder_->EmitCode(
&index_vec[0], static_cast<uint32_t>(index_vec.size()), pos_of_index,
1);
} else {
current_function_builder_->EmitCode(
&index_vec[0], static_cast<uint32_t>(index_vec.size()));
}
}
void VisitCompareOperation(CompareOperation* expr) {
switch (expr->op()) {
BINOP_CASE(Token::EQ, Eq, NON_SIGNED_BINOP, false);
......
......@@ -150,6 +150,15 @@ void WasmFunctionBuilder::EmitCode(const byte* code, uint32_t code_size) {
EmitCode(code, code_size, nullptr, 0);
}
void WasmFunctionBuilder::EmitGetLocal(uint32_t local_index) {
local_indices_.push_back(static_cast<uint32_t>(body_.size() + 1));
EmitWithVarInt(kExprGetLocal, local_index);
}
void WasmFunctionBuilder::EmitSetLocal(uint32_t local_index) {
local_indices_.push_back(static_cast<uint32_t>(body_.size() + 1));
EmitWithVarInt(kExprSetLocal, local_index);
}
void WasmFunctionBuilder::EmitCode(const byte* code, uint32_t code_size,
const uint32_t* local_indices,
......
......@@ -54,6 +54,8 @@ class WasmFunctionBuilder : public ZoneObject {
void EmitCode(const byte* code, uint32_t code_size,
const uint32_t* local_indices, uint32_t indices_size);
void Emit(WasmOpcode opcode);
void EmitGetLocal(uint32_t index);
void EmitSetLocal(uint32_t index);
void EmitWithU8(WasmOpcode opcode, const byte immediate);
void EmitWithU8U8(WasmOpcode opcode, const byte imm1, const byte imm2);
void EmitWithVarInt(WasmOpcode opcode, uint32_t immediate);
......
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