blitz-research / monkey2

zlib License
132 stars 44 forks source link

Debugging idea from Haxe #222

Open DruggedBunny opened 7 years ago

DruggedBunny commented 7 years ago

Not sure if this is the right place for it, but...

Just idly looked at the latest Haxe release info in passing on Hacker News (I've never actually used it), and noticed this:

  1. Improved C++ code output

While Haxe tries to create the most optimal code for each target, reading through the output code can be hard sometimes. Our Haxe C++ target maintainer Hugh did a great job in making the output noticeably cleaner. It is now much easier to make the connection between original Haxe and generated C++ code, since the latter is annotated with the corresponding Haxe line numbers via HXLINE():

I think HXLINE is probably a no-op macro something like this, or gets converted into "" (if macros can do that!).

Thought it might be something worth looking at, whether for allowing manual cross-referencing between .mx2 and output, or perhaps, even better, it might even help with debugging, taking (eg.) C++ debugger output and referencing it back to the .mx2 source in the Ted2 debug output.

XANOZOID commented 6 years ago

It is most certainly a macro, and in fact this is the exact area it is defined in:

https://github.com/HaxeFoundation/hxcpp/blob/206dbe9bb3ac356e81beebbc05795b323e6a7551/include/hx/StackContext.h#L175

// Emitted after every Haxe line.  number is the original Haxe line number.
// Only if stack lines are to be tracked
#ifdef HXCPP_STACK_LINE
   // If the debugger is enabled, must check for a breakpoint at every line.
   #ifdef HXCPP_DEBUGGER
      #define HX_STACK_LINE(number)                                           \
          _hx_stackframe.lineNumber = number;                                   \
          /* This is incorrect - a read memory barrier is needed here. */     \
          /* For now, just live with the exceedingly rare cases where */      \
          /* breakpoints are missed */                                        \
          if (hx::gShouldCallHandleBreakpoints) {                             \
              __hxcpp_on_line_changed(_hx_stackframe.ctx);                    \
         }
      #define HX_STACK_LINE_QUICK(number) _hx_stackframe.lineNumber = number;
   #else
      // Just set it
      #define HX_STACK_LINE(number) _hx_stackframe.lineNumber = number;
      #define HX_STACK_LINE_QUICK(number) _hx_stackframe.lineNumber = number;
   #endif
#else
   #define HX_STACK_LINE(number)
   #define HX_STACK_LINE_QUICK(number)
#endif

// For tidier generated code
#define HXLINE(number) HX_STACK_LINE(number)

Literally just slap it right after whatever line was most recently transpiled. And I assume if there was an error, the last line is marked and can be pointed too on exit. That's an awesome thing to have if we can get this in.