summaryrefslogtreecommitdiffhomepage
path: root/src/main.c
blob: 139b1dcf5c16ee82580d6704db7199b8c620f988 (plain)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
 * This file is part of cparser.
 * Copyright (C) 2012 Matthias Braun <matze@braunis.de>
 */
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "adt/panic.h"
#include "adt/strutil.h"
#include "ast/ast.h"
#include "driver/c_driver.h"
#include "driver/diagnostic.h"
#include "driver/driver.h"
#include "driver/driver_t.h"
#include "driver/help.h"
#include "driver/options.h"
#include "driver/target.h"
#include "driver/tempfile.h"
#include "driver/timing.h"
#include "firm/ast2firm.h"
#include "firm/firm_opt.h"
#include "firm/jittest.h"
#include "parser/parser.h"
#include "parser/preprocessor.h"
#include "wrappergen/write_compoundsizes.h"
#include "wrappergen/write_fluffy.h"
#include "wrappergen/write_jna.h"

typedef enum compile_mode_t {
	/* note the following is ordered according to gcc option precedence */
	MODE_GENERATE_DEPENDENCIES,
	MODE_PREPROCESS_ONLY,
	MODE_PARSE_ONLY,
	MODE_COMPILE,
	MODE_COMPILE_ASSEMBLE,

	MODE_COMPILE_ASSEMBLE_LINK,
	MODE_COMPILE_DUMP,
	MODE_COMPILE_EXPORTIR,
	MODE_BENCHMARK_PARSER,
	MODE_PRINT_AST,
	MODE_PRINT_FLUFFY,
	MODE_PRINT_JNA,
	MODE_PRINT_COMPOUND_SIZE,
	MODE_JITTEST,
} compile_mode_t;
static compile_mode_t mode = MODE_COMPILE_ASSEMBLE_LINK;

static bool print_fluffy(compilation_env_t *env, compilation_unit_t *unit)
{
	if (!open_output(env))
		return false;
	write_fluffy_decls(env->out, unit->ast);
	return true;
}

static bool print_jna(compilation_env_t *env, compilation_unit_t *unit)
{
	if (!open_output(env))
		return false;
	write_jna_decls(env->out, unit->ast);
	return true;
}

static bool print_compound_size(compilation_env_t *env,
                                compilation_unit_t *unit)
{
	if (!open_output(env))
		return false;
	write_compoundsizes(env->out, unit->ast);
	return true;
}

static bool parse_ignore_errors(compilation_env_t *env,
                                compilation_unit_t *unit)
{
	do_parsing(env, unit); /* ignore return value */
	return true;
}

static void set_mode_gcc_prec(compile_mode_t new_mode, const char *arg)
{
	/* in gcc the compilation modes appear to have a precedence */
	if (mode >= new_mode) {
		mode = new_mode;
	} else {
		warningf(WARN_UNUSED_OPTION, NULL,
		         "ignoring option '%s' because of gcc precedence", arg);
	}
}

static bool warn_unused_input(compilation_env_t *env, compilation_unit_t *unit)
{
	(void)env;
	warningf(WARN_UNUSED_OPTION, NULL,
	         "ignoring input '%s' in current compilation mode", unit->name);
	unit->name = NULL; /* avoid subsequence warnings */
	return true;
}

static bool warn_no_linking(compilation_env_t *env, compilation_unit_t *units)
{
	(void)env;
	for (compilation_unit_t *unit = units; unit != NULL; unit = unit->next) {
		/* if file was processed somehow don't warn */
		if (unit->original_name != unit->name)
			continue;
		warningf(WARN_UNUSED_OPTION, NULL,
		         "ignoring input '%s' because no linking is performed",
		         unit->name);
	}
	return true;
}

static void set_unused_after(compile_mode_t mode)
{
	switch (mode) {
	case MODE_PREPROCESS_ONLY:
		set_unit_handler(COMPILATION_UNIT_PREPROCESSED_C,   warn_unused_input,
		                 true);
		set_unit_handler(COMPILATION_UNIT_PREPROCESSED_CXX, warn_unused_input,
		                 true);
		/* FALLTHROUGH */
	case MODE_PARSE_ONLY:
		set_unit_handler(COMPILATION_UNIT_IR, warn_unused_input, true);
		/* FALLTHROUGH */
	case MODE_COMPILE:
		set_unit_handler(COMPILATION_UNIT_PREPROCESSED_ASSEMBLER,
		                 warn_unused_input, true);
		set_unit_handler(COMPILATION_UNIT_OBJECT, warn_unused_input, true);
		break;
	default:
		panic("invalid argument to set_unused_after");
	}
}

