Skip to content

Commit 3341c6d

Browse files
committed
sketching around object management
1 parent 192b6cb commit 3341c6d

File tree

12 files changed

+261
-35
lines changed

12 files changed

+261
-35
lines changed

.github/FUNDING.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# Copyright 2021 The pal authors (see AUTHORS)
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
# These are supported funding model platforms
216

317
github: [go-air] # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]

docs/_config.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
1+
# Copyright 2021 The pal authors (see AUTHORS)
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
theme: jekyll-theme-slate

memory/loc.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"github.com/go-air/pal/indexing"
2323
"github.com/go-air/pal/internal/plain"
24+
"github.com/go-air/pal/typeset"
2425
)
2526

2627
// Loc represents a memory location.
@@ -53,6 +54,7 @@ type loc struct {
5354
srcInfo SrcInfo
5455
root Loc
5556
parent Loc
57+
typ typeset.Type
5658

5759
lsz indexing.I // == 1 + Sum({c.vsz | c.parent == loc and c != loc})
5860

memory/model.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,77 @@ func (mod *Model) Check() error {
210210
return nil
211211
}
212212

213+
// p_add adds a root recursively according to ty.
214+
//
215+
// add is responsible for setting the size, parent, class, attrs, and root
216+
// of all added nodes.
217+
//
218+
func (mod *Model) p_add(ty types.Type, class Class, attrs Attrs, sk SrcKind, pos token.Pos, p, r Loc, sum *int) Loc {
219+
n := Loc(uint32(len(mod.locs)))
220+
l := loc{
221+
parent: p,
222+
root: r,
223+
class: class,
224+
attrs: attrs}
225+
lastSum := *sum
226+
switch ty := ty.(type) {
227+
case *types.Signature:
228+
// a virtual place for the func
229+
mod.locs = append(mod.locs, l)
230+
*sum++
231+
232+
case *types.Basic, *types.Pointer, *types.Interface:
233+
mod.locs = append(mod.locs, l)
234+
*sum++
235+
case *types.Array:
236+
mod.locs = append(mod.locs, l)
237+
*sum++
238+
m := int(ty.Len())
239+
for i := 0; i < m; i++ {
240+
mod.add(ty.Elem(), class, attrs, sk, pos, n, r, sum)
241+
}
242+
case *types.Map:
243+
mod.locs = append(mod.locs, l)
244+
*sum++
245+
mod.add(ty.Key(), class, attrs, sk, pos, n, r, sum)
246+
mod.add(ty.Elem(), class, attrs, sk, pos, n, r, sum)
247+
case *types.Struct:
248+
mod.locs = append(mod.locs, l)
249+
*sum++
250+
nf := ty.NumFields()
251+
for i := 0; i < nf; i++ {
252+
fty := ty.Field(i).Type()
253+
mod.add(fty, class, attrs, sk, pos, n, r, sum)
254+
}
255+
case *types.Slice:
256+
mod.locs = append(mod.locs, l)
257+
*sum++
258+
mod.add(ty.Elem(), class, attrs, sk, pos, n, r, sum)
259+
case *types.Chan:
260+
mod.locs = append(mod.locs, l)
261+
*sum++
262+
mod.add(ty.Elem(), class, attrs, sk, pos, n, r, sum)
263+
264+
case *types.Tuple:
265+
mod.locs = append(mod.locs, l)
266+
*sum++
267+
tn := ty.Len()
268+
for i := 0; i < tn; i++ {
269+
mod.add(ty.At(i).Type(), class, attrs, sk, pos, n, r, sum)
270+
}
271+
case *types.Named:
272+
// no space reserved for named types, go to
273+
// underlying
274+
return mod.add(ty.Underlying(), class, attrs, sk, pos, p, r, sum)
275+
276+
default:
277+
panic(fmt.Sprintf("%s: unexpected/unimplemented", ty))
278+
}
279+
// we added a slot at dst[n] for ty, set it
280+
mod.locs[n].lsz = mod.indexing.FromInt(*sum - lastSum)
281+
return n
282+
}
283+
213284
// add adds a root recursively according to ty.
214285
//
215286
// add is responsible for setting the size, parent, class, attrs, and root

objects/builder.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright 2021 The pal authors (see AUTHORS)
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package objects
16+
17+
import (
18+
"go/token"
19+
"go/types"
20+
21+
"github.com/go-air/pal/indexing"
22+
"github.com/go-air/pal/memory"
23+
"github.com/go-air/pal/typeset"
24+
)
25+
26+
type Builder struct {
27+
pkgPath string
28+
indexing indexing.T
29+
mmod *memory.Model
30+
ts *typeset.TypeSet
31+
objects []Object
32+
33+
start memory.Loc
34+
35+
class memory.Class
36+
attrs memory.Attrs
37+
pos token.Pos
38+
srcKind memory.SrcKind
39+
typ typeset.Type
40+
}
41+
42+
func NewBuilder(pkgPath string, ind indexing.T, mem *memory.Model, ts *typeset.TypeSet) *Builder {
43+
b := &Builder{}
44+
b.pkgPath = pkgPath
45+
b.indexing = ind
46+
b.mmod = mem
47+
b.ts = ts
48+
return b
49+
}
50+
51+
func (b *Builder) Reset() *Builder {
52+
b.class = memory.Class(0)
53+
b.attrs = memory.Attrs(0)
54+
b.pos = token.NoPos
55+
b.srcKind = memory.SrcKind(0)
56+
b.typ = typeset.NoType
57+
return b
58+
}
59+
60+
func (b *Builder) Class(c memory.Class) *Builder {
61+
b.class = c
62+
return b
63+
}
64+
65+
func (b *Builder) Attrs(a memory.Attrs) *Builder {
66+
b.attrs = a
67+
return b
68+
}
69+
70+
func (b *Builder) Pos(p token.Pos) *Builder {
71+
b.pos = p
72+
return b
73+
}
74+
75+
func (b *Builder) Kind(k memory.SrcKind) *Builder {
76+
b.srcKind = k
77+
return b
78+
}
79+
80+
func (b *Builder) Type(t typeset.Type) *Builder {
81+
b.typ = t
82+
return b
83+
}
84+
85+
func (b *Builder) GoType(t types.Type) *Builder {
86+
b.typ = b.ts.FromGoType(t)
87+
return b
88+
}

objects/doc.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2021 The pal authors (see AUTHORS)
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Package objects coordinates lower level memory and typeset
16+
// models with Go objects.
17+
package objects

objects/object.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ type object struct {
3131
typ typeset.Type
3232
}
3333

34-
func (o *object) Loc() memory.Loc { return o.loc }
34+
func (o *object) Loc() memory.Loc { return o.loc }
35+
func (o *object) Type() typeset.Type { return o.typ }

objects/pointer.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2021 The pal authors (see AUTHORS)
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package objects
16+
17+
type Pointer struct {
18+
object
19+
}

typeset/gotypes.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"go/types"
2020
)
2121

