Fixed compilation error in i386-asm.c
[tinycc/kirr.git] / tcc.c
blobce5e09f9380566353b14df06c878402f5d66f714
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 #ifdef TCC_USE_LIBTCC
22 #include "tcc.h"
23 #else
24 #include "libtcc.c"
25 #endif
27 void help(void)
29 printf("tcc version " TCC_VERSION " - Tiny C Compiler - Copyright (C) 2001-2006 Fabrice Bellard\n"
30 "usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
31 " [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-soname name]\n"
32 " [-static] [infile1 infile2...] [-run infile args...]\n"
33 "\n"
34 "General options:\n"
35 " -v display current version, increase verbosity\n"
36 " -c compile only - generate an object file\n"
37 " -o outfile set output filename\n"
38 " -Bdir set tcc internal library path\n"
39 " -bench output compilation statistics\n"
40 " -run run compiled source\n"
41 " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
42 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
43 " -w disable all warnings\n"
44 "Preprocessor options:\n"
45 " -E preprocess only\n"
46 " -Idir add include path 'dir'\n"
47 " -Dsym[=val] define 'sym' with value 'val'\n"
48 " -Usym undefine 'sym'\n"
49 "Linker options:\n"
50 " -Ldir add library path 'dir'\n"
51 " -llib link with dynamic or static library 'lib'\n"
52 " -shared generate a shared library\n"
53 " -soname set name for shared library to be used at runtime\n"
54 " -static static linking\n"
55 " -rdynamic export all global symbols to dynamic linker\n"
56 " -r generate (relocatable) object file\n"
57 "Debugger options:\n"
58 " -g generate runtime debug info\n"
59 #ifdef CONFIG_TCC_BCHECK
60 " -b compile with built-in memory and bounds checker (implies -g)\n"
61 #endif
62 #ifdef CONFIG_TCC_BACKTRACE
63 " -bt N show N callers in stack traces\n"
64 #endif
68 static char **files;
69 static int nb_files, nb_libraries;
70 static int multiple_files;
71 static int print_search_dirs;
72 static int output_type;
73 static int reloc_output;
74 static const char *outfile;
75 static int do_bench = 0;
77 #define TCC_OPTION_HAS_ARG 0x0001
78 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
80 typedef struct TCCOption {
81 const char *name;
82 uint16_t index;
83 uint16_t flags;
84 } TCCOption;
86 enum {
87 TCC_OPTION_HELP,
88 TCC_OPTION_I,
89 TCC_OPTION_D,
90 TCC_OPTION_U,
91 TCC_OPTION_L,
92 TCC_OPTION_B,
93 TCC_OPTION_l,
94 TCC_OPTION_bench,
95 TCC_OPTION_bt,
96 TCC_OPTION_b,
97 TCC_OPTION_g,
98 TCC_OPTION_c,
99 TCC_OPTION_static,
100 TCC_OPTION_shared,
101 TCC_OPTION_soname,
102 TCC_OPTION_o,
103 TCC_OPTION_r,
104 TCC_OPTION_Wl,
105 TCC_OPTION_W,
106 TCC_OPTION_O,
107 TCC_OPTION_m,
108 TCC_OPTION_f,
109 TCC_OPTION_nostdinc,
110 TCC_OPTION_nostdlib,
111 TCC_OPTION_print_search_dirs,
112 TCC_OPTION_rdynamic,
113 TCC_OPTION_run,
114 TCC_OPTION_v,
115 TCC_OPTION_w,
116 TCC_OPTION_pipe,
117 TCC_OPTION_E,
118 TCC_OPTION_x,
121 static const TCCOption tcc_options[] = {
122 { "h", TCC_OPTION_HELP, 0 },
123 { "?", TCC_OPTION_HELP, 0 },
124 { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
125 { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
126 { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
127 { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
128 { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
129 { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
130 { "bench", TCC_OPTION_bench, 0 },
131 { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
132 #ifdef CONFIG_TCC_BCHECK
133 { "b", TCC_OPTION_b, 0 },
134 #endif
135 { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
136 { "c", TCC_OPTION_c, 0 },
137 { "static", TCC_OPTION_static, 0 },
138 { "shared", TCC_OPTION_shared, 0 },
139 { "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG },
140 { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
141 { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
142 { "rdynamic", TCC_OPTION_rdynamic, 0 },
143 { "r", TCC_OPTION_r, 0 },
144 { "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
145 { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
146 { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
147 { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG },
148 { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
149 { "nostdinc", TCC_OPTION_nostdinc, 0 },
150 { "nostdlib", TCC_OPTION_nostdlib, 0 },
151 { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
152 { "v", TCC_OPTION_v, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
153 { "w", TCC_OPTION_w, 0 },
154 { "pipe", TCC_OPTION_pipe, 0},
155 { "E", TCC_OPTION_E, 0},
156 { "x", TCC_OPTION_x, TCC_OPTION_HAS_ARG },
157 { NULL },
160 static int64_t getclock_us(void)
162 #ifdef _WIN32
163 struct _timeb tb;
164 _ftime(&tb);
165 return (tb.time * 1000LL + tb.millitm) * 1000LL;
166 #else
167 struct timeval tv;
168 gettimeofday(&tv, NULL);
169 return tv.tv_sec * 1000000LL + tv.tv_usec;
170 #endif
173 static int strstart(const char *str, const char *val, const char **ptr)
175 const char *p, *q;
176 p = str;
177 q = val;
178 while (*q != '\0') {
179 if (*p != *q)
180 return 0;
181 p++;
182 q++;
184 if (ptr)
185 *ptr = p;
186 return 1;
189 /* convert 'str' into an array of space separated strings */
190 static int expand_args(char ***pargv, const char *str)
192 const char *s1;
193 char **argv, *arg;
194 int argc, len;
196 argc = 0;
197 argv = NULL;
198 for(;;) {
199 while (is_space(*str))
200 str++;
201 if (*str == '\0')
202 break;
203 s1 = str;
204 while (*str != '\0' && !is_space(*str))
205 str++;
206 len = str - s1;
207 arg = tcc_malloc(len + 1);
208 memcpy(arg, s1, len);
209 arg[len] = '\0';
210 dynarray_add((void ***)&argv, &argc, arg);
212 *pargv = argv;
213 return argc;
216 int parse_args(TCCState *s, int argc, char **argv)
218 int optind;
219 const TCCOption *popt;
220 const char *optarg, *p1, *r1;
221 char *r;
223 optind = 0;
224 while (optind < argc) {
226 r = argv[optind++];
227 if (r[0] != '-' || r[1] == '\0') {
228 /* add a new file */
229 dynarray_add((void ***)&files, &nb_files, r);
230 if (!multiple_files) {
231 optind--;
232 /* argv[0] will be this file */
233 break;
235 } else {
236 /* find option in table (match only the first chars */
237 popt = tcc_options;
238 for(;;) {
239 p1 = popt->name;
240 if (p1 == NULL)
241 error("invalid option -- '%s'", r);
242 r1 = r + 1;
243 for(;;) {
244 if (*p1 == '\0')
245 goto option_found;
246 if (*r1 != *p1)
247 break;
248 p1++;
249 r1++;
251 popt++;
253 option_found:
254 if (popt->flags & TCC_OPTION_HAS_ARG) {
255 if (*r1 != '\0' || (popt->flags & TCC_OPTION_NOSEP)) {
256 optarg = r1;
257 } else {
258 if (optind >= argc)
259 error("argument to '%s' is missing", r);
260 optarg = argv[optind++];
262 } else {
263 if (*r1 != '\0')
264 return 0;
265 optarg = NULL;
268 switch(popt->index) {
269 case TCC_OPTION_HELP:
270 return 0;
272 case TCC_OPTION_I:
273 if (tcc_add_include_path(s, optarg) < 0)
274 error("too many include paths");
275 break;
276 case TCC_OPTION_D:
278 char *sym, *value;
279 sym = (char *)optarg;
280 value = strchr(sym, '=');
281 if (value) {
282 *value = '\0';
283 value++;
285 tcc_define_symbol(s, sym, value);
287 break;
288 case TCC_OPTION_U:
289 tcc_undefine_symbol(s, optarg);
290 break;
291 case TCC_OPTION_L:
292 tcc_add_library_path(s, optarg);
293 break;
294 case TCC_OPTION_B:
295 /* set tcc utilities path (mainly for tcc development) */
296 tcc_set_lib_path(s, optarg);
297 break;
298 case TCC_OPTION_l:
299 dynarray_add((void ***)&files, &nb_files, r);
300 nb_libraries++;
301 break;
302 case TCC_OPTION_bench:
303 do_bench = 1;
304 break;
305 #ifdef CONFIG_TCC_BACKTRACE
306 case TCC_OPTION_bt:
307 num_callers = atoi(optarg);
308 break;
309 #endif
310 #ifdef CONFIG_TCC_BCHECK
311 case TCC_OPTION_b:
312 s->do_bounds_check = 1;
313 s->do_debug = 1;
314 break;
315 #endif
316 case TCC_OPTION_g:
317 s->do_debug = 1;
318 break;
319 case TCC_OPTION_c:
320 multiple_files = 1;
321 output_type = TCC_OUTPUT_OBJ;
322 break;
323 case TCC_OPTION_static:
324 s->static_link = 1;
325 break;
326 case TCC_OPTION_shared:
327 output_type = TCC_OUTPUT_DLL;
328 break;
329 case TCC_OPTION_soname:
330 s->soname = optarg;
331 break;
332 case TCC_OPTION_o:
333 multiple_files = 1;
334 outfile = optarg;
335 break;
336 case TCC_OPTION_r:
337 /* generate a .o merging several output files */
338 reloc_output = 1;
339 output_type = TCC_OUTPUT_OBJ;
340 break;
341 case TCC_OPTION_nostdinc:
342 s->nostdinc = 1;
343 break;
344 case TCC_OPTION_nostdlib:
345 s->nostdlib = 1;
346 break;
347 case TCC_OPTION_print_search_dirs:
348 print_search_dirs = 1;
349 break;
350 case TCC_OPTION_run:
352 int argc1;
353 char **argv1;
354 argc1 = expand_args(&argv1, optarg);
355 if (argc1 > 0) {
356 parse_args(s, argc1, argv1);
358 multiple_files = 0;
359 output_type = TCC_OUTPUT_MEMORY;
361 break;
362 case TCC_OPTION_v:
363 do {
364 if (0 == s->verbose++)
365 printf("tcc version %s\n", TCC_VERSION);
366 } while (*optarg++ == 'v');
367 break;
368 case TCC_OPTION_f:
369 if (tcc_set_flag(s, optarg, 1) < 0 && s->warn_unsupported)
370 goto unsupported_option;
371 break;
372 case TCC_OPTION_W:
373 if (tcc_set_warning(s, optarg, 1) < 0 &&
374 s->warn_unsupported)
375 goto unsupported_option;
376 break;
377 case TCC_OPTION_w:
378 s->warn_none = 1;
379 break;
380 case TCC_OPTION_rdynamic:
381 s->rdynamic = 1;
382 break;
383 case TCC_OPTION_Wl:
385 const char *p;
386 if (strstart(optarg, "-Ttext,", &p)) {
387 s->text_addr = strtoul(p, NULL, 16);
388 s->has_text_addr = 1;
389 } else if (strstart(optarg, "--section-alignment,", &p)) {
390 s->section_align = strtoul(p, NULL, 16);
391 } else if (strstart(optarg, "--image-base,", &p)) {
392 s->text_addr = strtoul(p, NULL, 16);
393 s->has_text_addr = 1;
394 #ifdef TCC_TARGET_PE
395 } else if (strstart(optarg, "--file-alignment,", &p)) {
396 s->pe_file_align = strtoul(p, NULL, 16);
397 } else if (strstart(optarg, "--subsystem,", &p)) {
398 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
399 if (!strcmp(p, "native"))
400 s->pe_subsystem = 1;
401 else if (!strcmp(p, "console"))
402 s->pe_subsystem = 3;
403 else if (!strcmp(p, "gui"))
404 s->pe_subsystem = 2;
405 else if (!strcmp(p, "posix"))
406 s->pe_subsystem = 7;
407 else if (!strcmp(p, "efiapp"))
408 s->pe_subsystem = 10;
409 else if (!strcmp(p, "efiboot"))
410 s->pe_subsystem = 11;
411 else if (!strcmp(p, "efiruntime"))
412 s->pe_subsystem = 12;
413 else if (!strcmp(p, "efirom"))
414 s->pe_subsystem = 13;
415 #elif defined(TCC_TARGET_ARM)
416 if (!strcmp(p, "wince"))
417 s->pe_subsystem = 9;
418 #endif
419 else {
420 error("invalid subsystem '%s'", p);
422 #endif
423 } else if (strstart(optarg, "--oformat,", &p)) {
424 #if defined(TCC_TARGET_PE)
425 if (strstart(p, "pe-", NULL)) {
426 #else
427 #if defined(TCC_TARGET_X86_64)
428 if (strstart(p, "elf64-", NULL)) {
429 #else
430 if (strstart(p, "elf32-", NULL)) {
431 #endif
432 #endif
433 s->output_format = TCC_OUTPUT_FORMAT_ELF;
434 } else if (!strcmp(p, "binary")) {
435 s->output_format = TCC_OUTPUT_FORMAT_BINARY;
436 } else
437 #ifdef TCC_TARGET_COFF
438 if (!strcmp(p, "coff")) {
439 s->output_format = TCC_OUTPUT_FORMAT_COFF;
440 } else
441 #endif
443 error("target %s not found", p);
445 } else {
446 error("unsupported linker option '%s'", optarg);
449 break;
450 case TCC_OPTION_E:
451 output_type = TCC_OUTPUT_PREPROCESS;
452 break;
453 case TCC_OPTION_x:
454 break;
455 default:
456 if (s->warn_unsupported) {
457 unsupported_option:
458 warning("unsupported option '%s'", r);
460 break;
464 return optind + 1;
467 int main(int argc, char **argv)
469 int i;
470 TCCState *s;
471 int nb_objfiles, ret, optind;
472 char objfilename[1024];
473 int64_t start_time = 0;
475 s = tcc_new();
477 output_type = TCC_OUTPUT_EXE;
478 outfile = NULL;
479 multiple_files = 1;
480 files = NULL;
481 nb_files = 0;
482 nb_libraries = 0;
483 reloc_output = 0;
484 print_search_dirs = 0;
485 ret = 0;
487 optind = parse_args(s, argc - 1, argv + 1);
488 if (print_search_dirs) {
489 /* enough for Linux kernel */
490 printf("install: %s/\n", s->tcc_lib_path);
491 return 0;
493 if (optind == 0 || nb_files == 0) {
494 if (optind && s->verbose)
495 return 0;
496 help();
497 return 1;
500 nb_objfiles = nb_files - nb_libraries;
502 /* if outfile provided without other options, we output an
503 executable */
504 if (outfile && output_type == TCC_OUTPUT_MEMORY)
505 output_type = TCC_OUTPUT_EXE;
507 /* check -c consistency : only single file handled. XXX: checks file type */
508 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
509 /* accepts only a single input file */
510 if (nb_objfiles != 1)
511 error("cannot specify multiple files with -c");
512 if (nb_libraries != 0)
513 error("cannot specify libraries with -c");
517 if (output_type == TCC_OUTPUT_PREPROCESS) {
518 if (!outfile) {
519 s->outfile = stdout;
520 } else {
521 s->outfile = fopen(outfile, "w");
522 if (!s->outfile)
523 error("could not open '%s", outfile);
525 } else if (output_type != TCC_OUTPUT_MEMORY) {
526 if (!outfile) {
527 /* compute default outfile name */
528 char *ext;
529 const char *name =
530 strcmp(files[0], "-") == 0 ? "a" : tcc_basename(files[0]);
531 pstrcpy(objfilename, sizeof(objfilename), name);
532 ext = tcc_fileextension(objfilename);
533 #ifdef TCC_TARGET_PE
534 if (output_type == TCC_OUTPUT_DLL)
535 strcpy(ext, ".dll");
536 else
537 if (output_type == TCC_OUTPUT_EXE)
538 strcpy(ext, ".exe");
539 else
540 #endif
541 if (output_type == TCC_OUTPUT_OBJ && !reloc_output && *ext)
542 strcpy(ext, ".o");
543 else
544 pstrcpy(objfilename, sizeof(objfilename), "a.out");
545 outfile = objfilename;
549 if (do_bench) {
550 start_time = getclock_us();
553 tcc_set_output_type(s, output_type);
555 /* compile or add each files or library */
556 for(i = 0; i < nb_files && ret == 0; i++) {
557 const char *filename;
559 filename = files[i];
560 if (filename[0] == '-' && filename[1]) {
561 if (tcc_add_library(s, filename + 2) < 0) {
562 error_noabort("cannot find %s", filename);
563 ret = 1;
565 } else {
566 if (1 == s->verbose)
567 printf("-> %s\n", filename);
568 if (tcc_add_file(s, filename) < 0)
569 ret = 1;
573 /* free all files */
574 tcc_free(files);
576 if (0 == ret) {
577 if (do_bench)
578 tcc_print_stats(s, getclock_us() - start_time);
580 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
581 if (outfile)
582 fclose(s->outfile);
583 } else if (s->output_type == TCC_OUTPUT_MEMORY)
584 ret = tcc_run(s, argc - optind, argv + optind);
585 else
586 ret = tcc_output_file(s, outfile) ? 1 : 0;
589 tcc_delete(s);
591 #ifdef MEM_DEBUG
592 if (do_bench) {
593 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size);
595 #endif
596 return ret;