tcutil.h (5762B)
1 /* toolchain utility library: memory, paths, containers, option parsing */ 2 3 #ifndef ARUU_TCUTIL_H 4 #define ARUU_TCUTIL_H 5 6 #include <stddef.h> /* size_t */ 7 #include <stdint.h> /* intptr_t */ 8 #include <stdio.h> /* FILE */ 9 #include <sys/types.h> /* ssize_t */ 10 11 /* macros also defined by shared/util.h, undef to avoid redefinition warning */ 12 #undef MIN 13 #undef MAX 14 #undef ALIGN 15 16 #define MIN(a, b) ((a) < (b) ? (a) : (b)) 17 #define MAX(a, b) ((a) > (b) ? (a) : (b)) 18 #define ALIGN(x, align) (((x) + (align) - 1) & -(align)) /* align must be power of 2 */ 19 #define UNUSED(x) ((void)(x)) 20 #define IS_POWER_OF_2(x) ((x) > 0 && ((x) & ((x) - 1)) == 0) 21 #define INT2VOIDP(i) ((void *)(intptr_t)(i)) 22 #define UINT2VOIDP(i) ((void *)(uintptr_t)(i)) 23 #define VOIDP2INT(p) ((intptr_t)(p)) 24 #define VOIDP2UINT(p) ((uintptr_t)(p)) 25 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a))) 26 27 struct Name; 28 29 /* character classification */ 30 int isalnum_(int c); 31 int isutf8first(int c); 32 int isutf8follow(int c); 33 int isoctal(int c); 34 int xvalue(char c); 35 36 /* string and path helpers */ 37 int starts_with(const char *str, const char *prefix); 38 int is_fullpath(const char *filename); 39 char *join_paths(const char *paths[]); 40 char *get_ext(const char *filename); 41 char *change_ext(const char *path, const char *ext); 42 const char *skip_whitespaces(const char *s); 43 const char *block_comment_start(const char *p); 44 const char *block_comment_end(const char *p); 45 46 /* file I/O helpers */ 47 void *read_or_die(FILE *fp, void *buf, long offset, size_t size, const char *msg); 48 void put_padding(FILE *fp, long start); 49 int is_file(const char *path); 50 ssize_t getline_chomp(char **lineptr, size_t *n, FILE *stream); 51 ssize_t getline_cont(char **lineptr, size_t *n, FILE *stream, int *plineno); 52 53 /* memory: toolchain-local, same behavior as aruu emalloc/ecalloc/erealloc 54 * kept separate so the toolchain can build standalone without libutil */ 55 void *malloc_or_die(size_t size); 56 void *calloc_or_die(size_t size); 57 void *realloc_or_die(void *ptr, size_t size); 58 59 /* diagnostics */ 60 void show_version(const char *exe, int arch); 61 _Noreturn void error(const char *fmt, ...); 62 void show_error_line(const char *line, const char *p, int len); 63 64 /* value range checks for immediate operands */ 65 int is_im8(int64_t x); 66 int is_im16(int64_t x); 67 int is_im32(int64_t x); 68 int64_t wrap_value(int64_t value, int size, int is_unsigned); 69 int most_significant_bit(size_t x); 70 71 /* name interning (see table.h for struct name) */ 72 const struct Name *alloc_label(void); 73 74 /* container: growable pointer vector */ 75 struct Vector { 76 void **data; 77 int capacity; 78 int len; 79 }; 80 81 struct Vector *new_vector(void); 82 void free_vector(struct Vector *vec); 83 void vec_init(struct Vector *vec); 84 void vec_clear(struct Vector *vec); 85 void vec_push(struct Vector *vec, const void *elem); 86 void *vec_pop(struct Vector *vec); 87 void vec_insert(struct Vector *vec, int pos, const void *elem); 88 void vec_remove_at(struct Vector *vec, int index); 89 int vec_contains(struct Vector *vec, void *elem); 90 void vec_concat(struct Vector *dst, const struct Vector *src); 91 92 /* container: growable byte buffer */ 93 struct DataStorage { 94 struct Vector *chunk_stack; 95 unsigned char *buf; 96 size_t capacity; 97 size_t len; 98 }; 99 100 void data_release(struct DataStorage *data); 101 void data_init(struct DataStorage *data); 102 void data_reserve(struct DataStorage *data, size_t capacity); 103 void data_insert(struct DataStorage *data, ssize_t pos, const void *buf, size_t size); 104 void data_append(struct DataStorage *data, const void *buf, size_t size); 105 void data_push(struct DataStorage *data, unsigned char c); 106 void data_align(struct DataStorage *data, int align); 107 void data_concat(struct DataStorage *dst, struct DataStorage *src); 108 void data_leb128(struct DataStorage *data, ssize_t pos, int64_t val); 109 void data_uleb128(struct DataStorage *data, ssize_t pos, uint64_t val); 110 void data_string(struct DataStorage *data, const void *str, size_t len); 111 void data_open_chunk(struct DataStorage *data); 112 void data_close_chunk(struct DataStorage *data, ssize_t num); 113 void data_varint32(struct DataStorage *data, ssize_t pos, int64_t val); 114 void data_varuint32(struct DataStorage *data, ssize_t pos, uint64_t val); 115 116 /* container: rope-style string builder */ 117 struct StringBuffer { 118 struct Vector *elems; 119 }; 120 121 void sb_init(struct StringBuffer *sb); 122 void sb_clear(struct StringBuffer *sb); 123 int sb_empty(struct StringBuffer *sb); 124 void sb_insert(struct StringBuffer *sb, int pos, const char *start, const char *end); 125 char *sb_join(struct StringBuffer *sb, const char *separator); 126 127 static inline void 128 sb_append(struct StringBuffer *sb, const char *start, const char *end) 129 { 130 sb_insert(sb, sb->elems->len, start, end); 131 } 132 133 static inline void 134 sb_prepend(struct StringBuffer *sb, const char *start, const char *end) 135 { 136 sb_insert(sb, 0, start, end); 137 } 138 139 static inline char * 140 sb_to_string(struct StringBuffer *sb) 141 { 142 return sb_join(sb, NULL); 143 } 144 145 void escape_string(const char *str, size_t size, struct StringBuffer *sb); 146 147 /* convenience macro for join_paths */ 148 #define JOIN_PATHS(...) join_paths((const char *[]){__VA_ARGS__, NULL}) 149 150 /* option parser: long-option style for toolchain commands 151 * has_arg values: 0 = none, 1 = required, 2 = optional */ 152 #define no_argument 0 153 #define required_argument 1 154 #define optional_argument 2 155 156 struct option { 157 const char *name; 158 int has_arg; 159 int val; 160 }; 161 162 extern int optind; 163 extern int opterr; 164 extern int optopt; 165 extern char *optarg; 166 167 int optparse(int argc, char *const argv[], const struct option *opts); 168 169 #endif /* ARUU_TCUTIL_H */