[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Making yyGLRStackItem know its type
From: |
Akim Demaille |
Subject: |
Making yyGLRStackItem know its type |
Date: |
Thu, 10 Nov 2005 16:15:28 +0100 |
User-agent: |
Gnus/5.110004 (No Gnus v0.4) Emacs/21.4 (gnu/linux) |
In the process of trying to harness glr.c into a glr.cc, I found that
the union
union yyGLRStackItem {
yyGLRState yystate;
yySemanticOption yyoption;
};
with both cases starting by a Boolean was a bit troublesome. I do
understand that this allows shorter and simpler names, simple
assignments, but it is a bit unusual (to my eyes). Could it be also
to optimize in some way to size of the union?
So I would like to move to using a more traditional
struct yyGLRStackItem {
yybool yyisState;
union {
yyGLRState yystate;
yySemanticOption yyoption;
} u;
};
but to this end, I first wanted to clean up the allocations. Ok to
install?
And what about changing yyGLRStackItem as suggested above?
Index: ChangeLog
from Akim Demaille <address@hidden>
* data/glr.c (yynewGLRStackItem, YY_RESERVE_GLRSTACK): New.
(yyaddDeferredAction, yyglrShift, yyglrShiftDefer): Use them.
(yyexpandGLRStack, YYRELOC): Define only when YYSTACKEXPANDABLE.
Index: data/glr.c
===================================================================
RCS file: /cvsroot/bison/bison/data/glr.c,v
retrieving revision 1.131
diff -u -u -r1.131 glr.c
--- data/glr.c 31 Oct 2005 01:16:32 -0000 1.131
+++ data/glr.c 10 Nov 2005 15:14:04 -0000
@@ -591,6 +591,21 @@
# endif
#endif
+#if YYSTACKEXPANDABLE
+# define YY_RESERVE_GLRSTACK(Yystack) \
+ do { \
+ if (Yystack->yyspaceLeft < YYHEADROOM) \
+ yyexpandGLRStack (Yystack); \
+ } while (/*CONSTCOND*/ 0)
+#else
+# define YY_RESERVE_GLRSTACK(Yystack) \
+ do { \
+ if (Yystack->yyspaceLeft < YYHEADROOM) \
+ yyMemoryExhausted (Yystack);
+ } while (/*CONSTCOND*/ 0)
+#endif
+
+
#if YYERROR_VERBOSE
# ifndef yystpcpy
@@ -1020,21 +1035,32 @@
/* GLRStates */
+/** Return a fresh GLRStackItem. Callers should call
+ * YY_RESERVE_GLRSTACK afterwards to make sure there is sufficient
+ * headroom. */
+
+static inline yyGLRStackItem*
+yynewGLRStackItem (yyGLRStack* yystack, yybool yyisState)
+{
+ yyGLRStackItem* yynewItem = yystack->yynextFree;
+ yystack->yyspaceLeft -= 1;
+ yystack->yynextFree += 1;
+ yynewItem->yystate.yyisState = yyisState;
+ return yynewItem;
+}
+
static void
yyaddDeferredAction (yyGLRStack* yystack, yyGLRState* yystate,
yyGLRState* rhs, yyRuleNum yyrule)
{
- yySemanticOption* yynewItem;
- yynewItem = &yystack->yynextFree->yyoption;
- yystack->yyspaceLeft -= 1;
- yystack->yynextFree += 1;
- yynewItem->yyisState = yyfalse;
- yynewItem->yystate = rhs;
- yynewItem->yyrule = yyrule;
- yynewItem->yynext = yystate->yysemantics.yyfirstVal;
- yystate->yysemantics.yyfirstVal = yynewItem;
- if (yystack->yyspaceLeft < YYHEADROOM)
- yyexpandGLRStack (yystack);
+ yySemanticOption* yynewOption =
+ &yynewGLRStackItem (yystack, yyfalse)->yyoption;
+ yynewOption->yystate = rhs;
+ yynewOption->yyrule = yyrule;
+ yynewOption->yynext = yystate->yysemantics.yyfirstVal;
+ yystate->yysemantics.yyfirstVal = yynewOption;
+
+ YY_RESERVE_GLRSTACK (yystack);
}
/* GLRStacks */
@@ -1075,7 +1101,9 @@
return yyinitStateSet (&yystack->yytops);
}
-#define YYRELOC(YYFROMITEMS,YYTOITEMS,YYX,YYTYPE) \
+
+#if YYSTACKEXPANDABLE
+# define YYRELOC(YYFROMITEMS,YYTOITEMS,YYX,YYTYPE) \
&((YYTOITEMS) - ((YYFROMITEMS) - (yyGLRStackItem*) (YYX)))->YYTYPE
/** If STACK is expandable, extend it. WARNING: Pointers into the
@@ -1086,7 +1114,6 @@
static void
yyexpandGLRStack (yyGLRStack* yystack)
{
-#if YYSTACKEXPANDABLE
yyGLRStackItem* yynewItems;
yyGLRStackItem* yyp0, *yyp1;
size_t yysize, yynewSize;
@@ -1139,11 +1166,8 @@
yystack->yyitems = yynewItems;
yystack->yynextFree = yynewItems + yysize;
yystack->yyspaceLeft = yynewSize - yysize;
-
-#else
- yyMemoryExhausted (yystack);
-#endif
}
+#endif
static void
yyfreeGLRStack (yyGLRStack* yystack)
@@ -1221,21 +1245,17 @@
size_t yyposn,
YYSTYPE yysval, YYLTYPE* yylocp)
{
- yyGLRStackItem* yynewItem;
+ yyGLRState* yynewState = &yynewGLRStackItem (yystack, yytrue)->yystate;
- yynewItem = yystack->yynextFree;
- yystack->yynextFree += 1;
- yystack->yyspaceLeft -= 1;
- yynewItem->yystate.yyisState = yytrue;
- yynewItem->yystate.yylrState = yylrState;
- yynewItem->yystate.yyposn = yyposn;
- yynewItem->yystate.yyresolved = yytrue;
- yynewItem->yystate.yypred = yystack->yytops.yystates[yyk];
- yystack->yytops.yystates[yyk] = &yynewItem->yystate;
- yynewItem->yystate.yysemantics.yysval = yysval;
- yynewItem->yystate.yyloc = *yylocp;
- if (yystack->yyspaceLeft < YYHEADROOM)
- yyexpandGLRStack (yystack);
+ yynewState->yylrState = yylrState;
+ yynewState->yyposn = yyposn;
+ yynewState->yyresolved = yytrue;
+ yynewState->yypred = yystack->yytops.yystates[yyk];
+ yynewState->yysemantics.yysval = yysval;
+ yynewState->yyloc = *yylocp;
+ yystack->yytops.yystates[yyk] = yynewState;
+
+ YY_RESERVE_GLRSTACK (yystack);
}
/** Shift stack #K of YYSTACK, to a new state corresponding to LR
@@ -1245,19 +1265,17 @@
yyglrShiftDefer (yyGLRStack* yystack, size_t yyk, yyStateNum yylrState,
size_t yyposn, yyGLRState* rhs, yyRuleNum yyrule)
{
- yyGLRStackItem* yynewItem;
+ yyGLRState* yynewState = &yynewGLRStackItem (yystack, yytrue)->yystate;
- yynewItem = yystack->yynextFree;
- yynewItem->yystate.yyisState = yytrue;
- yynewItem->yystate.yylrState = yylrState;
- yynewItem->yystate.yyposn = yyposn;
- yynewItem->yystate.yyresolved = yyfalse;
- yynewItem->yystate.yypred = yystack->yytops.yystates[yyk];
- yynewItem->yystate.yysemantics.yyfirstVal = NULL;
- yystack->yytops.yystates[yyk] = &yynewItem->yystate;
- yystack->yynextFree += 1;
- yystack->yyspaceLeft -= 1;
- yyaddDeferredAction (yystack, &yynewItem->yystate, rhs, yyrule);
+ yynewState->yylrState = yylrState;
+ yynewState->yyposn = yyposn;
+ yynewState->yyresolved = yyfalse;
+ yynewState->yypred = yystack->yytops.yystates[yyk];
+ yynewState->yysemantics.yyfirstVal = NULL;
+ yystack->yytops.yystates[yyk] = yynewState;
+
+ /* Invokes YY_RESERVE_GLRSTACK. */
+ yyaddDeferredAction (yystack, yynewState, rhs, yyrule);
}
/** Pop the symbols consumed by reduction #RULE from the top of stack
- Making yyGLRStackItem know its type,
Akim Demaille <=