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