History

[NOMERGE] expose more internals through libtcc.h

Used by StreetLISP to access macro definitions, tokens, type information.

Sigrid Solveig Haflínudóttir committed 10 months ago

Commit 04da1001 parent b7791f64 files · +181 −116Browse files

Unified · Split

4 files
  1. libtcc.c +49 −0
  2. libtcc.h +118 −7
  3. tcc.h +13 −96
  4. tccpp.c +1 −13
libtcc.c+49−0
@@ -2262,3 +2262,52 @@ PUB_FUNC void tcc_print_stats(TCCState *s1, unsigned total_time) · 2261 unmodified lines
2262 fprintf(stderr, " %d max (bytes)\n", mem_max_size);2262 fprintf(stderr, " %d max (bytes)\n", mem_max_size);
2263#endif2263#endif
2264}2264}
2265
2266LIBTCCAPI const char *tcc_token_to_str(int v)
2267{
2268 return get_tok_str(v, NULL);
2269}
2270
2271LIBTCCAPI char *tcc_macro_to_str(const int *a)
2272{
2273 CString cstr = {0};
2274 const char *s;
2275 CValue cv;
2276 int t;
2277
2278 if(a == NULL || *a == 0)
2279 return NULL;
2280 while(*a){
2281 TOK_GET(&t, &a, &cv);
2282 s = get_tok_str(t, &cv);
2283 cstr_cat(&cstr, s, -1);
2284 }
2285 return cstr.data;
2286}
2287
2288LIBTCCAPI int tcc_ctype_array_size(const CType *ctype)
2289{
2290 if (ctype->ref == NULL || (ctype->t & VT_ARRAY) == 0)
2291 return -1;
2292 return ctype->ref->c;
2293}
2294
2295LIBTCCAPI int tcc_sym_token(Sym *s)
2296{
2297 return s->v;
2298}
2299
2300LIBTCCAPI struct Sym *tcc_sym_next(Sym *s)
2301{
2302 return s->next;
2303}
2304
2305LIBTCCAPI CType *tcc_sym_type(Sym *s)
2306{
2307 return &s->type;
2308}
2309
2310LIBTCCAPI long long tcc_sym_enum_val(Sym *s)
2311{
2312 return s->enum_val;
2313}
libtcc.h+118−7
@@ -30,13 +30,6 @@ LIBTCCAPI void tcc_set_lib_path(TCCState *s, const char *path); · 29 unmodified lines
30typedef void TCCErrorFunc(void *opaque, const char *msg);30typedef void TCCErrorFunc(void *opaque, const char *msg);
31LIBTCCAPI void tcc_set_error_func(TCCState *s, void *error_opaque, TCCErrorFunc *error_func);31LIBTCCAPI void tcc_set_error_func(TCCState *s, void *error_opaque, TCCErrorFunc *error_func);
32 32
33typedef struct CType CType;
34typedef void TCCDeclFunc(void *opaque, const char *file, int line, int v, int l, CType *type);
35LIBTCCAPI void tcc_set_decl_func(TCCState *s, void *decl_opaque, TCCDeclFunc *decl_func);
36
37typedef void TCCDefineFunc(void *opaque, const char *file, int line, int v, int macro_type, int *str);
38LIBTCCAPI void tcc_set_define_func(TCCState *s, void *define_opaque, TCCDefineFunc *define_func);
39
40/* set options as from command line (multiple supported) */33/* set options as from command line (multiple supported) */
41LIBTCCAPI int tcc_set_options(TCCState *s, const char *str);34LIBTCCAPI int tcc_set_options(TCCState *s, const char *str);
42 35
@@ -116,6 +109,124 @@ LIBTCCAPI void *_tcc_setjmp(TCCState *s1, void *jmp_buf, void *top_func, void *l · 73 unmodified lines
116typedef int TCCBtFunc(void *udata, void *pc, const char *file, int line, const char* func, const char *msg);109typedef int TCCBtFunc(void *udata, void *pc, const char *file, int line, const char* func, const char *msg);
117LIBTCCAPI void tcc_set_backtrace_func(TCCState *s1, void* userdata, TCCBtFunc*);110LIBTCCAPI void tcc_set_backtrace_func(TCCState *s1, void* userdata, TCCBtFunc*);
118 111
112
113/* The current value can be: */
114#define VT_VALMASK 0x003f /* mask for value location, register or: */
115#define VT_CONST 0x0030 /* constant in vc (must be first non register value) */
116#define VT_LLOCAL 0x0031 /* lvalue, offset on stack */
117#define VT_LOCAL 0x0032 /* offset on stack */
118#define VT_CMP 0x0033 /* the value is stored in processor flags (in vc) */
119#define VT_JMP 0x0034 /* value is the consequence of jmp true (even) */
120#define VT_JMPI 0x0035 /* value is the consequence of jmp false (odd) */
121#define VT_LVAL 0x0100 /* var is an lvalue */
122#define VT_SYM 0x0200 /* a symbol value is added */
123#define VT_MUSTCAST 0x0C00 /* value must be casted to be correct (used for
124 char/short stored in integer registers) */
125#define VT_NONCONST 0x1000 /* VT_CONST, but not an (C standard) integer
126 constant expression */
127#define VT_MUSTBOUND 0x4000 /* bound checking must be done before
128 dereferencing value */
129#define VT_BOUNDED 0x8000 /* value is bounded. The address of the
130 bounding function call point is in vc */
131/* types */
132#define VT_BTYPE 0x000f /* mask for basic type */
133#define VT_VOID 0 /* void type */
134#define VT_BYTE 1 /* signed byte type */
135#define VT_SHORT 2 /* short type */
136#define VT_INT 3 /* integer type */
137#define VT_LLONG 4 /* 64 bit integer */
138#define VT_PTR 5 /* pointer */
139#define VT_FUNC 6 /* function type */
140#define VT_STRUCT 7 /* struct/union definition */
141#define VT_FLOAT 8 /* IEEE float */
142#define VT_DOUBLE 9 /* IEEE double */
143#define VT_LDOUBLE 10 /* IEEE long double */
144#define VT_BOOL 11 /* ISOC99 boolean type */
145#define VT_QLONG 13 /* 128-bit integer. Only used for x86-64 ABI */
146#define VT_QFLOAT 14 /* 128-bit float. Only used for x86-64 ABI */
147
148#define VT_UNSIGNED 0x0010 /* unsigned type */
149#define VT_DEFSIGN 0x0020 /* explicitly signed or unsigned */
150#define VT_ARRAY 0x0040 /* array type (also has VT_PTR) */
151#define VT_BITFIELD 0x0080 /* bitfield modifier */
152#define VT_CONSTANT 0x0100 /* const modifier */
153#define VT_VOLATILE 0x0200 /* volatile modifier */
154#define VT_VLA 0x0400 /* VLA type (also has VT_PTR and VT_ARRAY) */
155#define VT_LONG 0x0800 /* long type (also has VT_INT rsp. VT_LLONG) */
156
157/* storage */
158#define VT_EXTERN 0x00001000 /* extern definition */
159#define VT_STATIC 0x00002000 /* static variable */
160#define VT_TYPEDEF 0x00004000 /* typedef definition */
161#define VT_INLINE 0x00008000 /* inline definition */
162#define VT_TLS 0x00010000 /* thread-local storage */
163/* currently unused: 0x000[248]0000 */
164
165#define VT_STRUCT_SHIFT 20 /* shift for bitfield shift values (32 - 2*6) */
166#define VT_STRUCT_MASK (((1U << (6+6)) - 1) << VT_STRUCT_SHIFT | VT_BITFIELD)
167#define BIT_POS(t) (((t) >> VT_STRUCT_SHIFT) & 0x3f)
168#define BIT_SIZE(t) (((t) >> (VT_STRUCT_SHIFT + 6)) & 0x3f)
169
170#define VT_UNION (1 << VT_STRUCT_SHIFT | VT_STRUCT)
171#define VT_ENUM (2 << VT_STRUCT_SHIFT) /* integral type is an enum really */
172#define VT_ENUM_VAL (3 << VT_STRUCT_SHIFT) /* integral type is an enum constant really */
173
174#define IS_ENUM(t) ((t & VT_STRUCT_MASK) == VT_ENUM)
175#define IS_ENUM_VAL(t) ((t & VT_STRUCT_MASK) == VT_ENUM_VAL)
176#define IS_UNION(t) ((t & (VT_STRUCT_MASK|VT_BTYPE)) == VT_UNION)
177
178#define VT_ATOMIC VT_VOLATILE
179
180/* type mask (except storage) */
181#define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE | VT_TLS)
182#define VT_TYPE (~(VT_STORAGE|VT_STRUCT_MASK))
183
184/* symbol was created by tccasm.c first */
185#define VT_ASM (VT_VOID | 4 << VT_STRUCT_SHIFT)
186#define VT_ASM_FUNC (VT_VOID | 5 << VT_STRUCT_SHIFT)
187#define IS_ASM_SYM(sym) (((sym)->type.t & ((VT_BTYPE|VT_STRUCT_MASK) & ~(1<<VT_STRUCT_SHIFT))) == VT_ASM)
188#define IS_ASM_FUNC(t) ((t & (VT_BTYPE|VT_STRUCT_MASK)) == VT_ASM_FUNC)
189
190/* base type is array (from typedef/typeof) */
191#define VT_BT_ARRAY (6 << VT_STRUCT_SHIFT)
192#define IS_BT_ARRAY(t) ((t & VT_STRUCT_MASK) == VT_BT_ARRAY)
193
194#define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
195#define SYM_FIELD 0x20000000 /* struct/union field symbol space */
196#define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */
197
198/* field 'Sym.t' for macros */
199#define MACRO_OBJ 0 /* object like macro */
200#define MACRO_FUNC 1 /* function like macro */
201#define MACRO_JOIN 2 /* macro uses ## */
202
203typedef struct Sym Sym;
204
205/* type definition */
206typedef struct CType {
207 int t;
208 struct Sym *ref;
209} CType;
210typedef void TCCDeclFunc(void *opaque, const char *file, int line, int v, int l, CType *type);
211LIBTCCAPI void tcc_set_decl_func(TCCState *s, void *decl_opaque, TCCDeclFunc *decl_func);
212
213typedef void TCCDefineFunc(void *opaque, const char *file, int line, int v, int macro_type, int *str);
214LIBTCCAPI void tcc_set_define_func(TCCState *s, void *define_opaque, TCCDefineFunc *define_func);
215
216LIBTCCAPI const char *tcc_token_to_str(int v);
217
218LIBTCCAPI char *tcc_macro_to_str(const int *a);
219
220LIBTCCAPI int tcc_ctype_array_size(const CType *ctype);
221
222LIBTCCAPI int tcc_sym_token(Sym *s);
223
224LIBTCCAPI struct Sym *tcc_sym_next(Sym *s);
225
226LIBTCCAPI CType *tcc_sym_type(Sym *s);
227
228LIBTCCAPI long long tcc_sym_enum_val(Sym *s);
229
119#ifdef __cplusplus230#ifdef __cplusplus
120}231}
121#endif232#endif
tcc.h+13−96
@@ -453,12 +453,6 @@ typedef struct CString { · 452 unmodified lines
453 char *data; /* nwchar_t* in cases */453 char *data; /* nwchar_t* in cases */
454} CString;454} CString;
455 455
456/* type definition */
457typedef struct CType {
458 int t;
459 struct Sym *ref;
460} CType;
461
462/* long double words on host(!) platform */456/* long double words on host(!) platform */
463#define LDOUBLE_WORDS ((sizeof(long double)+3)/4)457#define LDOUBLE_WORDS ((sizeof(long double)+3)/4)
464 458
@@ -590,10 +584,6 @@ typedef struct DLLReference { · 125 unmodified lines
590 584
591/* -------------------------------------------------- */585/* -------------------------------------------------- */
592 586
593#define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
594#define SYM_FIELD 0x20000000 /* struct/union field symbol space */
595#define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */
596
597/* stored in 'Sym->f.func_type' field */587/* stored in 'Sym->f.func_type' field */
598#define FUNC_NEW 1 /* ansi function prototype */588#define FUNC_NEW 1 /* ansi function prototype */
599#define FUNC_OLD 2 /* old function prototype */589#define FUNC_OLD 2 /* old function prototype */
@@ -608,11 +598,6 @@ typedef struct DLLReference { · 8 unmodified lines
608#define FUNC_FASTCALLW 5 /* first parameter in %ecx, %edx */598#define FUNC_FASTCALLW 5 /* first parameter in %ecx, %edx */
609#define FUNC_THISCALL 6 /* first param in %ecx */599#define FUNC_THISCALL 6 /* first param in %ecx */
610 600
611/* field 'Sym.t' for macros */
612#define MACRO_OBJ 0 /* object like macro */
613#define MACRO_FUNC 1 /* function like macro */
614#define MACRO_JOIN 2 /* macro uses ## */
615
616/* field 'Sym.r' for C labels */601/* field 'Sym.r' for C labels */
617#define LABEL_DEFINED 0 /* label is defined */602#define LABEL_DEFINED 0 /* label is defined */
618#define LABEL_FORWARD 1 /* label is forward defined */603#define LABEL_FORWARD 1 /* label is forward defined */
@@ -1023,87 +1008,6 @@ struct filespec { · 404 unmodified lines
1023 char name[1];1008 char name[1];
1024};1009};
1025 1010
1026/* The current value can be: */
1027#define VT_VALMASK 0x003f /* mask for value location, register or: */
1028#define VT_CONST 0x0030 /* constant in vc (must be first non register value) */
1029#define VT_LLOCAL 0x0031 /* lvalue, offset on stack */
1030#define VT_LOCAL 0x0032 /* offset on stack */
1031#define VT_CMP 0x0033 /* the value is stored in processor flags (in vc) */
1032#define VT_JMP 0x0034 /* value is the consequence of jmp true (even) */
1033#define VT_JMPI 0x0035 /* value is the consequence of jmp false (odd) */
1034#define VT_LVAL 0x0100 /* var is an lvalue */
1035#define VT_SYM 0x0200 /* a symbol value is added */
1036#define VT_MUSTCAST 0x0C00 /* value must be casted to be correct (used for
1037 char/short stored in integer registers) */
1038#define VT_NONCONST 0x1000 /* VT_CONST, but not an (C standard) integer
1039 constant expression */
1040#define VT_MUSTBOUND 0x4000 /* bound checking must be done before
1041 dereferencing value */
1042#define VT_BOUNDED 0x8000 /* value is bounded. The address of the
1043 bounding function call point is in vc */
1044/* types */
1045#define VT_BTYPE 0x000f /* mask for basic type */
1046#define VT_VOID 0 /* void type */
1047#define VT_BYTE 1 /* signed byte type */
1048#define VT_SHORT 2 /* short type */
1049#define VT_INT 3 /* integer type */
1050#define VT_LLONG 4 /* 64 bit integer */
1051#define VT_PTR 5 /* pointer */
1052#define VT_FUNC 6 /* function type */
1053#define VT_STRUCT 7 /* struct/union definition */
1054#define VT_FLOAT 8 /* IEEE float */
1055#define VT_DOUBLE 9 /* IEEE double */
1056#define VT_LDOUBLE 10 /* IEEE long double */
1057#define VT_BOOL 11 /* ISOC99 boolean type */
1058#define VT_QLONG 13 /* 128-bit integer. Only used for x86-64 ABI */
1059#define VT_QFLOAT 14 /* 128-bit float. Only used for x86-64 ABI */
1060
1061#define VT_UNSIGNED 0x0010 /* unsigned type */
1062#define VT_DEFSIGN 0x0020 /* explicitly signed or unsigned */
1063#define VT_ARRAY 0x0040 /* array type (also has VT_PTR) */
1064#define VT_BITFIELD 0x0080 /* bitfield modifier */
1065#define VT_CONSTANT 0x0100 /* const modifier */
1066#define VT_VOLATILE 0x0200 /* volatile modifier */
1067#define VT_VLA 0x0400 /* VLA type (also has VT_PTR and VT_ARRAY) */
1068#define VT_LONG 0x0800 /* long type (also has VT_INT rsp. VT_LLONG) */
1069
1070/* storage */
1071#define VT_EXTERN 0x00001000 /* extern definition */
1072#define VT_STATIC 0x00002000 /* static variable */
1073#define VT_TYPEDEF 0x00004000 /* typedef definition */
1074#define VT_INLINE 0x00008000 /* inline definition */
1075#define VT_TLS 0x00010000 /* thread-local storage */
1076/* currently unused: 0x000[248]0000 */
1077
1078#define VT_STRUCT_SHIFT 20 /* shift for bitfield shift values (32 - 2*6) */
1079#define VT_STRUCT_MASK (((1U << (6+6)) - 1) << VT_STRUCT_SHIFT | VT_BITFIELD)
1080#define BIT_POS(t) (((t) >> VT_STRUCT_SHIFT) & 0x3f)
1081#define BIT_SIZE(t) (((t) >> (VT_STRUCT_SHIFT + 6)) & 0x3f)
1082
1083#define VT_UNION (1 << VT_STRUCT_SHIFT | VT_STRUCT)
1084#define VT_ENUM (2 << VT_STRUCT_SHIFT) /* integral type is an enum really */
1085#define VT_ENUM_VAL (3 << VT_STRUCT_SHIFT) /* integral type is an enum constant really */
1086
1087#define IS_ENUM(t) ((t & VT_STRUCT_MASK) == VT_ENUM)
1088#define IS_ENUM_VAL(t) ((t & VT_STRUCT_MASK) == VT_ENUM_VAL)
1089#define IS_UNION(t) ((t & (VT_STRUCT_MASK|VT_BTYPE)) == VT_UNION)
1090
1091#define VT_ATOMIC VT_VOLATILE
1092
1093/* type mask (except storage) */
1094#define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE | VT_TLS)
1095#define VT_TYPE (~(VT_STORAGE|VT_STRUCT_MASK))
1096
1097/* symbol was created by tccasm.c first */
1098#define VT_ASM (VT_VOID | 4 << VT_STRUCT_SHIFT)
1099#define VT_ASM_FUNC (VT_VOID | 5 << VT_STRUCT_SHIFT)
1100#define IS_ASM_SYM(sym) (((sym)->type.t & ((VT_BTYPE|VT_STRUCT_MASK) & ~(1<<VT_STRUCT_SHIFT))) == VT_ASM)
1101#define IS_ASM_FUNC(t) ((t & (VT_BTYPE|VT_STRUCT_MASK)) == VT_ASM_FUNC)
1102
1103/* base type is array (from typedef/typeof) */
1104#define VT_BT_ARRAY (6 << VT_STRUCT_SHIFT)
1105#define IS_BT_ARRAY(t) ((t & VT_STRUCT_MASK) == VT_BT_ARRAY)
1106
1107/* general: set/get the pseudo-bitfield value for bit-mask M */1011/* general: set/get the pseudo-bitfield value for bit-mask M */
1108#define BFVAL(M,N) ((unsigned)((M) & ~((M) << 1)) * (N))1012#define BFVAL(M,N) ((unsigned)((M) & ~((M) << 1)) * (N))
1109#define BFGET(X,M) (((X) & (M)) / BFVAL(M,1))1013#define BFGET(X,M) (((X) & (M)) / BFVAL(M,1))
@@ -1376,8 +1280,21 @@ enum line_macro_output_format { · 266 unmodified lines
1376 LINE_MACRO_OUTPUT_FORMAT_P10 = 111280 LINE_MACRO_OUTPUT_FORMAT_P10 = 11
1377};1281};
1378 1282
1283#if 0
1284# define TOK_GET(t,p,c) tok_get(t,p,c)
1285#else
1286# define TOK_GET(t,p,c) do { \
1287 int _t = **(p); \
1288 if (TOK_HAS_VALUE(_t)) \
1289 tok_get(t, p, c); \
1290 else \
1291 *(t) = _t, ++*(p); \
1292 } while (0)
1293#endif
1294
1379ST_FUNC TokenSym *tok_alloc(const char *str, int len);1295ST_FUNC TokenSym *tok_alloc(const char *str, int len);
1380ST_FUNC int tok_alloc_const(const char *str);1296ST_FUNC int tok_alloc_const(const char *str);
1297ST_FUNC void tok_get(int *t, const int **pp, CValue *cv);
1381ST_FUNC const char *get_tok_str(int v, CValue *cv);1298ST_FUNC const char *get_tok_str(int v, CValue *cv);
1382ST_FUNC void begin_macro(TokenString *str, int alloc);1299ST_FUNC void begin_macro(TokenString *str, int alloc);
1383ST_FUNC void end_macro(void);1300ST_FUNC void end_macro(void);
tccpp.c+1−13
@@ -1177,7 +1177,7 @@ static void tok_str_add2_spc(TokenString *s, int t, CValue *cv) · 1176 unmodified lines
1177}1177}
1178 1178
1179/* get a token from an integer array and increment pointer. */1179/* get a token from an integer array and increment pointer. */
1180static inline void tok_get(int *t, const int **pp, CValue *cv)1180ST_FUNC void tok_get(int *t, const int **pp, CValue *cv)
1181{1181{
1182 const int *p = *pp;1182 const int *p = *pp;
1183 int n, *tab;1183 int n, *tab;
@@ -1232,18 +1232,6 @@ static inline void tok_get(int *t, const int **pp, CValue *cv) · 48 unmodified lines
1232 *pp = p;1232 *pp = p;
1233}1233}
1234 1234
1235#if 0
1236# define TOK_GET(t,p,c) tok_get(t,p,c)
1237#else
1238# define TOK_GET(t,p,c) do { \
1239 int _t = **(p); \
1240 if (TOK_HAS_VALUE(_t)) \
1241 tok_get(t, p, c); \
1242 else \
1243 *(t) = _t, ++*(p); \
1244 } while (0)
1245#endif
1246
1247static int macro_is_equal(const int *a, const int *b)1235static int macro_is_equal(const int *a, const int *b)
1248{1236{
1249 CValue cv;1237 CValue cv;

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