• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef MAPLEBE_INCLUDE_BE_BBT_H
17 #define MAPLEBE_INCLUDE_BE_BBT_H
18 /* MapleIR headers. */
19 #include "mir_nodes.h"
20 #include "mir_lower.h"
21 namespace maplebe {
22 using namespace maple;
23 
24 class BBT {
25     /*
26      * if stmt is a switch/rangegoto, succs gets defined, and condJumpBranch == fallthruBranch == nullptr.
27      * otherwise, succs.size() ==0 &&
28      *  1. for cond br stmt, both condJumpBranch and fallthruBranch are defined.
29      *  2. if bb ends with 'throw', both fields get nullptr.
30      *  3. for the others, condJumpBranch == nullptr && only fallthruBranch is defined
31      */
32 public:
33     enum BBTType : uint8 { kBBPlain, kBBTry, kBBEndTry, kBBCatch };
34 
BBT(StmtNode * s,StmtNode * e,MemPool * memPool)35     BBT(StmtNode *s, StmtNode *e, MemPool *memPool)
36         : alloc(memPool),
37           type(kBBPlain),
38           succs(alloc.Adapter()),
39           labelIdx(MIRLabelTable::GetDummyLabel()),
40           firstStmt(s != nullptr ? s : e),
41           lastStmt(e)
42     {
43     }
44 
45     ~BBT() = default;
46 
Extend(const StmtNode * sNode,StmtNode * eNode)47     void Extend(const StmtNode *sNode, StmtNode *eNode)
48     {
49         CHECK_FATAL(lastStmt != nullptr, "nullptr check");
50         CHECK_FATAL(sNode != nullptr ? lastStmt->GetNext() == sNode : lastStmt->GetNext() == eNode, "Extend fail");
51         lastStmt = eNode;
52     }
53 
SetLabelIdx(LabelIdx li)54     void SetLabelIdx(LabelIdx li)
55     {
56         labelIdx = li;
57     }
58 
IsLabeled()59     bool IsLabeled() const
60     {
61         return labelIdx != MIRLabelTable::GetDummyLabel();
62     }
63 
GetLabelIdx()64     LabelIdx GetLabelIdx() const
65     {
66         return labelIdx;
67     }
68 
SetType(BBTType t,StmtNode & k)69     void SetType(BBTType t, StmtNode &k)
70     {
71         type = t;
72         keyStmt = &k;
73     }
74 
IsTry()75     bool IsTry() const
76     {
77         return type == kBBTry;
78     }
79 
IsEndTry()80     bool IsEndTry() const
81     {
82         return type == kBBEndTry;
83     }
84 
IsCatch()85     bool IsCatch() const
86     {
87         return type == kBBCatch;
88     }
89 
AddSuccs(BBT * bb)90     void AddSuccs(BBT *bb)
91     {
92         succs.emplace_back(bb);
93     }
94 
SetCondJumpBranch(BBT * bb)95     void SetCondJumpBranch(BBT *bb)
96     {
97         condJumpBranch = bb;
98     }
99 
GetCondJumpBranch()100     BBT *GetCondJumpBranch()
101     {
102         return condJumpBranch;
103     }
SetFallthruBranch(BBT * bb)104     void SetFallthruBranch(BBT *bb)
105     {
106         fallthruBranch = bb;
107     }
108 
GetFallthruBranch()109     BBT *GetFallthruBranch()
110     {
111         return fallthruBranch;
112     }
113 
GetFirstStmt()114     StmtNode *GetFirstStmt()
115     {
116         return firstStmt;
117     }
118 
SetFirstStmt(StmtNode & stmt)119     void SetFirstStmt(StmtNode &stmt)
120     {
121         firstStmt = &stmt;
122     }
123 
GetLastStmt()124     StmtNode *GetLastStmt()
125     {
126         return lastStmt;
127     }
128 
SetLastStmt(StmtNode & stmt)129     void SetLastStmt(StmtNode &stmt)
130     {
131         lastStmt = &stmt;
132     }
133 
GetKeyStmt()134     StmtNode *GetKeyStmt()
135     {
136         return keyStmt;
137     }
138 
139 #if DEBUG
140     void Dump(const MIRModule &mod) const;
141     static void ValidateStmtList(StmtNode *head, StmtNode *detached = nullptr);
142 #endif
143 private:
144     MapleAllocator alloc;
145     BBTType type;
146     BBT *condJumpBranch = nullptr;
147     BBT *fallthruBranch = nullptr;
148     MapleVector<BBT *> succs;
149     LabelIdx labelIdx;
150     StmtNode *firstStmt;
151     StmtNode *lastStmt;
152     StmtNode *keyStmt = nullptr;
153 };
154 } /* namespace maplebe */
155 
156 #endif /* MAPLEBE_INCLUDE_BE_BBT_H */