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
/*******************************************************/
/* "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.30: Added support for path name argument to */
/* constructs-to-c. */
/* */
/* 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 DEFFUNCTION_CONSTRUCT && CONSTRUCT_COMPILER && (! RUN_TIME)
#include "conscomp.h"
#include "envrnmnt.h"
#include "dffnxcmp.h"
/***************************************/
/* LOCAL INTERNAL FUNCTION DEFINITIONS */
/***************************************/
static void ReadyDeffunctionsForCode(Environment *);
static bool DeffunctionsToCode(Environment *,const char *,const char *,char *,
unsigned int,FILE *,unsigned int,unsigned int);
static void CloseDeffunctionFiles(Environment *,FILE *,FILE *,unsigned int);
static void DeffunctionModuleToCode(Environment *,FILE *,Defmodule *,unsigned int,unsigned int);
static void SingleDeffunctionToCode(Environment *,FILE *,Deffunction *,
unsigned int,unsigned int,unsigned int);
static void InitDeffunctionCode(Environment *,FILE *,unsigned int,unsigned int);
/* =========================================
*****************************************
EXTERNALLY VISIBLE FUNCTIONS
=========================================
***************************************** */
/***************************************************
NAME : SetupDeffunctionCompiler
DESCRIPTION : Initializes the construct compiler
item for deffunctions
INPUTS : None
RETURNS : Nothing useful
SIDE EFFECTS : Code generator item initialized
NOTES : None
***************************************************/
void SetupDeffunctionCompiler(
Environment *theEnv)
{
DeffunctionData(theEnv)->DeffunctionCodeItem = AddCodeGeneratorItem(theEnv,"deffunctions",0,ReadyDeffunctionsForCode,
InitDeffunctionCode,DeffunctionsToCode,2);
}
/***************************************************
NAME : PrintDeffunctionReference
DESCRIPTION : Prints a reference to the run-time
deffunction array for the construct
compiler
INPUTS : 1) The file output destination
2) A pointer to the deffunction
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 PrintDeffunctionReference(
Environment *theEnv,
FILE *fp,
Deffunction *dfPtr,
unsigned imageID,
unsigned maxIndices)
{
if (dfPtr == NULL)
fprintf(fp,"NULL");
else
fprintf(fp,"&%s%d_%lu[%lu]",ConstructPrefix(DeffunctionData(theEnv)->DeffunctionCodeItem),imageID,
((dfPtr->header.bsaveID / maxIndices) + 1),
(dfPtr->header.bsaveID % maxIndices));
}
/****************************************************
NAME : DeffunctionCModuleReference
DESCRIPTION : Prints out a reference to a
deffunction 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 : Deffunction module reference printed
NOTES : None
****************************************************/
void DeffunctionCModuleReference(
Environment *theEnv,
FILE *theFile,
unsigned long count,
unsigned int imageID,
unsigned int maxIndices)
{
fprintf(theFile,"MIHS &%s%u_%lu[%lu]",
ModulePrefix(DeffunctionData(theEnv)->DeffunctionCodeItem),
imageID,
(count / maxIndices) + 1,
(count % maxIndices));
}
/* =========================================
*****************************************
INTERNALLY VISIBLE FUNCTIONS
=========================================
***************************************** */
/***************************************************
NAME : ReadyDeffunctionsForCode
DESCRIPTION : Sets index of deffunctions
for use in compiled expressions
INPUTS : None
RETURNS : Nothing useful
SIDE EFFECTS : BsaveIndices set
NOTES : None
***************************************************/
static void ReadyDeffunctionsForCode(
Environment *theEnv)
{
MarkConstructBsaveIDs(theEnv,DeffunctionData(theEnv)->DeffunctionModuleIndex);
}
/**************************************************/
/* InitDeffunctionCode: Writes out initialization */
/* code for deffunction for a run-time module. */
/**************************************************/
static void InitDeffunctionCode(
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," DeffunctionRunTimeInitialize(theEnv);\n");
}
/*******************************************************
NAME : DeffunctionsToCode
DESCRIPTION : Writes out static array code for
deffunctions
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 deffunctions written
SIDE EFFECTS : Code written to files
NOTES : None
*******************************************************/
static bool DeffunctionsToCode(
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;
Deffunction *theDeffunction;
unsigned int moduleCount = 0, moduleArrayCount = 0, moduleArrayVersion = 1;
unsigned int deffunctionArrayCount = 0, deffunctionArrayVersion = 1;
FILE *moduleFile = NULL, *deffunctionFile = NULL;
/* ===============================================
Include the appropriate deffunction header file
=============================================== */
fprintf(headerFP,"#include \"dffnxfun.h\"\n");
/* =============================================================
Loop through all the modules and all the deffunctions writing
their C code representation to the file as they are traversed
============================================================= */
theModule = GetNextDefmodule(theEnv,NULL);
while (theModule != NULL)
{
SetCurrentModule(theEnv,theModule);
moduleFile = OpenFileIfNeeded(theEnv,moduleFile,fileName,pathName,fileNameBuffer,fileID,imageID,&fileCount,
moduleArrayVersion,headerFP,
"DeffunctionModuleData",ModulePrefix(DeffunctionData(theEnv)->DeffunctionCodeItem),
false,NULL);
if (moduleFile == NULL)
{
CloseDeffunctionFiles(theEnv,moduleFile,deffunctionFile,maxIndices);
return false;
}
DeffunctionModuleToCode(theEnv,moduleFile,theModule,imageID,maxIndices);
moduleFile = CloseFileIfNeeded(theEnv,moduleFile,&moduleArrayCount,&moduleArrayVersion,
maxIndices,NULL,NULL);
theDeffunction = GetNextDeffunction(theEnv,NULL);
while (theDeffunction != NULL)
{
deffunctionFile = OpenFileIfNeeded(theEnv,deffunctionFile,fileName,pathName,fileNameBuffer,fileID,imageID,&fileCount,
deffunctionArrayVersion,headerFP,
"Deffunction",ConstructPrefix(DeffunctionData(theEnv)->DeffunctionCodeItem),
false,NULL);
if (deffunctionFile == NULL)
{
CloseDeffunctionFiles(theEnv,moduleFile,deffunctionFile,maxIndices);
return false;
}
SingleDeffunctionToCode(theEnv,deffunctionFile,theDeffunction,imageID,
maxIndices,moduleCount);
deffunctionArrayCount++;
deffunctionFile = CloseFileIfNeeded(theEnv,deffunctionFile,&deffunctionArrayCount,
&deffunctionArrayVersion,maxIndices,NULL,NULL);
theDeffunction = GetNextDeffunction(theEnv,theDeffunction);
}
theModule = GetNextDefmodule(theEnv,theModule);
moduleCount++;
moduleArrayCount++;
}
CloseDeffunctionFiles(theEnv,moduleFile,deffunctionFile,maxIndices);
return true;
}
/***************************************************
NAME : CloseDeffunctionFiles
DESCRIPTION : Closes construct compiler files
for deffunction structures
INPUTS : 1) The deffunction module file
2) The deffunction structure file
3) The maximum number of indices
allowed in an array
RETURNS : Nothing useful
SIDE EFFECTS : Files closed
NOTES : None
***************************************************/
static void CloseDeffunctionFiles(
Environment *theEnv,
FILE *moduleFile,
FILE *deffunctionFile,
unsigned int maxIndices)
{
unsigned int count = maxIndices;
unsigned int arrayVersion = 0;
if (deffunctionFile != NULL)
{
count = maxIndices;
CloseFileIfNeeded(theEnv,deffunctionFile,&count,&arrayVersion,maxIndices,NULL,NULL);
}
if (moduleFile != NULL)
{
count = maxIndices;
CloseFileIfNeeded(theEnv,moduleFile,&count,&arrayVersion,maxIndices,NULL,NULL);
}
}
/***************************************************
NAME : DeffunctionModuleToCode
DESCRIPTION : Writes out the C values for a
deffunction module item
INPUTS : 1) The output file
2) The module for the deffunctions
3) The compile image id
4) The maximum number of elements
in an array
RETURNS : Nothing useful
SIDE EFFECTS : Deffunction module item written
NOTES : None
***************************************************/
static void DeffunctionModuleToCode(
Environment *theEnv,
FILE *theFile,
Defmodule *theModule,
unsigned int imageID,
unsigned int maxIndices)
{
fprintf(theFile,"{");
ConstructModuleToCode(theEnv,theFile,theModule,imageID,maxIndices,
DeffunctionData(theEnv)->DeffunctionModuleIndex,ConstructPrefix(DeffunctionData(theEnv)->DeffunctionCodeItem));
fprintf(theFile,"}");
}
/***************************************************
NAME : SingleDeffunctionToCode
DESCRIPTION : Writes out a single deffunction's
data to the file
INPUTS : 1) The output file
2) The deffunction
3) The compile image id
4) The maximum number of
elements in an array
5) The module index
RETURNS : Nothing useful
SIDE EFFECTS : Deffunction data written
NOTES : None
***************************************************/
static void SingleDeffunctionToCode(
Environment *theEnv,
FILE *theFile,
Deffunction *theDeffunction,
unsigned int imageID,
unsigned int maxIndices,
unsigned int moduleCount)
{
/* ==================
Deffunction Header
================== */
fprintf(theFile,"{");
ConstructHeaderToCode(theEnv,theFile,&theDeffunction->header,imageID,maxIndices,moduleCount,
ModulePrefix(DeffunctionData(theEnv)->DeffunctionCodeItem),
ConstructPrefix(DeffunctionData(theEnv)->DeffunctionCodeItem));
/* =========================
Deffunction specific data
========================= */
fprintf(theFile,",0,0,0,");
ExpressionToCode(theEnv,theFile,theDeffunction->code);
fprintf(theFile,",%d,%d,%d",
theDeffunction->minNumberOfParameters,
theDeffunction->maxNumberOfParameters,
theDeffunction->numberOfLocalVars);
fprintf(theFile,"}");
}
#endif