1 /*******************************************************/
2 /* "C" Language Integrated Production System */
4 /* CLIPS Version 6.30 08/16/14 */
7 /*******************************************************/
9 /*************************************************************/
10 /* Purpose: Definstances Construct Compiler Code */
12 /* Principal Programmer(s): */
15 /* Contributing Programmer(s): */
17 /* Revision History: */
19 /* 6.30: Added support for path name argument to */
20 /* constructs-to-c. */
22 /* Added const qualifiers to remove C++ */
23 /* deprecation warnings. */
25 /*************************************************************/
27 /* =========================================
28 *****************************************
30 =========================================
31 ***************************************** */
34 #if DEFINSTANCES_CONSTRUCT && CONSTRUCT_COMPILER && (! RUN_TIME)
40 #define _DFINSCMP_SOURCE_
43 /* =========================================
44 *****************************************
45 INTERNALLY VISIBLE FUNCTION HEADERS
46 =========================================
47 ***************************************** */
49 static void ReadyDefinstancesForCode(void *);
50 static int DefinstancesToCode(void *,const char *,const char *,char *,int,FILE *,int,int);
51 static void CloseDefinstancesFiles(void *,FILE *,FILE *,int);
52 static void DefinstancesModuleToCode(void *,FILE *,struct defmodule *,int,int);
53 static void SingleDefinstancesToCode(void *,FILE *,DEFINSTANCES *,int,int,int);
55 /* =========================================
56 *****************************************
57 EXTERNALLY VISIBLE FUNCTIONS
58 =========================================
59 ***************************************** */
61 /***************************************************
62 NAME : SetupDefinstancesCompiler
63 DESCRIPTION : Initializes the construct compiler
66 RETURNS : Nothing useful
67 SIDE EFFECTS : Code generator item initialized
69 ***************************************************/
70 globle void SetupDefinstancesCompiler(
73 DefinstancesData(theEnv)->DefinstancesCodeItem = AddCodeGeneratorItem(theEnv,"definstances",0,ReadyDefinstancesForCode,
74 NULL,DefinstancesToCode,2);
78 /****************************************************
79 NAME : DefinstancesCModuleReference
80 DESCRIPTION : Prints out a reference to a
82 INPUTS : 1) The output file
83 2) The id of the module item
84 3) The id of the image
85 4) The maximum number of elements
87 RETURNS : Nothing useful
88 SIDE EFFECTS : Definstances module reference printed
90 ****************************************************/
91 globle void DefinstancesCModuleReference(
98 fprintf(theFile,"MIHS &%s%d_%d[%d]",
99 ModulePrefix(DefinstancesData(theEnv)->DefinstancesCodeItem),
101 (count / maxIndices) + 1,
102 (count % maxIndices));
105 /* =========================================
106 *****************************************
107 INTERNALLY VISIBLE FUNCTIONS
108 =========================================
109 ***************************************** */
111 /***************************************************
112 NAME : ReadyDefinstancesForCode
113 DESCRIPTION : Sets index of deffunctions
114 for use in compiled expressions
116 RETURNS : Nothing useful
117 SIDE EFFECTS : BsaveIndices set
119 ***************************************************/
120 static void ReadyDefinstancesForCode(
123 MarkConstructBsaveIDs(theEnv,DefinstancesData(theEnv)->DefinstancesModuleIndex);
126 /*******************************************************
127 NAME : DefinstancesToCode
128 DESCRIPTION : Writes out static array code for
130 INPUTS : 1) The base name of the construct set
131 2) The base id for this construct
132 3) The file pointer for the header file
133 4) The base id for the construct set
134 5) The max number of indices allowed
136 RETURNS : -1 if no definstances, 0 on errors,
137 1 if definstances written
138 SIDE EFFECTS : Code written to files
140 *******************************************************/
141 static int DefinstancesToCode(
143 const char *fileName,
144 const char *pathName,
145 char *fileNameBuffer,
152 struct defmodule *theModule;
153 DEFINSTANCES *theDefinstances;
154 int moduleCount = 0, moduleArrayCount = 0, moduleArrayVersion = 1;
155 int definstancesArrayCount = 0, definstancesArrayVersion = 1;
156 FILE *moduleFile = NULL, *definstancesFile = NULL;
158 /* ================================================
159 Include the appropriate definstances header file
160 ================================================ */
161 fprintf(headerFP,"#include \"defins.h\"\n");
163 /* =============================================================
164 Loop through all the modules and all the definstances writing
165 their C code representation to the file as they are traversed
166 ============================================================= */
167 theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,NULL);
169 while (theModule != NULL)
171 EnvSetCurrentModule(theEnv,(void *) theModule);
173 moduleFile = OpenFileIfNeeded(theEnv,moduleFile,fileName,pathName,fileNameBuffer,fileID,imageID,&fileCount,
174 moduleArrayVersion,headerFP,
175 "DEFINSTANCES_MODULE",ModulePrefix(DefinstancesData(theEnv)->DefinstancesCodeItem),
178 if (moduleFile == NULL)
180 CloseDefinstancesFiles(theEnv,moduleFile,definstancesFile,maxIndices);
184 DefinstancesModuleToCode(theEnv,moduleFile,theModule,imageID,maxIndices);
185 moduleFile = CloseFileIfNeeded(theEnv,moduleFile,&moduleArrayCount,&moduleArrayVersion,
186 maxIndices,NULL,NULL);
188 theDefinstances = (DEFINSTANCES *) EnvGetNextDefinstances(theEnv,NULL);
190 while (theDefinstances != NULL)
192 definstancesFile = OpenFileIfNeeded(theEnv,definstancesFile,fileName,pathName,fileNameBuffer,fileID,imageID,&fileCount,
193 definstancesArrayVersion,headerFP,
194 "DEFINSTANCES",ConstructPrefix(DefinstancesData(theEnv)->DefinstancesCodeItem),
196 if (definstancesFile == NULL)
198 CloseDefinstancesFiles(theEnv,moduleFile,definstancesFile,maxIndices);
202 SingleDefinstancesToCode(theEnv,definstancesFile,theDefinstances,imageID,
203 maxIndices,moduleCount);
204 definstancesArrayCount++;
205 definstancesFile = CloseFileIfNeeded(theEnv,definstancesFile,&definstancesArrayCount,
206 &definstancesArrayVersion,maxIndices,NULL,NULL);
208 theDefinstances = (DEFINSTANCES *) EnvGetNextDefinstances(theEnv,theDefinstances);
211 theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,theModule);
216 CloseDefinstancesFiles(theEnv,moduleFile,definstancesFile,maxIndices);
221 /***************************************************
222 NAME : CloseDefinstancesFiles
223 DESCRIPTION : Closes construct compiler files
224 for definstances structures
225 INPUTS : 1) The definstances module file
226 2) The definstances structure file
227 3) The maximum number of indices
229 RETURNS : Nothing useful
230 SIDE EFFECTS : Files closed
232 ***************************************************/
233 static void CloseDefinstancesFiles(
236 FILE *definstancesFile,
239 int count = maxIndices;
240 int arrayVersion = 0;
242 if (definstancesFile != NULL)
245 CloseFileIfNeeded(theEnv,definstancesFile,&count,&arrayVersion,
246 maxIndices,NULL,NULL);
249 if (moduleFile != NULL)
252 CloseFileIfNeeded(theEnv,moduleFile,&count,&arrayVersion,maxIndices,NULL,NULL);
256 /***************************************************
257 NAME : DefinstancesModuleToCode
258 DESCRIPTION : Writes out the C values for a
259 definstances module item
260 INPUTS : 1) The output file
261 2) The module for the definstances
262 3) The compile image id
263 4) The maximum number of elements
265 RETURNS : Nothing useful
266 SIDE EFFECTS : Definstances module item written
268 ***************************************************/
269 static void DefinstancesModuleToCode(
272 struct defmodule *theModule,
276 fprintf(theFile,"{");
277 ConstructModuleToCode(theEnv,theFile,theModule,imageID,maxIndices,
278 DefinstancesData(theEnv)->DefinstancesModuleIndex,ConstructPrefix(DefinstancesData(theEnv)->DefinstancesCodeItem));
279 fprintf(theFile,"}");
282 /***************************************************
283 NAME : SingleDefinstancesToCode
284 DESCRIPTION : Writes out a single definstances'
286 INPUTS : 1) The output file
288 3) The compile image id
289 4) The maximum number of
292 RETURNS : Nothing useful
293 SIDE EFFECTS : Definstances data written
295 ***************************************************/
296 static void SingleDefinstancesToCode(
299 DEFINSTANCES *theDefinstances,
304 /* ===================
306 =================== */
308 fprintf(theFile,"{");
309 ConstructHeaderToCode(theEnv,theFile,&theDefinstances->header,imageID,maxIndices,moduleCount,
310 ModulePrefix(DefinstancesData(theEnv)->DefinstancesCodeItem),
311 ConstructPrefix(DefinstancesData(theEnv)->DefinstancesCodeItem));
313 /* ==========================
314 Definstances specific data
315 ========================== */
316 fprintf(theFile,",0,");
317 ExpressionToCode(theEnv,theFile,theDefinstances->mkinstance);
318 fprintf(theFile,"}");