• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/common.h"
20 #include "slicer/code_ir.h"
21 #include "slicer/dex_ir.h"
22 #include "slicer/control_flow_graph.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::LineNumber* line) override;
50   virtual bool Visit(lir::DbgInfoHeader* dbg_header) override;
51   virtual bool Visit(lir::DbgInfoAnnotation* dbg_annotation) override;
52   virtual bool Visit(lir::TryBlockBegin* try_begin) override;
53   virtual bool Visit(lir::TryBlockEnd* try_end) override;
54 
55   void StartInstruction(const lir::Instruction* instr);
56   void EndInstruction(const lir::Instruction* instr);
57 
58  private:
59   std::shared_ptr<ir::DexFile> dex_ir_;
60   lir::ControlFlowGraph* cfg_ = nullptr;
61   size_t current_block_index_ = 0;
62 };
63 
64 // A .dex bytecode dissasembler using lir::CodeIr
65 class DexDissasembler {
66  public:
67   // The type of CFG (Control Flow Graph) used by the dissasembler:
68   //    None    - no CFG, plain listing
69   //    Compact - CFG with non-exceptional flow only
70   //    Verbose - CFG modeling the EH control flow too
71   enum class CfgType { None, Compact, Verbose };
72 
73  public:
74   explicit DexDissasembler(std::shared_ptr<ir::DexFile> dex_ir, CfgType cfg_type = CfgType::None)
dex_ir_(dex_ir)75       : dex_ir_(dex_ir), cfg_type_(cfg_type) {}
76 
77   DexDissasembler(const DexDissasembler&) = delete;
78   DexDissasembler& operator=(const DexDissasembler&) = delete;
79 
80   void DumpAllMethods() const;
81   void DumpMethod(ir::EncodedMethod* ir_method) const;
82 
83  private:
84   void Dissasemble(ir::EncodedMethod* ir_method) const;
85 
86  private:
87   std::shared_ptr<ir::DexFile> dex_ir_;
88   CfgType cfg_type_ = CfgType::None;
89 };
90 
91