-
Notifications
You must be signed in to change notification settings - Fork 77
/
main.c
193 lines (163 loc) · 4.03 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <sys/time.h>
#include <getopt.h>
#include <stdint.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "vmir.h"
static void
usage(const char *argv0)
{
printf("\n");
printf("Usage ... %s [OPTIONS] <file.bc>\n", argv0);
printf("\n");
printf(" -f FUNCTION Function to diagnose [ALL]\n");
printf(" -l Dump lowered function(s)\n");
printf(" -p Dump parsed function(s)\n");
printf(" -i List all functions\n");
printf(" -n Don't try to run code\n");
printf("\n");
}
static int64_t
get_ts(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (int64_t)tv.tv_sec * 1000000LL tv.tv_usec;
}
static void
dump_stats(ir_unit_t *iu)
{
const vmir_stats_t *s = vmir_get_stats(iu);
printf("\n");
printf(" Memory usage stats\n");
printf("\n");
printf(" VM code size: %d\n", s->vm_code_size);
printf(" JIT code size: %d\n", s->jit_code_size);
printf(" Data size: %d\n", s->data_size);
printf(" Peak heap size: %d\n", s->peak_heap_size);
printf(" Peak stack usage: %d\n", s->peak_stack_size);
printf("\n");
printf(" Code transformation stats\n");
printf("\n");
printf(" Moves killed: %d\n", s->moves_killed);
printf(" Lea Load combined: %d\n", s->lea_load_combined);
printf(" Lea Load comb-fail: %d\n", s->lea_load_combined_failed);
printf("Cmp Branch combined: %d\n", s->cmp_branch_combine);
printf("Cmp Select combined: %d\n", s->cmp_select_combine);
printf(" Mul Add combined: %d\n", s->mla_combine);
printf(" Load Cast combined: %d\n", s->load_cast_combine);
printf("\n");
}
/**
*
*/
int
main(int argc, char **argv)
{
int debug_flags = 0;
int run = 1;
const char *debugged_function = NULL;
int opt;
const char *argv0 = argv[0];
int print_stats = 0;
int verbose = 0;
while((opt = getopt(argc, argv, "plidf:nhrbsjIv")) != -1) {
switch(opt) {
case 'p':
debug_flags |= VMIR_DBG_DUMP_PARSED_FUNCTION;
break;
case 'l':
debug_flags |= VMIR_DBG_DUMP_LOWERED_FUNCTION;
break;
case 'i':
debug_flags |= VMIR_DBG_LIST_FUNCTIONS;
break;
case 'd':
debug_flags |= VMIR_DBG_DUMP_DEV;
break;
case 'r':
debug_flags |= VMIR_DBG_DUMP_REGALLOC;
break;
case 'b':
debug_flags |= VMIR_DBG_BB_INSTRUMENT;
break;
case 'j':
debug_flags |= VMIR_DBG_DISABLE_JIT;
break;
case 'I':
debug_flags |= VMIR_DBG_IGNORE_UNRESOLVED_FUNCTIONS;
break;
case 'f':
debugged_function = optarg;
break;
case 'h':
usage(argv0);
exit(0);
case 'n':
run = 0;
break;
case 's':
print_stats = 1;
break;
case 'v':
verbose = 1;
break;
default:
usage(argv0);
exit(1);
}
}
if(optind >= argc) {
fprintf(stderr, "Please provide a .wasm or .bc file as the first argument.\n");
exit(1);
}
argc -= optind;
argv = optind;
int fd = open(argv[0], O_RDONLY);
if(fd == -1) {
perror("open");
exit(1);
}
struct stat st;
if(fstat(fd, &st)) {
perror("stat");
exit(1);
}
uint8_t *buf = malloc(st.st_size);
if(read(fd, buf, st.st_size) != st.st_size) {
perror("read");
exit(1);
}
close(fd);
#define MB(x) ((x) * 1024 * 1024)
void *mem = calloc(1, MB(64));
ir_unit_t *iu = vmir_create(mem, MB(64), MB(1), MB(1), NULL);
vmir_set_debug_flags(iu, debug_flags);
vmir_set_debugged_function(iu, debugged_function);
if(verbose)
vmir_set_log_level(iu, VMIR_LOG_DEBUG);
if(vmir_load(iu, buf, st.st_size)) {
free(mem);
free(buf);
vmir_destroy(iu);
return -1;
}
free(buf);
if(run) {
int64_t ts = get_ts();
int rval;
vmir_run(iu, &rval, argc, argv);
ts = get_ts() - ts;
if(print_stats)
printf("main() executed for %d ms\n", (int)(ts / 1000LL));
}
if(print_stats)
dump_stats(iu);
vmir_instrumentation_dump(iu);
vmir_destroy(iu);
free(mem);
return 0;
}