1/*
2 * Copyright (C) 2016 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
28namespace JSC {
29namespace MacroAssemblerHelpers {
30
31// True if this:
32// branch8(cond, value, value)
33// Is the same as this:
34// branch32(cond, signExt8(value), signExt8(value))
35template<typename MacroAssemblerType>
36inline bool isSigned(typename MacroAssemblerType::RelationalCondition cond)
37{
38 switch (cond) {
39 case MacroAssemblerType::Equal:
40 case MacroAssemblerType::NotEqual:
41 case MacroAssemblerType::GreaterThan:
42 case MacroAssemblerType::GreaterThanOrEqual:
43 case MacroAssemblerType::LessThan:
44 case MacroAssemblerType::LessThanOrEqual:
45 return true;
46 default:
47 return false;
48 }
49}
50
51// True if this:
52// branch8(cond, value, value)
53// Is the same as this:
54// branch32(cond, zeroExt8(value), zeroExt8(value))
55template<typename MacroAssemblerType>
56inline bool isUnsigned(typename MacroAssemblerType::RelationalCondition cond)
57{
58 switch (cond) {
59 case MacroAssemblerType::Equal:
60 case MacroAssemblerType::NotEqual:
61 case MacroAssemblerType::Above:
62 case MacroAssemblerType::AboveOrEqual:
63 case MacroAssemblerType::Below:
64 case MacroAssemblerType::BelowOrEqual:
65 return true;
66 default:
67 return false;
68 }
69}
70
71// True if this:
72// test8(cond, value, value)
73// Is the same as this:
74// test32(cond, signExt8(value), signExt8(value))
75template<typename MacroAssemblerType>
76inline bool isSigned(typename MacroAssemblerType::ResultCondition cond)
77{
78 switch (cond) {
79 case MacroAssemblerType::Signed:
80 case MacroAssemblerType::PositiveOrZero:
81 case MacroAssemblerType::Zero:
82 case MacroAssemblerType::NonZero:
83 return true;
84 default:
85 return false;
86 }
87}
88
89// True if this:
90// test8(cond, value, value)
91// Is the same as this:
92// test32(cond, zeroExt8(value), zeroExt8(value))
93template<typename MacroAssemblerType>
94inline bool isUnsigned(typename MacroAssemblerType::ResultCondition cond)
95{
96 switch (cond) {
97 case MacroAssemblerType::Zero:
98 case MacroAssemblerType::NonZero:
99 return true;
100 default:
101 return false;
102 }
103}
104
105template<typename MacroAssemblerType>
106inline typename MacroAssemblerType::TrustedImm32 mask8OnCondition(MacroAssemblerType&, typename MacroAssemblerType::RelationalCondition cond, typename MacroAssemblerType::TrustedImm32 value)
107{
108 if (isUnsigned<MacroAssemblerType>(cond))
109 return typename MacroAssemblerType::TrustedImm32(static_cast<uint8_t>(value.m_value));
110 return typename MacroAssemblerType::TrustedImm32(static_cast<int8_t>(value.m_value));
111}
112
113template<typename MacroAssemblerType>
114inline typename MacroAssemblerType::TrustedImm32 mask8OnCondition(MacroAssemblerType&, typename MacroAssemblerType::ResultCondition cond, typename MacroAssemblerType::TrustedImm32 value)
115{
116 if (isUnsigned<MacroAssemblerType>(cond))
117 return typename MacroAssemblerType::TrustedImm32(static_cast<uint8_t>(value.m_value));
118 ASSERT_WITH_MESSAGE(cond != MacroAssemblerType::Overflow, "Overflow is not used for 8bit test operations.");
119 ASSERT(isSigned<MacroAssemblerType>(cond));
120 return typename MacroAssemblerType::TrustedImm32(static_cast<int8_t>(value.m_value));
121}
122
123template<typename MacroAssemblerType, typename Condition, typename ...Args>
124void load8OnCondition(MacroAssemblerType& jit, Condition cond, Args... args)
125{
126 if (isUnsigned<MacroAssemblerType>(cond))
127 return jit.load8(std::forward<Args>(args)...);
128 return jit.load8SignedExtendTo32(std::forward<Args>(args)...);
129}
130
131} } // namespace JSC
132