blob: 29889005baa24aa0967666742b67369246d780f8 [file] [log] [blame]
Ben Chengba4fc8b2009-06-01 13:00:29 -07001/*
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#include "Dalvik.h"
18#include "CompilerInternals.h"
19
20/* Allocate a new basic block */
21BasicBlock *dvmCompilerNewBB(BBType blockType)
22{
Carl Shapirofc75f3e2010-12-07 11:43:38 -080023 BasicBlock *bb = (BasicBlock *)dvmCompilerNew(sizeof(BasicBlock), true);
Ben Chengba4fc8b2009-06-01 13:00:29 -070024 bb->blockType = blockType;
25 return bb;
26}
27
28/* Insert an MIR instruction to the end of a basic block */
29void dvmCompilerAppendMIR(BasicBlock *bb, MIR *mir)
30{
31 if (bb->firstMIRInsn == NULL) {
Ben Cheng4238ec22009-08-24 16:32:22 -070032 assert(bb->lastMIRInsn == NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -070033 bb->lastMIRInsn = bb->firstMIRInsn = mir;
34 mir->prev = mir->next = NULL;
35 } else {
36 bb->lastMIRInsn->next = mir;
37 mir->prev = bb->lastMIRInsn;
38 mir->next = NULL;
39 bb->lastMIRInsn = mir;
40 }
41}
42
Ben Cheng4238ec22009-08-24 16:32:22 -070043/* Insert an MIR instruction to the head of a basic block */
44void dvmCompilerPrependMIR(BasicBlock *bb, MIR *mir)
45{
46 if (bb->firstMIRInsn == NULL) {
47 assert(bb->lastMIRInsn == NULL);
48 bb->lastMIRInsn = bb->firstMIRInsn = mir;
49 mir->prev = mir->next = NULL;
50 } else {
51 bb->firstMIRInsn->prev = mir;
52 mir->next = bb->firstMIRInsn;
53 mir->prev = NULL;
54 bb->firstMIRInsn = mir;
55 }
56}
57
Ben Cheng7a2697d2010-06-07 13:44:23 -070058/* Insert an MIR instruction after the specified MIR */
59void dvmCompilerInsertMIRAfter(BasicBlock *bb, MIR *currentMIR, MIR *newMIR)
60{
61 newMIR->prev = currentMIR;
62 newMIR->next = currentMIR->next;
63 currentMIR->next = newMIR;
64
65 if (newMIR->next) {
66 /* Is not the last MIR in the block */
67 newMIR->next->prev = newMIR;
68 } else {
69 /* Is the last MIR in the block */
70 bb->lastMIRInsn = newMIR;
71 }
72}
73
Ben Chengba4fc8b2009-06-01 13:00:29 -070074/*
75 * Append an LIR instruction to the LIR list maintained by a compilation
76 * unit
77 */
78void dvmCompilerAppendLIR(CompilationUnit *cUnit, LIR *lir)
79{
80 if (cUnit->firstLIRInsn == NULL) {
81 assert(cUnit->lastLIRInsn == NULL);
82 cUnit->lastLIRInsn = cUnit->firstLIRInsn = lir;
83 lir->prev = lir->next = NULL;
84 } else {
85 cUnit->lastLIRInsn->next = lir;
86 lir->prev = cUnit->lastLIRInsn;
87 lir->next = NULL;
88 cUnit->lastLIRInsn = lir;
89 }
90}
Ben Chenge9695e52009-06-16 16:11:47 -070091
92/*
93 * Insert an LIR instruction before the current instruction, which cannot be the
94 * first instruction.
95 *
96 * prevLIR <-> newLIR <-> currentLIR
97 */
98void dvmCompilerInsertLIRBefore(LIR *currentLIR, LIR *newLIR)
99{
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800100 assert(currentLIR->prev != NULL);
Ben Chenge9695e52009-06-16 16:11:47 -0700101 LIR *prevLIR = currentLIR->prev;
102
103 prevLIR->next = newLIR;
104 newLIR->prev = prevLIR;
105 newLIR->next = currentLIR;
106 currentLIR->prev = newLIR;
107}
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700108
109/*
110 * Insert an LIR instruction after the current instruction, which cannot be the
111 * first instruction.
112 *
113 * currentLIR -> newLIR -> oldNext
114 */
115void dvmCompilerInsertLIRAfter(LIR *currentLIR, LIR *newLIR)
116{
117 newLIR->prev = currentLIR;
118 newLIR->next = currentLIR->next;
119 currentLIR->next = newLIR;
120 newLIR->next->prev = newLIR;
121}