transformer.cpp revision 3f30b6202dd5ad6ff66959131d216405850ed152
1/*
2 * Copyright 2017, The Android Open Source Project
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#include "transformer.h"
18
19#include "module.h"
20
21namespace android {
22namespace spirit {
23
24Module *Transformer::run(Module *module, int *error) {
25  auto words = runAndSerialize(module, error);
26  std::unique_ptr<InputWordStream> IS(InputWordStream::Create(words));
27  return Deserialize<Module>(*IS);
28}
29
30std::vector<uint32_t> Transformer::runAndSerialize(Module *m, int *error) {
31  mModule = m;
32
33  // Since contents in the decoration or global section may change, transform
34  // and serialize the function definitions first.
35  mVisit = 0;
36  mShouldRecord = false;
37  mStream = mStreamFunctions.get();
38  m->accept(this);
39
40  // Record in the annotation section any new annotations added
41  m->consolidateAnnotations();
42
43  // After the functions are transformed, serialize the other sections to
44  // capture any changes made during the function transformation, and append
45  // the new words from function serialization.
46
47  mVisit = 1;
48  mShouldRecord = true;
49  mStream = mStreamFinal.get();
50
51  // TODO fix Module::accept() to have the header serialization code there
52  m->SerializeHeader(*mStream);
53  m->accept(this);
54
55  auto output = mStream->getWords();
56  auto functions = mStreamFunctions->getWords();
57  output.insert(output.end(), functions.begin(), functions.end());
58
59  if (error) {
60    *error = 0;
61  }
62
63  return output;
64}
65
66void Transformer::insert(Instruction *inst) {
67  // TODO: warn on nullptr inst
68  inst->Serialize(*mStream);
69}
70
71} // namespace spirit
72} // namespace android
73