• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #ifndef ResultType_h
27 #define ResultType_h
28 
29 namespace JSC {
30 
31     struct ResultType {
32         friend struct OperandTypes;
33 
34         typedef char Type;
35         static const Type TypeReusable = 1;
36         static const Type TypeInt32    = 2;
37 
38         static const Type TypeMaybeNumber = 0x04;
39         static const Type TypeMaybeString = 0x08;
40         static const Type TypeMaybeNull   = 0x10;
41         static const Type TypeMaybeBool   = 0x20;
42         static const Type TypeMaybeOther  = 0x40;
43 
44         static const Type TypeBits = TypeMaybeNumber | TypeMaybeString | TypeMaybeNull | TypeMaybeBool | TypeMaybeOther;
45 
ResultTypeResultType46         explicit ResultType(Type type)
47             : m_type(type)
48         {
49         }
50 
isReusableResultType51         bool isReusable()
52         {
53             return m_type & TypeReusable;
54         }
55 
isInt32ResultType56         bool isInt32()
57         {
58             return m_type & TypeInt32;
59         }
60 
definitelyIsNumberResultType61         bool definitelyIsNumber()
62         {
63             return (m_type & TypeBits) == TypeMaybeNumber;
64         }
65 
mightBeNumberResultType66         bool mightBeNumber()
67         {
68             return m_type & TypeMaybeNumber;
69         }
70 
isNotNumberResultType71         bool isNotNumber()
72         {
73             return !mightBeNumber();
74         }
75 
nullTypeResultType76         static ResultType nullType()
77         {
78             return ResultType(TypeMaybeNull);
79         }
80 
booleanTypeResultType81         static ResultType booleanType()
82         {
83             return ResultType(TypeMaybeBool);
84         }
85 
numberTypeResultType86         static ResultType numberType()
87         {
88             return ResultType(TypeMaybeNumber);
89         }
90 
numberTypeCanReuseResultType91         static ResultType numberTypeCanReuse()
92         {
93             return ResultType(TypeReusable | TypeMaybeNumber);
94         }
95 
numberTypeCanReuseIsInt32ResultType96         static ResultType numberTypeCanReuseIsInt32()
97         {
98             return ResultType(TypeReusable | TypeInt32 | TypeMaybeNumber);
99         }
100 
stringOrNumberTypeCanReuseResultType101         static ResultType stringOrNumberTypeCanReuse()
102         {
103             return ResultType(TypeReusable | TypeMaybeNumber | TypeMaybeString);
104         }
105 
stringTypeResultType106         static ResultType stringType()
107         {
108             return ResultType(TypeMaybeString);
109         }
110 
unknownTypeResultType111         static ResultType unknownType()
112         {
113             return ResultType(TypeBits);
114         }
115 
forAddResultType116         static ResultType forAdd(ResultType op1, ResultType op2)
117         {
118             if (op1.definitelyIsNumber() && op2.definitelyIsNumber())
119                 return numberTypeCanReuse();
120             if (op1.isNotNumber() || op2.isNotNumber())
121                 return stringType();
122             return stringOrNumberTypeCanReuse();
123         }
124 
forBitOpResultType125         static ResultType forBitOp()
126         {
127             return numberTypeCanReuseIsInt32();
128         }
129 
130     private:
131         Type m_type;
132     };
133 
134     struct OperandTypes
135     {
136         OperandTypes(ResultType first = ResultType::unknownType(), ResultType second = ResultType::unknownType())
137         {
138             // We have to initialize one of the int to ensure that
139             // the entire struct is initialized.
140             m_u.i = 0;
141             m_u.rds.first = first.m_type;
142             m_u.rds.second = second.m_type;
143         }
144 
145         union {
146             struct {
147                 ResultType::Type first;
148                 ResultType::Type second;
149             } rds;
150             int i;
151         } m_u;
152 
firstOperandTypes153         ResultType first()
154         {
155             return ResultType(m_u.rds.first);
156         }
157 
secondOperandTypes158         ResultType second()
159         {
160             return ResultType(m_u.rds.second);
161         }
162 
toIntOperandTypes163         int toInt()
164         {
165             return m_u.i;
166         }
fromIntOperandTypes167         static OperandTypes fromInt(int value)
168         {
169             OperandTypes types;
170             types.m_u.i = value;
171             return types;
172         }
173     };
174 
175 } // namespace JSC
176 
177 #endif // ResultType_h
178