1/*
2 * Copyright (C) 2015-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#include "config.h"
27#include "B3SwitchValue.h"
28
29#if ENABLE(B3_JIT)
30
31#include "B3BasicBlockInlines.h"
32#include <wtf/ListDump.h>
33
34namespace JSC { namespace B3 {
35
36SwitchValue::~SwitchValue()
37{
38}
39
40BasicBlock* SwitchValue::fallThrough(const BasicBlock* owner)
41{
42 ASSERT(hasFallThrough());
43 BasicBlock* fallThrough = owner->successor(owner->numSuccessors() - 1).block();
44 ASSERT(fallThrough == owner->fallThrough().block());
45 return fallThrough;
46}
47
48bool SwitchValue::hasFallThrough(const BasicBlock* block) const
49{
50 unsigned numSuccessors = block->numSuccessors();
51 unsigned numValues = m_values.size();
52 RELEASE_ASSERT(numValues == numSuccessors || numValues + 1 == numSuccessors);
53
54 return numValues + 1 == numSuccessors;
55}
56
57bool SwitchValue::hasFallThrough() const
58{
59 return hasFallThrough(owner);
60}
61
62void SwitchValue::setFallThrough(BasicBlock* block, const FrequentedBlock& target)
63{
64 if (!hasFallThrough())
65 block->successors().append(target);
66 else
67 block->successors().last() = target;
68 ASSERT(hasFallThrough(block));
69}
70
71void SwitchValue::appendCase(BasicBlock* block, const SwitchCase& switchCase)
72{
73 if (!hasFallThrough())
74 block->successors().append(switchCase.target());
75 else {
76 block->successors().append(block->successors().last());
77 block->successor(block->numSuccessors() - 2) = switchCase.target();
78 }
79 m_values.append(switchCase.caseValue());
80}
81
82void SwitchValue::setFallThrough(const FrequentedBlock& target)
83{
84 setFallThrough(owner, target);
85}
86
87void SwitchValue::appendCase(const SwitchCase& switchCase)
88{
89 appendCase(owner, switchCase);
90}
91
92void SwitchValue::dumpSuccessors(const BasicBlock* block, PrintStream& out) const
93{
94 // We must not crash due to a number-of-successors mismatch! Someone debugging a
95 // number-of-successors bug will want to dump IR!
96 if (numCaseValues() + 1 != block->numSuccessors()) {
97 Value::dumpSuccessors(block, out);
98 return;
99 }
100
101 out.print(cases(block));
102}
103
104void SwitchValue::dumpMeta(CommaPrinter& comma, PrintStream& out) const
105{
106 out.print(comma, "cases = [", listDump(m_values), "]");
107}
108
109SwitchValue::SwitchValue(Origin origin, Value* child)
110 : Value(CheckedOpcode, Switch, Void, One, origin, child)
111{
112}
113
114} } // namespace JSC::B3
115
116#endif // ENABLE(B3_JIT)
117