2 * (c)2006 Galois Connections, Inc.
15 /* This is the runtime support for the Haskell Program Coverage (hpc) toolkit,
22 static int hpc_inited = 0; // Have you started this component?
23 static FILE *tixFile; // file being read/written
24 static int tix_ch; // current char
26 typedef struct _Info {
27 char *modName; // name of module
28 int tickCount; // number of ticks
29 int tickOffset; // offset into a single large .tix Array
30 StgWord64 *tixArr; // tix Array from the program execution (local for this module)
36 StgWord64 *tixBoxes = 0; // local copy of tixBoxes array, from file.
37 int totalTixes = 0; // total number of tix boxes.
41 static char *tixFilename = "Main.tix";
43 static void failure(char *msg) {
44 printf("Hpc failure: %s\n",msg);
45 printf("(perhaps remove .tix file?)\n");
50 static int init_open(char *filename)
52 tixFile = fopen(filename,"r");
56 tix_ch = getc(tixFile);
60 static void expect(char c) {
62 printf("Hpc: parse failed (%c,%c)\n",tix_ch,c);
65 tix_ch = getc(tixFile);
68 static void ws(void) {
69 while (tix_ch == ' ') {
70 tix_ch = getc(tixFile);
74 static char *expectString(void) {
78 while (tix_ch != '"') {
79 tmp[tmp_ix++] = tix_ch;
80 tix_ch = getc(tixFile);
89 static StgWord64 expectWord64(void) {
91 while (isdigit(tix_ch)) {
92 tmp = tmp * 10 + (tix_ch -'0');
93 tix_ch = getc(tixFile);
98 static void hpc_init(void) {
102 if (hpc_inited != 0) {
107 if (init_open(tixFilename)) {
119 while(tix_ch != ']') {
120 tmpModule = (Info *)calloc(1,sizeof(Info));
123 tmpModule -> modName = expectString();
127 tmpModule -> tickCount = (int)expectWord64();
132 tmpModule -> tickOffset = totalTixes;
133 totalTixes += tmpModule -> tickCount;
135 tmpModule -> tixArr = 0;
140 nextModule->next=tmpModule;
142 nextModule=tmpModule;
150 tixBoxes = (StgWord64 *)calloc(totalTixes,sizeof(StgWord64));
153 for(i = 0;i < totalTixes;i++) {
158 tixBoxes[i] = expectWord64();
167 /* Called on a per-module basis, at startup time, declaring where the tix boxes are stored in memory.
168 * This memory can be uninitized, because we will initialize it with either the contents
169 * of the tix file, or all zeros.
173 hs_hpc_module(char *modName,int modCount,StgWord64 *tixArr) {
174 Info *tmpModule, *lastModule;
178 printf("hs_hpc_module(%s,%d)\n",modName,modCount);
186 for(;tmpModule != 0;tmpModule = tmpModule->next) {
187 if (!strcmp(tmpModule->modName,modName)) {
188 if (tmpModule->tickCount != modCount) {
189 failure("inconsistent number of tick boxes");
191 assert(tmpModule->tixArr == 0);
192 assert(tixBoxes != 0);
193 tmpModule->tixArr = tixArr;
194 for(i=0;i < modCount;i++) {
195 tixArr[i] = tixBoxes[i + tmpModule->tickOffset];
199 lastModule = tmpModule;
201 // Did not find entry so add one on.
202 tmpModule = (Info *)calloc(1,sizeof(Info));
203 tmpModule->modName = modName;
204 tmpModule->tickCount = modCount;
206 tmpModule->tickOffset = lastModule->tickOffset + lastModule->tickCount;
208 tmpModule->tickOffset = 0;
210 tmpModule->tixArr = tixArr;
211 for(i=0;i < modCount;i++) {
219 lastModule->next=tmpModule;
223 printf("end: hs_hpc_module\n");
227 /* This is called after all the modules have registered their local tixboxes,
228 * and does a sanity check: are we good to go?
235 printf("startupHpc\n");
238 if (hpc_inited == 0) {
245 for(;tmpModule != 0;tmpModule = tmpModule->next) {
246 if (!tmpModule->tixArr) {
247 fprintf(stderr,"error: module %s did not register any hpc tick data\n",
249 fprintf(stderr,"(perhaps remove %s ?)\n",tixFilename);
256 /* Called at the end of execution, to write out the Hpc *.tix file
257 * for this exection. Safe to call, even if coverage is not used.
268 if (hpc_inited == 0) {
272 FILE *f = fopen(tixFilename,"w");
276 fprintf(f,"Tix 0 [");
278 for(;tmpModule != 0;tmpModule = tmpModule->next) {
284 fprintf(f,"(\"%s\",%d)",
286 tmpModule->tickCount);
288 fprintf(stderr,"%s: %d (offset=%d)\n",
290 tmpModule->tickCount,
291 tmpModule->tickOffset);
298 for(;tmpModule != 0;tmpModule = tmpModule->next) {
299 if (!tmpModule->tixArr) {
300 fprintf(stderr,"warning: module %s did not register any hpc tick data\n",
304 for(i = 0;i < tmpModule->tickCount;i++) {
311 if (tmpModule->tixArr) {
312 fprintf(f,"%lld",tmpModule->tixArr[i]);