blob: 108bc835212026510455e787ff2008234048bd78 [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
17/*
18 * This file contains target-independent codegen and support, and is
19 * included by:
20 *
21 * $(TARGET_ARCH)/Codegen-$(TARGET_ARCH_VARIANT).c
22 *
23 * which combines this common code with specific support found in the
24 * applicable directories below this one.
25 *
26 * Prior to including this file, TGT_LIR should be #defined.
27 * For example, for arm:
28 * #define TGT_LIR ArmLIR
29 * and for x86:
30 * #define TGT_LIR X86LIR
31 */
32
33
34/* Load a word at base + displacement. Displacement must be word multiple */
buzbeeed3e9302011-09-23 17:34:19 -070035STATIC TGT_LIR* loadWordDisp(CompilationUnit* cUnit, int rBase,
buzbee67bf8852011-08-17 17:51:35 -070036 int displacement, int rDest)
37{
38 return loadBaseDisp(cUnit, NULL, rBase, displacement, rDest, kWord,
39 INVALID_SREG);
40}
41
buzbeeed3e9302011-09-23 17:34:19 -070042STATIC TGT_LIR* storeWordDisp(CompilationUnit* cUnit, int rBase,
buzbee67bf8852011-08-17 17:51:35 -070043 int displacement, int rSrc)
44{
45 return storeBaseDisp(cUnit, rBase, displacement, rSrc, kWord);
46}
47
48/*
49 * Load a Dalvik register into a physical register. Take care when
50 * using this routine, as it doesn't perform any bookkeeping regarding
51 * register liveness. That is the responsibility of the caller.
52 */
buzbeeed3e9302011-09-23 17:34:19 -070053STATIC void loadValueDirect(CompilationUnit* cUnit, RegLocation rlSrc,
buzbee67bf8852011-08-17 17:51:35 -070054 int reg1)
55{
56 rlSrc = oatUpdateLoc(cUnit, rlSrc);
57 if (rlSrc.location == kLocPhysReg) {
58 genRegCopy(cUnit, reg1, rlSrc.lowReg);
59 } else {
buzbeeed3e9302011-09-23 17:34:19 -070060 DCHECK(rlSrc.location == kLocDalvikFrame);
buzbee67bf8852011-08-17 17:51:35 -070061 loadWordDisp(cUnit, rSP, rlSrc.spOffset, reg1);
62 }
63}
64
65/*
66 * Similar to loadValueDirect, but clobbers and allocates the target
67 * register. Should be used when loading to a fixed register (for example,
68 * loading arguments to an out of line call.
69 */
buzbeeed3e9302011-09-23 17:34:19 -070070STATIC void loadValueDirectFixed(CompilationUnit* cUnit, RegLocation rlSrc,
buzbee67bf8852011-08-17 17:51:35 -070071 int reg1)
72{
73 oatClobber(cUnit, reg1);
74 oatMarkInUse(cUnit, reg1);
75 loadValueDirect(cUnit, rlSrc, reg1);
76}
77
78/*
79 * Load a Dalvik register pair into a physical register[s]. Take care when
80 * using this routine, as it doesn't perform any bookkeeping regarding
81 * register liveness. That is the responsibility of the caller.
82 */
buzbeeed3e9302011-09-23 17:34:19 -070083STATIC void loadValueDirectWide(CompilationUnit* cUnit, RegLocation rlSrc,
buzbee67bf8852011-08-17 17:51:35 -070084 int regLo, int regHi)
85{
86 rlSrc = oatUpdateLocWide(cUnit, rlSrc);
87 if (rlSrc.location == kLocPhysReg) {
88 genRegCopyWide(cUnit, regLo, regHi, rlSrc.lowReg, rlSrc.highReg);
89 } else {
buzbeeed3e9302011-09-23 17:34:19 -070090 DCHECK(rlSrc.location == kLocDalvikFrame);
buzbee67bf8852011-08-17 17:51:35 -070091 loadBaseDispWide(cUnit, NULL, rSP, rlSrc.spOffset,
92 regLo, regHi, INVALID_SREG);
93 }
94}
95
96/*
97 * Similar to loadValueDirect, but clobbers and allocates the target
98 * registers. Should be used when loading to a fixed registers (for example,
99 * loading arguments to an out of line call.
100 */
buzbeeed3e9302011-09-23 17:34:19 -0700101STATIC void loadValueDirectWideFixed(CompilationUnit* cUnit, RegLocation rlSrc,
buzbee67bf8852011-08-17 17:51:35 -0700102 int regLo, int regHi)
103{
104 oatClobber(cUnit, regLo);
105 oatClobber(cUnit, regHi);
106 oatMarkInUse(cUnit, regLo);
107 oatMarkInUse(cUnit, regHi);
108 loadValueDirectWide(cUnit, rlSrc, regLo, regHi);
109}
110
buzbeeed3e9302011-09-23 17:34:19 -0700111STATIC RegLocation loadValue(CompilationUnit* cUnit, RegLocation rlSrc,
buzbee67bf8852011-08-17 17:51:35 -0700112 RegisterClass opKind)
113{
114 rlSrc = oatEvalLoc(cUnit, rlSrc, opKind, false);
115 if (rlSrc.location == kLocDalvikFrame) {
116 loadValueDirect(cUnit, rlSrc, rlSrc.lowReg);
117 rlSrc.location = kLocPhysReg;
118 oatMarkLive(cUnit, rlSrc.lowReg, rlSrc.sRegLow);
119 }
120 return rlSrc;
121}
122
buzbeeed3e9302011-09-23 17:34:19 -0700123STATIC void storeValue(CompilationUnit* cUnit, RegLocation rlDest,
buzbee67bf8852011-08-17 17:51:35 -0700124 RegLocation rlSrc)
125{
126 LIR* defStart;
127 LIR* defEnd;
buzbeeed3e9302011-09-23 17:34:19 -0700128 DCHECK(!rlDest.wide);
129 DCHECK(!rlSrc.wide);
buzbee67bf8852011-08-17 17:51:35 -0700130 rlSrc = oatUpdateLoc(cUnit, rlSrc);
131 rlDest = oatUpdateLoc(cUnit, rlDest);
132 if (rlSrc.location == kLocPhysReg) {
133 if (oatIsLive(cUnit, rlSrc.lowReg) ||
134 (rlDest.location == kLocPhysReg)) {
135 // Src is live or Dest has assigned reg.
136 rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false);
137 genRegCopy(cUnit, rlDest.lowReg, rlSrc.lowReg);
138 } else {
139 // Just re-assign the registers. Dest gets Src's regs
140 rlDest.lowReg = rlSrc.lowReg;
141 oatClobber(cUnit, rlSrc.lowReg);
142 }
143 } else {
144 // Load Src either into promoted Dest or temps allocated for Dest
145 rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false);
146 loadValueDirect(cUnit, rlSrc, rlDest.lowReg);
147 }
148
149 // Dest is now live and dirty (until/if we flush it to home location)
150 oatMarkLive(cUnit, rlDest.lowReg, rlDest.sRegLow);
151 oatMarkDirty(cUnit, rlDest);
152
153
154 oatResetDefLoc(cUnit, rlDest);
155 if (oatIsDirty(cUnit, rlDest.lowReg) &&
156 oatLiveOut(cUnit, rlDest.sRegLow)) {
157 defStart = (LIR* )cUnit->lastLIRInsn;
158 storeBaseDisp(cUnit, rSP, rlDest.spOffset, rlDest.lowReg, kWord);
159 oatMarkClean(cUnit, rlDest);
160 defEnd = (LIR* )cUnit->lastLIRInsn;
161 oatMarkDef(cUnit, rlDest, defStart, defEnd);
162 }
163}
164
buzbeeed3e9302011-09-23 17:34:19 -0700165STATIC RegLocation loadValueWide(CompilationUnit* cUnit, RegLocation rlSrc,
buzbee67bf8852011-08-17 17:51:35 -0700166 RegisterClass opKind)
167{
buzbeeed3e9302011-09-23 17:34:19 -0700168 DCHECK(rlSrc.wide);
buzbee67bf8852011-08-17 17:51:35 -0700169 rlSrc = oatEvalLoc(cUnit, rlSrc, opKind, false);
170 if (rlSrc.location == kLocDalvikFrame) {
171 loadValueDirectWide(cUnit, rlSrc, rlSrc.lowReg, rlSrc.highReg);
172 rlSrc.location = kLocPhysReg;
173 oatMarkLive(cUnit, rlSrc.lowReg, rlSrc.sRegLow);
174 oatMarkLive(cUnit, rlSrc.highReg,
175 oatSRegHi(rlSrc.sRegLow));
176 }
177 return rlSrc;
178}
179
buzbeeed3e9302011-09-23 17:34:19 -0700180STATIC void storeValueWide(CompilationUnit* cUnit, RegLocation rlDest,
buzbee67bf8852011-08-17 17:51:35 -0700181 RegLocation rlSrc)
182{
183 LIR* defStart;
184 LIR* defEnd;
Elliott Hughes80609252011-09-23 17:24:51 -0700185 if (FPREG(rlSrc.lowReg)!=FPREG(rlSrc.highReg)) {
buzbee3ea4ec52011-08-22 17:37:19 -0700186 LOG(WARNING) << "rlSrc.lowreg:" << rlSrc.lowReg << ", rlSrc.highReg:"
187 << rlSrc.highReg;
188 }
buzbeeed3e9302011-09-23 17:34:19 -0700189 DCHECK_EQ(FPREG(rlSrc.lowReg), FPREG(rlSrc.highReg));
190 DCHECK(rlDest.wide);
191 DCHECK(rlSrc.wide);
buzbee67bf8852011-08-17 17:51:35 -0700192 if (rlSrc.location == kLocPhysReg) {
193 if (oatIsLive(cUnit, rlSrc.lowReg) ||
194 oatIsLive(cUnit, rlSrc.highReg) ||
195 (rlDest.location == kLocPhysReg)) {
196 // Src is live or Dest has assigned reg.
197 rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false);
198 genRegCopyWide(cUnit, rlDest.lowReg, rlDest.highReg,
199 rlSrc.lowReg, rlSrc.highReg);
200 } else {
201 // Just re-assign the registers. Dest gets Src's regs
202 rlDest.lowReg = rlSrc.lowReg;
203 rlDest.highReg = rlSrc.highReg;
204 oatClobber(cUnit, rlSrc.lowReg);
205 oatClobber(cUnit, rlSrc.highReg);
206 }
207 } else {
208 // Load Src either into promoted Dest or temps allocated for Dest
209 rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false);
210 loadValueDirectWide(cUnit, rlSrc, rlDest.lowReg,
211 rlDest.highReg);
212 }
213
214 // Dest is now live and dirty (until/if we flush it to home location)
215 oatMarkLive(cUnit, rlDest.lowReg, rlDest.sRegLow);
216 oatMarkLive(cUnit, rlDest.highReg,
217 oatSRegHi(rlDest.sRegLow));
218 oatMarkDirty(cUnit, rlDest);
219 oatMarkPair(cUnit, rlDest.lowReg, rlDest.highReg);
220
221
222 oatResetDefLocWide(cUnit, rlDest);
223 if ((oatIsDirty(cUnit, rlDest.lowReg) ||
224 oatIsDirty(cUnit, rlDest.highReg)) &&
225 (oatLiveOut(cUnit, rlDest.sRegLow) ||
226 oatLiveOut(cUnit, oatSRegHi(rlDest.sRegLow)))) {
227 defStart = (LIR*)cUnit->lastLIRInsn;
buzbeeed3e9302011-09-23 17:34:19 -0700228 DCHECK_EQ((oatS2VReg(cUnit, rlDest.sRegLow)+1),
buzbee67bf8852011-08-17 17:51:35 -0700229 oatS2VReg(cUnit, oatSRegHi(rlDest.sRegLow)));
230 storeBaseDispWide(cUnit, rSP, rlDest.spOffset,
231 rlDest.lowReg, rlDest.highReg);
232 oatMarkClean(cUnit, rlDest);
233 defEnd = (LIR*)cUnit->lastLIRInsn;
234 oatMarkDefWide(cUnit, rlDest, defStart, defEnd);
235 }
236}