2017-02-11 20:35:02 +00:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
2017-02-12 18:32:44 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2017-02-11 20:35:02 +00:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/mediocregopher/ginger/lang"
|
|
|
|
"llvm.org/llvm/bindings/go/llvm"
|
|
|
|
)
|
|
|
|
|
2017-02-15 15:18:19 +00:00
|
|
|
type op interface {
|
2017-02-12 18:32:44 +00:00
|
|
|
inType() valType
|
|
|
|
outType() valType
|
|
|
|
build(*Module) (llvm.Value, error)
|
2017-02-11 20:35:02 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 18:32:44 +00:00
|
|
|
type valType struct {
|
|
|
|
term lang.Term
|
|
|
|
llvm llvm.Type
|
2017-02-11 20:35:02 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 18:44:10 +00:00
|
|
|
func (vt valType) isInt() bool {
|
|
|
|
return lang.Equal(Int, vt.term)
|
|
|
|
}
|
|
|
|
|
2017-02-15 15:18:19 +00:00
|
|
|
func (vt valType) eq(vt2 valType) bool {
|
|
|
|
return lang.Equal(vt.term, vt2.term) && vt.llvm == vt2.llvm
|
|
|
|
}
|
|
|
|
|
|
|
|
// primitive valTypes
|
|
|
|
var (
|
|
|
|
valTypeInt = valType{term: Int, llvm: llvm.Int64Type()}
|
|
|
|
)
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2017-02-12 18:32:44 +00:00
|
|
|
// most types don't have an input, so we use this as a shortcut
|
|
|
|
type voidIn struct{}
|
|
|
|
|
|
|
|
func (voidIn) inType() valType {
|
|
|
|
return valType{
|
|
|
|
term: lang.Tuple{},
|
|
|
|
llvm: llvm.VoidType(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2017-02-15 15:18:19 +00:00
|
|
|
type intOp struct {
|
2017-02-12 18:32:44 +00:00
|
|
|
voidIn
|
|
|
|
c lang.Const
|
|
|
|
}
|
|
|
|
|
2017-02-15 15:18:19 +00:00
|
|
|
func (io intOp) outType() valType {
|
|
|
|
return valTypeInt
|
2017-02-11 20:35:02 +00:00
|
|
|
}
|
|
|
|
|
2017-02-15 15:18:19 +00:00
|
|
|
func (io intOp) build(mod *Module) (llvm.Value, error) {
|
|
|
|
ci, err := strconv.ParseInt(string(io.c), 10, 64)
|
2017-02-12 18:32:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return llvm.Value{}, err
|
2017-02-11 20:35:02 +00:00
|
|
|
}
|
2017-02-12 18:32:44 +00:00
|
|
|
return llvm.ConstInt(llvm.Int64Type(), uint64(ci), false), nil
|
2017-02-11 20:35:02 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 18:32:44 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2017-02-15 15:18:19 +00:00
|
|
|
type tupOp struct {
|
2017-02-12 18:32:44 +00:00
|
|
|
voidIn
|
2017-02-15 15:18:19 +00:00
|
|
|
els []op
|
2017-02-12 18:32:44 +00:00
|
|
|
}
|
|
|
|
|
2017-02-15 15:18:19 +00:00
|
|
|
func (to tupOp) outType() valType {
|
|
|
|
termTypes := make(lang.Tuple, len(to.els))
|
|
|
|
llvmTypes := make([]llvm.Type, len(to.els))
|
|
|
|
for i := range to.els {
|
|
|
|
elValType := to.els[i].outType()
|
2017-02-12 18:32:44 +00:00
|
|
|
termTypes[i] = elValType.term
|
|
|
|
llvmTypes[i] = elValType.llvm
|
|
|
|
}
|
|
|
|
vt := valType{term: lang.Tuple{Tuple, termTypes}}
|
|
|
|
if len(llvmTypes) == 0 {
|
|
|
|
vt.llvm = llvm.VoidType()
|
|
|
|
} else {
|
|
|
|
vt.llvm = llvm.StructType(llvmTypes, false)
|
|
|
|
}
|
|
|
|
return vt
|
|
|
|
}
|
|
|
|
|
2017-02-15 15:18:19 +00:00
|
|
|
func (to tupOp) build(mod *Module) (llvm.Value, error) {
|
|
|
|
str := llvm.Undef(to.outType().llvm)
|
2017-02-12 18:32:44 +00:00
|
|
|
var val llvm.Value
|
|
|
|
var err error
|
2017-02-15 15:18:19 +00:00
|
|
|
for i := range to.els {
|
|
|
|
if val, err = to.els[i].build(mod); err != nil {
|
2017-02-12 18:32:44 +00:00
|
|
|
str = mod.b.CreateInsertValue(str, val, i, "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return str, err
|
2017-02-11 20:35:02 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 18:32:44 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2017-02-15 15:18:19 +00:00
|
|
|
type addOp struct {
|
2017-02-12 18:32:44 +00:00
|
|
|
voidIn
|
2017-02-15 15:18:19 +00:00
|
|
|
a, b op
|
2017-02-12 18:32:44 +00:00
|
|
|
}
|
|
|
|
|
2017-02-15 15:18:19 +00:00
|
|
|
func (ao addOp) outType() valType {
|
|
|
|
return ao.a.outType()
|
2017-02-12 18:32:44 +00:00
|
|
|
}
|
|
|
|
|
2017-02-15 15:18:19 +00:00
|
|
|
func (ao addOp) build(mod *Module) (llvm.Value, error) {
|
|
|
|
av, err := ao.a.build(mod)
|
2017-02-12 18:32:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return llvm.Value{}, err
|
|
|
|
}
|
2017-02-15 15:18:19 +00:00
|
|
|
bv, err := ao.b.build(mod)
|
2017-02-12 18:32:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return llvm.Value{}, err
|
|
|
|
}
|
|
|
|
return mod.b.CreateAdd(av, bv, ""), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2017-02-15 15:18:19 +00:00
|
|
|
func termToOp(t lang.Term) (op, error) {
|
2017-02-11 20:35:02 +00:00
|
|
|
aPat := func(a lang.Atom) lang.Tuple {
|
|
|
|
return lang.Tuple{lang.AAtom, a}
|
|
|
|
}
|
|
|
|
cPat := func(t lang.Term) lang.Tuple {
|
|
|
|
return lang.Tuple{lang.AConst, t}
|
|
|
|
}
|
|
|
|
tPat := func(el ...lang.Term) lang.Tuple {
|
2017-02-12 16:53:59 +00:00
|
|
|
return lang.Tuple{Tuple, lang.Tuple(el)}
|
2017-02-11 20:35:02 +00:00
|
|
|
}
|
2017-02-12 18:32:44 +00:00
|
|
|
|
|
|
|
if !lang.Match(tPat(aPat(lang.AUnder), lang.TDblUnder), t) {
|
|
|
|
return nil, fmt.Errorf("term %v does not look like a vm command", t)
|
2017-02-11 20:35:02 +00:00
|
|
|
}
|
2017-02-12 18:32:44 +00:00
|
|
|
k := t.(lang.Tuple)[0].(lang.Atom)
|
|
|
|
v := t.(lang.Tuple)[1]
|
|
|
|
|
|
|
|
// for when v is a Tuple argument, convenience function for casting
|
2017-02-15 15:18:19 +00:00
|
|
|
vAsTup := func(n int) ([]op, error) {
|
|
|
|
vop, err := termToOp(v)
|
2017-02-12 18:32:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-15 15:18:19 +00:00
|
|
|
vtup, ok := vop.(tupOp)
|
2017-02-12 18:32:44 +00:00
|
|
|
if !ok || len(vtup.els) != n {
|
2017-02-15 15:18:19 +00:00
|
|
|
return nil, fmt.Errorf("op %v expects a %d-tuple argument", k, n)
|
2017-02-12 18:32:44 +00:00
|
|
|
}
|
|
|
|
return vtup.els, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch k {
|
|
|
|
case Int:
|
|
|
|
if !lang.Match(cPat(lang.AUnder), v) {
|
|
|
|
return nil, errors.New("int requires constant arg")
|
|
|
|
}
|
2017-02-15 15:18:19 +00:00
|
|
|
return intOp{c: v.(lang.Const)}, nil
|
2017-02-12 18:32:44 +00:00
|
|
|
case Tuple:
|
|
|
|
if !lang.Match(lang.Tuple{Tuple, lang.AUnder}, v) {
|
|
|
|
return nil, errors.New("tup requires tuple arg")
|
|
|
|
}
|
|
|
|
tup := v.(lang.Tuple)
|
2017-02-15 15:18:19 +00:00
|
|
|
tc := tupOp{els: make([]op, len(tup))}
|
2017-02-12 18:32:44 +00:00
|
|
|
var err error
|
|
|
|
for i := range tup {
|
2017-02-15 15:18:19 +00:00
|
|
|
if tc.els[i], err = termToOp(tup[i]); err != nil {
|
2017-02-12 18:32:44 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tc, nil
|
|
|
|
case Add:
|
|
|
|
els, err := vAsTup(2)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-02-15 15:18:19 +00:00
|
|
|
} else if !els[0].outType().eq(valTypeInt) {
|
|
|
|
return nil, errors.New("add args must be numbers of the same type")
|
|
|
|
} else if !els[1].outType().eq(valTypeInt) {
|
2017-02-12 18:44:10 +00:00
|
|
|
return nil, errors.New("add args must be numbers of the same type")
|
2017-02-12 18:32:44 +00:00
|
|
|
}
|
2017-02-15 15:18:19 +00:00
|
|
|
return addOp{a: els[0], b: els[1]}, nil
|
2017-02-12 18:32:44 +00:00
|
|
|
default:
|
2017-02-15 15:18:19 +00:00
|
|
|
return nil, fmt.Errorf("op %v unknown, or its args are malformed", t)
|
2017-02-11 20:35:02 +00:00
|
|
|
}
|
|
|
|
}
|