move minor things from libtcc.c to other files
[tinycc/k1w1.git] / tcc.c
blob556ca382c5ce600384b42e114d936316a147db9d
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 #if !defined(LIBTCC)
25 void help(void)
27 printf("tcc version " TCC_VERSION " - Tiny C Compiler - Copyright (C) 2001-2006 Fabrice Bellard\n"
28 "usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
29 " [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-soname name]\n"
30 " [-static] [infile1 infile2...] [-run infile args...]\n"
31 "\n"
32 "General options:\n"
33 " -v display current version, increase verbosity\n"
34 " -c compile only - generate an object file\n"
35 " -o outfile set output filename\n"
36 " -Bdir set tcc internal library path\n"
37 " -bench output compilation statistics\n"
38 " -run run compiled source\n"
39 " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
40 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
41 " -w disable all warnings\n"
42 "Preprocessor options:\n"
43 " -E preprocess only\n"
44 " -Idir add include path 'dir'\n"
45 " -Dsym[=val] define 'sym' with value 'val'\n"
46 " -Usym undefine 'sym'\n"
47 "Linker options:\n"
48 " -Ldir add library path 'dir'\n"
49 " -llib link with dynamic or static library 'lib'\n"
50 " -shared generate a shared library\n"
51 " -soname set name for shared library to be used at runtime\n"
52 " -static static linking\n"
53 " -rdynamic export all global symbols to dynamic linker\n"
54 " -r generate (relocatable) object file\n"
55 "Debugger options:\n"
56 " -g generate runtime debug info\n"
57 #ifdef CONFIG_TCC_BCHECK
58 " -b compile with built-in memory and bounds checker (implies -g)\n"
59 #endif
60 #ifdef CONFIG_TCC_BACKTRACE
61 " -bt N show N callers in stack traces\n"
62 #endif
66 static char **files;
67 static int nb_files, nb_libraries;
68 static int multiple_files;
69 static int print_search_dirs;
70 static int output_type;
71 static int reloc_output;
72 static const char *outfile;
73 static int do_bench = 0;
75 #define TCC_OPTION_HAS_ARG 0x0001
76 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
78 typedef struct TCCOption {
79 const char *name;
80 uint16_t index;
81 uint16_t flags;
82 } TCCOption;
84 enum {
85 TCC_OPTION_HELP,
86 TCC_OPTION_I,
87 TCC_OPTION_D,
88 TCC_OPTION_U,
89 TCC_OPTION_L,
90 TCC_OPTION_B,
91 TCC_OPTION_l,
92 TCC_OPTION_bench,
93 TCC_OPTION_bt,
94 TCC_OPTION_b,
95 TCC_OPTION_g,
96 TCC_OPTION_c,
97 TCC_OPTION_static,
98 TCC_OPTION_shared,
99 TCC_OPTION_soname,
100 TCC_OPTION_o,
101 TCC_OPTION_r,
102 TCC_OPTION_Wl,
103 TCC_OPTION_W,
104 TCC_OPTION_O,
105 TCC_OPTION_m,
106 TCC_OPTION_f,
107 TCC_OPTION_nostdinc,
108 TCC_OPTION_nostdlib,
109 TCC_OPTION_print_search_dirs,
110 TCC_OPTION_rdynamic,
111 TCC_OPTION_run,
112 TCC_OPTION_v,
113 TCC_OPTION_w,
114 TCC_OPTION_pipe,
115 TCC_OPTION_E,
118 static const TCCOption tcc_options[] = {
119 { "h", TCC_OPTION_HELP, 0 },
120 { "?", TCC_OPTION_HELP, 0 },
121 { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
122 { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
123 { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
124 { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
125 { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
126 { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
127 { "bench", TCC_OPTION_bench, 0 },
128 { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
129 #ifdef CONFIG_TCC_BCHECK
130 { "b", TCC_OPTION_b, 0 },
131 #endif
132 { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
133 { "c", TCC_OPTION_c, 0 },
134 { "static", TCC_OPTION_static, 0 },
135 { "shared", TCC_OPTION_shared, 0 },
136 { "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG },
137 { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
138 { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
139 { "rdynamic", TCC_OPTION_rdynamic, 0 },
140 { "r", TCC_OPTION_r, 0 },
141 { "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
142 { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
143 { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
144 { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG },
145 { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
146 { "nostdinc", TCC_OPTION_nostdinc, 0 },
147 { "nostdlib", TCC_OPTION_nostdlib, 0 },
148 { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
149 { "v", TCC_OPTION_v, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
150 { "w", TCC_OPTION_w, 0 },
151 { "pipe", TCC_OPTION_pipe, 0},
152 { "E", TCC_OPTION_E, 0},
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 do_bounds_check = 1;
309 do_debug = 1;
310 break;
311 #endif
312 case TCC_OPTION_g:
313 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 == 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 default:
408 if (s->warn_unsupported) {
409 unsupported_option:
410 warning("unsupported option '%s'", r);
412 break;
416 return optind + 1;
419 int main(int argc, char **argv)
421 int i;
422 TCCState *s;
423 int nb_objfiles, ret, optind;
424 char objfilename[1024];
425 int64_t start_time = 0;
427 s = tcc_new();
428 #ifdef _WIN32
429 tcc_set_lib_path_w32(s);
430 #endif
431 output_type = TCC_OUTPUT_EXE;
432 outfile = NULL;
433 multiple_files = 1;
434 files = NULL;
435 nb_files = 0;
436 nb_libraries = 0;
437 reloc_output = 0;
438 print_search_dirs = 0;
439 ret = 0;
441 optind = parse_args(s, argc - 1, argv + 1);
442 if (print_search_dirs) {
443 /* enough for Linux kernel */
444 printf("install: %s/\n", tcc_lib_path);
445 return 0;
447 if (optind == 0 || nb_files == 0) {
448 if (optind && verbose)
449 return 0;
450 help();
451 return 1;
454 nb_objfiles = nb_files - nb_libraries;
456 /* if outfile provided without other options, we output an
457 executable */
458 if (outfile && output_type == TCC_OUTPUT_MEMORY)
459 output_type = TCC_OUTPUT_EXE;
461 /* check -c consistency : only single file handled. XXX: checks file type */
462 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
463 /* accepts only a single input file */
464 if (nb_objfiles != 1)
465 error("cannot specify multiple files with -c");
466 if (nb_libraries != 0)
467 error("cannot specify libraries with -c");
471 if (output_type == TCC_OUTPUT_PREPROCESS) {
472 if (!outfile) {
473 s->outfile = stdout;
474 } else {
475 s->outfile = fopen(outfile, "w");
476 if (!s->outfile)
477 error("could not open '%s", outfile);
479 } else if (output_type != TCC_OUTPUT_MEMORY) {
480 if (!outfile) {
481 /* compute default outfile name */
482 char *ext;
483 const char *name =
484 strcmp(files[0], "-") == 0 ? "a" : tcc_basename(files[0]);
485 pstrcpy(objfilename, sizeof(objfilename), name);
486 ext = tcc_fileextension(objfilename);
487 #ifdef TCC_TARGET_PE
488 if (output_type == TCC_OUTPUT_DLL)
489 strcpy(ext, ".dll");
490 else
491 if (output_type == TCC_OUTPUT_EXE)
492 strcpy(ext, ".exe");
493 else
494 #endif
495 if (output_type == TCC_OUTPUT_OBJ && !reloc_output && *ext)
496 strcpy(ext, ".o");
497 else
498 pstrcpy(objfilename, sizeof(objfilename), "a.out");
499 outfile = objfilename;
503 if (do_bench) {
504 start_time = getclock_us();
507 tcc_set_output_type(s, output_type);
509 /* compile or add each files or library */
510 for(i = 0; i < nb_files && ret == 0; i++) {
511 const char *filename;
513 filename = files[i];
514 if (output_type == TCC_OUTPUT_PREPROCESS) {
515 if (tcc_add_file_internal(s, filename,
516 AFF_PRINT_ERROR | AFF_PREPROCESS) < 0)
517 ret = 1;
518 } else if (filename[0] == '-' && filename[1]) {
519 if (tcc_add_library(s, filename + 2) < 0)
520 error("cannot find %s", filename);
521 } else {
522 if (1 == 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 (ret)
533 goto the_end;
535 if (do_bench) {
536 double total_time;
537 total_time = (double)(getclock_us() - start_time) / 1000000.0;
538 if (total_time < 0.001)
539 total_time = 0.001;
540 if (total_bytes < 1)
541 total_bytes = 1;
542 printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
543 tok_ident - TOK_IDENT, total_lines, total_bytes,
544 total_time, (int)(total_lines / total_time),
545 total_bytes / total_time / 1000000.0);
548 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
549 if (outfile)
550 fclose(s->outfile);
551 } else if (s->output_type == TCC_OUTPUT_MEMORY) {
552 ret = tcc_run(s, argc - optind, argv + optind);
553 } else
554 ret = tcc_output_file(s, outfile) ? 1 : 0;
555 the_end:
556 /* XXX: cannot do it with bound checking because of the malloc hooks */
557 if (!do_bounds_check)
558 tcc_delete(s);
560 #ifdef MEM_DEBUG
561 if (do_bench) {
562 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size);
564 #endif
565 return ret;
568 #endif