a3d70a6653a07eefb1b528c0d5fcd77ddcba9ffa
[WebKit-https.git] / Source / JavaScriptCore / offlineasm / generate_offset_extractor.rb
1 #!/usr/bin/env ruby
2
3 # Copyright (C) 2011-2018 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 "config"
29 require "backends"
30 require "digest/sha1"
31 require "offsets"
32 require "parser"
33 require "self_hash"
34 require "settings"
35 require "transform"
36
37 IncludeFile.processIncludeOptions()
38
39 inputFlnm = ARGV.shift
40 settingsFlnm = ARGV.shift
41 outputFlnm = ARGV.shift
42
43 validBackends = canonicalizeBackendNames(ARGV.shift.split(/[,\s]+/))
44 includeOnlyBackends(validBackends)
45
46 begin
47     configurationList = configurationIndices(settingsFlnm)
48 rescue MissingMagicValuesException
49     $stderr.puts "OffsetExtractor: No magic values found. Skipping offsets extractor file generation."
50     exit 1
51 end
52
53 def emitMagicNumber
54     OFFSET_MAGIC_NUMBERS.each {
55         | number |
56         $output.puts "unsigned(#{number}),"
57     }
58 end
59
60 configurationHash = Digest::SHA1.hexdigest(configurationList.join(' '))
61 inputHash = "// OffsetExtractor input hash: #{parseHash(inputFlnm)} #{configurationHash} #{selfHash}"
62
63 if FileTest.exist? outputFlnm
64     File.open(outputFlnm, "r") {
65         | inp |
66         firstLine = inp.gets
67         if firstLine and firstLine.chomp == inputHash
68             $stderr.puts "OffsetExtractor: Nothing changed."
69             exit 0
70         end
71     }
72 end
73
74 ast = parse(inputFlnm)
75 settingsCombinations = computeSettingsCombinations(ast)
76
77 File.open(outputFlnm, "w") {
78     | outp |
79     $output = outp
80     outp.puts inputHash
81
82     outp.puts "static const int64_t offsetExtractorTable[] = {"
83     configurationList.each {
84         | configIndex |
85         forSettings(settingsCombinations[configIndex], ast) {
86             | concreteSettings, lowLevelAST, backend |
87
88             offsetsList = offsetsList(lowLevelAST)
89             sizesList = sizesList(lowLevelAST)
90             constsList = constsList(lowLevelAST)
91
92             emitCodeInConfiguration(concreteSettings, lowLevelAST, backend) {
93                 OFFSET_HEADER_MAGIC_NUMBERS.each {
94                     | number |
95                     outp.puts "unsigned(#{number}),"
96                 }
97
98                 emitMagicNumber
99                 outp.puts "#{configIndex},"
100                 offsetsList.each {
101                     | offset |
102                     emitMagicNumber
103                     outp.puts "OFFLINE_ASM_OFFSETOF(#{offset.struct}, #{offset.field}),"
104                 }
105                 sizesList.each {
106                     | sizeof |
107                     emitMagicNumber
108                     outp.puts "sizeof(#{sizeof.struct}),"
109                 }
110                 constsList.each {
111                     | const |
112                     emitMagicNumber
113                     outp.puts "static_cast<int64_t>(#{const.value}),"
114                 }
115             }
116         }
117     }
118     outp.puts "};"
119
120 }