# Java Assembly Language

There is currently no standard syntax for representing Java bytecodes in human-readable form. Sun's javap utility provides one representation, but its output is complicated by code offsets and constant pool index numbers. Thus, a simpler syntax of coding in Java bytecodes was created.

Note: This is documentation on the syntax only. To understand what the operands mean and their usage in programming, look at the Java Virtual Machine Specification. Most of the documentation is beta, meaning some of it is out-of-date. Look for the book by Tim Lindholm and Frank Yellin (to be available sometime this summer by Addison-Wesley) for the latest documentation. Excerpts are available at the above link.

## The Class File Format

Each Java Assembly file (usually suffixed by .jasm) should have the following format:

    [abstract] [final] [public] [interface] class classname
    [extends superclassname]
    [implements interfacename [interfacename [...] ] ]
    {
      [field_declarations]
      [method_declarations]
      [sourcefile sourcefilename]
    }

- classname is a valid class name for this class
- superclassname is a valid class name representing the class that this class inherits from. If superclassname is missing, the default java.lang.Object superclass is used.
- interfacename is a valid class name representing an interface that this class implements.
- sourcefilename is a quoted string literal of the name of the source file (if this assembly program is generated by a compiler).

## Field Declarations

The declaration of the class's fields should have the following format:

    field [access_specifier] [static] [final] [transient] [volatile] fieldname [= constantvalue]

- fieldname is an identifier representing the name of the field.
- constantvalue is a constant (appropriate to the type of the field) representing the constant value of this field.

## Method Declarations

The declaration of the class's methods should have the following format:

    method [access_specifier] [static] [abstract] [final] [native] [synchronized] returntype methodname ( [arg1 [, arg2 [, ...] ] ] )
    [throws exceptionname [exceptionname [...] ] ]
    max_stack value1
    [max_locals value2]
    {
      [code]
      [exceptiontable]
      [linenumbertable]
      [localvariabletable]
    }

- returntype is either void or a valid type.
- methodname is an identifier representing the method's name.
- arg1, arg2, ... are valid types followed by an optional identifier representing the name of that local variable.
- exceptionname is a valid class name identifying an exception that this method throws.
- value1 is an integer constant representing the maximum size of the stack in this method.
- value2 is an integer constant representing the maximum number of local variable slots needed for this method. If max_locals is not given, the assembler calculates max_locals based on local variable declarations.

## Access Specifier

An access specifer (used in field declarations and method declarations) is one of the following:

      private
      private protected
      protected
      public

## Code

Code is one or more lines in one of the following formats:

    [label] operation

or

    [label] vartype localvarname

- label is an identifier appended with a colon (':') and is used to mark that position in the code.
- vartype is a valid type denoting the type of the local variable.
- localvarname is an identifier representing the name of a local variable.

## Operation

An operation is in one of the following formats:

    no_arg_operand

    constant_arg_operand constantval

    field_ref_operand fieldtype fieldname

    method_ref_operand returntype methodname ( [arg1 [,arg2 [...]]] )

    class_ref_operand classname

    label_operand labelname

    local_var_operand localvararg

    anewarray anewarrayname

    iinc localvararg incrementvalue

    lookupswitch
    default defaultlabel
    {
      value1 : gotolabel1
      value2 : gotolabel2
      ...
    }

    multianewarray multianewarraytype dimensions

    newarray newarraytype

    tableswitch beginval to endval
    default defaultlabel
    {
      gotolabel1
      gotolabel2
      ...
    }

- constantval is a valid constant.
- fieldtype is a valid type.
- fieldname is a valid field name.
- returntype is either void or a valid type.
- methodname is a valid method name.
- arg1, arg2, ... are valid types.
- classname is a valid class name.
- labelname, defaultlabel, gotolabel1, gotolabel2 are identifiers corresponding to a label names.
- localvararg is either an integer representing a slot number or an identifier representing an already defined local variable.
- incrementvalue, value1, value2, dimensions, beginval, endval are integer constants.
- anewarraytype, multianewarraytype are either valid class names or array declarations which are valid types followed by one or more bracket pairs ("[]").
- newarraytype is one of the following: boolean, char, float, double, byte, short, int, long.

## Exception Table

An exception table has the following format:

    exceptions
    {
      startpc1 endpc1 handlerpc1 catchtype1
      startpc2 endpc2 handlerpc2 catchtype2
      ...
    }

- startpc1, startpc2, endpc1, endpc2, handlerpc1, handlerpc2 are identifiers corresponding to label names.
- catchtype1, catchtype2 are either valid class names or the integer 0.

## Line Number Table

