1
1
mirror of https://github.com/tweag/asterius.git synced 2024-09-11 08:55:32 +03:00

Remove bytestring shims

This commit is contained in:
Cheng Shao 2020-10-17 13:26:01 +00:00
parent 07b3462370
commit 4dddcb3c98
6 changed files with 316 additions and 125 deletions

90
asterius/libc/fpstring.c Normal file
View File

@ -0,0 +1,90 @@
/*
* Copyright (c) 2003 David Roundy
* Copyright (c) 2005-6 Don Stewart
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the names of the authors or the names of any contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "fpstring.h"
/* copy a string in reverse */
void fps_reverse(unsigned char *q, unsigned char *p, unsigned long n) {
p += n-1;
while (n-- != 0)
*q++ = *p--;
}
/* duplicate a string, interspersing the character through the elements
of the duplicated string */
void fps_intersperse(unsigned char *q,
unsigned char *p,
unsigned long n,
unsigned char c) {
while (n > 1) {
*q++ = *p++;
*q++ = c;
n--;
}
if (n == 1)
*q = *p;
}
/* find maximum char in a packed string */
unsigned char fps_maximum(unsigned char *p, unsigned long len) {
unsigned char *q, c = *p;
for (q = p; q < p + len; q++)
if (*q > c)
c = *q;
return c;
}
/* find minimum char in a packed string */
unsigned char fps_minimum(unsigned char *p, unsigned long len) {
unsigned char *q, c = *p;
for (q = p; q < p + len; q++)
if (*q < c)
c = *q;
return c;
}
/* count the number of occurences of a char in a string */
unsigned long fps_count(unsigned char *p, unsigned long len, unsigned char w) {
unsigned long c;
for (c = 0; len-- != 0; ++p)
if (*p == w)
++c;
return c;
}
/* This wrapper is here so that we can copy a sub-range of a ByteArray#.
We cannot construct a pointer to the interior of an unpinned ByteArray#,
except by doing an unsafe ffi call, and adjusting the pointer C-side. */
void * fps_memcpy_offsets(void *dst, unsigned long dst_off,
const void *src, unsigned long src_off, size_t n) {
return memcpy(dst + dst_off, src + src_off, n);
}

9
asterius/libc/fpstring.h Normal file
View File

@ -0,0 +1,9 @@
#include <string.h>
void fps_reverse(unsigned char *dest, unsigned char *from, unsigned long len);
void fps_intersperse(unsigned char *dest, unsigned char *from, unsigned long len, unsigned char c);
unsigned char fps_maximum(unsigned char *p, unsigned long len);
unsigned char fps_minimum(unsigned char *p, unsigned long len);
unsigned long fps_count(unsigned char *p, unsigned long len, unsigned char w);

215
asterius/libc/itoa.c Normal file
View File

