Skip to content

Commit

Permalink
kernel: replace hack in the reader for setting function locks
Browse files Browse the repository at this point in the history
The old code ignored the reader -> interpreter -> coder layering and
just assumed that the function currently being coded is referenced by
`CURR_FUNC()`. Instead we now inform the interpreter about the locks
which then (if applicable) informs the coder and finally that sets the
locks for the actual function being coded.
  • Loading branch information
fingolfin committed Dec 15, 2022
1 parent 6a513e9 commit 020d922
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 21 deletions.
7 changes: 7 additions & 0 deletions src/code.c
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,13 @@ void CodeFuncExprBegin(CodeState * cs,
assert( stat1 == OFFSET_FIRST_STAT );
}

#ifdef HPCGAP
void CodeFuncExprSetLocks(CodeState * cs, Obj locks)
{
SET_LCKS_FUNC(cs->currFunc, locks);
}
#endif

Expr CodeFuncExprEnd(CodeState * cs, UInt nr, BOOL pushExpr, Int endLine)
{
Expr expr; /* function expression, result */
Expand Down
4 changes: 4 additions & 0 deletions src/code.h
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,10 @@ void CodeFuncExprBegin(CodeState * cs,
UInt gapnameid,
Int startLine);

#ifdef HPCGAP
void CodeFuncExprSetLocks(CodeState * cs, Obj locks);
#endif

Expr CodeFuncExprEnd(CodeState * cs, UInt nr, BOOL pushExpr, Int endLine);

/****************************************************************************
Expand Down
14 changes: 14 additions & 0 deletions src/intrprtr.c
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,20 @@ void IntrFuncExprBegin(
CodeFuncExprBegin(intr->cs, narg, nloc, nams, intr->gapnameid, startLine);
}

#ifdef HPCGAP
void IntrFuncExprSetLocks(IntrState * intr, Obj locks)
{
/* ignore or code */
SKIP_IF_RETURNING();
SKIP_IF_IGNORING();

/* otherwise must be coding */
GAP_ASSERT(intr->coding > 0);

CodeFuncExprSetLocks(intr->cs, locks);
}
#endif

