blob: d1411568d456bcb263cc3e4907345c42680a8989 [file] [log] [blame]
buzbee67bf8852011-08-17 17:51:35 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080017namespace art {
18
buzbee67bf8852011-08-17 17:51:35 -070019/*
20 * This file contains target-independent codegen and support, and is
21 * included by:
22 *
23 * $(TARGET_ARCH)/Codegen-$(TARGET_ARCH_VARIANT).c
24 *
25 * which combines this common code with specific support found in the
26 * applicable directories below this one.
27 *
28 * Prior to including this file, TGT_LIR should be #defined.
29 * For example, for arm:
30 * #define TGT_LIR ArmLIR
buzbeee3acd072012-02-25 17:03:10 -080031 * for MIPS:
32 * #define TGT_LIR MipsLIR
buzbee67bf8852011-08-17 17:51:35 -070033 * and for x86:
34 * #define TGT_LIR X86LIR
35 */
36
37
38/* Load a word at base + displacement. Displacement must be word multiple */
buzbeeed3e9302011-09-23 17:34:19 -070039STATIC TGT_LIR* loadWordDisp(CompilationUnit* cUnit, int rBase,
buzbee67bf8852011-08-17 17:51:35 -070040 int displacement, int rDest)
41{
42 return loadBaseDisp(cUnit, NULL, rBase, displacement, rDest, kWord,
43 INVALID_SREG);
44}
45
buzbeeed3e9302011-09-23 17:34:19 -070046STATIC TGT_LIR* storeWordDisp(CompilationUnit* cUnit, int rBase,
buzbee67bf8852011-08-17 17:51:35 -070047 int displacement, int rSrc)
48{
49 return storeBaseDisp(cUnit, rBase, displacement, rSrc, kWord);
50}
51
52/*
53 * Load a Dalvik register into a physical register. Take care when
54 * using this routine, as it doesn't perform any bookkeeping regarding
55 * register liveness. That is the responsibility of the caller.
56 */
buzbeeed3e9302011-09-23 17:34:19 -070057STATIC void loadValueDirect(CompilationUnit* cUnit, RegLocation rlSrc,
buzbee67bf8852011-08-17 17:51:35 -070058 int reg1)
59{
60 rlSrc = oatUpdateLoc(cUnit, rlSrc);
61 if (rlSrc.location == kLocPhysReg) {
62 genRegCopy(cUnit, reg1, rlSrc.lowReg);
63 } else {
buzbeeed3e9302011-09-23 17:34:19 -070064 DCHECK(rlSrc.location == kLocDalvikFrame);
buzbee67bc2362011-10-11 18:08:40 -070065 loadWordDisp(cUnit, rSP, oatSRegOffset(cUnit, rlSrc.sRegLow), reg1);
buzbee67bf8852011-08-17 17:51:35 -070066 }
67}
68
69/*
70 * Similar to loadValueDirect, but clobbers and allocates the target
71 * register. Should be used when loading to a fixed register (for example,
72 * loading arguments to an out of line call.
73 */
buzbeeed3e9302011-09-23 17:34:19 -070074STATIC void loadValueDirectFixed(CompilationUnit* cUnit, RegLocation rlSrc,
buzbee67bf8852011-08-17 17:51:35 -070075 int reg1)
76{
77 oatClobber(cUnit, reg1);
78 oatMarkInUse(cUnit, reg1);
79 loadValueDirect(cUnit, rlSrc, reg1);
80}
81
82/*
83 * Load a Dalvik register pair into a physical register[s]. Take care when
84 * using this routine, as it doesn't perform any bookkeeping regarding
85 * register liveness. That is the responsibility of the caller.
86 */
buzbeeed3e9302011-09-23 17:34:19 -070087STATIC void loadValueDirectWide(CompilationUnit* cUnit, RegLocation rlSrc,
buzbee67bf8852011-08-17 17:51:35 -070088 int regLo, int regHi)
89{
90 rlSrc = oatUpdateLocWide(cUnit, rlSrc);
91 if (rlSrc.location == kLocPhysReg) {
92 genRegCopyWide(cUnit, regLo, regHi, rlSrc.lowReg, rlSrc.highReg);
93 } else {
buzbeeed3e9302011-09-23 17:34:19 -070094 DCHECK(rlSrc.location == kLocDalvikFrame);
buzbee67bc2362011-10-11 18:08:40 -070095 loadBaseDispWide(cUnit, NULL, rSP,
96 oatSRegOffset(cUnit, rlSrc.sRegLow),
buzbee67bf8852011-08-17 17:51:35 -070097 regLo, regHi, INVALID_SREG);
98 }
99}
100
101/*
102 * Similar to loadValueDirect, but clobbers and allocates the target
103 * registers. Should be used when loading to a fixed registers (for example,
104 * loading arguments to an out of line call.
105 */
buzbeeed3e9302011-09-23 17:34:19 -0700106STATIC void loadValueDirectWideFixed(CompilationUnit* cUnit, RegLocation rlSrc,
buzbee67bf8852011-08-17 17:51:35 -0700107 int regLo, int regHi)
108{
109 oatClobber(cUnit, regLo);
110 oatClobber(cUnit, regHi);
111 oatMarkInUse(cUnit, regLo);
112 oatMarkInUse(cUnit, regHi);
113 loadValueDirectWide(cUnit, rlSrc, regLo, regHi);
114}
115
buzbeeed3e9302011-09-23 17:34:19 -0700116STATIC RegLocation loadValue(CompilationUnit* cUnit, RegLocation rlSrc,
buzbee67bf8852011-08-17 17:51:35 -0700117 RegisterClass opKind)
118{
119 rlSrc = oatEvalLoc(cUnit, rlSrc, opKind, false);
120 if (rlSrc.location == kLocDalvikFrame) {
121 loadValueDirect(cUnit, rlSrc, rlSrc.lowReg);
122 rlSrc.location = kLocPhysReg;
123 oatMarkLive(cUnit, rlSrc.lowReg, rlSrc.sRegLow);
124 }
125 return rlSrc;
126}
127
buzbeeed3e9302011-09-23 17:34:19 -0700128STATIC void storeValue(CompilationUnit* cUnit, RegLocation rlDest,
buzbee67bf8852011-08-17 17:51:35 -0700129 RegLocation rlSrc)
130{
131 LIR* defStart;
132 LIR* defEnd;
buzbeeed3e9302011-09-23 17:34:19 -0700133 DCHECK(!rlDest.wide);
134 DCHECK(!rlSrc.wide);
buzbee67bf8852011-08-17 17:51:35 -0700135 rlSrc = oatUpdateLoc(cUnit, rlSrc);
136 rlDest = oatUpdateLoc(cUnit, rlDest);
137 if (rlSrc.location == kLocPhysReg) {
138 if (oatIsLive(cUnit, rlSrc.lowReg) ||
buzbeeb29e4d12011-09-26 15:05:48 -0700139 oatIsPromoted(cUnit, rlSrc.lowReg) ||
buzbee67bf8852011-08-17 17:51:35 -0700140 (rlDest.location == kLocPhysReg)) {
buzbeeb29e4d12011-09-26 15:05:48 -0700141 // Src is live/promoted or Dest has assigned reg.
buzbee67bf8852011-08-17 17:51:35 -0700142 rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false);
143 genRegCopy(cUnit, rlDest.lowReg, rlSrc.lowReg);
144 } else {
145 // Just re-assign the registers. Dest gets Src's regs
146 rlDest.lowReg = rlSrc.lowReg;
147 oatClobber(cUnit, rlSrc.lowReg);
148 }
149 } else {
150 // Load Src either into promoted Dest or temps allocated for Dest
151 rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false);
152 loadValueDirect(cUnit, rlSrc, rlDest.lowReg);
153 }
154
155 // Dest is now live and dirty (until/if we flush it to home location)
156 oatMarkLive(cUnit, rlDest.lowReg, rlDest.sRegLow);
157 oatMarkDirty(cUnit, rlDest);
158
159
160 oatResetDefLoc(cUnit, rlDest);
161 if (oatIsDirty(cUnit, rlDest.lowReg) &&
162 oatLiveOut(cUnit, rlDest.sRegLow)) {
163 defStart = (LIR* )cUnit->lastLIRInsn;
buzbee67bc2362011-10-11 18:08:40 -0700164 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, rlDest.sRegLow),
165 rlDest.lowReg, kWord);
buzbee67bf8852011-08-17 17:51:35 -0700166 oatMarkClean(cUnit, rlDest);
167 defEnd = (LIR* )cUnit->lastLIRInsn;
168 oatMarkDef(cUnit, rlDest, defStart, defEnd);
169 }
170}
171
buzbeeed3e9302011-09-23 17:34:19 -0700172STATIC RegLocation loadValueWide(CompilationUnit* cUnit, RegLocation rlSrc,
buzbee67bf8852011-08-17 17:51:35 -0700173 RegisterClass opKind)
174{
buzbeeed3e9302011-09-23 17:34:19 -0700175 DCHECK(rlSrc.wide);
buzbee67bf8852011-08-17 17:51:35 -0700176 rlSrc = oatEvalLoc(cUnit, rlSrc, opKind, false);
177 if (rlSrc.location == kLocDalvikFrame) {
178 loadValueDirectWide(cUnit, rlSrc, rlSrc.lowReg, rlSrc.highReg);
179 rlSrc.location = kLocPhysReg;
180 oatMarkLive(cUnit, rlSrc.lowReg, rlSrc.sRegLow);
181 oatMarkLive(cUnit, rlSrc.highReg,
182 oatSRegHi(rlSrc.sRegLow));
183 }
184 return rlSrc;
185}
186
buzbeeed3e9302011-09-23 17:34:19 -0700187STATIC void storeValueWide(CompilationUnit* cUnit, RegLocation rlDest,
buzbee67bf8852011-08-17 17:51:35 -0700188 RegLocation rlSrc)
189{
190 LIR* defStart;
191 LIR* defEnd;
buzbeeed3e9302011-09-23 17:34:19 -0700192 DCHECK_EQ(FPREG(rlSrc.lowReg), FPREG(rlSrc.highReg));
193 DCHECK(rlDest.wide);
194 DCHECK(rlSrc.wide);
buzbee67bf8852011-08-17 17:51:35 -0700195 if (rlSrc.location == kLocPhysReg) {
196 if (oatIsLive(cUnit, rlSrc.lowReg) ||
197 oatIsLive(cUnit, rlSrc.highReg) ||
buzbeeb29e4d12011-09-26 15:05:48 -0700198 oatIsPromoted(cUnit, rlSrc.lowReg) ||
199 oatIsPromoted(cUnit, rlSrc.highReg) ||
buzbee67bf8852011-08-17 17:51:35 -0700200 (rlDest.location == kLocPhysReg)) {
buzbeeb29e4d12011-09-26 15:05:48 -0700201 // Src is live or promoted or Dest has assigned reg.
buzbee67bf8852011-08-17 17:51:35 -0700202 rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false);
203 genRegCopyWide(cUnit, rlDest.lowReg, rlDest.highReg,
204 rlSrc.lowReg, rlSrc.highReg);
205 } else {
206 // Just re-assign the registers. Dest gets Src's regs
207 rlDest.lowReg = rlSrc.lowReg;
208 rlDest.highReg = rlSrc.highReg;
209 oatClobber(cUnit, rlSrc.lowReg);
210 oatClobber(cUnit, rlSrc.highReg);
211 }
212 } else {
213 // Load Src either into promoted Dest or temps allocated for Dest
214 rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false);
215 loadValueDirectWide(cUnit, rlSrc, rlDest.lowReg,
216 rlDest.highReg);
217 }
218
219 // Dest is now live and dirty (until/if we flush it to home location)
220 oatMarkLive(cUnit, rlDest.lowReg, rlDest.sRegLow);
221 oatMarkLive(cUnit, rlDest.highReg,
222 oatSRegHi(rlDest.sRegLow));
223 oatMarkDirty(cUnit, rlDest);
224 oatMarkPair(cUnit, rlDest.lowReg, rlDest.highReg);
225
226
227 oatResetDefLocWide(cUnit, rlDest);
228 if ((oatIsDirty(cUnit, rlDest.lowReg) ||
229 oatIsDirty(cUnit, rlDest.highReg)) &&
230 (oatLiveOut(cUnit, rlDest.sRegLow) ||
231 oatLiveOut(cUnit, oatSRegHi(rlDest.sRegLow)))) {
232 defStart = (LIR*)cUnit->lastLIRInsn;
buzbeeed3e9302011-09-23 17:34:19 -0700233 DCHECK_EQ((oatS2VReg(cUnit, rlDest.sRegLow)+1),
buzbee67bf8852011-08-17 17:51:35 -0700234 oatS2VReg(cUnit, oatSRegHi(rlDest.sRegLow)));
buzbee67bc2362011-10-11 18:08:40 -0700235 storeBaseDispWide(cUnit, rSP, oatSRegOffset(cUnit, rlDest.sRegLow),
buzbee67bf8852011-08-17 17:51:35 -0700236 rlDest.lowReg, rlDest.highReg);
237 oatMarkClean(cUnit, rlDest);
238 defEnd = (LIR*)cUnit->lastLIRInsn;
239 oatMarkDefWide(cUnit, rlDest, defStart, defEnd);
240 }
241}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800242
243} // namespace art