• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 - 2025 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 "JSemitter.h"
17 
18 #include "compiler/core/pandagen.h"
19 #include "varbinder/varbinder.h"
20 #include "parser/program/program.h"
21 #include "assembly-program.h"
22 #include "public/public.h"
23 
24 namespace ark::es2panda::compiler {
GenFunctionSignature()25 pandasm::Function *JSFunctionEmitter::GenFunctionSignature()
26 {
27 #ifdef PANDA_WITH_ECMASCRIPT
28     auto *func = new pandasm::Function(Cg()->InternalName().Mutf8(), panda_file::SourceLang::ECMASCRIPT);
29     GetProgramElement()->SetFunction(func);
30 
31     size_t paramCount = Cg()->InternalParamCount();
32     func->params.reserve(paramCount);
33 
34     for (uint32_t i = 0; i < paramCount; ++i) {
35         func->params.emplace_back(pandasm::Type("any", 0), panda_file::SourceLang::ECMASCRIPT);
36     }
37 
38     func->regsNum = VReg::REG_START - Cg()->TotalRegsNum();
39     func->returnType = pandasm::Type("any", 0);
40 
41     return func;
42 #else
43     ES2PANDA_UNREACHABLE();
44 #endif
45 }
46 
GenVariableSignature(pandasm::debuginfo::LocalVariable & variableDebug,varbinder::LocalVariable * variable) const47 void JSFunctionEmitter::GenVariableSignature(pandasm::debuginfo::LocalVariable &variableDebug,
48                                              [[maybe_unused]] varbinder::LocalVariable *variable) const
49 {
50     variableDebug.signature = "any";
51     variableDebug.signatureType = "any";
52 }
53 
GenSourceFileDebugInfo(pandasm::Function * func)54 void JSFunctionEmitter::GenSourceFileDebugInfo(pandasm::Function *func)
55 {
56     func->sourceFile = std::string {Cg()->VarBinder()->Program()->RelativeFilePath()};
57 
58     if (!Cg()->IsDebug()) {
59         return;
60     }
61 
62     if (Cg()->RootNode()->IsProgram()) {
63         func->sourceCode = SourceCode().EscapeSymbol<util::StringView::Mutf8Encode>();
64     }
65 }
66 
GenFunctionAnnotations(pandasm::Function * func)67 void JSFunctionEmitter::GenFunctionAnnotations(pandasm::Function *func)
68 {
69     pandasm::AnnotationData funcAnnotationData("_ESAnnotation");
70     pandasm::AnnotationElement icSizeAnnotationElement(
71         "icSize", std::make_unique<pandasm::ScalarValue>(
72                       pandasm::ScalarValue::Create<pandasm::Value::Type::U32>(Pg()->IcSize())));
73     funcAnnotationData.AddElement(std::move(icSizeAnnotationElement));
74 
75     pandasm::AnnotationElement parameterLengthAnnotationElement(
76         "parameterLength", std::make_unique<pandasm::ScalarValue>(
77                                pandasm::ScalarValue::Create<pandasm::Value::Type::U32>(Pg()->FormalParametersCount())));
78     funcAnnotationData.AddElement(std::move(parameterLengthAnnotationElement));
79 
80     pandasm::AnnotationElement funcNameAnnotationElement(
81         "funcName", std::make_unique<pandasm::ScalarValue>(
82                         pandasm::ScalarValue::Create<pandasm::Value::Type::STRING>(Pg()->FunctionName().Mutf8())));
83     funcAnnotationData.AddElement(std::move(funcNameAnnotationElement));
84 
85     func->metadata->AddAnnotations({funcAnnotationData});
86 }
87 
GenAnnotation()88 void JSEmitter::GenAnnotation()
89 {
90 #ifdef PANDA_WITH_ECMASCRIPT
91     Program()->lang = panda_file::SourceLang::ECMASCRIPT;
92     GenESAnnotationRecord();
93     GenESModuleModeRecord(Context()->parserProgram->Kind() == parser::ScriptKind::MODULE);
94 #else
95     ES2PANDA_UNREACHABLE();
96 #endif
97 }
98 
GenESAnnotationRecord()99 void JSEmitter::GenESAnnotationRecord()
100 {
101     auto annotationRecord = pandasm::Record("_ESAnnotation", Program()->lang);
102     annotationRecord.metadata->SetAttribute("external");
103     annotationRecord.metadata->SetAccessFlags(ACC_ANNOTATION);
104     Program()->recordTable.emplace(annotationRecord.name, std::move(annotationRecord));
105 }
106 
GenESModuleModeRecord(bool isModule)107 void JSEmitter::GenESModuleModeRecord(bool isModule)
108 {
109     auto modeRecord = pandasm::Record("_ESModuleMode", Program()->lang);
110     modeRecord.metadata->SetAccessFlags(ACC_PUBLIC);
111 
112     auto modeField = pandasm::Field(Program()->lang);
113     modeField.name = "isModule";
114     modeField.type = pandasm::Type("u8", 0);
115     modeField.metadata->SetValue(
116         pandasm::ScalarValue::Create<pandasm::Value::Type::U8>(static_cast<uint8_t>(isModule)));
117 
118     modeRecord.fieldList.emplace_back(std::move(modeField));
119 
120     Program()->recordTable.emplace(modeRecord.name, std::move(modeRecord));
121 }
122 }  // namespace ark::es2panda::compiler
123