From c6ce87df84c87b1049bb109a33dd36b16431190e Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Wed, 15 Feb 2017 08:18:19 -0700 Subject: [PATCH] rename cmd to op in vm --- vm/cmds.go | 86 ++++++++++++++++++++++++++++++------------------------ vm/vm.go | 12 ++++---- 2 files changed, 54 insertions(+), 44 deletions(-) diff --git a/vm/cmds.go b/vm/cmds.go index e2247c0..d5aa049 100644 --- a/vm/cmds.go +++ b/vm/cmds.go @@ -9,7 +9,7 @@ import ( "llvm.org/llvm/bindings/go/llvm" ) -type cmd interface { +type op interface { inType() valType outType() valType build(*Module) (llvm.Value, error) @@ -24,6 +24,17 @@ func (vt valType) isInt() bool { return lang.Equal(Int, vt.term) } +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()} +) + +//////////////////////////////////////////////////////////////////////////////// + // most types don't have an input, so we use this as a shortcut type voidIn struct{} @@ -36,20 +47,17 @@ func (voidIn) inType() valType { //////////////////////////////////////////////////////////////////////////////// -type intCmd struct { +type intOp struct { voidIn c lang.Const } -func (ic intCmd) outType() valType { - return valType{ - term: Int, - llvm: llvm.Int64Type(), - } +func (io intOp) outType() valType { + return valTypeInt } -func (ic intCmd) build(mod *Module) (llvm.Value, error) { - ci, err := strconv.ParseInt(string(ic.c), 10, 64) +func (io intOp) build(mod *Module) (llvm.Value, error) { + ci, err := strconv.ParseInt(string(io.c), 10, 64) if err != nil { return llvm.Value{}, err } @@ -58,16 +66,16 @@ func (ic intCmd) build(mod *Module) (llvm.Value, error) { //////////////////////////////////////////////////////////////////////////////// -type tupCmd struct { +type tupOp struct { voidIn - els []cmd + els []op } -func (tc tupCmd) outType() valType { - termTypes := make(lang.Tuple, len(tc.els)) - llvmTypes := make([]llvm.Type, len(tc.els)) - for i := range tc.els { - elValType := tc.els[i].outType() +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() termTypes[i] = elValType.term llvmTypes[i] = elValType.llvm } @@ -80,12 +88,12 @@ func (tc tupCmd) outType() valType { return vt } -func (tc tupCmd) build(mod *Module) (llvm.Value, error) { - str := llvm.Undef(tc.outType().llvm) +func (to tupOp) build(mod *Module) (llvm.Value, error) { + str := llvm.Undef(to.outType().llvm) var val llvm.Value var err error - for i := range tc.els { - if val, err = tc.els[i].build(mod); err != nil { + for i := range to.els { + if val, err = to.els[i].build(mod); err != nil { str = mod.b.CreateInsertValue(str, val, i, "") } } @@ -94,21 +102,21 @@ func (tc tupCmd) build(mod *Module) (llvm.Value, error) { //////////////////////////////////////////////////////////////////////////////// -type addCmd struct { +type addOp struct { voidIn - a, b cmd + a, b op } -func (ac addCmd) outType() valType { - return ac.a.outType() +func (ao addOp) outType() valType { + return ao.a.outType() } -func (ac addCmd) build(mod *Module) (llvm.Value, error) { - av, err := ac.a.build(mod) +func (ao addOp) build(mod *Module) (llvm.Value, error) { + av, err := ao.a.build(mod) if err != nil { return llvm.Value{}, err } - bv, err := ac.b.build(mod) + bv, err := ao.b.build(mod) if err != nil { return llvm.Value{}, err } @@ -117,7 +125,7 @@ func (ac addCmd) build(mod *Module) (llvm.Value, error) { //////////////////////////////////////////////////////////////////////////////// -func matchCmd(t lang.Term) (cmd, error) { +func termToOp(t lang.Term) (op, error) { aPat := func(a lang.Atom) lang.Tuple { return lang.Tuple{lang.AAtom, a} } @@ -135,14 +143,14 @@ func matchCmd(t lang.Term) (cmd, error) { v := t.(lang.Tuple)[1] // for when v is a Tuple argument, convenience function for casting - vAsTup := func(n int) ([]cmd, error) { - vcmd, err := matchCmd(v) + vAsTup := func(n int) ([]op, error) { + vop, err := termToOp(v) if err != nil { return nil, err } - vtup, ok := vcmd.(tupCmd) + vtup, ok := vop.(tupOp) if !ok || len(vtup.els) != n { - return nil, fmt.Errorf("cmd %v expects a %d-tuple argument", k, n) + return nil, fmt.Errorf("op %v expects a %d-tuple argument", k, n) } return vtup.els, nil } @@ -152,16 +160,16 @@ func matchCmd(t lang.Term) (cmd, error) { if !lang.Match(cPat(lang.AUnder), v) { return nil, errors.New("int requires constant arg") } - return intCmd{c: v.(lang.Const)}, nil + return intOp{c: v.(lang.Const)}, nil case Tuple: if !lang.Match(lang.Tuple{Tuple, lang.AUnder}, v) { return nil, errors.New("tup requires tuple arg") } tup := v.(lang.Tuple) - tc := tupCmd{els: make([]cmd, len(tup))} + tc := tupOp{els: make([]op, len(tup))} var err error for i := range tup { - if tc.els[i], err = matchCmd(tup[i]); err != nil { + if tc.els[i], err = termToOp(tup[i]); err != nil { return nil, err } } @@ -170,11 +178,13 @@ func matchCmd(t lang.Term) (cmd, error) { els, err := vAsTup(2) if err != nil { return nil, err - } else if !els[0].outType().isInt() || !els[1].outType().isInt() { + } 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) { return nil, errors.New("add args must be numbers of the same type") } - return addCmd{a: els[0], b: els[1]}, nil + return addOp{a: els[0], b: els[1]}, nil default: - return nil, fmt.Errorf("cmd %v unknown, or its args are malformed", t) + return nil, fmt.Errorf("op %v unknown, or its args are malformed", t) } } diff --git a/vm/vm.go b/vm/vm.go index 336d315..1757a6c 100644 --- a/vm/vm.go +++ b/vm/vm.go @@ -72,29 +72,29 @@ func (mod *Module) buildFn(tt ...lang.Term) (llvm.Value, error) { return llvm.Value{}, errors.New("function cannot be empty") } - cmds := make([]cmd, len(tt)) + ops := make([]op, len(tt)) var err error for i := range tt { - if cmds[i], err = matchCmd(tt[i]); err != nil { + if ops[i], err = termToOp(tt[i]); err != nil { return llvm.Value{}, err } } var llvmIns []llvm.Type - if in := cmds[0].inType(); in.llvm.TypeKind() == llvm.VoidTypeKind { + if in := ops[0].inType(); in.llvm.TypeKind() == llvm.VoidTypeKind { llvmIns = []llvm.Type{} } else { llvmIns = []llvm.Type{in.llvm} } - llvmOut := cmds[len(cmds)-1].outType().llvm + llvmOut := ops[len(ops)-1].outType().llvm fn := llvm.AddFunction(mod.m, "", llvm.FunctionType(llvmOut, llvmIns, false)) block := llvm.AddBasicBlock(fn, "") mod.b.SetInsertPoint(block, block.FirstInstruction()) var out llvm.Value - for i := range cmds { - if out, err = cmds[i].build(mod); err != nil { + for i := range ops { + if out, err = ops[i].build(mod); err != nil { return llvm.Value{}, nil } }