Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a coder to reread syntaxtrees #3371

Merged
merged 8 commits into from
May 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 37 additions & 42 deletions src/code.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ static Stat * ADDR_STAT(Stat stat)
return (Stat *)STATE(PtrBody) + stat / sizeof(Stat);
}

static void WRITE_EXPR(Expr expr, UInt idx, UInt val)
void WRITE_EXPR(Expr expr, UInt idx, UInt val)
{
ADDR_EXPR(expr)[idx] = val;
}
Expand Down Expand Up @@ -250,20 +250,7 @@ Obj GET_VALUE_FROM_CURRENT_BODY(Int ix)
return ELM_PLIST(values, ix);
}


/****************************************************************************
**
*F NewStat( <type>, <size> ) . . . . . . . . . . . allocate a new statement
**
** 'NewStat' allocates a new statement memory block of type <type> and
** <size> bytes. 'NewStat' returns the identifier of the new statement.
**
** NewStatWithProf( <type>, <size>, <line>, <file> ) allows the line number
** and fileid of the statement to also be specified, else the current line
** and file when NewStat was called is used. line=0, file=0 is used
** to denote a statement which should not be tracked.
*/
static Stat NewStatWithProf (
Stat NewStatOrExpr (
UInt type,
UInt size,
UInt line)
Expand Down Expand Up @@ -297,7 +284,7 @@ static Stat NewStatWithProf (

static Stat NewStat(UInt type, UInt size)
{
return NewStatWithProf(type, size, GetInputLineNumber());
return NewStatOrExpr(type, size, GetInputLineNumber());
}


Expand Down Expand Up @@ -337,7 +324,7 @@ static inline UInt CapacityStatStack(void)
return SIZE_BAG(CS(StackStat)) / sizeof(Stat) - 1;
}

static void PushStat (
void PushStat (
Stat stat )
{
/* there must be a stack, it must not be underfull or overfull */
Expand Down Expand Up @@ -553,14 +540,7 @@ static void PushBinaryOp(UInt type)
}


/****************************************************************************
**
*F PushValue( <val> ) . . . . . . . . . . . . . . store value in values list
**
** 'PushValue' pushes a value into the value list of the body, and returns
** the index at which the value was inserted.
*/
static Int PushValue(Obj val)
Int AddValueToBody(Obj val)
{
BodyHeader * header = (BodyHeader *)STATE(PtrBody);
Obj values = header->values;
Expand Down Expand Up @@ -853,7 +833,7 @@ void CodeFuncExprBegin (
assert( stat1 == OFFSET_FIRST_STAT );
}

void CodeFuncExprEnd(UInt nr)
Expr CodeFuncExprEnd(UInt nr, UInt pushExpr)
{
Expr expr; /* function expression, result */
Stat stat1; /* single statement of body */
Expand All @@ -873,10 +853,18 @@ void CodeFuncExprEnd(UInt nr)
}
else {
stat1 = PopStat();
PushStat( stat1 );
if ( TNUM_STAT(stat1) != T_RETURN_VOID
&& TNUM_STAT(stat1) != T_RETURN_OBJ )
{
PushStat(stat1);
// If we code a function where the body is already packed into nested
// sequence statements, e.g., from reading in a syntax tree, we need
// to find the last `real` statement of the last innermost sequence
// statement to determine if there is already a return or not.
while (T_SEQ_STAT <= TNUM_STAT(stat1) &&
TNUM_STAT(stat1) <= T_SEQ_STAT7) {
UInt size = SIZE_STAT(stat1) / sizeof(Stat);
stat1 = READ_STAT(stat1, size - 1);
}
if (TNUM_STAT(stat1) != T_RETURN_VOID &&
TNUM_STAT(stat1) != T_RETURN_OBJ) {
CodeReturnVoidWhichIsNotProfiled();
nr++;
}
Expand Down Expand Up @@ -917,17 +905,21 @@ void CodeFuncExprEnd(UInt nr)
/* if this was inside another function definition, make the expression */
/* and store it in the function expression list of the outer function */
if (STATE(CurrLVars) != CS(CodeLVars)) {
len = PushValue(fexp);
len = AddValueToBody(fexp);
expr = NewExpr( T_FUNC_EXPR, sizeof(Expr) );
WRITE_EXPR(expr, 0, len);
PushExpr( expr );
if (pushExpr) {
PushExpr(expr);
}
return expr;
}

// otherwise, make the function and store it in 'CS(CodeResult)'
else {
CS(CodeResult) = MakeFunction(fexp);
}

return 0;
}


Expand Down Expand Up @@ -1450,7 +1442,7 @@ void CodeReturnVoidWhichIsNotProfiled ( void )

/* allocate the return-statement, without profile information */

stat = NewStatWithProf( T_RETURN_VOID, 0 * sizeof(Expr), 0 );
stat = NewStatOrExpr( T_RETURN_VOID, 0 * sizeof(Expr), 0 );

/* push the return-statement */
PushStat( stat );
Expand Down Expand Up @@ -1618,7 +1610,7 @@ void CodeIntExpr(Obj val)
else {
GAP_ASSERT(TNUM_OBJ(val) == T_INTPOS || TNUM_OBJ(val) == T_INTNEG);
expr = NewExpr( T_INT_EXPR, sizeof(UInt) );
Int ix = PushValue(val);
Int ix = AddValueToBody(val);
WRITE_EXPR(expr, 0, ix);
}

Expand Down Expand Up @@ -1817,7 +1809,7 @@ void CodeStringExpr (
GAP_ASSERT(IS_STRING_REP(str));

Expr string = NewExpr( T_STRING_EXPR, sizeof(UInt) );
Int ix = PushValue(str);
Int ix = AddValueToBody(str);
WRITE_EXPR(string, 0, ix);
PushExpr( string );
}
Expand All @@ -1827,7 +1819,7 @@ void CodePragma(Obj pragma)
GAP_ASSERT(IS_STRING_REP(pragma));

Expr pragmaexpr = NewStat(T_PRAGMA, sizeof(UInt));
Int ix = PushValue(pragma);
Int ix = AddValueToBody(pragma);
WRITE_EXPR(pragmaexpr, 0, ix);
PushStat(pragmaexpr);
}
Expand Down Expand Up @@ -1906,7 +1898,7 @@ static UInt CheckForCommonFloat(const Char * str)
return 0;
}

static void CodeLazyFloatExpr(Obj str)
Expr CodeLazyFloatExpr(Obj str, UInt pushExpr)
{
UInt ix;

Expand All @@ -1917,19 +1909,22 @@ static void CodeLazyFloatExpr(Obj str)
if (!ix)
ix = getNextFloatExprNumber();
WRITE_EXPR(fl, 0, ix);
WRITE_EXPR(fl, 1, PushValue(str));
WRITE_EXPR(fl, 1, AddValueToBody(str));

/* push the expression */
PushExpr(fl);
if (pushExpr) {
PushExpr(fl);
}
return fl;
fingolfin marked this conversation as resolved.
Show resolved Hide resolved
}

static void CodeEagerFloatExpr(Obj str, Char mark)
{
/* Eager case, do the conversion now */
Expr fl = NewExpr(T_FLOAT_EXPR_EAGER, sizeof(UInt) * 3);
Obj v = CALL_2ARGS(CONVERT_FLOAT_LITERAL_EAGER, str, ObjsChar[(Int)mark]);
WRITE_EXPR(fl, 0, PushValue(v));
WRITE_EXPR(fl, 1, PushValue(str)); // store for printing
WRITE_EXPR(fl, 0, AddValueToBody(v));
WRITE_EXPR(fl, 1, AddValueToBody(str)); // store for printing
WRITE_EXPR(fl, 2, (UInt)mark);
PushExpr(fl);
}
Expand All @@ -1955,7 +1950,7 @@ void CodeFloatExpr(Obj s)
CodeEagerFloatExpr(s, mark);
}
else {
CodeLazyFloatExpr(s);
CodeLazyFloatExpr(s, 1);
}
}

