Skip to content

Commit

Permalink
Parameterization and generalization of the tool
Browse files Browse the repository at this point in the history
  • Loading branch information
francisco-ortin committed Jan 4, 2021
2 parents ab7ab9f + fb0786e commit 1379cdb
Show file tree
Hide file tree
Showing 122 changed files with 3,086 additions and 93,501 deletions.
178 changes: 26 additions & 152 deletions cnerator.py
Original file line number Diff line number Diff line change
@@ -1,182 +1,56 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-


from __future__ import print_function

import os
import sys

import cnerator
from debug import call_inspector, structure_inspector
from params.parameters import parse_args, get_modules_to_import
from params import parameters
from params.parameters import parse_args, get_modules_to_import, get_probs_to_override
from params.writter import write_in_files
from params import json_probs
import cnerator


def run(args):
# Sets the recursion limit
# Set the recursion limit
sys.setrecursionlimit(args.recursion)

# return_instrumentator.monkey_path()
# program = cnerator.generators.generate_program()

###
# 3k (ALL - NORMAL)
###
# program = cnerator.generators.generate_program_with_distribution({
# "v_functions": {"total": 300, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Void)},
# "b_functions": {"total": 300, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Bool)},
# "sc_functions": {"total": 300, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedChar)},
# "sSi_functions": {"total": 300, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedShortInt)},
# "si_functions": {"total": 300, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedInt)},
# "sLLi_functions": {"total": 300, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedLongLongInt)},
# "f_functions": {"total": 300, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Float)},
# "d_functions": {"total": 300, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Double)},
# "p_functions": {"total": 300, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Pointer)},
# "struct_functions": {"total": 300, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Struct)},
# }, 300*10, remove_outsiders=True)

###
# 0.15k (ALL - SNOWMAN)
###
# program = cnerator.generators.generate_program_with_distribution({
# "v_functions": {"total": 15, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Void)},
# "b_functions": {"total": 15, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Bool)},
# "sc_functions": {"total": 15, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedChar)},
# "sSi_functions": {"total": 15, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedShortInt)},
# "si_functions": {"total": 15, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedInt)},
# "sLLi_functions": {"total": 15, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedLongLongInt)},
# "f_functions": {"total": 15, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Float)},
# "d_functions": {"total": 15, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Double)},
# "p_functions": {"total": 15, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Pointer)},
# "struct_functions": {"total": 15, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Struct)},
# }, 15*10, remove_outsiders=True)

###
# FUNCTION vs PROCEDURE, 4k functions
###
# program = cnerator.generators.generate_program_with_distribution({
# "procedures": {"total": 2000, "cmp": lambda f: f.return_type == cnerator.ast.Void()},
# "functions": {"total": 2000, "cmp": lambda f: f.return_type != cnerator.ast.Void()},
# }, 4000, remove_outsiders=False)

###
# 3k (SIZE - NORMAL)
###
# program = cnerator.generators.generate_program_with_distribution({
# "v_functions": {"total": 429, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Void)},
# "b_functions": {"total": 215, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Bool)},
# "sc_functions": {"total": 214, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedChar)},
# "sSi_functions": {"total": 428, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedShortInt)},
# "si_functions": {"total": 142, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedInt)},
# "sLLi_functions": {"total": 428, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedLongLongInt)},
# "f_functions": {"total": 429, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Float)},
# "d_functions": {"total": 429, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Double)},
# "p_functions": {"total": 143, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Pointer)},
# "struct_functions": {"total": 143, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Struct)},
# }, 3000, remove_outsiders=True)

###
# 0.15k (SIZE - SNOWMAN)
###
# program = cnerator.generators.generate_program_with_distribution({
# "v_functions": {"total": 22, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Void)},
# "b_functions": {"total": 11, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Bool)},
# "sc_functions": {"total": 10, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedChar)},
# "sSi_functions": {"total": 21, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedShortInt)},
# "si_functions": {"total": 7, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedInt)},
# "sLLi_functions": {"total": 21, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedLongLongInt)},
# "f_functions": {"total": 22, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Float)},
# "d_functions": {"total": 22, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Double)},
# "p_functions": {"total": 7, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Pointer)},
# "struct_functions": {"total": 7, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Struct)},
# }, 150, remove_outsiders=True)

###
# 10 (SIZE - NORMAL)
###
program = cnerator.generators.generate_program_with_function_distribution({
"v_functions": {"total": 1, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Void)},
"b_functions": {"total": 1, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Bool)},
"sc_functions": {"total": 1, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedChar)},
"sSi_functions": {"total": 1, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedShortInt)},
"si_functions": {"total": 1, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedInt)},
"sLLi_functions": {"total": 1, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.SignedLongLongInt)},
"f_functions": {"total": 1, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Float)},
"d_functions": {"total": 1, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Double)},
"p_functions": {"total": 1, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Pointer)},
"struct_functions": {"total": 1, "cmp": lambda f: isinstance(f.return_type, cnerator.ast.Struct)},
}, args, 10, remove_unwanted_functions=True)

# [DEBUG]

# import cnerator.ast as ast
# import cnerator.type_inference as type_inference
# import cnerator.generators as generators

