1 /* 2 * Copyright (c) 2021 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_CLASS_LINKER_PANDA_FILE_TRANSLATOR_H 17 #define ECMASCRIPT_CLASS_LINKER_PANDA_FILE_TRANSLATOR_H 18 19 #include "ecmascript/ecma_vm.h" 20 #include "ecmascript/js_function.h" 21 #include "ecmascript/jspandafile/constpool_value.h" 22 #include "libpandafile/bytecode_instruction.h" 23 #include "libpandafile/code_data_accessor-inl.h" 24 #include "libpandafile/file-inl.h" 25 #include "utils/bit_field.h" 26 27 namespace panda::ecmascript { 28 struct BytecodeTranslationInfo { 29 std::vector<uint8_t *> pcArray {}; 30 const panda_file::File *file {nullptr}; 31 const JSMethod *method {nullptr}; 32 }; 33 34 class JSThread; 35 class Program; 36 class JSPandaFileManager; 37 class JSPandaFile; 38 39 class PandaFileTranslator { 40 public: 41 enum FixInstructionIndex : uint8_t { FIX_ONE = 1, FIX_TWO = 2, FIX_FOUR = 4 }; 42 43 explicit PandaFileTranslator(const JSPandaFile *jsPandaFile); 44 PandaFileTranslator(EcmaVM *vm, const JSPandaFile *jsPandaFile); 45 ~PandaFileTranslator() = default; 46 NO_COPY_SEMANTIC(PandaFileTranslator); 47 NO_MOVE_SEMANTIC(PandaFileTranslator); 48 void TranslateAndCollectPandaFile(const CString &methodName, std::vector<BytecodeTranslationInfo> *infoList); 49 JSHandle<JSFunction> DefineMethodInLiteral(JSThread *thread, uint32_t methodId, FunctionKind kind, 50 uint16_t length) const; 51 Program *GenerateProgram(); 52 static void TranslateClasses(JSPandaFile *jsPandaFile, const CString &methodName, 53 std::vector<BytecodeTranslationInfo> *infoList = nullptr); 54 55 private: 56 static void TranslateBytecode(JSPandaFile *jsPandaFile, uint32_t insSz, const uint8_t *insArr, 57 const JSMethod *method, std::vector<BytecodeTranslationInfo> *infoList); 58 static void FixInstructionId32(const BytecodeInstruction &inst, uint32_t index, uint32_t fixOrder = 0); 59 static void FixOpcode(uint8_t *pc); 60 static void UpdateICOffset(JSMethod *method, uint8_t *pc); 61 void DefineClassInConstPool(const JSHandle<ConstantPool> &constpool) const; 62 63 EcmaVM *ecmaVm_; 64 ObjectFactory *factory_; 65 JSThread *thread_; 66 const JSPandaFile *jsPandaFile_; 67 }; 68 } // namespace panda::ecmascript 69 #endif // ECMASCRIPT_CLASS_LINKER_PANDA_FILE_TRANSLATOR_H 70