__int128 support
[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 #define PSI_T_CAST PSI_T_EQUALS
51 #define PSI_T_POINTER PSI_T_ASTERISK
52
53 #define PSI_T_WHITESPACE -PSI_T_NO_WHITESPACE
54 #define PSI_T_BSLASH -PSI_T_SLASH
55 #define PSI_T_LONG_DOUBLE -PSI_T_DOUBLE
56
57 #if SIZEOF_CHAR == SIZEOF_INT8_T
58 # define PSI_T_INT8 PSI_T_CHAR
59 # define PSI_T_UINT8 -PSI_T_CHAR
60 # define ALIGNOF_INT8_T ALIGNOF_CHAR
61 # define ALIGNOF_UINT8_T ALIGNOF_CHAR
62 #else
63 # error SIZEOF_CHAR != 8
64 #endif
65 #if SIZEOF_SHORT == SIZEOF_INT16_T
66 # define PSI_T_INT16 PSI_T_SHORT
67 # define PSI_T_UINT16 -PSI_T_SHORT
68 # define ALIGNOF_INT16_T ALIGNOF_SHORT
69 # define ALIGNOF_UINT16_T ALIGNOF_SHORT
70 #else
71 # error SIZEOF_SHORT != 16
72 #endif
73 #if SIZEOF_INT == SIZEOF_INT32_T
74 # define PSI_T_INT32 PSI_T_INT
75 # define PSI_T_UINT32 -PSI_T_INT
76 # define ALIGNOF_INT32_T ALIGNOF_INT
77 # define ALIGNOF_UINT32_T ALIGNOF_INT
78 #elif SIZEOF_LONG == SIZEOF_INT32_T
79 # define PSI_T_INT32 PSI_T_LONG
80 # define PSI_T_UINT32 -PSI_T_LONG
81 # define ALIGNOF_INT32_T ALIGNOF_LONG
82 # define ALIGNOF_UINT32_T ALIGNOF_LONG
83 #else
84 # error SIZEOF_INT != 32 and SIZEOF_LONG != 32
85 #endif
86 #if SIZEOF_LONG == SIZEOF_INT64_T
87 # define PSI_T_INT64 PSI_T_LONG
88 # define PSI_T_UINT64 -PSI_T_LONG
89 # define ALIGNOF_INT64_T ALIGNOF_LONG
90 # define ALIGNOF_UINT64_T ALIGNOF_LONG
91 # elif HAVE_LONG_LONG_INT && SIZEOF_LONG_LONG_INT == SIZEOF_INT64_T
92 # define PSI_T_INT64 (PSI_T_LONG << 0xa)
93 # define PSI_T_UINT64 -(PSI_T_LONG << 0xa)
94 # define ALIGNOF_INT64_T ALIGNOF_LONG_LONG
95 # define ALIGNOF_UINT64_T ALIGNOF_LONG_LONG
96 #else
97 # error SIZEOF_LONG != 64 and SIZEOF_LONG_LONG != 64
98 #endif
99
100 #if HAVE_INT128
101 # define PSI_T_INT128 (PSI_T_LONG << 0xb)
102 # define PSI_T_UINT128 -(PSI_T_LONG << 0xb)
103 # define SIZEOF_INT128_T SIZEOF___INT128
104 # define SIZEOF_UINT128_T SIZEOF_UNSIGNED___INT128
105 # define ALIGNOF_INT128_T ALIGNOF___INT128
106 # define ALIGNOF_UINT128_T ALIGNOF___INT128
107 # define INT128_MAX ((__int128) (UINT128_MAX >> 1))
108 # define INT128_MIN (-INT128_MAX-1)
109 # define UINT128_MAX ((unsigned __int128) ~ 0)
110 typedef __int128 int128_t;
111 typedef unsigned __int128 uint128_t;
112 #endif
113
114 typedef int64_t token_t;
115
116 static inline size_t psi_t_alignment(token_t t)
117 {
118 #define PSI_ALIGNOF(T) case PSI_T_## T: return ALIGNOF_## T ##_T;
119 switch (t) {
120 PSI_ALIGNOF(INT8);
121 PSI_ALIGNOF(UINT8);
122 PSI_ALIGNOF(INT16);
123 PSI_ALIGNOF(UINT16);
124 PSI_ALIGNOF(INT32);
125 PSI_ALIGNOF(UINT32);
126 PSI_ALIGNOF(INT64);
127 PSI_ALIGNOF(UINT64);
128 #if HAVE_INT128
129 PSI_ALIGNOF(INT128);
130 PSI_ALIGNOF(UINT128);
131 #endif
132 case PSI_T_FLOAT:
133 return ALIGNOF_FLOAT;
134 case PSI_T_DOUBLE:
135 return ALIGNOF_DOUBLE;
136 case PSI_T_POINTER:
137 case PSI_T_FUNCTION:
138 return ALIGNOF_VOID_P;
139 case PSI_T_ENUM:
140 return ALIGNOF_INT;
141 #ifdef HAVE_LONG_DOUBLE
142 case PSI_T_LONG_DOUBLE:
143 return ALIGNOF_LONG_DOUBLE;
144 #endif
145 default:
146 assert(0);
147 }
148 return 0;
149 }
150
151 static inline size_t psi_t_size(token_t t)
152 {
153 #define PSI_SIZEOF(T) case PSI_T_## T : return SIZEOF_## T ##_T;
154 switch (t) {
155 PSI_SIZEOF(INT8);
156 PSI_SIZEOF(UINT8);
157 PSI_SIZEOF(INT16);
158 PSI_SIZEOF(UINT16);
159 PSI_SIZEOF(INT32);
160 PSI_SIZEOF(UINT32);
161 PSI_SIZEOF(INT64);
162 PSI_SIZEOF(UINT64);
163 #if HAVE_INT128
164 PSI_SIZEOF(INT128);
165 PSI_SIZEOF(UINT128);
166 #endif
167 case PSI_T_FLOAT:
168 return SIZEOF_FLOAT;
169 case PSI_T_DOUBLE:
170 return SIZEOF_DOUBLE;
171 case PSI_T_VOID:
172 case PSI_T_POINTER:
173 case PSI_T_FUNCTION:
174 return SIZEOF_VOID_P;
175 case PSI_T_ENUM:
176 return SIZEOF_INT;
177 #ifdef HAVE_LONG_DOUBLE
178 case PSI_T_LONG_DOUBLE:
179 return SIZEOF_LONG_DOUBLE;
180 #endif
181 default:
182 assert(!t);
183 }
184 return 0;
185 }
186
187 static inline const char *psi_t_indent(unsigned level) {
188 static const char indent[] =
189 "\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";
190 return &indent[32 - MIN(32, level)];
191 }
192
193 static inline const char *psi_t_indirection(unsigned pointer_level) {
194 static const char indir[] = "********************************";
195 return &indir[32 - MIN(32, pointer_level)];
196 }
197
198 struct psi_token {
199 token_t type;
200 unsigned size, line, col, flags;
201 char *text, *file;
202 char buf[1];
203 };
204
205 struct psi_parser;
206
207 struct psi_token *psi_token_init(token_t token_typ, const char *token_txt,
208 size_t token_len, unsigned col, unsigned line, const char *file);
209 size_t psi_token_alloc_size(size_t token_len, size_t fname_len);
210 struct psi_token *psi_token_copy(struct psi_token *src);
211 void psi_token_copy_ctor(struct psi_token **src);
212 struct psi_token *psi_token_cat(const char *sep, unsigned argc, ...);
213 struct psi_token *psi_token_prepend(const char *sep, struct psi_token *T, unsigned argc, ...);
214 struct psi_token *psi_token_append(const char *sep, struct psi_token *T, unsigned argc, ...);
215 struct psi_token *psi_token_translit(struct psi_token *T, char *from, char *to);
216 uint64_t psi_token_hash(struct psi_token *t, char *digest_buf);
217 void psi_token_dump(int fd, struct psi_token *t);
218 void psi_token_free(struct psi_token **token);
219
220 #endif