tccpe: use more official structs
[tinycc/kirr.git] / tcc.c
blobdaa10135a1ae75057f84f201bd3ee651c80525eb
1 /*
2 * TCC - Tiny C Compiler
3 *
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "libtcc.c"
23 void help(void)
25 printf("tcc version " TCC_VERSION " - Tiny C Compiler - Copyright (C) 2001-2006 Fabrice Bellard\n"
26 "usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
27 " [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-soname name]\n"
28 " [-static] [infile1 infile2...] [-run infile args...]\n"
29 "\n"
30 "General options:\n"
31 " -v display current version, increase verbosity\n"
32 " -c compile only - generate an object file\n"
33 " -o outfile set output filename\n"
34 " -Bdir set tcc internal library path\n"
35 " -bench output compilation statistics\n"
36 " -run run compiled source\n"
37 " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
38 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
39 " -w disable all warnings\n"
40 "Preprocessor options:\n"
41 " -E preprocess only\n"
42 " -Idir add include path 'dir'\n"
43 " -Dsym[=val] define 'sym' with value 'val'\n"
44 " -Usym undefine 'sym'\n"
45 "Linker options:\n"
46 " -Ldir add library path 'dir'\n"
47 " -llib link with dynamic or static library 'lib'\n"
48 " -shared generate a shared library\n"
49 " -soname set name for shared library to be used at runtime\n"
50 " -static static linking\n"
51 " -rdynamic export all global symbols to dynamic linker\n"
52 " -r generate (relocatable) object file\n"
53 "Debugger options:\n"
54 " -g generate runtime debug info\n"
55 #ifdef CONFIG_TCC_BCHECK
56 " -b compile with built-in memory and bounds checker (implies -g)\n"
57 #endif
58 #ifdef CONFIG_TCC_BACKTRACE
59 " -bt N show N callers in stack traces\n"
60 #endif
64 static char **files;
65 static int nb_files, nb_libraries;
66 static int multiple_files;
67 static int print_search_dirs;
68 static int output_type;
69 static int reloc_output;
70 static const char *outfile;
71 static int do_bench = 0;
73 #define TCC_OPTION_HAS_ARG 0x0001
74 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
76 typedef struct TCCOption {
77 const char *name;
78 uint16_t index;
79 uint16_t flags;
80 } TCCOption;
82 enum {
83 TCC_OPTION_HELP,
84 TCC_OPTION_I,
85 TCC_OPTION_D,
86 TCC_OPTION_U,
87 TCC_OPTION_L,
88 TCC_OPTION_B,
89 TCC_OPTION_l,
90 TCC_OPTION_bench,
91 TCC_OPTION_bt,
92 TCC_OPTION_b,
93 TCC_OPTION_g,
94 TCC_OPTION_c,
95 TCC_OPTION_static,
96 TCC_OPTION_shared,
97 TCC_OPTION_soname,
98 TCC_OPTION_o,
99 TCC_OPTION_r,
100 TCC_OPTION_Wl,
101 TCC_OPTION_W,
102 TCC_OPTION_O,
103 TCC_OPTION_m,
104 TCC_OPTION_f,
105 TCC_OPTION_nostdinc,
106 TCC_OPTION_nostdlib,
107 TCC_OPTION_print_search_dirs,
108 TCC_OPTION_rdynamic,
109 TCC_OPTION_run,
110 TCC_OPTION_v,
111 TCC_OPTION_w,
112 TCC_OPTION_pipe,
113 TCC_OPTION_E,
114 TCC_OPTION_x,
117 static const TCCOption tcc_options[] = {
118 { "h", TCC_OPTION_HELP, 0 },
119 { "?", TCC_OPTION_HELP, 0 },
120 { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
121 { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
122 { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
123 { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
124 { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
125 { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
126 { "bench", TCC_OPTION_bench, 0 },
127 { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
128 #ifdef CONFIG_TCC_BCHECK
129 { "b", TCC_OPTION_b, 0 },
130 #endif
131 { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
132 { "c", TCC_OPTION_c, 0 },
133 { "static", TCC_OPTION_static, 0 },
134 { "shared", TCC_OPTION_shared, 0 },
135 { "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG },
136 { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
137 { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
138 { "rdynamic", TCC_OPTION_rdynamic, 0 },
139 { "r", TCC_OPTION_r, 0 },
140 { "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
141 { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
142 { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
143 { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG },
144 { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
145 { "nostdinc", TCC_OPTION_nostdinc, 0 },
146 { "nostdlib", TCC_OPTION_nostdlib, 0 },
147 { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
148 { "v", TCC_OPTION_v, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
149 { "w", TCC_OPTION_w, 0 },
150 { "pipe", TCC_OPTION_pipe, 0},
151 { "E", TCC_OPTION_E, 0},
152 { "x", TCC_OPTION_x, TCC_OPTION_HAS_ARG },
153 { NULL },
156 static int64_t getclock_us(void)
158 #ifdef _WIN32
159 struct _timeb tb;
160 _ftime(&tb);
161 return (tb.time * 1000LL + tb.millitm) * 1000LL;
162 #else
163 struct timeval tv;
164 gettimeofday(&tv, NULL);
165 return tv.tv_sec * 1000000LL + tv.tv_usec;
166 #endif
169 static int strstart(const char *str, const char *val, const char **ptr)
171 const char *p, *q;
172 p = str;
173 q = val;
174 while (*q != '\0') {
175 if (*p != *q)
176 return 0;
177 p++;
178 q++;
180 if (ptr)
181 *ptr = p;
182 return 1;
185 /* convert 'str' into an array of space separated strings */
186 static int expand_args(char ***pargv, const char *str)
188 const char *s1;
189 char **argv, *arg;
190 int argc, len;
192 argc = 0;
193 argv = NULL;
194 for(;;) {
195 while (is_space(*str))
196 str++;
197 if (*str == '\0')
198 break;
199 s1 = str;
200 while (*str != '\0' && !is_space(*str))
201 str++;
202 len = str - s1;
203 arg = tcc_malloc(len + 1);
204 memcpy(arg, s1, len);
205 arg[len] = '\0';
206 dynarray_add((void ***)&argv, &argc, arg);
208 *pargv = argv;
209 return argc;
212 int parse_args(TCCState *s, int argc, char **argv)
214 int optind;
215 const TCCOption *popt;
216 const char *optarg, *p1, *r1;
217 char *r;
219 optind = 0;
220 while (optind < argc) {
222 r = argv[optind++];
223 if (r[0] != '-' || r[1] == '\0') {
224 /* add a new file */
225 dynarray_add((void ***)&files, &nb_files, r);
226 if (!multiple_files) {
227 optind--;
228 /* argv[0] will be this file */
229 break;
231 } else {
232 /* find option in table (match only the first chars */
233 popt = tcc_options;
234 for(;;) {
235 p1 = popt->name;
236 if (p1 == NULL)
237 error("invalid option -- '%s'", r);
238 r1 = r + 1;
239 for(;;) {
240 if (*p1 == '\0')
241 goto option_found;
242 if (*r1 != *p1)
243 break;
244 p1++;
245 r1++;
247 popt++;
249 option_found:
250 if (popt->flags & TCC_OPTION_HAS_ARG) {
251 if (*r1 != '\0' || (popt->flags & TCC_OPTION_NOSEP)) {
252 optarg = r1;
253 } else {
254 if (optind >= argc)
255 error("argument to '%s' is missing", r);
256 optarg = argv[optind++];
258 } else {
259 if (*r1 != '\0')
260 return 0;
261 optarg = NULL;
264 switch(popt->index) {
265 case TCC_OPTION_HELP:
266 return 0;
268 case TCC_OPTION_I:
269 if (tcc_add_include_path(s, optarg) < 0)
270 error("too many include paths");
271 break;
272 case TCC_OPTION_D:
274 char *sym, *value;
275 sym = (char *)optarg;
276 value = strchr(sym, '=');
277 if (value) {
278 *value = '\0';
279 value++;
281 tcc_define_symbol(s, sym, value);
283 break;
284 case TCC_OPTION_U:
285 tcc_undefine_symbol(s, optarg);
286 break;
287 case TCC_OPTION_L:
288 tcc_add_library_path(s, optarg);
289 break;
290 case TCC_OPTION_B:
291 /* set tcc utilities path (mainly for tcc development) */
292 tcc_set_lib_path(s, optarg);
293 break;
294 case TCC_OPTION_l:
295 dynarray_add((void ***)&files, &nb_files, r);
296 nb_libraries++;
297 break;
298 case TCC_OPTION_bench:
299 do_bench = 1;
300 break;
301 #ifdef CONFIG_TCC_BACKTRACE
302 case TCC_OPTION_bt:
303 num_callers = atoi(optarg);
304 break;
305 #endif
306 #ifdef CONFIG_TCC_BCHECK
307 case TCC_OPTION_b:
308 s->do_bounds_check = 1;
309 s->do_debug = 1;
310 break;
311 #endif
312 case TCC_OPTION_g:
313 s->do_debug = 1;
314 break;
315 case TCC_OPTION_c:
316 multiple_files = 1;
317 output_type = TCC_OUTPUT_OBJ;
318 break;
319 case TCC_OPTION_static:
320 s->static_link = 1;
321 break;
322 case TCC_OPTION_shared:
323 output_type = TCC_OUTPUT_DLL;
324 break;
325 case TCC_OPTION_soname:
326 s->soname = optarg;
327 break;
328 case TCC_OPTION_o:
329 multiple_files = 1;
330 outfile = optarg;
331 break;
332 case TCC_OPTION_r:
333 /* generate a .o merging several output files */
334 reloc_output = 1;
335 output_type = TCC_OUTPUT_OBJ;
336 break;
337 case TCC_OPTION_nostdinc:
338 s->nostdinc = 1;
339 break;
340 case TCC_OPTION_nostdlib:
341 s->nostdlib = 1;
342 break;
343 case TCC_OPTION_print_search_dirs:
344 print_search_dirs = 1;
345 break;
346 case TCC_OPTION_run:
348 int argc1;
349 char **argv1;
350 argc1 = expand_args(&argv1, optarg);
351 if (argc1 > 0) {
352 parse_args(s, argc1, argv1);
354 multiple_files = 0;
355 output_type = TCC_OUTPUT_MEMORY;
357 break;
358 case TCC_OPTION_v:
359 do {
360 if (0 == s->verbose++)
361 printf("tcc version %s\n", TCC_VERSION);
362 } while (*optarg++ == 'v');
363 break;
364 case TCC_OPTION_f:
365 if (tcc_set_flag(s, optarg, 1) < 0 && s->warn_unsupported)
366 goto unsupported_option;
367 break;
368 case TCC_OPTION_W:
369 if (tcc_set_warning(s, optarg, 1) < 0 &&
370 s->warn_unsupported)
371 goto unsupported_option;
372 break;
373 case TCC_OPTION_w:
374 s->warn_none = 1;
375 break;
376 case TCC_OPTION_rdynamic:
377 s->rdynamic = 1;
378 break;
379 case TCC_OPTION_Wl:
381 const char *p;
382 if (strstart(optarg, "-Ttext,", &p)) {
383 s->text_addr = strtoul(p, NULL, 16);
384 s->has_text_addr = 1;
385 } else if (strstart(optarg, "--oformat,", &p)) {
386 if (strstart(p, "elf32-", NULL)) {
387 s->output_format = TCC_OUTPUT_FORMAT_ELF;
388 } else if (!strcmp(p, "binary")) {
389 s->output_format = TCC_OUTPUT_FORMAT_BINARY;
390 } else
391 #ifdef TCC_TARGET_COFF
392 if (!strcmp(p, "coff")) {
393 s->output_format = TCC_OUTPUT_FORMAT_COFF;
394 } else
395 #endif
397 error("target %s not found", p);
399 } else {
400 error("unsupported linker option '%s'", optarg);
403 break;
404 case TCC_OPTION_E:
405 output_type = TCC_OUTPUT_PREPROCESS;
406 break;
407 case TCC_OPTION_x:
408 break;
409 default:
410 if (s->warn_unsupported) {
411 unsupported_option:
412 warning("unsupported option '%s'", r);
414 break;
418 return optind + 1;
421 int main(int argc, char **argv)
423 int i;
424 TCCState *s;
425 int nb_objfiles, ret, optind;
426 char objfilename[1024];
427 int64_t start_time = 0;
429 s = tcc_new();
430 #ifdef _WIN32
431 tcc_set_lib_path_w32(s);
432 #endif
433 output_type = TCC_OUTPUT_EXE;
434 outfile = NULL;
435 multiple_files = 1;
436 files = NULL;
437 nb_files = 0;
438 nb_libraries = 0;
439 reloc_output = 0;
440 print_search_dirs = 0;
441 ret = 0;
443 optind = parse_args(s, argc - 1, argv + 1);
444 if (print_search_dirs) {
445 /* enough for Linux kernel */
446 printf("install: %s/\n", s->tcc_lib_path);
447 return 0;
449 if (optind == 0 || nb_files == 0) {
450 if (optind && s->verbose)
451 return 0;
452 help();
453 return 1;
456 nb_objfiles = nb_files - nb_libraries;
458 /* if outfile provided without other options, we output an
459 executable */
460 if (outfile && output_type == TCC_OUTPUT_MEMORY)
461 output_type = TCC_OUTPUT_EXE;
463 /* check -c consistency : only single file handled. XXX: checks file type */
464 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
465 /* accepts only a single input file */
466 if (nb_objfiles != 1)
467 error("cannot specify multiple files with -c");
468 if (nb_libraries != 0)
469 error("cannot specify libraries with -c");
473 if (output_type == TCC_OUTPUT_PREPROCESS) {
474 if (!outfile) {
475 s->outfile = stdout;
476 } else {
477 s->outfile = fopen(outfile, "w");
478 if (!s->outfile)
479 error("could not open '%s", outfile);
481 } else if (output_type != TCC_OUTPUT_MEMORY) {
482 if (!outfile) {
483 /* compute default outfile name */
484 char *ext;
485 const char *name =
486 strcmp(files[0], "-") == 0 ? "a" : tcc_basename(files[0]);
487 pstrcpy(objfilename, sizeof(objfilename), name);
488 ext = tcc_fileextension(objfilename);
489 #ifdef TCC_TARGET_PE
490 if (output_type == TCC_OUTPUT_DLL)
491 strcpy(ext, ".dll");
492 else
493 if (output_type == TCC_OUTPUT_EXE)
494 strcpy(ext, ".exe");
495 else
496 #endif
497 if (output_type == TCC_OUTPUT_OBJ && !reloc_output && *ext)
498 strcpy(ext, ".o");
499 else
500 pstrcpy(objfilename, sizeof(objfilename), "a.out");
501 outfile = objfilename;
505 if (do_bench) {
506 start_time = getclock_us();
509 tcc_set_output_type(s, output_type);
511 /* compile or add each files or library */
512 for(i = 0; i < nb_files && ret == 0; i++) {
513 const char *filename;
515 filename = files[i];
516 if (filename[0] == '-' && filename[1]) {
517 if (tcc_add_library(s, filename + 2) < 0) {
518 error_noabort("cannot find %s", filename);
519 ret = 1;
521 } else {
522 if (1 == s->verbose)
523 printf("-> %s\n", filename);
524 if (tcc_add_file(s, filename) < 0)
525 ret = 1;
529 /* free all files */
530 tcc_free(files);
532 if (0 == ret) {
533 if (do_bench)
534 tcc_print_stats(s, getclock_us() - start_time);
536 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
537 if (outfile)
538 fclose(s->outfile);
539 } else if (s->output_type == TCC_OUTPUT_MEMORY)
540 ret = tcc_run(s, argc - optind, argv + optind);
541 else
542 ret = tcc_output_file(s, outfile) ? 1 : 0;
545 tcc_delete(s);
547 #ifdef MEM_DEBUG
548 if (do_bench) {
549 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size);
551 #endif
552 return ret;