Skip to content

Commit cc697f1

Browse files
committed
hide the type pointer so that reference types are forward compatible with C
1 parent 1230762 commit cc697f1

17 files changed

+160
-148
lines changed

base/sysimg.jl

+10-5
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,23 @@ include = Core.include
77

88
include("exports.jl")
99

10+
include("base.jl")
1011
if false
1112
# simple print definitions for debugging. enable these if something
1213
# goes wrong during bootstrap before printing code is available.
1314
length(a::Array) = arraylen(a)
1415
print(x) = print(STDOUT, x)
1516
show(x) = show(STDOUT, x)
16-
write(io::IO, a::Array{Uint8,1}) =
17-
ccall(:ios_write, Uint, (Ptr{Void}, Ptr{Void}, Uint),
18-
io.ios, a, length(a))
17+
18+
write{T}(io::IO, a::Array{T}) =
19+
if T === Uint8
20+
ccall(:jl_write, Uint, (Ptr{Void}, Ptr{Void}, Uint),
21+
io.handle, a, length(a))
22+
else
23+
write(io, "array?")
24+
end
1925
print(io::IO, s::Symbol) = ccall(:jl_print_symbol, Void, (Ptr{Void},Any,),
20-
io.ios, s)
26+
io.handle, s)
2127
print(io::IO, s::ASCIIString) = (write(io, s.data);nothing)
2228
print(io::IO, x) = show(io, x)
2329
println(io::IO, x) = (print(io, x); print(io, "\n"))
@@ -41,7 +47,6 @@ end
4147

4248
## Load essential files and libraries
4349

44-
include("base.jl")
4550
include("build_h.jl")
4651
include("c.jl")
4752

src/alloc.c

+22-22
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ jl_value_t *jl_new_bits(jl_datatype_t *bt, void *data)
100100

