2010-06-23 23:03:09 -05:00
|
|
|
|
|
|
|
#include "rust_internal.h"
|
|
|
|
|
2011-04-29 13:54:06 -05:00
|
|
|
#if !defined(__WIN32__)
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
/* Native builtins. */
|
|
|
|
|
|
|
|
extern "C" CDECL rust_str*
|
|
|
|
last_os_error(rust_task *task) {
|
2011-06-28 14:15:41 -05:00
|
|
|
rust_scheduler *sched = task->sched;
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, task, "last_os_error()");
|
2010-06-23 23:03:09 -05:00
|
|
|
|
|
|
|
#if defined(__WIN32__)
|
|
|
|
LPTSTR buf;
|
|
|
|
DWORD err = GetLastError();
|
|
|
|
DWORD res = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
NULL, err,
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
(LPTSTR) &buf, 0, NULL);
|
|
|
|
if (!res) {
|
|
|
|
task->fail(1);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#elif defined(_GNU_SOURCE)
|
2011-01-14 15:41:39 -06:00
|
|
|
char cbuf[BUF_BYTES];
|
2010-06-23 23:03:09 -05:00
|
|
|
char *buf = strerror_r(errno, cbuf, sizeof(cbuf));
|
|
|
|
if (!buf) {
|
|
|
|
task->fail(1);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#else
|
2011-01-14 15:41:39 -06:00
|
|
|
char buf[BUF_BYTES];
|
2010-06-23 23:03:09 -05:00
|
|
|
int err = strerror_r(errno, buf, sizeof(buf));
|
|
|
|
if (err) {
|
|
|
|
task->fail(1);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
size_t fill = strlen(buf) + 1;
|
|
|
|
size_t alloc = next_power_of_two(sizeof(rust_str) + fill);
|
2011-06-27 21:15:03 -05:00
|
|
|
void *mem = task->malloc(alloc, memory_region::LOCAL);
|
2010-06-23 23:03:09 -05:00
|
|
|
if (!mem) {
|
|
|
|
task->fail(1);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-06-29 17:11:20 -05:00
|
|
|
rust_str *st = new (mem) rust_str(sched, alloc, fill,
|
|
|
|
(const uint8_t *)buf);
|
2010-06-23 23:03:09 -05:00
|
|
|
|
|
|
|
#ifdef __WIN32__
|
|
|
|
LocalFree((HLOCAL)buf);
|
|
|
|
#endif
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
2011-06-17 13:12:51 -05:00
|
|
|
extern "C" CDECL rust_str *
|
|
|
|
rust_getcwd(rust_task *task) {
|
2011-06-28 14:15:41 -05:00
|
|
|
rust_scheduler *sched = task->sched;
|
2011-06-17 13:12:51 -05:00
|
|
|
LOG(task, task, "rust_getcwd()");
|
|
|
|
|
|
|
|
char cbuf[BUF_BYTES];
|
|
|
|
|
|
|
|
#if defined(__WIN32__)
|
|
|
|
if (!_getcwd(cbuf, sizeof(cbuf))) {
|
|
|
|
#else
|
|
|
|
if (!getcwd(cbuf, sizeof(cbuf))) {
|
|
|
|
#endif
|
|
|
|
task->fail(1);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t fill = strlen(cbuf) + 1;
|
|
|
|
size_t alloc = next_power_of_two(sizeof(rust_str) + fill);
|
2011-06-27 21:15:03 -05:00
|
|
|
void *mem = task->malloc(alloc, memory_region::LOCAL);
|
2011-06-17 13:12:51 -05:00
|
|
|
if (!mem) {
|
|
|
|
task->fail(1);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
rust_str *st;
|
2011-06-28 14:15:41 -05:00
|
|
|
st = new (mem) rust_str(sched, alloc, fill, (const uint8_t *)cbuf);
|
2011-06-17 13:12:51 -05:00
|
|
|
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
2011-03-24 20:20:49 -05:00
|
|
|
extern "C" CDECL
|
|
|
|
void squareroot(rust_task *task, double *input, double *output) {
|
|
|
|
*output = sqrt(*input);
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
extern "C" CDECL size_t
|
|
|
|
size_of(rust_task *task, type_desc *t) {
|
|
|
|
return t->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL size_t
|
|
|
|
align_of(rust_task *task, type_desc *t) {
|
|
|
|
return t->align;
|
|
|
|
}
|
|
|
|
|
2010-11-09 16:15:07 -06:00
|
|
|
extern "C" CDECL intptr_t
|
|
|
|
refcount(rust_task *task, type_desc *t, intptr_t *v) {
|
|
|
|
|
|
|
|
if (*v == CONST_REFCOUNT)
|
|
|
|
return CONST_REFCOUNT;
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
// Passed-in value has refcount 1 too high
|
|
|
|
// because it was ref'ed while making the call.
|
|
|
|
return (*v) - 1;
|
|
|
|
}
|
|
|
|
|
2010-06-26 01:57:30 -05:00
|
|
|
extern "C" CDECL void
|
2010-11-02 13:11:58 -05:00
|
|
|
do_gc(rust_task *task) {
|
2010-06-26 01:57:30 -05:00
|
|
|
task->gc(1);
|
|
|
|
}
|
|
|
|
|
2010-07-05 16:43:40 -05:00
|
|
|
extern "C" CDECL void
|
|
|
|
unsupervise(rust_task *task) {
|
|
|
|
task->unsupervise();
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
extern "C" CDECL rust_vec*
|
2010-07-22 19:47:32 -05:00
|
|
|
vec_alloc(rust_task *task, type_desc *t, type_desc *elem_t, size_t n_elts)
|
2010-06-23 23:03:09 -05:00
|
|
|
{
|
2011-06-28 14:15:41 -05:00
|
|
|
rust_scheduler *sched = task->sched;
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, mem, "vec_alloc %" PRIdPTR " elements of size %" PRIdPTR,
|
|
|
|
n_elts, elem_t->size);
|
2010-07-22 19:47:32 -05:00
|
|
|
size_t fill = n_elts * elem_t->size;
|
2010-06-23 23:03:09 -05:00
|
|
|
size_t alloc = next_power_of_two(sizeof(rust_vec) + fill);
|
2010-07-22 19:47:32 -05:00
|
|
|
void *mem = task->malloc(alloc, t->is_stateful ? t : NULL);
|
2010-06-23 23:03:09 -05:00
|
|
|
if (!mem) {
|
2010-07-26 00:21:07 -05:00
|
|
|
task->fail(4);
|
2010-06-23 23:03:09 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-06-28 14:15:41 -05:00
|
|
|
rust_vec *vec = new (mem) rust_vec(sched, alloc, 0, NULL);
|
2010-06-23 23:03:09 -05:00
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
2011-03-18 15:53:49 -05:00
|
|
|
extern "C" CDECL rust_vec*
|
|
|
|
vec_alloc_mut(rust_task *task, type_desc *t, type_desc *elem_t, size_t n_elts)
|
|
|
|
{
|
|
|
|
return vec_alloc(task, t, elem_t, n_elts);
|
|
|
|
}
|
|
|
|
|
2010-08-11 18:06:45 -05:00
|
|
|
extern "C" CDECL void *
|
|
|
|
vec_buf(rust_task *task, type_desc *ty, rust_vec *v, size_t offset)
|
|
|
|
{
|
|
|
|
return (void *)&v->data[ty->size * offset];
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL size_t
|
|
|
|
vec_len(rust_task *task, type_desc *ty, rust_vec *v)
|
|
|
|
{
|
|
|
|
return v->fill / ty->size;
|
|
|
|
}
|
|
|
|
|
2010-08-19 19:37:22 -05:00
|
|
|
extern "C" CDECL void
|
|
|
|
vec_len_set(rust_task *task, type_desc *ty, rust_vec *v, size_t len)
|
|
|
|
{
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, "vec_len_set(0x%" PRIxPTR ", %" PRIdPTR ") on vec with "
|
|
|
|
"alloc = %" PRIdPTR
|
|
|
|
", fill = %" PRIdPTR
|
|
|
|
", len = %" PRIdPTR
|
|
|
|
". New fill is %" PRIdPTR,
|
|
|
|
v, len, v->alloc, v->fill, v->fill / ty->size, len * ty->size);
|
2010-08-19 19:37:22 -05:00
|
|
|
v->fill = len * ty->size;
|
|
|
|
}
|
|
|
|
|
2010-08-12 15:11:49 -05:00
|
|
|
extern "C" CDECL void
|
|
|
|
vec_print_debug_info(rust_task *task, type_desc *ty, rust_vec *v)
|
|
|
|
{
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib,
|
|
|
|
"vec_print_debug_info(0x%" PRIxPTR ")"
|
|
|
|
" with tydesc 0x%" PRIxPTR
|
|
|
|
" (size = %" PRIdPTR ", align = %" PRIdPTR ")"
|
|
|
|
" alloc = %" PRIdPTR ", fill = %" PRIdPTR ", len = %" PRIdPTR
|
|
|
|
" , data = ...",
|
|
|
|
v,
|
|
|
|
ty,
|
|
|
|
ty->size,
|
|
|
|
ty->align,
|
|
|
|
v->alloc,
|
|
|
|
v->fill,
|
|
|
|
v->fill / ty->size);
|
2010-08-12 15:11:49 -05:00
|
|
|
|
|
|
|
for (size_t i = 0; i < v->fill; ++i) {
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, " %" PRIdPTR ": 0x%" PRIxPTR, i, v->data[i]);
|
2010-08-12 15:11:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-11 18:06:45 -05:00
|
|
|
/* Helper for str_alloc and str_from_vec. Returns NULL as failure. */
|
2011-03-10 08:56:51 -06:00
|
|
|
static rust_vec*
|
|
|
|
vec_alloc_with_data(rust_task *task,
|
|
|
|
size_t n_elts,
|
2010-08-11 18:06:45 -05:00
|
|
|
size_t fill,
|
2011-03-10 08:56:51 -06:00
|
|
|
size_t elt_size,
|
|
|
|
void *d)
|
2010-08-11 18:06:45 -05:00
|
|
|
{
|
2011-06-28 14:15:41 -05:00
|
|
|
rust_scheduler *sched = task->sched;
|
2011-03-10 08:56:51 -06:00
|
|
|
size_t alloc = next_power_of_two(sizeof(rust_vec) + (n_elts * elt_size));
|
2011-06-27 21:15:03 -05:00
|
|
|
void *mem = task->malloc(alloc, memory_region::LOCAL);
|
2011-03-10 08:56:51 -06:00
|
|
|
if (!mem) return NULL;
|
2011-06-28 14:15:41 -05:00
|
|
|
return new (mem) rust_vec(sched, alloc, fill * elt_size, (uint8_t*)d);
|
2010-08-11 18:06:45 -05:00
|
|
|
}
|
|
|
|
|
2011-03-24 19:21:09 -05:00
|
|
|
extern "C" CDECL rust_vec*
|
|
|
|
vec_from_vbuf(rust_task *task, type_desc *ty, void *vbuf, size_t n_elts)
|
|
|
|
{
|
|
|
|
return vec_alloc_with_data(task, n_elts, n_elts * ty->size, ty->size,
|
|
|
|
vbuf);
|
|
|
|
}
|
|
|
|
|
2011-04-27 20:10:02 -05:00
|
|
|
extern "C" CDECL rust_vec*
|
|
|
|
unsafe_vec_to_mut(rust_task *task, type_desc *ty, rust_vec *v)
|
|
|
|
{
|
|
|
|
if (v->ref_count != CONST_REFCOUNT) {
|
|
|
|
v->ref();
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2010-08-11 18:06:45 -05:00
|
|
|
extern "C" CDECL rust_str*
|
|
|
|
str_alloc(rust_task *task, size_t n_bytes)
|
|
|
|
{
|
2011-03-10 08:56:51 -06:00
|
|
|
rust_str *st = vec_alloc_with_data(task,
|
2010-08-11 18:06:45 -05:00
|
|
|
n_bytes + 1, // +1 to fit at least ""
|
2011-03-10 08:56:51 -06:00
|
|
|
1, 1,
|
|
|
|
(void*)"");
|
2010-08-11 18:06:45 -05:00
|
|
|
if (!st) {
|
|
|
|
task->fail(2);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
2011-03-24 06:11:32 -05:00
|
|
|
extern "C" CDECL rust_str*
|
|
|
|
str_push_byte(rust_task* task, rust_str* v, size_t byte)
|
|
|
|
{
|
|
|
|
size_t fill = v->fill;
|
|
|
|
size_t alloc = next_power_of_two(sizeof(rust_vec) + fill + 1);
|
|
|
|
if (v->ref_count > 1 || v->alloc < alloc) {
|
|
|
|
v = vec_alloc_with_data(task, fill + 1, fill, 1, (void*)&v->data[0]);
|
|
|
|
if (!v) {
|
|
|
|
task->fail(2);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (v->ref_count != CONST_REFCOUNT) {
|
|
|
|
v->ref();
|
|
|
|
}
|
|
|
|
v->data[fill-1] = (char)byte;
|
|
|
|
v->data[fill] = '\0';
|
|
|
|
v->fill++;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2011-05-10 23:05:03 -05:00
|
|
|
extern "C" CDECL rust_str*
|
|
|
|
str_slice(rust_task* task, rust_str* v, size_t begin, size_t end)
|
|
|
|
{
|
|
|
|
size_t len = end - begin;
|
|
|
|
rust_str *st =
|
|
|
|
vec_alloc_with_data(task,
|
|
|
|
len + 1, // +1 to fit at least '\0'
|
|
|
|
len,
|
|
|
|
1,
|
|
|
|
len ? v->data + begin : NULL);
|
|
|
|
if (!st) {
|
|
|
|
task->fail(2);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
st->data[st->fill++] = '\0';
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
extern "C" CDECL char const *
|
|
|
|
str_buf(rust_task *task, rust_str *s)
|
|
|
|
{
|
|
|
|
return (char const *)&s->data[0];
|
|
|
|
}
|
|
|
|
|
2011-04-27 15:06:19 -05:00
|
|
|
extern "C" CDECL rust_vec*
|
|
|
|
str_vec(rust_task *task, rust_str *s)
|
|
|
|
{
|
|
|
|
// FIXME: this should just upref s and return it, but we
|
|
|
|
// accidentally made too much of the language and runtime know
|
|
|
|
// and care about the difference between str and vec (trailing null);
|
|
|
|
// eliminate these differences and then rewrite this back to just
|
|
|
|
// the following:
|
|
|
|
//
|
|
|
|
// if (s->ref_count != CONST_REFCOUNT)
|
|
|
|
// s->ref();
|
|
|
|
// return s;
|
|
|
|
|
|
|
|
rust_str *v =
|
|
|
|
vec_alloc_with_data(task,
|
|
|
|
s->fill - 1,
|
|
|
|
s->fill - 1,
|
|
|
|
1,
|
|
|
|
(s->fill - 1) ? (void*)s->data : NULL);
|
|
|
|
if (!v) {
|
|
|
|
task->fail(2);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2010-07-25 02:36:03 -05:00
|
|
|
extern "C" CDECL size_t
|
|
|
|
str_byte_len(rust_task *task, rust_str *s)
|
|
|
|
{
|
|
|
|
return s->fill - 1; // -1 for the '\0' terminator.
|
|
|
|
}
|
|
|
|
|
2010-08-11 18:06:45 -05:00
|
|
|
extern "C" CDECL rust_str *
|
|
|
|
str_from_vec(rust_task *task, rust_vec *v)
|
2010-06-23 23:03:09 -05:00
|
|
|
{
|
2010-08-11 18:06:45 -05:00
|
|
|
rust_str *st =
|
2011-03-10 08:56:51 -06:00
|
|
|
vec_alloc_with_data(task,
|
2010-08-11 18:06:45 -05:00
|
|
|
v->fill + 1, // +1 to fit at least '\0'
|
|
|
|
v->fill,
|
2011-03-10 08:56:51 -06:00
|
|
|
1,
|
|
|
|
v->fill ? (void*)v->data : NULL);
|
2010-08-11 18:06:45 -05:00
|
|
|
if (!st) {
|
|
|
|
task->fail(2);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
st->data[st->fill++] = '\0';
|
|
|
|
return st;
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
2011-03-16 20:40:51 -05:00
|
|
|
extern "C" CDECL rust_str *
|
|
|
|
str_from_cstr(rust_task *task, char *sbuf)
|
|
|
|
{
|
|
|
|
size_t len = strlen(sbuf) + 1;
|
|
|
|
rust_str *st = vec_alloc_with_data(task, len, len, 1, sbuf);
|
|
|
|
if (!st) {
|
|
|
|
task->fail(2);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL rust_str *
|
|
|
|
str_from_buf(rust_task *task, char *buf, unsigned int len) {
|
|
|
|
rust_str *st = vec_alloc_with_data(task, len + 1, len, 1, buf);
|
|
|
|
if (!st) {
|
|
|
|
task->fail(2);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
st->data[st->fill++] = '\0';
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
2010-07-25 23:45:09 -05:00
|
|
|
extern "C" CDECL void *
|
|
|
|
rand_new(rust_task *task)
|
|
|
|
{
|
2011-06-28 14:15:41 -05:00
|
|
|
rust_scheduler *sched = task->sched;
|
2010-07-25 23:45:09 -05:00
|
|
|
randctx *rctx = (randctx *) task->malloc(sizeof(randctx));
|
|
|
|
if (!rctx) {
|
|
|
|
task->fail(1);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-06-28 14:15:41 -05:00
|
|
|
isaac_init(sched, rctx);
|
2010-07-25 23:45:09 -05:00
|
|
|
return rctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL size_t
|
|
|
|
rand_next(rust_task *task, randctx *rctx)
|
|
|
|
{
|
|
|
|
return rand(rctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
rand_free(rust_task *task, randctx *rctx)
|
|
|
|
{
|
|
|
|
task->free(rctx);
|
|
|
|
}
|
|
|
|
|
2010-08-11 23:23:34 -05:00
|
|
|
extern "C" CDECL void upcall_sleep(rust_task *task, size_t time_in_us);
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
task_sleep(rust_task *task, size_t time_in_us) {
|
|
|
|
upcall_sleep(task, time_in_us);
|
|
|
|
}
|
|
|
|
|
2011-05-31 19:44:54 -05:00
|
|
|
extern "C" CDECL void
|
|
|
|
task_yield(rust_task *task) {
|
|
|
|
task->yield(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
task_join(rust_task *task, rust_task *join_task) {
|
2011-06-24 18:50:06 -05:00
|
|
|
task->kernel->scheduler_lock.lock();
|
2011-06-13 20:19:08 -05:00
|
|
|
// If the other task is already dying, we don't have to wait for it.
|
|
|
|
if (join_task->dead() == false) {
|
|
|
|
join_task->tasks_waiting_to_join.push(task);
|
|
|
|
task->block(join_task, "joining local task");
|
2011-06-24 18:50:06 -05:00
|
|
|
task->kernel->scheduler_lock.unlock();
|
2011-06-13 20:19:08 -05:00
|
|
|
task->yield(2);
|
|
|
|
}
|
2011-06-22 17:44:47 -05:00
|
|
|
else {
|
2011-06-24 18:50:06 -05:00
|
|
|
task->kernel->scheduler_lock.unlock();
|
2011-06-22 17:44:47 -05:00
|
|
|
}
|
2011-05-31 19:44:54 -05:00
|
|
|
}
|
|
|
|
|
2010-08-24 20:37:42 -05:00
|
|
|
/* Debug builtins for std.dbg. */
|
|
|
|
|
|
|
|
static void
|
|
|
|
debug_tydesc_helper(rust_task *task, type_desc *t)
|
|
|
|
{
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, " size %" PRIdPTR ", align %" PRIdPTR
|
|
|
|
", stateful %" PRIdPTR ", first_param 0x%" PRIxPTR,
|
|
|
|
t->size, t->align, t->is_stateful, t->first_param);
|
2010-08-24 20:37:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
debug_tydesc(rust_task *task, type_desc *t)
|
|
|
|
{
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, "debug_tydesc");
|
2010-08-24 20:37:42 -05:00
|
|
|
debug_tydesc_helper(task, t);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
debug_opaque(rust_task *task, type_desc *t, uint8_t *front)
|
|
|
|
{
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, "debug_opaque");
|
2010-08-24 20:37:42 -05:00
|
|
|
debug_tydesc_helper(task, t);
|
|
|
|
// FIXME may want to actually account for alignment. `front` may not
|
|
|
|
// indeed be the front byte of the passed-in argument.
|
|
|
|
for (uintptr_t i = 0; i < t->size; ++front, ++i) {
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, " byte %" PRIdPTR ": 0x%" PRIx8, i, *front);
|
2010-08-24 20:37:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct rust_box : rc_base<rust_box> {
|
|
|
|
// FIXME `data` could be aligned differently from the actual box body data
|
|
|
|
uint8_t data[];
|
|
|
|
};
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
debug_box(rust_task *task, type_desc *t, rust_box *box)
|
|
|
|
{
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, "debug_box(0x%" PRIxPTR ")", box);
|
2010-08-24 20:37:42 -05:00
|
|
|
debug_tydesc_helper(task, t);
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, " refcount %" PRIdPTR,
|
|
|
|
box->ref_count == CONST_REFCOUNT
|
|
|
|
? CONST_REFCOUNT
|
|
|
|
: box->ref_count - 1); // -1 because we ref'ed for this call
|
2010-08-24 20:37:42 -05:00
|
|
|
for (uintptr_t i = 0; i < t->size; ++i) {
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, " byte %" PRIdPTR ": 0x%" PRIx8, i, box->data[i]);
|
2010-08-24 20:37:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct rust_tag {
|
|
|
|
uintptr_t discriminant;
|
|
|
|
uint8_t variant[];
|
|
|
|
};
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
debug_tag(rust_task *task, type_desc *t, rust_tag *tag)
|
|
|
|
{
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, "debug_tag");
|
2010-08-24 20:37:42 -05:00
|
|
|
debug_tydesc_helper(task, t);
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, " discriminant %" PRIdPTR, tag->discriminant);
|
2010-08-24 20:37:42 -05:00
|
|
|
|
|
|
|
for (uintptr_t i = 0; i < t->size - sizeof(tag->discriminant); ++i)
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, " byte %" PRIdPTR ": 0x%" PRIx8, i,
|
|
|
|
tag->variant[i]);
|
2010-08-24 20:37:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct rust_obj {
|
|
|
|
uintptr_t *vtbl;
|
|
|
|
rust_box *body;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
2010-08-24 21:49:39 -05:00
|
|
|
debug_obj(rust_task *task, type_desc *t, rust_obj *obj,
|
|
|
|
size_t nmethods, size_t nbytes)
|
2010-08-24 20:37:42 -05:00
|
|
|
{
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, "debug_obj with %" PRIdPTR " methods", nmethods);
|
2010-08-24 20:37:42 -05:00
|
|
|
debug_tydesc_helper(task, t);
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, " vtbl at 0x%" PRIxPTR, obj->vtbl);
|
|
|
|
LOG(task, stdlib, " body at 0x%" PRIxPTR, obj->body);
|
2010-08-24 20:37:42 -05:00
|
|
|
|
|
|
|
for (uintptr_t *p = obj->vtbl; p < obj->vtbl + nmethods; ++p)
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, " vtbl word: 0x%" PRIxPTR, *p);
|
2010-08-24 21:49:39 -05:00
|
|
|
|
|
|
|
for (uintptr_t i = 0; i < nbytes; ++i)
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, " body byte %" PRIdPTR ": 0x%" PRIxPTR,
|
|
|
|
i, obj->body->data[i]);
|
2010-08-24 20:37:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct rust_fn {
|
|
|
|
uintptr_t *thunk;
|
|
|
|
rust_box *closure;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
debug_fn(rust_task *task, type_desc *t, rust_fn *fn)
|
|
|
|
{
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, "debug_fn");
|
2010-08-24 20:37:42 -05:00
|
|
|
debug_tydesc_helper(task, t);
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, " thunk at 0x%" PRIxPTR, fn->thunk);
|
|
|
|
LOG(task, stdlib, " closure at 0x%" PRIxPTR, fn->closure);
|
2010-09-30 19:39:37 -05:00
|
|
|
if (fn->closure) {
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, " refcount %" PRIdPTR, fn->closure->ref_count);
|
2010-09-30 19:39:37 -05:00
|
|
|
}
|
2010-08-24 20:37:42 -05:00
|
|
|
}
|
|
|
|
|
2010-09-07 01:24:01 -05:00
|
|
|
extern "C" CDECL void *
|
|
|
|
debug_ptrcast(rust_task *task,
|
|
|
|
type_desc *from_ty,
|
|
|
|
type_desc *to_ty,
|
|
|
|
void *ptr)
|
|
|
|
{
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, "debug_ptrcast from");
|
2010-09-07 01:24:01 -05:00
|
|
|
debug_tydesc_helper(task, from_ty);
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, "to");
|
2010-09-07 01:24:01 -05:00
|
|
|
debug_tydesc_helper(task, to_ty);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2010-12-31 12:35:39 -06:00
|
|
|
extern "C" CDECL void
|
|
|
|
debug_trap(rust_task *task, rust_str *s)
|
|
|
|
{
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, "trapping: %s", s->data);
|
2010-12-31 12:35:39 -06:00
|
|
|
// FIXME: x86-ism.
|
|
|
|
__asm__("int3");
|
|
|
|
}
|
|
|
|
|
2011-03-10 08:56:51 -06:00
|
|
|
rust_str* c_str_to_rust(rust_task *task, char const *str) {
|
|
|
|
size_t len = strlen(str) + 1;
|
|
|
|
return vec_alloc_with_data(task, len, len, 1, (void*)str);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL rust_vec*
|
|
|
|
rust_list_files(rust_task *task, rust_str *path) {
|
|
|
|
array_list<rust_str*> strings;
|
2011-06-10 14:56:42 -05:00
|
|
|
#if defined(__WIN32__)
|
2011-03-10 08:56:51 -06:00
|
|
|
WIN32_FIND_DATA FindFileData;
|
|
|
|
HANDLE hFind = FindFirstFile((char*)path->data, &FindFileData);
|
|
|
|
if (hFind != INVALID_HANDLE_VALUE) {
|
|
|
|
do {
|
|
|
|
strings.push(c_str_to_rust(task, FindFileData.cFileName));
|
|
|
|
} while (FindNextFile(hFind, &FindFileData));
|
|
|
|
FindClose(hFind);
|
|
|
|
}
|
2011-03-25 02:09:20 -05:00
|
|
|
#else
|
2011-06-10 14:56:42 -05:00
|
|
|
DIR *dirp = opendir((char*)path->data);
|
|
|
|
if (dirp) {
|
|
|
|
struct dirent *dp;
|
|
|
|
while ((dp = readdir(dirp)))
|
|
|
|
strings.push(c_str_to_rust(task, dp->d_name));
|
|
|
|
closedir(dirp);
|
|
|
|
}
|
2011-03-25 02:09:20 -05:00
|
|
|
#endif
|
2011-06-10 14:56:42 -05:00
|
|
|
return vec_alloc_with_data(task, strings.size(), strings.size(),
|
|
|
|
sizeof(rust_str*), strings.data());
|
2011-03-25 02:09:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(__WIN32__)
|
|
|
|
extern "C" CDECL rust_str *
|
|
|
|
rust_dirent_filename(rust_task *task, void* ent) {
|
|
|
|
return NULL;
|
2011-03-10 08:56:51 -06:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
extern "C" CDECL rust_str *
|
|
|
|
rust_dirent_filename(rust_task *task, dirent* ent) {
|
|
|
|
return c_str_to_rust(task, ent->d_name);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
extern "C" CDECL int
|
|
|
|
rust_file_is_dir(rust_task *task, rust_str *path) {
|
|
|
|
struct stat buf;
|
|
|
|
stat((char*)path->data, &buf);
|
|
|
|
return S_ISDIR(buf.st_mode);
|
|
|
|
}
|
2010-08-24 20:37:42 -05:00
|
|
|
|
2011-03-10 09:02:53 -06:00
|
|
|
extern "C" CDECL FILE* rust_get_stdin() {return stdin;}
|
|
|
|
extern "C" CDECL FILE* rust_get_stdout() {return stdout;}
|
|
|
|
|
2011-04-21 18:44:17 -05:00
|
|
|
extern "C" CDECL int
|
|
|
|
rust_ptr_eq(rust_task *task, type_desc *t, rust_box *a, rust_box *b) {
|
|
|
|
return a == b;
|
|
|
|
}
|
|
|
|
|
2011-04-29 13:54:06 -05:00
|
|
|
#if defined(__WIN32__)
|
|
|
|
extern "C" CDECL void
|
|
|
|
get_time(rust_task *task, uint32_t *sec, uint32_t *usec) {
|
|
|
|
SYSTEMTIME systemTime;
|
|
|
|
FILETIME fileTime;
|
|
|
|
GetSystemTime(&systemTime);
|
|
|
|
if (!SystemTimeToFileTime(&systemTime, &fileTime)) {
|
|
|
|
task->fail(1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: This is probably completely wrong.
|
|
|
|
*sec = fileTime.dwHighDateTime;
|
|
|
|
*usec = fileTime.dwLowDateTime;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
extern "C" CDECL void
|
|
|
|
get_time(rust_task *task, uint32_t *sec, uint32_t *usec) {
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
*sec = tv.tv_sec;
|
|
|
|
*usec = tv.tv_usec;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-06-16 12:32:52 -05:00
|
|
|
/**
|
|
|
|
* Preallocates the exact number of bytes in the given interior vector.
|
|
|
|
*/
|
|
|
|
extern "C" void
|
|
|
|
ivec_reserve(rust_task *task, type_desc *ty, rust_ivec *v, size_t n_elems)
|
|
|
|
{
|
|
|
|
size_t new_alloc = n_elems * ty->size;
|
|
|
|
if (new_alloc <= v->alloc)
|
|
|
|
return; // Already big enough.
|
|
|
|
|
|
|
|
rust_ivec_heap *heap_part;
|
2011-06-16 20:01:13 -05:00
|
|
|
if (v->fill || !v->payload.ptr) {
|
2011-06-16 12:32:52 -05:00
|
|
|
// On stack; spill to heap.
|
|
|
|
heap_part = (rust_ivec_heap *)task->malloc(new_alloc +
|
|
|
|
sizeof(size_t));
|
|
|
|
heap_part->fill = v->fill;
|
|
|
|
memcpy(&heap_part->data, v->payload.data, v->fill);
|
|
|
|
|
|
|
|
v->fill = 0;
|
|
|
|
v->payload.ptr = heap_part;
|
|
|
|
} else {
|
|
|
|
// On heap; resize.
|
|
|
|
heap_part = (rust_ivec_heap *)task->realloc(v->payload.ptr,
|
2011-06-19 19:16:21 -05:00
|
|
|
new_alloc + sizeof(size_t));
|
2011-06-16 12:32:52 -05:00
|
|
|
v->payload.ptr = heap_part;
|
|
|
|
}
|
|
|
|
|
|
|
|
v->alloc = new_alloc;
|
|
|
|
}
|
|
|
|
|
2011-06-16 18:08:26 -05:00
|
|
|
/**
|
|
|
|
* Returns true if the given vector is on the heap and false if it's on the
|
|
|
|
* stack.
|
|
|
|
*/
|
|
|
|
extern "C" bool
|
|
|
|
ivec_on_heap(rust_task *task, type_desc *ty, rust_ivec *v)
|
|
|
|
{
|
|
|
|
return !v->fill && v->payload.ptr;
|
|
|
|
}
|
|
|
|
|
2011-06-16 19:06:24 -05:00
|
|
|
/**
|
|
|
|
* Returns an unsafe pointer to the data part of an interior vector.
|
|
|
|
*/
|
|
|
|
extern "C" void *
|
|
|
|
ivec_to_ptr(rust_task *task, type_desc *ty, rust_ivec *v)
|
|
|
|
{
|
|
|
|
return v->fill ? v->payload.data : v->payload.ptr->data;
|
|
|
|
}
|
|
|
|
|
2011-06-16 20:01:13 -05:00
|
|
|
static size_t
|
|
|
|
get_ivec_size(rust_ivec *v)
|
|
|
|
{
|
|
|
|
if (v->fill)
|
|
|
|
return v->fill;
|
|
|
|
if (v->payload.ptr)
|
|
|
|
return v->payload.ptr->fill;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-06-16 19:06:24 -05:00
|
|
|
/**
|
|
|
|
* Copies elements in an unsafe buffer to the given interior vector. The
|
|
|
|
* vector must have size zero.
|
|
|
|
*/
|
|
|
|
extern "C" void
|
|
|
|
ivec_copy_from_buf(rust_task *task, type_desc *ty, rust_ivec *v, void *ptr,
|
|
|
|
size_t count)
|
|
|
|
{
|
2011-06-16 20:01:13 -05:00
|
|
|
size_t old_size = get_ivec_size(v);
|
|
|
|
if (old_size) {
|
2011-06-16 19:06:24 -05:00
|
|
|
task->fail(1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ivec_reserve(task, ty, v, count);
|
|
|
|
|
|
|
|
size_t new_size = count * ty->size;
|
2011-06-17 18:49:04 -05:00
|
|
|
if (v->fill || !v->payload.ptr) {
|
2011-06-16 19:06:24 -05:00
|
|
|
// On stack.
|
|
|
|
memmove(v->payload.data, ptr, new_size);
|
|
|
|
v->fill = new_size;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// On heap.
|
|
|
|
memmove(v->payload.ptr->data, ptr, new_size);
|
|
|
|
v->payload.ptr->fill = new_size;
|
|
|
|
}
|
|
|
|
|
2011-06-29 20:47:47 -05:00
|
|
|
extern "C" void
|
|
|
|
pin_task(rust_task *task) {
|
|
|
|
task->pin();
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
unpin_task(rust_task *task) {
|
|
|
|
task->unpin();
|
|
|
|
}
|
2011-06-16 12:32:52 -05:00
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
//
|
|
|
|
// Local Variables:
|
|
|
|
// mode: C++
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
2011-06-10 14:56:42 -05:00
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
2010-06-23 23:03:09 -05:00
|
|
|
// End:
|
|
|
|
//
|