1 /* 2 * Copyright (c) 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_COMPILER_ASSEMBLER_MODULE_H 17 #define ECMASCRIPT_COMPILER_ASSEMBLER_MODULE_H 18 19 #include <string> 20 #include <vector> 21 22 #include "ecmascript/compiler/assembler/assembler.h" 23 #include "ecmascript/compiler/call_signature.h" 24 #include "ecmascript/compiler/rt_call_signature.h" 25 #include "ecmascript/frames.h" 26 #include "ecmascript/stubs/runtime_stubs.h" 27 28 namespace panda::ecmascript::kungfu { 29 class CompilationConfig; 30 class AssemblerModule { 31 public: 32 AssemblerModule() = default; ~AssemblerModule()33 ~AssemblerModule() 34 { 35 for (auto it : symbolTable_) { 36 delete it.second; 37 } 38 } 39 40 void Run(const CompilationConfig *cfg, Chunk* chunk); 41 GetFunctionCount()42 size_t GetFunctionCount() const 43 { 44 return symbolTable_.size(); 45 } 46 GetFunction(int id)47 size_t GetFunction(int id) const 48 { 49 panda::ecmascript::Label *label = GetFunctionLabel(id); 50 if (label->IsBound()) { 51 return label->GetPos(); 52 } else { 53 LOG_ECMA(FATAL) << "this branch is unreachable"; 54 UNREACHABLE(); 55 } 56 } 57 GetFunctionLabel(int id)58 panda::ecmascript::Label* GetFunctionLabel(int id) const 59 { 60 return symbolTable_.at(id); 61 } 62 GetBuffer()63 uint8_t* GetBuffer() const 64 { 65 return buffer_; 66 } 67 GetBufferSize()68 size_t GetBufferSize() const 69 { 70 return bufferSize_; 71 } 72 73 void SetUpForAsmStubs(); 74 GetCSigns()75 const std::vector<const CallSignature*> &GetCSigns() const 76 { 77 return asmCallSigns_; 78 } 79 GetCSign(size_t i)80 const CallSignature *GetCSign(size_t i) const 81 { 82 return asmCallSigns_[i]; 83 } 84 85 void GenerateStubsX64(Chunk* chunk); 86 void GenerateStubsAarch64(Chunk* chunk); 87 88 static bool IsCallNew(JSCallMode mode); 89 static int GetArgcFromJSCallMode(JSCallMode mode); 90 static bool JSModeHaveThisArg(JSCallMode mode); 91 static bool JSModeHaveNewTargetArg(JSCallMode mode); 92 static bool IsJumpToCallCommonEntry(JSCallMode mode); 93 private: 94 std::vector<const CallSignature *> asmCallSigns_; 95 std::map<int, panda::ecmascript::Label *> symbolTable_; 96 uint8_t* buffer_ {nullptr}; 97 size_t bufferSize_ {0}; 98 }; 99 100 class AssemblerStub { 101 public: 102 virtual void GenerateX64(Assembler* assembler) = 0; 103 virtual void GenerateAarch64(Assembler* assembler) = 0; 104 virtual ~AssemblerStub() = default; 105 }; 106 107 #define DECLARE_ASM_STUB_CLASS(name) \ 108 class name##Stub : public AssemblerStub { \ 109 public: \ 110 ~name##Stub() = default; \ 111 void GenerateX64(Assembler* assembler) override; \ 112 void GenerateAarch64(Assembler* assembler) override; \ 113 }; 114 RUNTIME_ASM_STUB_LIST(DECLARE_ASM_STUB_CLASS) 115 #undef DECLARE_ASM_STUB_CLASS 116 } // namespace panda::ecmascript::kunfu 117 #endif // ECMASCRIPT_COMPILER_ASSEMBLER_MODULE_H 118