1 /* libs/pixelflinger/codeflinger/ARMAssemblerInterface.cpp
2 **
3 ** Copyright 2006, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 ** http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17
18
19 #include <errno.h>
20 #include <stdlib.h>
21 #include <stdint.h>
22 #include <sys/types.h>
23
24 #include <cutils/log.h>
25 #include "codeflinger/ARMAssemblerInterface.h"
26
27 namespace android {
28
29 // ----------------------------------------------------------------------------
30
~ARMAssemblerInterface()31 ARMAssemblerInterface::~ARMAssemblerInterface()
32 {
33 }
34
buildImmediate(uint32_t immediate,uint32_t & rot,uint32_t & imm)35 int ARMAssemblerInterface::buildImmediate(
36 uint32_t immediate, uint32_t& rot, uint32_t& imm)
37 {
38 rot = 0;
39 imm = immediate;
40 if (imm > 0x7F) { // skip the easy cases
41 while (!(imm&3) || (imm&0xFC000000)) {
42 uint32_t newval;
43 newval = imm >> 2;
44 newval |= (imm&3) << 30;
45 imm = newval;
46 rot += 2;
47 if (rot == 32) {
48 rot = 0;
49 break;
50 }
51 }
52 }
53 rot = (16 - (rot>>1)) & 0xF;
54
55 if (imm>=0x100)
56 return -EINVAL;
57
58 if (((imm>>(rot<<1)) | (imm<<(32-(rot<<1)))) != immediate)
59 return -1;
60
61 return 0;
62 }
63
64 // shifters...
65
isValidImmediate(uint32_t immediate)66 bool ARMAssemblerInterface::isValidImmediate(uint32_t immediate)
67 {
68 uint32_t rot, imm;
69 return buildImmediate(immediate, rot, imm) == 0;
70 }
71
imm(uint32_t immediate)72 uint32_t ARMAssemblerInterface::imm(uint32_t immediate)
73 {
74 uint32_t rot, imm;
75 int err = buildImmediate(immediate, rot, imm);
76
77 LOG_ALWAYS_FATAL_IF(err==-EINVAL,
78 "immediate %08x cannot be encoded",
79 immediate);
80
81 LOG_ALWAYS_FATAL_IF(err,
82 "immediate (%08x) encoding bogus!",
83 immediate);
84
85 return (1<<25) | (rot<<8) | imm;
86 }
87
reg_imm(int Rm,int type,uint32_t shift)88 uint32_t ARMAssemblerInterface::reg_imm(int Rm, int type, uint32_t shift)
89 {
90 return ((shift&0x1F)<<7) | ((type&0x3)<<5) | (Rm&0xF);
91 }
92
reg_rrx(int Rm)93 uint32_t ARMAssemblerInterface::reg_rrx(int Rm)
94 {
95 return (ROR<<5) | (Rm&0xF);
96 }
97
reg_reg(int Rm,int type,int Rs)98 uint32_t ARMAssemblerInterface::reg_reg(int Rm, int type, int Rs)
99 {
100 return ((Rs&0xF)<<8) | ((type&0x3)<<5) | (1<<4) | (Rm&0xF);
101 }
102
103 // addressing modes...
104 // LDR(B)/STR(B)/PLD (immediate and Rm can be negative, which indicate U=0)
immed12_pre(int32_t immed12,int W)105 uint32_t ARMAssemblerInterface::immed12_pre(int32_t immed12, int W)
106 {
107 LOG_ALWAYS_FATAL_IF(abs(immed12) >= 0x800,
108 "LDR(B)/STR(B)/PLD immediate too big (%08x)",
109 immed12);
110 return (1<<24) | (((uint32_t(immed12)>>31)^1)<<23) |
111 ((W&1)<<21) | (abs(immed12)&0x7FF);
112 }
113
immed12_post(int32_t immed12)114 uint32_t ARMAssemblerInterface::immed12_post(int32_t immed12)
115 {
116 LOG_ALWAYS_FATAL_IF(abs(immed12) >= 0x800,
117 "LDR(B)/STR(B)/PLD immediate too big (%08x)",
118 immed12);
119
120 return (((uint32_t(immed12)>>31)^1)<<23) | (abs(immed12)&0x7FF);
121 }
122
reg_scale_pre(int Rm,int type,uint32_t shift,int W)123 uint32_t ARMAssemblerInterface::reg_scale_pre(int Rm, int type,
124 uint32_t shift, int W)
125 {
126 return (1<<25) | (1<<24) |
127 (((uint32_t(Rm)>>31)^1)<<23) | ((W&1)<<21) |
128 reg_imm(abs(Rm), type, shift);
129 }
130
reg_scale_post(int Rm,int type,uint32_t shift)131 uint32_t ARMAssemblerInterface::reg_scale_post(int Rm, int type, uint32_t shift)
132 {
133 return (1<<25) | (((uint32_t(Rm)>>31)^1)<<23) | reg_imm(abs(Rm), type, shift);
134 }
135
136 // LDRH/LDRSB/LDRSH/STRH (immediate and Rm can be negative, which indicate U=0)
immed8_pre(int32_t immed8,int W)137 uint32_t ARMAssemblerInterface::immed8_pre(int32_t immed8, int W)
138 {
139 uint32_t offset = abs(immed8);
140
141 LOG_ALWAYS_FATAL_IF(abs(immed8) >= 0x100,
142 "LDRH/LDRSB/LDRSH/STRH immediate too big (%08x)",
143 immed8);
144
145 return (1<<24) | (1<<22) | (((uint32_t(immed8)>>31)^1)<<23) |
146 ((W&1)<<21) | (((offset&0xF0)<<4)|(offset&0xF));
147 }
148
immed8_post(int32_t immed8)149 uint32_t ARMAssemblerInterface::immed8_post(int32_t immed8)
150 {
151 uint32_t offset = abs(immed8);
152
153 LOG_ALWAYS_FATAL_IF(abs(immed8) >= 0x100,
154 "LDRH/LDRSB/LDRSH/STRH immediate too big (%08x)",
155 immed8);
156
157 return (1<<22) | (((uint32_t(immed8)>>31)^1)<<23) |
158 (((offset&0xF0)<<4) | (offset&0xF));
159 }
160
reg_pre(int Rm,int W)161 uint32_t ARMAssemblerInterface::reg_pre(int Rm, int W)
162 {
163 return (1<<24) | (((uint32_t(Rm)>>31)^1)<<23) | ((W&1)<<21) | (abs(Rm)&0xF);
164 }
165
reg_post(int Rm)166 uint32_t ARMAssemblerInterface::reg_post(int Rm)
167 {
168 return (((uint32_t(Rm)>>31)^1)<<23) | (abs(Rm)&0xF);
169 }
170
171
172 }; // namespace android
173
174