3 # Copyright (C) 2013 Apple Inc. All rights reserved.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
8 # 1. Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # 2. Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
14 # THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
15 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16 # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
18 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24 # THE POSSIBILITY OF SUCH DAMAGE.
27 attr_reader :kind, :number
29 def initialize(kind, number)
34 def resolve(hashTable, bangTable)
37 result = hashTable[@number]
39 result = bangTable[@number]
51 before, match, string = string.partition(/[!#]([0-9]+)/)
57 result << Reference.new(match[0..0], match[1..-1].to_i)
63 attr_reader :index, :name, :parent, :val
65 def initialize(index, name, parent, val)
77 $attributesBackMap = {}
81 line = $stdin.readline
82 if line =~ /^; NOTE: THIS IS A COMBINED MODULE/
87 break if line =~ /^define/
90 puts "; NOTE: THIS IS A COMBINED MODULE"
92 # Loop over all definitions.
95 # We're starting a new definition.
98 line = $stdin.readline
99 break if line.chomp == "}"
108 unresolvedMetaData = []
114 shouldContinue = false
116 elsif line =~ /^define/
118 elsif line =~ /^declare/
119 declarations << parse(line)
120 elsif line =~ /!([0-9]+) = metadata !{metadata !\"([a-zA-Z0-9_]+)\"}/
123 unless $metaData[name]
124 $metaData[name] = MetaData.new($metaData.size, name, nil, nil)
126 metaDataMap[index] = $metaData[$2].index
127 elsif line =~ /!([0-9]+) = metadata !{metadata !\"([a-zA-Z0-9_]+)\", metadata !([0-9]+)/
128 metaData = MetaData.new($1.to_i, $2, $3.to_i, nil)
129 unresolvedMetaData << metaData
130 elsif line =~ /!([0-9]+) = metadata !{metadata !\"branch_weights\"/
132 arr1 = line.split(',');
135 name = "branch_weights"
141 unless $metaData[name]
142 $metaData[name] = MetaData.new($metaData.size, "branch_weights", nil, arr2)
144 metaDataMap[index] = $metaData[name].index
145 elsif line =~ /!([0-9]+) = metadata !{i32 ([-+0-9]+), i32 ([-+0-9]+)}/
148 unless $metaData[name]
149 $metaData[name] = MetaData.new($metaData.size, nil, nil, [$2, $3])
151 metaDataMap[index] = $metaData[name].index
152 elsif line =~ /attributes #([0-9]+) = /
153 attributeNumber = $1.to_i
154 attributeBody = $~.post_match
155 if $attributesBackMap[attributeBody]
156 attributeMap[attributeNumber] = $attributesBackMap[attributeBody]
158 attributeMap[attributeNumber] = $attributes.size
159 $attributesBackMap[attributeBody] = $attributes.size
160 $attributes << attributeBody
165 # Iteratively resolve meta-data references
166 until unresolvedMetaData.empty?
168 while index < unresolvedMetaData.size
169 metaData = unresolvedMetaData[index]
170 if $metaData[metaData.name]
171 metaDataMap[metaData.index] = $metaData[metaData.name].index
172 unresolvedMetaData[index] = unresolvedMetaData[-1]
173 unresolvedMetaData.pop
174 elsif metaDataMap[metaData.parent]
175 metaDataMap[metaData.index] = $metaData.size
176 $metaData[metaData.name] = MetaData.new($metaData.size, metaData.name, metaDataMap[metaData.parent], nil)
177 unresolvedMetaData[index] = unresolvedMetaData[-1]
178 unresolvedMetaData.pop
185 # Output the body with all of the things remapped.
186 puts "define i64 @jsBody_#{$count += 1}() {"
189 if thing.is_a? Reference
190 print(thing.kind + thing.resolve(attributeMap, metaDataMap).to_s)
197 # Figure out what to do with declarations.
200 declaration = declaration.map {
202 if thing.is_a? Reference
203 thing.kind + thing.resolve(attributeMap, metaDataMap).to_s
208 declaration = declaration.join('')
210 next if $declarations[declaration]
212 $declarations[declaration] = true
216 $declarations.each_key {
221 $attributes.each_with_index {
223 puts "attributes ##{index} = #{attribute}"
226 $metaData.each_value {
228 print "!#{metaData.index} = metadata !{"
230 print "metadata !\"#{metaData.name}\""
233 print ", metadata !#{metaData.parent}"
240 metaData.val.each { |a|