Merge pull request #505 from roji/shutdown_flow
[mono.git] / mono / tests / gc-descriptors / gen-descriptor-tests.py
1 #!/usr/bin/env python
2
3 from optparse import OptionParser
4
5 parser = OptionParser ()
6 parser.add_option ("--switch", action = "store_true", dest = "switch")
7 parser.add_option ("--one-method-if", action = "store_true", dest = "one_method_if")
8
9 (options, args) = parser.parse_args ()
10
11 def print_file (file_name):
12     f = open (file_name, "r")
13     for line in f:
14         print (line),
15     f.close ()
16
17 print_file ("descriptor-tests-prefix.cs")
18
19 print ("public struct NoRef1 { int x; }")
20 for i in range (1, 17):
21     print ("public struct NoRef%d { NoRef%d a, b; }" % (1 << i, 1 << (i-1)))
22
23 print ("")
24
25 names = []
26
27 max_offset = 512
28 max_bitmap = 257
29
30 for offset in range (0, max_offset, 19):
31     for bitmap in range (0, max_bitmap):
32         name = "Bitmap%dSkip%d" % (bitmap, offset)
33         names.append (name)
34         print ("public struct %s : Filler {" % name)
35         for i in range (0, 16):
36             bit = 1 << i
37             if offset & bit:
38                 print ("  NoRef%d skip%d;" % (bit, bit))
39         for i in range (0, 9):
40             bit = 1 << i
41             if bitmap & bit:
42                 print ("  object ref%d;" % i)
43             else:
44                 print ("  int bit%d;" % i)
45         print ("  public void Fill (object[] refs) {")
46         for i in range (0, 9):
47             bit = 1 << i
48             if bitmap & bit:
49                 print ("    ref%d = refs [%d];" % (i, i))
50         print ("  }")
51         print ("}")
52         print ("public class %sWrapper : Filler {" % name)
53         print ("  %s[] a;" % name)
54         print ("  public %sWrapper () {" % name)
55         print ("    a = new %s [1];" % name)
56         print ("  }")
57         print ("  public void Fill (object[] refs) {")
58         print ("    a [0].Fill (refs);")
59         print ("  }")
60         print ("}\n")
61
62 def search_method_name (left, right):
63     return "MakeAndFillL%dR%d" % (left, right)
64
65 def gen_new (name):
66     print ("Filler b;")
67     print ("if (wrap)")
68     print ("  b = new %sWrapper ();" % name)
69     print ("else")
70     print ("  b = new %s ();" % name)
71     print ("b.Fill (refs); return b;")
72
73 def gen_binary_search_body (left, right, one_method):
74     if left + 1 >= right:
75         gen_new (names [left])
76     else:
77         mid = (left + right) // 2
78         print ("if (which < %d) {" % mid)
79         if one_method:
80             gen_binary_search_body (left, mid, one_method)
81         else:
82             print ("return %s (which, refs, wrap);" % search_method_name (left, mid))
83         print ("} else {")
84         if one_method:
85             gen_binary_search_body (mid, right, one_method)
86         else:
87             print ("return %s (which, refs, wrap);" % search_method_name (mid, right))
88         print ("}")
89
90 def gen_binary_search (left, right, one_method):
91     name = search_method_name (left, right)
92     print ("public static Filler %s (int which, object[] refs, bool wrap) {" % name)
93     gen_binary_search_body (left, right, one_method)
94     print ("}")
95     if not one_method and left + 1 < right:
96         mid = (left + right) // 2
97         gen_binary_search (left, mid, one_method)
98         gen_binary_search (mid, right, one_method)
99     return name
100
101 print ("public class Bitmaps {")
102 if options.switch:
103     print ("  public static Filler MakeAndFill (int which, object[] refs, bool wrap) {")
104     print ("    switch (which) {")
105     for i in range (0, len (names)):
106         print ("      case %d: {" % i)
107         gen_new (names [i])
108         print ("}")
109     print ("      default: return null;")
110     print ("    }")
111     print ("  }")
112 else:
113     method_name = gen_binary_search (0, len (names), options.one_method_if)
114     print ("  public static Filler MakeAndFill (int which, object[] refs, bool wrap) {")
115     print ("    if (which >= %d) return null;" % len (names))
116     print ("    return %s (which, refs, wrap);" % method_name)
117     print ("  }")
118 print ("  public const int NumWhich = %d;" % len (names))
119 print ("}")
120
121 print ("")
122
123 print_file ("descriptor-tests-driver.cs")