blob: 4b2fd4a9f6b80931f875ba2fcc7daee4225eea00 [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
31 * and for x86:
32 * #define TGT_LIR X86LIR
33 */
34
35
36/* Load a word at base + displacement. Displacement must be word multiple */
buzbeeed3e9302011-09-23 17:34:19 -070037STATIC TGT_LIR* loadWordDisp(CompilationUnit* cUnit, int rBase,
buzbee67bf8852011-08-17 17:51:35 -070038 int displacement, int rDest)
39{
40 return loadBaseDisp(cUnit, NULL, rBase, displacement, rDest, kWord,
41 INVALID_SREG);
42}
43
buzbeeed3e9302011-09-23 17:34:19 -070044STATIC TGT_LIR* storeWordDisp(CompilationUnit* cUnit, int rBase,
buzbee67bf8852011-08-17 17:51:35 -070045 int displacement, int rSrc)
46{
47 return storeBaseDisp(cUnit, rBase, displacement, rSrc, kWord);
48}
49
50/*
51 * Load a Dalvik register into a physical register. Take care when
52 * using this routine, as it doesn't perform any bookkeeping regarding
53 * register liveness. That is the responsibility of the caller.
54 */
buzbeeed3e9302011-09-23 17:34:19 -070055STATIC void loadValueDirect(CompilationUnit* cUnit, RegLocation rlSrc,
buzbee67bf8852011-08-17 17:51:35 -070056 int reg1)
57{
58 rlSrc = oatUpdateLoc(cUnit, rlSrc);
59 if (rlSrc.location == kLocPhysReg) {
60 genRegCopy(cUnit, reg1, rlSrc.lowReg);
61 } else {
buzbeeed3e9302011-09-23 17:34:19 -070062 DCHECK(rlSrc.location == kLocDalvikFrame);
buzbee67bc2362011-10-11 18:08:40 -070063 loadWordDisp(cUnit, rSP, oatSRegOffset(cUnit, rlSrc.sRegLow), reg1);
buzbee67bf8852011-08-17 17:51:35 -070064 }
65}
66
67/*
68 * Similar to loadValueDirect, but clobbers and allocates the target
69 * register. Should be used when loading to a fixed register (for example,
70 * loading arguments to an out of line call.
71 */
buzbeeed3e9302011-09-23 17:34:19 -070072STATIC void loadValueDirectFixed(CompilationUnit* cUnit, RegLocation rlSrc,
buzbee67bf8852011-08-17 17:51:35 -070073 int reg1)
74{
75 oatClobber(cUnit, reg1);
76 oatMarkInUse(cUnit, reg1);
77 loadValueDirect(cUnit, rlSrc, reg1);
78}
79
80/*
81 * Load a Dalvik register pair into a physical register[s]. Take care when
82 * using this routine, as it doesn't perform any bookkeeping regarding
83 * register liveness. That is the responsibility of the caller.
84 */
buzbeeed3e9302011-09-23 17:34:19 -070085STATIC void loadValueDirectWide(CompilationUnit* cUnit, RegLocation rlSrc,
buzbee67bf8852011-08-17 17:51:35 -070086 int regLo, int regHi)
87{
88 rlSrc = oatUpdateLocWide(cUnit, rlSrc);
89 if (rlSrc.location == kLocPhysReg) {
90 genRegCopyWide(cUnit, regLo, regHi, rlSrc.lowReg, rlSrc.highReg);
91 } else {
buzbeeed3e9302011-09-23 17:34:19 -070092 DCHECK(rlSrc.location == kLocDalvikFrame);
buzbee67bc2362011-10-11 18:08:40 -070093 loadBaseDispWide(cUnit, NULL, rSP,
94 oatSRegOffset(cUnit, rlSrc.sRegLow),
buzbee67bf8852011-08-17 17:51:35 -070095 regLo, regHi, INVALID_SREG);
96 }
97}
98
99/*
100 * Similar to loadValueDirect, but clobbers and allocates the target
101 * registers. Should be used when loading to a fixed registers (for example,
102 * loading arguments to an out of line call.
103 */
buzbeeed3e9302011-09-23 17:34:19 -0700104STATIC void loadValueDirectWideFixed(CompilationUnit* cUnit, RegLocation rlSrc,
buzbee67bf8852011-08-17 17:51:35 -0700105 int regLo, int regHi)
106{
107 oatClobber(cUnit, regLo);
108 oatClobber(cUnit, regHi);
109 oatMarkInUse(cUnit, regLo);
110 oatMarkInUse(cUnit, regHi);
111 loadValueDirectWide(cUnit, rlSrc, regLo, regHi);
112}
113
buzbeeed3e9302011-09-23 17:34:19 -0700114STATIC RegLocation loadValue(CompilationUnit* cUnit, RegLocation rlSrc,
buzbee67bf8852011-08-17 17:51:35 -0700115 RegisterClass opKind)
116{
117 rlSrc = oatEvalLoc(cUnit, rlSrc, opKind, false);
118 if (rlSrc.location == kLocDalvikFrame) {
119 loadValueDirect(cUnit, rlSrc, rlSrc.lowReg);
120 rlSrc.location = kLocPhysReg;
121 oatMarkLive(cUnit, rlSrc.lowReg, rlSrc.sRegLow);
122 }
123 return rlSrc;
124}
125
buzbeeed3e9302011-09-23 17:34:19 -0700126STATIC void storeValue(CompilationUnit* cUnit, RegLocation rlDest,
buzbee67bf8852011-08-17 17:51:35 -0700127 RegLocation rlSrc)
128{
129 LIR* defStart;
130 LIR* defEnd;
buzbeeed3e9302011-09-23 17:34:19 -0700131 DCHECK(!rlDest.wide);
132 DCHECK(!rlSrc.wide);
buzbee67bf8852011-08-17 17:51:35 -0700133 rlSrc = oatUpdateLoc(cUnit, rlSrc);
134 rlDest = oatUpdateLoc(cUnit, rlDest);
135 if (rlSrc.location == kLocPhysReg) {
136 if (oatIsLive(cUnit, rlSrc.lowReg) ||
buzbeeb29e4d12011-09-26 15:05:48 -0700137 oatIsPromoted(cUnit, rlSrc.lowReg) ||
buzbee67bf8852011-08-17 17:51:35 -0700138 (rlDest.location == kLocPhysReg)) {
buzbeeb29e4d12011-09-26 15:05:48 -0700139 // Src is live/promoted or Dest has assigned reg.
buzbee67bf8852011-08-17 17:51:35 -0700140 rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false);
141 genRegCopy(cUnit, rlDest.lowReg, rlSrc.lowReg);
142 } else {
143 // Just re-assign the registers. Dest gets Src's regs
144 rlDest.lowReg = rlSrc.lowReg;
145 oatClobber(cUnit, rlSrc.lowReg);
146 }
147 } else {
148 // Load Src either into promoted Dest or temps allocated for Dest
149 rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false);
150 loadValueDirect(cUnit, rlSrc, rlDest.lowReg);
151 }
152
153 // Dest is now live and dirty (until/if we flush it to home location)
154 oatMarkLive(cUnit, rlDest.lowReg, rlDest.sRegLow);
155 oatMarkDirty(cUnit, rlDest);
156
157
158 oatResetDefLoc(cUnit, rlDest);
159 if (oatIsDirty(cUnit, rlDest.lowReg) &&
160 oatLiveOut(cUnit, rlDest.sRegLow)) {
161 defStart = (LIR* )cUnit->lastLIRInsn;
buzbee67bc2362011-10-11 18:08:40 -0700162 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, rlDest.sRegLow),
163 rlDest.lowReg, kWord);
buzbee67bf8852011-08-17 17:51:35 -0700164 oatMarkClean(cUnit, rlDest);
165 defEnd = (LIR* )cUnit->lastLIRInsn;
166 oatMarkDef(cUnit, rlDest, defStart, defEnd);
167 }
168}
169
buzbeeed3e9302011-09-23 17:34:19 -0700170STATIC RegLocation loadValueWide(CompilationUnit* cUnit, RegLocation rlSrc,
buzbee67bf8852011-08-17 17:51:35 -0700171 RegisterClass opKind)
172{
buzbeeed3e9302011-09-23 17:34:19 -0700173 DCHECK(rlSrc.wide);
buzbee67bf8852011-08-17 17:51:35 -0700174 rlSrc = oatEvalLoc(cUnit, rlSrc, opKind, false);
175 if (rlSrc.location == kLocDalvikFrame) {
176 loadValueDirectWide(cUnit, rlSrc, rlSrc.lowReg, rlSrc.highReg);
177 rlSrc.location = kLocPhysReg;
178 oatMarkLive(cUnit, rlSrc.lowReg, rlSrc.sRegLow);
179 oatMarkLive(cUnit, rlSrc.highReg,
180 oatSRegHi(rlSrc.sRegLow));
181 }
182 return rlSrc;
183}
184
buzbeeed3e9302011-09-23 17:34:19 -0700185STATIC void storeValueWide(CompilationUnit* cUnit, RegLocation rlDest,
buzbee67bf8852011-08-17 17:51:35 -0700186 RegLocation rlSrc)
187{
188 LIR* defStart;
189 LIR* defEnd;
buzbeeed3e9302011-09-23 17:34:19 -0700190 DCHECK_EQ(FPREG(rlSrc.lowReg), FPREG(rlSrc.highReg));
191 DCHECK(rlDest.wide);
192 DCHECK(rlSrc.wide);
buzbee67bf8852011-08-17 17:51:35 -0700193 if (rlSrc.location == kLocPhysReg) {
194 if (oatIsLive(cUnit, rlSrc.lowReg) ||
195 oatIsLive(cUnit, rlSrc.highReg) ||
buzbeeb29e4d12011-09-26 15:05:48 -0700196 oatIsPromoted(cUnit, rlSrc.lowReg) ||
197 oatIsPromoted(cUnit, rlSrc.highReg) ||
buzbee67bf8852011-08-17 17:51:35 -0700198 (rlDest.location == kLocPhysReg)) {
buzbeeb29e4d12011-09-26 15:05:48 -0700199 // Src is live or promoted or Dest has assigned reg.
buzbee67bf8852011-08-17 17:51:35 -0700200 rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false);
201 genRegCopyWide(cUnit, rlDest.lowReg, rlDest.highReg,
202 rlSrc.lowReg, rlSrc.highReg);
203 } else {
204 // Just re-assign the registers. Dest gets Src's regs
205 rlDest.lowReg = rlSrc.lowReg;
206 rlDest.highReg = rlSrc.highReg;
207 oatClobber(cUnit, rlSrc.lowReg);
208 oatClobber(cUnit, rlSrc.highReg);
209 }
210 } else {
211 // Load Src either into promoted Dest or temps allocated for Dest
212 rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false);
213 loadValueDirectWide(cUnit, rlSrc, rlDest.lowReg,
214 rlDest.highReg);
215 }
216
217 // Dest is now live and dirty (until/if we flush it to home location)
218 oatMarkLive(cUnit, rlDest.lowReg, rlDest.sRegLow);
219 oatMarkLive(cUnit, rlDest.highReg,
220 oatSRegHi(rlDest.sRegLow));
221 oatMarkDirty(cUnit, rlDest);
222 oatMarkPair(cUnit, rlDest.lowReg, rlDest.highReg);
223
224
225 oatResetDefLocWide(cUnit, rlDest);
226 if ((oatIsDirty(cUnit, rlDest.lowReg) ||
227 oatIsDirty(cUnit, rlDest.highReg)) &&
228 (oatLiveOut(cUnit, rlDest.sRegLow) ||
229 oatLiveOut(cUnit, oatSRegHi(rlDest.sRegLow)))) {
230 defStart = (LIR*)cUnit->lastLIRInsn;
buzbeeed3e9302011-09-23 17:34:19 -0700231 DCHECK_EQ((oatS2VReg(cUnit, rlDest.sRegLow)+1),
buzbee67bf8852011-08-17 17:51:35 -0700232 oatS2VReg(cUnit, oatSRegHi(rlDest.sRegLow)));
buzbee67bc2362011-10-11 18:08:40 -0700233 storeBaseDispWide(cUnit, rSP, oatSRegOffset(cUnit, rlDest.sRegLow),
buzbee67bf8852011-08-17 17:51:35 -0700234 rlDest.lowReg, rlDest.highReg);
235 oatMarkClean(cUnit, rlDest);
236 defEnd = (LIR*)cUnit->lastLIRInsn;
237 oatMarkDefWide(cUnit, rlDest, defStart, defEnd);
238 }
239}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800240
241} // namespace art