Skip to content

Commit 1379cdb

Browse files
Parameterization and generalization of the tool
2 parents ab7ab9f + fb0786e commit 1379cdb

File tree

122 files changed

+3086
-93501
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

122 files changed

+3086
-93501
lines changed

cnerator.py

Lines changed: 26 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -1,182 +1,56 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4-
54
from __future__ import print_function
65

76
import os
87
import sys
98

10-
import cnerator
119
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
1312
from params.writter import write_in_files
13+
from params import json_probs
14+
import cnerator
1415

1516

1617
def run(args):
17-
# Sets the recursion limit
18+
# Set the recursion limit
1819
sys.setrecursionlimit(args.recursion)
1920

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
16336
modules = get_modules_to_import(args.visitors)
16437
for module in modules:
16538
module.visit(program)
16639

16740
# 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)
17043

17144
# Write code to files
17245
write_in_files(program, args)
17346

47+
# Write debug graph information
17448
if args.debug:
17549
# 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"))
17751
# 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"))
18054

18155

18256
def main():

cnerator/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
from cnerator import ast
21
from cnerator import generators
3-
from cnerator import type_inference
4-
2+
from cnerator import probs

cnerator/ast.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import re
77
import string
88

9-
from cnerator import graph, utils, operators
10-
119

1210
########### Utils ##############
1311

@@ -577,6 +575,7 @@ class LongDouble(NumericType):
577575

578576

579577
def structs_references_graph(structs):
578+
from cnerator import graph
580579
references = collections.defaultdict(list)
581580
for s in structs:
582581
for f, t in s.fields:
@@ -738,7 +737,7 @@ def generate_literal(self, from_declaration, size=0):
738737
if isinstance(self.type, (SignedChar, UnsignedChar)):
739738
return generate_string_literal(size, from_declaration, self)
740739
return Literal("{{ {} }}".format(
741-
", ".join(self.type.generate_literal(from_declaration=from_declaration) for _ in range(size))
740+
", ".join(str(self.type.generate_literal(from_declaration=from_declaration)) for _ in range(size))
742741
), self)
743742

744743
def __hash__(self):
@@ -759,6 +758,7 @@ class FuncProc(object):
759758
########## Helper functions ##############
760759

761760
def get_operators(c_type, _type):
761+
from cnerator import utils, operators
762762
operator_type = "{}_{}_operators".format(
763763
utils.camel_case_to_snake_case(c_type.__class__.__name__),
764764
_type

0 commit comments

Comments
 (0)