1/*
2 * Copyright (C) 2017 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#pragma once
27
28#if ENABLE(B3_JIT)
29
30#include "B3Bank.h"
31#include "B3Type.h"
32
33#if ASSERT_DISABLED
34IGNORE_RETURN_TYPE_WARNINGS_BEGIN
35#endif
36
37namespace JSC { namespace B3 {
38
39enum Width : int8_t {
40 Width8,
41 Width16,
42 Width32,
43 Width64
44};
45
46inline Width pointerWidth()
47{
48 if (sizeof(void*) == 8)
49 return Width64;
50 return Width32;
51}
52
53// Don't use this unless the compiler forces you to.
54#if CPU(X86_64) || CPU(ARM64)
55#define POINTER_WIDTH Width64
56#else
57#define POINTER_WIDTH Width32
58#endif
59
60inline Width widthForType(Type type)
61{
62 switch (type) {
63 case Void:
64 ASSERT_NOT_REACHED();
65 return Width8;
66 case Int32:
67 case Float:
68 return Width32;
69 case Int64:
70 case Double:
71 return Width64;
72 }
73 ASSERT_NOT_REACHED();
74 return Width8;
75}
76
77inline Width canonicalWidth(Width width)
78{
79 return std::max(Width32, width);
80}
81
82inline bool isCanonicalWidth(Width width)
83{
84 return width >= Width32;
85}
86
87Type bestType(Bank bank, Width width);
88
89inline Width conservativeWidth(Bank bank)
90{
91 return bank == GP ? pointerWidth() : Width64;
92}
93
94inline Width minimumWidth(Bank bank)
95{
96 return bank == GP ? Width8 : Width32;
97}
98
99inline unsigned bytes(Width width)
100{
101 return 1 << width;
102}
103
104inline Width widthForBytes(unsigned bytes)
105{
106 switch (bytes) {
107 case 0:
108 case 1:
109 return Width8;
110 case 2:
111 return Width16;
112 case 3:
113 case 4:
114 return Width32;
115 default:
116 return Width64;
117 }
118}
119
120inline uint64_t mask(Width width)
121{
122 switch (width) {
123 case Width8:
124 return 0x00000000000000ffllu;
125 case Width16:
126 return 0x000000000000ffffllu;
127 case Width32:
128 return 0x00000000ffffffffllu;
129 case Width64:
130 return 0xffffffffffffffffllu;
131 }
132 ASSERT_NOT_REACHED();
133}
134
135} } // namespace JSC::B3
136
137namespace WTF {
138
139class PrintStream;
140
141void printInternal(PrintStream&, JSC::B3::Width);
142
143} // namespace WTF
144
145#if ASSERT_DISABLED
146IGNORE_RETURN_TYPE_WARNINGS_END
147#endif
148
149#endif // ENABLE(B3_JIT)
150
151