History

suppressed section_ptr - fixed constant string allocation

bellard committed 24 years ago

Commit f946dff1 parent 524be932 files · +45 −54Browse files

Unified · Split

2 files
  1. tcc.c +42 −52
  2. tccelf.c +3 −2
tcc.c+42−52
@@ -562,9 +562,10 @@ int expr_const(void); · 561 unmodified lines
562void expr_eq(void);562void expr_eq(void);
563void gexpr(void);563void gexpr(void);
564void decl(int l);564void decl(int l);
565void decl_initializer(int t, Section *sec, unsigned long c, int first, int size_only);565static void decl_initializer(int t, Section *sec, unsigned long c,
566void decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init,566 int first, int size_only);
567 int v, int scope);567static void decl_initializer_alloc(int t, AttributeDef *ad, int r,
568 int has_init, int v, int scope);
568int gv(int rc);569int gv(int rc);
569void gv2(int rc1, int rc2);570void gv2(int rc1, int rc2);
570void move_reg(int r, int s);571void move_reg(int r, int s);
@@ -607,7 +608,6 @@ static Sym *get_sym_ref(int t, Section *sec, · 36 unmodified lines
607 608
608/* section generation */609/* section generation */
609static void section_realloc(Section *sec, unsigned long new_size);610static void section_realloc(Section *sec, unsigned long new_size);
610static void *section_ptr(Section *sec, unsigned long size);
611static void *section_ptr_add(Section *sec, unsigned long size);611static void *section_ptr_add(Section *sec, unsigned long size);
612static void put_extern_sym(Sym *sym, Section *section, 612static void put_extern_sym(Sym *sym, Section *section,
613 unsigned long value, unsigned long size);613 unsigned long value, unsigned long size);
@@ -873,17 +873,7 @@ static void section_realloc(Section *sec, unsigned long new_size) · 259 unmodified lines
873}873}
874 874
875/* reserve at least 'size' bytes in section 'sec' from875/* reserve at least 'size' bytes in section 'sec' from
876 sec->data_offset. Optimized for speed */876 sec->data_offset. */
877static inline void *section_ptr(Section *sec, unsigned long size)
878{
879 unsigned long offset, offset1;
880 offset = sec->data_offset;
881 offset1 = offset + size;
882 if (offset1 > sec->data_allocated)
883 section_realloc(sec, offset1);
884 return sec->data + offset;
885}
886
887static void *section_ptr_add(Section *sec, unsigned long size)877static void *section_ptr_add(Section *sec, unsigned long size)
888{878{
889 unsigned long offset, offset1;879 unsigned long offset, offset1;
@@ -5010,7 +5000,7 @@ void gfunc_param_typed(GFuncContext *gf, Sym *func, Sym *arg) · 4120 unmodified lines
5010 5000
5011void unary(void)5001void unary(void)
5012{5002{
5013 int n, t, ft, fc, align, size, r, data_offset;5003 int n, t, ft, align, size, r;
5014 Sym *s;5004 Sym *s;
5015 GFuncContext gf;5005 GFuncContext gf;
5016 AttributeDef ad;5006 AttributeDef ad;
@@ -5057,19 +5047,9 @@ void unary(void) · 40 unmodified lines
5057 /* string parsing */5047 /* string parsing */
5058 t = VT_BYTE;5048 t = VT_BYTE;
5059 str_init:5049 str_init:
5060 type_size(t, &align);
5061 data_offset = data_section->data_offset;
5062 data_offset = (data_offset + align - 1) & -align;
5063 fc = data_offset;
5064 /* we must declare it as an array first to use initializer parser */
5065 t = VT_ARRAY | mk_pointer(t);5050 t = VT_ARRAY | mk_pointer(t);
5066 /* XXX: fix it */5051 memset(&ad, 0, sizeof(AttributeDef));
5067 section_ptr(data_section, 1024);5052 decl_initializer_alloc(t, &ad, VT_CONST, 2, 0, 0);
5068 decl_initializer(t, data_section, data_offset, 1, 0);
5069 size = type_size(t, &align);
5070 data_offset += size;
5071 vpush_ref(t, data_section, fc, size);
5072 data_section->data_offset = data_offset;
5073 } else {5053 } else {
5074 t = tok;5054 t = tok;
5075 next();5055 next();
@@ -5794,9 +5774,9 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 718 unmodified lines
5794 address. cur_index/cur_field is the pointer to the current5774 address. cur_index/cur_field is the pointer to the current
5795 value. 'size_only' is true if only size info is needed (only used5775 value. 'size_only' is true if only size info is needed (only used
5796 in arrays) */5776 in arrays) */
5797void decl_designator(int t, Section *sec, unsigned long c, 5777static void decl_designator(int t, Section *sec, unsigned long c,
5798 int *cur_index, Sym **cur_field, 5778 int *cur_index, Sym **cur_field,
5799 int size_only)5779 int size_only)
5800{5780{
5801 Sym *s, *f;5781 Sym *s, *f;
5802 int notfirst, index, align, l;5782 int notfirst, index, align, l;
@@ -5871,8 +5851,8 @@ void decl_designator(int t, Section *sec, unsigned long c, · 68 unmodified lines
5871#define EXPR_ANY 25851#define EXPR_ANY 2
5872 5852
5873/* store a value or an expression directly in global data or in local array */5853/* store a value or an expression directly in global data or in local array */
5874void init_putv(int t, Section *sec, unsigned long c, 5854static void init_putv(int t, Section *sec, unsigned long c,
5875 int v, int expr_type)5855 int v, int expr_type)
5876{5856{
5877 int saved_global_expr, bt;5857 int saved_global_expr, bt;
5878 void *ptr;5858 void *ptr;
@@ -5941,7 +5921,7 @@ void init_putv(int t, Section *sec, unsigned long c, · 62 unmodified lines
5941}5921}
5942 5922
5943/* put zeros for variable based init */5923/* put zeros for variable based init */
5944void init_putz(int t, Section *sec, unsigned long c, int size)5924static void init_putz(int t, Section *sec, unsigned long c, int size)
5945{5925{
5946 GFuncContext gf;5926 GFuncContext gf;
5947 5927
@@ -5965,7 +5945,8 @@ void init_putz(int t, Section *sec, unsigned long c, int size) · 17 unmodified lines
5965 allocation. 'first' is true if array '{' must be read (multi5945 allocation. 'first' is true if array '{' must be read (multi
5966 dimension implicit array init handling). 'size_only' is true if5946 dimension implicit array init handling). 'size_only' is true if
5967 size only evaluation is wanted (only for arrays). */5947 size only evaluation is wanted (only for arrays). */
5968void decl_initializer(int t, Section *sec, unsigned long c, int first, int size_only)5948static void decl_initializer(int t, Section *sec, unsigned long c,
5949 int first, int size_only)
5969{5950{
5970 int index, array_length, n, no_oblock, nb, parlevel, i;5951 int index, array_length, n, no_oblock, nb, parlevel, i;
5971 int t1, size1, align1, expr_type;5952 int t1, size1, align1, expr_type;
@@ -6118,14 +6099,15 @@ void decl_initializer(int t, Section *sec, unsigned long c, int first, int size_ · 146 unmodified lines
6118 }6099 }
6119}6100}
6120 6101
6121/* parse an initializer for type 't' if 'has_init' is true, and6102/* parse an initializer for type 't' if 'has_init' is non zero, and
6122 allocate space in local or global data space ('r' is either6103 allocate space in local or global data space ('r' is either
6123 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated6104 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
6124 variable 'v' of scope 'scope' is declared before initializers are6105 variable 'v' of scope 'scope' is declared before initializers are
6125 parsed. If 'v' is zero, then a reference to the new object is put6106 parsed. If 'v' is zero, then a reference to the new object is put
6126 in the value stack. */6107 in the value stack. If 'has_init' is 2, a special parsing is done
6127void decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init,6108 to handle string constants. */
6128 int v, int scope)6109static void decl_initializer_alloc(int t, AttributeDef *ad, int r,
6110 int has_init, int v, int scope)
6129{6111{
6130 int size, align, addr, data_offset;6112 int size, align, addr, data_offset;
6131 int level;6113 int level;
@@ -6145,19 +6127,27 @@ void decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init, · 13 unmodified lines
6145 if (!has_init) 6127 if (!has_init)
6146 error("unknown type size");6128 error("unknown type size");
6147 /* get all init string */6129 /* get all init string */
6148 level = 0;6130 if (has_init == 2) {
6149 while (level > 0 || (tok != ',' && tok != ';')) {6131 /* only get strings */
6150 if (tok < 0)6132 while (tok == TOK_STR || tok == TOK_LSTR) {
6151 error("unexpected end of file in initializer");6133 tok_str_add_tok(&init_str);
6152 tok_str_add_tok(&init_str);6134 next();
6153 if (tok == '{')6135 }
6154 level++;6136 } else {
6155 else if (tok == '}') {6137 level = 0;
6156 if (level == 0)6138 while (level > 0 || (tok != ',' && tok != ';')) {
6157 break;6139 if (tok < 0)
6158 level--;6140 error("unexpected end of file in initializer");
6141 tok_str_add_tok(&init_str);
6142 if (tok == '{')
6143 level++;
6144 else if (tok == '}') {
6145 if (level == 0)
6146 break;
6147 level--;
6148 }
6149 next();
6159 }6150 }
6160 next();
6161 }6151 }
6162 tok_str_add(&init_str, -1);6152 tok_str_add(&init_str, -1);
6163 tok_str_add(&init_str, 0);6153 tok_str_add(&init_str, 0);
@@ -7138,7 +7128,7 @@ int tcc_set_output_type(TCCState *s, int output_type) · 974 unmodified lines
7138 7128
7139void help(void)7129void help(void)
7140{7130{
7141 printf("tcc version 0.9.10pre1 - Tiny C Compiler - Copyright (C) 2001, 2002 Fabrice Bellard\n" 7131 printf("tcc version 0.9.10 - Tiny C Compiler - Copyright (C) 2001, 2002 Fabrice Bellard\n"
7142 "usage: tcc [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"7132 "usage: tcc [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
7143 " [-g] [-b] [-Ldir] [-llib] [-shared] [-static]\n"7133 " [-g] [-b] [-Ldir] [-llib] [-shared] [-static]\n"
7144 " [--] infile1 [infile2... --] [infile_args...]\n"7134 " [--] infile1 [infile2... --] [infile_args...]\n"
tccelf.c+3−2
@@ -1511,10 +1511,11 @@ static int tcc_load_object_file(TCCState *s1, · 1510 unmodified lines
1511 if (sh->sh_type != SHT_NOBITS) {1511 if (sh->sh_type != SHT_NOBITS) {
1512 unsigned char *ptr;1512 unsigned char *ptr;
1513 lseek(fd, file_offset + sh->sh_offset, SEEK_SET);1513 lseek(fd, file_offset + sh->sh_offset, SEEK_SET);
1514 ptr = section_ptr(s, size);1514 ptr = section_ptr_add(s, size);
1515 read(fd, ptr, size);1515 read(fd, ptr, size);
1516 } else {
1517 s->data_offset += size;
1516 }1518 }
1517 s->data_offset += size;
1518 }1519 }
1519 1520
1520 /* second short pass to update sh_link and sh_info fields of new1521 /* second short pass to update sh_link and sh_info fields of new

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