Expand Down
46 changes: 43 additions & 3 deletions src/code.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,25 @@ EXPORT_INLINE Obj VALUES_BODY(Obj body)
return BODY_HEADER(body)->values;
}


/****************************************************************************
**
*F NewStatOrExpr(<type>,<size>,<line>) . . . . . . allocate a new statement
**
** 'NewStatOrExpr' allocates a new statement or expressions memory block of
** type <type> and with <size> bytes. It also records the line number <line>
** of the statement for profiling. It returns the offset of the new
** statement.
**
** Callers may pass zero for <line> to denote a statement which should not
** be tracked by the profiling code.
*/
Stat NewStatOrExpr(UInt type, UInt size, UInt line);


void PushStat(Stat stat);


/****************************************************************************
**
*V OFFSET_FIRST_STAT . . . . . . . . . . offset of first statement in a body
Expand All @@ -128,6 +147,9 @@ enum {
Obj NewFunctionBody(void);


void WRITE_EXPR(Expr expr, UInt idx, UInt val);


/****************************************************************************
**
*S T_<name> . . . . . . . . . . . . . . symbolic names for statement types
Expand Down Expand Up @@ -650,7 +672,7 @@ void CodeFuncCallEnd(UInt funccall, UInt options, UInt nr);
/****************************************************************************
**
*F CodeFuncExprBegin(<narg>,<nloc>,<nams>,<startline>) . code function expression, begin
*F CodeFuncExprEnd(<nr>) . . . . . . . . . . . code function expression, end
*F CodeFuncExprEnd(<nr>,<pushExpr>) . . . . . code function expression, end
**
** 'CodeFuncExprBegin' is an action to code a function expression. It is
** called when the reader encounters the beginning of a function expression.
Expand All @@ -660,12 +682,23 @@ void CodeFuncCallEnd(UInt funccall, UInt options, UInt nr);
**
** 'CodeFuncExprEnd' is an action to code a function expression. It is
** called when the reader encounters the end of a function expression. <nr>
** is the number of statements in the body of the function.
** is the number of statements in the body of the function. If <pushExpr>
** is set, the current function expression is pushed on the expression stack.
**
*/
void CodeFuncExprBegin(Int narg, Int nloc, Obj nams, Int startLine);

