1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# Copyright 2020 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7"""Command script without compiler support for pass level bisection.
8
9This script generates a pseudo log which a workable compiler should print out.
10It assumes that -opt-bisect-limit and -print-debug-counter are supported by the
11compiler.
12"""
13
14from __future__ import print_function
15
16import os
17import sys
18
19from binary_search_tool.test import common
20
21
22def Main(argv):
23  if not os.path.exists('./is_setup'):
24    return 1
25
26  if len(argv) != 3:
27    return 1
28
29  limit_flags = os.environ['LIMIT_FLAGS']
30  opt_bisect_exist = False
31  debug_counter_exist = False
32
33  for option in limit_flags.split():
34    if '-opt-bisect-limit' in option:
35      opt_bisect_limit = int(option.split('=')[-1])
36      opt_bisect_exist = True
37    if '-debug-counter=' in option:
38      debug_counter = int(option.split('=')[-1])
39      debug_counter_exist = True
40
41  if not opt_bisect_exist:
42    return 1
43
44  # Manually set total number and bad number
45  total_pass = 10
46  total_transform = 20
47  bad_pass = int(argv[1])
48  bad_transform = int(argv[2])
49
50  if opt_bisect_limit == -1:
51    opt_bisect_limit = total_pass
52
53  for i in range(1, total_pass + 1):
54    bisect_str = 'BISECT: %srunning pass (%d) Combine redundant ' \
55                 'instructions on function (f1)' \
56                 % ('NOT ' if i > opt_bisect_limit else '', i)
57    print(bisect_str, file=sys.stderr)
58
59  if debug_counter_exist:
60    print('Counters and values:', file=sys.stderr)
61    print(
62        'instcombine-visit : {%d, 0, %d}' % (total_transform, debug_counter),
63        file=sys.stderr)
64
65  if opt_bisect_limit > bad_pass or \
66    (debug_counter_exist and debug_counter > bad_transform):
67    common.WriteWorkingSet([1])
68  else:
69    common.WriteWorkingSet([0])
70
71  return 0
72
73
74if __name__ == '__main__':
75  retval = Main(sys.argv)
76  sys.exit(retval)
77