1/*
2 * Copyright (C) 2012-2018 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#include "CodeLocation.h"
29#include <wtf/Forward.h>
30
31namespace JSC {
32
33enum class HandlerType {
34 Catch = 0,
35 Finally = 1,
36 SynthesizedCatch = 2,
37 SynthesizedFinally = 3
38};
39
40enum class RequiredHandler {
41 CatchHandler,
42 AnyHandler
43};
44
45struct HandlerInfoBase {
46 HandlerType type() const { return static_cast<HandlerType>(typeBits); }
47 void setType(HandlerType type) { typeBits = static_cast<uint32_t>(type); }
48
49 const char* typeName() const
50 {
51 switch (type()) {
52 case HandlerType::Catch:
53 return "catch";
54 case HandlerType::Finally:
55 return "finally";
56 case HandlerType::SynthesizedCatch:
57 return "synthesized catch";
58 case HandlerType::SynthesizedFinally:
59 return "synthesized finally";
60 default:
61 ASSERT_NOT_REACHED();
62 }
63 return nullptr;
64 }
65
66 bool isCatchHandler() const { return type() == HandlerType::Catch; }
67
68 template<typename Handler>
69 static Handler* handlerForIndex(Vector<Handler>& exeptionHandlers, unsigned index, RequiredHandler requiredHandler)
70 {
71 for (Handler& handler : exeptionHandlers) {
72 if ((requiredHandler == RequiredHandler::CatchHandler) && !handler.isCatchHandler())
73 continue;
74
75 // Handlers are ordered innermost first, so the first handler we encounter
76 // that contains the source address is the correct handler to use.
77 // This index used is either the BytecodeOffset or a CallSiteIndex.
78 if (handler.start <= index && handler.end > index)
79 return &handler;
80 }
81
82 return nullptr;
83 }
84
85 uint32_t start;
86 uint32_t end;
87 uint32_t target;
88 uint32_t typeBits : 2; // HandlerType
89};
90
91struct UnlinkedHandlerInfo : public HandlerInfoBase {
92 UnlinkedHandlerInfo()
93 {
94 }
95
96 UnlinkedHandlerInfo(uint32_t start, uint32_t end, uint32_t target, HandlerType handlerType)
97 {
98 this->start = start;
99 this->end = end;
100 this->target = target;
101 setType(handlerType);
102 ASSERT(type() == handlerType);
103 }
104};
105
106struct HandlerInfo : public HandlerInfoBase {
107 void initialize(const UnlinkedHandlerInfo& unlinkedInfo)
108 {
109 start = unlinkedInfo.start;
110 end = unlinkedInfo.end;
111 target = unlinkedInfo.target;
112 typeBits = unlinkedInfo.typeBits;
113 }
114
115#if ENABLE(JIT)
116 void initialize(const UnlinkedHandlerInfo& unlinkedInfo, CodeLocationLabel<ExceptionHandlerPtrTag> label)
117 {
118 initialize(unlinkedInfo);
119 nativeCode = label;
120 }
121
122 CodeLocationLabel<ExceptionHandlerPtrTag> nativeCode;
123#endif
124};
125
126} // namespace JSC
127