static bool jittest(compilation_env_t *env, compilation_unit_t *unit)
{
	(void)env;
	(void)unit;
	jit_compile_execute_main();
	return true;
}

/** modify compilation sequence based on choosen compilation mode */
static void set_handlers(compile_mode_t mode)
{
	set_default_handlers();
	switch (mode) {
	case MODE_COMPILE_ASSEMBLE_LINK:
		break;
	case MODE_PREPROCESS_ONLY:
		set_unit_handler(COMPILATION_UNIT_LEXER_TOKENS_C,
		                 print_preprocessing_tokens, true);
		set_unit_handler(COMPILATION_UNIT_LEXER_TOKENS_CXX,
		                 print_preprocessing_tokens, true);
		set_unit_handler(COMPILATION_UNIT_LEXER_TOKENS_ASSEMBLER,
		                 print_preprocessing_tokens, true);
		set_unused_after(MODE_PREPROCESS_ONLY);
		return;
	case MODE_PRINT_AST:
		set_unit_handler(COMPILATION_UNIT_LEXER_TOKENS_C,
		                 parse_ignore_errors, false);
		set_unit_handler(COMPILATION_UNIT_LEXER_TOKENS_CXX,
		                 parse_ignore_errors, false);
		set_unit_handler(COMPILATION_UNIT_AST, do_print_ast, true);
		set_unused_after(MODE_PARSE_ONLY);
		return;
	case MODE_BENCHMARK_PARSER:
		set_unit_handler(COMPILATION_UNIT_AST, do_nothing, true);
		set_unused_after(MODE_PARSE_ONLY);
		return;
	case MODE_PRINT_FLUFFY:
		set_unit_handler(COMPILATION_UNIT_AST, print_fluffy, true);
		set_unused_after(MODE_PARSE_ONLY);
		return;
	case MODE_PRINT_JNA:
		set_unit_handler(COMPILATION_UNIT_AST, print_jna, true);
		set_unused_after(MODE_PARSE_ONLY);
		return;
	case MODE_PRINT_COMPOUND_SIZE:
		set_unit_handler(COMPILATION_UNIT_AST, print_compound_size, true);
		set_unused_after(MODE_PARSE_ONLY);
		return;
	case MODE_PARSE_ONLY:
	case MODE_COMPILE_DUMP:
		set_unit_handler(COMPILATION_UNIT_AST, build_firm_ir, true);
		set_unused_after(MODE_COMPILE);
	    return;
	case MODE_COMPILE_EXPORTIR:
		set_unit_handler(COMPILATION_UNIT_INTERMEDIATE_REPRESENTATION,
		                 write_ir_file, true);
		set_unused_after(MODE_COMPILE);
	    return;
	case MODE_COMPILE:
		set_unit_handler(COMPILATION_UNIT_INTERMEDIATE_REPRESENTATION,
		                 generate_code_final, true);
		set_unused_after(MODE_COMPILE);
		return;
	case MODE_JITTEST:
		set_unit_handler(COMPILATION_UNIT_INTERMEDIATE_REPRESENTATION,
		                 jittest, true);
		set_unused_after(MODE_COMPILE);
		return;
	case MODE_COMPILE_ASSEMBLE:
		set_unit_handler(COMPILATION_UNIT_PREPROCESSED_ASSEMBLER,
		                 assemble_final, true);
		return;
	case MODE_GENERATE_DEPENDENCIES:
		set_unit_handler(COMPILATION_UNIT_LEXER_TOKENS_C,
		                 generate_dependencies, true);
		set_unit_handler(COMPILATION_UNIT_LEXER_TOKENS_CXX,
		                 generate_dependencies, true);
		set_unit_handler(COMPILATION_UNIT_LEXER_TOKENS_ASSEMBLER,
		                 generate_dependencies, true);
		set_unused_after(MODE_PREPROCESS_ONLY);
		return;
	}
}

