File: opcodes.go

package info (click to toggle)
goawk 1.29.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,560 kB
  • sloc: awk: 3,060; yacc: 198; fortran: 189; python: 131; sh: 58; makefile: 12
file content (184 lines) | stat: -rw-r--r-- 3,982 bytes parent folder | download
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package compiler

//go:generate go run golang.org/x/tools/cmd/stringer@v0.10.0 -type=Opcode,AugOp,BuiltinOp

// Opcode represents a single virtual machine instruction (or argument). The
// comments beside each opcode show any arguments that instruction consumes.
//
// Normally this is called "bytecode", but I've avoided that term here as each
// opcode is a 32-bit word, not an 8-bit byte.
//
// I tested various bit widths, and I believe 32 bit was the fastest, but also
// means we don't have to worry about jump offsets overflowing. That's tested
// in the compiler, but who's going to have an AWK program bigger than 2GB?
type Opcode int32

const (
	Nop Opcode = iota

	// Stack operations
	Num // numIndex
	Str // strIndex
	Dupe
	Drop
	Swap
	Rote

	// Fetch a field, variable, or array item
	Field
	FieldInt // index
	FieldByName
	FieldByNameStr // strIndex
	Global         // index
	Local          // index
	Special        // index
	ArrayGlobal    // arrayIndex
	ArrayLocal     // arrayIndex
	InGlobal       // arrayIndex
	InLocal        // arrayIndex

	// Assign a field, variable, or array item
	AssignField
	AssignGlobal      // index
	AssignLocal       // index
	AssignSpecial     // index
	AssignArrayGlobal // arrayIndex
	AssignArrayLocal  // arrayIndex

	// Delete statement
	Delete    // arrayScope arrayIndex
	DeleteAll // arrayScope arrayIndex

	// Post-increment and post-decrement
	IncrField       // amount
	IncrGlobal      // amount index
	IncrLocal       // amount index
	IncrSpecial     // amount index
	IncrArrayGlobal // amount arrayIndex
	IncrArrayLocal  // amount arrayIndex

	// Augmented assignment (also used for pre-increment and pre-decrement)
	AugAssignField       // augOp
	AugAssignGlobal      // augOp index
	AugAssignLocal       // augOp index
	AugAssignSpecial     // augOp index
	AugAssignArrayGlobal // augOp arrayIndex
	AugAssignArrayLocal  // augOp arrayIndex

	// Stand-alone regex expression /foo/
	Regex // regexIndex

	// Multi-index concatenation
	IndexMulti // num

	// Multi-value concatenation
	ConcatMulti // num

	// Binary operators
	Add
	Subtract
	Multiply
	Divide
	Power
	Modulo
	Equals
	NotEquals
	Less
	Greater
	LessOrEqual
	GreaterOrEqual
	Concat
	Match
	NotMatch

	// Unary operators
	Not
	UnaryMinus
	UnaryPlus
	Boolean

	// Control flow
	Jump               // offset
	JumpFalse          // offset
	JumpTrue           // offset
	JumpEquals         // offset
	JumpNotEquals      // offset
	JumpLess           // offset
	JumpGreater        // offset
	JumpLessOrEqual    // offset
	JumpGreaterOrEqual // offset
	Next
	Nextfile
	Exit
	ExitStatus
	ForIn // varScope varIndex arrayScope arrayIndex offset
	BreakForIn

	// Builtin functions
	CallBuiltin     // builtinOp
	CallLengthArray // arrayScope arrayIndex
	CallSplit       // arrayScope arrayIndex
	CallSplitSep    // arrayScope arrayIndex
	CallSprintf     // numArgs

	// User and native functions
	CallUser   // funcIndex numArrayArgs [arrayScope1 arrayIndex1 ...]
	CallNative // funcIndex numArgs
	Return
	ReturnNull
	Nulls // numNulls

	// Print, printf, and getline
	Print          // numArgs redirect
	Printf         // numArgs redirect
	Getline        // redirect
	GetlineField   // redirect
	GetlineGlobal  // redirect index
	GetlineLocal   // redirect index
	GetlineSpecial // redirect index
	GetlineArray   // redirect arrayScope arrayIndex

	EndOpcode
)

// AugOp represents an augmented assignment operation.
type AugOp Opcode

const (
	AugOpAdd AugOp = iota
	AugOpSub
	AugOpMul
	AugOpDiv
	AugOpPow
	AugOpMod
)

// BuiltinOp represents a builtin function call.
type BuiltinOp Opcode

const (
	BuiltinAtan2 BuiltinOp = iota
	BuiltinClose
	BuiltinCos
	BuiltinExp
	BuiltinFflush
	BuiltinFflushAll
	BuiltinGsub
	BuiltinIndex
	BuiltinInt
	BuiltinLength
	BuiltinLengthArg
	BuiltinLog
	BuiltinMatch
	BuiltinRand
	BuiltinSin
	BuiltinSqrt
	BuiltinSrand
	BuiltinSrandSeed
	BuiltinSub
	BuiltinSubstr
	BuiltinSubstrLength
	BuiltinSystem
	BuiltinTolower
	BuiltinToupper
)