@ -0,0 +1,215 @@
///////////////////////////////////////////////////////////////
// Encoding numbers using ASCII characters //
// //
// inspired by: http://www.jb.man.ac.uk/~slowe/cpp/itoa.html //
///////////////////////////////////////////////////////////////
#include <stdio.h>
// Decimal Encoding
///////////////////
static const char* digits = "0123456789abcdef";
// signed integers
char* _hs_bytestring_int_dec (int x, char* buf)
{
char c, *ptr = buf, *next_free;
int x_tmp;
// we cannot negate directly as 0 - (minBound :: Int) = minBound
if (x < 0) {
*ptr++ = '-';
buf++;
x_tmp = x;
x /= 10;
*ptr++ = digits[x * 10 - x_tmp];
if (x == 0)
return ptr;
else
x = -x;
}
// encode positive number as little-endian decimal
do {
x_tmp = x;
x /= 10;
*ptr++ = digits[x_tmp - x * 10];
} while ( x );
// reverse written digits
next_free = ptr--;
while (buf < ptr) {
c = *ptr;
*ptr-- = *buf;
*buf++ = c;
}
return next_free;
}
// signed long long ints (64 bit integers)
char* _hs_bytestring_long_long_int_dec (long long int x, char* buf)
{
char c, *ptr = buf, *next_free;
long long int x_tmp;
// we cannot negate directly as 0 - (minBound :: Int) = minBound
if (x < 0) {
*ptr++ = '-';
buf++;
x_tmp = x;
x /= 10;
*ptr++ = digits[x * 10 - x_tmp];
if (x == 0)
return ptr;
else
x = -x;
}
// encode positive number as little-endian decimal
do {
x_tmp = x;
x /= 10;
*ptr++ = digits[x_tmp - x * 10];
} while ( x );
// reverse written digits
next_free = ptr--;
while (buf < ptr) {
c = *ptr;
*ptr-- = *buf;
*buf++ = c;
}
return next_free;
}
// unsigned integers
char* _hs_bytestring_uint_dec (unsigned int x, char* buf)
{
char c, *ptr = buf, *next_free;
unsigned int x_tmp;
// encode positive number as little-endian decimal
do {
x_tmp = x;
x /= 10;
*ptr++ = digits[x_tmp - x * 10];
} while ( x );
// reverse written digits
next_free = ptr--;
while (buf < ptr) {
c = *ptr;
*ptr-- = *buf;
*buf++ = c;
}
return next_free;
}
// unsigned long ints
char* _hs_bytestring_long_long_uint_dec (long long unsigned int x, char* buf)
{
char c, *ptr = buf, *next_free;
long long unsigned int x_tmp;
// encode positive number as little-endian decimal
do {
x_tmp = x;
x /= 10;
*ptr++ = digits[x_tmp - x * 10];
} while ( x );
// reverse written digits
next_free = ptr--;
while (buf < ptr) {
c = *ptr;
*ptr-- = *buf;
*buf++ = c;
}
return next_free;
}
// Padded, decimal, positive integers for the decimal output of bignums
///////////////////////////////////////////////////////////////////////
// Padded (9 digits), decimal, positive int:
// We will use it with numbers that fit in 31 bits; i.e., numbers smaller than
// 10^9, as "31 * log 2 / log 10 = 9.33"
void _hs_bytestring_int_dec_padded9 (int x, char* buf)
{
const int max_width_int32_dec = 9;
char* ptr = buf + max_width_int32_dec;
int x_tmp;
// encode positive number as little-endian decimal
do {
x_tmp = x;
x /= 10;
*(--ptr) = digits[x_tmp - x * 10];
} while ( x );
// pad beginning
while (buf < ptr) { *(--ptr) = '0'; }
}
// Padded (19 digits), decimal, positive long long int:
// We will use it with numbers that fit in 63 bits; i.e., numbers smaller than
// 10^18, as "63 * log 2 / log 10 = 18.96"
void _hs_bytestring_long_long_int_dec_padded18 (long long int x, char* buf)
{
const int max_width_int64_dec = 18;
char* ptr = buf + max_width_int64_dec;
long long int x_tmp;
// encode positive number as little-endian decimal
do {
x_tmp = x;
x /= 10;
*(--ptr) = digits[x_tmp - x * 10];
} while ( x );
// pad beginning
while (buf < ptr) { *(--ptr) = '0'; }
}
///////////////////////
// Hexadecimal encoding
///////////////////////
// unsigned ints (32 bit words)
char* _hs_bytestring_uint_hex (unsigned int x, char* buf) {
// write hex representation in reverse order
char c, *ptr = buf, *next_free;
do {
*ptr++ = digits[x & 0xf];
x >>= 4;
} while ( x );
// invert written digits
next_free = ptr--;
while(buf < ptr) {
c = *ptr;
*ptr-- = *buf;
*buf++ = c;
}
return next_free;
};
// unsigned long ints (64 bit words)
char* _hs_bytestring_long_long_uint_hex (long long unsigned int x, char* buf) {
// write hex representation in reverse order
char c, *ptr = buf, *next_free;
do {
*ptr++ = digits[x & 0xf];
x >>= 4;
} while ( x );
// invert written digits
next_free = ptr--;
while(buf < ptr) {
c = *ptr;
*ptr-- = *buf;
*buf++ = c;
}
return next_free;
};

View File

@ -1,95 +0,0 @@
import { Memory } from "./rts.memory.mjs";
export class ByteStringCBits {
constructor(memory) {
this.memory = memory;
Object.seal(this);
}
fps_reverse(_q, _p, n) {
const q = Memory.unTag(_q),
p = Memory.unTag(_p);
this.memory.i8View.copyWithin(q, p, p + n);
this.memory.i8View.subarray(q, q + n).reverse();
}
fps_intersperse(_q, _p, n, c) {
let q = Memory.unTag(_q),
p = Memory.unTag(_p);
while (n > 1) {
this.memory.i8View[q++] = this.memory.i8View[p++];
this.memory.i8View[q++] = c;
--n;
}
if (n === 1) this.memory.i8View[q] = this.memory.i8View[p];
}
fps_maximum(_p, len) {
const p = Memory.unTag(_p),
buffer = this.memory.i8View.subarray(p, p + len);
return buffer.reduce((x, y) => Math.max(x, y), buffer[0]);
}
fps_minimum(_p, len) {
const p = Memory.unTag(_p),
buffer = this.memory.i8View.subarray(p, p + len);
return buffer.reduce((x, y) => Math.min(x, y), buffer[0]);
}
fps_count(_p, len, w) {
const p = Memory.unTag(_p),
buffer = this.memory.i8View.subarray(p, p + len);
return buffer.reduce((tot, c) => (c === w ? tot + 1 : tot), 0);
}
fps_memcpy_offsets(_dst, dst_off, _src, src_off, n) {
const dst = Memory.unTag(_dst),
src = Memory.unTag(_src);
this.memory.i8View.copyWithin(
dst + dst_off,
src + src_off,
src + src_off + n
);
return _dst + dst_off;
}
_hs_bytestring_itoa(x, _buf, base, pad) {
const buf = Memory.unTag(_buf),
x_str = x.toString(base).padStart(pad, "0");
for (let i = 0; i < x_str.length; ++i)
this.memory.i8View[buf + i] = x_str.codePointAt(i);
return _buf + x_str.length;
}
_hs_bytestring_int_dec(x, _buf) {
return this._hs_bytestring_itoa(x, _buf, 10, 0);
}
_hs_bytestring_long_long_int_dec(x, _buf) {
return this._hs_bytestring_itoa(x, _buf, 10, 0);
}
_hs_bytestring_uint_dec(x, _buf) {
return this._hs_bytestring_itoa(x, _buf, 10, 0);
}
_hs_bytestring_long_long_uint_dec(x, _buf) {
return this._hs_bytestring_itoa(x, _buf, 10, 0);
}
_hs_bytestring_int_dec_padded9(x, _buf) {
this._hs_bytestring_itoa(x, _buf, 10, 9);
}
_hs_bytestring_long_long_int_dec_padded18(x, _buf) {
this._hs_bytestring_itoa(x, _buf, 10, 18);
}
_hs_bytestring_uint_hex(x, _buf) {
return this._hs_bytestring_itoa(x, _buf, 16, 0);
}
_hs_bytestring_long_long_uint_hex(x, _buf) {
return this._hs_bytestring_itoa(x, _buf, 16, 0);
}
}

