Skip to content

Commit

Permalink
unified object refactoring (XL)
Browse files Browse the repository at this point in the history
This change continues the unified object refactoring.

The new object builder is now in use in ssa2pal instead
of the old one.  This is still buggy, crashing on analyzing
its own source code, but atleast several packages are built
without crash, and atleast things compile.

results pkgres and related are not yet refactored.
  • Loading branch information
scott-cotton committed Aug 18, 2021
1 parent a6c3032 commit bcbf71a
Show file tree
Hide file tree
Showing 25 changed files with 540 additions and 288 deletions.
2 changes: 2 additions & 0 deletions docs/design/func.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# pal functional architecture




## Memory Models

Memory is modelled as a set of nodes (as in nodes in a graph), called _locs_.
Expand Down
2 changes: 0 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<<<<<<< HEAD
# pal website

Hello
Expand All @@ -9,4 +8,3 @@ pal is a library for doing pointer analysis.



>>>>>>> proto
126 changes: 100 additions & 26 deletions indexing/constvals.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,63 +15,121 @@
package indexing

import (
"fmt"
"io"
"strconv"

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

type consts struct{}
type C struct{ p *int64 }

func (c C) PlainEncode(w io.Writer) error {
if c.p == nil {
_, err := fmt.Fprintf(w, ".")
return err
}
_, err := w.Write(strconv.AppendInt(nil, *c.p, 16))
return err
}

func isHexLower(b byte) bool {
return (b >= byte('0') && b <= byte('9')) || (b >= byte('a') && b <= byte('f'))
}

func (c C) PlainDecode(r io.Reader) error {
var buf [16]byte
_, err := io.ReadFull(r, buf[:1])
if err != nil {
return err
}
if buf[0] == byte('.') {
c.p = nil
return nil
}
i := 1
for i < 16 {
_, err = io.ReadFull(r, buf[i:i+1])
if err != nil {
return err
}
if !isHexLower(buf[i]) {
break
}

i++
}
v, _ := strconv.ParseInt(string(buf[:i]), 16, 64)
*c.p = v
return nil
}

func ConstVals() T {
return consts{}
}

var zero int64 = 0
var one int64 = 1

func (c consts) Zero() I {
return I(0)
z := int64(0)
return I(C{&z})
}

func (c consts) One() I {
return I(1)
o := int64(1)
return I(C{&o})
}

func (c consts) IsVar(v I) bool {
return false
return v.(C).p == nil
}

func (c consts) Var() I {
panic("constant variable requested.")
return C{nil}
}

func (c consts) FromInt(v int) I {
return v
func (c consts) FromInt64(v int64) I {
return C{&v}
}

func (c consts) ToInt(v I) (int, bool) {
vv, ok := v.(int)
if !ok {
func (c consts) ToInt64(v I) (int64, bool) {
p := v.(C).p
if p == nil {
return 0, false
}
return vv, true
return *p, true
}

func (c consts) Plus(a, b I) I {
aa, bb := a.(int), b.(int)
return I(aa + bb)
pa, pb := a.(C).p, b.(C).p
if pa == nil || pb == nil {
return c.Var()
}
r := *pa + *pb
return c.FromInt64(r)
}

func (c consts) Times(a, b I) I {
aa, bb := a.(int), b.(int)
return I(aa * bb)
pa, pb := a.(C).p, b.(C).p
if pa == nil || pb == nil {
return c.Var()
}
r := *pa * *pb
return c.FromInt64(r)
}

func (c consts) Div(a, b I) (I, xtruth.T) {
switch c.Equal(b, c.Zero()) {
case xtruth.True:
return c.Zero(), xtruth.False
case xtruth.False:
return I(a.(int) / b.(int)), xtruth.True
pa, pb := a.(C).p, b.(C).p
r := *pa / *pb
return c.FromInt64(r), xtruth.True
case xtruth.X:
panic("non-const op")
return c.Var(), xtruth.X
}
return c.Zero(), xtruth.X
}
Expand All @@ -81,21 +139,31 @@ func (c consts) Rem(a, b I) (I, xtruth.T) {
case xtruth.True:
return c.Zero(), xtruth.False
case xtruth.False:
return I(a.(int) % b.(int)), xtruth.True
pa, pb := a.(C).p, b.(C).p
r := *pa % *pb
return c.FromInt64(r), xtruth.True
case xtruth.X:
panic("non-const op")
return c.Var(), xtruth.X
}
return c.Zero(), xtruth.X
}

func (c consts) Band(a, b I) I {
aa, bb := a.(int), b.(int)
return I(aa & bb)
pa, pb := a.(C).p, b.(C).p
if pa == nil || pb == nil {
return c.Var()
}
z := *pa & *pb
return c.FromInt64(z)
}

func (c consts) Bnot(a I) I {
aa := a.(int)
return I(^aa)
pa := a.(C).p
if pa == nil {
return c.Var()
}
z := ^(*pa)
return c.FromInt64(z)
}

func (c consts) Lshift(a, s I) (I, xtruth.T) {
Expand All @@ -107,16 +175,22 @@ func (c consts) Rshift(a, s I) (I, xtruth.T) {
}

func (c consts) Less(a, b I) xtruth.T {
aa, bb := a.(int), b.(int)
if aa < bb {
pa, pb := a.(C).p, b.(C).p
if pa == nil || pb == nil {
return xtruth.X
}
if *pa < *pb {
return xtruth.True
}
return xtruth.False
}

func (c consts) Equal(a, b I) xtruth.T {
aa, bb := a.(int), b.(int)
if aa == bb {
pa, pb := a.(C).p, b.(C).p
if pa == nil || pb == nil {
return xtruth.X
}
if *pa == *pb {
return xtruth.True
}
return xtruth.False
Expand Down
5 changes: 3 additions & 2 deletions indexing/t.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ import (
)

type I interface {
plain.Coder
}

type T interface {
// replace with go/constant.
Zero() I
One() I
// replace with go/constant?
ToInt(v I) (i int, ok bool)
ToInt64(v I) (i int64, ok bool)

FromInt(i int) I
FromInt64(i int64) I

IsVar(v I) bool
Var() I
Expand Down
34 changes: 34 additions & 0 deletions internal/plain/plain.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"bytes"
"fmt"
"io"
"strconv"

"strings"
)
Expand Down Expand Up @@ -89,6 +90,39 @@ func DecodeJoin(r io.Reader, sep string, ds ...Decoder) error {
return nil
}

func isHexLower(b byte) bool {
return (b >= byte('0') && b <= byte('9')) || (b >= byte('a') && b <= byte('f'))
}

func EncodeInt64(w io.Writer, v int64) error {
var buf [16]byte
_, err := w.Write(strconv.AppendInt(buf[:0], v, 16))
return err
}

func DecodeInt64(r io.Reader, p *int64) error {
var buf [16]byte
i := 0
var err error
for i < 16 {
_, err = io.ReadFull(r, buf[i:i+1])
if err != nil {
return err
}
if !isHexLower(buf[i]) {
break
}

i++
}
if i == 0 {
return fmt.Errorf("no plain i64 input")
}
v, _ := strconv.ParseInt(string(buf[:i]), 16, 64)
*p = v
return nil
}

type Coder interface {
Encoder
Decoder
Expand Down
4 changes: 0 additions & 4 deletions memory/constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,6 @@ func Store(dst, src Loc) Constraint {
return Constraint{Kind: KStore, Dest: dst, Src: src}
}

func Transfer(dst, src Loc) Constraint {
return TransferIndex(dst, src, 0)
}

func TransferIndex(dst, src Loc, i indexing.I) Constraint {
return Constraint{Kind: KTransfer, Dest: dst, Src: src, Index: i}
}
Expand Down
Loading

0 comments on commit bcbf71a

Please sign in to comment.