• 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_TYPE_RECORDER_H
17 #define ECMASCRIPT_COMPILER_TYPE_RECORDER_H
18 
19 #include "ecmascript/compiler/type.h"
20 #include "ecmascript/jspandafile/js_pandafile.h"
21 #include "ecmascript/method.h"
22 #include "ecmascript/pgo_profiler/pgo_profiler_decoder.h"
23 #include "ecmascript/ts_types/ts_manager.h"
24 
25 namespace panda::ecmascript::kungfu {
26 enum class TypedArgIdx : uint8_t {
27     FUNC = 0,
28     NEW_TARGET,
29     THIS_OBJECT,
30     NUM_OF_TYPED_ARGS,
31 };
32 
33 class TypeRecorder {
34 public:
35     TypeRecorder(const JSPandaFile *jsPandaFile, const MethodLiteral *methodLiteral,
36                  TSManager *tsManager, const CString &recordName, PGOProfilerDecoder *decoder,
37                  const MethodPcInfo &methodPCInfo, const Bytecodes *bytecodes, bool enableOptTrackField);
38     ~TypeRecorder() = default;
39 
40     GateType GetType(const int32_t offset) const;
41     PGOSampleType GetOrUpdatePGOType(TSManager *tsManager, int32_t offset, const GateType &type) const;
42     PGORWOpType GetRwOpType(int32_t offset) const;
43     ElementsKind GetElementsKind(int32_t offset) const;
44     GateType GetArgType(const uint32_t argIndex) const;
45     GateType UpdateType(const int32_t offset, const GateType &type) const;
46     GateType GetCallTargetType(int32_t offset) const;
47 
48     static constexpr int METHOD_ANNOTATION_THIS_TYPE_OFFSET = -2;
49 
50 private:
51     void LoadTypes(const JSPandaFile *jsPandaFile, const MethodLiteral *methodLiteral,
52                    TSManager *tsManager, const CString &recordName);
53 
54     void CreateTypesForPGO(const JSPandaFile *jsPandaFile, const MethodLiteral *methodLiteral,
55                            TSManager *tsManager, const CString &recordName);
56 
57     void LoadTypesFromPGO(const JSPandaFile *jsPandaFile, const MethodLiteral *methodLiteral,
58                           const CString &recordName);
59 
60     void LoadArgTypes(const TSManager *tsManager, GlobalTSTypeRef funcGT, GlobalTSTypeRef thisGT);
61 
62     // This function tries to get the type of 'this'. In success, the type of the class is returned if the function is
63     // a static member, or the type of instances of the class is returned. Otherwise the type 'any' is returned.
64     GateType TryGetThisType(const TSManager *tsManager, GlobalTSTypeRef funcGT, GlobalTSTypeRef thisGT) const;
65 
66     // This function tries to get the type of 'newTarget'. In success, the type of the class is returned. Otherwise,
67     // the type 'any' is returned.
68     GateType TryGetNewTargetType(const TSManager *tsManager, GlobalTSTypeRef thisGT) const;
69 
70     // This function tries to get the type of the function. On failure, the type 'any' is returned.
71     GateType TryGetFuncType(GlobalTSTypeRef funcGT) const;
72 
73     bool TypeNeedFilter(GlobalTSTypeRef gt) const;
74 
75     bool CheckTypeMarkForDefineFunc(uint32_t checkBc) const;
76 
77     void CollectLiteralGT(TSManager *tsManager, TypeLocation &tLoc, GlobalTSTypeRef gt);
78 
79     std::unordered_map<int32_t, GateType> bcOffsetGtMap_ {};
80     std::unordered_map<int32_t, GateType> bcOffsetCallTargetGtMap_ {};
81     std::unordered_map<int32_t, PGOSampleType> bcOffsetPGOOpTypeMap_ {};
82     std::unordered_map<int32_t, PGORWOpType> bcOffsetPGORwTypeMap_ {};
83     std::vector<GateType> argTypes_;
84     PGOProfilerDecoder *decoder_ {nullptr};
85     bool enableOptTrackField_ {false};
86     const std::vector<const uint8_t*> &pcOffsets_;
87     const Bytecodes *bytecodes_ {nullptr};
88 };
89 }  // panda::ecmascript::kungfu
90 #endif  // ECMASCRIPT_COMPILER_TYPE_RECORDER_H
91