move libtcc interface and helper functions to libtcc.c
[tinycc/kirr.git] / tcc.c
blob599f2109dfdee0d70d1236066658021223dfee0f
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 static int64_t getclock_us(void)
27 #ifdef _WIN32
28 struct _timeb tb;
29 _ftime(&tb);
30 return (tb.time * 1000LL + tb.millitm) * 1000LL;
31 #else
32 struct timeval tv;
33 gettimeofday(&tv, NULL);
34 return tv.tv_sec * 1000000LL + tv.tv_usec;
35 #endif
38 void help(void)
40 printf("tcc version " TCC_VERSION " - Tiny C Compiler - Copyright (C) 2001-2006 Fabrice Bellard\n"
41 "usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
42 " [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-soname name]\n"
43 " [-static] [infile1 infile2...] [-run infile args...]\n"
44 "\n"
45 "General options:\n"
46 " -v display current version, increase verbosity\n"
47 " -c compile only - generate an object file\n"
48 " -o outfile set output filename\n"
49 " -Bdir set tcc internal library path\n"
50 " -bench output compilation statistics\n"
51 " -run run compiled source\n"
52 " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
53 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
54 " -w disable all warnings\n"
55 "Preprocessor options:\n"
56 " -E preprocess only\n"
57 " -Idir add include path 'dir'\n"
58 " -Dsym[=val] define 'sym' with value 'val'\n"
59 " -Usym undefine 'sym'\n"
60 "Linker options:\n"
61 " -Ldir add library path 'dir'\n"
62 " -llib link with dynamic or static library 'lib'\n"
63 " -shared generate a shared library\n"
64 " -soname set name for shared library to be used at runtime\n"
65 " -static static linking\n"
66 " -rdynamic export all global symbols to dynamic linker\n"
67 " -r generate (relocatable) object file\n"
68 "Debugger options:\n"
69 " -g generate runtime debug info\n"
70 #ifdef CONFIG_TCC_BCHECK
71 " -b compile with built-in memory and bounds checker (implies -g)\n"
72 #endif
73 #ifdef CONFIG_TCC_BACKTRACE
74 " -bt N show N callers in stack traces\n"
75 #endif
79 #define TCC_OPTION_HAS_ARG 0x0001
80 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
82 typedef struct TCCOption {
83 const char *name;
84 uint16_t index;
85 uint16_t flags;
86 } TCCOption;
88 enum {
89 TCC_OPTION_HELP,
90 TCC_OPTION_I,
91 TCC_OPTION_D,
92 TCC_OPTION_U,
93 TCC_OPTION_L,
94 TCC_OPTION_B,
95 TCC_OPTION_l,
96 TCC_OPTION_bench,
97 TCC_OPTION_bt,
98 TCC_OPTION_b,
99 TCC_OPTION_g,
100 TCC_OPTION_c,
101 TCC_OPTION_static,
102 TCC_OPTION_shared,
103 TCC_OPTION_soname,
104 TCC_OPTION_o,
105 TCC_OPTION_r,
106 TCC_OPTION_Wl,
107 TCC_OPTION_W,
108 TCC_OPTION_O,
109 TCC_OPTION_m,
110 TCC_OPTION_f,
111 TCC_OPTION_nostdinc,
112 TCC_OPTION_nostdlib,
113 TCC_OPTION_print_search_dirs,
114 TCC_OPTION_rdynamic,
115 TCC_OPTION_run,
116 TCC_OPTION_v,
117 TCC_OPTION_w,
118 TCC_OPTION_pipe,
119 TCC_OPTION_E,
122 static const TCCOption tcc_options[] = {
123 { "h", TCC_OPTION_HELP, 0 },
124 { "?", TCC_OPTION_HELP, 0 },
125 { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
126 { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
127 { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
128 { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
129 { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
130 { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
131 { "bench", TCC_OPTION_bench, 0 },
132 { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
133 #ifdef CONFIG_TCC_BCHECK
134 { "b", TCC_OPTION_b, 0 },
135 #endif
136 { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
137 { "c", TCC_OPTION_c, 0 },
138 { "static", TCC_OPTION_static, 0 },
139 { "shared", TCC_OPTION_shared, 0 },
140 { "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG },
141 { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
142 { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
143 { "rdynamic", TCC_OPTION_rdynamic, 0 },
144 { "r", TCC_OPTION_r, 0 },
145 { "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
146 { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
147 { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
148 { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG },
149 { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
150 { "nostdinc", TCC_OPTION_nostdinc, 0 },
151 { "nostdlib", TCC_OPTION_nostdlib, 0 },
152 { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
153 { "v", TCC_OPTION_v, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
154 { "w", TCC_OPTION_w, 0 },
155 { "pipe", TCC_OPTION_pipe, 0},
156 { "E", TCC_OPTION_E, 0},
157 { NULL },
160 /* convert 'str' into an array of space separated strings */
161 static int expand_args(char ***pargv, const char *str)
163 const char *s1;
164 char **argv, *arg;
165 int argc, len;
167 argc = 0;
168 argv = NULL;
169 for(;;) {
170 while (is_space(*str))
171 str++;
172 if (*str == '\0')
173 break;
174 s1 = str;
175 while (*str != '\0' && !is_space(*str))
176 str++;
177 len = str - s1;
178 arg = tcc_malloc(len + 1);
179 memcpy(arg, s1, len);
180 arg[len] = '\0';
181 dynarray_add((void ***)&argv, &argc, arg);
183 *pargv = argv;
184 return argc;
187 static char **files;
188 static int nb_files, nb_libraries;
189 static int multiple_files;
190 static int print_search_dirs;
191 static int output_type;
192 static int reloc_output;
193 static const char *outfile;
195 int parse_args(TCCState *s, int argc, char **argv)
197 int optind;
198 const TCCOption *popt;
199 const char *optarg, *p1, *r1;
200 char *r;
202 optind = 0;
203 while (optind < argc) {
205 r = argv[optind++];
206 if (r[0] != '-' || r[1] == '\0') {
207 /* add a new file */
208 dynarray_add((void ***)&files, &nb_files, r);
209 if (!multiple_files) {
210 optind--;
211 /* argv[0] will be this file */
212 break;
214 } else {
215 /* find option in table (match only the first chars */
216 popt = tcc_options;
217 for(;;) {
218 p1 = popt->name;
219 if (p1 == NULL)
220 error("invalid option -- '%s'", r);
221 r1 = r + 1;
222 for(;;) {
223 if (*p1 == '\0')
224 goto option_found;
225 if (*r1 != *p1)
226 break;
227 p1++;
228 r1++;
230 popt++;
232 option_found:
233 if (popt->flags & TCC_OPTION_HAS_ARG) {
234 if (*r1 != '\0' || (popt->flags & TCC_OPTION_NOSEP)) {
235 optarg = r1;
236 } else {
237 if (optind >= argc)
238 error("argument to '%s' is missing", r);
239 optarg = argv[optind++];
241 } else {
242 if (*r1 != '\0')
243 return 0;
244 optarg = NULL;
247 switch(popt->index) {
248 case TCC_OPTION_HELP:
249 return 0;
251 case TCC_OPTION_I:
252 if (tcc_add_include_path(s, optarg) < 0)
253 error("too many include paths");
254 break;
255 case TCC_OPTION_D:
257 char *sym, *value;
258 sym = (char *)optarg;
259 value = strchr(sym, '=');
260 if (value) {
261 *value = '\0';
262 value++;
264 tcc_define_symbol(s, sym, value);
266 break;
267 case TCC_OPTION_U:
268 tcc_undefine_symbol(s, optarg);
269 break;
270 case TCC_OPTION_L:
271 tcc_add_library_path(s, optarg);
272 break;
273 case TCC_OPTION_B:
274 /* set tcc utilities path (mainly for tcc development) */
275 tcc_set_lib_path(s, optarg);
276 break;
277 case TCC_OPTION_l:
278 dynarray_add((void ***)&files, &nb_files, r);
279 nb_libraries++;
280 break;
281 case TCC_OPTION_bench:
282 do_bench = 1;
283 break;
284 #ifdef CONFIG_TCC_BACKTRACE
285 case TCC_OPTION_bt:
286 num_callers = atoi(optarg);
287 break;
288 #endif
289 #ifdef CONFIG_TCC_BCHECK
290 case TCC_OPTION_b:
291 do_bounds_check = 1;
292 do_debug = 1;
293 break;
294 #endif
295 case TCC_OPTION_g:
296 do_debug = 1;
297 break;
298 case TCC_OPTION_c:
299 multiple_files = 1;
300 output_type = TCC_OUTPUT_OBJ;
301 break;
302 case TCC_OPTION_static:
303 s->static_link = 1;
304 break;
305 case TCC_OPTION_shared:
306 output_type = TCC_OUTPUT_DLL;
307 break;
308 case TCC_OPTION_soname:
309 s->soname = optarg;
310 break;
311 case TCC_OPTION_o:
312 multiple_files = 1;
313 outfile = optarg;
314 break;
315 case TCC_OPTION_r:
316 /* generate a .o merging several output files */
317 reloc_output = 1;
318 output_type = TCC_OUTPUT_OBJ;
319 break;
320 case TCC_OPTION_nostdinc:
321 s->nostdinc = 1;
322 break;
323 case TCC_OPTION_nostdlib:
324 s->nostdlib = 1;
325 break;
326 case TCC_OPTION_print_search_dirs:
327 print_search_dirs = 1;
328 break;
329 case TCC_OPTION_run:
331 int argc1;
332 char **argv1;
333 argc1 = expand_args(&argv1, optarg);
334 if (argc1 > 0) {
335 parse_args(s, argc1, argv1);
337 multiple_files = 0;
338 output_type = TCC_OUTPUT_MEMORY;
340 break;
341 case TCC_OPTION_v:
342 do {
343 if (0 == verbose++)
344 printf("tcc version %s\n", TCC_VERSION);
345 } while (*optarg++ == 'v');
346 break;
347 case TCC_OPTION_f:
348 if (tcc_set_flag(s, optarg, 1) < 0 && s->warn_unsupported)
349 goto unsupported_option;
350 break;
351 case TCC_OPTION_W:
352 if (tcc_set_warning(s, optarg, 1) < 0 &&
353 s->warn_unsupported)
354 goto unsupported_option;
355 break;
356 case TCC_OPTION_w:
357 s->warn_none = 1;
358 break;
359 case TCC_OPTION_rdynamic:
360 s->rdynamic = 1;
361 break;
362 case TCC_OPTION_Wl:
364 const char *p;
365 if (strstart(optarg, "-Ttext,", &p)) {
366 s->text_addr = strtoul(p, NULL, 16);
367 s->has_text_addr = 1;
368 } else if (strstart(optarg, "--oformat,", &p)) {
369 if (strstart(p, "elf32-", NULL)) {
370 s->output_format = TCC_OUTPUT_FORMAT_ELF;
371 } else if (!strcmp(p, "binary")) {
372 s->output_format = TCC_OUTPUT_FORMAT_BINARY;
373 } else
374 #ifdef TCC_TARGET_COFF
375 if (!strcmp(p, "coff")) {
376 s->output_format = TCC_OUTPUT_FORMAT_COFF;
377 } else
378 #endif
380 error("target %s not found", p);
382 } else {
383 error("unsupported linker option '%s'", optarg);
386 break;
387 case TCC_OPTION_E:
388 output_type = TCC_OUTPUT_PREPROCESS;
389 break;
390 default:
391 if (s->warn_unsupported) {
392 unsupported_option:
393 warning("unsupported option '%s'", r);
395 break;
399 return optind + 1;
402 int main(int argc, char **argv)
404 int i;
405 TCCState *s;
406 int nb_objfiles, ret, optind;
407 char objfilename[1024];
408 int64_t start_time = 0;
410 s = tcc_new();
411 #ifdef _WIN32
412 tcc_set_lib_path_w32(s);
413 #endif
414 output_type = TCC_OUTPUT_EXE;
415 outfile = NULL;
416 multiple_files = 1;
417 files = NULL;
418 nb_files = 0;
419 nb_libraries = 0;
420 reloc_output = 0;
421 print_search_dirs = 0;
422 ret = 0;
424 optind = parse_args(s, argc - 1, argv + 1);
425 if (print_search_dirs) {
426 /* enough for Linux kernel */
427 printf("install: %s/\n", tcc_lib_path);
428 return 0;
430 if (optind == 0 || nb_files == 0) {
431 if (optind && verbose)
432 return 0;
433 help();
434 return 1;
437 nb_objfiles = nb_files - nb_libraries;
439 /* if outfile provided without other options, we output an
440 executable */
441 if (outfile && output_type == TCC_OUTPUT_MEMORY)
442 output_type = TCC_OUTPUT_EXE;
444 /* check -c consistency : only single file handled. XXX: checks file type */
445 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
446 /* accepts only a single input file */
447 if (nb_objfiles != 1)
448 error("cannot specify multiple files with -c");
449 if (nb_libraries != 0)
450 error("cannot specify libraries with -c");
454 if (output_type == TCC_OUTPUT_PREPROCESS) {
455 if (!outfile) {
456 s->outfile = stdout;
457 } else {
458 s->outfile = fopen(outfile, "w");
459 if (!s->outfile)
460 error("could not open '%s", outfile);
462 } else if (output_type != TCC_OUTPUT_MEMORY) {
463 if (!outfile) {
464 /* compute default outfile name */
465 char *ext;
466 const char *name =
467 strcmp(files[0], "-") == 0 ? "a" : tcc_basename(files[0]);
468 pstrcpy(objfilename, sizeof(objfilename), name);
469 ext = tcc_fileextension(objfilename);
470 #ifdef TCC_TARGET_PE
471 if (output_type == TCC_OUTPUT_DLL)
472 strcpy(ext, ".dll");
473 else
474 if (output_type == TCC_OUTPUT_EXE)
475 strcpy(ext, ".exe");
476 else
477 #endif
478 if (output_type == TCC_OUTPUT_OBJ && !reloc_output && *ext)
479 strcpy(ext, ".o");
480 else
481 pstrcpy(objfilename, sizeof(objfilename), "a.out");
482 outfile = objfilename;
486 if (do_bench) {
487 start_time = getclock_us();
490 tcc_set_output_type(s, output_type);
492 /* compile or add each files or library */
493 for(i = 0; i < nb_files && ret == 0; i++) {
494 const char *filename;
496 filename = files[i];
497 if (output_type == TCC_OUTPUT_PREPROCESS) {
498 if (tcc_add_file_internal(s, filename,
499 AFF_PRINT_ERROR | AFF_PREPROCESS) < 0)
500 ret = 1;
501 } else if (filename[0] == '-' && filename[1]) {
502 if (tcc_add_library(s, filename + 2) < 0)
503 error("cannot find %s", filename);
504 } else {
505 if (1 == verbose)
506 printf("-> %s\n", filename);
507 if (tcc_add_file(s, filename) < 0)
508 ret = 1;
512 /* free all files */
513 tcc_free(files);
515 if (ret)
516 goto the_end;
518 if (do_bench) {
519 double total_time;
520 total_time = (double)(getclock_us() - start_time) / 1000000.0;
521 if (total_time < 0.001)
522 total_time = 0.001;
523 if (total_bytes < 1)
524 total_bytes = 1;
525 printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
526 tok_ident - TOK_IDENT, total_lines, total_bytes,
527 total_time, (int)(total_lines / total_time),
528 total_bytes / total_time / 1000000.0);
531 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
532 if (outfile)
533 fclose(s->outfile);
534 } else if (s->output_type == TCC_OUTPUT_MEMORY) {
535 ret = tcc_run(s, argc - optind, argv + optind);
536 } else
537 ret = tcc_output_file(s, outfile) ? 1 : 0;
538 the_end:
539 /* XXX: cannot do it with bound checking because of the malloc hooks */
540 if (!do_bounds_check)
541 tcc_delete(s);
543 #ifdef MEM_DEBUG
544 if (do_bench) {
545 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size);
547 #endif
548 return ret;
551 #endif