A dynamic tracer for Linux

node.h 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #ifndef _PLY_NODE_H
  2. #define _PLY_NODE_H
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include "type.h"
  6. typedef struct node node_t;
  7. struct cons {
  8. node_t *car;
  9. node_t *cdr;
  10. };
  11. typedef enum op {
  12. OP_AGG = '@',
  13. OP_CALL = '(',
  14. OP_DEREF = '*',
  15. OP_DOT = '.',
  16. OP_MAP = '{',
  17. } op_t;
  18. typedef enum ntype {
  19. N_CONS,
  20. N_OP,
  21. N_IDENT,
  22. N_NUM,
  23. N_STRING,
  24. } ntype_t;
  25. struct node {
  26. ntype_t ntype;
  27. union {
  28. /* atom_t atom; */
  29. struct cons cons;
  30. op_t op;
  31. char *ident;
  32. int64_t num;
  33. char *string;
  34. };
  35. node_t *up;
  36. type_t *type;
  37. };
  38. /* debug */
  39. typedef struct ndump_info {
  40. FILE *fp;
  41. int indent;
  42. } ndump_info_t;
  43. void ndump(node_t *n, ndump_info_t *info);
  44. typedef int (*walk_fn)(node_t *, void *);
  45. int nwalk(node_t *n, walk_fn pre, walk_fn post, void *ctx);
  46. /* high-level constructors */
  47. node_t *ncall(char *name, node_t *args);
  48. node_t *nmap (char *name, node_t *key);
  49. /* basic constructors */
  50. node_t *ncons (node_t *car, node_t *cdr);
  51. node_t *nop (op_t op);
  52. node_t *nident (char *name);
  53. node_t *nnum (int64_t num);
  54. node_t *nstring(char *string);
  55. /* utilities */
  56. static inline int nop_is(node_t *n, op_t op)
  57. {
  58. if (!n || (n->ntype != N_OP))
  59. return 0;
  60. return n->op == op;
  61. }
  62. static inline node_t *ncar(node_t *n)
  63. {
  64. if (!n || (n->ntype != N_CONS))
  65. return NULL;
  66. return n->cons.car;
  67. }
  68. static inline node_t *ncdr(node_t *n)
  69. {
  70. if (!n || (n->ntype != N_CONS))
  71. return NULL;
  72. return n->cons.cdr;
  73. }
  74. static inline node_t *nup(node_t *n)
  75. {
  76. return n? n->up : NULL;
  77. }
  78. #endif /* _PLY_NODE_H */