1# Copyright (C) 2011 Apple Inc. All rights reserved.
2#
3# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions
5# are met:
6# 1. Redistributions of source code must retain the above copyright
7#    notice, this list of conditions and the following disclaimer.
8# 2. Redistributions in binary form must reproduce the above copyright
9#    notice, this list of conditions and the following disclaimer in the
10#    documentation and/or other materials provided with the distribution.
11#
12# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
13# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
14# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
15# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
16# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
17# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
18# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
19# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
20# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
21# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
22# THE POSSIBILITY OF SUCH DAMAGE.
23
24require "config"
25
26# Interesting invariant, which we take advantage of: branching instructions
27# always begin with "b", and no non-branching instructions begin with "b".
28# Terminal instructions are "jmp" and "ret".
29
30MACRO_INSTRUCTIONS =
31    [
32     "addi",
33     "andi",
34     "lshifti",
35     "lshiftp",
36     "lshiftq",
37     "muli",
38     "negi",
39     "negp",
40     "negq",
41     "noti",
42     "ori",
43     "rshifti",
44     "urshifti",
45     "rshiftp",
46     "urshiftp",
47     "rshiftq",
48     "urshiftq",
49     "subi",
50     "xori",
51     "loadi",
52     "loadis",
53     "loadb",
54     "loadbs",
55     "loadh",
56     "loadhs",
57     "storei",
58     "storeb",
59     "loadd",
60     "moved",
61     "stored",
62     "addd",
63     "divd",
64     "subd",
65     "muld",
66     "sqrtd",
67     "ci2d",
68     "fii2d", # usage: fii2d <gpr with least significant bits>, <gpr with most significant bits>, <fpr>
69     "fd2ii", # usage: fd2ii <fpr>, <gpr with least significant bits>, <gpr with most significant bits>
70     "fq2d",
71     "fd2q",
72     "bdeq",
73     "bdneq",
74     "bdgt",
75     "bdgteq",
76     "bdlt",
77     "bdlteq",
78     "bdequn",
79     "bdnequn",
80     "bdgtun",
81     "bdgtequn",
82     "bdltun",
83     "bdltequn",
84     "btd2i",
85     "td2i",
86     "bcd2i",
87     "movdz",
88     "pop",
89     "push",
90     "move",
91     "sxi2q",
92     "zxi2q",
93     "nop",
94     "bieq",
95     "bineq",
96     "bia",
97     "biaeq",
98     "bib",
99     "bibeq",
100     "bigt",
101     "bigteq",
102     "bilt",
103     "bilteq",
104     "bbeq",
105     "bbneq",
106     "bba",
107     "bbaeq",
108     "bbb",
109     "bbbeq",
110     "bbgt",
111     "bbgteq",
112     "bblt",
113     "bblteq",
114     "btis",
115     "btiz",
116     "btinz",
117     "btbs",
118     "btbz",
119     "btbnz",
120     "jmp",
121     "baddio",
122     "baddis",
123     "baddiz",
124     "baddinz",
125     "bsubio",
126     "bsubis",
127     "bsubiz",
128     "bsubinz",
129     "bmulio",
130     "bmulis",
131     "bmuliz",
132     "bmulinz",
133     "borio",
134     "boris",
135     "boriz",
136     "borinz",
137     "break",
138     "call",
139     "ret",
140     "cbeq",
141     "cbneq",
142     "cba",
143     "cbaeq",
144     "cbb",
145     "cbbeq",
146     "cbgt",
147     "cbgteq",
148     "cblt",
149     "cblteq",
150     "cieq",
151     "cineq",
152     "cia",
153     "ciaeq",
154     "cib",
155     "cibeq",
156     "cigt",
157     "cigteq",
158     "cilt",
159     "cilteq",
160     "tis",
161     "tiz",
162     "tinz",
163     "tbs",
164     "tbz",
165     "tbnz",
166     "tps",
167     "tpz",
168     "tpnz",
169     "peek",
170     "poke",
171     "bpeq",
172     "bpneq",
173     "bpa",
174     "bpaeq",
175     "bpb",
176     "bpbeq",
177     "bpgt",
178     "bpgteq",
179     "bplt",
180     "bplteq",
181     "addp",
182     "mulp",
183     "andp",
184     "orp",
185     "subp",
186     "xorp",
187     "loadp",
188     "cpeq",
189     "cpneq",
190     "cpa",
191     "cpaeq",
192     "cpb",
193     "cpbeq",
194     "cpgt",
195     "cpgteq",
196     "cplt",
197     "cplteq",
198     "storep",
199     "btps",
200     "btpz",
201     "btpnz",
202     "baddpo",
203     "baddps",
204     "baddpz",
205     "baddpnz",
206     "tqs",
207     "tqz",
208     "tqnz",
209     "peekq",
210     "pokeq",
211     "bqeq",
212     "bqneq",
213     "bqa",
214     "bqaeq",
215     "bqb",
216     "bqbeq",
217     "bqgt",
218     "bqgteq",
219     "bqlt",
220     "bqlteq",
221     "addq",
222     "mulq",
223     "andq",
224     "orq",
225     "subq",
226     "xorq",
227     "loadq",
228     "cqeq",
229     "cqneq",
230     "cqa",
231     "cqaeq",
232     "cqb",
233     "cqbeq",
234     "cqgt",
235     "cqgteq",
236     "cqlt",
237     "cqlteq",
238     "storeq",
239     "btqs",
240     "btqz",
241     "btqnz",
242     "baddqo",
243     "baddqs",
244     "baddqz",
245     "baddqnz",
246     "bo",
247     "bs",
248     "bz",
249     "bnz",
250     "leai",
251     "leap",
252    ]
253
254X86_INSTRUCTIONS =
255    [
256     "cdqi",
257     "idivi",
258     "resetX87Stack"
259    ]
260
261ARM_INSTRUCTIONS =
262    [
263     "smulli",
264     "addis",
265     "subis",
266     "oris"
267    ]
268
269MIPS_INSTRUCTIONS =
270    [
271    "movz",
272    "movn",
273    "slt",
274    "sltu",
275    "pichdr",
276    "pichdrra"
277    ]
278
279SH4_INSTRUCTIONS =
280    [
281    "shllx",
282    "shlrx",
283    "shld",
284    "shad",
285    "bdnan",
286    "loaddReversedAndIncrementAddress",
287    "storedReversedAndDecrementAddress",
288    "ldspr",
289    "stspr",
290    "callf",
291    "jmpf"
292    ]
293
294CXX_INSTRUCTIONS =
295    [
296     "cloopCrash",           # no operands
297     "cloopCallJSFunction",  # operands: callee
298     "cloopCallNative",      # operands: callee
299     "cloopCallSlowPath",    # operands: callTarget, currentFrame, currentPC
300
301     # For debugging only:
302     # Takes no operands but simply emits whatever follows in // comments as
303     # a line of C++ code in the generated LLIntAssembly.h file. This can be
304     # used to insert instrumentation into the interpreter loop to inspect
305     # variables of interest. Do not leave these instructions in production
306     # code.
307     "cloopDo",              # no operands
308    ]
309
310INSTRUCTIONS = MACRO_INSTRUCTIONS + X86_INSTRUCTIONS + ARM_INSTRUCTIONS + MIPS_INSTRUCTIONS + SH4_INSTRUCTIONS + CXX_INSTRUCTIONS
311
312INSTRUCTION_PATTERN = Regexp.new('\\A((' + INSTRUCTIONS.join(')|(') + '))\\Z')
313
314def isBranch(instruction)
315    instruction =~ /^b/
316end
317
318def hasFallThrough(instruction)
319    instruction != "ret" and instruction != "jmp"
320end
321
322