Skip to content

Commit 5f433d9

Browse files
vtjnashstevengj
authored andcommitted
various globals cleanups (#56750)
While doing some work on analyzing what code runs at toplevel, I found a few things to simplify or fix: - simplify float.jl loop not to call functions just defined to get back the value just stored there - add method to the correct checkbounds function (instead of a local) - missing world push/pop around jl_interpret_toplevel_thunk after #56509 - jl_lineno use maybe missed in #53799 - add some debugging dump for scm_to_julia_ mistakes
1 parent e3779c7 commit 5f433d9

File tree

5 files changed

+23
-22
lines changed

5 files changed

+23
-22
lines changed

base/float.jl

+8-5
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,16 @@ significand_mask(::Type{Float16}) = 0x03ff
122122
mantissa(x::T) where {T} = reinterpret(Unsigned, x) & significand_mask(T)
123123

124124
for T in (Float16, Float32, Float64)
125-
@eval significand_bits(::Type{$T}) = $(trailing_ones(significand_mask(T)))
126-
@eval exponent_bits(::Type{$T}) = $(sizeof(T)*8 - significand_bits(T) - 1)
127-
@eval exponent_bias(::Type{$T}) = $(Int(exponent_one(T) >> significand_bits(T)))
125+
sb = trailing_ones(significand_mask(T))
126+
em = exponent_mask(T)
127+
eb = Int(exponent_one(T) >> sb)
128+
@eval significand_bits(::Type{$T}) = $(sb)
129+
@eval exponent_bits(::Type{$T}) = $(sizeof(T)*8 - sb - 1)
130+
@eval exponent_bias(::Type{$T}) = $(eb)
128131
# maximum float exponent
129-
@eval exponent_max(::Type{$T}) = $(Int(exponent_mask(T) >> significand_bits(T)) - exponent_bias(T) - 1)
132+
@eval exponent_max(::Type{$T}) = $(Int(em >> sb) - eb - 1)
130133
# maximum float exponent without bias
131-
@eval exponent_raw_max(::Type{$T}) = $(Int(exponent_mask(T) >> significand_bits(T)))
134+
@eval exponent_raw_max(::Type{$T}) = $(Int(em >> sb))
132135
end
133136

134137
"""

base/range.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ function _getindex(v::UnitRange{T}, i::Integer) where {T<:OverflowSafe}
954954
end
955955

956956
let BitInteger64 = Union{Int8,Int16,Int32,Int64,UInt8,UInt16,UInt32,UInt64} # for bootstrapping
957-
function checkbounds(::Type{Bool}, v::StepRange{<:BitInteger64, <:BitInteger64}, i::BitInteger64)
957+
global function checkbounds(::Type{Bool}, v::StepRange{<:BitInteger64, <:BitInteger64}, i::BitInteger64)
958958
res = widemul(step(v), i-oneunit(i)) + first(v)
959959
(0 < i) & ifelse(0 < step(v), res <= last(v), res >= last(v))
960960
end

src/ast.c

+2
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,8 @@ static jl_value_t *scm_to_julia_(fl_context_t *fl_ctx, value_t e, jl_module_t *m
669669
if (iscvalue(e) && cv_class((cvalue_t*)ptr(e)) == jl_ast_ctx(fl_ctx)->jvtype) {
670670
return *(jl_value_t**)cv_data((cvalue_t*)ptr(e));
671671
}
672+
fl_print(fl_ctx, ios_stderr, e);
673+
ios_putc('\n', ios_stderr);
672674
jl_error("malformed tree");
673675
}
674676

src/interpreter.c

-3
Original file line numberDiff line numberDiff line change
@@ -893,10 +893,7 @@ jl_value_t *NOINLINE jl_interpret_toplevel_thunk(jl_module_t *m, jl_code_info_t
893893
s->mi = NULL;
894894
s->ci = NULL;
895895
JL_GC_ENABLEFRAME(s);
896-
jl_task_t *ct = jl_current_task;
897-
size_t last_age = ct->world_age;
898896
jl_value_t *r = eval_body(stmts, s, 0, 1);
899-
ct->world_age = last_age;
900897
JL_GC_POP();
901898
return r;
902899
}

src/toplevel.c

+12-13
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,10 @@ JL_DLLEXPORT jl_value_t *jl_toplevel_eval_flex(jl_module_t *JL_NONNULL m, jl_val
10651065
if (has_opaque) {
10661066
jl_resolve_globals_in_ir((jl_array_t*)thk->code, m, NULL, 0);
10671067
}
1068+
size_t world = jl_atomic_load_acquire(&jl_world_counter);
1069+
ct->world_age = world;
10681070
result = jl_interpret_toplevel_thunk(m, thk);
1071+
ct->world_age = last_age;
10691072
}
10701073

10711074
JL_GC_POP();
@@ -1075,8 +1078,8 @@ JL_DLLEXPORT jl_value_t *jl_toplevel_eval_flex(jl_module_t *JL_NONNULL m, jl_val
10751078
JL_DLLEXPORT jl_value_t *jl_toplevel_eval(jl_module_t *m, jl_value_t *v)
10761079
{
10771080
const char *filename = jl_filename;
1078-
int lieno = jl_lineno;
1079-
return jl_toplevel_eval_flex(m, v, 1, 0, &filename, &lieno);
1081+
int lineno = jl_lineno;
1082+
return jl_toplevel_eval_flex(m, v, 1, 0, &filename, &lineno);
10801083
}
10811084

10821085
// Check module `m` is open for `eval/include`, or throw an error.
@@ -1177,14 +1180,13 @@ static jl_value_t *jl_parse_eval_all(jl_module_t *module, jl_value_t *text,
11771180
jl_task_t *ct = jl_current_task;
11781181
int last_lineno = jl_lineno;
11791182
const char *last_filename = jl_filename;
1180-
size_t last_age = ct->world_age;
11811183
int lineno = 0;
11821184
jl_lineno = 0;
11831185
const char *filename_str = jl_string_data(filename);
11841186
jl_filename = filename_str;
1185-
int err = 0;
11861187

11871188
JL_TRY {
1189+
size_t last_age = ct->world_age;
11881190
ct->world_age = jl_atomic_load_acquire(&jl_world_counter);
11891191
for (size_t i = 0; i < jl_expr_nargs(ast); i++) {
11901192
expression = jl_exprarg(ast, i);
@@ -1200,23 +1202,20 @@ static jl_value_t *jl_parse_eval_all(jl_module_t *module, jl_value_t *text,
12001202
ct->world_age = jl_atomic_load_relaxed(&jl_world_counter);
12011203
result = jl_toplevel_eval_flex(module, expression, 1, 1, &filename_str, &lineno);
12021204
}
1205+
ct->world_age = last_age;
12031206
}
12041207
JL_CATCH {
1205-
result = jl_box_long(jl_lineno); // (ab)use result to root error line
1206-
err = 1;
1207-
goto finally; // skip jl_restore_excstack
1208-
}
1209-
finally:
1210-
ct->world_age = last_age;
1211-
jl_lineno = last_lineno;
1212-
jl_filename = last_filename;
1213-
if (err) {
1208+
result = jl_box_long(lineno); // (ab)use result to root error line
1209+
jl_lineno = last_lineno;
1210+
jl_filename = last_filename;
12141211
if (jl_loaderror_type == NULL)
12151212
jl_rethrow();
12161213
else
12171214
jl_rethrow_other(jl_new_struct(jl_loaderror_type, filename, result,
12181215
jl_current_exception(ct)));
12191216
}
1217+
jl_lineno = last_lineno;
1218+
jl_filename = last_filename;
12201219
JL_GC_POP();
12211220
return result;
12221221
}

0 commit comments

Comments
 (0)