1 /* 2 * Copyright (c) 2023 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_BUILTIN_INLINE_H 17 #define ECMASCRIPT_COMPILER_BUILTIN_INLINE_H 18 19 #include "ecmascript/compiler/builtins/builtins_call_signature.h" 20 #include "ecmascript/compiler/circuit_builder.h" 21 #include "ecmascript/compiler/gate_accessor.h" 22 #include "ecmascript/compiler/graph_linearizer.h" 23 #include "ecmascript/compiler/pass_manager.h" 24 #include "ecmascript/compiler/share_gate_meta_data.h" 25 #include "ecmascript/compiler/type_info_accessors.h" 26 #include "ecmascript/js_dataview.h" 27 28 namespace panda::ecmascript::kungfu { 29 class NativeInlineLowering { 30 public: NativeInlineLowering(Circuit * circuit,CompilationConfig * cmpCfg,PassContext * ctx,bool enableLog,const std::string & name,Chunk * chunk)31 explicit NativeInlineLowering(Circuit *circuit, CompilationConfig* cmpCfg, PassContext *ctx, bool enableLog, 32 const std::string& name, Chunk *chunk) 33 : circuit_(circuit), 34 builder_(circuit, cmpCfg), 35 acc_(circuit), 36 glue_(acc_.GetGlueFromArgList()), 37 enableLog_(enableLog), 38 methodName_(name), 39 nocheck_(ctx->GetCompilationEnv()->GetJSOptions().IsCompilerNoCheck()), 40 traceInline_(ctx->GetCompilationEnv()->GetJSOptions().GetTraceInline()), 41 compilationEnv_(ctx->GetCompilationEnv()), 42 chunk_(chunk) {} 43 ~NativeInlineLowering() = default; 44 void RunNativeInlineLowering(); 45 46 private: 47 enum IncludesOrIndexOf : uint8_t { INCLUDES = 0, INDEXOF }; 48 std::optional<std::pair<size_t, bool>> GetCallInfo(GateRef gate); 49 void TryInlineStringFromCharCode(GateRef gate, size_t argc, bool skipThis); 50 void TryInlineStringSubstring(GateRef gate, size_t argc, bool skipThis); 51 void TryInlineStringSubStr(GateRef gate, size_t argc, bool skipThis); 52 void TryInlineStringSlice(GateRef gate, size_t argc, bool skipThis); 53 void TryInlineNumberIsFinite(GateRef gate, size_t argc, bool skipThis); 54 void TryInlineNumberIsInteger(GateRef gate, size_t argc, bool skipThis); 55 void TryInlineNumberIsNaN(GateRef gate, size_t argc, bool skipThis); 56 void TryInlineNumberParseFloat(GateRef gate, size_t argc, bool skipThis); 57 void TryInlineNumberParseInt(GateRef gate, size_t argc, bool skipThis); 58 void TryInlineNumberIsSafeInteger(GateRef gate, size_t argc, bool skipThis); 59 void TryInlineTypedArrayIteratorBuiltin(GateRef gate, BuiltinsStubCSigns::ID id, 60 const GateMetaData* op, bool skipThis); 61 void TryInlineMathUnaryBuiltin(GateRef gate, size_t argc, BuiltinsStubCSigns::ID id, const GateMetaData* op, 62 bool skipThis); 63 void TryInlineMathBinaryBuiltin(GateRef gate, size_t argc, BuiltinsStubCSigns::ID id, const GateMetaData* op, 64 bool skipThis); 65 void TryInlineMathImulBuiltin(GateRef gate, size_t argc, BuiltinsStubCSigns::ID id, const GateMetaData* op, 66 bool skipThis); 67 void TryInlineGlobalFiniteBuiltin(GateRef gate, size_t argc, BuiltinsStubCSigns::ID id, const GateMetaData* op, 68 bool skipThis); 69 void TryInlineGlobalNanBuiltin(GateRef gate, size_t argc, BuiltinsStubCSigns::ID id, const GateMetaData* op, 70 bool skipThis); 71 void TryInlineMathMinMaxBuiltin(GateRef gate, size_t argc, BuiltinsStubCSigns::ID id, const GateMetaData* op, 72 double defaultValue, bool skipThis); 73 void TryInlineMathAbsBuiltin(GateRef gate, size_t argc, bool skipThis); 74 void TryInlineMathClz32Builtin(GateRef gate, size_t argc, bool skipThis); 75 void TryInlineArrayBufferIsView(GateRef gate, size_t argc, BuiltinsStubCSigns::ID id, bool skipThis); 76 void TryInlineBigIntAsIntN(GateRef gate, size_t argc, BuiltinsStubCSigns::ID id, bool skipThis); 77 void TryInlineDataViewGet(GateRef gate, size_t argc, BuiltinsStubCSigns::ID id, bool skipThis); 78 void TryInlineDataViewSet(GateRef gate, size_t argc, BuiltinsStubCSigns::ID id, bool skipThis); 79 void InlineStubBuiltin(GateRef gate, size_t builtinArgc, size_t realArgc, BuiltinsStubCSigns::ID id, 80 const GateMetaData* op, bool skipThis); 81 void TryInlineDateGetTime(GateRef gate, size_t argc, bool skipThis); 82 void TryInlineWhitoutParamBuiltin(GateRef gate, size_t argc, BuiltinsStubCSigns::ID id, 83 const GateMetaData* op, bool skipThis); 84 void TryInlineObjectIs(GateRef gate, size_t argc, BuiltinsStubCSigns::ID id, bool skipThis); 85 void TryInlineObjectGetPrototypeOf(GateRef gate, size_t argc, BuiltinsStubCSigns::ID id, bool skipThis); 86 void TryInlineObjectGetProto(GateRef gate, size_t argc, BuiltinsStubCSigns::ID id, bool skipThis); 87 void TryInlineObjectCreate(GateRef gate, size_t argc, BuiltinsStubCSigns::ID id, bool skipThis); 88 void TryInlineObjectIsPrototypeOf(GateRef gate, size_t argc, BuiltinsStubCSigns::ID id, bool skipThis); 89 void TryInlineObjectHasOwnProperty(GateRef gate, size_t argc, BuiltinsStubCSigns::ID id, bool skipThis); 90 void TryInlineReflectGetPrototypeOf(GateRef gate, size_t argc, BuiltinsStubCSigns::ID id, bool skipThis); 91 void TryInlineReflectGet(GateRef gate, size_t argc, BuiltinsStubCSigns::ID id, bool skipThis); 92 void TryInlineReflectHas(GateRef gate, size_t argc, BuiltinsStubCSigns::ID id, bool skipThis); 93 void TryInlineReflectConstruct(GateRef gate, size_t argc, BuiltinsStubCSigns::ID id, bool skipThis); 94 void TryInlineReflectApply(GateRef gate, size_t argc, BuiltinsStubCSigns::ID id, bool skipThis); 95 void TryInlineFunctionPrototypeApply(GateRef gate, size_t argc, BuiltinsStubCSigns::ID id, bool skipThis); 96 void TryInlineFunctionPrototypeBind(GateRef gate, size_t argc, BuiltinsStubCSigns::ID id, bool skipThis); 97 void TryInlineFunctionPrototypeCall(GateRef gate, size_t argc, BuiltinsStubCSigns::ID id, bool skipThis); 98 void TryInlineFunctionPrototypeHasInstance(GateRef gate, size_t argc, BuiltinsStubCSigns::ID id, bool skipThis); 99 100 void TryInlineBigIntConstructor(GateRef gate, size_t argc, bool skipThis); 101 void ReplaceGateWithPendingException(GateRef hirGate, GateRef value); 102 void AddTraceLogs(GateRef gate, BuiltinsStubCSigns::ID id); 103 void TryInlineIndexOfIncludes(GateRef gate, size_t argc, BuiltinsStubCSigns::ID id, bool skipThis); 104 void TryInlineArrayIterator(GateRef gate, BuiltinsStubCSigns::ID id, bool skipThis); 105 void TryInlineArrayForEach(GateRef gate, size_t argc, BuiltinsStubCSigns::ID id, bool skipThis); 106 void TryInlineArrayFindOrFindIndex(GateRef gate, size_t argc, BuiltinsStubCSigns::ID id, bool skipThis); 107 void TryInlineArrayFilter(GateRef gate, size_t argc, BuiltinsStubCSigns::ID id, bool skipThis); 108 void TryInlineArrayMap(GateRef gate, size_t argc, BuiltinsStubCSigns::ID id, bool skipThis); 109 void TryInlineArraySome(GateRef gate, size_t argc, BuiltinsStubCSigns::ID id, bool skipThis); 110 void TryInlineArrayEvery(GateRef gate, size_t argc, BuiltinsStubCSigns::ID id, bool skipThis); 111 void TryInlineArrayPop(GateRef gate, size_t argc, BuiltinsStubCSigns::ID id, bool skipThis); 112 void TryInlineArraySlice(GateRef gate, size_t argc, BuiltinsStubCSigns::ID id, bool skipThis); 113 void TryInlineArraySort(GateRef gate, size_t argc, BuiltinsStubCSigns::ID id, bool skipThis); 114 EnableLog()115 bool EnableLog() const 116 { 117 return enableLog_; 118 } 119 GetMethodName()120 const std::string& GetMethodName() const 121 { 122 return methodName_; 123 } 124 Uncheck()125 bool Uncheck() const 126 { 127 return nocheck_; 128 } 129 EnableTrace()130 bool EnableTrace() const 131 { 132 return traceInline_; 133 } 134 135 private: 136 Circuit *circuit_ {nullptr}; 137 CircuitBuilder builder_; 138 GateAccessor acc_; 139 GateRef glue_; 140 bool enableLog_; 141 std::string methodName_; 142 bool nocheck_; 143 bool traceInline_; 144 const CompilationEnv *compilationEnv_ {nullptr}; 145 Chunk* chunk_ {nullptr}; 146 }; 147 } 148 #endif // ECMASCRIPT_COMPILER_BUILTIN_INLINE_H 149