Upstream version 6.35.131.0
[platform/framework/web/crosswalk.git] / src / v8 / src / third_party / xdk / xdk-code-map.h
1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef XDK_CODE_MAP_H_
6 #define XDK_CODE_MAP_H_
7
8 // ----------------------------------------------------------------------------
9 //
10 // This file contains the FunctionSnapshot and related objects declarations
11 //
12 // The FunctionSnapshot object maintains a map of JIT compiled functions.
13 // It is modified on code events(CodeAdded, CodeMoved and CodeDeleted) from
14 // V8 built-in profiler.
15 //
16 // ----------------------------------------------------------------------------
17
18 #include "xdk-types.h"
19 #include <map>
20 #include <list>
21 #include <string>
22 #include <algorithm>
23
24 namespace xdk {
25 namespace internal {
26
27 class LineMap;
28 typedef std::map<
29           v8engine::Address,  // start address of code
30           LineMap*> LineMaps;
31
32 // This class is used to record the JITted code position info for JIT
33 // code profiling.
34 class LineMap {
35  public:
36   struct LineEntry {
37     LineEntry(size_t offset, size_t line)
38       : pcOffset(offset), line(line) { }
39
40     size_t pcOffset;  // PC offset from the begining of the code trace.
41     size_t line;      // Can be either position returned from V8 assembler
42                       // (which needs to be converted to src line) or src line
43                       // number.
44   };
45
46   typedef std::list<LineEntry> Entries;
47
48   void setPosition(size_t offset, size_t line) {
49     addCodeLineEntry(LineEntry(offset, line));
50   }
51
52   inline size_t getSize() const { return m_lines.size(); }
53   const Entries* getEntries() const { return &m_lines; }
54
55  private:
56   void addCodeLineEntry(const LineEntry& entry) { m_lines.push_back(entry); }
57
58   Entries m_lines;
59 };
60
61 // This class describes the function reported with CodeAdded event.
62 class Function {
63  public:
64   explicit Function(v8engine::Address codeAddr, uint32_t codeLen,
65                     const std::string& name, const std::string& type,
66                     const LineMap* lineMap);
67
68   inline v8engine::Address getCodeAddress() const { return m_codeAddr; }
69   inline uint32_t getCodeLength() const { return m_codeLen; }
70
71   inline const std::string& getType() const { return m_type; }
72   inline const std::string& getName() const { return m_name; }
73   inline const std::string& getLogLine() const { return m_logLine; }
74
75   const LineMap& getLineMap() const { return m_lineMap; }
76
77  private:
78   v8engine::Address m_codeAddr;
79   uint32_t m_codeLen;
80   std::string m_name;
81   std::string m_type;
82   std::string m_logLine;
83   LineMap m_lineMap;
84 };
85
86 // This class describes the code range related to object of Function type.
87 // The start address and length are taken from CodeAdded event.
88 class Range {
89  public:
90   class Comparator : public std::binary_function<Range&, Range&, bool> {
91    public:
92      bool operator()(const Range& l, const Range& r) const {
93        return (l.start() + l.length() <= r.start());
94      }
95   };
96
97   Range(v8engine::Address start, uint32_t length)
98     : m_start(start), m_length(length) { }
99
100   inline v8engine::Address start() const { return m_start; }
101   inline uint32_t length() const { return m_length; }
102
103  private:
104   v8engine::Address m_start;
105   uint32_t m_length;
106 };
107
108 // This class maintains a map of JIT compiled functions.
109 // The content is changed on CodeAdded, CodeMoved and CodeDeleted events.
110 typedef std::map<Range, const Function, Range::Comparator> CodeMap;
111
112 class FunctionSnapshot {
113  public:
114   explicit FunctionSnapshot() {}
115   virtual ~FunctionSnapshot() { m_impl.clear(); }
116
117   void insert(const Function& func);
118   void move(v8engine::Address from, v8engine::Address to);
119   void remove(v8engine::Address addr);
120
121   inline const CodeMap& entries() { return m_impl; }
122
123  private:
124   FunctionSnapshot(const FunctionSnapshot&);
125   FunctionSnapshot& operator=(const FunctionSnapshot&);
126
127   void removeAll(const Range& range);
128
129   CodeMap m_impl;
130 };
131
132 } }  // namespace xdk::internal
133
134 #endif  // XDK_CODE_MAP_H_