Home | History | Annotate | Download | only in go
      1 /* ------------------------------------------------------------
      2  * goruntime.swg
      3  *
      4  * Go runtime code for the various generated files.
      5  * ------------------------------------------------------------ */
      6 
      7 %insert(runtime) %{
      8 #include <stddef.h>
      9 #include <stdio.h>
     10 #include <stdlib.h>
     11 #include <string.h>
     12 #include <sys/types.h>
     13 
     14 %}
     15 
     16 #if SWIGGO_INTGO_SIZE == 32
     17 %insert(runtime) %{
     18 typedef int intgo;
     19 typedef unsigned int uintgo;
     20 %}
     21 #elif SWIGGO_INTGO_SIZE == 64
     22 %insert(runtime) %{
     23 typedef long long intgo;
     24 typedef unsigned long long uintgo;
     25 %}
     26 #else
     27 %insert(runtime) %{
     28 typedef ptrdiff_t intgo;
     29 typedef size_t uintgo;
     30 %}
     31 #endif
     32 
     33 %insert(runtime) %{
     34 
     35 typedef struct { char *p; intgo n; } _gostring_;
     36 typedef struct { void* array; intgo len; intgo cap; } _goslice_;
     37 
     38 %}
     39 
     40 #ifndef SWIGGO_GCCGO
     41 /* Boilerplate for C/C++ code when using 6g/8g.  This code is compiled
     42    with gcc.  */
     43 %insert(runtime) %{
     44 
     45 #define swiggo_size_assert_eq(x, y, name) typedef char name[(x-y)*(x-y)*-2+1];
     46 #define swiggo_size_assert(t, n) swiggo_size_assert_eq(sizeof(t), n, swiggo_sizeof_##t##_is_not_##n)
     47 
     48 swiggo_size_assert(char, 1)
     49 swiggo_size_assert(short, 2)
     50 swiggo_size_assert(int, 4)
     51 typedef long long swiggo_long_long;
     52 swiggo_size_assert(swiggo_long_long, 8)
     53 swiggo_size_assert(float, 4)
     54 swiggo_size_assert(double, 8)
     55 
     56 #ifdef __cplusplus
     57 extern "C" {
     58 #endif
     59 extern void crosscall2(void (*fn)(void *, int), void *, int);
     60 extern void _cgo_allocate(void *, int);
     61 extern void _cgo_panic(void *, int);
     62 #ifdef __cplusplus
     63 }
     64 #endif
     65 
     66 static void *_swig_goallocate(size_t len) {
     67   struct {
     68     size_t len;
     69     void *ret;
     70   } a;
     71   a.len = len;
     72   crosscall2(_cgo_allocate, &a, (int) sizeof a);
     73   return a.ret;
     74 }
     75 
     76 static void _swig_gopanic(const char *p) {
     77   struct {
     78     const char *p;
     79   } a;
     80   a.p = p;
     81   crosscall2(_cgo_panic, &a, (int) sizeof a);
     82 }
     83 
     84 %}
     85 
     86 /* Boilerplate for C code when using 6g/8g.  This code is compiled
     87    with 6c/8c.  */
     88 %insert(gc_header) %{
     89 #include "runtime.h"
     90 #include "cgocall.h"
     91 
     92 #ifdef _64BIT
     93 #define SWIG_PARM_SIZE 8
     94 #else
     95 #define SWIG_PARM_SIZE 4
     96 #endif
     97 %}
     98 
     99 #else
    100 
    101 /* Boilerplate for C/C++ code when using gccgo.  */
    102 %insert(runtime) %{
    103 #define SWIGGO_GCCGO
    104 
    105 #ifdef __cplusplus
    106 extern "C" {
    107 #endif
    108 extern void *_cgo_allocate(size_t);
    109 extern void _cgo_panic(const char *);
    110 
    111 /* Implementations of SwigCgocall and friends for different versions
    112    of gccgo.  The Go code will call these functions using C names with
    113    a prefix of the module name.  The implementations here call the
    114    routine in libgo.  The routines to call vary depending on the gccgo
    115    version.  We assume that the version of gcc used to compile this
    116    file is the same as the version of gccgo.  */
    117 
    118 #define SWIGCONCAT2(s1, s2) s1 ## s2
    119 #define SWIGCONCAT1(s1, s2) SWIGCONCAT2(s1, s2)
    120 #define SwigCgocall SWIGCONCAT1(SWIGMODULE, SwigCgocall)
    121 #define SwigCgocallDone SWIGCONCAT1(SWIGMODULE, SwigCgocallDone)
    122 #define SwigCgocallBack SWIGCONCAT1(SWIGMODULE, SwigCgocallBack)
    123 #define SwigCgocallBackDone SWIGCONCAT1(SWIGMODULE, SwigCgocallBackDone)
    124 
    125 #define SWIG_GCC_VERSION \
    126   (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC__PATH_LEVEL__)
    127 
    128 #if SWIG_GCC_VERSION < 40700
    129 #define SwigDoCgocall()
    130 #define SwigDoCgocallDone()
    131 #define SwigDoCgocallBack()
    132 #define SwigDoCgocallBackDone()
    133 #elif SWIG_GCC_VERSION == 40700
    134 void SwigDoCgocall(void) __asm__("libgo_syscall.syscall.Entersyscall");
    135 void SwigDoCgocallDone(void) __asm__("libgo_syscall.syscall.Exitsyscall");
    136 void SwigDoCgocallBack(void) __asm__("libgo_syscall.syscall.Exitsyscall");
    137 void SwigDoCgocallBackDone(void) __asm__("libgo_syscall.syscall.Entersyscall");
    138 #else
    139 void SwigDoCgocall(void) __asm__("syscall.Cgocall");
    140 void SwigDoCgocallDone(void) __asm__("syscall.CgocallDone");
    141 void SwigDoCgocallBack(void) __asm__("syscall.CgocallBack");
    142 void SwigDoCgocallBackDone(void) __asm__("syscall.CgocallBackDone");
    143 #endif
    144 
    145 void SwigCgocall() {
    146   SwigDoCgocall();
    147 }
    148 
    149 void SwigCgocallDone() {
    150   SwigDoCgocallDone();
    151 }
    152 
    153 void SwigCgocallBack() {
    154   SwigDoCgocallBack();
    155 }
    156 
    157 void SwigCgocallBackDone() {
    158   SwigDoCgocallBackDone();
    159 }
    160 
    161 #ifdef __cplusplus
    162 }
    163 #endif
    164 
    165 #define _swig_goallocate _cgo_allocate
    166 #define _swig_gopanic _cgo_panic
    167 
    168 %}
    169 
    170 #endif
    171 
    172 %insert(runtime) %{
    173 
    174 static _gostring_ _swig_makegostring(const char *p, size_t l) {
    175   _gostring_ ret;
    176   ret.p = (char*)_swig_goallocate(l + 1);
    177   memcpy(ret.p, p, l);
    178   ret.n = l;
    179   return ret;
    180 }
    181 
    182 #define SWIG_contract_assert(expr, msg) \
    183   if (!(expr)) { _swig_gopanic(msg); } else
    184 %}
    185 
    186 #ifndef SWIGGO_GCCGO
    187 
    188 %insert(go_header) %{
    189 
    190 import _ "runtime/cgo"
    191 import "unsafe"
    192 
    193 type _ unsafe.Pointer
    194 
    195 %}
    196 
    197 #else
    198 
    199 %insert(go_header) %{
    200 
    201 import "syscall"
    202 import "unsafe"
    203 
    204 type _ syscall.Sockaddr
    205 
    206 type _ unsafe.Pointer
    207 
    208 %}
    209 
    210 #endif
    211 
    212 /* Function pointers are translated by the code in go.cxx into
    213    _swig_fnptr.  Member pointers are translated to _swig_memberptr.  */
    214 
    215 %insert(go_header) %{
    216 type _swig_fnptr *byte
    217 type _swig_memberptr *byte
    218 %}
    219