tizen 2.4 release
[external/clips.git] / src / dfinscmp.c
1    /*******************************************************/
2    /*      "C" Language Integrated Production System      */
3    /*                                                     */
4    /*              CLIPS Version 6.30  08/16/14           */
5    /*                                                     */
6    /*                                                     */
7    /*******************************************************/
8
9 /*************************************************************/
10 /* Purpose: Definstances Construct Compiler Code             */
11 /*                                                           */
12 /* Principal Programmer(s):                                  */
13 /*      Brian L. Dantes                                      */
14 /*                                                           */
15 /* Contributing Programmer(s):                               */
16 /*                                                           */
17 /* Revision History:                                         */
18 /*                                                           */
19 /*      6.30: Added support for path name argument to        */
20 /*            constructs-to-c.                               */
21 /*                                                           */
22 /*            Added const qualifiers to remove C++           */
23 /*            deprecation warnings.                          */
24 /*                                                           */
25 /*************************************************************/
26
27 /* =========================================
28    *****************************************
29                EXTERNAL DEFINITIONS
30    =========================================
31    ***************************************** */
32 #include "setup.h"
33
34 #if DEFINSTANCES_CONSTRUCT && CONSTRUCT_COMPILER && (! RUN_TIME)
35
36 #include "conscomp.h"
37 #include "envrnmnt.h"
38 #include "defins.h"
39
40 #define _DFINSCMP_SOURCE_
41 #include "dfinscmp.h"
42
43 /* =========================================
44    *****************************************
45       INTERNALLY VISIBLE FUNCTION HEADERS
46    =========================================
47    ***************************************** */
48
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);
54
55 /* =========================================
56    *****************************************
57           EXTERNALLY VISIBLE FUNCTIONS
58    =========================================
59    ***************************************** */
60
61 /***************************************************
62   NAME         : SetupDefinstancesCompiler
63   DESCRIPTION  : Initializes the construct compiler
64                    item for definstances
65   INPUTS       : None
66   RETURNS      : Nothing useful
67   SIDE EFFECTS : Code generator item initialized
68   NOTES        : None
69  ***************************************************/
70 globle void SetupDefinstancesCompiler(
71   void *theEnv)
72   {
73    DefinstancesData(theEnv)->DefinstancesCodeItem = AddCodeGeneratorItem(theEnv,"definstances",0,ReadyDefinstancesForCode,
74                                                NULL,DefinstancesToCode,2);
75   }
76
77
78 /****************************************************
79   NAME         : DefinstancesCModuleReference
80   DESCRIPTION  : Prints out a reference to a
81                  definstances module
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
86                     allowed in an array
87   RETURNS      : Nothing useful
88   SIDE EFFECTS : Definstances module reference printed
89   NOTES        : None
90  ****************************************************/
91 globle void DefinstancesCModuleReference(
92   void *theEnv,
93   FILE *theFile,
94   int count,
95   int imageID,
96   int maxIndices)
97   {
98    fprintf(theFile,"MIHS &%s%d_%d[%d]",
99                       ModulePrefix(DefinstancesData(theEnv)->DefinstancesCodeItem),
100                       imageID,
101                       (count / maxIndices) + 1,
102                       (count % maxIndices));
103   }
104
105 /* =========================================
106    *****************************************
107           INTERNALLY VISIBLE FUNCTIONS
108    =========================================
109    ***************************************** */
110
111 /***************************************************
112   NAME         : ReadyDefinstancesForCode
113   DESCRIPTION  : Sets index of deffunctions
114                    for use in compiled expressions
115   INPUTS       : None
116   RETURNS      : Nothing useful
117   SIDE EFFECTS : BsaveIndices set
118   NOTES        : None
119  ***************************************************/
120 static void ReadyDefinstancesForCode(
121   void *theEnv)
122   {
123    MarkConstructBsaveIDs(theEnv,DefinstancesData(theEnv)->DefinstancesModuleIndex);
124   }
125
126 /*******************************************************
127   NAME         : DefinstancesToCode
128   DESCRIPTION  : Writes out static array code for
129                    definstances
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
135                     in an array
136   RETURNS      : -1 if no definstances, 0 on errors,
137                   1 if definstances written
138   SIDE EFFECTS : Code written to files
139   NOTES        : None
140  *******************************************************/
141 static int DefinstancesToCode(
142   void *theEnv,
143   const char *fileName,
144   const char *pathName,
145   char *fileNameBuffer,
146   int fileID,
147   FILE *headerFP,
148   int imageID,
149   int maxIndices)
150   {
151    int fileCount = 1;
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;
157
158    /* ================================================
159       Include the appropriate definstances header file
160       ================================================ */
161    fprintf(headerFP,"#include \"defins.h\"\n");
162
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);
168
169    while (theModule != NULL)
170      {
171       EnvSetCurrentModule(theEnv,(void *) theModule);
172
173       moduleFile = OpenFileIfNeeded(theEnv,moduleFile,fileName,pathName,fileNameBuffer,fileID,imageID,&fileCount,
174                                     moduleArrayVersion,headerFP,
175                                     "DEFINSTANCES_MODULE",ModulePrefix(DefinstancesData(theEnv)->DefinstancesCodeItem),
176                                     FALSE,NULL);
177
178       if (moduleFile == NULL)
179         {
180          CloseDefinstancesFiles(theEnv,moduleFile,definstancesFile,maxIndices);
181          return(0);
182         }
183
184       DefinstancesModuleToCode(theEnv,moduleFile,theModule,imageID,maxIndices);
185       moduleFile = CloseFileIfNeeded(theEnv,moduleFile,&moduleArrayCount,&moduleArrayVersion,
186                                      maxIndices,NULL,NULL);
187
188       theDefinstances = (DEFINSTANCES *) EnvGetNextDefinstances(theEnv,NULL);
189
190       while (theDefinstances != NULL)
191         {
192          definstancesFile = OpenFileIfNeeded(theEnv,definstancesFile,fileName,pathName,fileNameBuffer,fileID,imageID,&fileCount,
193                                              definstancesArrayVersion,headerFP,
194                                              "DEFINSTANCES",ConstructPrefix(DefinstancesData(theEnv)->DefinstancesCodeItem),
195                                              FALSE,NULL);
196          if (definstancesFile == NULL)
197            {
198             CloseDefinstancesFiles(theEnv,moduleFile,definstancesFile,maxIndices);
199             return(0);
200            }
201
202          SingleDefinstancesToCode(theEnv,definstancesFile,theDefinstances,imageID,
203                                   maxIndices,moduleCount);
204          definstancesArrayCount++;
205          definstancesFile = CloseFileIfNeeded(theEnv,definstancesFile,&definstancesArrayCount,
206                                               &definstancesArrayVersion,maxIndices,NULL,NULL);
207
208          theDefinstances = (DEFINSTANCES *) EnvGetNextDefinstances(theEnv,theDefinstances);
209         }
210
211       theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,theModule);
212       moduleCount++;
213       moduleArrayCount++;
214      }
215
216    CloseDefinstancesFiles(theEnv,moduleFile,definstancesFile,maxIndices);
217
218    return(1);
219   }
220
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
228                     allowed in an array
229   RETURNS      : Nothing useful
230   SIDE EFFECTS : Files closed
231   NOTES        : None
232  ***************************************************/
233 static void CloseDefinstancesFiles(
234   void *theEnv,
235   FILE *moduleFile,
236   FILE *definstancesFile,
237   int maxIndices)
238   {
239    int count = maxIndices;
240    int arrayVersion = 0;
241
242    if (definstancesFile != NULL)
243      {
244       count = maxIndices;
245       CloseFileIfNeeded(theEnv,definstancesFile,&count,&arrayVersion,
246                                          maxIndices,NULL,NULL);
247      }
248
249    if (moduleFile != NULL)
250      {
251       count = maxIndices;
252       CloseFileIfNeeded(theEnv,moduleFile,&count,&arrayVersion,maxIndices,NULL,NULL);
253      }
254   }
255
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
264                     in an array
265   RETURNS      : Nothing useful
266   SIDE EFFECTS : Definstances module item written
267   NOTES        : None
268  ***************************************************/
269 static void DefinstancesModuleToCode(
270   void *theEnv,
271   FILE *theFile,
272   struct defmodule *theModule,
273   int imageID,
274   int maxIndices)
275   {
276    fprintf(theFile,"{");
277    ConstructModuleToCode(theEnv,theFile,theModule,imageID,maxIndices,
278                          DefinstancesData(theEnv)->DefinstancesModuleIndex,ConstructPrefix(DefinstancesData(theEnv)->DefinstancesCodeItem));
279    fprintf(theFile,"}");
280   }
281
282 /***************************************************
283   NAME         : SingleDefinstancesToCode
284   DESCRIPTION  : Writes out a single definstances'
285                  data to the file
286   INPUTS       : 1) The output file
287                  2) The definstances
288                  3) The compile image id
289                  4) The maximum number of
290                     elements in an array
291                  5) The module index
292   RETURNS      : Nothing useful
293   SIDE EFFECTS : Definstances data written
294   NOTES        : None
295  ***************************************************/
296 static void SingleDefinstancesToCode(
297   void *theEnv,
298   FILE *theFile,
299   DEFINSTANCES *theDefinstances,
300   int imageID,
301   int maxIndices,
302   int moduleCount)
303   {
304    /* ===================
305       Definstances Header
306       =================== */
307
308    fprintf(theFile,"{");
309    ConstructHeaderToCode(theEnv,theFile,&theDefinstances->header,imageID,maxIndices,moduleCount,
310                          ModulePrefix(DefinstancesData(theEnv)->DefinstancesCodeItem),
311                          ConstructPrefix(DefinstancesData(theEnv)->DefinstancesCodeItem));
312
313    /* ==========================
314       Definstances specific data
315       ========================== */
316    fprintf(theFile,",0,");
317    ExpressionToCode(theEnv,theFile,theDefinstances->mkinstance);
318    fprintf(theFile,"}");
319   }
320
321 #endif