on master help sign in
add basic rts options
commit1df7d1591afac6ae340112a2d796f3b92814844f authorSam T <sta.cs.vsu@gmail.com> dateThu 14 Mar 2013, 15:04 (14 years ago) parent9d2ef24
4 files changed+147−7
rts/Makefile+2−2 rts/idris_main.c+17−5 rts/idris_opts.c+110 rts/idris_opts.h+18
rts/Makefile+2−2
@@ -1,7 +1,7 @@
11include ../config.mk
22
3−OBJS = idris_rts.o idris_heap.o idris_gc.o idris_gmp.o idris_stdfgn.o idris_bitstring.o
4−HDRS = idris_rts.h idris_heap.h idris_gc.h idris_gmp.h idris_stdfgn.h idris_bitstring.h
3+OBJS = idris_rts.o idris_heap.o idris_gc.o idris_gmp.o idris_stdfgn.o idris_bitstring.o idris_opts.o
4+HDRS = idris_rts.h idris_heap.h idris_gc.h idris_gmp.h idris_stdfgn.h idris_bitstring.h idris_opts.h
55CFLAGS = -O2 -Wall
66ifneq ($(GMP_INCLUDE_DIR),)
77 CFLAGS += -isystem $(GMP_INCLUDE_DIR)
rts/idris_main.c+17−5
@@ -1,9 +1,21 @@
1+#include "idris_opts.h"
2+
3+// The default options should give satisfactory results under many circumstances.
4+RTSOpts opts = {
5+ .init_heap_size = 4096000,
6+ .max_stack_size = 4096000,
7+ .show_summary = 0
8+};
9+
110int main(int argc, char* argv[]) {
2− VM* vm = init_vm(4096000, 4096000, 1, argc, argv); // 1024000);
11+ parse_shift_args(&opts, &argc, &argv);
12+
13+ VM* vm = init_vm(opts.max_stack_size, opts.init_heap_size, 1, argc, argv);
314 _idris__123_runMain0_125_(vm, NULL);
4− //_idris_main(vm, NULL);
5−#ifdef IDRIS_TRACE
6− idris_gcInfo(vm, 1);
7−#endif
15+
16+ if (opts.show_summary) {
17+ idris_gcInfo(vm, 1);
18+ }
19+
820 terminate(vm);
921}
rts/idris_opts.cnew file+110
@@ -0,0 +1,110 @@
1+#include "idris_opts.h"
2+
3+#include <stdlib.h>
4+#include <stddef.h>
5+#include <stdio.h>
6+#include <string.h>
7+
8+
9+char usage[] =
10+ "\n" \
11+ "Usage: <prog> [+RTS <rtsopts> -RTS] <args>\n\n" \
12+ "Options:\n\n" \
13+ " -? Print this message and exits.\n" \
14+ " -s Summary GC statistics.\n" \
15+ " -H Initial heap size. Egs: -H4M, -H500K, -H1G\n" \
16+ " -K Sets the maximum stack size. Egs: -K8M\n" \
17+ "\n";
18+
19+void print_usage() {
20+ fprintf(stderr, usage);
21+}
22+
23+int read_size(char * str) {
24+ int size = 0;
25+ char mult = ' ';
26+
27+ int r = sscanf(str, "%u%c", &size, &mult);
28+
29+ if (r == 1)
30+ return size;
31+
32+ if (r == 2) {
33+ switch (mult) {
34+ case 'K': size = size << 10; break;
35+ case 'M': size = size << 20; break;
36+ case 'G': size = size << 30; break;
37+ default:
38+ fprintf(stderr,
39+ "RTS Opts: Unable to recognize size suffix `%c'.\n" \
40+ " Possible suffixes are K or M or G.\n",
41+ mult);
42+ print_usage();
43+ exit(EXIT_FAILURE);
44+ }
45+ return size;
46+ }
47+
48+ fprintf(stderr, "RTS Opts: Unable to parse size. Egs: 1K, 10M, 2G.\n");
49+ print_usage();
50+ exit(EXIT_FAILURE);
51+}
52+
53+
54+int parse_args(RTSOpts * opts, int argc, char *argv[])
55+{
56+ if (argc == 0)
57+ return 0;
58+
59+ if (strcmp(argv[0], "+RTS") != 0)
60+ return 0;
61+
62+ int i;
63+ for (i = 1; i < argc; i++) {
64+ if (strcmp(argv[i], "-RTS") == 0) {
65+ return i + 1;
66+ }
67+
68+ if (argv[i][0] != '-') {
69+ fprintf(stderr, "RTS options should start with '-'.\n");
70+ print_usage();
71+ exit(EXIT_FAILURE);
72+ }
73+
74+ switch (argv[i][1])
75+ {
76+ case '?':
77+ print_usage();
78+ exit(EXIT_SUCCESS);
79+ break;
80+
81+ case 's':
82+ opts->show_summary = 1;
83+ break;
84+
85+ case 'H':
86+ opts->init_heap_size = read_size(argv[i] + 2);
87+ break;
88+
89+ case 'K':
90+ opts->max_stack_size = read_size(argv[i] + 2);
91+ break;
92+
93+ default:
94+ printf("RTS opts: Wrong argument: %s\n", argv[i]);
95+ print_usage();
96+ exit(EXIT_FAILURE);
97+ }
98+ }
99+
100+ return argc;
101+}
102+
103+void parse_shift_args(RTSOpts * opts, int * argc, char ** argv[]) {
104+ size_t shift = parse_args(opts, (*argc) - 1, (*argv) + 1);
105+
106+ char *prg = (*argv)[0];
107+ *argc = *argc - shift;
108+ *argv = *argv + shift;
109+ (*argv)[0] = prg;
110+}
rts/idris_opts.hnew file+18
@@ -0,0 +1,18 @@
1+#ifndef _IDRIS_OPTS_H
2+#define _IDRIS_OPTS_H
3+
4+#include <stddef.h>
5+
6+typedef struct {
7+ size_t init_heap_size;
8+ size_t max_stack_size;
9+ int show_summary;
10+} RTSOpts;
11+
12+void print_usage();
13+
14+// Parse rts options and shift arguments such that rts options becomes invisible
15+// for main program.
16+void parse_shift_args(RTSOpts * opts, int * argc, char ** argv[]);
17+
18+#endif