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
65 def initialize(index, name, parent)
76 $attributesBackMap = {}
80 line = $stdin.readline
81 break if line =~ /^define/
84 # Loop over all definitions.
87 # We're starting a new definition.
90 line = $stdin.readline
91 break if line.chomp == "}"
100 unresolvedMetaData = []
106 shouldContinue = false
108 elsif line =~ /^define/
110 elsif line =~ /^declare/
111 declarations << parse(line)
112 elsif line =~ /!([0-9]+) = metadata !{metadata !\"([a-zA-Z0-9_]+)\"}/
115 unless $metaData[name]
116 $metaData[name] = MetaData.new($metaData.size, name, nil)
118 metaDataMap[index] = $metaData[$2].index
119 elsif line =~ /!([0-9]+) = metadata !{metadata !\"([a-zA-Z0-9_]+)\", metadata !([0-9]+)/
120 metaData = MetaData.new($1.to_i, $2, $3.to_i)
121 unresolvedMetaData << metaData
122 elsif line =~ /attributes #([0-9]+) = /
123 attributeNumber = $1.to_i
124 attributeBody = $~.post_match
125 if $attributesBackMap[attributeBody]
126 attributeMap[attributeNumber] = $attributesBackMap[attributeBody]
128 attributeMap[attributeNumber] = $attributes.size
129 $attributesBackMap[attributeBody] = $attributes.size
130 $attributes << attributeBody
135 # Iteratively resolve meta-data references
136 until unresolvedMetaData.empty?
138 while index < unresolvedMetaData.size
139 metaData = unresolvedMetaData[index]
140 if $metaData[metaData.name]
141 metaDataMap[metaData.index] = $metaData[metaData.name].index
142 unresolvedMetaData[index] = unresolvedMetaData[-1]
143 unresolvedMetaData.pop
144 elsif metaDataMap[metaData.parent]
145 metaDataMap[metaData.index] = $metaData.size
146 $metaData[metaData.name] = MetaData.new($metaData.size, metaData.name, metaDataMap[metaData.parent])
147 unresolvedMetaData[index] = unresolvedMetaData[-1]
148 unresolvedMetaData.pop
155 # Output the body with all of the things remapped.
156 puts "define i64 @jsBody_#{$count += 1}(i64) {"
159 if thing.is_a? Reference
160 print(thing.kind + thing.resolve(attributeMap, metaDataMap).to_s)
167 # Figure out what to do with declarations.
170 declaration = declaration.map {
172 if thing.is_a? Reference
173 thing.kind + thing.resolve(attributeMap, metaDataMap).to_s
178 declaration = declaration.join('')
180 next if $declarations[declaration]
182 $declarations[declaration] = true
186 $declarations.each_key {
191 $attributes.each_with_index {
193 puts "attributes ##{index} = #{attribute}"
196 $metaData.each_value {
198 print "!#{metaData.index} = metadata !{metadata !\"#{metaData.name}\""
200 print ", metadata !#{metaData.parent}"