1 /*
2  * Copyright (c) 2021-2022 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 #ifndef ECMASCRIPT_JSPANDAFILE_PANDA_FILE_TRANSLATOR_H
17 #define ECMASCRIPT_JSPANDAFILE_PANDA_FILE_TRANSLATOR_H
18 
19 #include "ecmascript/ecma_vm.h"
20 #include "ecmascript/jspandafile/bytecode_inst/old_instruction.h"
21 #include "ecmascript/jspandafile/constpool_value.h"
22 #include "ecmascript/jspandafile/js_pandafile.h"
23 
24 #include "libpandabase/utils/bit_field.h"
25 #include "libpandafile/code_data_accessor-inl.h"
26 
27 namespace panda::ecmascript {
28 class Program;
29 
30 class PUBLIC_API PandaFileTranslator {
31 public:
32     enum FixInstructionIndex : uint8_t { FIX_ONE = 1, FIX_TWO = 2, FIX_FOUR = 4 };
33 
34     PandaFileTranslator() = default;
35     ~PandaFileTranslator() = default;
36     NO_COPY_SEMANTIC(PandaFileTranslator);
37     NO_MOVE_SEMANTIC(PandaFileTranslator);
38     static JSHandle<Program> GenerateProgram(EcmaVM *vm, const JSPandaFile *jsPandaFile,
39                                              std::string_view entryPoint);
40     static void TranslateClasses(const JSThread *thread, JSPandaFile *jsPandaFile, const CString &methodName);
41     static void TranslateClass(const JSThread *thread, JSPandaFile *jsPandaFile, const CString &methodName,
42         size_t methodIdx, size_t classIdx);
43 
44 private:
45     static JSHandle<Program> GenerateProgramInternal(EcmaVM *vm, MethodLiteral *mainMethodLiteral,
46                                                      JSHandle<ConstantPool> constpool, const JSPandaFile *jsPandaFile);
47     static void TranslateBytecode(JSPandaFile *jsPandaFile, uint32_t insSz, const uint8_t *insArr,
48         const MethodLiteral *methodLiteral, const CString &methodName = JSPandaFile::ENTRY_FUNCTION_NAME);
49     static void FixInstructionId32(const OldBytecodeInst &inst, uint32_t index, uint32_t fixOrder = 0);
50     static void FixOpcode(MethodLiteral *method, const OldBytecodeInst &inst);
51     static void UpdateICOffset(MethodLiteral *method, uint8_t *pc);
52     static std::pair<JSHandle<ConstantPool>, JSHandle<ConstantPool>> ParseConstPool(
53         EcmaVM *vm, const JSPandaFile *jsPandaFile);
54     static void ParseFuncAndLiteralConstPool(EcmaVM *vm, const JSPandaFile *jsPandaFile, const CString &entryPoint,
55         JSHandle<ConstantPool> sconstpool, JSHandle<ConstantPool> constpool);
56     static JSHandle<ConstantPool> AllocateConstPool(EcmaVM *vm, const JSPandaFile *jsPandaFile);
57     static JSHandle<ConstantPool> AllocateSharedConstPool(EcmaVM *vm, const JSPandaFile *jsPandaFile);
58 
59     friend class PatchLoader;
60 };
61 }  // namespace panda::ecmascript
62 #endif  // ECMASCRIPT_JSPANDAFILE_PANDA_FILE_TRANSLATOR_H
63