GNU PROLOG with UTF8 support
arch_dep.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------*
2  * GNU Prolog *
3  * *
4  * Part : configuration *
5  * File : arch_dep.h *
6  * Descr.: architecture dependent features - Header file *
7  * Author: Daniel Diaz *
8  * *
9  * Copyright (C) 1999-2015 Daniel Diaz *
10  * *
11  * This file is part of GNU Prolog *
12  * *
13  * GNU Prolog is free software: you can redistribute it and/or *
14  * modify it under the terms of either: *
15  * *
16  * - the GNU Lesser General Public License as published by the Free *
17  * Software Foundation; either version 3 of the License, or (at your *
18  * option) any later version. *
19  * *
20  * or *
21  * *
22  * - the GNU General Public License as published by the Free *
23  * Software Foundation; either version 2 of the License, or (at your *
24  * option) any later version. *
25  * *
26  * or both in parallel, as here. *
27  * *
28  * GNU Prolog is distributed in the hope that it will be useful, *
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
31  * General Public License for more details. *
32  * *
33  * You should have received copies of the GNU General Public License and *
34  * the GNU Lesser General Public License along with this program. If *
35  * not, see http://www.gnu.org/licenses/. *
36  *-------------------------------------------------------------------------*/
37 
38 
39 #ifndef _ARCH_DEP_H
40 #define _ARCH_DEP_H
41 
42 #define CPP_STR1(s) #s
43 #define CPP_STR(s) CPP_STR1(s)
44 
45 #define CPP_CAT1(x, y) x ## y
46 #define CPP_CAT(x, y) CPP_CAT1(x, y)
47 
48 
49 /* C compiler version (for more general handling see http://sourceforge.net/projects/predef) */
50 
51 #if defined(__clang__) /* put before because also defines __GNUC__ */
52 
53 #define CC_MAJOR __clang_major__
54 #define CC_MINOR __clang_minor__
55 #define CC_PATCHLEVEL __clang_patchlevel__
56 
57 #elif defined(__GNUC__)
58 
59 #define CC_MAJOR __GNUC__
60 #define CC_MINOR __GNUC_MINOR__
61 #define CC_PATCHLEVEL __GNUC_PATCHLEVEL__
62 
63 #elif defined(_MSC_FULL_VER)
64 
65 #define CC_MAJOR (_MSC_FULL_VER / 1000000)
66 #define CC_MINOR (_MSC_FULL_VER % 1000000 / 10000)
67 #define CC_PATCHLEVEL (_MSC_FULL_VER % 10000)
68 
69 #elif defined(_MSC_VER)
70 
71 #define CC_MAJOR (_MSC_VER / 100)
72 #define CC_MINOR (_MSC_VER % 100)
73 #define CC_PATCHLEVEL 0
74 
75 #else
76 
77 #define CC_MAJOR 0
78 #define CC_MINOR 0
79 #define CC_PATCHLEVEL 0
80 
81 #endif
82 
83 
84 
85 /* Compile date */
86 
87 #if defined(__DATE__) && defined(__TIME__)
88 #define COMPILED_AT __DATE__ ", " __TIME__
89 #else
90 #define COMPILED_AT "unknown date"
91 #endif
92 
93 
94 
95 
96 #if defined(_WIN32) && !defined(__CYGWIN__)
97 
98 /* There are 2 kinds of MSVC warning C4996 one wants to remove:
99  * 1) XXX was declared deprecated ... This function or variable may be unsafe
100  * solution: #define _CRT_SECURE_NO_DEPRECATE 1
101  * 2) The POSIX name for this item is deprecated
102  * solution: #define _CRT_NONSTDC_NO_DEPRECATE 1
103  * However, these defines only work if they are before any #include <...>
104  * So: pass to cl: -D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE
105  * or deactivate the warning with the following pragma. We do both !
106  */
107 
108 #ifdef _MSC_VER
109 #pragma warning(disable : 4996)
110 #endif
111 
112 #define MAXPATHLEN 1024
113 #define SIGQUIT SIGTERM
114 #define fdopen _fdopen
115 #define dup _dup
116 #define dup2 _dup2
117 #define getcwd _getcwd
118 #define chdir _chdir
119 #define close _close
120 #define pclose _pclose
121 #define popen _popen
122 #define pclose _pclose
123 #define getpid _getpid
124 #define tempnam _tempnam
125 #define unlink _unlink
126 #define tzset _tzset
127 #define access _access
128 #ifdef _MSC_VER
129 #define strcasecmp stricmp
130 #define strncasecmp strnicmp
131 #define spawnvp _spawnvp
132 #endif
133 
134 #ifndef F_OK
135 #define F_OK 00
136 #define W_OK 02
137 #define R_OK 04
138 #define X_OK F_OK
139 #endif
140 
141 #ifndef S_ISDIR
142 #define S_ISDIR(m) (((m)&_S_IFMT) == _S_IFDIR)
143 #define S_ISCHR(m) (((m)&_S_IFMT) == _S_IFCHR)
144 #define S_ISFIFO(m) (((m)&_S_IFMT) == _S_IFIFO)
145 #define S_ISREG(m) (((m)&_S_IFMT) == _S_IFREG)
146 #endif
147 
148 #ifndef S_IRUSR
149 #define S_IRUSR _S_IREAD
150 #define S_IWUSR _S_IWRITE
151 #define S_IXUSR _S_IEXEC
152 #endif
153 
154 #define DIR_SEP_S "\\"
155 #define DIR_SEP_C '\\'
156 #define DIR_SEP_C_ALT '/'
157 
158 #elif defined(__CYGWIN__)
159 
160 #define DIR_SEP_S "/"
161 #define DIR_SEP_C '/'
162 #define DIR_SEP_C_ALT '\\'
163 
164 #else /* Unix */
165 
166 #define DIR_SEP_S "/"
167 #define DIR_SEP_C '/'
168 #define DIR_SEP_C_ALT '/'
169 
170 #endif
171 
172 #define Is_Dir_Sep(c) ((c) == DIR_SEP_C || (c) == DIR_SEP_C_ALT)
173 
174 
175 
176 #define Find_Last_Dir_Sep(_p, _path) \
177  do { \
178  char *_ptr; \
179  \
180  for((_p) = NULL, _ptr = (_path); *_ptr; _ptr++) \
181  if (Is_Dir_Sep(*_ptr)) \
182  (_p) = _ptr; \
183  } while(0)
184 
185 
186 
187 #define Has_Drive_Specif(str) \
188  (((*(str) >= 'a' && *(str) <= 'z') || (*(str) >= 'A' && *(str) <= 'Z')) && (str)[1] == ':')
189 
190 
191 
192 #if defined(M_ix86_cygwin) || defined(M_ix86_sco)
193 #define Set_Line_Buf(s) setvbuf(s, NULL, _IOLBF, 0)
194 #elif defined(_WIN32)
195 #define Set_Line_Buf(s) setbuf(s, NULL)
196 #else
197 #define Set_Line_Buf(s) setlinebuf(s)
198 #endif
199 
200 
201 #ifndef NO_USE_GUI_CONSOLE
202 #define W32_GUI_CONSOLE
203 #endif
204 
205 #ifdef M_sparc_sunos
206 #define __USE_FIXED_PROTOTYPES__
207 #endif
208 
209 
210 #if defined(M_ix86_sco)
211 
212 #ifndef MAXPATHLEN
213 #define MAXPATHLEN 1024
214 #endif
215 
216 #endif
217 
218 
219 
220 #if !defined(_WIN32) && !defined(__unix__)
221 #define __unix__
222 #endif
223 
224 #ifndef HAVE_FGETC
225 #define fgetc getc
226 #endif
227 
228 
229 #ifndef HAVE_SIGSETJMP
230 #define sigjmp_buf jmp_buf
231 #define sigsetjmp(jb, x) setjmp(jb)
232 #define siglongjmp longjmp
233 #endif
234 
235 #if defined(_WIN64) && !defined(_MSC_VER)
236 /* Mingw64-gcc implements setjmp with msvcrt's _setjmp. This _setjmp
237  * has an additional (hidden) argument. If it is NULL, longjmp will NOT do
238  * stack unwinding (needed for SEH). By default the the second argument is
239  * NOT null (it is $rsp), then longjmp will try a stack unwinding which will
240  * crash gprolog.
241  * NB: _setjmp stores this argument in the jmp_buf (in the first bytes)
242  * Mingw-gcc v < 4.6 fixed this at longjmp (before calling msvcrt's _longjmp)
243  * (see file: lib64_libmingwex_a-mingw_getsp.o in library libmingwex.a)
244  *
245  * 0000000000000006 <longjmp>: # x86_64 ABI: jmp_buf is in $rcx
246  * 6: 31 c0 xor %eax,%eax
247  * 8: 89 01 mov %eax,(%rcx) # set 0 in the first word of jmp_buf
248  * a: 48 8d 05 00 00 00 00 lea 0x0(%rip),%rax # this will call dll msvcrt's longjmp
249  * 11: ff 20 jmpq *(%rax)
250  *
251  * while in >= 4.6: (no more fixes)
252  *
253  * 0000000000000006 <longjmp>:
254  * 6: 48 8d 05 00 00 00 00 lea 0x0(%rip),%rax # this will call dll msvcrt's longjmp
255  * d: ff 20 jmpq *(%rax)
256  * f: 90 nop
257  *
258  */
259 #ifdef setjmp
260 #undef setjmp
261 #endif
262 #define setjmp(buf) _setjmp(buf, NULL)
263 #endif
264  /* Fast call macros */
265 #if defined(M_ix86)
266 
267 /* FC_MAX_ARGS_IN_REGS can be decreased (0, 1, 2) - but the inline_asm_data
268  * is compiled with 3, if changed, change inlined code in mapper */
269 
270 #define COULD_COMPILE_FOR_FC
271 #ifdef __GNUC__
272 #define FC_MAX_ARGS_IN_REGS 3
273 #define FC_SET_OF_REGISTERS { "%eax", "%edx", "%ecx" };
274 #define FC_ATTRIB __attribute__((regparm(FC_MAX_ARGS_IN_REGS)))
275 #elif 0 /* under MSVC++ we can use __fastcall convention (#elif 1 if wanted) */
276  /* see file ix86_any.c to see why it is not selected by default */
277 #define FC_MAX_ARGS_IN_REGS 2
278 #define FC_SET_OF_REGISTERS { "%ecx", "%edx" };
279 #define FC_ATTRIB __fastcall
280 #else
281 #define FC_MAX_ARGS_IN_REGS 0
282 #define FC_SET_OF_REGISTERS { NULL };
283 #define FC_ATTRIB
284 #endif
285 
286 #endif
287 
288 #if !defined(NO_USE_FAST_CALL) && defined(FC_ATTRIB)
289 #define FC_USED_TO_COMPILE_CORE
290 #ifndef FC /* to compile Ma2Asm/check.c without FC */
291 #define FC FC_ATTRIB
292 #endif
293 #else
294 #define FC
295 #endif
296 
297 
298 /* Win32 SEH macros */
299 
300 #if defined(_WIN32) && !defined(_WIN64) || defined(__CYGWIN__)
301 #define USE_SEH
302 #endif
303 
304 #if defined(USE_SEH)
305  /* from MSVC++ windows.h + renaming */
306 typedef enum {
307  ExceptContinueExecution,
308  ExceptContinueSearch,
309  ExceptNestedException,
310  ExceptCollidedUnwind
311 } EXCEPT_DISPOSITION;
312 
313 typedef struct _excp_lst
314 {
315  struct _excp_lst *chain;
316  EXCEPT_DISPOSITION (*handler)();
317 } excp_lst;
318 
319 
320 #ifdef __GNUC__
321 # define SEH_PUSH(new_handler) \
322 { \
323  excp_lst e; \
324  EXCEPT_DISPOSITION new_handler(); \
325  e.handler = new_handler; \
326  asm("movl %%fs:0,%0" : "=r" (e.chain)); \
327  asm("movl %0,%%fs:0" : : "r" (&e));
328 
329 
330 # define SEH_POP \
331  asm("movl %0,%%fs:0" : : "r" (e.chain)); \
332 }
333 
334 #elif defined(_MSC_VER)
335 
336 # pragma warning(disable:4733) /* we know what we are doing with SEH */
337 
338 # define SEH_PUSH(new_handler) \
339 { \
340  excp_lst e; \
341  EXCEPT_DISPOSITION new_handler(); \
342  e.handler = new_handler; \
343  __asm push eax \
344  __asm mov eax,dword ptr fs:[0] \
345  __asm mov dword ptr [e.chain],eax \
346  __asm lea eax,[e] \
347  __asm mov dword ptr fs:[0],eax \
348  __asm pop eax
349 
350 # define SEH_POP \
351  __asm push eax \
352  __asm mov eax,dword ptr [e.chain] \
353  __asm mov dword ptr fs:[0],eax \
354  __asm pop eax \
355 }
356 
357 #elif defined(__LCC__)
358  /* below in movl %eax,%e and movel %e,%eax %e should be %e.chain the lcc asm
359  does not support it. Here %e works since chain is the 1st field */
360 # define SEH_PUSH(new_handler) \
361 { \
362  excp_lst e; \
363  EXCEPT_DISPOSITION new_handler(); \
364  e.handler = new_handler; \
365  _asm("pushl %eax"); \
366  _asm("movl %fs:0,%eax"); \
367  _asm("movl %eax,%e"); \
368  _asm("leal %e,%eax"); \
369  _asm("movl %eax,%fs:0"); \
370  _asm("popl %eax");
371 
372 # define SEH_POP \
373  _asm("pushl %eax"); \
374  _asm("movl %e,%eax"); \
375  _asm("movl %eax,%fs:0"); \
376  _asm("popl %eax"); \
377 }
378 
379 #else
380 
381 # error macros SEH_PUSH/POP undefined for this compiler
382 
383 #endif
384 
385 #endif /* defined(USE_SEH) */
386 
387 #endif /* !_ARCH_DEP_H */
388 
Definition: WIN32_all_SIGSEGV.c:18
EXCEPTION_DISPOSITION(* handler)()
Definition: WIN32_all_SIGSEGV.c:21
struct _excp_lst * chain
Definition: WIN32_all_SIGSEGV.c:20
struct _excp_lst excp_lst