Skip to content

Commit

Permalink
sketching around object management
Browse files Browse the repository at this point in the history
  • Loading branch information
scott-cotton committed Aug 16, 2021
1 parent 192b6cb commit 3341c6d
Show file tree
Hide file tree
Showing 12 changed files with 261 additions and 35 deletions.
14 changes: 14 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright 2021 The pal authors (see AUTHORS)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# These are supported funding model platforms

github: [go-air] # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
14 changes: 14 additions & 0 deletions docs/_config.yml
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
# Copyright 2021 The pal authors (see AUTHORS)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

theme: jekyll-theme-slate
2 changes: 2 additions & 0 deletions memory/loc.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/go-air/pal/indexing"
"github.com/go-air/pal/internal/plain"
"github.com/go-air/pal/typeset"
)

// Loc represents a memory location.
Expand Down Expand Up @@ -53,6 +54,7 @@ type loc struct {
srcInfo SrcInfo
root Loc
parent Loc
typ typeset.Type

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

Expand Down
71 changes: 71 additions & 0 deletions memory/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,77 @@ func (mod *Model) Check() error {
return nil
}

// p_add adds a root recursively according to ty.
//
// add is responsible for setting the size, parent, class, attrs, and root
// of all added nodes.
//
func (mod *Model) p_add(ty types.Type, class Class, attrs Attrs, sk SrcKind, pos token.Pos, p, r Loc, sum *int) Loc {
n := Loc(uint32(len(mod.locs)))
l := loc{
parent: p,
root: r,
class: class,
attrs: attrs}
lastSum := *sum
switch ty := ty.(type) {
case *types.Signature:
// a virtual place for the func
mod.locs = append(mod.locs, l)
*sum++

case *types.Basic, *types.Pointer, *types.Interface:
mod.locs = append(mod.locs, l)
*sum++
case *types.Array:
mod.locs = append(mod.locs, l)
*sum++
m := int(ty.Len())
for i := 0; i < m; i++ {
mod.add(ty.Elem(), class, attrs, sk, pos, n, r, sum)
}
case *types.Map:
mod.locs = append(mod.locs, l)
*sum++
mod.add(ty.Key(), class, attrs, sk, pos, n, r, sum)
mod.add(ty.Elem(), class, attrs, sk, pos, n, r, sum)
case *types.Struct:
mod.locs = append(mod.locs, l)
*sum++
nf := ty.NumFields()
for i := 0; i < nf; i++ {
fty := ty.Field(i).Type()
mod.add(fty, class, attrs, sk, pos, n, r, sum)
}
case *types.Slice:
mod.locs = append(mod.locs, l)
*sum++
mod.add(ty.Elem(), class, attrs, sk, pos, n, r, sum)
case *types.Chan:
mod.locs = append(mod.locs, l)
*sum++
mod.add(ty.Elem(), class, attrs, sk, pos, n, r, sum)

case *types.Tuple:
mod.locs = append(mod.locs, l)
*sum++
tn := ty.Len()
for i := 0; i < tn; i++ {
mod.add(ty.At(i).Type(), class, attrs, sk, pos, n, r, sum)
}
case *types.Named:
// no space reserved for named types, go to
// underlying
return mod.add(ty.Underlying(), class, attrs, sk, pos, p, r, sum)

default:
panic(fmt.Sprintf("%s: unexpected/unimplemented", ty))
}
// we added a slot at dst[n] for ty, set it
mod.locs[n].lsz = mod.indexing.FromInt(*sum - lastSum)
return n
}

// add adds a root recursively according to ty.
//
// add is responsible for setting the size, parent, class, attrs, and root
Expand Down
88 changes: 88 additions & 0 deletions objects/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright 2021 The pal authors (see AUTHORS)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package objects

import (
"go/token"
"go/types"

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

type Builder struct {
pkgPath string
indexing indexing.T
mmod *memory.Model
ts *typeset.TypeSet
objects []Object

start memory.Loc

class memory.Class
attrs memory.Attrs
pos token.Pos
srcKind memory.SrcKind
typ typeset.Type
}

func NewBuilder(pkgPath string, ind indexing.T, mem *memory.Model, ts *typeset.TypeSet) *Builder {
b := &Builder{}
b.pkgPath = pkgPath
b.indexing = ind
b.mmod = mem
b.ts = ts
return b
}

func (b *Builder) Reset() *Builder {
b.class = memory.Class(0)
b.attrs = memory.Attrs(0)
b.pos = token.NoPos
b.srcKind = memory.SrcKind(0)
b.typ = typeset.NoType
return b
}

func (b *Builder) Class(c memory.Class) *Builder {
b.class = c
return b
}

func (b *Builder) Attrs(a memory.Attrs) *Builder {
b.attrs = a
return b
}

func (b *Builder) Pos(p token.Pos) *Builder {
b.pos = p
return b
}

func (b *Builder) Kind(k memory.SrcKind) *Builder {
b.srcKind = k
return b
}

func (b *Builder) Type(t typeset.Type) *Builder {
b.typ = t
return b
}

func (b *Builder) GoType(t types.Type) *Builder {
b.typ = b.ts.FromGoType(t)
return b
}
17 changes: 17 additions & 0 deletions objects/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2021 The pal authors (see AUTHORS)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package objects coordinates lower level memory and typeset
// models with Go objects.
package objects
3 changes: 2 additions & 1 deletion objects/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ type object struct {
typ typeset.Type
}

func (o *object) Loc() memory.Loc { return o.loc }
func (o *object) Loc() memory.Loc { return o.loc }
func (o *object) Type() typeset.Type { return o.typ }
19 changes: 19 additions & 0 deletions objects/pointer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2021 The pal authors (see AUTHORS)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package objects

type Pointer struct {
object
}
4 changes: 2 additions & 2 deletions typeset/gotypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"go/types"
)

func (t *T) FromGoType(gotype types.Type) Type {
func (t *TypeSet) FromGoType(gotype types.Type) Type {
// that line is a headache...
switch ty := gotype.(type) {
case *types.Basic:
Expand Down Expand Up @@ -142,6 +142,6 @@ func (t *T) FromGoType(gotype types.Type) Type {
}
}

func (t *T) ToGoType(ty Type) types.Type {
func (t *TypeSet) ToGoType(ty Type) types.Type {
return nil
}
4 changes: 2 additions & 2 deletions typeset/plain.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"io"
)

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

func (t *T) PlainDecode(r io.Reader) error {
func (t *TypeSet) PlainDecode(r io.Reader) error {
tt := New()
var N int
var H int
Expand Down
Loading

0 comments on commit 3341c6d

Please sign in to comment.