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