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 "ecmascript/compiler/codegen/maple/litecg_codegen.h"
17 #if defined(PANDA_TARGET_MACOS) || defined(PANDA_TARGET_IOS)
18 #include "ecmascript/base/llvm_helper.h"
19 #endif
20
21 #include <cstring>
22 #include <iomanip>
23 #include <vector>
24
25 #include "ecmascript/compiler/call_signature.h"
26 #include "ecmascript/compiler/compiler_log.h"
27 #include "ecmascript/compiler/debug_info.h"
28 #include "ecmascript/compiler/litecg_ir_builder.h"
29 #include "ecmascript/ecma_macros.h"
30 #include "ecmascript/mem/region.h"
31 #include "ecmascript/object_factory.h"
32 #include "ecmascript/stackmap/litecg_stackmap_type.h"
33 #include "ecmascript/stackmap/llvm_stackmap_parser.h"
34 #include "ecmascript/compiler/codegen/maple/maple_be/include/litecg/lmir_builder.h"
35 #include "ecmascript/compiler/codegen/maple/maple_be/include/litecg/litecg.h"
36
37 namespace panda::ecmascript::kungfu {
38 class CompilerLog;
39
40 using namespace panda::ecmascript;
41
LiteCGAssembler(LMIRModule & module)42 LiteCGAssembler::LiteCGAssembler(LMIRModule &module) : lmirModule(module) {}
43
AllocateCodeSection(void * object,uint32_t size,uint32_t alignment,const std::string & sectionName)44 static uint8_t *AllocateCodeSection(void *object, uint32_t size, [[maybe_unused]] uint32_t alignment,
45 const std::string §ionName)
46 {
47 struct CodeInfo &state = *static_cast<struct CodeInfo *>(object);
48 return state.AllocaCodeSection(size, sectionName.c_str());
49 }
50
SaveFunc2Addr(void * object,std::string funcName,uint32_t address)51 static void SaveFunc2Addr(void *object, std::string funcName, uint32_t address)
52 {
53 struct CodeInfo &state = *static_cast<struct CodeInfo *>(object);
54 state.SaveFunc2Addr(funcName, address);
55 }
56
SaveFunc2FPtoPrevSPDelta(void * object,std::string funcName,int32_t fp2PrevSpDelta)57 static void SaveFunc2FPtoPrevSPDelta(void *object, std::string funcName, int32_t fp2PrevSpDelta)
58 {
59 struct CodeInfo &state = *static_cast<struct CodeInfo *>(object);
60 state.SaveFunc2FPtoPrevSPDelta(funcName, fp2PrevSpDelta);
61 }
62
SaveFunc2CalleeOffsetInfo(void * object,std::string funcName,kungfu::CalleeRegAndOffsetVec calleeRegInfo)63 static void SaveFunc2CalleeOffsetInfo(void *object, std::string funcName, kungfu::CalleeRegAndOffsetVec calleeRegInfo)
64 {
65 struct CodeInfo &state = *static_cast<struct CodeInfo *>(object);
66 state.SaveFunc2CalleeOffsetInfo(funcName, calleeRegInfo);
67 }
68
SavePC2DeoptInfo(void * object,uint64_t pc,std::vector<uint64_t> deoptInfo)69 static void SavePC2DeoptInfo(void *object, uint64_t pc, std::vector<uint64_t> deoptInfo)
70 {
71 struct CodeInfo &state = *static_cast<struct CodeInfo *>(object);
72 state.SavePC2DeoptInfo(pc, deoptInfo);
73 }
74
SavePC2CallSiteInfo(void * object,uint64_t pc,std::vector<uint64_t> callSiteInfo)75 void SavePC2CallSiteInfo(void *object, uint64_t pc, std::vector<uint64_t> callSiteInfo)
76 {
77 struct CodeInfo &state = *static_cast<struct CodeInfo *>(object);
78 state.SavePC2CallSiteInfo(pc, callSiteInfo);
79 }
80
Run(const CompilerLog & log,bool fastCompileMode)81 void LiteCGAssembler::Run(const CompilerLog &log, [[maybe_unused]] bool fastCompileMode)
82 {
83 maple::litecg::LiteCG liteCG(*lmirModule.GetModule());
84 if (log.OutputLLIR()) {
85 std::string irFileName = lmirModule.GetModule()->GetFileName() + ".mpl";
86 liteCG.DumpIRToFile(irFileName);
87 }
88 liteCG.SetupLiteCGEmitMemoryManager(&codeInfo_, AllocateCodeSection, SaveFunc2Addr, SaveFunc2FPtoPrevSPDelta,
89 SaveFunc2CalleeOffsetInfo, SavePC2DeoptInfo, SavePC2CallSiteInfo);
90 liteCG.DoCG();
91 }
92
GenerateCodeForStub(Circuit * circuit,const ControlFlowGraph & graph,size_t index,const CompilationConfig * cfg)93 void LiteCGIRGeneratorImpl::GenerateCodeForStub(Circuit *circuit, const ControlFlowGraph &graph, size_t index,
94 const CompilationConfig *cfg)
95 {
96 (void)circuit;
97 (void)graph;
98 (void)index;
99 (void)cfg;
100 }
101
GenerateCode(Circuit * circuit,const ControlFlowGraph & graph,const CompilationConfig * cfg,const panda::ecmascript::MethodLiteral * methodLiteral,const JSPandaFile * jsPandaFile,const std::string & methodName,bool enableOptInlining,bool enableBranchProfiling)102 void LiteCGIRGeneratorImpl::GenerateCode(Circuit *circuit, const ControlFlowGraph &graph, const CompilationConfig *cfg,
103 const panda::ecmascript::MethodLiteral *methodLiteral,
104 const JSPandaFile *jsPandaFile, const std::string &methodName,
105 bool enableOptInlining, [[maybe_unused]]bool enableBranchProfiling)
106 {
107 circuit->SetFrameType(FrameType::OPTIMIZED_JS_FUNCTION_FRAME);
108 CallSignature::CallConv conv;
109 if (methodLiteral->IsFastCall()) {
110 conv = CallSignature::CallConv::CCallConv;
111 } else {
112 conv = CallSignature::CallConv::WebKitJSCallConv;
113 }
114 LiteCGIRBuilder builder(&graph, circuit, module_, cfg, conv, enableLog_, enableOptInlining, methodLiteral,
115 jsPandaFile, methodName);
116 builder.Build();
117 }
118
CollectAnStackMap(CGStackMapInfo & stackMapInfo)119 void LiteCGAssembler::CollectAnStackMap(CGStackMapInfo &stackMapInfo)
120 {
121 auto &liteCGStackMapInfo = static_cast<LiteCGStackMapInfo&>(stackMapInfo);
122 const auto &codeInfo = GetCodeInfo();
123 liteCGStackMapInfo.AppendCallSiteInfo(codeInfo.GetPC2CallsiteInfo());
124 liteCGStackMapInfo.AppendDeoptInfo(codeInfo.GetPC2DeoptInfo());
125 }
126 } // namespace panda::ecmascript::kungfu
127