The Java Virtual Machine Specification Java SE 7

Embed Size (px)

Text of The Java Virtual Machine Specification Java SE 7

  • The Java VirtualMachine Specification

    Java SE 7 Edition

    Tim LindholmFrank YellinGilad BrachaAlex Buckley


  • Specification: JSR-000924 Java Virtual Machine Specification ("Specification")Version: 7Status: Final ReleaseRelease: July 2011

    Copyright 1997, 2013, Oracle America, Inc. and/or its affiliates. All rights reserved.500 Oracle Parkway, Redwood City, California 94065, U.S.A.

    Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names maybe trademarks of their respective owners.

    The Specification provided herein is provided to you only under the Limited License Grantincluded herein as Appendix A. Please see Appendix A, Limited License Grant.

  • iii

    Table of Contents

    Preface to the Java SE 7 Edition xi

    Preface to the Second Edition xiii

    Preface to the First Edition xv

    1 Introduction 11.1 A Bit of History 11.2 The Java Virtual Machine 21.3 Summary of Chapters 31.4 Notation 4

    2 The Structure of the Java Virtual Machine 52.1 The class File Format 52.2 Data Types 62.3 Primitive Types and Values 6

    2.3.1 Integral Types and Values 72.3.2 Floating-Point Types, Value Sets, and Values 82.3.3 The returnAddress Type and Values 102.3.4 The boolean Type 10

    2.4 Reference Types and Values 112.5 Run-Time Data Areas 11

    2.5.1 The pc Register 122.5.2 Java Virtual Machine Stacks 122.5.3 Heap 132.5.4 Method Area 132.5.5 Run-Time Constant Pool 142.5.6 Native Method Stacks 14

    2.6 Frames 152.6.1 Local Variables 162.6.2 Operand Stacks 172.6.3 Dynamic Linking 182.6.4 Normal Method Invocation Completion 182.6.5 Abrupt Method Invocation Completion 18

    2.7 Representation of Objects 192.8 Floating-Point Arithmetic 19

    2.8.1 Java Virtual Machine Floating-Point Arithmetic and IEEE754 19

    2.8.2 Floating-Point Modes 20

  • The Java Virtual Machine Specification


    2.8.3 Value Set Conversion 202.9 Special Methods 212.10 Exceptions 232.11 Instruction Set Summary 25

    2.11.1 Types and the Java Virtual Machine 262.11.2 Load and Store Instructions 292.11.3 Arithmetic Instructions 302.11.4 Type Conversion Instructions 322.11.5 Object Creation and Manipulation 342.11.6 Operand Stack Management Instructions 342.11.7 Control Transfer Instructions 342.11.8 Method Invocation and Return Instructions 352.11.9 Throwing Exceptions 362.11.10 Synchronization 36

    2.12 Class Libraries 372.13 Public Design, Private Implementation 37

    3 Compiling for the Java Virtual Machine 393.1 Format of Examples 393.2 Use of Constants, Local Variables, and Control Constructs 403.3 Arithmetic 453.4 Accessing the Run-Time Constant Pool 463.5 More Control Examples 473.6 Receiving Arguments 493.7 Invoking Methods 503.8 Working with Class Instances 533.9 Arrays 553.10 Compiling Switches 573.11 Operations on the Operand Stack 583.12 Throwing and Handling Exceptions 593.13 Compiling finally 633.14 Synchronization 663.15 Annotations 67

    4 The class File Format 694.1 The ClassFile Structure 704.2 The Internal Form of Names 75

    4.2.1 Binary Class and Interface Names 754.2.2 Unqualified Names 75

    4.3 Descriptors and Signatures 754.3.1 Grammar Notation 764.3.2 Field Descriptors 764.3.3 Method Descriptors 784.3.4 Signatures 79

    4.4 The Constant Pool 824.4.1 The CONSTANT_Class_info Structure 83

  • The Java Virtual Machine Specification


    4.4.2 The CONSTANT_Fieldref_info, CONSTANT_Methodref_info, andCONSTANT_InterfaceMethodref_info Structures 84

    4.4.3 The CONSTANT_String_info Structure 864.4.4 The CONSTANT_Integer_info and CONSTANT_Float_info

    Structures 864.4.5 The CONSTANT_Long_info and CONSTANT_Double_info

    Structures 884.4.6 The CONSTANT_NameAndType_info Structure 894.4.7 The CONSTANT_Utf8_info Structure 904.4.8 The CONSTANT_MethodHandle_info Structure 924.4.9 The CONSTANT_MethodType_info Structure 934.4.10 The CONSTANT_InvokeDynamic_info Structure 94

    4.5 Fields 954.6 Methods 974.7 Attributes 100

    4.7.1 Defining and Naming New Attributes 1024.7.2 The ConstantValue Attribute 1034.7.3 The Code Attribute 1044.7.4 The StackMapTable Attribute 1074.7.5 The Exceptions Attribute 1154.7.6 The InnerClasses Attribute 1164.7.7 The EnclosingMethod Attribute 1194.7.8 The Synthetic Attribute 1204.7.9 The Signature Attribute 1204.7.10 The SourceFile Attribute 1214.7.11 The SourceDebugExtension Attribute 1224.7.12 The LineNumberTable Attribute 1234.7.13 The LocalVariableTable Attribute 1244.7.14 The LocalVariableTypeTable Attribute 1264.7.15 The Deprecated Attribute 1284.7.16 The RuntimeVisibleAnnotations attribute 128 The element_value structure 1304.7.17 The RuntimeInvisibleAnnotations attribute 1334.7.18 The RuntimeVisibleParameterAnnotations attribute 1344.7.19 The RuntimeInvisibleParameterAnnotations attribute 1364.7.20 The AnnotationDefault attribute 1374.7.21 The BootstrapMethods attribute 138

    4.8 Format Checking 1404.9 Constraints on Java Virtual Machine code 140

    4.9.1 Static Constraints 1414.9.2 Structural Constraints 144

    4.10 Verification of class Files 1484.10.1 Verification by Type Checking 149 Accessors for Java Virtual Machine Artifacts 1524.10.1.2 Verification Type System 1554.10.1.3 Instruction Representation 1594.10.1.4 Stack Map Frame Representation 1604.10.1.5 Type Checking Abstract and Native Methods 166

  • The Java Virtual Machine Specification

    vi Type Checking Methods with Code 1674.10.1.7 Type Checking Load and Store Instructions 1744.10.1.8 Type Checking for protected Members 1764.10.1.9 Type Checking Instructions 179

    4.10.2 Verification by Type Inference 3274.10.2.1 The Process of Verification by Type Inference 3274.10.2.2 The Bytecode Verifier 3284.10.2.3 Values of Types long and double 3304.10.2.4 Instance Initialization Methods and Newly Created

    Objects 3314.10.2.5 Exceptions and finally 332

    4.11 Limitations of the Java Virtual Machine 334

    5 Loading, Linking, and Initializing 3375.1 The Run-Time Constant Pool 3375.2 Java Virtual Machine Startup 3405.3 Creation and Loading 340

    5.3.1 Loading Using the Bootstrap Class Loader 3425.3.2 Loading Using a User-defined Class Loader 3435.3.3 Creating Array Classes 3445.3.4 Loading Constraints 3445.3.5 Deriving a Class from a class File Representation 346

    5.4 Linking 3475.4.1 Verification 3485.4.2 Preparation 3485.4.3 Resolution 349 Class and Interface Resolution 3505.4.3.2 Field Resolution 3515.4.3.3 Method Resolution 3525.4.3.4 Interface Method Resolution 3535.4.3.5 Method Type and Method Handle Resolution 3545.4.3.6 Call Site Specifier Resolution 357

    5.4.4 Access Control 3585.4.5 Method overriding 359

    5.5 Initialization 3595.6 Binding Native Method Implementations 3625.7 Java Virtual Machine Exit 362

    6 The Java Virtual Machine Instruction Set 3636.1 Assumptions: The Meaning of "Must" 3636.2 Reserved Opcodes 3646.3 Virtual Machine Errors 3646.4 Format of Instruction Descriptions 365

    mnemonic 3666.5 Instructions 368

    aaload 369aastore 370

  • The Java Virtual Machine Specification


    aconst_null 372aload 373aload_ 374anewarray 375areturn 376arraylength 377astore 378astore_ 379athrow 380baload 382bastore 383bipush 384caload 385castore 386checkcast 387d2f 389d2i 390d2l 391dadd 392daload 394dastore 395dcmp 396dconst_ 398ddiv 399dload 401dload_ 402dmul 403dneg 405drem 406dreturn 408dstore 409dstore_ 410dsub 411dup 412dup_x1 413dup_x2 414dup2 415dup2_x1 416dup2_x2 417f2d 419f2i 420f2l 421fadd 422faload 424fastore 425fcmp 426fconst_ 428fdiv 429

  • The Java Virtual Machine Specification


    fload 431fload_ 432fmul 433fneg 435frem 436freturn 438fstore 439fstore_ 440fsub 441getfield 442getstatic 444goto 446goto_w 447i2b 448i2c 449i2d 450i2f 451i2l 452i2s 453iadd 454iaload 455iand 456iastore 457iconst_ 458idiv 459if_acmp 460if_icmp 461if 463ifnonnull 465ifnull 466iinc 467iload 468iload_ 469imul 470ineg 471instanceof 472invokedynamic 474invokeinterface 479invokespecial 482invokestatic 486invokevirtual 489ior 494irem 495ireturn 496ishl 497ishr 498istore 499istore_ 500

  • The Java Virtual Machine Specification


    isub 501iushr 502ixor 503jsr 504jsr_w 505l2d 506l2f 507l2i 508ladd 509laload 510land 511lastore 512lcmp 513lconst_ 514ldc 515ldc_w 517ldc2_w 519ldiv 520lload 521lload_ 522lmul 523lneg 524lookupswitch 525lor 527lrem 528lreturn 529lshl 530lshr 531lstore 532lstore_ 533lsub 534lushr 535lxor 536monitorenter 537monitorexit 539multianewarray 541new 543newarray 545nop 547pop 548pop2 549putfield 550putstatic 552ret 554return 555saload 556sastore 557sipush 558

  • The Java Virtual Machine Specification


    swap 559tableswitch 560wide 562

    7 Opcode Mnemonics by Opcode 565

    Index 569

    A Limited License Grant 587

  • xi

    Preface to the Java SE 7 Edition

    THE Java SE 7 Edition of The Java Virtual Machine Specification incorporatesall the changes that have been made to the Java Virtual Machine since the SecondEdition in 1999. In addition, numerous corrections and clarifications have beenmade to align with popular implementations of the Java Virtual Machine, and withconcepts common to the Java Virtual Machine and the Java programming language.

    Readers may send feedback about errors and ambiguities in The Java VirtualMachine Specification to

    The Java SE 5.0 platform in 2004 brought momentous changes to the Javaprogramming language but had a relatively muted effect on the design of the JavaVirtual Machine. Additions were made to the class file format to support new Javaprogramming language features such as generics and variable arity methods.

    The Java SE 6 platform in 2006 saw no changes to the Java programminglanguage but an entirely new a