Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
ltzmaxwell committed Jan 16, 2025
1 parent 3b81300 commit b935904
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 30 deletions.
23 changes: 16 additions & 7 deletions gnovm/pkg/gnolang/alloc.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ func (alloc *Allocator) Fork() *Allocator {
}

func (alloc *Allocator) MemStats() string {
return fmt.Sprintf("Allocator{maxBytes:%d,bytes,bytes:%d}", alloc.maxBytes, alloc.bytes)
return fmt.Sprintf("Allocator{maxBytes:%d, bytes:%d}", alloc.maxBytes, alloc.bytes)
}

func (alloc *Allocator) GC() {
println("---gc")
debug2.Println2("---gc")
// a throwaway allocator
throwaway := NewAllocator(3000, nil)
debug2.Println2("m: ", alloc.m)
Expand Down Expand Up @@ -151,7 +151,7 @@ func (alloc *Allocator) GC() {
debug2.Println2("---throwaway.bytes: ", throwaway.bytes)
debug2.Println2("---before reset, alloc.bytes: ", alloc.bytes)
alloc.bytes = throwaway.bytes
println("---after reset, alloc.bytes: ", alloc.bytes)
debug2.Println2("---after reset, alloc.bytes: ", alloc.bytes)
}

func (throwaway *Allocator) allocate2(v Value) {
Expand All @@ -165,7 +165,16 @@ func (throwaway *Allocator) allocate2(v Value) {
throwaway.allocate2(field.V)
}
case *FuncValue:
throwaway.AllocateFunc()
// TODO: is this right?
// if closure if fileNode, no allocate,
// cuz it's already done in compile stage.
debug2.Println2("funcValue, vv: ", vv)
debug2.Println2("clo: ", vv.Closure)
debug2.Println2("clo...Source: ", vv.Closure.(*Block).Source, reflect.TypeOf(vv.Closure.(*Block).Source))
if _, ok := vv.Closure.(*Block).Source.(*FileNode); !ok {
debug2.Println2("not a FileNode, alloc func")
throwaway.AllocateFunc()
}
case PointerValue:
throwaway.AllocatePointer()
throwaway.allocate2(vv.Base)
Expand Down Expand Up @@ -213,14 +222,14 @@ func (alloc *Allocator) Allocate(size int64) {

debug2.Println2("new allocated: ", size)
alloc.bytes += size
debug2.Println2("bytes after allocated: ", alloc.bytes)
debug2.Println2("===========bytes after allocated============: ", alloc.bytes)
if alloc.bytes > alloc.maxBytes {
panic("allocation limit exceeded")
}
}

func (alloc *Allocator) AllocateString(size int64) {
debug2.Println2("AllocateString")
debug2.Println2("AllocateString, size: ", size)
alloc.Allocate(allocString + allocStringByte*size)
}

Expand Down Expand Up @@ -317,7 +326,7 @@ func (alloc *Allocator) AllocateHeapItem() {
// constructor utilities.

func (alloc *Allocator) NewString(s string) StringValue {
debug2.Println2("NewString: ", s)
debug2.Printf2("NewString, s: \"%s\" \n", s)
alloc.AllocateString(int64(len(s)))
return StringValue(s)
}
Expand Down
7 changes: 5 additions & 2 deletions gnovm/pkg/gnolang/debug_false.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

package gnolang

const debug debugging = false
//const debug debugging = true

//const debug2 debugging = false

const debug2 debugging = false
const debug2 debugging = true
const debug debugging = false
22 changes: 17 additions & 5 deletions gnovm/pkg/gnolang/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ var machinePool = sync.Pool{
// Machines initialized through this constructor must be finalized with
// [Machine.Release].
func NewMachineWithOptions(opts MachineOptions) *Machine {
debug2.Println2("NewMachineWithOptions start")
preprocessorMode := opts.PreprocessorMode
readOnly := opts.ReadOnly
maxCycles := opts.MaxCycles
Expand Down Expand Up @@ -184,6 +185,7 @@ func NewMachineWithOptions(opts MachineOptions) *Machine {
if pv != nil {
mm.SetActivePackage(pv)
}
debug2.Println2("NewMachineWithOptions end, Memstats: ", mm.Alloc.MemStats())
return mm
}

Expand Down Expand Up @@ -233,6 +235,8 @@ func (m *Machine) SetActivePackage(pv *PackageValue) {
// NOTE: package paths not beginning with gno.land will be allowed to override,
// to support cases of stdlibs processed through [RunMemPackagesWithOverrides].
func (m *Machine) PreprocessAllFilesAndSaveBlockNodes() {
debug2.Println2("=======PreprocessAllFilesAndSaveBlockNodes")
defer debug2.Println2("=========Done PreprocessAllFilesAndSaveBlockNodes")
ch := m.Store.IterMemPackage()
for memPkg := range ch {
fset := ParseMemPackage(memPkg)
Expand Down Expand Up @@ -462,8 +466,9 @@ func (m *Machine) RunFiles(fns ...*FileNode) {
// This will also run each init function encountered.
// Returns the updated typed values of package.
func (m *Machine) runFileDecls(fns ...*FileNode) []TypedValue {
debug2.Println2("===runFileDecls", len(fns))
defer debug2.Println2("===Done runFileDecls")
debug2.Println2("===runFileDecls, fns: ", fns)
debug2.Println2("Memstats: ", m.Alloc.MemStats())
defer debug2.Println2("=======Done runFileDecls========")
// Files' package names must match the machine's active one.
// if there is one.
for _, fn := range fns {
Expand Down Expand Up @@ -515,7 +520,8 @@ func (m *Machine) runFileDecls(fns ...*FileNode) []TypedValue {
// Each file for each *PackageValue gets its own file *Block,
// with values copied over from each file's
// *FileNode.StaticBlock.
debug2.Println2("---machine, runFileDecls")
debug2.Println2("============machine, runFileDecls, count from here===============")
debug2.Println2("Memstats: ", m.Alloc.MemStats())
fb := m.Alloc.NewBlock(fn, pb)
fb.Values = make([]TypedValue, len(fn.StaticBlock.Values))
copy(fb.Values, fn.StaticBlock.Values)
Expand All @@ -530,6 +536,7 @@ func (m *Machine) runFileDecls(fns ...*FileNode) []TypedValue {
// recursive function for var declarations.
var runDeclarationFor func(fn *FileNode, decl Decl)
runDeclarationFor = func(fn *FileNode, decl Decl) {
debug2.Println2("runDeclarationFor, decl: ", decl)
// get fileblock of fn.
// fb := pv.GetFileBlock(nil, fn.Name)
// get dependencies of decl.
Expand Down Expand Up @@ -669,6 +676,7 @@ func (m *Machine) resavePackageValues(rlm *Realm) {
}

func (m *Machine) RunFunc(fn Name) {
debug2.Println2("Running func", fn)
defer func() {
if r := recover(); r != nil {
switch r := r.(type) {
Expand All @@ -686,6 +694,7 @@ func (m *Machine) RunFunc(fn Name) {
}

func (m *Machine) RunMain() {
debug2.Println2("Running main")
defer func() {
r := recover()

Expand Down Expand Up @@ -814,8 +823,9 @@ func (m *Machine) EvalStaticTypeOf(last BlockNode, x Expr) Type {
func (m *Machine) RunStatement(s Stmt) {
sn := m.LastBlock().GetSource(m.Store)
s = Preprocess(m.Store, sn, s).(Stmt)
debug2.Println2("Machine.RunStatement, s: ", s)
debug2.Println2("current machine: ", m)
debug2.Println2("==========Machine.RunStatement, s: ", s)
debug2.Println2("memstats: ", m.Alloc.MemStats())
//debug2.Println2("current machine: ", m)
m.PushOp(OpHalt)
m.PushStmt(s)
m.PushOp(OpExec)
Expand Down Expand Up @@ -854,6 +864,7 @@ func (m *Machine) RunDeclaration(d Decl) {
// package level, for which evaluations happen during
// preprocessing).
func (m *Machine) runDeclaration(d Decl) {
debug2.Println2("runDeclaration: ", d)
switch d := d.(type) {
case *FuncDecl:
// nothing to do.
Expand Down Expand Up @@ -1659,6 +1670,7 @@ func (m *Machine) PushValue(tv TypedValue) {
if debug {
m.Printf("+v %v\n", tv)
}
debug2.Printf2("+v %v\n", tv)
if len(m.Values) == m.NumValues {
// TODO tune. also see PushOp().
newValues := make([]TypedValue, len(m.Values)*2)
Expand Down
2 changes: 2 additions & 0 deletions gnovm/pkg/gnolang/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,7 @@ func (x *PackageNode) NewPackage() *PackageValue {
// NOTE: declared methods do not get their closures set here. See
// *DeclaredType.GetValueAt() which returns a filled copy.
func (x *PackageNode) PrepareNewValues(pv *PackageValue) []TypedValue {
debug2.Println2("PrepareNewValues")
if pv.PkgPath == "" {
// nothing to prepare for throwaway packages.
// TODO: double check to see if still relevant.
Expand Down Expand Up @@ -1444,6 +1445,7 @@ func (x *PackageNode) PrepareNewValues(pv *PackageValue) []TypedValue {
for i, tv := range nvs {
if fv, ok := tv.V.(*FuncValue); ok {
// copy function value and assign closure from package value.
debug2.Println2("Copy fv: ", fv)
fv = fv.Copy(nilAllocator)
fv.Closure = pv.fBlocksMap[fv.FileName]
if fv.Closure == nil {
Expand Down
3 changes: 2 additions & 1 deletion gnovm/pkg/gnolang/op_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,9 @@ func (m *Machine) doOpCall() {
}
} else {
list := m.PopCopyValues(nvar)
debug2.Println2("list: ", list)
vart := pts[numParams-1].Type.(*SliceType)
debug2.Println2("varg")
debug2.Println2("has varg")
varg := m.Alloc.NewSliceFromList(list)
m.PushValue(TypedValue{
T: vart,
Expand Down
1 change: 1 addition & 0 deletions gnovm/pkg/gnolang/op_expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ func (m *Machine) doOpRef() {
if elt == DataByteType {
elt = xv.TV.V.(DataByteValue).ElemType
}
debug2.Println2("doOpRef, new type")
m.PushValue(TypedValue{
T: m.Alloc.NewType(&PointerType{Elt: elt}),
V: xv,
Expand Down
1 change: 1 addition & 0 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
// Anything predefined or preprocessed here get skipped during the Preprocess
// phase.
func PredefineFileSet(store Store, pn *PackageNode, fset *FileSet) {
debug2.Println2("PredefineFileSet")
// First, initialize all file nodes and connect to package node.
// This will also reserve names on BlockNode.StaticBlock by
// calling StaticBlock.Predefine().
Expand Down
3 changes: 3 additions & 0 deletions gnovm/pkg/gnolang/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ func (ds *defaultStore) SetPackageGetter(pg PackageGetter) {

// Gets package from cache, or loads it from baseStore, or gets it from package getter.
func (ds *defaultStore) GetPackage(pkgPath string, isImport bool) *PackageValue {
debug2.Println2("GetPackage, pkgPath: ", pkgPath)
debug2.Println("ds.Alloc: ", ds.alloc)
// helper to detect circular imports
if isImport {
if slices.Contains(ds.current, pkgPath) {
Expand Down Expand Up @@ -430,6 +432,7 @@ func (ds *defaultStore) GetObjectSafe(oid ObjectID) Object {
// loads and caches an object.
// CONTRACT: object isn't already in the cache.
func (ds *defaultStore) loadObjectSafe(oid ObjectID) Object {
debug2.Println2("loadObjectSafe", oid)
if bm.OpsEnabled {
bm.PauseOpCode()
defer bm.ResumeOpCode()
Expand Down
7 changes: 7 additions & 0 deletions gnovm/pkg/gnolang/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,13 @@ func (fv *FuncValue) IsNative() bool {
func (fv *FuncValue) Copy(alloc *Allocator) *FuncValue {
debug2.Println2("Copy, fv: ", fv)
debug2.Println2("allocator: ", alloc)
debug2.Println2("fv.clo: ", fv.Closure)
if b, ok := fv.Closure.(*Block); ok {
debug2.Println2("containing block of fv is: ", b)
if b != nil {
debug2.Println2("fv...source, type of source", b.Source, reflect.TypeOf(b.Source))
}
}
alloc.AllocateFunc()
return &FuncValue{
Type: fv.Type,
Expand Down
3 changes: 2 additions & 1 deletion gnovm/pkg/test/filetest.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ type runResult struct {
}

func (opts *TestOptions) runTest(m *gno.Machine, pkgPath, filename string, content []byte) (rr runResult) {
fmt.Println("runTest")
pkgName := gno.Name(pkgPath[strings.LastIndexByte(pkgPath, '/')+1:])

// Eagerly load imports.
Expand Down Expand Up @@ -236,7 +237,7 @@ func (opts *TestOptions) runTest(m *gno.Machine, pkgPath, filename string, conte
m.SetActivePackage(pv)
n := gno.MustParseFile(filename, string(content))
m.RunFiles(n)
//fmt.Println("---after RunFiles, m: ", m)
fmt.Println("======after RunFiles, m.Alloc.Memstats: ", m.Alloc.MemStats())

m.RunStatement(gno.S(gno.Call(gno.X("main"))))
} else {
Expand Down
1 change: 1 addition & 0 deletions gnovm/pkg/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ func (opts *TestOptions) runTestFiles(
files *gno.FileSet,
cw storetypes.Store, gs gno.TransactionStore,
) (errs error) {
fmt.Println("runTestFiles")
var m *gno.Machine
defer func() {
if r := recover(); r != nil {
Expand Down
34 changes: 34 additions & 0 deletions gnovm/stdlibs/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions gnovm/stdlibs/runtime/runtime.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package runtime

func GC()
func MemStats() string
11 changes: 11 additions & 0 deletions gnovm/stdlibs/runtime/runtime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package runtime

import gno "github.com/gnolang/gno/gnovm/pkg/gnolang"

func GC(m *gno.Machine) {
m.Alloc.GC()
}

func MemStats(m *gno.Machine) string {
return m.Alloc.MemStats()
}
Loading

0 comments on commit b935904

Please sign in to comment.