blob: dd63621050fdaaa424a9b902e84ff7ef78249544 [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 */
35static TGT_LIR* loadWordDisp(CompilationUnit* cUnit, int rBase,
36 int displacement, int rDest)
37{
38 return loadBaseDisp(cUnit, NULL, rBase, displacement, rDest, kWord,
39 INVALID_SREG);
40}
41
42static TGT_LIR* storeWordDisp(CompilationUnit* cUnit, int rBase,
43 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,
54 int reg1)
55{
56 rlSrc = oatUpdateLoc(cUnit, rlSrc);
57 if (rlSrc.location == kLocPhysReg) {
58 genRegCopy(cUnit, reg1, rlSrc.lowReg);
59 } else {
60 assert(rlSrc.location == kLocDalvikFrame);
61 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 */
70static void loadValueDirectFixed(CompilationUnit* cUnit, RegLocation rlSrc,
71 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 */
83static void loadValueDirectWide(CompilationUnit* cUnit, RegLocation rlSrc,
84 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 {
90 assert(rlSrc.location == kLocDalvikFrame);
91 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 */
101static void loadValueDirectWideFixed(CompilationUnit* cUnit, RegLocation rlSrc,
102 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
111static RegLocation loadValue(CompilationUnit* cUnit, RegLocation rlSrc,
112 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
123static void storeValue(CompilationUnit* cUnit, RegLocation rlDest,
124 RegLocation rlSrc)
125{
126 LIR* defStart;
127 LIR* defEnd;
128 assert(!rlDest.wide);
129 assert(!rlSrc.wide);
130 oatKillNullCheckedLoc(cUnit, rlDest);
131 rlSrc = oatUpdateLoc(cUnit, rlSrc);
132 rlDest = oatUpdateLoc(cUnit, rlDest);
133 if (rlSrc.location == kLocPhysReg) {
134 if (oatIsLive(cUnit, rlSrc.lowReg) ||
135 (rlDest.location == kLocPhysReg)) {
136 // Src is live or Dest has assigned reg.
137 rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false);
138 genRegCopy(cUnit, rlDest.lowReg, rlSrc.lowReg);
139 } else {
140 // Just re-assign the registers. Dest gets Src's regs
141 rlDest.lowReg = rlSrc.lowReg;
142 oatClobber(cUnit, rlSrc.lowReg);
143 }
144 } else {
145 // Load Src either into promoted Dest or temps allocated for Dest
146 rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false);
147 loadValueDirect(cUnit, rlSrc, rlDest.lowReg);
148 }
149
150 // Dest is now live and dirty (until/if we flush it to home location)
151 oatMarkLive(cUnit, rlDest.lowReg, rlDest.sRegLow);
152 oatMarkDirty(cUnit, rlDest);
153
154
155 oatResetDefLoc(cUnit, rlDest);
156 if (oatIsDirty(cUnit, rlDest.lowReg) &&
157 oatLiveOut(cUnit, rlDest.sRegLow)) {
158 defStart = (LIR* )cUnit->lastLIRInsn;
159 storeBaseDisp(cUnit, rSP, rlDest.spOffset, rlDest.lowReg, kWord);
160 oatMarkClean(cUnit, rlDest);
161 defEnd = (LIR* )cUnit->lastLIRInsn;
162 oatMarkDef(cUnit, rlDest, defStart, defEnd);
163 }
164}
165
166static RegLocation loadValueWide(CompilationUnit* cUnit, RegLocation rlSrc,
167 RegisterClass opKind)
168{
169 assert(rlSrc.wide);
170 rlSrc = oatEvalLoc(cUnit, rlSrc, opKind, false);
171 if (rlSrc.location == kLocDalvikFrame) {
172 loadValueDirectWide(cUnit, rlSrc, rlSrc.lowReg, rlSrc.highReg);
173 rlSrc.location = kLocPhysReg;
174 oatMarkLive(cUnit, rlSrc.lowReg, rlSrc.sRegLow);
175 oatMarkLive(cUnit, rlSrc.highReg,
176 oatSRegHi(rlSrc.sRegLow));
177 }
178 return rlSrc;
179}
180
181static void storeValueWide(CompilationUnit* cUnit, RegLocation rlDest,
182 RegLocation rlSrc)
183{
184 LIR* defStart;
185 LIR* defEnd;
186 assert(FPREG(rlSrc.lowReg)==FPREG(rlSrc.highReg));
187 assert(rlDest.wide);
188 assert(rlSrc.wide);
189 oatKillNullCheckedLoc(cUnit, rlDest);
190 if (rlSrc.location == kLocPhysReg) {
191 if (oatIsLive(cUnit, rlSrc.lowReg) ||
192 oatIsLive(cUnit, rlSrc.highReg) ||
193 (rlDest.location == kLocPhysReg)) {
194 // Src is live or Dest has assigned reg.
195 rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false);
196 genRegCopyWide(cUnit, rlDest.lowReg, rlDest.highReg,
197 rlSrc.lowReg, rlSrc.highReg);
198 } else {
199 // Just re-assign the registers. Dest gets Src's regs
200 rlDest.lowReg = rlSrc.lowReg;
201 rlDest.highReg = rlSrc.highReg;
202 oatClobber(cUnit, rlSrc.lowReg);
203 oatClobber(cUnit, rlSrc.highReg);
204 }
205 } else {
206 // Load Src either into promoted Dest or temps allocated for Dest
207 rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false);
208 loadValueDirectWide(cUnit, rlSrc, rlDest.lowReg,
209 rlDest.highReg);
210 }
211
212 // Dest is now live and dirty (until/if we flush it to home location)
213 oatMarkLive(cUnit, rlDest.lowReg, rlDest.sRegLow);
214 oatMarkLive(cUnit, rlDest.highReg,
215 oatSRegHi(rlDest.sRegLow));
216 oatMarkDirty(cUnit, rlDest);
217 oatMarkPair(cUnit, rlDest.lowReg, rlDest.highReg);
218
219
220 oatResetDefLocWide(cUnit, rlDest);
221 if ((oatIsDirty(cUnit, rlDest.lowReg) ||
222 oatIsDirty(cUnit, rlDest.highReg)) &&
223 (oatLiveOut(cUnit, rlDest.sRegLow) ||
224 oatLiveOut(cUnit, oatSRegHi(rlDest.sRegLow)))) {
225 defStart = (LIR*)cUnit->lastLIRInsn;
226 assert((oatS2VReg(cUnit, rlDest.sRegLow)+1) ==
227 oatS2VReg(cUnit, oatSRegHi(rlDest.sRegLow)));
228 storeBaseDispWide(cUnit, rSP, rlDest.spOffset,
229 rlDest.lowReg, rlDest.highReg);
230 oatMarkClean(cUnit, rlDest);
231 defEnd = (LIR*)cUnit->lastLIRInsn;
232 oatMarkDefWide(cUnit, rlDest, defStart, defEnd);
233 }
234}