#ifndef LIBTCC_H#define LIBTCC_H #ifndef LIBTCCAPI# define LIBTCCAPI#endif #ifdef __cplusplusextern "C" {#endif /*****************************//* set custom allocator for all allocations (optional), NULL for default. */typedef void *TCCReallocFunc(void *ptr, unsigned long size);LIBTCCAPI void tcc_set_realloc(TCCReallocFunc *my_realloc); /*****************************/typedef struct TCCState TCCState; /* create a new TCC compilation context */LIBTCCAPI TCCState *tcc_new(void); /* free a TCC compilation context */LIBTCCAPI void tcc_delete(TCCState *s); /* set CONFIG_TCCDIR at runtime */LIBTCCAPI void tcc_set_lib_path(TCCState *s, const char *path); /* set error/warning callback (optional) */typedef void TCCErrorFunc(void *opaque, const char *msg);LIBTCCAPI void tcc_set_error_func(TCCState *s, void *error_opaque, TCCErrorFunc *error_func); /* set options as from command line (multiple supported) */LIBTCCAPI int tcc_set_options(TCCState *s, const char *str); /*****************************//* preprocessor */ /* add include path */LIBTCCAPI int tcc_add_include_path(TCCState *s, const char *pathname); /* add in system include path */LIBTCCAPI int tcc_add_sysinclude_path(TCCState *s, const char *pathname); /* define preprocessor symbol 'sym'. value can be NULL, sym can be "sym=val" */LIBTCCAPI void tcc_define_symbol(TCCState *s, const char *sym, const char *value); /* undefine preprocess symbol 'sym' */LIBTCCAPI void tcc_undefine_symbol(TCCState *s, const char *sym); /*****************************//* compiling */ /* add a file (C file, dll, object, library, ld script). Return -1 if error. */LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename); /* compile a string containing a C source. Return -1 if error. */LIBTCCAPI int tcc_compile_string(TCCState *s, const char *buf); /* Tip: to have more specific errors/warnings from tcc_compile_string(), you can prefix the string with "#line <num> \"<filename>\"\n" */ /*****************************//* linking commands */ /* set output type. MUST BE CALLED before any compilation */LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type);#define TCC_OUTPUT_MEMORY 1 /* output will be run in memory */#define TCC_OUTPUT_EXE 2 /* executable file */#define TCC_OUTPUT_DLL 4 /* dynamic library */#define TCC_OUTPUT_OBJ 3 /* object file */#define TCC_OUTPUT_PREPROCESS 5 /* only preprocess */ /* equivalent to -Lpath option */LIBTCCAPI int tcc_add_library_path(TCCState *s, const char *pathname); /* the library name is the same as the argument of the '-l' option */LIBTCCAPI int tcc_add_library(TCCState *s, const char *libraryname); /* add a symbol to the compiled program */LIBTCCAPI int tcc_add_symbol(TCCState *s, const char *name, const void *val); /* output an executable, library or object file. DO NOT call tcc_relocate() before. */LIBTCCAPI int tcc_output_file(TCCState *s, const char *filename); /* link and run main() function and return its value. DO NOT call tcc_relocate() before. */LIBTCCAPI int tcc_run(TCCState *s, int argc, char **argv); /* do all relocations (needed before using tcc_get_symbol()) */LIBTCCAPI int tcc_relocate(TCCState *s1); /* return symbol value or NULL if not found */LIBTCCAPI void *tcc_get_symbol(TCCState *s, const char *name); /* list all (global) symbols and their values via 'symbol_cb()' */LIBTCCAPI void tcc_list_symbols(TCCState *s, void *ctx, void (*symbol_cb)(void *ctx, const char *name, const void *val)); /* experimental/advanced section (see libtcc_test_mt.c for an example) */ /* catch runtime exceptions (optionally limit backtraces at top_func), when using tcc_set_options("-bt") and when not using tcc_run() */LIBTCCAPI void *_tcc_setjmp(TCCState *s1, void *jmp_buf, void *top_func, void *longjmp);#define tcc_setjmp(s1,jb,f) setjmp(_tcc_setjmp(s1, jb, f, longjmp)) /* custom error printer for runtime exceptions. Returning 0 stops backtrace */typedef int TCCBtFunc(void *udata, void *pc, const char *file, int line, const char* func, const char *msg);LIBTCCAPI void tcc_set_backtrace_func(TCCState *s1, void* userdata, TCCBtFunc*); /* The current value can be: */#define VT_VALMASK 0x003f /* mask for value location, register or: */#define VT_CONST 0x0030 /* constant in vc (must be first non register value) */#define VT_LLOCAL 0x0031 /* lvalue, offset on stack */#define VT_LOCAL 0x0032 /* offset on stack */#define VT_CMP 0x0033 /* the value is stored in processor flags (in vc) */#define VT_JMP 0x0034 /* value is the consequence of jmp true (even) */#define VT_JMPI 0x0035 /* value is the consequence of jmp false (odd) */#define VT_LVAL 0x0100 /* var is an lvalue */#define VT_SYM 0x0200 /* a symbol value is added */#define VT_MUSTCAST 0x0C00 /* value must be casted to be correct (used for char/short stored in integer registers) */#define VT_NONCONST 0x1000 /* VT_CONST, but not an (C standard) integer constant expression */#define VT_MUSTBOUND 0x4000 /* bound checking must be done before dereferencing value */#define VT_BOUNDED 0x8000 /* value is bounded. The address of the bounding function call point is in vc *//* types */#define VT_BTYPE 0x000f /* mask for basic type */#define VT_VOID 0 /* void type */#define VT_BYTE 1 /* signed byte type */#define VT_SHORT 2 /* short type */#define VT_INT 3 /* integer type */#define VT_LLONG 4 /* 64 bit integer */#define VT_PTR 5 /* pointer */#define VT_FUNC 6 /* function type */#define VT_STRUCT 7 /* struct/union definition */#define VT_FLOAT 8 /* IEEE float */#define VT_DOUBLE 9 /* IEEE double */#define VT_LDOUBLE 10 /* IEEE long double */#define VT_BOOL 11 /* ISOC99 boolean type */#define VT_QLONG 13 /* 128-bit integer. Only used for x86-64 ABI */#define VT_QFLOAT 14 /* 128-bit float. Only used for x86-64 ABI */ #define VT_UNSIGNED 0x0010 /* unsigned type */#define VT_DEFSIGN 0x0020 /* explicitly signed or unsigned */#define VT_ARRAY 0x0040 /* array type (also has VT_PTR) */#define VT_BITFIELD 0x0080 /* bitfield modifier */#define VT_CONSTANT 0x0100 /* const modifier */#define VT_VOLATILE 0x0200 /* volatile modifier */#define VT_VLA 0x0400 /* VLA type (also has VT_PTR and VT_ARRAY) */#define VT_LONG 0x0800 /* long type (also has VT_INT rsp. VT_LLONG) */ /* storage */#define VT_EXTERN 0x00001000 /* extern definition */#define VT_STATIC 0x00002000 /* static variable */#define VT_TYPEDEF 0x00004000 /* typedef definition */#define VT_INLINE 0x00008000 /* inline definition */#define VT_TLS 0x00010000 /* thread-local storage *//* currently unused: 0x000[248]0000 */ #define VT_STRUCT_SHIFT 20 /* shift for bitfield shift values (32 - 2*6) */#define VT_STRUCT_MASK (((1U << (6+6)) - 1) << VT_STRUCT_SHIFT | VT_BITFIELD)#define BIT_POS(t) (((t) >> VT_STRUCT_SHIFT) & 0x3f)#define BIT_SIZE(t) (((t) >> (VT_STRUCT_SHIFT + 6)) & 0x3f) #define VT_UNION (1 << VT_STRUCT_SHIFT | VT_STRUCT)#define VT_ENUM (2 << VT_STRUCT_SHIFT) /* integral type is an enum really */#define VT_ENUM_VAL (3 << VT_STRUCT_SHIFT) /* integral type is an enum constant really */ #define IS_ENUM(t) ((t & VT_STRUCT_MASK) == VT_ENUM)#define IS_ENUM_VAL(t) ((t & VT_STRUCT_MASK) == VT_ENUM_VAL)#define IS_UNION(t) ((t & (VT_STRUCT_MASK|VT_BTYPE)) == VT_UNION) #define VT_ATOMIC VT_VOLATILE /* type mask (except storage) */#define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE | VT_TLS)#define VT_TYPE (~(VT_STORAGE|VT_STRUCT_MASK)) /* symbol was created by tccasm.c first */#define VT_ASM (VT_VOID | 4 << VT_STRUCT_SHIFT)#define VT_ASM_FUNC (VT_VOID | 5 << VT_STRUCT_SHIFT)#define IS_ASM_SYM(sym) (((sym)->type.t & ((VT_BTYPE|VT_STRUCT_MASK) & ~(1<<VT_STRUCT_SHIFT))) == VT_ASM)#define IS_ASM_FUNC(t) ((t & (VT_BTYPE|VT_STRUCT_MASK)) == VT_ASM_FUNC) /* base type is array (from typedef/typeof) */#define VT_BT_ARRAY (6 << VT_STRUCT_SHIFT)#define IS_BT_ARRAY(t) ((t & VT_STRUCT_MASK) == VT_BT_ARRAY) #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */#define SYM_FIELD 0x20000000 /* struct/union field symbol space */#define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */ /* field 'Sym.t' for macros */#define MACRO_OBJ 0 /* object like macro */#define MACRO_FUNC 1 /* function like macro */#define MACRO_JOIN 2 /* macro uses ## */ typedef struct Sym Sym; /* type definition */typedef struct CType { int t; struct Sym *ref;} CType;typedef void TCCDeclFunc(void *opaque, const char *file, int line, int v, int l, CType *type);LIBTCCAPI void tcc_set_decl_func(TCCState *s, void *decl_opaque, TCCDeclFunc *decl_func); typedef void TCCDefineFunc(void *opaque, const char *file, int line, int v, int macro_type, int *str);LIBTCCAPI void tcc_set_define_func(TCCState *s, void *define_opaque, TCCDefineFunc *define_func); LIBTCCAPI const char *tcc_token_to_str(int v); LIBTCCAPI char *tcc_macro_to_str(const int *a); LIBTCCAPI int tcc_ctype_array_size(const CType *ctype); LIBTCCAPI int tcc_sym_token(Sym *s); LIBTCCAPI struct Sym *tcc_sym_next(Sym *s); LIBTCCAPI CType *tcc_sym_type(Sym *s); LIBTCCAPI long long tcc_sym_enum_val(Sym *s); #ifdef __cplusplus}#endif #endif
#ifndef LIBTCC_H
#define LIBTCC_H
#ifndef LIBTCCAPI
# define LIBTCCAPI
#endif
#ifdef __cplusplus
extern "C" {
#endif
/*****************************/
/* set custom allocator for all allocations (optional), NULL for default. */
typedef void *TCCReallocFunc(void *ptr, unsigned long size);
LIBTCCAPI void tcc_set_realloc(TCCReallocFunc *my_realloc);
/*****************************/
typedef struct TCCState TCCState;
/* create a new TCC compilation context */
LIBTCCAPI TCCState *tcc_new(void);
/* free a TCC compilation context */
LIBTCCAPI void tcc_delete(TCCState *s);
/* set CONFIG_TCCDIR at runtime */
LIBTCCAPI void tcc_set_lib_path(TCCState *s, const char *path);
/* set error/warning callback (optional) */
typedef void TCCErrorFunc(void *opaque, const char *msg);
LIBTCCAPI void tcc_set_error_func(TCCState *s, void *error_opaque, TCCErrorFunc *error_func);
/* set options as from command line (multiple supported) */
LIBTCCAPI int tcc_set_options(TCCState *s, const char *str);
/*****************************/
/* preprocessor */
/* add include path */
LIBTCCAPI int tcc_add_include_path(TCCState *s, const char *pathname);
/* add in system include path */
LIBTCCAPI int tcc_add_sysinclude_path(TCCState *s, const char *pathname);
/* define preprocessor symbol 'sym'. value can be NULL, sym can be "sym=val" */
LIBTCCAPI void tcc_define_symbol(TCCState *s, const char *sym, const char *value);
/* undefine preprocess symbol 'sym' */
LIBTCCAPI void tcc_undefine_symbol(TCCState *s, const char *sym);
/*****************************/
/* compiling */
/* add a file (C file, dll, object, library, ld script). Return -1 if error. */
LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename);
/* compile a string containing a C source. Return -1 if error. */
LIBTCCAPI int tcc_compile_string(TCCState *s, const char *buf);
/* Tip: to have more specific errors/warnings from tcc_compile_string(),
you can prefix the string with "#line <num> \"<filename>\"\n" */
/*****************************/
/* linking commands */
/* set output type. MUST BE CALLED before any compilation */
LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type);
#define TCC_OUTPUT_MEMORY 1 /* output will be run in memory */
#define TCC_OUTPUT_EXE 2 /* executable file */
#define TCC_OUTPUT_DLL 4 /* dynamic library */
#define TCC_OUTPUT_OBJ 3 /* object file */
#define TCC_OUTPUT_PREPROCESS 5 /* only preprocess */
/* equivalent to -Lpath option */
LIBTCCAPI int tcc_add_library_path(TCCState *s, const char *pathname);
/* the library name is the same as the argument of the '-l' option */
LIBTCCAPI int tcc_add_library(TCCState *s, const char *libraryname);
/* add a symbol to the compiled program */
LIBTCCAPI int tcc_add_symbol(TCCState *s, const char *name, const void *val);
/* output an executable, library or object file. DO NOT call
tcc_relocate() before. */
LIBTCCAPI int tcc_output_file(TCCState *s, const char *filename);
/* link and run main() function and return its value. DO NOT call
tcc_relocate() before. */
LIBTCCAPI int tcc_run(TCCState *s, int argc, char **argv);
/* do all relocations (needed before using tcc_get_symbol()) */
LIBTCCAPI int tcc_relocate(TCCState *s1);
/* return symbol value or NULL if not found */
LIBTCCAPI void *tcc_get_symbol(TCCState *s, const char *name);
/* list all (global) symbols and their values via 'symbol_cb()' */
LIBTCCAPI void tcc_list_symbols(TCCState *s, void *ctx,
void (*symbol_cb)(void *ctx, const char *name, const void *val));
/* experimental/advanced section (see libtcc_test_mt.c for an example) */
/* catch runtime exceptions (optionally limit backtraces at top_func),
when using tcc_set_options("-bt") and when not using tcc_run() */
LIBTCCAPI void *_tcc_setjmp(TCCState *s1, void *jmp_buf, void *top_func, void *longjmp);
#define tcc_setjmp(s1,jb,f) setjmp(_tcc_setjmp(s1, jb, f, longjmp))
/* custom error printer for runtime exceptions. Returning 0 stops backtrace */
typedef int TCCBtFunc(void *udata, void *pc, const char *file, int line, const char* func, const char *msg);
LIBTCCAPI void tcc_set_backtrace_func(TCCState *s1, void* userdata, TCCBtFunc*);
/* The current value can be: */
#define VT_VALMASK 0x003f /* mask for value location, register or: */
#define VT_CONST 0x0030 /* constant in vc (must be first non register value) */
#define VT_LLOCAL 0x0031 /* lvalue, offset on stack */
#define VT_LOCAL 0x0032 /* offset on stack */
#define VT_CMP 0x0033 /* the value is stored in processor flags (in vc) */
#define VT_JMP 0x0034 /* value is the consequence of jmp true (even) */
#define VT_JMPI 0x0035 /* value is the consequence of jmp false (odd) */
#define VT_LVAL 0x0100 /* var is an lvalue */
#define VT_SYM 0x0200 /* a symbol value is added */
#define VT_MUSTCAST 0x0C00 /* value must be casted to be correct (used for
char/short stored in integer registers) */
#define VT_NONCONST 0x1000 /* VT_CONST, but not an (C standard) integer
constant expression */
#define VT_MUSTBOUND 0x4000 /* bound checking must be done before
dereferencing value */
#define VT_BOUNDED 0x8000 /* value is bounded. The address of the
bounding function call point is in vc */
/* types */
#define VT_BTYPE 0x000f /* mask for basic type */
#define VT_VOID 0 /* void type */
#define VT_BYTE 1 /* signed byte type */
#define VT_SHORT 2 /* short type */
#define VT_INT 3 /* integer type */
#define VT_LLONG 4 /* 64 bit integer */
#define VT_PTR 5 /* pointer */
#define VT_FUNC 6 /* function type */
#define VT_STRUCT 7 /* struct/union definition */
#define VT_FLOAT 8 /* IEEE float */
#define VT_DOUBLE 9 /* IEEE double */
#define VT_LDOUBLE 10 /* IEEE long double */
#define VT_BOOL 11 /* ISOC99 boolean type */
#define VT_QLONG 13 /* 128-bit integer. Only used for x86-64 ABI */
#define VT_QFLOAT 14 /* 128-bit float. Only used for x86-64 ABI */
#define VT_UNSIGNED 0x0010 /* unsigned type */
#define VT_DEFSIGN 0x0020 /* explicitly signed or unsigned */
#define VT_ARRAY 0x0040 /* array type (also has VT_PTR) */
#define VT_BITFIELD 0x0080 /* bitfield modifier */
#define VT_CONSTANT 0x0100 /* const modifier */
#define VT_VOLATILE 0x0200 /* volatile modifier */
#define VT_VLA 0x0400 /* VLA type (also has VT_PTR and VT_ARRAY) */
#define VT_LONG 0x0800 /* long type (also has VT_INT rsp. VT_LLONG) */
/* storage */
#define VT_EXTERN 0x00001000 /* extern definition */
#define VT_STATIC 0x00002000 /* static variable */
#define VT_TYPEDEF 0x00004000 /* typedef definition */
#define VT_INLINE 0x00008000 /* inline definition */
#define VT_TLS 0x00010000 /* thread-local storage */
/* currently unused: 0x000[248]0000 */
#define VT_STRUCT_SHIFT 20 /* shift for bitfield shift values (32 - 2*6) */
#define VT_STRUCT_MASK (((1U << (6+6)) - 1) << VT_STRUCT_SHIFT | VT_BITFIELD)
#define BIT_POS(t) (((t) >> VT_STRUCT_SHIFT) & 0x3f)
#define BIT_SIZE(t) (((t) >> (VT_STRUCT_SHIFT + 6)) & 0x3f)
#define VT_UNION (1 << VT_STRUCT_SHIFT | VT_STRUCT)
#define VT_ENUM (2 << VT_STRUCT_SHIFT) /* integral type is an enum really */
#define VT_ENUM_VAL (3 << VT_STRUCT_SHIFT) /* integral type is an enum constant really */
#define IS_ENUM(t) ((t & VT_STRUCT_MASK) == VT_ENUM)
#define IS_ENUM_VAL(t) ((t & VT_STRUCT_MASK) == VT_ENUM_VAL)
#define IS_UNION(t) ((t & (VT_STRUCT_MASK|VT_BTYPE)) == VT_UNION)
#define VT_ATOMIC VT_VOLATILE
/* type mask (except storage) */
#define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE | VT_TLS)
#define VT_TYPE (~(VT_STORAGE|VT_STRUCT_MASK))
/* symbol was created by tccasm.c first */
#define VT_ASM (VT_VOID | 4 << VT_STRUCT_SHIFT)
#define VT_ASM_FUNC (VT_VOID | 5 << VT_STRUCT_SHIFT)
#define IS_ASM_SYM(sym) (((sym)->type.t & ((VT_BTYPE|VT_STRUCT_MASK) & ~(1<<VT_STRUCT_SHIFT))) == VT_ASM)
#define IS_ASM_FUNC(t) ((t & (VT_BTYPE|VT_STRUCT_MASK)) == VT_ASM_FUNC)
/* base type is array (from typedef/typeof) */
#define VT_BT_ARRAY (6 << VT_STRUCT_SHIFT)
#define IS_BT_ARRAY(t) ((t & VT_STRUCT_MASK) == VT_BT_ARRAY)
#define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
#define SYM_FIELD 0x20000000 /* struct/union field symbol space */
#define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */
/* field 'Sym.t' for macros */
#define MACRO_OBJ 0 /* object like macro */
#define MACRO_FUNC 1 /* function like macro */
#define MACRO_JOIN 2 /* macro uses ## */
typedef struct Sym Sym;
/* type definition */
typedef struct CType {
int t;
struct Sym *ref;
} CType;
typedef void TCCDeclFunc(void *opaque, const char *file, int line, int v, int l, CType *type);
LIBTCCAPI void tcc_set_decl_func(TCCState *s, void *decl_opaque, TCCDeclFunc *decl_func);
typedef void TCCDefineFunc(void *opaque, const char *file, int line, int v, int macro_type, int *str);
LIBTCCAPI void tcc_set_define_func(TCCState *s, void *define_opaque, TCCDefineFunc *define_func);
LIBTCCAPI const char *tcc_token_to_str(int v);
LIBTCCAPI char *tcc_macro_to_str(const int *a);
LIBTCCAPI int tcc_ctype_array_size(const CType *ctype);
LIBTCCAPI int tcc_sym_token(Sym *s);
LIBTCCAPI struct Sym *tcc_sym_next(Sym *s);
LIBTCCAPI CType *tcc_sym_type(Sym *s);
LIBTCCAPI long long tcc_sym_enum_val(Sym *s);
#ifdef __cplusplus
}
#endif
#endif