History

libtcc: new function tcc_open_bf to create BufferedFile

Use it in: - tcc_open - tcc_compile_string - tcc_define_symbol - tcc_assemble_inline

grischka committed 15 years ago

Commit e97bf881 parent f1c9f644 files · +77 −104Browse files

Unified · Split

4 files
  1. libtcc.c +53 −68
  2. tcc.h +5 −2
  3. tccasm.c +6 −16
  4. tccpp.c +13 −18
libtcc.c+53−68
@@ -635,25 +635,15 @@ PUB_FUNC void warning(const char *fmt, ...) · 634 unmodified lines
635/********************************************************/635/********************************************************/
636/* I/O layer */636/* I/O layer */
637 637
638ST_FUNC BufferedFile *tcc_open(TCCState *s1, const char *filename)638ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen)
639{639{
640 int fd;
641 BufferedFile *bf;640 BufferedFile *bf;
641 int buflen = initlen ? initlen : IO_BUF_SIZE;
642 642
643 if (strcmp(filename, "-") == 0)643 bf = tcc_malloc(sizeof(BufferedFile) + buflen);
644 fd = 0, filename = "stdin";
645 else
646 fd = open(filename, O_RDONLY | O_BINARY);
647 if ((s1->verbose == 2 && fd >= 0) || s1->verbose == 3)
648 printf("%s %*s%s\n", fd < 0 ? "nf":"->",
649 (int)(s1->include_stack_ptr - s1->include_stack), "", filename);
650 if (fd < 0)
651 return NULL;
652 bf = tcc_malloc(sizeof(BufferedFile));
653 bf->fd = fd;
654 bf->buf_ptr = bf->buffer;644 bf->buf_ptr = bf->buffer;
655 bf->buf_end = bf->buffer;645 bf->buf_end = bf->buffer + initlen;
656 bf->buffer[0] = CH_EOB; /* put eob symbol */646 bf->buf_end[0] = CH_EOB; /* put eob symbol */
657 pstrcpy(bf->filename, sizeof(bf->filename), filename);647 pstrcpy(bf->filename, sizeof(bf->filename), filename);
658#ifdef _WIN32648#ifdef _WIN32
659 normalize_slashes(bf->filename);649 normalize_slashes(bf->filename);
@@ -661,17 +651,40 @@ ST_FUNC BufferedFile *tcc_open(TCCState *s1, const char *filename) · 1 unmodified line
661 bf->line_num = 1;651 bf->line_num = 1;
662 bf->ifndef_macro = 0;652 bf->ifndef_macro = 0;
663 bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;653 bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
664 // printf("opening '%s'\n", filename);654 bf->fd = -1;
665 return bf;655 bf->prev = file;
656 file = bf;
666}657}
667 658
668ST_FUNC void tcc_close(BufferedFile *bf)659ST_FUNC void tcc_close(void)
669{660{
670 total_lines += bf->line_num;661 BufferedFile *bf = file;
671 close(bf->fd);662 if (bf->fd > 0) {
663 close(bf->fd);
664 total_lines += bf->line_num;
665 }
666 file = bf->prev;
672 tcc_free(bf);667 tcc_free(bf);
673}668}
674 669
670ST_FUNC int tcc_open(TCCState *s1, const char *filename)
671{
672 int fd;
673 if (strcmp(filename, "-") == 0)
674 fd = 0, filename = "stdin";
675 else
676 fd = open(filename, O_RDONLY | O_BINARY);
677 if ((s1->verbose == 2 && fd >= 0) || s1->verbose == 3)
678 printf("%s %*s%s\n", fd < 0 ? "nf":"->",
679 (int)(s1->include_stack_ptr - s1->include_stack), "", filename);
680 if (fd < 0)
681 return -1;
682
683 tcc_open_bf(s1, filename, 0);
684 file->fd = fd;
685 return fd;
686}
687
675/* compile the C file opened in 'file'. Return non zero if errors. */688/* compile the C file opened in 'file'. Return non zero if errors. */
676static int tcc_compile(TCCState *s1)689static int tcc_compile(TCCState *s1)
677{690{
@@ -780,60 +793,38 @@ static int tcc_compile(TCCState *s1) · 102 unmodified lines
780 793
781LIBTCCAPI int tcc_compile_string(TCCState *s, const char *str)794LIBTCCAPI int tcc_compile_string(TCCState *s, const char *str)
782{795{
783 BufferedFile bf1, *bf = &bf1;796 int len, ret;
784 int ret, len;
785 char *buf;
786
787 /* init file structure */
788 bf->fd = -1;
789 /* XXX: avoid copying */
790 len = strlen(str);797 len = strlen(str);
791 buf = tcc_malloc(len + 1);
792 if (!buf)
793 return -1;
794 memcpy(buf, str, len);
795 buf[len] = CH_EOB;
796 bf->buf_ptr = buf;
797 bf->buf_end = buf + len;
798 pstrcpy(bf->filename, sizeof(bf->filename), "<string>");
799 bf->line_num = 1;
800 file = bf;
801 ret = tcc_compile(s);
802 file = NULL;
803 tcc_free(buf);
804 798
805 /* currently, no need to close */799 tcc_open_bf(s, "<string>", len);
800 memcpy(file->buffer, str, len);
801 ret = tcc_compile(s);
802 tcc_close();
806 return ret;803 return ret;
807}804}
808 805
809/* define a preprocessor symbol. A value can also be provided with the '=' operator */806/* define a preprocessor symbol. A value can also be provided with the '=' operator */
810LIBTCCAPI void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)807LIBTCCAPI void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
811{808{
812 BufferedFile bf1, *bf = &bf1;809 int len1, len2;
813
814 pstrcpy(bf->buffer, IO_BUF_SIZE, sym);
815 pstrcat(bf->buffer, IO_BUF_SIZE, " ");
816 /* default value */810 /* default value */
817 if (!value) 811 if (!value)
818 value = "1";812 value = "1";
819 pstrcat(bf->buffer, IO_BUF_SIZE, value);813 len1 = strlen(sym);
820 814 len2 = strlen(value);
815
821 /* init file structure */816 /* init file structure */
822 bf->fd = -1;817 tcc_open_bf(s1, "<define>", len1 + len2 + 1);
823 bf->buf_ptr = bf->buffer;818 memcpy(file->buffer, sym, len1);
824 bf->buf_end = bf->buffer + strlen(bf->buffer);819 file->buffer[len1] = ' ';
825 *bf->buf_end = CH_EOB;820 memcpy(file->buffer + len1 + 1, value, len2);
826 bf->filename[0] = '\0';
827 bf->line_num = 1;
828 file = bf;
829
830 s1->include_stack_ptr = s1->include_stack;
831 821
832 /* parse with define parser */822 /* parse with define parser */
833 ch = file->buf_ptr[0];823 ch = file->buf_ptr[0];
834 next_nomacro();824 next_nomacro();
835 parse_define();825 parse_define();
836 file = NULL;826
827 tcc_close();
837}828}
838 829
839/* undefine a preprocessor symbol */830/* undefine a preprocessor symbol */
@@ -1082,9 +1073,6 @@ ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags) · 242 unmodified lines
1082 const char *ext;1073 const char *ext;
1083 ElfW(Ehdr) ehdr;1074 ElfW(Ehdr) ehdr;
1084 int fd, ret, size;1075 int fd, ret, size;
1085 BufferedFile *saved_file;
1086
1087 ret = -1;
1088 1076
1089 /* find source file type with extension */1077 /* find source file type with extension */
1090 ext = tcc_fileextension(filename);1078 ext = tcc_fileextension(filename);
@@ -1092,12 +1080,11 @@ ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags) · 1 unmodified line
1092 ext++;1080 ext++;
1093 1081
1094 /* open the file */1082 /* open the file */
1095 saved_file = file;1083 ret = tcc_open(s1, filename);
1096 file = tcc_open(s1, filename);1084 if (ret < 0) {
1097 if (!file) {
1098 if (flags & AFF_PRINT_ERROR)1085 if (flags & AFF_PRINT_ERROR)
1099 error_noabort("file '%s' not found", filename);1086 error_noabort("file '%s' not found", filename);
1100 goto the_end;1087 return ret;
1101 }1088 }
1102 1089
1103 /* update target deps */1090 /* update target deps */
@@ -1192,9 +1179,7 @@ ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags) · 88 unmodified lines
1192 error_noabort("unrecognized file type");1179 error_noabort("unrecognized file type");
1193 1180
1194the_end:1181the_end:
1195 if (file)1182 tcc_close();
1196 tcc_close(file);
1197 file = saved_file;
1198 return ret;1183 return ret;
1199}1184}
1200 1185
tcc.h+5−2
@@ -334,6 +334,7 @@ typedef struct BufferedFile { · 333 unmodified lines
334 uint8_t *buf_ptr;334 uint8_t *buf_ptr;
335 uint8_t *buf_end;335 uint8_t *buf_end;
336 int fd;336 int fd;
337 struct BufferedFile *prev;
337 int line_num; /* current line number - here to simplify code */338 int line_num; /* current line number - here to simplify code */
338 int ifndef_macro; /* #ifndef macro / #endif search */339 int ifndef_macro; /* #ifndef macro / #endif search */
339 int ifndef_macro_saved; /* saved ifndef_macro */340 int ifndef_macro_saved; /* saved ifndef_macro */
@@ -946,8 +947,10 @@ ST_INLN Sym *struct_find(int v); · 606 unmodified lines
946ST_INLN Sym *sym_find(int v);947ST_INLN Sym *sym_find(int v);
947ST_FUNC Sym *global_identifier_push(int v, int t, int c);948ST_FUNC Sym *global_identifier_push(int v, int t, int c);
948 949
949ST_FUNC BufferedFile *tcc_open(TCCState *s1, const char *filename);950ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen);
950ST_FUNC void tcc_close(BufferedFile *bf);951ST_FUNC int tcc_open(TCCState *s1, const char *filename);
952ST_FUNC void tcc_close(void);
953
951ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags);954ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags);
952ST_FUNC int tcc_add_dll(TCCState *s, const char *filename, int flags);955ST_FUNC int tcc_add_dll(TCCState *s, const char *filename, int flags);
953PUB_FUNC int tcc_set_flag(TCCState *s, const char *flag_name, int value);956PUB_FUNC int tcc_set_flag(TCCState *s, const char *flag_name, int value);
tccasm.c+6−16
@@ -825,32 +825,22 @@ ST_FUNC int tcc_assemble(TCCState *s1, int do_preprocess) · 824 unmodified lines
825 end */825 end */
826static void tcc_assemble_inline(TCCState *s1, char *str, int len)826static void tcc_assemble_inline(TCCState *s1, char *str, int len)
827{827{
828 BufferedFile *bf, *saved_file;
829 int saved_parse_flags;828 int saved_parse_flags;
830 const int *saved_macro_ptr;829 const int *saved_macro_ptr;
831 830
832 bf = tcc_malloc(sizeof(BufferedFile));
833 memset(bf, 0, sizeof(BufferedFile));
834 bf->fd = -1;
835 bf->buf_ptr = str;
836 bf->buf_end = str + len;
837 str[len] = CH_EOB;
838 /* same name as current file so that errors are correctly
839 reported */
840 pstrcpy(bf->filename, sizeof(bf->filename), file->filename);
841 bf->line_num = file->line_num;
842 saved_file = file;
843 file = bf;
844 saved_parse_flags = parse_flags;831 saved_parse_flags = parse_flags;
845 saved_macro_ptr = macro_ptr;832 saved_macro_ptr = macro_ptr;
833
834 tcc_open_bf(s1, file->filename, len);
835 file->line_num = file->prev->line_num;
836 memcpy(file->buffer, str, len);
837
846 macro_ptr = NULL;838 macro_ptr = NULL;
847
848 tcc_assemble_internal(s1, 0);839 tcc_assemble_internal(s1, 0);
840 tcc_close();
849 841
850 parse_flags = saved_parse_flags;842 parse_flags = saved_parse_flags;
851 macro_ptr = saved_macro_ptr;843 macro_ptr = saved_macro_ptr;
852 file = saved_file;
853 tcc_free(bf);
854}844}
855 845
856/* find a constraint by its number or id (gcc 3 extended846/* find a constraint by its number or id (gcc 3 extended
tccpp.c+13−18
@@ -1433,10 +1433,9 @@ ST_FUNC void preprocess(int is_bof) · 1432 unmodified lines
1433 n = s1->nb_include_paths + s1->nb_sysinclude_paths;1433 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
1434 for (i = -2; i < n; ++i) {1434 for (i = -2; i < n; ++i) {
1435 char buf1[sizeof file->filename];1435 char buf1[sizeof file->filename];
1436 BufferedFile *f;
1437 CachedInclude *e;1436 CachedInclude *e;
1438 const char *path;1437 const char *path;
1439 int size;1438 int size, fd;
1440 1439
1441 if (i == -2) {1440 if (i == -2) {
1442 /* check absolute include path */1441 /* check absolute include path */
@@ -1471,21 +1470,21 @@ ST_FUNC void preprocess(int is_bof) · 28 unmodified lines
1471#ifdef INC_DEBUG1470#ifdef INC_DEBUG
1472 printf("%s: skipping %s\n", file->filename, buf);1471 printf("%s: skipping %s\n", file->filename, buf);
1473#endif1472#endif
1474 f = NULL;1473 fd = 0;
1475 } else {1474 } else {
1476 f = tcc_open(s1, buf1);1475 fd = tcc_open(s1, buf1);
1477 if (!f)1476 if (fd < 0)
1478 continue;1477 continue;
1479 }1478 }
1480 1479
1481 if (tok == TOK_INCLUDE_NEXT) {1480 if (tok == TOK_INCLUDE_NEXT) {
1482 tok = TOK_INCLUDE;1481 tok = TOK_INCLUDE;
1483 if (f)1482 if (fd)
1484 tcc_close(f);1483 tcc_close();
1485 continue;1484 continue;
1486 }1485 }
1487 1486
1488 if (!f)1487 if (0 == fd)
1489 goto include_done;1488 goto include_done;
1490 1489
1491#ifdef INC_DEBUG1490#ifdef INC_DEBUG
@@ -1494,17 +1493,14 @@ ST_FUNC void preprocess(int is_bof) · 2 unmodified lines
1494 /* update target deps */1493 /* update target deps */
1495 dynarray_add((void ***)&s1->target_deps, &s1->nb_target_deps,1494 dynarray_add((void ***)&s1->target_deps, &s1->nb_target_deps,
1496 tcc_strdup(buf1));1495 tcc_strdup(buf1));
1497
1498 /* XXX: fix current line init */1496 /* XXX: fix current line init */
1499 /* push current file in stack */1497 /* push current file in stack */
1500 *s1->include_stack_ptr++ = file;1498 *s1->include_stack_ptr++ = file->prev;
1501 f->inc_type = c;1499 file->inc_type = c;
1502 pstrcpy(f->inc_filename, sizeof(f->inc_filename), buf1);1500 pstrcpy(file->inc_filename, sizeof(file->inc_filename), buf1);
1503 file = f;
1504 /* add include file debug info */1501 /* add include file debug info */
1505 if (s1->do_debug) {1502 if (s1->do_debug)
1506 put_stabs(file->filename, N_BINCL, 0, 0, 0);1503 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1507 }
1508 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;1504 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
1509 ch = file->buf_ptr[0];1505 ch = file->buf_ptr[0];
1510 goto the_end;1506 goto the_end;
@@ -2134,9 +2130,8 @@ static inline void next_nomacro1(void) · 623 unmodified lines
2134 put_stabd(N_EINCL, 0, 0);2130 put_stabd(N_EINCL, 0, 0);
2135 }2131 }
2136 /* pop include stack */2132 /* pop include stack */
2137 tcc_close(file);2133 tcc_close();
2138 s1->include_stack_ptr--;2134 s1->include_stack_ptr--;
2139 file = *s1->include_stack_ptr;
2140 p = file->buf_ptr;2135 p = file->buf_ptr;
2141 goto redo_no_start;2136 goto redo_no_start;
2142 }2137 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t