Classfile Grammar
Although the complete code is on the Apress web site (http://www.apress.com), I have included the complete CUP specification here (see Listing A-1). All the underlying code has been removed, but it does provide an overview of a classfile’s bytecode grammar.
Listing A-1. Bytcode Grammar
terminal ROOT, MAGICNUM, MAJORVER, MINORVER, CPCOUNT, CONSTPOOL, CPTAG;
terminal CPINDEX, TYPETAG, ACCFLAGS, XROOT, XMAGICNUM, XMAJORVER;
terminal XMINORVER, XCPCOUNT, XCONSTPOOL, XCPTAG, XCPINDEX, XTYPETAG;
terminal XACCFLAGS, NAMEINDEX, DESCINDEX, VALTAG, THISCL, SUPERCL, INTCNT;
terminal INTERFACES, FIELDCNT, FIELDS, FIELD, XNAMEINDEX, XDESCINDEX;
terminal XVALTAG, XTHISCL, XSUPERCL, XINTCNT, XINTERFACES, XFIELDCNT;
terminal XFIELDS, XFIELD, METHCNT, METHODS, METHOD, ATTCNT, ATTRIBS, ATTRIB;
terminal ATTTYPE, ATTTYPEINDEX, ATTLENGTH, MAXSTACK, MINSTACK;
terminal XMETHCNT, XMETHODS, XMETHOD, XATTCNT, XATTRIBS, XATTRIB;
terminal XATTTYPE, XATTTYPEINDEX, XATTLENGTH, XMAXSTACK, XMINSTACK;
terminal CODELEN, CODETAG, LINETAG, EXCLEN, EXCTABLE, CODEATTCNT;
terminal CODEATTNAME, CODEATTLEN, LNTABLECNT, LINENUMTABLE;
terminal XCODELEN, XCODETAG, XLINETAG, XEXCLEN, XEXCTABLE, XCODEATTCNT;
terminal XCODEATTNAME, XCODEATTLEN, XLNTABLECNT, XLINENUMTABLE;
terminal LINENUM, LNMAP, STARTPC, ENDPC, HANDLER, CATCHTYPE, SRCFILE;
terminal CONSTIDX, XLINENUM, XLNMAP, XSTARTPC, XENDPC, XHANDLER;
terminal XCATCHTYPE, XSRCFILE, XCONSTIDX, ACCESS, PROPERTY, CONSTNAME;
terminal CONSTANT, DECIMALPT, COMMA, CHARRAY, INTEGER, FLOAT, LONG;
terminal DOUBLE, STRING, CLASSREF, FIELDREF, METHODREF, INTERFACEREF;
terminal NAMEANDTYPE, NEGATIVE, HEXNUM, NUMBER, TYPE, ATTRIBNAME;
terminal NOP, CONST, BIPUSH, LDC, LOAD, STORE, POP, POP2, DUP, DUPX, NEW;
terminal ASTORE, ALOAD, NEWARRAY, ARRAYLENGTH;
terminal SWAP, NEG, ADD, SUB, MUL, DIV, REM, SHL, SHR, AND, OR, XOR, IINC;
terminal I2L, CMP, IF, IF_ICMP, GOTO, JSR, RET, RETURN;
terminal TABLESWITCH, LOOKUPSWITCH, GETSTATIC, GETFIELD, PUTSTATIC;
terminal PUTFIELD, INVOKE, ATHROW, CHECKCAST, INSTANCEOF;
terminal MONITORENTER, MONITOREXIT, WIDE, MULTIANEWARRAY, M1, NULL;
non terminal startfile, file, constantpool, constantelement, classname;
non terminal interfaces, fields, field, methods, method, attribs, attrib;
non terminal definitionparts, stmts, expr_part, other, property;
non terminal properties, number, type, access, return, invoke, load;
non terminal bipush, iinc, const, stackops, cmp, if_icmp, if, store, goto, arith;
non terminal conv, object, arrayops, astore, aload, newarray, codeattribs;
non terminal endcodeattribs, linenumtable, linenummapping, exceptiontable;
start with file;
startfile ::= number DECIMALPT number ROOT MAGICNUM HEXNUM XMAGICNUM
MINORVER number XMINORVER MAJORVER number XMAJORVER
;
file ::= startfile CPCOUNT number XCPCOUNT CONSTPOOL constantpool XCONSTPOOL
classname interfaces FIELDCNT number XFIELDCNT FIELDS XFIELDS METHCNT number
XMETHCNT METHODS methods XMETHODS ATTCNT number XATTCNT ATTRIBS attribs
XATTRIBS XROOT
| startfile CPCOUNT number XCPCOUNT CONSTPOOL constantpool XCONSTPOOL
classname interfaces FIELDCNT number XFIELDCNT FIELDS XFIELDS METHCNT number
XMETHCNT METHODS methods XMETHODS ATTCNT number XATTCNT XROOT
| startfile CPCOUNT number XCPCOUNT CONSTPOOL constantpool XCONSTPOOL
classname interfaces FIELDCNT number XFIELDCNT FIELDS fields XFIELDS METHCNT
number XMETHCNT METHODS methods XMETHODS ATTCNT number XATTCNT ATTRIBS
attribs XATTRIBS XROOT
| startfile CPCOUNT number XCPCOUNT CONSTPOOL constantpool XCONSTPOOL
classname interfaces FIELDCNT number XFIELDCNT FIELDS fields XFIELDS METHCNT
number XMETHCNT METHODS methods XMETHODS ATTCNT number XATTCNT XROOT
;
attribs ::= attribs attrib
| attrib
;
attrib ::= ATTRIB ATTTYPE ATTRIBNAME XATTTYPE ATTTYPEINDEX number
XATTTYPEINDEX ATTLENGTH number XATTLENGTH SRCFILE number XSRCFILE
XATTRIB
| ATTRIB ATTTYPE ATTRIBNAME XATTTYPE ATTTYPEINDEX number
XATTTYPEINDEX ATTLENGTH number XATTLENGTH CONSTIDX number XCONSTIDX
XATTRIB
;
constantpool ::= constantpool CPTAG constantelement XCPTAG
| CPTAG constantelement XCPTAG
;
constantelement ::= CPINDEX number:n XCPINDEX TYPETAG CONSTANT CHARRAY:t
XTYPETAG VALTAG CONSTNAME:s XVALTAG
| CPINDEX number:n XCPINDEX TYPETAG CONSTANT INTEGER:t XTYPETAG VALTAG number:intVal XVALTAG
| CPINDEX number:n XCPINDEX TYPETAG CONSTANT INTEGER:t XTYPETAG VALTAG NEGATIVE number:intVal XVALTAG
| CPINDEX number:n XCPINDEX TYPETAG CONSTANT LONG:t XTYPETAG
VALTAG number:longVal
| CPINDEX number:n XCPINDEX TYPETAG CONSTANT LONG:t XTYPETAG
VALTAG NEGATIVE number:longVal XVALTAG
| CPINDEX number:n XCPINDEX TYPETAG CONSTANT FLOAT:t XTYPETAG
VALTAG number:f1 DECIMALPT number:f2 XVALTAG
| CPINDEX number:n XCPINDEX TYPETAG CONSTANT FLOAT:t XTYPETAG
VALTAG NEGATIVE number:f1 DECIMALPT number:f2 XVALTAG
| CPINDEX number:n XCPINDEX TYPETAG CONSTANT DOUBLE:t XTYPETAG
VALTAG number:d1 DECIMALPT number:d2 XVALTAG
| CPINDEX number:n XCPINDEX TYPETAG CONSTANT DOUBLE:t XTYPETAG
VALTAG NEGATIVE number:d1 DECIMALPT number:d2 XVALTAG
| CPINDEX number:n XCPINDEX TYPETAG CONSTANT STRING:t XTYPETAG
VALTAG number:index XVALTAG
| CPINDEX number:n XCPINDEX TYPETAG CONSTANT CLASSREF:t XTYPETAG VALTAG number:index XVALTAG
| CPINDEX number:n XCPINDEX TYPETAG CONSTANT FIELDREF:t XTYPETAG VALTAG number:classindex COMMA number:NaTindex XVALTAG
| CPINDEX number:n XCPINDEX TYPETAG CONSTANT METHODREF:t
XTYPETAG VALTAG number:classindex COMMA number:NaTindex XVALTAG
| CPINDEX number:n XCPINDEX TYPETAG CONSTANT INTERFACEREF:t
XTYPETAG VALTAG number:classindex COMMA number:NaTindex XVALTAG
| CPINDEX number:n XCPINDEX TYPETAG CONSTANT NAMEANDTYPE:t
XTYPETAG VALTAG number:nameindex COMMA number:typeindex XVALTAG
| error
;
classname ::= ACCFLAGS access:a XACCFLAGS THISCL number:classnum XTHISCL
SUPERCL number XSUPERCL
;
interfaces ::= INTCNT number XINTCNT INTERFACES XINTERFACES
;
fields ::= fields FIELD field XFIELD | FIELD field XFIELD
;
field ::= ACCFLAGS access:a definitionparts:params ATTCNT number XATTCNT ATTRIBS
XATTRIBS
;
methods ::= methods METHOD method XMETHOD | METHOD method XMETHOD
;
codeattribs ::= ATTCNT number XATTCNT ATTRIBS ATTRIB ATTTYPE ATTRIBNAME
XATTTYPE ATTTYPEINDEX number XATTTYPEINDEX ATTLENGTH number
XATTLENGTH MAXSTACK number XMAXSTACK MINSTACK number XMINSTACK
CODELEN number XCODELEN CODETAG
;
endcodeattribs ::= XCODETAG EXCLEN number XEXCLEN EXCTABLE exceptiontable
XEXCTABLE CODEATTCNT number XCODEATTCNT CODEATTNAME number
XCODEATTNAME CODEATTLEN number XCODEATTLEN LNTABLECNT number
XLNTABLECNT LINENUMTABLE linenumtable XLINENUMTABLE XATTRIB XATTRIBS
;
linenumtable ::= linenumtable LNMAP linenummapping XLNMAP | LNMAP linenummapping
XLNMAP
;
linenummapping ::=
| STARTPC number XSTARTPC LINENUM number XLINENUM
;
exceptiontable ::=
| STARTPC number XSTARTPC ENDPC number XENDPC HANDLER number
XHANDLER CATCHTYPE number XCATCHTYPE
;
method ::= ACCFLAGS access:a definitionparts:desc codeattribs stmts endcodeattribs
;
definitionparts ::= properties XACCFLAGS NAMEINDEX number:name XNAMEINDEX
DESCINDEX number:params XDESCINDEX
| XACCFLAGS NAMEINDEX number:name XNAMEINDEX DESCINDEX
number:params XDESCINDEX
;
stmts ::= stmts LINETAG expr_part XLINETAG
| LINETAG expr_part XLINETAG
;
expr_part ::= store
| load
| stackops
| bipush
| const
| cmp
| if_icmp
| if
| iinc
| arith
| conv
| goto
| number:l other
| invoke
| object
| return
| arrayops
;
return ::= number:l RETURN:c
| number:l type:t RETURN:c
;
store ::= number:l type:t STORE:s number:n
;
load ::= number:l type:t LOAD:i number:n
| number:l LDC:ld number:n
;
invoke ::= number:l INVOKE:s number:n
;
object ::= number:l NEW:s number:n
| number:l PUTFIELD:s number:n
| number:l PUTSTATIC:s number:n
| number:l GETSTATIC:s number:n
| number:l GETFIELD:s number:n
;
stackops ::= number:l DUP
;
bipush ::= number:l BIPUSH:p number:n
;
const ::= number:l type:t CONST:c number:n
| number:l type:t CONST:c M1:m
| number:l type:t CONST:c NULL:n
;
conv ::= number:l I2L:i type:t
;
arith ::= number:l type:t NEG:r
| number:l type:t REM:r
| number:l type:t ADD:m
| number:l type:t SUB:m
| number:l type:t MUL:m
| number:l type:t DIV:m
;
iinc ::= number:l IINC:p number:n1 number:n2
| number:l IINC:p number:n1 NEGATIVE number:n2
;
cmp ::= number:l type:t CMP:c
;
if_icmp ::= number:l IF_ICMP:c number:n
;
if ::= number:l IF:c number:n
;
goto ::= number:l GOTO:c number:n
;
arrayops ::= aload
| astore
| newarray
;
aload ::= number:l ALOAD:s
;
astore ::= number:l ASTORE:s
;
newarray ::= number:l NEWARRAY:s number:n
| number:l type:t NEWARRAY:s number:n
;
number ::= NUMBER:n
;
access ::= | ACCESS:a
;
properties ::= properties property | property
;
property ::= PROPERTY:p
;
type ::= TYPE:t
;
other ::= NOP
| POP
| POP2
| DUPX
| SWAP
| SHL
| SHR
| AND
| OR
| XOR
| JSR
| RET
| TABLESWITCH
| LOOKUPSWITCH
| ATHROW
| CHECKCAST
| INSTANCEOF
| MONITORENTER
| MONITOREXIT
| WIDE
| MULTIANEWARRAY
| ARRAYLENGTH
;
3.15.186.79