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 #ifndef TRANSFORMER_H 18 #define TRANSFORMER_H 19 20 #include <vector> 21 22 #include "instructions.h" 23 #include "pass.h" 24 #include "visitor.h" 25 #include "word_stream.h" 26 27 namespace android { 28 namespace spirit { 29 30 // Transformer is the base class for a transformation that transforms a Module. 31 // An instance of a derived class can be added to a PassQueue and applied to a 32 // Module. 33 class Transformer : public Pass, public DoNothingVisitor { 34 public: Transformer()35 Transformer() 36 : mStreamFunctions(WordStream::Create()), 37 mStreamFinal(WordStream::Create()) {} 38 ~Transformer()39 virtual ~Transformer() {} 40 41 Module *run(Module *m, int *error = nullptr) override; 42 43 std::vector<uint32_t> runAndSerialize(Module *module, 44 int *error = nullptr) override; 45 46 // Returns the module being transformed getModule()47 Module *getModule() const { return mModule; } 48 49 // Inserts a new instruction before the current instruction. 50 // Call this from a transform() method in a derived class. 51 void insert(Instruction *); 52 visit(FunctionDefinition * fdef)53 void visit(FunctionDefinition *fdef) override { 54 mShouldRecord = (mVisit == 0); 55 DoNothingVisitor::visit(fdef); 56 } 57 58 // Transforms the current instruction into a new instruction as specified by 59 // the return value. If returns nullptr, deletes the current instruction. 60 // Override this in a derived class for desired behavior. 61 #define HANDLE_INSTRUCTION(OPCODE, INST_CLASS) \ 62 virtual Instruction *transform(INST_CLASS *inst) { \ 63 return static_cast<Instruction *>(inst); \ 64 } \ 65 virtual void visit(INST_CLASS *inst) { \ 66 if (!mShouldRecord) { \ 67 return; \ 68 } \ 69 if (Instruction *transformed = transform(inst)) { \ 70 transformed->Serialize(*mStream); \ 71 } \ 72 } 73 #include "instruction_dispatches_generated.h" 74 #undef HANDLE_INSTRUCTION 75 76 private: 77 Module *mModule; 78 int mVisit; 79 bool mShouldRecord; 80 std::unique_ptr<WordStream> mStreamFunctions; 81 std::unique_ptr<WordStream> mStreamFinal; 82 WordStream *mStream; 83 }; 84 85 } // namespace spirit 86 } // namespace android 87 88 #endif // TRANSFORMER_H 89