ginger/vm/cmds.go

144 lines
3.4 KiB
Go
Raw Normal View History

2017-02-11 20:35:02 +00:00
package vm
import (
"strconv"
"github.com/mediocregopher/ginger/lang"
"llvm.org/llvm/bindings/go/llvm"
)
type buildCmd struct {
pattern lang.Tuple
inTypeFn func(lang.Term) (llvm.Type, error)
outTypeFn func(lang.Term) (llvm.Type, error)
2017-02-12 15:55:27 +00:00
buildFn func(lang.Term) (val, error)
2017-02-11 20:35:02 +00:00
}
func (cmd buildCmd) matches(t lang.Term) bool {
return lang.Match(cmd.pattern, t)
}
func (cmd buildCmd) inType(t lang.Term) (llvm.Type, error) {
if cmd.inTypeFn == nil {
return llvm.VoidType(), nil
}
return cmd.inTypeFn(t)
}
func (cmd buildCmd) outType(t lang.Term) (llvm.Type, error) {
if cmd.outTypeFn == nil {
return llvm.VoidType(), nil
}
return cmd.outTypeFn(t)
}
2017-02-12 15:55:27 +00:00
func (cmd buildCmd) build(t lang.Term) (val, error) {
2017-02-11 20:35:02 +00:00
return cmd.buildFn(t)
}
func buildCmds(mod *Module) []buildCmd {
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 {
return lang.Tuple{lang.ATuple, lang.Tuple(el)}
}
buildPat := func(a lang.Atom, b lang.Tuple) lang.Tuple {
return tPat(aPat(a), b)
}
return []buildCmd{
{ // (int 42)
pattern: buildPat(lang.AInt, cPat(lang.AUnder)),
outTypeFn: func(t lang.Term) (llvm.Type, error) {
return llvm.Int64Type(), nil
},
2017-02-12 15:55:27 +00:00
buildFn: func(t lang.Term) (val, error) {
2017-02-11 20:35:02 +00:00
con := t.(lang.Const)
coni, err := strconv.ParseInt(string(con), 10, 64)
if err != nil {
2017-02-12 15:55:27 +00:00
return val{}, err
2017-02-11 20:35:02 +00:00
}
2017-02-12 15:55:27 +00:00
return val{
// TODO why does this have to be cast?
v: llvm.ConstInt(llvm.Int64Type(), uint64(coni), false),
typ: lang.AInt,
}, nil
2017-02-11 20:35:02 +00:00
},
},
{ // (tup ((atom foo) (const 10)))
pattern: buildPat(lang.ATuple, lang.Tuple{lang.ATuple, lang.AUnder}),
outTypeFn: func(t lang.Term) (llvm.Type, error) {
tup := t.(lang.Tuple)
if len(tup) == 0 {
return llvm.VoidType(), nil
}
var err error
typs := make([]llvm.Type, len(tup))
for i := range tup {
if typs[i], err = mod.outType(tup[i]); err != nil {
return llvm.Type{}, err
}
}
return llvm.StructType(typs, false), nil
},
2017-02-12 15:55:27 +00:00
buildFn: func(t lang.Term) (val, error) {
2017-02-11 20:35:02 +00:00
tup := t.(lang.Tuple)
// if the tuple is empty then it is a void
if len(tup) == 0 {
2017-02-12 15:55:27 +00:00
return val{
v: llvm.Undef(llvm.VoidType()),
typ: lang.Tuple{lang.ATuple, lang.Tuple{}},
}, nil
2017-02-11 20:35:02 +00:00
}
var err error
2017-02-12 15:55:27 +00:00
vals := make([]val, len(tup))
2017-02-11 20:35:02 +00:00
typs := make([]llvm.Type, len(tup))
2017-02-12 15:55:27 +00:00
ttyps := make([]lang.Term, len(tup))
2017-02-11 20:35:02 +00:00
for i := range tup {
if vals[i], err = mod.build(tup[i]); err != nil {
2017-02-12 15:55:27 +00:00
return val{}, err
2017-02-11 20:35:02 +00:00
}
2017-02-12 15:55:27 +00:00
typs[i] = vals[i].v.Type()
ttyps[i] = vals[i].typ
2017-02-11 20:35:02 +00:00
}
str := llvm.Undef(llvm.StructType(typs, false))
for i := range vals {
2017-02-12 15:55:27 +00:00
str = mod.b.CreateInsertValue(str, vals[i].v, i, "")
2017-02-11 20:35:02 +00:00
}
2017-02-12 15:55:27 +00:00
return val{
v: str,
typ: lang.Tuple{lang.ATuple, lang.Tuple(ttyps)},
}, nil
2017-02-11 20:35:02 +00:00
},
},
{ // (add ((const 5) (var foo)))
pattern: buildPat(lang.AAdd, tPat(lang.TDblUnder, lang.TDblUnder)),
outTypeFn: func(t lang.Term) (llvm.Type, error) {
return llvm.Int64Type(), nil
},
2017-02-12 15:55:27 +00:00
buildFn: func(t lang.Term) (val, error) {
2017-02-11 20:35:02 +00:00
tup := t.(lang.Tuple)
v1, err := mod.build(tup[0])
if err != nil {
2017-02-12 15:55:27 +00:00
return val{}, err
2017-02-11 20:35:02 +00:00
}
v2, err := mod.build(tup[1])
if err != nil {
2017-02-12 15:55:27 +00:00
return val{}, err
2017-02-11 20:35:02 +00:00
}
2017-02-12 15:55:27 +00:00
return val{
v: mod.b.CreateAdd(v1.v, v2.v, ""),
typ: v1.typ,
}, nil
2017-02-11 20:35:02 +00:00
},
},
}
}