def OP_DEBUG_PRINT(self, op): # XXX from pypy.rpython.lltypesystem.rstr import STR format = [] argv = [] for arg in op.args: T = arg.concretetype if T == Ptr(STR): if isinstance(arg, Constant): format.append(''.join(arg.value.chars).replace('%', '%%')) else: format.append('%s') argv.append('RPyString_AsCharP(%s)' % self.expr(arg)) continue elif T == Signed: format.append('%d') elif T == Float: format.append('%f') elif isinstance(T, Ptr) or T == Address: format.append('%p') elif T == Char: if isinstance(arg, Constant): format.append(arg.value.replace('%', '%%')) continue format.append('%c') elif T == Bool: format.append('%s') argv.append('(%s) ? "True" : "False"' % self.expr(arg)) continue else: raise Exception("don't know how to debug_print %r" % (T, )) argv.append(self.expr(arg)) return "fprintf(stderr, %s%s); RPyString_FreeCache();" % ( c_string_constant(' '.join(format) + '\n\000'), ''.join( [', ' + s for s in argv]))
def OP_DEBUG_PRINT(self, op): # XXX from pypy.rpython.lltypesystem.rstr import STR format = [] argv = [] for arg in op.args: T = arg.concretetype if T == Ptr(STR): if isinstance(arg, Constant): format.append(''.join(arg.value.chars).replace('%', '%%')) else: format.append('%s') argv.append('RPyString_AsString(%s)' % self.expr(arg)) continue elif T == Signed: format.append('%d') elif T == Float: format.append('%f') elif isinstance(T, Ptr) or T in (Address, WeakGcAddress): format.append('%p') elif T == Char: if isinstance(arg, Constant): format.append(arg.value.replace('%', '%%')) continue format.append('%c') else: raise Exception("don't know how to debug_print %r" % (T,)) argv.append(self.expr(arg)) return "fprintf(stderr, %s%s);" % ( c_string_constant(' '.join(format) + '\n\000'), ''.join([', ' + s for s in argv]))
def _op_debug(self, opname, arg): if isinstance(arg, Constant): string_literal = c_string_constant(''.join(arg.value.chars)) return "%s(%s);" % (opname, string_literal) else: x = "%s(RPyString_AsCharP(%s));\n" % (opname, self.expr(arg)) x += "RPyString_FreeCache();" return x
def OP_DEBUG_FATALERROR(self, op): # XXX from pypy.rpython.lltypesystem.rstr import STR msg = op.args[0] assert msg.concretetype == Ptr(STR) if isinstance(msg, Constant): msg = c_string_constant(''.join(msg.value.chars)) else: msg = 'RPyString_AsCharP(%s)' % self.expr(msg) return 'fprintf(stderr, "%%s\\n", %s); abort();' % msg
def OP_DEBUG_PRINT(self, op): # XXX from pypy.rpython.lltypesystem.rstr import STR format = [] argv = [] free_line = "" for arg in op.args: T = arg.concretetype if T == Ptr(STR): if isinstance(arg, Constant): format.append(''.join(arg.value.chars).replace('%', '%%')) else: format.append('%s') argv.append('RPyString_AsCharP(%s)' % self.expr(arg)) free_line = "RPyString_FreeCache();" continue elif T == Signed: format.append('%ld') elif T == Unsigned: format.append('%lu') elif T == Float: format.append('%f') elif isinstance(T, Ptr) or T == Address: format.append('%p') elif T == Char: if isinstance(arg, Constant): format.append(arg.value.replace('%', '%%')) continue format.append('%c') elif T == Bool: format.append('%s') argv.append('(%s) ? "True" : "False"' % self.expr(arg)) continue elif T == SignedLongLong: if sys.platform == 'win32': format.append('%I64d') else: format.append('%lld') elif T == UnsignedLongLong: if sys.platform == 'win32': format.append('%I64u') else: format.append('%llu') else: raise Exception("don't know how to debug_print %r" % (T,)) argv.append(self.expr(arg)) argv.insert(0, c_string_constant(' '.join(format) + '\n')) return ( "if (PYPY_HAVE_DEBUG_PRINTS) { fprintf(PYPY_DEBUG_FILE, %s); %s}" % (', '.join(argv), free_line))
def OP_DEBUG_ASSERT(self, op): return 'RPyAssert(%s, %s);' % (self.expr(op.args[0]), c_string_constant(op.args[1].value))
def OP_DEBUG_STOP(self, op): arg = op.args[0] assert isinstance(arg, Constant) return "PYPY_DEBUG_STOP(%s);" % (c_string_constant(''.join( arg.value.chars)), )
def gen_source(database, modulename, targetdir, defines={}, exports={}, symboltable=None): assert not database.standalone if isinstance(targetdir, str): targetdir = py.path.local(targetdir) filename = targetdir.join(modulename + '.c') f = filename.open('w') incfilename = targetdir.join('common_header.h') fi = incfilename.open('w') # # Header # print >> f, '#include "common_header.h"' print >> f for key, value in defines.items(): print >> fi, '#define %s %s' % (key, value) print >> fi, '#include "pyconfig.h"' for line in database.gcpolicy.pre_pre_gc_code(): print >> fi, line print >> fi, '#include "src/g_prerequisite.h"' for line in database.gcpolicy.pre_gc_code(): print >> fi, line includes, sources, include_dirs = extra_information(database) for include in includes: print >> fi, '#include <%s>' % (include,) fi.close() if database.translator is None or database.translator.rtyper is None: preimplementationlines = [] else: preimplementationlines = list( pre_include_code_lines(database, database.translator.rtyper)) # # 1) All declarations # 2) Implementation of functions and global structures and arrays # sg = SourceGenerator(database, preimplementationlines) sg.set_strategy(targetdir) sg.gen_readable_parts_of_source(f) # # Debugging info # if symboltable: print >> f print >> f, '/*******************************************************/' print >> f, '/*** Debugging info ***/' print >> f print >> f, 'static int debuginfo_offsets[] = {' for node in database.structdefnodes.values(): for expr in symboltable.generate_type_info(database, node): print >> f, '\t%s,' % expr print >> f, '\t0 };' print >> f, 'static void *debuginfo_globals[] = {' for node in database.globalcontainers(): if not isinstance(node, PyObjectNode): result = symboltable.generate_global_info(database, node) print >> f, '\t%s,' % (result,) print >> f, '\tNULL };' print >> f, '#include "src/debuginfo.h"' # # PyObject support (strange) code # pyobjmaker = database.pyobjmaker print >> f print >> f, '/***********************************************************/' print >> f, '/*** Table of global PyObjects ***/' print >> f print >> f, 'static globalobjectdef_t globalobjectdefs[] = {' for node in database.containerlist: if isinstance(node, (PyObjectNode, PyObjHeadNode)): for target in node.where_to_copy_me: print >> f, '\t{%s, "%s"},' % (target, node.exported_name) print >> f, '\t{ NULL, NULL }\t/* Sentinel */' print >> f, '};' print >> f print >> f, 'static cpyobjheaddef_t cpyobjheaddefs[] = {' for node in database.containerlist: if isinstance(node, PyObjHeadNode): print >> f, '\t{"%s", %s, %s},' % (node.exported_name, node.ptrname, node.get_setupfn_name()) print >> f, '\t{ NULL, NULL, NULL }\t/* Sentinel */' print >> f, '};' print >> f print >> f, '/***********************************************************/' print >> f, '/*** Table of functions ***/' print >> f print >> f, 'static globalfunctiondef_t globalfunctiondefs[] = {' wrappers = pyobjmaker.wrappers.items() wrappers.sort() for globalobject_name, (base_name, wrapper_name, func_doc) in wrappers: print >> f, ('\t{&%s, "%s", {"%s", (PyCFunction)%s, ' 'METH_VARARGS|METH_KEYWORDS, %s}},' % ( globalobject_name, globalobject_name, base_name, wrapper_name, func_doc and c_string_constant(func_doc) or 'NULL')) print >> f, '\t{ NULL }\t/* Sentinel */' print >> f, '};' print >> f, 'static globalfunctiondef_t *globalfunctiondefsptr = &globalfunctiondefs[0];' print >> f print >> f, '/***********************************************************/' print >> f, '/*** Frozen Python bytecode: the initialization code ***/' print >> f print >> f, 'static char *frozen_initcode[] = {"\\' bytecode, originalsource = database.pyobjmaker.getfrozenbytecode() g = targetdir.join('frozen.py').open('w') g.write(originalsource) g.close() def char_repr(c): if c in '\\"': return '\\' + c if ' ' <= c < '\x7F': return c return '\\%03o' % ord(c) for i in range(0, len(bytecode), 32): print >> f, ''.join([char_repr(c) for c in bytecode[i:i+32]])+'\\' if (i+32) % 1024 == 0: print >> f, '", "\\' print >> f, '"};' print >> f, "#define FROZEN_INITCODE_SIZE %d" % len(bytecode) print >> f # # Module initialization function # print >> f, '/***********************************************************/' print >> f, '/*** Module initialization function ***/' print >> f gen_startupcode(f, database) print >> f print >> f, 'MODULE_INITFUNC(%s)' % modulename print >> f, '{' print >> f, '\tSETUP_MODULE(%s);' % modulename for publicname, pyobjptr in exports.items(): # some fishing needed to find the name of the obj pyobjnode = database.containernodes[pyobjptr._obj] print >> f, '\tPyModule_AddObject(m, "%s", %s);' % (publicname, pyobjnode.name) print >> f, '\tcall_postsetup(m);' print >> f, '}' f.close() # # Generate a setup.py while we're at it # pypy_include_dir = autopath.this_dir f = targetdir.join('setup.py').open('w') f.write(SETUP_PY % locals()) f.close() return filename, sg.getextrafiles() + sources, include_dirs
def OP_DEBUG_STOP(self, op): arg = op.args[0] assert isinstance(arg, Constant) return "PYPY_DEBUG_STOP(%s);" % ( c_string_constant(''.join(arg.value.chars)),)