Skip to content

Commit bcbf71a

Browse files
committed
unified object refactoring (XL)
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.
1 parent a6c3032 commit bcbf71a

File tree

25 files changed

+540
-288
lines changed

25 files changed

+540
-288
lines changed

docs/design/func.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# pal functional architecture
22

33

4+
5+
46
## Memory Models
57

68
Memory is modelled as a set of nodes (as in nodes in a graph), called _locs_.

docs/index.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
<<<<<<< HEAD
21
# pal website
32

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

109

1110

12-
>>>>>>> proto

indexing/constvals.go

Lines changed: 100 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,63 +15,121 @@
1515
package indexing
1616

1717
import (
18+
"fmt"
1819
"io"
20+
"strconv"
1921

2022
"github.com/go-air/pal/xtruth"
2123
)
2224

2325
type consts struct{}
26+
type C struct{ p *int64 }
27+
28+
func (c C) PlainEncode(w io.Writer) error {
29+
if c.p == nil {
30+
_, err := fmt.Fprintf(w, ".")
31+
return err
32+
}
33+
_, err := w.Write(strconv.AppendInt(nil, *c.p, 16))
34+
return err
35+
}
36+
37+
func isHexLower(b byte) bool {
38+
return (b >= byte('0') && b <= byte('9')) || (b >= byte('a') && b <= byte('f'))
39+
}
40+
41+
func (c C) PlainDecode(r io.Reader) error {
42+
var buf [16]byte
43+
_, err := io.ReadFull(r, buf[:1])
44+
if err != nil {
45+
return err
46+
}
47+
if buf[0] == byte('.') {
48+
c.p = nil
49+
return nil
50+
}
51+
i := 1
52+
for i < 16 {
53+
_, err = io.ReadFull(r, buf[i:i+1])
54+
if err != nil {
55+
return err
56+
}
57+
if !isHexLower(buf[i]) {
58+
break
59+
}
60+
61+
i++
62+
}
63+
v, _ := strconv.ParseInt(string(buf[:i]), 16, 64)
64+
*c.p = v
65+
return nil
66+
}
2467

2568
func ConstVals() T {
2669
return consts{}
2770
}
2871

72+
var zero int64 = 0
73+
var one int64 = 1
74+
2975
func (c consts) Zero() I {
30-
return I(0)
76+
z := int64(0)
77+
return I(C{&z})
3178
}
3279

3380
func (c consts) One() I {
34-
return I(1)
81+
o := int64(1)
82+
return I(C{&o})
3583
}
3684

3785
func (c consts) IsVar(v I) bool {
38-
return false
86+
return v.(C).p == nil
3987
}
4088

4189
func (c consts) Var() I {
42-
panic("constant variable requested.")
90+
return C{nil}
4391
}
4492

