Skip to content

Commit

Permalink
irgen: Prettify debug output and add test case for implicit void return.
Browse files Browse the repository at this point in the history
  • Loading branch information
mewmew committed Jun 14, 2016
1 parent fdd2b42 commit 12e2373
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 16 deletions.
8 changes: 8 additions & 0 deletions irgen/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@ package irgen

import (
"fmt"
"log"
"os"

"github.com/llir/llvm/ir"
"github.com/llir/llvm/ir/instruction"
irtypes "github.com/llir/llvm/ir/types"
"github.com/llir/llvm/ir/value"
"github.com/mewkiz/pkg/errutil"
"github.com/mewkiz/pkg/term"
"github.com/mewmew/uc/sem"
)

// dbg is a logger which prefixes debug messages with "irgen:".
var dbg *log.Logger

// A Module represents an LLVM IR module generator.
type Module struct {
// Module being generated.
Expand All @@ -21,6 +27,8 @@ type Module struct {

// NewModule returns a new module generator.
func NewModule(info *sem.Info) *Module {
// TODO: Remove debug output.
dbg = log.New(os.Stderr, term.WhiteBold("irgen:"), log.Lshortfile)
m := ir.NewModule()
return &Module{Module: m, info: info}
}
Expand Down
16 changes: 12 additions & 4 deletions irgen/irgen_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package irgen_test

import (
"fmt"
"io/ioutil"
"log"
"testing"

"github.com/mewmew/uc/ast"
Expand All @@ -17,6 +17,7 @@ func TestGen(t *testing.T) {
path string
want string
}{
// Global variable declarations.
{
path: "../testdata/extra/irgen/global_def.c",
want: "../testdata/extra/irgen/global_def.ll",
Expand All @@ -25,17 +26,23 @@ func TestGen(t *testing.T) {
path: "../testdata/extra/irgen/tentative_def.c",
want: "../testdata/extra/irgen/tentative_def.ll",
},
// Return statements.
{
path: "../testdata/extra/irgen/void_ret.c",
want: "../testdata/extra/irgen/void_ret.ll",
},
{
path: "../testdata/extra/irgen/implicit_void_ret.c",
want: "../testdata/extra/irgen/implicit_void_ret.ll",
},
{
path: "../testdata/extra/irgen/int_ret.c",
want: "../testdata/extra/irgen/int_ret.ll",
},
// Local variable declarations.
{
path: "../testdata/extra/irgen/local.c",
want: "../testdata/extra/irgen/local.ll",
path: "../testdata/extra/irgen/local_def.c",
want: "../testdata/extra/irgen/local_def.ll",
},
}

Expand Down Expand Up @@ -66,7 +73,8 @@ func TestGen(t *testing.T) {
}

// Generate IR.
log.Println("path:", g.want) // TODO: Remove debug output.
// TODO: Remove debug output.
fmt.Printf("\n=== [ %s ] ====================================\n\n", g.want)
module := irgen.Gen(file, info)

// Compare generated module against gold standard.
Expand Down
11 changes: 5 additions & 6 deletions irgen/lower.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package irgen

import (
"fmt"
"log"
"strconv"

"github.com/llir/llvm/ir"
Expand All @@ -29,7 +28,7 @@ func gen(file *ast.File, info *sem.Info) *ir.Module {
for _, decl := range file.Decls {
// Ignore tentative definitions.
if isTentativeDef(decl) {
log.Printf("ignoring tentative definition of %q", decl.Name())
dbg.Printf("ignoring tentative definition of %q", decl.Name())
continue
}
switch decl := decl.(type) {
Expand Down Expand Up @@ -60,14 +59,14 @@ func (m *Module) funcDecl(n *ast.FuncDecl) {
}
f := NewFunction(name, sig)
if !astutil.IsDef(n) {
log.Printf("create function declaration: %v", n)
dbg.Printf("create function declaration: %v", n)
// Emit function declaration.
m.emitFunc(f)
return
}

// Generate function body.
log.Printf("create function definition: %v", n)
dbg.Printf("create function definition: %v", n)
m.funcBody(f, n.Body)
}

Expand Down Expand Up @@ -99,7 +98,7 @@ func (m *Module) globalVarDecl(n *ast.VarDecl) {
// Output:
// @x = global i32 0
name := n.Name().Name
log.Printf("create global variable: %v", n)
dbg.Printf("create global variable: %v", n)
typ := toIrType(n.Type())
var val value.Value
switch {
Expand Down Expand Up @@ -140,7 +139,7 @@ func (m *Module) localVarDef(f *Function, n *ast.VarDecl) {
// Output:
// %a = alloca i32
name := n.Name().Name
log.Printf("create local variable: %v", n)
dbg.Printf("create local variable: %v", n)
typ := toIrType(n.Type())
inst, err := instruction.NewAlloca(typ, 1)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions irgen/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package irgen

import (
"fmt"
"log"

irtypes "github.com/llir/llvm/ir/types"
"github.com/mewkiz/pkg/errutil"
Expand Down Expand Up @@ -53,7 +52,7 @@ func toIrType(n uctypes.Type) irtypes.Type {
break
}
pt := toIrType(p.Type)
log.Printf("converting type %#v to %#v", p.Type, pt)
dbg.Printf("converting type %#v to %#v", p.Type, pt)
params = append(params, irtypes.NewParam(pt, ""))
}
result := toIrType(ucType.Result)
Expand Down
2 changes: 2 additions & 0 deletions testdata/extra/irgen/implicit_void_ret.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
void f() {
}
4 changes: 4 additions & 0 deletions testdata/extra/irgen/implicit_void_ret.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
define void @f() {
0:
ret void
}
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions testdata/extra/irgen/post_strip.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
for f in "int_ret.ll" "local.ll"; do
sar -i "\t%1 = alloca i32\n" "" "${f}"
sar -i "\tstore i32 0, i32[*] %1\n" "" "${f}"
done
#for f in "foo.ll"; do
# sar -i "\t%1 = alloca i32\n" "" "${f}"
# sar -i "\tstore i32 0, i32[*] %1\n" "" "${f}"
#done

0 comments on commit 12e2373

Please sign in to comment.