move static prototypes to libtcc.c
[tinycc/k1w1.git] / tcc.c
blob9cd88f3956e8dd7b9272e62986dc211069445a59
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,
116 static const TCCOption tcc_options[] = {
117 { "h", TCC_OPTION_HELP, 0 },
118 { "?", TCC_OPTION_HELP, 0 },
119 { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
120 { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
121 { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
122 { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
123 { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
124 { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
125 { "bench", TCC_OPTION_bench, 0 },
126 { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
127 #ifdef CONFIG_TCC_BCHECK
128 { "b", TCC_OPTION_b, 0 },
129 #endif
130 { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
131 { "c", TCC_OPTION_c, 0 },
132 { "static", TCC_OPTION_static, 0 },
133 { "shared", TCC_OPTION_shared, 0 },
134 { "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG },
135 { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
136 { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
137 { "rdynamic", TCC_OPTION_rdynamic, 0 },
138 { "r", TCC_OPTION_r, 0 },
139 { "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
140 { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
141 { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
142 { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG },
143 { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
144 { "nostdinc", TCC_OPTION_nostdinc, 0 },
145 { "nostdlib", TCC_OPTION_nostdlib, 0 },
146 { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
147 { "v", TCC_OPTION_v, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
148 { "w", TCC_OPTION_w, 0 },
149 { "pipe", TCC_OPTION_pipe, 0},
150 { "E", TCC_OPTION_E, 0},
151 { NULL },
154 static int64_t getclock_us(void)
156 #ifdef _WIN32
157 struct _timeb tb;
158 _ftime(&tb);
159 return (tb.time * 1000LL + tb.millitm) * 1000LL;
160 #else
161 struct timeval tv;
162 gettimeofday(&tv, NULL);
163 return tv.tv_sec * 1000000LL + tv.tv_usec;
164 #endif
167 static int strstart(const char *str, const char *val, const char **ptr)
169 const char *p, *q;
170 p = str;
171 q = val;
172 while (*q != '\0') {
173 if (*p != *q)
174 return 0;
175 p++;
176 q++;
178 if (ptr)
179 *ptr = p;
180 return 1;
183 /* convert 'str' into an array of space separated strings */
184 static int expand_args(char ***pargv, const char *str)
186 const char *s1;
187 char **argv, *arg;
188 int argc, len;
190 argc = 0;
191 argv = NULL;
192 for(;;) {
193 while (is_space(*str))
194 str++;
195 if (*str == '\0')
196 break;
197 s1 = str;
198 while (*str != '\0' && !is_space(*str))
199 str++;
200 len = str - s1;
201 arg = tcc_malloc(len + 1);
202 memcpy(arg, s1, len);
203 arg[len] = '\0';
204 dynarray_add((void ***)&argv, &argc, arg);
206 *pargv = argv;
207 return argc;
210 int parse_args(TCCState *s, int argc, char **argv)
212 int optind;
213 const TCCOption *popt;
214 const char *optarg, *p1, *r1;
215 char *r;
217 optind = 0;
218 while (optind < argc) {
220 r = argv[optind++];
221 if (r[0] != '-' || r[1] == '\0') {
222 /* add a new file */
223 dynarray_add((void ***)&files, &nb_files, r);
224 if (!multiple_files) {
225 optind--;
226 /* argv[0] will be this file */
227 break;
229 } else {
230 /* find option in table (match only the first chars */
231 popt = tcc_options;
232 for(;;) {
233 p1 = popt->name;
234 if (p1 == NULL)
235 error("invalid option -- '%s'", r);
236 r1 = r + 1;
237 for(;;) {
238 if (*p1 == '\0')
239 goto option_found;
240 if (*r1 != *p1)
241 break;
242 p1++;
243 r1++;
245 popt++;
247 option_found:
248 if (popt->flags & TCC_OPTION_HAS_ARG) {
249 if (*r1 != '\0' || (popt->flags & TCC_OPTION_NOSEP)) {
250 optarg = r1;
251 } else {
252 if (optind >= argc)
253 error("argument to '%s' is missing", r);
254 optarg = argv[optind++];
256 } else {
257 if (*r1 != '\0')
258 return 0;
259 optarg = NULL;
262 switch(popt->index) {
263 case TCC_OPTION_HELP:
264 return 0;
266 case TCC_OPTION_I:
267 if (tcc_add_include_path(s, optarg) < 0)
268 error("too many include paths");
269 break;
270 case TCC_OPTION_D:
272 char *sym, *value;
273 sym = (char *)optarg;
274 value = strchr(sym, '=');
275 if (value) {
276 *value = '\0';
277 value++;
279 tcc_define_symbol(s, sym, value);
281 break;
282 case TCC_OPTION_U:
283 tcc_undefine_symbol(s, optarg);
284 break;
285 case TCC_OPTION_L:
286 tcc_add_library_path(s, optarg);
287 break;
288 case TCC_OPTION_B:
289 /* set tcc utilities path (mainly for tcc development) */
290 tcc_set_lib_path(s, optarg);
291 break;
292 case TCC_OPTION_l:
293 dynarray_add((void ***)&files, &nb_files, r);
294 nb_libraries++;
295 break;
296 case TCC_OPTION_bench:
297 do_bench = 1;
298 break;
299 #ifdef CONFIG_TCC_BACKTRACE
300 case TCC_OPTION_bt:
301 num_callers = atoi(optarg);
302 break;
303 #endif
304 #ifdef CONFIG_TCC_BCHECK
305 case TCC_OPTION_b:
306 s->do_bounds_check = 1;
307 s->do_debug = 1;
308 break;
309 #endif
310 case TCC_OPTION_g:
311 s->do_debug = 1;
312 break;
313 case TCC_OPTION_c:
314 multiple_files = 1;
315 output_type = TCC_OUTPUT_OBJ;
316 break;
317 case TCC_OPTION_static:
318 s->static_link = 1;
319 break;
320 case TCC_OPTION_shared:
321 output_type = TCC_OUTPUT_DLL;
322 break;
323 case TCC_OPTION_soname:
324 s->soname = optarg;
325 break;
326 case TCC_OPTION_o:
327 multiple_files = 1;
328 outfile = optarg;
329 break;
330 case TCC_OPTION_r:
331 /* generate a .o merging several output files */
332 reloc_output = 1;
333 output_type = TCC_OUTPUT_OBJ;
334 break;
335 case TCC_OPTION_nostdinc:
336 s->nostdinc = 1;
337 break;
338 case TCC_OPTION_nostdlib:
339 s->nostdlib = 1;
340 break;
341 case TCC_OPTION_print_search_dirs:
342 print_search_dirs = 1;
343 break;
344 case TCC_OPTION_run:
346 int argc1;
347 char **argv1;
348 argc1 = expand_args(&argv1, optarg);
349 if (argc1 > 0) {
350 parse_args(s, argc1, argv1);
352 multiple_files = 0;
353 output_type = TCC_OUTPUT_MEMORY;
355 break;
356 case TCC_OPTION_v:
357 do {
358 if (0 == s->verbose++)
359 printf("tcc version %s\n", TCC_VERSION);
360 } while (*optarg++ == 'v');
361 break;
362 case TCC_OPTION_f:
363 if (tcc_set_flag(s, optarg, 1) < 0 && s->warn_unsupported)
364 goto unsupported_option;
365 break;
366 case TCC_OPTION_W:
367 if (tcc_set_warning(s, optarg, 1) < 0 &&
368 s->warn_unsupported)
369 goto unsupported_option;
370 break;
371 case TCC_OPTION_w:
372 s->warn_none = 1;
373 break;
374 case TCC_OPTION_rdynamic:
375 s->rdynamic = 1;
376 break;
377 case TCC_OPTION_Wl:
379 const char *p;
380 if (strstart(optarg, "-Ttext,", &p)) {
381 s->text_addr = strtoul(p, NULL, 16);
382 s->has_text_addr = 1;
383 } else if (strstart(optarg, "--oformat,", &p)) {
384 if (strstart(p, "elf32-", NULL)) {
385 s->output_format = TCC_OUTPUT_FORMAT_ELF;
386 } else if (!strcmp(p, "binary")) {
387 s->output_format = TCC_OUTPUT_FORMAT_BINARY;
388 } else
389 #ifdef TCC_TARGET_COFF
390 if (!strcmp(p, "coff")) {
391 s->output_format = TCC_OUTPUT_FORMAT_COFF;
392 } else
393 #endif
395 error("target %s not found", p);
397 } else {
398 error("unsupported linker option '%s'", optarg);
401 break;
402 case TCC_OPTION_E:
403 output_type = TCC_OUTPUT_PREPROCESS;
404 break;
405 default:
406 if (s->warn_unsupported) {
407 unsupported_option:
408 warning("unsupported option '%s'", r);
410 break;
414 return optind + 1;
417 int main(int argc, char **argv)
419 int i;
420 TCCState *s;
421 int nb_objfiles, ret, optind;
422 char objfilename[1024];
423 int64_t start_time = 0;
425 s = tcc_new();
426 #ifdef _WIN32
427 tcc_set_lib_path_w32(s);
428 #endif
429 output_type = TCC_OUTPUT_EXE;
430 outfile = NULL;
431 multiple_files = 1;
432 files = NULL;
433 nb_files = 0;
434 nb_libraries = 0;
435 reloc_output = 0;
436 print_search_dirs = 0;
437 ret = 0;
439 optind = parse_args(s, argc - 1, argv + 1);
440 if (print_search_dirs) {
441 /* enough for Linux kernel */
442 printf("install: %s/\n", s->tcc_lib_path);
443 return 0;
445 if (optind == 0 || nb_files == 0) {
446 if (optind && s->verbose)
447 return 0;
448 help();
449 return 1;
452 nb_objfiles = nb_files - nb_libraries;
454 /* if outfile provided without other options, we output an
455 executable */
456 if (outfile && output_type == TCC_OUTPUT_MEMORY)
457 output_type = TCC_OUTPUT_EXE;
459 /* check -c consistency : only single file handled. XXX: checks file type */
460 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
461 /* accepts only a single input file */
462 if (nb_objfiles != 1)
463 error("cannot specify multiple files with -c");
464 if (nb_libraries != 0)
465 error("cannot specify libraries with -c");
469 if (output_type == TCC_OUTPUT_PREPROCESS) {
470 if (!outfile) {
471 s->outfile = stdout;
472 } else {
473 s->outfile = fopen(outfile, "w");
474 if (!s->outfile)
475 error("could not open '%s", outfile);
477 } else if (output_type != TCC_OUTPUT_MEMORY) {
478 if (!outfile) {
479 /* compute default outfile name */
480 char *ext;
481 const char *name =
482 strcmp(files[0], "-") == 0 ? "a" : tcc_basename(files[0]);
483 pstrcpy(objfilename, sizeof(objfilename), name);
484 ext = tcc_fileextension(objfilename);
485 #ifdef TCC_TARGET_PE
486 if (output_type == TCC_OUTPUT_DLL)
487 strcpy(ext, ".dll");
488 else
489 if (output_type == TCC_OUTPUT_EXE)
490 strcpy(ext, ".exe");
491 else
492 #endif
493 if (output_type == TCC_OUTPUT_OBJ && !reloc_output && *ext)
494 strcpy(ext, ".o");
495 else
496 pstrcpy(objfilename, sizeof(objfilename), "a.out");
497 outfile = objfilename;
501 if (do_bench) {
502 start_time = getclock_us();
505 tcc_set_output_type(s, output_type);
507 /* compile or add each files or library */
508 for(i = 0; i < nb_files && ret == 0; i++) {
509 const char *filename;
511 filename = files[i];
512 if (output_type == TCC_OUTPUT_PREPROCESS) {
513 if (tcc_add_file_internal(s, filename,
514 AFF_PRINT_ERROR | AFF_PREPROCESS) < 0)
515 ret = 1;
516 } else if (filename[0] == '-' && filename[1]) {
517 if (tcc_add_library(s, filename + 2) < 0)
518 error("cannot find %s", filename);
519 } else {
520 if (1 == s->verbose)
521 printf("-> %s\n", filename);
522 if (tcc_add_file(s, filename) < 0)
523 ret = 1;
527 /* free all files */
528 tcc_free(files);
530 if (ret)
531 goto the_end;
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;
543 the_end:
544 /* XXX: cannot do it with bound checking because of the malloc hooks */
545 if (!s->do_bounds_check)
546 tcc_delete(s);
548 #ifdef MEM_DEBUG
549 if (do_bench) {
550 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size);
552 #endif
553 return ret;