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