Adobe ®
ActionScript Virtual Machine 2 (AVM2) Overview
May 2007
Copyright © 2006–2007 Adobe Systems Incorporated. All rights reserved.
NOTICE: All information contained herein is the property of Adobe Systems Incorporated. No part of this publication (whether in hardcopy or electronic
form) may be reproduced or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior
written consent of Adobe Systems Incorporated.
Adobe, the Adobe Logo, and ActionScript, are trademarks or registered trademarks of Adobe Systems Incorporated in the United States and/or other
countries. All other trademarks are the property of their respective owners.
This publication and the information herein is furnished AS IS, is subject to change without notice, and should not be construed as a commitment by
Adobe Systems Incorporated. Adobe Systems Incorporated assumes no responsibility or liability for any errors or inaccuracies, makes no warranty of any
kind (express, implied, or statutory) with respect to this publication, and expressly disclaims any and all warranties of merchantability, fitness for particular
purposes, and noninfringement of third party rights.
Adobe Systems Incorporated
345 Park Avenue
San Jose, CA 95110
(408) 536-6000
Adobe ActionScript Virtual Machine 2 (AVM2) Overview
2
Contents
1
1.1
Introduction ............................................................................................................................ 5
Concepts................................................................................................................................................5
2
The structure of the ActionScript Virtual Machine .............................................................. 7
2.1 Constant values..................................................................................................................................... 7
2.2 Virtual machine overview..................................................................................................................... 7
2.3 Names .................................................................................................................................................... 8
2.3.1 QName (Qualified Name)...................................................................................................... 8
2.3.2 RTQName (Runtime Qualified Name) ................................................................................ 8
2.3.3 RTQNameL (Runtime Qualified Name Late).....................................................................9
2.3.4 Multiname (Multiple Namespace Name) ............................................................................9
2.3.5 MultinameL (Multiple Namespace Name Late).................................................................9
2.3.6 Resolving multinames ......................................................................................................... 10
2.4 Method invocation notes ................................................................................................................... 10
Instruction set summary.......................................................................................................................11
2.5
2.5.1 Load and store instructions ..................................................................................................11
2.5.2 Arithmetic instructions...........................................................................................................11
2.5.3 Bit manipulation instructions ................................................................................................11
2.5.4 Type conversion instructions................................................................................................11
2.5.5 Object creation and manipulation instructions ................................................................ 12
2.5.6 Stack management instructions ........................................................................................ 12
2.5.7 Control transfer instructions ............................................................................................... 12
2.5.8 Function invocation and return instructions..................................................................... 12
2.5.9 Exception instructions ......................................................................................................... 12
2.5.10 Debugging instructions ....................................................................................................... 13
3
Loading, linking, verification, and execution.......................................................................14
3.1 Overview .............................................................................................................................................. 14
3.2 Loading and linking ............................................................................................................................ 14
3.3 Execution ............................................................................................................................................. 15
3.3.1 Program invocation and exit............................................................................................... 15
3.3.2 Class initialization................................................................................................................. 15
3.3.3 Method entry......................................................................................................................... 15
3.3.4 Execution mechanics .......................................................................................................... 16
3.3.5 Calling and returning............................................................................................................ 16
3.3.6 Exception handling .............................................................................................................. 16
3.4 Verification........................................................................................................................................... 16
The ActionScript Byte Code (abc) format...........................................................................18
4
4.1 Primitive data types .............................................................................................................................18
4.2 abcFile.................................................................................................................................................. 19
4.3 Constant pool ..................................................................................................................................... 20
4.4 String .................................................................................................................................................... 21
4.4.1 Namespace .......................................................................................................................... 22
4.4.2 Namespace set.................................................................................................................... 22
4.4.3 Multiname............................................................................................................................. 23
4.5 Method signature............................................................................................................................... 24
4.5.1 Optional parameters ........................................................................................................... 25
4.5.2 Parameter names.................................................................................................................27
4.6 metadata_info......................................................................................................................................27
4.7
Instance............................................................................................................................................... 28
4.8 Trait...................................................................................................................................................... 29
4.8.1 Summary of trait types ....................................................................................................... 29
4.8.2 Slot and const traits ............................................................................................................ 30
4.8.3 Class traits............................................................................................................................ 30
4.8.4 Function traits....................................................................................................................... 31
Adobe ActionScript Virtual Machine 2 (AVM2) Overview
3
4.8.5 Method, getter, and setter traits......................................................................................... 31
4.8.6 Trait attributes....................................................................................................................... 31
4.9 Class ..................................................................................................................................................... 31
4.10 Script ................................................................................................................................................... 32
4.11 Method body ...................................................................................................................................... 32
4.12 Exception ............................................................................................................................................ 34
5
AVM2 instructions...............................................................................................................35
6 Hints for compiler writers.................................................................................................. 107
6.1 Reestablishing the scope stack following an exception ............................................................. 107
6.2
In-line subroutines for handling “finally”........................................................................................ 107
6.3 Creating an environment for nested functions .............................................................................108
6.4 Optimizing method dispatch ...........................................................................................................108
Adobe ActionScript Virtual Machine 2 (AVM2) Overview
4
1
Introduction
The Adobe® ActionScript™ Virtual Machine 2, or AVM2 for short, was designed to execute programs written in
the ActionScript 3.0 language. ActionScript 3.0 is based on ECMAScript, the international standardized
programming language for scripting. ActionScript 3.0 is compliant with the ECMAScript Language
Specification, Third Edition (ECMA-262). It also contains functionality based on ongoing work on
ECMAScript Edition 4, occurring within the Ecma International standards body.
This document describes the operation of the AVM2 and defines the file formats, data structures, and
instruction formats used by the AVM2.
1.1 Concepts
The AVM2 was designed to support the ActionScript (AS) 3.0 language, and for the remaining chapters it is
assumed that the reader is aware of the terminology and concepts of the language.
The following vocabulary and associated definitions are taken from the ActionScript 3.0 Language Specification
and are presented only as a review of the material. For full details, refer to the language specification.
Virtual Machine—A virtual machine is a mechanism that takes as its input the description of a
computation and that performs that computation. For the AVM2, the input is in the form of an ABC file,
which contains compiled programs; these comprise constant data, instructions from the AVM2 instruction
set, and various kinds of metadata.
Script—A script set of traits and an initializer method; a script populates a top-level environment with
definitions and data.
Bytecode, code—Bytecode or code is a specification of computation in the form of a sequence of simple
actions on the virtual machine state.
Scope—Scope is a mapping from names to locations, where no two names are the same. Scopes can nest,
and nested scopes can contain bindings (associations between names and locations) that shadow the
bindings of the nesting scope.
Object—An object is an unordered collection of named properties, which are containers that hold values.
A value in ActionScript 3.0 is either an Object reference or one of the special values null or undefined.
Namespace—Namespaces are used to control the visibility of a set of properties independent of the major
structure of the program.
Class—A class is a named description of a group of objects. Objects are created from classes by
instantiation.
Inheritance—New classes can be derived from older classes by the mechanism known as inheritance or
subclassing. The new class is called the derived class or subclass of the old class, and the old class is called the
base class or superclass.
Trait—A trait is a fixed-name property shared by all objects that are instances of the same class; a set of
traits expresses the type of an object.
Method—The word method is used with two separate meanings. One meaning is a method body, which is
an object that contains code as well as data that belong to that code or that describe the code. The other
meaning is a method closure, which is a method body together with a reference to the environment in which
the closure was created. In this document, functions, constructors, ActionScript 3.0 class methods, and
other objects that can be invoked are collectively referred to as method closures.
Adobe ActionScript Virtual Machine 2 (AVM2) Overview
5
Verification—The contents of an ABC file undergo verification when the file is loaded into the AVM2.
The ABC file is rejected by the verifier if it does not conform to the AVM2 Overview. Verification is
described in Chapter 3.
Just-in-Time (JIT) Compiler—AVM2 implementations may contain an optional run-time compiler for
transforming AVM2 instructions into processor-specific instructions. Although not an implementation
requirement, employing a JIT compiler provides a performance benefit for many applications.
Adobe ActionScript Virtual Machine 2 (AVM2) Overview
6
2 The structure of the ActionScript Virtual Machine
2.1 Constant values
The constant values of the AVM2 are one of the following types: int, uint, double, string, namespace,
undefined, or null. The values of these types appear directly in the ABC file or in the instruction encodings.
Important characteristics of these types are:
int—This type is used to represent an integer valued number whose values are 32-bit signed two’s
complement integers. The range of values is from -2,147,483,648 to 2,147,483,647 (-231 to 231-1),
inclusive.
uint—This type is used for integer valued numbers with values that are 32-bit unsigned two’s complement
integers. The range of values is from 0 to 4,294,967,296 (232), inclusive.
double—This type is used for capturing floating point numbers using 64-bit double precision IEEE 754
values as specified in IEEE Standard for Binary Floating-Point Arithmetic (ANSI/IEEE Standard. 754-
1985).
string—This type represents a sequence of Unicode characters. Strings are represented in UTF-8 and can
be as long as 230-1 bytes.
namespace—Namespaces tie a URI (represented internally by a string) to a trait. The relationship is
unidirectional, meaning the namespace data type only contains a URI. Each namespace is also of a
particular kind, and there are restrictions regarding the relationships between the trait and kind. These
rules are defined later in this chapter.
null—A singleton value representing “no object”.
undefined—A singleton value representing “no meaningful value”. This constant value is allowed only in
certain contexts.
The AVM2 utilizes several representations for values in its instruction encoding and in the ABC file in order to
provide as compact an encoding as is required.
2.2 Virtual machine overview
Computation in the AVM2 is based on executing the code of a method body in the context of method
information, a local data area, a constant pool, a heap for non-primitive data objects created at run-time, and a
run-time environment. Many data elements are static and are read at startup from an ABC file, whose structure is
defined in Chapter 4.
The code for a method body is composed of instructions, defined in Chapter 5. Each instruction
modifies the state of the machine in some way, or has an effect on the external environment by means
of input or output.1
The method information determines how the method is used—for example, how default argument
values should be substituted for missing arguments when the method is called.
The local data area for the method consists of the operand stack, the scope stack, and the local registers.
1 In practice, the AVM2 may transform the code at run-time by means of a JIT, but this does not affect the
semantics of execution, only its performance.
Adobe ActionScript Virtual Machine 2 (AVM2) Overview
7
o The operand stack holds operands for the instructions and receives their results. Arguments
are pushed onto the stack top or popped off the stack top. The top element always has
address 0; the one below it has address 1, and so on. Stack addresses are not used except as a
specification mechanism.
o The scope stack is part of the run-time environment and holds objects that are to be searched
by the AVM2 when an instruction is executed that calls for name lookup. Instructions push
elements onto the scope stack as part of the implementation of exception handling, closure
creation, and for the ActionScript 3.0 with statement.
o The local registers hold parameter values, local variables in some cases, and temporaries.
The constant pool holds constant values that are referenced, ultimately, by the instruction stream:
numbers, strings, and various kinds of names.
Instructions and the AVM2 can create new objects at run-time, and these objects are allocated in the
heap. The only way to access the heap is through an object allocated in it. Objects in the heap that
are no longer needed will eventually be reclaimed by the AVM2.
The run-time environment logically consists of a chain of objects, and named properties on these
objects are the locations found during a name lookup at run-time. Name lookup proceeds from the
innermost (most recently pushed) scope toward the outermost (global) scope.
The creation of a method closure causes the run-time environment that is current at the time of
creation to be captured in the closure; when the closure is later invoked, that scope is made current,
and will be extended by the code in the method body.
2.3 Names
Names in the AVM are represented by a combination of an unqualified name and one or more namespaces.
These are collectively called multinames. Multiname entries usually consist of a name index, and a namespace or
namespace set index. Some multinames can have the name and/or namespace part resolved at runtime. There
are a number of different types of multinames as described below. Properties of objects are always named by a
simple QName (a pair of name and namespace). The other types of multinames are used to resolve properties
at runtime.
RTQName, RTQNameL, and MultinameL are collectively referred to as runtime multinames.
2.3.1 QName (Qualified Name)
This is the simplest form of a multiname. It is a name with exactly one namespace, hence QName for qualified
name. QName entries will have a name index followed by a namespace index. The name index is an index into
the string constant pool, and the namespace index is an index into the namespace constant pool.
QNames are typically used to represent the names of variables, and for type annotations.
public var s : String;
This code will produce two QName entries, one for the variable s (public namespace, name "s") and one for the
type String (public namespace, name "String").
2.3.2 RTQName (Runtime Qualified Name)
This is a runtime QName, where the namespace is not resolved until runtime. RTQName entries will have
only a name index, which is an index into the string constant pool. The namespace is determined at runtime.
When a RTQName is an operand to an opcode, there should be a namespace value on the stack the
Adobe ActionScript Virtual Machine 2 (AVM2) Overview
8