101101
size_t nb = jl_datatype_size(bt);
102102
jl_value_t *v =
103-
(jl_value_t*)allocobj((NWORDS(LLT_ALIGN(nb,sizeof(void*)))+1)*
104-
sizeof(void*));
105-
v->type = (jl_value_t*)bt;
103+
((jl_value_t*)allocobj((NWORDS(LLT_ALIGN(nb,sizeof(void*)))+1)*
104+
sizeof(void*)));
105+
jl_typeof(v) = (jl_value_t*)bt;
106106
switch (nb) {
107107
case 1: *(int8_t*) jl_data_ptr(v) = *(int8_t*)data; break;
108108
case 2: *(int16_t*) jl_data_ptr(v) = *(int16_t*)data; break;
@@ -143,7 +143,7 @@ int jl_field_index(jl_datatype_t *t, jl_sym_t *fld, int err)
143143
jl_value_t *jl_get_nth_field(jl_value_t *v, size_t i)
144144
{
145145
jl_datatype_t *st = (jl_datatype_t*)jl_typeof(v);
146-
size_t offs = jl_field_offset(st,i) + sizeof(void*);
146+
size_t offs = jl_field_offset(st,i);
147147
if (st->fields[i].isptr) {
148148
return *(jl_value_t**)((char*)v + offs);
149149
}
@@ -156,7 +156,7 @@ int jl_field_isdefined(jl_value_t *v, jl_sym_t *fld, int err)
156156
jl_datatype_t *st = (jl_datatype_t*)jl_typeof(v);
157157
int i = jl_field_index(st, fld, err);
158158
if (i == -1) return 0;
159-
size_t offs = jl_field_offset(st,i) + sizeof(void*);
159+
size_t offs = jl_field_offset(st,i);
160160
if (st->fields[i].isptr) {
161161
return *(jl_value_t**)((char*)v + offs) != NULL;
162162
}
@@ -166,7 +166,7 @@ int jl_field_isdefined(jl_value_t *v, jl_sym_t *fld, int err)
166166
jl_value_t *jl_set_nth_field(jl_value_t *v, size_t i, jl_value_t *rhs)
167167
{
168168
jl_datatype_t *st = (jl_datatype_t*)jl_typeof(v);
169-
size_t offs = jl_field_offset(st,i) + sizeof(void*);
169+
size_t offs = jl_field_offset(st,i);
170170
if (st->fields[i].isptr) {
171171
*(jl_value_t**)((char*)v + offs) = rhs;
172172
}
@@ -201,7 +201,7 @@ DLLEXPORT jl_value_t *jl_new_structv(jl_datatype_t *type, jl_value_t **args, uin
201201
}
202202
for(size_t i=na; i < nf; i++) {
203203
if (type->fields[i].isptr)
204-
*(jl_value_t**)((char*)jv+jl_field_offset(type,i)+sizeof(void*)) = NULL;
204+
*(jl_value_t**)((char*)jv+jl_field_offset(type,i)) = NULL;
205205
}
206206
if (type->size == 0) type->instance = jv;
207207
return jv;
@@ -212,7 +212,7 @@ DLLEXPORT jl_value_t *jl_new_struct_uninit(jl_datatype_t *type)
212212
if (type->instance != NULL) return type->instance;
213213
jl_value_t *jv = newstruct(type);
214214
if (type->size == 0) type->instance = jv;
215-
else memset(&((void**)jv)[1], 0, type->size);
215+
else memset(&((void**)jv)[0], 0, type->size);
216216
return jv;
217217
}
218218

@@ -241,7 +241,7 @@ jl_tuple_t *jl_tuple1(void *a)
241241
#else
242242
jl_tuple_t *t = (jl_tuple_t*)alloc_3w();
243243
#endif
244-
t->type = (jl_value_t*)jl_tuple_type;
244+
jl_typeof(t) = (jl_value_t*)jl_tuple_type;
245245
jl_tuple_set_len_unsafe(t, 1);
246246
jl_tupleset(t, 0, a);
247247
return t;
@@ -254,7 +254,7 @@ jl_tuple_t *jl_tuple2(void *a, void *b)
254254
#else
255255
jl_tuple_t *t = (jl_tuple_t*)alloc_4w();
256256
#endif
257-
t->type = (jl_value_t*)jl_tuple_type;
257+
jl_typeof(t) = (jl_value_t*)jl_tuple_type;
258258
jl_tuple_set_len_unsafe(t, 2);
259259
jl_tupleset(t, 0, a);
260260
jl_tupleset(t, 1, b);
@@ -312,7 +312,7 @@ DLLEXPORT jl_function_t *jl_new_closure(jl_fptr_t fptr, jl_value_t *env,
312312
jl_lambda_info_t *linfo)
313313
{
314314
jl_function_t *f = (jl_function_t*)alloc_4w();
315-
f->type = (jl_value_t*)jl_function_type;
315+
jl_typeof(f) = (jl_value_t*)jl_function_type;
316316
f->fptr = (fptr!=NULL ? fptr : linfo->fptr);
317317
f->env = env;
318318
f->linfo = linfo;
@@ -363,8 +363,8 @@ static jl_sym_t *mk_symbol(const char *str)
363363
jl_sym_t *sym;
364364
size_t len = strlen(str);
365365

366-
sym = (jl_sym_t*)malloc((sizeof(jl_sym_t)-sizeof(void*)+len+1+7)&-8);
367-
sym->type = (jl_value_t*)jl_sym_type;
366+
sym = (jl_sym_t*)((char*)malloc((sizeof(jl_sym_t)+sizeof(void*)+len+1+7)&-8)+sizeof(void*));
367+
jl_typeof(sym) = (jl_value_t*)jl_sym_type;
368368
sym->left = sym->right = NULL;
369369
#ifdef _P64
370370
sym->hash = memhash(str, len)^0xAAAAAAAAAAAAAAAAL;
@@ -378,7 +378,7 @@ static jl_sym_t *mk_symbol(const char *str)
378378
static void unmark_symbols_(jl_sym_t *root)
379379
{
380380
while (root != NULL) {
381-
root->type = (jl_value_t*)(((uptrint_t)root->type)&~1UL);
381+
jl_typeof(root) = (jl_value_t*)(((uptrint_t)jl_typeof(root))&~1UL);
382382
unmark_symbols_(root->left);
383383
root = root->right;
384384
}
@@ -528,7 +528,7 @@ jl_datatype_t *jl_new_uninitialized_datatype(size_t nfields)
528528
{
529529
return (jl_datatype_t*)
530530
newobj((jl_value_t*)jl_datatype_type,
531-
NWORDS(sizeof(jl_datatype_t) - sizeof(void*) +
531+
NWORDS(sizeof(jl_datatype_t) + sizeof(void) +
532532
(nfields-1)*sizeof(jl_fielddesc_t)));
533533
}
534534

@@ -659,7 +659,7 @@ jl_value_t *jl_box##nb(jl_datatype_t *t, int##nb##_t x) \
659659
assert(jl_is_bitstype(t)); \
660660
assert(jl_datatype_size(t) == sizeof(x)); \
661661
jl_value_t *v = alloc_##nw##w(); \
662-
v->type = (jl_value_t*)t; \
662+
jl_typeof(v) = (jl_value_t*)t; \
663663
*(int##nb##_t*)jl_data_ptr(v) = x; \
664664
return v; \
665665
}
@@ -696,7 +696,7 @@ UNBOX_FUNC(voidpointer, void*)
696696
jl_value_t *pfx##_##typ(c_type x) \
697697
{ \
698698
jl_value_t *v = alloc_##nw##w(); \
699-
v->type = (jl_value_t*)jl_##typ##_type; \
699+
jl_typeof(v) = (jl_value_t*)jl_##typ##_type; \
700700
*(c_type*)jl_data_ptr(v) = x; \
701701
return v; \
702702
}
@@ -718,7 +718,7 @@ jl_value_t *jl_box_##typ(c_type x) \
718718
if ((u##c_type)idx < (u##c_type)NBOX_C) \
719719
return boxed_##typ##_cache[idx]; \
720720
jl_value_t *v = alloc_##nw##w(); \
721-
v->type = (jl_value_t*)jl_##typ##_type; \
721+
jl_typeof(v) = (jl_value_t*)jl_##typ##_type; \
722722
*(c_type*)jl_data_ptr(v) = x; \
723723
return v; \
724724
}
@@ -729,7 +729,7 @@ jl_value_t *jl_box_##typ(c_type x) \
729729
if (x < NBOX_C) \
730730
return boxed_##typ##_cache[x]; \
731731
jl_value_t *v = alloc_##nw##w(); \
732-
v->type = (jl_value_t*)jl_##typ##_type; \
732+
jl_typeof(v) = (jl_value_t*)jl_##typ##_type; \
733733
*(c_type*)jl_data_ptr(v) = x; \
734734
return v; \
735735
}
@@ -816,7 +816,7 @@ jl_expr_t *jl_exprn(jl_sym_t *head, size_t n)
816816
jl_array_t *ar = n==0 ? (jl_array_t*)jl_an_empty_cell : jl_alloc_cell_1d(n);
817817
JL_GC_PUSH(&ar);
818818
jl_expr_t *ex = (jl_expr_t*)alloc_4w();
819-
ex->type = (jl_value_t*)jl_expr_type;
819+
jl_typeof(ex) = (jl_value_t*)jl_expr_type;
820820
ex->head = head;
821821
ex->args = ar;
822822
ex->etype = (jl_value_t*)jl_any_type;
@@ -835,7 +835,7 @@ JL_CALLABLE(jl_f_new_expr)
835835
for(size_t i=1; i < nargs; i++)
836836
jl_cellset(ar, i-1, args[i]);
837837
jl_expr_t *ex = (jl_expr_t*)alloc_4w();
838-
ex->type = (jl_value_t*)jl_expr_type;
838+
jl_typeof(ex) = (jl_value_t*)jl_expr_type;
839839
ex->head = (jl_sym_t*)args[0];
840840
ex->args = ar;
841841
ex->etype = (jl_value_t*)jl_any_type;
@@ -847,7 +847,7 @@ JL_CALLABLE(jl_f_new_box)
847847
{
848848
JL_NARGS(Box, 1, 1);
849849
jl_value_t *box = (jl_value_t*)alloc_2w();
850-
box->type = jl_box_any_type;
850+
jl_typeof(box) = jl_box_any_type;
851851
((jl_value_t**)box)[1] = args[0];
852852
return box;
853853
}

src/array.c

+11-11
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ static jl_array_t *_new_array(jl_value_t *atype,
5151
}
5252

5353
int ndimwords = jl_array_ndimwords(ndims);
54-
size_t tsz = sizeof(jl_array_t)-sizeof(void*);
54+
size_t tsz = sizeof(jl_array_t);
5555
tsz += ndimwords*sizeof(size_t);
5656
if (tot <= ARRAY_INLINE_NBYTES) {
5757
size_t basesz = tsz;
@@ -65,10 +65,10 @@ static jl_array_t *_new_array(jl_value_t *atype,
6565
}
6666
tsz = (tsz+15)&-16; // align whole object 16
6767
a = allocobj(tsz);
68-
a->type = atype;
68+
jl_typeof(a) = atype;
6969
a->ismalloc = 0;
7070
a->isinline = 1;
71-
data = (char*)a + doffs;
71+
data = (char*)a + doffs - sizeof(void*);
7272
if (tot > 0 && !isunboxed) {
7373
memset(data, 0, tot);
7474
}
@@ -81,7 +81,7 @@ static jl_array_t *_new_array(jl_value_t *atype,
8181
tsz = (tsz+15)&-16; // align whole object size 16
8282
a = allocobj(tsz);
8383
JL_GC_PUSH(&a);
84-
a->type = atype;
84+
jl_typeof(a) = atype;
8585
a->ismalloc = 1;
8686
a->isinline = 0;
8787
// temporarily initialize to make gc-safe
@@ -126,8 +126,8 @@ jl_array_t *jl_reshape_array(jl_value_t *atype, jl_array_t *data,
126126
size_t ndims = jl_tuple_len(dims);
127127

128128
int ndimwords = jl_array_ndimwords(ndims);
129-
a = allocobj((sizeof(jl_array_t) + ndimwords*sizeof(size_t) + 15)&-16);
130-
a->type = atype;
129+
a = allocobj((sizeof(jl_array_t) + sizeof(void*) + ndimwords*sizeof(size_t) + 15)&-16);
130+
jl_typeof(a) = atype;
131131
a->ndims = ndims;
132132
a->data = NULL;
133133
a->isinline = 0;
@@ -218,8 +218,8 @@ jl_array_t *jl_ptr_to_array_1d(jl_value_t *atype, void *data, size_t nel,
218218
else
219219
elsz = sizeof(void*);
220220

221-
a = allocobj((sizeof(jl_array_t)+jl_array_ndimwords(1)*sizeof(size_t)+15)&-16);
222-
a->type = atype;
221+
a = allocobj((sizeof(jl_array_t)+sizeof(void*)+jl_array_ndimwords(1)*sizeof(size_t)+15)&-16);
222+
jl_typeof(a) = atype;
223223
a->data = data;
224224
#ifdef STORE_ARRAY_LEN
225225
a->length = nel;
@@ -264,8 +264,8 @@ jl_array_t *jl_ptr_to_array(jl_value_t *atype, void *data, jl_tuple_t *dims,
264264
elsz = sizeof(void*);
265265

266266
int ndimwords = jl_array_ndimwords(ndims);
267-
a = allocobj((sizeof(jl_array_t) + ndimwords*sizeof(size_t)+15)&-16);
268-
a->type = atype;
267+
a = allocobj((sizeof(jl_array_t) + sizeof(void*) + ndimwords*sizeof(size_t)+15)&-16);
268+
jl_typeof(a) = atype;
269269
a->data = data;
270270
#ifdef STORE_ARRAY_LEN
271271
a->length = nel;
@@ -344,7 +344,7 @@ jl_value_t *jl_array_to_string(jl_array_t *a)
344344
jl_datatype_t* string_type = u8_isvalid(a->data, jl_array_len(a)) == 1 ? // ASCII
345345
jl_ascii_string_type : jl_utf8_string_type;
346346
jl_value_t *s = alloc_2w();
347-
s->type = (jl_value_t*)string_type;
347+
jl_typeof(s) = (jl_value_t*)string_type;
348348
jl_set_nth_field(s, 0, (jl_value_t*)a);
349349
return s;
350350
}

src/builtins.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ void jl_show(jl_value_t *stream, jl_value_t *v)
662662
// comma_one prints a comma for 1 element, e.g. "(x,)"
663663
void jl_show_tuple(jl_value_t *st, jl_tuple_t *t, char opn, char cls, int comma_one)
664664
{
665-
JL_STREAM *s = ((JL_STREAM**)st)[1];
665+
JL_STREAM *s = ((JL_STREAM**)st)[0];
666666
JL_PUTC(opn, s);
667667
size_t i, n=jl_tuple_len(t);
668668
for(i=0; i < n; i++) {
@@ -685,7 +685,7 @@ static void show_function(JL_STREAM *s, jl_value_t *v)
685685

686686
static void show_type(jl_value_t *st, jl_value_t *t)
687687
{
688-
uv_stream_t *s =((uv_stream_t**)st)[1];
688+
uv_stream_t *s =((uv_stream_t**)st)[0];
689689
if (jl_is_uniontype(t)) {
690690
if (t == (jl_value_t*)jl_bottom_type) {
691691
JL_WRITE(s, "None", 4);
@@ -717,7 +717,7 @@ static void show_type(jl_value_t *st, jl_value_t *t)
717717

718718
DLLEXPORT void jl_show_any(jl_value_t *str, jl_value_t *v)
719719
{
720-
uv_stream_t *s = ((uv_stream_t**)str)[1];
720+
uv_stream_t *s = ((uv_stream_t**)str)[0];
721721
// fallback for printing some other builtin types
722722
if (jl_is_tuple(v)) {
723723
jl_show_tuple(str, (jl_tuple_t*)v, '(', ')', 1);

src/ccall.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,14 @@ static Value *julia_to_native(Type *ty, jl_value_t *jt, Value *jv,
255255
return builder.CreateBitCast(emit_arrayptr(jv), ty);
256256
}
257257
if (aty == (jl_value_t*)jl_ascii_string_type || aty == (jl_value_t*)jl_utf8_string_type) {
258-
return builder.CreateBitCast(emit_arrayptr(emit_nthptr(jv,1)), ty);
258+
return builder.CreateBitCast(emit_arrayptr(emit_nthptr(jv,(ssize_t)0)), ty);
259259
}
260260
if (jl_is_structtype(aty) && jl_is_leaf_type(aty) && !jl_is_array_type(aty)) {
261261
if (!addressOf) {
262262
emit_error("ccall: expected addressOf operator", ctx);
263263
return literal_pointer_val(jl_nothing);
264264
}
265-
return builder.CreateBitCast(emit_nthptr_addr(jv, (size_t)1), ty); // skip type tag field
265+
return builder.CreateBitCast(emit_nthptr_addr(jv, (ssize_t)0), ty); // skip type tag field
266266
}
267267
Value *p = builder.CreateCall4(value_to_pointer_func,
268268
literal_pointer_val(jl_tparam0(jt)), jv,
@@ -285,7 +285,7 @@ static Value *julia_to_native(Type *ty, jl_value_t *jt, Value *jv,
285285
//if (!jl_is_structtype(aty))
286286
// emit_typecheck(emit_typeof(jv), (jl_value_t*)jl_struct_kind, "ccall: Struct argument called with something that isn't a struct", ctx);
287287
// //safe thing would be to also check that jl_typeof(aty)->size > sizeof(ty) here and/or at runtime
288-
Value *pjv = builder.CreateBitCast(emit_nthptr_addr(jv, (size_t)1), PointerType::get(ty,0));
288+
Value *pjv = builder.CreateBitCast(emit_nthptr_addr(jv, (ssize_t)0), PointerType::get(ty,0));
289289
return builder.CreateLoad(pjv, false);
290290
}
291291
// TODO: error for & with non-pointer argument type
@@ -628,10 +628,10 @@ static Value *emit_ccall(jl_value_t **args, size_t nargs, jl_codectx_t *ctx)
628628
ConstantInt::get(T_size,
629629
sizeof(void*)+((jl_datatype_t*)rt)->size));
630630
builder.CreateStore(literal_pointer_val((jl_value_t*)rt),
631-
emit_nthptr_addr(strct, (size_t)0));
631+
emit_nthptr_addr(strct, (ssize_t)-1));
632632
builder.CreateStore(result,
633633
builder.CreateBitCast(
634-
emit_nthptr_addr(strct, (size_t)1),
634+
emit_nthptr_addr(strct, (ssize_t)0),
635635
PointerType::get(lrt,0)));
636636
return mark_julia_type(strct, rt);
637637
}

0 commit comments

Comments
 (0)