blob: 61e29d7d1f75fef80eb520674dcd183cd2f0a086 [file] [log] [blame]
Ben Cheng5d90c202009-11-22 23:31:11 -08001/*
2 * Copyright (C) 2009 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/*
buzbeee3f97d32010-09-26 13:47:06 -070018 * This file contains target-independent codegen and support, and is
19 * included by:
Ben Cheng5d90c202009-11-22 23:31:11 -080020 *
buzbeee3f97d32010-09-26 13:47:06 -070021 * $(TARGET_ARCH)/Codegen-$(TARGET_ARCH_VARIANT).c
Ben Cheng5d90c202009-11-22 23:31:11 -080022 *
23 * which combines this common code with specific support found in the
buzbeee3f97d32010-09-26 13:47:06 -070024 * 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
Ben Cheng5d90c202009-11-22 23:31:11 -080031 */
32
33
34/* Load a word at base + displacement. Displacement must be word multiple */
buzbeee3f97d32010-09-26 13:47:06 -070035static TGT_LIR *loadWordDisp(CompilationUnit *cUnit, int rBase,
36 int displacement, int rDest)
Ben Cheng5d90c202009-11-22 23:31:11 -080037{
38 return loadBaseDisp(cUnit, NULL, rBase, displacement, rDest, kWord,
39 INVALID_SREG);
40}
41
buzbeee3f97d32010-09-26 13:47:06 -070042static TGT_LIR *storeWordDisp(CompilationUnit *cUnit, int rBase,
Ben Cheng5d90c202009-11-22 23:31:11 -080043 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 */
53static void loadValueDirect(CompilationUnit *cUnit, RegLocation rlSrc,
buzbeee3f97d32010-09-26 13:47:06 -070054 int reg1)
Ben Cheng5d90c202009-11-22 23:31:11 -080055{
Bill Buzbeec6f10662010-02-09 11:16:15 -080056 rlSrc = dvmCompilerUpdateLoc(cUnit, rlSrc);
Ben Cheng5d90c202009-11-22 23:31:11 -080057 if (rlSrc.location == kLocPhysReg) {
58 genRegCopy(cUnit, reg1, rlSrc.lowReg);
59 } else if (rlSrc.location == kLocRetval) {
buzbee9f601a92011-02-11 17:48:20 -080060 loadWordDisp(cUnit, rSELF, offsetof(Thread, retval), reg1);
Ben Cheng5d90c202009-11-22 23:31:11 -080061 } else {
62 assert(rlSrc.location == kLocDalvikFrame);
Bill Buzbeec6f10662010-02-09 11:16:15 -080063 loadWordDisp(cUnit, rFP, dvmCompilerS2VReg(cUnit, rlSrc.sRegLow) << 2,
Ben Cheng5d90c202009-11-22 23:31:11 -080064 reg1);
65 }
66}
67
68/*
69 * Similar to loadValueDirect, but clobbers and allocates the target
70 * register. Should be used when loading to a fixed register (for example,
71 * loading arguments to an out of line call.
72 */
73static void loadValueDirectFixed(CompilationUnit *cUnit, RegLocation rlSrc,
74 int reg1)
75{
Bill Buzbeec6f10662010-02-09 11:16:15 -080076 dvmCompilerClobber(cUnit, reg1);
Elliott Hughes6a555132010-02-25 15:41:42 -080077 dvmCompilerMarkInUse(cUnit, reg1);
Ben Cheng5d90c202009-11-22 23:31:11 -080078 loadValueDirect(cUnit, rlSrc, reg1);
79}
80
81/*
82 * Load a Dalvik register pair into a physical register[s]. Take care when
83 * using this routine, as it doesn't perform any bookkeeping regarding
84 * register liveness. That is the responsibility of the caller.
85 */
86static void loadValueDirectWide(CompilationUnit *cUnit, RegLocation rlSrc,
87 int regLo, int regHi)
88{
Bill Buzbeec6f10662010-02-09 11:16:15 -080089 rlSrc = dvmCompilerUpdateLocWide(cUnit, rlSrc);
Ben Cheng5d90c202009-11-22 23:31:11 -080090 if (rlSrc.location == kLocPhysReg) {
91 genRegCopyWide(cUnit, regLo, regHi, rlSrc.lowReg, rlSrc.highReg);
92 } else if (rlSrc.location == kLocRetval) {
buzbee9f601a92011-02-11 17:48:20 -080093 loadBaseDispWide(cUnit, NULL, rSELF, offsetof(Thread, retval),
Ben Cheng5d90c202009-11-22 23:31:11 -080094 regLo, regHi, INVALID_SREG);
95 } else {
96 assert(rlSrc.location == kLocDalvikFrame);
97 loadBaseDispWide(cUnit, NULL, rFP,
Bill Buzbeec6f10662010-02-09 11:16:15 -080098 dvmCompilerS2VReg(cUnit, rlSrc.sRegLow) << 2,
Ben Cheng5d90c202009-11-22 23:31:11 -080099 regLo, regHi, INVALID_SREG);
100 }
101}
102
103/*
104 * Similar to loadValueDirect, but clobbers and allocates the target
105 * registers. Should be used when loading to a fixed registers (for example,
106 * loading arguments to an out of line call.
107 */
108static void loadValueDirectWideFixed(CompilationUnit *cUnit, RegLocation rlSrc,
109 int regLo, int regHi)
110{
Bill Buzbeec6f10662010-02-09 11:16:15 -0800111 dvmCompilerClobber(cUnit, regLo);
112 dvmCompilerClobber(cUnit, regHi);
Elliott Hughes6a555132010-02-25 15:41:42 -0800113 dvmCompilerMarkInUse(cUnit, regLo);
114 dvmCompilerMarkInUse(cUnit, regHi);
Ben Cheng5d90c202009-11-22 23:31:11 -0800115 loadValueDirectWide(cUnit, rlSrc, regLo, regHi);
116}
117
118static RegLocation loadValue(CompilationUnit *cUnit, RegLocation rlSrc,
119 RegisterClass opKind)
120{
Bill Buzbeec6f10662010-02-09 11:16:15 -0800121 rlSrc = dvmCompilerEvalLoc(cUnit, rlSrc, opKind, false);
Ben Cheng5d90c202009-11-22 23:31:11 -0800122 if (rlSrc.location == kLocDalvikFrame) {
123 loadValueDirect(cUnit, rlSrc, rlSrc.lowReg);
124 rlSrc.location = kLocPhysReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800125 dvmCompilerMarkLive(cUnit, rlSrc.lowReg, rlSrc.sRegLow);
Ben Cheng5d90c202009-11-22 23:31:11 -0800126 } else if (rlSrc.location == kLocRetval) {
buzbee9f601a92011-02-11 17:48:20 -0800127 loadWordDisp(cUnit, rSELF, offsetof(Thread, retval), rlSrc.lowReg);
Ben Cheng5d90c202009-11-22 23:31:11 -0800128 rlSrc.location = kLocPhysReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800129 dvmCompilerClobber(cUnit, rlSrc.lowReg);
Ben Cheng5d90c202009-11-22 23:31:11 -0800130 }
131 return rlSrc;
132}
133
134static void storeValue(CompilationUnit *cUnit, RegLocation rlDest,
135 RegLocation rlSrc)
136{
Ben Cheng5d90c202009-11-22 23:31:11 -0800137 LIR *defStart;
138 LIR *defEnd;
139 assert(!rlDest.wide);
140 assert(!rlSrc.wide);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800141 dvmCompilerKillNullCheckedLoc(cUnit, rlDest);
142 rlSrc = dvmCompilerUpdateLoc(cUnit, rlSrc);
143 rlDest = dvmCompilerUpdateLoc(cUnit, rlDest);
Ben Cheng5d90c202009-11-22 23:31:11 -0800144 if (rlSrc.location == kLocPhysReg) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800145 if (dvmCompilerIsLive(cUnit, rlSrc.lowReg) ||
146 (rlDest.location == kLocPhysReg)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800147 // Src is live or Dest has assigned reg.
Bill Buzbeec6f10662010-02-09 11:16:15 -0800148 rlDest = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, false);
Ben Cheng5d90c202009-11-22 23:31:11 -0800149 genRegCopy(cUnit, rlDest.lowReg, rlSrc.lowReg);
150 } else {
151 // Just re-assign the registers. Dest gets Src's regs
152 rlDest.lowReg = rlSrc.lowReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800153 dvmCompilerClobber(cUnit, rlSrc.lowReg);
Ben Cheng5d90c202009-11-22 23:31:11 -0800154 }
155 } else {
156 // Load Src either into promoted Dest or temps allocated for Dest
Bill Buzbeec6f10662010-02-09 11:16:15 -0800157 rlDest = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, false);
Ben Cheng5d90c202009-11-22 23:31:11 -0800158 loadValueDirect(cUnit, rlSrc, rlDest.lowReg);
159 }
160
161 // Dest is now live and dirty (until/if we flush it to home location)
Bill Buzbeec6f10662010-02-09 11:16:15 -0800162 dvmCompilerMarkLive(cUnit, rlDest.lowReg, rlDest.sRegLow);
163 dvmCompilerMarkDirty(cUnit, rlDest.lowReg);
Ben Cheng5d90c202009-11-22 23:31:11 -0800164
165
166 if (rlDest.location == kLocRetval) {
buzbee9f601a92011-02-11 17:48:20 -0800167 storeBaseDisp(cUnit, rSELF, offsetof(Thread, retval),
Ben Cheng5d90c202009-11-22 23:31:11 -0800168 rlDest.lowReg, kWord);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800169 dvmCompilerClobber(cUnit, rlDest.lowReg);
Ben Cheng5d90c202009-11-22 23:31:11 -0800170 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800171 dvmCompilerResetDefLoc(cUnit, rlDest);
172 if (dvmCompilerLiveOut(cUnit, rlDest.sRegLow)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800173 defStart = (LIR *)cUnit->lastLIRInsn;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800174 int vReg = dvmCompilerS2VReg(cUnit, rlDest.sRegLow);
Ben Cheng5d90c202009-11-22 23:31:11 -0800175 storeBaseDisp(cUnit, rFP, vReg << 2, rlDest.lowReg, kWord);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800176 dvmCompilerMarkClean(cUnit, rlDest.lowReg);
Ben Cheng5d90c202009-11-22 23:31:11 -0800177 defEnd = (LIR *)cUnit->lastLIRInsn;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800178 dvmCompilerMarkDef(cUnit, rlDest, defStart, defEnd);
Ben Cheng5d90c202009-11-22 23:31:11 -0800179 }
180 }
181}
182
183static RegLocation loadValueWide(CompilationUnit *cUnit, RegLocation rlSrc,
184 RegisterClass opKind)
185{
Ben Cheng5d90c202009-11-22 23:31:11 -0800186 assert(rlSrc.wide);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800187 rlSrc = dvmCompilerEvalLoc(cUnit, rlSrc, opKind, false);
Ben Cheng5d90c202009-11-22 23:31:11 -0800188 if (rlSrc.location == kLocDalvikFrame) {
189 loadValueDirectWide(cUnit, rlSrc, rlSrc.lowReg, rlSrc.highReg);
190 rlSrc.location = kLocPhysReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800191 dvmCompilerMarkLive(cUnit, rlSrc.lowReg, rlSrc.sRegLow);
192 dvmCompilerMarkLive(cUnit, rlSrc.highReg,
193 dvmCompilerSRegHi(rlSrc.sRegLow));
Ben Cheng5d90c202009-11-22 23:31:11 -0800194 } else if (rlSrc.location == kLocRetval) {
buzbee9f601a92011-02-11 17:48:20 -0800195 loadBaseDispWide(cUnit, NULL, rSELF, offsetof(Thread, retval),
Ben Cheng5d90c202009-11-22 23:31:11 -0800196 rlSrc.lowReg, rlSrc.highReg, INVALID_SREG);
197 rlSrc.location = kLocPhysReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800198 dvmCompilerClobber(cUnit, rlSrc.lowReg);
199 dvmCompilerClobber(cUnit, rlSrc.highReg);
Ben Cheng5d90c202009-11-22 23:31:11 -0800200 }
201 return rlSrc;
202}
203
204static void storeValueWide(CompilationUnit *cUnit, RegLocation rlDest,
buzbeee3f97d32010-09-26 13:47:06 -0700205 RegLocation rlSrc)
Ben Cheng5d90c202009-11-22 23:31:11 -0800206{
Ben Cheng5d90c202009-11-22 23:31:11 -0800207 LIR *defStart;
208 LIR *defEnd;
Ben Cheng5d90c202009-11-22 23:31:11 -0800209 assert(FPREG(rlSrc.lowReg)==FPREG(rlSrc.highReg));
210 assert(rlDest.wide);
211 assert(rlSrc.wide);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800212 dvmCompilerKillNullCheckedLoc(cUnit, rlDest);
Ben Cheng5d90c202009-11-22 23:31:11 -0800213 if (rlSrc.location == kLocPhysReg) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800214 if (dvmCompilerIsLive(cUnit, rlSrc.lowReg) ||
215 dvmCompilerIsLive(cUnit, rlSrc.highReg) ||
Ben Cheng5d90c202009-11-22 23:31:11 -0800216 (rlDest.location == kLocPhysReg)) {
217 // Src is live or Dest has assigned reg.
Bill Buzbeec6f10662010-02-09 11:16:15 -0800218 rlDest = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, false);
Ben Cheng5d90c202009-11-22 23:31:11 -0800219 genRegCopyWide(cUnit, rlDest.lowReg, rlDest.highReg,
220 rlSrc.lowReg, rlSrc.highReg);
221 } else {
222 // Just re-assign the registers. Dest gets Src's regs
223 rlDest.lowReg = rlSrc.lowReg;
224 rlDest.highReg = rlSrc.highReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800225 dvmCompilerClobber(cUnit, rlSrc.lowReg);
226 dvmCompilerClobber(cUnit, rlSrc.highReg);
Ben Cheng5d90c202009-11-22 23:31:11 -0800227 }
228 } else {
229 // Load Src either into promoted Dest or temps allocated for Dest
Bill Buzbeec6f10662010-02-09 11:16:15 -0800230 rlDest = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, false);
Ben Cheng5d90c202009-11-22 23:31:11 -0800231 loadValueDirectWide(cUnit, rlSrc, rlDest.lowReg,
232 rlDest.highReg);
233 }
234
235 // Dest is now live and dirty (until/if we flush it to home location)
Bill Buzbeec6f10662010-02-09 11:16:15 -0800236 dvmCompilerMarkLive(cUnit, rlDest.lowReg, rlDest.sRegLow);
237 dvmCompilerMarkLive(cUnit, rlDest.highReg,
238 dvmCompilerSRegHi(rlDest.sRegLow));
239 dvmCompilerMarkDirty(cUnit, rlDest.lowReg);
240 dvmCompilerMarkDirty(cUnit, rlDest.highReg);
241 dvmCompilerMarkPair(cUnit, rlDest.lowReg, rlDest.highReg);
Ben Cheng5d90c202009-11-22 23:31:11 -0800242
243
244 if (rlDest.location == kLocRetval) {
buzbee9f601a92011-02-11 17:48:20 -0800245 storeBaseDispWide(cUnit, rSELF, offsetof(Thread, retval),
Ben Cheng5d90c202009-11-22 23:31:11 -0800246 rlDest.lowReg, rlDest.highReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800247 dvmCompilerClobber(cUnit, rlDest.lowReg);
248 dvmCompilerClobber(cUnit, rlDest.highReg);
Ben Cheng5d90c202009-11-22 23:31:11 -0800249 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800250 dvmCompilerResetDefLocWide(cUnit, rlDest);
251 if (dvmCompilerLiveOut(cUnit, rlDest.sRegLow) ||
252 dvmCompilerLiveOut(cUnit, dvmCompilerSRegHi(rlDest.sRegLow))) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800253 defStart = (LIR *)cUnit->lastLIRInsn;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800254 int vReg = dvmCompilerS2VReg(cUnit, rlDest.sRegLow);
255 assert((vReg+1) == dvmCompilerS2VReg(cUnit,
256 dvmCompilerSRegHi(rlDest.sRegLow)));
Ben Cheng5d90c202009-11-22 23:31:11 -0800257 storeBaseDispWide(cUnit, rFP, vReg << 2, rlDest.lowReg,
258 rlDest.highReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800259 dvmCompilerMarkClean(cUnit, rlDest.lowReg);
260 dvmCompilerMarkClean(cUnit, rlDest.highReg);
Ben Cheng5d90c202009-11-22 23:31:11 -0800261 defEnd = (LIR *)cUnit->lastLIRInsn;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800262 dvmCompilerMarkDefWide(cUnit, rlDest, defStart, defEnd);
Ben Cheng5d90c202009-11-22 23:31:11 -0800263 }
264 }
265}