2019-10-13 15:37:43 +02:00
|
|
|
module compiler
|
2019-08-19 00:18:07 +02:00
|
|
|
|
|
|
|
const (
|
2019-12-19 22:29:37 +01:00
|
|
|
c_common_macros = '
|
2019-12-16 17:05:33 +01:00
|
|
|
|
|
|
|
#define EMPTY_STRUCT_DECLARATION
|
|
|
|
#define EMPTY_STRUCT_INITIALIZATION 0
|
|
|
|
// Due to a tcc bug, the length of an array needs to be specified, but GCC crashes if it is...
|
|
|
|
#define EMPTY_ARRAY_OF_ELEMS(x,n) (x[])
|
|
|
|
#define TCCSKIP(x) x
|
|
|
|
|
|
|
|
#ifdef __TINYC__
|
|
|
|
#undef EMPTY_STRUCT_DECLARATION
|
|
|
|
#undef EMPTY_STRUCT_INITIALIZATION
|
|
|
|
#define EMPTY_STRUCT_DECLARATION char _dummy
|
|
|
|
#define EMPTY_STRUCT_INITIALIZATION 0
|
|
|
|
#undef EMPTY_ARRAY_OF_ELEMS
|
|
|
|
#define EMPTY_ARRAY_OF_ELEMS(x,n) (x[n])
|
|
|
|
#undef TCCSKIP
|
|
|
|
#define TCCSKIP(x)
|
|
|
|
#endif
|
|
|
|
|
2019-12-21 22:46:09 +01:00
|
|
|
// for __offset_of
|
2019-12-21 23:01:44 +01:00
|
|
|
#ifndef __offsetof
|
2019-12-21 22:46:09 +01:00
|
|
|
#define __offsetof(s,memb) \\
|
|
|
|
((size_t)((char *)&((s *)0)->memb - (char *)0))
|
2019-12-21 23:01:44 +01:00
|
|
|
#endif
|
2019-12-21 22:46:09 +01:00
|
|
|
|
2019-12-16 17:05:33 +01:00
|
|
|
#define OPTION_CAST(x) (x)
|
|
|
|
'
|
2019-12-19 22:29:37 +01:00
|
|
|
c_headers = '
|
2019-08-19 00:18:07 +02:00
|
|
|
|
2019-11-16 09:10:38 +01:00
|
|
|
//#include <inttypes.h> // int64_t etc
|
2019-08-19 00:18:07 +02:00
|
|
|
#include <stdio.h> // TODO remove all these includes, define all function signatures and types manually
|
|
|
|
#include <stdlib.h>
|
2019-11-05 18:25:43 +01:00
|
|
|
|
|
|
|
//#include "fns.h"
|
|
|
|
#include <signal.h>
|
2019-08-19 00:18:07 +02:00
|
|
|
#include <stdarg.h> // for va_list
|
2019-11-05 18:25:43 +01:00
|
|
|
#include <string.h> // memcpy
|
2019-08-19 00:18:07 +02:00
|
|
|
|
2019-11-15 14:14:28 +01:00
|
|
|
#if INTPTR_MAX == INT32_MAX
|
|
|
|
#define TARGET_IS_32BIT 1
|
|
|
|
#elif INTPTR_MAX == INT64_MAX
|
|
|
|
#define TARGET_IS_64BIT 1
|
|
|
|
#else
|
|
|
|
#error "The environment is not 32 or 64-bit."
|
|
|
|
#endif
|
|
|
|
|
2019-11-16 09:10:38 +01:00
|
|
|
#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ || defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN || defined(__BIG_ENDIAN__) || defined(__ARMEB__) || defined(__THUMBEB__) || defined(__AARCH64EB__) || defined(_MIBSEB) || defined(__MIBSEB) || defined(__MIBSEB__)
|
2019-11-15 14:14:28 +01:00
|
|
|
#define TARGET_ORDER_IS_BIG
|
2019-11-16 09:10:38 +01:00
|
|
|
#elif defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ || defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN || defined(__LITTLE_ENDIAN__) || defined(__ARMEL__) || defined(__THUMBEL__) || defined(__AARCH64EL__) || defined(_MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__) || defined(_M_AMD64) || defined(_M_X64) || defined(_M_IX86)
|
2019-11-15 14:14:28 +01:00
|
|
|
#define TARGET_ORDER_IS_LITTLE
|
|
|
|
#else
|
|
|
|
#error "Unknown architecture endianness"
|
|
|
|
#endif
|
|
|
|
|
2019-08-23 22:55:45 +02:00
|
|
|
#ifndef _WIN32
|
|
|
|
#include <ctype.h>
|
2019-11-05 18:25:43 +01:00
|
|
|
#include <locale.h> // tolower
|
2019-08-30 00:05:58 +02:00
|
|
|
#include <sys/time.h>
|
2019-11-05 18:25:43 +01:00
|
|
|
#include <unistd.h> // sleep
|
2019-08-23 22:55:45 +02:00
|
|
|
#endif
|
|
|
|
|
2019-11-05 18:00:00 +01:00
|
|
|
#if defined(__CYGWIN__) && !defined(_WIN32)
|
|
|
|
#error Cygwin is not supported, please use MinGW or Visual Studio.
|
|
|
|
#endif
|
|
|
|
|
2019-09-11 12:31:29 +02:00
|
|
|
|
2019-08-27 22:29:13 +02:00
|
|
|
#ifdef __linux__
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h> // os__wait uses wait on nix
|
|
|
|
#endif
|
|
|
|
|
2019-10-09 13:28:30 +02:00
|
|
|
#ifdef __FreeBSD__
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h> // os__wait uses wait on nix
|
|
|
|
#endif
|
|
|
|
|
2019-10-10 19:05:22 +02:00
|
|
|
#ifdef __DragonFly__
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h> // os__wait uses wait on nix
|
|
|
|
#endif
|
2019-10-20 06:00:01 +02:00
|
|
|
|
|
|
|
#ifdef __OpenBSD__
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/wait.h> // os__wait uses wait on nix
|
|
|
|
#endif
|
2019-10-10 19:05:22 +02:00
|
|
|
|
2020-01-21 16:55:07 +01:00
|
|
|
#ifdef __NetBSD__
|
|
|
|
#include <sys/wait.h> // os__wait uses wait on nix
|
|
|
|
#endif
|
|
|
|
|
2019-12-19 22:29:37 +01:00
|
|
|
$c_common_macros
|
2019-08-19 00:18:07 +02:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2019-09-26 21:54:15 +02:00
|
|
|
#define WINVER 0x0600
|
2019-11-16 00:30:50 +01:00
|
|
|
#ifdef _WIN32_WINNT
|
|
|
|
#undef _WIN32_WINNT
|
|
|
|
#endif
|
2019-09-26 21:54:15 +02:00
|
|
|
#define _WIN32_WINNT 0x0600
|
2019-08-19 00:18:07 +02:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2019-09-17 13:56:32 +02:00
|
|
|
#define _UNICODE
|
|
|
|
#define UNICODE
|
2019-08-19 00:18:07 +02:00
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
#include <io.h> // _waccess
|
|
|
|
#include <direct.h> // _wgetcwd
|
|
|
|
//#include <WinSock2.h>
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
// On MSVC these are the same (as long as /volatile:ms is passed)
|
|
|
|
#define _Atomic volatile
|
|
|
|
|
2019-08-23 01:38:18 +02:00
|
|
|
// MSVC cannot parse some things properly
|
2019-09-15 14:52:12 +02:00
|
|
|
#undef EMPTY_STRUCT_DECLARATION
|
2019-08-19 00:18:07 +02:00
|
|
|
#undef OPTION_CAST
|
2019-09-15 23:41:12 +02:00
|
|
|
|
|
|
|
#define EMPTY_STRUCT_DECLARATION int ____dummy_variable
|
2019-08-19 00:18:07 +02:00
|
|
|
#define OPTION_CAST(x)
|
2020-01-10 14:06:40 +01:00
|
|
|
|
|
|
|
#include <dbghelp.h>
|
|
|
|
#pragma comment(lib, "Dbghelp.lib")
|
|
|
|
|
2019-08-19 00:18:07 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#else
|
|
|
|
#include <pthread.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
//============================== HELPER C MACROS =============================*/
|
2019-10-10 00:07:00 +02:00
|
|
|
#define _PUSH(arr, val, tmp, tmp_typ) {tmp_typ tmp = (val); array_push(arr, &tmp);}
|
|
|
|
#define _PUSH_MANY(arr, val, tmp, tmp_typ) {tmp_typ tmp = (val); array_push_many(arr, tmp.data, tmp.len);}
|
2019-08-19 00:18:07 +02:00
|
|
|
#define _IN(typ, val, arr) array_##typ##_contains(arr, val)
|
2019-10-10 00:07:00 +02:00
|
|
|
#define _IN_MAP(val, m) map_exists(m, val)
|
2019-09-29 16:39:38 +02:00
|
|
|
#define DEFAULT_EQUAL(a, b) (a == b)
|
2019-10-01 20:21:05 +02:00
|
|
|
#define DEFAULT_NOT_EQUAL(a, b) (a != b)
|
|
|
|
#define DEFAULT_LT(a, b) (a < b)
|
|
|
|
#define DEFAULT_LE(a, b) (a <= b)
|
|
|
|
#define DEFAULT_GT(a, b) (a > b)
|
|
|
|
#define DEFAULT_GE(a, b) (a >= b)
|
2020-02-16 16:13:45 +01:00
|
|
|
|
|
|
|
// NB: macro_fXX_eq and macro_fXX_ne are NOT used
|
|
|
|
// in the generated C code. They are here just for
|
|
|
|
// completeness/testing.
|
|
|
|
|
|
|
|
#define macro_f64_eq(a, b) (a == b)
|
|
|
|
#define macro_f64_ne(a, b) (a != b)
|
|
|
|
#define macro_f64_lt(a, b) (a < b)
|
|
|
|
#define macro_f64_le(a, b) (a <= b)
|
|
|
|
#define macro_f64_gt(a, b) (a > b)
|
|
|
|
#define macro_f64_ge(a, b) (a >= b)
|
|
|
|
|
|
|
|
#define macro_f32_eq(a, b) (a == b)
|
|
|
|
#define macro_f32_ne(a, b) (a != b)
|
|
|
|
#define macro_f32_lt(a, b) (a < b)
|
|
|
|
#define macro_f32_le(a, b) (a <= b)
|
|
|
|
#define macro_f32_gt(a, b) (a > b)
|
|
|
|
#define macro_f32_ge(a, b) (a >= b)
|
|
|
|
|
2019-08-19 00:18:07 +02:00
|
|
|
//================================== GLOBALS =================================*/
|
2020-01-04 17:53:37 +01:00
|
|
|
byte g_str_buf[1024];
|
2019-08-19 00:18:07 +02:00
|
|
|
int load_so(byteptr);
|
|
|
|
void reload_so();
|
2020-01-08 10:19:12 +01:00
|
|
|
|
2019-08-19 00:18:07 +02:00
|
|
|
'
|
2019-12-19 22:29:37 +01:00
|
|
|
js_headers = '
|
2019-09-14 22:48:30 +02:00
|
|
|
|
2019-09-18 23:03:54 +02:00
|
|
|
var array_string = function() {}
|
|
|
|
var array_byte = function() {}
|
|
|
|
var array_int = function() {}
|
|
|
|
var byte = function() {}
|
|
|
|
var double = function() {}
|
|
|
|
var int = function() {}
|
|
|
|
var f64 = function() {}
|
|
|
|
var f32 = function() {}
|
|
|
|
var i64 = function() {}
|
|
|
|
var i32 = function() {}
|
|
|
|
var i16 = function() {}
|
|
|
|
var u64 = function() {}
|
|
|
|
var u32 = function() {}
|
|
|
|
var u16 = function() {}
|
|
|
|
var i8 = function() {}
|
|
|
|
var bool = function() {}
|
|
|
|
var rune = function() {}
|
|
|
|
var map_string = function() {}
|
|
|
|
var map_int = function() {}
|
2019-09-14 22:48:30 +02:00
|
|
|
|
|
|
|
'
|
2019-12-19 22:29:37 +01:00
|
|
|
c_builtin_types = '
|
2019-11-14 08:23:44 +01:00
|
|
|
|
2019-11-16 09:10:38 +01:00
|
|
|
//#include <inttypes.h> // int64_t etc
|
|
|
|
//#include <stdint.h> // int64_t etc
|
|
|
|
|
2019-11-14 08:23:44 +01:00
|
|
|
//================================== TYPEDEFS ================================*/
|
|
|
|
|
|
|
|
typedef int64_t i64;
|
|
|
|
typedef int16_t i16;
|
|
|
|
typedef int8_t i8;
|
|
|
|
typedef uint64_t u64;
|
|
|
|
typedef uint32_t u32;
|
|
|
|
typedef uint16_t u16;
|
|
|
|
typedef uint8_t byte;
|
|
|
|
typedef uint32_t rune;
|
|
|
|
typedef float f32;
|
|
|
|
typedef double f64;
|
|
|
|
typedef unsigned char* byteptr;
|
|
|
|
typedef int* intptr;
|
|
|
|
typedef void* voidptr;
|
2019-11-30 13:40:08 +01:00
|
|
|
typedef char* charptr;
|
2019-11-14 08:23:44 +01:00
|
|
|
typedef struct array array;
|
|
|
|
typedef struct map map;
|
|
|
|
typedef array array_string;
|
|
|
|
typedef array array_int;
|
|
|
|
typedef array array_byte;
|
|
|
|
typedef array array_f32;
|
|
|
|
typedef array array_f64;
|
2019-11-22 03:22:18 +01:00
|
|
|
typedef array array_u16;
|
|
|
|
typedef array array_u32;
|
|
|
|
typedef array array_u64;
|
2019-11-14 08:23:44 +01:00
|
|
|
typedef map map_int;
|
|
|
|
typedef map map_string;
|
|
|
|
#ifndef bool
|
|
|
|
typedef int bool;
|
|
|
|
#define true 1
|
|
|
|
#define false 0
|
|
|
|
#endif
|
|
|
|
'
|
2019-12-19 22:29:37 +01:00
|
|
|
bare_c_headers = '
|
2019-12-01 09:27:36 +01:00
|
|
|
|
2019-12-16 17:05:33 +01:00
|
|
|
$c_common_macros
|
2019-12-08 11:44:52 +01:00
|
|
|
|
2019-12-07 20:25:19 +01:00
|
|
|
#ifndef exit
|
|
|
|
#define exit(rc) sys_exit(rc)
|
|
|
|
void sys_exit (int);
|
|
|
|
#endif
|
2019-12-01 09:27:36 +01:00
|
|
|
'
|
2019-08-20 13:34:29 +02:00
|
|
|
)
|
2019-12-16 17:05:33 +01:00
|
|
|
|