@@ -287,6 +287,10 @@ PyUnstable_Optimizer_NewCounter(void)
287287
288288///////////////////// Experimental UOp Interpreter /////////////////////
289289
290+ #ifdef Py_DEBUG
291+ #define LLTRACE 1
292+ #endif
293+
290294typedef struct {
291295 _PyOptimizerObject base ;
292296 int traces_executed ;
@@ -298,7 +302,7 @@ typedef struct {
298302// UOp opcodes are outside the range of bytecodes or pseudo ops
299303#define EXIT_TRACE 512
300304#define SET_IP 513
301- // TODOL Generate these in Tools/cases_generator
305+ // TODO: Generate these in Tools/cases_generator
302306#define _BINARY_OP_MULTIPLY_FLOAT 514
303307#define _BINARY_OP_ADD_FLOAT 515
304308#define _BINARY_OP_SUBTRACT_FLOAT 516
@@ -339,6 +343,9 @@ static PyTypeObject UOpExecutor_Type = {
339343static _PyInterpreterFrame *
340344uop_execute (_PyExecutorObject * executor , _PyInterpreterFrame * frame , PyObject * * stack_pointer )
341345{
346+ #ifdef LLTRACE
347+ int lltrace = PyDict_Contains (GLOBALS (), & _Py_ID (__lltrace__ ));
348+ #endif
342349 UOpExecutorObject * self = (UOpExecutorObject * )executor ;
343350 self -> optimizer -> traces_executed ++ ;
344351 _Py_CODEUNIT * ip_offset = (_Py_CODEUNIT * )_PyFrame_GetCode (frame )-> co_code_adaptive - 1 ;
@@ -348,7 +355,15 @@ uop_execute(_PyExecutorObject *executor, _PyInterpreterFrame *frame, PyObject **
348355 for (;;) {
349356 opcode = self -> trace [pc ].opcode ;
350357 oparg = self -> trace [pc ].oparg ;
351- // fprintf(stderr, "uop %d, oparg %d, stack_level %d\n", opcode, oparg, (int)(stack_pointer - _PyFrame_Stackbase(frame)));
358+ #ifdef LLTRACE
359+ if (lltrace ) {
360+ // TODO: Print line numbers; uop names.
361+ char * opname = opcode < 256 ? _PyOpcode_OpName [opcode ] : "" ;
362+ int stack_level = (int )(stack_pointer - _PyFrame_Stackbase (frame ));
363+ fprintf (stderr , "uop %s %d, oparg %d, stack_level %d\n" ,
364+ opname , opcode , oparg , stack_level );
365+ }
366+ #endif
352367 pc ++ ;
353368 self -> optimizer -> instrs_executed ++ ;
354369 switch (opcode ) {
@@ -359,14 +374,12 @@ uop_execute(_PyExecutorObject *executor, _PyInterpreterFrame *frame, PyObject **
359374
360375 case SET_IP :
361376 {
362- // fprintf(stderr, "SET_IP %d\n", oparg);
363377 frame -> prev_instr = ip_offset + oparg ;
364378 break ;
365379 }
366380
367381 case EXIT_TRACE :
368382 {
369- // fprintf(stderr, "EXIT_TRACE\n");
370383 _PyFrame_SetStackPointer (frame , stack_pointer );
371384 Py_DECREF (self );
372385 return frame ;
@@ -395,7 +408,11 @@ uop_execute(_PyExecutorObject *executor, _PyInterpreterFrame *frame, PyObject **
395408error :
396409 // On ERROR_IF we return NULL as the frame.
397410 // The caller recovers the frame from cframe.current_frame.
398- // fprintf(stderr, "error: [Opcode %d, oparg %d]\n", opcode, oparg);
411+ #ifdef LLTRACE
412+ if (lltrace ) {
413+ fprintf (stderr , "error: [Opcode %d, oparg %d]\n" , opcode , oparg );
414+ }
415+ #endif
399416 _PyFrame_SetStackPointer (frame , stack_pointer );
400417 Py_DECREF (self );
401418 return NULL ;
@@ -408,7 +425,11 @@ PREDICTED(BINARY_SUBSCR)
408425PREDICTED (BINARY_OP )
409426 // On DEOPT_IF we just repeat the last instruction.
410427 // This presumes nothing was popped from the stack (nor pushed).
411- // fprintf(stderr, "DEOPT: [Opcode %d, oparg %d]\n", opcode, oparg);
428+ #ifdef LLTRACE
429+ if (lltrace ) {
430+ fprintf (stderr , "DEOPT: [Opcode %d, oparg %d]\n" , opcode , oparg );
431+ }
432+ #endif
412433 _PyFrame_SetStackPointer (frame , stack_pointer );
413434 Py_DECREF (self );
414435 return frame ;
0 commit comments