open2b / scriggo

The world’s most powerful template engine and Go embeddable interpreter
https://scriggo.com
BSD 3-Clause "New" or "Revised" License
425 stars 19 forks source link

compiler: change disassembler to follow the bytecode documentation #441

Open gazerro opened 4 years ago

gazerro commented 4 years ago

Change the disassembler to follow the bytecode documentation of Call, LoadFunc and Func instructions at https://www.scriggo.com/doc/developers/vm.html

zapateo commented 4 years ago

Commit https://github.com/open2b/scriggo/commit/daed85e4046c61aa340e48241bca08e673ef7b32 changed the way that OpCall* instructions are disassembled. Here's an example:

   1
   2 
   3 package main
   4 
   5 import "fmt"
   6 
   7 func F(a, b int) string {
   8     return "ciao"
   9 }
  10 
  11 func G(s string) error {
  12     return nil
  13 }
  14 
  15 func main() {
  16     f := F
  17     f(2, 3)
  18     fmt.Println("Hello, World!")
  19     G("ciao")
  20     a := 20
  21     b := 40
  22     F(a, b)
  23 }

   1 
   2 Package main
   3 
   4 Import "fmt"
   5 
   6 Func main()
   7     ; regs(4,0,1,4)
   8     Func g2 (i1 int, i2 int) (s1 string)
   9         ; regs(2,0,1,0)
  10         Move "ciao" s1
  11         Return
  12     Move g2 g1
  13     Move g1 g3
  14     LoadNumber int 2 i1
  15     LoadNumber int 3 i2
  16     Call (g3) i1 _ s1 g4    ; func(i1 int, i2 int) (s1 string)
  17     Typify string "Hello, World!" g4
  18     Call fmt.Println ...1 i1 _ _ g3 ; func(g4 interface {}) (i1 int, g3 error)
  19     Move "ciao" s1
  20     Call main.G _ _ s1 g3   ; func(s1 string) (g3 error)
  21     Move 20 i1
  22     Move 40 i2
  23     Move i1 i3
  24     Move i2 i4
  25     Call main.F i3 _ s1 g3  ; func(i3 int, i4 int) (s1 string)
  26     Return
  27 
  28 Func F(i1 int, i2 int) (s1 string)
  29     ; regs(2,0,1,0)
  30     Move "ciao" s1
  31     Return
  32 
  33 Func G(s1 string) (g1 error)
  34     ; regs(0,0,1,1)
  35     Move nil g1
  36     Return
  37 
zapateo commented 4 years ago

Commit https://github.com/open2b/scriggo/commit/67f4c7e0d3eaa88e05d61a7d51214d080d9aafc9 renamed OpGetFunc in OpLoadFunc.

zapateo commented 4 years ago

Note that currently there's a bug in the disassembling of LoadFunc on functions defined at the Scriggo package level.

zapateo commented 4 years ago

https://github.com/open2b/scriggo/commit/07802206a859d565efe5ada64852a150cd814bb2 fixed the disassembling of OpLoadFunc, but the byte code is still different from the documentation.