def compile_Succs(self, node): body = [] body.extend(self.compile(node.body)) body.append(cyast.E("return " + node.arg_marking_acc_var.name)) f1 = cyast.Builder.FunctionCpDef( name=node.function_name, args=self.main_succ_function_args(node), body=body, lang=cyast.CpDef(public=True), returns=cyast.Name("set"), decl=[ cyast.CVar(name=node.arg_marking_acc_var.name, type=self.env.type2str( node.arg_marking_acc_var.type), init=self.env.marking_set_type.new_marking_set_expr( self.env)) ]) body = [cyast.E("l = ctypes_ext.neco_list_new()")] body.append( cyast.For( target=cyast.to_ast(cyast.E("e")), iter=cyast.to_ast(cyast.E("succs(m, ctx)")), body=[ cyast.to_ast( cyast.stmt(cyast.E("ctypes_ext.__Pyx_INCREF(e)"))), cyast.Expr( cyast.Call( func=cyast.to_ast( cyast.E("ctypes_ext.neco_list_push_front")), args=[cyast.to_ast(cyast.E("l")), cyast.Name("e")], keywords=[], starargs=None, kwargs=None)) ])) body.append(cyast.E("return l")) f2 = cyast.Builder.FunctionCDef( name="neco_succs", args=(cyast.A( "m", type=self.env.type2str(node.arg_marking_var.type)).param( "ctx", type=self.env.type2str(node.arg_ctx_var.type))), body=body, returns=cyast.Name("ctypes_ext.neco_list_t*"), decl=[ cyast.CVar(name="l", type="ctypes_ext.neco_list_t*"), cyast.CVar(name="e", type="Marking") ]) return [f1]
def compile_SuccT(self, node): self.env.push_cvar_env() self.env.push_variable_provider(node.variable_provider) self.var_helper = node.transition_info.variable_helper stmts = [ self.compile(node.body) ] decl = CVarSet() input_arcs = node.transition_info.input_arcs for input_arc in input_arcs: decl.extend(self.try_gen_type_decl(input_arc)) inter_vars = node.transition_info.intermediary_variables for var in inter_vars: if (not var.type.is_UserType) or self.env.is_cython_type(var.type): decl.add(cyast.CVar(name = var.name, type = self.env.type2str(var.type)) ) additionnal_decls = self.env.pop_cvar_env() for var in additionnal_decls: decl.add(var) result = cyast.to_ast(cyast.Builder.FunctionDef(name = node.function_name, args = self.succ_function_args(node), body = stmts, lang = cyast.CDef(public = True), returns = cyast.Name(""), decl = decl)) return result
def compile_IsFireable(self, node): self.env.push_cvar_env() self.env.push_variable_provider(node.variable_provider) self.var_helper = node.transition_info.variable_helper stmts = [self.compile(node.body)] decl = netir.CVarSet() input_arcs = node.transition_info.input_arcs for input_arc in input_arcs: decl.extend(self.try_gen_type_decl(input_arc)) inter_vars = node.transition_info.intermediary_variables for var in inter_vars: if (not var.type.is_UserType) or self.env.is_cython_type(var.type): decl.add( cyast.CVar(name=var.name, type=self.env.type2str(var.type))) additionnal_decls = self.env.pop_cvar_env() for var in additionnal_decls: decl.add(var) result = cyast.to_ast( cyast.Builder.FunctionDef(name=node.function_name, args=(cyast.A( node.arg_marking_var.name, type=self.env.type2str( node.arg_marking_var.type))), body=stmts, lang=cyast.CDef(public=False), returns=cyast.Name("int"), decl=decl)) return result
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_api(self, env): cls = cyast.Builder.ClassCDef(name="Marking", bases=[]) ################################################################################ # methods ################################################################################ for method in self.generate_methods(env): cls.add_method(method) # cls.add_method( self.gen_init_method(env) ) # cls.add_method( self.gen_dealloc_method(env) ) # cls.add_method( self.gen_str_method(env) ) # cls.add_method( self.gen_richcmp_method(env) ) # cls.add_method( self.gen_hash_method(env) ) # cls.add_method( self.gen_copy_method(env) ) # cls.add_method( self.dump_expr_method(env) ) ################################################################################ # comments ################################################################################ # # attributes = set() # for place_type in self.place_types.itervalues(): # if place_type.is_packed: # attributes.add("{attribute}[{offset}]".format(attribute=self.id_provider.get(self._pack), # offset=self._pack.get_field_native_offset(place_type))) # else: # attributes.add(self.id_provider.get(place_type)) # attribute_max = max(len(attr) for attr in attributes) # # comms = set([]) # for place_type in self.place_types.itervalues(): # if place_type.is_packed: # attr = "{attribute}[{offset}]".format(attribute=self.id_provider.get(self._pack), # offset=self._pack.get_field_native_offset(place_type)) # else: # attr = self.id_provider.get(place_type) # comms.add("{info} - packed: {packed:1} - attribute: {attribute:{attribute_max}} #" # .format(info=place_type.info, # packed=place_type.is_packed, # attribute=attr, # attribute_max=attribute_max)) # max_length = max(len(x) - 2 for x in comms) # comms = list(comms) # comms.insert(0, "{text:*^{max_length}} #".format(text=' Marking Structure ', max_length=max_length)) # comms.append("{text:*^{max_length}} #".format(text='*', max_length=max_length)) # # comms_ast = [ cyast.NComment(comm) for comm in comms ] # cls.add_decl(comms_ast) ################################################################################ # C api ################################################################################ # capi = [] # capi.append( self._gen_C_hash(env) ) # capi.append( self._gen_C_copy(env) ) # capi.append( self._gen_C_compare(env) ) # capi.append( self._gen_C_dump(env) ) return [cyast.to_ast(cls), self.generate_C_functions(env)]
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_copy(self, env, src_marking, dst_marking, modified_places): """ @param modified_places: @type modified_places: C{} """ nodes = [] nodes.append(cyast.E(dst_marking.name + " = Marking()")) copy_attributes = set() assign_attributes = set() for place_type in self.place_types.itervalues(): if place_type.info in modified_places: copy_attributes.add(place_type.get_attribute_name()) else: assign_attributes.add(place_type.get_attribute_name()) # a place in copy from a pack forces the copy of the whole pack assign_attributes = assign_attributes - copy_attributes copied = set() # copy packed if self.chunk_manager.packed_bits() > 0: attr_name, _, count = self.chunk_manager.packed_attribute() copied.add(attr_name) for i in range(count): target_expr = cyast.E('{object}.{attribute}[{index!s}]'.format( object=dst_marking.name, attribute=attr_name, index=i)) value_expr = cyast.E('{object}.{attribute}[{index!s}]'.format( object=src_marking.name, attribute=attr_name, index=i)) nodes.append( cyast.Assign(targets=[target_expr], value=value_expr)) # copy modified attributes for place_type in self.place_types.itervalues(): attr_name = place_type.get_attribute_name() if attr_name in copied: continue if attr_name in copy_attributes: nodes.append( place_type.copy_stmt(env, dst_marking, src_marking)) nodes.append( cyast.Comment('copy: {} {!s}'.format( place_type.info.name, place_type.info.type))) elif attr_name in assign_attributes: nodes.append( place_type.light_copy_stmt(env, dst_marking, src_marking)) nodes.append( cyast.Comment('assign: {} {!s}'.format( place_type.info.name, place_type.info.type))) copied.add(attr_name) return cyast.to_ast(nodes)
def compile_Succs(self, node): body = [] body.extend(self.compile(node.body)) body.append(cyast.E("return " + node.arg_marking_acc_var.name)) f1 = cyast.Builder.FunctionCpDef(name = node.function_name, args = self.main_succ_function_args(node), body = body, lang = cyast.CpDef(public = True), returns = cyast.Name("set"), decl = [ cyast.CVar(name = node.arg_marking_acc_var.name, type = self.env.type2str(node.arg_marking_acc_var.type), init = self.env.marking_set_type.new_marking_set_expr(self.env)) ] ) body = [ cyast.E("l = ctypes_ext.neco_list_new()") ] body.append(cyast.For(target = cyast.to_ast(cyast.E("e")), iter = cyast.to_ast(cyast.E("succs(m, ctx)")), body = [ cyast.to_ast(cyast.stmt(cyast.E("ctypes_ext.__Pyx_INCREF(e)"))), cyast.Expr(cyast.Call(func = cyast.to_ast(cyast.E("ctypes_ext.neco_list_push_front")), args = [cyast.to_ast(cyast.E("l")), cyast.Name("e")], keywords = [], starargs = None, kwargs = None)) ])) body.append(cyast.E("return l")) f2 = cyast.Builder.FunctionCDef(name = "neco_succs", args = (cyast.A("m", type = self.env.type2str(node.arg_marking_var.type)) .param("ctx", type = self.env.type2str(node.arg_ctx_var.type))), body = body, returns = cyast.Name("ctypes_ext.neco_list_t*"), decl = [cyast.CVar(name = "l", type = "ctypes_ext.neco_list_t*"), cyast.CVar(name = "e", type = "Marking")] ) return [f1]
def generate_pxd(self, env): cls = cyast.Builder.PublicClassCDef(name="Marking", bases=[cyast.E("object")], spec=cyast.type_name_spec( o="Marking", t="MarkingType")) ################################################################################ # attributes ################################################################################ if self.chunk_manager.packed_bits() > 0: (attr_name, attr_type, count) = self.chunk_manager.packed_attribute() cls.add_decl( cyast.CVar(attr_name + '[' + str(count) + ']', type=env.type2str(attr_type))) for chunk in self.chunk_manager.normal_chunks: attr_name = chunk.get_attribute_name() attr_type = chunk.get_cython_type() cls.add_decl( cyast.CVar(name=attr_name, type=env.type2str(attr_type))) # place = chunk_place_map[attr_name] cls.add_decl(cyast.Comment("{}".format(chunk.hint))) cls.add_method( cyast.FunctionDecl(name='copy', args=cyast.to_ast( cyast.A("self", cyast.Name(env.type2str( self.type)))), returns=cyast.Name(env.type2str(self.type)), lang=cyast.CDef())) return cyast.to_ast(cls)
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 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))