janet-lang / janet

A dynamic language and bytecode vm
https://janet-lang.org
MIT License
3.45k stars 223 forks source link

Add source view to .ppasm output #1213

Closed sogaiu closed 1 year ago

sogaiu commented 1 year ago

This draft PR adds a Janet source view to the output of .ppasm.

In order to solicit and process feedback about placement, aesthetics, etc. the PR is currently a draft. Please comment if interested :)

Below is a demo of how it can look:

$ ./build/janet -d
Janet 1.29.1-026c64fa linux/x64/gcc - '(doc)' for help
repl:1:> (import ../../scratch/debug)
@{_ @{:value <cycle 0>} debug/dump @{:private true} debug/fun @{:private true} debug/fun-2 @{:private true} :debug true :redef true}
repl:2:> (debug/fbreak debug/fun-2)
nil
repl:3:> (debug/fun-2 3)
debug: 
  in fun-2 [../../scratch/debug.janet] on line 9, column 3
  in _thunk [repl] (tailcall) on line 3, column 1
entering debug[1] - (quit) to exit
debug[1]:1:> (.ppasm)

  signal:     
  status:     debug
  function:   fun-2 [../../scratch/debug.janet]
  constants:  @[]
  slots:      @[3 nil nil nil nil nil]

    4:         b 2]
    5:     (+ x (* a b))))
    6: 
    7: (defn fun-2
    8:   [x]
 >  9:   (var y 0)
   10:   (for i 0 x
   11:     (+= y i))
   12:   y)
   13: 

*> ldi 2 0              # line 9, column 3
   ldi 3 0              # line 10, column 3
   lt 4 3 0            
   jmpno 4 5           
   movn 5 3            
   add 2 2 5            # line 11, column 5
   addim 3 3 1          # line 10, column 3
   jmp -5              
   ret 2                # line 7, column 1

nil
debug[1]:2:> 
pepe commented 1 year ago

This is indeed a perfect addition.

sogaiu commented 1 year ago

I guess it's good enough as a start :)

Thanks for taking a look @pepe and @ianthehenry!