[project @ 2001-01-13 12:11:00 by qrczak]
[ghc-hetmet.git] / ghc / utils / hsc2hs / template-hsc.h
1 #if __GLASGOW_HASKELL__ && __GLASGOW_HASKELL__ < 409
2 #include <Rts.h>
3 #endif
4 #include <HsFFI.h>
5
6 #include <stddef.h>
7 #include <string.h>
8 #include <stdio.h>
9
10 #ifndef offsetof
11 #define offsetof(t, f) ((size_t) &((t *)0)->f)
12 #endif
13
14 #define hsc_const(x)                        \
15     if ((x) < 0)                            \
16         printf ("%ld", (long)(x));          \
17     else                                    \
18         printf ("%lu", (unsigned long)(x));
19
20 #define hsc_const_str(x)                                          \
21     {                                                             \
22         const char *s = (x);                                      \
23         printf ("\"");                                            \
24         while (*s != '\0')                                        \
25         {                                                         \
26             if (*s == '"' || *s == '\\')                          \
27                 printf ("\\%c", *s);                              \
28             else if (*s >= 0x20 && *s <= 0x7E)                    \
29                 printf ("%c", *s);                                \
30             else                                                  \
31                 printf ("\\%d%s",                                 \
32                         (unsigned char) *s,                       \
33                         s[1] >= '0' && s[1] <= '9' ? "\\&" : ""); \
34             s++;                                                  \
35         }                                                         \
36         printf ("\"");                                            \
37     }
38
39 #define hsc_type(t)                                         \
40     if ((t)(int)(t)1.4 == (t)1.4)                           \
41         printf ("%s%d",                                     \
42                 (t)(-1) < (t)0 ? "Int" : "Word",            \
43                 sizeof (t) * 8);                            \
44     else                                                    \
45         printf ("%s",                                       \
46                 sizeof (t) >  sizeof (double) ? "LDouble" : \
47                 sizeof (t) == sizeof (double) ? "Double"  : \
48                 "Float");
49
50 #define hsc_peek(t, f) \
51     printf ("(\\hsc_ptr -> peekByteOff hsc_ptr %ld)", (long) offsetof (t, f));
52
53 #define hsc_poke(t, f) \
54     printf ("(\\hsc_ptr -> pokeByteOff hsc_ptr %ld)", (long) offsetof (t, f));
55
56 #define hsc_ptr(t, f) \
57     printf ("(\\hsc_ptr -> hsc_ptr `plusPtr` %ld)", (long) offsetof (t, f));
58