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