|
1 | 1 | #!/usr/bin/env python |
2 | 2 | # -*- coding: utf-8 -*- |
3 | 3 |
|
4 | | - |
5 | 4 | from __future__ import print_function |
6 | 5 |
|
7 | 6 | import os |
8 | 7 | import sys |
9 | 8 |
|
10 | | -import cnerator |
11 | 9 | from debug import call_inspector, structure_inspector |
12 | | -from params.parameters import parse_args, get_modules_to_import |
| 10 | +from params import parameters |
| 11 | +from params.parameters import parse_args, get_modules_to_import, get_probs_to_override |
13 | 12 | from params.writter import write_in_files |
| 13 | +from params import json_probs |
| 14 | +import cnerator |
14 | 15 |
|
15 | 16 |
|
16 | 17 | def run(args): |
17 | | - # Sets the recursion limit |
| 18 | + # Set the recursion limit |
18 | 19 | sys.setrecursionlimit(args.recursion) |
19 | 20 |
|
20 | | - # return_instrumentator.monkey_path() |
21 | | - # program = cnerator.generators.generate_program() |
22 | | - |
23 | | - ### |
24 | | - # 3k (ALL - NORMAL) |
25 | | - ### |
26 | | - # program = cnerator.generators.generate_program_with_distribution({ |
27 | | - # "v_functions": {"total": 300, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Void)}, |
28 | | - # "b_functions": {"total": 300, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Bool)}, |
29 | | - # "sc_functions": {"total": 300, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedChar)}, |
30 | | - # "sSi_functions": {"total": 300, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedShortInt)}, |
31 | | - # "si_functions": {"total": 300, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedInt)}, |
32 | | - # "sLLi_functions": {"total": 300, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedLongLongInt)}, |
33 | | - # "f_functions": {"total": 300, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Float)}, |
34 | | - # "d_functions": {"total": 300, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Double)}, |
35 | | - # "p_functions": {"total": 300, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Pointer)}, |
36 | | - # "struct_functions": {"total": 300, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Struct)}, |
37 | | - # }, 300*10, remove_outsiders=True) |
38 | | - |
39 | | - ### |
40 | | - # 0.15k (ALL - SNOWMAN) |
41 | | - ### |
42 | | - # program = cnerator.generators.generate_program_with_distribution({ |
43 | | - # "v_functions": {"total": 15, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Void)}, |
44 | | - # "b_functions": {"total": 15, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Bool)}, |
45 | | - # "sc_functions": {"total": 15, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedChar)}, |
46 | | - # "sSi_functions": {"total": 15, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedShortInt)}, |
47 | | - # "si_functions": {"total": 15, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedInt)}, |
48 | | - # "sLLi_functions": {"total": 15, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedLongLongInt)}, |
49 | | - # "f_functions": {"total": 15, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Float)}, |
50 | | - # "d_functions": {"total": 15, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Double)}, |
51 | | - # "p_functions": {"total": 15, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Pointer)}, |
52 | | - # "struct_functions": {"total": 15, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Struct)}, |
53 | | - # }, 15*10, remove_outsiders=True) |
54 | | - |
55 | | - ### |
56 | | - # FUNCTION vs PROCEDURE, 4k functions |
57 | | - ### |
58 | | - # program = cnerator.generators.generate_program_with_distribution({ |
59 | | - # "procedures": {"total": 2000, "cmp": lambda f: f.return_type == cnerator.ast.Void()}, |
60 | | - # "functions": {"total": 2000, "cmp": lambda f: f.return_type != cnerator.ast.Void()}, |
61 | | - # }, 4000, remove_outsiders=False) |
62 | | - |
63 | | - ### |
64 | | - # 3k (SIZE - NORMAL) |
65 | | - ### |
66 | | - # program = cnerator.generators.generate_program_with_distribution({ |
67 | | - # "v_functions": {"total": 429, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Void)}, |
68 | | - # "b_functions": {"total": 215, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Bool)}, |
69 | | - # "sc_functions": {"total": 214, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedChar)}, |
70 | | - # "sSi_functions": {"total": 428, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedShortInt)}, |
71 | | - # "si_functions": {"total": 142, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedInt)}, |
72 | | - # "sLLi_functions": {"total": 428, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedLongLongInt)}, |
73 | | - # "f_functions": {"total": 429, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Float)}, |
74 | | - # "d_functions": {"total": 429, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Double)}, |
75 | | - # "p_functions": {"total": 143, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Pointer)}, |
76 | | - # "struct_functions": {"total": 143, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Struct)}, |
77 | | - # }, 3000, remove_outsiders=True) |
78 | | - |
79 | | - ### |
80 | | - # 0.15k (SIZE - SNOWMAN) |
81 | | - ### |
82 | | - # program = cnerator.generators.generate_program_with_distribution({ |
83 | | - # "v_functions": {"total": 22, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Void)}, |
84 | | - # "b_functions": {"total": 11, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Bool)}, |
85 | | - # "sc_functions": {"total": 10, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedChar)}, |
86 | | - # "sSi_functions": {"total": 21, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedShortInt)}, |
87 | | - # "si_functions": {"total": 7, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedInt)}, |
88 | | - # "sLLi_functions": {"total": 21, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedLongLongInt)}, |
89 | | - # "f_functions": {"total": 22, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Float)}, |
90 | | - # "d_functions": {"total": 22, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Double)}, |
91 | | - # "p_functions": {"total": 7, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Pointer)}, |
92 | | - # "struct_functions": {"total": 7, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Struct)}, |
93 | | - # }, 150, remove_outsiders=True) |
94 | | - |
95 | | - ### |
96 | | - # 10 (SIZE - NORMAL) |
97 | | - ### |
98 | | - program = cnerator.generators.generate_program_with_function_distribution({ |
99 | | - "v_functions": {"total": 1, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Void)}, |
100 | | - "b_functions": {"total": 1, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Bool)}, |
101 | | - "sc_functions": {"total": 1, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedChar)}, |
102 | | - "sSi_functions": {"total": 1, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedShortInt)}, |
103 | | - "si_functions": {"total": 1, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedInt)}, |
104 | | - "sLLi_functions": {"total": 1, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedLongLongInt)}, |
105 | | - "f_functions": {"total": 1, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Float)}, |
106 | | - "d_functions": {"total": 1, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Double)}, |
107 | | - "p_functions": {"total": 1, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Pointer)}, |
108 | | - "struct_functions": {"total": 1, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Struct)}, |
109 | | - }, args, 10, remove_unwanted_functions=True) |
110 | | - |
111 | | - # [DEBUG] |
112 | | - |
113 | | - # import cnerator.ast as ast |
114 | | - # import cnerator.type_inference as type_inference |
115 | | - # import cnerator.generators as generators |
116 | | - |
117 | | - # person_struct = Struct("person", |
118 | | - # ["xyz", Array(LongDouble(), 3)], |
119 | | - # ["age", UnsignedInt()], |
120 | | - # ["is_man", Bool()], |
121 | | - # ) |
122 | | - # person_struct.add_field("next", Pointer(person_struct)) |
123 | | - # person_struct.add_field("parents", Array(Pointer(person_struct), 2)) |
124 | | - # person_struct.add_field("fake_argc", Pointer(Array(SignedChar()))) |
125 | | - # person_struct.add_field("argc1", Pointer(Pointer(SignedChar()))) |
126 | | - # person_struct.add_field("argc2", Array(Pointer(SignedChar()), pointer_literal=True)) |
127 | | - # self.structs.append(person_struct) |
128 | | - # self.global_vars[person_struct] = [person_struct.generate_literal(), person_struct.generate_literal()] |
129 | | - |
130 | | - # Some global vars |
131 | | - |
132 | | - # t1 = ast.Array(ast.Pointer(ast.Array(ast.UnsignedChar(), 2)), 3) |
133 | | - # program.global_vars[t1.name] = [(t1, t1.generate_literal(from_declaration=True))] |
134 | | - # |
135 | | - # t2 = ast.Pointer(ast.Array(ast.Array(ast.UnsignedChar(), 2), 3)) |
136 | | - # program.global_vars[t2.name] = [(t2, t2.generate_literal(from_declaration=True))] |
137 | | - # |
138 | | - # t3 = ast.Array(ast.Array(ast.Pointer(ast.UnsignedChar()), 2), 3) |
139 | | - # program.global_vars[t3.name] = [(t3, t3.generate_literal(from_declaration=True))] |
140 | | - # |
141 | | - # t4 = ast.Array(ast.Array(ast.Array(ast.Bool(), 2), 3), 4, pointer_declaration=True, pointer_literal=True) |
142 | | - # program.global_vars[t4.name] = [(t4, t4.generate_literal(from_declaration=True))] |
143 | | - # assert t4.__declaration__(var_name=None, from_return_in_func_decl=True) == "bool * * *" |
144 | | - |
145 | | - # Some statements in Main |
146 | | - |
147 | | - # assignment_type = ast.Double() |
148 | | - # # Left part |
149 | | - # var = generators.generate_local_var(program, program.main, assignment_type) |
150 | | - # left = var |
151 | | - # # Right part |
152 | | - # dereference_types = type_inference.infer_operands_type(program, program.main, 1, "*", assignment_type) # Pointer |
153 | | - # dereference_type = dereference_types[0] |
154 | | - # # address_of_types_cls = type_inference.infer_operands_type(program, program.main, 1, "&", dereference_type) |
155 | | - # address_of_type = ast.Double() |
156 | | - # var = generators.generate_local_var(program, program.main, address_of_type) |
157 | | - # right = ast.UnaryExpression("*", ast.UnaryExpression("&", var, address_of_type), dereference_type) |
158 | | - # program.main.stmts.append(ast.Assignment(left, "=", right, assignment_type)) |
159 | | - |
160 | | - # [/DEBUG] |
161 | | - |
162 | | - # Load all the visitor modules and run them, in the same order |
| 21 | + # Get the probabilites from the command line arguments and modify the default ones |
| 22 | + cnerator.probs.set_probabilites(get_probs_to_override(args.probs)) |
| 23 | + if args.probsfile: |
| 24 | + from cnerator import probs |
| 25 | + probabilites = json_probs.parse_probabilites_specification_json_file(args.probsfile) |
| 26 | + probs.set_probabilites(probabilites) |
| 27 | + |
| 28 | + # Process the json file to create functions, or generates a program using the probabilities defined |
| 29 | + if args.functions: # if a json file was passed, it defines the functions to be generated |
| 30 | + dictionary = parameters.parse_function_specification_json_file(args.functions) |
| 31 | + program = cnerator.generators.generate_program_with_function_distribution(dictionary, args, remove_unwanted_functions=True) |
| 32 | + else: # otherwise, a random program is generated, considering the specified probabilities |
| 33 | + program = cnerator.generators.generate_program() |
| 34 | + |
| 35 | + # Load all the visitor modules and run them, in the same order as specified by the user |
163 | 36 | modules = get_modules_to_import(args.visitors) |
164 | 37 | for module in modules: |
165 | 38 | module.visit(program) |
166 | 39 |
|
167 | 40 | # Create output directory |
168 | | - if not os.path.isdir(args.working_dir): |
169 | | - os.mkdir(args.working_dir) |
| 41 | + if not os.path.isdir(args.output_dir): |
| 42 | + os.mkdir(args.output_dir) |
170 | 43 |
|
171 | 44 | # Write code to files |
172 | 45 | write_in_files(program, args) |
173 | 46 |
|
| 47 | + # Write debug graph information |
174 | 48 | if args.debug: |
175 | 49 | # Write structure graph |
176 | | - structure_inspector.write_graph(program, os.path.join(args.working_dir, args.output + ".structure.dot")) |
| 50 | + structure_inspector.write_graph(program, os.path.join(args.output_dir, args.output + ".structure.dot")) |
177 | 51 | # Write call graph |
178 | | - call_inspector.write_graph(program, True, os.path.join(args.working_dir, args.output + ".call.dot")) |
179 | | - call_inspector.write_graph(program, False, os.path.join(args.working_dir, args.output + ".call_light.dot")) |
| 52 | + call_inspector.write_graph(program, True, os.path.join(args.output_dir, args.output + ".call.dot")) |
| 53 | + call_inspector.write_graph(program, False, os.path.join(args.output_dir, args.output + ".call_light.dot")) |
180 | 54 |
|
181 | 55 |
|
182 | 56 | def main(): |
|
0 commit comments