void CodeFuncExprEnd(UInt nr);
Expr CodeFuncExprEnd(UInt nr, UInt pushExpr);

/****************************************************************************
**
*F AddValueToBody( <val> ) . . . . . . . . . . . store value in values list
**
** 'AddValueToBody' adds a value into the value list of the body of the
** function currently being coded, and returns the index at which the value
** was inserted. This function must only be called while coding a function.
*/
Int AddValueToBody(Obj val);

/****************************************************************************
**
Expand Down Expand Up @@ -1071,6 +1104,13 @@ void CodeStringExpr(Obj str);
void CodePragma(Obj pragma);


/****************************************************************************
**
*F CodeLazyFloatExpr(<str>,<pushExpr>) . . . . . code lazy float expression
*/
Expr CodeLazyFloatExpr(Obj str, UInt pushExpr);


/****************************************************************************
**
*F CodeFloatExpr(<str>) . . . . . . . . . . code literal float expression
Expand Down
8 changes: 8 additions & 0 deletions src/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,14 @@ Obj RequireArgumentEx(const char * funcname,
"must be a mutable " type)


/****************************************************************************
**
*F RequirePlainRec
*/
#define RequirePlainRec(funcname, op) \
RequireArgumentCondition(funcname, op, IS_PREC(op), \
"must be a plain record")

/****************************************************************************
**
*F GetSmallIntEx, GetSmallInt
Expand Down
4 changes: 2 additions & 2 deletions src/intrprtr.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ static inline void FinishAndCallFakeFuncExpr(void)
assert(STATE(IntrCoding) == 0);

// code a function expression (with one statement in the body)
CodeFuncExprEnd(1);
CodeFuncExprEnd(1, 1);

// switch back to immediate mode and get the function
Obj func = CodeEnd(0);
Expand Down Expand Up @@ -526,7 +526,7 @@ void IntrFuncExprEnd(UInt nr)
assert(STATE(IntrCoding) > 0);

STATE(IntrCoding)--;
CodeFuncExprEnd(nr);
CodeFuncExprEnd(nr, 1);

if (STATE(IntrCoding) == 0) {
// switch back to immediate mode and get the function
Expand Down
Loading