Skip to content

Commit

Permalink
hide the type pointer so that reference types are forward compatible …
Browse files Browse the repository at this point in the history
…with C
  • Loading branch information
vtjnash committed Apr 10, 2013
1 parent 1230762 commit cc697f1
Show file tree
Hide file tree
Showing 17 changed files with 160 additions and 148 deletions.
15 changes: 10 additions & 5 deletions base/sysimg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@ include = Core.include

include("exports.jl")

include("base.jl")
if false
# simple print definitions for debugging. enable these if something
# goes wrong during bootstrap before printing code is available.
length(a::Array) = arraylen(a)
print(x) = print(STDOUT, x)
show(x) = show(STDOUT, x)
write(io::IO, a::Array{Uint8,1}) =
ccall(:ios_write, Uint, (Ptr{Void}, Ptr{Void}, Uint),
io.ios, a, length(a))

write{T}(io::IO, a::Array{T}) =
if T === Uint8
ccall(:jl_write, Uint, (Ptr{Void}, Ptr{Void}, Uint),
io.handle, a, length(a))
else
write(io, "array?")
end
print(io::IO, s::Symbol) = ccall(:jl_print_symbol, Void, (Ptr{Void},Any,),
io.ios, s)
io.handle, s)
print(io::IO, s::ASCIIString) = (write(io, s.data);nothing)
print(io::IO, x) = show(io, x)
println(io::IO, x) = (print(io, x); print(io, "\n"))
Expand All @@ -41,7 +47,6 @@ end

## Load essential files and libraries

include("base.jl")
include("build_h.jl")
include("c.jl")

Expand Down
44 changes: 22 additions & 22 deletions src/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ jl_value_t *jl_new_bits(jl_datatype_t *bt, void *data)