45-
func (c consts) FromInt(v int) I {
46-
return v
93+
func (c consts) FromInt64(v int64) I {
94+
return C{&v}
4795
}
4896

49-
func (c consts) ToInt(v I) (int, bool) {
50-
vv, ok := v.(int)
51-
if !ok {
97+
func (c consts) ToInt64(v I) (int64, bool) {
98+
p := v.(C).p
99+
if p == nil {
52100
return 0, false
53101
}
54-
return vv, true
102+
return *p, true
55103
}
56104

57105
func (c consts) Plus(a, b I) I {
58-
aa, bb := a.(int), b.(int)
59-
return I(aa + bb)
106+
pa, pb := a.(C).p, b.(C).p
107+
if pa == nil || pb == nil {
108+
return c.Var()
109+
}
110+
r := *pa + *pb
111+
return c.FromInt64(r)
60112
}
61113

62114
func (c consts) Times(a, b I) I {
63-
aa, bb := a.(int), b.(int)
64-
return I(aa * bb)
115+
pa, pb := a.(C).p, b.(C).p
116+
if pa == nil || pb == nil {
117+
return c.Var()
118+
}
119+
r := *pa * *pb
120+
return c.FromInt64(r)
65121
}
66122

67123
func (c consts) Div(a, b I) (I, xtruth.T) {
68124
switch c.Equal(b, c.Zero()) {
69125
case xtruth.True:
70126
return c.Zero(), xtruth.False
71127
case xtruth.False:
72-
return I(a.(int) / b.(int)), xtruth.True
128+
pa, pb := a.(C).p, b.(C).p
129+
r := *pa / *pb
130+
return c.FromInt64(r), xtruth.True
73131
case xtruth.X:
74-
panic("non-const op")
132+
return c.Var(), xtruth.X
75133
}
76134
return c.Zero(), xtruth.X
77135
}
@@ -81,21 +139,31 @@ func (c consts) Rem(a, b I) (I, xtruth.T) {
81139
case xtruth.True:
82140
return c.Zero(), xtruth.False
83141
case xtruth.False:
84-
return I(a.(int) % b.(int)), xtruth.True
142+
pa, pb := a.(C).p, b.(C).p
143+
r := *pa % *pb
144+
return c.FromInt64(r), xtruth.True
85145
case xtruth.X:
86-
panic("non-const op")
146+
return c.Var(), xtruth.X
87147
}
88148
return c.Zero(), xtruth.X
89149
}
90150

91151
func (c consts) Band(a, b I) I {
92-
aa, bb := a.(int), b.(int)
93-
return I(aa & bb)
152+
pa, pb := a.(C).p, b.(C).p
153+
if pa == nil || pb == nil {
154+
return c.Var()
155+
}
156+
z := *pa & *pb
157+
return c.FromInt64(z)
94158
}
95159

96160
func (c consts) Bnot(a I) I {
97-
aa := a.(int)
98-
return I(^aa)
161+
pa := a.(C).p
162+
if pa == nil {
163+
return c.Var()
164+
}
165+
z := ^(*pa)
166+
return c.FromInt64(z)
99167
}
100168

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

109177
func (c consts) Less(a, b I) xtruth.T {
110-
aa, bb := a.(int), b.(int)
111-
if aa < bb {
178+
pa, pb := a.(C).p, b.(C).p
179+
if pa == nil || pb == nil {
180+
return xtruth.X
181+
}
182+
if *pa < *pb {
112183
return xtruth.True
113184
}
114185
return xtruth.False
115186
}
116187

117188
func (c consts) Equal(a, b I) xtruth.T {
118-
aa, bb := a.(int), b.(int)
119-
if aa == bb {
189+
pa, pb := a.(C).p, b.(C).p
190+
if pa == nil || pb == nil {
191+
return xtruth.X
192+
}
193+
if *pa == *pb {
120194
return xtruth.True
121195
}
122196
return xtruth.False

indexing/t.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,17 @@ import (
2020
)
2121

2222
type I interface {
23+
plain.Coder
2324
}
2425

2526
type T interface {
2627
// replace with go/constant.
2728
Zero() I
2829
One() I
2930
// replace with go/constant?
30-
ToInt(v I) (i int, ok bool)
31+
ToInt64(v I) (i int64, ok bool)
3132

32-
FromInt(i int) I
33+
FromInt64(i int64) I
3334

3435
IsVar(v I) bool
3536
Var() I

internal/plain/plain.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"bytes"
2424
"fmt"
2525
"io"
26+
"strconv"
2627

2728
"strings"
2829
)
@@ -89,6 +90,39 @@ func DecodeJoin(r io.Reader, sep string, ds ...Decoder) error {
8990
return nil
9091
}
9192

93+
func isHexLower(b byte) bool {
94+
return (b >= byte('0') && b <= byte('9')) || (b >= byte('a') && b <= byte('f'))
95+
}
96+
97+
func EncodeInt64(w io.Writer, v int64) error {
98+
var buf [16]byte
99+
_, err := w.Write(strconv.AppendInt(buf[:0], v, 16))
100+
return err
101+
}
102+
103+
func DecodeInt64(r io.Reader, p *int64) error {
104+
var buf [16]byte
105+
i := 0
106+
var err error
107+
for i < 16 {
108+
_, err = io.ReadFull(r, buf[i:i+1])
109+
if err != nil {
110+
return err
111+
}
112+
if !isHexLower(buf[i]) {
113+
break
114+
}
115+
116+
i++
117+
}
118+
if i == 0 {
119+
return fmt.Errorf("no plain i64 input")
120+
}
121+
v, _ := strconv.ParseInt(string(buf[:i]), 16, 64)
122+
*p = v
123+
return nil
124+
}
125+
92126
type Coder interface {
93127
Encoder
94128
Decoder

memory/constraint.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,6 @@ func Store(dst, src Loc) Constraint {
8282
return Constraint{Kind: KStore, Dest: dst, Src: src}
8383
}
8484

85-
func Transfer(dst, src Loc) Constraint {
86-
return TransferIndex(dst, src, 0)
87-
}
88-
8985
func TransferIndex(dst, src Loc, i indexing.I) Constraint {
9086
return Constraint{Kind: KTransfer, Dest: dst, Src: src, Index: i}
9187
}

0 commit comments

Comments
 (0)