Skip to content

Commit

Permalink
kernel: add currFunc and currBody to struct CodeState
Browse files Browse the repository at this point in the history
  • Loading branch information
fingolfin committed Dec 15, 2022
1 parent 020d922 commit 228b898
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
38 changes: 19 additions & 19 deletions src/code.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ Stat NewStatOrExpr(CodeState * cs, UInt type, UInt size, UInt line)
stat + ((size + sizeof(Stat) - 1) / sizeof(Stat)) * sizeof(Stat);

/* make certain that the current body bag is large enough */
Obj body = BODY_FUNC(CURR_FUNC());
Obj body = cs->currBody;
UInt bodySize = SIZE_BAG(body);
if (bodySize == 0)
bodySize = cs->OffsBody;
Expand Down Expand Up @@ -516,18 +516,13 @@ static void PushBinaryOp(CodeState * cs, UInt type)
}


Int AddValueToBody(Obj val)
Int AddValueToBody(CodeState * cs, Obj val)
{
BodyHeader * header = (BodyHeader *)STATE(PtrBody);
Obj values = header->values;
Obj values = VALUES_BODY(cs->currBody);
if (!values) {
values = NEW_PLIST(T_PLIST, 4);
// Recalculate header in case NEW_PLIST caused a GC
header = (BodyHeader *)STATE(PtrBody);
header->values = values;
GAP_ASSERT(STATE(PtrBody) == PTR_BAG(BODY_FUNC(CURR_FUNC())));
// This is the bag PtrBody points at
CHANGED_BAG(BODY_FUNC(CURR_FUNC()));
BODY_HEADER(cs->currBody)->values = values;
CHANGED_BAG(cs->currBody);
}
return PushPlist(values, val);
}
Expand Down Expand Up @@ -800,6 +795,9 @@ void CodeFuncExprBegin(CodeState * cs,
/* switch to this function */
SWITCH_TO_NEW_LVARS(fexp, (narg > 0 ? narg : -narg), nloc);

cs->currFunc = fexp;
cs->currBody = body;

/* allocate the top level statement sequence */
stat1 = NewStat(cs, STAT_SEQ_STAT, 8 * sizeof(Stat));
assert( stat1 == OFFSET_FIRST_STAT );
Expand All @@ -821,7 +819,7 @@ Expr CodeFuncExprEnd(CodeState * cs, UInt nr, BOOL pushExpr, Int endLine)
UInt i; /* loop variable */

/* get the function expression */
fexp = CURR_FUNC();
fexp = cs->currFunc;

/* get the body of the function */
/* push an additional return-void-statement if necessary */
Expand Down Expand Up @@ -867,7 +865,7 @@ Expr CodeFuncExprEnd(CodeState * cs, UInt nr, BOOL pushExpr, Int endLine)
}

// make the body values list (if any) immutable
Obj values = ((BodyHeader *)STATE(PtrBody))->values;
Obj values = VALUES_BODY(cs->currBody);
if (values)
MakeImmutable(values);

Expand All @@ -880,11 +878,13 @@ Expr CodeFuncExprEnd(CodeState * cs, UInt nr, BOOL pushExpr, Int endLine)

/* restore the remembered offset */
PopOffsBody(cs);
cs->currFunc = FUNC_LVARS(ENVI_FUNC(fexp));
cs->currBody = BODY_FUNC(cs->currFunc);

