def create_global_symtab(self): symtab = Symtab() for (iname,obj) in self.root_ast.children(): if (isinstance(obj, c_ast.Decl)): # print # print obj.__dict__ # print ast_show(obj) if (isinstance(obj.type, c_ast.FuncDecl)): if (self.verbose): print "Ignoring funcdecl %s for now" % (obj.name) pass else: if (self.verbose): print print ast_show(obj) symtab = self.declare_variable(obj, symtab) elif (isinstance(obj, c_ast.FuncDef)): if (self.verbose): print "Ignoring funcdef %s for now" % (obj.decl.name) pass else: print obj.__class__ assert(False) symtab.declare(PseudoSymbol("_unroll"), self.dfg(Undefined)) return symtab
def handle_scope(self, node, scope): # For all buffers, insert extra variables in the scope. # The variables are also accessible from the buffer_info # on the buffer entry bufvars = [entry for name, entry in scope.entries.iteritems() if entry.type.is_buffer] if len(bufvars) > 0: bufvars.sort(key=lambda entry: entry.name) self.buffers_exists = True memviewslicevars = [entry for name, entry in scope.entries.iteritems() if entry.type.is_memoryviewslice] if len(memviewslicevars) > 0: self.buffers_exists = True for (name, entry) in scope.entries.iteritems(): if name == 'memoryview' and isinstance(entry.utility_code_definition, CythonUtilityCode): self.using_memoryview = True break if isinstance(node, ModuleNode) and len(bufvars) > 0: # for now...note that pos is wrong raise CompileError(node.pos, "Buffer vars not allowed in module scope") for entry in bufvars: if entry.type.dtype.is_ptr: raise CompileError(node.pos, "Buffers with pointer types not yet supported.") name = entry.name buftype = entry.type if buftype.ndim > Options.buffer_max_dims: raise CompileError(node.pos, "Buffer ndims exceeds Options.buffer_max_dims = %d" % Options.buffer_max_dims) if buftype.ndim > self.max_ndim: self.max_ndim = buftype.ndim # Declare auxiliary vars def decvar(type, prefix): cname = scope.mangle(prefix, name) aux_var = scope.declare_var(name=None, cname=cname, type=type, pos=node.pos) if entry.is_arg: aux_var.used = True # otherwise, NameNode will mark whether it is used return aux_var auxvars = ((PyrexTypes.c_pyx_buffer_nd_type, Naming.pybuffernd_prefix), (PyrexTypes.c_pyx_buffer_type, Naming.pybufferstruct_prefix)) pybuffernd, rcbuffer = [decvar(type, prefix) for (type, prefix) in auxvars] entry.buffer_aux = Symtab.BufferAux(pybuffernd, rcbuffer) scope.buffer_entries = bufvars self.scope = scope
def handle_scope(self, node, scope): # For all buffers, insert extra variables in the scope. # The variables are also accessible from the buffer_info # on the buffer entry bufvars = [entry for name, entry in scope.entries.iteritems() if entry.type.is_buffer] if len(bufvars) > 0: self.buffers_exists = True if isinstance(node, ModuleNode) and len(bufvars) > 0: # for now...note that pos is wrong raise CompileError(node.pos, "Buffer vars not allowed in module scope") for entry in bufvars: if entry.type.dtype.is_ptr: raise CompileError(node.pos, "Buffers with pointer types not yet supported.") name = entry.name buftype = entry.type if buftype.ndim > self.max_ndim: self.max_ndim = buftype.ndim # Declare auxiliary vars cname = scope.mangle(Naming.bufstruct_prefix, name) bufinfo = scope.declare_var(name="$%s" % cname, cname=cname, type=PyrexTypes.c_py_buffer_type, pos=node.pos) if entry.is_arg: bufinfo.used = True # otherwise, NameNode will mark whether it is used def var(prefix, idx, initval): cname = scope.mangle(prefix, "%d_%s" % (idx, name)) result = scope.declare_var("$%s" % cname, PyrexTypes.c_py_ssize_t_type, node.pos, cname=cname, is_cdef=True) result.init = initval if entry.is_arg: result.used = True return result stridevars = [var(Naming.bufstride_prefix, i, "0") for i in range(entry.type.ndim)] shapevars = [var(Naming.bufshape_prefix, i, "0") for i in range(entry.type.ndim)] mode = entry.type.mode if mode == 'full': suboffsetvars = [var(Naming.bufsuboffset_prefix, i, "-1") for i in range(entry.type.ndim)] else: suboffsetvars = None entry.buffer_aux = Symtab.BufferAux(bufinfo, stridevars, shapevars, suboffsetvars) scope.buffer_entries = bufvars self.scope = scope
def create_scope(self, param_decls, param_exprs, symtab): scope_symtab = Symtab(symtab, scope=set()) if (not (len(param_decls)==len(param_exprs))): raise ParameterListMismatchesDeclaration( "declared with %d parameters; called with %d parameters" % (len(param_decls), len(param_exprs))) for parami in range(len(param_decls)): param_decl = param_decls[parami] #print "create_scope declares %s as %s" % (ast_show(param_decl, oneline=True), param_exprs[parami]) scope_symtab = self.declare_variable( param_decl, scope_symtab, initial_value=param_exprs[parami]) if (self.verbose): print("scope symtab is: %s" % scope_symtab) return scope_symtab
def transform_if(self, statement, symtab): # returns symtab cond_state = self.decode_expression_val(statement.cond, symtab) symtab = cond_state.symtab try: # If condition is statically evaluable. Just run one branch. cond_val = self.evaluate(cond_state.expr) if (cond_val): return self.transform_statement(statement.iftrue, symtab) elif (statement.iffalse!=None): return self.transform_statement(statement.iffalse, symtab) else: # no-op. return symtab except NonconstantExpression, ex: # If condition is dynamic. Run both branches (in dedicated scopes) # and make resulting storage updates conditional on the # dynamic condition. then_scope = Symtab(symtab, scope=set()) then_symtab = self.transform_statement( statement.iftrue, then_scope) else_scope = Symtab(symtab, scope=set()) if (statement.iffalse!=None): else_symtab = self.transform_statement( statement.iffalse, else_scope) else: else_symtab = else_scope #new_symtab = Symtab(symtab) # DEAD new_symtab = symtab modified_idents = then_scope.scope.union(else_scope.scope) #print "If modifies %s" % modified_idents for key in modified_idents: expr = self.dfg(Conditional, cond_state.expr, then_symtab.lookup(key), else_symtab.lookup(key)) new_symtab.assign(key, expr) return new_symtab
def unroll_dynamic_loop(self, cond, body_compound, symtab): self.loops+=1 try: _unroll_val = self.evaluate(symtab.lookup(PseudoSymbol("_unroll"))) except UndefinedExpression: print "At line %s:" % cond.coord.line raise # build up nested conditional scopes working_symtab = symtab cond_stack = [] scope_stack = [] for i in range(_unroll_val): self.loop_msg("dyn iter %s" % i) cond_state = self.decode_expression_val(cond, working_symtab) scope = Symtab(cond_state.symtab, scope=set()) cond_stack.append(cond_state.expr) scope_stack.append(scope) #print "rep %d; k is: %s" % (i, self.eager_lookup(Symbol("k"), scope)) working_symtab = self.transform_statement(body_compound, scope) assert(_unroll_val == len(cond_stack)) while (len(cond_stack)>0): cond = cond_stack.pop() scope = scope_stack.pop() modified_idents = scope.scope #applied_symtab = Symtab(working_symtab) #DEAD applied_symtab = working_symtab for ref in modified_idents: if (self.verbose): print print "cond: %s" % cond print "iftrue: %s" % working_symtab.lookup(ref) print "iffalse: %s" % scope.parent.lookup(ref) applied_symtab.assign(ref, self.dfg(Conditional, cond, working_symtab.lookup(ref), scope.parent.lookup(ref))) working_symtab = applied_symtab self.loops-=1 return working_symtab