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
/*******************************************************/
/* "C" Language Integrated Production System */
/* */
/* CLIPS Version 6.40 07/30/16 */
/* */
/* DEFFACTS CONSTRUCTS-TO-C MODULE */
/*******************************************************/
/*************************************************************/
/* Purpose: Implements the constructs-to-c feature for the */
/* deffacts construct. */
/* */
/* Principal Programmer(s): */
/* Gary D. Riley */
/* */
/* Contributing Programmer(s): */
/* Brian L. Dantes */
/* */
/* Revision History: */
/* */
/* 6.30: Added support for path name argument to */
/* constructs-to-c. */
/* */
/* Removed conditional code for unsupported */
/* compilers/operating systems (IBM_MCW, */
/* MAC_MCW, and IBM_TBC). */
/* */
/* 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. */
/* */
/*************************************************************/
#include "setup.h"
#if DEFFACTS_CONSTRUCT && CONSTRUCT_COMPILER && (! RUN_TIME)
#include <stdio.h>
#include "conscomp.h"
#include "dffctdef.h"
#include "envrnmnt.h"
#include "dffctcmp.h"
/***************************************/
/* LOCAL INTERNAL FUNCTION DEFINITIONS */
/***************************************/
static bool ConstructToCode(Environment *,const char *,const char *,char *,
unsigned int,FILE *,unsigned int,unsigned int);
static void DeffactsToCode(Environment *,FILE *,Deffacts *,
unsigned int,unsigned int,unsigned int);
static void DeffactsModuleToCode(Environment *,FILE *,Defmodule *,
unsigned int,unsigned int,unsigned int);
static void CloseDeffactsFiles(Environment *,FILE *,FILE *,unsigned int);
static void BeforeDeffactsToCode(Environment *);
static void InitDeffactsCode(Environment *,FILE *,unsigned,unsigned int);
/*************************************************************/
/* DeffactsCompilerSetup: Initializes the deffacts construct */
/* for use with the constructs-to-c command. */
/*************************************************************/
void DeffactsCompilerSetup(
Environment *theEnv)
{
DeffactsData(theEnv)->DeffactsCodeItem =
AddCodeGeneratorItem(theEnv,"deffacts",0,BeforeDeffactsToCode,
InitDeffactsCode,ConstructToCode,2);
}
/*************************************************************/
/* BeforeDeffactsToCode: Assigns each deffacts a unique ID */
/* which will be used for pointer references when the data */
/* structures are written to a file as C code */
/*************************************************************/
static void BeforeDeffactsToCode(
Environment *theEnv)
{
MarkConstructBsaveIDs(theEnv,DeffactsData(theEnv)->DeffactsModuleIndex);
}
/***********************************************/
/* InitDeffactsCode: Writes out initialization */
/* code for deffacts for a run-time module. */
/***********************************************/
static void InitDeffactsCode(
Environment *theEnv,
FILE *initFP,
unsigned imageID,
unsigned maxIndices)
{
#if MAC_XCD
#pragma unused(maxIndices)
#pragma unused(imageID)
#pragma unused(theEnv)
#endif
fprintf(initFP," DeffactsRunTimeInitialize(theEnv);\n");
}
/**********************************************************/
/* ConstructToCode: Produces deffacts code for a run-time */
/* module created using the constructs-to-c function. */
/**********************************************************/
static bool ConstructToCode(
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;
Deffacts *theDeffacts;
unsigned int moduleCount = 0, moduleArrayCount = 0, moduleArrayVersion = 1;
unsigned int deffactsArrayCount = 0, deffactsArrayVersion = 1;
FILE *moduleFile = NULL, *deffactsFile = NULL;
/*===============================================*/
/* Include the appropriate deffacts header file. */
/*===============================================*/
fprintf(headerFP,"#include \"dffctdef.h\"\n");
/*=================================================================*/
/* Loop through all the modules and all the deffacts writing their */
/* C code representation to the file as they are traversed. */
/*=================================================================*/
for (theModule = GetNextDefmodule(theEnv,NULL);
theModule != NULL;
theModule = GetNextDefmodule(theEnv,theModule))
{
SetCurrentModule(theEnv,theModule);
moduleFile = OpenFileIfNeeded(theEnv,moduleFile,fileName,pathName,fileNameBuffer,fileID,imageID,&fileCount,
moduleArrayVersion,headerFP,
"struct deffactsModule",ModulePrefix(DeffactsData(theEnv)->DeffactsCodeItem),
false,NULL);
if (moduleFile == NULL)
{
CloseDeffactsFiles(theEnv,moduleFile,deffactsFile,maxIndices);
return false;
}
DeffactsModuleToCode(theEnv,moduleFile,theModule,imageID,maxIndices,moduleCount);
moduleFile = CloseFileIfNeeded(theEnv,moduleFile,&moduleArrayCount,&moduleArrayVersion,
maxIndices,NULL,NULL);
/*===================================================*/
/* Loop through each of the deffacts in this module. */
/*===================================================*/
for (theDeffacts = GetNextDeffacts(theEnv,NULL);
theDeffacts != NULL;
theDeffacts = GetNextDeffacts(theEnv,theDeffacts))
{
deffactsFile = OpenFileIfNeeded(theEnv,deffactsFile,fileName,pathName,fileNameBuffer,fileID,imageID,&fileCount,
deffactsArrayVersion,headerFP,
"Deffacts",ConstructPrefix(DeffactsData(theEnv)->DeffactsCodeItem),
false,NULL);
if (deffactsFile == NULL)
{
CloseDeffactsFiles(theEnv,moduleFile,deffactsFile,maxIndices);
return false;
}
DeffactsToCode(theEnv,deffactsFile,theDeffacts,imageID,maxIndices,moduleCount);
deffactsArrayCount++;
deffactsFile = CloseFileIfNeeded(theEnv,deffactsFile,&deffactsArrayCount,
&deffactsArrayVersion,maxIndices,NULL,NULL);
}
moduleCount++;
moduleArrayCount++;
}
CloseDeffactsFiles(theEnv,moduleFile,deffactsFile,maxIndices);
return true;
}
/*********************************************************/
/* CloseDeffactsFiles: Closes all of the C files created */
/* for deffacts. Called when an error occurs or when */
/* the deffacts have all been written to the files. */
/*********************************************************/
static void CloseDeffactsFiles(
Environment *theEnv,
FILE *moduleFile,
FILE *deffactsFile,
unsigned maxIndices)
{
unsigned int count = maxIndices;
unsigned int arrayVersion = 0;
if (deffactsFile != NULL)
{
count = maxIndices;
CloseFileIfNeeded(theEnv,deffactsFile,&count,&arrayVersion,maxIndices,NULL,NULL);
}
if (moduleFile != NULL)
{
count = maxIndices;
CloseFileIfNeeded(theEnv,moduleFile,&count,&arrayVersion,maxIndices,NULL,NULL);
}
}
/**********************************************************/
/* DeffactsModuleToCode: Writes the C code representation */
/* of a single deffacts module to the specified file. */
/**********************************************************/
static void DeffactsModuleToCode(
Environment *theEnv,
FILE *theFile,
Defmodule *theModule,
unsigned int imageID,
unsigned int maxIndices,
unsigned int moduleCount)
{
#if MAC_XCD
#pragma unused(moduleCount)
#endif
fprintf(theFile,"{");
ConstructModuleToCode(theEnv,theFile,theModule,imageID,maxIndices,
DeffactsData(theEnv)->DeffactsModuleIndex,
ConstructPrefix(DeffactsData(theEnv)->DeffactsCodeItem));
fprintf(theFile,"}");
}
/*********************************************************/
/* DeffactsToCode: Writes the C code representation of a */
/* single deffacts construct to the specified file. */
/*********************************************************/
static void DeffactsToCode(
Environment *theEnv,
FILE *theFile,
Deffacts *theDeffacts,
unsigned int imageID,
unsigned int maxIndices,
unsigned int moduleCount)
{
/*=================*/
/* Deffacts Header */
/*=================*/
fprintf(theFile,"{");
ConstructHeaderToCode(theEnv,theFile,&theDeffacts->header,imageID,maxIndices,
moduleCount,ModulePrefix(DeffactsData(theEnv)->DeffactsCodeItem),
ConstructPrefix(DeffactsData(theEnv)->DeffactsCodeItem));
fprintf(theFile,",");
/*=============*/
/* Assert List */
/*=============*/
ExpressionToCode(theEnv,theFile,theDeffacts->assertList);
fprintf(theFile,"}");
}
/**************************************************************/
/* DeffactsCModuleReference: Writes the C code representation */
/* of a reference to a deffacts module data structure. */
/**************************************************************/
void DeffactsCModuleReference(
Environment *theEnv,
FILE *theFile,
unsigned long count,
unsigned int imageID,
unsigned int maxIndices)
{
fprintf(theFile,"MIHS &%s%u_%lu[%lu]",
ModulePrefix(DeffactsData(theEnv)->DeffactsCodeItem),
imageID,
(count / maxIndices) + 1,
(count % maxIndices));
}
#endif /* DEFFACTS_CONSTRUCT && CONSTRUCT_COMPILER && (! RUN_TIME) */