History

better file output - fixed order of variable declaration if initializer is present

bellard committed 24 years ago

Commit 12f3f351 parent 17cbb381 file · +99 −65Browse files

Unified · Split

tcc.c+99−65
@@ -520,7 +520,8 @@ void expr_eq(void); · 519 unmodified lines
520520 void gexpr(void);
521521 void decl(int l);
522522 void decl_initializer(int t, int r, int c, int first, int size_only);
523−int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init);
523+int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init,
524+ int v, int scope);
524525 int gv(int rc);
525526 void gv2(int rc1, int rc2);
526527 void move_reg(int r, int s);
@@ -4670,7 +4671,7 @@ void unary(void) · 4143 unmodified lines
46704671 if (!(ft & VT_ARRAY))
46714672 r |= lvalue_type(ft);
46724673 memset(&ad, 0, sizeof(AttributeDef));
4673− fc = decl_initializer_alloc(ft, &ad, r, 1);
4674+ fc = decl_initializer_alloc(ft, &ad, r, 1, 0, 0);
46744675 vset(ft, r, fc);
46754676 } else {
46764677 unary();
@@ -5674,8 +5675,11 @@ void decl_initializer(int t, int r, int c, int first, int size_only) · 997 unmodified lines
56745675
56755676 /* parse an initializer for type 't' if 'has_init' is true, and
56765677 allocate space in local or global data space ('r' is either
5677− VT_LOCAL or VT_CONST). The allocated address in returned */
5678−int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init)
5678+ VT_LOCAL or VT_CONST). The allocated address in returned. If 'v' is
5679+ non zero, then an associated variable 'v' of scope 'scope' is
5680+ declared before initializers are parsed. */
5681+int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init,
5682+ int v, int scope)
56795683 {
56805684 int size, align, addr, data_offset;
56815685 int level;
@@ -5785,6 +5789,24 @@ int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init) · 103 unmodified lines
57855789 }
57865790 sec->data_ptr = (unsigned char *)data_offset;
57875791 }
5792+ if (v) {
5793+ Sym *sym;
5794+ if (scope == VT_CONST) {
5795+ /* global scope: see if already defined */
5796+ sym = sym_find(v);
5797+ if (!sym)
5798+ goto do_def;
5799+ if (!is_compatible_types(sym->t, t))
5800+ error("incompatible types for redefinition of '%s'",
5801+ get_tok_str(v, NULL));
5802+ if (!(sym->r & VT_FORWARD))
5803+ error("redefinition of '%s'", get_tok_str(v, NULL));
5804+ greloc_patch(sym, addr);
5805+ } else {
5806+ do_def:
5807+ sym_push(v, t, r, addr);
5808+ }
5809+ }
57885810 if (has_init) {
57895811 decl_initializer(t, r, addr, 1, 0);
57905812 /* restore parse state if needed */
@@ -5798,16 +5820,8 @@ int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init) · 7 unmodified lines
57985820
57995821 void put_func_debug(int t)
58005822 {
5801− int bind;
58025823 char buf[512];
58035824
5804− if (t & VT_STATIC)
5805− bind = STB_LOCAL;
5806− else
5807− bind = STB_GLOBAL;
5808− put_elf_sym(symtab_section, ind, 0,
5809− ELF32_ST_INFO(bind, STT_FUNC), 0,
5810− cur_text_section->sh_num, funcname);
58115825 /* stabs info */
58125826 /* XXX: we put here a dummy type */
58135827 snprintf(buf, sizeof(buf), "%s:%c1",
@@ -5880,7 +5894,7 @@ void analyse_function(void) · 66 unmodified lines
58805894 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
58815895 void decl(int l)
58825896 {
5883− int t, b, v, addr, has_init, r;
5897+ int t, b, v, has_init, r, bind;
58845898 Sym *sym;
58855899 AttributeDef ad;
58865900
@@ -5972,6 +5986,15 @@ void decl(int l) · 85 unmodified lines
59725986 }
59735987 sym->r = VT_CONST;
59745988 funcname = get_tok_str(v, NULL);
5989+ /* add symbol info */
5990+ if (t & VT_STATIC)
5991+ bind = STB_LOCAL;
5992+ else
5993+ bind = STB_GLOBAL;
5994+ put_elf_sym(symtab_section, ind, 0,
5995+ ELF32_ST_INFO(bind, STT_FUNC), 0,
5996+ cur_text_section->sh_num, funcname);
5997+
59755998 /* put debug symbol */
59765999 if (do_debug)
59776000 put_func_debug(t);
@@ -6027,23 +6050,8 @@ void decl(int l) · 49 unmodified lines
60276050 has_init = (tok == '=');
60286051 if (has_init)
60296052 next();
6030− addr = decl_initializer_alloc(t, &ad, r,
6031− has_init);
6032− if (l == VT_CONST) {
6033− /* global scope: see if already defined */
6034− sym = sym_find(v);
6035− if (!sym)
6036− goto do_def;
6037− if (!is_compatible_types(sym->t, t))
6038− error("incompatible types for redefinition of '%s'",
6039− get_tok_str(v, NULL));
6040− if (!(sym->r & VT_FORWARD))
6041− error("redefinition of '%s'", get_tok_str(v, NULL));
6042− greloc_patch(sym, addr);
6043− } else {
6044− do_def:
6045− sym_push(v, t, r, addr);
6046− }
6053+ decl_initializer_alloc(t, &ad, r,
6054+ has_init, v, l);
60476055 }
60486056 }
60496057 if (tok != ',') {
@@ -6310,8 +6318,8 @@ static int put_elf_str(Section *s, const char *sym) · 260 unmodified lines
63106318 }
63116319
63126320 static void put_elf_sym(Section *s,
6313− unsigned long value, unsigned long size,
6314− int info, int other, int shndx, const char *name)
6321+ unsigned long value, unsigned long size,
6322+ int info, int other, int shndx, const char *name)
63156323 {
63166324 int name_offset;
63176325 Elf32_Sym *sym;
@@ -6393,7 +6401,8 @@ int tcc_output_file(TCCState *s, const char *filename, int file_type) · 75 unmodified lines
63936401 phnum = 0;
63946402 for(sec = first_section; sec != NULL; sec = sec->next) {
63956403 shnum++;
6396− if (sec->sh_flags & SHF_ALLOC)
6404+ if ((sec->sh_flags & SHF_ALLOC) &&
6405+ file_type != TCC_FILE_OBJ)
63976406 phnum++;
63986407 }
63996408 /* allocate section headers */
@@ -6401,6 +6410,7 @@ int tcc_output_file(TCCState *s, const char *filename, int file_type) · 1 unmodified line
64016410 if (!shdr)
64026411 error("memory full");
64036412 memset(shdr, 0, shnum * sizeof(Elf32_Shdr));
6413+
64046414 /* allocate program segment headers */
64056415 phdr = malloc(phnum * sizeof(Elf32_Phdr));
64066416 if (!phdr)
@@ -6434,24 +6444,26 @@ int tcc_output_file(TCCState *s, const char *filename, int file_type) · 27 unmodified lines
64346444 file_offset += sh->sh_size;
64356445 }
64366446 /* build program headers (simplistic - not fully correct) */
6437− j = 0;
6438− for(i=1;i<shnum;i++) {
6439− sh = &shdr[i];
6440− if (sh->sh_type == SHT_PROGBITS &&
6441− (sh->sh_flags & SHF_ALLOC) != 0) {
6442− ph = &phdr[j++];
6443− ph->p_type = PT_LOAD;
6444− ph->p_offset = sh->sh_offset;
6445− ph->p_vaddr = sh->sh_addr;
6446− ph->p_paddr = ph->p_vaddr;
6447− ph->p_filesz = sh->sh_size;
6448− ph->p_memsz = sh->sh_size;
6449− ph->p_flags = PF_R;
6450− if (sh->sh_flags & SHF_WRITE)
6451− ph->p_flags |= PF_W;
6452− if (sh->sh_flags & SHF_EXECINSTR)
6453− ph->p_flags |= PF_X;
6454− ph->p_align = sh->sh_addralign;
6447+ if (phnum > 0) {
6448+ j = 0;
6449+ for(i=1;i<shnum;i++) {
6450+ sh = &shdr[i];
6451+ if (sh->sh_type == SHT_PROGBITS &&
6452+ (sh->sh_flags & SHF_ALLOC) != 0) {
6453+ ph = &phdr[j++];
6454+ ph->p_type = PT_LOAD;
6455+ ph->p_offset = sh->sh_offset;
6456+ ph->p_vaddr = sh->sh_addr;
6457+ ph->p_paddr = ph->p_vaddr;
6458+ ph->p_filesz = sh->sh_size;
6459+ ph->p_memsz = sh->sh_size;
6460+ ph->p_flags = PF_R;
6461+ if (sh->sh_flags & SHF_WRITE)
6462+ ph->p_flags |= PF_W;
6463+ if (sh->sh_flags & SHF_EXECINSTR)
6464+ ph->p_flags |= PF_X;
6465+ ph->p_align = sh->sh_addralign;
6466+ }
64556467 }
64566468 }
64576469
@@ -6466,7 +6478,18 @@ int tcc_output_file(TCCState *s, const char *filename, int file_type) · 8 unmodified lines
64666478 ehdr.e_ident[4] = ELFCLASS32;
64676479 ehdr.e_ident[5] = ELFDATA2LSB;
64686480 ehdr.e_ident[6] = EV_CURRENT;
6469− ehdr.e_type = ET_EXEC;
6481+ switch(file_type) {
6482+ default:
6483+ case TCC_FILE_EXE:
6484+ ehdr.e_type = ET_EXEC;
6485+ break;
6486+ case TCC_FILE_DLL:
6487+ ehdr.e_type = ET_DYN;
6488+ break;
6489+ case TCC_FILE_OBJ:
6490+ ehdr.e_type = ET_REL;
6491+ break;
6492+ }
64706493 ehdr.e_machine = EM_386;
64716494 ehdr.e_version = EV_CURRENT;
64726495 ehdr.e_entry = 0; /* XXX: patch it */
@@ -6502,6 +6525,9 @@ int tcc_output_file(TCCState *s, const char *filename, int file_type) · 29 unmodified lines
65026525 }
65036526 fwrite(shdr, 1, shnum * sizeof(Elf32_Shdr), f);
65046527 fclose(f);
6528+
6529+ free(shdr);
6530+ free(phdr);
65056531 return 0;
65066532 }
65076533
@@ -6736,7 +6762,15 @@ TCCState *tcc_new(void) · 228 unmodified lines
67366762 data_section = new_section(".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
67376763 /* XXX: should change type to SHT_NOBITS */
67386764 bss_section = new_section(".bss", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
6739−
6765+
6766+ /* symbols are always generated for linking stage */
6767+ symtab_section = new_section(".symtab", SHT_SYMTAB, 0);
6768+ symtab_section->sh_entsize = sizeof(Elf32_Sym);
6769+ strtab_section = new_section(".strtab", SHT_STRTAB, 0);
6770+ put_elf_str(strtab_section, "");
6771+ symtab_section->link = strtab_section;
6772+ put_elf_sym(symtab_section, 0, 0, 0, 0, 0, NULL);
6773+
67406774 return s;
67416775 }
67426776
@@ -6764,7 +6798,7 @@ void help(void) · 21 unmodified lines
67646798 {
67656799 printf("tcc version 0.9.8 - Tiny C Compiler - Copyright (C) 2001, 2002 Fabrice Bellard\n"
67666800 "usage: tcc [-Idir] [-Dsym[=val]] [-Usym] [-llib] [-g] [-b]\n"
6767− " [-i infile] infile [infile_args...]\n"
6801+ " [-i infile] [-bench] infile [infile_args...]\n"
67686802 "\n"
67696803 "-Idir : add include path 'dir'\n"
67706804 "-Dsym[=val] : define 'sym' with value 'val'\n"
@@ -6775,16 +6809,18 @@ void help(void) · 4 unmodified lines
67756809 "-b : compile with built-in memory and bounds checker (implies -g)\n"
67766810 #endif
67776811 "-i infile : compile infile\n"
6812+ "-bench : output compilation statistics\n"
67786813 );
67796814 }
67806815
67816816 int main(int argc, char **argv)
67826817 {
67836818 char *r, *outfile;
6784− int optind;
6819+ int optind, file_type;
67856820 TCCState *s;
67866821
67876822 s = tcc_new();
6823+ file_type = TCC_FILE_EXE;
67886824
67896825 optind = 1;
67906826 outfile = NULL;
@@ -6852,17 +6888,15 @@ int main(int argc, char **argv) · 61 unmodified lines
68526888 stab_section->link = stabstr_section;
68536889 /* put first entry */
68546890 put_stabs("", 0, 0, 0, 0);
6855−
6856− /* elf symbols */
6857− symtab_section = new_section(".symtab", SHT_SYMTAB, 0);
6858− symtab_section->sh_entsize = sizeof(Elf32_Sym);
6859− strtab_section = new_section(".strtab", SHT_STRTAB, 0);
6860− put_elf_str(strtab_section, "");
6861− symtab_section->link = strtab_section;
6862− put_elf_sym(symtab_section, 0, 0, 0, 0, 0, NULL);
68636891 }
6892+ } else
6893+ /* the following options are only for testing, so not
6894+ documented */
6895+ if (r[1] == 'c') {
6896+ file_type = TCC_FILE_OBJ;
6897+ } else if (!strcmp(r + 1, "shared")) {
6898+ file_type = TCC_FILE_DLL;
68646899 } else if (r[1] == 'o') {
6865− /* currently, only for testing, so not documented */
68666900 if (optind >= argc)
68676901 goto show_help;
68686902 outfile = argv[optind++];
@@ -6879,7 +6913,7 @@ int main(int argc, char **argv) · 10 unmodified lines
68796913 }
68806914
68816915 if (outfile) {
6882− tcc_output_file(s, outfile, TCC_FILE_EXE);
6916+ tcc_output_file(s, outfile, file_type);
68836917 return 0;
68846918 } else {
68856919 return tcc_run(s, argc - optind, argv + optind);

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