From 47c9021401a69d407a3e730012824cae75dd66f9 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Tue, 24 Oct 2017 20:36:28 -0700 Subject: notation change: AST => IR (#1245) The IR is indeed a tree, but not an "abstract syntax tree" since there is no language for which it is the syntax (except in the most trivial and meaningless sense). --- src/ir/memory-utils.h | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/ir/memory-utils.h (limited to 'src/ir/memory-utils.h') diff --git a/src/ir/memory-utils.h b/src/ir/memory-utils.h new file mode 100644 index 000000000..920583f7d --- /dev/null +++ b/src/ir/memory-utils.h @@ -0,0 +1,56 @@ +/* + * Copyright 2017 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. + */ + +#ifndef wasm_ir_memory_h +#define wasm_ir_memory_h + +#include +#include + +#include "literal.h" +#include "wasm.h" + +namespace wasm { + +namespace MemoryUtils { + // flattens memory into a single data segment. returns true if successful + inline bool flatten(Memory& memory) { + if (memory.segments.size() == 0) return true; + std::vector data; + for (auto& segment : memory.segments) { + auto* offset = segment.offset->dynCast(); + if (!offset) return false; + } + for (auto& segment : memory.segments) { + auto* offset = segment.offset->dynCast(); + auto start = offset->value.getInteger(); + auto end = start + segment.data.size(); + if (end > data.size()) { + data.resize(end); + } + std::copy(segment.data.begin(), segment.data.end(), data.begin() + start); + } + memory.segments.resize(1); + memory.segments[0].offset->cast()->value = Literal(int32_t(0)); + memory.segments[0].data.swap(data); + return true; + } +}; + +} // namespace wasm + +#endif // wasm_ir_memory_h + -- cgit v1.2.3