Skip to content

Commit

Permalink
HTML output of new IR to ssa.html
Browse files Browse the repository at this point in the history
  • Loading branch information
rj45 committed Dec 23, 2021
1 parent 3c94abd commit c94e2d3
Show file tree
Hide file tree
Showing 11 changed files with 1,276 additions and 10 deletions.
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@
- [x] Get it to parse programs to ir2
- [x] Handle translating tuples to multiple return values
- [x] Can emit IR code in a way that simplifies text, assembly and html generation
- [ ] Emits to ssa.html
- [x] Emits to ssa.html
- [ ] Implement a simplified type system
- [ ] integer types i/u 8,16,32,64
- [ ] bool type
Expand Down
27 changes: 27 additions & 0 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/rj45/nanogo/frontend"
"github.com/rj45/nanogo/goenv"
"github.com/rj45/nanogo/html"
html2 "github.com/rj45/nanogo/html2"
"github.com/rj45/nanogo/ir2"
"github.com/rj45/nanogo/ir2/parseir"
"github.com/rj45/nanogo/parser"
Expand Down Expand Up @@ -64,6 +65,18 @@ func (nopDumper) WriteAsm(string, *asm.Func)
func (nopDumper) WriteSources(phase string, fn string, lines []string, startline int) {}
func (nopDumper) Close() {}

type dumper2 interface {
WritePhase(string, string)
WriteSources(phase string, fn string, lines []string, startline int)
Close()
}

type nopDumper2 struct{}

func (nopDumper2) WritePhase(string, string) {}
func (nopDumper2) WriteSources(phase string, fn string, lines []string, startline int) {}
func (nopDumper2) Close() {}

type nopWriteCloser struct{ w io.Writer }

func (nopWriteCloser) Close() error {
Expand Down Expand Up @@ -123,9 +136,23 @@ func Compile(outname, dir string, patterns []string, mode Mode) int {
fe := frontend.NewFrontEnd(dir, patterns...)
fe.Scan()
for fn := fe.NextUnparsedFunc(); fn != nil; fn = fe.NextUnparsedFunc() {
var w dumper2
w = nopDumper2{}
if *dump != "" && strings.Contains(fn.FullName, *dump) {
w = html2.NewHTMLWriter("ssa.html", fn)
filename, lines, start := fe.DumpOrignalSource(fn)
w.WriteSources("go", filename, lines, start)
// w.WriteAsmBuf("tools/go/ssa", parser.DumpOriginalSSA(fn))
}
defer w.Close()

fe.ParseFunc(fn)

w.WritePhase("initial", "initial")

xform2.Transform(xform2.Elaboration, fn)

w.WritePhase("elaboration", "elaboration")
}

fe.Program().Emit(finalout, ir2.SSAString{})
Expand Down
14 changes: 11 additions & 3 deletions frontend/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,15 @@ func NewFrontEnd(dir string, patterns ...string) *FrontEnd {
log.Fatal(err)
}

return &FrontEnd{prog: &ir2.Program{}, members: members}
var fset *token.FileSet
if len(members) > 0 {
fset = members[0].Package().Prog.Fset
}

return &FrontEnd{
prog: &ir2.Program{FileSet: fset},
members: members,
}
}

var dumptypes = flag.Bool("dumptypes", false, "Dump all types in a program")
Expand Down Expand Up @@ -148,15 +156,15 @@ func (fe *FrontEnd) DumpOrignalSource(fn *ir2.Func) (filename string, lines []st

startp := fset.PositionFor(start, true)
filename = startp.Filename
startline = startp.Line - 1
startline = startp.Line

endp := fset.PositionFor(end, true)
buf, err := os.ReadFile(startp.Filename)
if err != nil {
log.Fatal(err)
}
lines = strings.Split(string(buf), "\n")
lines = lines[startline:endp.Line]
lines = lines[startline-1 : endp.Line]

return
}
Expand Down
7 changes: 4 additions & 3 deletions frontend/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ func (fe *FrontEnd) translateFunc(irFunc *ir2.Func, ssaFunc *ssa.Function) {
irFunc.InsertBlock(-1, irBlock)

if bn == 0 {
for _, param := range ssaFunc.Params {
instr := irFunc.NewInstr(op.Parameter, param.Type(), param.Name())
for i, param := range ssaFunc.Params {
instr := irFunc.NewInstr(op.Parameter, param.Type(), i)
irBlock.InsertInstr(-1, instr)

// todo: fixme for multiple defs
instr.Pos = getPos(param)

fe.val2instr[param] = instr
}
}
Expand Down
2 changes: 2 additions & 0 deletions frontend/instrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ func (fe *FrontEnd) translateInstrs(irBlock *ir2.Block, ssaBlock *ssa.BasicBlock
ins.InsertArg(-1, irBlock.Func().ValueFor(typ, con))
}

ins.Pos = getPos(instr)

if arg != nil {
ins.InsertArg(-1, arg)
}
Expand Down
6 changes: 6 additions & 0 deletions frontend/posinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ func getPos(val posser) token.Pos {
// No position information is known.
switch val := val.(type) {
case *ssa.MakeInterface:
refs := val.Referrers()
for _, ref := range *refs {
if getPos(ref) != token.NoPos {
return getPos(ref)
}
}
return getPos(val.X)
case *ssa.MakeClosure:
return val.Fn.(*ssa.Function).Pos()
Expand Down
Loading

0 comments on commit c94e2d3

Please sign in to comment.