[Nickle] nickle: Branch 'master' - 6 commits
Keith Packard
keithp at keithp.com
Mon Oct 31 10:23:03 PDT 2011
Makefile.am | 1
array.c | 2
box.c | 15 --
builtin-math.c | 2
builtin.c | 2
configure.in | 11 --
debian/changelog | 8 +
execute.c | 8 +
float.c | 2
gcd.c | 10 -
integer.c | 4
lex.l | 2
main.c | 4
mem.c | 4
nickle.h | 68 ++++++++----
ref.c | 21 ---
stack.h | 21 ---
value.c | 23 ----
value.h | 294 +++++++++++++++++++++++++++++++++++++++----------------
19 files changed, 289 insertions(+), 213 deletions(-)
New commits:
commit c4b149a4cca5a5c011d74502374a15900c5adfaf
Author: Keith Packard <keithp at keithp.com>
Date: Mon Oct 31 10:20:35 2011 -0700
Catch attempts to use uninitialized pointer contents
Dereferencing a pointer to uninitialized storage is an error, instead
of passing this value along to callers, catch it immediately and raise
an exception. Check for this case in the ++ and -- operators to
generate a better error message (otherwise, we'll pass Void along and
generate an error much later).
Signed-off-by: Keith Packard <keithp at keithp.com>
diff --git a/execute.c b/execute.c
index 6594704..64f2624 100644
--- a/execute.c
+++ b/execute.c
@@ -1290,12 +1290,16 @@ ThreadsRun (Value thread, Value lex)
break;
case OpPreOp:
v = Dereference (value);
+ if (aborting)
+ break;
v = ValueIncDec (v, inst->binop.op);
ThreadAssign (value, v, False);
value = v;
break;
case OpPostOp:
v = Dereference (value);
+ if (aborting)
+ break;
ThreadAssign (value, ValueIncDec (v, inst->binop.op), False);
value = v;
break;
diff --git a/nickle.h b/nickle.h
index 81a7155..3ee5600 100644
--- a/nickle.h
+++ b/nickle.h
@@ -850,7 +850,12 @@ static inline Value Dereference (Value v) {
v);
return Void;
}
- return REFERENCE (RefValueGet (v));
+ v = RefValueGet(v);
+ if (!v) {
+ RaiseStandardException (exception_uninitialized_value, 0);
+ return (Void);
+ }
+ return REFERENCE (v);
}
/* vararg builtins */
commit 7a7a8a063796ab581af5179ce211f4cd5b602879
Author: Keith Packard <keithp at keithp.com>
Date: Thu Sep 29 01:49:42 2011 -0700
Exit after two consecutive interrupts
If the first interrupt isn't received by the nickle code,
when the second one comes in, just exit
Signed-off-by: Keith Packard <keithp at keithp.com>
diff --git a/configure.in b/configure.in
index 6f58071..a2941f0 100644
--- a/configure.in
+++ b/configure.in
@@ -7,7 +7,7 @@ dnl for licensing information.
AC_PREREQ(2.59)
AC_INIT([nickle],
- 2.70,
+ 2.71,
[http://nickle.org],
nickle)
diff --git a/debian/changelog b/debian/changelog
index 3914abb..f9de370 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,11 @@
+nickle (2.71-1) unstable; urgency=low
+
+ * Update old-school variable length struct allocation to ansi-C
+ * Replace most parameterized macros in nickle.h and value.h with
+ static inline functions.
+
+ -- Keith Packard <keithp at keithp.com> Thu, 29 Sep 2011 01:19:50 -0700
+
nickle (2.70-1) unstable; urgency=low
* Make parse_csv_t type public
diff --git a/main.c b/main.c
index 2456c13..b6ca4f1 100644
--- a/main.c
+++ b/main.c
@@ -158,6 +158,10 @@ RETSIGTYPE
intr (int sig)
{
resetSignal (SIGINT, intr);
+ if (signalInterrupt) {
+ write(2,"Double interrupt, exiting\n", 26);
+ exit(1);
+ }
SetSignalInterrupt ();
}
commit cc6c2f55d76e2d1a07ded25bf09e313d5e6de255
Author: Keith Packard <keithp at keithp.com>
Date: Thu Sep 29 01:15:39 2011 -0700
Cleanup struct type changes
diff --git a/value.h b/value.h
index 6998ee8..28119b6 100644
--- a/value.h
+++ b/value.h
@@ -1015,7 +1015,7 @@ BoxPtr BoxRewrite (BoxPtr box, int *ep);
typedef struct {
DataType *data;
int size;
- char *values[0];
+ char values[0];
} DataCache, *DataCachePtr;
DataCachePtr NewDataCache (int size);
commit 0a8a685e99043f5e983d7b46a0db155e91cc2af4
Author: Keith Packard <keithp at keithp.com>
Date: Thu Sep 29 00:43:08 2011 -0700
Replace most parameterized macros with static inline functions
Typechecking, decent compiler warnings and smaller code.
Signed-off-by: Keith Packard <keithp at keithp.com>
diff --git a/box.c b/box.c
index 3d7d909..549d97f 100644
--- a/box.c
+++ b/box.c
@@ -64,19 +64,6 @@ NewTypedBox (Bool array, BoxTypesPtr bt)
RETURN (box);
}
-#ifndef HAVE_C_INLINE
-Value
-BoxValue (BoxPtr box, int e)
-{
- if (!BoxElements(box)[e].value)
- {
- RaiseStandardException (exception_uninitialized_value, 0);
- return (Void);
- }
- return (BoxElements(box)[e].value);
-}
-#endif
-
static void
MarkBoxReplace (void *object)
{
@@ -194,7 +181,7 @@ AddBoxType (BoxTypesPtr *btp, Type *t)
bt = new;
}
position = bt->count++;
- BoxTypesValue(bt,position) = t;
+ BoxTypesValueSet(bt,position,t);
EXIT ();
return position;
}
diff --git a/configure.in b/configure.in
index 65063b4..6f58071 100644
--- a/configure.in
+++ b/configure.in
@@ -51,7 +51,7 @@ AC_DEFUN([AS_COMPILER_FLAG],
save_CFLAGS="$CFLAGS"
CFLAGS="$CFLAGS $1"
- AC_LINK_IFELSE(AC_LANG_PROGRAM(,), [flag_ok=yes], [flag_ok=no])
+ AC_LINK_IFELSE([AC_LANG_PROGRAM([],[return 0])], [flag_ok=yes], [flag_ok=no])
CFLAGS="$save_CFLAGS"
if test "X$flag_ok" = Xyes; then
@@ -79,13 +79,6 @@ AC_CHECK_HEADERS(fcntl.h strings.h time.h sys/time.h unistd.h sys/resource.h)
AC_CHECK_HEADERS(stropts.h)
AC_CHECK_HEADERS(dlfcn.h)
-dnl Checks for typedefs, structures, and compiler characteristics.
-AC_C_INLINE
-case "$ac_cv_c_inline" in
- no) ;;
- *) AC_DEFINE([HAVE_C_INLINE], 1, [C compiler has some form of inline.]) ;;
-esac
-
dnl Checks for precise integer types
AC_CHECK_HEADERS(stdint.h)
case "$ac_cv_header_stdint_h" in
diff --git a/execute.c b/execute.c
index ce44c76..6594704 100644
--- a/execute.c
+++ b/execute.c
@@ -270,7 +270,7 @@ ThreadStaticInit (Value thread, InstPtr *next)
EXIT ();
}
-static INLINE void
+static inline void
ThreadAssign (Value ref, Value v, Bool initialize)
{
ENTER ();
diff --git a/mem.c b/mem.c
index 984d884..ef3f13a 100644
--- a/mem.c
+++ b/mem.c
@@ -338,11 +338,7 @@ mark (void)
MemReference (TemporaryData);
}
-#if HAVE_C_INLINE
static inline int
-#else
-static int
-#endif
busy (unsigned char *data)
{
DataType *type;
diff --git a/nickle.h b/nickle.h
index c82de40..81a7155 100644
--- a/nickle.h
+++ b/nickle.h
@@ -76,6 +76,15 @@ extern SymbolPtr NewSymbolStatic (Atom name, Type *Rep);
extern SymbolPtr NewSymbolAuto (Atom name, Type *type);
extern SymbolPtr NewSymbolNamespace (Atom name, NamespacePtr namespace);
+static inline Type *_TypeNameType(TypePtr t) {
+ if (t->name.name)
+ return t->name.name->symbol.type;
+ else
+ return NULL;
+}
+
+#define TypeNameType(t) _TypeNameType(t)
+
typedef struct _namelist *NamelistPtr;
typedef struct _namelist {
@@ -183,7 +192,14 @@ TwixtPtr
NewTwixt (ContinuationPtr continuation, InstPtr enter, InstPtr leave);
InstPtr TwixtJump (Value thread, TwixtPtr twixt, Bool enter);
-#define TwixtDepth(t) ((t) ? (t)->depth : 0)
+
+static inline int TwixtDepth(Twixt *t) {
+ if (t)
+ return t->depth;
+ else
+ return 0;
+}
+
TwixtPtr TwixtNext (TwixtPtr twixt, TwixtPtr last);
# define NOTHING 0
@@ -577,12 +593,21 @@ typedef struct _obj {
double_digit sub_ticks;
Bool error;
NonLocal *nonLocal;
+ Inst insts[0];
} Obj;
-#define ObjCode(obj,i) (((InstPtr) (obj + 1)) + (i))
-#define ObjLast(obj) ((obj)->used - 1)
+static inline InstPtr ObjCode(Obj *obj, int i) {
+ return &(obj->insts[i]);
+}
-#define ObjStat(obj,i) (((StatPtr) ObjCode(obj,(obj)->size)) + (i))
+static inline int ObjLast(Obj *obj) {
+ return obj->used - 1;
+}
+
+static inline StatPtr ObjStat(Obj *obj, int i) {
+ StatPtr stats = (StatPtr) ObjCode(obj,obj->size);
+ return &stats[i];
+}
ObjPtr CompileStat (ExprPtr expr, CodePtr code);
ObjPtr CompileExpr (ExprPtr expr, CodePtr code);
@@ -788,7 +813,6 @@ CheckStandardException (void);
Value
JumpStandardException (Value thread, InstPtr *next);
-#ifdef HAVE_C_INLINE
static inline Value
IntIncDec (Value av, BinaryOp operator)
{
@@ -802,15 +826,15 @@ IntIncDec (Value av, BinaryOp operator)
return NewIntInteger (a);
return NewInt (a);
}
-#define ValueIncDec(v,o) (ValueIsInt(v) ? IntIncDec (v, o) : BinaryOperate (v, One, o))
-#else
-#define ValueIncDec(v,o) (BinaryOperate (v, One, o))
-#endif
-#ifdef HAVE_C_INLINE
-static inline Value
-BoxValue (BoxPtr box, int e)
-{
+static inline Value ValueIncDec(Value v, BinaryOp o) {
+ if (ValueIsInt(v))
+ return IntIncDec(v,o);
+ else
+ return BinaryOperate(v, One, o);
+}
+
+static inline Value BoxValue (BoxPtr box, int e) {
Value v = BoxElements(box)[e];
if (!v)
{
@@ -820,21 +844,14 @@ BoxValue (BoxPtr box, int e)
return v;
}
-static inline Value
-Dereference (Value v)
-{
- if (!ValueIsRef(v))
- {
+static inline Value Dereference (Value v) {
+ if (!ValueIsRef(v)) {
RaiseStandardException (exception_invalid_unop_value, 1,
v);
return Void;
}
return REFERENCE (RefValueGet (v));
}
-#else
-extern Value BoxValue (BoxPtr box, int e);
-extern Value Dereference (Value);
-#endif
/* vararg builtins */
Value do_printf (int, Value *);
diff --git a/ref.c b/ref.c
index 3a02ddc..ac430e3 100644
--- a/ref.c
+++ b/ref.c
@@ -195,24 +195,6 @@ NewRefReal (BoxPtr box, int element, Value *re)
RETURN (ret);
}
-#ifndef HAVE_C_INLINE
-
-Value
-NewRef (BoxPtr box, int element)
-{
- int c = ((unsigned) (&BoxElements(box)[element])) % REF_CACHE_SIZE;
- Value *re = (Value *) &DataCacheValues(refCache)[c];
- Value ret = *re;
-
- if (ret && ret->ref.box == box && ret->ref.element == element)
- {
- REFERENCE(ret);
- return ret;
- }
- return NewRefReal (box, element, re);
-}
-#endif
-
void
RefRewrite (Value rv)
{
diff --git a/stack.h b/stack.h
index b261dd5..f5d6e26 100644
--- a/stack.h
+++ b/stack.h
@@ -78,7 +78,6 @@ panic (char *, ...);
StackPush ((s), (o)) : (*--STACK_TOP(s) = (o)))
#endif
-#ifdef HAVE_C_INLINE
static inline StackElement
StackPushInline(StackObject *s, StackElement o)
{
@@ -99,25 +98,5 @@ StackReturnInline(StackObject *s, StackPointer sp, StackElement o)
return *--STACK_TOP(s) = o;
}
#define STACK_RETURN(s,sp,o) StackReturnInline(s,sp,o)
-#endif
-
-#ifndef STACK_PUSH
-#define STACK_PUSH(s,o) StackPush(s,o)
-#endif
-#ifndef STACK_POP
-#define STACK_POP(s) StackPop(s)
-#endif
-#ifndef STACK_DROP
-#define STACK_DROP(s,i) StackDrop(s,i)
-#endif
-#ifndef STACK_RESET
-#define STACK_RESET(s,sp) StackReset(s,sp)
-#endif
-#ifndef STACK_ELT
-#define STACK_ELT(s,i) StackElt(s,i)
-#endif
-#ifndef STACK_RETURN
-#define STACK_RETURN(s,sp,o) StackReturn(s,sp,o)
-#endif
#endif /* _STACK_H_ */
diff --git a/value.c b/value.c
index dc7c50b..7add3cd 100644
--- a/value.c
+++ b/value.c
@@ -763,16 +763,6 @@ CopyMutable (Value v)
RETURN (nv);
}
-#ifndef HAVE_C_INLINE
-Value
-Copy (Value v)
-{
- if (v && Mutablep (ValueTag(v)))
- return CopyMutable (v);
- return v;
-}
-#endif
-
Value
ValueEqual (Value a, Value b, int expandOk)
{
@@ -792,19 +782,6 @@ ValueHash (Value v)
return NewInt ((*rep->hash) (v) & MAX_NICKLE_INT);
}
-#ifndef HAVE_C_INLINE
-Value
-Dereference (Value v)
-{
- if (!ValueIsRef(v))
- {
- RaiseStandardException (exception_invalid_unop_value, 1, v);
- return Void;
- }
- return REFERENCE (RefValue (v));
-}
-#endif
-
static Value
UnitEqual (Value av, Value bv, int expandOk)
{
diff --git a/value.h b/value.h
index 3415198..6998ee8 100644
--- a/value.h
+++ b/value.h
@@ -241,26 +241,18 @@ extern Natural *zero_natural;
extern Natural *one_natural;
extern Natural *two_natural;
-typedef enum _sign { Positive, Negative } Sign;
+typedef enum _sign { Positive = 0, Negative = 1 } Sign;
-#define SignNegate(sign) ((sign) == Positive ? Negative : Positive)
+static inline Sign SignNegate(Sign sign) {
+ return 1 - sign;
+}
typedef enum _signcat {
- BothPositive, FirstPositive, SecondPositive, BothNegative
+ BothPositive = 0, FirstPositive = 1, SecondPositive = 2, BothNegative = 3
} Signcat;
static inline Signcat catagorize_signs(Sign s1, Sign s2) {
- if (s1 == Positive) {
- if (s2 == Positive)
- return BothPositive;
- else
- return FirstPositive;
- } else {
- if (s2 == Positive)
- return SecondPositive;
- else
- return BothNegative;
- }
+ return (s1 << 1) | s2;
}
typedef enum _binaryOp {
@@ -322,8 +314,13 @@ extern ValueRep FuncRep, ThreadRep;
extern ValueRep SemaphoreRep, ContinuationRep, UnitRep, BoolRep;
extern ValueRep ForeignRep;
-#define NewInt(i) ((Value) IntToPtr ((((i) << 1) | 1)))
-#define IntSign(i) ((i) < 0 ? Negative : Positive)
+static inline Value NewInt(int i) {
+ return (Value) IntToPtr ((((i) << 1) | 1));
+}
+
+static inline Sign IntSign(int i) {
+ return (i) < 0 ? Negative : Positive;
+}
/*
* Use all but one bit to hold immediate integer values
@@ -354,9 +351,17 @@ extern ValueRep ForeignRep;
#define One NewInt(1)
#define Zero NewInt(0)
-#define ValueIsPtr(v) ((PtrToInt(v) & 1) == 0)
-#define ValueIsInt(v) (!ValueIsPtr(v))
-#define ValueInt(v) (PtrToInt (v) >> 1)
+static inline Bool ValueIsPtr (Value v) {
+ return (PtrToInt(v) & 1) == 0;
+}
+
+static inline Bool ValueIsInt (Value v) {
+ return !ValueIsPtr(v);
+}
+
+static inline int ValueInt(Value v) {
+ return PtrToInt (v) >> 1;
+}
static inline ValueRep *_ValueRep(Value v);
@@ -415,8 +420,6 @@ typedef struct _typeName {
SymbolPtr name;
} TypeName;
-#define TypeNameType(t) ((t)->name.name ? (t)->name.name->symbol.type : 0)
-
typedef struct _typeRef {
TypeBase base;
TypePtr ref;
@@ -519,8 +522,6 @@ Bool TypeTypesMember (Type *list, Type *type);
int TypeInit (void);
SymbolPtr TypeNameName (Type *t);
-#define TypeUnionElements(t) ((t)->unions.types)
-
Type *TypeCombineBinary (Type *left, int tag, Type *right);
Type *TypeCombineUnary (Type *down, int tag);
Type *TypeCombineStruct (Type *type, int tag, Atom atom);
@@ -573,14 +574,6 @@ typedef struct _integer {
Natural *magn;
} Integer;
-static inline Natural *IntegerMag(Value i) {
- return (Natural *) ((long) (i->integer.magn) & ~1);
-}
-
-static inline Sign IntgerSign(Value i) {
- return (Sign) ((long) (i->integer.magn) & 1);
-}
-
typedef struct _rational {
BaseValue base;
Sign sign;
@@ -705,7 +698,13 @@ static inline TypePtr *BoxTypesElements(BoxTypes *bt) {
return bt->elements;
}
-#define BoxTypesValue(bt,e) (BoxTypesElements(bt)[e])
+static inline TypePtr BoxTypesValue(BoxTypes *bt, int e) {
+ return BoxTypesElements(bt)[e];
+}
+
+static inline void BoxTypesValueSet(BoxTypes *bt, int e, TypePtr t) {
+ BoxTypesElements(bt)[e] = t;
+}
extern BoxTypesPtr NewBoxTypes (int size);
extern int AddBoxType (BoxTypesPtr *btp, TypePtr t);
@@ -939,6 +938,14 @@ static inline Rep ValueTag(Value v) {
return ValueRep(v)->tag;
}
+static inline Natural *IntegerMag(Value i) {
+ return (Natural *) ((long) (i->integer.magn) & ~1);
+}
+
+static inline Sign IntegerSign(Value i) {
+ return (Sign) ((long) (i->integer.magn) & 1);
+}
+
typedef struct _boxReplace {
DataType *data;
BoxPtr new;
@@ -1014,7 +1021,7 @@ typedef struct {
DataCachePtr NewDataCache (int size);
static inline void *DataCacheValues(DataCache *vc) {
- return (void *) vc->values
+ return (void *) vc->values;
}
static inline int *ArrayDims (Array *a)
@@ -1136,8 +1143,6 @@ int StringCharSize (unsigned c);
unsigned StringGet (char *src, long len, int i);
char *StrzPart (Value, char *error);
-#ifdef HAVE_C_INLINE
-
static inline Value
NewRef (BoxPtr box, int element)
{
@@ -1152,9 +1157,7 @@ NewRef (BoxPtr box, int element)
}
return NewRefReal (box, element, re);
}
-#else
-Value NewRef (BoxPtr box, int element);
-#endif
+
Value NewStruct (StructType *type, Bool constant);
StructType *NewStructType (int nelements);
Type *BuildStructType (int nelements, ...);
@@ -1333,22 +1336,10 @@ int IntPart (Value, char *error);
double DoublePart (Value av, char *error);
-#ifndef Numericp
-Bool Numericp (Rep);
-#endif
-#ifndef Integralp
-Bool Integralp (Rep);
-#endif
Bool Zerop (Value);
Bool Negativep (Value);
Bool Evenp (Value);
-#ifdef HAVE_C_INLINE
-#define INLINE inline
-#else
-#define INLINE
-#endif
-
int ArrayInit (void);
int AtomInit (void);
int FileInit (void);
@@ -1364,8 +1355,13 @@ int RefInit (void);
int ForeignInit (void);
int ValueInit (void);
-# define oneNp(n) ((n)->length == 1 && NaturalDigits(n)[0] == 1)
-# define zeroNp(n) ((n)->length == 0)
+static inline Bool oneNp (Natural *n) {
+ return n->length == 1 && NaturalDigits(n)[0] == 1;
+}
+
+static inline Bool zeroNp (Natural *n) {
+ return n->length == 0;
+}
void ferr(int);
void ignore_ferr (void);
commit 0640100035a54205ab8b7117aa82291de19a03e7
Author: Keith Packard <keithp at keithp.com>
Date: Wed Sep 28 23:40:12 2011 -0700
Replace macros with static inline functions in value.h
Actual type checking, and smaller compiler output to boot.
Signed-off-by: Keith Packard <keithp at keithp.com>
diff --git a/Makefile.am b/Makefile.am
index a66d6b6..9a93ba1 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -69,6 +69,7 @@ AM_CPPFLAGS = \
-DNICKLELIBDIR=\"@nicklelibdir@\"
AM_CFLAGS = \
+ -D_FORTIFY_SOURCE=2 -O2 \
-Wall -Wpointer-arith -Wstrict-prototypes \
-Wmissing-prototypes -Wmissing-declarations \
-Wnested-externs -fno-strict-aliasing -fwrapv
diff --git a/array.c b/array.c
index 29fa704..2ae4959 100644
--- a/array.c
+++ b/array.c
@@ -152,7 +152,7 @@ ArrayPrint (Value f, Value av, char format, int base, int width, int prec, int f
for (i = 0; i < a->ndim; i++)
FileOutput (f, '}');
EXIT ();
- return True;
+ return ret;
}
#define hrot(i) (((i) << 1) | ((i) >> (sizeof (HashValue) * 8 - 1)))
diff --git a/builtin-math.c b/builtin-math.c
index 6b2f61c..6d3178f 100644
--- a/builtin-math.c
+++ b/builtin-math.c
@@ -66,7 +66,7 @@ do_Math_assignpow (Value a, Value b)
ENTER ();
Value ret;
- ret = Pow (RefValue (a), b);
+ ret = Pow (RefValueGet(a), b);
RefValueSet (a, ret);
RETURN (ret);
}
diff --git a/builtin.c b/builtin.c
index 74d232b..b0cfc11 100644
--- a/builtin.c
+++ b/builtin.c
@@ -277,7 +277,7 @@ BuiltinAddException (NamespacePtr *namespacep,
args = BuiltinArgType (format, &argc);
type = NewTypeFunc (typePoly, args);
sym = BuiltinException (namespacep, name,
- NewTypeFunc (typePoly, args),
+ type,
doc);
RegisterStandardException (exception, sym);
EXIT ();
diff --git a/execute.c b/execute.c
index 64e4ce0..ce44c76 100644
--- a/execute.c
+++ b/execute.c
@@ -1244,7 +1244,7 @@ ThreadsRun (Value thread, Value lex)
value);
break;
}
- value = RefValue (value);
+ value = RefValueGet(value);
/* fall through ... */
case OpDot:
case OpDotRef:
diff --git a/float.c b/float.c
index 7dd3eea..3f2d2f4 100644
--- a/float.c
+++ b/float.c
@@ -653,7 +653,6 @@ FloatExp (Value exp2, Value *ratio, int ibase, unsigned prec)
Value r;
Value min, max, mean, nmean;
Value pow2;
- Value base;
Value base_f;
Value two;
Value two_f;
@@ -662,7 +661,6 @@ FloatExp (Value exp2, Value *ratio, int ibase, unsigned prec)
DebugV ("exp2", exp2);
two = NewInt (2);
two_f = NewIntFloat (2, prec + 32);
- base = NewInt (ibase);
base_f = NewIntFloat (ibase, prec + 32);
/*
* Compute expbase, this is a bit tricky as log is only
diff --git a/gcd.c b/gcd.c
index 0ae0768..2eaff30 100644
--- a/gcd.c
+++ b/gcd.c
@@ -340,7 +340,7 @@ NaturalBdivmodStepInplace (Natural *u, Natural *v, digit b, Bool shift)
*/
while (--rd >= NaturalDigits (u) && *rd == 0)
;
- NaturalLength (u) = ((rd + 1) - NaturalDigits (u));
+ u->length = ((rd + 1) - NaturalDigits (u));
}
static void
@@ -796,7 +796,7 @@ NaturalKaryReductionInplace (Natural *u, Natural *v)
i = rd - NaturalDigits (u);
while (i > 0 && *--rd == 0)
i--;
- NaturalLength (u) = i;
+ u->length = i;
#ifdef DEBUG_KARY
FilePrintf (FileStdout, "Reduction result %n\n", u);
#endif
@@ -858,7 +858,7 @@ NaturalGcdNormalize (Natural *u, int shift)
}
while (*--rt == 0)
;
- NaturalLength (r) = rt - NaturalDigits (r) + 1;
+ r->length = rt - NaturalDigits (r) + 1;
RETURN (r);
}
@@ -963,10 +963,10 @@ NaturalGcd (Natural *u0, Natural *v0)
if (NaturalLength (u) != 2)
u = v;
NaturalDigits(u)[1] = (digit) td;
- NaturalLength (u) = 2;
+ u->length = 2;
}
else
- NaturalLength (u) = 1;
+ u->length = 1;
NaturalDigits(u)[0] = (digit) ud;
FINISH ("gcd2");
}
diff --git a/integer.c b/integer.c
index 5bd6ba3..affaf0d 100644
--- a/integer.c
+++ b/integer.c
@@ -154,7 +154,7 @@ IntegerMod (Value av, Value bv, int expandOk)
{
ENTER ();
Integer *a = &av->integer, *b = &bv->integer;
- Natural *quo, *rem;
+ Natural *rem;
if (NaturalZero (IMag(b)))
{
@@ -162,7 +162,7 @@ IntegerMod (Value av, Value bv, int expandOk)
av, bv);
RETURN (Void);
}
- quo = NaturalDivide (IMag(a), IMag(b), &rem);
+ (void) NaturalDivide (IMag(a), IMag(b), &rem);
if (ISign(a) == Negative && !NaturalZero (rem))
rem = NaturalMinus (IMag(b), rem);
RETURN (NewInteger (Positive, rem));
diff --git a/lex.l b/lex.l
index 8c350dd..2f51c80 100644
--- a/lex.l
+++ b/lex.l
@@ -209,9 +209,7 @@ LexGetInteractiveChar (void)
if (!line)
{
char *p;
- Value v;
- v = prompt ();
p = StrzPart (prompt (), "invalid prompt");
if (!p)
p = "??? ";
diff --git a/nickle.h b/nickle.h
index 25c3ade..c82de40 100644
--- a/nickle.h
+++ b/nickle.h
@@ -829,7 +829,7 @@ Dereference (Value v)
v);
return Void;
}
- return REFERENCE (RefValue (v));
+ return REFERENCE (RefValueGet (v));
}
#else
extern Value BoxValue (BoxPtr box, int e);
diff --git a/ref.c b/ref.c
index 284f1cd..3a02ddc 100644
--- a/ref.c
+++ b/ref.c
@@ -213,7 +213,7 @@ NewRef (BoxPtr box, int element)
}
#endif
-int
+void
RefRewrite (Value rv)
{
Ref *ref= &rv->ref;
@@ -221,7 +221,6 @@ RefRewrite (Value rv)
if (box->replace)
ref->box = BoxRewrite (box, &ref->element);
- return 0;
}
int
diff --git a/value.h b/value.h
index 83596ff..3415198 100644
--- a/value.h
+++ b/value.h
@@ -185,8 +185,15 @@ typedef struct _natural {
digit digits[0];
} Natural;
-#define NaturalLength(n) ((n)->length)
-#define NaturalDigits(n) ((n)->digits)
+static inline int NaturalLength(Natural *n)
+{
+ return n->length;
+}
+
+static inline digit * NaturalDigits(Natural *n)
+{
+ return n->digits;
+}
Natural *NewNatural (unsigned value);
Natural *NewDoubleDigitNatural (double_digit dd);
@@ -242,11 +249,19 @@ typedef enum _signcat {
BothPositive, FirstPositive, SecondPositive, BothNegative
} Signcat;
-# define catagorize_signs(s1,s2)\
- ((s1) == Positive ? \
- ((s2) == Positive ? BothPositive : FirstPositive) \
- : \
- ((s2) == Positive ? SecondPositive : BothNegative))
+static inline Signcat catagorize_signs(Sign s1, Sign s2) {
+ if (s1 == Positive) {
+ if (s2 == Positive)
+ return BothPositive;
+ else
+ return FirstPositive;
+ } else {
+ if (s2 == Positive)
+ return SecondPositive;
+ else
+ return BothNegative;
+ }
+}
typedef enum _binaryOp {
PlusOp, MinusOp, TimesOp, DivideOp, DivOp, ModOp,
@@ -296,6 +311,8 @@ typedef enum _rep {
/* because rep_undef is -1, using (unsigned) makes these a single compare */
#define Numericp(t) ((unsigned) (t) <= (unsigned) rep_float)
#define Integralp(t) ((unsigned) (t) <= (unsigned) rep_integer)
+
+
#define Mutablep(t) ((t) >= rep_array)
extern ValueRep IntRep, IntegerRep, RationalRep, FloatRep;
@@ -341,7 +358,9 @@ extern ValueRep ForeignRep;
#define ValueIsInt(v) (!ValueIsPtr(v))
#define ValueInt(v) (PtrToInt (v) >> 1)
-#define ValueRep(v) (ValueIsInt(v) ? &IntRep : (v)->value.type)
+static inline ValueRep *_ValueRep(Value v);
+
+#define ValueRep(v) _ValueRep(v)
#define ValueIsInteger(v) (ValueRep(v) == &IntegerRep)
#define ValueIsRational(v) (ValueRep(v) == &RationalRep)
#define ValueIsFloat(v) (ValueRep(v) == &FloatRep)
@@ -543,7 +562,7 @@ typedef enum _publish {
publish_private, publish_protected, publish_public, publish_extend
} Publish;
-#define ValueTag(v) (ValueRep(v)->tag)
+static inline Rep ValueTag(Value v);
typedef struct _baseValue {
ValueRep *type;
@@ -554,8 +573,13 @@ typedef struct _integer {
Natural *magn;
} Integer;
-#define IntegerMag(i) ((Natural *) ((long) ((i)->integer.magn) & ~1))
-#define IntegerSign(i) ((Sign) ((long) ((i)->integer.magn) & 1))
+static inline Natural *IntegerMag(Value i) {
+ return (Natural *) ((long) (i->integer.magn) & ~1);
+}
+
+static inline Sign IntgerSign(Value i) {
+ return (Sign) ((long) (i->integer.magn) & 1);
+}
typedef struct _rational {
BaseValue base;
@@ -583,7 +607,11 @@ typedef struct _string {
char chars[0];
} String;
-#define StringChars(s) ((s)->chars)
+static inline char *
+StringChars (String *s)
+{
+ return s->chars;
+}
typedef struct _foreign {
BaseValue base;
@@ -606,7 +634,10 @@ typedef struct _boxVector {
BoxPtr boxes[0];
} BoxVector, *BoxVectorPtr;
-#define BoxVectorBoxes(v) ((v)->boxes)
+static inline BoxPtr *BoxVectorBoxes(BoxVector *v)
+{
+ return (BoxPtr *) v->boxes;
+}
typedef struct _array {
BaseValue base;
@@ -619,17 +650,6 @@ typedef struct _array {
int dims[0];
} Array;
-#define ArrayDims(a) ((a)->dims)
-#define ArrayLimits(a) (ArrayDims(a) + (a)->ndim)
-#define ArrayConstant
-#define ArrayNvalues(a) ((a)->resizable ? (a)->u.resize->nvalues : (a)->u.fix->nvalues)
-#define ArrayValueBox(a,i) ((a)->resizable ? BoxVectorBoxes((a)->u.resize)[i] : (a)->u.fix)
-#define ArrayValueElt(a,i) ((a)->resizable ? 0 : (i))
-#define ArrayType(a) ((a)->resizable ? (a)->u.resize->type : (a)->u.fix->u.type)
-#define ArrayValue(a,i) (BoxValue(ArrayValueBox(a,i),ArrayValueElt(a,i)))
-#define ArrayValueGet(a,i) (BoxValueGet(ArrayValueBox(a,i),ArrayValueElt(a,i)))
-#define ArrayValueSet(a,i,v) (BoxValueSet(ArrayValueBox(a,i),ArrayValueElt(a,i), v))
-
typedef struct _io_chain {
struct _io_chain *next;
int size;
@@ -681,7 +701,10 @@ typedef struct _boxTypes {
TypePtr elements[0];
} BoxTypes, *BoxTypesPtr;
-#define BoxTypesElements(bt) ((bt)->elements)
+static inline TypePtr *BoxTypesElements(BoxTypes *bt) {
+ return bt->elements;
+}
+
#define BoxTypesValue(bt,e) (BoxTypesElements(bt)[e])
extern BoxTypesPtr NewBoxTypes (int size);
@@ -693,15 +716,6 @@ typedef struct _ref {
int element;
} Ref;
-int RefRewrite (Value r);
-
-#define RefCheck(r) ((r)->ref.box->replace ? RefRewrite(r) : 0)
-#define RefValueSet(r,v) (RefCheck (r), BoxValueSet((r)->ref.box, (r)->ref.element, (v)))
-#define RefValue(r) (RefCheck (r), BoxValue((r)->ref.box, (r)->ref.element))
-#define RefValueGet(r) (RefCheck (r), BoxValueGet((r)->ref.box, (r)->ref.element))
-#define RefType(r) (RefCheck (r), BoxType((r)->ref.box, (r)->ref.element))
-#define RefConstant(r) BoxConstant((r)->ref.box, (r)->ref.element)
-
typedef struct _structType {
DataType *data;
int nelements;
@@ -915,6 +929,16 @@ struct _valueType {
Hash hash;
};
+static inline ValueRep *_ValueRep(Value v) {
+ if (ValueIsInt(v))
+ return &IntRep;
+ return v->value.type;
+}
+
+static inline Rep ValueTag(Value v) {
+ return ValueRep(v)->tag;
+}
+
typedef struct _boxReplace {
DataType *data;
BoxPtr new;
@@ -937,18 +961,45 @@ typedef struct _box {
} Box;
#if 1
-#define BoxCheck(box) assert (!(box)->replace),
+#define BoxCheck(box) assert (!(box)->replace)
#else
#define BoxCheck(box)
#endif
-#define BoxElements(box) (BoxCheck (box) ((box)->values))
-#define BoxValueSet(box,e,v) ((BoxElements(box)[e]) = (v))
-#define BoxValueGet(box,e) ((BoxElements(box)[e]))
-#define BoxConstant(box,e) ((box)->constant)
-#define BoxReplace(box) ((box)->replace)
-#define BoxType(box,e) (BoxCheck (box) ((box)->homogeneous ? (box)->u.type : \
- BoxTypesValue((box)->u.types, e)))
-
+
+static inline Value *BoxElements(Box *box)
+{
+ BoxCheck(box);
+ return box->values;
+}
+
+static inline Value BoxValueSet(Box *box, long e, Value v) {
+ return BoxElements(box)[e] = v;
+}
+
+static inline Value BoxValueGet(Box *box, long e) {
+ return BoxElements(box)[e];
+}
+
+static inline Bool BoxConstant(Box *box, int e) {
+ return box->constant;
+}
+
+static inline Bool _BoxReplace(Box *box) {
+ return box->replace;
+}
+
+#define BoxReplace(box) _BoxReplace(box)
+
+static inline TypePtr _BoxType(Box *box, long e) {
+ BoxCheck(box);
+ if (box->homogeneous)
+ return box->u.type;
+ else
+ return BoxTypesValue(box->u.types, e);
+}
+
+#define BoxType(box, e) _BoxType(box, e)
+
extern BoxPtr NewBox (Bool constant, Bool array, int nvalues, TypePtr type);
extern BoxPtr NewTypedBox (Bool array, BoxTypesPtr types);
void BoxSetReplace (BoxPtr old, BoxPtr new, int oldstride, int newstride);
@@ -962,7 +1013,80 @@ typedef struct {
DataCachePtr NewDataCache (int size);
-#define DataCacheValues(vc) ((void *) ((vc)->values))
+static inline void *DataCacheValues(DataCache *vc) {
+ return (void *) vc->values
+}
+
+static inline int *ArrayDims (Array *a)
+{
+ return a->dims;
+}
+
+#define ArrayLimits(a) (ArrayDims(a) + (a)->ndim)
+#define ArrayConstant
+
+static inline long ArrayNvalues(Array *a) {
+ if (a->resizable)
+ return a->u.resize->nvalues;
+ else
+ return a->u.fix->nvalues;
+}
+
+static inline BoxPtr ArrayValueBox(Array *a, long i) {
+ if (a->resizable)
+ return BoxVectorBoxes(a->u.resize)[i];
+ else
+ return a->u.fix;
+}
+
+static inline int ArrayValueElt(Array *a, long i) {
+ if (a->resizable)
+ return 0;
+ else
+ return i;
+}
+
+static inline TypePtr ArrayType(Array *a) {
+ if (a->resizable)
+ return a->u.resize->type;
+ else
+ return a->u.fix->u.type;
+}
+
+#define ArrayValue(a,i) (BoxValue(ArrayValueBox(a,i),ArrayValueElt(a,i)))
+#define ArrayValueGet(a,i) (BoxValueGet(ArrayValueBox(a,i),ArrayValueElt(a,i)))
+
+static inline void ArrayValueSet(Array *a, long i, Value v)
+{
+ BoxValueSet(ArrayValueBox(a,i),ArrayValueElt(a,i), v);
+}
+
+void
+RefRewrite (Value r);
+
+static inline void RefCheck(Value r) {
+ if (BoxReplace(r->ref.box))
+ RefRewrite(r);
+}
+
+static inline void RefValueSet(Value r, Value v) {
+ RefCheck(r);
+ BoxValueSet(r->ref.box, r->ref.element, v);
+}
+
+static inline Value RefValueGet(Value r) {
+ RefCheck(r);
+ return BoxValueGet(r->ref.box, r->ref.element);
+}
+
+static inline TypePtr RefType (Value r) {
+ RefCheck(r);
+ return BoxType(r->ref.box, r->ref.element);
+}
+
+static inline Bool RefConstant(Value r) {
+ return BoxConstant(r->ref.box, r->ref.element);
+}
Value NewInteger (Sign sign, Natural *mag);
Value NewIntInteger (int value);
@@ -1140,7 +1264,7 @@ Bool Print (Value, Value, char format, int base, int width, int prec, int fill);
void PrintError (char *s, ...);
HashValue HashCrc32 (unsigned char *bytes, int nbytes);
Value CopyMutable (Value v);
-#ifdef HAVE_C_INLINE
+
static inline Value
Copy (Value v)
{
@@ -1148,9 +1272,7 @@ Copy (Value v)
return CopyMutable (v);
return v;
}
-#else
-Value Copy (Value);
-#endif
+
Value ValueEqual (Value a, Value b, int expandOk);
Value ValueHash (Value a);
commit ee70963b79933423751ae8602882891bbefbb6bc
Author: Keith Packard <keithp at keithp.com>
Date: Wed Sep 28 23:32:17 2011 -0700
Get rid of old-school variable length struct allocations
This confuses the new _FORTIFY_SOURCE bits in GCC, so use the
'sanctioned' form of placing a zero-length array at the end of the
struct.
Signed-off-by: Keith Packard <keithp at keithp.com>
diff --git a/value.h b/value.h
index d5aa64c..83596ff 100644
--- a/value.h
+++ b/value.h
@@ -182,10 +182,11 @@ typedef int HashValue;
typedef struct _natural {
DataType *type;
int length;
+ digit digits[0];
} Natural;
#define NaturalLength(n) ((n)->length)
-#define NaturalDigits(n) ((digit *) ((n) + 1))
+#define NaturalDigits(n) ((n)->digits)
Natural *NewNatural (unsigned value);
Natural *NewDoubleDigitNatural (double_digit dd);
@@ -499,7 +500,7 @@ Bool TypeTypesMember (Type *list, Type *type);
int TypeInit (void);
SymbolPtr TypeNameName (Type *t);
-#define TypeUnionElements(t) ((Type **) (&t->unions + 1))
+#define TypeUnionElements(t) ((t)->unions.types)
Type *TypeCombineBinary (Type *left, int tag, Type *right);
Type *TypeCombineUnary (Type *down, int tag);
@@ -579,9 +580,10 @@ typedef struct _float {
typedef struct _string {
BaseValue base;
long length;
+ char chars[0];
} String;
-#define StringChars(s) ((char *) ((s) + 1))
+#define StringChars(s) ((s)->chars)
typedef struct _foreign {
BaseValue base;
@@ -601,9 +603,10 @@ typedef struct _boxVector {
DataType *data;
int nvalues;
TypePtr type;
+ BoxPtr boxes[0];
} BoxVector, *BoxVectorPtr;
-#define BoxVectorBoxes(v) ((BoxPtr *) ((v) + 1))
+#define BoxVectorBoxes(v) ((v)->boxes)
typedef struct _array {
BaseValue base;
@@ -613,9 +616,10 @@ typedef struct _array {
BoxPtr fix;
BoxVectorPtr resize;
} u;
+ int dims[0];
} Array;
-#define ArrayDims(a) ((int *) ((a) + 1))
+#define ArrayDims(a) ((a)->dims)
#define ArrayLimits(a) (ArrayDims(a) + (a)->ndim)
#define ArrayConstant
#define ArrayNvalues(a) ((a)->resizable ? (a)->u.resize->nvalues : (a)->u.fix->nvalues)
@@ -631,6 +635,7 @@ typedef struct _io_chain {
int size;
int used;
int ptr;
+ unsigned char buffer[0];
} FileChain, *FileChainPtr;
typedef struct _file {
@@ -652,7 +657,7 @@ typedef struct _file {
#define FileEOF -1
#define FileBlocked -2
#define FileError -3
-#define FileBuffer(ic) ((unsigned char *) ((ic) + 1))
+#define FileBuffer(ic) ((ic)->buffer)
#define FileReadable 0x0001
#define FileWritable 0x0002
@@ -673,9 +678,10 @@ typedef struct _boxTypes {
DataType *data;
int count;
int size;
+ TypePtr elements[0];
} BoxTypes, *BoxTypesPtr;
-#define BoxTypesElements(bt) ((TypePtr *) ((bt) + 1))
+#define BoxTypesElements(bt) ((bt)->elements)
#define BoxTypesValue(bt,e) (BoxTypesElements(bt)[e])
extern BoxTypesPtr NewBoxTypes (int size);
@@ -700,9 +706,10 @@ typedef struct _structType {
DataType *data;
int nelements;
BoxTypesPtr types;
+ Atom atoms[0];
} StructType;
-#define StructTypeAtoms(st) ((Atom *) (st + 1))
+#define StructTypeAtoms(st) ((st)->atoms)
typedef struct _struct {
BaseValue base;
@@ -715,6 +722,7 @@ typedef struct _union {
StructType *type;
Atom tag;
BoxPtr value;
+ Type *types[0];
} Union;
typedef struct _func {
@@ -925,6 +933,7 @@ typedef struct _box {
TypePtr type;
BoxReplacePtr replace;
} u;
+ Value values[0];
} Box;
#if 1
@@ -932,7 +941,7 @@ typedef struct _box {
#else
#define BoxCheck(box)
#endif
-#define BoxElements(box) (BoxCheck (box) ((Value *) ((box) + 1)))
+#define BoxElements(box) (BoxCheck (box) ((box)->values))
#define BoxValueSet(box,e,v) ((BoxElements(box)[e]) = (v))
#define BoxValueGet(box,e) ((BoxElements(box)[e]))
#define BoxConstant(box,e) ((box)->constant)
@@ -948,11 +957,12 @@ BoxPtr BoxRewrite (BoxPtr box, int *ep);
typedef struct {
DataType *data;
int size;
+ char *values[0];
} DataCache, *DataCachePtr;
DataCachePtr NewDataCache (int size);
-#define DataCacheValues(vc) ((void *) ((vc) + 1))
+#define DataCacheValues(vc) ((void *) ((vc)->values))
Value NewInteger (Sign sign, Natural *mag);
Value NewIntInteger (int value);
More information about the Nickle
mailing list