mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
compiler: add support for $if x32, x64, big_endian, little_endian
This commit is contained in:
committed by
Alexander Medvednikov
parent
751a89ccc8
commit
e577b40743
@@ -12,6 +12,22 @@ c_headers = '
|
||||
#include <stdarg.h> // for va_list
|
||||
#include <string.h> // memcpy
|
||||
|
||||
#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
|
||||
|
||||
#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__)
|
||||
#define TARGET_ORDER_IS_BIG
|
||||
#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)
|
||||
#define TARGET_ORDER_IS_LITTLE
|
||||
#else
|
||||
#error "Unknown architecture endianness"
|
||||
#endif
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <ctype.h>
|
||||
#include <locale.h> // tolower
|
||||
|
||||
@@ -62,18 +62,27 @@ fn (p mut Parser) comp_time() {
|
||||
p.genln('#endif')
|
||||
}
|
||||
}
|
||||
else if name == 'x64' {
|
||||
p.comptime_if_block('TARGET_IS_64BIT')
|
||||
}
|
||||
else if name == 'x32' {
|
||||
p.comptime_if_block('TARGET_IS_32BIT')
|
||||
}
|
||||
else if name == 'big_endian' {
|
||||
p.comptime_if_block('TARGET_ORDER_IS_BIG')
|
||||
}
|
||||
else if name == 'little_endian' {
|
||||
p.comptime_if_block('TARGET_ORDER_IS_LITTLE')
|
||||
}
|
||||
else if name == 'debug' {
|
||||
p.genln('#ifdef VDEBUG')
|
||||
p.check(.lcbr)
|
||||
p.statements_no_rcbr()
|
||||
p.genln('#endif')
|
||||
p.comptime_if_block('VDEBUG')
|
||||
}
|
||||
else if name == 'tinyc' {
|
||||
p.comptime_if_block('__TINYC__')
|
||||
}
|
||||
else if name == 'glibc' {
|
||||
p.comptime_if_block('__GLIBC__')
|
||||
}
|
||||
}
|
||||
else if name == 'mingw' {
|
||||
p.comptime_if_block('__MINGW32__')
|
||||
}
|
||||
|
||||
26
vlib/compiler/tests/comptime_bittness_and_endianess_test.v
Normal file
26
vlib/compiler/tests/comptime_bittness_and_endianess_test.v
Normal file
@@ -0,0 +1,26 @@
|
||||
|
||||
fn test_bitness(){
|
||||
mut x := 0
|
||||
$if x32 {
|
||||
println('system is 32 bit')
|
||||
x = 1
|
||||
}
|
||||
$if x64 {
|
||||
println('system is 64 bit')
|
||||
x = 2
|
||||
}
|
||||
assert x > 0
|
||||
}
|
||||
|
||||
fn test_endianness(){
|
||||
mut x := 0
|
||||
$if little_endian {
|
||||
println('system is little endian')
|
||||
x = 1
|
||||
}
|
||||
$if big_endian {
|
||||
println('system is big endian')
|
||||
x = 2
|
||||
}
|
||||
assert x > 0
|
||||
}
|
||||
Reference in New Issue
Block a user