Errno updates with errno for linux/win/macos. Updated $$ syntax to also match compiler constants.
This commit is contained in:
Christoffer Lerno
2022-01-18 01:31:14 +01:00
committed by GitHub
parent 3f60443d66
commit e4e8abbc6c
22 changed files with 186 additions and 99 deletions

View File

@@ -3,10 +3,10 @@
// a copy of which can be found in the LICENSE_STDLIB file.
module std::cinterop;
const C_INT_SIZE = ${C_INT_SIZE};
const C_LONG_SIZE = ${C_LONG_SIZE};
const C_SHORT_SIZE = ${C_SHORT_SIZE};
const C_LONG_LONG_SIZE = ${C_LONG_LONG_SIZE};
const C_INT_SIZE = $$C_INT_SIZE;
const C_LONG_SIZE = $$C_LONG_SIZE;
const C_SHORT_SIZE = $$C_SHORT_SIZE;
const C_LONG_LONG_SIZE = $$C_LONG_LONG_SIZE;
$assert (C_SHORT_SIZE < 32);
$assert (C_INT_SIZE < 128);
@@ -16,55 +16,55 @@ $assert (C_SHORT_SIZE <= C_INT_SIZE);
$assert (C_INT_SIZE <= C_LONG_SIZE);
$assert (C_LONG_SIZE <= C_LONG_LONG_SIZE);
$if (C_INT_SIZE == 64):
$if C_INT_SIZE == 64:
define CInt = long;
define CUInt = ulong;
$elif (C_INT_SIZE == 32):
$elif C_INT_SIZE == 32:
define CInt = int;
define CUInt = uint;
$elif (C_INT_SIZE == 16):
$elif C_INT_SIZE == 16:
define CInt = short;
define CUInt = ushort;
$else:
$assert(false, "Invalid C int size");
$endif;
$if (C_LONG_SIZE == 64):
$if C_LONG_SIZE == 64:
define CLong = long;
define CULong = ulong;
$elif (C_LONG_SIZE == 32):
$elif C_LONG_SIZE == 32:
define CLong = int;
define CULong = uint;
$elif (C_LONG_SIZE == 16):
$elif C_LONG_SIZE == 16:
define CLong = short;
define CULong = ushort;
$else:
$assert(false, "Invalid C long size");
$endif;
$if (C_SHORT_SIZE == 32):
$if C_SHORT_SIZE == 32:
define CShort = int;
define CUShort = uint;
$elif (C_SHORT_SIZE == 16):
$elif C_SHORT_SIZE == 16:
define CShort = short;
define CUShort = ushort;
$elif (C_SHORT_SIZE == 8):
$elif C_SHORT_SIZE == 8:
define CShort = ichar;
define CUShort = char;
$else:
$assert(false, "Invalid C short size");
$endif;
$if (C_LONG_LONG_SIZE == 128):
$if C_LONG_LONG_SIZE == 128:
define CLongLong = int128;
define CULongLong = uint128;
$elif (C_LONG_LONG_SIZE == 64):
$elif C_LONG_LONG_SIZE == 64:
define CLongLong = long;
define CULongLong = ulong;
$elif (C_LONG_LONG_SIZE == 32):
$elif C_LONG_LONG_SIZE == 32:
define CLongLong = int;
define CULongLong = uint;
$elif (C_LONG_LONG_SIZE == 16):
$elif C_LONG_LONG_SIZE == 16:
define CLongLong = short;
define CULongLong = ushort;
$else:
@@ -74,7 +74,7 @@ $endif;
define CSChar = ichar;
define CUChar = char;
$if (${C_CHAR_IS_SIGNED}):
$if $$C_CHAR_IS_SIGNED:
define CChar = ichar;
$else:
define CChar = char;

View File

