1/*
2 * Copyright 2017 WebAssembly Community Group participants
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef wasm_ir_manipulation_h
18#define wasm_ir_manipulation_h
19
20#include "wasm.h"
21
22namespace wasm::ExpressionManipulator {
23
24// Re-use a node's memory. This helps avoid allocation when optimizing.
25template<typename InputType, typename OutputType>
26inline OutputType* convert(InputType* input) {
27 static_assert(sizeof(OutputType) <= sizeof(InputType),
28 "Can only convert to a smaller size Expression node");
29 input->~InputType(); // arena-allocaed, so no destructor, but avoid UB.
30 OutputType* output = (OutputType*)(input);
31 new (output) OutputType;
32 return output;
33}
34
35// Convenience methods for certain instructions, which are common conversions
36template<typename InputType> inline Nop* nop(InputType* target) {
37 auto* ret = convert<InputType, Nop>(target);
38 ret->finalize();
39 return ret;
40}
41
42template<typename InputType>
43inline RefNull* refNull(InputType* target, Type type) {
44 assert(type.isNullable() && type.getHeapType().isBottom());
45 auto* ret = convert<InputType, RefNull>(target);
46 ret->finalize(type);
47 return ret;
48}
49
50template<typename InputType>
51inline Unreachable* unreachable(InputType* target) {
52 auto* ret = convert<InputType, Unreachable>(target);
53 ret->finalize();
54 return ret;
55}
56
57// Convert a node that allocates
58template<typename InputType, typename OutputType>
59inline OutputType* convert(InputType* input, MixedArena& allocator) {
60 assert(sizeof(OutputType) <= sizeof(InputType));
61 input->~InputType(); // arena-allocaed, so no destructor, but avoid UB.
62 OutputType* output = (OutputType*)(input);
63 new (output) OutputType(allocator);
64 return output;
65}
66
67using CustomCopier = std::function<Expression*(Expression*)>;
68Expression*
69flexibleCopy(Expression* original, Module& wasm, CustomCopier custom);
70
71inline Expression* copy(Expression* original, Module& wasm) {
72 auto copy = [](Expression* curr) { return nullptr; };
73 return flexibleCopy(original, wasm, copy);
74}
75
76// Splice an item into the middle of a block's list
77void spliceIntoBlock(Block* block, Index index, Expression* add);
78} // namespace wasm::ExpressionManipulator
79
80#endif // wams_ir_manipulation_h
81

source code of dart_sdk/third_party/binaryen/src/src/ir/manipulation.h