• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/constpool_value.h"
21 #include "ecmascript/jspandafile/js_pandafile.h"
22 
23 #include "libpandabase/utils/bit_field.h"
24 #include "libpandafile/code_data_accessor-inl.h"
25 #include "libpandafile/file-inl.h"
26 
27 #include "ecmascript/jspandafile/bytecode_inst/old_instruction.h"
28 
29 namespace panda::ecmascript {
30 class Program;
31 
32 class PUBLIC_API PandaFileTranslator {
33 public:
34     enum FixInstructionIndex : uint8_t { FIX_ONE = 1, FIX_TWO = 2, FIX_FOUR = 4 };
35 
36     PandaFileTranslator() = default;
37     ~PandaFileTranslator() = default;
38     NO_COPY_SEMANTIC(PandaFileTranslator);
39     NO_MOVE_SEMANTIC(PandaFileTranslator);
40     static JSHandle<Program> GenerateProgram(EcmaVM *vm, const JSPandaFile *jsPandaFile,
41                                              std::string_view entryPoint);
42     static void TranslateClasses(JSPandaFile *jsPandaFile, const CString &methodName);
43 
44 private:
45     static JSHandle<Program> GenerateProgramInternal(EcmaVM *vm, const JSPandaFile *jsPandaFile,
46                                                      uint32_t mainMethodIndex, JSHandle<ConstantPool> constpool);
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 JSHandle<ConstantPool> ParseConstPool(EcmaVM *vm, const JSPandaFile *jsPandaFile);
53     static void ParseFuncAndLiteralConstPool(EcmaVM *vm, const JSPandaFile *jsPandaFile, const CString &entryPoint,
54                                              JSHandle<ConstantPool> constpool);
55     static JSHandle<ConstantPool> AllocateConstPool(EcmaVM *vm, const JSPandaFile *jsPandaFile);
56     friend class QuickFixLoader;
57 };
58 }  // namespace panda::ecmascript
59 #endif  // ECMASCRIPT_JSPANDAFILE_PANDA_FILE_TRANSLATOR_H
60