A line number table has the following format:

    linenumbertable
    {
      startpc1 linenumber1
      startpc2 linenumber2
      ...
    }

- startpc1, startpc2 are identifiers corresponding to label names.
- linenumber1, linenumber2 are valid integers corresponding to the line numbers in the source file.

## Local Variable Table

An local variable table has the following format:

    localvariabletable
    {
      startpc1 endpc1 type1 localvar1 slotnum1
      startpc2 endpc2 type2 localvar2 slotnum2
      ...
    }

- startpc1, startpc2, endpc1, endpc2, are identifiers corresponding to label names which represent the range during which the local variables will have a value.
- type1, type2 are valid types denoting the types of the given local variables.
- localvar1, localvar2 are identifiers representing the names of the given local variables.
- slotnum1, slotnum2 are integer constants representing the slots of the local variables in the method's frame.

## Class Name

A class name has the following format:

    part1[.part2[.part3[...]]]

- part1, part2, ... are identifiers comprising the different parts of the class name

## Field Name

A field name has the following format:

    [aclass].afield

- aclass is a valid class name. If aclass is missing, the current class is used.
- afield is an identifier representing the field name.

## Method Name

A method name has the following format:

    [aclass].amethod

- aclass is a valid class name. If aclass is missing, the current class is used.
- amethod is an identifier representing the method name.

## Constant

Constants are defined much like in Java:

- floating-point numbers are assumed to be double, unless suffixed by the letter F, in which case they are considered to be of type float.
- integers are assumed to be of type int unless suffixed by the letter L, in which case they are considered to be of type long.
- strings should be enclosed in double-quotes.
- characters should be enclosed in single-quotes.
- 0x indicates that the following number is in hexadecimal format.

## Type

A type has the following format:

    basetype[bracketpair[bracketpair[...]]]

- basetype is either a class name or one of the following: byte, char, double, float, int, long, short, boolean.
- bracketpair is a matching pair of brackets ("[]") and denotes array dimensions.

## No Arg Operand

The following are no arg operands:

aaload aastore aconst_null aload_0 aload_1 aload_2 aload_3 areturn arraylength astore_0 astore_1 astore_2 astore_3 athrow baload bastore caload castore d2f d2i d2l dadd daload dastore dcmpg dcmpl dconst_0 dconst_1 ddiv dload_0 dload_1 dload_2 dload_3 dmul dneg drem dreturn dstore_0 dstore_1 dstore_2 dstore_3 dsub dup dup_x1 dup_x2 dup2 dup2_x1 dup2_x2 f2d f2i f2l fadd faload fastore fcmpg fcmpl fconst_0 fconst_1 fdiv fload_0 fload_1 fload_2 fload_3 fmul fneg frem freturn fstore_0 fstore_1 fstore_2 fstore_3 fsub i2b i2c i2d i2f i2l i2s iadd iaload iand iastore iconst_0 iconst_1 iconst_2 iconst_3 iconst_4 iconst_5 iconst_m1 idiv iload_0 iload_1 iload_2 iload_3 imul ineg ior irem ireturn ishl ishr istore_0 istore_1 istore_2 istore_3 isub iushr ixor l2d l2f l2i ladd laload land lastore lcmp lconst_0 lconst_1 ldiv lload_0 lload_1 lload_2 lload_3 lmul lneg lor lrem lreturn lshl lshr lstore_0 lstore_1 lstore_2 lstore_3 lsub lushr lxor monitorenter monitorexit nop pop pop2 return saload sastore swap wide

## Constant Arg Operand

The following are constant arg operands:

bipush ldc ldc_w ldc2_w sipush

## Field Ref Operand

The following are field ref operands:

getfield getstatic putfield putstatic

## Method Ref Operand

The following are method ref operands:

invokeinterface invokestatic invokenonvirtual invokevirtual

## Class Ref Operand

The following are class ref operands:

checkcast instanceof new

## Label Operand

The following are label operands:

goto goto_w if_acmpeq if_acmpne if_icmpeq if_icmpne if_icmplt if_icmpge if_icmpgt if_icmple ifeq ifne iflt ifge ifgt ifle ifnonnull ifnull jsr jsr_w

## Local Var Operand

The following are local var operands:

iload fload aload lload dload istore fstore astore lstore dstore ret load store

- load and store are not standard Java bytecode operands. When followed by a local variable name, load and store are changed to the proper xload or xstore command based on the type of the local variable.

## General Notes

- Keywords are NOT case-sensitive. However, labels and local variable names are case-sensitive.
- C-like comments (using /* and */) are accepted.
- Keywords cannot be used as identifiers.
- Only ascii (not unicode) is supported.