Support for InstanceOfD translations
[hiphop-php.git] / src / runtime / vm / translator / hopt / type.cpp
blobed5644ba575c2c209d6bbaddca1ca10c7a37f318
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010- Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #include "runtime/vm/translator/hopt/ir.h"
19 namespace HPHP { namespace VM { namespace JIT {
21 //////////////////////////////////////////////////////////////////////
23 Type::Tag outputType(const IRInstruction* inst) {
24 assert(inst->hasDst() && "outputType requires inst->hasDst()");
26 switch (inst->getOpcode()) {
27 case Unbox:
28 return Type::unbox(inst->getSrc(0)->getType());
30 case Box:
31 return Type::box(inst->getSrc(0)->getType());
33 case StRef:
34 case StRefNT:
35 return Type::box(inst->getSrc(1)->getType());
37 case AddElem: return Type::Arr;
38 case AddNewElem: return Type::Arr;
39 case ArrayAdd: return Type::Arr;
40 case AssertStk: return Type::StkPtr;
41 case Call: return Type::StkPtr;
42 case Concat: return Type::Str;
43 case CreateCont: return Type::Obj;
44 case DefActRec: return Type::ActRec;
45 case DefCns: return Type::Bool;
46 case DefFP: return Type::StkPtr;
47 case DefFunc: return Type::FuncPtr;
48 case DefSP: return Type::StkPtr;
49 case FreeActRec: return Type::StkPtr;
50 case GenericRetDecRefs: return Type::StkPtr;
51 case GuardStk: return Type::StkPtr;
52 case InstanceOf: return Type::Bool;
53 case InstanceOfBitmask: return Type::Bool;
54 case ExtendsClass: return Type::Bool;
55 case InterpOne: return Type::StkPtr;
56 case IsNSet: return Type::Bool;
57 case IsNType: return Type::Bool;
58 case IsSet: return Type::Bool;
59 case IsType: return Type::Bool;
60 case LdARFuncPtr: return Type::FuncPtr;
61 case LdCachedClass: return Type::ClassPtr;
62 case LdClsMethodCache: return Type::FuncClassPtr;
63 case LdClsMethod: return Type::FuncPtr;
64 case LdClsPropAddr: return Type::PtrToGen;
65 case LdCls: return Type::ClassPtr;
66 case LdContLocalsPtr: return Type::PtrToCell;
67 case LdCurFuncPtr: return Type::FuncPtr;
68 case LdFixedFunc: return Type::FuncPtr;
69 case LdFuncCls: return Type::ClassPtr;
70 case LdFunc: return Type::FuncPtr;
71 case LdHome: return Type::Home;
72 case LdLocAddr: return Type::PtrToGen;
73 case LdObjClass: return Type::ClassPtr;
74 case LdObjMethod: return Type::FuncPtr;
75 case LdPropAddr: return Type::PtrToGen;
76 case LdRetAddr: return Type::RetAddr;
77 case LdStackAddr: return Type::PtrToGen;
78 case LdThis: return Type::Obj;
79 case NewArray: return Type::Arr;
80 case NewObj: return Type::StkPtr;
81 case NewTuple: return Type::Arr;
82 case NInstanceOf: return Type::Bool;
83 case NInstanceOfBitmask: return Type::Bool;
84 case RetAdjustStack: return Type::StkPtr;
85 case SpillStack: return Type::StkPtr;
86 case UnboxPtr: return Type::PtrToCell;
88 // Vector translator opcodes
89 case DefMIStateBase: return Type::PtrToCell;
90 case PropX: return Type::PtrToGen;
91 case CGetProp:
92 case CGetElem: return Type::Cell;
94 case OpAdd:
95 case OpSub:
96 case OpAnd:
97 case OpOr:
98 case OpXor:
99 case OpMul:
100 return Type::Int;
102 case OpGt:
103 case OpGte:
104 case OpLt:
105 case OpLte:
106 case OpEq:
107 case OpNeq:
108 case OpSame:
109 case OpNSame:
110 case IterNext:
111 case IterNextK:
112 case IterInit:
113 case IterInitK:
114 return Type::Bool;
116 // Jumps have dests just to allow finding the TCA to patch. The
117 // dest SSATmp doesn't actually carry any value.
118 case JmpGt:
119 case JmpGte:
120 case JmpLt:
121 case JmpLte:
122 case JmpEq:
123 case JmpNeq:
124 case JmpZero:
125 case JmpNZero:
126 case JmpSame:
127 case JmpNSame:
128 case JmpInstanceOf:
129 case JmpNInstanceOf:
130 case JmpInstanceOfBitmask:
131 case JmpNInstanceOfBitmask:
132 case JmpIsSet:
133 case JmpIsType:
134 case JmpIsNSet:
135 case JmpIsNType:
136 case Jmp_:
137 return Type::None;
139 // Output type is the same as the first input's type.
140 case Mov:
141 case IncRef:
142 case Spill:
143 case Reload:
144 case LdAddr:
145 return inst->getSrc(0)->getType();
147 // Output type is given by a type paramter to the instruction.
148 case Conv:
149 case LdClsCns:
150 case LdLoc:
151 case LdMem:
152 case LdProp:
153 case LdRaw:
154 case LdRef:
155 case LdStack:
156 case DefConst:
157 case LdConst:
158 case GuardType:
159 return inst->getTypeParam();
161 default:
162 always_assert(0 && "outputType not known for opcode");
166 void assertOperandTypes(const IRInstruction* inst) {
167 #ifdef DEBUG
168 auto const tparam = inst->getTypeParam();
169 auto const s0 = inst->getSrc(0);
170 auto const s1 = inst->getSrc(1);
171 auto const t0 = s0 ? s0->getType() : Type::None;
172 auto const t1 = s1 ? s1->getType() : Type::None;
174 auto constStaticStr = [] (SSATmp* ssa) {
175 assert(ssa->isConst() && ssa->getType() == Type::StaticStr);
178 auto constInt = [] (SSATmp* ssa) {
179 assert(ssa->isConst() && ssa->getType() == Type::Int);
182 switch (inst->getOpcode()) {
183 case OpAdd:
184 case OpSub:
185 case OpAnd:
186 case OpOr:
187 case OpXor:
188 case OpMul:
189 assert(t0 == Type::Int || t0 == Type::Bool);
190 assert(t1 == Type::Int || t1 == Type::Bool);
191 break;
193 case LdRaw:
194 assert(tparam == Type::Int || tparam == Type::Bool ||
195 tparam == Type::StkPtr || tparam == Type::TCA);
196 break;
198 case LdPropAddr:
199 assert(t0 == Type::Obj);
200 constInt(s1);
201 break;
203 case LdCls:
204 constStaticStr(s0);
205 break;
207 case LdClsCns:
208 constStaticStr(s0);
209 constStaticStr(s1);
210 break;
212 case LdObjClass:
213 assert(t0 == Type::Obj);
214 break;
216 case StRaw: {
217 auto valT = inst->getSrc(2)->getType();
218 assert(valT == Type::Int || valT == Type::Bool);
219 break;
222 case StMem:
223 assert(t0 == Type::PtrToCell || t0 == Type::PtrToGen);
224 break;
226 default:
227 break;
229 #endif
232 //////////////////////////////////////////////////////////////////////