blob: c0ad1cc2e9039e921f90b35335e6a62aa583dd63 [file] [log] [blame]
Johnny Chen4baf2e32011-01-24 18:24:53 +00001//===-- lldb_ARMUtils.h -----------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef lldb_ARMUtils_h_
11#define lldb_ARMUtils_h_
12
Johnny Chen108d5aa2011-01-26 01:00:55 +000013#include "InstructionUtils.h"
14
Johnny Chenc3b5bc32011-01-24 19:50:30 +000015// Common utilities for the ARM/Thumb Instruction Set Architecture.
Johnny Chen4baf2e32011-01-24 18:24:53 +000016
17namespace lldb_private {
18
Johnny Chenc3b5bc32011-01-24 19:50:30 +000019// ARM conditions
20#define COND_EQ 0x0
21#define COND_NE 0x1
22#define COND_CS 0x2
23#define COND_HS 0x2
24#define COND_CC 0x3
25#define COND_LO 0x3
26#define COND_MI 0x4
27#define COND_PL 0x5
28#define COND_VS 0x6
29#define COND_VC 0x7
30#define COND_HI 0x8
31#define COND_LS 0x9
32#define COND_GE 0xA
33#define COND_LT 0xB
34#define COND_GT 0xC
35#define COND_LE 0xD
36#define COND_AL 0xE
37#define COND_UNCOND 0xF
38
39// Masks for CPSR
40#define MASK_CPSR_MODE_MASK (0x0000001fu)
41#define MASK_CPSR_T (1u << 5)
42#define MASK_CPSR_F (1u << 6)
43#define MASK_CPSR_I (1u << 7)
44#define MASK_CPSR_A (1u << 8)
45#define MASK_CPSR_E (1u << 9)
46#define MASK_CPSR_GE_MASK (0x000f0000u)
47#define MASK_CPSR_J (1u << 24)
48#define MASK_CPSR_Q (1u << 27)
49#define MASK_CPSR_V (1u << 28)
50#define MASK_CPSR_C (1u << 29)
51#define MASK_CPSR_Z (1u << 30)
52#define MASK_CPSR_N (1u << 31)
53
54
Johnny Chen4c0e0bc2011-01-25 22:45:28 +000055static inline uint32_t bits(const uint32_t val, const uint32_t msbit, const uint32_t lsbit)
56{
Johnny Chen108d5aa2011-01-26 01:00:55 +000057 return Bits32(val, msbit, lsbit);
Johnny Chen4c0e0bc2011-01-25 22:45:28 +000058}
59
60static inline uint32_t bit(const uint32_t val, const uint32_t msbit)
61{
62 return bits(val, msbit, msbit);
63}
64
Johnny Chen60c0d622011-01-25 23:49:39 +000065static uint32_t ror(uint32_t val, uint32_t N, uint32_t shift)
Johnny Chen4c0e0bc2011-01-25 22:45:28 +000066{
Johnny Chen60c0d622011-01-25 23:49:39 +000067 uint32_t m = shift % N;
68 return (val >> m) | (val << (N - m));
69}
70
71static inline uint32_t ARMExpandImm(uint32_t val)
72{
73 uint32_t imm = bits(val, 7, 0); // immediate value
74 uint32_t rot = 2 * bits(val, 11, 8); // rotate amount
Johnny Chen4c0e0bc2011-01-25 22:45:28 +000075 return (imm >> rot) | (imm << (32 - rot));
76}
77
Johnny Chen60c0d622011-01-25 23:49:39 +000078static inline uint32_t ThumbExpandImm(uint32_t val)
Johnny Chen4c0e0bc2011-01-25 22:45:28 +000079{
Johnny Chen60c0d622011-01-25 23:49:39 +000080 uint32_t imm32 = 0;
81 const uint32_t i = bit(val, 26);
82 const uint32_t imm3 = bits(val, 14, 12);
83 const uint32_t abcdefgh = bits(val, 7, 0);
84 const uint32_t imm12 = i << 11 | imm3 << 8 | abcdefgh;
85
86 if (bits(imm12, 10, 11) == 0)
87 {
88 switch (bits(imm12, 8, 9)) {
89 case 0:
90 imm32 = abcdefgh;
91 break;
92
93 case 1:
94 imm32 = abcdefgh << 16 | abcdefgh;
95 break;
96
97 case 2:
98 imm32 = abcdefgh << 24 | abcdefgh << 8;
99 break;
100
101 case 3:
102 imm32 = abcdefgh << 24 | abcdefgh << 16 | abcdefgh << 8 | abcdefgh;
103 break;
104 }
105 }
106 else
107 {
108 const uint32_t unrotated_value = 0x80 | bits(imm12, 0, 6);
109 imm32 = ror(unrotated_value, 32, bits(imm12, 7, 11));
110 }
111 return imm32;
112}
113
114// imm32 = ZeroExtend(i:imm3:imm8, 32)
115static inline uint32_t ThumbImm12(uint32_t val)
116{
117 const uint32_t i = bit(val, 26);
118 const uint32_t imm3 = bits(val, 14, 12);
119 const uint32_t imm8 = bits(val, 7, 0);
120 const uint32_t imm12 = i << 11 | imm3 << 8 | imm8;
121 return imm12;
Johnny Chen4c0e0bc2011-01-25 22:45:28 +0000122}
123
Johnny Chene4455022011-01-26 00:08:59 +0000124// imm32 = ZeroExtend(imm7:'00', 32)
125static inline uint32_t ThumbImmScaled(uint32_t val)
126{
127 const uint32_t imm7 = bits(val, 6, 0);
128 return imm7 * 4;
129}
130
Johnny Chen4baf2e32011-01-24 18:24:53 +0000131// This function performs the check for the register numbers 13 and 15 that are
132// not permitted for many Thumb register specifiers.
133static inline bool BadReg(uint32_t n) { return n == 13 || n == 15; }
134
Johnny Chen4baf2e32011-01-24 18:24:53 +0000135} // namespace lldb_private
136
137#endif // lldb_ARMUtils_h_