• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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             UNREACHABLE();
54         }
55     }
56 
GetFunctionLabel(int id)57     panda::ecmascript::Label* GetFunctionLabel(int id) const
58     {
59         return symbolTable_.at(id);
60     }
61 
GetBuffer()62     uint8_t* GetBuffer() const
63     {
64         return buffer_;
65     }
66 
GetBufferSize()67     size_t GetBufferSize() const
68     {
69         return bufferSize_;
70     }
71 
72     void SetUpForAsmStubs();
73 
GetCSigns()74     const std::vector<const CallSignature*> &GetCSigns() const
75     {
76         return asmCallSigns_;
77     }
78 
GetCSign(size_t i)79     const CallSignature *GetCSign(size_t i) const
80     {
81         return asmCallSigns_[i];
82     }
83 
84     void GenerateStubsX64(Chunk* chunk);
85     void GenerateStubsAarch64(Chunk* chunk);
86 
87     static bool IsCallNew(JSCallMode mode);
88     static int GetArgcFromJSCallMode(JSCallMode mode);
89     static bool JSModeHaveThisArg(JSCallMode mode);
90     static bool JSModeHaveNewTargetArg(JSCallMode mode);
91     static bool IsJumpToCallCommonEntry(JSCallMode mode);
92 private:
93     std::vector<const CallSignature *> asmCallSigns_;
94     std::map<int, panda::ecmascript::Label *> symbolTable_;
95     uint8_t* buffer_ {nullptr};
96     size_t bufferSize_ {0};
97 };
98 
99 class AssemblerStub {
100 public:
101     virtual void GenerateX64(Assembler* assembler) = 0;
102     virtual void GenerateAarch64(Assembler* assembler) = 0;
103     virtual ~AssemblerStub() = default;
104 };
105 
106 #define DECLARE_ASM_STUB_CLASS(name)                        \
107 class name##Stub : public AssemblerStub {                   \
108 public:                                                     \
109     ~name##Stub() = default;                                \
110     void GenerateX64(Assembler* assembler) override;        \
111     void GenerateAarch64(Assembler* assembler) override;    \
112 };
113 RUNTIME_ASM_STUB_LIST(DECLARE_ASM_STUB_CLASS)
114 #undef DECLARE_ASM_STUB_CLASS
115 }  // namespace panda::ecmascript::kunfu
116 #endif  // ECMASCRIPT_COMPILER_ASSEMBLER_MODULE_H
117