1 /*
2  * Copyright (C) 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 #pragma once
18 
19 #include "slicer/control_flow_graph.h"
20 #include "slicer/common.h"
21 #include "slicer/code_ir.h"
22 #include "slicer/dex_ir.h"
23 
24 #include <memory>
25 
26 // Code IR formatting visitor
27 class PrintCodeIrVisitor : public lir::Visitor {
28  public:
PrintCodeIrVisitor(std::shared_ptr<ir::DexFile> dex_ir,lir::ControlFlowGraph * cfg)29   PrintCodeIrVisitor(std::shared_ptr<ir::DexFile> dex_ir, lir::ControlFlowGraph* cfg)
30       : dex_ir_(dex_ir), cfg_(cfg) {}
31 
32  private:
33   virtual bool Visit(lir::Bytecode* bytecode) override;
34   virtual bool Visit(lir::PackedSwitchPayload* packed_switch) override;
35   virtual bool Visit(lir::SparseSwitchPayload* sparse_switch) override;
36   virtual bool Visit(lir::ArrayData* array_data) override;
37   virtual bool Visit(lir::Label* label) override;
38   virtual bool Visit(lir::CodeLocation* location) override;
39   virtual bool Visit(lir::Const32* const32) override;
40   virtual bool Visit(lir::Const64* const64) override;
41   virtual bool Visit(lir::VReg* vreg) override;
42   virtual bool Visit(lir::VRegPair* vreg_pair) override;
43   virtual bool Visit(lir::VRegList* vreg_list) override;
44   virtual bool Visit(lir::VRegRange* vreg_range) override;
45   virtual bool Visit(lir::String* string) override;
46   virtual bool Visit(lir::Type* type) override;
47   virtual bool Visit(lir::Field* field) override;
48   virtual bool Visit(lir::Method* method) override;
49   virtual bool Visit(lir::MethodHandle* method_handle) override;
50   virtual bool Visit(lir::Proto* proto) override;
51   virtual bool Visit(lir::LineNumber* line) override;
52   virtual bool Visit(lir::DbgInfoHeader* dbg_header) override;
53   virtual bool Visit(lir::DbgInfoAnnotation* dbg_annotation) override;
54   virtual bool Visit(lir::TryBlockBegin* try_begin) override;
55   virtual bool Visit(lir::TryBlockEnd* try_end) override;
56 
57   void StartInstruction(const lir::Instruction* instr);
58   void EndInstruction(const lir::Instruction* instr);
59 
60  private:
61   std::shared_ptr<ir::DexFile> dex_ir_;
62   lir::ControlFlowGraph* cfg_ = nullptr;
63   size_t current_block_index_ = 0;
64 };
65 
66 // A .dex bytecode disassembler using lir::CodeIr
67 class DexDisassembler {
68  public:
69   // The type of CFG (Control Flow Graph) used by the disassembler:
70   //    None    - no CFG, plain listing
71   //    Compact - CFG with non-exceptional flow only
72   //    Verbose - CFG modeling the EH control flow too
73   enum class CfgType { None, Compact, Verbose };
74 
75  public:
76   explicit DexDisassembler(std::shared_ptr<ir::DexFile> dex_ir, CfgType cfg_type = CfgType::None)
dex_ir_(dex_ir)77       : dex_ir_(dex_ir), cfg_type_(cfg_type) {}
78 
79   DexDisassembler(const DexDisassembler&) = delete;
80   DexDisassembler& operator=(const DexDisassembler&) = delete;
81 
82   void DumpAllMethods() const;
83   void DumpMethod(ir::EncodedMethod* ir_method) const;
84 
85  private:
86   void Disassemble(ir::EncodedMethod* ir_method) const;
87 
88  private:
89   std::shared_ptr<ir::DexFile> dex_ir_;
90   CfgType cfg_type_ = CfgType::None;
91 };
92 
93