gdbinit
[m6w6/ext-psi] / src / token.h
1 /*******************************************************************************
2 Copyright (c) 2016, Michael Wallner <mike@php.net>.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
18 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *******************************************************************************/
25
26 #ifndef PSI_TOKEN_H
27 #define PSI_TOKEN_H
28
29 #include <stddef.h>
30 #include <stdarg.h>
31 #include <stdint.h>
32 #include <assert.h>
33 #include <sys/param.h>
34
35 static inline size_t psi_align(size_t s, size_t a) {
36 return ((s - 1) | (a - 1)) + 1;
37 }
38
39 static inline size_t psi_offset_padding(size_t diff, size_t alignment) {
40 if (diff && diff <= psi_align(diff, alignment)) {
41 diff = 0;
42 }
43
44 return diff;
45 }
46
47 #include "parser_proc.h"
48 #undef YYDEBUG
49
50 #if SIZEOF_CHAR == 1
51 # define ALIGNOF_INT8_T ALIGNOF_CHAR
52 # define ALIGNOF_UINT8_T ALIGNOF_CHAR
53 #else
54 # error SIZEOF_CHAR != 8
55 #endif
56 #if SIZEOF_SHORT == 2
57 # define ALIGNOF_INT16_T ALIGNOF_SHORT
58 # define ALIGNOF_UINT16_T ALIGNOF_SHORT
59 #else
60 # error SIZEOF_SHORT != 16
61 #endif
62 #if SIZEOF_INT == 4
63 # define ALIGNOF_INT32_T ALIGNOF_INT
64 # define ALIGNOF_UINT32_T ALIGNOF_INT
65 #elif SIZEOF_LONG == 4
66 # define ALIGNOF_INT32_T ALIGNOF_LONG
67 # define ALIGNOF_UINT32_T ALIGNOF_LONG
68 #else
69 # error SIZEOF_INT != 32 and SIZEOF_LONG != 32
70 #endif
71 #if SIZEOF_LONG == 8
72 # define ALIGNOF_INT64_T ALIGNOF_LONG
73 # define ALIGNOF_UINT64_T ALIGNOF_LONG
74 # elif HAVE_LONG_LONG_INT && SIZEOF_LONG_LONG_INT == 8
75 # define ALIGNOF_INT64_T ALIGNOF_LONG_LONG
76 # define ALIGNOF_UINT64_T ALIGNOF_LONG_LONG
77 #else
78 # error SIZEOF_LONG != 64 and SIZEOF_LONG_LONG != 64
79 #endif
80
81 #if HAVE_INT128
82 # define SIZEOF_INT128_T SIZEOF___INT128
83 # define SIZEOF_UINT128_T SIZEOF_UNSIGNED___INT128
84 # define ALIGNOF_INT128_T ALIGNOF___INT128
85 # define ALIGNOF_UINT128_T ALIGNOF___INT128
86 # define INT128_MAX ((__int128) (UINT128_MAX >> 1))
87 # define INT128_MIN (-INT128_MAX-1)
88 # define UINT128_MAX ((unsigned __int128) ~ 0)
89 typedef __int128 int128_t;
90 typedef unsigned __int128 uint128_t;
91 #endif
92
93 typedef enum psi_token_type token_t;
94
95 static inline size_t psi_t_alignment(token_t t)
96 {
97 #define PSI_ALIGNOF(T,S) case PSI_T_## T ##S: return S/8;
98 switch (t) {
99 PSI_ALIGNOF(INT,8);
100 PSI_ALIGNOF(UINT,8);
101 PSI_ALIGNOF(INT,16);
102 PSI_ALIGNOF(UINT,16);
103 PSI_ALIGNOF(INT,32);
104 PSI_ALIGNOF(UINT,32);
105 PSI_ALIGNOF(INT,64);
106 PSI_ALIGNOF(UINT,64);
107 #if HAVE_INT128
108 PSI_ALIGNOF(INT,128);
109 PSI_ALIGNOF(UINT,128);
110 #endif
111 case PSI_T_FLOAT:
112 return ALIGNOF_FLOAT;
113 case PSI_T_DOUBLE:
114 return ALIGNOF_DOUBLE;
115 case PSI_T_POINTER:
116 case PSI_T_FUNCTION:
117 return ALIGNOF_VOID_P;
118 case PSI_T_ENUM:
119 return ALIGNOF_INT;
120 #ifdef HAVE_LONG_DOUBLE
121 case PSI_T_LONG_DOUBLE:
122 return ALIGNOF_LONG_DOUBLE;
123 #endif
124 default:
125 assert(0);
126 }
127 return 0;
128 }
129
130 static inline size_t psi_t_size(token_t t)
131 {
132 #define PSI_SIZEOF(T,S) case PSI_T_## T ##S : return S/8;
133 switch (t) {
134 PSI_SIZEOF(INT,8);
135 PSI_SIZEOF(UINT,8);
136 PSI_SIZEOF(INT,16);
137 PSI_SIZEOF(UINT,16);
138 PSI_SIZEOF(INT,32);
139 PSI_SIZEOF(UINT,32);
140 PSI_SIZEOF(INT,64);
141 PSI_SIZEOF(UINT,64);
142 #if HAVE_INT128
143 PSI_SIZEOF(INT,128);
144 PSI_SIZEOF(UINT,128);
145 #endif
146 case PSI_T_FLOAT:
147 return SIZEOF_FLOAT;
148 case PSI_T_DOUBLE:
149 return SIZEOF_DOUBLE;
150 case PSI_T_VOID:
151 case PSI_T_POINTER:
152 case PSI_T_FUNCTION:
153 return SIZEOF_VOID_P;
154 case PSI_T_ENUM:
155 return SIZEOF_INT;
156 #ifdef HAVE_LONG_DOUBLE
157 case PSI_T_LONG_DOUBLE:
158 return SIZEOF_LONG_DOUBLE;
159 #endif
160 default:
161 assert(!t);
162 }
163 return 0;
164 }
165
166 static inline const char *psi_t_indent(unsigned level) {
167 static const char indent[] =
168 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
169 return &indent[32 - MIN(32, level)];
170 }
171
172 static inline const char *psi_t_indirection(unsigned pointer_level) {
173 static const char indir[] = "********************************";
174 return &indir[32 - MIN(32, pointer_level)];
175 }
176
177 struct psi_token {
178 token_t type;
179 unsigned size, line, col, flags;
180 char *text, *file;
181 char buf[1];
182 };
183
184 struct psi_parser;
185
186 struct psi_token *psi_token_init(token_t token_typ, const char *token_txt,
187 size_t token_len, unsigned col, unsigned line, const char *file);
188 size_t psi_token_alloc_size(size_t token_len, size_t fname_len);
189 struct psi_token *psi_token_copy(struct psi_token *src);
190 void psi_token_copy_ctor(struct psi_token **src);
191 struct psi_token *psi_token_cat(const char *sep, unsigned argc, ...);
192 struct psi_token *psi_token_prepend(const char *sep, struct psi_token *T, unsigned argc, ...);
193 struct psi_token *psi_token_append(const char *sep, struct psi_token *T, unsigned argc, ...);
194 struct psi_token *psi_token_translit(struct psi_token *T, char *from, char *to);
195 uint64_t psi_token_hash(struct psi_token *t, char *digest_buf);
196 void psi_token_dump(int fd, struct psi_token *t);
197 void psi_token_free(struct psi_token **token);
198
199 #endif