summaryrefslogtreecommitdiff
path: root/src/compiler/codegen/CodegenFactory.cc
blob: 9d380c7e3617d58e49d9d1742c290ba26581ba60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace art {

/*
 * This file contains target-independent codegen and support, and is
 * included by:
 *
 *        $(TARGET_ARCH)/Codegen-$(TARGET_ARCH_VARIANT).c
 *
 * which combines this common code with specific support found in the
 * applicable directories below this one.
 *
 */

/*
 * Load an immediate value into a fixed or temp register.  Target
 * register is clobbered, and marked inUse.
 */
LIR* loadConstant(CompilationUnit* cUnit, int rDest, int value)
{
  if (oatIsTemp(cUnit, rDest)) {
    oatClobber(cUnit, rDest);
    oatMarkInUse(cUnit, rDest);
  }
  return loadConstantNoClobber(cUnit, rDest, value);
}

/* Load a word at base + displacement.  Displacement must be word multiple */
LIR* loadWordDisp(CompilationUnit* cUnit, int rBase, int displacement,
                  int rDest)
{
  return loadBaseDisp(cUnit, rBase, displacement, rDest, kWord,
                      INVALID_SREG);
}

LIR* storeWordDisp(CompilationUnit* cUnit, int rBase, int displacement,
                   int rSrc)
{
  return storeBaseDisp(cUnit, rBase, displacement, rSrc, kWord);
}

/*
 * Load a Dalvik register into a physical register.  Take care when
 * using this routine, as it doesn't perform any bookkeeping regarding
 * register liveness.  That is the responsibility of the caller.
 */
void loadValueDirect(CompilationUnit* cUnit, RegLocation rlSrc, int rDest)
{
  rlSrc = oatUpdateLoc(cUnit, rlSrc);
  if (rlSrc.location == kLocPhysReg) {
    opRegCopy(cUnit, rDest, rlSrc.lowReg);
  } else {
    DCHECK((rlSrc.location == kLocDalvikFrame) ||
           (rlSrc.location == kLocCompilerTemp));
    loadWordDisp(cUnit, rSP, oatSRegOffset(cUnit, rlSrc.sRegLow), rDest);
  }
}

/*
 * Similar to loadValueDirect, but clobbers and allocates the target
 * register.  Should be used when loading to a fixed register (for example,
 * loading arguments to an out of line call.
 */
void loadValueDirectFixed(CompilationUnit* cUnit, RegLocation rlSrc, int rDest)
{
  oatClobber(cUnit, rDest);
  oatMarkInUse(cUnit, rDest);
  loadValueDirect(cUnit, rlSrc, rDest);
}

/*
 * Load a Dalvik register pair into a physical register[s].  Take care when
 * using this routine, as it doesn't perform any bookkeeping regarding
 * register liveness.  That is the responsibility of the caller.
 */
void loadValueDirectWide(CompilationUnit* cUnit, RegLocation rlSrc, int regLo,
             int regHi)
{
  rlSrc = oatUpdateLocWide(cUnit, rlSrc);
  if (rlSrc.location == kLocPhysReg) {
    opRegCopyWide(cUnit, regLo, regHi, rlSrc.lowReg, rlSrc.highReg);
  } else {
    DCHECK((rlSrc.location == kLocDalvikFrame) ||
           (rlSrc.location == kLocCompilerTemp));
    loadBaseDispWide(cUnit, rSP, oatSRegOffset(cUnit, rlSrc.sRegLow),
                     regLo, regHi, INVALID_SREG);
  }
}

/*
 * Similar to loadValueDirect, but clobbers and allocates the target
 * registers.  Should be used when loading to a fixed registers (for example,
 * loading arguments to an out of line call.
 */
void loadValueDirectWideFixed(CompilationUnit* cUnit, RegLocation rlSrc,
                              int regLo, int regHi)
{
  oatClobber(cUnit, regLo);
  oatClobber(cUnit, regHi);
  oatMarkInUse(cUnit, regLo);
  oatMarkInUse(cUnit, regHi);
  loadValueDirectWide(cUnit, rlSrc, regLo, regHi);
}

RegLocation loadValue(CompilationUnit* cUnit, RegLocation rlSrc,
                      RegisterClass opKind)
{
  rlSrc = oatEvalLoc(cUnit, rlSrc, opKind, false);
  if (rlSrc.location != kLocPhysReg) {
    DCHECK((rlSrc.location == kLocDalvikFrame) ||
           (rlSrc.location == kLocCompilerTemp));
    loadValueDirect(cUnit, rlSrc, rlSrc.lowReg);
    rlSrc.location = kLocPhysReg;
    oatMarkLive(cUnit, rlSrc.lowReg, rlSrc.sRegLow);
  }
  return rlSrc;
}

void storeValue(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc)
{
#ifndef NDEBUG
  /*
   * Sanity checking - should never try to store to the same
   * ssa name during the compilation of a single instruction
   * without an intervening oatClobberSReg().
   */
  DCHECK((cUnit->liveSReg == INVALID_SREG) ||
         (rlDest.sRegLow != cUnit->liveSReg));
  cUnit->liveSReg = rlDest.sRegLow;
#endif
  LIR* defStart;
  LIR* defEnd;
  DCHECK(!rlDest.wide);
  DCHECK(!rlSrc.wide);
  rlSrc = oatUpdateLoc(cUnit, rlSrc);
  rlDest = oatUpdateLoc(cUnit, rlDest);
  if (rlSrc.location == kLocPhysReg) {
    if (oatIsLive(cUnit, rlSrc.lowReg) ||
      oatIsPromoted(cUnit, rlSrc.lowReg) ||
      (rlDest.location == kLocPhysReg)) {
      // Src is live/promoted or Dest has assigned reg.
      rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false);
      opRegCopy(cUnit, rlDest.lowReg, rlSrc.lowReg);
    } else {
      // Just re-assign the registers.  Dest gets Src's regs
      rlDest.lowReg = rlSrc.lowReg;
      oatClobber(cUnit, rlSrc.lowReg);
    }
  } else {
    // Load Src either into promoted Dest or temps allocated for Dest
    rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false);
    loadValueDirect(cUnit, rlSrc, rlDest.lowReg);
  }

  // Dest is now live and dirty (until/if we flush it to home location)
  oatMarkLive(cUnit, rlDest.lowReg, rlDest.sRegLow);
  oatMarkDirty(cUnit, rlDest);


  oatResetDefLoc(cUnit, rlDest);
  if (oatIsDirty(cUnit, rlDest.lowReg) &&
      oatLiveOut(cUnit, rlDest.sRegLow)) {
    defStart = (LIR* )cUnit->lastLIRInsn;
    storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, rlDest.sRegLow),
                  rlDest.lowReg, kWord);
    oatMarkClean(cUnit, rlDest);
    defEnd = (LIR* )cUnit->lastLIRInsn;
    oatMarkDef(cUnit, rlDest, defStart, defEnd);
  }
}

