00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #define _MODULCMP_SOURCE_
00025
00026 #include "setup.h"
00027
00028 #if CONSTRUCT_COMPILER && (! RUN_TIME)
00029
00030 #include <stdio.h>
00031 #define _STDIO_INCLUDED_
00032
00033 #include "conscomp.h"
00034 #include "moduldef.h"
00035 #include "sysdep.h"
00036 #include "envrnmnt.h"
00037
00038 #include "modulcmp.h"
00039
00040
00041
00042
00043
00044 #define ItemPrefix() ArbitraryPrefix(DefmoduleData(theEnv)->DefmoduleCodeItem,0)
00045 #define DefmodulePrefix() ArbitraryPrefix(DefmoduleData(theEnv)->DefmoduleCodeItem,1)
00046 #define PortPrefix() ArbitraryPrefix(DefmoduleData(theEnv)->DefmoduleCodeItem,2)
00047
00048
00049
00050
00051
00052 static int ConstructToCode(void *,char *,char *,char *,int,FILE *,int,int);
00053 static void InitDefmoduleCode(void *,FILE *,int,int);
00054 static struct portItem *GetNextPortItem(void *,struct defmodule **,struct portItem **,
00055 int *,int *);
00056 static int PortItemsToCode(void *,char *,char *,char *,int,FILE *,int,int,int *);
00057 static void BeforeDefmodulesToCode(void *);
00058
00059
00060
00061
00062
00063 globle void DefmoduleCompilerSetup(
00064 void *theEnv)
00065 {
00066 DefmoduleData(theEnv)->DefmoduleCodeItem =
00067 AddCodeGeneratorItem(theEnv,"defmodule",200,BeforeDefmodulesToCode,
00068 InitDefmoduleCode,ConstructToCode,3);
00069 }
00070
00071
00072
00073
00074
00075
00076 static void BeforeDefmodulesToCode(
00077 void *theEnv)
00078 {
00079 int value = 0;
00080 struct defmodule *theModule;
00081
00082 for (theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,NULL);
00083 theModule != NULL;
00084 theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,theModule))
00085 { theModule->bsaveID = value++; }
00086 }
00087
00088
00089
00090
00091
00092 globle void PrintDefmoduleReference(
00093 void *theEnv,
00094 FILE *theFile,
00095 struct defmodule *theModule)
00096 {
00097 if (theModule == NULL) fprintf(theFile,"NULL");
00098 else fprintf(theFile,"&%s%d_%ld[%ld]",DefmodulePrefix(),ConstructCompilerData(theEnv)->ImageID,
00099 (long) ((theModule->bsaveID / ConstructCompilerData(theEnv)->MaxIndices) + 1),
00100 (long) (theModule->bsaveID % ConstructCompilerData(theEnv)->MaxIndices));
00101 }
00102
00103
00104
00105
00106
00107 #if WIN_BTC
00108 #pragma argsused
00109 #endif
00110 static void InitDefmoduleCode(
00111 void *theEnv,
00112 FILE *initFP,
00113 int imageID,
00114 int maxIndices)
00115 {
00116 #if MAC_MCW || WIN_MCW || MAC_XCD
00117 #pragma unused(maxIndices)
00118 #endif
00119
00120 if (EnvGetNextDefmodule(theEnv,NULL) != NULL)
00121 { fprintf(initFP," SetListOfDefmodules(theEnv,(void *) %s%d_1);\n",DefmodulePrefix(),imageID); }
00122 else
00123 { fprintf(initFP," SetListOfDefmodules(theEnv,NULL);\n"); }
00124 fprintf(initFP," EnvSetCurrentModule(theEnv,(void *) EnvGetNextDefmodule(theEnv,NULL));\n");
00125 }
00126
00127
00128
00129
00130
00131 static int ConstructToCode(
00132 void *theEnv,
00133 char *fileName,
00134 char *pathName,
00135 char *fileNameBuffer,
00136 int fileID,
00137 FILE *headerFP,
00138 int imageID,
00139 int maxIndices)
00140 {
00141 struct defmodule *theConstruct;
00142 FILE *moduleFile = NULL, *itemsFile;
00143 int portItemCount = 0;
00144 struct portItem *portItemPtr;
00145 int mihCount = 0, moduleCount = 0;
00146 int j;
00147 struct moduleItem *theItem;
00148 int moduleArrayVersion = 1;
00149 int fileCount = 2;
00150
00151
00152
00153
00154
00155 fprintf(headerFP,"#include \"moduldef.h\"\n");
00156
00157
00158
00159
00160
00161
00162
00163 if ((itemsFile = NewCFile(theEnv,fileName,pathName,fileNameBuffer,fileID,1,FALSE)) == NULL)
00164 { return(FALSE); }
00165 fprintf(itemsFile,"struct defmoduleItemHeader *%s%d_%d[] = {\n",ItemPrefix(),imageID,1);
00166 fprintf(headerFP,"extern struct defmoduleItemHeader *%s%d_%d[];\n",ItemPrefix(),imageID,1);
00167
00168
00169
00170
00171
00172
00173 for (theConstruct = (struct defmodule *) EnvGetNextDefmodule(theEnv,NULL);
00174 theConstruct != NULL;
00175 theConstruct = (struct defmodule *) EnvGetNextDefmodule(theEnv,theConstruct))
00176 {
00177
00178
00179
00180
00181 moduleFile = OpenFileIfNeeded(theEnv,moduleFile,fileName,pathName,fileNameBuffer,fileID,imageID,
00182 &fileCount,moduleArrayVersion,headerFP,
00183 "struct defmodule",DefmodulePrefix(),
00184 FALSE,NULL);
00185
00186 if (moduleFile == NULL)
00187 {
00188 moduleCount = maxIndices;
00189 CloseFileIfNeeded(theEnv,moduleFile,&moduleCount,
00190 &moduleArrayVersion,maxIndices,NULL,NULL);
00191 GenClose(theEnv,itemsFile);
00192 return(FALSE);
00193 }
00194
00195
00196
00197
00198
00199 fprintf(moduleFile,"{");
00200 PrintSymbolReference(theEnv,moduleFile,theConstruct->name);
00201 fprintf(moduleFile,",NULL,");
00202
00203
00204
00205
00206
00207 fprintf(moduleFile,"&%s%d_1[%d],",ItemPrefix(),imageID,mihCount);
00208
00209 for (j = 0, theItem = GetListOfModuleItems(theEnv);
00210 (j < GetNumberOfModuleItems(theEnv)) && (theItem != NULL) ;
00211 j++, theItem = theItem->next)
00212 {
00213 mihCount++;
00214 if (theItem->constructsToCModuleReference == NULL)
00215 { fprintf(itemsFile,"NULL"); }
00216 else
00217 { (*theItem->constructsToCModuleReference)(theEnv,itemsFile,(int) theConstruct->bsaveID,imageID,maxIndices); }
00218
00219 if ((j + 1) < GetNumberOfModuleItems(theEnv)) fprintf(itemsFile,",");
00220 else if (theConstruct->next != NULL) fprintf(itemsFile,",\n");
00221 }
00222
00223
00224
00225
00226
00227 if (theConstruct->importList == NULL)
00228 { fprintf(moduleFile,"NULL,"); }
00229 else
00230 {
00231 fprintf(moduleFile,"&%s%d_%d[%d],",PortPrefix(),imageID,
00232 (portItemCount / maxIndices) + 1,
00233 portItemCount % maxIndices);
00234 for (portItemPtr = theConstruct->importList;
00235 portItemPtr != NULL;
00236 portItemPtr = portItemPtr->next)
00237 { portItemCount++; }
00238 }
00239
00240
00241
00242
00243
00244 if (theConstruct->exportList == NULL)
00245 { fprintf(moduleFile,"NULL,"); }
00246 else
00247 {
00248 fprintf(moduleFile,"&%s%d_%d[%d],",PortPrefix(),imageID,
00249 (portItemCount / maxIndices) + 1,
00250 portItemCount % maxIndices);
00251 for (portItemPtr = theConstruct->exportList;
00252 portItemPtr != NULL;
00253 portItemPtr = portItemPtr->next)
00254 { portItemCount++; }
00255 }
00256
00257
00258
00259
00260
00261 fprintf(moduleFile,"0,%ld,",theConstruct->bsaveID);
00262
00263
00264
00265
00266
00267 fprintf(moduleFile,"NULL,");
00268
00269
00270
00271
00272
00273 if (theConstruct->next == NULL)
00274 { fprintf(moduleFile,"NULL}"); }
00275 else
00276 {
00277 fprintf(moduleFile,"&%s%d_%d[%d]}",ConstructPrefix(DefmoduleData(theEnv)->DefmoduleCodeItem),imageID,
00278 (int) (theConstruct->next->bsaveID / maxIndices) + 1,
00279 (int) theConstruct->next->bsaveID % maxIndices);
00280 }
00281
00282
00283
00284
00285
00286
00287 moduleCount++;
00288 moduleFile = CloseFileIfNeeded(theEnv,moduleFile,&moduleCount,&moduleArrayVersion,
00289 maxIndices,NULL,NULL);
00290
00291 }
00292
00293
00294
00295
00296
00297 moduleCount = maxIndices;
00298 CloseFileIfNeeded(theEnv,moduleFile,&moduleCount,
00299 &moduleArrayVersion,maxIndices,NULL,NULL);
00300 fprintf(itemsFile,"};\n");
00301 GenClose(theEnv,itemsFile);
00302
00303
00304
00305
00306
00307 if (portItemCount == 0) return(TRUE);
00308 return(PortItemsToCode(theEnv,fileName,pathName,fileNameBuffer,fileID,headerFP,imageID,maxIndices,&fileCount));
00309 }
00310
00311
00312
00313
00314
00315 static int PortItemsToCode(
00316 void *theEnv,
00317 char *fileName,
00318 char *pathName,
00319 char *fileNameBuffer,
00320 int fileID,
00321 FILE *headerFP,
00322 int imageID,
00323 int maxIndices,
00324 int *fileCount)
00325 {
00326 struct defmodule *theDefmodule = NULL;
00327 struct portItem *thePortItem = NULL;
00328 int portItemCount = 0;
00329 int importChecked = FALSE;
00330 int exportChecked = FALSE;
00331 FILE *portItemsFile = NULL;
00332 int portItemArrayVersion = 1;
00333
00334
00335
00336
00337
00338
00339 for (thePortItem = GetNextPortItem(theEnv,&theDefmodule,&thePortItem,&importChecked,&exportChecked);
00340 thePortItem != NULL;
00341 thePortItem = GetNextPortItem(theEnv,&theDefmodule,&thePortItem,&importChecked,&exportChecked))
00342 {
00343
00344
00345
00346
00347 portItemsFile = OpenFileIfNeeded(theEnv,portItemsFile,fileName,pathName,fileNameBuffer,fileID,imageID,
00348 fileCount,portItemArrayVersion,headerFP,
00349 "struct portItem",PortPrefix(),
00350 FALSE,NULL);
00351
00352 if (portItemsFile == NULL)
00353 {
00354 portItemCount = maxIndices;
00355 CloseFileIfNeeded(theEnv,portItemsFile,&portItemCount,
00356 &portItemArrayVersion,maxIndices,NULL,NULL);
00357 return(FALSE);
00358 }
00359
00360
00361
00362
00363
00364 fprintf(portItemsFile,"{");
00365 PrintSymbolReference(theEnv,portItemsFile,thePortItem->moduleName);
00366 fprintf(portItemsFile,",");
00367 PrintSymbolReference(theEnv,portItemsFile,thePortItem->constructType);
00368 fprintf(portItemsFile,",");
00369 PrintSymbolReference(theEnv,portItemsFile,thePortItem->constructName);
00370 fprintf(portItemsFile,",");
00371
00372 if (thePortItem->next == NULL)
00373 { fprintf(portItemsFile,"NULL}"); }
00374 else
00375 {
00376 fprintf(portItemsFile,"&%s%d_%d[%d]}",PortPrefix(),imageID,
00377 ((portItemCount+1) / maxIndices) + 1,
00378 (portItemCount+1) % maxIndices);
00379 }
00380
00381
00382
00383
00384
00385
00386 portItemCount++;
00387 CloseFileIfNeeded(theEnv,portItemsFile,&portItemCount,&portItemArrayVersion,
00388 maxIndices,NULL,NULL);
00389 }
00390
00391
00392
00393
00394
00395
00396 portItemCount = maxIndices;
00397 CloseFileIfNeeded(theEnv,portItemsFile,&portItemCount,
00398 &portItemArrayVersion,maxIndices,NULL,NULL);
00399
00400 return(TRUE);
00401 }
00402
00403
00404
00405
00406
00407
00408
00409 static struct portItem *GetNextPortItem(
00410 void *theEnv,
00411 struct defmodule **theDefmodule,
00412 struct portItem **thePortItem,
00413 int *importChecked,
00414 int *exportChecked)
00415 {
00416
00417
00418
00419
00420
00421
00422 if (*theDefmodule == NULL)
00423 {
00424 *theDefmodule = (struct defmodule *) EnvGetNextDefmodule(theEnv,NULL);
00425 *thePortItem = NULL;
00426 *importChecked = FALSE;
00427 *exportChecked = FALSE;
00428 }
00429
00430
00431
00432
00433
00434
00435 while (*theDefmodule != NULL)
00436 {
00437
00438
00439
00440
00441
00442
00443 if (*thePortItem != NULL) *thePortItem = (*thePortItem)->next;
00444 if (*thePortItem != NULL) return(*thePortItem);
00445
00446
00447
00448
00449
00450
00451
00452 if (! (*importChecked))
00453 {
00454 *thePortItem = (*theDefmodule)->importList;
00455 *importChecked = TRUE;
00456 if (*thePortItem == NULL)
00457 {
00458 *thePortItem = (*theDefmodule)->exportList;
00459 *exportChecked = TRUE;
00460 }
00461 }
00462
00463
00464
00465
00466
00467
00468 else if (! (*exportChecked))
00469 {
00470 *exportChecked = TRUE;
00471 *thePortItem = (*theDefmodule)->exportList;
00472 }
00473
00474
00475
00476
00477
00478
00479 if (*thePortItem != NULL) return(*thePortItem);
00480
00481
00482
00483
00484
00485
00486 *theDefmodule = (struct defmodule *) EnvGetNextDefmodule(theEnv,*theDefmodule);
00487 *importChecked = FALSE;
00488 *exportChecked = FALSE;
00489 }
00490
00491
00492
00493
00494
00495
00496 return(NULL);
00497 }
00498
00499 #endif
00500
00501