View File

@ -11,7 +11,6 @@ import { StableNameManager } from "./rts.stablename.mjs";
import { StaticPtrManager } from "./rts.staticptr.mjs";
import { Scheduler } from "./rts.scheduler.mjs";
import { IntegerManager } from "./rts.integer.mjs";
import { ByteStringCBits } from "./rts.bytestring.mjs";
import { TextCBits } from "./rts.text.mjs";
import { TimeCBits } from "./rts.time.mjs";
import { GC } from "./rts.gc.mjs";
@ -104,7 +103,6 @@ export async function newAsteriusInstance(req) {
__asterius_stableptr_manager
),
__asterius_integer_manager = new IntegerManager(),
__asterius_bytestring_cbits = new ByteStringCBits(null),
__asterius_text_cbits = new TextCBits(__asterius_memory),
__asterius_time_cbits = new TimeCBits(__asterius_memory, req.targetSpecificModule),
__asterius_gc = new GC(
@ -183,7 +181,6 @@ export async function newAsteriusInstance(req) {
write: (fd, buf, count) => __asterius_fs.write(fd, buf, count)
},
posix: modulify(new (req.targetSpecificModule.posix)(__asterius_memory, rtsConstants)),
bytestring: modulify(__asterius_bytestring_cbits),
text: modulify(__asterius_text_cbits),
time: modulify(__asterius_time_cbits),
// cannot name this float since float is a keyword.

View File

@ -136,8 +136,7 @@ rtsAsteriusModule opts =
SM.fromList $
map
(\(func_sym, (_, func)) -> (func_sym, func))
( byteStringCBits
<> floatCBits
( floatCBits
<> textCBits
)
}
@ -616,7 +615,7 @@ rtsFunctionImports debug =
)
<> map
(fst . snd)
( byteStringCBits <> floatCBits <> textCBits
( floatCBits <> textCBits
)
<> schedulerImports
<> exportsImports
@ -712,30 +711,6 @@ rtsGlobalExports = mempty
emitErrorMessage :: [ValueType] -> BS.ByteString -> Expression
emitErrorMessage vts ev = Barf {barfMessage = ev, barfReturnTypes = vts}
byteStringCBits :: [(EntitySymbol, (FunctionImport, Function))]
byteStringCBits =
map
( \(func_sym, param_vts, ret_vts) ->
( mkEntitySymbol func_sym,
generateRTSWrapper "bytestring" func_sym param_vts ret_vts
)
)
[ ("fps_reverse", [I64, I64, I64], []),
("fps_intersperse", [I64, I64, I64, I64], []),
("fps_maximum", [I64, I64], [I64]),
("fps_minimum", [I64, I64], [I64]),
("fps_count", [I64, I64, I64], [I64]),
("fps_memcpy_offsets", [I64, I64, I64, I64, I64], [I64]),
("_hs_bytestring_int_dec", [I64, I64], [I64]),
("_hs_bytestring_long_long_int_dec", [I64, I64], [I64]),
("_hs_bytestring_uint_dec", [I64, I64], [I64]),
("_hs_bytestring_long_long_uint_dec", [I64, I64], [I64]),
("_hs_bytestring_int_dec_padded9", [I64, I64], []),
("_hs_bytestring_long_long_int_dec_padded18", [I64, I64], []),
("_hs_bytestring_uint_hex", [I64, I64], [I64]),
("_hs_bytestring_long_long_uint_hex", [I64, I64], [I64])
]
textCBits :: [(EntitySymbol, (FunctionImport, Function))]
textCBits =
map