00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #define _TMPLTCMP_SOURCE_
00026
00027 #include "setup.h"
00028
00029 #if DEFTEMPLATE_CONSTRUCT && CONSTRUCT_COMPILER && (! RUN_TIME)
00030
00031 #define SlotPrefix() ArbitraryPrefix(DeftemplateData(theEnv)->DeftemplateCodeItem,2)
00032
00033 #include <stdio.h>
00034 #define _STDIO_INCLUDED_
00035
00036 #include "conscomp.h"
00037 #include "factcmp.h"
00038 #include "cstrncmp.h"
00039 #include "tmpltdef.h"
00040 #include "envrnmnt.h"
00041
00042 #include "tmpltcmp.h"
00043
00044
00045
00046
00047
00048 static int ConstructToCode(void *,char *,char *,char *,int,FILE *,int,int);
00049 static void SlotToCode(void *,FILE *,struct templateSlot *,int,int,int);
00050 static void DeftemplateModuleToCode(void *,FILE *,struct defmodule *,int,int,int);
00051 static void DeftemplateToCode(void *,FILE *,struct deftemplate *,
00052 int,int,int,int);
00053 static void CloseDeftemplateFiles(void *,FILE *,FILE *,FILE *,int);
00054 static void InitDeftemplateCode(void *,FILE *,int,int);
00055
00056
00057
00058
00059
00060 globle void DeftemplateCompilerSetup(
00061 void *theEnv)
00062 {
00063 DeftemplateData(theEnv)->DeftemplateCodeItem = AddCodeGeneratorItem(theEnv,"deftemplate",0,NULL,InitDeftemplateCode,ConstructToCode,3);
00064 }
00065
00066
00067
00068
00069
00070 static int ConstructToCode(
00071 void *theEnv,
00072 char *fileName,
00073 char *pathName,
00074 char *fileNameBuffer,
00075 int fileID,
00076 FILE *headerFP,
00077 int imageID,
00078 int maxIndices)
00079 {
00080 int fileCount = 1;
00081 struct defmodule *theModule;
00082 struct deftemplate *theTemplate;
00083 struct templateSlot *slotPtr;
00084 int slotCount = 0, slotArrayCount = 0, slotArrayVersion = 1;
00085 int moduleCount = 0, moduleArrayCount = 0, moduleArrayVersion = 1;
00086 int templateArrayCount = 0, templateArrayVersion = 1;
00087 FILE *slotFile = NULL, *moduleFile = NULL, *templateFile = NULL;
00088
00089
00090
00091
00092
00093 fprintf(headerFP,"#include \"tmpltdef.h\"\n");
00094
00095
00096
00097
00098
00099
00100
00101 theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,NULL);
00102
00103 while (theModule != NULL)
00104 {
00105 EnvSetCurrentModule(theEnv,(void *) theModule);
00106
00107 moduleFile = OpenFileIfNeeded(theEnv,moduleFile,fileName,pathName,fileNameBuffer,fileID,imageID,&fileCount,
00108 moduleArrayVersion,headerFP,
00109 "struct deftemplateModule",ModulePrefix(DeftemplateData(theEnv)->DeftemplateCodeItem),
00110 FALSE,NULL);
00111
00112 if (moduleFile == NULL)
00113 {
00114 CloseDeftemplateFiles(theEnv,moduleFile,templateFile,slotFile,maxIndices);
00115 return(0);
00116 }
00117
00118 DeftemplateModuleToCode(theEnv,moduleFile,theModule,imageID,maxIndices,moduleCount);
00119 moduleFile = CloseFileIfNeeded(theEnv,moduleFile,&moduleArrayCount,&moduleArrayVersion,
00120 maxIndices,NULL,NULL);
00121
00122
00123
00124
00125
00126 theTemplate = (struct deftemplate *) EnvGetNextDeftemplate(theEnv,NULL);
00127
00128 while (theTemplate != NULL)
00129 {
00130 templateFile = OpenFileIfNeeded(theEnv,templateFile,fileName,pathName,fileNameBuffer,fileID,imageID,&fileCount,
00131 templateArrayVersion,headerFP,
00132 "struct deftemplate",ConstructPrefix(DeftemplateData(theEnv)->DeftemplateCodeItem),
00133 FALSE,NULL);
00134 if (templateFile == NULL)
00135 {
00136 CloseDeftemplateFiles(theEnv,moduleFile,templateFile,slotFile,maxIndices);
00137 return(0);
00138 }
00139
00140 DeftemplateToCode(theEnv,templateFile,theTemplate,imageID,maxIndices,
00141 moduleCount,slotCount);
00142 templateArrayCount++;
00143 templateFile = CloseFileIfNeeded(theEnv,templateFile,&templateArrayCount,&templateArrayVersion,
00144 maxIndices,NULL,NULL);
00145
00146
00147
00148
00149
00150 slotPtr = theTemplate->slotList;
00151 while (slotPtr != NULL)
00152 {
00153 slotFile = OpenFileIfNeeded(theEnv,slotFile,fileName,pathName,fileNameBuffer,fileID,imageID,&fileCount,
00154 slotArrayVersion,headerFP,
00155 "struct templateSlot",SlotPrefix(),FALSE,NULL);
00156 if (slotFile == NULL)
00157 {
00158 CloseDeftemplateFiles(theEnv,moduleFile,templateFile,slotFile,maxIndices);
00159 return(0);
00160 }
00161
00162 SlotToCode(theEnv,slotFile,slotPtr,imageID,maxIndices,slotCount);
00163 slotCount++;
00164 slotArrayCount++;
00165 slotFile = CloseFileIfNeeded(theEnv,slotFile,&slotArrayCount,&slotArrayVersion,
00166 maxIndices,NULL,NULL);
00167 slotPtr = slotPtr->next;
00168 }
00169
00170 theTemplate = (struct deftemplate *) EnvGetNextDeftemplate(theEnv,theTemplate);
00171 }
00172
00173 theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,theModule);
00174 moduleCount++;
00175 moduleArrayCount++;
00176
00177 }
00178
00179 CloseDeftemplateFiles(theEnv,moduleFile,templateFile,slotFile,maxIndices);
00180
00181 return(1);
00182 }
00183
00184
00185
00186
00187
00188
00189 static void CloseDeftemplateFiles(
00190 void *theEnv,
00191 FILE *moduleFile,
00192 FILE *templateFile,
00193 FILE *slotFile,
00194 int maxIndices)
00195 {
00196 int count = maxIndices;
00197 int arrayVersion = 0;
00198
00199 if (slotFile != NULL)
00200 {
00201 count = maxIndices;
00202 CloseFileIfNeeded(theEnv,slotFile,&count,&arrayVersion,maxIndices,NULL,NULL);
00203 }
00204
00205 if (templateFile != NULL)
00206 {
00207 count = maxIndices;
00208 CloseFileIfNeeded(theEnv,templateFile,&count,&arrayVersion,maxIndices,NULL,NULL);
00209 }
00210
00211 if (moduleFile != NULL)
00212 {
00213 count = maxIndices;
00214 CloseFileIfNeeded(theEnv,moduleFile,&count,&arrayVersion,maxIndices,NULL,NULL);
00215 }
00216 }
00217
00218
00219
00220
00221
00222 #if WIN_BTC
00223 #pragma argsused
00224 #endif
00225 static void DeftemplateModuleToCode(
00226 void *theEnv,
00227 FILE *theFile,
00228 struct defmodule *theModule,
00229 int imageID,
00230 int maxIndices,
00231 int moduleCount)
00232 {
00233 #if MAC_MCW || WIN_MCW || MAC_XCD
00234 #pragma unused(moduleCount)
00235 #endif
00236
00237 fprintf(theFile,"{");
00238
00239 ConstructModuleToCode(theEnv,theFile,theModule,imageID,maxIndices,
00240 DeftemplateData(theEnv)->DeftemplateModuleIndex,ConstructPrefix(DeftemplateData(theEnv)->DeftemplateCodeItem));
00241
00242 fprintf(theFile,"}");
00243 }
00244
00245
00246
00247
00248
00249 static void DeftemplateToCode(
00250 void *theEnv,
00251 FILE *theFile,
00252 struct deftemplate *theTemplate,
00253 int imageID,
00254 int maxIndices,
00255 int moduleCount,
00256 int slotCount)
00257 {
00258
00259
00260
00261
00262 fprintf(theFile,"{");
00263
00264 ConstructHeaderToCode(theEnv,theFile,&theTemplate->header,imageID,maxIndices,
00265 moduleCount,ModulePrefix(DeftemplateData(theEnv)->DeftemplateCodeItem),
00266 ConstructPrefix(DeftemplateData(theEnv)->DeftemplateCodeItem));
00267 fprintf(theFile,",");
00268
00269
00270
00271
00272
00273 if (theTemplate->slotList == NULL)
00274 { fprintf(theFile,"NULL,"); }
00275 else
00276 {
00277 fprintf(theFile,"&%s%d_%d[%d],",SlotPrefix(),
00278 imageID,
00279 (slotCount / maxIndices) + 1,
00280 slotCount % maxIndices);
00281 }
00282
00283
00284
00285
00286
00287
00288 fprintf(theFile,"%d,0,0,%d,%ld,",theTemplate->implied,theTemplate->numberOfSlots,theTemplate->busyCount);
00289
00290
00291
00292
00293
00294 if (theTemplate->patternNetwork == NULL)
00295 { fprintf(theFile,"NULL"); }
00296 else
00297 { FactPatternNodeReference(theEnv,theTemplate->patternNetwork,theFile,imageID,maxIndices); }
00298
00299
00300
00301
00302
00303
00304 fprintf(theFile,",NULL,NULL}");
00305 }
00306
00307
00308
00309
00310
00311 static void SlotToCode(
00312 void *theEnv,
00313 FILE *theFile,
00314 struct templateSlot *theSlot,
00315 int imageID,
00316 int maxIndices,
00317 int slotCount)
00318 {
00319
00320
00321
00322
00323 fprintf(theFile,"{");
00324 PrintSymbolReference(theEnv,theFile,theSlot->slotName);
00325
00326
00327
00328
00329
00330 fprintf(theFile,",%d,%d,%d,%d,",theSlot->multislot,theSlot->noDefault,
00331 theSlot->defaultPresent,theSlot->defaultDynamic);
00332
00333
00334
00335
00336
00337 PrintConstraintReference(theEnv,theFile,theSlot->constraints,imageID,maxIndices);
00338
00339
00340
00341
00342
00343 fprintf(theFile,",");
00344 PrintHashedExpressionReference(theEnv,theFile,theSlot->defaultList,imageID,maxIndices);
00345
00346
00347
00348
00349
00350 fprintf(theFile,",");
00351 PrintHashedExpressionReference(theEnv,theFile,theSlot->facetList,imageID,maxIndices);
00352 fprintf(theFile,",");
00353
00354
00355
00356
00357
00358 if (theSlot->next == NULL)
00359 { fprintf(theFile,"NULL}"); }
00360 else
00361 {
00362 fprintf(theFile,"&%s%d_%d[%d]}",SlotPrefix(),imageID,
00363 ((slotCount+1) / maxIndices) + 1,
00364 (slotCount+1) % maxIndices);
00365 }
00366 }
00367
00368
00369
00370
00371
00372 globle void DeftemplateCModuleReference(
00373 void *theEnv,
00374 FILE *theFile,
00375 int count,
00376 int imageID,
00377 int maxIndices)
00378 {
00379 fprintf(theFile,"MIHS &%s%d_%d[%d]",ModulePrefix(DeftemplateData(theEnv)->DeftemplateCodeItem),
00380 imageID,
00381 (count / maxIndices) + 1,
00382 (count % maxIndices));
00383 }
00384
00385
00386
00387
00388
00389 globle void DeftemplateCConstructReference(
00390 void *theEnv,
00391 FILE *theFile,
00392 void *vTheTemplate,
00393 int imageID,
00394 int maxIndices)
00395 {
00396 struct deftemplate *theTemplate = (struct deftemplate *) vTheTemplate;
00397
00398 if (theTemplate == NULL)
00399 { fprintf(theFile,"NULL"); }
00400 else
00401 {
00402 fprintf(theFile,"&%s%d_%ld[%ld]",ConstructPrefix(DeftemplateData(theEnv)->DeftemplateCodeItem),
00403 imageID,
00404 (theTemplate->header.bsaveID / maxIndices) + 1,
00405 theTemplate->header.bsaveID % maxIndices);
00406 }
00407
00408 }
00409
00410
00411
00412
00413
00414 #if WIN_BTC
00415 #pragma argsused
00416 #endif
00417 static void InitDeftemplateCode(
00418 void *theEnv,
00419 FILE *initFP,
00420 int imageID,
00421 int maxIndices)
00422 {
00423 #if MAC_MCW || WIN_MCW || MAC_XCD
00424 #pragma unused(theEnv)
00425 #pragma unused(imageID)
00426 #pragma unused(maxIndices)
00427 #endif
00428
00429 fprintf(initFP," DeftemplateRunTimeInitialize(theEnv);\n");
00430 }
00431
00432 #endif
00433