gcmole.lua revision 537ba893e2530051ec7f296e769fdd37bb4ae4a0
1e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET-- Copyright 2011 the V8 project authors. All rights reserved.
2e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET-- Redistribution and use in source and binary forms, with or without
3e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET-- modification, are permitted provided that the following conditions are
4e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET-- met:
5e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET--
6e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET--     * Redistributions of source code must retain the above copyright
7e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET--       notice, this list of conditions and the following disclaimer.
8e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET--     * Redistributions in binary form must reproduce the above
9e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET--       copyright notice, this list of conditions and the following
10e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET--       disclaimer in the documentation and/or other materials provided
11e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET--       with the distribution.
12e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET--     * Neither the name of Google Inc. nor the names of its
13e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET--       contributors may be used to endorse or promote products derived
14e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET--       from this software without specific prior written permission.
15e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET--
16e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET-- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET-- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET-- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET-- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET-- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET-- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET-- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET-- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET-- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
28e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET-- This is main driver for gcmole tool. See README for more details.
29e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET-- Usage: CLANG_BIN=clang-bin-dir lua tools/gcmole/gcmole.lua [arm|ia32|x64]
30e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
31e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETlocal DIR = arg[0]:match("^(.+)/[^/]+$")
32e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
33e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETlocal FLAGS = {
34e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   -- Do not build gcsuspects file and reuse previously generated one.
35e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   reuse_gcsuspects = false;
36e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
37e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   -- Don't use parallel python runner.
38e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   sequential = false;
39e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
40e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   -- Print commands to console before executing them.
41e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   verbose = false;
42e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
43e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   -- Perform dead variable analysis (generates many false positives).
44e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   -- TODO add some sort of whiteliste to filter out false positives.
45e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   dead_vars = false;
46e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
47e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   -- When building gcsuspects whitelist certain functions as if they
48e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   -- can be causing GC. Currently used to reduce number of false
49e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   -- positives in dead variables analysis. See TODO for WHITELIST
50e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   -- below.
51e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   whitelist = true;
52e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET}
53e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETlocal ARGS = {}
54e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
55e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETfor i = 1, #arg do
56e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   local flag = arg[i]:match "^%-%-([%w_-]+)$"
57e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   if flag then
58e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      local no, real_flag = flag:match "^(no)([%w_-]+)$"
59e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      if real_flag then flag = real_flag end
60e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
61e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      flag = flag:gsub("%-", "_")
62e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      if FLAGS[flag] ~= nil then
63e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET         FLAGS[flag] = (no ~= "no")
64e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      else
65e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET         error("Unknown flag: " .. flag)
66e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      end
67e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   else
68e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      table.insert(ARGS, arg[i])
69e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   end
70e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETend
71e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
72e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETlocal ARCHS = ARGS[1] and { ARGS[1] } or { 'ia32', 'arm', 'x64', 'arm64' }
73e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
74e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETlocal io = require "io"
75e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETlocal os = require "os"
76e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
77e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETfunction log(...)
78e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   io.stderr:write(string.format(...))
79e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   io.stderr:write "\n"
80e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETend
81e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
82e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET-------------------------------------------------------------------------------
83e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET-- Clang invocation
84e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
85e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETlocal CLANG_BIN = os.getenv "CLANG_BIN"
86e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETlocal CLANG_PLUGINS = os.getenv "CLANG_PLUGINS"
87e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
88e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETif not CLANG_BIN or CLANG_BIN == "" then
89e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   error "CLANG_BIN not set"
90e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETend
91e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
92e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETif not CLANG_PLUGINS or CLANG_PLUGINS == "" then
93e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   CLANG_PLUGINS = DIR
94e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETend
95e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
96e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETlocal function MakeClangCommandLine(
97e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      plugin, plugin_args, triple, arch_define, arch_options)
98e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   if plugin_args then
99e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET     for i = 1, #plugin_args do
100e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET        plugin_args[i] = "-Xclang -plugin-arg-" .. plugin
101e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET           .. " -Xclang " .. plugin_args[i]
102e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET     end
103e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET     plugin_args = " " .. table.concat(plugin_args, " ")
104e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   end
105e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   return CLANG_BIN .. "/clang++ -std=c++11 -c "
106e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      .. " -Xclang -load -Xclang " .. CLANG_PLUGINS .. "/libgcmole.so"
107e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      .. " -Xclang -plugin -Xclang "  .. plugin
108e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      .. (plugin_args or "")
109e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      .. " -Xclang -triple -Xclang " .. triple
110e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      .. " -D" .. arch_define
111e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      .. " -DENABLE_DEBUGGER_SUPPORT"
112e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      .. " -DV8_I18N_SUPPORT"
113e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      .. " -I./"
114e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      .. " -Iinclude/"
115e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      .. " -Ithird_party/icu/source/common"
116e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      .. " -Ithird_party/icu/source/i18n"
117e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      .. " " .. arch_options
118e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETend
119e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
120e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETlocal function IterTable(t)
121e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  return coroutine.wrap(function ()
122e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    for i, v in ipairs(t) do
123e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      coroutine.yield(v)
124e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    end
125e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  end)
126e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETend
127e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
128e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETlocal function SplitResults(lines, func)
129e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   -- Splits the output of parallel.py and calls func on each result.
130e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   -- Bails out in case of an error in one of the executions.
131e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   local current = {}
132e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   local filename = ""
133e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   for line in lines do
134e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      local new_file = line:match "^______________ (.*)$"
135e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      local code = line:match "^______________ finish (%d+) ______________$"
136e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      if code then
137e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET         if tonumber(code) > 0 then
138e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET            log(table.concat(current, "\n"))
139e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET            log("Failed to examine " .. filename)
140e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET            return false
141e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET         end
142e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET         log("-- %s", filename)
143e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET         func(filename, IterTable(current))
144e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      elseif new_file then
145e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET         filename = new_file
146e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET         current = {}
147e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      else
148e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET         table.insert(current, line)
149e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      end
150e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   end
151e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   return true
152e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETend
153e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
154e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETfunction InvokeClangPluginForEachFile(filenames, cfg, func)
155e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   local cmd_line = MakeClangCommandLine(cfg.plugin,
156e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET                                         cfg.plugin_args,
157e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET                                         cfg.triple,
158e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET                                         cfg.arch_define,
159e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET                                         cfg.arch_options)
160e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   if FLAGS.sequential then
161e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      log("** Sequential execution.")
162e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      for _, filename in ipairs(filenames) do
163e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET         log("-- %s", filename)
164e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET         local action = cmd_line .. " " .. filename .. " 2>&1"
165e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET         if FLAGS.verbose then print('popen ', action) end
166e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET         local pipe = io.popen(action)
167e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET         func(filename, pipe:lines())
168e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET         local success = pipe:close()
169e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET         if not success then error("Failed to run: " .. action) end
170e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      end
171e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   else
172e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      log("** Parallel execution.")
173e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      local action = "python tools/gcmole/parallel.py \""
174e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET         .. cmd_line .. "\" " .. table.concat(filenames, " ")
175e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      if FLAGS.verbose then print('popen ', action) end
176e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      local pipe = io.popen(action)
177e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      local success = SplitResults(pipe:lines(), func)
178e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      local closed = pipe:close()
179e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      if not (success and closed) then error("Failed to run: " .. action) end
180e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   end
181e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETend
182e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
183e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET-------------------------------------------------------------------------------
184e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET-- GYP file parsing
185e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
186e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETlocal function ParseGYPFile()
187e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   local gyp = ""
188e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   local gyp_files = { "src/v8.gyp", "test/cctest/cctest.gyp" }
189e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   for i = 1, #gyp_files do
190e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      local f = assert(io.open(gyp_files[i]), "failed to open GYP file")
191e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      local t = f:read('*a')
192e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      gyp = gyp .. t
193e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      f:close()
194e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   end
195e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
196e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   local result = {}
197e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
198e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   for condition, sources in
199e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      gyp:gmatch "'sources': %[.-### gcmole%((.-)%) ###(.-)%]" do
200e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      if result[condition] == nil then result[condition] = {} end
201e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      for file in sources:gmatch "'%.%./%.%./src/([^']-%.cc)'" do
202e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET         table.insert(result[condition], "src/" .. file)
203e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      end
204e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      for file in sources:gmatch "'(test-[^']-%.cc)'" do
205e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET         table.insert(result[condition], "test/cctest/" .. file)
206      end
207   end
208
209   return result
210end
211
212local function EvaluateCondition(cond, props)
213   if cond == 'all' then return true end
214
215   local p, v = cond:match "(%w+):(%w+)"
216
217   assert(p and v, "failed to parse condition: " .. cond)
218   assert(props[p] ~= nil, "undefined configuration property: " .. p)
219
220   return props[p] == v
221end
222
223local function BuildFileList(sources, props)
224   local list = {}
225   for condition, files in pairs(sources) do
226      if EvaluateCondition(condition, props) then
227         for i = 1, #files do table.insert(list, files[i]) end
228      end
229   end
230   return list
231end
232
233local sources = ParseGYPFile()
234
235local function FilesForArch(arch)
236   return BuildFileList(sources, { os = 'linux',
237                                   arch = arch,
238                                   mode = 'debug',
239                                   simulator = ''})
240end
241
242local mtConfig = {}
243
244mtConfig.__index = mtConfig
245
246local function config (t) return setmetatable(t, mtConfig) end
247
248function mtConfig:extend(t)
249   local e = {}
250   for k, v in pairs(self) do e[k] = v end
251   for k, v in pairs(t) do e[k] = v end
252   return config(e)
253end
254
255local ARCHITECTURES = {
256   ia32 = config { triple = "i586-unknown-linux",
257                   arch_define = "V8_TARGET_ARCH_IA32",
258                   arch_options = "-m32" },
259   arm = config { triple = "i586-unknown-linux",
260                  arch_define = "V8_TARGET_ARCH_ARM",
261                  arch_options = "-m32" },
262   x64 = config { triple = "x86_64-unknown-linux",
263                  arch_define = "V8_TARGET_ARCH_X64",
264                  arch_options = "" },
265   arm64 = config { triple = "x86_64-unknown-linux",
266                    arch_define = "V8_TARGET_ARCH_ARM64",
267                    arch_options = "" },
268}
269
270-------------------------------------------------------------------------------
271-- GCSuspects Generation
272
273local gc, gc_caused, funcs
274
275local WHITELIST = {
276   -- The following functions call CEntryStub which is always present.
277   "MacroAssembler.*CallExternalReference",
278   "MacroAssembler.*CallRuntime",
279   "CompileCallLoadPropertyWithInterceptor",
280   "CallIC.*GenerateMiss",
281
282   -- DirectCEntryStub is a special stub used on ARM. 
283   -- It is pinned and always present.
284   "DirectCEntryStub.*GenerateCall",  
285
286   -- TODO GCMole currently is sensitive enough to understand that certain 
287   --      functions only cause GC and return Failure simulataneously. 
288   --      Callsites of such functions are safe as long as they are properly 
289   --      check return value and propagate the Failure to the caller.
290   --      It should be possible to extend GCMole to understand this.
291   "Heap.*AllocateFunctionPrototype",
292
293   -- Ignore all StateTag methods.
294   "StateTag",
295
296   -- Ignore printing of elements transition.
297   "PrintElementsTransition"
298};
299
300local function AddCause(name, cause)
301   local t = gc_caused[name]
302   if not t then
303      t = {}
304      gc_caused[name] = t
305   end
306   table.insert(t, cause)
307end
308
309local function resolve(name)
310   local f = funcs[name]
311
312   if not f then
313      f = {}
314      funcs[name] = f
315
316      if name:match "Collect.*Garbage" then
317         gc[name] = true
318         AddCause(name, "<GC>")
319      end
320
321      if FLAGS.whitelist then
322         for i = 1, #WHITELIST do
323            if name:match(WHITELIST[i]) then
324               gc[name] = false
325            end
326         end
327      end
328   end
329
330    return f
331end
332
333local function parse (filename, lines)
334   local scope
335
336   for funcname in lines do
337      if funcname:sub(1, 1) ~= '\t' then
338         resolve(funcname)
339         scope = funcname
340      else
341         local name = funcname:sub(2)
342         resolve(name)[scope] = true
343      end
344   end
345end
346
347local function propagate ()
348   log "** Propagating GC information"
349
350   local function mark(from, callers)
351      for caller, _ in pairs(callers) do
352         if gc[caller] == nil then
353            gc[caller] = true
354            mark(caller, funcs[caller])
355         end
356         AddCause(caller, from)
357      end
358   end
359
360   for funcname, callers in pairs(funcs) do
361      if gc[funcname] then mark(funcname, callers) end
362   end
363end
364
365local function GenerateGCSuspects(arch, files, cfg)
366   -- Reset the global state.
367   gc, gc_caused, funcs = {}, {}, {}
368
369   log ("** Building GC Suspects for %s", arch)
370   InvokeClangPluginForEachFile (files,
371                                 cfg:extend { plugin = "dump-callees" },
372                                 parse)
373
374   propagate()
375
376   local out = assert(io.open("gcsuspects", "w"))
377   for name, value in pairs(gc) do if value then out:write (name, '\n') end end
378   out:close()
379
380   local out = assert(io.open("gccauses", "w"))
381   out:write "GC = {"
382   for name, causes in pairs(gc_caused) do
383      out:write("['", name, "'] = {")
384      for i = 1, #causes do out:write ("'", causes[i], "';") end
385      out:write("};\n")
386   end
387   out:write "}"
388   out:close()
389
390   log ("** GCSuspects generated for %s", arch)
391end
392
393--------------------------------------------------------------------------------
394-- Analysis
395
396local function CheckCorrectnessForArch(arch)
397   local files = FilesForArch(arch)
398   local cfg = ARCHITECTURES[arch]
399
400   if not FLAGS.reuse_gcsuspects then
401      GenerateGCSuspects(arch, files, cfg)
402   end
403
404   local processed_files = 0
405   local errors_found = false
406   local function SearchForErrors(filename, lines)
407      processed_files = processed_files + 1
408      for l in lines do
409         errors_found = errors_found or
410            l:match "^[^:]+:%d+:%d+:" or
411            l:match "error" or
412            l:match "warning"
413         print(l)
414      end
415   end
416
417   log("** Searching for evaluation order problems%s for %s",
418       FLAGS.dead_vars and " and dead variables" or "",
419       arch)
420   local plugin_args
421   if FLAGS.dead_vars then plugin_args = { "--dead-vars" } end
422   InvokeClangPluginForEachFile(files,
423                                cfg:extend { plugin = "find-problems",
424                                             plugin_args = plugin_args },
425                                SearchForErrors)
426   log("** Done processing %d files. %s",
427       processed_files,
428       errors_found and "Errors found" or "No errors found")
429
430   return errors_found
431end
432
433local function SafeCheckCorrectnessForArch(arch)
434   local status, errors = pcall(CheckCorrectnessForArch, arch)
435   if not status then
436      print(string.format("There was an error: %s", errors))
437      errors = true
438   end
439   return errors
440end
441
442local errors = false
443
444for _, arch in ipairs(ARCHS) do
445   if not ARCHITECTURES[arch] then
446      error ("Unknown arch: " .. arch)
447   end
448
449   errors = SafeCheckCorrectnessForArch(arch, report) or errors
450end
451
452os.exit(errors and 1 or 0)
453