@@ -11,7 +11,48 @@ enum CompilerOptLevel
O3
}
const CompilerOptLevel COMPILER_OPT_LEVEL = (CompilerOptLevel)(${COMPILER_OPT_LEVEL});
const bool BIG_ENDIAN = ${PLATFORM_BIG_ENDIAN};
const bool I128_SUPPORT = ${PLATFORM_I128_SUPPORTED};
const bool COMPILER_SAFE_MODE = ${COMPILER_SAFE_MODE};
enum OsType
{
UNKNOWN,
NONE,
ANANAS,
CLOUD_ABI,
DRAGON_FLY,
FREEBSD,
FUCHSIA,
IOS,
KFREEBSD,
LINUX,
PS3,
MACOSX,
NETBSD,
OPENBSD,
SOLARIS,
WIN32,
HAIKU,
MINIX,
RTEMS,
NACL, // Native Client
CNK, // BG/P Compute-Node Kernel
AIX,
CUDA,
NVOPENCL,
AMDHSA,
PS4,
ELFIAMCU,
TVOS,
WATCHOS,
MESA3D,
CONTIKI,
AMDPAL,
HERMITCORE,
HURD,
WASI,
EMSCRIPTEN,
}
const OsType OS_TYPE = (OsType)($$OS_TYPE);
const CompilerOptLevel COMPILER_OPT_LEVEL = (CompilerOptLevel)($$COMPILER_OPT_LEVEL);
const bool BIG_ENDIAN = $$PLATFORM_BIG_ENDIAN;
const bool I128_SUPPORT = $$PLATFORM_I128_SUPPORTED;
const bool COMPILER_SAFE_MODE = $$COMPILER_SAFE_MODE;

View File

@@ -3,7 +3,10 @@
// a copy of which can be found in the LICENSE_STDLIB file.
module libc;
import std::cinterop;
import std::env;
import std::os::linux;
import std::os::macos;
import std::os::windows;
// stdlib
// Constants need to be per os/arch
@@ -160,7 +163,19 @@ enum Errno : ErrnoType
ENOTRECOVERABLE = 131, /* State not recoverable */
}
extern fn Errno errno();
fn Errno errno()
{
$if (env::OS_TYPE == OsType.WIN32):
return windows::errno();
$elif (env::OS_TYPE == OsType.MACOSX):
return macos::errno();
$elif (env::OS_TYPE == OsType.LINUX):
return linux::errno();
$else:
return Errno.ENOTRECOVERABLE;
$endif;
}
define TerminateFunction = fn void();
define CompareFunction = fn int(void*, void*);
@@ -233,15 +248,15 @@ const int EOF = -1;
const int FOPEN_MAX = 20;
const int FILENAME_MAX = 1024;
$if (${C_INT_SIZE} == 64):
$if $$C_INT_SIZE == 64:
define ErrnoType = long;
$elif (${C_INT_SIZE} == 32):
$elif $$C_INT_SIZE == 32:
define ErrnoType = int;
$else:
define ErrnoType = short;
$endif;
$if (${C_LONG_SIZE} == 64):
$if $$C_LONG_SIZE == 64:
define SeekIndex = long;
$else:
define SeekIndex = int;
@@ -287,7 +302,7 @@ extern fn void perror(char* str);
// time.h
$if (${C_LONG_SIZE} == 64):
$if $$C_LONG_SIZE == 64:
define TimeOffset = long;
$else:
define TimeOffset = int;

View File

@@ -0,0 +1,18 @@
module std::os::linux;
import std::env;
$if (env::OS_TYPE == OsType.LINUX):
extern fn int* __errno_location();
fn int errno() @inline
{
return *__errno_location();
}
fn void errno_set(int errno)
{
*(__errno_location()) = errno;
}
$endif;

View File

@@ -0,0 +1,15 @@
module std::os::macos;
import std::env;
$if (env::OS_TYPE == OsType.MACOSX):
extern fn int* __error();
fn int errno() @inline
{
return *__error();
}
fn void errno_set(int errno)
{
*(__error()) = errno;
}
$endif;

View File

@@ -0,0 +1,10 @@
module std::os::windows;
import std::env;
$if (env::OS_TYPE == OsType.WIN32):
extern fn int getLastError() @stdcall @extname("GetLastError");
fn int errno() @inline
{
return getLastError();
}
$endif;