Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow branches without label #407

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions build/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ func (c *Context) Package(path string) {
c.pkg = pkg
}

// Include adds an include pre-processor directive for the provided path, if it
// is not already present.
func (c *Context) Include(path string) {
c.file.Include(path)
}

// Constraints sets build constraints for the file.
func (c *Context) Constraints(t buildtags.ConstraintsConvertable) {
cs := t.ToConstraints()
Expand Down Expand Up @@ -160,6 +166,11 @@ func (c *Context) Label(name string) {
c.activefunc().AddLabel(ir.Label(name))
}

// Preprocessor adds a preprocessor macro to the active function.
func (c *Context) Preprocessor(macro string) {
c.activefunc().AddPreprocessor(macro)
}

// Comment adds comment lines to the active function.
func (c *Context) Comment(lines ...string) {
c.activefunc().AddComment(lines...)
Expand Down
7 changes: 7 additions & 0 deletions build/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,16 @@ func Generate() {
}
}

// Include adds an include pre-processor directive for the provided path to the
// current file, if it is not already present.
func Include(path string) { ctx.Include(path) }

// Package sets the package the generated file will belong to. Required to be able to reference types in the package.
func Package(path string) { ctx.Package(path) }

// Preprocessor adds a pre-processor macro to the current function.
func Preprocessor(macro string) { ctx.Preprocessor(macro) }

// Constraints sets build constraints for the file.
func Constraints(t buildtags.ConstraintsConvertable) { ctx.Constraints(t) }

Expand Down
27 changes: 26 additions & 1 deletion ir/ir.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ir

import (
"errors"

"github.com/mmcloughlin/avo/attr"
"github.com/mmcloughlin/avo/buildtags"
"github.com/mmcloughlin/avo/gotypes"
Expand All @@ -15,6 +14,16 @@ type Node interface {
node()
}

// Preprocessor macro within a function.
type Preprocessor string

func (p Preprocessor) node() {}

// NewPreprocessor builds a Preprocessor from the provided macro string.
func NewPreprocessor(macro string) Preprocessor {
return Preprocessor(macro)
}

// Label within a function.
type Label string

Expand Down Expand Up @@ -162,6 +171,17 @@ func (f *File) Functions() []*Function {
return fns
}

// Include adds an include pre-processor directive for the provided path, if it
// is not already present.
func (f *File) Include(path string) {
for _, p := range f.Includes {
if p == path {
return
}
}
f.Includes = append(f.Includes, path)
}

// Pragma represents a function compiler directive.
type Pragma struct {
Directive string
Expand Down Expand Up @@ -230,6 +250,11 @@ func (f *Function) AddLabel(l Label) {
f.AddNode(l)
}

// AddPreprocessor adds a pre-processor macro to f.
func (f *Function) AddPreprocessor(macro string) {
f.AddNode(NewPreprocessor(macro))
}

// AddComment adds comment lines to f.
func (f *Function) AddComment(lines ...string) {
f.AddNode(NewComment(lines...))
Expand Down
13 changes: 6 additions & 7 deletions pass/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,13 @@ func CFG(fn *ir.Function) error {
// If it's a branch, locate the target.
if cur.IsBranch {
lbl := cur.TargetLabel()
if lbl == nil {
return errors.New("no label for branch instruction")
if lbl != nil {
target, found := fn.LabelTarget[*lbl]
if !found {
return fmt.Errorf("unknown label %q", *lbl)
}
cur.Succ = append(cur.Succ, target)
}
target, found := fn.LabelTarget[*lbl]
if !found {
return fmt.Errorf("unknown label %q", *lbl)
}
cur.Succ = append(cur.Succ, target)
}

// Otherwise, could continue to the following instruction.
Expand Down
3 changes: 3 additions & 0 deletions printer/goasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ func (p *goasm) function(f *ir.Function) {
for _, line := range n.Lines {
p.Printf("\t// %s\n", line)
}
case ir.Preprocessor:
p.flush()
p.Printf("#%s\n", string(n))
default:
panic("unexpected node type")
}
Expand Down
48 changes: 48 additions & 0 deletions printer/goasm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,51 @@ func TestOpcodeSuffixes(t *testing.T) {
"",
})
}

func TestPreprocessor(t *testing.T) {
ctx := build.NewContext()
ctx.Function("preprocessor")
ctx.SignatureExpr("func()")
ctx.Preprocessor("ifndef hasAVX2")
ctx.ADDQ(reg.RBX, reg.RAX)
ctx.VZEROUPPER()
ctx.Preprocessor("else")
ctx.ADDQ(reg.RAX, reg.RBX)
ctx.Preprocessor("endif")

AssertPrintsLines(t, ctx, printer.NewGoAsm, []string{
"// Code generated by avo. DO NOT EDIT.",
"",
"// func preprocessor()",
"TEXT ·preprocessor(SB), $0",
"#ifndef hasAVX2",
"\tADDQ BX, AX",
"\tVZEROUPPER",
"#else",
"\tADDQ AX, BX",
"#endif",
"",
})
}

func TestInclude(t *testing.T) {
ctx := build.NewContext()
ctx.Include("before.h")
ctx.Function("preprocessor")
ctx.SignatureExpr("func()")
ctx.Include("after.h")
ctx.VZEROUPPER()

AssertPrintsLines(t, ctx, printer.NewGoAsm, []string{
"// Code generated by avo. DO NOT EDIT.",
"",
"#include \"before.h\"",
"#include \"after.h\"",
"",
"// func preprocessor()",
"TEXT ·preprocessor(SB), $0",
"\tVZEROUPPER",
"",
})

}