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(JSPandaFile *jsPandaFile, const CString &methodName); 41 42 private: 43 static JSHandle<Program> GenerateProgramInternal(EcmaVM *vm, MethodLiteral *mainMethodLiteral, 44 JSHandle<ConstantPool> constpool); 45 static void TranslateBytecode(JSPandaFile *jsPandaFile, uint32_t insSz, const uint8_t *insArr, 46 const MethodLiteral *methodLiteral, const CString &methodName = JSPandaFile::ENTRY_FUNCTION_NAME); 47 static void FixInstructionId32(const OldBytecodeInst &inst, uint32_t index, uint32_t fixOrder = 0); 48 static void FixOpcode(MethodLiteral *method, const OldBytecodeInst &inst); 49 static void UpdateICOffset(MethodLiteral *method, uint8_t *pc); 50 static JSHandle<ConstantPool> ParseConstPool(EcmaVM *vm, const JSPandaFile *jsPandaFile); 51 static void ParseFuncAndLiteralConstPool(EcmaVM *vm, const JSPandaFile *jsPandaFile, const CString &entryPoint, 52 JSHandle<ConstantPool> constpool); 53 static JSHandle<ConstantPool> AllocateConstPool(EcmaVM *vm, const JSPandaFile *jsPandaFile); 54 55 friend class PatchLoader; 56 }; 57 } // namespace panda::ecmascript 58 #endif // ECMASCRIPT_JSPANDAFILE_PANDA_FILE_TRANSLATOR_H 59