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_COMPILER_INTERPRETER_STUB_H 17 #define ECMASCRIPT_COMPILER_INTERPRETER_STUB_H 18 19 #include "ecmascript/base/config.h" 20 #include "ecmascript/compiler/bc_call_signature.h" 21 #include "ecmascript/compiler/rt_call_signature.h" 22 #include "ecmascript/compiler/stub_builder.h" 23 24 namespace panda::ecmascript::kungfu { 25 struct StringIdInfo { 26 enum class Offset : uint8_t { 27 BYTE_0, 28 BYTE_1, 29 BYTE_2, 30 INVALID, 31 }; 32 enum class Length : uint8_t { 33 BITS_16, 34 BITS_32, 35 INVALID, 36 }; 37 38 GateRef constpool { 0 }; 39 GateRef pc { 0 }; 40 Offset offset { Offset::INVALID }; 41 Length length { Length::INVALID }; 42 IsValidStringIdInfo43 bool IsValid() const 44 { 45 return (constpool != 0) && (pc != 0) && (offset != Offset::INVALID) && (length != Length::INVALID); 46 } 47 }; 48 49 class InterpreterStubBuilder : public StubBuilder { 50 public: InterpreterStubBuilder(CallSignature * callSignature,Environment * env)51 InterpreterStubBuilder(CallSignature *callSignature, Environment *env) 52 : StubBuilder(callSignature, env) {} 53 ~InterpreterStubBuilder() = default; 54 NO_MOVE_SEMANTIC(InterpreterStubBuilder); 55 NO_COPY_SEMANTIC(InterpreterStubBuilder); 56 virtual void GenerateCircuit() = 0; 57 58 inline void SetVregValue(GateRef glue, GateRef sp, GateRef idx, GateRef val); 59 inline GateRef GetVregValue(GateRef sp, GateRef idx); 60 inline GateRef ReadInst4_0(GateRef pc); 61 inline GateRef ReadInst4_1(GateRef pc); 62 inline GateRef ReadInst4_2(GateRef pc); 63 inline GateRef ReadInst4_3(GateRef pc); 64 inline GateRef ReadInst8_0(GateRef pc); 65 inline GateRef ReadInst8_1(GateRef pc); 66 inline GateRef ReadInst8_2(GateRef pc); 67 inline GateRef ReadInst8_3(GateRef pc); 68 inline GateRef ReadInst8_4(GateRef pc); 69 inline GateRef ReadInst8_5(GateRef pc); 70 inline GateRef ReadInst8_6(GateRef pc); 71 inline GateRef ReadInst8_7(GateRef pc); 72 inline GateRef ReadInst8_8(GateRef pc); 73 inline GateRef ReadInst16_0(GateRef pc); 74 inline GateRef ReadInst16_1(GateRef pc); 75 inline GateRef ReadInst16_2(GateRef pc); 76 inline GateRef ReadInst16_3(GateRef pc); 77 inline GateRef ReadInst16_4(GateRef pc); 78 inline GateRef ReadInst16_5(GateRef pc); 79 inline GateRef ReadInst16_6(GateRef pc); 80 inline GateRef ReadInstSigned8_0(GateRef pc); 81 inline GateRef ReadInstSigned16_0(GateRef pc); 82 inline GateRef ReadInstSigned32_0(GateRef pc); 83 inline GateRef ReadInst32_0(GateRef pc); 84 inline GateRef ReadInst32_1(GateRef pc); 85 inline GateRef ReadInst32_2(GateRef pc); 86 inline GateRef ReadInst64_0(GateRef pc); 87 88 inline GateRef GetFrame(GateRef frame); 89 inline GateRef GetCurrentSpFrame(GateRef glue); 90 inline GateRef GetLastLeaveFrame(GateRef glue); 91 inline GateRef GetCurrentFrame(GateRef glue); 92 inline GateRef GetPcFromFrame(GateRef frame); 93 inline GateRef GetCallSizeFromFrame(GateRef frame); 94 inline GateRef GetFunctionFromFrame(GateRef frame); 95 inline GateRef GetThisFromFrame(GateRef frame); 96 inline GateRef GetAccFromFrame(GateRef frame); 97 inline GateRef GetEnvFromFrame(GateRef frame); 98 inline GateRef GetEnvFromFunction(GateRef frame); 99 inline GateRef GetConstpoolFromMethod(GateRef function); 100 inline GateRef GetProfileTypeInfoFromMethod(GateRef function); 101 inline GateRef GetModuleFromFunction(GateRef function); 102 inline GateRef GetHomeObjectFromFunction(GateRef function); 103 inline GateRef GetResumeModeFromGeneratorObject(GateRef obj); 104 inline GateRef GetResumeModeFromAsyncGeneratorObject(GateRef obj); 105 inline GateRef GetHotnessCounterFromMethod(GateRef method); 106 107 inline void SetCurrentSpFrame(GateRef glue, GateRef sp); 108 inline void SetLastLeaveFrame(GateRef glue, GateRef sp); 109 inline void SetPcToFrame(GateRef glue, GateRef frame, GateRef value); 110 inline void SetCallSizeToFrame(GateRef glue, GateRef frame, GateRef value); 111 inline void SetFunctionToFrame(GateRef glue, GateRef frame, GateRef value); 112 inline void SetAccToFrame(GateRef glue, GateRef frame, GateRef value); 113 inline void SetEnvToFrame(GateRef glue, GateRef frame, GateRef value); 114 inline void SetHomeObjectToFunction(GateRef glue, GateRef function, GateRef value); 115 inline void SetModuleToFunction(GateRef glue, GateRef function, GateRef value); 116 inline void SetFrameState(GateRef glue, GateRef sp, GateRef function, GateRef acc, 117 GateRef env, GateRef pc, GateRef prev, GateRef type); 118 119 inline void CheckException(GateRef glue, GateRef sp, GateRef pc, GateRef constpool, 120 GateRef profileTypeInfo, GateRef acc, GateRef hotnessCounter, 121 GateRef res, GateRef offset); 122 inline void CheckPendingException(GateRef glue, GateRef sp, GateRef pc, GateRef constpool, 123 GateRef profileTypeInfo, GateRef acc, GateRef hotnessCounter, 124 GateRef res, GateRef offset); 125 inline void CheckExceptionWithJump(GateRef glue, GateRef sp, GateRef pc, GateRef constpool, 126 GateRef profileTypeInfo, GateRef acc, GateRef hotnessCounter, 127 GateRef res, Label *jump); 128 inline void CheckExceptionWithVar(GateRef glue, GateRef sp, GateRef pc, GateRef constpool, 129 GateRef profileTypeInfo, GateRef acc, GateRef hotnessCounter, 130 GateRef res, GateRef offset); 131 132 inline GateRef CheckStackOverflow(GateRef glue, GateRef sp); 133 inline GateRef PushArg(GateRef glue, GateRef sp, GateRef value); 134 inline GateRef PushUndefined(GateRef glue, GateRef sp, GateRef num); 135 inline GateRef PushRange(GateRef glue, GateRef sp, GateRef array, GateRef startIndex, GateRef endIndex); 136 inline GateRef GetStartIdxAndNumArgs(GateRef sp, GateRef restIdx); 137 138 inline void Dispatch(GateRef glue, GateRef sp, GateRef pc, GateRef constpool, 139 GateRef profileTypeInfo, GateRef acc, GateRef hotnessCounter, GateRef format); 140 inline void DispatchWithId(GateRef glue, GateRef sp, GateRef pc, GateRef constpool, 141 GateRef profileTypeInfo, GateRef acc, GateRef hotnessCounter, GateRef index); 142 inline void DispatchLast(GateRef glue, GateRef sp, GateRef pc, GateRef constpool, 143 GateRef profileTypeInfo, GateRef acc, GateRef hotnessCounter); 144 inline void DispatchDebugger(GateRef glue, GateRef sp, GateRef pc, GateRef constpool, 145 GateRef profileTypeInfo, GateRef acc, GateRef hotnessCounter); 146 inline void DispatchDebuggerLast(GateRef glue, GateRef sp, GateRef pc, GateRef constpool, 147 GateRef profileTypeInfo, GateRef acc, GateRef hotnessCounter); 148 template <bool needPrint> 149 void DebugPrintInstruction(); 150 private: 151 template<typename... Args> 152 void DispatchBase(GateRef target, GateRef glue, Args... args); 153 }; 154 155 class InterpreterToolsStubBuilder : private InterpreterStubBuilder { 156 public: InterpreterToolsStubBuilder(CallSignature * callSignature,Environment * env)157 explicit InterpreterToolsStubBuilder(CallSignature *callSignature, Environment *env) 158 : InterpreterStubBuilder(callSignature, env) {} 159 ~InterpreterToolsStubBuilder() = default; 160 NO_MOVE_SEMANTIC(InterpreterToolsStubBuilder); 161 NO_COPY_SEMANTIC(InterpreterToolsStubBuilder); GenerateCircuit()162 void GenerateCircuit() override {} 163 164 inline GateRef GetStringId(const StringIdInfo &info); 165 }; 166 167 #define DECLARE_HANDLE_STUB_CLASS(name) \ 168 class name##StubBuilder : public InterpreterStubBuilder { \ 169 public: \ 170 explicit name##StubBuilder(CallSignature *callSignature, Environment *env) \ 171 : InterpreterStubBuilder(callSignature, env) \ 172 { \ 173 env->GetCircuit()->SetFrameType(FrameType::ASM_INTERPRETER_FRAME); \ 174 } \ 175 ~name##StubBuilder() = default; \ 176 NO_MOVE_SEMANTIC(name##StubBuilder); \ 177 NO_COPY_SEMANTIC(name##StubBuilder); \ 178 void GenerateCircuit() override; \ 179 \ 180 private: \ 181 void GenerateCircuitImpl(GateRef glue, GateRef sp, GateRef pc, GateRef constpool, \ 182 GateRef profileTypeInfo, GateRef acc, GateRef hotnessCounter); \ 183 }; 184 INTERPRETER_BC_STUB_LIST(DECLARE_HANDLE_STUB_CLASS) 185 ASM_INTERPRETER_BC_HELPER_STUB_LIST(DECLARE_HANDLE_STUB_CLASS) 186 #undef DECLARE_HANDLE_STUB_CLASS 187 } // namespace panda::ecmascript::kungfu 188 #endif // ECMASCRIPT_COMPILER_INTERPRETER_STUB_H 189