]> git.saurik.com Git - apple/javascriptcore.git/blob - offlineasm/generate_offset_extractor.rb
b2a8c2c832be9f648cd01a214d73a1354c1293f1
[apple/javascriptcore.git] / offlineasm / generate_offset_extractor.rb
1 #!/usr/bin/env ruby
2
3 # Copyright (C) 2011 Apple Inc. All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
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.
13 #
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.
25
26 $: << File.dirname(__FILE__)
27
28 require "backends"
29 require "digest/sha1"
30 require "offsets"
31 require "parser"
32 require "self_hash"
33 require "settings"
34 require "transform"
35
36 inputFlnm = ARGV.shift
37 outputFlnm = ARGV.shift
38
39 $stderr.puts "offlineasm: Parsing #{inputFlnm} and creating offset extractor #{outputFlnm}."
40
41 def emitMagicNumber
42 OFFSET_MAGIC_NUMBERS.each {
43 | number |
44 $output.puts "#{number},"
45 }
46 end
47
48 inputHash = "// offlineasm input hash: #{parseHash(inputFlnm)} #{selfHash}"
49
50 if FileTest.exist? outputFlnm
51 File.open(outputFlnm, "r") {
52 | inp |
53 firstLine = inp.gets
54 if firstLine and firstLine.chomp == inputHash
55 $stderr.puts "offlineasm: Nothing changed."
56 exit 0
57 end
58 }
59 end
60
61 originalAST = parse(inputFlnm)
62
63 #
64 # Optimize the AST to make configuration extraction faster. This reduces the AST to a form
65 # that only contains the things that matter for our purposes: offsets, sizes, and if
66 # statements.
67 #
68
69 class Node
70 def offsetsPruneTo(sequence)
71 children.each {
72 | child |
73 child.offsetsPruneTo(sequence)
74 }
75 end
76
77 def offsetsPrune
78 result = Sequence.new(codeOrigin, [])
79 offsetsPruneTo(result)
80 result
81 end
82 end
83
84 class IfThenElse
85 def offsetsPruneTo(sequence)
86 ifThenElse = IfThenElse.new(codeOrigin, predicate, thenCase.offsetsPrune)
87 ifThenElse.elseCase = elseCase.offsetsPrune
88 sequence.list << ifThenElse
89 end
90 end
91
92 class StructOffset
93 def offsetsPruneTo(sequence)
94 sequence.list << self
95 end
96 end
97
98 class Sizeof
99 def offsetsPruneTo(sequence)
100 sequence.list << self
101 end
102 end
103
104 prunedAST = originalAST.offsetsPrune
105
106 File.open(outputFlnm, "w") {
107 | outp |
108 $output = outp
109 outp.puts inputHash
110 length = 0
111 emitCodeInAllConfigurations(prunedAST) {
112 | settings, ast, backend, index |
113 offsetsList = ast.filter(StructOffset).uniq.sort
114 sizesList = ast.filter(Sizeof).uniq.sort
115 length += OFFSET_HEADER_MAGIC_NUMBERS.size + (OFFSET_MAGIC_NUMBERS.size + 1) * (1 + offsetsList.size + sizesList.size)
116 }
117 outp.puts "static const unsigned extractorTable[#{length}] = {"
118 emitCodeInAllConfigurations(prunedAST) {
119 | settings, ast, backend, index |
120 OFFSET_HEADER_MAGIC_NUMBERS.each {
121 | number |
122 $output.puts "#{number},"
123 }
124
125 offsetsList = ast.filter(StructOffset).uniq.sort
126 sizesList = ast.filter(Sizeof).uniq.sort
127
128 emitMagicNumber
129 outp.puts "#{index},"
130 offsetsList.each {
131 | offset |
132 emitMagicNumber
133 outp.puts "OFFLINE_ASM_OFFSETOF(#{offset.struct}, #{offset.field}),"
134 }
135 sizesList.each {
136 | offset |
137 emitMagicNumber
138 outp.puts "sizeof(#{offset.struct}),"
139 }
140 }
141 outp.puts "};"
142 }
143
144 $stderr.puts "offlineasm: offset extractor #{outputFlnm} successfully generated."
145