static PyObject* do_fullcompile(PyFrameObject* frame, PyObject* arg) { PyCodeStats* cs; cs = PyCodeStats_Get(frame->f_code); if (cs->st_codebuf == NULL) { /* not already compiled, compile it now */ PyObject* g = frame->f_globals; int rec, module; stats_printf(("stats: full compile code: %s\n", PyCodeObject_NAME(frame->f_code))); if (cs->st_globals && PyInt_Check(cs->st_globals)) rec = PyInt_AS_LONG(cs->st_globals); else rec = DEFAULT_RECURSION; module = frame->f_globals == frame->f_locals; cs->st_codebuf = PsycoCode_CompileCode(frame->f_code, g, rec, module); if (cs->st_codebuf == Py_None) g = NULL; /* failed */ else { Py_INCREF(g); extra_assert(CodeBuffer_Check(cs->st_codebuf)); } Py_XDECREF(cs->st_globals); cs->st_globals = g; } /* already compiled a Psyco version, run it if the globals match */ extra_assert(frame->f_globals != NULL); if (cs->st_globals == frame->f_globals) { Py_INCREF(cs->st_codebuf); return cs->st_codebuf; } return NULL; }
static PyObject* profile_call(PyFrameObject* frame, PyObject* arg) { PyCodeStats* cs; psyco_stats_append(frame->f_tstate, frame->f_back); cs = PyCodeStats_Get(frame->f_code); if (cs->st_globals != NULL) { /* we want to accelerate this code object */ if (cs->st_codebuf == NULL) { /* not already compiled, compile it now */ PyObject* codebuf; PyObject* g = frame->f_globals; int rec, module; stats_printf(("stats: compile code: %s\n", PyCodeObject_NAME(frame->f_code))); if (PyInt_Check(cs->st_globals)) rec = PyInt_AS_LONG(cs->st_globals); else rec = DEFAULT_RECURSION; module = frame->f_globals == frame->f_locals; codebuf = PsycoCode_CompileCode(frame->f_code, g, rec, module); /* rare race condition: 'cs' might have been mutated during the call to PsycoCode_CompileCode(), so cs->st_codebuf might no longer be NULL, or cs->st_globals might be NULL again */ Py_XDECREF(cs->st_codebuf); cs->st_codebuf = codebuf; if (cs->st_codebuf == Py_None) g = NULL; /* failed */ else { Py_INCREF(g); extra_assert (CodeBuffer_Check(cs->st_codebuf)); } Py_XDECREF(cs->st_globals); cs->st_globals = g; } /* already compiled a Psyco version, run it if the globals match */ extra_assert(frame->f_globals != NULL); if (cs->st_globals == frame->f_globals) { Py_INCREF(cs->st_codebuf); return cs->st_codebuf; } } return NULL; }
DEFINEFN void psyco_dump_code_buffers(void) { static int is_dumping = 0; FILE* f; #if CODE_DUMP >= 3 static int alt = 1; char filename[200]; sprintf(filename, "%s-%d", CODE_DUMP_FILE, alt); alt = 3-alt; #else char* filename = CODE_DUMP_FILE; #endif #if defined(PSYCO_TRACE) trace_flush(); #endif if (is_dumping) return; is_dumping = 1; f = fopen(filename, "wb"); if (f != NULL) { CodeBufferObject* obj; PyObject *exc, *val, *tb; long buftablepos; void** chain; int bufcount = 0; long* buftable; PyErr_Fetch(&exc, &val, &tb); debug_printf(1, ("writing %s\n", filename)); for (obj=psyco_codebuf_chained_list; obj != NULL; obj=obj->chained_list) bufcount++; buftable = PyMem_NEW(long, bufcount); fprintf(f, "Psyco dump [%s]\n", MACHINE_CODE_FORMAT); fwrite(&bufcount, sizeof(bufcount), 1, f); buftablepos = ftell(f); fwrite(buftable, sizeof(long), bufcount, f); /* give the address of an arbitrary symbol from the Python interpreter and from the Psyco module */ fprintf(f, "PyInt_FromLong: 0x%lx\n", (long) &PyInt_FromLong); fprintf(f, "psyco_dump_code_buffers: 0x%lx\n", (long) &psyco_dump_code_buffers); for (obj=psyco_codebuf_chained_list; obj != NULL; obj=obj->chained_list) { PyCodeObject* co = obj->snapshot.fz_pyc_data ? obj->snapshot.fz_pyc_data->co : NULL; fprintf(f, "CodeBufferObject 0x%lx %d '%s' '%s' %d '%s'\n", (long) obj->codestart, get_stack_depth(&obj->snapshot), co?PyString_AsString(co->co_filename):"", co?PyCodeObject_NAME(co):"", co?obj->snapshot.fz_pyc_data->next_instr:-1, obj->codemode); } psyco_dump_bigbuffers(f); for (chain = psyco_codebuf_spec_dict_list; chain; chain=(void**)*chain) { PyObject* spec_dict = (PyObject*)(chain[-1]); int i = 0; PyObject *key, *value; fprintf(f, "spec_dict 0x%lx\n", (long) chain); while (PyDict_Next(spec_dict, &i, &key, &value)) { PyObject* repr; if (PyInt_Check(key)) { repr = (key->ob_type->tp_as_number->nb_hex)(key); } else { repr = PyObject_Repr(key); } psyco_assert(!PyErr_Occurred()); psyco_assert(PyString_Check(repr)); psyco_assert(CodeBuffer_Check(value)); fprintf(f, "0x%lx %s\n", (long)((CodeBufferObject*)value)->codestart, PyString_AsString(repr)); Py_DECREF(repr); } fprintf(f, "\n"); } { int i = 0; fprintf(f, "vinfo_array\n"); for (obj=psyco_codebuf_chained_list; obj != NULL; obj=obj->chained_list) { PsycoObject* live_po; PyObject* d; if (psyco_top_array_count(&obj->snapshot) > 0) live_po = fpo_unfreeze(&obj->snapshot); else live_po = NULL; d = PyDict_New(); psyco_assert(d); buftable[i++] = ftell(f); vinfo_array_dump(live_po ? &live_po->vlocals : NullArray, f, d); Py_DECREF(d); if (live_po) PsycoObject_Delete(live_po); } psyco_assert(i==bufcount); fseek(f, buftablepos, 0); fwrite(buftable, sizeof(long), bufcount, f); } PyMem_FREE(buftable); psyco_assert(!PyErr_Occurred()); fclose(f); PyErr_Restore(exc, val, tb); }