22-
func (t *T) FromGoType(gotype types.Type) Type {
22+
func (t *TypeSet) FromGoType(gotype types.Type) Type {
2323
// that line is a headache...
2424
switch ty := gotype.(type) {
2525
case *types.Basic:
@@ -142,6 +142,6 @@ func (t *T) FromGoType(gotype types.Type) Type {
142142
}
143143
}
144144

145-
func (t *T) ToGoType(ty Type) types.Type {
145+
func (t *TypeSet) ToGoType(ty Type) types.Type {
146146
return nil
147147
}

typeset/plain.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"io"
2020
)
2121

22-
func (t *T) PlainEncode(w io.Writer) error {
22+
func (t *TypeSet) PlainEncode(w io.Writer) error {
2323
N := len(t.nodes)
2424
_, err := fmt.Fprintf(w, "%d:%d\n", N-int(_endType), cap(t.hash))
2525
if err != nil {
@@ -38,7 +38,7 @@ func (t *T) PlainEncode(w io.Writer) error {
3838
return nil
3939
}
4040

41-
func (t *T) PlainDecode(r io.Reader) error {
41+
func (t *TypeSet) PlainDecode(r io.Reader) error {
4242
tt := New()
4343
var N int
4444
var H int

0 commit comments

Comments
 (0)