d613ed9d409fc411a2fc2afa14a0f37dc9a85e0f
[cacao.git] / src / vmcore / options.c
1 /* src/vmcore/options.c - contains global options
2
3    Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25 */
26
27
28 #include "config.h"
29
30 #include <errno.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34
35 #if defined(HAVE_STRING_H)
36 # include <string.h>
37 #endif
38
39 #include <limits.h>
40
41 #include "mm/memory.h"
42
43 #include "native/jni.h"
44
45 #include "vm/vm.h"
46
47 #include "vmcore/options.h"
48
49
50 /* command line option ********************************************************/
51
52 s4    opt_index = 0;            /* index of processed arguments               */
53 char *opt_arg;                  /* this one exports the option argument       */
54
55 bool opt_foo = false;           /* option for development                     */
56
57 bool opt_jar = false;
58
59 #if defined(ENABLE_JIT)
60 bool opt_jit = true;            /* JIT mode execution (default)               */
61 bool opt_intrp = false;         /* interpreter mode execution                 */
62 #else
63 bool opt_jit = false;           /* JIT mode execution                         */
64 bool opt_intrp = true;          /* interpreter mode execution (default)       */
65 #endif
66
67 bool opt_run = true;
68
69 s4   opt_heapmaxsize   = 0;     /* maximum heap size                          */
70 s4   opt_heapstartsize = 0;     /* initial heap size                          */
71 s4   opt_stacksize     = 0;     /* thread stack size                          */
72
73 bool opt_verbose = false;
74 bool opt_debugcolor = false;    /* use ANSI terminal sequences                */
75 bool compileall = false;
76
77 bool loadverbose = false;
78 bool initverbose = false;
79
80 bool opt_verboseclass     = false;
81 bool opt_verbosegc        = false;
82 bool opt_verbosejni       = false;
83 bool opt_verbosecall      = false;      /* trace all method invocation        */
84
85 bool showmethods = false;
86 bool showconstantpool = false;
87 bool showutf = false;
88
89 char *opt_method = NULL;
90 char *opt_signature = NULL;
91
92 bool compileverbose =  false;           /* trace compiler actions             */
93 bool showstack = false;
94
95 bool opt_showdisassemble    = false;    /* generate disassembler listing      */
96 bool opt_shownops           = false;
97 bool opt_showddatasegment   = false;    /* generate data segment listing      */
98 bool opt_showintermediate   = false;    /* generate intermediate code listing */
99
100 bool checkbounds = true;       /* check array bounds                         */
101 bool opt_noieee = false;       /* don't implement ieee compliant floats      */
102 bool checksync = true;         /* do synchronization                         */
103 #if defined(ENABLE_LOOP)
104 bool opt_loops = false;        /* optimize array accesses in loops           */
105 #endif
106
107 bool makeinitializations = true;
108
109 #if defined(ENABLE_STATISTICS)
110 bool opt_stat    = false;
111 bool opt_getloadingtime = false;   /* to measure the runtime                 */
112 bool opt_getcompilingtime = false; /* compute compile time                   */
113 #endif
114 #if defined(ENABLE_VERIFIER)
115 bool opt_verify  = true;       /* true if classfiles should be verified      */
116 #endif
117
118 #if defined(ENABLE_PROFILING)
119 bool opt_prof    = false;
120 bool opt_prof_bb = false;
121 #endif
122
123
124 /* inlining options ***********************************************************/
125
126 #if defined(ENABLE_INLINING)
127 bool opt_inlining = false;
128 #if defined(ENABLE_INLINING_DEBUG) || !defined(NDEBUG)
129 s4 opt_inline_debug_min_size = 0;
130 s4 opt_inline_debug_max_size = INT_MAX;
131 s4 opt_inline_debug_end_counter = INT_MAX;
132 bool opt_inline_debug_all = false;
133 #endif /* defined(ENABLE_INLINING_DEBUG) || !defined(NDEBUG) */
134 #if !defined(NDEBUG)
135 bool opt_inline_debug_log = false;
136 #endif /* !defined(NDEBUG) */
137 #endif /* defined(ENABLE_INLINING) */
138
139
140 /* optimization options *******************************************************/
141
142 #if defined(ENABLE_IFCONV)
143 bool opt_ifconv = false;
144 #endif
145
146 #if defined(ENABLE_LSRA) || defined(ENABLE_SSA)
147 bool opt_lsra = false;
148 #endif
149
150
151 /* interpreter options ********************************************************/
152
153 #if defined(ENABLE_INTRP)
154 bool opt_no_dynamic = false;            /* suppress dynamic superinstructions */
155 bool opt_no_replication = false;        /* don't use replication in intrp     */
156 bool opt_no_quicksuper = false;         /* instructions for quickening cannot be
157                                                                                    part of dynamic superinstructions */
158
159 s4   opt_static_supers = 0x7fffffff;
160 bool vm_debug = false;          /* XXX this should be called `opt_trace'      */
161 #endif
162
163 #if defined(ENABLE_DEBUG_FILTER)
164 const char *opt_filter_verbosecall_include = 0;
165 const char *opt_filter_verbosecall_exclude = 0;
166 const char *opt_filter_show_method = 0;
167 #endif
168
169
170 /* -XX options ****************************************************************/
171
172 /* NOTE: For better readability keep these alpha-sorted. */
173
174 int      opt_DebugExceptions           = 0;
175 int      opt_DebugLocks                = 0;
176 int      opt_DebugPackage              = 0;
177 int      opt_DebugPatcher              = 0;
178 int      opt_DebugProperties           = 0;
179 int32_t  opt_DebugStackFrameInfo       = 0;
180 int      opt_DebugStackTrace           = 0;
181 int      opt_DebugThreads              = 0;
182 #if defined(ENABLE_DISASSEMBLER)
183 int      opt_DisassembleStubs          = 0;
184 #endif
185 #if defined(ENABLE_GC_CACAO)
186 int32_t  opt_GCDebugRootSet            = 0;
187 int32_t  opt_GCStress                  = 0;
188 #endif
189 int32_t  opt_MaxPermSize               = 0;
190 int32_t  opt_PermSize                  = 0;
191 int      opt_PrintConfig               = 0;
192 int32_t  opt_ProfileGCMemoryUsage      = 0;
193 int32_t  opt_ProfileMemoryUsage        = 0;
194 FILE    *opt_ProfileMemoryUsageGNUPlot = NULL;
195 #if defined(ENABLE_REPLACEMENT)
196 int      opt_TestReplacement           = 0;
197 #endif
198 int32_t  opt_ThreadStackSize           = 0;
199 int      opt_TraceCompilerCalls        = 0;
200 int32_t  opt_TraceExceptions           = 0;
201 int32_t  opt_TraceJavaCalls            = 0;
202 int32_t  opt_TraceJNICalls             = 0;
203 int32_t  opt_TraceJVMCalls             = 0;
204 int32_t  opt_TraceLinkClass            = 0;
205 #if defined(ENABLE_REPLACEMENT)
206 int32_t  opt_TraceReplacement          = 0;
207 #endif
208
209
210 enum {
211         OPT_TYPE_BOOLEAN,
212         OPT_TYPE_VALUE
213 };
214
215 enum {
216         OPT_DebugExceptions,
217         OPT_DebugLocks,
218         OPT_DebugPackage,
219         OPT_DebugPatcher,
220         OPT_DebugProperties,
221         OPT_DebugStackFrameInfo,
222         OPT_DebugStackTrace,
223         OPT_DebugThreads,
224         OPT_DisassembleStubs,
225         OPT_GCDebugRootSet,
226         OPT_GCStress,
227         OPT_MaxPermSize,
228         OPT_PermSize,
229         OPT_PrintConfig,
230         OPT_ProfileGCMemoryUsage,
231         OPT_ProfileMemoryUsage,
232         OPT_ProfileMemoryUsageGNUPlot,
233         OPT_TestReplacement,
234         OPT_ThreadStackSize,
235         OPT_TraceCompilerCalls,
236         OPT_TraceExceptions,
237         OPT_TraceJavaCalls,
238         OPT_TraceJNICalls,
239         OPT_TraceJVMCalls,
240         OPT_TraceLinkClass,
241         OPT_TraceReplacement
242 };
243
244
245 option_t options_XX[] = {
246         { "DebugExceptions",           OPT_DebugExceptions,           OPT_TYPE_BOOLEAN, "debug exceptions" },
247         { "DebugLocks",                OPT_DebugLocks,                OPT_TYPE_BOOLEAN, "print debug information for locks" },
248         { "DebugPackage",              OPT_DebugPackage,              OPT_TYPE_BOOLEAN, "debug Java boot-packages" },
249         { "DebugPatcher",              OPT_DebugPatcher,              OPT_TYPE_BOOLEAN, "debug JIT code patching" },
250         { "DebugProperties",           OPT_DebugProperties,           OPT_TYPE_BOOLEAN, "print debug information for properties" },
251         { "DebugStackFrameInfo",       OPT_DebugStackFrameInfo,       OPT_TYPE_BOOLEAN, "TODO" },
252         { "DebugStackTrace",           OPT_DebugStackTrace,           OPT_TYPE_BOOLEAN, "debug stacktrace creation" },
253         { "DebugThreads",              OPT_DebugThreads,              OPT_TYPE_BOOLEAN, "print debug information for threads" },
254 #if defined(ENABLE_DISASSEMBLER)
255         { "DisassembleStubs",          OPT_DisassembleStubs,          OPT_TYPE_BOOLEAN, "disassemble builtin and native stubs when generated" },
256 #endif
257 #if defined(ENABLE_GC_CACAO)
258         { "GCDebugRootSet",            OPT_GCDebugRootSet,            OPT_TYPE_BOOLEAN, "GC: print root-set at collection" },
259         { "GCStress",                  OPT_GCStress,                  OPT_TYPE_BOOLEAN, "GC: forced collection at every allocation" },
260 #endif
261         { "MaxPermSize",               OPT_MaxPermSize,               OPT_TYPE_VALUE,   "not implemented" },
262         { "PermSize",                  OPT_PermSize,                  OPT_TYPE_VALUE,   "not implemented" },
263         { "PrintConfig",               OPT_PrintConfig,               OPT_TYPE_BOOLEAN, "print VM configuration" },
264         { "ProfileGCMemoryUsage",      OPT_ProfileGCMemoryUsage,      OPT_TYPE_VALUE,   "profiles GC memory usage in the given interval, <value> is in seconds (default: 5)" },
265         { "ProfileMemoryUsage",        OPT_ProfileMemoryUsage,        OPT_TYPE_VALUE,   "TODO" },
266         { "ProfileMemoryUsageGNUPlot", OPT_ProfileMemoryUsageGNUPlot, OPT_TYPE_VALUE,   "TODO" },
267 #if defined(ENABLE_REPLACEMENT)
268         { "TestReplacement"          , OPT_TestReplacement,           OPT_TYPE_BOOLEAN, "activate all replacement points during code generation" },
269 #endif
270         { "ThreadStackSize",           OPT_ThreadStackSize,           OPT_TYPE_VALUE,   "TODO" },
271         { "TraceCompilerCalls",        OPT_TraceCompilerCalls,        OPT_TYPE_BOOLEAN, "trace JIT compiler calls" },
272         { "TraceExceptions",           OPT_TraceExceptions,           OPT_TYPE_BOOLEAN, "trace Exception throwing" },
273         { "TraceJavaCalls",            OPT_TraceJavaCalls,            OPT_TYPE_BOOLEAN, "trace Java method calls" },
274         { "TraceJNICalls",             OPT_TraceJNICalls,             OPT_TYPE_BOOLEAN, "trace JNI method calls" },
275         { "TraceJVMCalls",             OPT_TraceJVMCalls,             OPT_TYPE_BOOLEAN, "TODO" },
276         { "TraceLinkClass",            OPT_TraceLinkClass,            OPT_TYPE_BOOLEAN, "trace class linking" },
277 #if defined(ENABLE_REPLACEMENT)
278         { "TraceReplacement",          OPT_TraceReplacement,          OPT_TYPE_VALUE,   "trace on-stack replacement with the given verbosity level (default: 1)" },
279 #endif
280
281         /* end marker */
282
283         { NULL,                        -1,                            -1, NULL }
284 };
285
286
287 /* options_get *****************************************************************
288
289    DOCUMENT ME!!!
290
291 *******************************************************************************/
292
293 s4 options_get(opt_struct *opts, JavaVMInitArgs *vm_args)
294 {
295         char *option;
296         s4    i;
297
298         if (opt_index >= vm_args->nOptions)
299                 return OPT_DONE;
300
301         /* get the current option */
302
303         option = vm_args->options[opt_index].optionString;
304
305         if ((option == NULL) || (option[0] != '-'))
306                 return OPT_DONE;
307
308         for (i = 0; opts[i].name; i++) {
309                 if (!opts[i].arg) {
310                         /* boolean option found */
311
312                         if (strcmp(option + 1, opts[i].name) == 0) {
313                                 opt_index++;
314                                 return opts[i].value;
315                         }
316
317                 } else {
318                         /* parameter option found */
319
320                         /* with a space between */
321
322                         if (strcmp(option + 1, opts[i].name) == 0) {
323                                 opt_index++;
324
325                                 if (opt_index < vm_args->nOptions) {
326
327 #if defined(HAVE_STRDUP)
328                                         opt_arg = strdup(vm_args->options[opt_index].optionString);
329 #else
330 # error !HAVE_STRDUP
331 #endif
332
333                                         opt_index++;
334                                         return opts[i].value;
335                                 }
336
337                                 return OPT_ERROR;
338
339                         } else {
340                                 /* parameter and option have no space between */
341
342                                 /* FIXME: this assumption is plain wrong, hits you if there is a
343                                  * parameter with no argument starting with same letter as param with argument
344                                  * but named after that one, ouch! */
345
346                                 size_t l = strlen(opts[i].name);
347
348                                 if (strlen(option + 1) > l) {
349                                         if (memcmp(option + 1, opts[i].name, l) == 0) {
350                                                 opt_index++;
351
352 #if defined(HAVE_STRDUP)
353                                                 opt_arg = strdup(option + 1 + l);
354 #else
355 # error !HAVE_STRDUP
356 #endif
357
358                                                 return opts[i].value;
359                                         }
360                                 }
361                         }
362                 }
363         }
364
365         return OPT_ERROR;
366 }
367
368
369 /* options_xxusage *************************************************************
370
371    Print usage message for debugging options.
372
373 *******************************************************************************/
374
375 static void options_xxusage(void)
376 {
377         option_t *opt;
378         int       length;
379         int       i;
380         char     *c;
381
382         for (opt = options_XX; opt->name != NULL; opt++) {
383                 printf("    -XX:");
384
385                 switch (opt->type) {
386                 case OPT_TYPE_BOOLEAN:
387                         printf("+%s", opt->name);
388                         length = strlen("    -XX:+") + strlen(opt->name);
389                         break;
390                 case OPT_TYPE_VALUE:
391                         printf("%s=<value>", opt->name);
392                         length = strlen("    -XX:") + strlen(opt->name) + strlen("=<value>");
393                         break;
394                 }
395
396                 /* Check if the help fits into one 80-column line.
397                    Documentation starts at column 29. */
398
399                 if (length < (29 - 1)) {
400                         /* Print missing spaces up to column 29. */
401
402                         for (i = length; i < 29; i++)
403                                 printf(" ");
404                 }
405                 else {
406                         printf("\n");
407                         printf("                             "); /* 29 spaces */
408                 }
409
410                 /* Check documentation length. */
411
412                 length = strlen(opt->doc);
413
414                 if (length < (80 - 29)) {
415                         printf("%s", opt->doc);
416                 }
417                 else {
418                         for (c = opt->doc, i = 29; *c != 0; c++, i++) {
419                                 /* If we are at the end of the line, break it. */
420
421                                 if (i == 80) {
422                                         printf("\n");
423                                         printf("                             "); /* 29 spaces */
424                                         i = 29;
425                                 }
426
427                                 printf("%c", *c);
428                         }
429                 }
430
431                 printf("\n");
432         }
433
434         /* exit with error code */
435
436         exit(1);
437 }
438
439
440 /* options_xx ******************************************************************
441
442    Handle -XX: options.
443
444 *******************************************************************************/
445
446 void options_xx(JavaVMInitArgs *vm_args)
447 {
448         const char *name;
449         const char *start;
450         char       *end;
451         int         length;
452         int         enable;
453         char       *value;
454         option_t   *opt;
455         char       *filename;
456         FILE       *file;
457         int         i;
458
459         /* Iterate over all passed options. */
460
461         for (i = 0; i < vm_args->nOptions; i++) {
462                 /* Get the current option. */
463
464                 name = vm_args->options[i].optionString;
465
466                 /* Check for help (-XX). */
467
468                 if (strcmp(name, "-XX") == 0)
469                         options_xxusage();
470
471                 /* Check if the option start with -XX. */
472
473                 start = strstr(name, "-XX:");
474
475                 if ((start == NULL) || (start != name))
476                         continue;
477
478                 /* Check if the option is a boolean option. */
479
480                 if (name[4] == '+') {
481                         start  = name + 4 + 1;
482                         enable = 1;
483                 }
484                 else if (name[4] == '-') {
485                         start  = name + 4 + 1;
486                         enable = 0;
487                 }
488                 else {
489                         start  = name + 4;
490                         enable = -1;
491                 }
492
493                 /* Search for a '=' in the option name and get the option name
494                    length and the value of the option. */
495
496                 end = strchr(start, '=');
497
498                 if (end == NULL) {
499                         length = strlen(start);
500                         value  = NULL;
501                 }
502                 else {
503                         length = end - start;
504                         value  = end + 1;
505                 }
506
507                 /* Search the option in the option array. */
508
509                 for (opt = options_XX; opt->name != NULL; opt++) {
510                         if (strncmp(opt->name, start, length) == 0) {
511                                 /* Check if the options passed fits to the type. */
512
513                                 switch (opt->type) {
514                                 case OPT_TYPE_BOOLEAN:
515                                         if ((enable == -1) || (value != NULL))
516                                                 options_xxusage();
517                                         break;
518                                 case OPT_TYPE_VALUE:
519                                         if ((enable != -1) || (value == NULL))
520                                                 options_xxusage();
521                                         break;
522                                 default:
523                                         vm_abort("options_xx: unknown option type %d for option %s",
524                                                          opt->type, opt->name);
525                                 }
526
527                                 break;
528                         }
529                 }
530
531                 /* Process the option. */
532
533                 switch (opt->value) {
534                 case OPT_DebugExceptions:
535                         opt_DebugExceptions = enable;
536                         break;
537
538                 case OPT_DebugLocks:
539                         opt_DebugLocks = enable;
540                         break;
541
542                 case OPT_DebugPackage:
543                         opt_DebugPackage = enable;
544                         break;
545
546                 case OPT_DebugPatcher:
547                         opt_DebugPatcher = enable;
548                         break;
549
550                 case OPT_DebugProperties:
551                         opt_DebugProperties = enable;
552                         break;
553
554                 case OPT_DebugStackFrameInfo:
555                         opt_DebugStackFrameInfo = enable;
556                         break;
557
558                 case OPT_DebugStackTrace:
559                         opt_DebugStackTrace = enable;
560                         break;
561
562                 case OPT_DebugThreads:
563                         opt_DebugThreads = enable;
564                         break;
565
566 #if defined(ENABLE_DISASSEMBLER)
567                 case OPT_DisassembleStubs:
568                         opt_DisassembleStubs = enable;
569                         break;
570 #endif
571
572 #if defined(ENABLE_GC_CACAO)
573                 case OPT_GCDebugRootSet:
574                         opt_GCDebugRootSet = enable;
575                         break;
576
577                 case OPT_GCStress:
578                         opt_GCStress = enable;
579                         break;
580 #endif
581
582                 case OPT_MaxPermSize:
583                         /* currently ignored */
584                         break;
585
586                 case OPT_PermSize:
587                         /* currently ignored */
588                         break;
589
590                 case OPT_PrintConfig:
591                         opt_PrintConfig = enable;
592                         break;
593
594                 case OPT_ProfileGCMemoryUsage:
595                         if (value == NULL)
596                                 opt_ProfileGCMemoryUsage = 5;
597                         else
598                                 opt_ProfileGCMemoryUsage = atoi(value);
599                         break;
600
601                 case OPT_ProfileMemoryUsage:
602                         if (value == NULL)
603                                 opt_ProfileMemoryUsage = 5;
604                         else
605                                 opt_ProfileMemoryUsage = atoi(value);
606
607 # if defined(ENABLE_STATISTICS)
608                         /* we also need statistics */
609
610                         opt_stat = true;
611 # endif
612                         break;
613
614                 case OPT_ProfileMemoryUsageGNUPlot:
615                         if (value == NULL)
616                                 filename = "profile.dat";
617                         else
618                                 filename = value;
619
620                         file = fopen(filename, "w");
621
622                         if (file == NULL)
623                                 vm_abort("options_xx: fopen failed: %s", strerror(errno));
624
625                         opt_ProfileMemoryUsageGNUPlot = file;
626                         break;
627
628 #if defined(ENABLE_REPLACEMENT)
629                 case OPT_TestReplacement:
630                         opt_TestReplacement = enable;
631                         break;
632 #endif
633
634                 case OPT_ThreadStackSize:
635                         /* currently ignored */
636                         break;
637
638                 case OPT_TraceCompilerCalls:
639                         opt_TraceCompilerCalls = enable;
640                         break;
641
642                 case OPT_TraceExceptions:
643                         opt_TraceExceptions = enable;
644                         break;
645
646                 case OPT_TraceJavaCalls:
647                         opt_verbosecall = enable;
648                         opt_TraceJavaCalls = enable;
649                         break;
650
651                 case OPT_TraceJNICalls:
652                         opt_TraceJNICalls = enable;
653                         break;
654
655                 case OPT_TraceJVMCalls:
656                         opt_TraceJVMCalls = enable;
657                         break;
658
659                 case OPT_TraceLinkClass:
660                         opt_TraceLinkClass = enable;
661                         break;
662
663 #if defined(ENABLE_REPLACEMENT)
664                 case OPT_TraceReplacement:
665                         if (value == NULL)
666                                 opt_TraceReplacement = 1;
667                         else
668                                 opt_TraceReplacement = atoi(value);
669                         break;
670 #endif
671
672                 default:
673                         printf("Unknown -XX option: %s\n", name);
674                         break;
675                 }
676         }
677 }
678
679
680 /*
681  * These are local overrides for various environment variables in Emacs.
682  * Please do not remove this and leave it at the end of the file, where
683  * Emacs will automagically detect them.
684  * ---------------------------------------------------------------------
685  * Local variables:
686  * mode: c
687  * indent-tabs-mode: t
688  * c-basic-offset: 4
689  * tab-width: 4
690  * End:
691  */