Permalink
Browse files

ast: move "ast" types from root to ir sub-package

Closes #32
  • Loading branch information...
mmcloughlin committed Jan 6, 2019
1 parent 4a920c2 commit 0f63e0906da271f335e77399026d16ba70bae528
Showing with 3,925 additions and 3,925 deletions.
  1. +15 −15 build/context.go
  2. +0 −1 internal/gen/avotypes.go
  3. +3 −3 internal/gen/ctors.go
  4. +2 −0 ir/doc.go
  5. +1 −1 ast.go → ir/ir.go
  6. +7 −7 pass/cfg.go
  7. +58 −59 pass/cfg_test.go
  8. +8 −8 pass/pass.go
  9. +5 −5 pass/reg.go
  10. +3 −3 pass/reg_test.go
  11. +5 −5 pass/textflag.go
  12. +10 −10 printer/goasm.go
  13. +2 −2 printer/printer.go
  14. +2 −2 printer/stubs.go
  15. +3,804 −3,804 x86/zctors.go
@@ -4,10 +4,10 @@ import (
"errors"
"go/types"

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

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

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

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

// 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)
}

// Label adds a label to the active function.
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 {
c.adderrormessage("no active function")
return avo.NewFunction("")
return ir.NewFunction("")
}
return c.function
}
@@ -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.
func (c *Context) StaticGlobal(name string) operand.Mem {
c.global = avo.NewStaticGlobal(name)
c.global = ir.NewStaticGlobal(name)
c.file.AddSection(c.global)
return c.global.Base()
}
@@ -157,7 +157,7 @@ func (c *Context) DataAttributes(a attr.Attribute) {

// AddDatum adds constant v at offset to the current active global data section.
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)
}
}
@@ -167,10 +167,10 @@ func (c *Context) AppendDatum(v operand.Constant) {
c.activeglobal().Append(v)
}

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

// 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()
}
@@ -7,7 +7,6 @@ import (

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

@@ -27,7 +27,7 @@ func (c *ctors) Generate(is []inst.Instruction) ([]byte, error) {
c.Printf("package x86\n\n")
c.Printf("import (\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/operand\"\n", pkg)
c.Printf(")\n\n")
@@ -46,7 +46,7 @@ func (c *ctors) instruction(i inst.Instruction) {

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.Printf("}\n\n")
}
@@ -85,7 +85,7 @@ func (c *ctors) forms(i inst.Instruction, s signature) {

func construct(i inst.Instruction, f inst.Form, s signature) string {
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, "\tOperands: %s,\n", s.ParameterSlice())

@@ -0,0 +1,2 @@
// Package ir provides the intermediate representation of avo programs.
package ir
@@ -1,4 +1,4 @@
package avo
package ir

import (
"errors"
@@ -4,16 +4,16 @@ import (
"errors"
"fmt"

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

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

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

// Populate successors.
for i := 0; i < n; i++ {
cur := is[i]
var nxt *avo.Instruction
var nxt *ir.Instruction
if i+1 < n {
nxt = is[i+1]
}
Oops, something went wrong.

0 comments on commit 0f63e09

Please sign in to comment.