x86_64/elf: only variadic calls need rax
[tinycc.git] / tcc.c
blob5e0a64a1bfa33488746b91376dc677d711bf67ca
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 ONE_SOURCE
22 #include "libtcc.c"
23 #else
24 #include "tcc.h"
25 #endif
27 static void print_paths(const char *msg, char **paths, int nb_paths)
29 int i;
30 printf("%s:\n%s", msg, nb_paths ? "" : " -\n");
31 for(i = 0; i < nb_paths; i++)
32 printf(" %s\n", paths[i]);
35 static void display_info(TCCState *s, int what)
37 switch (what) {
38 case 0:
39 printf("tcc version %s ("
40 #ifdef TCC_TARGET_I386
41 "i386"
42 #elif defined TCC_TARGET_X86_64
43 "x86-64"
44 #elif defined TCC_TARGET_C67
45 "C67"
46 #elif defined TCC_TARGET_ARM
47 "ARM"
48 # ifdef TCC_ARM_HARDFLOAT
49 " Hard Float"
50 # endif
51 #elif defined TCC_TARGET_ARM64
52 "AArch64"
53 # ifdef TCC_ARM_HARDFLOAT
54 " Hard Float"
55 # endif
56 #endif
57 #ifdef TCC_TARGET_PE
58 " Windows"
59 #else
60 " Linux"
61 #endif
62 ")\n", TCC_VERSION);
63 break;
64 case 1:
65 printf("install: %s\n", s->tcc_lib_path);
66 /* print_paths("programs", NULL, 0); */
67 print_paths("include", s->sysinclude_paths, s->nb_sysinclude_paths);
68 print_paths("libraries", s->library_paths, s->nb_library_paths);
69 #ifndef TCC_TARGET_PE
70 print_paths("crt", s->crt_paths, s->nb_crt_paths);
71 printf("elfinterp:\n %s\n", DEFAULT_ELFINTERP(s));
72 #endif
73 break;
77 static void help(void)
79 printf("Tiny C Compiler "TCC_VERSION" - Copyright (C) 2001-2006 Fabrice Bellard\n"
80 "Usage: tcc [options...] [-o outfile] [-c] infile(s)...\n"
81 " tcc [options...] -run infile [arguments...]\n"
82 "General options:\n"
83 " -c compile only - generate an object file\n"
84 " -o outfile set output filename\n"
85 " -run run compiled source\n"
86 " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
87 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
88 " -w disable all warnings\n"
89 " -v show version\n"
90 " -vv show included files (as sole argument: show search paths)\n"
91 " -dumpversion\n"
92 " -bench show compilation statistics\n"
93 " -xc -xa specify type of the next infile\n"
94 " - use stdin pipe as infile\n"
95 " @listfile read line separated arguments from 'listfile'\n"
96 "Preprocessor options:\n"
97 " -Idir add include path 'dir'\n"
98 " -Dsym[=val] define 'sym' with value 'val'\n"
99 " -Usym undefine 'sym'\n"
100 " -E preprocess only\n"
101 " -P[1] no / alternative #line output with -E\n"
102 " -dD -dM output #define directives with -E\n"
103 "Linker options:\n"
104 " -Ldir add library path 'dir'\n"
105 " -llib link with dynamic or static library 'lib'\n"
106 " -pthread link with -lpthread and -D_REENTRANT (POSIX Linux)\n"
107 " -r generate (relocatable) object file\n"
108 " -rdynamic export all global symbols to dynamic linker\n"
109 " -shared generate a shared library\n"
110 " -soname set name for shared library to be used at runtime\n"
111 " -static static linking\n"
112 " -Wl,-opt[=val] set linker option (see manual)\n"
113 "Debugger options:\n"
114 " -g generate runtime debug info\n"
115 #ifdef CONFIG_TCC_BCHECK
116 " -b compile with built-in memory and bounds checker (implies -g)\n"
117 #endif
118 #ifdef CONFIG_TCC_BACKTRACE
119 " -bt N show N callers in stack traces\n"
120 #endif
121 "Misc options:\n"
122 " -nostdinc do not use standard system include paths\n"
123 " -nostdlib do not link with standard crt and libraries\n"
124 " -Bdir use 'dir' as tcc internal library and include path\n"
125 " -MD generate target dependencies for make\n"
126 " -MF depfile put generated dependencies here\n"
130 /* re-execute the i386/x86_64 cross-compilers with tcc -m32/-m64: */
131 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
132 #ifdef _WIN32
133 #include <process.h>
134 static int execvp_win32(const char *prog, char **argv)
136 int ret = _spawnvp(P_NOWAIT, prog, (const char *const*)argv);
137 if (-1 == ret)
138 return ret;
139 cwait(&ret, ret, WAIT_CHILD);
140 exit(ret);
142 #define execvp execvp_win32
143 #endif
144 static void exec_other_tcc(TCCState *s, char **argv, const char *optarg)
146 char child_path[4096], *child_name; const char *target;
147 switch (atoi(optarg)) {
148 #ifdef TCC_TARGET_I386
149 case 32: break;
150 case 64: target = "x86_64";
151 #else
152 case 64: break;
153 case 32: target = "i386";
154 #endif
155 pstrcpy(child_path, sizeof child_path - 40, argv[0]);
156 child_name = tcc_basename(child_path);
157 strcpy(child_name, target);
158 #ifdef TCC_TARGET_PE
159 strcat(child_name, "-win32");
160 #endif
161 strcat(child_name, "-tcc");
162 if (strcmp(argv[0], child_path)) {
163 if (s->verbose > 0)
164 printf("tcc: using '%s'\n", child_name), fflush(stdout);
165 execvp(argv[0] = child_path, argv);
167 tcc_error("'%s' not found", child_name);
168 case 0: /* ignore -march etc. */
169 break;
170 default:
171 tcc_warning("unsupported option \"-m%s\"", optarg);
174 #else
175 #define exec_other_tcc(s, argv, optarg)
176 #endif
178 static void gen_makedeps(TCCState *s, const char *target, const char *filename)
180 FILE *depout;
181 char buf[1024], *ext;
182 int i;
184 if (!filename) {
185 /* compute filename automatically
186 * dir/file.o -> dir/file.d */
187 pstrcpy(buf, sizeof(buf), target);
188 ext = tcc_fileextension(buf);
189 pstrcpy(ext, sizeof(buf) - (ext-buf), ".d");
190 filename = buf;
193 if (s->verbose)
194 printf("<- %s\n", filename);
196 /* XXX return err codes instead of error() ? */
197 depout = fopen(filename, "w");
198 if (!depout)
199 tcc_error("could not open '%s'", filename);
201 fprintf(depout, "%s : \\\n", target);
202 for (i=0; i<s->nb_target_deps; ++i)
203 fprintf(depout, " %s \\\n", s->target_deps[i]);
204 fprintf(depout, "\n");
205 fclose(depout);
208 static char *default_outputfile(TCCState *s, const char *first_file)
210 char buf[1024];
211 char *ext;
212 const char *name = "a";
214 if (first_file && strcmp(first_file, "-"))
215 name = tcc_basename(first_file);
216 pstrcpy(buf, sizeof(buf), name);
217 ext = tcc_fileextension(buf);
218 #ifdef TCC_TARGET_PE
219 if (s->output_type == TCC_OUTPUT_DLL)
220 strcpy(ext, ".dll");
221 else
222 if (s->output_type == TCC_OUTPUT_EXE)
223 strcpy(ext, ".exe");
224 else
225 #endif
226 if (( (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) ||
227 (s->output_type == TCC_OUTPUT_PREPROCESS) )
228 && *ext)
229 strcpy(ext, ".o");
230 else
231 strcpy(buf, "a.out");
233 return tcc_strdup(buf);
236 static int64_t getclock_us(void)
238 #ifdef _WIN32
239 LARGE_INTEGER frequency, t1;
240 QueryPerformanceFrequency(&frequency);
241 QueryPerformanceCounter(&t1);
242 return t1.QuadPart * 1000000LL / frequency.QuadPart;
243 #else
244 struct timeval tv;
245 gettimeofday(&tv, NULL);
246 return tv.tv_sec * 1000000LL + tv.tv_usec;
247 #endif
250 static void set_whole_archive(TCCState* s, int on)
252 s->whole_archive = on;
253 if (1 == s->verbose)
254 printf("%cwhole-archive>\n", s->whole_archive? '+' : '-');
257 int main(int argc, char **argv)
259 TCCState *s;
260 int ret, optind, i;
261 int64_t start_time = 0;
263 s = tcc_new();
265 optind = tcc_parse_args(s, argc - 1, argv + 1);
267 if (s->do_bench)
268 start_time = getclock_us();
270 tcc_set_environment(s);
272 if (optind == 0) {
273 help();
274 return 1;
277 if (s->option_m)
278 exec_other_tcc(s, argv, s->option_m);
280 if (s->verbose)
281 display_info(s, 0);
283 if (s->print_search_dirs || (s->verbose == 2 && optind == 1)) {
284 tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
285 display_info(s, 1);
286 return 0;
289 if (s->verbose && optind == 1)
290 return 0;
292 if (s->nb_files == 0)
293 tcc_error("no input files\n");
295 /* check -c consistency : only single file handled. XXX: checks file type */
296 if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) {
297 if (s->nb_libraries != 0)
298 tcc_error("cannot specify libraries with -c");
299 /* accepts only a single input file */
300 if ((s->nb_files != 1) && s->outfile) {
301 tcc_error("cannot specify multiple files with -c and -o");
305 tcc_set_output_type(s, s->output_type);
307 /* compile or add each files or library */
308 for(i = ret = 0; i < s->nb_files && ret == 0; i++) {
309 int filetype = *(unsigned char *)s->files[i];
310 const char *filename = s->files[i] + 1;
311 if (filetype == TCC_FILETYPE_AR_WHOLE_ON ||
312 filetype == TCC_FILETYPE_AR_WHOLE_OFF)
314 set_whole_archive(s, filetype == TCC_FILETYPE_AR_WHOLE_ON);
315 continue;
317 if (1 == s->verbose)
318 printf("-> %s\n", filename);
319 if (filename[0] == '-' && filename[1] == 'l') {
320 if (tcc_add_library(s, filename + 2) < 0) {
321 /* don't fail on -lm as it's harmless to skip math lib */
322 if (strcmp(filename + 2, "m")) {
323 tcc_error_noabort("cannot find library 'lib%s'", filename + 2);
324 ret = 1;
327 } else {
328 if (!s->outfile)
329 s->outfile = default_outputfile(s, filename);
330 if (tcc_add_file(s, filename, filetype) < 0)
331 ret = 1;
332 else
333 if (s->output_type == TCC_OUTPUT_OBJ) {
334 ret = !!tcc_output_file(s, s->outfile);
335 if (s->gen_deps && !ret)
336 gen_makedeps(s, s->outfile, s->deps_outfile);
337 if (!ret) {
338 if ((i+1) < s->nb_files) {
339 tcc_delete(s);
340 s = tcc_new();
341 tcc_parse_args(s, argc - 1, argv + 1);
342 tcc_set_environment(s);
343 if (s->output_type != TCC_OUTPUT_OBJ)
344 tcc_error("internal error");
345 tcc_set_output_type(s, s->output_type);
351 set_whole_archive(s, 0);
353 if (0 == ret) {
354 if (s->output_type == TCC_OUTPUT_MEMORY) {
355 #ifdef TCC_IS_NATIVE
356 ret = tcc_run(s, argc - 1 - optind, argv + 1 + optind);
357 #else
358 tcc_error_noabort("-run is not available in a cross compiler");
359 ret = 1;
360 #endif
361 } else
362 if (s->output_type == TCC_OUTPUT_EXE ||
363 s->output_type == TCC_OUTPUT_DLL)
365 ret = !!tcc_output_file(s, s->outfile);
366 if (s->gen_deps && !ret)
367 gen_makedeps(s, s->outfile, s->deps_outfile);
371 if (s->do_bench)
372 tcc_print_stats(s, getclock_us() - start_time);
374 tcc_delete(s);
375 return ret;