1 /** 2 * Copyright 2023 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #ifndef MINDSPORE_PI_JIT_OPCODE_UTIL_H 17 #define MINDSPORE_PI_JIT_OPCODE_UTIL_H 18 19 namespace mindspore { 20 namespace pijit { 21 22 class Opcode { 23 public: 24 static const Opcode k_ILLEGAL_OPCODE; Map(int op)25 static const Opcode &Map(int op) { return Map()[op > 0 && op < kMaxCode ? op : 0]; } 26 27 private: 28 static const Opcode *Map(); 29 30 static constexpr auto kMaxCode = 256; 31 static Opcode opmap[kMaxCode]; 32 33 public: 34 // opcode class 35 enum Class : unsigned char { 36 kStack = 0, // stack operations 37 kLocal, // local access 38 kGlobal, // global access 39 kCell, // cell (types.CellType) access 40 kItem, // __getitem__, __setitem__, __delitem__ 41 kAttr, // __getattr__, __setattr__, __delattr__ 42 kUnaryMath, // +,-,not,~ 43 kBinaryMath, // +,-,*,**,/,//,@,&,|,^,<<,>>,<,<=,>,>=,==,!= 44 kContainerBuild, // (),[],{a:b},{a},[:], build a new object 45 kContainerMerge, // with container modify 46 kCall, // with object call 47 kControl, // has jump 48 kUnpack, // a,b=c; a,b,*=c; 49 kNop, // generally, no operations 50 kException, // exception raise and handler, with syntax, try syntax 51 kOther, 52 kCount, 53 }; 54 55 Opcode(); Opcode(const char * name,int op,Class cls,int flag)56 Opcode(const char *name, int op, Class cls, int flag) : name_(name), code_(op), class_(cls), flag_(flag) {} Opcode(int op)57 explicit Opcode(int op) { *this = Map(op); } 58 Opcode &operator=(int &&op) { 59 *this = Map(op); 60 return *this; 61 } 62 Opcode &operator=(const Opcode &) = default; 63 bool operator==(int op) const { return code_ == op; } 64 bool operator!=(int op) const { return code_ != op; } 65 bool operator==(const Opcode &o) const { return code_ == o.code_; } 66 bool operator!=(const Opcode &o) const { return code_ != o.code_; } name()67 const char *name() const { return name_; } flag()68 int flag() const { return flag_; } 69 70 bool IsJRel() const; 71 bool IsJAbs() const; 72 bool IsNotFall() const; 73 bool HasName() const; 74 bool HasFree() const; 75 bool HasConst() const; 76 bool CanDelete(int oparg = 0) const; 77 bool MayDelete(int oparg = 0) const; 78 IsCall()79 bool IsCall() const { return class_ == Class::kCall; } IsBinaryMath()80 bool IsBinaryMath() const { return class_ == Class::kBinaryMath; } IsUnaryMath()81 bool IsUnaryMath() const { return class_ == Class::kUnaryMath; } IsCellAccess()82 bool IsCellAccess() const { return class_ == Class::kCell; } IsLocalAccess()83 bool IsLocalAccess() const { return class_ == Class::kLocal; } 84 85 // python3.9 explicit IS_OP from COMPARE_OP 86 bool CheckIsOp(int oparg, bool *invert = nullptr) const; 87 // python3.9 explicit CONTAINS_OP from COMPARE_OP 88 bool CheckContainsOp(int oparg, bool *invert = nullptr) const; 89 // python3.11 merge binary math opcode to BINARY_OP 90 // CheckInplaceBinaryOp... 91 GetClass()92 Class GetClass() const { return class_; } 93 94 bool HasArg() const; 95 int JumpTarget(int pc, int off) const; 96 int JumpOffset(int pc, int tar) const; 97 98 constexpr operator int() const { return code_; } 99 100 private: 101 const char *name_; 102 unsigned char code_; 103 Class class_; 104 int flag_; 105 }; 106 107 } // namespace pijit 108 } // namespace mindspore 109 110 #endif // MINDSPORE_PI_JIT_OPCODE_UTIL_H 111