forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombineModules.rb
executable file
·251 lines (227 loc) · 7.32 KB
/
combineModules.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env ruby
# Copyright (C) 2013 Apple Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
# THE POSSIBILITY OF SUCH DAMAGE.
class Reference
attr_reader :kind, :number
def initialize(kind, number)
@kind = kind
@number = number
end
def resolve(hashTable, bangTable)
case @kind
when "#"
result = hashTable[@number]
when "!"
result = bangTable[@number]
else
raise
end
raise unless result
result
end
end
def parse(string)
result = []
until string.empty?
before, match, string = string.partition(/[!#]([0-9]+)/)
result << before
if match.empty?
result << string
break
end
result << Reference.new(match[0..0], match[1..-1].to_i)
end
result
end
class MetaData
attr_reader :index, :name, :parent, :val
def initialize(index, name, parent, val)
@index = index
@name = name
@parent = parent
@val = val
end
end
$definitions = []
$declarations = {}
$attributes = []
$metaData = {}
$attributesBackMap = {}
$count = 0
loop {
line = $stdin.readline
if line =~ /^; NOTE: THIS IS A COMBINED MODULE/
puts line
puts $stdin.read
exit 0
end
break if line =~ /^define/
}
puts "; NOTE: THIS IS A COMBINED MODULE"
# Loop over all definitions.
shouldContinue = true
while shouldContinue
# We're starting a new definition.
body = ""
loop {
line = $stdin.readline
break if line.chomp == "}"
body += line
}
body = parse(body)
declarations=[]
metaDataMap=[]
attributeMap = []
unresolvedMetaData = []
loop {
line = $stdin.gets
if not line
shouldContinue = false
break
elsif line =~ /^define/
break
elsif line =~ /^declare/
declarations << parse(line)
elsif line =~ /!([0-9]+) = metadata !{metadata !\"([a-zA-Z0-9_]+)\"}/
index = $1.to_i
name = $2
unless $metaData[name]
$metaData[name] = MetaData.new($metaData.size, name, nil, nil)
end
metaDataMap[index] = $metaData[$2].index
elsif line =~ /!([0-9]+) = metadata !{metadata !\"([a-zA-Z0-9_]+)\", metadata !([0-9]+)/
metaData = MetaData.new($1.to_i, $2, $3.to_i, nil)
unresolvedMetaData << metaData
elsif line =~ /!([0-9]+) = metadata !{metadata !\"branch_weights\"/
index = $1.to_i
arr1 = line.split(',');
arr1.shift
arr2 = Array.new
name = "branch_weights"
arr1.each { |a|
a =~ /i32 ([0-9]+)/
name.concat($1)
arr2.push($1)
}
unless $metaData[name]
$metaData[name] = MetaData.new($metaData.size, "branch_weights", nil, arr2)
end
metaDataMap[index] = $metaData[name].index
elsif line =~ /!([0-9]+) = metadata !{i32 ([-+0-9]+), i32 ([-+0-9]+)}/
index = $1.to_i
name = "#$2#$3"
unless $metaData[name]
$metaData[name] = MetaData.new($metaData.size, nil, nil, [$2, $3])
end
metaDataMap[index] = $metaData[name].index
elsif line =~ /attributes #([0-9]+) = /
attributeNumber = $1.to_i
attributeBody = $~.post_match
if $attributesBackMap[attributeBody]
attributeMap[attributeNumber] = $attributesBackMap[attributeBody]
else
attributeMap[attributeNumber] = $attributes.size
$attributesBackMap[attributeBody] = $attributes.size
$attributes << attributeBody
end
end
}
# Iteratively resolve meta-data references
until unresolvedMetaData.empty?
index = 0
while index < unresolvedMetaData.size
metaData = unresolvedMetaData[index]
if $metaData[metaData.name]
metaDataMap[metaData.index] = $metaData[metaData.name].index
unresolvedMetaData[index] = unresolvedMetaData[-1]
unresolvedMetaData.pop
elsif metaDataMap[metaData.parent]
metaDataMap[metaData.index] = $metaData.size
$metaData[metaData.name] = MetaData.new($metaData.size, metaData.name, metaDataMap[metaData.parent], nil)
unresolvedMetaData[index] = unresolvedMetaData[-1]
unresolvedMetaData.pop
else
index += 1
end
end
end
# Output the body with all of the things remapped.
puts "define i64 @jsBody_#{$count += 1}() {"
body.each {
| thing |
if thing.is_a? Reference
print(thing.kind + thing.resolve(attributeMap, metaDataMap).to_s)
else
print(thing)
end
}
puts "}"
# Figure out what to do with declarations.
declarations.each {
| declaration |
declaration = declaration.map {
| thing |
if thing.is_a? Reference
thing.kind + thing.resolve(attributeMap, metaDataMap).to_s
else
thing
end
}
declaration = declaration.join('')
next if $declarations[declaration]
$declarations[declaration] = true
}
end
$declarations.each_key {
| declaration |
puts declaration
}
$attributes.each_with_index {
| attribute, index |
puts "attributes ##{index} = #{attribute}"
}
$metaData.each_value {
| metaData |
print "!#{metaData.index} = metadata !{"
if metaData.name
print "metadata !\"#{metaData.name}\""
end
if metaData.parent
print ", metadata !#{metaData.parent}"
end
if (metaData.val)
index = 0
if metaData.name
index = 1
end
metaData.val.each { |a|
if (index == 0)
print "i32 #{a}"
else
print ", i32 #{a}"
end
index += 1
}
end
puts "}"
}