blob: 5dbcd9769a5f929d41590ffde67f8144c8cc174b [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);
buzbee67bc2362011-10-11 18:08:40 -070061 loadWordDisp(cUnit, rSP, oatSRegOffset(cUnit, rlSrc.sRegLow), reg1);
buzbee67bf8852011-08-17 17:51:35 -070062 }
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);
buzbee67bc2362011-10-11 18:08:40 -070091 loadBaseDispWide(cUnit, NULL, rSP,
92 oatSRegOffset(cUnit, rlSrc.sRegLow),
buzbee67bf8852011-08-17 17:51:35 -070093 regLo, regHi, INVALID_SREG);
94 }
95}
96
97/*
98 * Similar to loadValueDirect, but clobbers and allocates the target
99 * registers. Should be used when loading to a fixed registers (for example,
100 * loading arguments to an out of line call.
101 */
buzbeeed3e9302011-09-23 17:34:19 -0700102STATIC void loadValueDirectWideFixed(CompilationUnit* cUnit, RegLocation rlSrc,
buzbee67bf8852011-08-17 17:51:35 -0700103 int regLo, int regHi)
104{
105 oatClobber(cUnit, regLo);
106 oatClobber(cUnit, regHi);
107 oatMarkInUse(cUnit, regLo);
108 oatMarkInUse(cUnit, regHi);
109 loadValueDirectWide(cUnit, rlSrc, regLo, regHi);
110}
111
buzbeeed3e9302011-09-23 17:34:19 -0700112STATIC RegLocation loadValue(CompilationUnit* cUnit, RegLocation rlSrc,
buzbee67bf8852011-08-17 17:51:35 -0700113 RegisterClass opKind)
114{
115 rlSrc = oatEvalLoc(cUnit, rlSrc, opKind, false);
116 if (rlSrc.location == kLocDalvikFrame) {
117 loadValueDirect(cUnit, rlSrc, rlSrc.lowReg);
118 rlSrc.location = kLocPhysReg;
119 oatMarkLive(cUnit, rlSrc.lowReg, rlSrc.sRegLow);
120 }
121 return rlSrc;
122}
123
buzbeeed3e9302011-09-23 17:34:19 -0700124STATIC void storeValue(CompilationUnit* cUnit, RegLocation rlDest,
buzbee67bf8852011-08-17 17:51:35 -0700125 RegLocation rlSrc)
126{
127 LIR* defStart;
128 LIR* defEnd;
buzbeeed3e9302011-09-23 17:34:19 -0700129 DCHECK(!rlDest.wide);
130 DCHECK(!rlSrc.wide);
buzbee67bf8852011-08-17 17:51:35 -0700131 rlSrc = oatUpdateLoc(cUnit, rlSrc);
132 rlDest = oatUpdateLoc(cUnit, rlDest);
133 if (rlSrc.location == kLocPhysReg) {
134 if (oatIsLive(cUnit, rlSrc.lowReg) ||
buzbeeb29e4d12011-09-26 15:05:48 -0700135 oatIsPromoted(cUnit, rlSrc.lowReg) ||
buzbee67bf8852011-08-17 17:51:35 -0700136 (rlDest.location == kLocPhysReg)) {
buzbeeb29e4d12011-09-26 15:05:48 -0700137 // Src is live/promoted or Dest has assigned reg.
buzbee67bf8852011-08-17 17:51:35 -0700138 rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false);
139 genRegCopy(cUnit, rlDest.lowReg, rlSrc.lowReg);
140 } else {
141 // Just re-assign the registers. Dest gets Src's regs
142 rlDest.lowReg = rlSrc.lowReg;
143 oatClobber(cUnit, rlSrc.lowReg);
144 }
145 } else {
146 // Load Src either into promoted Dest or temps allocated for Dest
147 rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false);
148 loadValueDirect(cUnit, rlSrc, rlDest.lowReg);
149 }
150
151 // Dest is now live and dirty (until/if we flush it to home location)
152 oatMarkLive(cUnit, rlDest.lowReg, rlDest.sRegLow);
153 oatMarkDirty(cUnit, rlDest);
154
155
156 oatResetDefLoc(cUnit, rlDest);
157 if (oatIsDirty(cUnit, rlDest.lowReg) &&
158 oatLiveOut(cUnit, rlDest.sRegLow)) {
159 defStart = (LIR* )cUnit->lastLIRInsn;
buzbee67bc2362011-10-11 18:08:40 -0700160 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, rlDest.sRegLow),
161 rlDest.lowReg, kWord);
buzbee67bf8852011-08-17 17:51:35 -0700162 oatMarkClean(cUnit, rlDest);
163 defEnd = (LIR* )cUnit->lastLIRInsn;
164 oatMarkDef(cUnit, rlDest, defStart, defEnd);
165 }
166}
167
buzbeeed3e9302011-09-23 17:34:19 -0700168STATIC RegLocation loadValueWide(CompilationUnit* cUnit, RegLocation rlSrc,
buzbee67bf8852011-08-17 17:51:35 -0700169 RegisterClass opKind)
170{
buzbeeed3e9302011-09-23 17:34:19 -0700171 DCHECK(rlSrc.wide);
buzbee67bf8852011-08-17 17:51:35 -0700172 rlSrc = oatEvalLoc(cUnit, rlSrc, opKind, false);
173 if (rlSrc.location == kLocDalvikFrame) {
174 loadValueDirectWide(cUnit, rlSrc, rlSrc.lowReg, rlSrc.highReg);
175 rlSrc.location = kLocPhysReg;
176 oatMarkLive(cUnit, rlSrc.lowReg, rlSrc.sRegLow);
177 oatMarkLive(cUnit, rlSrc.highReg,
178 oatSRegHi(rlSrc.sRegLow));
179 }
180 return rlSrc;
181}
182
buzbeeed3e9302011-09-23 17:34:19 -0700183STATIC void storeValueWide(CompilationUnit* cUnit, RegLocation rlDest,
buzbee67bf8852011-08-17 17:51:35 -0700184 RegLocation rlSrc)
185{
186 LIR* defStart;
187 LIR* defEnd;
buzbeeed3e9302011-09-23 17:34:19 -0700188 DCHECK_EQ(FPREG(rlSrc.lowReg), FPREG(rlSrc.highReg));
189 DCHECK(rlDest.wide);
190 DCHECK(rlSrc.wide);
buzbee67bf8852011-08-17 17:51:35 -0700191 if (rlSrc.location == kLocPhysReg) {
192 if (oatIsLive(cUnit, rlSrc.lowReg) ||
193 oatIsLive(cUnit, rlSrc.highReg) ||
buzbeeb29e4d12011-09-26 15:05:48 -0700194 oatIsPromoted(cUnit, rlSrc.lowReg) ||
195 oatIsPromoted(cUnit, rlSrc.highReg) ||
buzbee67bf8852011-08-17 17:51:35 -0700196 (rlDest.location == kLocPhysReg)) {
buzbeeb29e4d12011-09-26 15:05:48 -0700197 // Src is live or promoted or Dest has assigned reg.
buzbee67bf8852011-08-17 17:51:35 -0700198 rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false);
199 genRegCopyWide(cUnit, rlDest.lowReg, rlDest.highReg,
200 rlSrc.lowReg, rlSrc.highReg);
201 } else {
202 // Just re-assign the registers. Dest gets Src's regs
203 rlDest.lowReg = rlSrc.lowReg;
204 rlDest.highReg = rlSrc.highReg;
205 oatClobber(cUnit, rlSrc.lowReg);
206 oatClobber(cUnit, rlSrc.highReg);
207 }
208 } else {
209 // Load Src either into promoted Dest or temps allocated for Dest
210 rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false);
211 loadValueDirectWide(cUnit, rlSrc, rlDest.lowReg,
212 rlDest.highReg);
213 }
214
215 // Dest is now live and dirty (until/if we flush it to home location)
216 oatMarkLive(cUnit, rlDest.lowReg, rlDest.sRegLow);
217 oatMarkLive(cUnit, rlDest.highReg,
218 oatSRegHi(rlDest.sRegLow));
219 oatMarkDirty(cUnit, rlDest);
220 oatMarkPair(cUnit, rlDest.lowReg, rlDest.highReg);
221
222
223 oatResetDefLocWide(cUnit, rlDest);
224 if ((oatIsDirty(cUnit, rlDest.lowReg) ||
225 oatIsDirty(cUnit, rlDest.highReg)) &&
226 (oatLiveOut(cUnit, rlDest.sRegLow) ||
227 oatLiveOut(cUnit, oatSRegHi(rlDest.sRegLow)))) {
228 defStart = (LIR*)cUnit->lastLIRInsn;
buzbeeed3e9302011-09-23 17:34:19 -0700229 DCHECK_EQ((oatS2VReg(cUnit, rlDest.sRegLow)+1),
buzbee67bf8852011-08-17 17:51:35 -0700230 oatS2VReg(cUnit, oatSRegHi(rlDest.sRegLow)));
buzbee67bc2362011-10-11 18:08:40 -0700231 storeBaseDispWide(cUnit, rSP, oatSRegOffset(cUnit, rlDest.sRegLow),
buzbee67bf8852011-08-17 17:51:35 -0700232 rlDest.lowReg, rlDest.highReg);
233 oatMarkClean(cUnit, rlDest);
234 defEnd = (LIR*)cUnit->lastLIRInsn;
235 oatMarkDefWide(cUnit, rlDest, defStart, defEnd);
236 }
237}