1 /*
   2  * Copyright (c) 2003, 2019, 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 /*
  26  * This is to provide sanity check in jhelper.d which compares SCCS
  27  * versions of generateJvmOffsets.cpp used to create and extract
  28  * contents of __JvmOffsets[] table.
  29  * The __JvmOffsets[] table is located in generated JvmOffsets.cpp.
  30  *
  31  * GENOFFS_SCCS_VER 34
  32  */
  33 
  34 #include <stdio.h>
  35 #include <strings.h>
  36 
  37 /* A workaround for private and protected fields */
  38 #define private   public
  39 #define protected public
  40 
  41 #include <proc_service.h>
  42 #include "gc/shared/collectedHeap.hpp"
  43 #include "memory/heap.hpp"
  44 #include "oops/compressedOops.hpp"
  45 #include "runtime/vmStructs.hpp"
  46 
  47 typedef enum GEN_variant {
  48         GEN_OFFSET = 0,
  49         GEN_INDEX  = 1,
  50         GEN_TABLE  = 2
  51 } GEN_variant;
  52 
  53 #ifdef COMPILER1
  54 #ifdef ASSERT
  55 
  56 /*
  57  * To avoid the most part of potential link errors
  58  * we link this program with -z nodefs .
  59  *
  60  * But for 'debug1' and 'fastdebug1' we still have to provide
  61  * a particular workaround for the following symbols below.
  62  * It will be good to find out a generic way in the future.
  63  */
  64 
  65 #pragma weak tty
  66 
  67 #if defined(i386) || defined(__i386) || defined(__amd64)
  68 #pragma weak noreg
  69 #endif /* i386 */
  70 
  71 LIR_Opr LIR_OprFact::illegalOpr = (LIR_Opr) 0;
  72 
  73 address StubRoutines::_call_stub_return_address = NULL;
  74 
  75 StubQueue* AbstractInterpreter::_code = NULL;
  76 
  77 #endif /* ASSERT */
  78 #endif /* COMPILER1 */
  79 
  80 #define GEN_OFFS_NAME(Type,Name,OutputType)             \
  81   switch(gen_variant) {                                 \
  82   case GEN_OFFSET:                                      \
  83     printf("#define OFFSET_%-33s %ld\n",                \
  84             #OutputType #Name, offset_of(Type, Name));  \
  85     break;                                              \
  86   case GEN_INDEX:                                       \
  87     printf("#define IDX_OFFSET_%-33s %d\n",             \
  88             #OutputType #Name, index++);                \
  89     break;                                              \
  90   case GEN_TABLE:                                       \
  91     printf("\tOFFSET_%s,\n", #OutputType #Name);        \
  92     break;                                              \
  93   }
  94 
  95 #define GEN_OFFS(Type,Name)                             \
  96   GEN_OFFS_NAME(Type,Name,Type)
  97 
  98 #define GEN_SIZE(Type)                                  \
  99   switch(gen_variant) {                                 \
 100   case GEN_OFFSET:                                      \
 101     printf("#define SIZE_%-35s %ld\n",                  \
 102             #Type, sizeof(Type));                       \
 103     break;                                              \
 104   case GEN_INDEX:                                       \
 105     printf("#define IDX_SIZE_%-35s %d\n",               \
 106             #Type, index++);                            \
 107     break;                                              \
 108   case GEN_TABLE:                                       \
 109     printf("\tSIZE_%s,\n", #Type);                      \
 110     break;                                              \
 111   }
 112 
 113 #define GEN_VALUE(String,Value)                         \
 114   switch(gen_variant) {                                 \
 115   case GEN_OFFSET:                                      \
 116     printf("#define %-40s %d\n", #String, Value);       \
 117     break;                                              \
 118   case GEN_INDEX:                                       \
 119     printf("#define IDX_%-40s %d\n", #String, index++); \
 120     break;                                              \
 121   case GEN_TABLE:                                       \
 122     printf("\t" #String ",\n");                         \
 123     break;                                              \
 124   }
 125 
 126 void gen_prologue(GEN_variant gen_variant) {
 127     const char *suffix = "Undefined-Suffix";
 128 
 129     switch(gen_variant) {
 130       case GEN_OFFSET: suffix = ".h";        break;
 131       case GEN_INDEX:  suffix = "Index.h";   break;
 132       case GEN_TABLE:  suffix = ".cpp";      break;
 133     }
 134 
 135     printf("/*\n");
 136     printf(" * JvmOffsets%s !!!DO NOT EDIT!!! \n", suffix);
 137     printf(" * The generateJvmOffsets program generates this file!\n");
 138     printf(" */\n\n");
 139     switch(gen_variant) {
 140 
 141       case GEN_OFFSET:
 142       case GEN_INDEX:
 143         break;
 144 
 145       case GEN_TABLE:
 146         printf("#include \"JvmOffsets.h\"\n");
 147         printf("\n");
 148         printf("int __JvmOffsets[] = {\n");
 149         break;
 150     }
 151 }
 152 
 153 void gen_epilogue(GEN_variant gen_variant) {
 154     if (gen_variant != GEN_TABLE) {
 155         return;
 156     }
 157     printf("};\n\n");
 158     return;
 159 }
 160 
 161 int generateJvmOffsets(GEN_variant gen_variant) {
 162   int index = 0;        /* It is used to generate JvmOffsetsIndex.h */
 163   int pointer_size = sizeof(void *);
 164   int data_model = (pointer_size == 4) ? PR_MODEL_ILP32 : PR_MODEL_LP64;
 165 
 166   gen_prologue(gen_variant);
 167 
 168   GEN_VALUE(DATA_MODEL, data_model);
 169   GEN_VALUE(POINTER_SIZE, pointer_size);
 170 #if defined(TIERED)
 171   GEN_VALUE(COMPILER, 3);
 172 #elif COMPILER1
 173   GEN_VALUE(COMPILER, 1);
 174 #elif COMPILER2
 175   GEN_VALUE(COMPILER, 2);
 176 #else
 177   GEN_VALUE(COMPILER, 0);
 178 #endif // COMPILER1 && COMPILER2
 179   printf("\n");
 180 
 181   GEN_OFFS(CollectedHeap, _reserved);
 182   GEN_OFFS(MemRegion, _start);
 183   GEN_OFFS(MemRegion, _word_size);
 184   GEN_SIZE(HeapWord);
 185   printf("\n");
 186 
 187   GEN_OFFS(VMStructEntry, typeName);
 188   GEN_OFFS(VMStructEntry, fieldName);
 189   GEN_OFFS(VMStructEntry, address);
 190   GEN_SIZE(VMStructEntry);
 191   printf("\n");
 192 
 193   GEN_VALUE(MAX_METHOD_CODE_SIZE, max_method_code_size);
 194 #if defined(sparc) || defined(__sparc)
 195   GEN_VALUE(OFFSET_interpreter_frame_method, 2 * pointer_size);     /* L2 in saved window */
 196   GEN_VALUE(OFFSET_interpreter_frame_sender_sp, 13 * pointer_size); /* I5 in saved window */
 197   // Fake value for consistency. It is not going to be used.
 198   GEN_VALUE(OFFSET_interpreter_frame_bcp_offset, 0xFFFF);
 199 #elif defined(i386) || defined(__i386) || defined(__amd64)
 200   GEN_VALUE(OFFSET_interpreter_frame_sender_sp, -1 * pointer_size);
 201   GEN_VALUE(OFFSET_interpreter_frame_method, -3 * pointer_size);
 202   GEN_VALUE(OFFSET_interpreter_frame_bcp_offset, -7 * pointer_size);
 203 #endif
 204 
 205   GEN_OFFS(Klass, _name);
 206   GEN_OFFS(ConstantPool, _pool_holder);
 207   printf("\n");
 208 
 209   GEN_VALUE(OFFSET_HeapBlockHeader_used, (int) offset_of(HeapBlock::Header, _used));
 210   GEN_OFFS(oopDesc, _metadata);
 211   printf("\n");
 212 
 213   GEN_VALUE(AccessFlags_NATIVE, JVM_ACC_NATIVE);
 214   GEN_VALUE(ConstMethod_has_linenumber_table, ConstMethod::_has_linenumber_table);
 215   GEN_OFFS(AccessFlags, _flags);
 216   GEN_OFFS(Symbol, _length);
 217   GEN_OFFS(Symbol, _body);
 218   printf("\n");
 219 
 220   GEN_OFFS(Method, _constMethod);
 221   GEN_OFFS(Method, _access_flags);
 222   printf("\n");
 223 
 224   GEN_OFFS(ConstMethod, _constants);
 225   GEN_OFFS(ConstMethod, _flags);
 226   GEN_OFFS(ConstMethod, _code_size);
 227   GEN_OFFS(ConstMethod, _name_index);
 228   GEN_OFFS(ConstMethod, _signature_index);
 229   printf("\n");
 230 
 231   GEN_OFFS(CodeHeap, _memory);
 232   GEN_OFFS(CodeHeap, _segmap);
 233   GEN_OFFS(CodeHeap, _log2_segment_size);
 234   printf("\n");
 235 
 236   GEN_OFFS(VirtualSpace, _low_boundary);
 237   GEN_OFFS(VirtualSpace, _high_boundary);
 238   GEN_OFFS(VirtualSpace, _low);
 239   GEN_OFFS(VirtualSpace, _high);
 240   printf("\n");
 241 
 242   /* We need to use different names here because of the template parameter */
 243   GEN_OFFS_NAME(GrowableArray<CodeHeap*>, _data, GrowableArray_CodeHeap);
 244   GEN_OFFS_NAME(GrowableArray<CodeHeap*>, _len, GrowableArray_CodeHeap);
 245   printf("\n");
 246 
 247   GEN_OFFS(CodeBlob, _name);
 248   GEN_OFFS(CodeBlob, _header_size);
 249   GEN_OFFS(CodeBlob, _content_begin);
 250   GEN_OFFS(CodeBlob, _code_begin);
 251   GEN_OFFS(CodeBlob, _code_end);
 252   GEN_OFFS(CodeBlob, _data_offset);
 253   GEN_OFFS(CodeBlob, _frame_size);
 254   printf("\n");
 255 
 256   GEN_OFFS(nmethod, _method);
 257   GEN_OFFS(nmethod, _dependencies_offset);
 258   GEN_OFFS(nmethod, _metadata_offset);
 259   GEN_OFFS(nmethod, _scopes_data_begin);
 260   GEN_OFFS(nmethod, _scopes_pcs_offset);
 261   GEN_OFFS(nmethod, _handler_table_offset);
 262   GEN_OFFS(nmethod, _deopt_handler_begin);
 263   GEN_OFFS(nmethod, _orig_pc_offset);
 264 
 265   GEN_OFFS(PcDesc, _pc_offset);
 266   GEN_OFFS(PcDesc, _scope_decode_offset);
 267 
 268   printf("\n");
 269 
 270   GEN_OFFS(NarrowPtrStruct, _base);
 271   GEN_OFFS(NarrowPtrStruct, _shift);
 272   printf("\n");
 273 
 274   GEN_VALUE(SIZE_HeapBlockHeader, (int) sizeof(HeapBlock::Header));
 275   GEN_SIZE(oopDesc);
 276   GEN_SIZE(ConstantPool);
 277   printf("\n");
 278 
 279   GEN_SIZE(PcDesc);
 280   GEN_SIZE(Method);
 281   GEN_SIZE(ConstMethod);
 282   GEN_SIZE(nmethod);
 283   GEN_SIZE(CodeBlob);
 284   GEN_SIZE(BufferBlob);
 285   GEN_SIZE(SingletonBlob);
 286   GEN_SIZE(RuntimeStub);
 287   GEN_SIZE(SafepointBlob);
 288 
 289   gen_epilogue(gen_variant);
 290   printf("\n");
 291 
 292   fflush(stdout);
 293   return 0;
 294 }
 295 
 296 const char *HELP =
 297     "HELP: generateJvmOffsets {-header | -index | -table} \n";
 298 
 299 int main(int argc, const char *argv[]) {
 300     GEN_variant gen_var;
 301 
 302     if (argc != 2) {
 303         printf("%s", HELP);
 304         return 1;
 305     }
 306 
 307     if (0 == strcmp(argv[1], "-header")) {
 308         gen_var = GEN_OFFSET;
 309     }
 310     else if (0 == strcmp(argv[1], "-index")) {
 311         gen_var = GEN_INDEX;
 312     }
 313     else if (0 == strcmp(argv[1], "-table")) {
 314         gen_var = GEN_TABLE;
 315     }
 316     else {
 317         printf("%s", HELP);
 318         return 1;
 319     }
 320     return generateJvmOffsets(gen_var);
 321 }