1 | # Copyright (C) 2011-2022 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 |
|
---|
24 | require "config"
|
---|
25 | require "arm"
|
---|
26 | require "arm64"
|
---|
27 | require "ast"
|
---|
28 | require "x86"
|
---|
29 | require "mips"
|
---|
30 | require "riscv64"
|
---|
31 | require "cloop"
|
---|
32 |
|
---|
33 | begin
|
---|
34 | require "arm64e"
|
---|
35 | rescue LoadError
|
---|
36 | end
|
---|
37 |
|
---|
38 | BACKENDS =
|
---|
39 | [
|
---|
40 | "X86",
|
---|
41 | "X86_WIN",
|
---|
42 | "X86_64",
|
---|
43 | "X86_64_WIN",
|
---|
44 | "ARMv7",
|
---|
45 | "ARM64",
|
---|
46 | "ARM64E",
|
---|
47 | "MIPS",
|
---|
48 | "RISCV64",
|
---|
49 | "C_LOOP",
|
---|
50 | "C_LOOP_WIN"
|
---|
51 | ]
|
---|
52 |
|
---|
53 | # Keep the set of working backends separate from the set of backends that might be
|
---|
54 | # supported. This is great because the BACKENDS list is almost like a reserved
|
---|
55 | # words list, in that it causes settings resolution to treat those words specially.
|
---|
56 | # Hence this lets us set aside the name of a backend we might want to support in
|
---|
57 | # the future while not actually supporting the backend yet.
|
---|
58 | WORKING_BACKENDS =
|
---|
59 | [
|
---|
60 | "X86",
|
---|
61 | "X86_WIN",
|
---|
62 | "X86_64",
|
---|
63 | "X86_64_WIN",
|
---|
64 | "ARMv7",
|
---|
65 | "ARM64",
|
---|
66 | "ARM64E",
|
---|
67 | "MIPS",
|
---|
68 | "RISCV64",
|
---|
69 | "C_LOOP",
|
---|
70 | "C_LOOP_WIN"
|
---|
71 | ]
|
---|
72 |
|
---|
73 | BACKEND_PATTERN = Regexp.new('\\A(' + BACKENDS.join(')|(') + ')\\Z')
|
---|
74 |
|
---|
75 | $allBackends = {}
|
---|
76 | $validBackends = {}
|
---|
77 | BACKENDS.each {
|
---|
78 | | backend |
|
---|
79 | $validBackends[backend] = true
|
---|
80 | $allBackends[backend] = true
|
---|
81 | }
|
---|
82 |
|
---|
83 | def canonicalizeBackendNames(backendNames)
|
---|
84 | newBackendNames = []
|
---|
85 | backendNames.each {
|
---|
86 | | backendName |
|
---|
87 | backendName = backendName.upcase
|
---|
88 | if backendName =~ /ARM.*/
|
---|
89 | backendName.sub!(/ARMV7([KS]?)(.*)/) { | _ | 'ARMv7' + $1.downcase + $2 }
|
---|
90 | backendName = "ARM64" if backendName == "ARM64_32"
|
---|
91 | end
|
---|
92 | backendName = "X86" if backendName == "I386"
|
---|
93 | newBackendNames << backendName
|
---|
94 | newBackendNames << "ARMv7" if backendName.start_with?("ARMv7")
|
---|
95 | }
|
---|
96 | newBackendNames.uniq
|
---|
97 | end
|
---|
98 |
|
---|
99 | def includeOnlyBackends(list)
|
---|
100 | newValidBackends = {}
|
---|
101 | list.each {
|
---|
102 | | backend |
|
---|
103 | if $validBackends[backend]
|
---|
104 | newValidBackends[backend] = true
|
---|
105 | end
|
---|
106 | }
|
---|
107 | $validBackends = newValidBackends
|
---|
108 | end
|
---|
109 |
|
---|
110 | def isBackend?(backend)
|
---|
111 | $allBackends[backend]
|
---|
112 | end
|
---|
113 |
|
---|
114 | def isValidBackend?(backend)
|
---|
115 | $validBackends[backend]
|
---|
116 | end
|
---|
117 |
|
---|
118 | def validBackends
|
---|
119 | $validBackends.keys
|
---|
120 | end
|
---|
121 |
|
---|
122 | class LoweringError < StandardError
|
---|
123 | attr_reader :originString
|
---|
124 |
|
---|
125 | def initialize(e, originString)
|
---|
126 | super "#{e} (due to #{originString})"
|
---|
127 | @originString = originString
|
---|
128 | set_backtrace e.backtrace
|
---|
129 | end
|
---|
130 | end
|
---|
131 |
|
---|
132 | class Node
|
---|
133 | def lower(name)
|
---|
134 | begin
|
---|
135 | $activeBackend = name
|
---|
136 | send("prepareToLower", name)
|
---|
137 | send("lower#{name}")
|
---|
138 | rescue => e
|
---|
139 | raise LoweringError.new(e, codeOriginString)
|
---|
140 | end
|
---|
141 | end
|
---|
142 | end
|
---|
143 |
|
---|
144 | # Overrides for lower() for those nodes that are backend-agnostic
|
---|
145 |
|
---|
146 | class Label
|
---|
147 | def lower(name)
|
---|
148 | $asm.debugAnnotation codeOrigin.debugDirective if $enableDebugAnnotations
|
---|
149 | $asm.putsLabel(self.name[1..-1], @global)
|
---|
150 | end
|
---|
151 | end
|
---|
152 |
|
---|
153 | class LocalLabel
|
---|
154 | def lower(name)
|
---|
155 | $asm.putsLocalLabel "jsc_llint_#{self.name[1..-1]}"
|
---|
156 | end
|
---|
157 | end
|
---|
158 |
|
---|
159 | class LabelReference
|
---|
160 | def asmLabel
|
---|
161 | if extern?
|
---|
162 | Assembler.externLabelReference(name[1..-1])
|
---|
163 | else
|
---|
164 | Assembler.labelReference(name[1..-1])
|
---|
165 | end
|
---|
166 | end
|
---|
167 |
|
---|
168 | def cLabel
|
---|
169 | Assembler.cLabelReference(name[1..-1])
|
---|
170 | end
|
---|
171 | end
|
---|
172 |
|
---|
173 | class LocalLabelReference
|
---|
174 | def asmLabel
|
---|
175 | Assembler.localLabelReference("jsc_llint_"+name[1..-1])
|
---|
176 | end
|
---|
177 |
|
---|
178 | def cLabel
|
---|
179 | Assembler.cLocalLabelReference("jsc_llint_"+name[1..-1])
|
---|
180 | end
|
---|
181 | end
|
---|
182 |
|
---|
183 | class Skip
|
---|
184 | def lower(name)
|
---|
185 | end
|
---|
186 | end
|
---|
187 |
|
---|
188 | class Sequence
|
---|
189 | def lower(name)
|
---|
190 | $activeBackend = name
|
---|
191 | if respond_to? "getModifiedList#{name}"
|
---|
192 | newList = send("getModifiedList#{name}")
|
---|
193 | newList.each {
|
---|
194 | | node |
|
---|
195 | node.lower(name)
|
---|
196 | }
|
---|
197 | elsif respond_to? "lower#{name}"
|
---|
198 | send("lower#{name}")
|
---|
199 | else
|
---|
200 | @list.each {
|
---|
201 | | node |
|
---|
202 | node.lower(name)
|
---|
203 | }
|
---|
204 | end
|
---|
205 | end
|
---|
206 | end
|
---|
207 |
|
---|