1 /*
2  * Copyright 2019 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_table_h
18 #define wasm_ir_table_h
19 
20 #include "ir/literal-utils.h"
21 #include "wasm-traversal.h"
22 #include "wasm.h"
23 
24 namespace wasm {
25 
26 namespace TableUtils {
27 
28 struct FlatTable {
29   std::vector<Name> names;
30   bool valid;
31 
FlatTableFlatTable32   FlatTable(Table& table) {
33     valid = true;
34     for (auto& segment : table.segments) {
35       auto offset = segment.offset;
36       if (!offset->is<Const>()) {
37         // TODO: handle some non-constant segments
38         valid = false;
39         return;
40       }
41       Index start = offset->cast<Const>()->value.geti32();
42       Index end = start + segment.data.size();
43       if (end > names.size()) {
44         names.resize(end);
45       }
46       for (Index i = 0; i < segment.data.size(); i++) {
47         names[start + i] = segment.data[i];
48       }
49     }
50   }
51 };
52 
getSingletonSegment(Table & table,Module & wasm)53 inline Table::Segment& getSingletonSegment(Table& table, Module& wasm) {
54   if (table.segments.size() != 1) {
55     Fatal() << "Table doesn't have a singleton segment.";
56   }
57   return table.segments[0];
58 }
59 
60 // Appends a name to the table. This assumes the table has 0 or 1 segments,
61 // as with 2 or more it's ambiguous what we should do (use a hole in the middle
62 // or not).
63 // This works on code from wasm-ld, but on arbitrary code it may not be valid
64 // in the presence of a dynamic linking section. Specifically, we assume the
65 // module has a single table segment, and that the dylink section indicates
66 // we can validly append to that segment, see the check below.
append(Table & table,Name name,Module & wasm)67 inline Index append(Table& table, Name name, Module& wasm) {
68   auto& segment = getSingletonSegment(table, wasm);
69   auto tableIndex = segment.data.size();
70   if (wasm.dylinkSection) {
71     if (segment.data.size() != wasm.dylinkSection->tableSize) {
72       Fatal() << "Appending to the table in a module with a dylink section "
73                  "that has tableSize which indicates it wants to reserve more "
74                  "table space than the actual table elements in the module. "
75                  "We don't know how to correctly update the dylink section in "
76                  "that case.";
77     }
78     wasm.dylinkSection->tableSize++;
79   }
80   segment.data.push_back(name);
81   table.initial = table.initial + 1;
82   return tableIndex;
83 }
84 
85 // Checks if a function is already in the table. Returns that index if so,
86 // otherwise appends it.
getOrAppend(Table & table,Name name,Module & wasm)87 inline Index getOrAppend(Table& table, Name name, Module& wasm) {
88   auto& segment = getSingletonSegment(table, wasm);
89   for (Index i = 0; i < segment.data.size(); i++) {
90     if (segment.data[i] == name) {
91       return i;
92     }
93   }
94   return append(table, name, wasm);
95 }
96 
97 } // namespace TableUtils
98 
99 } // namespace wasm
100 
101 #endif // wasm_ir_table_h
102