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
/*******************************************************/
/* "C" Language Integrated Production System */
/* */
/* CLIPS Version 6.40 07/30/16 */
/* */
/* DEFGLOBAL CONSTRUCTS-TO-C MODULE */
/*******************************************************/
/*************************************************************/
/* Purpose: Implements the constructs-to-c feature for the */
/* defglobal construct. */
/* */
/* Principal Programmer(s): */
/* Gary D. Riley */
/* */
/* Contributing Programmer(s): */
/* Brian L. Dantes */
/* */
/* Revision History: */
/* */
/* 6.30: Removed conditional code for unsupported */
/* compilers/operating systems (IBM_MCW, */
/* MAC_MCW, and IBM_TBC). */
/* */
/* 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. */
/* */
/*************************************************************/
#include "setup.h"
#if DEFGLOBAL_CONSTRUCT && CONSTRUCT_COMPILER && (! RUN_TIME)
#include <stdio.h>
#include "conscomp.h"
#include "envrnmnt.h"
#include "globldef.h"
#include "globlcmp.h"
/***************************************/
/* LOCAL INTERNAL FUNCTION DEFINITIONS */
/***************************************/
static bool ConstructToCode(Environment *,const char *,const char *,char *,unsigned int,
FILE *,unsigned int,unsigned int);
static void DefglobalToCode(Environment *,FILE *,Defglobal *,
unsigned int,unsigned int,unsigned int);
static void DefglobalModuleToCode(Environment *,FILE *,Defmodule *,unsigned int,unsigned int,unsigned int);
static void CloseDefglobalFiles(Environment *,FILE *,FILE *,unsigned int);
static void BeforeDefglobalsToCode(Environment *);
static void InitDefglobalsCode(Environment *,FILE *,unsigned,unsigned);
/***************************************************************/
/* DefglobalCompilerSetup: Initializes the defglobal construct */
/* for use with the constructs-to-c command. */
/***************************************************************/
void DefglobalCompilerSetup(
Environment *theEnv)
{
DefglobalData(theEnv)->DefglobalCodeItem =
AddCodeGeneratorItem(theEnv,"defglobal",0,BeforeDefglobalsToCode,
InitDefglobalsCode,ConstructToCode,2);
}
/**************************************************************/
/* BeforeDefglobalsToCode: Assigns each defglobal a unique ID */
/* which will be used for pointer references when the data */
/* structures are written to a file as C code */
/**************************************************************/
static void BeforeDefglobalsToCode(
Environment *theEnv)
{
MarkConstructBsaveIDs(theEnv,DefglobalData(theEnv)->DefglobalModuleIndex);
}
/*************************************************/
/* InitDefglobalsCode: Writes out initialization */
/* code for defglobals for a run-time module. */
/*************************************************/
static void InitDefglobalsCode(
Environment *theEnv,
FILE *initFP,
unsigned imageID,
unsigned maxIndices)
{
#if MAC_XCD
#pragma unused(maxIndices)
#pragma unused(imageID)
#pragma unused(theEnv)
#endif
fprintf(initFP," DefglobalRunTimeInitialize(theEnv);\n");
fprintf(initFP," ResetDefglobals(theEnv,NULL);\n");
}
/***********************************************************/
/* ConstructToCode: Produces defglobal 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;
Defglobal *theDefglobal;
unsigned int moduleCount = 0, moduleArrayCount = 0, moduleArrayVersion = 1;
unsigned int defglobalArrayCount = 0, defglobalArrayVersion = 1;
FILE *moduleFile = NULL, *defglobalFile = NULL;
/*================================================*/
/* Include the appropriate defglobal header file. */
/*================================================*/
fprintf(headerFP,"#include \"globldef.h\"\n");
/*===================================================================*/
/* Loop through all the modules and all the defglobals 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 defglobalModule",ModulePrefix(DefglobalData(theEnv)->DefglobalCodeItem),
false,NULL);
if (moduleFile == NULL)
{
CloseDefglobalFiles(theEnv,moduleFile,defglobalFile,maxIndices);
return false;
}
DefglobalModuleToCode(theEnv,moduleFile,theModule,imageID,maxIndices,moduleCount);
moduleFile = CloseFileIfNeeded(theEnv,moduleFile,&moduleArrayCount,&moduleArrayVersion,
maxIndices,NULL,NULL);
for (theDefglobal = GetNextDefglobal(theEnv,NULL);
theDefglobal != NULL;
theDefglobal = GetNextDefglobal(theEnv,theDefglobal))
{
defglobalFile = OpenFileIfNeeded(theEnv,defglobalFile,fileName,pathName,fileNameBuffer,fileID,imageID,&fileCount,
defglobalArrayVersion,headerFP,
"Defglobal",ConstructPrefix(DefglobalData(theEnv)->DefglobalCodeItem),
false,NULL);
if (defglobalFile == NULL)
{
CloseDefglobalFiles(theEnv,moduleFile,defglobalFile,maxIndices);
return false;
}
DefglobalToCode(theEnv,defglobalFile,theDefglobal,imageID,maxIndices,moduleCount);
defglobalArrayCount++;
defglobalFile = CloseFileIfNeeded(theEnv,defglobalFile,&defglobalArrayCount,
&defglobalArrayVersion,maxIndices,NULL,NULL);
}
moduleCount++;
moduleArrayCount++;
}
CloseDefglobalFiles(theEnv,moduleFile,defglobalFile,maxIndices);
return true;
}
/**********************************************************/
/* CloseDefglobalFiles: Closes all of the C files created */
/* for defglobals. Called when an error occurs or when */
/* the defglobals have all been written to the files. */
/**********************************************************/
static void CloseDefglobalFiles(
Environment *theEnv,
FILE *moduleFile,
FILE *defglobalFile,
unsigned int maxIndices)
{
unsigned int count = maxIndices;
unsigned int arrayVersion = 0;
if (defglobalFile != NULL)
{
count = maxIndices;
CloseFileIfNeeded(theEnv,defglobalFile,&count,&arrayVersion,maxIndices,NULL,NULL);
}
if (moduleFile != NULL)
{
count = maxIndices;
CloseFileIfNeeded(theEnv,moduleFile,&count,&arrayVersion,maxIndices,NULL,NULL);
}
}
/***********************************************************/
/* DefglobalModuleToCode: Writes the C code representation */
/* of a single defglobal module to the specified file. */
/***********************************************************/
static void DefglobalModuleToCode(
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,
DefglobalData(theEnv)->DefglobalModuleIndex,ConstructPrefix(DefglobalData(theEnv)->DefglobalCodeItem));
fprintf(theFile,"}");
}
/**********************************************************/
/* DefglobalToCode: Writes the C code representation of a */
/* single defglobal construct to the specified file. */
/**********************************************************/
static void DefglobalToCode(
Environment *theEnv,
FILE *theFile,
Defglobal *theDefglobal,
unsigned int imageID,
unsigned int maxIndices,
unsigned int moduleCount)
{
/*==================*/
/* Defglobal Header */
/*==================*/
fprintf(theFile,"{");
ConstructHeaderToCode(theEnv,theFile,&theDefglobal->header,imageID,maxIndices,
moduleCount,ModulePrefix(DefglobalData(theEnv)->DefglobalCodeItem),
ConstructPrefix(DefglobalData(theEnv)->DefglobalCodeItem));
fprintf(theFile,",");
/*============================================*/
/* Watch Flag, In Scope Flag, and Busy Count. */
/*============================================*/
fprintf(theFile,"0,0,%ld,",theDefglobal->busyCount);
/*================*/
/* Current Value. */
/*================*/
fprintf(theFile,"{ { NULL } }");
/*=====================*/
/* Initial Expression. */
/*=====================*/
fprintf(theFile,",");
PrintHashedExpressionReference(theEnv,theFile,theDefglobal->initial,imageID,maxIndices);
fprintf(theFile,"}");
}
/***************************************************************/
/* DefglobalCModuleReference: Writes the C code representation */
/* of a reference to a defglobal module data structure. */
/***************************************************************/
void DefglobalCModuleReference(
Environment *theEnv,
FILE *theFile,
unsigned long count,
unsigned int imageID,
unsigned int maxIndices)
{
fprintf(theFile,"MIHS &%s%u_%lu[%lu]",
ModulePrefix(DefglobalData(theEnv)->DefglobalCodeItem),
imageID,
(count / maxIndices) + 1,
(count % maxIndices));
}
/******************************************************************/
/* DefglobalCConstructReference: Writes the C code representation */
/* of a reference to a defglobal data structure. */
/******************************************************************/
void DefglobalCConstructReference(
Environment *theEnv,
FILE *theFile,
Defglobal *theGlobal,
unsigned int imageID,
unsigned int maxIndices)
{
if (theGlobal == NULL)
{ fprintf(theFile,"NULL"); }
else
{
fprintf(theFile,"&%s%u_%lu[%lu]",ConstructPrefix(DefglobalData(theEnv)->DefglobalCodeItem),
imageID,
(theGlobal->header.bsaveID / maxIndices) + 1,
theGlobal->header.bsaveID % maxIndices);
}
}
#endif /* DEFGLOBAL_CONSTRUCT && CONSTRUCT_COMPILER && (! RUN_TIME) */