crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbianInstructionLib/Arm/Library/ArmLibrary.cs
changeset 0 818e61de6cd1
equal deleted inserted replaced
-1:000000000000 0:818e61de6cd1
       
     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 SymbianUtils.Tracer;
       
    21 using SymbianUtils.PluginManager;
       
    22 using SymbianStructuresLib.Arm;
       
    23 using SymbianStructuresLib.Arm.Disassembler;
       
    24 using SymbianStructuresLib.Arm.Instructions;
       
    25 using SymbianInstructionLib.Arm.Instructions.Arm;
       
    26 using SymbianInstructionLib.Arm.Instructions.Thumb;
       
    27 using SymbianInstructionLib.Arm.Instructions.Common;
       
    28 using System.Reflection;
       
    29 
       
    30 namespace SymbianInstructionLib.Arm.Library
       
    31 {
       
    32     public class ArmLibrary
       
    33     {
       
    34         #region Constructors
       
    35         public ArmLibrary()
       
    36         {
       
    37         }
       
    38 
       
    39         static ArmLibrary()
       
    40         {
       
    41             // Load all the ARM instructions
       
    42             Comparison<ArmInstruction> armInstructionComparer = delegate( ArmInstruction aLeft, ArmInstruction aRight )
       
    43             {
       
    44                 // We sort in reverse order, hence right compared to left
       
    45                 return aRight.SortOrder.CompareTo( aLeft.SortOrder );
       
    46             };
       
    47             iArmInstructions = new PluginManager<ArmInstruction>();
       
    48             iArmInstructions.LoadFromCallingAssembly();
       
    49             iArmInstructions.Sort( armInstructionComparer );
       
    50 
       
    51             // Load all the THUMB instructions
       
    52             Comparison<ThumbInstruction> thumbInstructionComparer = delegate( ThumbInstruction aLeft, ThumbInstruction aRight )
       
    53             {
       
    54                 // We sort in reverse order, hence right compared to left
       
    55                 return aRight.SortOrder.CompareTo( aLeft.SortOrder );
       
    56             };
       
    57             iThumbInstructions = new PluginManager<ThumbInstruction>();
       
    58             iThumbInstructions.LoadFromCallingAssembly();
       
    59             iThumbInstructions.Sort( thumbInstructionComparer );
       
    60 
       
    61             // Load disassembler if present
       
    62             iDisassembler = new PluginManager<IArmDisassembler>();
       
    63             iDisassembler.Load( null );
       
    64         }
       
    65         #endregion
       
    66 
       
    67         #region API
       
    68         public IArmInstruction[] ConvertToInstructions( TArmInstructionSet aInstructionSet, uint[] aRawInstructions, uint aStartingAddress )
       
    69         {
       
    70             IArmInstruction[] ret = null;
       
    71             //
       
    72             switch ( aInstructionSet )
       
    73             {
       
    74             case TArmInstructionSet.EARM:
       
    75                 ret = ConvertToArm( aRawInstructions, aStartingAddress );
       
    76                 break;
       
    77             case TArmInstructionSet.ETHUMB:
       
    78                 ret = ConvertToThumb( aRawInstructions, aStartingAddress );
       
    79                 break;
       
    80             default:
       
    81                 throw new NotSupportedException();
       
    82             }
       
    83             //
       
    84             return ret;
       
    85         }
       
    86         #endregion
       
    87 
       
    88         #region Properties
       
    89         #endregion
       
    90 
       
    91         #region Internal methods
       
    92         private void Disassemble( ArmBaseInstruction aInstruction )
       
    93         {
       
    94             if ( iDisassembler.Count > 0 )
       
    95             {
       
    96                 IArmDisassembler disassembler = iDisassembler[ 0 ];
       
    97                 string disassembly = disassembler.Disassemble( aInstruction );
       
    98                 aInstruction.AIDisassembly = disassembly;
       
    99             }
       
   100         }
       
   101 
       
   102         private IArmInstruction[] ConvertToArm( uint[] aRawInstructions, uint aStartingAddress )
       
   103         {
       
   104             // TODO: optimise this
       
   105             List<IArmInstruction> ret = new List<IArmInstruction>();
       
   106             //
       
   107             uint address = aStartingAddress;
       
   108             for ( int i = 0; i < aRawInstructions.Length; i++, address += 4 )
       
   109             {
       
   110                 uint raw = aRawInstructions[ i ];
       
   111                 foreach ( ArmInstruction inst in iArmInstructions )
       
   112                 {
       
   113                     if ( inst.Matches( raw ) )
       
   114                     {
       
   115                         Type type = inst.GetType();
       
   116                         ConstructorInfo ctor = type.GetConstructor( new Type[] { } );
       
   117                         ArmInstruction copy = (ArmInstruction) ctor.Invoke( new object[] { } );
       
   118                         copy.AIAddress = address;
       
   119                         copy.AIRawValue = raw;
       
   120                         ret.Add( copy );
       
   121                         Disassemble( copy );
       
   122                         break;
       
   123                     }
       
   124                 }
       
   125             }
       
   126             //
       
   127             return ret.ToArray();
       
   128         }
       
   129 
       
   130         private IArmInstruction[] ConvertToThumb( uint[] aRawInstructions, uint aStartingAddress )
       
   131         {
       
   132             // TODO: optimise this
       
   133             List<IArmInstruction> ret = new List<IArmInstruction>();
       
   134             //
       
   135             uint address = aStartingAddress;
       
   136             for ( int i = 0; i < aRawInstructions.Length; i++, address += 2 )
       
   137             {
       
   138                 uint raw = aRawInstructions[ i ];
       
   139                 foreach ( ThumbInstruction inst in iThumbInstructions )
       
   140                 {
       
   141                     if ( inst.Matches( raw ) )
       
   142                     {
       
   143                         Type type = inst.GetType();
       
   144                         ConstructorInfo ctor = type.GetConstructor( new Type[] { } );
       
   145                         ThumbInstruction copy = (ThumbInstruction) ctor.Invoke( new object[] { } );
       
   146                         copy.AIAddress = address;
       
   147                         copy.AIRawValue = raw;
       
   148                         ret.Add( copy );
       
   149                         Disassemble( copy );
       
   150                         break;
       
   151                     }
       
   152                 }
       
   153             }
       
   154             //
       
   155             return ret.ToArray();
       
   156         }
       
   157         #endregion
       
   158 
       
   159         #region Data members
       
   160         private static readonly PluginManager<ArmInstruction> iArmInstructions;
       
   161         private static readonly PluginManager<ThumbInstruction> iThumbInstructions;
       
   162         private static readonly PluginManager<IArmDisassembler> iDisassembler;
       
   163         #endregion
       
   164     }
       
   165 }