Fix Extended Asm ignored constraints
[tinycc.git] / tcc.c
blob263beba9313cf5bd0ac953462f32ca80229be7f4
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 "tcc.h"
22 #if ONE_SOURCE
23 # include "libtcc.c"
24 #endif
25 #include "tcctools.c"
27 static const char help[] =
28 "Tiny C Compiler "TCC_VERSION" - Copyright (C) 2001-2006 Fabrice Bellard\n"
29 "Usage: tcc [options...] [-o outfile] [-c] infile(s)...\n"
30 " tcc [options...] -run infile (or --) [arguments...]\n"
31 "General options:\n"
32 " -c compile only - generate an object file\n"
33 " -o outfile set output filename\n"
34 " -run run compiled source\n"
35 " -fflag set or reset (with 'no-' prefix) 'flag' (see tcc -hh)\n"
36 " -std=c99 Conform to the ISO 1999 C standard (default).\n"
37 " -std=c11 Conform to the ISO 2011 C standard.\n"
38 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see tcc -hh)\n"
39 " -w disable all warnings\n"
40 " -v --version show version\n"
41 " -vv show search paths or loaded files\n"
42 " -h -hh show this, show more help\n"
43 " -bench show compilation statistics\n"
44 " - use stdin pipe as infile\n"
45 " @listfile read arguments from listfile\n"
46 "Preprocessor options:\n"
47 " -Idir add include path 'dir'\n"
48 " -Dsym[=val] define 'sym' with value 'val'\n"
49 " -Usym undefine 'sym'\n"
50 " -E preprocess only\n"
51 "Linker options:\n"
52 " -Ldir add library path 'dir'\n"
53 " -llib link with dynamic or static library 'lib'\n"
54 " -r generate (relocatable) object file\n"
55 " -shared generate a shared library/dll\n"
56 " -rdynamic export all global symbols to dynamic linker\n"
57 " -soname set name for shared library to be used at runtime\n"
58 " -Wl,-opt[=val] set linker option (see tcc -hh)\n"
59 "Debugger options:\n"
60 " -g generate stab runtime debug info\n"
61 " -gdwarf[-x] generate dwarf runtime debug info\n"
62 #ifdef TCC_TARGET_PE
63 " -g.pdb create .pdb debug database\n"
64 #endif
65 #ifdef CONFIG_TCC_BCHECK
66 " -b compile with built-in memory and bounds checker (implies -g)\n"
67 #endif
68 #ifdef CONFIG_TCC_BACKTRACE
69 " -bt[N] link with backtrace (stack dump) support [show max N callers]\n"
70 #endif
71 "Misc. options:\n"
72 " -x[c|a|b|n] specify type of the next infile (C,ASM,BIN,NONE)\n"
73 " -nostdinc do not use standard system include paths\n"
74 " -nostdlib do not link with standard crt and libraries\n"
75 " -Bdir set tcc's private include/library dir\n"
76 " -M[M]D generate make dependency file [ignore system files]\n"
77 " -M[M] as above but no other output\n"
78 " -MF file specify dependency file name\n"
79 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
80 " -m32/64 defer to i386/x86_64 cross compiler\n"
81 #endif
82 "Tools:\n"
83 " create library : tcc -ar [crstvx] lib [files]\n"
84 #ifdef TCC_TARGET_PE
85 " create def file : tcc -impdef lib.dll [-v] [-o lib.def]\n"
86 #endif
89 static const char help2[] =
90 "Tiny C Compiler "TCC_VERSION" - More Options\n"
91 "Special options:\n"
92 " -P -P1 with -E: no/alternative #line output\n"
93 " -dD -dM with -E: output #define directives\n"
94 " -pthread same as -D_REENTRANT and -lpthread\n"
95 " -On same as -D__OPTIMIZE__ for n > 0\n"
96 " -Wp,-opt same as -opt\n"
97 " -include file include 'file' above each input file\n"
98 " -isystem dir add 'dir' to system include path\n"
99 " -static link to static libraries (not recommended)\n"
100 " -dumpversion print version\n"
101 " -print-search-dirs print search paths\n"
102 " -dt with -run/-E: auto-define 'test_...' macros\n"
103 "Ignored options:\n"
104 " -arch -C --param -pedantic -pipe -s -traditional\n"
105 "-W[no-]... warnings:\n"
106 " all turn on some (*) warnings\n"
107 " error[=warning] stop after warning (any or specified)\n"
108 " write-strings strings are const\n"
109 " unsupported warn about ignored options, pragmas, etc.\n"
110 " implicit-function-declaration warn for missing prototype (*)\n"
111 " discarded-qualifiers warn when const is dropped (*)\n"
112 "-f[no-]... flags:\n"
113 " unsigned-char default char is unsigned\n"
114 " signed-char default char is signed\n"
115 " common use common section instead of bss\n"
116 " leading-underscore decorate extern symbols\n"
117 " ms-extensions allow anonymous struct in struct\n"
118 " dollars-in-identifiers allow '$' in C symbols\n"
119 " test-coverage create code coverage code\n"
120 "-m... target specific options:\n"
121 " ms-bitfields use MSVC bitfield layout\n"
122 #ifdef TCC_TARGET_ARM
123 " float-abi hard/softfp on arm\n"
124 #endif
125 #ifdef TCC_TARGET_X86_64
126 " no-sse disable floats on x86_64\n"
127 #endif
128 "-Wl,... linker options:\n"
129 " -nostdlib do not link with standard crt/libs\n"
130 " -[no-]whole-archive load lib(s) fully/only as needed\n"
131 " -export-all-symbols same as -rdynamic\n"
132 " -export-dynamic same as -rdynamic\n"
133 " -image-base= -Ttext= set base address of executable\n"
134 " -section-alignment= set section alignment in executable\n"
135 #ifdef TCC_TARGET_PE
136 " -file-alignment= set PE file alignment\n"
137 " -stack= set PE stack reserve\n"
138 " -large-address-aware set related PE option\n"
139 " -subsystem=[console/windows] set PE subsystem\n"
140 " -oformat=[pe-* binary] set executable output format\n"
141 "Predefined macros:\n"
142 " tcc -E -dM - < nul\n"
143 #else
144 " -rpath= set dynamic library search path\n"
145 " -enable-new-dtags set DT_RUNPATH instead of DT_RPATH\n"
146 " -soname= set DT_SONAME elf tag\n"
147 #if defined(TCC_TARGET_MACHO)
148 " -install_name= set DT_SONAME elf tag (soname macOS alias)\n"
149 #endif
150 " -Bsymbolic set DT_SYMBOLIC elf tag\n"
151 " -oformat=[elf32/64-* binary] set executable output format\n"
152 " -init= -fini= -Map= -as-needed -O (ignored)\n"
153 "Predefined macros:\n"
154 " tcc -E -dM - < /dev/null\n"
155 #endif
156 "See also the manual for more details.\n"
159 static const char version[] =
160 "tcc version "TCC_VERSION
161 #ifdef TCC_GITHASH
162 " "TCC_GITHASH
163 #endif
164 " ("
165 #ifdef TCC_TARGET_I386
166 "i386"
167 #elif defined TCC_TARGET_X86_64
168 "x86_64"
169 #elif defined TCC_TARGET_C67
170 "C67"
171 #elif defined TCC_TARGET_ARM
172 "ARM"
173 # ifdef TCC_ARM_EABI
174 " eabi"
175 # ifdef TCC_ARM_HARDFLOAT
176 "hf"
177 # endif
178 # endif
179 #elif defined TCC_TARGET_ARM64
180 "AArch64"
181 #elif defined TCC_TARGET_RISCV64
182 "riscv64"
183 #endif
184 #ifdef TCC_TARGET_PE
185 " Windows"
186 #elif defined(TCC_TARGET_MACHO)
187 " Darwin"
188 #elif TARGETOS_FreeBSD || TARGETOS_FreeBSD_kernel
189 " FreeBSD"
190 #elif TARGETOS_OpenBSD
191 " OpenBSD"
192 #elif TARGETOS_NetBSD
193 " NetBSD"
194 #else
195 " Linux"
196 #endif
197 ")\n"
200 static void print_dirs(const char *msg, char **paths, int nb_paths)
202 int i;
203 printf("%s:\n%s", msg, nb_paths ? "" : " -\n");
204 for(i = 0; i < nb_paths; i++)
205 printf(" %s\n", paths[i]);
208 static void print_search_dirs(TCCState *s)
210 printf("install: %s\n", s->tcc_lib_path);
211 /* print_dirs("programs", NULL, 0); */
212 print_dirs("include", s->sysinclude_paths, s->nb_sysinclude_paths);
213 print_dirs("libraries", s->library_paths, s->nb_library_paths);
214 printf("libtcc1:\n %s/%s\n", s->library_paths[0], CONFIG_TCC_CROSSPREFIX TCC_LIBTCC1);
215 #if !defined TCC_TARGET_PE && !defined TCC_TARGET_MACHO
216 print_dirs("crt", s->crt_paths, s->nb_crt_paths);
217 printf("elfinterp:\n %s\n", DEFAULT_ELFINTERP(s));
218 #endif
221 static void set_environment(TCCState *s)
223 char * path;
225 path = getenv("C_INCLUDE_PATH");
226 if(path != NULL) {
227 tcc_add_sysinclude_path(s, path);
229 path = getenv("CPATH");
230 if(path != NULL) {
231 tcc_add_include_path(s, path);
233 path = getenv("LIBRARY_PATH");
234 if(path != NULL) {
235 tcc_add_library_path(s, path);
239 static char *default_outputfile(TCCState *s, const char *first_file)
241 char buf[1024];
242 char *ext;
243 const char *name = "a";
245 if (first_file && strcmp(first_file, "-"))
246 name = tcc_basename(first_file);
247 snprintf(buf, sizeof(buf), "%s", name);
248 ext = tcc_fileextension(buf);
249 #ifdef TCC_TARGET_PE
250 if (s->output_type == TCC_OUTPUT_DLL)
251 strcpy(ext, ".dll");
252 else
253 if (s->output_type == TCC_OUTPUT_EXE)
254 strcpy(ext, ".exe");
255 else
256 #endif
257 if ((s->just_deps || s->output_type == TCC_OUTPUT_OBJ) && !s->option_r && *ext)
258 strcpy(ext, ".o");
259 else
260 strcpy(buf, "a.out");
261 return tcc_strdup(buf);
264 static unsigned getclock_ms(void)
266 #ifdef _WIN32
267 return GetTickCount();
268 #else
269 struct timeval tv;
270 gettimeofday(&tv, NULL);
271 return tv.tv_sec*1000 + (tv.tv_usec+500)/1000;
272 #endif
275 int main(int argc0, char **argv0)
277 TCCState *s, *s1;
278 int ret, opt, n = 0, t = 0, done;
279 unsigned start_time = 0, end_time = 0;
280 const char *first_file;
281 int argc; char **argv;
282 FILE *ppfp = stdout;
284 redo:
285 argc = argc0, argv = argv0;
286 s = s1 = tcc_new();
287 #ifdef CONFIG_TCC_SWITCHES /* predefined options */
288 tcc_set_options(s, CONFIG_TCC_SWITCHES);
289 #endif
290 opt = tcc_parse_args(s, &argc, &argv, 1);
291 if (opt < 0)
292 return 1;
294 if (n == 0) {
295 if (opt == OPT_HELP) {
296 fputs(help, stdout);
297 if (!s->verbose)
298 return 0;
299 ++opt;
301 if (opt == OPT_HELP2) {
302 fputs(help2, stdout);
303 return 0;
305 if (opt == OPT_M32 || opt == OPT_M64)
306 return tcc_tool_cross(s, argv, opt);
307 if (s->verbose)
308 printf("%s", version);
309 if (opt == OPT_AR)
310 return tcc_tool_ar(s, argc, argv);
311 #ifdef TCC_TARGET_PE
312 if (opt == OPT_IMPDEF)
313 return tcc_tool_impdef(s, argc, argv);
314 #endif
315 if (opt == OPT_V)
316 return 0;
317 if (opt == OPT_PRINT_DIRS) {
318 /* initialize search dirs */
319 set_environment(s);
320 tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
321 print_search_dirs(s);
322 return 0;
325 if (s->nb_files == 0) {
326 tcc_error_noabort("no input files");
327 } else if (s->output_type == TCC_OUTPUT_PREPROCESS) {
328 if (s->outfile && 0!=strcmp("-",s->outfile)) {
329 ppfp = fopen(s->outfile, "wb");
330 if (!ppfp)
331 tcc_error_noabort("could not write '%s'", s->outfile);
333 } else if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) {
334 if (s->nb_libraries)
335 tcc_error_noabort("cannot specify libraries with -c");
336 else if (s->nb_files > 1 && s->outfile)
337 tcc_error_noabort("cannot specify output file with -c many files");
339 if (s->nb_errors)
340 return 1;
341 if (s->do_bench)
342 start_time = getclock_ms();
345 set_environment(s);
346 if (s->output_type == 0)
347 s->output_type = TCC_OUTPUT_EXE;
348 tcc_set_output_type(s, s->output_type);
349 s->ppfp = ppfp;
351 if ((s->output_type == TCC_OUTPUT_MEMORY
352 || s->output_type == TCC_OUTPUT_PREPROCESS)
353 && (s->dflag & 16)) { /* -dt option */
354 if (t)
355 s->dflag |= 32;
356 s->run_test = ++t;
357 if (n)
358 --n;
361 /* compile or add each files or library */
362 first_file = NULL;
363 do {
364 struct filespec *f = s->files[n];
365 s->filetype = f->type;
366 if (f->type & AFF_TYPE_LIB) {
367 ret = tcc_add_library_err(s, f->name);
368 } else {
369 if (1 == s->verbose)
370 printf("-> %s\n", f->name);
371 if (!first_file)
372 first_file = f->name;
373 ret = tcc_add_file(s, f->name);
375 done = ret || ++n >= s->nb_files;
376 } while (!done && (s->output_type != TCC_OUTPUT_OBJ || s->option_r));
378 if (s->do_bench)
379 end_time = getclock_ms();
381 if (s->run_test) {
382 t = 0;
383 } else if (s->output_type == TCC_OUTPUT_PREPROCESS) {
385 } else if (0 == ret) {
386 if (s->output_type == TCC_OUTPUT_MEMORY) {
387 #ifdef TCC_IS_NATIVE
388 ret = tcc_run(s, argc, argv);
389 #endif
390 } else {
391 if (!s->outfile)
392 s->outfile = default_outputfile(s, first_file);
393 if (!s->just_deps && tcc_output_file(s, s->outfile))
395 else if (s->gen_deps)
396 gen_makedeps(s, s->outfile, s->deps_outfile);
400 done = 1;
401 if (t)
402 done = 0; /* run more tests with -dt -run */
403 else if (s->nb_errors)
404 ret = 1;
405 else if (n < s->nb_files)
406 done = 0; /* compile more files with -c */
407 else if (s->do_bench)
408 tcc_print_stats(s, end_time - start_time);
409 tcc_delete(s);
410 if (!done)
411 goto redo;
412 if (ppfp && ppfp != stdout)
413 fclose(ppfp);
414 return ret;