RegLocation loadValueWide(CompilationUnit* cUnit, RegLocation rlSrc,
              RegisterClass opKind)
{
  DCHECK(rlSrc.wide);
  rlSrc = oatEvalLoc(cUnit, rlSrc, opKind, false);
  if (rlSrc.location != kLocPhysReg) {
    DCHECK((rlSrc.location == kLocDalvikFrame) ||
        (rlSrc.location == kLocCompilerTemp));
    loadValueDirectWide(cUnit, rlSrc, rlSrc.lowReg, rlSrc.highReg);
    rlSrc.location = kLocPhysReg;
    oatMarkLive(cUnit, rlSrc.lowReg, rlSrc.sRegLow);
    oatMarkLive(cUnit, rlSrc.highReg,
                oatSRegHi(rlSrc.sRegLow));
  }
  return rlSrc;
}

void storeValueWide(CompilationUnit* cUnit, RegLocation rlDest,
          RegLocation rlSrc)
{
#ifndef NDEBUG
  /*
   * Sanity checking - should never try to store to the same
   * ssa name during the compilation of a single instruction
   * without an intervening oatClobberSReg().
   */
  DCHECK((cUnit->liveSReg == INVALID_SREG) ||
      (rlDest.sRegLow != cUnit->liveSReg));
  cUnit->liveSReg = rlDest.sRegLow;
#endif
  LIR* defStart;
  LIR* defEnd;
  DCHECK_EQ(FPREG(rlSrc.lowReg), FPREG(rlSrc.highReg));
  DCHECK(rlDest.wide);
  DCHECK(rlSrc.wide);
  if (rlSrc.location == kLocPhysReg) {
    if (oatIsLive(cUnit, rlSrc.lowReg) ||
        oatIsLive(cUnit, rlSrc.highReg) ||
        oatIsPromoted(cUnit, rlSrc.lowReg) ||
        oatIsPromoted(cUnit, rlSrc.highReg) ||
        (rlDest.location == kLocPhysReg)) {
      // Src is live or promoted or Dest has assigned reg.
      rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false);
      opRegCopyWide(cUnit, rlDest.lowReg, rlDest.highReg,
                    rlSrc.lowReg, rlSrc.highReg);
    } else {
      // Just re-assign the registers.  Dest gets Src's regs
      rlDest.lowReg = rlSrc.lowReg;
      rlDest.highReg = rlSrc.highReg;
      oatClobber(cUnit, rlSrc.lowReg);
      oatClobber(cUnit, rlSrc.highReg);
    }
  } else {
    // Load Src either into promoted Dest or temps allocated for Dest
    rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false);
    loadValueDirectWide(cUnit, rlSrc, rlDest.lowReg, rlDest.highReg);
  }

  // Dest is now live and dirty (until/if we flush it to home location)
  oatMarkLive(cUnit, rlDest.lowReg, rlDest.sRegLow);
  oatMarkLive(cUnit, rlDest.highReg, oatSRegHi(rlDest.sRegLow));
  oatMarkDirty(cUnit, rlDest);
  oatMarkPair(cUnit, rlDest.lowReg, rlDest.highReg);


  oatResetDefLocWide(cUnit, rlDest);
  if ((oatIsDirty(cUnit, rlDest.lowReg) ||
      oatIsDirty(cUnit, rlDest.highReg)) &&
      (oatLiveOut(cUnit, rlDest.sRegLow) ||
      oatLiveOut(cUnit, oatSRegHi(rlDest.sRegLow)))) {
    defStart = (LIR*)cUnit->lastLIRInsn;
    DCHECK_EQ((SRegToVReg(cUnit, rlDest.sRegLow)+1),
              SRegToVReg(cUnit, oatSRegHi(rlDest.sRegLow)));
    storeBaseDispWide(cUnit, rSP, oatSRegOffset(cUnit, rlDest.sRegLow),
                      rlDest.lowReg, rlDest.highReg);
    oatMarkClean(cUnit, rlDest);
    defEnd = (LIR*)cUnit->lastLIRInsn;
    oatMarkDefWide(cUnit, rlDest, defStart, defEnd);
  }
}

