blob: d1ba45c18c01d350c67dde0fc893d3c846627324 [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#include "Dalvik.h"
18#include "CompilerInternals.h"
19
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080020namespace art {
21
Ian Rogers680b1bd2012-03-07 20:18:49 -080022static const char* gOpKindNames[kOpInvalid + 1] = {
23 "OpMov",
24 "OpMvn",
25 "OpCmp",
26 "OpLsl",
27 "OpLsr",
28 "OpAsr",
29 "OpRor",
30 "OpNot",
31 "OpAnd",
32 "OpOr",
33 "OpXor",
34 "OpNeg",
35 "OpAdd",
36 "OpAdc",
37 "OpSub",
38 "OpSbc",
39 "OpRsub",
40 "OpMul",
41 "OpDiv",
42 "OpRem",
43 "OpBic",
44 "OpCmn",
45 "OpTst",
46 "OpBkpt",
47 "OpBlx",
48 "OpPush",
49 "OpPop",
50 "Op2Char",
51 "Op2Short",
52 "Op2Byte",
53 "OpCondBr",
54 "OpUncondBr",
55 "OpBx",
56 "OpInvalid",
57};
58
59std::ostream& operator<<(std::ostream& os, const OpKind& kind) {
60 if (kind >= kOpMov && kind <= kOpInvalid) {
61 os << gOpKindNames[kind];
62 } else {
63 os << "Unknown Op " << static_cast<int>(kind);
64 }
65 return os;
66}
67
buzbee67bf8852011-08-17 17:51:35 -070068/* Allocate a new basic block */
buzbee5abfa3e2012-01-31 17:01:43 -080069BasicBlock* oatNewBB(CompilationUnit* cUnit, BBType blockType, int blockId)
buzbee67bf8852011-08-17 17:51:35 -070070{
buzbeeba938cb2012-02-03 14:47:55 -080071 BasicBlock* bb = (BasicBlock* )oatNew(cUnit, sizeof(BasicBlock), true,
72 kAllocBB);
buzbee67bf8852011-08-17 17:51:35 -070073 bb->blockType = blockType;
74 bb->id = blockId;
buzbeeba938cb2012-02-03 14:47:55 -080075 bb->predecessors = (GrowableList*) oatNew(cUnit, sizeof(GrowableList),
76 false, kAllocPredecessors);
77 oatInitGrowableList(cUnit, bb->predecessors,
78 (blockType == kExitBlock) ? 2048 : 2,
buzbee5abfa3e2012-01-31 17:01:43 -080079 kListPredecessors);
buzbee67bf8852011-08-17 17:51:35 -070080 return bb;
81}
82
83/* Insert an MIR instruction to the end of a basic block */
84void oatAppendMIR(BasicBlock* bb, MIR* mir)
85{
86 if (bb->firstMIRInsn == NULL) {
buzbeeed3e9302011-09-23 17:34:19 -070087 DCHECK(bb->lastMIRInsn == NULL);
buzbee67bf8852011-08-17 17:51:35 -070088 bb->lastMIRInsn = bb->firstMIRInsn = mir;
89 mir->prev = mir->next = NULL;
90 } else {
91 bb->lastMIRInsn->next = mir;
92 mir->prev = bb->lastMIRInsn;
93 mir->next = NULL;
94 bb->lastMIRInsn = mir;
95 }
96}
97
98/* Insert an MIR instruction to the head of a basic block */
99void oatPrependMIR(BasicBlock* bb, MIR* mir)
100{
101 if (bb->firstMIRInsn == NULL) {
buzbeeed3e9302011-09-23 17:34:19 -0700102 DCHECK(bb->lastMIRInsn == NULL);
buzbee67bf8852011-08-17 17:51:35 -0700103 bb->lastMIRInsn = bb->firstMIRInsn = mir;
104 mir->prev = mir->next = NULL;
105 } else {
106 bb->firstMIRInsn->prev = mir;
107 mir->next = bb->firstMIRInsn;
108 mir->prev = NULL;
109 bb->firstMIRInsn = mir;
110 }
111}
112
buzbeee1965672012-03-11 18:39:19 -0700113/* Insert a MIR instruction after the specified MIR */
buzbee67bf8852011-08-17 17:51:35 -0700114void oatInsertMIRAfter(BasicBlock* bb, MIR* currentMIR, MIR* newMIR)
115{
116 newMIR->prev = currentMIR;
117 newMIR->next = currentMIR->next;
118 currentMIR->next = newMIR;
119
120 if (newMIR->next) {
121 /* Is not the last MIR in the block */
122 newMIR->next->prev = newMIR;
123 } else {
124 /* Is the last MIR in the block */
125 bb->lastMIRInsn = newMIR;
126 }
127}
128
129/*
130 * Append an LIR instruction to the LIR list maintained by a compilation
131 * unit
132 */
133void oatAppendLIR(CompilationUnit *cUnit, LIR* lir)
134{
135 if (cUnit->firstLIRInsn == NULL) {
buzbeeed3e9302011-09-23 17:34:19 -0700136 DCHECK(cUnit->lastLIRInsn == NULL);
buzbee67bf8852011-08-17 17:51:35 -0700137 cUnit->lastLIRInsn = cUnit->firstLIRInsn = lir;
138 lir->prev = lir->next = NULL;
139 } else {
140 cUnit->lastLIRInsn->next = lir;
141 lir->prev = cUnit->lastLIRInsn;
142 lir->next = NULL;
143 cUnit->lastLIRInsn = lir;
144 }
145}
146
147/*
148 * Insert an LIR instruction before the current instruction, which cannot be the
149 * first instruction.
150 *
151 * prevLIR <-> newLIR <-> currentLIR
152 */
153void oatInsertLIRBefore(LIR* currentLIR, LIR* newLIR)
154{
buzbeeed3e9302011-09-23 17:34:19 -0700155 DCHECK(currentLIR->prev != NULL);
buzbee67bf8852011-08-17 17:51:35 -0700156 LIR *prevLIR = currentLIR->prev;
157
158 prevLIR->next = newLIR;
159 newLIR->prev = prevLIR;
160 newLIR->next = currentLIR;
161 currentLIR->prev = newLIR;
162}
163
164/*
165 * Insert an LIR instruction after the current instruction, which cannot be the
166 * first instruction.
167 *
168 * currentLIR -> newLIR -> oldNext
169 */
170void oatInsertLIRAfter(LIR* currentLIR, LIR* newLIR)
171{
172 newLIR->prev = currentLIR;
173 newLIR->next = currentLIR->next;
174 currentLIR->next = newLIR;
175 newLIR->next->prev = newLIR;
176}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800177
178} // namespace art