comparison tcc.c @ 554:8c020de0af57

Fix earlier options.c break-up by migrating some stuff from tcc.h to tinycc.h. (This means i386-tinycc can once again rebuild itself without segfaulting.)
author Rob Landley <rob@landley.net>
date Thu, 06 Mar 2008 20:53:55 -0600
parents 4533aa54ffcf
children 646f1f0972b6
comparison
equal deleted inserted replaced
553:4533aa54ffcf 554:8c020de0af57
5 * Copyright (c) 2006-2007 Rob Landley 5 * Copyright (c) 2006-2007 Rob Landley
6 * 6 *
7 * Licensed under GPLv2, see file LICENSE in this tarball 7 * Licensed under GPLv2, see file LICENSE in this tarball
8 */ 8 */
9 9
10 #include "tinycc.h"
10 #include "tcc.h" 11 #include "tcc.h"
11 12
12 // This stuff is used by the code generation backend. 13 // This stuff is used by the code generation backend.
13 14
14 static int ind; /* output code index */ 15 static int ind; /* output code index */
394 name = buf; 395 name = buf;
395 break; 396 break;
396 } 397 }
397 } 398 }
398 #endif 399 #endif
399 if (tcc_state->leading_underscore && can_add_underscore) { 400 if (tccg_leading_underscore && can_add_underscore) {
400 buf1[0] = '_'; 401 buf1[0] = '_';
401 pstrcpy(buf1 + 1, sizeof(buf1) - 1, name); 402 pstrcpy(buf1 + 1, sizeof(buf1) - 1, name);
402 name = buf1; 403 name = buf1;
403 } 404 }
404 info = ELF32_ST_INFO(sym_bind, sym_type); 405 info = ELF32_ST_INFO(sym_bind, sym_type);
483 /* default case: stderr */ 484 /* default case: stderr */
484 fprintf(stderr, "%s\n", buf); 485 fprintf(stderr, "%s\n", buf);
485 } else { 486 } else {
486 s1->error_func(s1->error_opaque, buf); 487 s1->error_func(s1->error_opaque, buf);
487 } 488 }
488 if (!is_warning || s1->warn_error) 489 if (!is_warning || tccg_warn_error)
489 s1->nb_errors++; 490 s1->nb_errors++;
490 } 491 }
491 492
492 #ifdef LIBTCC 493 #ifdef LIBTCC
493 void tcc_set_error_func(TCCState *s, void *error_opaque, 494 void tcc_set_error_func(TCCState *s, void *error_opaque,
534 void warning(char *fmt, ...) 535 void warning(char *fmt, ...)
535 { 536 {
536 TCCState *s1 = tcc_state; 537 TCCState *s1 = tcc_state;
537 va_list ap; 538 va_list ap;
538 539
539 if (s1->warn_none) 540 if (tccg_warn_none)
540 return; 541 return;
541 542
542 va_start(ap, fmt); 543 va_start(ap, fmt);
543 error1(s1, 1, fmt, ap); 544 error1(s1, 1, fmt, ap);
544 va_end(ap); 545 va_end(ap);
1001 if (fch == CH_EOB) 1002 if (fch == CH_EOB)
1002 fch = handle_eob(); 1003 fch = handle_eob();
1003 } 1004 }
1004 1005
1005 /* space excluding newline */ 1006 /* space excluding newline */
1006 //static inline int is_space(int ch) 1007 int is_space(int ch)
1007 //{ 1008 {
1008 // return ch == ' ' || ch == '\t' || ch == '\v' || ch == '\f' || ch == '\r'; 1009 return strchr(" \t\v\f\r", ch);
1009 //} 1010 }
1010 1011
1011 /* handle '\[\r]\n' */ 1012 /* handle '\[\r]\n' */
1012 static int handle_stray_noerror(void) 1013 static int handle_stray_noerror(void)
1013 { 1014 {
1014 while (fch == '\\') { 1015 while (fch == '\\') {
2004 } 2005 }
2005 } 2006 }
2006 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE) 2007 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
2007 error("#include recursion too deep"); 2008 error("#include recursion too deep");
2008 /* now search in all the include paths */ 2009 /* now search in all the include paths */
2009 n = s1->include_paths.len + s1->sysinclude_paths.len; 2010 n = tccg_include_paths.len + s1->sysinclude_paths.len;
2010 for(i = 0; i < n; i++) { 2011 for(i = 0; i < n; i++) {
2011 char *path; 2012 char *path;
2012 int verbose = s1->verbose; 2013 int verbose = tccg_verbose;
2013 2014
2014 verbose -= (s1->include_stack_ptr != s1->include_stack); 2015 verbose -= (s1->include_stack_ptr != s1->include_stack);
2015 if (i < s1->include_paths.len) 2016 if (i < tccg_include_paths.len)
2016 path = s1->include_paths.data[i]; 2017 path = tccg_include_paths.data[i];
2017 else 2018 else
2018 path = s1->sysinclude_paths.data[i - s1->include_paths.len]; 2019 path = s1->sysinclude_paths.data[i - tccg_include_paths.len];
2019 pstrcpy(buf1, sizeof(buf1), path); 2020 pstrcpy(buf1, sizeof(buf1), path);
2020 pstrcat(buf1, sizeof(buf1), "/"); 2021 pstrcat(buf1, sizeof(buf1), "/");
2021 pstrcat(buf1, sizeof(buf1), buf); 2022 pstrcat(buf1, sizeof(buf1), buf);
2022 f = tcc_open(s1, buf1); 2023 f = tcc_open(s1, buf1);
2023 if (verbose > !f) 2024 if (verbose > !f)
5613 #endif 5614 #endif
5614 case TOK_DLLEXPORT: 5615 case TOK_DLLEXPORT:
5615 ad->dllexport = 1; 5616 ad->dllexport = 1;
5616 break; 5617 break;
5617 default: 5618 default:
5618 if (tcc_state->warn_unsupported) 5619 if (tccg_warn_unsupported)
5619 warning("'%s' attribute ignored", get_tok_str(t, NULL)); 5620 warning("'%s' attribute ignored", get_tok_str(t, NULL));
5620 /* skip parameters */ 5621 /* skip parameters */
5621 if (tok == '(') { 5622 if (tok == '(') {
5622 int parenthesis = 0; 5623 int parenthesis = 0;
5623 do { 5624 do {
5992 type_found = 1; 5993 type_found = 1;
5993 } 5994 }
5994 the_end: 5995 the_end:
5995 if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED)) 5996 if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED))
5996 error("signed and unsigned modifier"); 5997 error("signed and unsigned modifier");
5997 if (tcc_state->char_is_unsigned) { 5998 if (tccg_char_is_unsigned) {
5998 if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE) 5999 if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE)
5999 t |= VT_UNSIGNED; 6000 t |= VT_UNSIGNED;
6000 } 6001 }
6001 t &= ~VT_SIGNED; 6002 t &= ~VT_SIGNED;
6002 6003
6374 goto str_init; 6375 goto str_init;
6375 case TOK_STR: 6376 case TOK_STR:
6376 /* string parsing */ 6377 /* string parsing */
6377 t = VT_BYTE; 6378 t = VT_BYTE;
6378 str_init: 6379 str_init:
6379 if (tcc_state->warn_write_strings) 6380 if (tccg_warn_write_strings)
6380 t |= VT_CONSTANT; 6381 t |= VT_CONSTANT;
6381 type.t = t; 6382 type.t = t;
6382 mk_pointer(&type); 6383 mk_pointer(&type);
6383 type.t |= VT_ARRAY; 6384 type.t |= VT_ARRAY;
6384 memset(&ad, 0, sizeof(AttributeDef)); 6385 memset(&ad, 0, sizeof(AttributeDef));
6561 if (!s) { 6562 if (!s) {
6562 if (tok != '(') 6563 if (tok != '(')
6563 error("'%s' undeclared", get_tok_str(t, NULL)); 6564 error("'%s' undeclared", get_tok_str(t, NULL));
6564 /* for simple function calls, we tolerate undeclared 6565 /* for simple function calls, we tolerate undeclared
6565 external reference to int() function */ 6566 external reference to int() function */
6566 if (tcc_state->warn_implicit_function_declaration) 6567 if (tccg_warn_implicit_function_declaration)
6567 warning("implicit declaration of function '%s'", 6568 warning("implicit declaration of function '%s'",
6568 get_tok_str(t, NULL)); 6569 get_tok_str(t, NULL));
6569 s = external_global_sym(t, &func_old_type, 0); 6570 s = external_global_sym(t, &func_old_type, 0);
6570 } 6571 }
6571 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) == 6572 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
8002 /* allocate symbol in corresponding section */ 8003 /* allocate symbol in corresponding section */
8003 sec = ad->section; 8004 sec = ad->section;
8004 if (!sec) { 8005 if (!sec) {
8005 if (has_init) 8006 if (has_init)
8006 sec = data_section; 8007 sec = data_section;
8007 else if (tcc_state->nocommon) 8008 else if (tccg_nocommon)
8008 sec = bss_section; 8009 sec = bss_section;
8009 } 8010 }
8010 if (sec) { 8011 if (sec) {
8011 data_offset = sec->data_offset; 8012 data_offset = sec->data_offset;
8012 data_offset = (data_offset + align - 1) & -align; 8013 data_offset = (data_offset + align - 1) & -align;
8541 next(); 8542 next();
8542 for(;;) { 8543 for(;;) {
8543 if (tok == TOK_EOF) 8544 if (tok == TOK_EOF)
8544 break; 8545 break;
8545 if (!last_is_space) { 8546 if (!last_is_space) {
8546 fputc(' ', s1->outfile); 8547 fputc(' ', tccg_outfile);
8547 } 8548 }
8548 fputs(get_tok_str(tok, &tokc), s1->outfile); 8549 fputs(get_tok_str(tok, &tokc), tccg_outfile);
8549 if (tok == TOK_LINEFEED) { 8550 if (tok == TOK_LINEFEED) {
8550 last_is_space = 1; 8551 last_is_space = 1;
8551 /* XXX: suppress that hack */ 8552 /* XXX: suppress that hack */
8552 parse_flags &= ~PARSE_FLAG_LINEFEED; 8553 parse_flags &= ~PARSE_FLAG_LINEFEED;
8553 next(); 8554 next();
8783 TCCState *s; 8784 TCCState *s;
8784 TokenSym *ts; 8785 TokenSym *ts;
8785 int i, c; 8786 int i, c;
8786 8787
8787 s = tcc_state = xzmalloc(sizeof(TCCState)); 8788 s = tcc_state = xzmalloc(sizeof(TCCState));
8788 tcc_state->output_type = TCC_OUTPUT_MEMORY; 8789 tccg_output_type = TCC_OUTPUT_MEMORY;
8789 8790
8790 /* init isidnum table */ 8791 /* init isidnum table */
8791 for(i=0;i<256;i++) 8792 for(i=0;i<256;i++)
8792 isidnum_table[i] = (i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z') 8793 isidnum_table[i] = (i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z')
8793 || i == '_' || isnum(i); 8794 || i == '_' || isnum(i);
8870 ".dynstrtab", 8871 ".dynstrtab",
8871 ".dynhashtab", SHF_PRIVATE); 8872 ".dynhashtab", SHF_PRIVATE);
8872 s->alacarte_link = 1; 8873 s->alacarte_link = 1;
8873 8874
8874 #ifdef CHAR_IS_UNSIGNED 8875 #ifdef CHAR_IS_UNSIGNED
8875 s->char_is_unsigned = 1; 8876 tccg_char_is_unsigned = 1;
8876 #endif 8877 #endif
8877 #if defined(TCC_TARGET_PE) && 0 8878 #if defined(TCC_TARGET_PE) && 0
8878 /* XXX: currently the PE linker is not ready to support that */ 8879 /* XXX: currently the PE linker is not ready to support that */
8879 s->leading_underscore = 1; 8880 tccg_leading_underscore = 1;
8880 #endif 8881 #endif
8881 return s; 8882 return s;
8882 } 8883 }
8883 8884
8884 void tcc_delete(TCCState *s1) 8885 void tcc_delete(TCCState *s1)
8910 for(i = 0; i < s1->nb_loaded_dlls; i++) 8911 for(i = 0; i < s1->nb_loaded_dlls; i++)
8911 free(s1->loaded_dlls[i]); 8912 free(s1->loaded_dlls[i]);
8912 free(s1->loaded_dlls); 8913 free(s1->loaded_dlls);
8913 8914
8914 /* library paths */ 8915 /* library paths */
8915 for(i = 0; i < s1->library_paths.len; i++) 8916 for(i = 0; i < tccg_library_paths.len; i++)
8916 free(s1->library_paths.data[i]); 8917 free(tccg_library_paths.data[i]);
8917 free(s1->library_paths.data); 8918 free(tccg_library_paths.data);
8918 8919
8919 /* cached includes */ 8920 /* cached includes */
8920 for(i = 0; i < s1->nb_cached_includes; i++) 8921 for(i = 0; i < s1->nb_cached_includes; i++)
8921 free(s1->cached_includes[i]); 8922 free(s1->cached_includes[i]);
8922 free(s1->cached_includes); 8923 free(s1->cached_includes);
8923 8924
8924 for(i = 0; i < s1->include_paths.len; i++) 8925 for(i = 0; i < tccg_include_paths.len; i++)
8925 free(s1->include_paths.data[i]); 8926 free(tccg_include_paths.data[i]);
8926 free(s1->include_paths.data); 8927 free(tccg_include_paths.data);
8927 8928
8928 for(i = 0; i < s1->sysinclude_paths.len; i++) 8929 for(i = 0; i < s1->sysinclude_paths.len; i++)
8929 free(s1->sysinclude_paths.data[i]); 8930 free(s1->sysinclude_paths.data[i]);
8930 free(s1->sysinclude_paths.data); 8931 free(s1->sysinclude_paths.data);
8931 8932
8950 ext++; 8951 ext++;
8951 8952
8952 /* open the file */ 8953 /* open the file */
8953 saved_file = file; 8954 saved_file = file;
8954 file = tcc_open(s1, filename); 8955 file = tcc_open(s1, filename);
8955 if (s1->verbose > !file) 8956 if (tccg_verbose > !file)
8956 printf("%s file '%s'\n", file ? "Read" : "Tried", filename); 8957 printf("%s file '%s'\n", file ? "Read" : "Tried", filename);
8957 if (!file) { 8958 if (!file) {
8958 if (flags & AFF_PRINT_ERROR) { 8959 if (flags & AFF_PRINT_ERROR) {
8959 error_noabort("file '%s' not found", filename); 8960 error_noabort("file '%s' not found", filename);
8960 } 8961 }
8999 ehdr.e_ident[3] == ELFMAG3) { 9000 ehdr.e_ident[3] == ELFMAG3) {
9000 file->line_num = 0; /* do not display line number if error */ 9001 file->line_num = 0; /* do not display line number if error */
9001 if (ehdr.e_type == ET_REL) { 9002 if (ehdr.e_type == ET_REL) {
9002 ret = tcc_load_object_file(s1, fd, 0); 9003 ret = tcc_load_object_file(s1, fd, 0);
9003 } else if (ehdr.e_type == ET_DYN) { 9004 } else if (ehdr.e_type == ET_DYN) {
9004 if (s1->output_type == TCC_OUTPUT_MEMORY) { 9005 if (tccg_output_type == TCC_OUTPUT_MEMORY) {
9005 #ifdef TCC_TARGET_PE 9006 #ifdef TCC_TARGET_PE
9006 ret = -1; 9007 ret = -1;
9007 #else 9008 #else
9008 void *h; 9009 void *h;
9009 h = dlopen(filename, RTLD_GLOBAL | RTLD_LAZY); 9010 h = dlopen(filename, RTLD_GLOBAL | RTLD_LAZY);
9059 static int tcc_add_dll(TCCState *s, char *filename, int flags) 9060 static int tcc_add_dll(TCCState *s, char *filename, int flags)
9060 { 9061 {
9061 char buf[1024]; 9062 char buf[1024];
9062 int i; 9063 int i;
9063 9064
9064 for(i = 0; i < s->library_paths.len; i++) { 9065 for(i = 0; i < tccg_library_paths.len; i++) {
9065 snprintf(buf, sizeof(buf), "%s/%s", 9066 snprintf(buf, sizeof(buf), "%s/%s",
9066 s->library_paths.data[i], filename); 9067 tccg_library_paths.data[i], filename);
9067 if (tcc_add_file_internal(s, buf, flags) == 0) 9068 if (tcc_add_file_internal(s, buf, flags) == 0)
9068 return 0; 9069 return 0;
9069 } 9070 }
9070 return -1; 9071 return -1;
9071 } 9072 }
9075 { 9076 {
9076 char buf[1024]; 9077 char buf[1024];
9077 int i; 9078 int i;
9078 9079
9079 /* first we look for the dynamic library if not static linking */ 9080 /* first we look for the dynamic library if not static linking */
9080 if (!s->static_link) { 9081 if (!tccg_static_link) {
9081 #ifdef TCC_TARGET_PE 9082 #ifdef TCC_TARGET_PE
9082 snprintf(buf, sizeof(buf), "%s.def", libraryname); 9083 snprintf(buf, sizeof(buf), "%s.def", libraryname);
9083 #else 9084 #else
9084 snprintf(buf, sizeof(buf), "lib%s.so", libraryname); 9085 snprintf(buf, sizeof(buf), "lib%s.so", libraryname);
9085 #endif 9086 #endif
9086 if (tcc_add_dll(s, buf, 0) == 0) 9087 if (tcc_add_dll(s, buf, 0) == 0)
9087 return 0; 9088 return 0;
9088 } 9089 }
9089 9090
9090 /* then we look for the static library */ 9091 /* then we look for the static library */
9091 for(i = 0; i < s->library_paths.len; i++) { 9092 for(i = 0; i < tccg_library_paths.len; i++) {
9092 snprintf(buf, sizeof(buf), "%s/lib%s.a", 9093 snprintf(buf, sizeof(buf), "%s/lib%s.a",
9093 s->library_paths.data[i], libraryname); 9094 tccg_library_paths.data[i], libraryname);
9094 if (tcc_add_file_internal(s, buf, 0) == 0) 9095 if (tcc_add_file_internal(s, buf, 0) == 0)
9095 return 0; 9096 return 0;
9096 } 9097 }
9097 return -1; 9098 return -1;
9098 } 9099 }
9105 return 0; 9106 return 0;
9106 } 9107 }
9107 9108
9108 int init_output_type(TCCState *s) 9109 int init_output_type(TCCState *s)
9109 { 9110 {
9110 if (!s->nostdinc) { 9111 if (!tccg_nostdinc) {
9111 char buf[1024]; 9112 char buf[1024];
9112 9113
9113 /* default include paths */ 9114 /* default include paths */
9114 /* XXX: reverse order needed if -isystem support */ 9115 /* XXX: reverse order needed if -isystem support */
9115 add_dynarray_path(s, "/usr/local/include:/usr/include", 9116 add_dynarray_path(s, "/usr/local/include:/usr/include",
9116 &(s->sysinclude_paths)); 9117 &(s->sysinclude_paths));
9117 snprintf(buf, sizeof(buf), "%s/include", tinycc_path); 9118 snprintf(buf, sizeof(buf), "%s/include", tinycc_path);
9118 add_dynarray_path(s, buf, &(s->sysinclude_paths)); 9119 add_dynarray_path(s, buf, &(s->sysinclude_paths));
9119 } 9120 }
9120 9121
9121 if (!s->nostdlib) { 9122 if (!tccg_nostdlib) {
9122 char buf[1024]; 9123 char buf[1024];
9123 snprintf(buf, sizeof(buf), "%s/lib", tinycc_path); 9124 snprintf(buf, sizeof(buf), "%s/lib", tinycc_path);
9124 add_dynarray_path(s, buf, &(s->library_paths)); 9125 add_dynarray_path(s, buf, &(tccg_library_paths));
9125 add_dynarray_path(s, CC_LIBPATH, &(s->library_paths)); 9126 add_dynarray_path(s, CC_LIBPATH, &(tccg_library_paths));
9126 } 9127 }
9127 9128
9128 /* if bound checking, then add corresponding sections */ 9129 /* if bound checking, then add corresponding sections */
9129 #ifdef CONFIG_TCC_BCHECK 9130 #ifdef CONFIG_TCC_BCHECK
9130 if (do_bounds_check) { 9131 if (do_bounds_check) {
9136 lbounds_section = new_section(s, ".lbounds", 9137 lbounds_section = new_section(s, ".lbounds",
9137 SHT_PROGBITS, SHF_ALLOC); 9138 SHT_PROGBITS, SHF_ALLOC);
9138 } 9139 }
9139 #endif 9140 #endif
9140 9141
9141 if (s->char_is_unsigned) { 9142 if (tccg_char_is_unsigned) {
9142 tcc_define_symbol(s, "__CHAR_UNSIGNED__", NULL); 9143 tcc_define_symbol(s, "__CHAR_UNSIGNED__", NULL);
9143 } 9144 }
9144 9145
9145 /* add debug sections */ 9146 /* add debug sections */
9146 if (do_debug) { 9147 if (do_debug) {
9154 put_stabs("", 0, 0, 0, 0); 9155 put_stabs("", 0, 0, 0, 0);
9155 } 9156 }
9156 9157
9157 /* add libc crt1/crti objects */ 9158 /* add libc crt1/crti objects */
9158 #ifndef TCC_TARGET_PE 9159 #ifndef TCC_TARGET_PE
9159 if ((s->output_type == TCC_OUTPUT_EXE || s->output_type == TCC_OUTPUT_DLL) 9160 if ((tccg_output_type == TCC_OUTPUT_EXE || tccg_output_type == TCC_OUTPUT_DLL)
9160 && !s->nostdlib) 9161 && !tccg_nostdlib)
9161 { 9162 {
9162 if (s->output_type != TCC_OUTPUT_DLL) 9163 if (tccg_output_type != TCC_OUTPUT_DLL)
9163 tcc_add_file(s, CC_CRTDIR "/crt1.o"); 9164 tcc_add_file(s, CC_CRTDIR "/crt1.o");
9164 tcc_add_file(s, CC_CRTDIR "/crti.o"); 9165 tcc_add_file(s, CC_CRTDIR "/crti.o");
9165 } 9166 }
9166 #endif 9167 #endif
9167 return 0; 9168 return 0;