X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-psi;a=blobdiff_plain;f=src%2Fdata.h;h=37658cc1f1794c82c94c38aeda74745e9e45441c;hp=57bc5c5ac2b8849b4f6b20ada0667b9b3bcc28c7;hb=fb8f7887c289ae74d6e8dd85d55ae09e6796e890;hpb=18ca609e4fa08a1c8fcdbb58e9aeb5fe55538b3c diff --git a/src/data.h b/src/data.h index 57bc5c5..37658cc 100644 --- a/src/data.h +++ b/src/data.h @@ -29,18 +29,89 @@ #include "types.h" #include "error.h" #include "plist.h" +#include "validate.h" + +#include +#include #define PSI_DEBUG 0x1 #define PSI_SILENT 0x2 -#include +#ifndef RTLD_NEXT +# define RTLD_NEXT ((void *) -1l) +#endif +#ifndef RTLD_DEFAULT +# define RTLD_DEFAULT ((void *) 0) +#endif + +#if PSI_THREADED_PARSER +zend_string *psi_string_init_interned(const char *buf, size_t len, int p); +zend_string *psi_new_interned_string(zend_string *str); +#else +# define psi_string_init_interned zend_string_init_interned +# define psi_new_interned_string zend_new_interned_string +#endif -#define PSI_DEBUG_PRINT(ctx, msg, ...) do { \ +static inline void *psi_dlsym(struct psi_plist *dllist, const char *name, const char *redir) +{ + void *dl, *sym = NULL; + const char *test = redir ?: name; + +again: + if (dllist) { + size_t i = 0; + + while (!sym && psi_plist_get(dllist, i++, &dl)) { + sym = dlsym(dl, test); + } + } + if (!sym) { + sym = dlsym(RTLD_DEFAULT, test); + } + if (!sym && test == redir) { + test = name; + goto again; + } + + return sym; +} + +#define PSI_DEBUG_PRINT(ctx, ...) do { \ if ((ctx) && (PSI_DATA(ctx)->flags & PSI_DEBUG)) { \ - fprintf(stderr, msg, __VA_ARGS__); \ + dprintf(PSI_DATA(ctx)->debug_fd, __VA_ARGS__); \ } \ } while(0) +#define PSI_DEBUG_PRINTV(ctx, msg, argv) do { \ + if ((ctx) && (PSI_DATA(ctx)->flags & PSI_DEBUG)) { \ + vdprintf(PSI_DATA(ctx)->debug_fd, msg, argv); \ + } \ +} while(0) +#define PSI_DEBUG_DUMP(ctx, dump_func, ...) do { \ + if ((ctx) && (PSI_DATA(ctx)->flags & PSI_DEBUG)) { \ + struct psi_dump dump_ = {{ .fd = PSI_DATA(ctx)->debug_fd}, \ + .fun = (psi_dump_cb) dprintf}; \ + dump_func(&dump_, __VA_ARGS__); \ + } \ +} while (0) +union psi_dump_arg { + void *hn; + int fd; +}; +typedef int (*psi_dump_cb)(union psi_dump_arg, const char *msg, ...); +struct psi_dump { + union psi_dump_arg ctx; + psi_dump_cb fun; +}; +#define PSI_DUMP(dump, ...) do { \ + struct psi_dump _dump_tmp, *_dump_ptr = dump; \ + if (!_dump_ptr) { \ + _dump_ptr = &_dump_tmp; \ + _dump_tmp.ctx.fd = STDOUT_FILENO; \ + _dump_tmp.fun = (psi_dump_cb) dprintf; \ + } \ + _dump_ptr->fun(_dump_ptr->ctx, __VA_ARGS__); \ +} while(0) #define PSI_DATA(D) ((struct psi_data *) (D)) @@ -52,12 +123,13 @@ struct psi_plist *unions; \ struct psi_plist *enums; \ struct psi_plist *decls; \ + struct psi_plist *vars; \ struct psi_plist *impls; \ - struct psi_plist *libs; \ psi_error_cb error; \ char last_error[0x1000]; \ unsigned errors; \ - unsigned flags + unsigned flags; \ + int debug_fd struct psi_data { PSI_DATA_MEMBERS; @@ -66,56 +138,7 @@ struct psi_data { struct psi_data *psi_data_ctor(struct psi_data *data, psi_error_cb error, unsigned flags); struct psi_data *psi_data_ctor_with_dtors(struct psi_data *data, psi_error_cb error, unsigned flags); struct psi_data *psi_data_exchange(struct psi_data *dest, struct psi_data *src); -bool psi_data_validate(struct psi_data *dst, struct psi_data *src); void psi_data_dtor(struct psi_data *data); -void psi_data_dump(int fd, struct psi_data *data); - -struct psi_validate_stack { - HashTable types; - HashTable structs; - HashTable unions; -}; - -static inline void psi_validate_stack_ctor(struct psi_validate_stack *stack) -{ - zend_hash_init(&stack->types, 0, NULL, NULL, 0); - zend_hash_init(&stack->structs, 0, NULL, NULL, 0); - zend_hash_init(&stack->unions, 0, NULL, NULL, 0); -} - -static inline void psi_validate_stack_dtor(struct psi_validate_stack *stack) -{ - zend_hash_destroy(&stack->types); - zend_hash_destroy(&stack->structs); - zend_hash_destroy(&stack->unions); -} - -#define psi_validate_stack_has_type(s, t) \ - ((s) ? zend_hash_str_exists(&(s)->types, (t), strlen(t)) : false) -#define psi_validate_stack_has_struct(s, t) \ - ((s) ? zend_hash_str_exists(&(s)->structs, (t), strlen(t)) : false) -#define psi_validate_stack_has_union(s, t) \ - ((s) ? zend_hash_str_exists(&(s)->unions, (t), strlen(t)) : false) - -#define psi_validate_stack_add_type(s, t, p) \ - do { if (s) zend_hash_str_add_ptr(&(s)->types, (t), strlen(t), (p)); } while(0) -#define psi_validate_stack_add_struct(s, t, p) \ - do { if (s) zend_hash_str_add_ptr(&(s)->structs, (t), strlen(t), (p)); } while(0) -#define psi_validate_stack_add_union(s, t, p) \ - do { if (s) zend_hash_str_add_ptr(&(s)->unions, (t), strlen(t), (p)); } while(0) - -#define psi_validate_stack_get_type(s, t) \ - ((s) ? zend_hash_str_find_ptr(&(s)->types, (t), strlen(t)) : NULL) -#define psi_validate_stack_get_struct(s, t) \ - ((s) ? zend_hash_str_find_ptr(&(s)->structs, (t), strlen(t)) : NULL) -#define psi_validate_stack_get_union(s, t) \ - ((s) ? zend_hash_str_find_ptr(&(s)->unions, (t), strlen(t)) : NULL) - -#define psi_validate_stack_del_type(s, t) \ - do { if (s) zend_hash_str_del(&(s)->types, (t), strlen(t)); } while(0) -#define psi_validate_stack_del_struct(s, t) \ - do { if (s) zend_hash_str_del(&(s)->structs, (t), strlen(t)); } while(0) -#define psi_validate_stack_del_union(s, t) \ - do { if (s) zend_hash_str_del(&(s)->unions, (t), strlen(t)); } while(0) +void psi_data_dump(struct psi_dump *dump, struct psi_data *data); #endif