Skip to content

Commit

Permalink
ast: move "ast" types from root to ir sub-package
Browse files Browse the repository at this point in the history
Closes #32
  • Loading branch information
mmcloughlin committed Jan 6, 2019
1 parent 4a920c2 commit 0f63e09
Show file tree
Hide file tree
Showing 15 changed files with 3,925 additions and 3,925 deletions.
30 changes: 15 additions & 15 deletions build/context.go
Expand Up @@ -4,10 +4,10 @@ import (
"errors" "errors"
"go/types" "go/types"


"github.com/mmcloughlin/avo"
"github.com/mmcloughlin/avo/attr" "github.com/mmcloughlin/avo/attr"
"github.com/mmcloughlin/avo/buildtags" "github.com/mmcloughlin/avo/buildtags"
"github.com/mmcloughlin/avo/gotypes" "github.com/mmcloughlin/avo/gotypes"
"github.com/mmcloughlin/avo/ir"
"github.com/mmcloughlin/avo/operand" "github.com/mmcloughlin/avo/operand"
"github.com/mmcloughlin/avo/reg" "github.com/mmcloughlin/avo/reg"
"golang.org/x/tools/go/packages" "golang.org/x/tools/go/packages"
Expand All @@ -16,17 +16,17 @@ import (
// Context maintains state for incrementally building an avo File. // Context maintains state for incrementally building an avo File.
type Context struct { type Context struct {
pkg *packages.Package pkg *packages.Package
file *avo.File file *ir.File
function *avo.Function function *ir.Function
global *avo.Global global *ir.Global
errs ErrorList errs ErrorList
reg.Collection reg.Collection
} }


// NewContext initializes an empty build Context. // NewContext initializes an empty build Context.
func NewContext() *Context { func NewContext() *Context {
return &Context{ return &Context{
file: avo.NewFile(), file: ir.NewFile(),
Collection: *reg.NewCollection(), Collection: *reg.NewCollection(),
} }
} }
Expand Down Expand Up @@ -81,7 +81,7 @@ func (c *Context) ConstraintExpr(expr string) {


// Function starts building a new function with the given name. // Function starts building a new function with the given name.
func (c *Context) Function(name string) { func (c *Context) Function(name string) {
c.function = avo.NewFunction(name) c.function = ir.NewFunction(name)
c.file.AddSection(c.function) c.file.AddSection(c.function)
} }


Expand Down Expand Up @@ -124,19 +124,19 @@ func (c *Context) AllocLocal(size int) operand.Mem {
} }


// Instruction adds an instruction to the active function. // Instruction adds an instruction to the active function.
func (c *Context) Instruction(i *avo.Instruction) { func (c *Context) Instruction(i *ir.Instruction) {
c.activefunc().AddInstruction(i) c.activefunc().AddInstruction(i)
} }


// Label adds a label to the active function. // Label adds a label to the active function.
func (c *Context) Label(name string) { func (c *Context) Label(name string) {
c.activefunc().AddLabel(avo.Label(name)) c.activefunc().AddLabel(ir.Label(name))
} }


func (c *Context) activefunc() *avo.Function { func (c *Context) activefunc() *ir.Function {
if c.function == nil { if c.function == nil {
c.adderrormessage("no active function") c.adderrormessage("no active function")
return avo.NewFunction("") return ir.NewFunction("")
} }
return c.function return c.function
} }
Expand All @@ -145,7 +145,7 @@ func (c *Context) activefunc() *avo.Function {


// StaticGlobal adds a new static data section to the file and returns a pointer to it. // StaticGlobal adds a new static data section to the file and returns a pointer to it.
func (c *Context) StaticGlobal(name string) operand.Mem { func (c *Context) StaticGlobal(name string) operand.Mem {
c.global = avo.NewStaticGlobal(name) c.global = ir.NewStaticGlobal(name)
c.file.AddSection(c.global) c.file.AddSection(c.global)
return c.global.Base() return c.global.Base()
} }
Expand All @@ -157,7 +157,7 @@ func (c *Context) DataAttributes(a attr.Attribute) {


// AddDatum adds constant v at offset to the current active global data section. // AddDatum adds constant v at offset to the current active global data section.
func (c *Context) AddDatum(offset int, v operand.Constant) { func (c *Context) AddDatum(offset int, v operand.Constant) {
if err := c.activeglobal().AddDatum(avo.NewDatum(offset, v)); err != nil { if err := c.activeglobal().AddDatum(ir.NewDatum(offset, v)); err != nil {
c.adderror(err) c.adderror(err)
} }
} }
Expand All @@ -167,10 +167,10 @@ func (c *Context) AppendDatum(v operand.Constant) {
c.activeglobal().Append(v) c.activeglobal().Append(v)
} }


func (c *Context) activeglobal() *avo.Global { func (c *Context) activeglobal() *ir.Global {
if c.global == nil { if c.global == nil {
c.adderrormessage("no active global") c.adderrormessage("no active global")
return avo.NewStaticGlobal("") return ir.NewStaticGlobal("")
} }
return c.global return c.global
} }
Expand All @@ -184,6 +184,6 @@ func (c *Context) adderrormessage(msg string) {
} }


// Result returns the built file and any accumulated errors. // Result returns the built file and any accumulated errors.
func (c *Context) Result() (*avo.File, error) { func (c *Context) Result() (*ir.File, error) {
return c.file, c.errs.Err() return c.file, c.errs.Err()
} }
1 change: 0 additions & 1 deletion internal/gen/avotypes.go
Expand Up @@ -7,7 +7,6 @@ import (


const ( const (
pkg = "github.com/mmcloughlin/avo" pkg = "github.com/mmcloughlin/avo"
instType = "avo.Instruction"
operandType = "operand.Op" operandType = "operand.Op"
) )


Expand Down
6 changes: 3 additions & 3 deletions internal/gen/ctors.go
Expand Up @@ -27,7 +27,7 @@ func (c *ctors) Generate(is []inst.Instruction) ([]byte, error) {
c.Printf("package x86\n\n") c.Printf("package x86\n\n")
c.Printf("import (\n") c.Printf("import (\n")
c.Printf("\t\"errors\"\n") c.Printf("\t\"errors\"\n")
c.Printf("\t\"%s\"\n", pkg) c.Printf("\tintrep \"%s/ir\"\n", pkg)
c.Printf("\t\"%s/reg\"\n", pkg) c.Printf("\t\"%s/reg\"\n", pkg)
c.Printf("\t\"%s/operand\"\n", pkg) c.Printf("\t\"%s/operand\"\n", pkg)
c.Printf(")\n\n") c.Printf(")\n\n")
Expand All @@ -46,7 +46,7 @@ func (c *ctors) instruction(i inst.Instruction) {


s := params(i) s := params(i)


c.Printf("func %s(%s) (*%s, error) {\n", i.Opcode, s.ParameterList(), instType) c.Printf("func %s(%s) (*intrep.Instruction, error) {\n", i.Opcode, s.ParameterList())
c.forms(i, s) c.forms(i, s)
c.Printf("}\n\n") c.Printf("}\n\n")
} }
Expand Down Expand Up @@ -85,7 +85,7 @@ func (c *ctors) forms(i inst.Instruction, s signature) {


func construct(i inst.Instruction, f inst.Form, s signature) string { func construct(i inst.Instruction, f inst.Form, s signature) string {
buf := bytes.NewBuffer(nil) buf := bytes.NewBuffer(nil)
fmt.Fprintf(buf, "%s{\n", instType) fmt.Fprintf(buf, "intrep.Instruction{\n")
fmt.Fprintf(buf, "\tOpcode: %#v,\n", i.Opcode) fmt.Fprintf(buf, "\tOpcode: %#v,\n", i.Opcode)
fmt.Fprintf(buf, "\tOperands: %s,\n", s.ParameterSlice()) fmt.Fprintf(buf, "\tOperands: %s,\n", s.ParameterSlice())


Expand Down
2 changes: 2 additions & 0 deletions ir/doc.go
@@ -0,0 +1,2 @@
// Package ir provides the intermediate representation of avo programs.
package ir
2 changes: 1 addition & 1 deletion ast.go → ir/ir.go
@@ -1,4 +1,4 @@
package avo package ir


import ( import (
"errors" "errors"
Expand Down
14 changes: 7 additions & 7 deletions pass/cfg.go
Expand Up @@ -4,16 +4,16 @@ import (
"errors" "errors"
"fmt" "fmt"


"github.com/mmcloughlin/avo" "github.com/mmcloughlin/avo/ir"
) )


// LabelTarget populates the LabelTarget of the given function. This maps from // LabelTarget populates the LabelTarget of the given function. This maps from
// label name to the following instruction. // label name to the following instruction.
func LabelTarget(fn *avo.Function) error { func LabelTarget(fn *ir.Function) error {
target := map[avo.Label]*avo.Instruction{} target := map[ir.Label]*ir.Instruction{}
for idx := 0; idx < len(fn.Nodes); idx++ { for idx := 0; idx < len(fn.Nodes); idx++ {
// Is this a label? // Is this a label?
lbl, ok := fn.Nodes[idx].(avo.Label) lbl, ok := fn.Nodes[idx].(ir.Label)
if !ok { if !ok {
continue continue
} }
Expand All @@ -27,7 +27,7 @@ func LabelTarget(fn *avo.Function) error {
} }
idx++ idx++
// Should be an instruction. // Should be an instruction.
i, ok := fn.Nodes[idx].(*avo.Instruction) i, ok := fn.Nodes[idx].(*ir.Instruction)
if !ok { if !ok {
return errors.New("instruction should follow a label") return errors.New("instruction should follow a label")
} }
Expand All @@ -38,14 +38,14 @@ func LabelTarget(fn *avo.Function) error {
} }


// CFG constructs the call-flow-graph for the function. // CFG constructs the call-flow-graph for the function.
func CFG(fn *avo.Function) error { func CFG(fn *ir.Function) error {
is := fn.Instructions() is := fn.Instructions()
n := len(is) n := len(is)


// Populate successors. // Populate successors.
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
cur := is[i] cur := is[i]
var nxt *avo.Instruction var nxt *ir.Instruction
if i+1 < n { if i+1 < n {
nxt = is[i+1] nxt = is[i+1]
} }
Expand Down

0 comments on commit 0f63e09

Please sign in to comment.