github.com/gotranspile/cxgo@v0.3.7/libs/stdlib.go (about)

     1  package libs
     2  
     3  import (
     4  	"os"
     5  	"fmt"
     6  	
     7  	"github.com/gotranspile/cxgo/runtime/cmath"
     8  	"github.com/gotranspile/cxgo/runtime/libc"
     9  	"github.com/gotranspile/cxgo/types"
    10  )
    11  
    12  const StdlibH = "stdlib.h"
    13  
    14  // https://pubs.opengroup.org/onlinepubs/9699919799/
    15  
    16  func init() {
    17  	RegisterLibrary(StdlibH, func(c *Env) *Library {
    18  		voidPtr := c.PtrT(nil)
    19  		gstrT := c.Go().String()
    20  		cstrT := c.C().String()
    21  		wstrT := c.C().WString()
    22  		intT := types.IntT(4)
    23  		gintT := c.Go().Int()
    24  		longT := types.IntT(8)
    25  		uintT := types.UintT(4)
    26  		l := &Library{
    27  			Imports: map[string]string{
    28  				"libc":  RuntimeLibc,
    29  				"cmath": RuntimePrefix + "cmath",
    30  			},
    31  			Idents: map[string]*types.Ident{
    32  				"abs":      c.NewIdent("abs", "cmath.Abs", cmath.Abs, c.FuncTT(longT, longT)),
    33  				"_Exit":    c.Go().OsExitFunc(),
    34  				"malloc":   c.C().MallocFunc(),
    35  				"calloc":   c.C().CallocFunc(),
    36  				"realloc":  c.NewIdent("realloc", "libc.Realloc", libc.Realloc, c.FuncTT(voidPtr, voidPtr, gintT)),
    37  				"free":     c.C().FreeFunc(),
    38  				"atoi":     c.NewIdent("atoi", "libc.Atoi", libc.Atoi, c.FuncTT(gintT, gstrT)),
    39  				"atol":     c.NewIdent("atol", "libc.Atoi", libc.Atoi, c.FuncTT(gintT, gstrT)),
    40  				"atof":     c.NewIdent("atof", "libc.Atof", libc.Atof, c.FuncTT(types.FloatT(8), gstrT)),
    41  				"rand":     c.NewIdent("rand", "libc.Rand", libc.Rand, c.FuncTT(intT)),
    42  				"srand":    c.NewIdent("srand", "libc.SeedRand", libc.SeedRand, c.FuncTT(nil, uintT)),
    43  				"qsort":    c.NewIdent("qsort", "libc.Sort", libc.Sort, c.FuncTT(nil, voidPtr, uintT, uintT, c.FuncTT(intT, voidPtr, voidPtr))),
    44  				"bsearch":  c.NewIdent("bsearch", "libc.Search", libc.Search, c.FuncTT(voidPtr, voidPtr, voidPtr, uintT, uintT, c.FuncTT(intT, voidPtr, voidPtr))),
    45  				"mbstowcs": c.NewIdent("mbstowcs", "libc.Mbstowcs", libc.Mbstowcs, c.FuncTT(uintT, wstrT, cstrT, uintT)),
    46  			},
    47  			Header: `
    48  #include <` + BuiltinH + `>
    49  #include <` + stddefH + `>
    50  
    51  #define EXIT_FAILURE 1
    52  #define EXIT_SUCCESS 0
    53  
    54  #define malloc __builtin_malloc
    55  #define abort __builtin_abort
    56  void free(void*);
    57  
    58  typedef struct {
    59  	int quot;
    60  	int rem;
    61  } div_t;
    62  
    63  typedef struct {
    64  	long quot;
    65  	long rem;
    66  } ldiv_t;
    67  
    68  typedef struct {
    69  	long long quot;
    70  	long long rem;
    71  } lldiv_t;
    72  
    73  void          _Exit(_cxgo_go_int);
    74  long          a64l(const char *);
    75  _cxgo_int64   abs(_cxgo_int64);
    76  int           atexit(void (*)(void));
    77  double        atof(const char *);
    78  _cxgo_go_int  atoi(const char *);
    79  _cxgo_go_int  atol(const char *);
    80  long long     atoll(const char *);
    81  void         *bsearch(const void *, const void *, _cxgo_uint32, _cxgo_uint32, _cxgo_int32 (*)(const void *, const void *));
    82  void         *calloc(_cxgo_go_int, _cxgo_go_int);
    83  div_t         div(int, int);
    84  double        drand48(void);
    85  double        erand48(unsigned short [3]);
    86  #define exit(x) _Exit(x)
    87  int           getsubopt(char **, char *const *, char **);
    88  int           grantpt(int);
    89  char         *initstate(unsigned, char *, size_t);
    90  long          jrand48(unsigned short [3]);
    91  char         *l64a(long);
    92  long          labs(long);
    93  void          lcong48(unsigned short [7]);
    94  ldiv_t        ldiv(long, long);
    95  long long     llabs(long long);
    96  lldiv_t       lldiv(long long, long long);
    97  long          lrand48(void);
    98  void         *malloc(_cxgo_go_int);
    99  int           mblen(const char *, size_t);
   100  _cxgo_uint32  mbstowcs(wchar_t *restrict, const char *restrict, _cxgo_uint32);
   101  int           mbtowc(wchar_t *restrict, const char *restrict, size_t);
   102  char         *mkdtemp(char *);
   103  int           mkstemp(char *);
   104  long          mrand48(void);
   105  long          nrand48(unsigned short [3]);
   106  int           posix_memalign(void **, size_t, size_t);
   107  int           posix_openpt(int);
   108  char         *ptsname(int);
   109  int           putenv(char *);
   110  void          qsort(void *, _cxgo_uint32, _cxgo_uint32, _cxgo_int32 (*)(const void *, const void *));
   111  _cxgo_sint32  rand(void);
   112  int           rand_r(unsigned *);
   113  long          random(void);
   114  void         *realloc(void *, _cxgo_go_int);
   115  char         *realpath(const char *restrict, char *restrict);
   116  unsigned short *seed48(unsigned short [3]);
   117  int           setenv(const char *, const char *, int);
   118  void          setkey(const char *);
   119  char         *setstate(char *);
   120  void          srand(_cxgo_uint32);
   121  void          srand48(long);
   122  void          srandom(unsigned);
   123  double        strtod(const char *restrict, char **restrict);
   124  float         strtof(const char *restrict, char **restrict);
   125  long          strtol(const char *restrict, char **restrict, int);
   126  long double   strtold(const char *restrict, char **restrict);
   127  long long     strtoll(const char *restrict, char **restrict, int);
   128  unsigned long strtoul(const char *restrict, char **restrict, int);
   129  unsigned long long strtoull(const char *restrict, char **restrict, int);
   130  int           system(const char *);
   131  int           unlockpt(int);
   132  int           unsetenv(const char *);
   133  size_t        wcstombs(char *restrict, const wchar_t *restrict, size_t);
   134  int           wctomb(char *, wchar_t);
   135  `,
   136  		}
   137  		l.Header += fmt.Sprintf("#define RAND_MAX %d\n", libc.RandMax)
   138  		l.Declare(
   139  			c.NewIdent("getenv", "os.Getenv", os.Getenv, c.FuncTT(c.Go().String(), c.Go().String())),
   140  		)
   141  		return l
   142  	})
   143  }