The Java® Virtual Machine Specification Java SE 14 Edition
Total Page:16
File Type:pdf, Size:1020Kb
The Java® Virtual Machine Specification Java SE 14 Edition Tim Lindholm Frank Yellin Gilad Bracha Alex Buckley Daniel Smith 2020-02-20 Specification: JSR-389 Java SE 14 Version: 14 Status: Final Release Release: March 2020 Copyright © 1997, 2020, Oracle America, Inc. All rights reserved. The Specification provided herein is provided to you only under the Limited License Grant included herein as Appendix A. Please see Appendix A, Limited License Grant. Table of Contents 1 Introduction 1 1.1 A Bit of History 1 1.2 The Java Virtual Machine 2 1.3 Organization of the Specification 3 1.4 Notation 4 1.5 Feedback 4 2 The Structure of the Java Virtual Machine 5 2.1 The class File Format 5 2.2 Data Types 6 2.3 Primitive Types and Values 6 2.3.1 Integral Types and Values 7 2.3.2 Floating-Point Types, Value Sets, and Values 8 2.3.3 The returnAddress Type and Values 10 2.3.4 The boolean Type 10 2.4 Reference Types and Values 11 2.5 Run-Time Data Areas 11 2.5.1 The pc Register 12 2.5.2 Java Virtual Machine Stacks 12 2.5.3 Heap 13 2.5.4 Method Area 13 2.5.5 Run-Time Constant Pool 14 2.5.6 Native Method Stacks 14 2.6 Frames 15 2.6.1 Local Variables 16 2.6.2 Operand Stacks 17 2.6.3 Dynamic Linking 18 2.6.4 Normal Method Invocation Completion 18 2.6.5 Abrupt Method Invocation Completion 18 2.7 Representation of Objects 19 2.8 Floating-Point Arithmetic 19 2.8.1 Java Virtual Machine Floating-Point Arithmetic and IEEE 754 19 2.8.2 Floating-Point Modes 20 2.8.3 Value Set Conversion 20 2.9 Special Methods 22 2.9.1 Instance Initialization Methods 22 2.9.2 Class Initialization Methods 22 2.9.3 Signature Polymorphic Methods 23 2.10 Exceptions 23 iii The Java® Virtual Machine Specification 2.11 Instruction Set Summary 26 2.11.1 Types and the Java Virtual Machine 26 2.11.2 Load and Store Instructions 29 2.11.3 Arithmetic Instructions 30 2.11.4 Type Conversion Instructions 32 2.11.5 Object Creation and Manipulation 34 2.11.6 Operand Stack Management Instructions 34 2.11.7 Control Transfer Instructions 34 2.11.8 Method Invocation and Return Instructions 35 2.11.9 Throwing Exceptions 36 2.11.10 Synchronization 36 2.12 Class Libraries 37 2.13 Public Design, Private Implementation 37 3 Compiling for the Java Virtual Machine 39 3.1 Format of Examples 39 3.2 Use of Constants, Local Variables, and Control Constructs 40 3.3 Arithmetic 45 3.4 Accessing the Run-Time Constant Pool 46 3.5 More Control Examples 47 3.6 Receiving Arguments 50 3.7 Invoking Methods 51 3.8 Working with Class Instances 53 3.9 Arrays 55 3.10 Compiling Switches 57 3.11 Operations on the Operand Stack 59 3.12 Throwing and Handling Exceptions 59 3.13 Compiling finally 63 3.14 Synchronization 66 3.15 Annotations 67 3.16 Modules 68 4 The class File Format 71 4.1 The ClassFile Structure 72 4.2 Names 78 4.2.1 Binary Class and Interface Names 78 4.2.2 Unqualified Names 79 4.2.3 Module and Package Names 79 4.3 Descriptors 80 4.3.1 Grammar Notation 80 4.3.2 Field Descriptors 80 4.3.3 Method Descriptors 82 4.4 The Constant Pool 83 4.4.1 The CONSTANT_Class_info Structure 86 4.4.2 The CONSTANT_Fieldref_info, CONSTANT_Methodref_info, and CONSTANT_InterfaceMethodref_info Structures 87 4.4.3 The CONSTANT_String_info Structure 88 iv The Java® Virtual Machine Specification 4.4.4 The CONSTANT_Integer_info and CONSTANT_Float_info Structures 88 4.4.5 The CONSTANT_Long_info and CONSTANT_Double_info Structures 90 4.4.6 The CONSTANT_NameAndType_info Structure 91 4.4.7 The CONSTANT_Utf8_info Structure 92 4.4.8 The CONSTANT_MethodHandle_info Structure 94 4.4.9 The CONSTANT_MethodType_info Structure 96 4.4.10 The CONSTANT_Dynamic_info and CONSTANT_InvokeDynamic_info Structures 96 4.4.11 The CONSTANT_Module_info Structure 97 4.4.12 The CONSTANT_Package_info Structure 98 4.5 Fields 99 4.6 Methods 101 4.7 Attributes 104 4.7.1 Defining and Naming New Attributes 111 4.7.2 The ConstantValue Attribute 111 4.7.3 The Code Attribute 112 4.7.4 The StackMapTable Attribute 116 4.7.5 The Exceptions Attribute 124 4.7.6 The InnerClasses Attribute 125 4.7.7 The EnclosingMethod Attribute 127 4.7.8 The Synthetic Attribute 129 4.7.9 The Signature Attribute 129 4.7.9.1 Signatures 130 4.7.10 The SourceFile Attribute 135 4.7.11 The SourceDebugExtension Attribute 136 4.7.12 The LineNumberTable Attribute 137 4.7.13 The LocalVariableTable Attribute 138 4.7.14 The LocalVariableTypeTable Attribute 140 4.7.15 The Deprecated Attribute 141 4.7.16 The RuntimeVisibleAnnotations Attribute 142 4.7.16.1 The element_value structure 144 4.7.17 The RuntimeInvisibleAnnotations Attribute 147 4.7.18 The RuntimeVisibleParameterAnnotations Attribute 148 4.7.19 The RuntimeInvisibleParameterAnnotations Attribute 149 4.7.20 The RuntimeVisibleTypeAnnotations Attribute 151 4.7.20.1 The target_info union 157 4.7.20.2 The type_path structure 162 4.7.21 The RuntimeInvisibleTypeAnnotations Attribute 167 4.7.22 The AnnotationDefault Attribute 168 4.7.23 The BootstrapMethods Attribute 169 4.7.24 The MethodParameters Attribute 171 4.7.25 The Module Attribute 173 4.7.26 The ModulePackages Attribute 180 4.7.27 The ModuleMainClass Attribute 181 4.7.28 The NestHost Attribute 182 4.7.29 The NestMembers Attribute 182 v The Java® Virtual Machine Specification 4.8 Format Checking 184 4.9 Constraints on Java Virtual Machine Code 184 4.9.1 Static Constraints 185 4.9.2 Structural Constraints 188 4.10 Verification of class Files 192 4.10.1 Verification by Type Checking 194 4.10.1.1 Accessors for Java Virtual Machine Artifacts 196 4.10.1.2 Verification Type System 200 4.10.1.3 Instruction Representation 204 4.10.1.4 Stack Map Frames and Type Transitions 207 4.10.1.5 Type Checking Abstract and Native Methods 211 4.10.1.6 Type Checking Methods with Code 214 4.10.1.7 Type Checking Load and Store Instructions 223 4.10.1.8 Type Checking for protected Members 225 4.10.1.9 Type Checking Instructions 228 4.10.2 Verification by Type Inference 347 4.10.2.1 The Process of Verification by Type Inference 347 4.10.2.2 The Bytecode Verifier 347 4.10.2.3 Values of Types long and double 351 4.10.2.4 Instance Initialization Methods and Newly Created Objects 351 4.10.2.5 Exceptions and finally 353 4.11 Limitations of the Java Virtual Machine 355 5 Loading, Linking, and Initializing 357 5.1 The Run-Time Constant Pool 357 5.2 Java Virtual Machine Startup 360 5.3 Creation and Loading 361 5.3.1 Loading Using the Bootstrap Class Loader 363 5.3.2 Loading Using a User-defined Class Loader 363 5.3.3 Creating Array Classes 364 5.3.4 Loading Constraints 365 5.3.5 Deriving a Class from a class File Representation 366 5.3.6 Modules and Layers 368 5.4 Linking 370 5.4.1 Verification 371 5.4.2 Preparation 371 5.4.3 Resolution 372 5.4.3.1 Class and Interface Resolution 374 5.4.3.2 Field Resolution 375 5.4.3.3 Method Resolution 376 5.4.3.4 Interface Method Resolution 378 5.4.3.5 Method Type and Method Handle Resolution 379 5.4.3.6 Dynamically-Computed Constant and Call Site Resolution 384 5.4.4 Access Control 388 5.4.5 Method Overriding 390 vi The Java® Virtual Machine Specification 5.4.6 Method Selection 391 5.5 Initialization 392 5.6 Binding Native Method Implementations 395 5.7 Java Virtual Machine Exit 395 6 The Java Virtual Machine Instruction Set 397 6.1 Assumptions: The Meaning of "Must" 397 6.2 Reserved Opcodes 398 6.3 Virtual Machine Errors 398 6.4 Format of Instruction Descriptions 399 mnemonic 400 6.5 Instructions 402 aaload 403 aastore 404 aconst_null 406 aload 407 aload_<n> 408 anewarray 409 areturn 410 arraylength 411 astore 412 astore_<n> 413 athrow 414 baload 416 bastore 417 bipush 418 caload 419 castore 420 checkcast 421 d2f 423 d2i 424 d2l 425 dadd 426 daload 428 dastore 429 dcmp<op> 430 dconst_<d> 432 ddiv 433 dload 435 dload_<n> 436 dmul 437 dneg 439 drem 440 dreturn 442 dstore 443 dstore_<n> 444 dsub 445 vii The Java® Virtual Machine Specification dup 446 dup_x1 447 dup_x2 448 dup2 449 dup2_x1 450 dup2_x2 451 f2d 453 f2i 454 f2l 455 fadd 456 faload 458 fastore 459 fcmp<op> 460 fconst_<f> 462 fdiv 463 fload 465 fload_<n> 466 fmul 467 fneg 469 frem 470 freturn 472 fstore 473 fstore_<n> 474 fsub 475 getfield 476 getstatic 477 goto 479 goto_w 480 i2b 481 i2c 482 i2d 483 i2f 484 i2l 485 i2s 486 iadd 487 iaload 488 iand 489 iastore 490 iconst_<i> 491 idiv 492 if_acmp<cond> 493 if_icmp<cond> 494 if<cond> 496 ifnonnull 498 ifnull 499 iinc 500 iload 501 iload_<n> 502 viii The Java® Virtual Machine Specification imul 503 ineg 504 instanceof 505 invokedynamic 507 invokeinterface 509 invokespecial 513 invokestatic 518 invokevirtual 521 ior 528 irem 529 ireturn 530 ishl 532 ishr 533 istore 534 istore_<n> 535 isub 536 iushr 537 ixor 538 jsr 539 jsr_w 540 l2d 541 l2f 542 l2i 543 ladd 544 laload 545 land 546 lastore 547 lcmp 548 lconst_<l> 549 ldc 550 ldc_w 552 ldc2_w 554 ldiv 556 lload 557 lload_<n> 558 lmul 559 lneg 560 lookupswitch 561 lor 563 lrem 564 lreturn 565 lshl 566 lshr 567 lstore 568 lstore_<n> 569 lsub 570 lushr 571 lxor 572 ix The Java® Virtual Machine Specification monitorenter 573 monitorexit 575 multianewarray 577 new 579 newarray 581 nop 583 pop 584 pop2 585 putfield 586 putstatic 588 ret 590 return 591 saload 592 sastore 593 sipush 594 swap 595 tableswitch 596 wide 598 7 Opcode Mnemonics by Opcode 601 A Limited License Grant 605 x CHAPTER 1 Introduction 1.1 A Bit of History The Java® programming language is a general-purpose, concurrent, object-oriented language.