• 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 "cgfunc.h"
18 #include "cg.h"
19 #include "debug_info.h"
20 
21 namespace maplebe {
22 using namespace maple;
23 
Run() const24 void LabelCreation::Run() const
25 {
26     CreateStartEndLabel();
27 }
28 
CreateStartEndLabel() const29 void LabelCreation::CreateStartEndLabel() const
30 {
31     DEBUG_ASSERT(cgFunc != nullptr, "expect a cgfunc before CreateStartEndLabel");
32     MIRBuilder *mirBuilder = cgFunc->GetFunction().GetModule()->GetMIRBuilder();
33     DEBUG_ASSERT(mirBuilder != nullptr, "get mirbuilder failed in CreateStartEndLabel");
34     /* create start label */
35     LabelIdx startLblIdx = cgFunc->CreateLabel();
36     LabelNode *startLabel = mirBuilder->CreateStmtLabel(startLblIdx);
37     cgFunc->SetStartLabel(*startLabel);
38     cgFunc->GetFunction().GetBody()->InsertFirst(startLabel);
39     /* creat return label */
40     LabelIdx returnLblIdx = cgFunc->CreateLabel();
41     LabelNode *returnLabel = mirBuilder->CreateStmtLabel(returnLblIdx);
42     cgFunc->SetReturnLabel(*returnLabel);
43     cgFunc->GetFunction().GetBody()->InsertLast(returnLabel);
44 
45     /* create end label */
46     LabelIdx endLblIdx = cgFunc->CreateLabel();
47     LabelNode *endLabel = mirBuilder->CreateStmtLabel(endLblIdx);
48     cgFunc->SetEndLabel(*endLabel);
49     cgFunc->GetFunction().GetBody()->InsertLast(endLabel);
50     DEBUG_ASSERT(cgFunc->GetFunction().GetBody()->GetLast() == endLabel, "last stmt must be a endLabel");
51 
52     /* create function's low/high pc if dwarf enabled */
53     MIRFunction *func = &cgFunc->GetFunction();
54     CG *cg = cgFunc->GetCG();
55     if (cg->GetCGOptions().WithDwarf() && cgFunc->GetWithSrc()) {
56         DebugInfo *di = cg->GetMIRModule()->GetDbgInfo();
57         DBGDie *fdie = di->GetFuncDie(*func);
58         DEBUG_ASSERT(fdie != nullptr, "nullptr check");
59         fdie->SetAttr(DW_AT_low_pc, startLblIdx);
60         fdie->SetAttr(DW_AT_high_pc, endLblIdx);
61     }
62     /* add start/end labels into the static map table in class cg */
63     if (!CG::IsInFuncWrapLabels(func)) {
64         CG::SetFuncWrapLabels(func, std::make_pair(startLblIdx, endLblIdx));
65     }
66 }
67 
PhaseRun(maplebe::CGFunc & f)68 bool CgCreateLabel::PhaseRun(maplebe::CGFunc &f)
69 {
70     MemPool *memPool = GetPhaseMemPool();
71     LabelCreation *labelCreate = memPool->New<LabelCreation>(f);
72     labelCreate->Run();
73     return false;
74 }
75 } /* namespace maplebe */
76