vlib: replace macros that resolve to __builtin_bswapnn calls for tcc (#19305)

The tcc compiler does not have __builtin_bswap64, __builtin_bswap32,
and __builtin_bswap16 functions.  The various hton and ntoh macros
resolve down to these functions.  When compiling with tcc, we should
be using the analogous functions from net.conv.
This commit is contained in:
Kim Shrier 2023-09-07 22:42:28 -06:00 committed by GitHub
parent bea12e2623
commit 2d4ccf6829
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 59 additions and 10 deletions

View file

@ -1,6 +1,7 @@
module net module net
import io.util import io.util
import net.conv
import os import os
union AddrData { union AddrData {
@ -16,11 +17,17 @@ const (
// new_ip6 creates a new Addr from the IP6 address family, based on the given port and addr // new_ip6 creates a new Addr from the IP6 address family, based on the given port and addr
pub fn new_ip6(port u16, addr [16]u8) Addr { pub fn new_ip6(port u16, addr [16]u8) Addr {
n_port := $if tinyc {
conv.hton16(port)
} $else {
u16(C.htons(port))
}
a := Addr{ a := Addr{
f: u8(AddrFamily.ip6) f: u8(AddrFamily.ip6)
addr: AddrData{ addr: AddrData{
Ip6: Ip6{ Ip6: Ip6{
port: u16(C.htons(port)) port: n_port
} }
} }
} }
@ -32,11 +39,17 @@ pub fn new_ip6(port u16, addr [16]u8) Addr {
// new_ip creates a new Addr from the IPv4 address family, based on the given port and addr // new_ip creates a new Addr from the IPv4 address family, based on the given port and addr
pub fn new_ip(port u16, addr [4]u8) Addr { pub fn new_ip(port u16, addr [4]u8) Addr {
n_port := $if tinyc {
conv.hton16(port)
} $else {
u16(C.htons(port))
}
a := Addr{ a := Addr{
f: u8(AddrFamily.ip) f: u8(AddrFamily.ip)
addr: AddrData{ addr: AddrData{
Ip: Ip{ Ip: Ip{
port: u16(C.htons(port)) port: n_port
} }
} }
} }
@ -79,7 +92,11 @@ pub fn (a Ip) str() string {
} }
saddr := unsafe { cstring_to_vstring(&buf[0]) } saddr := unsafe { cstring_to_vstring(&buf[0]) }
port := C.ntohs(a.port) port := $if tinyc {
conv.hton16(a.port)
} $else {
C.ntohs(a.port)
}
return '${saddr}:${port}' return '${saddr}:${port}'
} }
@ -95,7 +112,11 @@ pub fn (a Ip6) str() string {
} }
saddr := unsafe { cstring_to_vstring(&buf[0]) } saddr := unsafe { cstring_to_vstring(&buf[0]) }
port := C.ntohs(a.port) port := $if tinyc {
conv.hton16(a.port)
} $else {
C.ntohs(a.port)
}
return '[${saddr}]:${port}' return '[${saddr}]:${port}'
} }

View file

@ -4,6 +4,7 @@
module websocket module websocket
import net import net
import net.conv
import net.http import net.http
import net.ssl import net.ssl
import net.urllib import net.urllib
@ -261,7 +262,12 @@ pub fn (mut ws Client) write_ptr(bytes &u8, payload_len int, code OPCode) !int {
if payload_len <= 125 { if payload_len <= 125 {
header[1] = u8(payload_len) header[1] = u8(payload_len)
} else if payload_len > 125 && payload_len <= 0xffff { } else if payload_len > 125 && payload_len <= 0xffff {
len16 := C.htons(payload_len) len16 := $if tinyc {
conv.hton16(u16(payload_len))
} $else {
C.htons(payload_len)
}
header[1] = 126 header[1] = 126
unsafe { C.memcpy(&header[2], &len16, 2) } unsafe { C.memcpy(&header[2], &len16, 2) }
} else if payload_len > 0xffff && payload_len <= 0x7fffffff { } else if payload_len > 0xffff && payload_len <= 0x7fffffff {
@ -277,7 +283,11 @@ pub fn (mut ws Client) write_ptr(bytes &u8, payload_len int, code OPCode) !int {
header[4] = masking_key[2] header[4] = masking_key[2]
header[5] = masking_key[3] header[5] = masking_key[3]
} else if payload_len > 125 && payload_len <= 0xffff { } else if payload_len > 125 && payload_len <= 0xffff {
len16 := C.htons(payload_len) len16 := $if tinyc {
conv.hton16(u16(payload_len))
} $else {
C.htons(payload_len)
}
header[1] = (126 | 0x80) header[1] = (126 | 0x80)
unsafe { C.memcpy(&header[2], &len16, 2) } unsafe { C.memcpy(&header[2], &len16, 2) }
header[4] = masking_key[0] header[4] = masking_key[0]
@ -346,7 +356,11 @@ pub fn (mut ws Client) close(code int, message string) ! {
ws.set_state(.closing) ws.set_state(.closing)
// mut code32 := 0 // mut code32 := 0
if code > 0 { if code > 0 {
code_ := C.htons(code) code_ := $if tinyc {
conv.hton16(u16(code))
} $else {
C.htons(code)
}
message_len := message.len + 2 message_len := message.len + 2
mut close_frame := []u8{len: message_len} mut close_frame := []u8{len: message_len}
close_frame[0] = u8(code_ & 0xFF) close_frame[0] = u8(code_ & 0xFF)

View file

@ -1,6 +1,7 @@
module picoev module picoev
import net import net
import net.conv
import picohttpparser import picohttpparser
#include <errno.h> #include <errno.h>
@ -123,10 +124,23 @@ fn listen(config Config) int {
} }
// addr settings // addr settings
sin_port := $if tinyc {
conv.hton16(u16(config.port))
} $else {
C.htons(config.port)
}
sin_addr := $if tinyc {
conv.hton32(u32(C.INADDR_ANY))
} $else {
C.htonl(C.INADDR_ANY)
}
mut addr := C.sockaddr_in{ mut addr := C.sockaddr_in{
sin_family: u8(C.AF_INET) sin_family: u8(C.AF_INET)
sin_port: C.htons(config.port) sin_port: sin_port
sin_addr: C.htonl(C.INADDR_ANY) sin_addr: sin_addr
} }
size := sizeof(C.sockaddr_in) size := sizeof(C.sockaddr_in)
bind_res := C.bind(fd, voidptr(unsafe { &net.Addr(&addr) }), size) bind_res := C.bind(fd, voidptr(unsafe { &net.Addr(&addr) }), size)

View file

@ -803,7 +803,7 @@ static inline uint64_t _wymix(uint64_t A, uint64_t B){ _wymum(&A,&B); return A^B
#if (WYHASH_LITTLE_ENDIAN) #if (WYHASH_LITTLE_ENDIAN)
static inline uint64_t _wyr8(const uint8_t *p) { uint64_t v; memcpy(&v, p, 8); return v;} static inline uint64_t _wyr8(const uint8_t *p) { uint64_t v; memcpy(&v, p, 8); return v;}
static inline uint64_t _wyr4(const uint8_t *p) { uint32_t v; memcpy(&v, p, 4); return v;} static inline uint64_t _wyr4(const uint8_t *p) { uint32_t v; memcpy(&v, p, 4); return v;}
#elif defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__) #elif !defined(__TINYC__) && (defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__))
static inline uint64_t _wyr8(const uint8_t *p) { uint64_t v; memcpy(&v, p, 8); return __builtin_bswap64(v);} static inline uint64_t _wyr8(const uint8_t *p) { uint64_t v; memcpy(&v, p, 8); return __builtin_bswap64(v);}
static inline uint64_t _wyr4(const uint8_t *p) { uint32_t v; memcpy(&v, p, 4); return __builtin_bswap32(v);} static inline uint64_t _wyr4(const uint8_t *p) { uint32_t v; memcpy(&v, p, 4); return __builtin_bswap32(v);}
#elif defined(_MSC_VER) #elif defined(_MSC_VER)