Qore Programming Language  0.8.11.1
macros-i386.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  Qore Programming Language
4 
5  Copyright (C) 2003 - 2014 David Nichols
6 
7  Permission is hereby granted, free of charge, to any person obtaining a
8  copy of this software and associated documentation files (the "Software"),
9  to deal in the Software without restriction, including without limitation
10  the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  and/or sell copies of the Software, and to permit persons to whom the
12  Software is furnished to do so, subject to the following conditions:
13 
14  The above copyright notice and this permission notice shall be included in
15  all copies or substantial portions of the Software.
16 
17  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  DEALINGS IN THE SOFTWARE.
24 
25  Note that the Qore library is released under a choice of three open-source
26  licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
27  information.
28 */
29 
30 #ifndef _QORE_MACHINE_MACROS_H
31 #define _QORE_MACHINE_MACROS_H
32 
33 #define STACK_DIRECTION_DOWN 1
34 
35 #ifdef __GNUC__
36 #define HAVE_ATOMIC_MACROS
37 
38 // returns 1 when counter reaches zero, 0 if not
39 static inline int atomic_dec(volatile int *a) {
40  unsigned char rc;
41 
42  __asm(
43  "lock; decl %0; sete %1"
44  : "=m" (*a), "=qm" (rc)
45  : "m" (*a) : "memory"
46  );
47  return rc != 0;
48 }
49 
50 static inline void atomic_inc(volatile int *a) {
51  __asm(
52  "lock; incl %0"
53  : "=m" (*a)
54  );
55 }
56 
57 #define HAVE_CHECK_STACK_POS
58 
59 static inline size_t get_stack_pos() {
60  size_t addr;
61  __asm("movl %%esp, %0" : "=g" (addr) );
62  return addr;
63 }
64 
65 #endif // __GNUC__
66 
67 #ifdef __SUNPRO_CC
68 #define HAVE_ATOMIC_MACROS
69 
70 // these routines are implemented in assembler
71 extern "C" int atomic_dec(volatile int *a);
72 extern "C" void atomic_inc(volatile int *a);
73 
74 #define HAVE_CHECK_STACK_POS
75 extern "C" size_t get_stack_pos();
76 #endif // #ifdef __SUNPRO_CC
77 
78 #endif // #ifndef _QORE_MACHINE_MACROS_H
79