1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/*******************************************************/
/* "C" Language Integrated Production System */
/* */
/* CLIPS Version 6.40 11/01/16 */
/* */
/* */
/*******************************************************/
/*************************************************************/
/* Purpose: Binary Load/Save Functions for Deffunctions */
/* */
/* Principal Programmer(s): */
/* Brian L. Dantes */
/* */
/* Contributing Programmer(s): */
/* */
/* Revision History: */
/* */
/* 6.30: Removed conditional code for unsupported */
/* compilers/operating systems (IBM_MCW, */
/* MAC_MCW, and IBM_TBC). */
/* */
/* Changed integer type/precision. */
/* */
/* 6.40: Pragma once and other inclusion changes. */
/* */
/* Added support for booleans with <stdbool.h>. */
/* */
/* Removed use of void pointers for specific */
/* data structures. */
/* */
/*************************************************************/
/* =========================================
*****************************************
EXTERNAL DEFINITIONS
=========================================
***************************************** */
#include "setup.h"
#if DEFFUNCTION_CONSTRUCT && (BLOAD || BLOAD_ONLY || BLOAD_AND_BSAVE)
#include "bload.h"
#include "bsave.h"
#include "cstrcbin.h"
#include "cstrccom.h"
#include "envrnmnt.h"
#include "memalloc.h"
#include "modulbin.h"
#include "dffnxbin.h"
/* =========================================
*****************************************
CONSTANTS
=========================================
***************************************** */
/* =========================================
*****************************************
MACROS AND TYPES
=========================================
***************************************** */
typedef struct bsaveDeffunctionModule
{
struct bsaveDefmoduleItemHeader header;
} BSAVE_DEFFUNCTION_MODULE;
typedef struct bsaveDeffunctionStruct
{
struct bsaveConstructHeader header;
unsigned short minNumberOfParameters;
unsigned short maxNumberOfParameters;
unsigned short numberOfLocalVars;
unsigned long name;
unsigned long code;
} BSAVE_DEFFUNCTION;
/****************************************/
/* LOCAL INTERNAL FUNCTION DEFINITIONS */
/****************************************/
#if BLOAD_AND_BSAVE
static void BsaveDeffunctionFind(Environment *);
static void MarkDeffunctionItems(Environment *,ConstructHeader *,void *);
static void BsaveDeffunctionExpressions(Environment *,FILE *);
static void BsaveDeffunctionExpression(Environment *,ConstructHeader *,void *);
static void BsaveStorageDeffunctions(Environment *,FILE *);
static void BsaveDeffunctions(Environment *,FILE *);
static void BsaveDeffunction(Environment *,ConstructHeader *,void *);
#endif
static void BloadStorageDeffunctions(Environment *);
static void BloadDeffunctions(Environment *);
static void UpdateDeffunctionModule(Environment *,void *,unsigned long);
static void UpdateDeffunction(Environment *,void *,unsigned long);
static void ClearDeffunctionBload(Environment *);
static void DeallocateDeffunctionBloadData(Environment *);
/* =========================================
*****************************************
EXTERNALLY VISIBLE FUNCTIONS
=========================================
***************************************** */
/***********************************************************
NAME : SetupDeffunctionsBload
DESCRIPTION : Initializes data structures and
routines for binary loads of deffunctions
INPUTS : None
RETURNS : Nothing useful
SIDE EFFECTS : Routines defined and structures initialized
NOTES : None
***********************************************************/
void SetupDeffunctionsBload(
Environment *theEnv)
{
AllocateEnvironmentData(theEnv,DFFNXBIN_DATA,sizeof(struct deffunctionBinaryData),DeallocateDeffunctionBloadData);
#if BLOAD_AND_BSAVE
AddBinaryItem(theEnv,"deffunctions",0,BsaveDeffunctionFind,BsaveDeffunctionExpressions,
BsaveStorageDeffunctions,BsaveDeffunctions,
BloadStorageDeffunctions,BloadDeffunctions,
ClearDeffunctionBload);
#else
AddBinaryItem(theEnv,"deffunctions",0,NULL,NULL,NULL,NULL,
BloadStorageDeffunctions,BloadDeffunctions,
ClearDeffunctionBload);
#endif
}
/***********************************************************/
/* DeallocateDeffunctionBloadData: Deallocates environment */
/* data for the deffunction bsave functionality. */
/***********************************************************/
static void DeallocateDeffunctionBloadData(
Environment *theEnv)
{
size_t space;
#if (BLOAD || BLOAD_ONLY || BLOAD_AND_BSAVE) && (! RUN_TIME)
space = DeffunctionBinaryData(theEnv)->DeffunctionCount * sizeof(Deffunction);
if (space != 0) genfree(theEnv,DeffunctionBinaryData(theEnv)->DeffunctionArray,space);
space = DeffunctionBinaryData(theEnv)->ModuleCount * sizeof(struct deffunctionModuleData);
if (space != 0) genfree(theEnv,DeffunctionBinaryData(theEnv)->ModuleArray,space);
#endif
}
/***************************************************
NAME : BloadDeffunctionModuleReference
DESCRIPTION : Returns a pointer to the
appropriate deffunction module
INPUTS : The index of the module
RETURNS : A pointer to the module
SIDE EFFECTS : None
NOTES : None
***************************************************/
void *BloadDeffunctionModuleReference(
Environment *theEnv,
unsigned long theIndex)
{
return (void *) &DeffunctionBinaryData(theEnv)->ModuleArray[theIndex];
}
/* =========================================
*****************************************
INTERNALLY VISIBLE FUNCTIONS
=========================================
***************************************** */
#if BLOAD_AND_BSAVE
/***************************************************************************
NAME : BsaveDeffunctionFind
DESCRIPTION : For all deffunctions, this routine marks all
the needed symbols.
Also, it also counts the number of
expression structures needed.
Also, counts total number of deffunctions.
INPUTS : None
RETURNS : Nothing useful
SIDE EFFECTS : ExpressionCount (a global from BSAVE.C) is incremented
for every expression needed
Symbols are marked in their structures
NOTES : Also sets bsaveIndex for each deffunction (assumes
deffunctions will be bsaved in order of binary list)
***************************************************************************/
static void BsaveDeffunctionFind(
Environment *theEnv)
{
SaveBloadCount(theEnv,DeffunctionBinaryData(theEnv)->ModuleCount);
SaveBloadCount(theEnv,DeffunctionBinaryData(theEnv)->DeffunctionCount);
DeffunctionBinaryData(theEnv)->DeffunctionCount = 0L;
DeffunctionBinaryData(theEnv)->ModuleCount = GetNumberOfDefmodules(theEnv);
DoForAllConstructs(theEnv,MarkDeffunctionItems,
DeffunctionData(theEnv)->DeffunctionModuleIndex,
false,NULL);
}
/***************************************************
NAME : MarkDeffunctionItems
DESCRIPTION : Marks the needed items for
a deffunction bsave
INPUTS : 1) The deffunction
2) User data buffer (ignored)
RETURNS : Nothing useful
SIDE EFFECTS : Needed items marked
NOTES : None
***************************************************/
static void MarkDeffunctionItems(
Environment *theEnv,
ConstructHeader *theDeffunction,
void *userBuffer)
{
#if MAC_XCD
#pragma unused(userBuffer)
#endif
MarkConstructHeaderNeededItems(theDeffunction,DeffunctionBinaryData(theEnv)->DeffunctionCount++);
ExpressionData(theEnv)->ExpressionCount += ExpressionSize(((Deffunction *) theDeffunction)->code);
MarkNeededItems(theEnv,((Deffunction *) theDeffunction)->code);
}
/***************************************************
NAME : BsaveDeffunctionExpressions
DESCRIPTION : Writes out all expressions needed
by deffunctyions
INPUTS : The file pointer of the binary file
RETURNS : Nothing useful
SIDE EFFECTS : File updated
NOTES : None
***************************************************/
static void BsaveDeffunctionExpressions(
Environment *theEnv,
FILE *fp)
{
DoForAllConstructs(theEnv,BsaveDeffunctionExpression,DeffunctionData(theEnv)->DeffunctionModuleIndex,
false,fp);
}
/***************************************************
NAME : BsaveDeffunctionExpression
DESCRIPTION : Saves the needed expressions for
a deffunction bsave
INPUTS : 1) The deffunction
2) Output data file pointer
RETURNS : Nothing useful
SIDE EFFECTS : Expressions saved
NOTES : None
***************************************************/
static void BsaveDeffunctionExpression(
Environment *theEnv,
ConstructHeader *theDeffunction,
void *userBuffer)
{
BsaveExpression(theEnv,((Deffunction *) theDeffunction)->code,(FILE *) userBuffer);
}
/***********************************************************
NAME : BsaveStorageDeffunctions
DESCRIPTION : Writes out number of each type of structure
required for deffunctions
Space required for counts (unsigned long)
INPUTS : File pointer of binary file
RETURNS : Nothing useful
SIDE EFFECTS : Binary file adjusted
NOTES : None
***********************************************************/
static void BsaveStorageDeffunctions(
Environment *theEnv,
FILE *fp)
{
size_t space;
space = sizeof(unsigned long) * 2;
GenWrite(&space,sizeof(size_t),fp);
GenWrite(&DeffunctionBinaryData(theEnv)->ModuleCount,sizeof(unsigned long),fp);
GenWrite(&DeffunctionBinaryData(theEnv)->DeffunctionCount,sizeof(unsigned long),fp);
}
/*************************************************************************************
NAME : BsaveDeffunctions
DESCRIPTION : Writes out deffunction in binary format
Space required (unsigned long)
All deffunctions (sizeof(Deffunction) * Number of deffunctions)
INPUTS : File pointer of binary file
RETURNS : Nothing useful
SIDE EFFECTS : Binary file adjusted
NOTES : None
*************************************************************************************/
static void BsaveDeffunctions(
Environment *theEnv,
FILE *fp)
{
size_t space;
Defmodule *theModule;
DeffunctionModuleData *theModuleItem;
BSAVE_DEFFUNCTION_MODULE dummy_mitem;
space = ((sizeof(BSAVE_DEFFUNCTION_MODULE) * DeffunctionBinaryData(theEnv)->ModuleCount) +
(sizeof(BSAVE_DEFFUNCTION) * DeffunctionBinaryData(theEnv)->DeffunctionCount));
GenWrite(&space,sizeof(size_t),fp);
/* =================================
Write out each deffunction module
================================= */
DeffunctionBinaryData(theEnv)->DeffunctionCount = 0L;
theModule = GetNextDefmodule(theEnv,NULL);
while (theModule != NULL)
{
theModuleItem = (DeffunctionModuleData *)
GetModuleItem(theEnv,theModule,FindModuleItem(theEnv,"deffunction")->moduleIndex);
AssignBsaveDefmdlItemHdrVals(&dummy_mitem.header,&theModuleItem->header);
GenWrite(&dummy_mitem,sizeof(BSAVE_DEFFUNCTION_MODULE),fp);
theModule = GetNextDefmodule(theEnv,theModule);
}
/* ==========================
Write out each deffunction
========================== */
DoForAllConstructs(theEnv,BsaveDeffunction,DeffunctionData(theEnv)->DeffunctionModuleIndex,
false,fp);
RestoreBloadCount(theEnv,&DeffunctionBinaryData(theEnv)->ModuleCount);
RestoreBloadCount(theEnv,&DeffunctionBinaryData(theEnv)->DeffunctionCount);
}
/***************************************************
NAME : BsaveDeffunction
DESCRIPTION : Bsaves a deffunction
INPUTS : 1) The deffunction
2) Output data file pointer
RETURNS : Nothing useful
SIDE EFFECTS : Deffunction saved
NOTES : None
***************************************************/
static void BsaveDeffunction(
Environment *theEnv,
ConstructHeader *theDeffunction,
void *userBuffer)
{
Deffunction *dptr = (Deffunction *) theDeffunction;
BSAVE_DEFFUNCTION dummy_df;
AssignBsaveConstructHeaderVals(&dummy_df.header,&dptr->header);
dummy_df.minNumberOfParameters = dptr->minNumberOfParameters;
dummy_df.maxNumberOfParameters = dptr->maxNumberOfParameters;
dummy_df.numberOfLocalVars = dptr->numberOfLocalVars;
if (dptr->code != NULL)
{
dummy_df.code = ExpressionData(theEnv)->ExpressionCount;
ExpressionData(theEnv)->ExpressionCount += ExpressionSize(dptr->code);
}
else
dummy_df.code = ULONG_MAX;
GenWrite(&dummy_df,sizeof(BSAVE_DEFFUNCTION),(FILE *) userBuffer);
}
#endif
/***********************************************************************
NAME : BloadStorageDeffunctions
DESCRIPTION : This routine space required for deffunction
structures and allocates space for them
INPUTS : Nothing
RETURNS : Nothing useful
SIDE EFFECTS : Arrays allocated and set
NOTES : This routine makes no attempt to reset any pointers
within the structures
***********************************************************************/
static void BloadStorageDeffunctions(
Environment *theEnv)
{
size_t space;
GenReadBinary(theEnv,&space,sizeof(size_t));
if (space == 0L)
return;
GenReadBinary(theEnv,&DeffunctionBinaryData(theEnv)->ModuleCount,sizeof(unsigned long));
GenReadBinary(theEnv,&DeffunctionBinaryData(theEnv)->DeffunctionCount,sizeof(unsigned long));
if (DeffunctionBinaryData(theEnv)->ModuleCount == 0L)
{
DeffunctionBinaryData(theEnv)->ModuleArray = NULL;
DeffunctionBinaryData(theEnv)->DeffunctionArray = NULL;
return;
}
space = (DeffunctionBinaryData(theEnv)->ModuleCount * sizeof(DeffunctionModuleData));
DeffunctionBinaryData(theEnv)->ModuleArray = (DeffunctionModuleData *) genalloc(theEnv,space);
if (DeffunctionBinaryData(theEnv)->DeffunctionCount == 0L)
{
DeffunctionBinaryData(theEnv)->DeffunctionArray = NULL;
return;
}
space = (DeffunctionBinaryData(theEnv)->DeffunctionCount * sizeof(Deffunction));
DeffunctionBinaryData(theEnv)->DeffunctionArray = (Deffunction *) genalloc(theEnv,space);
}
/*********************************************************************
NAME : BloadDeffunctions
DESCRIPTION : This routine reads deffunction information from
a binary file
This routine moves through the deffunction
binary array updating pointers
INPUTS : None
RETURNS : Nothing useful
SIDE EFFECTS : Pointers reset from array indices
NOTES : Assumes all loading is finished
********************************************************************/
static void BloadDeffunctions(
Environment *theEnv)
{
size_t space;
GenReadBinary(theEnv,&space,sizeof(size_t));
BloadandRefresh(theEnv,DeffunctionBinaryData(theEnv)->ModuleCount,sizeof(BSAVE_DEFFUNCTION_MODULE),UpdateDeffunctionModule);
BloadandRefresh(theEnv,DeffunctionBinaryData(theEnv)->DeffunctionCount,sizeof(BSAVE_DEFFUNCTION),UpdateDeffunction);
}
/*******************************************************
NAME : UpdateDeffunctionModule
DESCRIPTION : Updates deffunction module with binary
load data - sets pointers from
offset information
INPUTS : 1) A pointer to the bloaded data
2) The index of the binary array
element to update
RETURNS : Nothing useful
SIDE EFFECTS : Deffunction moudle pointers updated
NOTES : None
*******************************************************/
static void UpdateDeffunctionModule(
Environment *theEnv,
void *buf,
unsigned long obji)
{
BSAVE_DEFFUNCTION_MODULE *bdptr;
bdptr = (BSAVE_DEFFUNCTION_MODULE *) buf;
UpdateDefmoduleItemHeader(theEnv,&bdptr->header,&DeffunctionBinaryData(theEnv)->ModuleArray[obji].header,
sizeof(Deffunction),DeffunctionBinaryData(theEnv)->DeffunctionArray);
}
/***************************************************
NAME : UpdateDeffunction
DESCRIPTION : Updates deffunction with binary
load data - sets pointers from
offset information
INPUTS : 1) A pointer to the bloaded data
2) The index of the binary array
element to update
RETURNS : Nothing useful
SIDE EFFECTS : Deffunction pointers upadted
NOTES : None
***************************************************/
static void UpdateDeffunction(
Environment *theEnv,
void *buf,
unsigned long obji)
{
BSAVE_DEFFUNCTION *bdptr;
Deffunction *dptr;
bdptr = (BSAVE_DEFFUNCTION *) buf;
dptr = &DeffunctionBinaryData(theEnv)->DeffunctionArray[obji];
UpdateConstructHeader(theEnv,&bdptr->header,&dptr->header,DEFFUNCTION,
sizeof(DeffunctionModuleData),DeffunctionBinaryData(theEnv)->ModuleArray,
sizeof(Deffunction),DeffunctionBinaryData(theEnv)->DeffunctionArray);
dptr->code = ExpressionPointer(bdptr->code);
dptr->busy = 0;
dptr->executing = 0;
#if DEBUGGING_FUNCTIONS
dptr->trace = DeffunctionData(theEnv)->WatchDeffunctions;
#endif
dptr->minNumberOfParameters = bdptr->minNumberOfParameters;
dptr->maxNumberOfParameters = bdptr->maxNumberOfParameters;
dptr->numberOfLocalVars = bdptr->numberOfLocalVars;
}
/***************************************************************
NAME : ClearDeffunctionBload
DESCRIPTION : Release all binary-loaded deffunction
structure arrays
Resets deffunction list to NULL
INPUTS : None
RETURNS : Nothing useful
SIDE EFFECTS : Memory cleared
NOTES : Deffunction name symbol counts decremented
***************************************************************/
static void ClearDeffunctionBload(
Environment *theEnv)
{
unsigned long i;
size_t space;
space = (sizeof(DeffunctionModuleData) * DeffunctionBinaryData(theEnv)->ModuleCount);
if (space == 0L)
return;
genfree(theEnv,DeffunctionBinaryData(theEnv)->ModuleArray,space);
DeffunctionBinaryData(theEnv)->ModuleArray = NULL;
DeffunctionBinaryData(theEnv)->ModuleCount = 0L;
for (i = 0 ; i < DeffunctionBinaryData(theEnv)->DeffunctionCount ; i++)
UnmarkConstructHeader(theEnv,&DeffunctionBinaryData(theEnv)->DeffunctionArray[i].header);
space = (sizeof(Deffunction) * DeffunctionBinaryData(theEnv)->DeffunctionCount);
if (space == 0)
return;
genfree(theEnv,DeffunctionBinaryData(theEnv)->DeffunctionArray,space);
DeffunctionBinaryData(theEnv)->DeffunctionArray = NULL;
DeffunctionBinaryData(theEnv)->DeffunctionCount = 0L;
}
#endif