0
0
mirror of https://github.com/vim/vim.git synced 2025-07-26 11:04:33 -04:00

patch 8.2.2409: Vim9: profiling only works for one function

Problem:    Vim9: profiling only works for one function.
Solution:   Select the right instructions when calling and returning.
            (closes #7743)
This commit is contained in:
Bram Moolenaar 2021-01-25 21:01:48 +01:00
parent 5c829bf229
commit e5ea346a07
5 changed files with 54 additions and 26 deletions

View File

@ -5,6 +5,7 @@ CheckFeature profile
source shared.vim source shared.vim
source screendump.vim source screendump.vim
source vim9.vim
func Test_profile_func() func Test_profile_func()
call RunProfileFunc('func', 'let', 'let') call RunProfileFunc('func', 'let', 'let')
@ -583,4 +584,21 @@ func Test_profile_typed_func()
call delete('XtestProfile') call delete('XtestProfile')
endfunc endfunc
func Test_vim9_profiling()
" only tests that compiling and calling functions doesn't crash
let lines =<< trim END
vim9script
def Func()
Crash()
enddef
def Crash()
enddef
prof start /tmp/profile.log
prof func Func
Func()
END
call CheckScriptSuccess(lines)
endfunc
" vim: shiftwidth=2 sts=2 expandtab " vim: shiftwidth=2 sts=2 expandtab

View File

@ -750,6 +750,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
2409,
/**/ /**/
2408, 2408,
/**/ /**/

View File

@ -408,3 +408,13 @@ extern garray_T def_functions;
// Used for "lnum" when a range is to be taken from the stack and "!" is used. // Used for "lnum" when a range is to be taken from the stack and "!" is used.
#define LNUM_VARIABLE_RANGE_ABOVE -888 #define LNUM_VARIABLE_RANGE_ABOVE -888
#ifdef FEAT_PROFILE
# define PROFILING(ufunc) (do_profiling == PROF_YES && (ufunc)->uf_profiling)
# define INSTRUCTIONS(dfunc) \
((do_profiling == PROF_YES && (dfunc->df_ufunc)->uf_profiling) \
? (dfunc)->df_instr_prof : (dfunc)->df_instr)
#else
# define PROFILING FALSE
# define INSTRUCTIONS(dfunc) ((dfunc)->df_instr)
#endif

View File

@ -1775,9 +1775,9 @@ generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
return FAIL; return FAIL;
} }
} }
if (func_needs_compiling(ufunc, cctx->ctx_profiling) if (func_needs_compiling(ufunc, PROFILING(ufunc))
&& compile_def_function(ufunc, ufunc->uf_ret_type == NULL, && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
cctx->ctx_profiling, NULL) == FAIL) PROFILING(ufunc), NULL) == FAIL)
return FAIL; return FAIL;
} }
@ -2615,8 +2615,8 @@ generate_funcref(cctx_T *cctx, char_u *name)
return FAIL; return FAIL;
// Need to compile any default values to get the argument types. // Need to compile any default values to get the argument types.
if (func_needs_compiling(ufunc, cctx->ctx_profiling) if (func_needs_compiling(ufunc, PROFILING(ufunc))
&& compile_def_function(ufunc, TRUE, cctx->ctx_profiling, NULL) && compile_def_function(ufunc, TRUE, PROFILING(ufunc), NULL)
== FAIL) == FAIL)
return FAIL; return FAIL;
return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type); return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
@ -3111,7 +3111,7 @@ compile_lambda(char_u **arg, cctx_T *cctx)
clear_tv(&rettv); clear_tv(&rettv);
// Compile the function into instructions. // Compile the function into instructions.
compile_def_function(ufunc, TRUE, cctx->ctx_profiling, cctx); compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx);
clear_evalarg(&evalarg, NULL); clear_evalarg(&evalarg, NULL);
@ -5088,8 +5088,8 @@ compile_nested_function(exarg_T *eap, cctx_T *cctx)
r = eap->skip ? OK : FAIL; r = eap->skip ? OK : FAIL;
goto theend; goto theend;
} }
if (func_needs_compiling(ufunc, cctx->ctx_profiling) if (func_needs_compiling(ufunc, PROFILING(ufunc))
&& compile_def_function(ufunc, TRUE, cctx->ctx_profiling, cctx) && compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx)
== FAIL) == FAIL)
{ {
func_ptr_unref(ufunc); func_ptr_unref(ufunc);

View File

@ -181,6 +181,16 @@ call_dfunc(int cdf_idx, partial_T *pt, int argcount_arg, ectx_T *ectx)
return FAIL; return FAIL;
} }
#ifdef FEAT_PROFILE
// Profiling might be enabled/disabled along the way. This should not
// fail, since the function was compiled before and toggling profiling
// doesn't change any errors.
if (func_needs_compiling(ufunc, PROFILING(ufunc))
&& compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
== FAIL)
return FAIL;
#endif
if (ufunc->uf_va_name != NULL) if (ufunc->uf_va_name != NULL)
{ {
// Need to make a list out of the vararg arguments. // Need to make a list out of the vararg arguments.
@ -293,7 +303,7 @@ call_dfunc(int cdf_idx, partial_T *pt, int argcount_arg, ectx_T *ectx)
// Set execution state to the start of the called function. // Set execution state to the start of the called function.
ectx->ec_dfunc_idx = cdf_idx; ectx->ec_dfunc_idx = cdf_idx;
ectx->ec_instr = dfunc->df_instr; ectx->ec_instr = INSTRUCTIONS(dfunc);
entry = estack_push_ufunc(ufunc, 1); entry = estack_push_ufunc(ufunc, 1);
if (entry != NULL) if (entry != NULL)
{ {
@ -542,7 +552,7 @@ func_return(ectx_T *ectx)
ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
+ STACK_FRAME_IDX_OFF)->vval.v_number; + STACK_FRAME_IDX_OFF)->vval.v_number;
dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx; dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
ectx->ec_instr = dfunc->df_instr; ectx->ec_instr = INSTRUCTIONS(dfunc);
if (ret_idx > 0) if (ret_idx > 0)
{ {
@ -1103,6 +1113,7 @@ fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
return OK; return OK;
} }
/* /*
* Call a "def" function from old Vim script. * Call a "def" function from old Vim script.
* Return OK or FAIL. * Return OK or FAIL.
@ -1135,11 +1146,6 @@ call_def_function(
int save_did_emsg_def = did_emsg_def; int save_did_emsg_def = did_emsg_def;
int trylevel_at_start = trylevel; int trylevel_at_start = trylevel;
int orig_funcdepth; int orig_funcdepth;
#ifdef FEAT_PROFILE
int profiling = do_profiling == PROF_YES && ufunc->uf_profiling;
#else
# define profiling FALSE
#endif
// Get pointer to item in the stack. // Get pointer to item in the stack.
#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx) #define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
@ -1152,8 +1158,8 @@ call_def_function(
#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx) #define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
if (ufunc->uf_def_status == UF_NOT_COMPILED if (ufunc->uf_def_status == UF_NOT_COMPILED
|| (func_needs_compiling(ufunc, profiling) || (func_needs_compiling(ufunc, PROFILING(ufunc))
&& compile_def_function(ufunc, FALSE, profiling, NULL) && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
== FAIL)) == FAIL))
{ {
if (did_emsg_cumul + did_emsg == did_emsg_before) if (did_emsg_cumul + did_emsg == did_emsg_before)
@ -1166,11 +1172,7 @@ call_def_function(
// Check the function was really compiled. // Check the function was really compiled.
dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
+ ufunc->uf_dfunc_idx; + ufunc->uf_dfunc_idx;
if (( if (INSTRUCTIONS(dfunc) == NULL)
#ifdef FEAT_PROFILE
profiling ? dfunc->df_instr_prof :
#endif
dfunc->df_instr) == NULL)
{ {
iemsg("using call_def_function() on not compiled function"); iemsg("using call_def_function() on not compiled function");
return FAIL; return FAIL;
@ -1309,11 +1311,7 @@ call_def_function(
++ectx.ec_stack.ga_len; ++ectx.ec_stack.ga_len;
} }
#ifdef FEAT_PROFILE ectx.ec_instr = INSTRUCTIONS(dfunc);
ectx.ec_instr = profiling ? dfunc->df_instr_prof : dfunc->df_instr;
#else
ectx.ec_instr = dfunc->df_instr;
#endif
} }
// Following errors are in the function, not the caller. // Following errors are in the function, not the caller.