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