• 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 #include "label_creation.h"
17 #include "cg.h"
18 
19 namespace maplebe {
20 using namespace maple;
21 
Run() const22 void LabelCreation::Run() const
23 {
24     CreateStartEndLabel();
25 }
26 
CreateStartEndLabel() const27 void LabelCreation::CreateStartEndLabel() const
28 {
29     DEBUG_ASSERT(cgFunc != nullptr, "expect a cgfunc before CreateStartEndLabel");
30     MIRBuilder *mirBuilder = cgFunc->GetFunction().GetModule()->GetMIRBuilder();
31     DEBUG_ASSERT(mirBuilder != nullptr, "get mirbuilder failed in CreateStartEndLabel");
32     /* create start label */
33     LabelIdx startLblIdx = cgFunc->CreateLabel();
34     LabelNode *startLabel = mirBuilder->CreateStmtLabel(startLblIdx);
35     cgFunc->GetFunction().GetBody()->InsertFirst(startLabel);
36     /* creat return label */
37     LabelIdx returnLblIdx = cgFunc->CreateLabel();
38     LabelNode *returnLabel = mirBuilder->CreateStmtLabel(returnLblIdx);
39     cgFunc->GetFunction().GetBody()->InsertLast(returnLabel);
40 
41     /* create end label */
42     LabelIdx endLblIdx = cgFunc->CreateLabel();
43     LabelNode *endLabel = mirBuilder->CreateStmtLabel(endLblIdx);
44     cgFunc->SetEndLabel(*endLabel);
45     cgFunc->GetFunction().GetBody()->InsertLast(endLabel);
46     DEBUG_ASSERT(cgFunc->GetFunction().GetBody()->GetLast() == endLabel, "last stmt must be a endLabel");
47 
48     /* create function's low/high pc if dwarf enabled */
49     MIRFunction *func = &cgFunc->GetFunction();
50     /* add start/end labels into the static map table in class cg */
51     if (!CG::IsInFuncWrapLabels(func)) {
52         CG::SetFuncWrapLabels(func, std::make_pair(startLblIdx, endLblIdx));
53     }
54 }
55 
PhaseRun(maplebe::CGFunc & f)56 bool CgCreateLabel::PhaseRun(maplebe::CGFunc &f)
57 {
58     MemPool *memPool = GetPhaseMemPool();
59     LabelCreation *labelCreate = memPool->New<LabelCreation>(f);
60     labelCreate->Run();
61     return false;
62 }
63 } /* namespace maplebe */
64