|
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.Collections.Generic; |
|
19 using System.Text; |
|
20 using System.IO; |
|
21 using SymbianUtils.BasicTypes; |
|
22 using SymbianStructuresLib.Arm; |
|
23 using SymbianStructuresLib.Arm.Registers; |
|
24 using SymbianStructuresLib.Arm.Instructions; |
|
25 using SymbianInstructionLib.Arm.Instructions.Arm; |
|
26 using SymbianInstructionLib.Arm.Instructions.Thumb; |
|
27 |
|
28 namespace SymbianInstructionLib.Arm.Instructions.Common |
|
29 { |
|
30 public abstract class ArmBaseInstruction : IArmInstruction |
|
31 { |
|
32 #region Constructors |
|
33 protected ArmBaseInstruction( TArmInstructionSet aInstructionSet ) |
|
34 { |
|
35 iInstructionSet = aInstructionSet; |
|
36 } |
|
37 #endregion |
|
38 |
|
39 #region API |
|
40 public virtual bool Matches( uint aOpCode ) |
|
41 { |
|
42 uint masked = (uint) ( aOpCode & BitMask ); |
|
43 if ( masked == BitValue ) |
|
44 { |
|
45 return true; |
|
46 } |
|
47 |
|
48 return false; |
|
49 } |
|
50 |
|
51 protected void SetMask( string aMostSignificantByte, string aLeastSignificantByte ) |
|
52 { |
|
53 SetMask( aMostSignificantByte + aLeastSignificantByte ); |
|
54 } |
|
55 |
|
56 protected void SetMask( string aSignificantBitValues ) |
|
57 { |
|
58 int bit = 0; |
|
59 uint mask = 0; |
|
60 uint value = 0; |
|
61 |
|
62 // Loop through all characters in the mask, starting from the RHS, working |
|
63 // towards the left hand side. |
|
64 int count = aSignificantBitValues.Length; |
|
65 for ( int charIndex = count - 1; charIndex >= 0; charIndex-- ) |
|
66 { |
|
67 // Get a character from the string |
|
68 char c = aSignificantBitValues[ charIndex ]; |
|
69 // |
|
70 if ( c == KBitIsSet ) |
|
71 { |
|
72 mask |= (uint) ( 1 << bit ); |
|
73 value |= (uint) ( 1 << bit ); |
|
74 } |
|
75 else if ( c == KBitIsClear ) |
|
76 { |
|
77 mask |= (uint) ( 1 << bit ); |
|
78 } |
|
79 else if ( c == KBitIsNotApplicable ) |
|
80 { |
|
81 } |
|
82 // |
|
83 if ( c != ' ' ) |
|
84 { |
|
85 ++bit; |
|
86 } |
|
87 } |
|
88 // |
|
89 iBitMask = mask; |
|
90 iBitValue = value; |
|
91 } |
|
92 #endregion |
|
93 |
|
94 #region Properties |
|
95 protected uint BitMask |
|
96 { |
|
97 get { return iBitMask; } |
|
98 } |
|
99 |
|
100 protected uint BitValue |
|
101 { |
|
102 get { return iBitValue; } |
|
103 } |
|
104 |
|
105 internal virtual int SortOrder |
|
106 { |
|
107 get { return 0; } |
|
108 } |
|
109 #endregion |
|
110 |
|
111 #region From IArmInstruction |
|
112 public TArmInstructionSet AIType |
|
113 { |
|
114 get { return iInstructionSet; } |
|
115 } |
|
116 |
|
117 public TArmInstructionGroup AIGroup |
|
118 { |
|
119 get { return iGroup; } |
|
120 internal set |
|
121 { |
|
122 iGroup = value; |
|
123 } |
|
124 } |
|
125 |
|
126 public TArmInstructionTarget AITarget |
|
127 { |
|
128 get { return iTarget; } |
|
129 internal set { iTarget = value; } |
|
130 } |
|
131 |
|
132 public TArmInstructionCondition AIConditionCode |
|
133 { |
|
134 get { return iConditionCode; } |
|
135 internal set { iConditionCode = value; } |
|
136 } |
|
137 |
|
138 public SymUInt32 AIRawValue |
|
139 { |
|
140 get { return iRawValue; } |
|
141 internal set |
|
142 { |
|
143 if ( iRawValue != value ) |
|
144 { |
|
145 iRawValue = value; |
|
146 OnRawValueAssigned(); |
|
147 } |
|
148 } |
|
149 } |
|
150 |
|
151 public string AIBinaryString |
|
152 { |
|
153 get |
|
154 { |
|
155 return iRawValue.Binary; |
|
156 } |
|
157 } |
|
158 |
|
159 public string AIHexString |
|
160 { |
|
161 get |
|
162 { |
|
163 uint size = this.AISize * 2; |
|
164 string format = "x" + size; |
|
165 string ret = this.AIRawValue.ToString( format ); |
|
166 return ret; |
|
167 } |
|
168 } |
|
169 |
|
170 public string AIDisassembly |
|
171 { |
|
172 get { return iDisassembly; } |
|
173 internal set { iDisassembly = value; } |
|
174 } |
|
175 |
|
176 public uint AIAddress |
|
177 { |
|
178 get { return iAddress; } |
|
179 internal set { iAddress = value; } |
|
180 } |
|
181 |
|
182 public uint AISize |
|
183 { |
|
184 get |
|
185 { |
|
186 TArmInstructionSet instSet = this.AIType; |
|
187 uint ret = (uint) instSet; |
|
188 return ret; |
|
189 } |
|
190 } |
|
191 |
|
192 public bool AIIsUnknown |
|
193 { |
|
194 get { return ( this is Arm_Unknown ) || ( this is Thumb_Unknown ); } |
|
195 } |
|
196 |
|
197 public SymBit this[ int aIndex ] |
|
198 { |
|
199 get |
|
200 { |
|
201 SymBit ret = SymBitUtils.GetBit( AIRawValue, aIndex ); |
|
202 return ret; |
|
203 } |
|
204 } |
|
205 |
|
206 public bool QueryInvolvement( TArmRegisterType aRegister ) |
|
207 { |
|
208 bool source = QueryInvolvementAsSource( aRegister ); |
|
209 bool destination = QueryInvolvementAsDestination( aRegister ); |
|
210 return ( source || destination ); |
|
211 } |
|
212 |
|
213 public bool QueryInvolvementAsSource( TArmRegisterType aRegister ) |
|
214 { |
|
215 bool ret = DoQueryInvolvementAsSource( aRegister ); |
|
216 return ret; |
|
217 } |
|
218 |
|
219 public bool QueryInvolvementAsDestination( TArmRegisterType aRegister ) |
|
220 { |
|
221 bool ret = DoQueryInvolvementAsDestination( aRegister ); |
|
222 return ret; |
|
223 } |
|
224 #endregion |
|
225 |
|
226 #region Framework API |
|
227 protected virtual void OnRawValueAssigned() |
|
228 { |
|
229 } |
|
230 |
|
231 protected virtual bool DoQueryInvolvementAsSource( TArmRegisterType aRegister ) |
|
232 { |
|
233 return false; |
|
234 } |
|
235 |
|
236 protected virtual bool DoQueryInvolvementAsDestination( TArmRegisterType aRegister ) |
|
237 { |
|
238 return false; |
|
239 } |
|
240 #endregion |
|
241 |
|
242 #region From System.Object |
|
243 public override string ToString() |
|
244 { |
|
245 StringBuilder t = new StringBuilder(); |
|
246 // |
|
247 if ( this.AIRawValue == 0u ) |
|
248 { |
|
249 t.Append( GetType().Name ); |
|
250 } |
|
251 else |
|
252 { |
|
253 const int padAmount = 60; |
|
254 // |
|
255 string type = "A"; |
|
256 if ( this.AIType == TArmInstructionSet.ETHUMB ) |
|
257 { |
|
258 type = "T"; |
|
259 } |
|
260 // |
|
261 bool isRecognised = this.AIIsUnknown == false; |
|
262 t.AppendFormat( "[{0:x8}] [{1}] 0x{2:x8} {3} {4} ", |
|
263 this.AIAddress, |
|
264 type, |
|
265 this.AIRawValue, |
|
266 this.AIBinaryString, |
|
267 isRecognised ? "Y" : "?" ); |
|
268 // |
|
269 string disassembly = this.AIDisassembly; |
|
270 string typeName = isRecognised ? this.GetType().Name : string.Empty; |
|
271 // |
|
272 if ( !isRecognised ) |
|
273 { |
|
274 t.AppendFormat( " {0} ", typeName.PadRight( padAmount, ' ' ) ); |
|
275 t.Append( disassembly ); |
|
276 } |
|
277 else |
|
278 { |
|
279 if ( disassembly.Contains( "r13" ) || disassembly.Contains( "SP" ) ) |
|
280 { |
|
281 t.AppendFormat( "* {0} ", disassembly.PadRight( padAmount, ' ' ) ); |
|
282 t.Append( typeName ); |
|
283 } |
|
284 else |
|
285 { |
|
286 t.AppendFormat( " {0} ", disassembly.PadRight( padAmount, ' ' ) ); |
|
287 t.Append( typeName ); |
|
288 } |
|
289 } |
|
290 } |
|
291 // |
|
292 return t.ToString(); |
|
293 } |
|
294 #endregion |
|
295 |
|
296 #region Internal constants |
|
297 private const char KBitIsSet = '1'; |
|
298 private const char KBitIsClear = '0'; |
|
299 private const char KBitIsNotApplicable = '#'; |
|
300 #endregion |
|
301 |
|
302 #region Data members |
|
303 private readonly TArmInstructionSet iInstructionSet; |
|
304 private SymUInt32 iRawValue = new SymUInt32( 0 ); |
|
305 private TArmInstructionGroup iGroup = TArmInstructionGroup.EGroupUndefined; |
|
306 private TArmInstructionTarget iTarget = TArmInstructionTarget.EDefault; |
|
307 private TArmInstructionCondition iConditionCode = TArmInstructionCondition.ENotApplicable; |
|
308 private uint iAddress = 0; |
|
309 private string iDisassembly = string.Empty; |
|
310 private uint iBitMask = 0; |
|
311 private uint iBitValue = 0; |
|
312 #endregion |
|
313 } |
|
314 } |
|
315 |