View Javadoc
1   package io.jawk.backend;
2   
3   /*-
4    * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
5    * Jawk
6    * ჻჻჻჻჻჻
7    * Copyright (C) 2006 - 2026 MetricsHub
8    * ჻჻჻჻჻჻
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   *
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   *
19   * You should have received a copy of the GNU General Lesser Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
23   */
24  
25  import java.io.Closeable;
26  import java.io.IOException;
27  import java.io.PrintStream;
28  import java.util.AbstractSet;
29  import java.util.Iterator;
30  import java.util.Arrays;
31  import java.util.HashSet;
32  import java.util.LinkedHashMap;
33  import java.util.LinkedHashSet;
34  import java.util.Objects;
35  import java.util.ArrayDeque;
36  import java.util.ArrayList;
37  import java.util.Collections;
38  import java.util.Deque;
39  import java.util.Enumeration;
40  import java.util.HashMap;
41  import java.util.IdentityHashMap;
42  import java.util.List;
43  import java.util.Locale;
44  import java.util.Map;
45  import java.util.Set;
46  import java.util.function.BiConsumer;
47  import java.util.regex.Pattern;
48  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
49  import io.jawk.AwkExpression;
50  import io.jawk.AwkProgram;
51  import io.jawk.AwkSandboxException;
52  import io.jawk.ExitException;
53  import io.jawk.ext.AbstractExtension;
54  import io.jawk.ext.ExtensionFunction;
55  import io.jawk.ext.ForInKeyOrder;
56  import io.jawk.ext.JawkExtension;
57  import io.jawk.intermediate.Address;
58  import io.jawk.intermediate.BuiltinFunction;
59  import io.jawk.intermediate.Opcode;
60  import io.jawk.intermediate.PositionTracker;
61  import io.jawk.intermediate.Tuple;
62  import io.jawk.intermediate.Tuple.BooleanTuple;
63  import io.jawk.intermediate.Tuple.CallFunctionTuple;
64  import io.jawk.intermediate.Tuple.ClassTuple;
65  import io.jawk.intermediate.Tuple.CountAndAppendTuple;
66  import io.jawk.intermediate.Tuple.CountTuple;
67  import io.jawk.intermediate.Tuple.DereferenceTuple;
68  import io.jawk.intermediate.Tuple.ExtensionTuple;
69  import io.jawk.intermediate.Tuple.IndirectCallTuple;
70  import io.jawk.intermediate.Tuple.IndirectFunctionTarget;
71  import io.jawk.intermediate.Tuple.InputFieldTuple;
72  import io.jawk.intermediate.Tuple.LongTuple;
73  import io.jawk.intermediate.Tuple.PushDoubleTuple;
74  import io.jawk.intermediate.Tuple.PushLongTuple;
75  import io.jawk.intermediate.Tuple.PushStringTuple;
76  import io.jawk.intermediate.Tuple.RegexTuple;
77  import io.jawk.intermediate.Tuple.ScalarPopTuple;
78  import io.jawk.intermediate.Tuple.SubstitutionVariableTuple;
79  import io.jawk.intermediate.Tuple.VariableTuple;
80  import io.jawk.intermediate.UninitializedObject;
81  import io.jawk.intermediate.UntypedObject;
82  import io.jawk.jrt.AssocArray;
83  import io.jawk.jrt.AwkRuntimeException;
84  import io.jawk.jrt.AwkSink;
85  import io.jawk.jrt.ConditionPair;
86  import io.jawk.jrt.InputSource;
87  import io.jawk.jrt.JRT;
88  import io.jawk.jrt.VariableManager;
89  import io.jawk.util.AwkSettings;
90  import io.jawk.jrt.BSDRandom;
91  
92  /**
93   * The Jawk interpreter.
94   * <p>
95   * It takes tuples constructed by the intermediate step
96   * and executes each tuple in accordance to their instruction semantics.
97   * The tuples correspond to the Awk script compiled by the parser.
98   * The interpreter consists of an instruction processor (interpreter),
99   * a runtime stack, and machinery to support the instruction set
100  * contained within the tuples.
101  * <p>
102  * The interpreter runs completely independent of the frontend/intermediate step.
103  * In fact, an intermediate file produced by Jawk is sufficient to
104  * execute on this interpreter. The binding data-structure is
105  * the {@link AwkSettings}, which can contain options pertinent to
106  * the interpreter. For example, the interpreter must know about
107  * the -v command line argument values, as well as the file/variable list
108  * parameter values (ARGC/ARGV) after the script on the command line.
109  * However, if programmatic access to the AVM is required, meaningful
110  * {@link AwkSettings} are not required.
111  * <p>
112  * Semantic analysis has occurred prior to execution of the interpreter.
113  * Therefore, the interpreter throws AwkRuntimeExceptions upon most
114  * errors/conditions. It can also throw a <code>java.lang.Error</code> if an
115  * interpreter error is encountered.
116  * <p>
117  * AVM instances are reusable, but they are not thread-safe. Reuse the same
118  * interpreter only sequentially, or create one AVM per concurrent execution.
119  * When AVM is used directly, callers own its lifecycle and must
120  * {@link #close()} it when done.
121  *
122  * @author Danny Daglas
123  */
124 public class AVM implements VariableManager, Closeable {
125 
126 	private RuntimeStack runtimeStack = new RuntimeStack();
127 
128 	// operand stack: a flat Object[] with an explicit top index so push/pop
129 	// stay single array accesses; depth is bounded by expression nesting, so
130 	// the array stays small and rarely grows
131 	private Object[] operandStack = new Object[INITIAL_OPERAND_STACK_CAPACITY];
132 	private int operandStackSize;
133 	// Untyped array elements currently passed as user-function arguments, in
134 	// call order. Each reference lives exactly as long as the call frame that
135 	// received it, so this stack stays empty unless an untyped element
136 	// argument is in flight.
137 	private final Deque<IndirectArrayArgumentReference> elementArgumentReferences = new ArrayDeque<IndirectArrayArgumentReference>();
138 	private List<String> arguments;
139 	private boolean sortedArrayKeys;
140 	private final Map<String, Object> baseInitialVariables;
141 	private final Map<String, Object> baseSpecialVariables;
142 	private Map<String, Object> executionInitialVariables;
143 	private Map<String, Object> executionSpecialVariables;
144 	private JRT jrt;
145 	private Map<String, JawkExtension> extensionInstances;
146 
147 	private static final int INITIAL_OPERAND_STACK_CAPACITY = 64;
148 
149 	// stack methods
150 	private Object pop() {
151 		Object value = operandStack[--operandStackSize];
152 		operandStack[operandStackSize] = null;
153 		return value;
154 	}
155 
156 	private void push(Object o) {
157 		if (operandStackSize == operandStack.length) {
158 			operandStack = Arrays.copyOf(operandStack, operandStack.length * 2);
159 		}
160 		operandStack[operandStackSize++] = o;
161 	}
162 
163 	private void clearOperandStack() {
164 		Arrays.fill(operandStack, 0, operandStackSize, null);
165 		operandStackSize = 0;
166 	}
167 
168 	private final AwkSettings settings;
169 	private final boolean profiling;
170 	private final Map<Opcode, ProfilingReport.Accumulator> tupleProfilingStats;
171 	private final Map<String, ProfilingReport.Accumulator> functionProfilingStats;
172 	private final Deque<ActiveFunction> activeProfilingFunctions;
173 	private InputSource resolvedInputSource;
174 	private AwkExpression installedEvalExpression;
175 	private boolean mergedGlobalLayoutActive;
176 
177 	/** Optional extension-provided ordering for {@code for (index in array)} traversal. */
178 	private ForInKeyOrder forInKeyOrder;
179 
180 	/** Description of the primary script source, for runtime diagnostics. */
181 	private String sourceDescription;
182 
183 	/** Script line of the extension call currently being dispatched. */
184 	private int currentLineNumber;
185 
186 	/** Whether the extension beforeStart hooks already ran for this AVM. */
187 	private boolean beforeStartHooksExecuted;
188 
189 	/** Offset of a materialized SYMTAB array, for live operand-assignment updates. */
190 	private long symtabOffset = NULL_OFFSET;
191 
192 	/**
193 	 * Construct the interpreter.
194 	 * <p>
195 	 * Provided to allow programmatic construction of the interpreter
196 	 * outside of the framework which is used by Jawk.
197 	 */
198 	public AVM() {
199 		this(null, Collections.<String, JawkExtension>emptyMap());
200 	}
201 
202 	/**
203 	 * Construct the interpreter, accepting parameters which may have been
204 	 * set on the command-line arguments to the JVM.
205 	 *
206 	 * @param parameters The parameters affecting the behavior of the
207 	 *        interpreter.
208 	 * @param extensionInstances Map of the extensions to load
209 	 */
210 	public AVM(final AwkSettings parameters, final Map<String, JawkExtension> extensionInstances) {
211 		this(parameters, extensionInstances, false);
212 	}
213 
214 	/**
215 	 * Construct the interpreter, optionally enabling runtime profiling.
216 	 *
217 	 * @param parameters The parameters affecting the behavior of the
218 	 *        interpreter.
219 	 * @param extensionInstances Map of the extensions to load
220 	 * @param profilingEnabled Whether to collect profiling statistics
221 	 */
222 	public AVM(final AwkSettings parameters, final Map<String, JawkExtension> extensionInstances,
223 			final boolean profilingEnabled) {
224 		this.settings = parameters != null ? parameters : AwkSettings.DEFAULT_SETTINGS;
225 		this.extensionInstances = extensionInstances == null ?
226 				Collections.<String, JawkExtension>emptyMap() :
227 				extensionInstances;
228 		this.profiling = profilingEnabled;
229 		if (profilingEnabled) {
230 			this.tupleProfilingStats = new java.util.EnumMap<Opcode, ProfilingReport.Accumulator>(Opcode.class);
231 			this.functionProfilingStats = new LinkedHashMap<String, ProfilingReport.Accumulator>();
232 			this.activeProfilingFunctions = new ArrayDeque<ActiveFunction>();
233 		} else {
234 			this.tupleProfilingStats = null;
235 			this.functionProfilingStats = null;
236 			this.activeProfilingFunctions = null;
237 		}
238 
239 		arguments = Collections.emptyList();
240 		sortedArrayKeys = this.settings.isUseSortedArrayKeys();
241 		baseInitialVariables = new HashMap<String, Object>(this.settings.getVariables());
242 		baseSpecialVariables = JRT.copySpecialVariables(baseInitialVariables);
243 		executionInitialVariables = baseInitialVariables;
244 		executionSpecialVariables = baseSpecialVariables;
245 
246 		jrt = createJrt();
247 		initExtensions();
248 	}
249 
250 	/**
251 	 * Creates the runtime this interpreter drives.
252 	 * <p>
253 	 * Called once from the constructor, so that a subclass can install its own
254 	 * runtime — {@link SandboxedAVM} returns a
255 	 * {@link io.jawk.jrt.SandboxedJRT} this way. It runs before the subclass
256 	 * constructor body, so an override must not depend on the subclass's own
257 	 * fields.
258 	 * </p>
259 	 *
260 	 * @return the runtime bound to this interpreter, never {@code null}
261 	 */
262 	protected JRT createJrt() {
263 		return new JRT(this, this.settings.getLocale(), AwkSink.NOP_SINK, null);
264 	}
265 
266 	/**
267 	 * Returns the runtime settings associated with this interpreter.
268 	 *
269 	 * @return the settings, never {@code null}
270 	 */
271 	protected AwkSettings getSettings() {
272 		return settings;
273 	}
274 
275 	/**
276 	 * Returns the JRT (Jawk Runtime) instance associated with this interpreter.
277 	 *
278 	 * @return the JRT instance, never {@code null}
279 	 */
280 	@SuppressFBWarnings("EI_EXPOSE_REP")
281 	public JRT getJrt() {
282 		return jrt;
283 	}
284 
285 	/**
286 	 * Sets the sink used by default {@code print} and {@code printf}
287 	 * operations on this runtime.
288 	 *
289 	 * @param sink sink to use
290 	 */
291 	public void setAwkSink(AwkSink sink) {
292 		jrt.setAwkSink(Objects.requireNonNull(sink, "sink"));
293 	}
294 
295 	/**
296 	 * Sets the stream used for the stderr output of spawned processes
297 	 * (e.g.&nbsp;{@code system("...")}).
298 	 *
299 	 * @param errorStream stream to receive process stderr
300 	 */
301 	public void setErrorStream(PrintStream errorStream) {
302 		jrt.setErrorStream(errorStream);
303 	}
304 
305 	/**
306 	 * Sets the stream that receives runtime warning messages (gawk-style
307 	 * diagnostics). Warnings default to {@link System#err}.
308 	 *
309 	 * @param warningStream stream to receive runtime warnings
310 	 */
311 	public void setWarningStream(PrintStream warningStream) {
312 		jrt.setWarningStream(warningStream);
313 	}
314 
315 	/**
316 	 * Registers the hook that decides the key traversal order of
317 	 * {@code for (index in array)} statements.
318 	 * <p>
319 	 * When no hook is registered, iteration uses the array's natural key order.
320 	 * Extensions typically register a hook from their {@code beforeStart}
321 	 * method; the last registration wins.
322 	 * </p>
323 	 *
324 	 * @param keyOrder traversal-order provider, or {@code null} to restore the
325 	 *        natural key order
326 	 */
327 	public void setForInKeyOrder(ForInKeyOrder keyOrder) {
328 		forInKeyOrder = keyOrder;
329 	}
330 
331 	/**
332 	 * Returns the default sink used by this runtime.
333 	 *
334 	 * @return the current AWK sink
335 	 */
336 	public AwkSink getAwkSink() {
337 		return jrt.getAwkSink();
338 	}
339 
340 	/**
341 	 * Returns the locale configured for this runtime.
342 	 *
343 	 * @return runtime locale
344 	 */
345 	protected Locale getLocale() {
346 		return jrt.getLocale();
347 	}
348 
349 	/**
350 	 * Evaluates a compiled expression against the AVM state exactly as it
351 	 * currently stands.
352 	 *
353 	 * @param expression compiled expression to evaluate
354 	 * @return the resulting value
355 	 * @throws IOException if evaluation fails
356 	 */
357 	public Object eval(AwkExpression expression) throws IOException {
358 		AwkExpression compiledExpression = Objects.requireNonNull(expression, "expression");
359 		installExpressionMetadata(compiledExpression);
360 		jrt.setPosix(compiledExpression.isPosix());
361 
362 		try {
363 			executeTuples(compiledExpression.top());
364 		} catch (ExitException e) {
365 			// Expression tuples must never contain EXIT opcodes. If callers pass an
366 			// invalid compiled expression, fail fast without poisoning later evals.
367 			throwExitException = false;
368 			exitCode = 0;
369 			throw new IllegalStateException("eval(AwkExpression) cannot execute EXIT opcodes.", e);
370 		}
371 		return operandStackSize == 0 ? null : JRT.toJavaScalar(pop());
372 	}
373 
374 	/**
375 	 * Evaluates a compiled expression against the supplied input source.
376 	 *
377 	 * @param expression compiled expression to evaluate
378 	 * @param inputSource input source providing the current record
379 	 * @return the resulting value
380 	 * @throws IOException if evaluation fails
381 	 */
382 	public Object eval(AwkExpression expression, InputSource inputSource) throws IOException {
383 		return eval(expression, inputSource, null);
384 	}
385 
386 	/**
387 	 * Evaluates a compiled expression against the supplied input source with
388 	 * per-call variable overrides.
389 	 *
390 	 * @param expression compiled expression to evaluate
391 	 * @param inputSource input source providing the current record
392 	 * @param variableOverrides additional variable assignments applied on top of
393 	 *        the settings-level variables (may be {@code null})
394 	 * @return the resulting value
395 	 * @throws IOException if evaluation fails
396 	 */
397 	public Object eval(AwkExpression expression, InputSource inputSource, Map<String, Object> variableOverrides)
398 			throws IOException {
399 		prepareForEval(inputSource, Collections.<String>emptyList(), variableOverrides);
400 		return eval(expression);
401 	}
402 
403 	/**
404 	 * Executes a compiled AWK program with the current runtime defaults.
405 	 *
406 	 * @param program compiled program to execute
407 	 * @param inputSource input source providing records
408 	 * @throws ExitException when the program terminates via {@code exit}
409 	 * @throws IOException if execution fails
410 	 */
411 	public void execute(AwkProgram program, InputSource inputSource) throws ExitException, IOException {
412 		execute(program, inputSource, Collections.<String>emptyList(), null);
413 	}
414 
415 	/**
416 	 * Executes a compiled AWK program with explicit runtime arguments.
417 	 *
418 	 * @param program compiled program to execute
419 	 * @param inputSource input source providing records
420 	 * @param runtimeArguments name=value or filename entries from the command line
421 	 * @throws ExitException when the program terminates via {@code exit}
422 	 * @throws IOException if execution fails
423 	 */
424 	public void execute(AwkProgram program, InputSource inputSource, List<String> runtimeArguments)
425 			throws ExitException,
426 			IOException {
427 		execute(program, inputSource, runtimeArguments, null);
428 	}
429 
430 	/**
431 	 * Executes a compiled AWK program with explicit runtime arguments and
432 	 * variable overrides.
433 	 *
434 	 * @param program compiled program to execute
435 	 * @param inputSource input source providing records
436 	 * @param runtimeArguments name=value or filename entries from the command line
437 	 * @param variableOverrides additional variable assignments applied on top of
438 	 *        the settings-level variables (may be {@code null})
439 	 * @throws ExitException when the program terminates via {@code exit}
440 	 * @throws IOException if execution fails
441 	 */
442 	public void execute(
443 			AwkProgram program,
444 			InputSource inputSource,
445 			List<String> runtimeArguments,
446 			Map<String, Object> variableOverrides)
447 			throws ExitException,
448 			IOException {
449 		AwkProgram compiledProgram = Objects.requireNonNull(program, "program");
450 		InputSource resolvedSource = Objects.requireNonNull(inputSource, "inputSource");
451 		resetRuntimeState(runtimeArguments, variableOverrides);
452 		installProgramMetadata(compiledProgram);
453 
454 		jrt.setPosix(compiledProgram.isPosix());
455 		jrt.prepareForExecution(settings.getFieldSeparator(), settings.getDefaultRS());
456 		if (!executionSpecialVariables.isEmpty()) {
457 			jrt.applySpecialVariables(executionSpecialVariables);
458 		}
459 		rebindResolvedInputSource(resolvedSource);
460 		executeTuples(compiledProgram.top());
461 	}
462 
463 	/**
464 	 * Executes a compiled AWK program while persisting user-defined global
465 	 * variables across repeated executions on this AVM instance.
466 	 * <p>
467 	 * Before the new program starts, this method imports any user-defined
468 	 * globals currently materialized in the AVM and remaps them onto the
469 	 * incoming program's compiled global slots.
470 	 *
471 	 * @param program compiled program to execute
472 	 * @param inputSource input source providing records
473 	 * @throws ExitException when the program terminates via {@code exit}
474 	 * @throws IOException if execution fails
475 	 */
476 	public void executePersistingGlobals(AwkProgram program, InputSource inputSource) throws ExitException, IOException {
477 		executePersistingGlobals(program, inputSource, Collections.<String>emptyList(), null);
478 	}
479 
480 	/**
481 	 * Executes a compiled AWK program while persisting user-defined global
482 	 * variables across repeated executions on this AVM instance.
483 	 * <p>
484 	 * Before the new program starts, this method imports any user-defined
485 	 * globals currently materialized in the AVM and remaps them onto the
486 	 * incoming program's compiled global slots.
487 	 *
488 	 * @param program compiled program to execute
489 	 * @param inputSource input source providing records
490 	 * @param runtimeArguments name=value or filename entries from the command line
491 	 * @throws ExitException when the program terminates via {@code exit}
492 	 * @throws IOException if execution fails
493 	 */
494 	public void executePersistingGlobals(AwkProgram program, InputSource inputSource, List<String> runtimeArguments)
495 			throws ExitException,
496 			IOException {
497 		executePersistingGlobals(program, inputSource, runtimeArguments, null);
498 	}
499 
500 	/**
501 	 * Executes a compiled AWK program while persisting user-defined global
502 	 * variables across repeated executions on this AVM instance.
503 	 * <p>
504 	 * Before the new program starts, this method imports any user-defined
505 	 * globals currently materialized in the AVM and remaps them onto the
506 	 * incoming program's compiled global slots.
507 	 *
508 	 * @param program compiled program to execute
509 	 * @param inputSource input source providing records
510 	 * @param runtimeArguments name=value or filename entries from the command line
511 	 * @param variableOverrides additional variable assignments applied on top of
512 	 *        the settings-level variables (may be {@code null})
513 	 * @throws ExitException when the program terminates via {@code exit}
514 	 * @throws IOException if execution fails
515 	 */
516 	public void executePersistingGlobals(
517 			AwkProgram program,
518 			InputSource inputSource,
519 			List<String> runtimeArguments,
520 			Map<String, Object> variableOverrides)
521 			throws ExitException,
522 			IOException {
523 		AwkProgram compiledProgram = Objects.requireNonNull(program, "program");
524 		InputSource resolvedSource = Objects.requireNonNull(inputSource, "inputSource");
525 		mergeRuntimeState(runtimeArguments, variableOverrides, compiledProgram);
526 
527 		jrt.setPosix(compiledProgram.isPosix());
528 		jrt.prepareForExecution(settings.getFieldSeparator(), settings.getDefaultRS());
529 		if (!executionSpecialVariables.isEmpty()) {
530 			jrt.applySpecialVariables(executionSpecialVariables);
531 		}
532 		rebindResolvedInputSource(resolvedSource);
533 		executeTuples(compiledProgram.top());
534 	}
535 
536 	/**
537 	 * Clears the user-defined globals retained in the current runtime stack.
538 	 * <p>
539 	 * The next {@link #executePersistingGlobals(AwkProgram, InputSource, List, Map)}
540 	 * call will therefore start from an empty persistent global bank.
541 	 */
542 	public void clearPersistentGlobals() {
543 		runtimeStack.clearGlobals();
544 		mergedGlobalLayoutActive = false;
545 	}
546 
547 	/**
548 	 * Captures the user-defined globals currently retained by this AVM for
549 	 * persistent execution.
550 	 * <p>
551 	 * The returned snapshot is serializable and can later be fed back into
552 	 * {@link #restorePersistentMemory(Map)} on this or another AVM instance.
553 	 *
554 	 * @return serializable snapshot of the persistent user-global bank
555 	 */
556 	public Map<String, Object> snapshotPersistentMemory() {
557 		return new LinkedHashMap<>(collectPersistentGlobalValues());
558 	}
559 
560 	/**
561 	 * Restores the user-defined globals retained by this AVM from a previously
562 	 * captured persistent-memory snapshot.
563 	 * <p>
564 	 * Restoring a snapshot replaces the current retained global bank. The next
565 	 * {@link #executePersistingGlobals(AwkProgram, InputSource, List, Map)} call
566 	 * will merge these globals into the compiled layout of the incoming program.
567 	 *
568 	 * @param snapshot snapshot to restore
569 	 */
570 	public void restorePersistentMemory(Map<String, Object> snapshot) {
571 		Map<String, Object> restoredSnapshot = Objects.requireNonNull(snapshot, "snapshot");
572 		Map<String, Object> restoredGlobals = filterToPersistentEligible(restoredSnapshot);
573 		runtimeStack.clearGlobals();
574 		if (!restoredGlobals.isEmpty()) {
575 			runtimeStack.rebindGlobals(new ArrayList<>(restoredGlobals.keySet()));
576 			applyGlobalsToStack(restoredGlobals);
577 		}
578 		mergedGlobalLayoutActive = false;
579 	}
580 
581 	private void initExtensions() {
582 		if (extensionInstances.isEmpty()) {
583 			return;
584 		}
585 		Set<JawkExtension> initialized = new LinkedHashSet<JawkExtension>();
586 		for (JawkExtension extension : extensionInstances.values()) {
587 			if (initialized.add(extension)) {
588 				extension.init(this, jrt, settings); // this = VariableManager
589 			}
590 		}
591 	}
592 
593 	// Offsets for globals that remain runtime-managed by the tuple stream.
594 	// ARGC is always materialized; ENVIRON and ARGV are emitted on demand.
595 	private long environOffset = NULL_OFFSET;
596 	private long argcOffset = NULL_OFFSET;
597 	private long argvOffset = NULL_OFFSET;
598 
599 	private static final Integer ZERO = Integer.valueOf(0);
600 	private static final Integer ONE = Integer.valueOf(1);
601 
602 	/** Random number generator used for rand() */
603 	private final BSDRandom randomNumberGenerator = new BSDRandom(1);
604 
605 	/**
606 	 * Address of the END blocks section, read from the compiled program;
607 	 * {@code null} for expression streams.
608 	 */
609 	private Address exitAddress = null;
610 
611 	/**
612 	 * Address of the ENDFILE section, read from the compiled program when it
613 	 * has BEGINFILE/ENDFILE rules; {@code null} otherwise.
614 	 */
615 	private Address endFileAddress = null;
616 
617 	/**
618 	 * Address of the NEXT_FILE tuple that opens each input file, read from
619 	 * the compiled program when it requires per-file input stepping
620 	 * (BEGINFILE/ENDFILE rules or a {@code nextfile} statement);
621 	 * {@code null} otherwise.
622 	 */
623 	private Address nextFileAddress = null;
624 
625 	/**
626 	 * Address of the main input loop's next-record entry point, read from the
627 	 * compiled program when it consumes input, where a {@code next} statement
628 	 * executed from a user-defined function resumes; {@code null} otherwise.
629 	 */
630 	private Address nextAddress = null;
631 
632 	/**
633 	 * <code>true</code> if execution position is within a BEGINFILE rule;
634 	 * <code>false</code> otherwise.
635 	 */
636 	private boolean withinBeginFileBlocks = false;
637 
638 	/**
639 	 * <code>true</code> if execution position is within an ENDFILE rule;
640 	 * <code>false</code> otherwise.
641 	 */
642 	private boolean withinEndFileBlocks = false;
643 
644 	/**
645 	 * <code>true</code> once the per-file main input loop has advanced to its
646 	 * first input file; <code>false</code> while still in the BEGIN blocks.
647 	 */
648 	private boolean inputFileLoopStarted = false;
649 
650 	/**
651 	 * <code>true</code> once the main input loop has started consuming input
652 	 * records; <code>false</code> while still in the BEGIN blocks (and, for
653 	 * per-file programs, while the first BEGINFILE rules run).
654 	 */
655 	private boolean mainInputLoopStarted = false;
656 
657 	/**
658 	 * <code>true</code> if execution position is within an END block;
659 	 * <code>false</code> otherwise.
660 	 */
661 	private boolean withinEndBlocks = false;
662 
663 	/**
664 	 * Exit code set by the <code>exit NN</code> command (0 by default)
665 	 */
666 	private int exitCode = 0;
667 
668 	/**
669 	 * Whether <code>exit</code> has been called and we should throw ExitException
670 	 */
671 	private boolean throwExitException = false;
672 
673 	/**
674 	 * Maps global variable names to their global array offsets.
675 	 * It is useful when passing variable assignments from the file-list
676 	 * portion of the command-line arguments.
677 	 */
678 	private Map<String, Integer> globalVariableOffsets;
679 	/**
680 	 * Indicates whether the variable, by name, is a scalar
681 	 * or not. If not, then it is an Associative Array.
682 	 */
683 	private Map<String, Boolean> globalVariableArrays;
684 	private Set<String> functionNames = Collections.emptySet();
685 	private Map<String, Integer> initializedEvalGlobalVariableOffsets;
686 	private Map<String, Boolean> initializedEvalGlobalVariableArrays;
687 
688 	/**
689 	 * Resets the interpreter to a fresh eval state and binds one text record as
690 	 * the current input.
691 	 *
692 	 * @param input text record to expose as {@code $0}
693 	 * @return {@code true} when a record was prepared, {@code false} when the
694 	 *         provided text represents no input
695 	 * @throws IOException if binding the input fails
696 	 */
697 	public boolean prepareForEval(String input) throws IOException {
698 		return prepareForEval(new SingleRecordInputSource(input), Collections.<String>emptyList(), null);
699 	}
700 
701 	/**
702 	 * Resets the interpreter to a fresh eval state and binds at most one record
703 	 * from the provided input source as the current input. Calling this method
704 	 * again on the same source advances to the next available record.
705 	 *
706 	 * @param inputSource source providing the record to bind
707 	 * @return {@code true} when a record was prepared, {@code false} when the
708 	 *         source is exhausted
709 	 * @throws IOException if reading the input fails
710 	 */
711 	public boolean prepareForEval(InputSource inputSource) throws IOException {
712 		return prepareForEval(inputSource, Collections.<String>emptyList(), null);
713 	}
714 
715 	private boolean prepareForEval(
716 			InputSource inputSource,
717 			List<String> runtimeArguments,
718 			Map<String, Object> variableOverrides)
719 			throws IOException {
720 		InputSource resolvedSource = Objects.requireNonNull(inputSource, "inputSource");
721 		resetRuntimeState(runtimeArguments, variableOverrides);
722 		rebindResolvedInputSource(resolvedSource);
723 
724 		jrt.jrtCloseAll();
725 		jrt.prepareForExecution(settings.getFieldSeparator(), settings.getDefaultRS());
726 		if (!executionSpecialVariables.isEmpty()) {
727 			jrt.applySpecialVariables(executionSpecialVariables);
728 		}
729 		return jrt.consumeInputForEval(resolvedInputSource);
730 	}
731 
732 	private void resetRuntimeState(List<String> runtimeArguments, Map<String, Object> variableOverrides) {
733 		resetTransientRuntimeState(runtimeArguments, variableOverrides);
734 		runtimeStack.clearGlobals();
735 	}
736 
737 	private void resetTransientRuntimeState(List<String> runtimeArguments, Map<String, Object> variableOverrides) {
738 		// Reset the AVM-owned state that must not leak across executions.
739 		clearOperandStack();
740 		elementArgumentReferences.clear();
741 		environOffset = NULL_OFFSET;
742 		argcOffset = NULL_OFFSET;
743 		argvOffset = NULL_OFFSET;
744 		symtabOffset = NULL_OFFSET;
745 		exitAddress = null;
746 		endFileAddress = null;
747 		nextFileAddress = null;
748 		nextAddress = null;
749 		withinBeginFileBlocks = false;
750 		withinEndFileBlocks = false;
751 		inputFileLoopStarted = false;
752 		mainInputLoopStarted = false;
753 		withinEndBlocks = false;
754 		exitCode = 0;
755 		throwExitException = false;
756 		globalVariableOffsets = null;
757 		globalVariableArrays = null;
758 		functionNames = Collections.emptySet();
759 		initializedEvalGlobalVariableOffsets = null;
760 		initializedEvalGlobalVariableArrays = null;
761 		installedEvalExpression = null;
762 		mergedGlobalLayoutActive = false;
763 		runtimeStack.resetTransientState();
764 		randomNumberGenerator.setSeed(1);
765 
766 		prepareExecutionInputs(runtimeArguments, variableOverrides);
767 	}
768 
769 	private void installExpressionMetadata(AwkExpression compiledExpression) {
770 		if (installedEvalExpression == compiledExpression) {
771 			return;
772 		}
773 		globalVariableOffsets = compiledExpression.getGlobalVariableOffsetMap();
774 		globalVariableArrays = compiledExpression.getGlobalVariableAarrayMap();
775 		functionNames = compiledExpression.getFunctionNameSet();
776 		installedEvalExpression = compiledExpression;
777 	}
778 
779 	private void installProgramMetadata(AwkProgram compiledProgram) {
780 		globalVariableOffsets = compiledProgram.getGlobalVariableOffsetMap();
781 		globalVariableArrays = compiledProgram.getGlobalVariableAarrayMap();
782 		functionNames = compiledProgram.getFunctionNameSet();
783 		sourceDescription = compiledProgram.getSourceDescription();
784 		exitAddress = compiledProgram.getExitAddress();
785 		endFileAddress = compiledProgram.getEndFileAddress();
786 		nextFileAddress = compiledProgram.getNextFileAddress();
787 		nextAddress = compiledProgram.getNextAddress();
788 	}
789 
790 	private void rebindResolvedInputSource(InputSource resolvedSource) {
791 		InputSource previousResolvedSource = resolvedInputSource;
792 		if (previousResolvedSource != null && previousResolvedSource != resolvedSource) {
793 			closeInputSource(previousResolvedSource);
794 		}
795 		resolvedInputSource = resolvedSource;
796 		// The /dev/stdin special filename follows the source bound to the run, so
797 		// that a reused AVM never reads the stream of a previous execution.
798 		jrt.bindStandardInput(resolvedSource);
799 	}
800 
801 	private boolean hasCompatibleEvalGlobalLayout(long numGlobals) {
802 		Object[] globals = runtimeStack.getNumGlobals();
803 		return globals != null
804 				&& globals.length == numGlobals
805 				&& Objects.equals(initializedEvalGlobalVariableOffsets, globalVariableOffsets)
806 				&& Objects.equals(initializedEvalGlobalVariableArrays, globalVariableArrays);
807 	}
808 
809 	/**
810 	 * Resets transient execution state, installs the new program metadata, and
811 	 * merges the previously retained user globals into the new compiled global
812 	 * layout.
813 	 *
814 	 * @param runtimeArguments name=value or filename entries for this execution
815 	 * @param variableOverrides per-call variable overrides for this execution
816 	 * @param compiledProgram program whose global layout should become active
817 	 */
818 	private void mergeRuntimeState(
819 			List<String> runtimeArguments,
820 			Map<String, Object> variableOverrides,
821 			AwkProgram compiledProgram) {
822 		Map<String, Object> carriedGlobals = collectPersistentGlobalValues();
823 		resetTransientRuntimeState(runtimeArguments, variableOverrides);
824 		installProgramMetadata(compiledProgram);
825 
826 		Map<String, Object> basePersistentSeeds = collectBasePersistentGlobalSeeds();
827 		Map<String, Object> executionUserSeeds = collectExecutionUserGlobalSeeds(variableOverrides);
828 		List<String> mergedGlobalNamesByOffset = buildMergedGlobalNamesByOffset(
829 				carriedGlobals,
830 				basePersistentSeeds,
831 				executionUserSeeds);
832 
833 		runtimeStack.rebindGlobals(mergedGlobalNamesByOffset);
834 		applyGlobalsToStack(carriedGlobals);
835 		applyGlobalsToStack(basePersistentSeeds);
836 		applyGlobalsToStack(executionUserSeeds);
837 		mergedGlobalLayoutActive = true;
838 	}
839 
840 	/**
841 	 * Returns whether the current runtime stack already contains a merged global
842 	 * layout for the compiled program about to execute.
843 	 * <p>
844 	 * Persistent execution may append previously retained globals after the
845 	 * compiled globals of the incoming program. The tuple stream only dereferences
846 	 * the prefix defined by {@code SET_NUM_GLOBALS}, so appended globals are valid
847 	 * as long as the compiled prefix still matches name-for-name and offset-for-offset.
848 	 *
849 	 * @param numGlobals number of globals compiled into the active program
850 	 * @return {@code true} when the merged layout is compatible with the active
851 	 *         program
852 	 */
853 	private boolean hasCompatiblePersistentGlobalLayout(long numGlobals) {
854 		Object[] globals = runtimeStack.getNumGlobals();
855 		if (!mergedGlobalLayoutActive || globals == null || globalVariableOffsets == null || globals.length < numGlobals) {
856 			return false;
857 		}
858 		for (Map.Entry<String, Integer> entry : globalVariableOffsets.entrySet()) {
859 			int offset = entry.getValue().intValue();
860 			if (offset < 0 || offset >= globals.length || !entry.getKey().equals(runtimeStack.getGlobalName(offset))) {
861 				return false;
862 			}
863 		}
864 		return true;
865 	}
866 
867 	/**
868 	 * Applies execution-level initial variables to stack-managed globals.
869 	 * <p>
870 	 * Plain {@link #execute(AwkProgram, InputSource, List, Map)} calls apply all
871 	 * compatible globals here. Persistent executions only reapply non-persistent
872 	 * globals so carried user globals are not overwritten unless they were
873 	 * explicitly supplied for the current run.
874 	 *
875 	 * @param skipPersistentEligibleGlobals whether persistent user globals should
876 	 *        be skipped by this application pass
877 	 */
878 	private void applyExecutionInitialVariablesToGlobalSlots(boolean skipPersistentEligibleGlobals) {
879 		for (Map.Entry<String, Object> entry : executionInitialVariables.entrySet()) {
880 			String key = entry.getKey();
881 			if (skipPersistentEligibleGlobals && isPersistentEligibleGlobal(key)) {
882 				continue;
883 			}
884 			if (functionNames.contains(key)) {
885 				throw new IllegalArgumentException("Cannot assign a scalar to a function name (" + key + ").");
886 			}
887 			Integer offsetObj = globalVariableOffsets.get(key);
888 			Boolean arrayObj = globalVariableArrays.get(key);
889 			if (offsetObj != null) {
890 				Object obj = normalizeExternalVariableValue(entry.getValue());
891 				if (arrayObj.booleanValue()) {
892 					if (obj instanceof Map) {
893 						runtimeStack.setFilelistVariable(offsetObj.intValue(), obj);
894 					} else {
895 						throw new IllegalArgumentException("Cannot assign a scalar to a non-scalar variable (" + key + ").");
896 					}
897 				} else {
898 					runtimeStack.setFilelistVariable(offsetObj.intValue(), obj);
899 				}
900 			}
901 		}
902 	}
903 
904 	/**
905 	 * Prepares the per-execution runtime arguments and variable overrides.
906 	 * <p>
907 	 * Base settings-level variables remain the default source. Per-call overrides
908 	 * are layered on top without mutating the base snapshot held by this AVM.
909 	 *
910 	 * @param runtimeArguments name=value or filename entries for this execution
911 	 * @param variableOverrides per-call variable overrides for this execution
912 	 */
913 	private void prepareExecutionInputs(List<String> runtimeArguments, Map<String, Object> variableOverrides) {
914 		this.arguments = runtimeArguments != null ? new ArrayList<>(runtimeArguments) : Collections.<String>emptyList();
915 
916 		if (variableOverrides == null || variableOverrides.isEmpty()) {
917 			executionInitialVariables = baseInitialVariables;
918 			executionSpecialVariables = baseSpecialVariables;
919 		} else {
920 			executionInitialVariables = new HashMap<>(baseInitialVariables);
921 			// Normalized here, like base variables in assignVariable(), so that a
922 			// variable the script never references (read by an extension through
923 			// getVariable()) is a copy with numeric strings too
924 			for (Map.Entry<String, Object> entry : variableOverrides.entrySet()) {
925 				executionInitialVariables.put(entry.getKey(), normalizeExternalVariableValue(entry.getValue()));
926 			}
927 
928 			Map<String, Object> specialOverrides = JRT.copySpecialVariables(variableOverrides);
929 			if (specialOverrides.isEmpty()) {
930 				executionSpecialVariables = baseSpecialVariables;
931 			} else {
932 				executionSpecialVariables = new HashMap<>(baseSpecialVariables);
933 				executionSpecialVariables.putAll(specialOverrides);
934 			}
935 		}
936 	}
937 
938 	/**
939 	 * Filters the given map to retain only entries whose keys are
940 	 * persistent-eligible globals.
941 	 *
942 	 * @param source source map to filter
943 	 * @return insertion-ordered map containing only persistent-eligible entries
944 	 */
945 	private Map<String, Object> filterToPersistentEligible(Map<String, Object> source) {
946 		Map<String, Object> result = new LinkedHashMap<>();
947 		for (Map.Entry<String, Object> entry : source.entrySet()) {
948 			if (isPersistentEligibleGlobal(entry.getKey())) {
949 				result.put(entry.getKey(), entry.getValue());
950 			}
951 		}
952 		return result;
953 	}
954 
955 	/**
956 	 * Collects the current user-defined globals retained in the runtime stack.
957 	 *
958 	 * @return retained user globals keyed by name, in current runtime order
959 	 */
960 	private Map<String, Object> collectPersistentGlobalValues() {
961 		return filterToPersistentEligible(runtimeStack.snapshotGlobalVariables());
962 	}
963 
964 	/**
965 	 * Collects the AVM-wide baseline variables that should be reapplied before
966 	 * each persistent execution.
967 	 *
968 	 * @return baseline user globals keyed by name
969 	 */
970 	private Map<String, Object> collectBasePersistentGlobalSeeds() {
971 		Map<String, Object> basePersistentSeeds = new LinkedHashMap<>();
972 		for (Map.Entry<String, Object> entry : baseInitialVariables.entrySet()) {
973 			String name = entry.getKey();
974 			if (isPersistentEligibleGlobal(name)) {
975 				validateSeededGlobalName(name);
976 				Object value = normalizeExternalVariableValue(entry.getValue());
977 				validateSeededGlobalValue(name, value);
978 				basePersistentSeeds.put(name, value);
979 			}
980 		}
981 		return basePersistentSeeds;
982 	}
983 
984 	/**
985 	 * Collects the user-defined variables that should override the retained
986 	 * global bank for the current persistent execution.
987 	 * <p>
988 	 * Only user globals are included here. JRT-managed special variables still
989 	 * flow through the normal execution setup.
990 	 *
991 	 * @param variableOverrides per-call variable overrides for this execution
992 	 * @return insertion-ordered overriding seed values keyed by variable name
993 	 */
994 	private Map<String, Object> collectExecutionUserGlobalSeeds(Map<String, Object> variableOverrides) {
995 		Map<String, Object> executionUserSeeds = new LinkedHashMap<>();
996 		if (variableOverrides != null) {
997 			for (Map.Entry<String, Object> entry : variableOverrides.entrySet()) {
998 				String name = entry.getKey();
999 				if (isPersistentEligibleGlobal(name)) {
1000 					validateSeededGlobalName(name);
1001 					Object value = normalizeExternalVariableValue(entry.getValue());
1002 					validateSeededGlobalValue(name, value);
1003 					executionUserSeeds.put(name, value);
1004 				}
1005 			}
1006 		}
1007 		return executionUserSeeds;
1008 	}
1009 
1010 	/**
1011 	 * Builds the slot order for the next persistent execution.
1012 	 * <p>
1013 	 * The compiled globals are always installed first in their compiled offset
1014 	 * order. Retained globals and seeded user globals that are not compiled by
1015 	 * the incoming program are appended afterwards so future runs can still reuse
1016 	 * them without changing the current program's compiled offsets.
1017 	 *
1018 	 * @param carriedGlobals retained user globals from the previous execution
1019 	 * @param basePersistentSeeds baseline user globals coming from the AVM settings
1020 	 * @param executionUserSeeds per-call user overrides for this execution
1021 	 * @return merged slot-to-name layout for the next persistent run
1022 	 */
1023 	private List<String> buildMergedGlobalNamesByOffset(
1024 			Map<String, Object> carriedGlobals,
1025 			Map<String, Object> basePersistentSeeds,
1026 			Map<String, Object> executionUserSeeds) {
1027 		LinkedHashSet<String> orderedNames = new LinkedHashSet<>();
1028 		List<Map.Entry<String, Integer>> compiledGlobals = new ArrayList<>(globalVariableOffsets.entrySet());
1029 		compiledGlobals.sort(java.util.Comparator.comparingInt(Map.Entry::getValue));
1030 		for (Map.Entry<String, Integer> entry : compiledGlobals) {
1031 			orderedNames.add(entry.getKey());
1032 		}
1033 		orderedNames.addAll(carriedGlobals.keySet());
1034 		orderedNames.addAll(basePersistentSeeds.keySet());
1035 		orderedNames.addAll(executionUserSeeds.keySet());
1036 		return new ArrayList<>(orderedNames);
1037 	}
1038 
1039 	/**
1040 	 * Writes each entry from {@code globals} into the corresponding named slot in
1041 	 * the runtime stack.
1042 	 *
1043 	 * @param globals map of variable name to value to apply
1044 	 */
1045 	private void applyGlobalsToStack(Map<String, Object> globals) {
1046 		for (Map.Entry<String, Object> entry : globals.entrySet()) {
1047 			runtimeStack.setGlobalVariable(entry.getKey(), entry.getValue());
1048 		}
1049 	}
1050 
1051 	/**
1052 	 * Returns whether the given global name should participate in persistent
1053 	 * memory.
1054 	 *
1055 	 * @param name global variable name
1056 	 * @return {@code true} when the variable should persist across runs
1057 	 */
1058 	private boolean isPersistentEligibleGlobal(String name) {
1059 		return name != null && !isManagedSpecialVariable(name) && !NON_PERSISTENT_GLOBALS.contains(name);
1060 	}
1061 
1062 	/**
1063 	 * Returns whether the named variable is a JRT-managed special variable in
1064 	 * the current execution mode. Most special variables (NR, FS, FILENAME,
1065 	 * ...) always are. The gawk-only ERRNO and ARGIND are special outside
1066 	 * POSIX mode only: with {@code --posix} they are ordinary global
1067 	 * variables, exactly like {@code gawk --posix} treats them.
1068 	 *
1069 	 * @param name variable name to inspect
1070 	 * @return {@code true} when reads and writes of the variable go through
1071 	 *         the JRT instead of a global slot
1072 	 */
1073 	private boolean isManagedSpecialVariable(String name) {
1074 		if (!JRT.isJrtManagedSpecialVariable(name)) {
1075 			return false;
1076 		}
1077 		if (JRT.isGawkOnlySpecialVariable(name)) {
1078 			return !settings.isPosix();
1079 		}
1080 		return true;
1081 	}
1082 
1083 	/**
1084 	 * Validates that a seeded global name is compatible with the compiled
1085 	 * metadata of the current program before any value normalization can mutate a
1086 	 * caller-provided object graph.
1087 	 *
1088 	 * @param name variable name to validate
1089 	 */
1090 	private void validateSeededGlobalName(String name) {
1091 		if (functionNames.contains(name)) {
1092 			throw new IllegalArgumentException("Cannot assign a value to a function name (" + name + ").");
1093 		}
1094 	}
1095 
1096 	/**
1097 	 * Validates that a normalized seeded global value is compatible with the
1098 	 * compiled metadata of the current program.
1099 	 *
1100 	 * @param name variable name to validate
1101 	 * @param value proposed seeded value after normalization
1102 	 */
1103 	private void validateSeededGlobalValue(String name, Object value) {
1104 		Boolean arrayObj = globalVariableArrays.get(name);
1105 		if (Boolean.TRUE.equals(arrayObj) && !(value instanceof Map)) {
1106 			throw new IllegalArgumentException("Cannot assign a scalar to a non-scalar variable (" + name + ").");
1107 		}
1108 	}
1109 
1110 	/**
1111 	 * Executes the tuple stream after the runtime has been fully prepared.
1112 	 *
1113 	 * @param position current position in the tuple stream
1114 	 * @throws ExitException when the AWK program executes {@code exit}
1115 	 * @throws IOException when runtime input operations fail
1116 	 */
1117 	private void executeTuples(PositionTracker position) throws ExitException, IOException {
1118 		Map<Long, ConditionPair> conditionPairs = null;
1119 		Opcode opcode = null;
1120 		long tupleStartNanos = 0L;
1121 		try {
1122 			while (!position.isEOF()) {
1123 				// System_out.println("--> "+position);
1124 				Tuple tuple = position.current();
1125 				opcode = tuple.getOpcode();
1126 				if (profiling) {
1127 					tupleStartNanos = beforeProfiledTuple(tuple, opcode);
1128 				}
1129 				// switch on OPCODE
1130 				switch (opcode) {
1131 				case PRINT: {
1132 					execPrint((CountTuple) tuple);
1133 					position.next();
1134 					break;
1135 				}
1136 				case PRINT_TO_FILE: {
1137 					execPrintToFile((CountAndAppendTuple) tuple);
1138 					position.next();
1139 					break;
1140 				}
1141 				case PRINT_TO_PIPE: {
1142 					execPrintToPipe((CountTuple) tuple);
1143 					position.next();
1144 					break;
1145 				}
1146 				case PRINTF: {
1147 					execPrintf((CountTuple) tuple);
1148 					position.next();
1149 					break;
1150 				}
1151 				case PRINTF_TO_FILE: {
1152 					execPrintfToFile((CountAndAppendTuple) tuple);
1153 					position.next();
1154 					break;
1155 				}
1156 				case PRINTF_TO_PIPE: {
1157 					execPrintfToPipe((CountTuple) tuple);
1158 					position.next();
1159 					break;
1160 				}
1161 				case SPRINTF: {
1162 					// arg[0] = # of sprintf arguments
1163 					// stack[0] = arg1 (format string)
1164 					// stack[1] = arg2
1165 					// etc.
1166 					CountTuple countTuple = (CountTuple) tuple;
1167 					long numArgs = countTuple.getCount();
1168 					push(sprintfFunction(numArgs));
1169 					position.next();
1170 					break;
1171 				}
1172 				case LENGTH: {
1173 					execLength((CountTuple) tuple);
1174 					position.next();
1175 					break;
1176 				}
1177 				case PUSH_LONG: {
1178 					// arg[0] = long constant to push onto the stack
1179 					PushLongTuple pushTuple = (PushLongTuple) tuple;
1180 					push(pushTuple.getValue());
1181 					position.next();
1182 					break;
1183 				}
1184 				case PUSH_DOUBLE: {
1185 					// arg[0] = double constant to push onto the stack
1186 					PushDoubleTuple pushTuple = (PushDoubleTuple) tuple;
1187 					push(pushTuple.getValue());
1188 					position.next();
1189 					break;
1190 				}
1191 				case PUSH_STRING: {
1192 					// arg[0] = string constant to push onto the stack
1193 					PushStringTuple pushTuple = (PushStringTuple) tuple;
1194 					push(pushTuple.getValue());
1195 					position.next();
1196 					break;
1197 				}
1198 				case POP: {
1199 					// stack[0] = item to pop from the stack
1200 					Object discarded = pop();
1201 					if (tuple instanceof ScalarPopTuple) {
1202 						checkScalar(discarded);
1203 					}
1204 					position.next();
1205 					break;
1206 				}
1207 				case IFFALSE: {
1208 					// arg[0] = address to jump to if top of stack is false
1209 					// stack[0] = item to check
1210 
1211 					// if int, then check for 0
1212 					// if double, then check for 0
1213 					// if String, then check for "" or double value of "0"
1214 					boolean jump = !jrt.toBoolean(pop());
1215 					if (jump) {
1216 						position.jump(tuple.getAddress());
1217 					} else {
1218 						position.next();
1219 					}
1220 					break;
1221 				}
1222 				case TO_NUMBER: {
1223 					// stack[0] = item to convert to a number
1224 
1225 					// if int, then check for 0
1226 					// if double, then check for 0
1227 					// if String, then check for "" or double value of "0"
1228 					boolean val = jrt.toBoolean(pop());
1229 					push(val ? ONE : ZERO);
1230 					position.next();
1231 					break;
1232 				}
1233 				case IFTRUE: {
1234 					// arg[0] = address to jump to if top of stack is true
1235 					// stack[0] = item to check
1236 
1237 					// if int, then check for 0
1238 					// if double, then check for 0
1239 					// if String, then check for "" or double value of "0"
1240 					boolean jump = jrt.toBoolean(pop());
1241 					if (jump) {
1242 						position.jump(tuple.getAddress());
1243 					} else {
1244 						position.next();
1245 					}
1246 					break;
1247 				}
1248 				case NOT: {
1249 					// stack[0] = item to logically negate
1250 
1251 					Object o = pop();
1252 
1253 					boolean result = jrt.toBoolean(o);
1254 
1255 					if (result) {
1256 						push(0);
1257 					} else {
1258 						push(1);
1259 					}
1260 					position.next();
1261 					break;
1262 				}
1263 				case NEGATE: {
1264 					// stack[0] = item to numerically negate
1265 
1266 					push(JRT.negate(pop()));
1267 					position.next();
1268 					break;
1269 				}
1270 				case UNARY_PLUS: {
1271 					// stack[0] = item to convert to a number
1272 					// A numeric scalar is already a number: pushing it back
1273 					// unchanged avoids both re-boxing and precision loss.
1274 					push(numericValueOf(pop()));
1275 					position.next();
1276 					break;
1277 				}
1278 				case GOTO: {
1279 					// arg[0] = address
1280 
1281 					position.jump(tuple.getAddress());
1282 					break;
1283 				}
1284 				case NOP: {
1285 					// do nothing, just advance the position
1286 					position.next();
1287 					break;
1288 				}
1289 				case CONCAT: {
1290 					// stack[0] = string1
1291 					// stack[1] = string2
1292 					String s2 = jrt.toAwkString(pop());
1293 					String s1 = jrt.toAwkString(pop());
1294 					String resultString = s1 + s2;
1295 					push(resultString);
1296 					position.next();
1297 					break;
1298 				}
1299 				case MULTI_CONCAT: {
1300 					// arg[0] = number of stack items to concatenate
1301 					// stack[0] = last concatenation operand
1302 					CountTuple countTuple = (CountTuple) tuple;
1303 					int count = (int) countTuple.getCount();
1304 					// Store String references so appends run left-to-right. Converting
1305 					// operands to char[] would copy them once before StringBuilder
1306 					// copies them again, and front-inserting would shift existing
1307 					// content on each operand.
1308 					String[] values = new String[count];
1309 					int resultLength = 0;
1310 					for (int i = count - 1; i >= 0; i--) {
1311 						values[i] = jrt.toAwkString(pop());
1312 						resultLength += values[i].length();
1313 					}
1314 					StringBuilder resultString = new StringBuilder(resultLength);
1315 					for (String value : values) {
1316 						resultString.append(value);
1317 					}
1318 					push(resultString.toString());
1319 					position.next();
1320 					break;
1321 				}
1322 				case ASSIGN:
1323 				case ASSIGN_NOPUSH: {
1324 					// arg[0] = offset
1325 					// arg[1] = isGlobal
1326 					// stack[0] = value
1327 					VariableTuple variableTuple = (VariableTuple) tuple;
1328 					Object value = pop();
1329 					assign(variableTuple.getVariableOffset(), value, variableTuple.isGlobal(), position, opcode == Opcode.ASSIGN);
1330 					position.next();
1331 					break;
1332 				}
1333 				case ASSIGN_ARRAY: {
1334 					// arg[0] = offset
1335 					// arg[1] = isGlobal
1336 					// stack[0] = array index
1337 					// stack[1] = value
1338 					Object arrIdx = pop();
1339 					Object rhs = pop();
1340 					if (rhs == null) {
1341 						rhs = BLANK;
1342 					}
1343 					VariableTuple variableTuple = (VariableTuple) tuple;
1344 					long offset = variableTuple.getVariableOffset();
1345 					boolean isGlobal = variableTuple.isGlobal();
1346 					assignArray(offset, arrIdx, rhs, isGlobal);
1347 					position.next();
1348 					break;
1349 				}
1350 				case ASSIGN_MAP_ELEMENT: {
1351 					// stack[0] = array index
1352 					// stack[1] = associative array
1353 					// stack[2] = value
1354 					Object arrIdx = pop();
1355 					Map<Object, Object> array = toMap(pop());
1356 					Object rhs = pop();
1357 					if (rhs == null) {
1358 						rhs = BLANK;
1359 					}
1360 					assignMapElement(array, arrIdx, rhs);
1361 					position.next();
1362 					break;
1363 				}
1364 				case PLUS_EQ_ARRAY:
1365 				case MINUS_EQ_ARRAY:
1366 				case MULT_EQ_ARRAY:
1367 				case DIV_EQ_ARRAY:
1368 				case MOD_EQ_ARRAY:
1369 				case POW_EQ_ARRAY: {
1370 					execCompoundAssignArray(opcode, (VariableTuple) tuple);
1371 					position.next();
1372 					break;
1373 				}
1374 				case PLUS_EQ_MAP_ELEMENT:
1375 				case MINUS_EQ_MAP_ELEMENT:
1376 				case MULT_EQ_MAP_ELEMENT:
1377 				case DIV_EQ_MAP_ELEMENT:
1378 				case MOD_EQ_MAP_ELEMENT:
1379 				case POW_EQ_MAP_ELEMENT: {
1380 					execCompoundAssignMapElement(opcode);
1381 					position.next();
1382 					break;
1383 				}
1384 
1385 				case ASSIGN_AS_INPUT: {
1386 					// stack[0] = value
1387 					jrt.setInputLine(pop());
1388 					push(jrt.getInputLine());
1389 					position.next();
1390 					break;
1391 				}
1392 
1393 				case ASSIGN_AS_INPUT_FIELD: {
1394 					// stack[0] = field number
1395 					// stack[1] = value
1396 					Object fieldNumObj = pop();
1397 					long fieldNum = JRT.parseFieldNumber(fieldNumObj);
1398 					Object value = pop();
1399 					push(value); // leave the result on the stack
1400 					if (fieldNum == 0) {
1401 						jrt.setInputLine(value);
1402 						jrt.jrtParseFields();
1403 					} else {
1404 						jrt.jrtSetInputField(value, fieldNum);
1405 					}
1406 					position.next();
1407 					break;
1408 				}
1409 				case PLUS_EQ:
1410 				case MINUS_EQ:
1411 				case MULT_EQ:
1412 				case DIV_EQ:
1413 				case MOD_EQ:
1414 				case POW_EQ: {
1415 					execCompoundAssignVariable(opcode, (VariableTuple) tuple);
1416 					position.next();
1417 					break;
1418 				}
1419 				case PLUS_EQ_INPUT_FIELD:
1420 				case MINUS_EQ_INPUT_FIELD:
1421 				case MULT_EQ_INPUT_FIELD:
1422 				case DIV_EQ_INPUT_FIELD:
1423 				case MOD_EQ_INPUT_FIELD:
1424 				case POW_EQ_INPUT_FIELD: {
1425 					execCompoundAssignInputField(opcode);
1426 					position.next();
1427 					break;
1428 				}
1429 				case INC: {
1430 					// arg[0] = offset
1431 					// arg[1] = isGlobal
1432 					VariableTuple variableTuple = (VariableTuple) tuple;
1433 					inc(variableTuple.getVariableOffset(), variableTuple.isGlobal());
1434 					position.next();
1435 					break;
1436 				}
1437 				case DEC: {
1438 					// arg[0] = offset
1439 					// arg[1] = isGlobal
1440 					VariableTuple variableTuple = (VariableTuple) tuple;
1441 					dec(variableTuple.getVariableOffset(), variableTuple.isGlobal());
1442 					position.next();
1443 					break;
1444 				}
1445 				case POSTINC: {
1446 					// arg[0] = offset
1447 					// arg[1] = isGlobal
1448 					pop();
1449 					VariableTuple variableTuple = (VariableTuple) tuple;
1450 					push(inc(variableTuple.getVariableOffset(), variableTuple.isGlobal()));
1451 					position.next();
1452 					break;
1453 				}
1454 				case POSTDEC: {
1455 					// arg[0] = offset
1456 					// arg[1] = isGlobal
1457 					pop();
1458 					VariableTuple variableTuple = (VariableTuple) tuple;
1459 					push(dec(variableTuple.getVariableOffset(), variableTuple.isGlobal()));
1460 					position.next();
1461 					break;
1462 				}
1463 				case INC_ARRAY_REF: {
1464 					// arg[0] = offset
1465 					// arg[1] = isGlobal
1466 					// stack[0] = array index
1467 					VariableTuple variableTuple = (VariableTuple) tuple;
1468 					boolean isGlobal = variableTuple.isGlobal();
1469 					Map<Object, Object> aa = ensureMapVariable(variableTuple.getVariableOffset(), isGlobal);
1470 					Object key = pop();
1471 					checkScalar(key);
1472 					Object o = aa.get(key);
1473 					aa.put(key, JRT.inc(blankToZero(o)));
1474 					position.next();
1475 					break;
1476 				}
1477 				case DEC_ARRAY_REF: {
1478 					// arg[0] = offset
1479 					// arg[1] = isGlobal
1480 					// stack[0] = array index
1481 					VariableTuple variableTuple = (VariableTuple) tuple;
1482 					boolean isGlobal = variableTuple.isGlobal();
1483 					Map<Object, Object> aa = ensureMapVariable(variableTuple.getVariableOffset(), isGlobal);
1484 					Object key = pop();
1485 					checkScalar(key);
1486 					Object o = aa.get(key);
1487 					aa.put(key, JRT.dec(blankToZero(o)));
1488 					position.next();
1489 					break;
1490 				}
1491 				case INC_MAP_REF: {
1492 					// stack[0] = array index
1493 					// stack[1] = associative array
1494 					Object key = pop();
1495 					checkScalar(key);
1496 					Map<Object, Object> aa = toMap(pop());
1497 					Object o = aa.get(key);
1498 					aa.put(key, JRT.inc(blankToZero(o)));
1499 					position.next();
1500 					break;
1501 				}
1502 				case DEC_MAP_REF: {
1503 					// stack[0] = array index
1504 					// stack[1] = associative array
1505 					Object key = pop();
1506 					checkScalar(key);
1507 					Map<Object, Object> aa = toMap(pop());
1508 					Object o = aa.get(key);
1509 					aa.put(key, JRT.dec(blankToZero(o)));
1510 					position.next();
1511 					break;
1512 				}
1513 				case INC_DOLLAR_REF: {
1514 					// stack[0] = dollar index (field number)
1515 					long fieldnum = JRT.parseFieldNumber(pop());
1516 
1517 					Object numObj = blankToZero(jrt.jrtGetInputField(fieldnum));
1518 					Object original = numericValueOf(numObj);
1519 					setNumOnJRT(fieldnum, JRT.inc(original));
1520 
1521 					push(original);
1522 
1523 					position.next();
1524 					break;
1525 				}
1526 				case DEC_DOLLAR_REF: {
1527 					// stack[0] = dollar index (field number)
1528 					// same code as GET_INPUT_FIELD:
1529 					long fieldnum = JRT.parseFieldNumber(pop());
1530 
1531 					Object numObj = blankToZero(jrt.jrtGetInputField(fieldnum));
1532 					Object original = numericValueOf(numObj);
1533 					setNumOnJRT(fieldnum, JRT.dec(original));
1534 
1535 					push(original);
1536 
1537 					position.next();
1538 					break;
1539 				}
1540 				case DEREFERENCE: {
1541 					// arg[0] = offset
1542 					// arg[1] = isGlobal
1543 					DereferenceTuple dereferenceTuple = (DereferenceTuple) tuple;
1544 					boolean isGlobal = dereferenceTuple.isGlobal();
1545 					long offset = dereferenceTuple.getVariableOffset();
1546 					push(resolveVariable(offset, isGlobal, dereferenceTuple.isArray()));
1547 					position.next();
1548 					break;
1549 				}
1550 				case PEEK_DEREFERENCE: {
1551 					VariableTuple variableTuple = (VariableTuple) tuple;
1552 					Object value = runtimeStack.getVariable(variableTuple.getVariableOffset(), variableTuple.isGlobal());
1553 					push(value instanceof ArgumentReference ? resolveRawArgumentReference((ArgumentReference) value) : value);
1554 					position.next();
1555 					break;
1556 				}
1557 				case PUSH_INDIRECT_ARGUMENT: {
1558 					VariableTuple variableTuple = (VariableTuple) tuple;
1559 					Object scalarValue = runtimeStack.getVariable(variableTuple.getVariableOffset(), variableTuple.isGlobal());
1560 					// Typed values are passed as plain values; only untyped
1561 					// variables need a live link back to the caller's slot.
1562 					if (scalarValue instanceof ArgumentReference) {
1563 						push(resolveUserFunctionArgument(scalarValue));
1564 					} else if (isUntyped(scalarValue)) {
1565 						push(
1566 								new IndirectArgumentReference(
1567 										runtimeStack.getVariableFrame(variableTuple.isGlobal()),
1568 										variableTuple.getVariableOffset(),
1569 										scalarValue));
1570 					} else {
1571 						push(scalarValue);
1572 					}
1573 					position.next();
1574 					break;
1575 				}
1576 				case PUSH_INDIRECT_ARRAY_ARGUMENT: {
1577 					Object idx = pop();
1578 					checkScalar(idx);
1579 					Map<Object, Object> map = toMap(pop());
1580 					Object scalarValue = JRT.getAssocArrayValue(map, idx);
1581 					// Typed elements are passed as plain values; only untyped
1582 					// elements need a live link back to their containing array.
1583 					push(isUntyped(scalarValue) ? new IndirectArrayArgumentReference(map, idx, scalarValue) : scalarValue);
1584 					position.next();
1585 					break;
1586 				}
1587 				case DEREF_ARRAY: {
1588 					// stack[0] = array index
1589 					Object idx = pop(); // idx
1590 					checkScalar(idx);
1591 					Map<Object, Object> map = toMap(pop());
1592 					Object o = JRT.getAssocArrayValue(map, idx);
1593 					push(o);
1594 					position.next();
1595 					break;
1596 				}
1597 				case ENSURE_ARRAY_ELEMENT: {
1598 					// stack[0] = array index
1599 					// stack[1] = associative array
1600 					Object idx = pop();
1601 					Map<Object, Object> map = toMap(pop());
1602 					push(ensureArrayInArray(map, idx));
1603 					position.next();
1604 					break;
1605 				}
1606 				case PEEK_ARRAY_ELEMENT: {
1607 					// stack[0] = array index
1608 					Object idx = pop();
1609 					checkScalar(idx);
1610 					Map<Object, Object> map = toMap(pop());
1611 					if (map instanceof AssocArray && !JRT.containsAwkKey(map, idx)) {
1612 						push(AssocArray.UNTYPED);
1613 					} else {
1614 						Object value = map.get(idx);
1615 						push(value != null ? value : AssocArray.UNTYPED);
1616 					}
1617 					position.next();
1618 					break;
1619 				}
1620 				case SRAND: {
1621 					// arg[0] = numArgs (where 0 = no args, anything else = one argument)
1622 					// stack[0] = seed (only if numArgs != 0)
1623 					CountTuple countTuple = (CountTuple) tuple;
1624 					long numArgs = countTuple.getCount();
1625 					int seed;
1626 					if (numArgs == 0) {
1627 						// use the time of day for the seed
1628 						seed = JRT.timeSeed();
1629 					} else {
1630 						seed = (int) JRT.toDouble(pop());
1631 					}
1632 					int previousSeed = randomNumberGenerator.getSeed();
1633 					randomNumberGenerator.setSeed(seed);
1634 					push(previousSeed);
1635 					position.next();
1636 					break;
1637 				}
1638 				case RAND: {
1639 					push(randomNumberGenerator.nextDouble());
1640 					position.next();
1641 					break;
1642 				}
1643 				case INTFUNC: {
1644 					// stack[0] = arg to int() function
1645 					push(JRT.truncateToScalar(JRT.toDouble(pop())));
1646 					position.next();
1647 					break;
1648 				}
1649 				case SQRT: {
1650 					// stack[0] = arg to sqrt() function
1651 					push(Math.sqrt(JRT.toDouble(pop())));
1652 					position.next();
1653 					break;
1654 				}
1655 				case LOG: {
1656 					// stack[0] = arg to log() function
1657 					push(Math.log(JRT.toDouble(pop())));
1658 					position.next();
1659 					break;
1660 				}
1661 				case EXP: {
1662 					// stack[0] = arg to exp() function
1663 					push(Math.exp(JRT.toDouble(pop())));
1664 					position.next();
1665 					break;
1666 				}
1667 				case SIN: {
1668 					// stack[0] = arg to sin() function
1669 					push(Math.sin(JRT.toDouble(pop())));
1670 					position.next();
1671 					break;
1672 				}
1673 				case COS: {
1674 					// stack[0] = arg to cos() function
1675 					push(Math.cos(JRT.toDouble(pop())));
1676 					position.next();
1677 					break;
1678 				}
1679 				case ATAN2: {
1680 					// stack[0] = 2nd arg to atan2() function
1681 					// stack[1] = 1st arg to atan2() function
1682 					double d2 = JRT.toDouble(pop());
1683 					double d1 = JRT.toDouble(pop());
1684 					push(Math.atan2(d1, d2));
1685 					position.next();
1686 					break;
1687 				}
1688 				case MATCH: {
1689 					execMatch();
1690 					position.next();
1691 					break;
1692 				}
1693 				case INDEX: {
1694 					// stack[0] = 2nd arg to index() function
1695 					// stack[1] = 1st arg to index() function
1696 					String s2 = jrt.toAwkString(pop());
1697 					String s1 = jrt.toAwkString(pop());
1698 					push(jrt.index(s1, s2));
1699 					position.next();
1700 					break;
1701 				}
1702 				case SUB_FOR_DOLLAR_0: {
1703 					execSubForDollar0((BooleanTuple) tuple);
1704 					position.next();
1705 					break;
1706 				}
1707 				case SUB_FOR_DOLLAR_REFERENCE: {
1708 					execSubForDollarReference((BooleanTuple) tuple);
1709 					position.next();
1710 					break;
1711 				}
1712 				case SUB_FOR_VARIABLE: {
1713 					execSubForVariable((SubstitutionVariableTuple) tuple, position);
1714 					position.next();
1715 					break;
1716 				}
1717 				case SUB_FOR_ARRAY_REFERENCE: {
1718 					execSubForArrayReference((SubstitutionVariableTuple) tuple);
1719 					position.next();
1720 					break;
1721 				}
1722 				case SUB_FOR_MAP_REFERENCE: {
1723 					execSubForMapReference((BooleanTuple) tuple);
1724 					position.next();
1725 					break;
1726 				}
1727 				case SPLIT: {
1728 					execSplit((CountTuple) tuple, position);
1729 					position.next();
1730 					break;
1731 				}
1732 				case SUBSTR: {
1733 					execSubstr((CountTuple) tuple);
1734 					position.next();
1735 					break;
1736 				}
1737 				case TOLOWER: {
1738 					// stack[0] = string
1739 					push(jrt.toAwkString(pop()).toLowerCase());
1740 					position.next();
1741 					break;
1742 				}
1743 				case TOUPPER: {
1744 					// stack[0] = string
1745 					push(jrt.toAwkString(pop()).toUpperCase());
1746 					position.next();
1747 					break;
1748 				}
1749 				case SYSTEM: {
1750 					// stack[0] = command string
1751 					String s = jrt.toAwkString(pop());
1752 					push(jrt.jrtSystem(s));
1753 					position.next();
1754 					break;
1755 				}
1756 				case SWAP: {
1757 					// stack[0] = item1
1758 					// stack[1] = item2
1759 					Object o1 = pop();
1760 					Object o2 = pop();
1761 					push(o1);
1762 					push(o2);
1763 					position.next();
1764 					break;
1765 				}
1766 				case CMP_EQ: {
1767 					// stack[0] = item2
1768 					// stack[1] = item1
1769 					Object o2 = pop();
1770 					Object o1 = pop();
1771 					push(jrt.compare(o1, o2, 0) ? ONE : ZERO);
1772 					position.next();
1773 					break;
1774 				}
1775 				case CMP_LT: {
1776 					// stack[0] = item2
1777 					// stack[1] = item1
1778 					Object o2 = pop();
1779 					Object o1 = pop();
1780 					push(jrt.compare(o1, o2, -1) ? ONE : ZERO);
1781 					position.next();
1782 					break;
1783 				}
1784 				case CMP_GT: {
1785 					// stack[0] = item2
1786 					// stack[1] = item1
1787 					Object o2 = pop();
1788 					Object o1 = pop();
1789 					push(jrt.compare(o1, o2, 1) ? ONE : ZERO);
1790 					position.next();
1791 					break;
1792 				}
1793 				case MATCHES: {
1794 					// stack[0] = regexp (precompiled or dynamic)
1795 					// stack[1] = text
1796 					Object o2 = pop();
1797 					Object o1 = pop();
1798 					// The text operand must be converted with the AWK number-to-string rule, not with
1799 					// Object.toString(): a Double would otherwise be matched as "291.0" instead of "291".
1800 					push(jrt.matches(jrt.toAwkString(o1), o2) ? 1 : 0);
1801 					position.next();
1802 					break;
1803 				}
1804 				case ADD: {
1805 					// stack[0] = item2
1806 					// stack[1] = item1
1807 					Object o2 = pop();
1808 					Object o1 = pop();
1809 					push(JRT.add(o1, o2));
1810 					position.next();
1811 					break;
1812 				}
1813 				case SUBTRACT: {
1814 					// stack[0] = item2
1815 					// stack[1] = item1
1816 					Object o2 = pop();
1817 					Object o1 = pop();
1818 					push(JRT.subtract(o1, o2));
1819 					position.next();
1820 					break;
1821 				}
1822 				case MULTIPLY: {
1823 					// stack[0] = item2
1824 					// stack[1] = item1
1825 					Object o2 = pop();
1826 					Object o1 = pop();
1827 					push(JRT.multiply(o1, o2));
1828 					position.next();
1829 					break;
1830 				}
1831 				case DIVIDE: {
1832 					// stack[0] = item2
1833 					// stack[1] = item1
1834 					Object o2 = pop();
1835 					Object o1 = pop();
1836 					push(JRT.divide(o1, o2));
1837 					position.next();
1838 					break;
1839 				}
1840 				case MOD: {
1841 					// stack[0] = item2
1842 					// stack[1] = item1
1843 					Object o2 = pop();
1844 					Object o1 = pop();
1845 					push(JRT.mod(o1, o2));
1846 					position.next();
1847 					break;
1848 				}
1849 				case POW: {
1850 					// stack[0] = item2
1851 					// stack[1] = item1
1852 					Object o2 = pop();
1853 					Object o1 = pop();
1854 					push(JRT.pow(o1, o2));
1855 					position.next();
1856 					break;
1857 				}
1858 				case DUP: {
1859 					// stack[0] = top of stack item
1860 					Object o = pop();
1861 					push(o);
1862 					push(o);
1863 					position.next();
1864 					break;
1865 				}
1866 				case KEYLIST: {
1867 					Object o = pop();
1868 					if (isUntyped(o)) {
1869 						push(new ArrayDeque<>());
1870 						position.next();
1871 						break;
1872 					}
1873 					if (!(o instanceof Map)) {
1874 						throw new AwkRuntimeException("Attempting to use a scalar as an array.");
1875 					}
1876 					@SuppressWarnings("unchecked")
1877 					Map<Object, Object> map = (Map<Object, Object>) o;
1878 					push(new ArrayDeque<>(forInKeyOrder == null ? map.keySet() : forInKeyOrder.order(map)));
1879 					position.next();
1880 					break;
1881 				}
1882 				case IS_EMPTY_KEYLIST: {
1883 					// arg[0] = address
1884 					// stack[0] = Deque
1885 					Object o = pop();
1886 					if (o == null || !(o instanceof Deque)) {
1887 						throw new AwkRuntimeException(
1888 								position.lineNumber(),
1889 								"Cannot get a key list (via 'in') of a non associative array. arg = " + o.getClass() + ", " + o);
1890 					}
1891 					Deque<?> keylist = (Deque<?>) o;
1892 					if (keylist.isEmpty()) {
1893 						position.jump(tuple.getAddress());
1894 					} else {
1895 						position.next();
1896 					}
1897 					break;
1898 				}
1899 				case GET_FIRST_AND_REMOVE_FROM_KEYLIST: {
1900 					// stack[0] = Deque
1901 					Object o = pop();
1902 					if (o == null || !(o instanceof Deque)) {
1903 						throw new AwkRuntimeException(
1904 								position.lineNumber(),
1905 								"Cannot get a key list (via 'in') of a non associative array. arg = " + o.getClass() + ", " + o);
1906 					}
1907 					// pop off and return the head of the key set
1908 					Deque<?> keylist = (Deque<?>) o;
1909 					push(keylist.removeFirst());
1910 					position.next();
1911 					break;
1912 				}
1913 				case CHECK_CLASS: {
1914 					// arg[0] = class object
1915 					// stack[0] = item to check
1916 					ClassTuple checkTuple = (ClassTuple) tuple;
1917 					Object o = pop();
1918 					if (!checkTuple.getType().isInstance(o)) {
1919 						throw new AwkRuntimeException(
1920 								position.lineNumber(),
1921 								"Verification failed. Top-of-stack = " + o.getClass() + " isn't an instance of "
1922 										+ checkTuple.getType());
1923 					}
1924 					push(o);
1925 					position.next();
1926 					break;
1927 				}
1928 				case CONSUME_INPUT: {
1929 					// arg[0] = address
1930 					// store the next record into $0, $1, ...
1931 					mainInputLoopStarted = true;
1932 					if (jrt.consumeInput(resolvedInputSource)) {
1933 						position.next();
1934 					} else {
1935 						position.jump(tuple.getAddress());
1936 					}
1937 					break;
1938 				}
1939 				case CONSUME_FILE_INPUT: {
1940 					// arg[0] = address of the ENDFILE section
1941 					// store the next record of the current file into $0, $1, ...
1942 					mainInputLoopStarted = true;
1943 					withinBeginFileBlocks = false;
1944 					if (jrt.consumeCurrentFileInput(resolvedInputSource)) {
1945 						position.next();
1946 					} else {
1947 						withinEndFileBlocks = true;
1948 						position.jump(tuple.getAddress());
1949 					}
1950 					break;
1951 				}
1952 				case NEXT_FILE: {
1953 					// arg[0] = address to jump to when no input file remains
1954 					inputFileLoopStarted = true;
1955 					withinEndFileBlocks = false;
1956 					if (jrt.advanceToNextFile(resolvedInputSource)) {
1957 						withinBeginFileBlocks = true;
1958 						position.next();
1959 					} else {
1960 						position.jump(tuple.getAddress());
1961 					}
1962 					break;
1963 				}
1964 				case EXEC_NEXTFILE: {
1965 					executeNextfile(position);
1966 					break;
1967 				}
1968 				case EXEC_NEXT: {
1969 					executeNext(position);
1970 					break;
1971 				}
1972 
1973 				case GETLINE_INPUT: {
1974 					checkGetlineAllowed(position);
1975 					boolean consumed = isMainInputFileBounded() ?
1976 							jrt.consumeCurrentFileInput(resolvedInputSource) :
1977 							jrt.consumeInput(resolvedInputSource);
1978 					push(consumed ? 1 : 0);
1979 					position.next();
1980 					break;
1981 				}
1982 				case GETLINE_INPUT_TO_TARGET: {
1983 					// arg[0] = address to jump to when no record was read
1984 					checkGetlineAllowed(position);
1985 					Object input = isMainInputFileBounded() ?
1986 							jrt.consumeCurrentFileInputToTarget(resolvedInputSource) :
1987 							jrt.consumeInputToTarget(resolvedInputSource);
1988 					if (input != null) {
1989 						push(1);
1990 						push(input);
1991 						position.next();
1992 					} else {
1993 						push(0);
1994 						position.jump(tuple.getAddress());
1995 					}
1996 					break;
1997 				}
1998 				case USE_AS_FILE_INPUT: {
1999 					// stack[0] = filename
2000 					// arg[0] = address to jump to when no record was read
2001 					String s = jrt.toAwkString(pop());
2002 					Integer retcode = jrt.jrtConsumeFileInputForGetline(s);
2003 					push(retcode);
2004 					if (retcode.intValue() == 1) {
2005 						push(jrt.toInputScalar(jrt.jrtGetInputString()));
2006 						position.next();
2007 					} else {
2008 						position.jump(tuple.getAddress());
2009 					}
2010 					break;
2011 				}
2012 				case USE_AS_COMMAND_INPUT: {
2013 					// stack[0] = command line
2014 					// arg[0] = address to jump to when no record was read
2015 					String s = jrt.toAwkString(pop());
2016 					Integer retcode = jrt.jrtConsumeCommandInputForGetline(s);
2017 					push(retcode);
2018 					if (retcode.intValue() == 1) {
2019 						push(jrt.toInputScalar(jrt.jrtGetInputString()));
2020 						position.next();
2021 					} else {
2022 						position.jump(tuple.getAddress());
2023 					}
2024 					break;
2025 				}
2026 				case ENVIRON_OFFSET: {
2027 					// arg[0] = offset; already populated from SET_NUM_GLOBALS so
2028 					// beforeStart hooks observe the real value
2029 					populateEnviron(((LongTuple) tuple).getValue());
2030 					position.next();
2031 					break;
2032 				}
2033 				case ARGC_OFFSET: {
2034 					// arg[0] = offset; already populated from SET_NUM_GLOBALS
2035 					populateArgc(((LongTuple) tuple).getValue());
2036 					position.next();
2037 					break;
2038 				}
2039 				case ARGV_OFFSET: {
2040 					// arg[0] = offset; already populated from SET_NUM_GLOBALS
2041 					populateArgv(((LongTuple) tuple).getValue());
2042 					position.next();
2043 					break;
2044 				}
2045 				case GET_INPUT_FIELD: {
2046 					// stack[0] = field number
2047 					Object fieldNumber = pop();
2048 					push(jrt.jrtGetInputField(fieldNumber));
2049 					position.next();
2050 					break;
2051 				}
2052 				case GET_INPUT_FIELD_CONST: {
2053 					InputFieldTuple inputFieldTuple = (InputFieldTuple) tuple;
2054 					long fieldnum = inputFieldTuple.getFieldIndex();
2055 					push(jrt.jrtGetInputField(fieldnum));
2056 					position.next();
2057 					break;
2058 				}
2059 				case APPLY_RS: {
2060 					jrt.applyRS(jrt.getRSVar());
2061 					position.next();
2062 					break;
2063 				}
2064 				case CALL_FUNCTION: {
2065 					// arg[0] = function address
2066 					// arg[1] = function name
2067 					// arg[2] = # of formal parameters
2068 					// arg[3] = # of actual parameters
2069 					// stack[0] = last actual parameter
2070 					// stack[1] = before-last actual parameter
2071 					// ...
2072 					// stack[n-1] = first actual parameter
2073 					// etc.
2074 					CallFunctionTuple callTuple = (CallFunctionTuple) tuple;
2075 					Address funcAddr = callTuple.getAddress();
2076 					long numFormalParams = callTuple.getNumFormalParams();
2077 					long numActualParams = callTuple.getNumActualParams();
2078 					runtimeStack.pushFrame(numFormalParams, position.currentIndex());
2079 					// Arguments are stacked, so first in the stack is the last for the function
2080 					for (long i = numActualParams - 1; i >= 0; i--) {
2081 						Object argument = pop();
2082 						adoptElementArgumentReference(argument);
2083 						runtimeStack.setVariable(i, argument, false); // false = local
2084 					}
2085 					position.jump(funcAddr);
2086 					// position.next();
2087 					break;
2088 				}
2089 				case INDIRECT_CALL: {
2090 					execIndirectCall((IndirectCallTuple) tuple, position, tupleStartNanos);
2091 					break;
2092 				}
2093 				case FUNCTION: {
2094 					// important for compilation,
2095 					// not needed for interpretation
2096 					// arg[0] = function name
2097 					// arg[1] = # of formal parameters
2098 					position.next();
2099 					break;
2100 				}
2101 				case WARNING: {
2102 					jrt.printWarning(((Tuple.WarningTuple) tuple).getMessage());
2103 					position.next();
2104 					break;
2105 				}
2106 				case SET_RETURN_RESULT: {
2107 					// stack[0] = return result
2108 					runtimeStack.setReturnValue(pop());
2109 					position.next();
2110 					break;
2111 				}
2112 				case RETURN_FROM_FUNCTION: {
2113 					releaseElementArgumentReferences();
2114 					position.jump(runtimeStack.popFrame());
2115 					push(runtimeStack.getReturnValue());
2116 					position.next();
2117 					break;
2118 				}
2119 				case SET_NUM_GLOBALS: {
2120 					execSetNumGlobals((CountTuple) tuple);
2121 					position.next();
2122 					break;
2123 				}
2124 				case BEFORE_START_HOOKS: {
2125 					runBeforeStartHooks();
2126 					position.next();
2127 					break;
2128 				}
2129 				case UPDATE_SYMTAB: {
2130 					execUpdateSymtab(((LongTuple) tuple).getValue());
2131 					position.next();
2132 					break;
2133 				}
2134 				case UPDATE_FUNCTAB: {
2135 					execUpdateFunctab(((LongTuple) tuple).getValue());
2136 					position.next();
2137 					break;
2138 				}
2139 				case CLOSE: {
2140 					// stack[0] = file or command line to close
2141 					String s = jrt.toAwkString(pop());
2142 					push(jrt.jrtClose(s));
2143 					position.next();
2144 					break;
2145 				}
2146 				case APPLY_SUBSEP: {
2147 					execApplySubsep((CountTuple) tuple);
2148 					position.next();
2149 					break;
2150 				}
2151 				case APPLY_SUBSEP_UNDER_TOP: {
2152 					// stack[0] = value to keep on top (the "in" array operand)
2153 					// stack[1..count] = array indices to convert
2154 					Object top = pop();
2155 					execApplySubsep((CountTuple) tuple);
2156 					push(top);
2157 					position.next();
2158 					break;
2159 				}
2160 				case DELETE_ARRAY_ELEMENT: {
2161 					// arg[0] = offset
2162 					// arg[1] = isGlobal
2163 					// stack[0] = array index
2164 					VariableTuple variableTuple = (VariableTuple) tuple;
2165 					long offset = variableTuple.getVariableOffset();
2166 					boolean isGlobal = variableTuple.isGlobal();
2167 					Map<Object, Object> aa = getMapVariable(offset, isGlobal);
2168 					Object key = pop();
2169 					checkScalar(key);
2170 					if (aa != null) {
2171 						aa.remove(key);
2172 						detachMissingArrayArgumentReferences(aa);
2173 					}
2174 					position.next();
2175 					break;
2176 				}
2177 				case DELETE_MAP_ELEMENT: {
2178 					// stack[0] = array index
2179 					// stack[1] = associative array
2180 					Object key = pop();
2181 					checkScalar(key);
2182 					Map<Object, Object> aa = toMap(pop());
2183 					aa.remove(key);
2184 					detachMissingArrayArgumentReferences(aa);
2185 					position.next();
2186 					break;
2187 				}
2188 				case DELETE_ARRAY: {
2189 					// arg[0] = offset
2190 					// arg[1] = isGlobal
2191 					// (nothing on the stack)
2192 					VariableTuple variableTuple = (VariableTuple) tuple;
2193 					long offset = variableTuple.getVariableOffset();
2194 					boolean isGlobal = variableTuple.isGlobal();
2195 					Map<Object, Object> array = getMapVariable(offset, isGlobal);
2196 					if (array != null) {
2197 						array.clear();
2198 						detachMissingArrayArgumentReferences(array);
2199 					}
2200 					position.next();
2201 					break;
2202 				}
2203 				case SET_WITHIN_END_BLOCKS: {
2204 					// arg[0] = whether within the END blocks section
2205 					BooleanTuple endBlocksTuple = (BooleanTuple) tuple;
2206 					withinEndBlocks = endBlocksTuple.getValue();
2207 					position.next();
2208 					break;
2209 				}
2210 				case EXIT_WITHOUT_CODE:
2211 				case EXIT_WITH_CODE: {
2212 					if (opcode == Opcode.EXIT_WITH_CODE) {
2213 						// stack[0] = exit code
2214 						exitCode = (int) JRT.toDouble(pop());
2215 					}
2216 					throwExitException = true;
2217 					withinBeginFileBlocks = false;
2218 					withinEndFileBlocks = false;
2219 
2220 					// If in BEGIN or in a rule, jump to the END section
2221 					if (!withinEndBlocks && exitAddress != null) {
2222 						resetCallState();
2223 						position.jump(exitAddress);
2224 					} else {
2225 						// Exit immediately with ExitException
2226 						// clear operand stack
2227 						clearOperandStack();
2228 						throw new ExitException(exitCode, "The AWK script requested an exit");
2229 						// position.next();
2230 					}
2231 					break;
2232 				}
2233 				case REGEXP: {
2234 					// Literal regex tuples must provide a precompiled Pattern as arg[1]
2235 					RegexTuple regexTuple = (RegexTuple) tuple;
2236 					Pattern pattern = regexTuple.getPattern();
2237 					push(pattern);
2238 					position.next();
2239 					break;
2240 				}
2241 				case CONDITION_PAIR: {
2242 					// Legacy opcode kept for precompiled tuple streams
2243 					// stack[0] = End condition
2244 					// stack[1] = Start condition
2245 					if (conditionPairs == null) {
2246 						conditionPairs = new HashMap<Long, ConditionPair>();
2247 					}
2248 					long currentIndex = position.currentIndex();
2249 					ConditionPair cp = conditionPairs.get(currentIndex);
2250 					if (cp == null) {
2251 						cp = new ConditionPair();
2252 						conditionPairs.put(currentIndex, cp);
2253 					}
2254 					boolean end = jrt.toBoolean(pop());
2255 					boolean start = jrt.toBoolean(pop());
2256 					push(cp.update(start, end) ? ONE : ZERO);
2257 					position.next();
2258 					break;
2259 				}
2260 				case CONDITION_PAIR_IN_RANGE: {
2261 					// arg[0] = unique identifier of the range pattern
2262 					if (conditionPairs == null) {
2263 						conditionPairs = new HashMap<Long, ConditionPair>();
2264 					}
2265 					long id = ((LongTuple) tuple).getValue();
2266 					ConditionPair cp = conditionPairs.get(id);
2267 					if (cp == null) {
2268 						cp = new ConditionPair();
2269 						conditionPairs.put(id, cp);
2270 					}
2271 					push(cp.isWithin() ? ONE : ZERO);
2272 					position.next();
2273 					break;
2274 				}
2275 				case CONDITION_PAIR_ENTER: {
2276 					// arg[0] = unique identifier of the range pattern
2277 					// A CONDITION_PAIR_IN_RANGE tuple for the same identifier always
2278 					// executes first, so the pair is already registered
2279 					conditionPairs.get(((LongTuple) tuple).getValue()).enter();
2280 					position.next();
2281 					break;
2282 				}
2283 				case CONDITION_PAIR_LEAVE: {
2284 					// arg[0] = unique identifier of the range pattern
2285 					conditionPairs.get(((LongTuple) tuple).getValue()).leave();
2286 					position.next();
2287 					break;
2288 				}
2289 				case IS_IN: {
2290 					// stack[1] = key to check
2291 					Object arr = pop();
2292 					Object arg = pop();
2293 					checkScalar(arg);
2294 					if (isUntyped(arr)) {
2295 						push(ZERO);
2296 						position.next();
2297 						break;
2298 					}
2299 					if (!(arr instanceof Map)) {
2300 						throw new AwkRuntimeException("Attempting to use a scalar as an array.");
2301 					}
2302 					@SuppressWarnings("unchecked")
2303 					Map<Object, Object> aa = (Map<Object, Object>) arr;
2304 					boolean result = JRT.containsAwkKey(aa, arg);
2305 					push(result ? ONE : ZERO);
2306 					position.next();
2307 					break;
2308 				}
2309 				case THIS: {
2310 					// this is in preparation for a function
2311 					// call for the JVM-COMPILED script, only
2312 					// therefore, do NOTHING for the interpreted
2313 					// version
2314 					position.next();
2315 					break;
2316 				}
2317 				case EXTENSION: {
2318 					// arg[0] = extension function metadata
2319 					// arg[1] = # of args on the stack
2320 					// stack[0] = first actual parameter
2321 					// stack[1] = second actual parameter
2322 					// etc.
2323 					ExtensionTuple extensionTuple = (ExtensionTuple) tuple;
2324 					ExtensionFunction function = extensionTuple.getFunction();
2325 					long numArgs = extensionTuple.getArgCount();
2326 					push(invokeExtension(function, popArguments(numArgs), position.lineNumber()));
2327 
2328 					position.next();
2329 					break;
2330 				}
2331 				case ASSIGN_NF: {
2332 					Object v = pop();
2333 					jrt.setNF(v);
2334 					push(v);
2335 					position.next();
2336 					break;
2337 				}
2338 				case PUSH_NF: {
2339 					push(jrt.getNF());
2340 					position.next();
2341 					break;
2342 				}
2343 				case ASSIGN_NR: {
2344 					Object v = pop();
2345 					jrt.setNR(v);
2346 					push(v);
2347 					position.next();
2348 					break;
2349 				}
2350 				case PUSH_NR: {
2351 					push(jrt.getNR());
2352 					position.next();
2353 					break;
2354 				}
2355 				case ASSIGN_FNR: {
2356 					Object v = pop();
2357 					jrt.setFNR(v);
2358 					push(v);
2359 					position.next();
2360 					break;
2361 				}
2362 				case PUSH_FNR: {
2363 					push(jrt.getFNR());
2364 					position.next();
2365 					break;
2366 				}
2367 				case ASSIGN_FS: {
2368 					Object v = pop();
2369 					jrt.setFS(v);
2370 					push(v);
2371 					position.next();
2372 					break;
2373 				}
2374 				case ASSIGN_IGNORECASE: {
2375 					Object v = pop();
2376 					jrt.setIGNORECASE(v);
2377 					push(v);
2378 					position.next();
2379 					break;
2380 				}
2381 				case PUSH_IGNORECASE: {
2382 					push(jrt.getIGNORECASEVar());
2383 					position.next();
2384 					break;
2385 				}
2386 				case PUSH_FS: {
2387 					push(jrt.getFSVar());
2388 					position.next();
2389 					break;
2390 				}
2391 				case ASSIGN_RS: {
2392 					Object v = pop();
2393 					jrt.setRS(v);
2394 					push(v);
2395 					position.next();
2396 					break;
2397 				}
2398 				case PUSH_RS: {
2399 					push(jrt.getRSVar());
2400 					position.next();
2401 					break;
2402 				}
2403 				case ASSIGN_OFS: {
2404 					Object v = pop();
2405 					jrt.setOFS(v);
2406 					push(v);
2407 					position.next();
2408 					break;
2409 				}
2410 				case PUSH_OFS: {
2411 					push(jrt.getOFSVar());
2412 					position.next();
2413 					break;
2414 				}
2415 				case ASSIGN_ORS: {
2416 					Object v = pop();
2417 					jrt.setORS(v);
2418 					push(v);
2419 					position.next();
2420 					break;
2421 				}
2422 				case PUSH_ORS: {
2423 					push(jrt.getORSVar());
2424 					position.next();
2425 					break;
2426 				}
2427 				case ASSIGN_RSTART: {
2428 					Object v = pop();
2429 					jrt.setRSTART(v);
2430 					push(v);
2431 					position.next();
2432 					break;
2433 				}
2434 				case PUSH_RSTART: {
2435 					push(jrt.getRSTART());
2436 					position.next();
2437 					break;
2438 				}
2439 				case ASSIGN_RLENGTH: {
2440 					Object v = pop();
2441 					jrt.setRLENGTH(v);
2442 					push(v);
2443 					position.next();
2444 					break;
2445 				}
2446 				case PUSH_RLENGTH: {
2447 					push(jrt.getRLENGTH());
2448 					position.next();
2449 					break;
2450 				}
2451 				case ASSIGN_FILENAME: {
2452 					Object v = pop();
2453 					jrt.setFILENAMEViaJrt(v);
2454 					push(v == null ? "" : v);
2455 					position.next();
2456 					break;
2457 				}
2458 				case PUSH_FILENAME: {
2459 					push(jrt.getFILENAME());
2460 					position.next();
2461 					break;
2462 				}
2463 				case ASSIGN_ERRNO: {
2464 					Object v = pop();
2465 					jrt.setERRNO(v);
2466 					push(v == null ? "" : v);
2467 					position.next();
2468 					break;
2469 				}
2470 				case PUSH_ERRNO: {
2471 					push(jrt.getERRNO());
2472 					position.next();
2473 					break;
2474 				}
2475 				case ASSIGN_ARGIND: {
2476 					Object v = pop();
2477 					jrt.setARGIND(v);
2478 					push(v == null ? ZERO : v);
2479 					position.next();
2480 					break;
2481 				}
2482 				case PUSH_ARGIND: {
2483 					push(jrt.getARGIND());
2484 					position.next();
2485 					break;
2486 				}
2487 				case ASSIGN_SUBSEP: {
2488 					Object v = pop();
2489 					jrt.setSUBSEP(v);
2490 					push(v);
2491 					position.next();
2492 					break;
2493 				}
2494 				case PUSH_SUBSEP: {
2495 					push(jrt.getSUBSEPVar());
2496 					position.next();
2497 					break;
2498 				}
2499 				case ASSIGN_CONVFMT: {
2500 					Object v = pop();
2501 					jrt.setCONVFMT(v);
2502 					push(v);
2503 					position.next();
2504 					break;
2505 				}
2506 				case PUSH_CONVFMT: {
2507 					push(jrt.getCONVFMTVar());
2508 					position.next();
2509 					break;
2510 				}
2511 				case ASSIGN_OFMT: {
2512 					Object v = pop();
2513 					jrt.setOFMT(v);
2514 					push(v);
2515 					position.next();
2516 					break;
2517 				}
2518 				case PUSH_OFMT: {
2519 					push(getOFMT());
2520 					position.next();
2521 					break;
2522 				}
2523 				case ASSIGN_ARGC: {
2524 					Object v = pop();
2525 					if (argcOffset == NULL_OFFSET) {
2526 						throw new AwkRuntimeException("ARGC is read-only (not materialized).");
2527 					}
2528 					runtimeStack.setVariable(argcOffset, v, true);
2529 					push(v);
2530 					position.next();
2531 					break;
2532 				}
2533 				case PUSH_ARGC: {
2534 					if (argcOffset == NULL_OFFSET) {
2535 						push(getARGC());
2536 					} else {
2537 						push(runtimeStack.getVariable(argcOffset, true));
2538 					}
2539 					position.next();
2540 					break;
2541 				}
2542 				default:
2543 					throw new Error("invalid opcode: " + position.opcode());
2544 				}
2545 				if (profiling) {
2546 					afterProfiledTuple(opcode, tupleStartNanos);
2547 				}
2548 			}
2549 
2550 		} catch (ExitException ee) {
2551 			if (profiling && (opcode == Opcode.EXIT_WITH_CODE || opcode == Opcode.EXIT_WITHOUT_CODE)) {
2552 				afterProfiledTuple(opcode, tupleStartNanos);
2553 			}
2554 			throw ee;
2555 		} catch (IOException ioe) {
2556 			resetCallState();
2557 			throw ioe;
2558 		} catch (RuntimeException re) {
2559 			resetCallState();
2560 			if (re instanceof AwkSandboxException) {
2561 				throw re;
2562 			}
2563 			throw new AwkRuntimeException(position.lineNumber(), re.getMessage(), re);
2564 		} catch (AssertionError ae) {
2565 			resetCallState();
2566 			throw ae;
2567 		}
2568 
2569 		// If <code>exit</code> was called, throw an ExitException
2570 		if (throwExitException) {
2571 			throw new ExitException(exitCode, "The AWK script requested an exit");
2572 		}
2573 	}
2574 
2575 	/**
2576 	 * Clears all collected profiling statistics.
2577 	 */
2578 	public void resetProfiling() {
2579 		if (!profiling) {
2580 			return;
2581 		}
2582 		tupleProfilingStats.clear();
2583 		functionProfilingStats.clear();
2584 		activeProfilingFunctions.clear();
2585 	}
2586 
2587 	/**
2588 	 * Returns an immutable snapshot of the collected profiling statistics.
2589 	 *
2590 	 * @return profiling report snapshot
2591 	 */
2592 	public ProfilingReport getProfilingReport() {
2593 		if (!profiling) {
2594 			return ProfilingReport.empty();
2595 		}
2596 		return new ProfilingReport(tupleProfilingStats, functionProfilingStats);
2597 	}
2598 
2599 	// The exec* helpers below are extracted from executeTuples on purpose:
2600 	// that method must stay well under HotSpot's HugeMethodLimit (8000
2601 	// bytecodes) or the JIT never compiles the interpreter loop (see #562).
2602 
2603 	private void execCompoundAssignVariable(Opcode opcode, VariableTuple variableTuple) {
2604 		// arg[0] = offset
2605 		// arg[1] = isGlobal
2606 		// stack[0] = value
2607 		long offset = variableTuple.getVariableOffset();
2608 		boolean isGlobal = variableTuple.isGlobal();
2609 		Object o1 = blankToZero(resolveVariable(offset, isGlobal, false));
2610 		Object o2 = pop();
2611 		Object ans;
2612 		switch (opcode) {
2613 		case PLUS_EQ:
2614 			ans = JRT.add(o1, o2);
2615 			break;
2616 		case MINUS_EQ:
2617 			ans = JRT.subtract(o1, o2);
2618 			break;
2619 		case MULT_EQ:
2620 			ans = JRT.multiply(o1, o2);
2621 			break;
2622 		case DIV_EQ:
2623 			ans = JRT.divide(o1, o2);
2624 			break;
2625 		case MOD_EQ:
2626 			ans = JRT.mod(o1, o2);
2627 			break;
2628 		case POW_EQ:
2629 			ans = JRT.pow(o1, o2);
2630 			break;
2631 		default:
2632 			throw new Error("Invalid opcode here: " + opcode);
2633 		}
2634 		push(ans);
2635 		runtimeStack.setVariable(offset, ans, isGlobal);
2636 	}
2637 
2638 	private void execCompoundAssignArray(Opcode opcode, VariableTuple variableTuple) {
2639 		// arg[0] = offset
2640 		// arg[1] = isGlobal
2641 		// stack[0] = array index
2642 		// stack[1] = value
2643 		Object arrIdx = pop();
2644 		Object rhs = pop();
2645 		if (rhs == null) {
2646 			rhs = BLANK;
2647 		}
2648 		long offset = variableTuple.getVariableOffset();
2649 		boolean isGlobal = variableTuple.isGlobal();
2650 
2651 		Map<Object, Object> array = ensureMapVariable(offset, isGlobal);
2652 		checkScalar(arrIdx);
2653 		Object o = blankToZero(array.get(arrIdx));
2654 
2655 		Object newVal;
2656 
2657 		switch (opcode) {
2658 		case PLUS_EQ_ARRAY:
2659 			newVal = JRT.add(o, rhs);
2660 			break;
2661 		case MINUS_EQ_ARRAY:
2662 			newVal = JRT.subtract(o, rhs);
2663 			break;
2664 		case MULT_EQ_ARRAY:
2665 			newVal = JRT.multiply(o, rhs);
2666 			break;
2667 		case DIV_EQ_ARRAY:
2668 			newVal = JRT.divide(o, rhs);
2669 			break;
2670 		case MOD_EQ_ARRAY:
2671 			newVal = JRT.mod(o, rhs);
2672 			break;
2673 		case POW_EQ_ARRAY:
2674 			newVal = JRT.pow(o, rhs);
2675 			break;
2676 		default:
2677 			throw new Error("Invalid op code here: " + opcode);
2678 		}
2679 
2680 		assignArray(offset, arrIdx, newVal, isGlobal);
2681 	}
2682 
2683 	private void execCompoundAssignMapElement(Opcode opcode) {
2684 		// stack[0] = array index
2685 		// stack[1] = associative array
2686 		// stack[2] = value
2687 		Object arrIdx = pop();
2688 		Map<Object, Object> array = toMap(pop());
2689 		Object rhs = pop();
2690 		if (rhs == null) {
2691 			rhs = BLANK;
2692 		}
2693 
2694 		checkScalar(arrIdx);
2695 		Object o = blankToZero(array.get(arrIdx));
2696 		Object newVal;
2697 
2698 		switch (opcode) {
2699 		case PLUS_EQ_MAP_ELEMENT:
2700 			newVal = JRT.add(o, rhs);
2701 			break;
2702 		case MINUS_EQ_MAP_ELEMENT:
2703 			newVal = JRT.subtract(o, rhs);
2704 			break;
2705 		case MULT_EQ_MAP_ELEMENT:
2706 			newVal = JRT.multiply(o, rhs);
2707 			break;
2708 		case DIV_EQ_MAP_ELEMENT:
2709 			newVal = JRT.divide(o, rhs);
2710 			break;
2711 		case MOD_EQ_MAP_ELEMENT:
2712 			newVal = JRT.mod(o, rhs);
2713 			break;
2714 		case POW_EQ_MAP_ELEMENT:
2715 			newVal = JRT.pow(o, rhs);
2716 			break;
2717 		default:
2718 			throw new Error("Invalid op code here: " + opcode);
2719 		}
2720 
2721 		assignMapElement(array, arrIdx, newVal);
2722 	}
2723 
2724 	private void execCompoundAssignInputField(Opcode opcode) {
2725 		// stack[0] = dollar_fieldNumber
2726 		// stack[1] = inc value
2727 
2728 		// same code as GET_INPUT_FIELD:
2729 		long fieldnum = JRT.parseFieldNumber(pop());
2730 		Object incval = pop();
2731 
2732 		// except here, get the number, and add the incvalue
2733 		Object numObj = blankToZero(jrt.jrtGetInputField(fieldnum));
2734 		Object num;
2735 		switch (opcode) {
2736 		case PLUS_EQ_INPUT_FIELD:
2737 			num = JRT.add(numObj, incval);
2738 			break;
2739 		case MINUS_EQ_INPUT_FIELD:
2740 			num = JRT.subtract(numObj, incval);
2741 			break;
2742 		case MULT_EQ_INPUT_FIELD:
2743 			num = JRT.multiply(numObj, incval);
2744 			break;
2745 		case DIV_EQ_INPUT_FIELD:
2746 			num = JRT.divide(numObj, incval);
2747 			break;
2748 		case MOD_EQ_INPUT_FIELD:
2749 			num = JRT.mod(numObj, incval);
2750 			break;
2751 		case POW_EQ_INPUT_FIELD:
2752 			num = JRT.pow(numObj, incval);
2753 			break;
2754 		default:
2755 			throw new Error("Invalid opcode here: " + opcode);
2756 		}
2757 		setNumOnJRT(fieldnum, num);
2758 
2759 		// put the result value on the stack
2760 		push(num);
2761 	}
2762 
2763 	private void execIndirectCall(IndirectCallTuple callTuple, PositionTracker position, long tupleStartNanos) {
2764 		Object[] actualArguments = popArguments(callTuple.getNumActualParams());
2765 		String requestedName = jrt.toAwkString(pop());
2766 		String qualifiedName = normalizeIndirectFunctionName(requestedName);
2767 		IndirectFunctionTarget target = callTuple.getUserFunctions().get(qualifiedName);
2768 		if (target != null) {
2769 			long formalCount = target.getNumFormalParams();
2770 			if (actualArguments.length > formalCount) {
2771 				jrt.printWarning(
2772 						"gawk: " + callTuple.getSourceName() + ":" + callTuple.getSourceLine() + ": warning: function `"
2773 								+ qualifiedName + "' called with more arguments than declared");
2774 			}
2775 			if (profiling) {
2776 				activeProfilingFunctions.push(new ActiveFunction(qualifiedName, tupleStartNanos));
2777 			}
2778 			runtimeStack.pushFrame(formalCount, position.currentIndex());
2779 			adoptElementArgumentReferences(actualArguments);
2780 			int copiedArgumentCount = Math.min(actualArguments.length, (int) formalCount);
2781 			for (int i = 0; i < copiedArgumentCount; i++) {
2782 				runtimeStack.setVariable(i, actualArguments[i], false);
2783 			}
2784 			position.jump(target.getAddress());
2785 			return;
2786 		}
2787 
2788 		String awkName = requestedName.startsWith("awk::") ? requestedName.substring("awk::".length()) : requestedName;
2789 		BuiltinFunction builtin = BuiltinFunction.of(awkName);
2790 		if (builtin != null) {
2791 			resolveIndirectArguments(actualArguments, builtin);
2792 			push(invokeIndirectBuiltin(builtin, actualArguments, position.lineNumber()));
2793 			position.next();
2794 			return;
2795 		}
2796 		ExtensionFunction extensionFunction = callTuple.getExtensionFunctions().get(awkName);
2797 		if (extensionFunction != null) {
2798 			resolveIndirectArguments(actualArguments, extensionFunction);
2799 			if (profiling) {
2800 				activeProfilingFunctions.push(new ActiveFunction(awkName, tupleStartNanos));
2801 			}
2802 			try {
2803 				push(invokeExtension(extensionFunction, actualArguments, position.lineNumber()));
2804 			} finally {
2805 				if (profiling) {
2806 					recordFunctionExit(System.nanoTime());
2807 				}
2808 			}
2809 			position.next();
2810 			return;
2811 		}
2812 		throw new AwkRuntimeException(position.lineNumber(), "function `" + qualifiedName + "' is not defined");
2813 	}
2814 
2815 	private void execPrint(CountTuple tuple) throws IOException {
2816 		long numArgs = tuple.getCount();
2817 		if (numArgs == 0) {
2818 			jrt.printRecordDefault();
2819 		} else {
2820 			jrt.printDefault(popArguments(numArgs));
2821 		}
2822 	}
2823 
2824 	private void execPrintToFile(CountAndAppendTuple tuple) throws IOException {
2825 		String key = jrt.toAwkString(pop());
2826 		long numArgs = tuple.getCount();
2827 		jrt.printToFile(
2828 				key,
2829 				tuple.isAppend(),
2830 				numArgs == 0 ? new Object[]
2831 				{ jrt.jrtGetInputField(0) } : popArguments(numArgs));
2832 	}
2833 
2834 	private void execPrintToPipe(CountTuple tuple) throws IOException {
2835 		String cmd = jrt.toAwkString(pop());
2836 		long numArgs = tuple.getCount();
2837 		jrt.printToProcess(cmd, numArgs == 0 ? new Object[] { jrt.jrtGetInputField(0) } : popArguments(numArgs));
2838 	}
2839 
2840 	private void execPrintf(CountTuple tuple) throws IOException {
2841 		long numArgs = tuple.getCount();
2842 		Object[] values = popArguments(numArgs - 1);
2843 		String format = jrt.toAwkString(pop());
2844 		jrt.printfDefault(format, values);
2845 	}
2846 
2847 	private void execPrintfToFile(CountAndAppendTuple tuple) throws IOException {
2848 		String key = jrt.toAwkString(pop());
2849 		long numArgs = tuple.getCount();
2850 		Object[] values = popArguments(numArgs - 1);
2851 		String format = jrt.toAwkString(pop());
2852 		jrt.printfToFile(key, tuple.isAppend(), format, values);
2853 	}
2854 
2855 	private void execPrintfToPipe(CountTuple tuple) throws IOException {
2856 		String cmd = jrt.toAwkString(pop());
2857 		long numArgs = tuple.getCount();
2858 		Object[] values = popArguments(numArgs - 1);
2859 		String format = jrt.toAwkString(pop());
2860 		jrt.printfToProcess(cmd, format, values);
2861 	}
2862 
2863 	private void execLength(CountTuple tuple) {
2864 		long num = tuple.getCount();
2865 		if (num == 0) {
2866 			push(jrt.jrtGetInputField(0).toString().length());
2867 			return;
2868 		}
2869 		Object value = pop();
2870 		if (value instanceof ArgumentReference) {
2871 			value = resolveLengthArgumentReference((ArgumentReference) value);
2872 		}
2873 		push(lengthOf(value));
2874 	}
2875 
2876 	private Object lengthOf(Object value) {
2877 		return value instanceof Map ?
2878 				Long.valueOf(((Map<?, ?>) value).size()) :
2879 				Integer.valueOf(jrt.toAwkString(value).length());
2880 	}
2881 
2882 	private String normalizeIndirectFunctionName(String functionName) {
2883 		return functionName.startsWith("awk::") ? functionName.substring("awk::".length()) : functionName;
2884 	}
2885 
2886 	/**
2887 	 * Unwinds every call frame and clears the per-call runtime state, after an
2888 	 * {@code exit} or {@code nextfile} statement or an abandoned execution.
2889 	 * When profiling, the abandoned function calls are recorded as exited at
2890 	 * this point, so the timing report stays accurate and the active-function
2891 	 * stack does not leak entries (see #557).
2892 	 */
2893 	private void resetCallState() {
2894 		if (profiling) {
2895 			recordAllFunctionExits(System.nanoTime());
2896 		}
2897 		runtimeStack.popAllFrames();
2898 		elementArgumentReferences.clear();
2899 		clearOperandStack();
2900 	}
2901 
2902 	/**
2903 	 * Registers the untyped element references received by the call frame that
2904 	 * was just pushed, so array mutations occurring during the call can detach
2905 	 * them. References forwarded from an enclosing call keep their original
2906 	 * owner.
2907 	 */
2908 	private void adoptElementArgumentReferences(Object[] actualArguments) {
2909 		for (Object argument : actualArguments) {
2910 			adoptElementArgumentReference(argument);
2911 		}
2912 	}
2913 
2914 	private void adoptElementArgumentReference(Object argument) {
2915 		if (argument instanceof IndirectArrayArgumentReference) {
2916 			IndirectArrayArgumentReference reference = (IndirectArrayArgumentReference) argument;
2917 			if (reference.ownerFrame < 0) {
2918 				reference.ownerFrame = runtimeStack.frameCount();
2919 				elementArgumentReferences.push(reference);
2920 			}
2921 		}
2922 	}
2923 
2924 	/**
2925 	 * Drops the element references owned by the call frame that is about to be
2926 	 * popped. Frames are strictly nested, so the owned references always sit on
2927 	 * top of the tracking stack.
2928 	 */
2929 	private void releaseElementArgumentReferences() {
2930 		int depth = runtimeStack.frameCount();
2931 		while (!elementArgumentReferences.isEmpty() && elementArgumentReferences.peek().ownerFrame == depth) {
2932 			elementArgumentReferences.pop();
2933 		}
2934 	}
2935 
2936 	private Object resolveUserFunctionArgument(Object argument) {
2937 		if (!(argument instanceof ArgumentReference)) {
2938 			return argument;
2939 		}
2940 		ArgumentReference reference = (ArgumentReference) argument;
2941 		Object snapshot = reference.snapshot();
2942 		if (snapshot instanceof ArgumentReference) {
2943 			return resolveUserFunctionArgument(snapshot);
2944 		}
2945 		return isUntyped(snapshot) ? reference : snapshot;
2946 	}
2947 
2948 	private void resolveIndirectArguments(Object[] actualArgumentsParam, BuiltinFunction builtin) {
2949 		for (int index = 0; index < actualArgumentsParam.length; index++) {
2950 			boolean arrayArgument = builtin == BuiltinFunction.SPLIT && index == 1;
2951 			actualArgumentsParam[index] = resolveIndirectArgument(actualArgumentsParam[index], arrayArgument, false);
2952 		}
2953 	}
2954 
2955 	private void resolveIndirectArguments(Object[] actualArgumentsParam, ExtensionFunction function) {
2956 		boolean[] arrayArguments = new boolean[actualArgumentsParam.length];
2957 		boolean[] rawValueArguments = new boolean[actualArgumentsParam.length];
2958 		for (int index : function.collectAssocArrayIndexes(actualArgumentsParam.length)) {
2959 			arrayArguments[index] = true;
2960 		}
2961 		for (int index : function.collectRawValueIndexes(actualArgumentsParam.length)) {
2962 			rawValueArguments[index] = true;
2963 		}
2964 		for (int index = 0; index < actualArgumentsParam.length; index++) {
2965 			actualArgumentsParam[index] = resolveIndirectArgument(
2966 					actualArgumentsParam[index],
2967 					arrayArguments[index],
2968 					rawValueArguments[index]);
2969 		}
2970 	}
2971 
2972 	private Object resolveIndirectArgument(Object argument, boolean arrayArgument, boolean rawValueArgument) {
2973 		if (!(argument instanceof ArgumentReference)) {
2974 			return argument;
2975 		}
2976 		ArgumentReference reference = (ArgumentReference) argument;
2977 		if (rawValueArgument) {
2978 			return resolveRawArgumentReference(reference);
2979 		}
2980 		return resolveArgumentReference(reference, arrayArgument);
2981 	}
2982 
2983 	private Object invokeIndirectBuiltin(BuiltinFunction builtin, Object[] args, int lineNumber) {
2984 		switch (builtin) {
2985 		case ATAN2:
2986 			requireIndirectArgumentCount(builtin, args, 2, 2, lineNumber);
2987 			return Math.atan2(JRT.toDouble(args[0]), JRT.toDouble(args[1]));
2988 		case CLOSE:
2989 			requireIndirectArgumentCount(builtin, args, 1, 1, lineNumber);
2990 			return jrt.jrtClose(jrt.toAwkString(args[0]));
2991 		case COS:
2992 			requireIndirectArgumentCount(builtin, args, 1, 1, lineNumber);
2993 			return Math.cos(JRT.toDouble(args[0]));
2994 		case EXP:
2995 			requireIndirectArgumentCount(builtin, args, 1, 1, lineNumber);
2996 			return Math.exp(JRT.toDouble(args[0]));
2997 		case GSUB:
2998 		case SUB:
2999 			requireIndirectArgumentCount(builtin, args, 2, 2, lineNumber);
3000 			return substituteInputLine(builtin == BuiltinFunction.GSUB, args[0], args[1]);
3001 		case INDEX:
3002 			requireIndirectArgumentCount(builtin, args, 2, 2, lineNumber);
3003 			return jrt.index(jrt.toAwkString(args[0]), jrt.toAwkString(args[1]));
3004 		case INT:
3005 			requireIndirectArgumentCount(builtin, args, 1, 1, lineNumber);
3006 			return JRT.truncateToScalar(JRT.toDouble(args[0]));
3007 		case LENGTH:
3008 			requireIndirectArgumentCount(builtin, args, 0, 1, lineNumber);
3009 			return args.length == 0 ? Integer.valueOf(jrt.jrtGetInputField(0).toString().length()) : lengthOf(args[0]);
3010 		case LOG:
3011 			requireIndirectArgumentCount(builtin, args, 1, 1, lineNumber);
3012 			return Math.log(JRT.toDouble(args[0]));
3013 		case MATCH:
3014 			requireIndirectArgumentCount(builtin, args, 2, 2, lineNumber);
3015 			return jrt.matchPosition(jrt.toAwkString(args[0]), jrt.toAwkString(args[1]));
3016 		case RAND:
3017 			requireIndirectArgumentCount(builtin, args, 0, 0, lineNumber);
3018 			return randomNumberGenerator.nextDouble();
3019 		case SIN:
3020 			requireIndirectArgumentCount(builtin, args, 1, 1, lineNumber);
3021 			return Math.sin(JRT.toDouble(args[0]));
3022 		case SPLIT:
3023 			requireIndirectArgumentCount(builtin, args, 2, 3, lineNumber);
3024 			return splitIntoArray(args[0], args[1], args.length == 3 ? args[2] : jrt.getFSVar(), lineNumber);
3025 		case SPRINTF:
3026 			requireIndirectArgumentCount(builtin, args, 1, Integer.MAX_VALUE, lineNumber);
3027 			return jrt.sprintf(jrt.toAwkString(args[0]), Arrays.copyOfRange(args, 1, args.length));
3028 		case SQRT:
3029 			requireIndirectArgumentCount(builtin, args, 1, 1, lineNumber);
3030 			return Math.sqrt(JRT.toDouble(args[0]));
3031 		case SRAND:
3032 			requireIndirectArgumentCount(builtin, args, 0, 1, lineNumber);
3033 			int seed = args.length == 0 ? JRT.timeSeed() : (int) JRT.toDouble(args[0]);
3034 			int previousSeed = randomNumberGenerator.getSeed();
3035 			randomNumberGenerator.setSeed(seed);
3036 			return Integer.valueOf(previousSeed);
3037 		case SUBSTR:
3038 			requireIndirectArgumentCount(builtin, args, 2, 3, lineNumber);
3039 			return substring(args[0], args[1], args.length == 3 ? args[2] : null);
3040 		case SYSTEM:
3041 			requireIndirectArgumentCount(builtin, args, 1, 1, lineNumber);
3042 			return jrt.jrtSystem(jrt.toAwkString(args[0]));
3043 		case TOLOWER:
3044 			requireIndirectArgumentCount(builtin, args, 1, 1, lineNumber);
3045 			return jrt.toAwkString(args[0]).toLowerCase();
3046 		case TOUPPER:
3047 			requireIndirectArgumentCount(builtin, args, 1, 1, lineNumber);
3048 			return jrt.toAwkString(args[0]).toUpperCase();
3049 		default:
3050 			throw new AwkRuntimeException(
3051 					lineNumber,
3052 					"indirect calls are not implemented for builtin `" + builtin.getAwkName() + "'");
3053 		}
3054 	}
3055 
3056 	private void requireIndirectArgumentCount(
3057 			BuiltinFunction builtin,
3058 			Object[] args,
3059 			int minimum,
3060 			int maximum,
3061 			int lineNumber) {
3062 		if (args.length < minimum || args.length > maximum) {
3063 			String expected = minimum == maximum ? Integer.toString(minimum) : minimum + " to " + maximum;
3064 			throw new AwkRuntimeException(
3065 					lineNumber,
3066 					builtin.getAwkName() + " requires " + expected + " argument(s), not " + args.length);
3067 		}
3068 	}
3069 
3070 	private Object invokeExtension(ExtensionFunction function, Object[] args, int lineNumber) {
3071 		// Let extensions report diagnostics at the call location.
3072 		currentLineNumber = lineNumber;
3073 		String extensionClassName = function.getExtensionClassName();
3074 		JawkExtension extension = extensionInstances.get(extensionClassName);
3075 		if (extension == null) {
3076 			throw new AwkRuntimeException(
3077 					lineNumber,
3078 					"Extension instance for class '" + extensionClassName + "' is not registered");
3079 		}
3080 		if (!(extension instanceof AbstractExtension)) {
3081 			throw new AwkRuntimeException(
3082 					lineNumber,
3083 					"Extension instance for class '" + extensionClassName + "' does not extend "
3084 							+ AbstractExtension.class.getName());
3085 		}
3086 		Map<IndirectArrayArgumentReference, Object> attachedValues = captureAttachedArrayArgumentValues();
3087 		Object result;
3088 		try {
3089 			result = function.invoke((AbstractExtension) extension, args);
3090 		} finally {
3091 			detachReplacedArrayArgumentReferences(attachedValues);
3092 		}
3093 		if (result == null) {
3094 			return "";
3095 		}
3096 		if (result instanceof Number || result instanceof String || result instanceof Map) {
3097 			return result;
3098 		}
3099 		return jrt.toAwkString(result);
3100 	}
3101 
3102 	private void execMatch() {
3103 		String ere = jrt.toAwkString(pop());
3104 		String s = jrt.toAwkString(pop());
3105 		push(jrt.matchPosition(s, ere));
3106 	}
3107 
3108 	private void execSubForDollar0(BooleanTuple tuple) {
3109 		Object replacement = pop();
3110 		Object ere = pop();
3111 		push(substituteInputLine(tuple.getValue(), ere, replacement));
3112 	}
3113 
3114 	private Object substituteInputLine(boolean global, Object ere, Object replacement) {
3115 		String orig = jrt.toAwkString(jrt.jrtGetInputField(0));
3116 		Object replacements = global ?
3117 				jrt.replaceAll(orig, jrt.toAwkString(replacement), jrt.toAwkString(ere)) :
3118 				jrt.replaceFirst(orig, jrt.toAwkString(replacement), jrt.toAwkString(ere));
3119 		jrt.setInputLine(jrt.getReplaceResult());
3120 		jrt.jrtParseFields();
3121 		return replacements;
3122 	}
3123 
3124 	private void execSubForDollarReference(BooleanTuple tuple) {
3125 		boolean isGsub = tuple.getValue();
3126 		long fieldNum = JRT.parseFieldNumber(pop());
3127 		String orig = jrt.toAwkString(pop());
3128 		String repl = jrt.toAwkString(pop());
3129 		String ere = jrt.toAwkString(pop());
3130 		push(isGsub ? jrt.replaceAll(orig, repl, ere) : jrt.replaceFirst(orig, repl, ere));
3131 		String newstring = jrt.getReplaceResult();
3132 		if (fieldNum == 0) {
3133 			jrt.setInputLine(newstring);
3134 			jrt.jrtParseFields();
3135 		} else {
3136 			jrt.jrtSetInputField(newstring, fieldNum);
3137 		}
3138 	}
3139 
3140 	private void execSubForVariable(SubstitutionVariableTuple tuple, PositionTracker position) {
3141 		String newString = execSubOrGSub(tuple.isGlobalSubstitution());
3142 		assign(tuple.getVariableOffset(), newString, tuple.isGlobal(), position, false);
3143 	}
3144 
3145 	private void execSubForArrayReference(SubstitutionVariableTuple tuple) {
3146 		Object arrIdx = pop();
3147 		String newString = execSubOrGSub(tuple.isGlobalSubstitution());
3148 		assignArray(tuple.getVariableOffset(), arrIdx, newString, tuple.isGlobal());
3149 		pop();
3150 	}
3151 
3152 	private void execSubForMapReference(BooleanTuple tuple) {
3153 		Object arrIdx = pop();
3154 		Map<Object, Object> array = toMap(pop());
3155 		String newString = execSubOrGSub(tuple.getValue());
3156 		assignMapElement(array, arrIdx, newString);
3157 		pop();
3158 	}
3159 
3160 	private void execSplit(CountTuple tuple, PositionTracker position) {
3161 		long numArgs = tuple.getCount();
3162 		Object fs;
3163 		if (numArgs == 2) {
3164 			fs = jrt.getFSVar();
3165 		} else if (numArgs == 3) {
3166 			// a regexp literal arrives precompiled and stays that way, so the
3167 			// tokenizer can honor IGNORECASE through the pattern-twin cache
3168 			fs = pop();
3169 		} else {
3170 			throw new Error("Invalid # of args. split() requires 2 or 3. Got: " + numArgs);
3171 		}
3172 		Object target = pop();
3173 		Object source = pop();
3174 		push(splitIntoArray(source, target, fs, position.lineNumber()));
3175 	}
3176 
3177 	private Object splitIntoArray(Object source, Object target, Object separator, int lineNumber) {
3178 		if (!(target instanceof Map)) {
3179 			throw new AwkRuntimeException(lineNumber, target + " is not an array.");
3180 		}
3181 		Enumeration<Object> tokenizer = jrt.splitTokenizer(jrt.toAwkString(source), separator);
3182 		@SuppressWarnings("unchecked")
3183 		Map<Object, Object> assocArray = (Map<Object, Object>) target;
3184 		assocArray.clear();
3185 		detachMissingArrayArgumentReferences(assocArray);
3186 		long cnt = 0;
3187 		while (tokenizer.hasMoreElements()) {
3188 			Object value = tokenizer.nextElement();
3189 			assocArray.put(++cnt, jrt.toInputScalar(value));
3190 		}
3191 		return Long.valueOf(cnt);
3192 	}
3193 
3194 	private void execSubstr(CountTuple tuple) {
3195 		long numArgs = tuple.getCount();
3196 		Object length = null;
3197 		if (numArgs == 3) {
3198 			length = pop();
3199 		} else if (numArgs != 2) {
3200 			throw new Error("numArgs for SUBSTR must be 2 or 3. It is " + numArgs);
3201 		}
3202 		Object start = pop();
3203 		Object value = pop();
3204 		push(substring(value, start, length));
3205 	}
3206 
3207 	private Object substring(Object value, Object start, Object requestedLength) {
3208 		String s = jrt.toAwkString(value);
3209 		int startPos = (int) JRT.toDouble(start);
3210 		if (startPos <= 0) {
3211 			startPos = 1;
3212 		}
3213 		if (startPos > s.length()) {
3214 			return BLANK;
3215 		}
3216 		int available = s.length() - startPos + 1;
3217 		// The requested length is kept as a long and clamped to what the string
3218 		// can supply: a length beyond the int range would otherwise wrap around
3219 		// and turn "the rest of the string" into an empty result.
3220 		long length = requestedLength == null ? available : JRT.toLong(requestedLength);
3221 		if (length <= 0) {
3222 			return BLANK;
3223 		}
3224 		if (length > available) {
3225 			length = available;
3226 		}
3227 		return s.substring(startPos - 1, startPos - 1 + (int) length);
3228 	}
3229 
3230 	private void execSetNumGlobals(CountTuple tuple) {
3231 		long numGlobals = tuple.getCount();
3232 		Object[] globals = runtimeStack.getNumGlobals();
3233 		if (mergedGlobalLayoutActive) {
3234 			if (!hasCompatiblePersistentGlobalLayout(numGlobals)) {
3235 				throw new IllegalStateException("AVM globals are already initialized for an incompatible persistent layout.");
3236 			}
3237 			applyExecutionInitialVariablesToGlobalSlots(true);
3238 		} else if (globals == null) {
3239 			runtimeStack.setNumGlobals(numGlobals, globalVariableOffsets);
3240 			initializedEvalGlobalVariableOffsets = globalVariableOffsets;
3241 			initializedEvalGlobalVariableArrays = globalVariableArrays;
3242 			applyExecutionInitialVariablesToGlobalSlots(false);
3243 		} else if (!hasCompatibleEvalGlobalLayout(numGlobals)) {
3244 			throw new IllegalStateException(
3245 					"AVM globals are already initialized for a different eval layout. Call prepareForEval(...) first.");
3246 		}
3247 	}
3248 
3249 	private void populateEnviron(long offset) {
3250 		environOffset = offset;
3251 		for (Map.Entry<String, String> var : System.getenv().entrySet()) {
3252 			assignArray(environOffset, var.getKey(), jrt.toInputScalar(var.getValue()), true);
3253 			pop(); // clean up the stack after the assignment
3254 		}
3255 	}
3256 
3257 	private void populateArgc(long offset) {
3258 		argcOffset = offset;
3259 		// +1 to include the "jawk" program name (ARGV[0])
3260 		runtimeStack.setVariable(argcOffset, Integer.valueOf(arguments.size() + 1), true);
3261 	}
3262 
3263 	private void populateArgv(long offset) {
3264 		argvOffset = offset;
3265 		// A host-supplied ARGV takes precedence over the operand list: leave
3266 		// it untouched instead of overwriting its entries.
3267 		Object existing = runtimeStack.getVariable(argvOffset, true);
3268 		if (existing instanceof Map && !((Map<?, ?>) existing).isEmpty()) {
3269 			return;
3270 		}
3271 		forEachArgvEntry((index, value) -> {
3272 			assignArray(argvOffset, index, value, true);
3273 			pop(); // clean up the stack after the assignment
3274 		});
3275 	}
3276 
3277 	/**
3278 	 * Supplies the ARGV entries to the given consumer, in index order:
3279 	 * ARGV[0] is the program name, ARGV[1..n] the command-line arguments.
3280 	 * Indexes are supplied as {@code Long}, the canonical AWK array key
3281 	 * form. The count comes straight from the argument list because ARGC
3282 	 * may not be materialized when the script does not reference it.
3283 	 *
3284 	 * @param consumer receives each (index, value) ARGV entry
3285 	 */
3286 	private void forEachArgvEntry(BiConsumer<Long, Object> consumer) {
3287 		consumer.accept(Long.valueOf(0L), "jawk");
3288 		for (int i = 1; i <= arguments.size(); i++) {
3289 			consumer.accept(Long.valueOf(i), jrt.toInputScalar(arguments.get(i - 1)));
3290 		}
3291 	}
3292 
3293 	/*
3294 	 * Extension initialization can be heavy, so the hooks run at most once per
3295 	 * AVM instance: a reused AVM (repeated executions, expression evaluations)
3296 	 * does not reinitialize its extensions.
3297 	 */
3298 	private void runBeforeStartHooks() {
3299 		if (beforeStartHooksExecuted || extensionInstances.isEmpty()) {
3300 			return;
3301 		}
3302 		beforeStartHooksExecuted = true;
3303 		Set<JawkExtension> started = new LinkedHashSet<JawkExtension>();
3304 		for (JawkExtension extension : extensionInstances.values()) {
3305 			if (started.add(extension)) {
3306 				extension.beforeStart(this, jrt);
3307 			}
3308 		}
3309 	}
3310 
3311 	/*
3312 	 * SYMTAB is a gawk extension mirroring the symbol table: the script's
3313 	 * globals, the JRT-managed specials, and host-supplied variables that have
3314 	 * no compiled slot. The parser emits UPDATE_SYMTAB only when the script
3315 	 * references SYMTAB outside POSIX mode. Declared globals and managed special
3316 	 * variables remain linked to their runtime values.
3317 	 */
3318 	private void execUpdateSymtab(long offset) {
3319 		symtabOffset = offset;
3320 		if (runtimeStack.getVariable(offset, true) != null) {
3321 			// a host-supplied SYMTAB value wins
3322 			return;
3323 		}
3324 		SymtabArray symtab = new SymtabArray();
3325 		for (String name : executionInitialVariables.keySet()) {
3326 			symtab.put(name, getVariable(name));
3327 		}
3328 		for (String name : getGlobalVariableNames()) {
3329 			// gawk keeps the meta tables out of the symbol table snapshot
3330 			if ("SYMTAB".equals(name) || "FUNCTAB".equals(name)) {
3331 				continue;
3332 			}
3333 			symtab.put(name, getVariable(name));
3334 		}
3335 		// specials last: their accessors are authoritative even when the name
3336 		// also has a (possibly not yet materialized) global slot
3337 		for (String name : getSpecialVariableNames()) {
3338 			symtab.put(name, getVariable(name));
3339 		}
3340 		symtab.activate();
3341 		runtimeStack.setVariable(offset, symtab, true);
3342 	}
3343 
3344 	private final class SymtabArray extends java.util.AbstractMap<Object, Object> implements AssocArray {
3345 		private final Map<Object, Object> entries = newAwkArray();
3346 		private final Set<String> assignableNames = new HashSet<String>();
3347 		private boolean active;
3348 
3349 		private void activate() {
3350 			active = true;
3351 		}
3352 
3353 		/** {@inheritDoc} */
3354 		@Override
3355 		public Object get(Object key) {
3356 			String name = key == null ? "" : key.toString();
3357 			if (active && !isMetaTableName(name) && isLiveSpecialVariable(name)) {
3358 				return getVariable(name);
3359 			}
3360 			Integer offset = globalVariableOffsets == null ? null : globalVariableOffsets.get(name);
3361 			if (active && !isMetaTableName(name) && offset != null) {
3362 				return runtimeStack.getVariable(offset.intValue(), true);
3363 			}
3364 			return entries.get(key);
3365 		}
3366 
3367 		/** {@inheritDoc} */
3368 		@Override
3369 		public boolean containsKey(Object key) {
3370 			String name = key == null ? "" : key.toString();
3371 			return active
3372 					&& !isMetaTableName(name)
3373 					&& (isLiveSpecialVariable(name) || (globalVariableOffsets != null && globalVariableOffsets.containsKey(name)))
3374 					|| entries.containsKey(key);
3375 		}
3376 
3377 		/** {@inheritDoc} */
3378 		@Override
3379 		public Object put(Object key, Object value) {
3380 			String name = key == null ? "" : key.toString();
3381 			if (!active) {
3382 				if (key != null) {
3383 					assignableNames.add(name);
3384 				}
3385 				return entries.put(key, value);
3386 			}
3387 			if (!assignableNames.contains(name)) {
3388 				throw new AwkRuntimeException("Cannot assign to an arbitrary element of SYMTAB.");
3389 			}
3390 			validateGlobalType(name, value);
3391 			Object previous = entries.put(key, value);
3392 			if (isMetaTableName(name)) {
3393 				return previous;
3394 			}
3395 			if (applyLiveSpecialVariable(name, value)) {
3396 				return previous;
3397 			}
3398 			Integer offset = globalVariableOffsets == null ? null : globalVariableOffsets.get(name);
3399 			if (offset != null) {
3400 				runtimeStack.setVariable(offset.intValue(), value, true);
3401 			}
3402 			return previous;
3403 		}
3404 
3405 		private Object putRuntimeVariable(String name, Object value) {
3406 			assignableNames.add(name);
3407 			return put(name, value);
3408 		}
3409 
3410 		private boolean applyLiveSpecialVariable(String name, Object value) {
3411 			if ("ARGC".equals(name)) {
3412 				if (argcOffset == NULL_OFFSET) {
3413 					throw new AwkRuntimeException("ARGC is read-only (not materialized).");
3414 				}
3415 				runtimeStack.setVariable(argcOffset, value, true);
3416 				return true;
3417 			}
3418 			if ("RSTART".equals(name)) {
3419 				jrt.setRSTART(value);
3420 				return true;
3421 			}
3422 			if ("RLENGTH".equals(name)) {
3423 				jrt.setRLENGTH(value);
3424 				return true;
3425 			}
3426 			return isManagedSpecialVariable(name) && jrt.applySpecialVariable(name, value);
3427 		}
3428 
3429 		private boolean isLiveSpecialVariable(String name) {
3430 			return isManagedSpecialVariable(name) || "RSTART".equals(name) || "RLENGTH".equals(name);
3431 		}
3432 
3433 		/** {@inheritDoc} */
3434 		@Override
3435 		public Object remove(Object key) {
3436 			if (active) {
3437 				throw new AwkRuntimeException("Cannot delete an element from SYMTAB.");
3438 			}
3439 			return entries.remove(key);
3440 		}
3441 
3442 		/** {@inheritDoc} */
3443 		@Override
3444 		public void clear() {
3445 			if (active) {
3446 				throw new AwkRuntimeException("Cannot delete SYMTAB.");
3447 			}
3448 			entries.clear();
3449 		}
3450 
3451 		private void validateGlobalType(String name, Object value) {
3452 			Boolean array = globalVariableArrays == null ? null : globalVariableArrays.get(name);
3453 			if (Boolean.TRUE.equals(array) && !(value instanceof Map)) {
3454 				throw new AwkRuntimeException("Attempting to use array `" + name + "' in a scalar context.");
3455 			}
3456 			if (Boolean.FALSE.equals(array) && value instanceof Map) {
3457 				throw new AwkRuntimeException("Attempting to use scalar `" + name + "' as an array.");
3458 			}
3459 		}
3460 
3461 		/** {@inheritDoc} */
3462 		@Override
3463 		public Set<Map.Entry<Object, Object>> entrySet() {
3464 			return new AbstractSet<Map.Entry<Object, Object>>() {
3465 
3466 				@Override
3467 				public Iterator<Map.Entry<Object, Object>> iterator() {
3468 					final Iterator<Map.Entry<Object, Object>> iterator = entries.entrySet().iterator();
3469 					return new Iterator<Map.Entry<Object, Object>>() {
3470 
3471 						@Override
3472 						public boolean hasNext() {
3473 							return iterator.hasNext();
3474 						}
3475 
3476 						@Override
3477 						public Map.Entry<Object, Object> next() {
3478 							return new LiveSymtabEntry(iterator.next().getKey());
3479 						}
3480 
3481 						@Override
3482 						public void remove() {
3483 							if (active) {
3484 								throw new AwkRuntimeException("Cannot delete an element from SYMTAB.");
3485 							}
3486 							iterator.remove();
3487 						}
3488 					};
3489 				}
3490 
3491 				@Override
3492 				public int size() {
3493 					return entries.size();
3494 				}
3495 			};
3496 		}
3497 
3498 		private boolean isMetaTableName(String name) {
3499 			return "SYMTAB".equals(name) || "FUNCTAB".equals(name);
3500 		}
3501 
3502 		private final class LiveSymtabEntry implements Map.Entry<Object, Object> {
3503 
3504 			private final Object key;
3505 
3506 			private LiveSymtabEntry(Object key) {
3507 				this.key = key;
3508 			}
3509 
3510 			@Override
3511 			public Object getKey() {
3512 				return key;
3513 			}
3514 
3515 			@Override
3516 			public Object getValue() {
3517 				return SymtabArray.this.get(key);
3518 			}
3519 
3520 			@Override
3521 			public Object setValue(Object value) {
3522 				return SymtabArray.this.put(key, value);
3523 			}
3524 
3525 			@Override
3526 			public boolean equals(Object obj) {
3527 				if (!(obj instanceof Map.Entry)) {
3528 					return false;
3529 				}
3530 				Map.Entry<?, ?> other = (Map.Entry<?, ?>) obj;
3531 				return Objects.equals(key, other.getKey()) && Objects.equals(getValue(), other.getValue());
3532 			}
3533 
3534 			@Override
3535 			public int hashCode() {
3536 				return Objects.hashCode(key) ^ Objects.hashCode(getValue());
3537 			}
3538 		}
3539 	}
3540 
3541 	/*
3542 	 * FUNCTAB is a gawk extension listing the names of the standard built-in
3543 	 * functions, the program's user-defined functions, and the loaded
3544 	 * extensions' function keywords. The parser emits UPDATE_FUNCTAB only when
3545 	 * the script references FUNCTAB outside POSIX mode.
3546 	 */
3547 	private void execUpdateFunctab(long offset) {
3548 		if (runtimeStack.getVariable(offset, true) != null) {
3549 			return;
3550 		}
3551 		Map<Object, Object> functab = newAwkArray();
3552 		for (String name : BuiltinFunction.names()) {
3553 			functab.put(name, name);
3554 		}
3555 		for (String name : functionNames) {
3556 			functab.put(name, name);
3557 		}
3558 		Set<JawkExtension> seen = new LinkedHashSet<JawkExtension>();
3559 		for (JawkExtension extension : extensionInstances.values()) {
3560 			if (seen.add(extension)) {
3561 				for (String keyword : extension.getExtensionFunctions().keySet()) {
3562 					functab.put(keyword, keyword);
3563 				}
3564 			}
3565 		}
3566 		runtimeStack.setVariable(offset, new ReadOnlyArray("FUNCTAB", functab), true);
3567 	}
3568 
3569 	private static final class ReadOnlyArray extends java.util.AbstractMap<Object, Object> implements AssocArray {
3570 		private final String name;
3571 		private final Map<Object, Object> entries;
3572 
3573 		private ReadOnlyArray(String nameParam, Map<Object, Object> entriesParam) {
3574 			name = nameParam;
3575 			entries = entriesParam;
3576 		}
3577 
3578 		/** {@inheritDoc} */
3579 		@Override
3580 		public Object get(Object key) {
3581 			return JRT.containsAwkKey(entries, key) ? entries.get(key) : BLANK;
3582 		}
3583 
3584 		/** {@inheritDoc} */
3585 		@Override
3586 		public boolean containsKey(Object key) {
3587 			return JRT.containsAwkKey(entries, key);
3588 		}
3589 
3590 		/** {@inheritDoc} */
3591 		@Override
3592 		public Set<Map.Entry<Object, Object>> entrySet() {
3593 			return Collections.unmodifiableMap(entries).entrySet();
3594 		}
3595 
3596 		/** {@inheritDoc} */
3597 		@Override
3598 		public Object put(Object key, Object value) {
3599 			throw readOnlyError();
3600 		}
3601 
3602 		/** {@inheritDoc} */
3603 		@Override
3604 		public Object remove(Object key) {
3605 			throw readOnlyError();
3606 		}
3607 
3608 		/** {@inheritDoc} */
3609 		@Override
3610 		public void clear() {
3611 			throw readOnlyError();
3612 		}
3613 
3614 		private AwkRuntimeException readOnlyError() {
3615 			return new AwkRuntimeException(name + " is read-only.");
3616 		}
3617 	}
3618 
3619 	/** Reflects a command-line variable assignment into a materialized SYMTAB. */
3620 	private void updateSymtabEntry(String name, Object value) {
3621 		if (symtabOffset == NULL_OFFSET) {
3622 			return;
3623 		}
3624 		Object symtab = runtimeStack.getVariable(symtabOffset, true);
3625 		if (symtab instanceof SymtabArray) {
3626 			((SymtabArray) symtab).putRuntimeVariable(name, value);
3627 		} else if (symtab instanceof Map) {
3628 			@SuppressWarnings("unchecked")
3629 			Map<Object, Object> symtabMap = (Map<Object, Object>) symtab;
3630 			symtabMap.put(name, value);
3631 		}
3632 	}
3633 
3634 	private void execApplySubsep(CountTuple tuple) {
3635 		long count = tuple.getCount();
3636 		if (count == 1) {
3637 			Object value = pop();
3638 			checkScalar(value);
3639 			// An integral number is already in the form the array
3640 			// implementations canonicalize to a Long key, so it skips the
3641 			// CONVFMT string round-trip; every other scalar converts to a
3642 			// string with the CONVFMT currently in effect, fixing the key
3643 			// from that point on.
3644 			push(AssocArray.isIntegralNumberKey(value) ? value : jrt.toAwkString(value));
3645 			return;
3646 		}
3647 		StringBuilder sb = new StringBuilder();
3648 		Object value = pop();
3649 		checkScalar(value);
3650 		sb.append(jrt.toAwkString(value));
3651 		String subsep = jrt.toAwkString(jrt.getSUBSEPVar());
3652 		for (int i = 1; i < count; i++) {
3653 			sb.insert(0, subsep);
3654 			value = pop();
3655 			checkScalar(value);
3656 			sb.insert(0, jrt.toAwkString(value));
3657 		}
3658 		push(sb.toString());
3659 	}
3660 
3661 	private long beforeProfiledTuple(Tuple tuple, Opcode opcode) {
3662 		long now = System.nanoTime();
3663 		if (opcode == Opcode.CALL_FUNCTION) {
3664 			CallFunctionTuple callTuple = (CallFunctionTuple) tuple;
3665 			activeProfilingFunctions.push(new ActiveFunction(callTuple.getFunctionName(), now));
3666 		} else if (opcode == Opcode.EXTENSION) {
3667 			ExtensionTuple extensionTuple = (ExtensionTuple) tuple;
3668 			ExtensionFunction function = extensionTuple.getFunction();
3669 			activeProfilingFunctions.push(new ActiveFunction(function.getKeyword(), now));
3670 		}
3671 		return now;
3672 	}
3673 
3674 	private void afterProfiledTuple(Opcode opcode, long tupleStartNanos) {
3675 		long now = System.nanoTime();
3676 		statisticsFor(tupleProfilingStats, opcode).add(now - tupleStartNanos);
3677 		if (opcode == Opcode.EXIT_WITH_CODE || opcode == Opcode.EXIT_WITHOUT_CODE) {
3678 			recordAllFunctionExits(now);
3679 		} else if (opcode == Opcode.EXTENSION || opcode == Opcode.RETURN_FROM_FUNCTION) {
3680 			recordFunctionExit(now);
3681 		}
3682 	}
3683 
3684 	private static <K> ProfilingReport.Accumulator statisticsFor(Map<K, ProfilingReport.Accumulator> stats, K key) {
3685 		ProfilingReport.Accumulator accumulator = stats.get(key);
3686 		if (accumulator == null) {
3687 			accumulator = new ProfilingReport.Accumulator();
3688 			stats.put(key, accumulator);
3689 		}
3690 		return accumulator;
3691 	}
3692 
3693 	private void recordFunctionExit(long now) {
3694 		if (activeProfilingFunctions.isEmpty()) {
3695 			return;
3696 		}
3697 		ActiveFunction function = activeProfilingFunctions.pop();
3698 		statisticsFor(functionProfilingStats, function.name).add(now - function.startNanos);
3699 	}
3700 
3701 	private void recordAllFunctionExits(long now) {
3702 		while (!activeProfilingFunctions.isEmpty()) {
3703 			recordFunctionExit(now);
3704 		}
3705 	}
3706 
3707 	private static final class ActiveFunction {
3708 		private final String name;
3709 		private final long startNanos;
3710 
3711 		private ActiveFunction(String name, long startNanos) {
3712 			this.name = name;
3713 			this.startNanos = startNanos;
3714 		}
3715 	}
3716 
3717 	/**
3718 	 * Releases any prepared input source and runtime I/O resources owned by this
3719 	 * AVM.
3720 	 * <p>
3721 	 * Call this when you are done with an AVM obtained through expert-level
3722 	 * integration, or after direct {@link #eval(AwkExpression, InputSource)} /
3723 	 * {@link #execute(AwkProgram, InputSource)} usage.
3724 	 * The AVM may be prepared again afterwards, but callers should treat a closed
3725 	 * instance as end-of-use unless they intentionally reinitialize it.
3726 	 * </p>
3727 	 */
3728 	@Override
3729 	public void close() throws IOException {
3730 		jrt.jrtCloseAll();
3731 		closeResolvedInputSource();
3732 		resolvedInputSource = null;
3733 	}
3734 
3735 	/**
3736 	 * Close the resolved {@link InputSource} if it implements {@link Closeable}.
3737 	 * This is used by {@link #close()} and by explicit rebind operations such as
3738 	 * {@link #prepareForEval(InputSource)} when the AVM switches to a different
3739 	 * source instance.
3740 	 */
3741 	private void closeResolvedInputSource() {
3742 		closeInputSource(resolvedInputSource);
3743 	}
3744 
3745 	private void closeInputSource(InputSource inputSource) {
3746 		if (!(inputSource instanceof Closeable)) {
3747 			return;
3748 		}
3749 		try {
3750 			((Closeable) inputSource).close();
3751 		} catch (IOException ignored) {
3752 			// Best-effort close.
3753 		}
3754 	}
3755 
3756 	private Object[] popArguments(long numArgs) {
3757 		int count = (int) numArgs;
3758 		Object[] args = new Object[count];
3759 		operandStackSize -= count;
3760 		System.arraycopy(operandStack, operandStackSize, args, 0, count);
3761 		Arrays.fill(operandStack, operandStackSize, operandStackSize + count, null);
3762 		return args;
3763 	}
3764 
3765 	/**
3766 	 * sprintf() functionality
3767 	 */
3768 	private String sprintfFunction(long numArgs) {
3769 		Object[] argArray = popArguments(numArgs - 1);
3770 		String fmt = jrt.toAwkString(pop());
3771 		return jrt.sprintf(fmt, argArray);
3772 	}
3773 
3774 	private void setNumOnJRT(long fieldNum, Object num) {
3775 		// same code as ASSIGN_AS_INPUT_FIELD: a field retains the numeric
3776 		// scalar itself (so an exact integer survives repeated arithmetic),
3777 		// while $0 is record text and converts with CONVFMT immediately
3778 		if (fieldNum == 0) {
3779 			jrt.setInputLine(jrt.toAwkString(num));
3780 			jrt.jrtParseFields();
3781 		} else {
3782 			jrt.jrtSetInputField(num, fieldNum);
3783 		}
3784 	}
3785 
3786 	private String execSubOrGSub(boolean isGsub) {
3787 		String newString;
3788 
3789 		// stack[0] = original field value
3790 		// stack[1] = replacement string
3791 		// stack[2] = ere
3792 		String orig = jrt.toAwkString(pop());
3793 		String repl = jrt.toAwkString(pop());
3794 		String ere = jrt.toAwkString(pop());
3795 		push(isGsub ? jrt.replaceAll(orig, repl, ere) : jrt.replaceFirst(orig, repl, ere));
3796 		newString = jrt.getReplaceResult();
3797 
3798 		return newString;
3799 	}
3800 
3801 	/**
3802 	 * Awk variable assignment functionality.
3803 	 */
3804 	private void assign(long l, Object value, boolean isGlobal, PositionTracker position, boolean push) {
3805 		value = JRT.untypedToBlank(value);
3806 		checkScalar(value);
3807 		// check if curr value already refers to an array
3808 		if (resolveVariable(l, isGlobal, false) instanceof Map) {
3809 			throw new AwkRuntimeException(position.lineNumber(), "Attempting to use an array in a scalar context.");
3810 		}
3811 		if (push) {
3812 			push(value);
3813 		}
3814 		runtimeStack.setVariable(l, value, isGlobal);
3815 		// When specials are compiled correctly, they use ASSIGN_* and skip this path.
3816 	}
3817 
3818 	/**
3819 	 * Awk array element assignment functionality.
3820 	 */
3821 	private void assignArray(long offset, Object arrIdx, Object rhs, boolean isGlobal) {
3822 		assignMapElement(ensureMapVariable(offset, isGlobal), arrIdx, rhs);
3823 	}
3824 
3825 	private void assignMapElement(Map<Object, Object> array, Object arrIdx, Object rhs) {
3826 		checkScalar(arrIdx);
3827 		rhs = JRT.untypedToBlank(rhs);
3828 		checkScalar(rhs);
3829 		if (JRT.containsAwkKey(array, arrIdx) && JRT.getAssocArrayValue(array, arrIdx) instanceof Map) {
3830 			throw new AwkRuntimeException("Attempting to use an array in a scalar context.");
3831 		}
3832 		array.put(arrIdx, rhs);
3833 		push(rhs);
3834 	}
3835 
3836 	/**
3837 	 * Numerically increases an Awk variable by one; the result
3838 	 * is placed back into that variable.
3839 	 */
3840 	/**
3841 	 * Replaces a missing or uninitialized array element by numeric zero, so
3842 	 * that {@code ++}/{@code --} on it starts from an exact integer, the same
3843 	 * way {@link #inc(long, boolean)} treats an uninitialized variable.
3844 	 */
3845 	private static Object blankToZero(Object o) {
3846 		return o == null || o instanceof UninitializedObject ? ZERO : o;
3847 	}
3848 
3849 	/**
3850 	 * Coerces a scalar to its numeric value, keeping an already numeric
3851 	 * scalar unchanged so an exact integer is not rounded through a double.
3852 	 */
3853 	private static Object numericValueOf(Object o) {
3854 		if (o instanceof Long || o instanceof Integer || o instanceof Double) {
3855 			return o;
3856 		}
3857 		return JRT.toDouble(o);
3858 	}
3859 
3860 	private Object inc(long l, boolean isGlobal) {
3861 		Object o = resolveVariable(l, isGlobal, false);
3862 		if (o instanceof UninitializedObject) {
3863 			o = ZERO;
3864 			runtimeStack.setVariable(l, o, isGlobal);
3865 		}
3866 		Object updated = JRT.inc(o);
3867 		runtimeStack.setVariable(l, updated, isGlobal);
3868 		return o;
3869 	}
3870 
3871 	/**
3872 	 * Numerically decreases an Awk variable by one; the result
3873 	 * is placed back into that variable.
3874 	 */
3875 	private Object dec(long l, boolean isGlobal) {
3876 		Object o = resolveVariable(l, isGlobal, false);
3877 		if (o instanceof UninitializedObject) {
3878 			o = ZERO;
3879 			runtimeStack.setVariable(l, o, isGlobal);
3880 		}
3881 		Object updated = JRT.dec(o);
3882 		runtimeStack.setVariable(l, updated, isGlobal);
3883 		return o;
3884 	}
3885 
3886 	/** {@inheritDoc} */
3887 	@Override
3888 	public final Object getRS() {
3889 		return jrt.getRSVar();
3890 	}
3891 
3892 	/** {@inheritDoc} */
3893 	@Override
3894 	public final Object getOFS() {
3895 		return jrt.getOFSVar();
3896 	}
3897 
3898 	/** {@inheritDoc} */
3899 	@Override
3900 	public final Object getORS() {
3901 		return jrt.getORSVar();
3902 	}
3903 
3904 	/** {@inheritDoc} */
3905 	@Override
3906 	public final Object getSUBSEP() {
3907 		return jrt.getSUBSEPVar();
3908 	}
3909 
3910 	/**
3911 	 * Returns the names of the global variables declared by the compiled
3912 	 * program.
3913 	 *
3914 	 * @return unmodifiable set of global variable names, empty when no program
3915 	 *         metadata is installed
3916 	 */
3917 	public Set<String> getGlobalVariableNames() {
3918 		return globalVariableOffsets == null ?
3919 				Collections.<String>emptySet() :
3920 				Collections.unmodifiableSet(globalVariableOffsets.keySet());
3921 	}
3922 
3923 	/**
3924 	 * Returns the names of the user-defined functions of the compiled program.
3925 	 *
3926 	 * @return unmodifiable set of function names, empty when no program metadata
3927 	 *         is installed
3928 	 */
3929 	public Set<String> getFunctionNames() {
3930 		return functionNames == null ? Collections.<String>emptySet() : Collections.unmodifiableSet(functionNames);
3931 	}
3932 
3933 	/**
3934 	 * The names of the special variables the interpreter answers by name.
3935 	 * Must stay in sync with the switch in {@link #getVariable(String)}; a
3936 	 * unit test verifies that every listed name is answered.
3937 	 */
3938 	private static final Set<String> SPECIAL_VARIABLE_NAMES = Collections.unmodifiableSet(
3939 			new LinkedHashSet<String>(
3940 					Arrays.asList(
3941 							"FS",
3942 							"RS",
3943 							"OFS",
3944 							"ORS",
3945 							"FILENAME",
3946 							"SUBSEP",
3947 							"CONVFMT",
3948 							"OFMT",
3949 							"NF",
3950 							"NR",
3951 							"FNR",
3952 							"RSTART",
3953 							"RLENGTH",
3954 							"IGNORECASE",
3955 							"ERRNO",
3956 							"ARGIND",
3957 							"ARGC",
3958 							"ARGV")));
3959 
3960 	/**
3961 	 * Returns the names of the special variables that
3962 	 * {@link #getVariable(String)} answers directly.
3963 	 *
3964 	 * @return unmodifiable set of special variable names
3965 	 */
3966 	public Set<String> getSpecialVariableNames() {
3967 		return SPECIAL_VARIABLE_NAMES;
3968 	}
3969 
3970 	/** {@inheritDoc} */
3971 	@Override
3972 	public final Object getVariable(String name) {
3973 		if (name == null) {
3974 			return null;
3975 		}
3976 		switch (name) {
3977 		case "FS":
3978 			return getFS();
3979 		case "RS":
3980 			return getRS();
3981 		case "OFS":
3982 			return getOFS();
3983 		case "ORS":
3984 			return getORS();
3985 		case "FILENAME":
3986 			return jrt.getFILENAME();
3987 		case "SUBSEP":
3988 			return getSUBSEP();
3989 		case "CONVFMT":
3990 			return getCONVFMT();
3991 		case "OFMT":
3992 			return jrt.getOFMTString();
3993 		case "NF":
3994 			return jrt.getNF();
3995 		case "NR":
3996 			return jrt.getNR();
3997 		case "FNR":
3998 			return jrt.getFNR();
3999 		case "RSTART":
4000 			return jrt.getRSTART();
4001 		case "RLENGTH":
4002 			return jrt.getRLENGTH();
4003 		case "IGNORECASE":
4004 			return jrt.getIGNORECASEVar();
4005 		case "ERRNO":
4006 			if (isManagedSpecialVariable(name)) {
4007 				return jrt.getERRNO();
4008 			}
4009 			// POSIX mode: ordinary global, answered by the slot lookup below
4010 			break;
4011 		case "ARGIND":
4012 			if (isManagedSpecialVariable(name)) {
4013 				return jrt.getARGIND();
4014 			}
4015 			// POSIX mode: ordinary global, answered by the slot lookup below
4016 			break;
4017 		// lazily-materialized globals answered through their synthetic accessors
4018 		case "ARGC":
4019 			return getARGC();
4020 		case "ARGV":
4021 			return getARGV();
4022 		default:
4023 			break;
4024 		}
4025 		if (globalVariableOffsets == null) {
4026 			return executionInitialVariables.get(name);
4027 		}
4028 		Integer offsetObj = globalVariableOffsets.get(name);
4029 		if (offsetObj != null) {
4030 			return runtimeStack.getVariable(offsetObj.intValue(), true);
4031 		}
4032 		// Variables supplied through -v or the Java API but never referenced in
4033 		// the script have no compiled offset; they are still observable (e.g.
4034 		// IGNORECASE read by the gawk extension).
4035 		return executionInitialVariables == null ? null : executionInitialVariables.get(name);
4036 	}
4037 
4038 	/**
4039 	 * Returns the description of the primary script source (typically its file
4040 	 * name), for extension-emitted diagnostics.
4041 	 *
4042 	 * @return script source description, or {@code null} when unknown
4043 	 */
4044 	public String getSourceDescription() {
4045 		return sourceDescription;
4046 	}
4047 
4048 	/**
4049 	 * Returns the script line of the extension call currently being dispatched,
4050 	 * for extension-emitted diagnostics.
4051 	 *
4052 	 * @return current script line number
4053 	 */
4054 	public int getCurrentLineNumber() {
4055 		return currentLineNumber;
4056 	}
4057 
4058 	/** {@inheritDoc} */
4059 	@Override
4060 	public final void assignVariable(String name, Object obj) {
4061 		// When offsets are not available yet, treat the assignment as part of this
4062 		// AVM's baseline initial-variable snapshot.
4063 		if (globalVariableOffsets == null || globalVariableArrays == null) {
4064 			Object normalized = normalizeExternalVariableValue(obj);
4065 			baseInitialVariables.put(name, normalized);
4066 			if (isManagedSpecialVariable(name)) {
4067 				baseSpecialVariables.put(name, normalized);
4068 			}
4069 			return;
4070 		}
4071 
4072 		// make sure we're not receiving funcname=value assignments
4073 		if (functionNames.contains(name)) {
4074 			throw new IllegalArgumentException("Cannot assign a scalar to a function name (" + name + ").");
4075 		}
4076 
4077 		Object normalized = normalizeExternalVariableValue(obj);
4078 		// Runtime assignments to JRT-managed specials (e.g. IGNORECASE=1 or
4079 		// FS=: between input files) must reach the JRT, not a global slot.
4080 		// ARGC is excluded: JRT.setARGC delegates back to this method, and its
4081 		// authoritative storage is the compiled slot below.
4082 		if (!"ARGC".equals(name) && isManagedSpecialVariable(name)) {
4083 			jrt.applySpecialVariable(name, normalized);
4084 			updateSymtabEntry(name, normalized);
4085 			return;
4086 		}
4087 
4088 		Integer offsetObj = globalVariableOffsets.get(name);
4089 		Boolean arrayObj = globalVariableArrays.get(name);
4090 
4091 		if (offsetObj != null) {
4092 			if (arrayObj.booleanValue() && !(normalized instanceof Map)) {
4093 				throw new IllegalArgumentException("Cannot assign a scalar to a non-scalar variable (" + name + ").");
4094 			}
4095 			runtimeStack.setFilelistVariable(offsetObj.intValue(), normalized);
4096 		} else if (runtimeStack.hasGlobalVariable(name)) {
4097 			runtimeStack.setGlobalVariable(name, normalized);
4098 		}
4099 		// names without a compiled slot are still symbols: keep SYMTAB current
4100 		updateSymtabEntry(name, normalized);
4101 	}
4102 
4103 	/**
4104 	 * Executes the {@code nextfile} statement: abandons the current input file
4105 	 * and resumes the per-file input loop at the appropriate point. The
4106 	 * runtime and operand stacks are cleared, so {@code nextfile} unwinds
4107 	 * user-defined function calls, mirroring {@code exit}.
4108 	 * <p>
4109 	 * A {@code nextfile} written directly inside a BEGIN, END, or ENDFILE
4110 	 * rule is already rejected at compile time by the parser. The checks
4111 	 * below cover the uses reached through user-defined functions, where the
4112 	 * calling rule cannot be known statically (the same function may be
4113 	 * called from both an ordinary rule and an END rule); gawk performs the
4114 	 * same checks at runtime.
4115 	 * </p>
4116 	 *
4117 	 * @param position the tuple position tracker to redirect
4118 	 */
4119 	private void executeNextfile(PositionTracker position) {
4120 		if (nextFileAddress == null || !inputFileLoopStarted) {
4121 			throw new AwkRuntimeException(position.lineNumber(), "`nextfile' cannot be called from a BEGIN rule");
4122 		}
4123 		if (withinEndBlocks) {
4124 			throw new AwkRuntimeException(position.lineNumber(), "`nextfile' cannot be called from an END rule");
4125 		}
4126 		if (withinEndFileBlocks) {
4127 			throw new AwkRuntimeException(position.lineNumber(), "`nextfile' cannot be called from an ENDFILE rule");
4128 		}
4129 		// nextfile can be invoked from user-defined functions: unwind them.
4130 		resetCallState();
4131 		if (endFileAddress == null || withinBeginFileBlocks && jrt.hasPendingInputFileError(resolvedInputSource)) {
4132 			// No ENDFILE rules to run, or the file could not be opened: skip
4133 			// the ENDFILE section (gawk BEGINFILE error handling) and go
4134 			// straight to the next file.
4135 			withinBeginFileBlocks = false;
4136 			position.jump(nextFileAddress);
4137 		} else {
4138 			withinBeginFileBlocks = false;
4139 			withinEndFileBlocks = true;
4140 			position.jump(endFileAddress);
4141 		}
4142 	}
4143 
4144 	/**
4145 	 * Executes the {@code next} statement when it is reached through a
4146 	 * user-defined function call: abandons the current input record and
4147 	 * resumes the main input loop. The runtime and operand stacks are
4148 	 * cleared, so {@code next} unwinds user-defined function calls, mirroring
4149 	 * {@code nextfile}.
4150 	 * <p>
4151 	 * A {@code next} written directly inside an input rule compiles to a
4152 	 * plain jump, and direct uses inside BEGIN, END, BEGINFILE, or ENDFILE
4153 	 * rules are rejected at compile time by the parser. The checks below
4154 	 * cover the uses reached through user-defined functions, where the
4155 	 * calling rule cannot be known statically (the same function may be
4156 	 * called from both an input rule and a special rule); gawk performs the
4157 	 * same checks at runtime.
4158 	 * </p>
4159 	 *
4160 	 * @param position the tuple position tracker to redirect
4161 	 */
4162 	private void executeNext(PositionTracker position) {
4163 		if (withinEndBlocks) {
4164 			throw new AwkRuntimeException(position.lineNumber(), "`next' cannot be called from a `END' rule");
4165 		}
4166 		if (withinBeginFileBlocks) {
4167 			throw new AwkRuntimeException(position.lineNumber(), "`next' cannot be called from a `BEGINFILE' rule");
4168 		}
4169 		if (withinEndFileBlocks) {
4170 			throw new AwkRuntimeException(position.lineNumber(), "`next' cannot be called from a `ENDFILE' rule");
4171 		}
4172 		if (nextAddress == null || !mainInputLoopStarted) {
4173 			throw new AwkRuntimeException(position.lineNumber(), "`next' cannot be called from a `BEGIN' rule");
4174 		}
4175 		// next can be invoked from user-defined functions: unwind them.
4176 		resetCallState();
4177 		position.jump(nextAddress);
4178 	}
4179 
4180 	/**
4181 	 * Returns whether a non-redirected {@code getline} must be confined to
4182 	 * the current input file. While the per-file main input loop of a program
4183 	 * with BEGINFILE/ENDFILE rules is running, only the loop itself may cross
4184 	 * file boundaries, so that no file's hooks are ever skipped; a
4185 	 * {@code getline} in an action therefore reports end-of-input at the end
4186 	 * of the current file. In BEGIN and END rules — before the loop starts or
4187 	 * after it ends — {@code getline} keeps streaming across the remaining
4188 	 * input.
4189 	 *
4190 	 * @return {@code true} when getline must not advance to the next file
4191 	 */
4192 	private boolean isMainInputFileBounded() {
4193 		return endFileAddress != null && inputFileLoopStarted && !withinEndBlocks;
4194 	}
4195 
4196 	/**
4197 	 * Raises the gawk-compatible fatal error when a non-redirected
4198 	 * {@code getline} executes inside a BEGINFILE or ENDFILE rule.
4199 	 * <p>
4200 	 * A non-redirected {@code getline} written directly inside a
4201 	 * BEGINFILE/ENDFILE rule is already rejected at compile time by the
4202 	 * parser. This runtime check covers the uses reached through
4203 	 * user-defined functions, where the calling rule cannot be known
4204 	 * statically; it lives here rather than in {@link JRT} because the
4205 	 * current-rule flags are interpreter execution state.
4206 	 * </p>
4207 	 *
4208 	 * @param position the tuple position tracker, for error reporting
4209 	 */
4210 	private void checkGetlineAllowed(PositionTracker position) {
4211 		if (withinBeginFileBlocks || withinEndFileBlocks) {
4212 			throw new AwkRuntimeException(
4213 					position.lineNumber(),
4214 					"non-redirected `getline' invalid inside `" + (withinBeginFileBlocks ? "BEGINFILE" : "ENDFILE") + "' rule");
4215 		}
4216 	}
4217 
4218 	/** {@inheritDoc} */
4219 	@Override
4220 	public Object getFS() {
4221 		return jrt.getFSVar();
4222 	}
4223 
4224 	/** {@inheritDoc} */
4225 	@Override
4226 	public Object getCONVFMT() {
4227 		return jrt.getCONVFMTString();
4228 	}
4229 
4230 	/** {@inheritDoc} */
4231 	@Override
4232 	public void resetFNR() {
4233 		jrt.setFNR(0);
4234 	}
4235 
4236 	/** {@inheritDoc} */
4237 	@Override
4238 	public void incFNR() {
4239 		long v = jrt.getFNR();
4240 		jrt.setFNR(v + 1);
4241 	}
4242 
4243 	/** {@inheritDoc} */
4244 	@Override
4245 	public void incNR() {
4246 		long v = jrt.getNR();
4247 		jrt.setNR(v + 1);
4248 	}
4249 
4250 	/** {@inheritDoc} */
4251 	@Override
4252 	public void setNF(Integer newNf) {
4253 		jrt.setNF(newNf);
4254 	}
4255 
4256 	/** {@inheritDoc} */
4257 	@Override
4258 	public void setFILENAME(String filename) {
4259 		jrt.setFILENAMEViaJrt(jrt.toInputScalar(filename));
4260 	}
4261 
4262 	/** {@inheritDoc} */
4263 	@Override
4264 	public Object getARGV() {
4265 		if (argvOffset == NULL_OFFSET) {
4266 			Map<Object, Object> argv = newAwkArray();
4267 			forEachArgvEntry(argv::put);
4268 			return argv;
4269 		}
4270 		return runtimeStack.getVariable(argvOffset, true);
4271 	}
4272 
4273 	/** {@inheritDoc} */
4274 	@Override
4275 	public Object getARGC() {
4276 		if (argcOffset == NULL_OFFSET) {
4277 			return Long.valueOf(arguments.size() + 1);
4278 		}
4279 		return runtimeStack.getVariable(argcOffset, true);
4280 	}
4281 
4282 	private String getOFMT() {
4283 		return jrt.getOFMTString();
4284 	}
4285 
4286 	private Map<Object, Object> newAwkArray() {
4287 		return JRT.createAwkMap(sortedArrayKeys);
4288 	}
4289 
4290 	private Map<Object, Object> ensureMapVariable(long offset, boolean isGlobal) {
4291 		return toMap(resolveVariable(offset, isGlobal, true));
4292 	}
4293 
4294 	private Map<Object, Object> getMapVariable(long offset, boolean isGlobal) {
4295 		return toMap(resolveVariable(offset, isGlobal, true));
4296 	}
4297 
4298 	private Object resolveVariable(long offset, boolean isGlobal, boolean arrayContext) {
4299 		Object value = runtimeStack.getVariable(offset, isGlobal);
4300 		// Argument references are only ever stored in local parameter slots,
4301 		// so global reads skip the reference check entirely.
4302 		if (!isGlobal && value instanceof ArgumentReference) {
4303 			value = resolveArgumentReference((ArgumentReference) value, arrayContext);
4304 			runtimeStack.setVariable(offset, value, isGlobal);
4305 			return value;
4306 		}
4307 		if (!isUntyped(value)) {
4308 			return value;
4309 		}
4310 		value = arrayContext ? newAwkArray() : BLANK;
4311 		runtimeStack.setVariable(offset, value, isGlobal);
4312 		return value;
4313 	}
4314 
4315 	private Object resolveRawArgumentReference(ArgumentReference reference) {
4316 		Object currentValue = readCurrentArgumentValue(reference);
4317 		if (currentValue instanceof Map
4318 				|| currentValue instanceof UninitializedObject && !(currentValue instanceof UntypedObject)) {
4319 			return currentValue;
4320 		}
4321 		Object value = reference.snapshot();
4322 		return value instanceof ArgumentReference ? resolveRawArgumentReference((ArgumentReference) value) : value;
4323 	}
4324 
4325 	private Object readCurrentArgumentValue(ArgumentReference reference) {
4326 		Object value = reference.currentValue();
4327 		return value instanceof ArgumentReference ? readCurrentArgumentValue((ArgumentReference) value) : value;
4328 	}
4329 
4330 	private Object resolveArgumentReference(ArgumentReference reference, boolean arrayContext) {
4331 		if (!arrayContext) {
4332 			checkScalar(readCurrentArgumentValue(reference));
4333 		}
4334 		Object value = arrayContext ? reference.currentValue() : reference.snapshot();
4335 		if (value instanceof ArgumentReference) {
4336 			value = resolveArgumentReference((ArgumentReference) value, arrayContext);
4337 			if (arrayContext) {
4338 				reference.setValue(value);
4339 			} else {
4340 				reference.setScalarValue(value);
4341 			}
4342 			return value;
4343 		}
4344 		if (!isUntyped(value)) {
4345 			return value;
4346 		}
4347 		value = arrayContext ? newAwkArray() : BLANK;
4348 		if (arrayContext) {
4349 			reference.setValue(value);
4350 		} else {
4351 			reference.setScalarValue(value);
4352 		}
4353 		return value;
4354 	}
4355 
4356 	private Object resolveLengthArgumentReference(ArgumentReference reference) {
4357 		Object currentValue = readCurrentArgumentValue(reference);
4358 		return currentValue instanceof Map ? currentValue : resolveArgumentReference(reference, false);
4359 	}
4360 
4361 	private Map<IndirectArrayArgumentReference, Object> captureAttachedArrayArgumentValues() {
4362 		if (elementArgumentReferences.isEmpty()) {
4363 			return Collections.emptyMap();
4364 		}
4365 		Map<IndirectArrayArgumentReference, Object> values = new IdentityHashMap<IndirectArrayArgumentReference, Object>();
4366 		for (IndirectArrayArgumentReference reference : elementArgumentReferences) {
4367 			if (reference.isAttached()) {
4368 				values.put(reference, reference.currentValue());
4369 			}
4370 		}
4371 		return values;
4372 	}
4373 
4374 	private void detachReplacedArrayArgumentReferences(Map<IndirectArrayArgumentReference, Object> attachedValues) {
4375 		for (Map.Entry<IndirectArrayArgumentReference, Object> entry : attachedValues.entrySet()) {
4376 			entry.getKey().detachIfReplaced(entry.getValue());
4377 		}
4378 	}
4379 
4380 	private void detachMissingArrayArgumentReferences(Map<Object, Object> map) {
4381 		if (elementArgumentReferences.isEmpty()) {
4382 			return;
4383 		}
4384 		for (IndirectArrayArgumentReference reference : elementArgumentReferences) {
4385 			reference.detachIfMissing(map);
4386 		}
4387 	}
4388 
4389 	private static boolean isUntyped(Object value) {
4390 		return value == null || value instanceof UntypedObject;
4391 	}
4392 
4393 	/**
4394 	 * Casts an AWK value to an associative array.
4395 	 *
4396 	 * @param value value to validate
4397 	 * @return the associative array value
4398 	 * @throws AwkRuntimeException when {@code value} is scalar
4399 	 */
4400 	private Map<Object, Object> toMap(Object value) {
4401 		if (!(value instanceof Map)) {
4402 			throw new AwkRuntimeException("Attempting to use a scalar as an array.");
4403 		}
4404 		@SuppressWarnings("unchecked")
4405 		Map<Object, Object> map = (Map<Object, Object>) value;
4406 		return map;
4407 	}
4408 
4409 	/**
4410 	 * Ensures a value is scalar before using it in a scalar-only context such as
4411 	 * a subscript component.
4412 	 *
4413 	 * @param value value to validate
4414 	 * @throws AwkRuntimeException when {@code value} is an array
4415 	 */
4416 	private void checkScalar(Object value) {
4417 		if (value instanceof Map) {
4418 			throw new AwkRuntimeException("Attempting to use an array in a scalar context.");
4419 		}
4420 	}
4421 
4422 	/**
4423 	 * Returns the nested associative array stored in {@code map[key]}, creating it
4424 	 * when the key is undefined.
4425 	 *
4426 	 * @param map containing array
4427 	 * @param key nested-array key
4428 	 * @return the nested associative array stored at {@code key}
4429 	 * @throws AwkRuntimeException when {@code key} is scalar-incompatible or when
4430 	 *         the existing slot contains a scalar
4431 	 */
4432 	private Map<Object, Object> ensureArrayInArray(Map<Object, Object> map, Object key) {
4433 		checkScalar(key);
4434 		boolean existingKey = JRT.containsAwkKey(map, key);
4435 		Object value = JRT.getAssocArrayValue(map, key);
4436 		if (!existingKey || value == null || value instanceof UntypedObject) {
4437 			Map<Object, Object> nested = newAwkArray();
4438 			map.put(key, nested);
4439 			return nested;
4440 		}
4441 		if (!(value instanceof Map)) {
4442 			throw new AwkRuntimeException("Attempting to use a scalar as an array.");
4443 		}
4444 		@SuppressWarnings("unchecked")
4445 		Map<Object, Object> nested = (Map<Object, Object>) value;
4446 		return nested;
4447 	}
4448 
4449 	private Object normalizeExternalVariableValue(Object value) {
4450 		if (value instanceof String) {
4451 			return jrt.toInputScalar(value);
4452 		}
4453 		if (!(value instanceof Map) && !(value instanceof List)) {
4454 			return value;
4455 		}
4456 		// toString() also rebinds a StrNum left by a previous run to this run's
4457 		// decimal separator
4458 		return AssocArray.normalizeValue(value, sortedArrayKeys, leaf -> jrt.toInputScalar(leaf.toString()));
4459 	}
4460 
4461 	private static final UninitializedObject BLANK = new UninitializedObject();
4462 
4463 	private interface ArgumentReference {
4464 
4465 		Object snapshot();
4466 
4467 		Object currentValue();
4468 
4469 		void setValue(Object value);
4470 
4471 		void setScalarValue(Object value);
4472 	}
4473 
4474 	private static final class IndirectArgumentReference implements ArgumentReference {
4475 		private final Object[] frame;
4476 		private final long offset;
4477 		private final Object scalarValue;
4478 
4479 		private IndirectArgumentReference(Object[] frameParam, long offsetParam, Object scalarValueParam) {
4480 			frame = frameParam;
4481 			offset = offsetParam;
4482 			scalarValue = scalarValueParam;
4483 		}
4484 
4485 		@Override
4486 		public Object snapshot() {
4487 			return scalarValue;
4488 		}
4489 
4490 		@Override
4491 		public Object currentValue() {
4492 			return frame[(int) offset];
4493 		}
4494 
4495 		@Override
4496 		public void setValue(Object value) {
4497 			frame[(int) offset] = value;
4498 		}
4499 
4500 		@Override
4501 		public void setScalarValue(Object value) {
4502 			if (isUntyped(currentValue())) {
4503 				setValue(value);
4504 			}
4505 		}
4506 	}
4507 
4508 	private static final class IndirectArrayArgumentReference implements ArgumentReference {
4509 		private final Map<Object, Object> map;
4510 		private final Object key;
4511 		private Object detachedValue;
4512 		private boolean detached;
4513 		// Depth of the call frame that received this reference as an
4514 		// argument; negative until the call is entered.
4515 		private int ownerFrame = -1;
4516 
4517 		private IndirectArrayArgumentReference(Map<Object, Object> mapParam, Object keyParam, Object scalarValueParam) {
4518 			map = mapParam;
4519 			key = keyParam;
4520 			detachedValue = scalarValueParam;
4521 			detached = !JRT.containsAwkKey(map, key);
4522 		}
4523 
4524 		@Override
4525 		public Object snapshot() {
4526 			return detachedValue;
4527 		}
4528 
4529 		@Override
4530 		public Object currentValue() {
4531 			return !detached && JRT.containsAwkKey(map, key) ? JRT.getAssocArrayValue(map, key) : detachedValue;
4532 		}
4533 
4534 		@Override
4535 		public void setValue(Object value) {
4536 			if (!detached && JRT.containsAwkKey(map, key)) {
4537 				map.put(key, value);
4538 			} else {
4539 				detachedValue = value;
4540 			}
4541 		}
4542 
4543 		@Override
4544 		public void setScalarValue(Object value) {
4545 			if (detached || !JRT.containsAwkKey(map, key)) {
4546 				detachedValue = value;
4547 			} else if (isUntyped(JRT.getAssocArrayValue(map, key))) {
4548 				map.put(key, value);
4549 			}
4550 		}
4551 
4552 		private void detachIfMissing(Map<Object, Object> candidateMap) {
4553 			if (!detached && map == candidateMap && !JRT.containsAwkKey(map, key)) {
4554 				detached = true;
4555 			}
4556 		}
4557 
4558 		private boolean isAttached() {
4559 			return !detached && JRT.containsAwkKey(map, key);
4560 		}
4561 
4562 		private void detachIfReplaced(Object previousValue) {
4563 			if (!detached && (!JRT.containsAwkKey(map, key) || JRT.getAssocArrayValue(map, key) != previousValue)) {
4564 				detached = true;
4565 			}
4566 		}
4567 	}
4568 
4569 	/**
4570 	 * Global names that must not participate in persistent memory even though they
4571 	 * are technically user-visible variables.
4572 	 */
4573 	private static final Set<String> NON_PERSISTENT_GLOBALS = new HashSet<>(
4574 			Arrays.asList("ARGV", "ARGC", "ENVIRON", "RSTART", "RLENGTH", "IGNORECASE", "SYMTAB", "FUNCTAB"));
4575 
4576 	private static final class SingleRecordInputSource implements InputSource {
4577 
4578 		private final String record;
4579 		private boolean consumed;
4580 
4581 		private SingleRecordInputSource(String record) {
4582 			this.record = record;
4583 		}
4584 
4585 		@Override
4586 		public boolean nextRecord() {
4587 			if (consumed || record == null) {
4588 				return false;
4589 			}
4590 			consumed = true;
4591 			return true;
4592 		}
4593 
4594 		@Override
4595 		public String getRecordText() {
4596 			return consumed ? record : null;
4597 		}
4598 
4599 		@Override
4600 		public List<String> getFields() {
4601 			return null;
4602 		}
4603 
4604 		@Override
4605 		public boolean isFromFilenameList() {
4606 			return false;
4607 		}
4608 	}
4609 
4610 	/**
4611 	 * The value of an address which is not yet assigned a tuple index.
4612 	 */
4613 	public static final int NULL_OFFSET = -1;
4614 
4615 }