History

[NOMERGE] libtcc: add tcc_set_{decl,define}_func to call a func on every decl/define

This can be used by StreetLISP in order to automatically generate bindings for C libraries at run time rather than compile time.

Sigrid Solveig Haflínudóttir committed 10 months ago

Commit b7791f61 parent 85ba3ae5 files · +36 −2Browse files

Unified · Split

5 files
  1. libtcc.c +12 −0
  2. libtcc.h +7 −0
  3. tcc.h +6 −0
  4. tccgen.c +6 −1
  5. tccpp.c +5 −1
libtcc.c+12−0
@@ -704,6 +704,18 @@ LIBTCCAPI void tcc_set_error_func(TCCState *s, void *error_opaque, TCCErrorFunc · 703 unmodified lines
704704 s->error_func = error_func;
705705 }
706706
707+LIBTCCAPI void tcc_set_decl_func(TCCState *s, void *decl_opaque, TCCDeclFunc *decl_func)
708+{
709+ s->decl_opaque = decl_opaque;
710+ s->decl_func = decl_func;
711+}
712+
713+LIBTCCAPI void tcc_set_define_func(TCCState *s, void *define_opaque, TCCDefineFunc *define_func)
714+{
715+ s->define_opaque = define_opaque;
716+ s->define_func = define_func;
717+}
718+
707719 /* error without aborting current compilation */
708720 PUB_FUNC int _tcc_error_noabort(const char *fmt, ...)
709721 {
libtcc.h+7−0
@@ -30,6 +30,13 @@ LIBTCCAPI void tcc_set_lib_path(TCCState *s, const char *path); · 29 unmodified lines
3030 typedef void TCCErrorFunc(void *opaque, const char *msg);
3131 LIBTCCAPI void tcc_set_error_func(TCCState *s, void *error_opaque, TCCErrorFunc *error_func);
3232
33+typedef struct CType CType;
34+typedef void TCCDeclFunc(void *opaque, const char *file, int line, int v, int l, CType *type);
35+LIBTCCAPI void tcc_set_decl_func(TCCState *s, void *decl_opaque, TCCDeclFunc *decl_func);
36+
37+typedef void TCCDefineFunc(void *opaque, const char *file, int line, int v, int macro_type, int *str);
38+LIBTCCAPI void tcc_set_define_func(TCCState *s, void *define_opaque, TCCDefineFunc *define_func);
39+
3340 /* set options as from command line (multiple supported) */
3441 LIBTCCAPI int tcc_set_options(TCCState *s, const char *str);
3542
tcc.h+6−0
@@ -843,6 +843,12 @@ struct TCCState { · 842 unmodified lines
843843 /* -include options */
844844 CString cmdline_incl;
845845
846+ /* user callbacks */
847+ void *decl_opaque;
848+ void (*decl_func)(void *opaque, const char *filename, int line, int v, int l, CType *type);
849+ void *define_opaque;
850+ void (*define_func)(void *opaque, const char *filename, int line, int v, int macro_type, int *str);
851+
846852 /* error handling */
847853 void *error_opaque;
848854 void (*error_func)(void *opaque, const char *msg);
tccgen.c+6−1
@@ -8790,9 +8790,10 @@ static int decl(int l) · 8789 unmodified lines
87908790
87918791 while (1) { /* iterate thru each declaration */
87928792 type = btype;
8793− ad = adbase;
8793+ ad = adbase;
87948794 type_decl(&type, &ad, &v, l == VT_CMP ? TYPE_DIRECT | TYPE_PARAM : TYPE_DIRECT);
87958795 /*ptype("decl", &type, v);*/
8796+
87968797 if ((type.t & VT_BTYPE) == VT_FUNC) {
87978798 if ((type.t & VT_STATIC) && (l != VT_CONST))
87988799 tcc_error("function without file scope cannot be static");
@@ -8878,6 +8879,8 @@ static int decl(int l) · 79 unmodified lines
88788879 cur_text_section->sh_flags = text_section->sh_flags;
88798880 gen_function(sym);
88808881 }
8882+ if (tcc_state->decl_func)
8883+ tcc_state->decl_func(tcc_state->decl_opaque, file->filename, file->line_num, v, l, &type);
88818884 break;
88828885 } else {
88838886 has_init = 0;
@@ -8969,6 +8972,8 @@ static int decl(int l) · 85 unmodified lines
89698972 esym->st_value, esym->st_size, 1);
89708973 }
89718974 }
8975+ if (tcc_state->decl_func)
8976+ tcc_state->decl_func(tcc_state->decl_opaque, file->filename, file->line_num, v, l, &type);
89728977 if (tok != ',') {
89738978 if (l == VT_JMP)
89748979 return has_init ? v : 1;
tccpp.c+5−1
@@ -1266,6 +1266,7 @@ static int macro_is_equal(const int *a, const int *b) · 1265 unmodified lines
12661266 /* defines handling */
12671267 ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg)
12681268 {
1269+ TCCState *s1 = tcc_state;
12691270 Sym *s, *o;
12701271
12711272 o = define_find(v);
@@ -1274,8 +1275,11 @@ ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg) · 2 unmodified lines
12741275 s->next = first_arg;
12751276 table_ident[v - TOK_IDENT]->sym_define = s;
12761277
1278+ if (s1->define_func != NULL)
1279+ s1->define_func(s1->define_opaque, file->filename, file->line_num, v, macro_type, str);
1280+
12771281 if (o && !macro_is_equal(o->d, s->d))
1278− tcc_warning("%s redefined", get_tok_str(v, NULL));
1282+ tcc_warning("%s redefined", get_tok_str(v, NULL));
12791283 }
12801284
12811285 /* undefined a define symbol. Its name is just set to zero */

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