1 /*
   2  * Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_CLASSFILE_STACKMAPTABLEFORMAT_HPP
  26 #define SHARE_VM_CLASSFILE_STACKMAPTABLEFORMAT_HPP
  27 
  28 #include "classfile/verificationType.hpp"
  29 
  30 // These classes represent the stack-map substructures described in the JVMS
  31 // (hence the non-conforming naming scheme).
  32 
  33 // These classes work with the types in their compressed form in-place (as they
  34 // would appear in the classfile).  No virtual methods or fields allowed.
  35 
  36 class verification_type_info {
  37  private:
  38   // u1 tag
  39   // u2 cpool_index || u2 bci (for ITEM_Object & ITEM_Uninitailized only)
  40 
  41   address tag_addr() const { return (address)this; }
  42   address cpool_index_addr() const { return tag_addr() + sizeof(u1); }
  43   address bci_addr() const { return cpool_index_addr(); }
  44 
  45  protected:
  46   // No constructors  - should be 'private', but GCC issues a warning if it is
  47   verification_type_info() {}
  48   verification_type_info(const verification_type_info&) {}
  49 
  50  public:
  51 
  52   static verification_type_info* at(address addr) {
  53     return (verification_type_info*)addr;
  54   }
  55 
  56   static verification_type_info* create_at(address addr, u1 tag) {
  57     verification_type_info* vti = (verification_type_info*)addr;
  58     vti->set_tag(tag);
  59     return vti;
  60   }
  61 
  62   static verification_type_info* create_object_at(address addr, u2 cp_idx) {
  63     verification_type_info* vti = (verification_type_info*)addr;
  64     vti->set_tag(ITEM_Object);
  65     vti->set_cpool_index(cp_idx);
  66     return vti;
  67   }
  68 
  69   static verification_type_info* create_uninit_at(address addr, u2 bci) {
  70     verification_type_info* vti = (verification_type_info*)addr;
  71     vti->set_tag(ITEM_Uninitialized);
  72     vti->set_bci(bci);
  73     return vti;
  74   }
  75 
  76   static size_t calculate_size(u1 tag) {
  77     if (tag == ITEM_Object || tag == ITEM_Uninitialized) {
  78       return sizeof(u1) + sizeof(u2);
  79     } else {
  80       return sizeof(u1);
  81     }
  82   }
  83 
  84   static size_t max_size() { return sizeof(u1) + sizeof(u2); }
  85 
  86   u1 tag() const { return *(u1*)tag_addr(); }
  87   void set_tag(u1 tag) { *((u1*)tag_addr()) = tag; }
  88 
  89   bool is_object() const { return tag() == ITEM_Object; }
  90   bool is_uninitialized() const { return tag() == ITEM_Uninitialized; }
  91 
  92   u2 cpool_index() const {
  93     assert(is_object(), "This type has no cp_index");
  94     return Bytes::get_Java_u2(cpool_index_addr());
  95   }
  96   void set_cpool_index(u2 idx) {
  97     assert(is_object(), "This type has no cp_index");
  98     Bytes::put_Java_u2(cpool_index_addr(), idx);
  99   }
 100 
 101   u2 bci() const {
 102     assert(is_uninitialized(), "This type has no bci");
 103     return Bytes::get_Java_u2(bci_addr());
 104   }
 105 
 106   void set_bci(u2 bci) {
 107     assert(is_uninitialized(), "This type has no bci");
 108     Bytes::put_Java_u2(bci_addr(), bci);
 109   }
 110 
 111   void copy_from(verification_type_info* from) {
 112     set_tag(from->tag());
 113     if (from->is_object()) {
 114       set_cpool_index(from->cpool_index());
 115     } else if (from->is_uninitialized()) {
 116       set_bci(from->bci());
 117     }
 118   }
 119 
 120   size_t size() const {
 121     return calculate_size(tag());
 122   }
 123 
 124   verification_type_info* next() {
 125     return (verification_type_info*)((address)this + size());
 126   }
 127 
 128   // This method is used when reading unverified data in order to ensure
 129   // that we don't read past a particular memory limit.  It returns false
 130   // if any part of the data structure is outside the specified memory bounds.
 131   bool verify(address start, address end) {
 132     return ((address)this >= start &&
 133             (address)this < end &&
 134             (bci_addr() + sizeof(u2) <= end ||
 135                 !is_object() && !is_uninitialized()));
 136   }
 137 
 138   void print_on(outputStream* st) {
 139     switch (tag()) {
 140       case ITEM_Top: st->print("Top"); break;
 141       case ITEM_Integer: st->print("Integer"); break;
 142       case ITEM_Float: st->print("Float"); break;
 143       case ITEM_Double: st->print("Double"); break;
 144       case ITEM_Long: st->print("Long"); break;
 145       case ITEM_Null: st->print("Null"); break;
 146       case ITEM_UninitializedThis:
 147         st->print("UninitializedThis"); break;
 148       case ITEM_Uninitialized:
 149         st->print("Uninitialized[#%d]", bci()); break;
 150       case ITEM_Object:
 151         st->print("Object[#%d]", cpool_index()); break;
 152       default:
 153         assert(false, "Bad verification_type_info");
 154     }
 155   }
 156 };
 157 
 158 #define FOR_EACH_STACKMAP_FRAME_TYPE(macro, arg1, arg2) \
 159   macro(same_frame, arg1, arg2) \
 160   macro(same_frame_extended, arg1, arg2) \
 161   macro(same_locals_1_stack_item_frame, arg1, arg2) \
 162   macro(same_locals_1_stack_item_extended, arg1, arg2) \
 163   macro(chop_frame, arg1, arg2) \
 164   macro(append_frame, arg1, arg2) \
 165   macro(full_frame, arg1, arg2)
 166 
 167 #define SM_FORWARD_DECL(type, arg1, arg2) class type;
 168 FOR_EACH_STACKMAP_FRAME_TYPE(SM_FORWARD_DECL, x, x)
 169 #undef SM_FORWARD_DECL
 170 
 171 class stack_map_frame {
 172  protected:
 173   address frame_type_addr() const { return (address)this; }
 174 
 175   // No constructors  - should be 'private', but GCC issues a warning if it is
 176   stack_map_frame() {}
 177   stack_map_frame(const stack_map_frame&) {}
 178 
 179  public:
 180 
 181   static stack_map_frame* at(address addr) {
 182     return (stack_map_frame*)addr;
 183   }
 184 
 185   stack_map_frame* next() const {
 186     return at((address)this + size());
 187   }
 188 
 189   u1 frame_type() const { return *(u1*)frame_type_addr(); }
 190   void set_frame_type(u1 type) { *((u1*)frame_type_addr()) = type; }
 191 
 192   // pseudo-virtual methods
 193   inline size_t size() const;
 194   inline int offset_delta() const;
 195   inline void set_offset_delta(int offset_delta);
 196   inline int number_of_types() const; // number of types contained in the frame
 197   inline verification_type_info* types() const; // pointer to first type
 198   inline bool is_valid_offset(int offset_delta) const;
 199 
 200   // This method must be used when reading unverified data in order to ensure
 201   // that we don't read past a particular memory limit.  It returns false
 202   // if any part of the data structure is outside the specified memory bounds.
 203   inline bool verify(address start, address end) const;
 204 
 205   inline void print_on(outputStream* st, int current_offset) const;
 206   inline void print_truncated(outputStream* st, int current_offset) const;
 207 
 208   // Create as_xxx and is_xxx methods for the subtypes
 209 #define FRAME_TYPE_DECL(stackmap_frame_type, arg1, arg2) \
 210   inline stackmap_frame_type* as_##stackmap_frame_type() const; \
 211   bool is_##stackmap_frame_type() { \
 212     return as_##stackmap_frame_type() != NULL; \
 213   }
 214 
 215   FOR_EACH_STACKMAP_FRAME_TYPE(FRAME_TYPE_DECL, x, x)
 216 #undef FRAME_TYPE_DECL
 217 };
 218 
 219 class same_frame : public stack_map_frame {
 220  private:
 221   static int frame_type_to_offset_delta(u1 frame_type) {
 222       return frame_type + 1; }
 223   static u1 offset_delta_to_frame_type(int offset_delta) {
 224       return (u1)(offset_delta - 1); }
 225 
 226  public:
 227 
 228   static bool is_frame_type(u1 tag) {
 229     return tag < 64;
 230   }
 231 
 232   static same_frame* at(address addr) {
 233     assert(is_frame_type(*addr), "Wrong frame id");
 234     return (same_frame*)addr;
 235   }
 236 
 237   static same_frame* create_at(address addr, int offset_delta) {
 238     same_frame* sm = (same_frame*)addr;
 239     sm->set_offset_delta(offset_delta);
 240     return sm;
 241   }
 242 
 243   static size_t calculate_size() { return sizeof(u1); }
 244 
 245   size_t size() const { return calculate_size(); }
 246   int offset_delta() const { return frame_type_to_offset_delta(frame_type()); }
 247 
 248   void set_offset_delta(int offset_delta) {
 249     assert(offset_delta <= 64, "Offset too large for same_frame");
 250     set_frame_type(offset_delta_to_frame_type(offset_delta));
 251   }
 252 
 253   int number_of_types() const { return 0; }
 254   verification_type_info* types() const { return NULL; }
 255 
 256   bool is_valid_offset(int offset_delta) const {
 257     return is_frame_type(offset_delta_to_frame_type(offset_delta));
 258   }
 259 
 260   bool verify_subtype(address start, address end) const {
 261     return true;
 262   }
 263 
 264   void print_on(outputStream* st, int current_offset = -1) const {
 265     st->print("same_frame(@%d)", offset_delta() + current_offset);
 266   }
 267 
 268   void print_truncated(outputStream* st, int current_offset = -1) const {
 269     print_on(st, current_offset);
 270   }
 271 };
 272 
 273 class same_frame_extended : public stack_map_frame {
 274  private:
 275   enum { _frame_id = 251 };
 276   address offset_delta_addr() const { return frame_type_addr() + sizeof(u1); }
 277 
 278  public:
 279   static bool is_frame_type(u1 tag) {
 280     return tag == _frame_id;
 281   }
 282 
 283   static same_frame_extended* at(address addr) {
 284     assert(is_frame_type(*addr), "Wrong frame type");
 285     return (same_frame_extended*)addr;
 286   }
 287 
 288   static same_frame_extended* create_at(address addr, u2 offset_delta) {
 289     same_frame_extended* sm = (same_frame_extended*)addr;
 290     sm->set_frame_type(_frame_id);
 291     sm->set_offset_delta(offset_delta);
 292     return sm;
 293   }
 294 
 295   static size_t calculate_size() { return sizeof(u1) + sizeof(u2); }
 296 
 297   size_t size() const { return calculate_size(); }
 298   int offset_delta() const {
 299     return Bytes::get_Java_u2(offset_delta_addr()) + 1;
 300   }
 301 
 302   void set_offset_delta(int offset_delta) {
 303     Bytes::put_Java_u2(offset_delta_addr(), offset_delta - 1);
 304   }
 305 
 306   int number_of_types() const { return 0; }
 307   verification_type_info* types() const { return NULL; }
 308   bool is_valid_offset(int offset) const { return true; }
 309 
 310   bool verify_subtype(address start, address end) const {
 311     return frame_type_addr() + size() <= end;
 312   }
 313 
 314   void print_on(outputStream* st, int current_offset = -1) const {
 315     st->print("same_frame_extended(@%d)", offset_delta() + current_offset);
 316   }
 317 
 318   void print_truncated(outputStream* st, int current_offset = -1) const {
 319     print_on(st, current_offset);
 320   }
 321 };
 322 
 323 class same_locals_1_stack_item_frame : public stack_map_frame {
 324  private:
 325   address type_addr() const { return frame_type_addr() + sizeof(u1); }
 326 
 327   static int frame_type_to_offset_delta(u1 frame_type) {
 328       return frame_type - 63; }
 329   static u1 offset_delta_to_frame_type(int offset_delta) {
 330       return (u1)(offset_delta + 63); }
 331 
 332  public:
 333   static bool is_frame_type(u1 tag) {
 334     return tag >= 64 && tag < 128;
 335   }
 336 
 337   static same_locals_1_stack_item_frame* at(address addr) {
 338     assert(is_frame_type(*addr), "Wrong frame id");
 339     return (same_locals_1_stack_item_frame*)addr;
 340   }
 341 
 342   static same_locals_1_stack_item_frame* create_at(
 343       address addr, int offset_delta, verification_type_info* vti) {
 344     same_locals_1_stack_item_frame* sm = (same_locals_1_stack_item_frame*)addr;
 345     sm->set_offset_delta(offset_delta);
 346     if (vti != NULL) {
 347       sm->set_type(vti);
 348     }
 349     return sm;
 350   }
 351 
 352   static size_t calculate_size(verification_type_info* vti) {
 353     return sizeof(u1) + vti->size();
 354   }
 355 
 356   static size_t max_size() {
 357     return sizeof(u1) + verification_type_info::max_size();
 358   }
 359 
 360   size_t size() const { return calculate_size(types()); }
 361   int offset_delta() const { return frame_type_to_offset_delta(frame_type()); }
 362 
 363   void set_offset_delta(int offset_delta) {
 364     assert(offset_delta > 0 && offset_delta <= 64,
 365            "Offset too large for this frame type");
 366     set_frame_type(offset_delta_to_frame_type(offset_delta));
 367   }
 368 
 369   void set_type(verification_type_info* vti) {
 370     verification_type_info* cur = types();
 371     cur->copy_from(vti);
 372   }
 373 
 374   int number_of_types() const { return 1; }
 375   verification_type_info* types() const {
 376     return verification_type_info::at(type_addr());
 377   }
 378 
 379   bool is_valid_offset(int offset_delta) const {
 380     return is_frame_type(offset_delta_to_frame_type(offset_delta));
 381   }
 382 
 383   bool verify_subtype(address start, address end) const {
 384     return types()->verify(start, end);
 385   }
 386 
 387   void print_on(outputStream* st, int current_offset = -1) const {
 388     st->print("same_locals_1_stack_item_frame(@%d,",
 389         offset_delta() + current_offset);
 390     types()->print_on(st);
 391     st->print(")");
 392   }
 393 
 394   void print_truncated(outputStream* st, int current_offset = -1) const {
 395     st->print("same_locals_1_stack_item_frame(@%d), output truncated, Stackmap exceeds table size.",
 396               offset_delta() + current_offset);
 397   }
 398 };
 399 
 400 class same_locals_1_stack_item_extended : public stack_map_frame {
 401  private:
 402   address offset_delta_addr() const { return frame_type_addr() + sizeof(u1); }
 403   address type_addr() const { return offset_delta_addr() + sizeof(u2); }
 404 
 405   enum { _frame_id = 247 };
 406 
 407  public:
 408   static bool is_frame_type(u1 tag) {
 409     return tag == _frame_id;
 410   }
 411 
 412   static same_locals_1_stack_item_extended* at(address addr) {
 413     assert(is_frame_type(*addr), "Wrong frame id");
 414     return (same_locals_1_stack_item_extended*)addr;
 415   }
 416 
 417   static same_locals_1_stack_item_extended* create_at(
 418       address addr, int offset_delta, verification_type_info* vti) {
 419     same_locals_1_stack_item_extended* sm =
 420        (same_locals_1_stack_item_extended*)addr;
 421     sm->set_frame_type(_frame_id);
 422     sm->set_offset_delta(offset_delta);
 423     if (vti != NULL) {
 424       sm->set_type(vti);
 425     }
 426     return sm;
 427   }
 428 
 429   static size_t calculate_size(verification_type_info* vti) {
 430     return sizeof(u1) + sizeof(u2) + vti->size();
 431   }
 432 
 433   size_t size() const { return calculate_size(types()); }
 434   int offset_delta() const {
 435     return Bytes::get_Java_u2(offset_delta_addr()) + 1;
 436   }
 437 
 438   void set_offset_delta(int offset_delta) {
 439     Bytes::put_Java_u2(offset_delta_addr(), offset_delta - 1);
 440   }
 441 
 442   void set_type(verification_type_info* vti) {
 443     verification_type_info* cur = types();
 444     cur->copy_from(vti);
 445   }
 446 
 447   int number_of_types() const { return 1; }
 448   verification_type_info* types() const {
 449     return verification_type_info::at(type_addr());
 450   }
 451   bool is_valid_offset(int offset) { return true; }
 452 
 453   bool verify_subtype(address start, address end) const {
 454     return type_addr() < end && types()->verify(start, end);
 455   }
 456 
 457   void print_on(outputStream* st, int current_offset = -1) const {
 458     st->print("same_locals_1_stack_item_extended(@%d,",
 459         offset_delta() + current_offset);
 460     types()->print_on(st);
 461     st->print(")");
 462   }
 463 
 464   void print_truncated(outputStream* st, int current_offset = -1) const {
 465     st->print("same_locals_1_stack_item_extended(@%d), output truncated, Stackmap exceeds table size.",
 466               offset_delta() + current_offset);
 467   }
 468 };
 469 
 470 class chop_frame : public stack_map_frame {
 471  private:
 472   address offset_delta_addr() const { return frame_type_addr() + sizeof(u1); }
 473 
 474   static int frame_type_to_chops(u1 frame_type) {
 475     int chop = 251 - frame_type;
 476     return chop;
 477   }
 478 
 479   static u1 chops_to_frame_type(int chop) {
 480     return 251 - chop;
 481   }
 482 
 483  public:
 484   static bool is_frame_type(u1 tag) {
 485     return frame_type_to_chops(tag) > 0 && frame_type_to_chops(tag) < 4;
 486   }
 487 
 488   static chop_frame* at(address addr) {
 489     assert(is_frame_type(*addr), "Wrong frame id");
 490     return (chop_frame*)addr;
 491   }
 492 
 493   static chop_frame* create_at(address addr, int offset_delta, int chops) {
 494     chop_frame* sm = (chop_frame*)addr;
 495     sm->set_chops(chops);
 496     sm->set_offset_delta(offset_delta);
 497     return sm;
 498   }
 499 
 500   static size_t calculate_size() {
 501     return sizeof(u1) + sizeof(u2);
 502   }
 503 
 504   size_t size() const { return calculate_size(); }
 505   int offset_delta() const {
 506     return Bytes::get_Java_u2(offset_delta_addr()) + 1;
 507   }
 508   void set_offset_delta(int offset_delta) {
 509     Bytes::put_Java_u2(offset_delta_addr(), offset_delta - 1);
 510   }
 511 
 512   int chops() const {
 513     int chops = frame_type_to_chops(frame_type());
 514     assert(chops > 0 && chops < 4, "Invalid number of chops in frame");
 515     return chops;
 516   }
 517   void set_chops(int chops) {
 518     assert(chops > 0 && chops <= 3, "Bad number of chops");
 519     set_frame_type(chops_to_frame_type(chops));
 520   }
 521 
 522   int number_of_types() const { return 0; }
 523   verification_type_info* types() const { return NULL; }
 524   bool is_valid_offset(int offset) { return true; }
 525 
 526   bool verify_subtype(address start, address end) const {
 527     return frame_type_addr() + size() <= end;
 528   }
 529 
 530   void print_on(outputStream* st, int current_offset = -1) const {
 531     st->print("chop_frame(@%d,%d)", offset_delta() + current_offset, chops());
 532   }
 533 
 534   void print_truncated(outputStream* st, int current_offset = -1) const {
 535     print_on(st, current_offset);
 536   }
 537 };
 538 
 539 class append_frame : public stack_map_frame {
 540  private:
 541   address offset_delta_addr() const { return frame_type_addr() + sizeof(u1); }
 542   address types_addr() const { return offset_delta_addr() + sizeof(u2); }
 543 
 544   static int frame_type_to_appends(u1 frame_type) {
 545     int append = frame_type - 251;
 546     return append;
 547   }
 548 
 549   static u1 appends_to_frame_type(int appends) {
 550     assert(appends > 0 && appends < 4, "Invalid append amount");
 551     return 251 + appends;
 552   }
 553 
 554  public:
 555   static bool is_frame_type(u1 tag) {
 556     return frame_type_to_appends(tag) > 0 && frame_type_to_appends(tag) < 4;
 557   }
 558 
 559   static append_frame* at(address addr) {
 560     assert(is_frame_type(*addr), "Wrong frame id");
 561     return (append_frame*)addr;
 562   }
 563 
 564   static append_frame* create_at(
 565       address addr, int offset_delta, int appends,
 566       verification_type_info* types) {
 567     append_frame* sm = (append_frame*)addr;
 568     sm->set_appends(appends);
 569     sm->set_offset_delta(offset_delta);
 570     if (types != NULL) {
 571       verification_type_info* cur = sm->types();
 572       for (int i = 0; i < appends; ++i) {
 573         cur->copy_from(types);
 574         cur = cur->next();
 575         types = types->next();
 576       }
 577     }
 578     return sm;
 579   }
 580 
 581   static size_t calculate_size(int appends, verification_type_info* types) {
 582     size_t sz = sizeof(u1) + sizeof(u2);
 583     for (int i = 0; i < appends; ++i) {
 584       sz += types->size();
 585       types = types->next();
 586     }
 587     return sz;
 588   }
 589 
 590   static size_t max_size() {
 591     return sizeof(u1) + sizeof(u2) + 3 * verification_type_info::max_size();
 592   }
 593 
 594   size_t size() const { return calculate_size(number_of_types(), types()); }
 595   int offset_delta() const {
 596     return Bytes::get_Java_u2(offset_delta_addr()) + 1;
 597   }
 598 
 599   void set_offset_delta(int offset_delta) {
 600     Bytes::put_Java_u2(offset_delta_addr(), offset_delta - 1);
 601   }
 602 
 603   void set_appends(int appends) {
 604     assert(appends > 0 && appends < 4, "Bad number of appends");
 605     set_frame_type(appends_to_frame_type(appends));
 606   }
 607 
 608   int number_of_types() const {
 609     int appends = frame_type_to_appends(frame_type());
 610     assert(appends > 0 && appends < 4, "Invalid number of appends in frame");
 611     return appends;
 612   }
 613   verification_type_info* types() const {
 614     return verification_type_info::at(types_addr());
 615   }
 616   bool is_valid_offset(int offset) const { return true; }
 617 
 618   bool verify_subtype(address start, address end) const {
 619     verification_type_info* vti = types();
 620     if ((address)vti < end && vti->verify(start, end)) {
 621       int nof = number_of_types();
 622       vti = vti->next();
 623       if (nof < 2 || vti->verify(start, end)) {
 624         vti = vti->next();
 625         if (nof < 3 || vti->verify(start, end)) {
 626           return true;
 627         }
 628       }
 629     }
 630     return false;
 631   }
 632 
 633   void print_on(outputStream* st, int current_offset = -1) const {
 634     st->print("append_frame(@%d,", offset_delta() + current_offset);
 635     verification_type_info* vti = types();
 636     for (int i = 0; i < number_of_types(); ++i) {
 637       vti->print_on(st);
 638       if (i != number_of_types() - 1) {
 639         st->print(",");
 640       }
 641       vti = vti->next();
 642     }
 643     st->print(")");
 644   }
 645 
 646   void print_truncated(outputStream* st, int current_offset = -1) const {
 647     st->print("append_frame(@%d), output truncated, Stackmap exceeds table size.",
 648               offset_delta() + current_offset);
 649   }
 650 };
 651 
 652 class full_frame : public stack_map_frame {
 653  private:
 654   address offset_delta_addr() const { return frame_type_addr() + sizeof(u1); }
 655   address num_locals_addr() const { return offset_delta_addr() + sizeof(u2); }
 656   address locals_addr() const { return num_locals_addr() + sizeof(u2); }
 657   address stack_slots_addr(address end_of_locals) const {
 658       return end_of_locals; }
 659   address stack_addr(address end_of_locals) const {
 660       return stack_slots_addr(end_of_locals) + sizeof(u2); }
 661 
 662   enum { _frame_id = 255 };
 663 
 664  public:
 665   static bool is_frame_type(u1 tag) {
 666     return tag == _frame_id;
 667   }
 668 
 669   static full_frame* at(address addr) {
 670     assert(is_frame_type(*addr), "Wrong frame id");
 671     return (full_frame*)addr;
 672   }
 673 
 674   static full_frame* create_at(
 675       address addr, int offset_delta, int num_locals,
 676       verification_type_info* locals,
 677       int stack_slots, verification_type_info* stack) {
 678     full_frame* sm = (full_frame*)addr;
 679     sm->set_frame_type(_frame_id);
 680     sm->set_offset_delta(offset_delta);
 681     sm->set_num_locals(num_locals);
 682     if (locals != NULL) {
 683       verification_type_info* cur = sm->locals();
 684       for (int i = 0; i < num_locals; ++i) {
 685         cur->copy_from(locals);
 686         cur = cur->next();
 687         locals = locals->next();
 688       }
 689       address end_of_locals = (address)cur;
 690       sm->set_stack_slots(end_of_locals, stack_slots);
 691       cur = sm->stack(end_of_locals);
 692       for (int i = 0; i < stack_slots; ++i) {
 693         cur->copy_from(stack);
 694         cur = cur->next();
 695         stack = stack->next();
 696       }
 697     }
 698     return sm;
 699   }
 700 
 701   static size_t calculate_size(
 702       int num_locals, verification_type_info* locals,
 703       int stack_slots, verification_type_info* stack) {
 704     size_t sz = sizeof(u1) + sizeof(u2) + sizeof(u2) + sizeof(u2);
 705     verification_type_info* vti = locals;
 706     for (int i = 0; i < num_locals; ++i) {
 707       sz += vti->size();
 708       vti = vti->next();
 709     }
 710     vti = stack;
 711     for (int i = 0; i < stack_slots; ++i) {
 712       sz += vti->size();
 713       vti = vti->next();
 714     }
 715     return sz;
 716   }
 717 
 718   static size_t max_size(int locals, int stack) {
 719     return sizeof(u1) + 3 * sizeof(u2) +
 720         (locals + stack) * verification_type_info::max_size();
 721   }
 722 
 723   size_t size() const {
 724     address eol = end_of_locals();
 725     return calculate_size(num_locals(), locals(), stack_slots(eol), stack(eol));
 726   }
 727 
 728   int offset_delta() const {
 729     return Bytes::get_Java_u2(offset_delta_addr()) + 1;
 730   }
 731   int num_locals() const { return Bytes::get_Java_u2(num_locals_addr()); }
 732   verification_type_info* locals() const {
 733     return verification_type_info::at(locals_addr());
 734   }
 735   address end_of_locals() const {
 736     verification_type_info* vti = locals();
 737     for (int i = 0; i < num_locals(); ++i) {
 738       vti = vti->next();
 739     }
 740     return (address)vti;
 741   }
 742   int stack_slots(address end_of_locals) const {
 743     return Bytes::get_Java_u2(stack_slots_addr(end_of_locals));
 744   }
 745   verification_type_info* stack(address end_of_locals) const {
 746     return verification_type_info::at(stack_addr(end_of_locals));
 747   }
 748 
 749   void set_offset_delta(int offset_delta) {
 750     Bytes::put_Java_u2(offset_delta_addr(), offset_delta - 1);
 751   }
 752   void set_num_locals(int num_locals) {
 753     Bytes::put_Java_u2(num_locals_addr(), num_locals);
 754   }
 755   void set_stack_slots(address end_of_locals, int stack_slots) {
 756     Bytes::put_Java_u2(stack_slots_addr(end_of_locals), stack_slots);
 757   }
 758 
 759   // These return only the locals.  Extra processing is required for stack
 760   // types of full frames.
 761   int number_of_types() const { return num_locals(); }
 762   verification_type_info* types() const { return locals(); }
 763   bool is_valid_offset(int offset) { return true; }
 764 
 765   bool verify_subtype(address start, address end) const {
 766     verification_type_info* vti = types();
 767     if ((address)vti >= end) {
 768       return false;
 769     }
 770     int count = number_of_types();
 771     for (int i = 0; i < count; ++i) {
 772       if (!vti->verify(start, end)) {
 773         return false;
 774       }
 775       vti = vti->next();
 776     }
 777     address eol = (address)vti;
 778     if (eol + sizeof(u2) > end) {
 779       return false;
 780     }
 781     count = stack_slots(eol);
 782     vti = stack(eol);
 783     for (int i = 0; i < stack_slots(eol); ++i) {
 784       if (!vti->verify(start, end)) {
 785         return false;
 786       }
 787       vti = vti->next();
 788     }
 789     return true;
 790   }
 791 
 792   void print_on(outputStream* st, int current_offset = -1) const {
 793     st->print("full_frame(@%d,{", offset_delta() + current_offset);
 794     verification_type_info* vti = locals();
 795     for (int i = 0; i < num_locals(); ++i) {
 796       vti->print_on(st);
 797       if (i != num_locals() - 1) {
 798         st->print(",");
 799       }
 800       vti = vti->next();
 801     }
 802     st->print("},{");
 803     address end_of_locals = (address)vti;
 804     vti = stack(end_of_locals);
 805     int ss = stack_slots(end_of_locals);
 806     for (int i = 0; i < ss; ++i) {
 807       vti->print_on(st);
 808       if (i != ss - 1) {
 809         st->print(",");
 810       }
 811       vti = vti->next();
 812     }
 813     st->print("})");
 814   }
 815 
 816   void print_truncated(outputStream* st, int current_offset = -1) const {
 817     st->print("full_frame(@%d), output truncated, Stackmap exceeds table size.",
 818               offset_delta() + current_offset);
 819   }
 820 };
 821 
 822 #define VIRTUAL_DISPATCH(stack_frame_type, func_name, args) \
 823   stack_frame_type* item_##stack_frame_type = as_##stack_frame_type(); \
 824   if (item_##stack_frame_type != NULL) { \
 825     return item_##stack_frame_type->func_name args;  \
 826   }
 827 
 828 #define VOID_VIRTUAL_DISPATCH(stack_frame_type, func_name, args) \
 829   stack_frame_type* item_##stack_frame_type = as_##stack_frame_type(); \
 830   if (item_##stack_frame_type != NULL) { \
 831     item_##stack_frame_type->func_name args;  \
 832     return; \
 833   }
 834 
 835 size_t stack_map_frame::size() const {
 836   FOR_EACH_STACKMAP_FRAME_TYPE(VIRTUAL_DISPATCH, size, ());
 837   return 0;
 838 }
 839 
 840 int stack_map_frame::offset_delta() const {
 841   FOR_EACH_STACKMAP_FRAME_TYPE(VIRTUAL_DISPATCH, offset_delta, ());
 842   return 0;
 843 }
 844 
 845 void stack_map_frame::set_offset_delta(int offset_delta) {
 846   FOR_EACH_STACKMAP_FRAME_TYPE(
 847       VOID_VIRTUAL_DISPATCH, set_offset_delta, (offset_delta));
 848 }
 849 
 850 int stack_map_frame::number_of_types() const {
 851   FOR_EACH_STACKMAP_FRAME_TYPE(VIRTUAL_DISPATCH, number_of_types, ());
 852   return 0;
 853 }
 854 
 855 verification_type_info* stack_map_frame::types() const {
 856   FOR_EACH_STACKMAP_FRAME_TYPE(VIRTUAL_DISPATCH, types, ());
 857   return NULL;
 858 }
 859 
 860 bool stack_map_frame::is_valid_offset(int offset) const {
 861   FOR_EACH_STACKMAP_FRAME_TYPE(VIRTUAL_DISPATCH, is_valid_offset, (offset));
 862   return true;
 863 }
 864 
 865 bool stack_map_frame::verify(address start, address end) const {
 866   if (frame_type_addr() >= start && frame_type_addr() < end) {
 867     FOR_EACH_STACKMAP_FRAME_TYPE(
 868        VIRTUAL_DISPATCH, verify_subtype, (start, end));
 869   }
 870   return false;
 871 }
 872 
 873 void stack_map_frame::print_on(outputStream* st, int offs = -1) const {
 874   FOR_EACH_STACKMAP_FRAME_TYPE(VOID_VIRTUAL_DISPATCH, print_on, (st, offs));
 875 }
 876 
 877 void stack_map_frame::print_truncated(outputStream* st, int offs = -1) const {
 878   FOR_EACH_STACKMAP_FRAME_TYPE(VOID_VIRTUAL_DISPATCH, print_truncated, (st, offs));
 879 }
 880 
 881 #undef VIRTUAL_DISPATCH
 882 #undef VOID_VIRTUAL_DISPATCH
 883 
 884 #define AS_SUBTYPE_DEF(stack_frame_type, arg1, arg2) \
 885 stack_frame_type* stack_map_frame::as_##stack_frame_type() const { \
 886   if (stack_frame_type::is_frame_type(frame_type())) { \
 887     return (stack_frame_type*)this; \
 888   } else { \
 889     return NULL; \
 890   } \
 891 }
 892 
 893 FOR_EACH_STACKMAP_FRAME_TYPE(AS_SUBTYPE_DEF, x, x)
 894 #undef AS_SUBTYPE_DEF
 895 
 896 class stack_map_table {
 897  private:
 898   address number_of_entries_addr() const {
 899     return (address)this;
 900   }
 901   address entries_addr() const {
 902     return number_of_entries_addr() + sizeof(u2);
 903   }
 904 
 905  protected:
 906   // No constructors  - should be 'private', but GCC issues a warning if it is
 907   stack_map_table() {}
 908   stack_map_table(const stack_map_table&) {}
 909 
 910  public:
 911 
 912   static stack_map_table* at(address addr) {
 913     return (stack_map_table*)addr;
 914   }
 915 
 916   u2 number_of_entries() const {
 917     return Bytes::get_Java_u2(number_of_entries_addr());
 918   }
 919   stack_map_frame* entries() const {
 920     return stack_map_frame::at(entries_addr());
 921   }
 922 
 923   void set_number_of_entries(u2 num) {
 924     Bytes::put_Java_u2(number_of_entries_addr(), num);
 925   }
 926 };
 927 
 928 class stack_map_table_attribute {
 929  private:
 930   address name_index_addr() const {
 931       return (address)this; }
 932   address attribute_length_addr() const {
 933       return name_index_addr() + sizeof(u2); }
 934   address stack_map_table_addr() const {
 935       return attribute_length_addr() + sizeof(u4); }
 936 
 937  protected:
 938   // No constructors  - should be 'private', but GCC issues a warning if it is
 939   stack_map_table_attribute() {}
 940   stack_map_table_attribute(const stack_map_table_attribute&) {}
 941 
 942  public:
 943 
 944   static stack_map_table_attribute* at(address addr) {
 945     return (stack_map_table_attribute*)addr;
 946   }
 947 
 948   u2 name_index() const {
 949     return Bytes::get_Java_u2(name_index_addr()); }
 950   u4 attribute_length() const {
 951     return Bytes::get_Java_u4(attribute_length_addr()); }
 952   stack_map_table* table() const {
 953     return stack_map_table::at(stack_map_table_addr());
 954   }
 955 
 956   void set_name_index(u2 idx) {
 957     Bytes::put_Java_u2(name_index_addr(), idx);
 958   }
 959   void set_attribute_length(u4 len) {
 960     Bytes::put_Java_u4(attribute_length_addr(), len);
 961   }
 962 };
 963 
 964 #undef FOR_EACH_STACKMAP_FRAME_TYPE
 965 
 966 #endif // SHARE_VM_CLASSFILE_STACKMAPTABLEFORMAT_HPP