|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 using System; |
|
18 using System.IO; |
|
19 using System.Collections.Generic; |
|
20 using System.Text; |
|
21 using System.Text.RegularExpressions; |
|
22 using DecompressETB.Types; |
|
23 using SymbianUtils.Range; |
|
24 |
|
25 namespace DecompressETB.Utilities |
|
26 { |
|
27 public class RVCTDiassemblyUtility |
|
28 { |
|
29 #region Constructor |
|
30 public RVCTDiassemblyUtility() |
|
31 { |
|
32 } |
|
33 #endregion |
|
34 |
|
35 #region API |
|
36 public string Disassemble( uint aAddress ) |
|
37 { |
|
38 string ret = "Unknown instruction"; |
|
39 // |
|
40 RVCTFunctionComparer comparer = new RVCTFunctionComparer(); |
|
41 RVCTDisassemblyFunction temp = new RVCTDisassemblyFunction( aAddress ); |
|
42 int pos = iFunctions.BinarySearch( temp, comparer ); |
|
43 if ( pos >= 0 && pos < iFunctions.Count ) |
|
44 { |
|
45 temp = iFunctions[ pos ]; |
|
46 System.Diagnostics.Debug.Assert( temp.AddressRange.Contains( aAddress ) ); |
|
47 // |
|
48 ret = temp[ aAddress ]; |
|
49 } |
|
50 // |
|
51 return ret; |
|
52 } |
|
53 |
|
54 public void Read( string aFileName, uint aGlobalBaseAddress ) |
|
55 { |
|
56 using ( StreamReader reader = new StreamReader( aFileName ) ) |
|
57 { |
|
58 RVCTDisassemblyFunction fn = LastFunction; |
|
59 // |
|
60 string line = reader.ReadLine(); |
|
61 while ( line != null ) |
|
62 { |
|
63 Match m = null; |
|
64 // |
|
65 m = KRegExFunction.Match( line ); |
|
66 if ( m.Success ) |
|
67 { |
|
68 if ( fn != null ) |
|
69 { |
|
70 fn.Finalise(); |
|
71 |
|
72 // Only save functions that contain disassembly |
|
73 if ( fn.Count > 0 ) |
|
74 { |
|
75 iFunctions.Add( fn ); |
|
76 } |
|
77 } |
|
78 |
|
79 fn = new RVCTDisassemblyFunction( m.Groups[ "Name" ].Value, aGlobalBaseAddress ); |
|
80 } |
|
81 else if ( fn != null ) |
|
82 { |
|
83 fn.Offer( line ); |
|
84 } |
|
85 // |
|
86 line = reader.ReadLine(); |
|
87 } |
|
88 |
|
89 // Finalise any pending function |
|
90 if ( fn != null ) |
|
91 { |
|
92 fn.Finalise(); |
|
93 } |
|
94 } |
|
95 } |
|
96 #endregion |
|
97 |
|
98 #region Properties |
|
99 private RVCTDisassemblyFunction LastFunction |
|
100 { |
|
101 get |
|
102 { |
|
103 RVCTDisassemblyFunction ret = null; |
|
104 int count = iFunctions.Count; |
|
105 if ( count > 0 ) |
|
106 { |
|
107 ret = iFunctions[ count - 1 ]; |
|
108 } |
|
109 return ret; |
|
110 } |
|
111 } |
|
112 #endregion |
|
113 |
|
114 #region Internal classes |
|
115 class RVCTFunctionComparer : IComparer<RVCTDisassemblyFunction> |
|
116 { |
|
117 public int Compare( RVCTDisassemblyFunction aLeft, RVCTDisassemblyFunction aRight ) |
|
118 { |
|
119 int ret = -1; |
|
120 // |
|
121 AddressRange lr = aLeft.AddressRange; |
|
122 AddressRange rr = aRight.AddressRange; |
|
123 // |
|
124 if ( lr.Contains( rr ) || rr.Contains( lr ) ) |
|
125 { |
|
126 ret = 0; |
|
127 } |
|
128 else |
|
129 { |
|
130 ret = lr.CompareTo( rr ); |
|
131 } |
|
132 // |
|
133 return ret; |
|
134 } |
|
135 } |
|
136 #endregion |
|
137 |
|
138 #region Internal constants |
|
139 private static readonly Regex KRegExFunction = new Regex( |
|
140 @"^\s{4}(?<Name>[A-Za-z_0-9]+)$", |
|
141 RegexOptions.Multiline |
|
142 | RegexOptions.CultureInvariant |
|
143 | RegexOptions.IgnorePatternWhitespace |
|
144 | RegexOptions.Compiled |
|
145 ); |
|
146 #endregion |
|
147 |
|
148 #region Data members |
|
149 private List<RVCTDisassemblyFunction> iFunctions = new List<RVCTDisassemblyFunction>(); |
|
150 #endregion |
|
151 } |
|
152 |
|
153 internal class RVCTDisassemblyFunction |
|
154 { |
|
155 #region Constructor |
|
156 public RVCTDisassemblyFunction( string aName, uint aBaseAddress ) |
|
157 { |
|
158 iName = aName; |
|
159 iBaseAddress = aBaseAddress; |
|
160 } |
|
161 |
|
162 public RVCTDisassemblyFunction( uint aAddress ) |
|
163 { |
|
164 iName = string.Empty; |
|
165 iBaseAddress = aAddress; |
|
166 iAddressRange = new AddressRange( aAddress, aAddress ); |
|
167 } |
|
168 #endregion |
|
169 |
|
170 #region API |
|
171 internal void Finalise() |
|
172 { |
|
173 int size = InstructionSize; |
|
174 iAddressRange = new AddressRange( iFirstInstructionAddress, iFirstInstructionAddress + ( iInstructions.Count * size ) - 1 ); |
|
175 } |
|
176 |
|
177 internal void Offer( string aLine ) |
|
178 { |
|
179 Match m = KRegExInstruction.Match( aLine ); |
|
180 if ( m.Success ) |
|
181 { |
|
182 // get offset address |
|
183 Group addressGroup = m.Groups[ "Address" ]; |
|
184 uint address = uint.Parse( addressGroup.Value, System.Globalization.NumberStyles.HexNumber ) - KCodeBase; |
|
185 |
|
186 // Convert to global address |
|
187 address += iBaseAddress; |
|
188 if ( iFirstInstructionAddress == 0 ) |
|
189 { |
|
190 iFirstInstructionAddress = address; |
|
191 } |
|
192 |
|
193 // Create record |
|
194 string text = aLine.Substring( addressGroup.Index + addressGroup.Length + 1 ).Trim(); |
|
195 iInstructions.Add( address, text ); |
|
196 } |
|
197 } |
|
198 #endregion |
|
199 |
|
200 #region Properties |
|
201 public int Count |
|
202 { |
|
203 get { return iInstructions.Count; } |
|
204 } |
|
205 |
|
206 public int InstructionSize |
|
207 { |
|
208 get |
|
209 { |
|
210 int ret = 4; |
|
211 // |
|
212 if ( iInstructions.Count >= 2 ) |
|
213 { |
|
214 List<uint> addresses = new List<uint>(); |
|
215 // |
|
216 foreach ( KeyValuePair<uint, string> i in iInstructions ) |
|
217 { |
|
218 addresses.Add( i.Key ); |
|
219 if ( addresses.Count == 2 ) |
|
220 { |
|
221 break; |
|
222 } |
|
223 } |
|
224 // |
|
225 if ( addresses.Count == 2 ) |
|
226 { |
|
227 int diff = (int) ( addresses[ 1 ] - addresses[ 0 ] ); |
|
228 ret = diff; |
|
229 } |
|
230 } |
|
231 // |
|
232 return ret; |
|
233 } |
|
234 } |
|
235 |
|
236 public AddressRange AddressRange |
|
237 { |
|
238 get |
|
239 { |
|
240 return iAddressRange; |
|
241 } |
|
242 } |
|
243 |
|
244 internal string this[ uint aAddress ] |
|
245 { |
|
246 get |
|
247 { |
|
248 string ret = "??? No exact match"; |
|
249 // |
|
250 if ( iInstructions.ContainsKey( aAddress ) ) |
|
251 { |
|
252 ret = iInstructions[ aAddress ]; |
|
253 } |
|
254 // |
|
255 return ret; |
|
256 } |
|
257 } |
|
258 #endregion |
|
259 |
|
260 #region Internal constants |
|
261 private const uint KCodeBase = 0x8000; |
|
262 private static readonly Regex KRegExInstruction = new Regex( |
|
263 "^\\s{8}\r\n0x(?<Address>.{8}):\r\n\\s{4}\r\n(?<Instruction>.{8})\r\n" + |
|
264 "\\s{4}\r\n(?<Ascii>.{4})\r\n\\s{4}\r\n(?<Memnonic>\\p{Lu}*)\r\n\\s+\r\n" + |
|
265 "(?<Params>.+)\r\n$", |
|
266 RegexOptions.Multiline |
|
267 | RegexOptions.CultureInvariant |
|
268 | RegexOptions.IgnorePatternWhitespace |
|
269 | RegexOptions.Compiled |
|
270 ); |
|
271 #endregion |
|
272 |
|
273 #region Data members |
|
274 private readonly string iName; |
|
275 private readonly uint iBaseAddress; |
|
276 private uint iFirstInstructionAddress; |
|
277 private AddressRange iAddressRange; |
|
278 private Dictionary<uint, string> iInstructions = new Dictionary<uint, string>(); |
|
279 #endregion |
|
280 } |
|
281 } |