size_t nb = jl_datatype_size(bt);
jl_value_t *v =
(jl_value_t*)allocobj((NWORDS(LLT_ALIGN(nb,sizeof(void*)))+1)*
sizeof(void*));
v->type = (jl_value_t*)bt;
((jl_value_t*)allocobj((NWORDS(LLT_ALIGN(nb,sizeof(void*)))+1)*
sizeof(void*)));
jl_typeof(v) = (jl_value_t*)bt;
switch (nb) {
case 1: *(int8_t*) jl_data_ptr(v) = *(int8_t*)data; break;
case 2: *(int16_t*) jl_data_ptr(v) = *(int16_t*)data; break;
Expand Down Expand Up @@ -143,7 +143,7 @@ int jl_field_index(jl_datatype_t *t, jl_sym_t *fld, int err)
jl_value_t *jl_get_nth_field(jl_value_t *v, size_t i)
{
jl_datatype_t *st = (jl_datatype_t*)jl_typeof(v);
size_t offs = jl_field_offset(st,i) + sizeof(void*);
size_t offs = jl_field_offset(st,i);
if (st->fields[i].isptr) {
return *(jl_value_t**)((char*)v + offs);
}
Expand All @@ -156,7 +156,7 @@ int jl_field_isdefined(jl_value_t *v, jl_sym_t *fld, int err)
jl_datatype_t *st = (jl_datatype_t*)jl_typeof(v);
int i = jl_field_index(st, fld, err);
if (i == -1) return 0;
size_t offs = jl_field_offset(st,i) + sizeof(void*);
size_t offs = jl_field_offset(st,i);
if (st->fields[i].isptr) {
return *(jl_value_t**)((char*)v + offs) != NULL;
}
Expand All @@ -166,7 +166,7 @@ int jl_field_isdefined(jl_value_t *v, jl_sym_t *fld, int err)
jl_value_t *jl_set_nth_field(jl_value_t *v, size_t i, jl_value_t *rhs)
{
jl_datatype_t *st = (jl_datatype_t*)jl_typeof(v);
size_t offs = jl_field_offset(st,i) + sizeof(void*);
size_t offs = jl_field_offset(st,i);
if (st->fields[i].isptr) {
*(jl_value_t**)((char*)v + offs) = rhs;
}
Expand Down Expand Up @@ -201,7 +201,7 @@ DLLEXPORT jl_value_t *jl_new_structv(jl_datatype_t *type, jl_value_t **args, uin
}
for(size_t i=na; i < nf; i++) {
if (type->fields[i].isptr)
*(jl_value_t**)((char*)jv+jl_field_offset(type,i)+sizeof(void*)) = NULL;
*(jl_value_t**)((char*)jv+jl_field_offset(type,i)) = NULL;
}
if (type->size == 0) type->instance = jv;
return jv;
Expand All @@ -212,7 +212,7 @@ DLLEXPORT jl_value_t *jl_new_struct_uninit(jl_datatype_t *type)
if (type->instance != NULL) return type->instance;
jl_value_t *jv = newstruct(type);
if (type->size == 0) type->instance = jv;
else memset(&((void**)jv)[1], 0, type->size);
else memset(&((void**)jv)[0], 0, type->size);
return jv;
}

Expand Down Expand Up @@ -241,7 +241,7 @@ jl_tuple_t *jl_tuple1(void *a)
#else
jl_tuple_t *t = (jl_tuple_t*)alloc_3w();
#endif
t->type = (jl_value_t*)jl_tuple_type;
jl_typeof(t) = (jl_value_t*)jl_tuple_type;
jl_tuple_set_len_unsafe(t, 1);
jl_tupleset(t, 0, a);
return t;
Expand All @@ -254,7 +254,7 @@ jl_tuple_t *jl_tuple2(void *a, void *b)
#else
jl_tuple_t *t = (jl_tuple_t*)alloc_4w();
#endif
t->type = (jl_value_t*)jl_tuple_type;
jl_typeof(t) = (jl_value_t*)jl_tuple_type;
jl_tuple_set_len_unsafe(t, 2);
jl_tupleset(t, 0, a);
jl_tupleset(t, 1, b);
Expand Down Expand Up @@ -312,7 +312,7 @@ DLLEXPORT jl_function_t *jl_new_closure(jl_fptr_t fptr, jl_value_t *env,
jl_lambda_info_t *linfo)
{
jl_function_t *f = (jl_function_t*)alloc_4w();
f->type = (jl_value_t*)jl_function_type;
jl_typeof(f) = (jl_value_t*)jl_function_type;
f->fptr = (fptr!=NULL ? fptr : linfo->fptr);
f->env = env;
f->linfo = linfo;
Expand Down Expand Up @@ -363,8 +363,8 @@ static jl_sym_t *mk_symbol(const char *str)
jl_sym_t *sym;
size_t len = strlen(str);

sym = (jl_sym_t*)malloc((sizeof(jl_sym_t)-sizeof(void*)+len+1+7)&-8);
sym->type = (jl_value_t*)jl_sym_type;
sym = (jl_sym_t*)((char*)malloc((sizeof(jl_sym_t)+sizeof(void*)+len+1+7)&-8)+sizeof(void*));
jl_typeof(sym) = (jl_value_t*)jl_sym_type;
sym->left = sym->right = NULL;
#ifdef _P64
sym->hash = memhash(str, len)^0xAAAAAAAAAAAAAAAAL;
Expand All @@ -378,7 +378,7 @@ static jl_sym_t *mk_symbol(const char *str)
static void unmark_symbols_(jl_sym_t *root)
{
while (root != NULL) {
root->type = (jl_value_t*)(((uptrint_t)root->type)&~1UL);
jl_typeof(root) = (jl_value_t*)(((uptrint_t)jl_typeof(root))&~1UL);
unmark_symbols_(root->left);
root = root->right;
}
Expand Down Expand Up @@ -528,7 +528,7 @@ jl_datatype_t *jl_new_uninitialized_datatype(size_t nfields)
{
return (jl_datatype_t*)
newobj((jl_value_t*)jl_datatype_type,
NWORDS(sizeof(jl_datatype_t) - sizeof(void*) +
NWORDS(sizeof(jl_datatype_t) + sizeof(void) +
(nfields-1)*sizeof(jl_fielddesc_t)));
}

Expand Down Expand Up @@ -659,7 +659,7 @@ jl_value_t *jl_box##nb(jl_datatype_t *t, int##nb##_t x) \
assert(jl_is_bitstype(t)); \
assert(jl_datatype_size(t) == sizeof(x)); \
jl_value_t *v = alloc_##nw##w(); \
v->type = (jl_value_t*)t; \
jl_typeof(v) = (jl_value_t*)t; \
*(int##nb##_t*)jl_data_ptr(v) = x; \
return v; \
}
Expand Down Expand Up @@ -696,7 +696,7 @@ UNBOX_FUNC(voidpointer, void*)
jl_value_t *pfx##_##typ(c_type x) \
{ \
jl_value_t *v = alloc_##nw##w(); \
v->type = (jl_value_t*)jl_##typ##_type; \
jl_typeof(v) = (jl_value_t*)jl_##typ##_type; \
*(c_type*)jl_data_ptr(v) = x; \
return v; \
}
Expand All @@ -718,7 +718,7 @@ jl_value_t *jl_box_##typ(c_type x) \
if ((u##c_type)idx < (u##c_type)NBOX_C) \
return boxed_##typ##_cache[idx]; \
jl_value_t *v = alloc_##nw##w(); \
v->type = (jl_value_t*)jl_##typ##_type; \
jl_typeof(v) = (jl_value_t*)jl_##typ##_type; \
*(c_type*)jl_data_ptr(v) = x; \
return v; \
}
Expand All @@ -729,7 +729,7 @@ jl_value_t *jl_box_##typ(c_type x) \
if (x < NBOX_C) \
return boxed_##typ##_cache[x]; \
jl_value_t *v = alloc_##nw##w(); \
v->type = (jl_value_t*)jl_##typ##_type; \
jl_typeof(v) = (jl_value_t*)jl_##typ##_type; \
*(c_type*)jl_data_ptr(v) = x; \
return v; \
}
Expand Down Expand Up @@ -816,7 +816,7 @@ jl_expr_t *jl_exprn(jl_sym_t *head, size_t n)
jl_array_t *ar = n==0 ? (jl_array_t*)jl_an_empty_cell : jl_alloc_cell_1d(n);
JL_GC_PUSH(&ar);
jl_expr_t *ex = (jl_expr_t*)alloc_4w();
ex->type = (jl_value_t*)jl_expr_type;
jl_typeof(ex) = (jl_value_t*)jl_expr_type;
ex->head = head;
ex->args = ar;
ex->etype = (jl_value_t*)jl_any_type;
Expand All @@ -835,7 +835,7 @@ JL_CALLABLE(jl_f_new_expr)
for(size_t i=1; i < nargs; i++)
jl_cellset(ar, i-1, args[i]);
jl_expr_t *ex = (jl_expr_t*)alloc_4w();
ex->type = (jl_value_t*)jl_expr_type;
jl_typeof(ex) = (jl_value_t*)jl_expr_type;
ex->head = (jl_sym_t*)args[0];
ex->args = ar;
ex->etype = (jl_value_t*)jl_any_type;
Expand All @@ -847,7 +847,7 @@ JL_CALLABLE(jl_f_new_box)
{
JL_NARGS(Box, 1, 1);
jl_value_t *box = (jl_value_t*)alloc_2w();
box->type = jl_box_any_type;
jl_typeof(box) = jl_box_any_type;
((jl_value_t**)box)[1] = args[0];
return box;
}
22 changes: 11 additions & 11 deletions src/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static jl_array_t *_new_array(jl_value_t *atype,
}

int ndimwords = jl_array_ndimwords(ndims);
size_t tsz = sizeof(jl_array_t)-sizeof(void*);
size_t tsz = sizeof(jl_array_t);
tsz += ndimwords*sizeof(size_t);
if (tot <= ARRAY_INLINE_NBYTES) {
size_t basesz = tsz;
Expand All @@ -65,10 +65,10 @@ static jl_array_t *_new_array(jl_value_t *atype,
}
tsz = (tsz+15)&-16; // align whole object 16
a = allocobj(tsz);
a->type = atype;
jl_typeof(a) = atype;
a->ismalloc = 0;
a->isinline = 1;
data = (char*)a + doffs;
data = (char*)a + doffs - sizeof(void*);
if (tot > 0 && !isunboxed) {
memset(data, 0, tot);
}
Expand All @@ -81,7 +81,7 @@ static jl_array_t *_new_array(jl_value_t *atype,
tsz = (tsz+15)&-16; // align whole object size 16
a = allocobj(tsz);
JL_GC_PUSH(&a);
a->type = atype;
jl_typeof(a) = atype;
a->ismalloc = 1;
a->isinline = 0;
// temporarily initialize to make gc-safe
Expand Down Expand Up @@ -126,8 +126,8 @@ jl_array_t *jl_reshape_array(jl_value_t *atype, jl_array_t *data,
size_t ndims = jl_tuple_len(dims);

int ndimwords = jl_array_ndimwords(ndims);
a = allocobj((sizeof(jl_array_t) + ndimwords*sizeof(size_t) + 15)&-16);
a->type = atype;
a = allocobj((sizeof(jl_array_t) + sizeof(void*) + ndimwords*sizeof(size_t) + 15)&-16);
jl_typeof(a) = atype;
a->ndims = ndims;
a->data = NULL;
a->isinline = 0;
Expand Down Expand Up @@ -218,8 +218,8 @@ jl_array_t *jl_ptr_to_array_1d(jl_value_t *atype, void *data, size_t nel,
else
elsz = sizeof(void*);

a = allocobj((sizeof(jl_array_t)+jl_array_ndimwords(1)*sizeof(size_t)+15)&-16);
a->type = atype;
a = allocobj((sizeof(jl_array_t)+sizeof(void*)+jl_array_ndimwords(1)*sizeof(size_t)+15)&-16);
jl_typeof(a) = atype;
a->data = data;
#ifdef STORE_ARRAY_LEN
a->length = nel;
Expand Down Expand Up @@ -264,8 +264,8 @@ jl_array_t *jl_ptr_to_array(jl_value_t *atype, void *data, jl_tuple_t *dims,
elsz = sizeof(void*);

int ndimwords = jl_array_ndimwords(ndims);
a = allocobj((sizeof(jl_array_t) + ndimwords*sizeof(size_t)+15)&-16);
a->type = atype;
a = allocobj((sizeof(jl_array_t) + sizeof(void*) + ndimwords*sizeof(size_t)+15)&-16);
jl_typeof(a) = atype;
a->data = data;
#ifdef STORE_ARRAY_LEN
a->length = nel;
Expand Down Expand Up @@ -344,7 +344,7 @@ jl_value_t *jl_array_to_string(jl_array_t *a)
jl_datatype_t* string_type = u8_isvalid(a->data, jl_array_len(a)) == 1 ? // ASCII
jl_ascii_string_type : jl_utf8_string_type;
jl_value_t *s = alloc_2w();
s->type = (jl_value_t*)string_type;
jl_typeof(s) = (jl_value_t*)string_type;
jl_set_nth_field(s, 0, (jl_value_t*)a);
return s;
}
Expand Down
6 changes: 3 additions & 3 deletions src/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ void jl_show(jl_value_t *stream, jl_value_t *v)
// comma_one prints a comma for 1 element, e.g. "(x,)"
void jl_show_tuple(jl_value_t *st, jl_tuple_t *t, char opn, char cls, int comma_one)
{
JL_STREAM *s = ((JL_STREAM**)st)[1];
JL_STREAM *s = ((JL_STREAM**)st)[0];
JL_PUTC(opn, s);
size_t i, n=jl_tuple_len(t);
for(i=0; i < n; i++) {
Expand All @@ -685,7 +685,7 @@ static void show_function(JL_STREAM *s, jl_value_t *v)

static void show_type(jl_value_t *st, jl_value_t *t)
{
uv_stream_t *s =((uv_stream_t**)st)[1];
uv_stream_t *s =((uv_stream_t**)st)[0];
if (jl_is_uniontype(t)) {
if (t == (jl_value_t*)jl_bottom_type) {
JL_WRITE(s, "None", 4);
Expand Down Expand Up @@ -717,7 +717,7 @@ static void show_type(jl_value_t *st, jl_value_t *t)

DLLEXPORT void jl_show_any(jl_value_t *str, jl_value_t *v)
{
uv_stream_t *s = ((uv_stream_t**)str)[1];
uv_stream_t *s = ((uv_stream_t**)str)[0];
// fallback for printing some other builtin types
if (jl_is_tuple(v)) {
jl_show_tuple(str, (jl_tuple_t*)v, '(', ')', 1);
Expand Down
10 changes: 5 additions & 5 deletions src/ccall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,14 +255,14 @@ static Value *julia_to_native(Type *ty, jl_value_t *jt, Value *jv,
return builder.CreateBitCast(emit_arrayptr(jv), ty);
}
if (aty == (jl_value_t*)jl_ascii_string_type || aty == (jl_value_t*)jl_utf8_string_type) {
return builder.CreateBitCast(emit_arrayptr(emit_nthptr(jv,1)), ty);
return builder.CreateBitCast(emit_arrayptr(emit_nthptr(jv,(ssize_t)0)), ty);
}
if (jl_is_structtype(aty) && jl_is_leaf_type(aty) && !jl_is_array_type(aty)) {
if (!addressOf) {
emit_error("ccall: expected addressOf operator", ctx);
return literal_pointer_val(jl_nothing);
}
return builder.CreateBitCast(emit_nthptr_addr(jv, (size_t)1), ty); // skip type tag field
return builder.CreateBitCast(emit_nthptr_addr(jv, (ssize_t)0), ty); // skip type tag field
}
Value *p = builder.CreateCall4(value_to_pointer_func,
literal_pointer_val(jl_tparam0(jt)), jv,
Expand All @@ -285,7 +285,7 @@ static Value *julia_to_native(Type *ty, jl_value_t *jt, Value *jv,
//if (!jl_is_structtype(aty))
// emit_typecheck(emit_typeof(jv), (jl_value_t*)jl_struct_kind, "ccall: Struct argument called with something that isn't a struct", ctx);
// //safe thing would be to also check that jl_typeof(aty)->size > sizeof(ty) here and/or at runtime
Value *pjv = builder.CreateBitCast(emit_nthptr_addr(jv, (size_t)1), PointerType::get(ty,0));
Value *pjv = builder.CreateBitCast(emit_nthptr_addr(jv, (ssize_t)0), PointerType::get(ty,0));
return builder.CreateLoad(pjv, false);
}
// TODO: error for & with non-pointer argument type
Expand Down Expand Up @@ -628,10 +628,10 @@ static Value *emit_ccall(jl_value_t **args, size_t nargs, jl_codectx_t *ctx)
ConstantInt::get(T_size,
sizeof(void*)+((jl_datatype_t*)rt)->size));
builder.CreateStore(literal_pointer_val((jl_value_t*)rt),
emit_nthptr_addr(strct, (size_t)0));
emit_nthptr_addr(strct, (ssize_t)-1));
builder.CreateStore(result,
builder.CreateBitCast(
emit_nthptr_addr(strct, (size_t)1),
emit_nthptr_addr(strct, (ssize_t)0),
PointerType::get(lrt,0)));
return mark_julia_type(strct, rt);
}
Expand Down
Loading

0 comments on commit cc697f1

Please sign in to comment.