void IntrFuncExprEnd(IntrState * intr, UInt nr, Int endLine)
{
/* ignore or code */
Expand Down
5 changes: 4 additions & 1 deletion src/intrprtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,11 @@ void IntrFuncCallOptionsEnd(IntrState * intr, UInt nr);
void IntrFuncExprBegin(
IntrState * intr, Int narg, Int nloc, Obj nams, Int startLine);

void IntrFuncExprEnd(IntrState * intr, UInt nr, Int endLine);
#ifdef HPCGAP
void IntrFuncExprSetLocks(IntrState * intr, Obj locks);
#endif

void IntrFuncExprEnd(IntrState * intr, UInt nr, Int endLine);

/****************************************************************************
**
Expand Down
39 changes: 19 additions & 20 deletions src/read.c
Original file line number Diff line number Diff line change
Expand Up @@ -1149,7 +1149,7 @@ typedef struct {

/****************************************************************************
**
*F ReadFuncArgList(<follow>, <is_atomic>, <symbol>, <symbolstr>)
*F ReadFuncArgList(<follow>, <isAtomic>, <symbol>, <symbolstr>)
** . . . . . . . . . . read a function argument list.
**
** 'ReadFuncArgList' reads the argument list of a function. In case of an
Expand All @@ -1158,7 +1158,7 @@ typedef struct {
** <ArgList> := ('readwrite'|'readonly') <Ident>
** {',' ('readwrite'|'readonly') <Ident> } ( '...' )
**
** is_atomic: Is this an atomic function?
** isAtomic: Is this an atomic function?
** symbol: The end symbol of the arglist (usually S_RPAREN, but S_RBRACE
** for lambda functions).
** symbolstr: symbol as an ascii string
Expand All @@ -1169,7 +1169,7 @@ typedef struct {

static ArgList ReadFuncArgList(ReaderState * rs,
TypSymbolSet follow,
Int is_atomic,
BOOL isAtomic,
UInt symbol,
const Char * symbolstr)
{
Expand All @@ -1182,7 +1182,7 @@ static ArgList ReadFuncArgList(ReaderState * rs,
BOOL isvarg = FALSE; // does function have varargs?

#ifdef HPCGAP
if (is_atomic)
if (isAtomic)
locks = NEW_STRING(4);
#endif

Expand All @@ -1204,7 +1204,7 @@ static ArgList ReadFuncArgList(ReaderState * rs,
lockqual = LOCK_QUAL_NONE;
#endif
if (rs->s.Symbol == S_READWRITE) {
if (!is_atomic) {
if (!isAtomic) {
SyntaxError(&rs->s, "'readwrite' argument of non-atomic function");
}
#ifdef HPCGAP
Expand All @@ -1215,7 +1215,7 @@ static ArgList ReadFuncArgList(ReaderState * rs,
Match_(rs, S_READWRITE, "readwrite", follow);
}
else if (rs->s.Symbol == S_READONLY) {
if (!is_atomic) {
if (!isAtomic) {
SyntaxError(&rs->s, "'readonly' argument of non-atomic function");
}
#ifdef HPCGAP
Expand All @@ -1231,7 +1231,7 @@ static ArgList ReadFuncArgList(ReaderState * rs,
narg += 1;
PushPlist(nams, MakeImmString(rs->s.Value));
#ifdef HPCGAP
if (is_atomic) {
if (isAtomic) {
GrowString(locks, narg);
SET_LEN_STRING(locks, narg);
CHARS_STRING(locks)[narg - 1] = lockqual;
Expand Down Expand Up @@ -1271,7 +1271,7 @@ static ArgList ReadFuncArgList(ReaderState * rs,

static void ReadFuncExprBody(ReaderState * rs,
TypSymbolSet follow,
Int isAbbrev,
BOOL isAbbrev,
Int nloc,
ArgList args,
Int startLine)
Expand All @@ -1285,6 +1285,9 @@ static void ReadFuncExprBody(ReaderState * rs,
TRY_IF_NO_ERROR {
IntrFuncExprBegin(&rs->intr, args.isvarg ? -args.narg : args.narg, nloc,
args.nams, startLine);
#ifdef HPCGAP
IntrFuncExprSetLocks(&rs->intr, args.locks);
#endif
}

if (isAbbrev) {
Expand All @@ -1296,10 +1299,6 @@ static void ReadFuncExprBody(ReaderState * rs,
nr = 1;
}
else {
#ifdef HPCGAP
if (rs->s.NrError == 0)
SET_LCKS_FUNC(CURR_FUNC(), args.locks);
#endif
// <Statements>
UInt oldLoopNesting = rs->LoopNesting;
rs->LoopNesting = 0;
Expand Down Expand Up @@ -1372,30 +1371,30 @@ static UInt ReadLocals(ReaderState * rs, TypSymbolSet follow, Obj nams)
static void ReadFuncExpr(ReaderState * rs, TypSymbolSet follow, Char mode)
{
Int startLine; // line number of function keyword
int is_atomic = 0; // is this an atomic function?
BOOL isAtomic = FALSE; // is this an atomic function?
UInt nloc = 0; // number of locals
ArgList args;

/* begin the function */
startLine = GetInputLineNumber(rs->s.input);
if (rs->s.Symbol == S_ATOMIC) {
Match_(rs, S_ATOMIC, "atomic", follow);
is_atomic = 1;
isAtomic = TRUE;
} else if (mode == 'a') {
// in this case the atomic keyword was matched away by ReadAtomic
// before we realised we were reading an atomic function
is_atomic = 1;
isAtomic = TRUE;
}
Match_(rs, S_FUNCTION, "function", follow);
Match_(rs, S_LPAREN, "(", S_IDENT|S_RPAREN|S_LOCAL|STATBEGIN|S_END|follow);

args = ReadFuncArgList(rs, follow, is_atomic, S_RPAREN, ")");
args = ReadFuncArgList(rs, follow, isAtomic, S_RPAREN, ")");

if (rs->s.Symbol == S_LOCAL) {
nloc = ReadLocals(rs, follow, args.nams);
}

ReadFuncExprBody(rs, follow, 0, nloc, args, startLine);
ReadFuncExprBody(rs, follow, FALSE, nloc, args, startLine);

/* 'end' */
Match_(rs, S_END, "while parsing a function: statement or 'end'", follow);
Expand All @@ -1416,12 +1415,12 @@ static void ReadFuncExprAbbrevMulti(ReaderState * rs, TypSymbolSet follow)
{
Match_(rs, S_LBRACE, "{", follow);

ArgList args = ReadFuncArgList(rs, follow, 0, S_RBRACE, "}");
ArgList args = ReadFuncArgList(rs, follow, FALSE, S_RBRACE, "}");

/* match away the '->' */
Match_(rs, S_MAPTO, "->", follow);

ReadFuncExprBody(rs, follow, 1, 0, args, GetInputLineNumber(rs->s.input));
ReadFuncExprBody(rs, follow, TRUE, 0, args, GetInputLineNumber(rs->s.input));
}

/****************************************************************************
Expand Down Expand Up @@ -1451,7 +1450,7 @@ static void ReadFuncExprAbbrevSingle(ReaderState * rs, TypSymbolSet follow)
/* match away the '->' */
Match_(rs, S_MAPTO, "->", follow);

ReadFuncExprBody(rs, follow, 1, 0, args, GetInputLineNumber(rs->s.input));
ReadFuncExprBody(rs, follow, TRUE, 0, args, GetInputLineNumber(rs->s.input));
}

/****************************************************************************
Expand Down

0 comments on commit 020d922

Please sign in to comment.