def generate(self, env): marking_type = env.marking_type vp = VariableProvider() self_var = vp.new_variable(marking_type.type, name='self') new_pid_dict_var = vp.new_variable(marking_type.type, name='new_pid_dict') function = pyast.FunctionDef(name='update_pids', args=pyast.A(self_var.name).param(new_pid_dict_var.name).ast()) body = [] for name, place_type in marking_type.place_types.iteritems(): print place_type if not place_type.allow_pids: continue if name == GENERATOR_PLACE: body.append( pyast.Assign(targets = [ place_type.place_expr(env, self_var) ], value = pyast.Call(func = pyast.Name(stubs['generator_place_update_pids']), args = [ place_type.place_expr(env, self_var), pyast.Name(new_pid_dict_var.name) ])) ) else: body.append( place_type.update_pids_stmt(env, self_var, new_pid_dict_var) ) if not body: body = [pyast.Pass()] function.body = body return function
def generate(self, env): marking_type = env.marking_type vp = VariableProvider() self_var = vp.new_variable(marking_type.type, name='self') new_pid_dict_var = vp.new_variable(marking_type.type, name='new_pid_dict') builder = cyast.Builder() args = (cyast.A(self_var.name, type=env.type2str(self_var.type)).param( new_pid_dict_var.name, type=env.type2str(new_pid_dict_var.type))) function = builder.FunctionCDef(name='update_pids', args=args, returns=cyast.E("void")) body = [] for place_type in marking_type.place_types.itervalues(): print place_type if not place_type.allows_pids: continue #body.append( update_pids_stmt(env, place_type, self_var, new_pid_dict_var) ) if not body: body = [cyast.Pass()] function.body = body import pprint, ast pprint.pprint(ast.dump(function)) return function
def generate(self, env): marking_type = env.marking_type items = list(marking_type.place_types.iteritems()) items.sort(lambda (n1, t1), (n2, t2) : cmp(n1, n2)) vp = VariableProvider() self_var = vp.new_variable(marking_type.type, name = 'self') list_var = vp.new_variable() builder = pyast.Builder() builder.begin_FunctionDef(name = "__dump__", args = pyast.A(self_var.name).ast()) builder.emit(pyast.E('%s = ["{"]' % list_var.name)) for (place_name, place_type) in items: if place_type.is_ProcessPlace: builder.emit(place_type.dump_expr(env, self_var, list_var)) else: builder.emit(pyast.stmt(pyast.Call(func = pyast.E('{}.append'.format(list_var.name)), args = [ pyast.BinOp(left = pyast.Str(s = repr(place_name) + " : "), op = pyast.Add(), right = pyast.BinOp(left = place_type.dump_expr(env, self_var), op = pyast.Add(), right = pyast.Str(', ')) ) ] ) ) ) builder.emit(pyast.stmt(pyast.E('%s.append("}")' % list_var.name))) builder.emit_Return(pyast.E('"\\n".join({})'.format(list_var.name))) builder.end_FunctionDef() return builder.ast()
def generate(self, env): marking_type = env.marking_type config = marking_type.config vp = VariableProvider() self_var = vp.new_variable(env.marking_type.type, 'self') builder = pyast.Builder() builder.begin_FunctionDef(name='__pid_free_hash__', args=pyast.A(self_var.name).ast()) builder.emit(pyast.E('h = 0')) for (name, place_type) in marking_type.place_types.iteritems(): magic = hash(name) type_info = place_type.token_type if type_info.is_Pid: # builder.emit( pyast.E('h ^= hash(' + place_type.field.access_from(self_var) + ') * ' + str(magic)) ) right_operand = pyast.BinOp(left=place_type.pid_free_hash_expr( env, self_var, [0]), op=pyast.Mult(), right=pyast.E(str(magic))) builder.emit( pyast.AugAssign(target=[pyast.E("h")], op=pyast.BitXor(), value=right_operand)) elif type_info.has_pids: # must be tuple assert (type_info.is_TupleType) ignore = [ i for i, subtype in enumerate(type_info) if subtype.is_Pid ] # builder.emit("{!s} = {!r}".format(ig_var.name, ig)) right_operand = pyast.BinOp(left=place_type.pid_free_hash_expr( env, self_var, ignore), op=pyast.Mult(), right=pyast.E(str(magic))) builder.emit( pyast.AugAssign(target=[pyast.E("h")], op=pyast.BitXor(), value=right_operand)) else: builder.emit( pyast.E('h ^= hash(' + place_type.field.access_from(self_var) + ') * ' + str(magic))) builder.emit_Return(pyast.E("h")) builder.end_FunctionDef() return builder.ast()
def gen_check_function(checker_env, identifier, atom): marking_type = checker_env.marking_type variable_provider = VariableProvider() checker_env.push_cvar_env() checker_env.push_variable_provider(variable_provider) builder = cyast.Builder() marking_var = variable_provider.new_variable( variable_type=marking_type.type) function_name = "check_{}".format(identifier) builder.begin_FunctionCDef(name=function_name, args=cyast.A(marking_var.name, type=checker_env.type2str( marking_var.type)), returns=cyast.Name("int"), decl=[], public=False, api=False) formula = atom.formula builder.emit( cyast.Return(gen_check_expression(checker_env, marking_var, formula))) builder.end_FunctionDef() tree = cyast.to_ast(builder) tree = flatten_ast(tree) checker_env.register_check_function(function_name, FunctionWrapper(function_name, tree)) return tree
def gen_check_function(checker_env, identifier, atom): marking_type = checker_env.marking_type variable_provider = VariableProvider() checker_env.push_cvar_env() checker_env.push_variable_provider(variable_provider) builder = cyast.Builder() marking_var = variable_provider.new_variable(variable_type = marking_type.type) function_name = "check_{}".format(identifier) builder.begin_FunctionCDef(name = function_name, args = cyast.A(marking_var.name, type = checker_env.type2str(marking_var.type)), returns = cyast.Name("int"), decl = [], public = False, api = False) formula = atom.formula builder.emit(cyast.Return(gen_check_expression(checker_env, marking_var, formula))) builder.end_FunctionDef() tree = cyast.to_ast(builder) tree = flatten_ast(tree) checker_env.register_check_function(function_name, FunctionWrapper(function_name, tree)) return tree
def generate(self, env): marking_type = env.marking_type items = list(marking_type.place_types.iteritems()) items.sort(lambda (n1, t1), (n2, t2): cmp(n1, n2)) vp = VariableProvider() self_var = vp.new_variable(marking_type.type, 'self') builder = cyast.Builder() builder.begin_FunctionDef(name='__dump__', args=cyast.A('self')) builder.emit(cyast.E('s = ["{"]')) for (place_name, place_type) in items: if place_type.is_revelant: builder.emit( cyast.stmt( cyast.Call(func=cyast.E('s.append'), args=[ cyast.BinOp( left=cyast.Str(s=repr(place_name) + " : "), op=cyast.Add(), right=cyast.BinOp( left=place_type.dump_expr( env, self_var), op=cyast.Add(), right=cyast.Str(s=','))) ]))) builder.emit(cyast.stmt(cyast.E('s.append("}")'))) builder.emit_Return(cyast.E('"\\n".join(s)')) builder.end_FunctionDef() return cyast.to_ast(builder)
def generate(self, env): marking_type = env.marking_type config = marking_type.config vp = VariableProvider() self_var = vp.new_variable(env.marking_type.type, 'self') builder = pyast.Builder() builder.begin_FunctionDef( name = '__hash__', args = pyast.A(self_var.name).ast() ) builder.begin_If(test=pyast.E('self.{} != None'.format(marking_type.get_field('_hash').name))) builder.emit_Return(pyast.E('self.{}'.format(marking_type.get_field('_hash').name))) builder.end_If() builder.emit( pyast.E('h = 0') ) for name, place_type in marking_type.place_types.iteritems(): if name == GENERATOR_PLACE and config.normalize_pids: continue magic = hash(name) builder.emit( pyast.E('h ^= hash(' + place_type.field.access_from(self_var) + ') * ' + str(magic) ) ) builder.emit(pyast.E("self.{} = h".format(marking_type.get_field('_hash').name))) # builder.emit(pyast.E("print h")) builder.emit_Return(pyast.E("h")) builder.end_FunctionDef() return builder.ast()
def generate(self, env): marking_type = env.marking_type vp = VariableProvider() self_var = vp.new_variable(marking_type.type, name='self') new_pid_dict_var = vp.new_variable(marking_type.type, name='new_pid_dict') builder = cyast.Builder() args = (cyast.A(self_var.name, type=env.type2str(self_var.type)) .param(new_pid_dict_var.name, type=env.type2str(new_pid_dict_var.type))) function = builder.FunctionCDef(name='update_pids', args=args, returns=cyast.E("void")) body = [] for place_type in marking_type.place_types.itervalues(): print place_type if not place_type.allows_pids: continue #body.append( update_pids_stmt(env, place_type, self_var, new_pid_dict_var) ) if not body: body = [cyast.Pass()] function.body = body import pprint, ast pprint.pprint(ast.dump(function)) return function
def generate(self, env): marking_type = env.marking_type config = marking_type.config vp = VariableProvider() self_var = vp.new_variable(env.marking_type.type, 'self') other_var = vp.new_variable(env.marking_type.type, 'other') function = pyast.FunctionDef(name='__eq__', args=pyast.A(self_var.name).param( other_var.name).ast()) return_str = "return (" for i, (name, place_type) in enumerate(marking_type.place_types.iteritems()): if name == GENERATOR_PLACE and config.normalize_pids: continue id_name = place_type.field if i > 0: return_str += " and " return_str += "(%s == %s)" % (id_name.access_from(self_var), id_name.access_from(other_var)) return_str += ")" function.body = [pyast.E(return_str)] return function
def gen_main_check_function(checker_env, id_prop_map): function_name = "neco_check" builder = cyast.Builder() variable_provider = VariableProvider() checker_env.push_cvar_env() checker_env.push_variable_provider(variable_provider) marking_var = variable_provider.new_variable( variable_type=checker_env.marking_type.type) atom_var = variable_provider.new_variable( variable_type=TypeInfo.get('Int')) builder.begin_FunctionCDef( name=function_name, args=(cyast.A(marking_var.name, type=checker_env.type2str(marking_var.type)).param( atom_var.name, type=checker_env.type2str(TypeInfo.get('Int')))), returns=cyast.Name("int"), decl=[], public=True, api=True) for (i, (ident, _)) in enumerate(id_prop_map.iteritems()): if i == 0: builder.begin_If( test=cyast.Compare(left=cyast.Name(atom_var.name), ops=[cyast.Eq()], comparators=[cyast.Num(ident)])) else: builder.begin_Elif( test=cyast.Compare(left=cyast.Name(atom_var.name), ops=[cyast.Eq()], comparators=[cyast.Num(ident)])) builder.emit_Return( checker_env.get_check_function("check_{}".format(ident)).call( [cyast.Name(marking_var.name)])) for _ in id_prop_map: builder.end_If() builder.emit( cyast.Print(dest=cyast.E('sys.stderr'), values=[ cyast.Str(s='!W! invalid proposition identifier'), cyast.Name(atom_var.name) ], nl=True)) builder.emit_Return(cyast.Num(n=0)) builder.end_FunctionDef() tree = cyast.to_ast(builder) checker_env.register_check_function(function_name, FunctionWrapper(function_name, tree)) return tree
def generate(self, env): marking_type = env.marking_type vp = VariableProvider() self_var = vp.new_variable(marking_type.type, 'self') other_var = vp.new_variable(marking_type.type, 'other') cmp_var = vp.new_variable(marking_type.type, 'tmp') builder = pyast.Builder() builder.begin_FunctionDef(name='pid_free_compare', args=pyast.A(self_var.name).param( other_var.name).ast()) for place_type in marking_type.place_types.values(): type_info = place_type.token_type print type_info, place_type if type_info.is_Pid: builder.emit( pyast.Assign(targets=[pyast.E(cmp_var.name)], value=place_type.pid_free_compare_expr( env, self_var, other_var, [0]))) builder.begin_If(test=pyast.E('{} != 0'.format(cmp_var.name))) builder.emit_Return(pyast.E(cmp_var.name)) builder.end_If() elif type_info.has_pids: # must be tuple assert (type_info.is_TupleType) ignore = [ i for i, subtype in enumerate(type_info) if subtype.is_Pid ] # builder.emit("{!s} = {!r}".format(ig_var.name, ig)) builder.emit( pyast.Assign(targets=[pyast.E(cmp_var.name)], value=place_type.pid_free_compare_expr( env, self_var, other_var, ignore))) builder.begin_If(test=pyast.E('{} != 0'.format(cmp_var.name))) builder.emit_Return(pyast.E(cmp_var.name)) builder.end_If() else: builder.emit( pyast.Assign(targets=[pyast.E(cmp_var.name)], value=place_type.compare_expr( env, self_var, other_var))) builder.begin_If(test=pyast.E('{} != 0'.format(cmp_var.name))) builder.emit_Return(pyast.E(cmp_var.name)) builder.end_If() builder.emit_Return(pyast.Num(0)) builder.end_FunctionDef() return builder.ast()
def generate(self, env): marking_type = env.marking_type vp = VariableProvider() self_var = vp.new_variable(marking_type.type, name='self') tree_var = vp.new_variable(TypeInfo.get('AnyType'), name='tree') pid_dict_var = vp.new_variable(TypeInfo.get('Dict'), name='pid_dict') function = cyast.Builder.FunctionCDef(name='normalize_pids', args=cyast.A(self_var.name, type=env.type2str(self_var.type)), returns=cyast.E('void')) body = [] body.append(cyast.Assign(targets=[cyast.Name(id=tree_var.name)], value=cyast.Call(func=cyast.Name(stubs['create_pid_tree']), args=[]))) # build the tree for name, place_type in marking_type.place_types.iteritems(): if not place_type.allows_pids: continue if name == GENERATOR_PLACE: body.append(cyast.stmt(cyast.Call(func=cyast.Name(stubs['generator_place_update_pid_tree']), args=[ place_type.place_expr(env, self_var), cyast.Name(id=tree_var.name) ] ))) else: body.append( add_pids_to_tree(env, place_type, self_var, tree_var)) # normalize tree and get dict body.append(cyast.Assign(targets=[cyast.Name(id=pid_dict_var.name)], value=cyast.Call(func=cyast.Name(stubs['normalize_pid_tree']), args=[cyast.Name(tree_var.name)]))) # update tokens with new pids for name, place_type in marking_type.place_types.iteritems(): if not place_type.allows_pids: continue if name == GENERATOR_PLACE: body.append( cyast.Assign(targets=[ place_type.place_expr(env, self_var) ], value=cyast.Call(func=cyast.Name(stubs['generator_place_update_pids']), args=[place_type.place_expr(env, self_var), cyast.Name(pid_dict_var.name)]) ) ) else: body.append( place_type.update_pids_stmt(env, self_var, pid_dict_var) ) if not body: body = [cyast.Pass()] function.body = body function.body = [cyast.Pass()] return function
def generate(self, env): marking_type = env.marking_type config = marking_type.config vp = VariableProvider() self_var = vp.new_variable(env.marking_type.type, 'self') builder = pyast.Builder() builder.begin_FunctionDef( name = '__pid_free_hash__', args = pyast.A(self_var.name).ast() ) builder.emit( pyast.E('h = 0') ) for (name, place_type) in marking_type.place_types.iteritems(): magic = hash(name) type_info = place_type.token_type if type_info.is_Pid: # builder.emit( pyast.E('h ^= hash(' + place_type.field.access_from(self_var) + ') * ' + str(magic)) ) right_operand = pyast.BinOp( left = place_type.pid_free_hash_expr(env, self_var, [0]), op = pyast.Mult(), right = pyast.E(str(magic)) ) builder.emit( pyast.AugAssign( target = [pyast.E("h")], op = pyast.BitXor(), value = right_operand ) ) elif type_info.has_pids: # must be tuple assert( type_info.is_TupleType ) ignore = [ i for i, subtype in enumerate(type_info) if subtype.is_Pid ] # builder.emit("{!s} = {!r}".format(ig_var.name, ig)) right_operand = pyast.BinOp( left = place_type.pid_free_hash_expr(env, self_var, ignore), op = pyast.Mult(), right = pyast.E(str(magic)) ) builder.emit( pyast.AugAssign(target = [pyast.E("h")], op = pyast.BitXor(), value = right_operand ) ) else: builder.emit( pyast.E('h ^= hash(' + place_type.field.access_from(self_var) + ') * ' + str(magic)) ) builder.emit_Return(pyast.E("h")) builder.end_FunctionDef() return builder.ast()
def generate(self, env): marking_type = env.marking_type builder = cyast.Builder() vp = VariableProvider() self_var = vp.new_variable(variable_type=marking_type.type, name='self') marking_var = vp.new_variable(variable_type=marking_type.type, name='m') builder.begin_FunctionCDef( name="copy", args=cyast.A(self_var.name), returns=cyast.E(env.type2str(marking_type.type)), decl=[cyast.Builder.CVar(name='m', type='Marking')]) builder.emit(cyast.E('m = Marking()')) copied = set() # copy packed # print marking_type.chunk_manager.packed_bits() if marking_type.chunk_manager.packed_bits() > 0: attr_name, _, count = marking_type.chunk_manager.packed_attribute() # print attr_name copied.add(attr_name) for i in range(count): target_expr = cyast.E('{object}.{attribute}[{index!s}]'.format( object=marking_var.name, attribute=attr_name, index=i)) value_expr = cyast.E('{object}.{attribute}[{index!s}]'.format( object=self_var.name, attribute=attr_name, index=i)) builder.emit( cyast.Assign(targets=[target_expr], value=value_expr)) # copy modified attributes for place_type in marking_type.place_types.itervalues(): attr_name = place_type.get_attribute_name() if attr_name in copied: continue # print attr_name target_expr = cyast.E('{object}.{attribute}'.format( object=marking_var.name, attribute=attr_name)) builder.emit(place_type.copy_stmt(env, marking_var, self_var)) builder.emit_Return(cyast.E("m")) builder.end_FunctionDef() return cyast.to_ast(builder)
def gen_All_function(checker_env, function_name, place_name, arg_function_name): marking_type = checker_env.marking_type variable_provider = VariableProvider() checker_env.push_cvar_env() checker_env.push_variable_provider(variable_provider) builder = cyast.Builder() marking_var = variable_provider.new_variable( variable_type=marking_type.type) place_type = marking_type.get_place_type_by_name(place_name) token_var = variable_provider.new_variable( variable_type=place_type.token_type) # check_var = variable_provider.new_variable(variable_type=TypeInfo.Int) builder.begin_FunctionCDef(name=function_name, args=(cyast.A(marking_var.name, type=checker_env.type2str( marking_var.type))), returns=cyast.Name("int"), decl=[], public=False, api=False) main_body = [] loop_var = variable_provider.new_variable( variable_type=place_type.token_type) inner_body = cyast.If( cyast.UnaryOp( cyast.Not(), cyast.Name(arg_function_name + '(' + loop_var.name + ')')), [cyast.Return(cyast.Num(0))]) node = place_type.enumerate_tokens(checker_env, loop_var, marking_var, body=inner_body) main_body.append(node) main_body.append(cyast.Return(value=cyast.Num(1))) for stmt in main_body: builder.emit(stmt) builder.end_FunctionDef() return cyast.to_ast(builder)
def generate(self, env): marking_type = env.marking_type vp = VariableProvider() self_var = vp.new_variable(marking_type.type, name='self') tree_var = vp.new_variable(TypeInfo.get('AnyType'), name='tree') token_var = vp.new_variable(TypeInfo.get('AnyType'), name='token') pid_dict_var = vp.new_variable(TypeInfo.get('Dict'), name='pid_dict') tmp_pid_var = vp.new_variable(TypeInfo.get('Pid'), name='tpid') tmp_marking_var = vp.new_variable(TypeInfo.get('Marking'), name='tmkr') function = pyast.FunctionDef(name='buildPidTree', args=pyast.A(self_var.name).ast()) body = [] body.append( pyast.E("{} = defaultdict(Marking)".format(pid_dict_var.name))) body.append(pyast.E("{} = PidTree(0)".format(tree_var.name))) # build the tree for name, place_type in marking_type.place_types.iteritems(): if not place_type.allow_pids: continue if name == GENERATOR_PLACE: enum_body = [ pyast.If(test=pyast.E('not {}.has_key({}[0])'.format( pid_dict_var.name, token_var.name)), body=[ pyast.E("{}[ {}[0] ] = Marking(True)".format( pid_dict_var.name, token_var.name)) ]), pyast. E("{}[ Pid.from_list({}[0].data + [{}[1] + 1]) ] = 'next_pid'" .format(pid_dict_var.name, token_var.name, token_var.name)) ] body.append( place_type.enumerate(env, self_var, token_var, enum_body)) else: body.append( place_type.extract_pids(env, self_var, pid_dict_var)) # body.append(pyast.E("print {}".format(pid_dict_var.name))) body.append( pyast.For(target=pyast.E('{}, {}'.format(tmp_pid_var.name, tmp_marking_var.name)), iter=pyast.E( ('{}.iteritems()'.format(pid_dict_var.name))), body=[ pyast.stmt( pyast.E('{}.add_marking({}, {})'.format( tree_var.name, tmp_pid_var.name, tmp_marking_var.name))) ])) body.append(pyast.E("return {}".format(tree_var.name))) function.body = body return function
def generate(self, env): marking_type = env.marking_type function = pyast.FunctionDef(name = '__init__', args = pyast.A('self').param('alloc', default = 'True').ast()) vp = VariableProvider() self_var = vp.new_variable(marking_type.type, name = 'self') if_block = pyast.If(test = pyast.Name(id = 'alloc')) for place_type in marking_type.place_types.values(): if_block.body.append(place_type.new_place_stmt(env, self_var)) function.body = [ pyast.E('self.{} = None'.format(marking_type.get_field('_hash').name)), if_block ] return function
def generate(self, env): marking_type = env.marking_type vp = VariableProvider() self_var = vp.new_variable(marking_type.type, "self") builder = cyast.Builder() builder.begin_FunctionDef(name="__dealloc__", args=cyast.A("self", type="Marking")) for place_type in marking_type.place_types.itervalues(): if place_type.is_packed or place_type.is_helper: pass else: builder.emit( place_type.delete_stmt(env=env, marking_var=self_var)) builder.end_FunctionDef() return cyast.to_ast(builder)
def generate(self, env): marking_type = env.marking_type vp = VariableProvider() self_var = vp.new_variable(marking_type.type, 'self') other_var = vp.new_variable(marking_type.type, 'other') cmp_var = vp.new_variable(marking_type.type, 'tmp') builder = pyast.Builder() builder.begin_FunctionDef(name = 'pid_free_compare', args = pyast.A(self_var.name).param(other_var.name).ast()) for place_type in marking_type.place_types.values(): type_info = place_type.token_type print type_info, place_type if type_info.is_Pid: builder.emit( pyast.Assign(targets = [pyast.E(cmp_var.name)], value = place_type.pid_free_compare_expr(env, self_var, other_var, [0]) ) ) builder.begin_If(test=pyast.E('{} != 0'.format(cmp_var.name))) builder.emit_Return( pyast.E(cmp_var.name) ) builder.end_If() elif type_info.has_pids: # must be tuple assert( type_info.is_TupleType ) ignore = [ i for i, subtype in enumerate(type_info) if subtype.is_Pid ] # builder.emit("{!s} = {!r}".format(ig_var.name, ig)) builder.emit( pyast.Assign(targets = [pyast.E(cmp_var.name)], value = place_type.pid_free_compare_expr(env, self_var, other_var, ignore) ) ) builder.begin_If(test = pyast.E('{} != 0'.format(cmp_var.name))) builder.emit_Return(pyast.E(cmp_var.name)) builder.end_If() else: builder.emit( pyast.Assign(targets = [pyast.E(cmp_var.name)], value = place_type.compare_expr(env, self_var, other_var) ) ) builder.begin_If(test=pyast.E('{} != 0'.format(cmp_var.name))) builder.emit_Return( pyast.E(cmp_var.name) ) builder.end_If() builder.emit_Return(pyast.Num(0)) builder.end_FunctionDef() return builder.ast()
def extract_pids(self, env, marking_var, dict_var): place_expr = pyast.E(self.field.access_from(marking_var)) vp = VariableProvider() token_var = vp.new_variable(TypeInfo.get('AnyType'), name='pid') place_in_dict_marking = self.field.access_from_str( "{pid_dict}[ {token} ]".format(pid_dict=dict_var.name, token=token_var.name)) body = [ pyast.stmt( pyast.E("{marking}.add({token})".format( marking=place_in_dict_marking, token=token_var.name))) ] return pyast.For(target=pyast.E(token_var.name), iter=place_expr, body=body)
def extract_pids(self, env, marking_var, dict_var): place_expr = pyast.E(self.field.access_from(marking_var)) vp = VariableProvider() token_var = vp.new_variable(TypeInfo.get('AnyType'), name='token') if not self.token_type.has_pids: return [] if self.token_type.is_Pid: raise RuntimeError elif self.token_type.is_TupleType: print "!!! TUPLE >> ", self.token_type for index, subtype in enumerate(self.token_type): body = [] if subtype.is_Pid: if index == 0: # head is pid, owned token place_in_dict_marking = self.field.access_from_str( "{pid_dict}[ {token}[{index}] ]".format( pid_dict=dict_var.name, token=token_var.name, index=index)) body.append( pyast.stmt( pyast.E("{marking}.add({token})".format( marking=place_in_dict_marking, token=token_var.name)))) else: place_in_dict_marking = "{pid_dict}[ {token}[{index}] ]".format( pid_dict=dict_var.name, token=token_var.name, index=index) body.append( pyast.E("{marking} = Marking(True)".format( marking=place_in_dict_marking))) return pyast.For(target=pyast.E(token_var.name), iter=place_expr, body=body) else: print "!!! DATA >> ", self.token_type return []
def gen_main_check_function(checker_env, id_prop_map): function_name = "neco_check" builder = cyast.Builder() variable_provider = VariableProvider() checker_env.push_cvar_env() checker_env.push_variable_provider(variable_provider) marking_var = variable_provider.new_variable(variable_type = checker_env.marking_type.type) atom_var = variable_provider.new_variable(variable_type = TypeInfo.get('Int')) builder.begin_FunctionCDef(name = function_name, args = (cyast.A(marking_var.name, type = checker_env.type2str(marking_var.type)) .param(atom_var.name, type = checker_env.type2str(TypeInfo.get('Int')))), returns = cyast.Name("int"), decl = [], public = True, api = True) for (i, (ident, _)) in enumerate(id_prop_map.iteritems()): if i == 0: builder.begin_If(test = cyast.Compare(left = cyast.Name(atom_var.name), ops = [ cyast.Eq() ], comparators = [ cyast.Num(ident) ])) else: builder.begin_Elif(test = cyast.Compare(left = cyast.Name(atom_var.name), ops = [ cyast.Eq() ], comparators = [ cyast.Num(ident) ])) builder.emit_Return(checker_env.get_check_function("check_{}".format(ident)).call([cyast.Name(marking_var.name)])) for _ in id_prop_map: builder.end_If() builder.emit(cyast.Print(dest = cyast.E('sys.stderr'), values = [cyast.Str(s = '!W! invalid proposition identifier'), cyast.Name(atom_var.name)], nl = True)) builder.emit_Return(cyast.Num(n = 0)) builder.end_FunctionDef() tree = cyast.to_ast(builder) checker_env.register_check_function(function_name, FunctionWrapper(function_name, tree)) return tree
def gen_InPlace_function(checker_env, function_name, place_name): marking_type = checker_env.marking_type variable_provider = VariableProvider() checker_env.push_cvar_env() checker_env.push_variable_provider(variable_provider) builder = cyast.Builder() marking_var = variable_provider.new_variable(variable_type = marking_type.type) place_type = marking_type.get_place_type_by_name(place_name) token_var = variable_provider.new_variable(variable_type = place_type.token_type) # check_var = variable_provider.new_variable(variable_type=TypeInfo.Int) builder.begin_FunctionCDef(name = function_name, args = (cyast.A(marking_var.name, type = checker_env.type2str(marking_var.type)) .param(name = token_var.name, type = checker_env.type2str(token_var.type))), returns = cyast.Name("int"), decl = [], public = False, api = False) main_body = [] loop_var = variable_provider.new_variable(variable_type = place_type.token_type) inner_body = cyast.If(cyast.Compare(cyast.Name(token_var.name), [ cyast.Eq() ], [ cyast.Name(loop_var.name) ]), [ cyast.Return(cyast.Num(1)) ]) node = place_type.enumerate_tokens(checker_env, loop_var, marking_var, body = inner_body) main_body.append(node) main_body.append(cyast.Return(value = cyast.Num(0))) for stmt in main_body: builder.emit(stmt) builder.end_FunctionDef() return FunctionWrapper(function_name, cyast.to_ast(builder))
def generate(self, env): marking_type = env.marking_type vp = VariableProvider() self_var = vp.new_variable(marking_type.type, name = 'self') marking_var = vp.new_variable(marking_type.type) function = pyast.FunctionDef(name = 'copy', args = pyast.A(self_var.name).param('alloc', default = 'True').ast()) tmp = [ pyast.Assign(targets = [pyast.Name(id = marking_var.name)], value = pyast.E('Marking(False)')) ] for place_type in marking_type.place_types.values(): tmp.append(place_type.copy_stmt(env, marking_var, self_var)) tmp.append(pyast.Return(pyast.Name(id = marking_var.name))) function.body = tmp return function
def generate(self, env): marking_type = env.marking_type vp = VariableProvider() builder = cyast.Builder() left_marking_var = vp.new_variable(marking_type.type, "self") right_marking_var = vp.new_variable(marking_type.type, "other") builder.begin_FunctionCDef(name = "neco_marking_compare", args = (cyast.A("self", type = env.type2str(marking_type.type)) .param(right_marking_var.name, type = env.type2str(marking_type.type))), returns = cyast.E("int"), public = True, api = True, decl = [ cyast.Builder.CVar(name = 'tmp', type = env.type2str(TypeInfo.get('Int'))) ]) compared = set() tests = [] if marking_type.chunk_manager.packed_bits() > 0: attr, _, count = marking_type.chunk_manager.packed_attribute() compared.add(attr) for index in range(0, count): left = cyast.E("{object}.{attribute}[{index}]".format(object = left_marking_var.name, attribute = attr, index = index)) right = cyast.E("{object}.{attribute}[{index}]".format(object = right_marking_var.name, attribute = attr, index = index)) tests.append(cyast.BinOp(left = left, op = cyast.Sub(), right = right)) for place_type in marking_type.place_types.itervalues(): if place_type.get_attribute_name() in compared: continue tests.append(place_type.compare_expr(env, left_marking_var = left_marking_var, right_marking_var = right_marking_var) ) tests.reverse() _gen_C_compare_aux(builder, tests) builder.end_FunctionDef() return cyast.to_ast(builder)
def generate(self, env): marking_type = env.marking_type vp = VariableProvider() self_var = vp.new_variable(marking_type.type, 'self') other_var = vp.new_variable(marking_type.type, 'other') function = pyast.FunctionDef(name = '__eq__', args = pyast.A(self_var.name).param(other_var.name).ast()) return_str = "return (" for i, place_type in enumerate(marking_type.place_types.values()): if i > 0: return_str += " and " field = place_type.field return_str += "({} == {})".format(field.access_from(self_var), field.access_from(other_var)) return_str += ")" function.body = [ pyast.E(return_str) ] return function
def generate(self, env): marking_type = env.marking_type items = list(marking_type.place_types.iteritems()) items.sort(lambda (n1, t1), (n2, t2): cmp(n1, n2)) vp = VariableProvider() self_var = vp.new_variable(marking_type.type, "self") builder = cyast.Builder() builder.begin_FunctionDef(name="__repr__", args=cyast.A(self_var.name, type=env.type2str( marking_type.type))) builder.emit(cyast.E('s = "hdict({"')) visited = set() for i, (place_name, place_type) in enumerate(items): tmp = ',\n' if i > 0 else '' if place_type.is_packed: if place_type.pack in visited: continue place = marking_type.gen_get_place(env, marking_var=self_var, place_name=place_name) str_call = cyast.E('str').call([place]) builder.emit( cyast.E('s').add_assign( cyast.E("{tmp}'{place_name}' :".format( tmp=tmp, place_name=place_name)).add(str_call))) else: builder.emit( cyast.E('s').add_assign( cyast.E(tmp + "'" + place_name + "' : ").add( cyast.E('repr(self.{place})'.format( place=marking_type.id_provider.get( place_type)))))) builder.emit(cyast.E('s += "})"')) builder.emit_Return(cyast.E("s")) builder.end_FunctionDef() return cyast.to_ast(builder)
def generate(self, env): marking_type = env.marking_type items = list(marking_type.place_types.iteritems()) items.sort(lambda (n1, t1), (n2, t2): cmp(n1, n2)) builder = cyast.Builder() vp = VariableProvider() self_var = vp.new_variable(marking_type.type, "self") builder.begin_FunctionDef(name="__str__", args=cyast.A(self_var.name, type=env.type2str( marking_type.type))) builder.emit(cyast.E('s = ""')) first = True for (place_name, place_type) in items: place_type = marking_type.get_place_type_by_name(place_name) if not place_type.is_revelant: continue if not first: builder.emit(cyast.E('s += ", "')) first = False builder.emit(cyast.E('s += %s' % repr(place_name + ': '))) builder.emit( cyast.AugAssign(target=cyast.Name('s'), op=cyast.Add(), value=cyast.Call( func=cyast.Name("str"), args=[place_type.dump_expr(env, self_var)]))) builder.emit_Return(cyast.E('s')) builder.end_FunctionDef() return cyast.to_ast(builder)
def generate(self, env): marking_type = env.marking_type vp = VariableProvider() self_var = vp.new_variable(marking_type.type, "self") builder = cyast.Builder() builder.begin_FunctionDef(name="__cinit__", args=cyast.A("self").param("alloc", default="False")) builder.begin_If(cyast.Name('alloc')) initialized = set() # packed if marking_type.chunk_manager.packed_bits() > 0: (attr_name, _, count) = marking_type.chunk_manager.packed_attribute() initialized.add(attr_name) for index in range(0, count): builder.emit( cyast.E("{object}.{attribute}[{index!s}] = 0".format( object=self_var.name, attribute=attr_name, index=index))) # init other places for place_type in marking_type.place_types.itervalues(): attr_name = place_type.get_attribute_name() if attr_name in initialized: continue ################################################################################ # assumes that packed types will always be initialized to 0 !!! ################################################################################ builder.emit(place_type.new_place_stmt(env, self_var)) builder.emit( builder.Comment("{} - 1s: {} - {}".format( place_type.info.name, place_type.one_safe(), place_type.info.type))) builder.end_If() builder.end_FunctionDef() return cyast.to_ast(builder)
def generate(self, env): marking_type = env.marking_type vp = VariableProvider() self_var = vp.new_variable(marking_type.type, name='self') new_pid_dict_var = vp.new_variable(marking_type.type, name='new_pid_dict') function = pyast.FunctionDef(name='update_pids', args=pyast.A(self_var.name).param( new_pid_dict_var.name).ast()) body = [] for name, place_type in marking_type.place_types.iteritems(): print place_type if not place_type.allow_pids: continue if name == GENERATOR_PLACE: body.append( pyast.Assign( targets=[place_type.place_expr(env, self_var)], value=pyast.Call(func=pyast.Name( stubs['generator_place_update_pids']), args=[ place_type.place_expr( env, self_var), pyast.Name(new_pid_dict_var.name) ]))) else: body.append( place_type.update_pids_stmt(env, self_var, new_pid_dict_var)) if not body: body = [pyast.Pass()] function.body = body return function
def generate(self, env): marking_type = env.marking_type vp = VariableProvider() self_var = vp.new_variable(marking_type.type, 'self') builder = pyast.Builder() builder.begin_FunctionDef(name = '__hash__', args = pyast.A(self_var.name).ast()) builder.begin_If(test = pyast.E('self.{} != None'.format(marking_type.get_field('_hash').name))) builder.emit_Return(pyast.E('self.{}'.format(marking_type.get_field('_hash').name))) builder.end_If() builder.emit(pyast.E('h = 0')) for (name, place_type) in marking_type.place_types.iteritems(): magic = hash(name) builder.emit(pyast.E('h ^= hash(' + place_type.field.access_from(self_var) + ') * ' + str(magic))) # builder.emit(pyast.E("print h")) builder.emit_Return(pyast.E("h")) builder.end_FunctionDef() return builder.ast()
def generate(self, env): marking_type = env.marking_type config = marking_type.config vp = VariableProvider() self_var = vp.new_variable(env.marking_type.type, 'self') other_var = vp.new_variable(env.marking_type.type, 'other') function = pyast.FunctionDef(name = '__eq__', args = pyast.A(self_var.name).param(other_var.name).ast()) return_str = "return (" for i, (name, place_type) in enumerate(marking_type.place_types.iteritems()): if name == GENERATOR_PLACE and config.normalize_pids: continue id_name = place_type.field if i > 0: return_str += " and " return_str += "(%s == %s)" % (id_name.access_from(self_var), id_name.access_from(other_var)) return_str += ")" function.body = [ pyast.E(return_str) ] return function
def generate(self, env): marking_type = env.marking_type vp = VariableProvider() self_var = vp.new_variable(marking_type.type, name='self') tree_var = vp.new_variable(TypeInfo.get('AnyType'), name='tree') token_var = vp.new_variable(TypeInfo.get('AnyType'), name='token') pid_dict_var = vp.new_variable(TypeInfo.get('Dict'), name='pid_dict') tmp_pid_var = vp.new_variable(TypeInfo.get('Pid'), name='tpid') tmp_marking_var = vp.new_variable(TypeInfo.get('Marking'), name='tmkr') function = pyast.FunctionDef(name = 'buildPidTree', args = pyast.A(self_var.name).ast()) body = [] body.append( pyast.E("{} = defaultdict(Marking)".format(pid_dict_var.name)) ) body.append( pyast.E("{} = PidTree(0)".format(tree_var.name)) ) # build the tree for name, place_type in marking_type.place_types.iteritems(): if not place_type.allow_pids: continue if name == GENERATOR_PLACE: enum_body = [ pyast.If(test=pyast.E('not {}.has_key({}[0])'.format(pid_dict_var.name, token_var.name)), body=[pyast.E("{}[ {}[0] ] = Marking(True)".format(pid_dict_var.name, token_var.name))]), pyast.E("{}[ Pid.from_list({}[0].data + [{}[1] + 1]) ] = 'next_pid'".format(pid_dict_var.name, token_var.name, token_var.name)) ] body.append( place_type.enumerate( env, self_var, token_var, enum_body ) ) else: body.append( place_type.extract_pids(env, self_var, pid_dict_var) ) # body.append(pyast.E("print {}".format(pid_dict_var.name))) body.append(pyast.For(target=pyast.E('{}, {}'.format(tmp_pid_var.name, tmp_marking_var.name)), iter=pyast.E(('{}.iteritems()'.format(pid_dict_var.name))), body=[pyast.stmt(pyast.E('{}.add_marking({}, {})'.format(tree_var.name, tmp_pid_var.name, tmp_marking_var.name)))])) body.append(pyast.E("return {}".format(tree_var.name))) function.body = body return function
def generate(self, env): marking_type = env.marking_type vp = VariableProvider() self_var = vp.new_variable(marking_type.type, 'self') builder = cyast.Builder() builder.begin_FunctionDef( name="__hash__", args=cyast.A("self", type="Marking"), decl=[cyast.Builder.CVar(name='h', type='int')]) builder.emit(cyast.E("h = 0xDEADDAD")) mult = 0xBADBEEF i = 0 hashed = set() if marking_type.chunk_manager.packed_bits() > 0: attr_name, _, count = marking_type.chunk_manager.packed_attribute() hashed.add(attr_name) attr = "{}.{}".format(self_var.name, attr_name) for index in range(0, count): attr_expr = cyast.E('{!s}[{!s}]'.format(attr, index)) builder.emit( cyast.Assign(targets=[cyast.Name('h')], value=cyast.BinOp(left=cyast.BinOp( left=cyast.Name('h'), op=cyast.BitXor(), right=attr_expr), op=cyast.Mult(), right=cyast.Num(mult)))) mult = (mult + (82520L + i + i)) % sys.maxint i += 1 for place_type in marking_type.place_types.itervalues(): # if place_type.is_packed or place_type.is_helper: # pass # else: # if place_type.type.is_Int or place_type.type.is_Short or place_type.type.is_Char: # native_place = marking_type.id_provider.get(place_type) # builder.emit(cyast.E('h = (h ^ self.{place_name}) * {mult}'.format(place_name=native_place, # mult=mult)) # ) # else: if place_type.get_attribute_name() in hashed: continue place_hash = place_type.hash_expr(env, marking_var=self_var) builder.emit( cyast.Assign(targets=[cyast.Name('h')], value=cyast.BinOp(left=cyast.BinOp( left=cyast.Name('h'), op=cyast.BitXor(), right=place_hash), op=cyast.Mult(), right=cyast.Num(mult)))) mult = (mult + (82521 * i + i)) % sys.maxint i += 1 builder.emit_Return(cyast.E("h")) builder.end_FunctionDef() return cyast.to_ast(builder)
def generate(self, env): marking_type = env.marking_type vp = VariableProvider() self_var = vp.new_variable(marking_type.type, name='self') tree_var = vp.new_variable(TypeInfo.get('AnyType'), name='tree') pid_dict_var = vp.new_variable(TypeInfo.get('Dict'), name='pid_dict') function = cyast.Builder.FunctionCDef(name='normalize_pids', args=cyast.A(self_var.name, type=env.type2str( self_var.type)), returns=cyast.E('void')) body = [] body.append( cyast.Assign(targets=[cyast.Name(id=tree_var.name)], value=cyast.Call(func=cyast.Name( stubs['create_pid_tree']), args=[]))) # build the tree for name, place_type in marking_type.place_types.iteritems(): if not place_type.allows_pids: continue if name == GENERATOR_PLACE: body.append( cyast.stmt( cyast.Call(func=cyast.Name( stubs['generator_place_update_pid_tree']), args=[ place_type.place_expr(env, self_var), cyast.Name(id=tree_var.name) ]))) else: body.append( add_pids_to_tree(env, place_type, self_var, tree_var)) # normalize tree and get dict body.append( cyast.Assign(targets=[cyast.Name(id=pid_dict_var.name)], value=cyast.Call(func=cyast.Name( stubs['normalize_pid_tree']), args=[cyast.Name(tree_var.name)]))) # update tokens with new pids for name, place_type in marking_type.place_types.iteritems(): if not place_type.allows_pids: continue if name == GENERATOR_PLACE: body.append( cyast.Assign( targets=[place_type.place_expr(env, self_var)], value=cyast.Call(func=cyast.Name( stubs['generator_place_update_pids']), args=[ place_type.place_expr( env, self_var), cyast.Name(pid_dict_var.name) ]))) else: body.append( place_type.update_pids_stmt(env, self_var, pid_dict_var)) if not body: body = [cyast.Pass()] function.body = body function.body = [cyast.Pass()] return function