-
Notifications
You must be signed in to change notification settings - Fork 0
/
libtcc.c
2122 lines (1932 loc) · 59.7 KB
/
libtcc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* TCC - Tiny C Compiler
*
* Copyright (c) 2001-2004 Fabrice Bellard
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#if !defined ONE_SOURCE || ONE_SOURCE
#include "tccpp.c"
#include "tccgen.c"
#include "tccasm.c"
#include "tccelf.c"
#include "tccrun.c"
#ifdef TCC_TARGET_I386
#include "i386-gen.c"
#include "i386-link.c"
#include "i386-asm.c"
#elif defined(TCC_TARGET_ARM)
#include "arm-gen.c"
#include "arm-link.c"
#include "arm-asm.c"
#elif defined(TCC_TARGET_ARM64)
#include "arm64-gen.c"
#include "arm64-link.c"
#include "arm-asm.c"
#elif defined(TCC_TARGET_C67)
#include "c67-gen.c"
#include "c67-link.c"
#include "tcccoff.c"
#elif defined(TCC_TARGET_X86_64)
#include "x86_64-gen.c"
#include "x86_64-link.c"
#include "i386-asm.c"
#elif defined(TCC_TARGET_RISCV64)
#include "riscv64-gen.c"
#include "riscv64-link.c"
#include "riscv64-asm.c"
#else
#error unknown target
#endif
#ifdef TCC_TARGET_PE
#include "tccpe.c"
#endif
#ifdef TCC_TARGET_MACHO
#include "tccmacho.c"
#endif
#endif /* ONE_SOURCE */
#include "tcc.h"
/********************************************************/
/* global variables */
/* XXX: get rid of this ASAP (or maybe not) */
TCC_SEM(static tcc_compile_sem);
#ifdef MEM_DEBUG
static int nb_states;
#endif
/********************************************************/
#ifdef _WIN32
ST_FUNC char *normalize_slashes(char *path)
{
char *p;
for (p = path; *p; ++p)
if (*p == '\\')
*p = '/';
return path;
}
#if defined LIBTCC_AS_DLL && !defined CONFIG_TCCDIR
static HMODULE tcc_module;
BOOL WINAPI DllMain (HINSTANCE hDll, DWORD dwReason, LPVOID lpReserved)
{
if (DLL_PROCESS_ATTACH == dwReason)
tcc_module = hDll;
return TRUE;
}
#else
#define tcc_module NULL /* NULL means executable itself */
#endif
#ifndef CONFIG_TCCDIR
/* on win32, we suppose the lib and includes are at the location of 'tcc.exe' */
static inline char *config_tccdir_w32(char *path)
{
char *p;
GetModuleFileName(tcc_module, path, MAX_PATH);
p = tcc_basename(normalize_slashes(strlwr(path)));
if (p > path)
--p;
*p = 0;
return path;
}
#define CONFIG_TCCDIR config_tccdir_w32(alloca(MAX_PATH))
#endif
#ifdef TCC_TARGET_PE
static void tcc_add_systemdir(TCCState *S)
{
char buf[1000];
GetSystemDirectory(buf, sizeof buf);
tcc_add_library_path(S, normalize_slashes(buf));
}
#endif
#endif
/********************************************************/
#if CONFIG_TCC_SEMLOCK
#if defined _WIN32
ST_FUNC void wait_sem(TCCSem *p)
{
if (!p->init)
InitializeCriticalSection(&p->cr), p->init = 1;
EnterCriticalSection(&p->cr);
}
ST_FUNC void post_sem(TCCSem *p)
{
LeaveCriticalSection(&p->cr);
}
#elif defined __APPLE__
/* Half-compatible MacOS doesn't have non-shared (process local)
semaphores. Use the dispatch framework for lightweight locks. */
ST_FUNC void wait_sem(TCCSem *p)
{
if (!p->init)
p->sem = dispatch_semaphore_create(1), p->init = 1;
dispatch_semaphore_wait(p->sem, DISPATCH_TIME_FOREVER);
}
ST_FUNC void post_sem(TCCSem *p)
{
dispatch_semaphore_signal(p->sem);
}
#else
ST_FUNC void wait_sem(TCCSem *p)
{
if (!p->init)
sem_init(&p->sem, 0, 1), p->init = 1;
while (sem_wait(&p->sem) < 0 && errno == EINTR);
}
ST_FUNC void post_sem(TCCSem *p)
{
sem_post(&p->sem);
}
#endif
#endif
PUB_FUNC void tcc_enter_state(TCCState *S)
{
if (S->error_set_jmp_enabled)
return;
WAIT_SEM(&tcc_compile_sem);
/*S = S;*/
}
PUB_FUNC void tcc_exit_state(TCCState *S)
{
if (S->error_set_jmp_enabled)
return;
S = NULL;
POST_SEM(&tcc_compile_sem);
}
/********************************************************/
/* copy a string and truncate it. */
ST_FUNC char *pstrcpy(char *buf, size_t buf_size, const char *s)
{
char *q, *q_end;
int c;
if (buf_size > 0) {
q = buf;
q_end = buf + buf_size - 1;
while (q < q_end) {
c = *s++;
if (c == '\0')
break;
*q++ = c;
}
*q = '\0';
}
return buf;
}
/* strcat and truncate. */
ST_FUNC char *pstrcat(char *buf, size_t buf_size, const char *s)
{
size_t len;
len = strlen(buf);
if (len < buf_size)
pstrcpy(buf + len, buf_size - len, s);
return buf;
}
ST_FUNC char *pstrncpy(char *out, const char *in, size_t num)
{
memcpy(out, in, num);
out[num] = '\0';
return out;
}
/* extract the basename of a file */
PUB_FUNC char *tcc_basename(const char *name)
{
char *p = strchr(name, 0);
while (p > name && !IS_DIRSEP(p[-1]))
--p;
return p;
}
/* extract extension part of a file
*
* (if no extension, return pointer to end-of-string)
*/
PUB_FUNC char *tcc_fileextension (const char *name)
{
char *b = tcc_basename(name);
char *e = strrchr(b, '.');
return e ? e : strchr(b, 0);
}
ST_FUNC char *tcc_load_text(TCCState *S, int fd)
{
int len = lseek(fd, 0, SEEK_END);
char *buf = load_data(S, fd, 0, len + 1);
buf[len] = 0;
return buf;
}
/********************************************************/
/* memory management */
#undef free
#undef malloc
#undef realloc
PUB_FUNC void tcc_free_base(void *ptr)
{
free(ptr);
}
PUB_FUNC void *tcc_malloc_base(unsigned long size)
{
void *ptr;
ptr = malloc(size);
if (!ptr && size) {
printf("memory full (malloc)\n");
exit(1);
}
return ptr;
}
PUB_FUNC void *tcc_mallocz_base(unsigned long size)
{
void *ptr;
ptr = tcc_malloc_base(size);
if (size)
memset(ptr, 0, size);
return ptr;
}
#ifndef MEM_DEBUG
PUB_FUNC void tcc_free(TCCState *S, void *ptr)
{
free(ptr);
}
PUB_FUNC void *tcc_malloc(TCCState *S, unsigned long size)
{
void *ptr;
ptr = malloc(size);
if (!ptr && size)
_tcc_error(S, "memory full (malloc)");
return ptr;
}
PUB_FUNC void *tcc_mallocz(TCCState *S, unsigned long size)
{
void *ptr;
ptr = tcc_malloc(S, size);
if (size)
memset(ptr, 0, size);
return ptr;
}
PUB_FUNC void *tcc_realloc(TCCState *S, void *ptr, unsigned long size)
{
void *ptr1;
ptr1 = realloc(ptr, size);
if (!ptr1 && size)
_tcc_error(S, "memory full (realloc)");
return ptr1;
}
PUB_FUNC char *tcc_strdup(TCCState *S, const char *str)
{
char *ptr;
ptr = tcc_malloc(S, strlen(str) + 1);
strcpy(ptr, str);
return ptr;
}
#else
#define MEM_DEBUG_MAGIC1 0xFEEDDEB1
#define MEM_DEBUG_MAGIC2 0xFEEDDEB2
#define MEM_DEBUG_MAGIC3 0xFEEDDEB3
#define MEM_DEBUG_FILE_LEN 40
#define MEM_DEBUG_CHECK3(header) \
((mem_debug_header_t*)((char*)header + header->size))->magic3
#define MEM_USER_PTR(header) \
((char *)header + offsetof(mem_debug_header_t, magic3))
#define MEM_HEADER_PTR(ptr) \
(mem_debug_header_t *)((char*)ptr - offsetof(mem_debug_header_t, magic3))
struct mem_debug_header {
unsigned magic1;
unsigned size;
struct mem_debug_header *prev;
struct mem_debug_header *next;
int line_num;
char file_name[MEM_DEBUG_FILE_LEN + 1];
unsigned magic2;
ALIGNED(16) unsigned char magic3[4];
};
typedef struct mem_debug_header mem_debug_header_t;
static mem_debug_header_t *mem_debug_chain;
static unsigned mem_cur_size;
static unsigned mem_max_size;
static mem_debug_header_t *malloc_check(void *ptr, const char *msg)
{
mem_debug_header_t * header = MEM_HEADER_PTR(ptr);
if (header->magic1 != MEM_DEBUG_MAGIC1 ||
header->magic2 != MEM_DEBUG_MAGIC2 ||
read32le(MEM_DEBUG_CHECK3(header)) != MEM_DEBUG_MAGIC3 ||
header->size == (unsigned)-1) {
fprintf(stderr, "%s check failed\n", msg);
if (header->magic1 == MEM_DEBUG_MAGIC1)
fprintf(stderr, "%s:%u: block allocated here.\n",
header->file_name, header->line_num);
exit(1);
}
return header;
}
PUB_FUNC void *tcc_malloc_debug(TCCState *S, unsigned long size, const char *file, int line)
{
int ofs;
mem_debug_header_t *header;
header = malloc(sizeof(mem_debug_header_t) + size);
if (!header)
_tcc_error(S, "memory full (malloc)");
header->magic1 = MEM_DEBUG_MAGIC1;
header->magic2 = MEM_DEBUG_MAGIC2;
header->size = size;
write32le(MEM_DEBUG_CHECK3(header), MEM_DEBUG_MAGIC3);
header->line_num = line;
ofs = strlen(file) - MEM_DEBUG_FILE_LEN;
strncpy(header->file_name, file + (ofs > 0 ? ofs : 0), MEM_DEBUG_FILE_LEN);
header->file_name[MEM_DEBUG_FILE_LEN] = 0;
header->next = mem_debug_chain;
header->prev = NULL;
if (header->next)
header->next->prev = header;
mem_debug_chain = header;
mem_cur_size += size;
if (mem_cur_size > mem_max_size)
mem_max_size = mem_cur_size;
return MEM_USER_PTR(header);
}
PUB_FUNC void tcc_free_debug(TCCState *S, void *ptr)
{
mem_debug_header_t *header;
if (!ptr)
return;
header = malloc_check(ptr, "tcc_free");
mem_cur_size -= header->size;
header->size = (unsigned)-1;
if (header->next)
header->next->prev = header->prev;
if (header->prev)
header->prev->next = header->next;
if (header == mem_debug_chain)
mem_debug_chain = header->next;
free(header);
}
PUB_FUNC void *tcc_mallocz_debug(TCCState *S, unsigned long size, const char *file, int line)
{
void *ptr;
ptr = tcc_malloc_debug(S, size,file,line);
memset(ptr, 0, size);
return ptr;
}
PUB_FUNC void *tcc_realloc_debug(TCCState *S, void *ptr, unsigned long size, const char *file, int line)
{
mem_debug_header_t *header;
int mem_debug_chain_update = 0;
if (!ptr)
return tcc_malloc_debug(S, size, file, line);
header = malloc_check(ptr, "tcc_realloc");
mem_cur_size -= header->size;
mem_debug_chain_update = (header == mem_debug_chain);
header = realloc(header, sizeof(mem_debug_header_t) + size);
if (!header)
_tcc_error(S, "memory full (realloc)");
header->size = size;
write32le(MEM_DEBUG_CHECK3(header), MEM_DEBUG_MAGIC3);
if (header->next)
header->next->prev = header;
if (header->prev)
header->prev->next = header;
if (mem_debug_chain_update)
mem_debug_chain = header;
mem_cur_size += size;
if (mem_cur_size > mem_max_size)
mem_max_size = mem_cur_size;
return MEM_USER_PTR(header);
}
PUB_FUNC char *tcc_strdup_debug(TCCState *S, const char *str, const char *file, int line)
{
char *ptr;
ptr = tcc_malloc_debug(S, strlen(str) + 1, file, line);
strcpy(ptr, str);
return ptr;
}
PUB_FUNC void tcc_memcheck(void)
{
if (mem_cur_size) {
mem_debug_header_t *header = mem_debug_chain;
fprintf(stderr, "MEM_DEBUG: mem_leak= %d bytes, mem_max_size= %d bytes\n",
mem_cur_size, mem_max_size);
while (header) {
fprintf(stderr, "%s:%u: error: %u bytes leaked\n",
header->file_name, header->line_num, header->size);
header = header->next;
}
#if MEM_DEBUG-0 == 2
exit(2);
#endif
}
}
#endif /* MEM_DEBUG */
#define free(p) use_tcc_free(p)
#define malloc(s) use_tcc_malloc(s)
#define realloc(p, s) use_tcc_realloc(p, s)
/********************************************************/
/* virtual io */
LIBTCCAPI void tcc_set_vio_module(TCCState *S, vio_module_t *vio_module){
S->vio_module = vio_module;
vio_module->S = S;
}
void vio_initialize(vio_fd *fd) {
fd->fd = -1;
fd->vio_udata = NULL;
fd->vio_module = NULL;
}
int vio_open(struct TCCState *S, vio_fd *fd, const char *fn, int oflag) {
int rc;
vio_initialize(fd);
fd->vio_module = S->vio_module;
if(S->vio_module && (S->vio_module->call_vio_open_flags & CALL_VIO_OPEN_FIRST)) {
rc = S->vio_module->vio_open(fd, fn, oflag);
if(rc >= 0) return rc;
}
fd->fd = open(fn, oflag);
if(fd->fd < 0 && S->vio_module && (S->vio_module->call_vio_open_flags & CALL_VIO_OPEN_LAST)) {
rc = S->vio_module->vio_open(fd, fn, oflag);
if(rc >= 0) return rc;
}
//printf("vio_open = %d %s\n", fd->fd, fn);
return fd->fd;
}
off_t vio_lseek(vio_fd fd, off_t offset, int whence) {
if(fd.vio_udata) {
return fd.vio_module->vio_lseek(fd, offset, whence);
}
return lseek(fd.fd, offset, whence);
}
size_t vio_read(vio_fd fd, void *buf, size_t bytes) {
if(fd.vio_udata) {
return fd.vio_module->vio_read(fd, buf, bytes);
}
return read(fd.fd, buf, bytes);
}
int vio_close(vio_fd *fd) {
int rc = 0;
if(fd->vio_udata){
fd->vio_module->vio_close(fd);
} else rc = close(fd->fd);
vio_initialize(fd);
return rc;
}
/********************************************************/
/* dynarrays */
ST_FUNC void dynarray_add(TCCState *S, void *ptab, int *nb_ptr, void *data)
{
int nb, nb_alloc;
void **pp;
nb = *nb_ptr;
pp = *(void ***)ptab;
/* every power of two we double array size */
if ((nb & (nb - 1)) == 0) {
if (!nb)
nb_alloc = 1;
else
nb_alloc = nb * 2;
pp = tcc_realloc(S, pp, nb_alloc * sizeof(void *));
*(void***)ptab = pp;
}
pp[nb++] = data;
*nb_ptr = nb;
}
ST_FUNC void dynarray_reset(TCCState *S, void *pp, int *n)
{
void **p;
for (p = *(void***)pp; *n; ++p, --*n)
if (*p)
tcc_free(S, *p);
tcc_free(S, *(void**)pp);
*(void**)pp = NULL;
}
static void tcc_split_path(TCCState *S, void *p_ary, int *p_nb_ary, const char *in)
{
const char *p;
do {
int c;
CString str;
cstr_new(S, &str);
for (p = in; c = *p, c != '\0' && c != PATHSEP[0]; ++p) {
if (c == '{' && p[1] && p[2] == '}') {
c = p[1], p += 2;
if (c == 'B')
cstr_cat(S, &str, S->tcc_lib_path, -1);
if (c == 'f' && S->tccpp_file) {
/* substitute current file's dir */
const char *f = S->tccpp_file->true_filename;
const char *b = tcc_basename(f);
if (b > f)
cstr_cat(S, &str, f, b - f - 1);
else
cstr_cat(S, &str, ".", 1);
}
} else {
cstr_ccat(S, &str, c);
}
}
if (str.size) {
cstr_ccat(S, &str, '\0');
dynarray_add(S, p_ary, p_nb_ary, tcc_strdup(S, str.data));
}
cstr_free(S, &str);
in = p+1;
} while (*p);
}
/********************************************************/
/* warning / error */
/* warn_... option bits */
#define WARN_ON 1 /* warning is on (-Woption) */
#define WARN_ERR 2 /* warning is an error (-Werror=option) */
#define WARN_NOE 4 /* warning is not an error (-Wno-error=option) */
/* error1() modes */
enum { ERROR_WARN, ERROR_NOABORT, ERROR_ERROR };
static void error1(TCCState *S, int mode, const char *fmt, va_list ap)
{
BufferedFile **pf, *f;
CString cs;
cstr_new(S, &cs);
if (S == NULL)
/* can happen only if called from tcc_malloc(): 'out of memory' */
goto no_file;
tcc_exit_state(S);
if (mode == ERROR_WARN) {
if (S->warn_error)
mode = ERROR_ERROR;
if (S->warn_num) {
/* handle tcc_warning_c(warn_option)(fmt, ...) */
int wopt = *(&S->warn_none + S->warn_num);
S->warn_num = 0;
if (0 == (wopt & WARN_ON))
return;
if (wopt & WARN_ERR)
mode = ERROR_ERROR;
if (wopt & WARN_NOE)
mode = ERROR_WARN;
}
if (S->warn_none)
return;
}
f = NULL;
if (S->error_set_jmp_enabled) { /* we're called while parsing a file */
/* use upper file if inline ":asm:" or token ":paste:" */
for (f = S->tccpp_file; f && f->filename[0] == ':'; f = f->prev)
;
}
if (f) {
for(pf = S->include_stack; pf < S->include_stack_ptr; pf++)
cstr_printf(S, &cs, "In file included from %s:%d:\n",
(*pf)->filename, (*pf)->line_num);
cstr_printf(S, &cs, "%s:%d: ",
f->filename, (*pf)->line_num - !!(S->tok_flags & TOK_FLAG_BOL));
} else if (S->current_filename) {
cstr_printf(S, &cs, "%s: ", S->current_filename);
}
no_file:
if (0 == cs.size)
cstr_printf(S, &cs, "tcc: ");
cstr_printf(S, &cs, mode == ERROR_WARN ? "warning: " : "error: ");
cstr_vprintf(S, &cs, fmt, ap);
if (!S || !S->error_func) {
/* default case: stderr */
if (S && S->output_type == TCC_OUTPUT_PREPROCESS && S->ppfp == stdout)
printf("\n"); /* print a newline during tcc -E */
fflush(stdout); /* flush -v output */
fprintf(stderr, "%s\n", (char*)cs.data);
fflush(stderr); /* print error/warning now (win32) */
} else {
S->error_func(S->error_opaque, (char*)cs.data);
}
cstr_free(S, &cs);
if (S) {
if (mode != ERROR_WARN)
S->nb_errors++;
if (mode != ERROR_ERROR)
return;
if (S->error_set_jmp_enabled)
longjmp(S->error_jmp_buf, 1);
}
exit(1);
}
LIBTCCAPI void tcc_set_error_func(TCCState *S, void *error_opaque, TCCErrorFunc error_func)
{
S->error_opaque = error_opaque;
S->error_func = error_func;
}
LIBTCCAPI TCCErrorFunc tcc_get_error_func(TCCState *S)
{
return S->error_func;
}
LIBTCCAPI void *tcc_get_error_opaque(TCCState *S)
{
return S->error_opaque;
}
/* error without aborting current compilation */
PUB_FUNC void _tcc_error_noabort(TCCState *S, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
error1(S, ERROR_NOABORT, fmt, ap);
va_end(ap);
}
PUB_FUNC void _tcc_error(TCCState *S, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
for (;;) error1(S, ERROR_ERROR, fmt, ap);
}
PUB_FUNC void _tcc_warning(TCCState *S, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
error1(S, ERROR_WARN, fmt, ap);
va_end(ap);
}
/********************************************************/
/* I/O layer */
ST_FUNC void tcc_open_bf(TCCState *S, const char *filename, int initlen)
{
BufferedFile *bf;
int buflen = initlen ? initlen : IO_BUF_SIZE;
bf = tcc_mallocz(S, sizeof(BufferedFile) + buflen);
bf->buf_ptr = bf->buffer;
bf->buf_end = bf->buffer + initlen;
bf->buf_end[0] = CH_EOB; /* put eob symbol */
pstrcpy(bf->filename, sizeof(bf->filename), filename);
#ifdef _WIN32
normalize_slashes(bf->filename);
#endif
bf->true_filename = bf->filename;
bf->line_num = 1;
bf->ifdef_stack_ptr = S->ifdef_stack_ptr;
bf->fd = -1;
bf->prev = S->tccpp_file;
S->tccpp_file = bf;
S->tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
}
ST_FUNC void tcc_close(TCCState *S)
{
BufferedFile *bf = S->tccpp_file;
if (bf->fd > 0) {
close(bf->fd);
total_lines += bf->line_num;
}
if (bf->true_filename != bf->filename)
tcc_free(S, bf->true_filename);
S->tccpp_file = bf->prev;
tcc_free(S, bf);
}
static int _tcc_open(TCCState *S, const char *filename)
{
int fd;
if (strcmp(filename, "-") == 0)
fd = 0, filename = "<stdin>";
else
fd = open(filename, O_RDONLY | O_BINARY);
if ((S->verbose == 2 && fd >= 0) || S->verbose == 3)
printf("%s %*s%s\n", fd < 0 ? "nf":"->",
(int)(S->include_stack_ptr - S->include_stack), "", filename);
return fd;
}
ST_FUNC int tcc_open(TCCState *S, const char *filename)
{
int fd = _tcc_open(S, filename);
if (fd < 0)
return -1;
tcc_open_bf(S, filename, 0);
S->tccpp_file->fd = fd;
return 0;
}
/* compile the file opened in 'file'. Return non zero if errors. */
static int tcc_compile(TCCState *S, int filetype, const char *str, int fd)
{
/* Here we enter the code section where we use the global variables for
parsing and code generation (tccpp.c, tccgen.c, <target>-gen.c).
Other threads need to wait until we're done.
Alternatively we could use thread local storage for those global
variables, which may or may not have advantages */
tcc_enter_state(S);
S->error_set_jmp_enabled = 1;
if (setjmp(S->error_jmp_buf) == 0) {
S->nb_errors = 0;
if (fd == -1) {
int len = strlen(str);
tcc_open_bf(S, "<string>", len);
memcpy(S->tccpp_file->buffer, str, len);
} else {
tcc_open_bf(S, str, 0);
S->tccpp_file->fd = fd;
}
tccelf_begin_file(S);
preprocess_start(S, filetype);
tccgen_init(S);
if (S->output_type == TCC_OUTPUT_PREPROCESS) {
tcc_preprocess(S);
} else if (filetype & (AFF_TYPE_ASM | AFF_TYPE_ASMPP)) {
tcc_assemble(S, !!(filetype & AFF_TYPE_ASMPP));
} else {
tccgen_compile(S);
}
}
tccgen_finish(S);
preprocess_end(S);
S->error_set_jmp_enabled = 0;
tcc_exit_state(S);
tccelf_end_file(S);
return S->nb_errors != 0 ? -1 : 0;
}
LIBTCCAPI int tcc_compile_string(TCCState *S, const char *str)
{
return tcc_compile(S, S->filetype, str, -1);
}
/* define a preprocessor symbol. value can be NULL, sym can be "sym=val" */
LIBTCCAPI void tcc_define_symbol(TCCState *S, const char *sym, const char *value)
{
const char *eq;
if (NULL == (eq = strchr(sym, '=')))
eq = strchr(sym, 0);
if (NULL == value)
value = *eq ? eq + 1 : "1";
cstr_printf(S, &S->cmdline_defs, "#define %.*s %s\n", (int)(eq-sym), sym, value);
}
/* undefine a preprocessor symbol */
LIBTCCAPI void tcc_undefine_symbol(TCCState *S, const char *sym)
{
cstr_printf(S, &S->cmdline_defs, "#undef %s\n", sym);
}
LIBTCCAPI TCCState *tcc_new(void)
{
TCCState *s;
s = tcc_mallocz_base(sizeof(TCCState));
if (!s)
return NULL;
#ifdef MEM_DEBUG
++nb_states;
#endif
#undef gnu_ext
s->gnu_ext = 1;
s->tcc_ext = 1;
s->nocommon = 1;
s->dollars_in_identifiers = 1; /*on by default like in gcc/clang*/
s->cversion = 199901; /* default unless -std=c11 is supplied */
s->warn_implicit_function_declaration = 1;
s->warn_discarded_qualifiers = 1;
s->ms_extensions = 1;
#ifdef CHAR_IS_UNSIGNED
s->char_is_unsigned = 1;
#endif
#ifdef TCC_TARGET_I386
s->seg_size = 32;
#endif
/* enable this if you want symbols with leading underscore on windows: */
#if defined TCC_TARGET_MACHO /* || defined TCC_TARGET_PE */
s->leading_underscore = 1;
#endif
#ifdef TCC_TARGET_ARM
s->float_abi = ARM_FLOAT_ABI;
#endif
s->ppfp = stdout;
/* might be used in error() before preprocess_start() */
s->include_stack_ptr = s->include_stack;
tccelf_new(s);
tcc_set_lib_path(s, CONFIG_TCCDIR);
return s;
}
LIBTCCAPI void tcc_delete(TCCState *S)
{
/* free sections */
tccelf_delete(S);
/* free library paths */
dynarray_reset(S, &S->library_paths, &S->nb_library_paths);
dynarray_reset(S, &S->crt_paths, &S->nb_crt_paths);
/* free include paths */
dynarray_reset(S, &S->include_paths, &S->nb_include_paths);
dynarray_reset(S, &S->sysinclude_paths, &S->nb_sysinclude_paths);
tcc_free(S, S->tcc_lib_path);
tcc_free(S, S->soname);
tcc_free(S, S->rpath);
tcc_free(S, S->init_symbol);
tcc_free(S, S->fini_symbol);
tcc_free(S, S->outfile);
tcc_free(S, S->deps_outfile);
dynarray_reset(S, &S->files, &S->nb_files);
dynarray_reset(S, &S->target_deps, &S->nb_target_deps);
dynarray_reset(S, &S->pragma_libs, &S->nb_pragma_libs);
dynarray_reset(S, &S->argv, &S->argc);
cstr_free(S, &S->cmdline_defs);
cstr_free(S, &S->cmdline_incl);
#ifdef TCC_IS_NATIVE
/* free runtime memory */
tcc_run_free(S);
#endif
tcc_free_base(S);
#ifdef MEM_DEBUG
if (0 == --nb_states)
tcc_memcheck();
#endif
}
LIBTCCAPI int tcc_set_output_type(TCCState *S, int output_type)
{
S->output_type = output_type;
if (!S->nostdinc) {
/* default include paths */
/* -isystem paths have already been handled */
tcc_add_sysinclude_path(S, CONFIG_TCC_SYSINCLUDEPATHS);
}
#ifdef CONFIG_TCC_BCHECK
if (S->do_bounds_check) {
/* if bound checking, then add corresponding sections */
tccelf_bounds_new(S);
}
#endif
if (S->do_debug) {
/* add debug sections */
tccelf_stab_new(S);
}
if (output_type == TCC_OUTPUT_OBJ) {
/* always elf for objects */
S->output_format = TCC_OUTPUT_FORMAT_ELF;
return 0;
}
tcc_add_library_path(S, CONFIG_TCC_LIBPATHS);
#ifdef TCC_TARGET_PE
# ifdef _WIN32
/* allow linking with system dll's directly */
tcc_add_systemdir(S);
# endif
/* target PE has its own startup code in libtcc1.a */
return 0;
#elif defined TCC_TARGET_MACHO
# ifdef TCC_IS_NATIVE
tcc_add_macos_sdkpath(S);
# endif
/* Mach-O with LC_MAIN doesn't need any crt startup code. */
return 0;
#else
/* paths for crt objects */
tcc_split_path(S, &S->crt_paths, &S->nb_crt_paths, CONFIG_TCC_CRTPREFIX);
/* add libc crt1/crti objects */
if ((output_type == TCC_OUTPUT_EXE || output_type == TCC_OUTPUT_DLL) &&
!S->nostdlib) {
#if TARGETOS_OpenBSD
if (output_type != TCC_OUTPUT_DLL)
tcc_add_crt(S, "crt0.o");
if (output_type == TCC_OUTPUT_DLL)
tcc_add_crt(S, "crtbeginS.o");
else
tcc_add_crt(S, "crtbegin.o");
#elif TARGETOS_FreeBSD
if (output_type != TCC_OUTPUT_DLL)
tcc_add_crt(S, "crt1.o");
tcc_add_crt(S, "crti.o");
if (S->static_link)
tcc_add_crt(S, "crtbeginT.o");
else if (output_type == TCC_OUTPUT_DLL)