Skip to content

Commit

Permalink
Add line number / location information to compiled functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisJefferson committed Apr 19, 2016
1 parent 2b83b36 commit c9fd41f
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 20 deletions.
57 changes: 45 additions & 12 deletions src/calls.c
Original file line number Diff line number Diff line change
Expand Up @@ -1300,7 +1300,8 @@ void PrintFunction (
Obj oldLVars; /* terrible hack */
UInt i; /* loop variable */
UInt isvarg; /* does function have varargs? */


UInt outputtedfunc; /* Have we outputted function? */
isvarg = 0;

if ( IS_OPERATION(func) ) {
Expand Down Expand Up @@ -1348,7 +1349,29 @@ void PrintFunction (

/* print the body */
if ( BODY_FUNC(func) == 0 || SIZE_OBJ(BODY_FUNC(func)) == NUMBER_HEADER_ITEMS_BODY*sizeof(Obj) ) {
Pr("<<kernel or compiled code>>",0L,0L);
outputtedfunc = 0;
if ( BODY_FUNC(func) ) {
Obj body = BODY_FUNC(func);
if ( FILENAME_BODY(body) && IS_STRING_REP(FILENAME_BODY(body)) ) {
if ( STARTLINE_BODY(body) ) {
if ( (IS_STRING_REP(STARTLINE_BODY(body))) ) {
Pr("<<kernel code from %s:%s>>",
(Int)CSTR_STRING(FILENAME_BODY(body)),
(Int)CSTR_STRING(STARTLINE_BODY(body)));
outputtedfunc = 1;
}
else if ( (IS_INTOBJ(STARTLINE_BODY(body))) ) {
Pr("<<compiled GAP code from %s:%d>>",
(Int)CSTR_STRING(FILENAME_BODY(body)),
INT_INTOBJ(STARTLINE_BODY(body)));
outputtedfunc = 1;
}
}
}
}
if(!outputtedfunc) {
Pr("<<kernel or compiled code>>",0L,0L);
}
}
else {
SWITCH_TO_NEW_LVARS( func, narg, NLOC_FUNC(func),
Expand Down Expand Up @@ -1809,18 +1832,25 @@ Obj FuncFILENAME_FUNC(Obj self, Obj func) {
return Fail;
}

Obj FuncSTARTLINE_FUNC(Obj self, Obj func) {
Obj FuncSTART_FUNC(Obj self, Obj func) {
/* check the argument */
if ( TNUM_OBJ(func) != T_FUNCTION ) {
ErrorQuit( "<func> must be a function", 0L, 0L );
return 0;
}

/* check the argument */
if ( TNUM_OBJ(func) != T_FUNCTION ) {
ErrorQuit( "<func> must be a function", 0L, 0L );
return 0;
}
if (BODY_FUNC(func)) {
Obj sl = STARTLINE_BODY(BODY_FUNC(func));
if (sl)
return sl;
}
return Fail;
}

if (BODY_FUNC(func)) {
Obj sl = STARTLINE_BODY(BODY_FUNC(func));
if (sl)
return sl;
Obj FuncSTARTLINE_FUNC(Obj self, Obj func) {
Obj start = FuncSTART_FUNC(self, func);
if(IS_INTOBJ(start)) {
return start;
}
return Fail;
}
Expand Down Expand Up @@ -2029,6 +2059,9 @@ static StructGVarFunc GVarFuncs [] = {
{ "FILENAME_FUNC", 1, "func",
FuncFILENAME_FUNC, "src/calls.c:FILENAME_FUNC" },

{ "START_FUNC", 1, "func",
FuncSTART_FUNC, "src/calls.c:START_FUNC" },

{ "STARTLINE_FUNC", 1, "func",
FuncSTARTLINE_FUNC, "src/calls.c:STARTLINE_FUNC" },

Expand Down
19 changes: 17 additions & 2 deletions src/code.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,17 @@

/****************************************************************************
**
*V FIRST_STAT_CURR_FUNC . . . . . . . . index of first statement in a body
** Function headers
**
** 'FIRST_STAT_CURR_FUNC' is the index of the first statement in a body.
** 'FILENAME_BODY' is a string containing the file of a function
** 'STARTLINE_BODY' is either an integer containing the first line of the
** function, or a string containing the C function a kernel function
** is compiled from (generated from it's cookie).
** If this is a string, or int, can be used to distinguish between
** C and GAP code.
** 'ENDLINE_BODY' is the integer containing the last line of the function.
**
** All of these variables may be 0, if the information is not known.
*/


Expand All @@ -51,6 +59,13 @@
#define ENDLINE_BODY(body) (ADDR_OBJ(body)[2])
#define NUMBER_HEADER_ITEMS_BODY 3

/****************************************************************************
**
*V FIRST_STAT_CURR_FUNC . . . . . . . . index of first statement in a body
**
** 'FIRST_STAT_CURR_FUNC' is the index of the first statement in a body.
*/

#define FIRST_STAT_CURR_FUNC (sizeof(Stat)+NUMBER_HEADER_ITEMS_BODY*sizeof(Bag))

/****************************************************************************
Expand Down
30 changes: 25 additions & 5 deletions src/gap.c
Original file line number Diff line number Diff line change
Expand Up @@ -2620,6 +2620,25 @@ void InitGVarOpersFromTable (
}
}

void SetupFuncInfo(Obj func, const Char* cookie)
{
const Char* pos = strchr(cookie, ':');
if ( pos ) {
Obj filename, start;
Obj body_bag = NewBag( T_BODY, NUMBER_HEADER_ITEMS_BODY*sizeof(Obj) );
char buffer[512];
Int len = 511<(pos-cookie)?511:pos-cookie;
memcpy(buffer, cookie, len);
buffer[len] = 0;
C_NEW_STRING_DYN(filename, buffer);
C_NEW_STRING_DYN(start, pos+1);
FILENAME_BODY(body_bag) = filename;
STARTLINE_BODY(body_bag) = start;
BODY_FUNC(func) = body_bag;
CHANGED_BAG(body_bag);
CHANGED_BAG(func);
}
}

/****************************************************************************
**
Expand All @@ -2632,11 +2651,12 @@ void InitGVarFuncsFromTable (

for ( i = 0; tab[i].name != 0; i++ ) {
UInt gvar = GVarName( tab[i].name );
AssGVar( gvar,
NewFunction( NameGVarObj( gvar ),
tab[i].nargs,
ArgStringToList( tab[i].args ),
tab[i].handler ) );
Obj func = NewFunction( NameGVarObj( gvar ),
tab[i].nargs,
ArgStringToList( tab[i].args ),
tab[i].handler );
SetupFuncInfo( func, tab[i].cookie );
AssGVar( gvar, func );
MakeReadOnlyGVar( gvar );
}
}
Expand Down
10 changes: 9 additions & 1 deletion tst/testinstall/varargs.tst
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,15 @@ f := function(a,b..) end;
^
gap> Display(RETURN_FIRST);
function ( object... )
<<kernel or compiled code>>
<<kernel code from src/gap.c:RETURN_FIRST>>
end
gap> Display(RunImmediateMethods);
function ( <<arg-1>>, <<arg-2>> )
<<compiled GAP code from GAPROOT/lib/oper1.g:26>>
end
gap> Display(InstallMethod);
function ( <<arg-1>>... )
<<compiled GAP code from GAPROOT/lib/oper1.g:282>>
end
gap> [1..2];
[ 1, 2 ]
Expand Down

0 comments on commit c9fd41f

Please sign in to comment.