1 /*******************************************************/
2 /* "C" Language Integrated Production System */
4 /* CLIPS Version 6.30 08/16/14 */
6 /* DEFTEMPLATE CONSTRUCTS-TO-C MODULE */
7 /*******************************************************/
9 /*************************************************************/
10 /* Purpose: Implements the constructs-to-c feature for the */
11 /* deftemplate construct. */
13 /* Principal Programmer(s): */
16 /* Contributing Programmer(s): */
19 /* Revision History: */
21 /* 6.23: Added support for templates maintaining their */
22 /* own list of facts. */
24 /* 6.30: Added support for path name argument to */
25 /* constructs-to-c. */
27 /* Removed conditional code for unsupported */
28 /* compilers/operating systems (IBM_MCW, */
29 /* MAC_MCW, and IBM_TBC). */
31 /* Support for deftemplate slot facets. */
33 /* Added code for deftemplate run time */
34 /* initialization of hashed comparisons to */
37 /* Added const qualifiers to remove C++ */
38 /* deprecation warnings. */
40 /*************************************************************/
42 #define _TMPLTCMP_SOURCE_
46 #if DEFTEMPLATE_CONSTRUCT && CONSTRUCT_COMPILER && (! RUN_TIME)
48 #define SlotPrefix() ArbitraryPrefix(DeftemplateData(theEnv)->DeftemplateCodeItem,2)
51 #define _STDIO_INCLUDED_
61 /***************************************/
62 /* LOCAL INTERNAL FUNCTION DEFINITIONS */
63 /***************************************/
65 static int ConstructToCode(void *,const char *,const char *,char *,int,FILE *,int,int);
66 static void SlotToCode(void *,FILE *,struct templateSlot *,int,int,int);
67 static void DeftemplateModuleToCode(void *,FILE *,struct defmodule *,int,int,int);
68 static void DeftemplateToCode(void *,FILE *,struct deftemplate *,
70 static void CloseDeftemplateFiles(void *,FILE *,FILE *,FILE *,int);
71 static void InitDeftemplateCode(void *,FILE *,int,int);
73 /*********************************************************/
74 /* DeftemplateCompilerSetup: Initializes the deftemplate */
75 /* construct for use with the constructs-to-c command. */
76 /*********************************************************/
77 globle void DeftemplateCompilerSetup(
80 DeftemplateData(theEnv)->DeftemplateCodeItem = AddCodeGeneratorItem(theEnv,"deftemplate",0,NULL,InitDeftemplateCode,ConstructToCode,3);
83 /*************************************************************/
84 /* ConstructToCode: Produces deftemplate code for a run-time */
85 /* module created using the constructs-to-c function. */
86 /*************************************************************/
87 static int ConstructToCode(
98 struct defmodule *theModule;
99 struct deftemplate *theTemplate;
100 struct templateSlot *slotPtr;
101 int slotCount = 0, slotArrayCount = 0, slotArrayVersion = 1;
102 int moduleCount = 0, moduleArrayCount = 0, moduleArrayVersion = 1;
103 int templateArrayCount = 0, templateArrayVersion = 1;
104 FILE *slotFile = NULL, *moduleFile = NULL, *templateFile = NULL;
106 /*==================================================*/
107 /* Include the appropriate deftemplate header file. */
108 /*==================================================*/
110 fprintf(headerFP,"#include \"tmpltdef.h\"\n");
112 /*=============================================================*/
113 /* Loop through all the modules, all the deftemplates, and all */
114 /* the deftemplate slots writing their C code representation */
115 /* to the file as they are traversed. */
116 /*=============================================================*/
118 theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,NULL);
120 while (theModule != NULL)
122 EnvSetCurrentModule(theEnv,(void *) theModule);
124 moduleFile = OpenFileIfNeeded(theEnv,moduleFile,fileName,pathName,fileNameBuffer,fileID,imageID,&fileCount,
125 moduleArrayVersion,headerFP,
126 "struct deftemplateModule",ModulePrefix(DeftemplateData(theEnv)->DeftemplateCodeItem),
129 if (moduleFile == NULL)
131 CloseDeftemplateFiles(theEnv,moduleFile,templateFile,slotFile,maxIndices);
135 DeftemplateModuleToCode(theEnv,moduleFile,theModule,imageID,maxIndices,moduleCount);
136 moduleFile = CloseFileIfNeeded(theEnv,moduleFile,&moduleArrayCount,&moduleArrayVersion,
137 maxIndices,NULL,NULL);
139 /*=======================================================*/
140 /* Loop through each of the deftemplates in this module. */
141 /*=======================================================*/
143 theTemplate = (struct deftemplate *) EnvGetNextDeftemplate(theEnv,NULL);
145 while (theTemplate != NULL)
147 templateFile = OpenFileIfNeeded(theEnv,templateFile,fileName,pathName,fileNameBuffer,fileID,imageID,&fileCount,
148 templateArrayVersion,headerFP,
149 "struct deftemplate",ConstructPrefix(DeftemplateData(theEnv)->DeftemplateCodeItem),
151 if (templateFile == NULL)
153 CloseDeftemplateFiles(theEnv,moduleFile,templateFile,slotFile,maxIndices);
157 DeftemplateToCode(theEnv,templateFile,theTemplate,imageID,maxIndices,
158 moduleCount,slotCount);
159 templateArrayCount++;
160 templateFile = CloseFileIfNeeded(theEnv,templateFile,&templateArrayCount,&templateArrayVersion,
161 maxIndices,NULL,NULL);
163 /*======================================================*/
164 /* Loop through each of the slots for this deftemplate. */
165 /*======================================================*/
167 slotPtr = theTemplate->slotList;
168 while (slotPtr != NULL)
170 slotFile = OpenFileIfNeeded(theEnv,slotFile,fileName,pathName,fileNameBuffer,fileID,imageID,&fileCount,
171 slotArrayVersion,headerFP,
172 "struct templateSlot",SlotPrefix(),FALSE,NULL);
173 if (slotFile == NULL)
175 CloseDeftemplateFiles(theEnv,moduleFile,templateFile,slotFile,maxIndices);
179 SlotToCode(theEnv,slotFile,slotPtr,imageID,maxIndices,slotCount);
182 slotFile = CloseFileIfNeeded(theEnv,slotFile,&slotArrayCount,&slotArrayVersion,
183 maxIndices,NULL,NULL);
184 slotPtr = slotPtr->next;
187 theTemplate = (struct deftemplate *) EnvGetNextDeftemplate(theEnv,theTemplate);
190 theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,theModule);
196 CloseDeftemplateFiles(theEnv,moduleFile,templateFile,slotFile,maxIndices);
201 /************************************************************/
202 /* CloseDeftemplateFiles: Closes all of the C files created */
203 /* for deftemplates. Called when an error occurs or when */
204 /* the deftemplates have all been written to the files. */
205 /************************************************************/
206 static void CloseDeftemplateFiles(
213 int count = maxIndices;
214 int arrayVersion = 0;
216 if (slotFile != NULL)
219 CloseFileIfNeeded(theEnv,slotFile,&count,&arrayVersion,maxIndices,NULL,NULL);
222 if (templateFile != NULL)
225 CloseFileIfNeeded(theEnv,templateFile,&count,&arrayVersion,maxIndices,NULL,NULL);
228 if (moduleFile != NULL)
231 CloseFileIfNeeded(theEnv,moduleFile,&count,&arrayVersion,maxIndices,NULL,NULL);
235 /*************************************************************/
236 /* DeftemplateModuleToCode: Writes the C code representation */
237 /* of a single deftemplate module to the specified file. */
238 /*************************************************************/
239 static void DeftemplateModuleToCode(
242 struct defmodule *theModule,
248 #pragma unused(moduleCount)
251 fprintf(theFile,"{");
253 ConstructModuleToCode(theEnv,theFile,theModule,imageID,maxIndices,
254 DeftemplateData(theEnv)->DeftemplateModuleIndex,ConstructPrefix(DeftemplateData(theEnv)->DeftemplateCodeItem));
256 fprintf(theFile,"}");
259 /************************************************************/
260 /* DeftemplateToCode: Writes the C code representation of a */
261 /* single deftemplate construct to the specified file. */
262 /************************************************************/
263 static void DeftemplateToCode(
266 struct deftemplate *theTemplate,
272 /*====================*/
273 /* Deftemplate Header */
274 /*====================*/
276 fprintf(theFile,"{");
278 ConstructHeaderToCode(theEnv,theFile,&theTemplate->header,imageID,maxIndices,
279 moduleCount,ModulePrefix(DeftemplateData(theEnv)->DeftemplateCodeItem),
280 ConstructPrefix(DeftemplateData(theEnv)->DeftemplateCodeItem));
281 fprintf(theFile,",");
287 if (theTemplate->slotList == NULL)
288 { fprintf(theFile,"NULL,"); }
291 fprintf(theFile,"&%s%d_%d[%d],",SlotPrefix(),
293 (slotCount / maxIndices) + 1,
294 slotCount % maxIndices);
297 /*==========================================*/
298 /* Implied Flag, Watch Flag, In Scope Flag, */
299 /* Number of Slots, and Busy Count. */
300 /*==========================================*/
302 fprintf(theFile,"%d,0,0,%d,%ld,",theTemplate->implied,theTemplate->numberOfSlots,theTemplate->busyCount);
304 /*=================*/
305 /* Pattern Network */
306 /*=================*/
308 if (theTemplate->patternNetwork == NULL)
309 { fprintf(theFile,"NULL"); }
311 { FactPatternNodeReference(theEnv,theTemplate->patternNetwork,theFile,imageID,maxIndices); }
313 /*============================================*/
314 /* Print the factList and lastFact references */
315 /* and close the structure. */
316 /*============================================*/
318 fprintf(theFile,",NULL,NULL}");
321 /*****************************************************/
322 /* SlotToCode: Writes the C code representation of a */
323 /* single deftemplate slot to the specified file. */
324 /*****************************************************/
325 static void SlotToCode(
328 struct templateSlot *theSlot,
337 fprintf(theFile,"{");
338 PrintSymbolReference(theEnv,theFile,theSlot->slotName);
340 /*=============================*/
341 /* Multislot and Default Flags */
342 /*=============================*/
344 fprintf(theFile,",%d,%d,%d,%d,",theSlot->multislot,theSlot->noDefault,
345 theSlot->defaultPresent,theSlot->defaultDynamic);
351 PrintConstraintReference(theEnv,theFile,theSlot->constraints,imageID,maxIndices);
357 fprintf(theFile,",");
358 PrintHashedExpressionReference(theEnv,theFile,theSlot->defaultList,imageID,maxIndices);
364 fprintf(theFile,",");
365 PrintHashedExpressionReference(theEnv,theFile,theSlot->facetList,imageID,maxIndices);
366 fprintf(theFile,",");
372 if (theSlot->next == NULL)
373 { fprintf(theFile,"NULL}"); }
376 fprintf(theFile,"&%s%d_%d[%d]}",SlotPrefix(),imageID,
377 ((slotCount+1) / maxIndices) + 1,
378 (slotCount+1) % maxIndices);
382 /*****************************************************************/
383 /* DeftemplateCModuleReference: Writes the C code representation */
384 /* of a reference to a deftemplate module data structure. */
385 /*****************************************************************/
386 globle void DeftemplateCModuleReference(
393 fprintf(theFile,"MIHS &%s%d_%d[%d]",ModulePrefix(DeftemplateData(theEnv)->DeftemplateCodeItem),
395 (count / maxIndices) + 1,
396 (count % maxIndices));
399 /********************************************************************/
400 /* DeftemplateCConstructReference: Writes the C code representation */
401 /* of a reference to a deftemplate data structure. */
402 /********************************************************************/
403 globle void DeftemplateCConstructReference(
410 struct deftemplate *theTemplate = (struct deftemplate *) vTheTemplate;
412 if (theTemplate == NULL)
413 { fprintf(theFile,"NULL"); }
416 fprintf(theFile,"&%s%d_%ld[%ld]",ConstructPrefix(DeftemplateData(theEnv)->DeftemplateCodeItem),
418 (theTemplate->header.bsaveID / maxIndices) + 1,
419 theTemplate->header.bsaveID % maxIndices);
424 /*******************************************/
425 /* InitDeftemplateCode: Writes out runtime */
426 /* initialization code for deftemplates. */
427 /*******************************************/
428 static void InitDeftemplateCode(
435 #pragma unused(theEnv)
436 #pragma unused(imageID)
437 #pragma unused(maxIndices)
440 fprintf(initFP," DeftemplateRunTimeInitialize(theEnv);\n");
443 #endif /* DEFTEMPLATE_CONSTRUCT && CONSTRUCT_COMPILER && (! RUN_TIME) */