/* 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 = AddValueToBody(fexp);
len = AddValueToBody(cs, fexp);
expr = NewExpr(cs, EXPR_FUNC, sizeof(Expr));
WRITE_EXPR(expr, 0, len);
if (pushExpr) {
Expand Down Expand Up @@ -1576,7 +1576,7 @@ void CodeIntExpr(CodeState * cs, Obj val)
else {
GAP_ASSERT(TNUM_OBJ(val) == T_INTPOS || TNUM_OBJ(val) == T_INTNEG);
expr = NewExpr(cs, EXPR_INTPOS, sizeof(UInt));
Int ix = AddValueToBody(val);
Int ix = AddValueToBody(cs, val);
WRITE_EXPR(expr, 0, ix);
}

Expand Down Expand Up @@ -1765,7 +1765,7 @@ void CodeStringExpr(CodeState * cs, Obj str)
GAP_ASSERT(IS_STRING_REP(str));

Expr string = NewExpr(cs, EXPR_STRING, sizeof(UInt));
Int ix = AddValueToBody(str);
Int ix = AddValueToBody(cs, str);
WRITE_EXPR(string, 0, ix);
PushExpr( string );
}
Expand All @@ -1780,7 +1780,7 @@ void CodePragma(CodeState * cs, Obj pragma)
GAP_ASSERT(IS_STRING_REP(pragma));

Expr pragmaexpr = NewStat(cs, STAT_PRAGMA, sizeof(UInt));
Int ix = AddValueToBody(pragma);
Int ix = AddValueToBody(cs, pragma);
WRITE_EXPR(pragmaexpr, 0, ix);
PushStat(pragmaexpr);
}
Expand Down Expand Up @@ -1870,7 +1870,7 @@ Expr CodeLazyFloatExpr(CodeState * cs, Obj str, UInt pushExpr)
if (!ix)
ix = getNextFloatExprNumber();
WRITE_EXPR(fl, 0, ix);
WRITE_EXPR(fl, 1, AddValueToBody(str));
WRITE_EXPR(fl, 1, AddValueToBody(cs, str));

/* push the expression */
if (pushExpr) {
Expand All @@ -1884,8 +1884,8 @@ static void CodeEagerFloatExpr(CodeState * cs, Obj str, Char mark)
/* Eager case, do the conversion now */
Expr fl = NewExpr(cs, EXPR_FLOAT_EAGER, sizeof(UInt) * 3);
Obj v = CALL_2ARGS(CONVERT_FLOAT_LITERAL_EAGER, str, ObjsChar[(Int)mark]);
WRITE_EXPR(fl, 0, AddValueToBody(v));
WRITE_EXPR(fl, 1, AddValueToBody(str)); // store for printing
WRITE_EXPR(fl, 0, AddValueToBody(cs, v));
WRITE_EXPR(fl, 1, AddValueToBody(cs, str)); // store for printing
WRITE_EXPR(fl, 2, (UInt)mark);
PushExpr(fl);
}
Expand Down
8 changes: 7 additions & 1 deletion src/code.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ struct CodeState {

// place to store the previous value of STATE(CurrLVars)
Bag CodeLVars;

//
Obj currFunc;

//
Obj currBody;
};

typedef struct CodeState CodeState;
Expand Down Expand Up @@ -745,7 +751,7 @@ Expr CodeFuncExprEnd(CodeState * cs, UInt nr, BOOL pushExpr, Int endLine);
** 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);
Int AddValueToBody(CodeState * cs, Obj val);

/****************************************************************************
**
Expand Down
6 changes: 3 additions & 3 deletions src/syntaxtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -512,8 +512,8 @@ static Expr SyntaxTreeCodeFloatEager(CodeState * cs, Obj node)
Obj string = ElmRecST(EXPR_FLOAT_EAGER, node, "string");
Obj mark = ElmRecST(EXPR_FLOAT_EAGER, node, "mark");
Expr fl = NewStatOrExpr(cs, EXPR_FLOAT_EAGER, 3 * sizeof(UInt), 0);
WRITE_EXPR(fl, 0, AddValueToBody(value));
WRITE_EXPR(fl, 1, AddValueToBody(string));
WRITE_EXPR(fl, 0, AddValueToBody(cs, value));
WRITE_EXPR(fl, 1, AddValueToBody(cs, string));
WRITE_EXPR(fl, 2, (UInt)CHAR_VALUE(mark));
return fl;
}
Expand Down Expand Up @@ -586,7 +586,7 @@ static Expr SyntaxTreeCodeValue(CodeState * cs, Obj node)
UInt1 tnum = GetTypeTNum(node);
Obj value = ElmRecST(tnum, node, "value");
Expr expr = NewStatOrExpr(cs, tnum, sizeof(UInt), 0);
Int ix = AddValueToBody(value);
Int ix = AddValueToBody(cs, value);
WRITE_EXPR(expr, 0, ix);
return expr;
}
Expand Down

0 comments on commit 228b898

Please sign in to comment.