/** parse options that influence the compilation mode */
static bool parse_compile_mode_options(options_state_t *s)
{
	const char *full_option = s->argv[s->i];
	if (full_option[0] != '-')
		return false;
	const char *option = &full_option[1];

	const char *arg;
	bool is_M = false;
	if ((is_M = simple_arg("M", s)) || simple_arg("MM", s)) {
		set_mode_gcc_prec(MODE_GENERATE_DEPENDENCIES, full_option);
		driver_add_flag(&cppflags_obst, "-%s", option);
		print_dependencies_instead_of_preprocessing = true;
		include_system_headers_in_dependencies = is_M;
	} else if (simple_arg("c", s)) {
		set_mode_gcc_prec(MODE_COMPILE_ASSEMBLE, full_option);
	} else if (simple_arg("E", s)) {
		set_mode_gcc_prec(MODE_PREPROCESS_ONLY, full_option);
	} else if (simple_arg("S", s)) {
		set_mode_gcc_prec(MODE_COMPILE, full_option);
	} else if (simple_arg("-benchmark", s)) {
		mode = MODE_BENCHMARK_PARSER;
	} else if (simple_arg("-print-ast", s)) {
		mode = MODE_PRINT_AST;
	} else if (simple_arg("-print-fluffy", s)) {
		mode = MODE_PRINT_FLUFFY;
	} else if (simple_arg("-print-compound-sizes", s)) {
		mode = MODE_PRINT_COMPOUND_SIZE;
	} else if (simple_arg("-print-jna", s)) {
		mode = MODE_PRINT_JNA;
	} else if ((arg = spaced_arg("-dump-function", s)) != NULL) {
		dumpfunction = arg;
		mode         = MODE_COMPILE_DUMP;
	} else if (simple_arg("-export-ir", s)) {
		mode = MODE_COMPILE_EXPORTIR;
	} else if (simple_arg("-jittest", s)) {
		mode = MODE_JITTEST;
	} else if (simple_arg("fsyntax-only", s)) {
		set_mode_gcc_prec(MODE_PARSE_ONLY, full_option);
	} else if (simple_arg("fno-syntax-only", s)) {
	} else {
		return false;
	}
	return true;
}

int action_compile(const char *argv0)
{
	(void)argv0;
	set_handlers(mode);

	begin_statistics();
	if (do_timing)
		timer_init();

	compilation_env_t env;
	memset(&env, 0, sizeof(env));
	env.outname = outname;

	/* process single compilation units (parsing, assembling, ...) */
	int result = EXIT_SUCCESS;
	if (!process_all_units(&env))
		result = EXIT_FAILURE;

	/* link results */
	if (result == EXIT_SUCCESS) {
		bool (*final_step)(compilation_env_t *env, compilation_unit_t *units)
			= mode == MODE_COMPILE_ASSEMBLE_LINK ? link_program
			: mode == MODE_COMPILE_DUMP          ? dump_irg
			: warn_no_linking;
		bool res = final_step(&env, units);
		if (!res) {
			result = EXIT_FAILURE;
		}
	}

	if (do_timing)
		timer_term(print_timing ? stderr : NULL);
	end_statistics();
	return result;
}

int main(int argc, char **argv)
{
	init_temp_files();
	init_driver();
	init_default_driver();
	init_preprocessor();
	init_gen_firm(); /* initialize early because we need to parse options */
	init_ast();
	init_parser();

	options_state_t state;
	memset(&state, 0, sizeof(state));
	state.argc   = argc;
	state.argv   = argv;
	state.action = action_compile;

	/* do early option parsing */
	for (state.i = 1; state.i < argc; ++state.i) {
		if (options_parse_early_target(&state)
		 || options_parse_early_sysroot(&state)
		 || options_parse_early_codegen(&state))
			state.argv[state.i] = NULL;
	}

	if (!isysroot)
		isysroot = lsysroot;

	/* Setup target so later options can override the target defaults */
	target_set_defaults();

	/* parse rest of options */
	for (state.i = 1; state.i < argc; ++state.i) {
		if (state.argv[state.i] == NULL)
			continue;
		if (options_parse_assembler(&state)
		 || options_parse_c_dialect(&state)
		 || options_parse_codegen(&state)
		 || options_parse_diagnostics(&state)
		 || parse_compile_mode_options(&state)
		 || options_parse_driver(&state)
		 || options_parse_help(&state)
		 || options_parse_linker(&state)
		 || options_parse_preprocessor(&state)) {
			continue;
		}
		errorf(NULL, "unknown argument '%s'", argv[state.i]);
		state.argument_errors = true;
	}
	if (state.argument_errors) {
		help_usage(argv[0]);
		return EXIT_FAILURE;
	}

	if (!target_setup())
		return EXIT_FAILURE;

	assert(state.action != NULL);
	int ret = state.action(argv[0]);

	exit_gen_firm();
	exit_ast2firm();
	exit_parser();
	exit_ast();
	exit_preprocessor();
	exit_driver();
	exit_default_driver();
	exit_temp_files();
	return ret;
}