Skip to content

Commit

Permalink
vlib: replace macros that resolve to __builtin_bswapnn calls for tcc (#…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
kimshrier committed Sep 8, 2023
1 parent bea12e2 commit 2d4ccf6
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 10 deletions.
29 changes: 25 additions & 4 deletions vlib/net/address.v
@@ -1,6 +1,7 @@
module net

import io.util
import net.conv
import os

union AddrData {
Expand All @@ -16,11 +17,17 @@ const (

// 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 {
n_port := $if tinyc {
conv.hton16(port)
} $else {
u16(C.htons(port))
}

a := Addr{
f: u8(AddrFamily.ip6)
addr: AddrData{
Ip6: Ip6{
port: u16(C.htons(port))
port: n_port
}
}
}
Expand All @@ -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
pub fn new_ip(port u16, addr [4]u8) Addr {
n_port := $if tinyc {
conv.hton16(port)
} $else {
u16(C.htons(port))
}

a := Addr{
f: u8(AddrFamily.ip)
addr: AddrData{
Ip: Ip{
port: u16(C.htons(port))
port: n_port
}
}
}
Expand Down Expand Up @@ -79,7 +92,11 @@ pub fn (a Ip) str() string {
}

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}'
}
Expand All @@ -95,7 +112,11 @@ pub fn (a Ip6) str() string {
}

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}'
}
Expand Down
20 changes: 17 additions & 3 deletions vlib/net/websocket/websocket_client.v
Expand Up @@ -4,6 +4,7 @@
module websocket

import net
import net.conv
import net.http
import net.ssl
import net.urllib
Expand Down Expand Up @@ -261,7 +262,12 @@ pub fn (mut ws Client) write_ptr(bytes &u8, payload_len int, code OPCode) !int {
if payload_len <= 125 {
header[1] = u8(payload_len)
} 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
unsafe { C.memcpy(&header[2], &len16, 2) }
} else if payload_len > 0xffff && payload_len <= 0x7fffffff {
Expand All @@ -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[5] = masking_key[3]
} 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)
unsafe { C.memcpy(&header[2], &len16, 2) }
header[4] = masking_key[0]
Expand Down Expand Up @@ -346,7 +356,11 @@ pub fn (mut ws Client) close(code int, message string) ! {
ws.set_state(.closing)
// mut code32 := 0
if code > 0 {
code_ := C.htons(code)
code_ := $if tinyc {
conv.hton16(u16(code))
} $else {
C.htons(code)
}
message_len := message.len + 2
mut close_frame := []u8{len: message_len}
close_frame[0] = u8(code_ & 0xFF)
Expand Down
18 changes: 16 additions & 2 deletions vlib/picoev/socket_util.c.v
@@ -1,6 +1,7 @@
module picoev

import net
import net.conv
import picohttpparser

#include <errno.h>
Expand Down Expand Up @@ -123,10 +124,23 @@ fn listen(config Config) int {
}

// 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{
sin_family: u8(C.AF_INET)
sin_port: C.htons(config.port)
sin_addr: C.htonl(C.INADDR_ANY)
sin_port: sin_port
sin_addr: sin_addr
}
size := sizeof(C.sockaddr_in)
bind_res := C.bind(fd, voidptr(unsafe { &net.Addr(&addr) }), size)
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/gen/c/cheaders.v
Expand Up @@ -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)
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;}
#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 _wyr4(const uint8_t *p) { uint32_t v; memcpy(&v, p, 4); return __builtin_bswap32(v);}
#elif defined(_MSC_VER)
Expand Down

0 comments on commit 2d4ccf6

Please sign in to comment.