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 "litecg.h"
17 #include "mir_builder.h"
18 #include "cg_option.h"
19 #include "mad.h"
20 #include "cg.h"
21 #include "maple_phase_support.h"
22 #include "maple_phase.h"
23 #include "cg_phasemanager.h"
24 #include "triple.h"
25 #include <string>
26
27 namespace maple {
28
29 namespace litecg {
30
31 using namespace maplebe;
32
LiteCG(Module & mirModule)33 LiteCG::LiteCG(Module &mirModule) : module(mirModule)
34 {
35 // Create CGOption: set up default options
36 // should we make CGOptions local?
37 cgOptions = &CGOptions::GetInstance();
38 cgOptions->EnableLiteCG();
39 cgOptions->SetEmitFileType("obj");
40 cgOptions->SetQuiet(true);
41 #if TARGAARCH64
42 Triple::GetTriple().Init();
43 #endif
44 // module information prepare
45 std::string moduleName = module.GetFileName();
46 GStrIdx fileNameStrIdx = GlobalTables::GetStrTable().GetOrCreateStrIdxFromName(moduleName);
47
48 // is this strictly required?
49 GStrIdx nameStrIdx = GlobalTables::GetStrTable().GetOrCreateStrIdxFromName("INFO_filename");
50 module.PushFileInfoPair(MIRInfoPair(nameStrIdx, fileNameStrIdx.GetIdx()));
51 module.PushFileInfoIsString(true);
52
53 module.SetFlavor(kFlavorUnknown); // need a new flavor
54 module.GetImportFiles().clear();
55
56 // Setup output file name
57 module.SetOutputFileName(moduleName + ".s");
58 }
59
SetOutputType(OutputType config)60 LiteCG &LiteCG::SetOutputType(OutputType config)
61 {
62 cgOptions->SetEmitFileType((config == kAsm) ? "asm" : "obj");
63 return *this;
64 }
65
SetTargetType(TargetType config)66 LiteCG &LiteCG::SetTargetType(TargetType config)
67 {
68 // update target support
69 // cgOptions->SetTarget(X86_64)
70 return *this;
71 }
72
SetDebugType(DebugType config)73 LiteCG &LiteCG::SetDebugType(DebugType config)
74 {
75 // fix the exposed debug options
76 // cgOptions->SetDebug(?)
77 return *this;
78 }
79
SetVerbose(InfoType config)80 LiteCG &LiteCG::SetVerbose(InfoType config)
81 {
82 cgOptions->SetQuiet((config == kQuiet) ? true : false);
83 return *this;
84 }
85
DumpIRToFile(const std::string & fileName)86 void LiteCG::DumpIRToFile(const std::string &fileName)
87 {
88 module.DumpToFile(fileName);
89 }
90
DumpCGIR()91 void LiteCG::DumpCGIR()
92 {
93 cgOptions->GetDumpPhases().insert("*");
94 cgOptions->FuncFilter("*");
95 }
96
SetupLiteCGEmitMemoryManager(void * codeSpace,MemoryManagerAllocateDataSectionCallback dataSectionAllocator,MemoryManagerSaveFunc2AddressInfoCallback funcAddressSaver,maplebe::MemoryManagerSaveFunc2FPtoPrevSPDeltaCallback funcFpSPDeltaSaver,maplebe::MemoryManagerSaveFunc2CalleeOffsetInfoCallback funcCallOffsetSaver,maplebe::MemoryManagerSavePC2DeoptInfoCallback pc2DeoptInfoSaver,maplebe::MemoryManagerSavePC2CallSiteInfoCallback pc2CallSiteInfoSaver)97 LiteCG &LiteCG::SetupLiteCGEmitMemoryManager(
98 void *codeSpace, MemoryManagerAllocateDataSectionCallback dataSectionAllocator,
99 MemoryManagerSaveFunc2AddressInfoCallback funcAddressSaver,
100 maplebe::MemoryManagerSaveFunc2FPtoPrevSPDeltaCallback funcFpSPDeltaSaver,
101 maplebe::MemoryManagerSaveFunc2CalleeOffsetInfoCallback funcCallOffsetSaver,
102 maplebe::MemoryManagerSavePC2DeoptInfoCallback pc2DeoptInfoSaver,
103 maplebe::MemoryManagerSavePC2CallSiteInfoCallback pc2CallSiteInfoSaver)
104 {
105 cgOptions->SetupEmitMemoryManager(codeSpace, dataSectionAllocator, funcAddressSaver, funcFpSPDeltaSaver,
106 funcCallOffsetSaver, pc2DeoptInfoSaver, pc2CallSiteInfoSaver);
107 return *this;
108 }
109
DoCG()110 void LiteCG::DoCG()
111 {
112 bool timePhases = false;
113 MPLTimer timer;
114 if (timePhases) {
115 timer.Start();
116 }
117
118 Globals::GetInstance()->SetOptimLevel(cgOptions->GetOptimizeLevel());
119
120 // not sure how to do this.
121 auto cgPhaseManager = std::make_unique<ThreadLocalMemPool>(memPoolCtrler, "cg function phasemanager");
122 const MaplePhaseInfo *cgPMInfo = MaplePhaseRegister::GetMaplePhaseRegister()->GetPhaseByID(&CgFuncPM::id);
123 auto *cgfuncPhaseManager = static_cast<CgFuncPM *>(cgPMInfo->GetConstructor()(cgPhaseManager.get()));
124 cgfuncPhaseManager->SetQuiet(CGOptions::IsQuiet());
125
126 if (timePhases) {
127 cgfuncPhaseManager->InitTimeHandler();
128 }
129
130 /* It is a specifc work around (need refactor) */
131 cgfuncPhaseManager->SetCGOptions(cgOptions);
132 (void)cgfuncPhaseManager->PhaseRun(module);
133
134 if (timePhases) {
135 cgfuncPhaseManager->DumpPhaseTime();
136 timer.Stop();
137 LogInfo::MapleLogger() << "Mplcg consumed " << timer.ElapsedMilliseconds() << "ms" << '\n';
138 }
139 }
140
141 } // namespace litecg
142
143 } // namespace maple
144