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