blob: dec494a3b3526258c0ebe8231a91e321adbe3961 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2009 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28
Steve Blocka7e24c12009-10-30 11:49:00 +000029/**
30 * Constructs a mapper that maps addresses into code entries.
31 *
32 * @constructor
33 */
Steve Block1e0659c2011-05-24 12:43:12 +010034function CodeMap() {
Steve Blocka7e24c12009-10-30 11:49:00 +000035 /**
36 * Dynamic code entries. Used for JIT compiled code.
37 */
Steve Block1e0659c2011-05-24 12:43:12 +010038 this.dynamics_ = new SplayTree();
Steve Blocka7e24c12009-10-30 11:49:00 +000039
40 /**
41 * Name generator for entries having duplicate names.
42 */
Steve Block1e0659c2011-05-24 12:43:12 +010043 this.dynamicsNameGen_ = new CodeMap.NameGenerator();
Steve Blocka7e24c12009-10-30 11:49:00 +000044
45 /**
46 * Static code entries. Used for statically compiled code.
47 */
Steve Block1e0659c2011-05-24 12:43:12 +010048 this.statics_ = new SplayTree();
Steve Blocka7e24c12009-10-30 11:49:00 +000049
50 /**
51 * Libraries entries. Used for the whole static code libraries.
52 */
Steve Block1e0659c2011-05-24 12:43:12 +010053 this.libraries_ = new SplayTree();
Steve Blocka7e24c12009-10-30 11:49:00 +000054
55 /**
56 * Map of memory pages occupied with static code.
57 */
58 this.pages_ = [];
59};
60
61
62/**
63 * The number of alignment bits in a page address.
64 */
Steve Block1e0659c2011-05-24 12:43:12 +010065CodeMap.PAGE_ALIGNMENT = 12;
Steve Blocka7e24c12009-10-30 11:49:00 +000066
67
68/**
69 * Page size in bytes.
70 */
Steve Block1e0659c2011-05-24 12:43:12 +010071CodeMap.PAGE_SIZE =
72 1 << CodeMap.PAGE_ALIGNMENT;
Steve Blocka7e24c12009-10-30 11:49:00 +000073
74
75/**
76 * Adds a dynamic (i.e. moveable and discardable) code entry.
77 *
78 * @param {number} start The starting address.
Steve Block1e0659c2011-05-24 12:43:12 +010079 * @param {CodeMap.CodeEntry} codeEntry Code entry object.
Steve Blocka7e24c12009-10-30 11:49:00 +000080 */
Steve Block1e0659c2011-05-24 12:43:12 +010081CodeMap.prototype.addCode = function(start, codeEntry) {
Steve Blocka7e24c12009-10-30 11:49:00 +000082 this.dynamics_.insert(start, codeEntry);
83};
84
85
86/**
87 * Moves a dynamic code entry. Throws an exception if there is no dynamic
88 * code entry with the specified starting address.
89 *
90 * @param {number} from The starting address of the entry being moved.
91 * @param {number} to The destination address.
92 */
Steve Block1e0659c2011-05-24 12:43:12 +010093CodeMap.prototype.moveCode = function(from, to) {
Steve Blocka7e24c12009-10-30 11:49:00 +000094 var removedNode = this.dynamics_.remove(from);
95 this.dynamics_.insert(to, removedNode.value);
96};
97
98
99/**
100 * Discards a dynamic code entry. Throws an exception if there is no dynamic
101 * code entry with the specified starting address.
102 *
103 * @param {number} start The starting address of the entry being deleted.
104 */
Steve Block1e0659c2011-05-24 12:43:12 +0100105CodeMap.prototype.deleteCode = function(start) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000106 var removedNode = this.dynamics_.remove(start);
107};
108
109
110/**
111 * Adds a library entry.
112 *
113 * @param {number} start The starting address.
Steve Block1e0659c2011-05-24 12:43:12 +0100114 * @param {CodeMap.CodeEntry} codeEntry Code entry object.
Steve Blocka7e24c12009-10-30 11:49:00 +0000115 */
Steve Block1e0659c2011-05-24 12:43:12 +0100116CodeMap.prototype.addLibrary = function(
Steve Blocka7e24c12009-10-30 11:49:00 +0000117 start, codeEntry) {
118 this.markPages_(start, start + codeEntry.size);
119 this.libraries_.insert(start, codeEntry);
120};
121
122
123/**
124 * Adds a static code entry.
125 *
126 * @param {number} start The starting address.
Steve Block1e0659c2011-05-24 12:43:12 +0100127 * @param {CodeMap.CodeEntry} codeEntry Code entry object.
Steve Blocka7e24c12009-10-30 11:49:00 +0000128 */
Steve Block1e0659c2011-05-24 12:43:12 +0100129CodeMap.prototype.addStaticCode = function(
Steve Blocka7e24c12009-10-30 11:49:00 +0000130 start, codeEntry) {
131 this.statics_.insert(start, codeEntry);
132};
133
134
135/**
136 * @private
137 */
Steve Block1e0659c2011-05-24 12:43:12 +0100138CodeMap.prototype.markPages_ = function(start, end) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000139 for (var addr = start; addr <= end;
Steve Block1e0659c2011-05-24 12:43:12 +0100140 addr += CodeMap.PAGE_SIZE) {
141 this.pages_[addr >>> CodeMap.PAGE_ALIGNMENT] = 1;
Steve Blocka7e24c12009-10-30 11:49:00 +0000142 }
143};
144
145
146/**
147 * @private
148 */
Steve Block1e0659c2011-05-24 12:43:12 +0100149CodeMap.prototype.isAddressBelongsTo_ = function(addr, node) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000150 return addr >= node.key && addr < (node.key + node.value.size);
151};
152
153
154/**
155 * @private
156 */
Steve Block1e0659c2011-05-24 12:43:12 +0100157CodeMap.prototype.findInTree_ = function(tree, addr) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000158 var node = tree.findGreatestLessThan(addr);
159 return node && this.isAddressBelongsTo_(addr, node) ? node.value : null;
160};
161
162
163/**
164 * Finds a code entry that contains the specified address. Both static and
165 * dynamic code entries are considered.
166 *
167 * @param {number} addr Address.
168 */
Steve Block1e0659c2011-05-24 12:43:12 +0100169CodeMap.prototype.findEntry = function(addr) {
170 var pageAddr = addr >>> CodeMap.PAGE_ALIGNMENT;
Steve Blocka7e24c12009-10-30 11:49:00 +0000171 if (pageAddr in this.pages_) {
172 // Static code entries can contain "holes" of unnamed code.
173 // In this case, the whole library is assigned to this address.
174 return this.findInTree_(this.statics_, addr) ||
175 this.findInTree_(this.libraries_, addr);
176 }
177 var min = this.dynamics_.findMin();
178 var max = this.dynamics_.findMax();
179 if (max != null && addr < (max.key + max.value.size) && addr >= min.key) {
180 var dynaEntry = this.findInTree_(this.dynamics_, addr);
181 if (dynaEntry == null) return null;
182 // Dedupe entry name.
183 if (!dynaEntry.nameUpdated_) {
184 dynaEntry.name = this.dynamicsNameGen_.getName(dynaEntry.name);
185 dynaEntry.nameUpdated_ = true;
186 }
187 return dynaEntry;
188 }
189 return null;
190};
191
192
193/**
Leon Clarked91b9f72010-01-27 17:25:45 +0000194 * Returns a dynamic code entry using its starting address.
195 *
196 * @param {number} addr Address.
197 */
Steve Block1e0659c2011-05-24 12:43:12 +0100198CodeMap.prototype.findDynamicEntryByStartAddress =
Leon Clarked91b9f72010-01-27 17:25:45 +0000199 function(addr) {
200 var node = this.dynamics_.find(addr);
201 return node ? node.value : null;
202};
203
204
205/**
Steve Blocka7e24c12009-10-30 11:49:00 +0000206 * Returns an array of all dynamic code entries.
207 */
Steve Block1e0659c2011-05-24 12:43:12 +0100208CodeMap.prototype.getAllDynamicEntries = function() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000209 return this.dynamics_.exportValues();
210};
211
212
213/**
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000214 * Returns an array of pairs of all dynamic code entries and their addresses.
215 */
216CodeMap.prototype.getAllDynamicEntriesWithAddresses = function() {
217 return this.dynamics_.exportKeysAndValues();
218};
219
220
221/**
Steve Blocka7e24c12009-10-30 11:49:00 +0000222 * Returns an array of all static code entries.
223 */
Steve Block1e0659c2011-05-24 12:43:12 +0100224CodeMap.prototype.getAllStaticEntries = function() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000225 return this.statics_.exportValues();
226};
227
228
229/**
230 * Returns an array of all libraries entries.
231 */
Steve Block1e0659c2011-05-24 12:43:12 +0100232CodeMap.prototype.getAllLibrariesEntries = function() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000233 return this.libraries_.exportValues();
234};
235
236
237/**
238 * Creates a code entry object.
239 *
240 * @param {number} size Code entry size in bytes.
241 * @param {string} opt_name Code entry name.
242 * @constructor
243 */
Steve Block1e0659c2011-05-24 12:43:12 +0100244CodeMap.CodeEntry = function(size, opt_name) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000245 this.size = size;
246 this.name = opt_name || '';
247 this.nameUpdated_ = false;
248};
249
250
Steve Block1e0659c2011-05-24 12:43:12 +0100251CodeMap.CodeEntry.prototype.getName = function() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000252 return this.name;
253};
254
255
Steve Block1e0659c2011-05-24 12:43:12 +0100256CodeMap.CodeEntry.prototype.toString = function() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000257 return this.name + ': ' + this.size.toString(16);
258};
259
260
Steve Block1e0659c2011-05-24 12:43:12 +0100261CodeMap.NameGenerator = function() {
Steve Blockd0582a62009-12-15 09:54:21 +0000262 this.knownNames_ = {};
Steve Blocka7e24c12009-10-30 11:49:00 +0000263};
264
265
Steve Block1e0659c2011-05-24 12:43:12 +0100266CodeMap.NameGenerator.prototype.getName = function(name) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000267 if (!(name in this.knownNames_)) {
268 this.knownNames_[name] = 0;
269 return name;
270 }
271 var count = ++this.knownNames_[name];
272 return name + ' {' + count + '}';
273};