1/*
2 * Copyright (C) 2017-2019 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#if ENABLE(MASM_PROBE)
30
31#include "AirInst.h"
32#include "AirSpecial.h"
33#include "MacroAssemblerPrinter.h"
34
35namespace JSC {
36namespace Printer {
37
38typedef Vector<B3::Air::Arg> ArgList;
39
40// IsSameOrReference::value is true if T is the same type as U or U&. Else, it is false.
41
42template<typename T, typename U>
43static constexpr auto IsSameOrReferenceHelper(int) -> std::enable_if_t<std::is_same<T, U>::value || std::is_same<T, U&>::value, std::true_type>;
44
45template<typename T, typename U>
46static constexpr std::false_type IsSameOrReferenceHelper(...);
47
48template<class T, typename U>
49struct IsSameOrReference : public std::is_same<decltype(IsSameOrReferenceHelper<T, U>(0)), std::true_type> { };
50
51
52template<typename T, typename... Arguments, typename = std::enable_if_t<IsSameOrReference<T, B3::Air::Tmp>::value || IsSameOrReference<T, Reg>::value>>
53inline void appendAirArg(B3::Air::Inst& inst, T&& arg)
54{
55 inst.args.append(std::forward<T>(arg));
56}
57
58template<typename T, typename... Arguments, typename = std::enable_if_t<!IsSameOrReference<T, B3::Air::Tmp>::value && !IsSameOrReference<T, Reg>::value>>
59inline void appendAirArg(B3::Air::Inst&, T&&, int = 0) { }
60
61inline void appendAirArgs(B3::Air::Inst&) { }
62
63template<typename T, typename... Arguments>
64inline void appendAirArgs(B3::Air::Inst& inst, T&& t, Arguments&&... others)
65{
66 appendAirArg(inst, std::forward<T>(t));
67 appendAirArgs(inst, std::forward<Arguments>(others)...);
68}
69
70void printAirArg(PrintStream&, Context&);
71
72// Printer<Arg&> is only a place-holder which PrintSpecial::generate() will later
73// replace with a Printer for a register, constant, etc. as appropriate.
74template<>
75struct Printer<B3::Air::Tmp> : public PrintRecord {
76 Printer(B3::Air::Tmp&)
77 : PrintRecord(printAirArg)
78 { }
79};
80
81template<>
82struct Printer<Reg> : public PrintRecord {
83 Printer(Reg&)
84 : PrintRecord(printAirArg)
85 { }
86};
87
88} // namespace Printer
89
90namespace B3 { namespace Air {
91
92class PrintSpecial : public Special {
93public:
94 PrintSpecial(Printer::PrintRecordList*);
95 ~PrintSpecial();
96
97 // You cannot use this register to pass arguments. It just so happens that this register is not
98 // used for arguments in the C calling convention. By the way, this is the only thing that causes
99 // this special to be specific to C calls.
100 static constexpr GPRReg scratchRegister = GPRInfo::nonArgGPR0;
101
102protected:
103 void forEachArg(Inst&, const ScopedLambda<Inst::EachArgCallback>&) final;
104 bool isValid(Inst&) final;
105 bool admitsStack(Inst&, unsigned argIndex) final;
106 bool admitsExtendedOffsetAddr(Inst&, unsigned) final;
107 void reportUsedRegisters(Inst&, const RegisterSet&) final;
108 CCallHelpers::Jump generate(Inst&, CCallHelpers&, GenerationContext&) final;
109 RegisterSet extraEarlyClobberedRegs(Inst&) final;
110 RegisterSet extraClobberedRegs(Inst&) final;
111
112 void dumpImpl(PrintStream&) const final;
113 void deepDumpImpl(PrintStream&) const final;
114
115private:
116 static constexpr unsigned specialArgOffset = 0;
117 static constexpr unsigned numSpecialArgs = 1;
118 static constexpr unsigned calleeArgOffset = numSpecialArgs;
119 static constexpr unsigned numCalleeArgs = 1;
120 static constexpr unsigned returnGPArgOffset = numSpecialArgs + numCalleeArgs;
121 static constexpr unsigned numReturnGPArgs = 2;
122 static constexpr unsigned returnFPArgOffset = numSpecialArgs + numCalleeArgs + numReturnGPArgs;
123 static constexpr unsigned numReturnFPArgs = 1;
124 static constexpr unsigned argArgOffset =
125 numSpecialArgs + numCalleeArgs + numReturnGPArgs + numReturnFPArgs;
126
127 Printer::PrintRecordList* m_printRecordList;
128};
129
130} } } // namespace JSC::B3::Air
131
132#endif // ENABLE(MASM_PROBE)
133#endif // ENABLE(B3_JIT)
134