History

#pragma pack support (grischka)

bellard committed 21 years ago

Commit 0bca8321 parent 44211dd2 files · +73 −4Browse files

Unified · Split

2 files
  1. tcc.c +65 −4
  2. tcctok.h +8 −0
tcc.c+65−4
@@ -109,6 +109,7 @@ typedef int BOOL; · 108 unmodified lines
109109 #define IFDEF_STACK_SIZE 64
110110 #define VSTACK_SIZE 256
111111 #define STRING_MAX_SIZE 1024
112+#define PACK_STACK_SIZE 8
112113
113114 #define TOK_HASH_SIZE 8192 /* must be a power of two */
114115 #define TOK_ALLOC_INCR 512 /* must be a power of two */
@@ -481,6 +482,10 @@ struct TCCState { · 366 unmodified lines
481482
482483 /* see cached_includes */
483484 int cached_includes_hash[CACHED_INCLUDES_HASH_SIZE];
485+
486+ /* pack stack */
487+ int pack_stack[PACK_STACK_SIZE];
488+ int *pack_stack_ptr;
484489 };
485490
486491 /* The current value can be: */
@@ -789,7 +794,7 @@ static int put_elf_sym(Section *s, · 302 unmodified lines
789794 unsigned long value, unsigned long size,
790795 int info, int other, int shndx, const char *name);
791796 static int add_elf_sym(Section *s, unsigned long value, unsigned long size,
792− int info, int sh_num, const char *name);
797+ int info, int other, int sh_num, const char *name);
793798 static void put_elf_reloc(Section *symtab, Section *s, unsigned long offset,
794799 int type, int symbol);
795800 static void put_stabs(const char *str, int type, int other, int desc,
@@ -1251,7 +1256,7 @@ static void put_extern_sym(Sym *sym, Section *section, · 455 unmodified lines
12511256 }
12521257 #endif
12531258 info = ELF32_ST_INFO(sym_bind, sym_type);
1254− sym->c = add_elf_sym(symtab_section, value, size, info, sh_num, name);
1259+ sym->c = add_elf_sym(symtab_section, value, size, info, 0, sh_num, name);
12551260 } else {
12561261 esym = &((Elf32_Sym *)symtab_section->data)[sym->c];
12571262 esym->st_value = value;
@@ -2702,6 +2707,52 @@ static inline void add_cached_include(TCCState *s1, int type, · 1444 unmodified lines
27022707 s1->cached_includes_hash[h] = s1->nb_cached_includes;
27032708 }
27042709
2710+static void pragma_parse(TCCState *s1)
2711+{
2712+ int val;
2713+
2714+ next();
2715+ if (tok == TOK_pack) {
2716+ /*
2717+ This may be:
2718+ #pragma pack(1) // set
2719+ #pragma pack() // reset to default
2720+ #pragma pack(push,1) // push & set
2721+ #pragma pack(pop) // restore previous
2722+ */
2723+ skip('(');
2724+ if (tok == TOK_ASM_pop) {
2725+ next();
2726+ if (s1->pack_stack_ptr <= s1->pack_stack) {
2727+ stk_error:
2728+ error("out of pack stack");
2729+ }
2730+ s1->pack_stack_ptr--;
2731+ } else {
2732+ val = 0;
2733+ if (tok != ')') {
2734+ if (tok == TOK_ASM_push) {
2735+ next();
2736+ if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
2737+ goto stk_error;
2738+ s1->pack_stack_ptr++;
2739+ skip(',');
2740+ }
2741+ if (tok != TOK_CINT) {
2742+ pack_error:
2743+ error("invalid pack pragma");
2744+ }
2745+ val = tokc.i;
2746+ if (val < 1 || val > 16 || (val & (val - 1)) != 0)
2747+ goto pack_error;
2748+ next();
2749+ }
2750+ *s1->pack_stack_ptr = val;
2751+ skip(')');
2752+ }
2753+ }
2754+}
2755+
27052756 /* is_bof is true if first non space token at beginning of file */
27062757 static void preprocess(int is_bof)
27072758 {
@@ -2958,7 +3009,7 @@ static void preprocess(int is_bof) · 250 unmodified lines
29583009 warning("#warning %s", buf);
29593010 break;
29603011 case TOK_PRAGMA:
2961− /* ignored */
3012+ pragma_parse(s1);
29623013 break;
29633014 default:
29643015 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_CINT) {
@@ -6437,6 +6488,9 @@ static void struct_decl(CType *type, int u) · 3472 unmodified lines
64376488 align = ad.aligned;
64386489 } else if (ad.packed) {
64396490 align = 1;
6491+ } else if (*tcc_state->pack_stack_ptr) {
6492+ if (align > *tcc_state->pack_stack_ptr)
6493+ align = *tcc_state->pack_stack_ptr;
64406494 }
64416495 lbit_pos = 0;
64426496 if (bit_size >= 0) {
@@ -8971,6 +9025,11 @@ static void decl(int l) · 2528 unmodified lines
89719025 cur_text_section = text_section;
89729026 sym->r = VT_SYM | VT_CONST;
89739027 gen_function(sym);
9028+#ifdef TCC_TARGET_PE
9029+ if (ad.dllexport) {
9030+ ((Elf32_Sym *)symtab_section->data)[sym->c].st_other |= 1;
9031+ }
9032+#endif
89749033 }
89759034 break;
89769035 } else {
@@ -9032,6 +9091,8 @@ static void preprocess_init(TCCState *s1) · 55 unmodified lines
90329091
90339092 /* XXX: not ANSI compliant: bound checking says error */
90349093 vtop = vstack - 1;
9094+ s1->pack_stack[0] = 0;
9095+ s1->pack_stack_ptr = s1->pack_stack;
90359096 }
90369097
90379098 /* compile the C file opened in 'file'. Return non zero if errors. */
@@ -9903,7 +9964,7 @@ int tcc_add_library(TCCState *s, const char *libraryname) · 865 unmodified lines
99039964 int tcc_add_symbol(TCCState *s, const char *name, unsigned long val)
99049965 {
99059966 add_elf_sym(symtab_section, val, 0,
9906− ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),
9967+ ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
99079968 SHN_ABS, name);
99089969 return 0;
99099970 }
tcctok.h+8−0
@@ -109,6 +109,14 @@ · 108 unmodified lines
109109 DEF(TOK_REGPARM1, "regparm")
110110 DEF(TOK_REGPARM2, "__regparm__")
111111
112+/* pragma */
113+ DEF(TOK_pack, "pack")
114+#if !defined(TCC_TARGET_I386)
115+ /* already defined for assembler */
116+ DEF(TOK_ASM_push, "push")
117+ DEF(TOK_ASM_pop, "pop")
118+#endif
119+
112120 /* builtin functions or variables */
113121 DEF(TOK_memcpy, "memcpy")
114122 DEF(TOK_memset, "memset")

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