/*
 * Mark garbage collection card. Skip if the value we're storing is null.
 */
void markGCCard(CompilationUnit* cUnit, int valReg, int tgtAddrReg)
{
  int regCardBase = oatAllocTemp(cUnit);
  int regCardNo = oatAllocTemp(cUnit);
  LIR* branchOver = opCmpImmBranch(cUnit, kCondEq, valReg, 0, NULL);
#if !defined(TARGET_X86)
  loadWordDisp(cUnit, rSELF, Thread::CardTableOffset().Int32Value(),
               regCardBase);
#else
  newLIR2(cUnit, kX86Mov32RT, regCardBase,
          Thread::CardTableOffset().Int32Value());
#endif
  opRegRegImm(cUnit, kOpLsr, regCardNo, tgtAddrReg, GC_CARD_SHIFT);
  storeBaseIndexed(cUnit, regCardBase, regCardNo, regCardBase, 0,
                   kUnsignedByte);
  LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
  branchOver->target = (LIR*)target;
  oatFreeTemp(cUnit, regCardBase);
  oatFreeTemp(cUnit, regCardNo);
}

/* Utilities to load the current Method* */
void loadCurrMethodDirect(CompilationUnit *cUnit, int rTgt)
{
  loadValueDirectFixed(cUnit, cUnit->methodLoc, rTgt);
}

RegLocation loadCurrMethod(CompilationUnit *cUnit)
{
  return loadValue(cUnit, cUnit->methodLoc, kCoreReg);
}

bool methodStarInReg(CompilationUnit* cUnit)
{
   return (cUnit->methodLoc.location == kLocPhysReg);
}


}  // namespace art