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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/*******************************************************/
/* "C" Language Integrated Production System */
/* */
/* CLIPS Version 6.40 07/30/16 */
/* */
/* */
/*******************************************************/
/*************************************************************/
/* Purpose: Generic Function Construct Compiler Code */
/* */
/* Principal Programmer(s): */
/* Brian L. Dantes */
/* */
/* Contributing Programmer(s): */
/* */
/* Revision History: */
/* */
/* 6.24: Added pragmas to remove unused parameter */
/* warnings. */
/* */
/* 6.30: Added support for path name argument to */
/* constructs-to-c. */
/* */
/* Changed integer type/precision. */
/* */
/* Removed conditional code for unsupported */
/* compilers/operating systems (IBM_MCW and */
/* MAC_MCW). */
/* */
/* Added const qualifiers to remove C++ */
/* deprecation warnings. */
/* */
/* 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 DEFGENERIC_CONSTRUCT && CONSTRUCT_COMPILER && (! RUN_TIME)
#include "conscomp.h"
#include "envrnmnt.h"
#include "genrccom.h"
#if DEFRULE_CONSTRUCT
#include "network.h"
#endif
#if OBJECT_SYSTEM
#include "objcmp.h"
#endif
#include "genrccmp.h"
/* =========================================
*****************************************
CONSTANTS
=========================================
***************************************** */
#define MODULEI 0
#define GENERICI 1
#define METHODI 2
#define RESTRICTIONI 3
#define TYPEI 4
#define SAVE_ITEMS 5
/* =========================================
*****************************************
MACROS AND TYPES
=========================================
***************************************** */
#define MethodPrefix() ArbitraryPrefix(DefgenericData(theEnv)->DefgenericCodeItem,2)
#define RestrictionPrefix() ArbitraryPrefix(DefgenericData(theEnv)->DefgenericCodeItem,3)
#define TypePrefix() ArbitraryPrefix(DefgenericData(theEnv)->DefgenericCodeItem,4)
/***************************************/
/* LOCAL INTERNAL FUNCTION DEFINITIONS */
/***************************************/
static void ReadyDefgenericsForCode(Environment *);
static bool DefgenericsToCode(Environment *,const char *,const char *,char *,
unsigned int,FILE *,unsigned int,unsigned int);
static void CloseDefgenericFiles(Environment *,FILE *[SAVE_ITEMS],bool [SAVE_ITEMS],
struct CodeGeneratorFile [SAVE_ITEMS],unsigned int);
static void DefgenericModuleToCode(Environment *,FILE *,Defmodule *,unsigned int,unsigned int);
static void SingleDefgenericToCode(Environment *,FILE *,unsigned int,unsigned int,
Defgeneric *,unsigned int,unsigned int,unsigned int);
static void MethodToCode(Environment *,FILE *,unsigned int,unsigned int,Defmethod *,
unsigned int,unsigned int,unsigned int);
static void RestrictionToCode(Environment *,FILE *,unsigned int,RESTRICTION *,
unsigned int,unsigned int);
static void TypeToCode(Environment *,FILE *,unsigned int,void *,unsigned int);
static void InitDefgenericsCode(Environment *,FILE *,unsigned int,unsigned int);
/* =========================================
*****************************************
EXTERNALLY VISIBLE FUNCTIONS
=========================================
***************************************** */
/***************************************************
NAME : SetupGenericsCompiler
DESCRIPTION : Initializes the construct compiler
item for generic functions
INPUTS : None
RETURNS : Nothing useful
SIDE EFFECTS : Code generator item initialized
NOTES : None
***************************************************/
void SetupGenericsCompiler(
Environment *theEnv)
{
DefgenericData(theEnv)->DefgenericCodeItem = AddCodeGeneratorItem(theEnv,"generics",0,ReadyDefgenericsForCode,
InitDefgenericsCode,DefgenericsToCode,5);
}
/***************************************************
NAME : PrintGenericFunctionReference
DESCRIPTION : Prints a reference to the run-time
generic array for the construct
compiler
INPUTS : 1) The file output destination
2) A pointer to the generic
3) The id of the run-time image
4) The maximum number of indices
in any array
RETURNS : Nothing useful
SIDE EFFECTS : Reference printed
NOTES : None
***************************************************/
void PrintGenericFunctionReference(
Environment *theEnv,
FILE *fp,
Defgeneric *gfunc,
unsigned imageID,
unsigned maxIndices)
{
if (gfunc == NULL)
fprintf(fp,"NULL");
else
fprintf(fp,"&%s%d_%lu[%lul]",ConstructPrefix(DefgenericData(theEnv)->DefgenericCodeItem),imageID,
(gfunc->header.bsaveID / maxIndices) + 1,
(gfunc->header.bsaveID % maxIndices));
}
/****************************************************
NAME : DefgenericCModuleReference
DESCRIPTION : Prints out a reference to a
defgeneric module
INPUTS : 1) The output file
2) The id of the module item
3) The id of the image
4) The maximum number of elements
allowed in an array
RETURNS : Nothing useful
SIDE EFFECTS : Defgeneric module reference printed
NOTES : None
****************************************************/
void DefgenericCModuleReference(
Environment *theEnv,
FILE *theFile,
unsigned long count,
unsigned int imageID,
unsigned int maxIndices)
{
fprintf(theFile,"MIHS &%s%u_%lu[%lu]",
ModulePrefix(DefgenericData(theEnv)->DefgenericCodeItem),
imageID,
(count / maxIndices) + 1,
(count % maxIndices));
}
/* =========================================
*****************************************
INTERNALLY VISIBLE FUNCTIONS
=========================================
***************************************** */
/***************************************************
NAME : ReadyDefgenericsForCode
DESCRIPTION : Sets index of generic-functions
for use in compiled expressions
INPUTS : None
RETURNS : Nothing useful
SIDE EFFECTS : BsaveIndices set
NOTES : None
***************************************************/
static void ReadyDefgenericsForCode(
Environment *theEnv)
{
MarkConstructBsaveIDs(theEnv,DefgenericData(theEnv)->DefgenericModuleIndex);
}
/**************************************************/
/* InitDefgenericsCode: Writes out initialization */
/* code for defgenerics for a run-time module. */
/**************************************************/
static void InitDefgenericsCode(
Environment *theEnv,
FILE *initFP,
unsigned int imageID,
unsigned int maxIndices)
{
#if MAC_XCD
#pragma unused(maxIndices)
#pragma unused(imageID)
#pragma unused(theEnv)
#endif
fprintf(initFP," DefgenericRunTimeInitialize(theEnv);\n");
}
/*******************************************************
NAME : DefgenericsToCode
DESCRIPTION : Writes out static array code for
generic functions, methods, etc.
INPUTS : 1) The base name of the construct set
2) The base id for this construct
3) The file pointer for the header file
4) The base id for the construct set
5) The max number of indices allowed
in an array
RETURNS : False on errors,
true if generic functions written
SIDE EFFECTS : Code written to files
NOTES : None
*******************************************************/
static bool DefgenericsToCode(
Environment *theEnv,
const char *fileName,
const char *pathName,
char *fileNameBuffer,
unsigned int fileID,
FILE *headerFP,
unsigned int imageID,
unsigned int maxIndices)
{
unsigned int fileCount = 1;
Defmodule *theModule;
Defgeneric *theDefgeneric;
Defmethod *theMethod;
RESTRICTION *theRestriction;
short i,j,k;
unsigned int moduleCount = 0;
unsigned int itemArrayCounts[SAVE_ITEMS];
unsigned int itemArrayVersions[SAVE_ITEMS];
FILE *itemFiles[SAVE_ITEMS];
bool itemReopenFlags[SAVE_ITEMS];
struct CodeGeneratorFile itemCodeFiles[SAVE_ITEMS];
for (i = 0 ; i < SAVE_ITEMS ; i++)
{
itemArrayCounts[i] = 0;
itemArrayVersions[i] = 1;
itemFiles[i] = NULL;
itemReopenFlags[i] = false;
itemCodeFiles[i].filePrefix = NULL;
itemCodeFiles[i].pathName = pathName;
itemCodeFiles[i].fileNameBuffer = fileNameBuffer;
}
/* ===========================================
Include the appropriate generic header file
=========================================== */
fprintf(headerFP,"#include \"genrcfun.h\"\n");
/* =============================================================
Loop through all the modules and all the defgenerics writing
their C code representation to the file as they are traversed
============================================================= */
theModule = GetNextDefmodule(theEnv,NULL);
while (theModule != NULL)
{
SetCurrentModule(theEnv,theModule);
itemFiles[MODULEI] =
OpenFileIfNeeded(theEnv,itemFiles[MODULEI],fileName,pathName,fileNameBuffer,fileID,imageID,&fileCount,
itemArrayVersions[MODULEI],headerFP,
"DEFGENERIC_MODULE",ModulePrefix(DefgenericData(theEnv)->DefgenericCodeItem),
itemReopenFlags[MODULEI],&itemCodeFiles[MODULEI]);
if (itemFiles[MODULEI] == NULL)
goto GenericCodeError;
DefgenericModuleToCode(theEnv,itemFiles[MODULEI],theModule,imageID,maxIndices);
itemFiles[MODULEI] =
CloseFileIfNeeded(theEnv,itemFiles[MODULEI],&itemArrayCounts[MODULEI],
&itemArrayVersions[MODULEI],maxIndices,
&itemReopenFlags[MODULEI],&itemCodeFiles[MODULEI]);
theDefgeneric = GetNextDefgeneric(theEnv,NULL);
while (theDefgeneric != NULL)
{
itemFiles[GENERICI] =
OpenFileIfNeeded(theEnv,itemFiles[GENERICI],fileName,pathName,fileNameBuffer,fileID,imageID,&fileCount,
itemArrayVersions[GENERICI],headerFP,
"Defgeneric",ConstructPrefix(DefgenericData(theEnv)->DefgenericCodeItem),
itemReopenFlags[GENERICI],&itemCodeFiles[GENERICI]);
if (itemFiles[GENERICI] == NULL)
goto GenericCodeError;
SingleDefgenericToCode(theEnv,itemFiles[GENERICI],imageID,maxIndices,theDefgeneric,
moduleCount,itemArrayVersions[METHODI],
itemArrayCounts[METHODI]);
itemArrayCounts[GENERICI]++;
itemFiles[GENERICI] =
CloseFileIfNeeded(theEnv,itemFiles[GENERICI],&itemArrayCounts[GENERICI],
&itemArrayVersions[GENERICI],maxIndices,
&itemReopenFlags[GENERICI],&itemCodeFiles[GENERICI]);
if (theDefgeneric->mcnt > 0)
{
/* ===========================================
Make sure that all methods for a particular
generic function go into the same array
=========================================== */
itemFiles[METHODI] =
OpenFileIfNeeded(theEnv,itemFiles[METHODI],fileName,pathName,fileNameBuffer,fileID,imageID,&fileCount,
itemArrayVersions[METHODI],headerFP,
"Defmethod",MethodPrefix(),
itemReopenFlags[METHODI],&itemCodeFiles[METHODI]);
if (itemFiles[METHODI] == NULL)
goto GenericCodeError;
for (i = 0 ; i < theDefgeneric->mcnt ; i++)
{
theMethod = &theDefgeneric->methods[i];
if (i > 0)
fprintf(itemFiles[METHODI],",\n");
MethodToCode(theEnv,itemFiles[METHODI],imageID,maxIndices,theMethod,moduleCount,
itemArrayVersions[RESTRICTIONI],itemArrayCounts[RESTRICTIONI]);
if (theMethod->restrictionCount > 0)
{
/* ========================================
Make sure that all restrictions for a
particular method go into the same array
======================================== */
itemFiles[RESTRICTIONI] =
OpenFileIfNeeded(theEnv,itemFiles[RESTRICTIONI],fileName,pathName,fileNameBuffer,fileID,
imageID,&fileCount,
itemArrayVersions[RESTRICTIONI],headerFP,
"RESTRICTION",RestrictionPrefix(),
itemReopenFlags[RESTRICTIONI],&itemCodeFiles[RESTRICTIONI]);
if (itemFiles[RESTRICTIONI] == NULL)
goto GenericCodeError;
for (j = 0 ; j < theMethod->restrictionCount ; j++)
{
theRestriction = &theMethod->restrictions[j];
if (j > 0)
fprintf(itemFiles[RESTRICTIONI],",\n");
RestrictionToCode(theEnv,itemFiles[RESTRICTIONI],imageID,theRestriction,
itemArrayVersions[TYPEI],itemArrayCounts[TYPEI]);
if (theRestriction->tcnt > 0)
{
/* =========================================
Make sure that all types for a particular
restriction go into the same array
========================================= */
itemFiles[TYPEI] =
OpenFileIfNeeded(theEnv,itemFiles[TYPEI],fileName,pathName,fileNameBuffer,fileID,
imageID,&fileCount,
itemArrayVersions[TYPEI],headerFP,
"void *",TypePrefix(),
itemReopenFlags[TYPEI],&itemCodeFiles[TYPEI]);
if (itemFiles[TYPEI] == NULL)
goto GenericCodeError;
for (k = 0 ; k < theRestriction->tcnt ; k++)
{
if (k > 0)
fprintf(itemFiles[TYPEI],",\n");
TypeToCode(theEnv,itemFiles[TYPEI],imageID,
theRestriction->types[k],maxIndices);
}
itemArrayCounts[TYPEI] += theRestriction->tcnt;
itemFiles[TYPEI] =
CloseFileIfNeeded(theEnv,itemFiles[TYPEI],&itemArrayCounts[TYPEI],
&itemArrayVersions[TYPEI],maxIndices,
&itemReopenFlags[TYPEI],&itemCodeFiles[TYPEI]);
}
}
itemArrayCounts[RESTRICTIONI] += theMethod->restrictionCount;
itemFiles[RESTRICTIONI] =
CloseFileIfNeeded(theEnv,itemFiles[RESTRICTIONI],&itemArrayCounts[RESTRICTIONI],
&itemArrayVersions[RESTRICTIONI],maxIndices,
&itemReopenFlags[RESTRICTIONI],&itemCodeFiles[RESTRICTIONI]);
}
}
itemArrayCounts[METHODI] += theDefgeneric->mcnt;
itemFiles[METHODI] =
CloseFileIfNeeded(theEnv,itemFiles[METHODI],&itemArrayCounts[METHODI],
&itemArrayVersions[METHODI],maxIndices,
&itemReopenFlags[METHODI],&itemCodeFiles[METHODI]);
}
theDefgeneric = GetNextDefgeneric(theEnv,theDefgeneric);
}
theModule = GetNextDefmodule(theEnv,theModule);
moduleCount++;
itemArrayCounts[MODULEI]++;
}
CloseDefgenericFiles(theEnv,itemFiles,itemReopenFlags,itemCodeFiles,maxIndices);
return true;
GenericCodeError:
CloseDefgenericFiles(theEnv,itemFiles,itemReopenFlags,itemCodeFiles,maxIndices);
return false;
}
/******************************************************
NAME : CloseDefgenericFiles
DESCRIPTION : Closes construct compiler files
for defgeneric structures
INPUTS : 1) An array containing all the
pertinent file pointers
2) An array containing all the
pertinent file reopen flags
3) An array containing all the
pertinent file name/id/version info
4) The maximum number of indices
allowed in an array
RETURNS : Nothing useful
SIDE EFFECTS : Files closed
NOTES : None
*****************************************************/
static void CloseDefgenericFiles(
Environment *theEnv,
FILE *itemFiles[SAVE_ITEMS],
bool itemReopenFlags[SAVE_ITEMS],
struct CodeGeneratorFile itemCodeFiles[SAVE_ITEMS],
unsigned int maxIndices)
{
unsigned int count = maxIndices;
unsigned int arrayVersion = 0;
unsigned int i;
for (i = 0 ; i < SAVE_ITEMS ; i++)
{
count = maxIndices;
itemFiles[i] = CloseFileIfNeeded(theEnv,itemFiles[i],&count,&arrayVersion,
maxIndices,&itemReopenFlags[i],
&itemCodeFiles[i]);
}
}
/***************************************************
NAME : DefgenericModuleToCode
DESCRIPTION : Writes out the C values for a
defgeneric module item
INPUTS : 1) The output file
2) The module for the defgenerics
3) The compile image id
4) The maximum number of elements
in an array
RETURNS : Nothing useful
SIDE EFFECTS : Defgeneric module item written
NOTES : None
***************************************************/
static void DefgenericModuleToCode(
Environment *theEnv,
FILE *theFile,
Defmodule *theModule,
unsigned int imageID,
unsigned int maxIndices)
{
fprintf(theFile,"{");
ConstructModuleToCode(theEnv,theFile,theModule,imageID,maxIndices,
DefgenericData(theEnv)->DefgenericModuleIndex,ConstructPrefix(DefgenericData(theEnv)->DefgenericCodeItem));
fprintf(theFile,"}");
}
/****************************************************************
NAME : SingleDefgenericToCode
DESCRIPTION : Writes out a single defgeneric's
data to the file
INPUTS : 1) The output file
2) The compile image id
3) The maximum number of
elements in an array
4) The defgeneric
5) The module index
6) The partition holding the
generic methods
7) The relative index of the
generics methods in the partition
RETURNS : Nothing useful
SIDE EFFECTS : Defgeneric data written
NOTES : None
***************************************************************/
static void SingleDefgenericToCode(
Environment *theEnv,
FILE *theFile,
unsigned int imageID,
unsigned int maxIndices,
Defgeneric *theDefgeneric,
unsigned int moduleCount,
unsigned int methodArrayVersion,
unsigned int methodArrayCount)
{
/* ==================
Defgeneric Header
================== */
fprintf(theFile,"{");
ConstructHeaderToCode(theEnv,theFile,&theDefgeneric->header,imageID,maxIndices,moduleCount,
ModulePrefix(DefgenericData(theEnv)->DefgenericCodeItem),
ConstructPrefix(DefgenericData(theEnv)->DefgenericCodeItem));
/* =========================
Defgeneric specific data
========================= */
fprintf(theFile,",0,0,");
if (theDefgeneric->methods == NULL)
fprintf(theFile,"NULL");
else
{
fprintf(theFile,"&%s%d_%d[%d]",MethodPrefix(),imageID,
methodArrayVersion,methodArrayCount);
}
fprintf(theFile,",%hd,0}",theDefgeneric->mcnt);
}
/****************************************************************
NAME : MethodToCode
DESCRIPTION : Writes out a single method's
data to the file
INPUTS : 1) The output file
2) The compile image id
3) The method
4) The partition holding the
method restrictions
5) The relative index of the
method restrictions in the partition
RETURNS : Nothing useful
SIDE EFFECTS : Method data written
NOTES : None
***************************************************************/
static void MethodToCode(
Environment *theEnv,
FILE *theFile,
unsigned int imageID,
unsigned int maxIndices,
Defmethod *theMethod,
unsigned int moduleCount,
unsigned int restrictionArrayVersion,
unsigned int restrictionArrayCount)
{
fprintf(theFile,"{");
ConstructHeaderToCode(theEnv,theFile,&theMethod->header,imageID,maxIndices,moduleCount,
ModulePrefix(DefgenericData(theEnv)->DefgenericCodeItem),
"");
fprintf(theFile,",%u,0,%hd,%hu,%hu,%hd,%u,0,",
theMethod->index,theMethod->restrictionCount,
theMethod->minRestrictions,theMethod->maxRestrictions,
theMethod->localVarCount,theMethod->system);
if (theMethod->restrictions == NULL)
fprintf(theFile,"NULL,");
else
fprintf(theFile,"&%s%d_%d[%d],",RestrictionPrefix(),imageID,
restrictionArrayVersion,restrictionArrayCount);
ExpressionToCode(theEnv,theFile,theMethod->actions);
fprintf(theFile,"}");
}
/****************************************************************
NAME : RestrictionToCode
DESCRIPTION : Writes out a single restriction's
data to the file
INPUTS : 1) The output file
2) The compile image id
3) The restriction
4) The partition holding the
restriction types
5) The relative index of the
restriction types in the partition
RETURNS : Nothing useful
SIDE EFFECTS : Restriction data written
NOTES : None
***************************************************************/
static void RestrictionToCode(
Environment *theEnv,
FILE *theFile,
unsigned int imageID,
RESTRICTION *theRestriction,
unsigned int typeArrayVersion,
unsigned int typeArrayCount)
{
fprintf(theFile,"{");
if (theRestriction->types == NULL)
fprintf(theFile,"NULL,");
else
fprintf(theFile,"&%s%d_%d[%d],",TypePrefix(),imageID,
typeArrayVersion,typeArrayCount);
ExpressionToCode(theEnv,theFile,theRestriction->query);
fprintf(theFile,",%hd}",theRestriction->tcnt);
}
/****************************************************************
NAME : TypeToCode
DESCRIPTION : Writes out a single type's
data to the file
INPUTS : 1) The output file
2) The compile image id
3) The type
RETURNS : Nothing useful
SIDE EFFECTS : Type data written
NOTES : None
***************************************************************/
static void TypeToCode(
Environment *theEnv,
FILE *theFile,
unsigned int imageID,
void *theType,
unsigned int maxIndices)
{
#if OBJECT_SYSTEM
fprintf(theFile,"VS ");
PrintClassReference(theEnv,theFile,(Defclass *) theType,imageID,maxIndices);
#else
#if MAC_XCD
#pragma unused(imageID)
#pragma unused(maxIndices)
#endif
PrintIntegerReference(theEnv,theFile,(CLIPSInteger *) theType);
#endif
}
#endif