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
635635 /********************************************************/
636636 /* I/O layer */
637637
638−ST_FUNC BufferedFile *tcc_open(TCCState *s1, const char *filename)
638+ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen)
639639 {
640− int fd;
641640 BufferedFile *bf;
641+ int buflen = initlen ? initlen : IO_BUF_SIZE;
642642
643− if (strcmp(filename, "-") == 0)
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;
643+ bf = tcc_malloc(sizeof(BufferedFile) + buflen);
654644 bf->buf_ptr = bf->buffer;
655− bf->buf_end = bf->buffer;
656− bf->buffer[0] = CH_EOB; /* put eob symbol */
645+ bf->buf_end = bf->buffer + initlen;
646+ bf->buf_end[0] = CH_EOB; /* put eob symbol */
657647 pstrcpy(bf->filename, sizeof(bf->filename), filename);
658648 #ifdef _WIN32
659649 normalize_slashes(bf->filename);
@@ -661,17 +651,40 @@ ST_FUNC BufferedFile *tcc_open(TCCState *s1, const char *filename) · 1 unmodified line
661651 bf->line_num = 1;
662652 bf->ifndef_macro = 0;
663653 bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
664− // printf("opening '%s'\n", filename);
665− return bf;
654+ bf->fd = -1;
655+ bf->prev = file;
656+ file = bf;
666657 }
667658
668−ST_FUNC void tcc_close(BufferedFile *bf)
659+ST_FUNC void tcc_close(void)
669660 {
670− total_lines += bf->line_num;
671− close(bf->fd);
661+ BufferedFile *bf = file;
662+ if (bf->fd > 0) {
663+ close(bf->fd);
664+ total_lines += bf->line_num;
665+ }
666+ file = bf->prev;
672667 tcc_free(bf);
673668 }
674669
670+ST_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+
675688 /* compile the C file opened in 'file'. Return non zero if errors. */
676689 static int tcc_compile(TCCState *s1)
677690 {
@@ -780,60 +793,38 @@ static int tcc_compile(TCCState *s1) · 102 unmodified lines
780793
781794 LIBTCCAPI int tcc_compile_string(TCCState *s, const char *str)
782795 {
783− BufferedFile bf1, *bf = &bf1;
784− int ret, len;
785− char *buf;
786−
787− /* init file structure */
788− bf->fd = -1;
789− /* XXX: avoid copying */
796+ int len, ret;
790797 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);
804798
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();
806803 return ret;
807804 }
808805
809806 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
810807 LIBTCCAPI void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
811808 {
812− BufferedFile bf1, *bf = &bf1;
813−
814− pstrcpy(bf->buffer, IO_BUF_SIZE, sym);
815− pstrcat(bf->buffer, IO_BUF_SIZE, " ");
809+ int len1, len2;
816810 /* default value */
817− if (!value)
811+ if (!value)
818812 value = "1";
819− pstrcat(bf->buffer, IO_BUF_SIZE, value);
820−
813+ len1 = strlen(sym);
814+ len2 = strlen(value);
815+
821816 /* init file structure */
822− bf->fd = -1;
823− bf->buf_ptr = bf->buffer;
824− bf->buf_end = bf->buffer + strlen(bf->buffer);
825− *bf->buf_end = CH_EOB;
826− bf->filename[0] = '\0';
827− bf->line_num = 1;
828− file = bf;
829−
830− s1->include_stack_ptr = s1->include_stack;
817+ tcc_open_bf(s1, "<define>", len1 + len2 + 1);
818+ memcpy(file->buffer, sym, len1);
819+ file->buffer[len1] = ' ';
820+ memcpy(file->buffer + len1 + 1, value, len2);
831821
832822 /* parse with define parser */
833823 ch = file->buf_ptr[0];
834824 next_nomacro();
835825 parse_define();
836− file = NULL;
826+
827+ tcc_close();
837828 }
838829
839830 /* 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
10821073 const char *ext;
10831074 ElfW(Ehdr) ehdr;
10841075 int fd, ret, size;
1085− BufferedFile *saved_file;
1086−
1087− ret = -1;
10881076
10891077 /* find source file type with extension */
10901078 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
10921080 ext++;
10931081
10941082 /* open the file */
1095− saved_file = file;
1096− file = tcc_open(s1, filename);
1097− if (!file) {
1083+ ret = tcc_open(s1, filename);
1084+ if (ret < 0) {
10981085 if (flags & AFF_PRINT_ERROR)
10991086 error_noabort("file '%s' not found", filename);
1100− goto the_end;
1087+ return ret;
11011088 }
11021089
11031090 /* update target deps */
@@ -1192,9 +1179,7 @@ ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags) · 88 unmodified lines
11921179 error_noabort("unrecognized file type");
11931180
11941181 the_end:
1195− if (file)
1196− tcc_close(file);
1197− file = saved_file;
1182+ tcc_close();
11981183 return ret;
11991184 }
12001185
tcc.h+5−2
@@ -334,6 +334,7 @@ typedef struct BufferedFile { · 333 unmodified lines
334334 uint8_t *buf_ptr;
335335 uint8_t *buf_end;
336336 int fd;
337+ struct BufferedFile *prev;
337338 int line_num; /* current line number - here to simplify code */
338339 int ifndef_macro; /* #ifndef macro / #endif search */
339340 int ifndef_macro_saved; /* saved ifndef_macro */
@@ -946,8 +947,10 @@ ST_INLN Sym *struct_find(int v); · 606 unmodified lines
946947 ST_INLN Sym *sym_find(int v);
947948 ST_FUNC Sym *global_identifier_push(int v, int t, int c);
948949
949−ST_FUNC BufferedFile *tcc_open(TCCState *s1, const char *filename);
950−ST_FUNC void tcc_close(BufferedFile *bf);
950+ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen);
951+ST_FUNC int tcc_open(TCCState *s1, const char *filename);
952+ST_FUNC void tcc_close(void);
953+
951954 ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags);
952955 ST_FUNC int tcc_add_dll(TCCState *s, const char *filename, int flags);
953956 PUB_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
825825 end */
826826 static void tcc_assemble_inline(TCCState *s1, char *str, int len)
827827 {
828− BufferedFile *bf, *saved_file;
829828 int saved_parse_flags;
830829 const int *saved_macro_ptr;
831830
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;
844831 saved_parse_flags = parse_flags;
845832 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+
846838 macro_ptr = NULL;
847−
848839 tcc_assemble_internal(s1, 0);
840+ tcc_close();
849841
850842 parse_flags = saved_parse_flags;
851843 macro_ptr = saved_macro_ptr;
852− file = saved_file;
853− tcc_free(bf);
854844 }
855845
856846 /* 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
14331433 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
14341434 for (i = -2; i < n; ++i) {
14351435 char buf1[sizeof file->filename];
1436− BufferedFile *f;
14371436 CachedInclude *e;
14381437 const char *path;
1439− int size;
1438+ int size, fd;
14401439
14411440 if (i == -2) {
14421441 /* check absolute include path */
@@ -1471,21 +1470,21 @@ ST_FUNC void preprocess(int is_bof) · 28 unmodified lines
14711470 #ifdef INC_DEBUG
14721471 printf("%s: skipping %s\n", file->filename, buf);
14731472 #endif
1474− f = NULL;
1475− } else {
1476− f = tcc_open(s1, buf1);
1477− if (!f)
1473+ fd = 0;
1474+ } else {
1475+ fd = tcc_open(s1, buf1);
1476+ if (fd < 0)
14781477 continue;
14791478 }
14801479
14811480 if (tok == TOK_INCLUDE_NEXT) {
14821481 tok = TOK_INCLUDE;
1483− if (f)
1484− tcc_close(f);
1482+ if (fd)
1483+ tcc_close();
14851484 continue;
14861485 }
14871486
1488− if (!f)
1487+ if (0 == fd)
14891488 goto include_done;
14901489
14911490 #ifdef INC_DEBUG
@@ -1494,17 +1493,14 @@ ST_FUNC void preprocess(int is_bof) · 2 unmodified lines
14941493 /* update target deps */
14951494 dynarray_add((void ***)&s1->target_deps, &s1->nb_target_deps,
14961495 tcc_strdup(buf1));
1497−
14981496 /* XXX: fix current line init */
14991497 /* push current file in stack */
1500− *s1->include_stack_ptr++ = file;
1501− f->inc_type = c;
1502− pstrcpy(f->inc_filename, sizeof(f->inc_filename), buf1);
1503− file = f;
1498+ *s1->include_stack_ptr++ = file->prev;
1499+ file->inc_type = c;
1500+ pstrcpy(file->inc_filename, sizeof(file->inc_filename), buf1);
15041501 /* add include file debug info */
1505− if (s1->do_debug) {
1502+ if (s1->do_debug)
15061503 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1507− }
15081504 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
15091505 ch = file->buf_ptr[0];
15101506 goto the_end;
@@ -2134,9 +2130,8 @@ static inline void next_nomacro1(void) · 623 unmodified lines
21342130 put_stabd(N_EINCL, 0, 0);
21352131 }
21362132 /* pop include stack */
2137− tcc_close(file);
2133+ tcc_close();
21382134 s1->include_stack_ptr--;
2139− file = *s1->include_stack_ptr;
21402135 p = file->buf_ptr;
21412136 goto redo_no_start;
21422137 }

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