xgo/compiler/bytecode.go

101 lines
2.3 KiB
Go
Raw Normal View History

2019-01-09 10:17:42 +03:00
package compiler
import (
"encoding/gob"
"fmt"
2019-01-09 10:17:42 +03:00
"io"
"reflect"
2019-01-09 10:17:42 +03:00
"github.com/d5/tengo/compiler/source"
2019-01-09 10:17:42 +03:00
"github.com/d5/tengo/objects"
)
// Bytecode is a compiled instructions and constants.
2019-01-09 10:17:42 +03:00
type Bytecode struct {
FileSet *source.FileSet
MainFunction *objects.CompiledFunction
2019-01-09 10:17:42 +03:00
Constants []objects.Object
}
// Decode reads Bytecode data from the reader.
2019-01-09 10:17:42 +03:00
func (b *Bytecode) Decode(r io.Reader) error {
dec := gob.NewDecoder(r)
if err := dec.Decode(&b.FileSet); err != nil {
return err
}
// TODO: files in b.FileSet.File does not have their 'set' field properly set to b.FileSet
// as it's private field and not serialized by gob encoder/decoder.
if err := dec.Decode(&b.MainFunction); err != nil {
2019-01-09 10:17:42 +03:00
return err
}
if err := dec.Decode(&b.Constants); err != nil {
return err
}
return nil
2019-01-09 10:17:42 +03:00
}
// Encode writes Bytecode data to the writer.
2019-01-09 10:17:42 +03:00
func (b *Bytecode) Encode(w io.Writer) error {
enc := gob.NewEncoder(w)
if err := enc.Encode(b.FileSet); err != nil {
return err
}
if err := enc.Encode(b.MainFunction); err != nil {
2019-01-09 10:17:42 +03:00
return err
}
// constants
return enc.Encode(b.Constants)
2019-01-09 10:17:42 +03:00
}
// CountObjects returns the number of objects found in Constants.
func (b *Bytecode) CountObjects() int {
n := 0
for _, c := range b.Constants {
n += objects.CountObjects(c)
}
return n
}
// FormatInstructions returns human readable string representations of
// compiled instructions.
func (b *Bytecode) FormatInstructions() []string {
return FormatInstructions(b.MainFunction.Instructions, 0)
}
// FormatConstants returns human readable string representations of
// compiled constants.
func (b *Bytecode) FormatConstants() (output []string) {
for cidx, cn := range b.Constants {
switch cn := cn.(type) {
case *objects.CompiledFunction:
output = append(output, fmt.Sprintf("[% 3d] (Compiled Function|%p)", cidx, &cn))
for _, l := range FormatInstructions(cn.Instructions, 0) {
output = append(output, fmt.Sprintf(" %s", l))
}
default:
output = append(output, fmt.Sprintf("[% 3d] %s (%s|%p)", cidx, cn, reflect.TypeOf(cn).Elem().Name(), &cn))
}
}
return
}
2019-01-09 10:17:42 +03:00
func init() {
gob.Register(&source.FileSet{})
gob.Register(&source.File{})
2019-01-09 10:17:42 +03:00
gob.Register(&objects.Char{})
gob.Register(&objects.CompiledFunction{})
gob.Register(&objects.Float{})
gob.Register(&objects.Int{})
gob.Register(&objects.String{})
2019-01-09 10:17:42 +03:00
}