summaryrefslogtreecommitdiff
path: root/usr/austin/eval/func.go
blob: cc790452b7af06ef7c512e9897fe556d36ee3413 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2009 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package eval

import (
	"container/vector";
	"eval";
)

/*
 * Virtual machine
 */

type vm struct {
	pc uint;
	// The current execution frame.  If execution is within a
	// block, this may be a child of the original function
	// activation frame.
	f *Frame;
	// The original function activation frame.  This is used to
	// access function out args.
	activation *Frame;
}

type code []func(*vm)

func (i code) exec(fr *Frame) {
	v := vm{0, fr, fr};

	l := uint(len(i));
	for v.pc < l {
		pc := v.pc;
		v.pc++;
		i[pc](&v);
	}
}

/*
 * Code buffer
 */

type codeBuf struct {
	instrs code;
}

func newCodeBuf() *codeBuf {
	return &codeBuf{make(code, 0, 16)};
}

func (b *codeBuf) push(instr func(*vm)) {
	n := len(b.instrs);
	if n >= cap(b.instrs) {
		a := make(code, n, n*2);
		for i := range b.instrs {
			a[i] = b.instrs[i];
		}
		b.instrs = a;
	}
	b.instrs = b.instrs[0:n+1];
	b.instrs[n] = instr;
}

func (b *codeBuf) nextPC() uint {
	return uint(len(b.instrs));
}

func (b *codeBuf) get() code {
	// Freeze this buffer into an array of exactly the right size
	a := make(code, len(b.instrs));
	for i := range b.instrs {
		a[i] = b.instrs[i];
	}
	return code(a);
}

/*
 * User-defined functions
 */

type evalFunc struct {
	sc *Scope;
	fr *Frame;
	code code;
}

func (f *evalFunc) NewFrame() *Frame {
	return f.sc.NewFrame(f.fr);
}

func (f *evalFunc) Call(fr *Frame) {
	f.code.exec(fr);
}