stackMapTableFormat.hpp 27.5 KB
Newer Older
1
/*
2
 * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

25 26 27 28 29
#ifndef SHARE_VM_CLASSFILE_STACKMAPTABLEFORMAT_HPP
#define SHARE_VM_CLASSFILE_STACKMAPTABLEFORMAT_HPP

#include "classfile/verificationType.hpp"

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
// These classes represent the stack-map substructures described in the JVMS
// (hence the non-conforming naming scheme).

// These classes work with the types in their compressed form in-place (as they
// would appear in the classfile).  No virtual methods or fields allowed.

class verification_type_info {
 private:
  // u1 tag
  // u2 cpool_index || u2 bci (for ITEM_Object & ITEM_Uninitailized only)

  address tag_addr() const { return (address)this; }
  address cpool_index_addr() const { return tag_addr() + sizeof(u1); }
  address bci_addr() const { return cpool_index_addr(); }

 protected:
  // No constructors  - should be 'private', but GCC issues a warning if it is
  verification_type_info() {}
  verification_type_info(const verification_type_info&) {}

 public:

  static verification_type_info* at(address addr) {
    return (verification_type_info*)addr;
  }

  static verification_type_info* create_at(address addr, u1 tag) {
    verification_type_info* vti = (verification_type_info*)addr;
    vti->set_tag(tag);
    return vti;
  }

  static verification_type_info* create_object_at(address addr, u2 cp_idx) {
    verification_type_info* vti = (verification_type_info*)addr;
    vti->set_tag(ITEM_Object);
    vti->set_cpool_index(cp_idx);
    return vti;
  }

  static verification_type_info* create_uninit_at(address addr, u2 bci) {
    verification_type_info* vti = (verification_type_info*)addr;
    vti->set_tag(ITEM_Uninitialized);
    vti->set_bci(bci);
    return vti;
  }

  static size_t calculate_size(u1 tag) {
    if (tag == ITEM_Object || tag == ITEM_Uninitialized) {
      return sizeof(u1) + sizeof(u2);
    } else {
      return sizeof(u1);
    }
  }

  static size_t max_size() { return sizeof(u1) + sizeof(u2); }

  u1 tag() const { return *(u1*)tag_addr(); }
  void set_tag(u1 tag) { *((u1*)tag_addr()) = tag; }

  bool is_object() const { return tag() == ITEM_Object; }
  bool is_uninitialized() const { return tag() == ITEM_Uninitialized; }

  u2 cpool_index() const {
    assert(is_object(), "This type has no cp_index");
    return Bytes::get_Java_u2(cpool_index_addr());
  }
  void set_cpool_index(u2 idx) {
    assert(is_object(), "This type has no cp_index");
    Bytes::put_Java_u2(cpool_index_addr(), idx);
  }

  u2 bci() const {
    assert(is_uninitialized(), "This type has no bci");
    return Bytes::get_Java_u2(bci_addr());
  }

  void set_bci(u2 bci) {
    assert(is_uninitialized(), "This type has no bci");
    Bytes::put_Java_u2(bci_addr(), bci);
  }

  void copy_from(verification_type_info* from) {
    set_tag(from->tag());
    if (from->is_object()) {
      set_cpool_index(from->cpool_index());
    } else if (from->is_uninitialized()) {
      set_bci(from->bci());
    }
  }

  size_t size() const {
    return calculate_size(tag());
  }

  verification_type_info* next() {
    return (verification_type_info*)((address)this + size());
  }

  // This method is used when reading unverified data in order to ensure
  // that we don't read past a particular memory limit.  It returns false
  // if any part of the data structure is outside the specified memory bounds.
  bool verify(address start, address end) {
    return ((address)this >= start &&
            (address)this < end &&
            (bci_addr() + sizeof(u2) <= end ||
                !is_object() && !is_uninitialized()));
  }

  void print_on(outputStream* st) {
    switch (tag()) {
      case ITEM_Top: st->print("Top"); break;
      case ITEM_Integer: st->print("Integer"); break;
      case ITEM_Float: st->print("Float"); break;
      case ITEM_Double: st->print("Double"); break;
      case ITEM_Long: st->print("Long"); break;
      case ITEM_Null: st->print("Null"); break;
      case ITEM_UninitializedThis:
        st->print("UninitializedThis"); break;
      case ITEM_Uninitialized:
        st->print("Uninitialized[#%d]", bci()); break;
      case ITEM_Object:
        st->print("Object[#%d]", cpool_index()); break;
      default:
        assert(false, "Bad verification_type_info");
    }
  }
};

#define FOR_EACH_STACKMAP_FRAME_TYPE(macro, arg1, arg2) \
  macro(same_frame, arg1, arg2) \
  macro(same_frame_extended, arg1, arg2) \
161 162
  macro(same_locals_1_stack_item_frame, arg1, arg2) \
  macro(same_locals_1_stack_item_extended, arg1, arg2) \
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
  macro(chop_frame, arg1, arg2) \
  macro(append_frame, arg1, arg2) \
  macro(full_frame, arg1, arg2)

#define SM_FORWARD_DECL(type, arg1, arg2) class type;
FOR_EACH_STACKMAP_FRAME_TYPE(SM_FORWARD_DECL, x, x)
#undef SM_FORWARD_DECL

class stack_map_frame {
 protected:
  address frame_type_addr() const { return (address)this; }

  // No constructors  - should be 'private', but GCC issues a warning if it is
  stack_map_frame() {}
  stack_map_frame(const stack_map_frame&) {}

 public:

  static stack_map_frame* at(address addr) {
    return (stack_map_frame*)addr;
  }

  stack_map_frame* next() const {
    return at((address)this + size());
  }

  u1 frame_type() const { return *(u1*)frame_type_addr(); }
  void set_frame_type(u1 type) { *((u1*)frame_type_addr()) = type; }

  // pseudo-virtual methods
  inline size_t size() const;
  inline int offset_delta() const;
  inline void set_offset_delta(int offset_delta);
  inline int number_of_types() const; // number of types contained in the frame
  inline verification_type_info* types() const; // pointer to first type
  inline bool is_valid_offset(int offset_delta) const;

  // This method must be used when reading unverified data in order to ensure
  // that we don't read past a particular memory limit.  It returns false
  // if any part of the data structure is outside the specified memory bounds.
  inline bool verify(address start, address end) const;
204 205

  inline void print_on(outputStream* st, int current_offset) const;
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262

  // Create as_xxx and is_xxx methods for the subtypes
#define FRAME_TYPE_DECL(stackmap_frame_type, arg1, arg2) \
  inline stackmap_frame_type* as_##stackmap_frame_type() const; \
  bool is_##stackmap_frame_type() { \
    return as_##stackmap_frame_type() != NULL; \
  }

  FOR_EACH_STACKMAP_FRAME_TYPE(FRAME_TYPE_DECL, x, x)
#undef FRAME_TYPE_DECL
};

class same_frame : public stack_map_frame {
 private:
  static int frame_type_to_offset_delta(u1 frame_type) {
      return frame_type + 1; }
  static u1 offset_delta_to_frame_type(int offset_delta) {
      return (u1)(offset_delta - 1); }

 public:

  static bool is_frame_type(u1 tag) {
    return tag < 64;
  }

  static same_frame* at(address addr) {
    assert(is_frame_type(*addr), "Wrong frame id");
    return (same_frame*)addr;
  }

  static same_frame* create_at(address addr, int offset_delta) {
    same_frame* sm = (same_frame*)addr;
    sm->set_offset_delta(offset_delta);
    return sm;
  }

  static size_t calculate_size() { return sizeof(u1); }

  size_t size() const { return calculate_size(); }
  int offset_delta() const { return frame_type_to_offset_delta(frame_type()); }

  void set_offset_delta(int offset_delta) {
    assert(offset_delta <= 64, "Offset too large for same_frame");
    set_frame_type(offset_delta_to_frame_type(offset_delta));
  }

  int number_of_types() const { return 0; }
  verification_type_info* types() const { return NULL; }

  bool is_valid_offset(int offset_delta) const {
    return is_frame_type(offset_delta_to_frame_type(offset_delta));
  }

  bool verify_subtype(address start, address end) const {
    return true;
  }

263 264
  void print_on(outputStream* st, int current_offset = -1) const {
    st->print("same_frame(@%d)", offset_delta() + current_offset);
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
  }
};

class same_frame_extended : public stack_map_frame {
 private:
  enum { _frame_id = 251 };
  address offset_delta_addr() const { return frame_type_addr() + sizeof(u1); }

 public:
  static bool is_frame_type(u1 tag) {
    return tag == _frame_id;
  }

  static same_frame_extended* at(address addr) {
    assert(is_frame_type(*addr), "Wrong frame type");
    return (same_frame_extended*)addr;
  }

  static same_frame_extended* create_at(address addr, u2 offset_delta) {
    same_frame_extended* sm = (same_frame_extended*)addr;
    sm->set_frame_type(_frame_id);
    sm->set_offset_delta(offset_delta);
    return sm;
  }

  static size_t calculate_size() { return sizeof(u1) + sizeof(u2); }

  size_t size() const { return calculate_size(); }
  int offset_delta() const {
    return Bytes::get_Java_u2(offset_delta_addr()) + 1;
  }

  void set_offset_delta(int offset_delta) {
    Bytes::put_Java_u2(offset_delta_addr(), offset_delta - 1);
  }

  int number_of_types() const { return 0; }
  verification_type_info* types() const { return NULL; }
  bool is_valid_offset(int offset) const { return true; }

  bool verify_subtype(address start, address end) const {
    return frame_type_addr() + size() <= end;
  }

309 310
  void print_on(outputStream* st, int current_offset = -1) const {
    st->print("same_frame_extended(@%d)", offset_delta() + current_offset);
311 312 313
  }
};

314
class same_locals_1_stack_item_frame : public stack_map_frame {
315 316 317 318 319 320 321 322 323 324 325 326 327
 private:
  address type_addr() const { return frame_type_addr() + sizeof(u1); }

  static int frame_type_to_offset_delta(u1 frame_type) {
      return frame_type - 63; }
  static u1 offset_delta_to_frame_type(int offset_delta) {
      return (u1)(offset_delta + 63); }

 public:
  static bool is_frame_type(u1 tag) {
    return tag >= 64 && tag < 128;
  }

328
  static same_locals_1_stack_item_frame* at(address addr) {
329
    assert(is_frame_type(*addr), "Wrong frame id");
330
    return (same_locals_1_stack_item_frame*)addr;
331 332
  }

333
  static same_locals_1_stack_item_frame* create_at(
334
      address addr, int offset_delta, verification_type_info* vti) {
335
    same_locals_1_stack_item_frame* sm = (same_locals_1_stack_item_frame*)addr;
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
    sm->set_offset_delta(offset_delta);
    if (vti != NULL) {
      sm->set_type(vti);
    }
    return sm;
  }

  static size_t calculate_size(verification_type_info* vti) {
    return sizeof(u1) + vti->size();
  }

  static size_t max_size() {
    return sizeof(u1) + verification_type_info::max_size();
  }

  size_t size() const { return calculate_size(types()); }
  int offset_delta() const { return frame_type_to_offset_delta(frame_type()); }

  void set_offset_delta(int offset_delta) {
    assert(offset_delta > 0 && offset_delta <= 64,
           "Offset too large for this frame type");
    set_frame_type(offset_delta_to_frame_type(offset_delta));
  }

  void set_type(verification_type_info* vti) {
    verification_type_info* cur = types();
    cur->copy_from(vti);
  }

  int number_of_types() const { return 1; }
  verification_type_info* types() const {
    return verification_type_info::at(type_addr());
  }

  bool is_valid_offset(int offset_delta) const {
    return is_frame_type(offset_delta_to_frame_type(offset_delta));
  }

  bool verify_subtype(address start, address end) const {
    return types()->verify(start, end);
  }

378 379 380
  void print_on(outputStream* st, int current_offset = -1) const {
    st->print("same_locals_1_stack_item_frame(@%d,",
        offset_delta() + current_offset);
381 382 383 384 385
    types()->print_on(st);
    st->print(")");
  }
};

386
class same_locals_1_stack_item_extended : public stack_map_frame {
387 388 389 390 391 392 393 394 395 396 397
 private:
  address offset_delta_addr() const { return frame_type_addr() + sizeof(u1); }
  address type_addr() const { return offset_delta_addr() + sizeof(u2); }

  enum { _frame_id = 247 };

 public:
  static bool is_frame_type(u1 tag) {
    return tag == _frame_id;
  }

398
  static same_locals_1_stack_item_extended* at(address addr) {
399
    assert(is_frame_type(*addr), "Wrong frame id");
400
    return (same_locals_1_stack_item_extended*)addr;
401 402
  }

403
  static same_locals_1_stack_item_extended* create_at(
404
      address addr, int offset_delta, verification_type_info* vti) {
405 406
    same_locals_1_stack_item_extended* sm =
       (same_locals_1_stack_item_extended*)addr;
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
    sm->set_frame_type(_frame_id);
    sm->set_offset_delta(offset_delta);
    if (vti != NULL) {
      sm->set_type(vti);
    }
    return sm;
  }

  static size_t calculate_size(verification_type_info* vti) {
    return sizeof(u1) + sizeof(u2) + vti->size();
  }

  size_t size() const { return calculate_size(types()); }
  int offset_delta() const {
    return Bytes::get_Java_u2(offset_delta_addr()) + 1;
  }

  void set_offset_delta(int offset_delta) {
    Bytes::put_Java_u2(offset_delta_addr(), offset_delta - 1);
  }

  void set_type(verification_type_info* vti) {
    verification_type_info* cur = types();
    cur->copy_from(vti);
  }

  int number_of_types() const { return 1; }
  verification_type_info* types() const {
    return verification_type_info::at(type_addr());
  }
  bool is_valid_offset(int offset) { return true; }

  bool verify_subtype(address start, address end) const {
    return type_addr() < end && types()->verify(start, end);
  }

443 444 445
  void print_on(outputStream* st, int current_offset = -1) const {
    st->print("same_locals_1_stack_item_extended(@%d,",
        offset_delta() + current_offset);
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
    types()->print_on(st);
    st->print(")");
  }
};

class chop_frame : public stack_map_frame {
 private:
  address offset_delta_addr() const { return frame_type_addr() + sizeof(u1); }

  static int frame_type_to_chops(u1 frame_type) {
    int chop = 251 - frame_type;
    return chop;
  }

  static u1 chops_to_frame_type(int chop) {
    return 251 - chop;
  }

 public:
  static bool is_frame_type(u1 tag) {
    return frame_type_to_chops(tag) > 0 && frame_type_to_chops(tag) < 4;
  }

  static chop_frame* at(address addr) {
    assert(is_frame_type(*addr), "Wrong frame id");
    return (chop_frame*)addr;
  }

  static chop_frame* create_at(address addr, int offset_delta, int chops) {
    chop_frame* sm = (chop_frame*)addr;
    sm->set_chops(chops);
    sm->set_offset_delta(offset_delta);
    return sm;
  }

  static size_t calculate_size() {
    return sizeof(u1) + sizeof(u2);
  }

  size_t size() const { return calculate_size(); }
  int offset_delta() const {
    return Bytes::get_Java_u2(offset_delta_addr()) + 1;
  }
  void set_offset_delta(int offset_delta) {
    Bytes::put_Java_u2(offset_delta_addr(), offset_delta - 1);
  }

  int chops() const {
    int chops = frame_type_to_chops(frame_type());
    assert(chops > 0 && chops < 4, "Invalid number of chops in frame");
    return chops;
  }
  void set_chops(int chops) {
    assert(chops > 0 && chops <= 3, "Bad number of chops");
    set_frame_type(chops_to_frame_type(chops));
  }

  int number_of_types() const { return 0; }
  verification_type_info* types() const { return NULL; }
  bool is_valid_offset(int offset) { return true; }

  bool verify_subtype(address start, address end) const {
    return frame_type_addr() + size() <= end;
  }

511 512
  void print_on(outputStream* st, int current_offset = -1) const {
    st->print("chop_frame(@%d,%d)", offset_delta() + current_offset, chops());
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
  }
};

class append_frame : public stack_map_frame {
 private:
  address offset_delta_addr() const { return frame_type_addr() + sizeof(u1); }
  address types_addr() const { return offset_delta_addr() + sizeof(u2); }

  static int frame_type_to_appends(u1 frame_type) {
    int append = frame_type - 251;
    return append;
  }

  static u1 appends_to_frame_type(int appends) {
    assert(appends > 0 && appends < 4, "Invalid append amount");
    return 251 + appends;
  }

 public:
  static bool is_frame_type(u1 tag) {
    return frame_type_to_appends(tag) > 0 && frame_type_to_appends(tag) < 4;
  }

  static append_frame* at(address addr) {
    assert(is_frame_type(*addr), "Wrong frame id");
    return (append_frame*)addr;
  }

  static append_frame* create_at(
      address addr, int offset_delta, int appends,
      verification_type_info* types) {
    append_frame* sm = (append_frame*)addr;
    sm->set_appends(appends);
    sm->set_offset_delta(offset_delta);
    if (types != NULL) {
      verification_type_info* cur = sm->types();
      for (int i = 0; i < appends; ++i) {
        cur->copy_from(types);
        cur = cur->next();
        types = types->next();
      }
    }
    return sm;
  }

  static size_t calculate_size(int appends, verification_type_info* types) {
    size_t sz = sizeof(u1) + sizeof(u2);
    for (int i = 0; i < appends; ++i) {
      sz += types->size();
      types = types->next();
    }
    return sz;
  }

  static size_t max_size() {
    return sizeof(u1) + sizeof(u2) + 3 * verification_type_info::max_size();
  }

  size_t size() const { return calculate_size(number_of_types(), types()); }
  int offset_delta() const {
    return Bytes::get_Java_u2(offset_delta_addr()) + 1;
  }

  void set_offset_delta(int offset_delta) {
    Bytes::put_Java_u2(offset_delta_addr(), offset_delta - 1);
  }

  void set_appends(int appends) {
    assert(appends > 0 && appends < 4, "Bad number of appends");
    set_frame_type(appends_to_frame_type(appends));
  }

  int number_of_types() const {
    int appends = frame_type_to_appends(frame_type());
    assert(appends > 0 && appends < 4, "Invalid number of appends in frame");
    return appends;
  }
  verification_type_info* types() const {
    return verification_type_info::at(types_addr());
  }
  bool is_valid_offset(int offset) const { return true; }

  bool verify_subtype(address start, address end) const {
    verification_type_info* vti = types();
    if ((address)vti < end && vti->verify(start, end)) {
      int nof = number_of_types();
      vti = vti->next();
      if (nof < 2 || vti->verify(start, end)) {
        vti = vti->next();
        if (nof < 3 || vti->verify(start, end)) {
          return true;
        }
      }
    }
    return false;
  }

610 611
  void print_on(outputStream* st, int current_offset = -1) const {
    st->print("append_frame(@%d,", offset_delta() + current_offset);
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
    verification_type_info* vti = types();
    for (int i = 0; i < number_of_types(); ++i) {
      vti->print_on(st);
      if (i != number_of_types() - 1) {
        st->print(",");
      }
      vti = vti->next();
    }
    st->print(")");
  }
};

class full_frame : public stack_map_frame {
 private:
  address offset_delta_addr() const { return frame_type_addr() + sizeof(u1); }
  address num_locals_addr() const { return offset_delta_addr() + sizeof(u2); }
  address locals_addr() const { return num_locals_addr() + sizeof(u2); }
  address stack_slots_addr(address end_of_locals) const {
      return end_of_locals; }
  address stack_addr(address end_of_locals) const {
      return stack_slots_addr(end_of_locals) + sizeof(u2); }

  enum { _frame_id = 255 };

 public:
  static bool is_frame_type(u1 tag) {
    return tag == _frame_id;
  }

  static full_frame* at(address addr) {
    assert(is_frame_type(*addr), "Wrong frame id");
    return (full_frame*)addr;
  }

  static full_frame* create_at(
      address addr, int offset_delta, int num_locals,
      verification_type_info* locals,
      int stack_slots, verification_type_info* stack) {
    full_frame* sm = (full_frame*)addr;
    sm->set_frame_type(_frame_id);
    sm->set_offset_delta(offset_delta);
    sm->set_num_locals(num_locals);
    if (locals != NULL) {
      verification_type_info* cur = sm->locals();
      for (int i = 0; i < num_locals; ++i) {
        cur->copy_from(locals);
        cur = cur->next();
        locals = locals->next();
      }
      address end_of_locals = (address)cur;
      sm->set_stack_slots(end_of_locals, stack_slots);
      cur = sm->stack(end_of_locals);
      for (int i = 0; i < stack_slots; ++i) {
        cur->copy_from(stack);
        cur = cur->next();
        stack = stack->next();
      }
    }
    return sm;
  }

  static size_t calculate_size(
      int num_locals, verification_type_info* locals,
      int stack_slots, verification_type_info* stack) {
    size_t sz = sizeof(u1) + sizeof(u2) + sizeof(u2) + sizeof(u2);
    verification_type_info* vti = locals;
    for (int i = 0; i < num_locals; ++i) {
      sz += vti->size();
      vti = vti->next();
    }
    vti = stack;
    for (int i = 0; i < stack_slots; ++i) {
      sz += vti->size();
      vti = vti->next();
    }
    return sz;
  }

  static size_t max_size(int locals, int stack) {
    return sizeof(u1) + 3 * sizeof(u2) +
        (locals + stack) * verification_type_info::max_size();
  }

  size_t size() const {
    address eol = end_of_locals();
    return calculate_size(num_locals(), locals(), stack_slots(eol), stack(eol));
  }

  int offset_delta() const {
    return Bytes::get_Java_u2(offset_delta_addr()) + 1;
  }
  int num_locals() const { return Bytes::get_Java_u2(num_locals_addr()); }
  verification_type_info* locals() const {
    return verification_type_info::at(locals_addr());
  }
  address end_of_locals() const {
    verification_type_info* vti = locals();
    for (int i = 0; i < num_locals(); ++i) {
      vti = vti->next();
    }
    return (address)vti;
  }
  int stack_slots(address end_of_locals) const {
    return Bytes::get_Java_u2(stack_slots_addr(end_of_locals));
  }
  verification_type_info* stack(address end_of_locals) const {
    return verification_type_info::at(stack_addr(end_of_locals));
  }

  void set_offset_delta(int offset_delta) {
    Bytes::put_Java_u2(offset_delta_addr(), offset_delta - 1);
  }
  void set_num_locals(int num_locals) {
    Bytes::put_Java_u2(num_locals_addr(), num_locals);
  }
  void set_stack_slots(address end_of_locals, int stack_slots) {
    Bytes::put_Java_u2(stack_slots_addr(end_of_locals), stack_slots);
  }

  // These return only the locals.  Extra processing is required for stack
  // types of full frames.
  int number_of_types() const { return num_locals(); }
  verification_type_info* types() const { return locals(); }
  bool is_valid_offset(int offset) { return true; }

  bool verify_subtype(address start, address end) const {
    verification_type_info* vti = types();
    if ((address)vti >= end) {
      return false;
    }
    int count = number_of_types();
    for (int i = 0; i < count; ++i) {
      if (!vti->verify(start, end)) {
        return false;
      }
      vti = vti->next();
    }
    address eol = (address)vti;
    if (eol + sizeof(u2) > end) {
      return false;
    }
    count = stack_slots(eol);
    vti = stack(eol);
    for (int i = 0; i < stack_slots(eol); ++i) {
      if (!vti->verify(start, end)) {
        return false;
      }
      vti = vti->next();
    }
    return true;
  }

764 765
  void print_on(outputStream* st, int current_offset = -1) const {
    st->print("full_frame(@%d,{", offset_delta() + current_offset);
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
    verification_type_info* vti = locals();
    for (int i = 0; i < num_locals(); ++i) {
      vti->print_on(st);
      if (i != num_locals() - 1) {
        st->print(",");
      }
      vti = vti->next();
    }
    st->print("},{");
    address end_of_locals = (address)vti;
    vti = stack(end_of_locals);
    int ss = stack_slots(end_of_locals);
    for (int i = 0; i < ss; ++i) {
      vti->print_on(st);
      if (i != ss - 1) {
        st->print(",");
      }
      vti = vti->next();
    }
    st->print("})");
  }
};

#define VIRTUAL_DISPATCH(stack_frame_type, func_name, args) \
  stack_frame_type* item_##stack_frame_type = as_##stack_frame_type(); \
  if (item_##stack_frame_type != NULL) { \
    return item_##stack_frame_type->func_name args;  \
  }

#define VOID_VIRTUAL_DISPATCH(stack_frame_type, func_name, args) \
  stack_frame_type* item_##stack_frame_type = as_##stack_frame_type(); \
  if (item_##stack_frame_type != NULL) { \
    item_##stack_frame_type->func_name args;  \
    return; \
  }

size_t stack_map_frame::size() const {
  FOR_EACH_STACKMAP_FRAME_TYPE(VIRTUAL_DISPATCH, size, ());
  return 0;
}

int stack_map_frame::offset_delta() const {
  FOR_EACH_STACKMAP_FRAME_TYPE(VIRTUAL_DISPATCH, offset_delta, ());
  return 0;
}

void stack_map_frame::set_offset_delta(int offset_delta) {
  FOR_EACH_STACKMAP_FRAME_TYPE(
      VOID_VIRTUAL_DISPATCH, set_offset_delta, (offset_delta));
}

int stack_map_frame::number_of_types() const {
  FOR_EACH_STACKMAP_FRAME_TYPE(VIRTUAL_DISPATCH, number_of_types, ());
  return 0;
}

verification_type_info* stack_map_frame::types() const {
  FOR_EACH_STACKMAP_FRAME_TYPE(VIRTUAL_DISPATCH, types, ());
  return NULL;
}

bool stack_map_frame::is_valid_offset(int offset) const {
  FOR_EACH_STACKMAP_FRAME_TYPE(VIRTUAL_DISPATCH, is_valid_offset, (offset));
  return true;
}

bool stack_map_frame::verify(address start, address end) const {
  if (frame_type_addr() >= start && frame_type_addr() < end) {
    FOR_EACH_STACKMAP_FRAME_TYPE(
       VIRTUAL_DISPATCH, verify_subtype, (start, end));
  }
  return false;
}

840 841
void stack_map_frame::print_on(outputStream* st, int offs = -1) const {
  FOR_EACH_STACKMAP_FRAME_TYPE(VOID_VIRTUAL_DISPATCH, print_on, (st, offs));
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
}

#undef VIRTUAL_DISPATCH
#undef VOID_VIRTUAL_DISPATCH

#define AS_SUBTYPE_DEF(stack_frame_type, arg1, arg2) \
stack_frame_type* stack_map_frame::as_##stack_frame_type() const { \
  if (stack_frame_type::is_frame_type(frame_type())) { \
    return (stack_frame_type*)this; \
  } else { \
    return NULL; \
  } \
}

FOR_EACH_STACKMAP_FRAME_TYPE(AS_SUBTYPE_DEF, x, x)
#undef AS_SUBTYPE_DEF

859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
class stack_map_table {
 private:
  address number_of_entries_addr() const {
    return (address)this;
  }
  address entries_addr() const {
    return number_of_entries_addr() + sizeof(u2);
  }

 protected:
  // No constructors  - should be 'private', but GCC issues a warning if it is
  stack_map_table() {}
  stack_map_table(const stack_map_table&) {}

 public:

  static stack_map_table* at(address addr) {
    return (stack_map_table*)addr;
  }

  u2 number_of_entries() const {
    return Bytes::get_Java_u2(number_of_entries_addr());
  }
  stack_map_frame* entries() const {
    return stack_map_frame::at(entries_addr());
  }

  void set_number_of_entries(u2 num) {
    Bytes::put_Java_u2(number_of_entries_addr(), num);
  }
};

891 892 893 894 895 896
class stack_map_table_attribute {
 private:
  address name_index_addr() const {
      return (address)this; }
  address attribute_length_addr() const {
      return name_index_addr() + sizeof(u2); }
897
  address stack_map_table_addr() const {
898 899 900 901 902 903 904 905 906 907 908 909 910 911
      return attribute_length_addr() + sizeof(u4); }

 protected:
  // No constructors  - should be 'private', but GCC issues a warning if it is
  stack_map_table_attribute() {}
  stack_map_table_attribute(const stack_map_table_attribute&) {}

 public:

  static stack_map_table_attribute* at(address addr) {
    return (stack_map_table_attribute*)addr;
  }

  u2 name_index() const {
912
    return Bytes::get_Java_u2(name_index_addr()); }
913
  u4 attribute_length() const {
914 915 916
    return Bytes::get_Java_u4(attribute_length_addr()); }
  stack_map_table* table() const {
    return stack_map_table::at(stack_map_table_addr());
917 918 919 920 921 922 923 924 925
  }

  void set_name_index(u2 idx) {
    Bytes::put_Java_u2(name_index_addr(), idx);
  }
  void set_attribute_length(u4 len) {
    Bytes::put_Java_u4(attribute_length_addr(), len);
  }
};
926

927 928
#undef FOR_EACH_STACKMAP_FRAME_TYPE

929
#endif // SHARE_VM_CLASSFILE_STACKMAPTABLEFORMAT_HPP