# person_struct = Struct("person",
# ["xyz", Array(LongDouble(), 3)],
# ["age", UnsignedInt()],
# ["is_man", Bool()],
# )
# person_struct.add_field("next", Pointer(person_struct))
# person_struct.add_field("parents", Array(Pointer(person_struct), 2))
# person_struct.add_field("fake_argc", Pointer(Array(SignedChar())))
# person_struct.add_field("argc1", Pointer(Pointer(SignedChar())))
# person_struct.add_field("argc2", Array(Pointer(SignedChar()), pointer_literal=True))
# self.structs.append(person_struct)
# self.global_vars[person_struct] = [person_struct.generate_literal(), person_struct.generate_literal()]

# Some global vars

# t1 = ast.Array(ast.Pointer(ast.Array(ast.UnsignedChar(), 2)), 3)
# program.global_vars[t1.name] = [(t1, t1.generate_literal(from_declaration=True))]
#
# t2 = ast.Pointer(ast.Array(ast.Array(ast.UnsignedChar(), 2), 3))
# program.global_vars[t2.name] = [(t2, t2.generate_literal(from_declaration=True))]
#
# t3 = ast.Array(ast.Array(ast.Pointer(ast.UnsignedChar()), 2), 3)
# program.global_vars[t3.name] = [(t3, t3.generate_literal(from_declaration=True))]
#
# t4 = ast.Array(ast.Array(ast.Array(ast.Bool(), 2), 3), 4, pointer_declaration=True, pointer_literal=True)
# program.global_vars[t4.name] = [(t4, t4.generate_literal(from_declaration=True))]
# assert t4.__declaration__(var_name=None, from_return_in_func_decl=True) == "bool * * *"

# Some statements in Main

# assignment_type = ast.Double()
# # Left part
# var = generators.generate_local_var(program, program.main, assignment_type)
# left = var
# # Right part
# dereference_types = type_inference.infer_operands_type(program, program.main, 1, "*", assignment_type) # Pointer
# dereference_type = dereference_types[0]
# # address_of_types_cls = type_inference.infer_operands_type(program, program.main, 1, "&", dereference_type)
# address_of_type = ast.Double()
# var = generators.generate_local_var(program, program.main, address_of_type)
# right = ast.UnaryExpression("*", ast.UnaryExpression("&", var, address_of_type), dereference_type)
# program.main.stmts.append(ast.Assignment(left, "=", right, assignment_type))

# [/DEBUG]

# Load all the visitor modules and run them, in the same order
# Get the probabilites from the command line arguments and modify the default ones
cnerator.probs.set_probabilites(get_probs_to_override(args.probs))
if args.probsfile:
from cnerator import probs
probabilites = json_probs.parse_probabilites_specification_json_file(args.probsfile)
probs.set_probabilites(probabilites)

# Process the json file to create functions, or generates a program using the probabilities defined
if args.functions: # if a json file was passed, it defines the functions to be generated
dictionary = parameters.parse_function_specification_json_file(args.functions)
program = cnerator.generators.generate_program_with_function_distribution(dictionary, args, remove_unwanted_functions=True)
else: # otherwise, a random program is generated, considering the specified probabilities
program = cnerator.generators.generate_program()

# Load all the visitor modules and run them, in the same order as specified by the user
modules = get_modules_to_import(args.visitors)
for module in modules:
module.visit(program)

# Create output directory
if not os.path.isdir(args.working_dir):
os.mkdir(args.working_dir)
if not os.path.isdir(args.output_dir):
os.mkdir(args.output_dir)

# Write code to files
write_in_files(program, args)

# Write debug graph information
if args.debug:
# Write structure graph
structure_inspector.write_graph(program, os.path.join(args.working_dir, args.output + ".structure.dot"))
structure_inspector.write_graph(program, os.path.join(args.output_dir, args.output + ".structure.dot"))
# Write call graph
call_inspector.write_graph(program, True, os.path.join(args.working_dir, args.output + ".call.dot"))
call_inspector.write_graph(program, False, os.path.join(args.working_dir, args.output + ".call_light.dot"))
call_inspector.write_graph(program, True, os.path.join(args.output_dir, args.output + ".call.dot"))
call_inspector.write_graph(program, False, os.path.join(args.output_dir, args.output + ".call_light.dot"))


def main():
Expand Down
4 changes: 1 addition & 3 deletions cnerator/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
from cnerator import ast
from cnerator import generators
from cnerator import type_inference

from cnerator import probs
6 changes: 3 additions & 3 deletions cnerator/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import re
import string

from cnerator import graph, utils, operators


########### Utils ##############

Expand Down Expand Up @@ -577,6 +575,7 @@ class LongDouble(NumericType):


def structs_references_graph(structs):
from cnerator import graph
references = collections.defaultdict(list)
for s in structs:
for f, t in s.fields:
Expand Down Expand Up @@ -738,7 +737,7 @@ def generate_literal(self, from_declaration, size=0):
if isinstance(self.type, (SignedChar, UnsignedChar)):
return generate_string_literal(size, from_declaration, self)
return Literal("{{ {} }}".format(
", ".join(self.type.generate_literal(from_declaration=from_declaration) for _ in range(size))
", ".join(str(self.type.generate_literal(from_declaration=from_declaration)) for _ in range(size))
), self)

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

def get_operators(c_type, _type):
from cnerator import utils, operators
operator_type = "{}_{}_operators".format(
utils.camel_case_to_snake_case(c_type.__class__.__name__),
_type
Expand Down
Loading

0 comments on commit 1379cdb

Please sign in to comment.