Skip to content

Commit

Permalink
objects: chan creation and <-
Browse files Browse the repository at this point in the history
  • Loading branch information
scott-cotton committed Aug 22, 2021
1 parent 1f570e3 commit 5f0dfe4
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 10 deletions.
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Package pal provides a pointer analysis library.
// Package pal provides a pointer analysis library.
//
// ⊧
package pal
26 changes: 23 additions & 3 deletions objects/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ type Builder struct {
indexing indexing.T
mmod *memory.Model
ts *typeset.TypeSet
omap map[memory.Loc]Object
start memory.Loc
mgp *memory.GenParams
omap map[memory.Loc]Object // map memory locs to canonical objects
start memory.Loc // after import, what is our minimum?
mgp *memory.GenParams // memory loc generation parameters
}

// NewBuilder creates a new builder from a package Path
Expand Down Expand Up @@ -147,6 +147,13 @@ func (b *Builder) Map(gty *types.Map) *Map {
return b.omap[m].(*Map)
}

func (b *Builder) Chan(gty *types.Chan) *Chan {
ty := b.ts.FromGoType(gty)
m := b.Type(ty).Gen()
b.walkObj(m)
return b.omap[m].(*Chan)
}

func (b *Builder) Object(m memory.Loc) Object {
return b.omap[m]
}
Expand Down Expand Up @@ -269,6 +276,19 @@ func (b *Builder) walkObj(m memory.Loc) {
}

case typeset.Chan:
var ch *Chan
obj := b.omap[m]
if obj == nil {
ch = &Chan{}
ch.loc = m
ch.typ = ty
ch.slot = b.Type(b.ts.Elem(ty)).Gen()
b.mmod.AddAddressOf(ch.loc, ch.slot)
b.omap[m] = ch
} else {
ch = obj.(*Chan)
}
b.walkObj(ch.slot)
case typeset.Map:
var ma *Map
obj := b.omap[m]
Expand Down
35 changes: 31 additions & 4 deletions objects/chan.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,44 @@

package objects

import "io"
import (
"io"

"github.com/go-air/pal/memory"
)

// Chan object
//
// c.loc represents pointer
// c.slot represents things sent to
// and received from the channel.
type Chan struct {
object
slot memory.Loc
}

func (c *Chan) Recv(dst memory.Loc, mm *memory.Model) {
mm.AddLoad(dst, c.loc)
}

func (c *Chan) Send(src memory.Loc, mm *memory.Model) {
mm.AddStore(c.loc, src)
}

func (c *Chan) PlainEncode(w io.Writer) error {
return nil
var err error
if err = c.object.plainEncode(w); err != nil {
return err
}
return c.slot.PlainEncode(w)
}

func (c *Chan) PlainDecode(r io.Reader) error {
return nil
var err error
p := &c.object
if err = p.plainDecode(r); err != nil {
return err
}
ps := &c.slot
return ps.PlainDecode(r)
}

10 changes: 8 additions & 2 deletions ssa2pal/t.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,9 @@ func (p *T) genValueLoc(v ssa.Value) memory.Loc {
res = p.buildr.Func(ty, "", memory.NoAttrs).Loc()
case *types.Pointer:
res = p.buildr.Pointer(ty).Loc()
case *types.Basic:
res = p.buildr.Gen()
case *types.Chan:
res = p.buildr.Chan(ty).Loc()
case *types.Basic:
res = p.buildr.Gen()
case *types.Interface:
res = p.buildr.Gen()
Expand Down Expand Up @@ -493,6 +493,12 @@ func (p *T) genI9nConstraints(fnName string, i9n ssa.Instruction) error {
case token.MUL: // *p
p.buildr.AddLoad(p.vmap[i9n], p.vmap[i9n.X])
case token.ARROW: // <- TBD:
c := p.buildr.Object(p.vmap[i9n.X]).(*objects.Chan)
dst := p.vmap[i9n]
if dst == memory.NoLoc {
panic("no loc for <-")
}
c.Recv(dst, p.buildr.Memory())

default: // indexing
}
Expand Down

0 comments on commit 5f0dfe4

Please sign in to comment.