git @ Cat's Eye Technologies Befunge-93 / master src / befprof.c
master

Tree @master (Download .tar.gz)

befprof.c @masterraw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
/********************************************************************

   befprof.c - Befunge-93 Profiler

   Copyright (c)1998-2018, Chris Pressey, Cat's Eye Technologies.
   All rights reserved.
 
   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:

     Redistributions of source code must retain the above copyright
     notices, this list of conditions and the following disclaimer.

     Redistributions in binary form must reproduce the above copyright
     notices, this list of conditions, and the following disclaimer in
     the documentation and/or other materials provided with the
     distribution.

     Neither the names of the copyright holders nor the names of their
     contributors may be used to endorse or promote products derived
     from this software without specific prior written permission. 

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES INCLUDING, BUT NOT
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
   COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   POSSIBILITY OF SUCH DAMAGE.

   ******************************************************************

   Usage :

   befprof [-l] [-i] [-n count] [-r input-file] [-w report-file]
           <befunge-source>

      -i: use interactive I/O
      -r: redirect input from a specified file instead of stdin
          generally use these only if random input and logic
          does not well reflect reality, or causes a problem
          with your source code (e.g. anagram.bf)
      -l: randomize logic - makes | and _ execute randomly.
      -w: specify an output filename for report (if omitted,
          <input-file>.map is used)
      -n: repeat execution a given number of times (def. 1)
          use to generate data with a better resolution.
          although note that cells are stored as 2-byte words,
          so too many repetitions may cause them to wrap back to 0.

   Known to Compile Under :

        gcc 5.4.0 (Ubuntu 16.04)
        gcc 4.5.3 (NetBSD 6.1.5)
        DICE C 3.15 (AmigaDOS 1.3)
        DJGPP 2.05 gcc 8.1.0 (32-bit Protected-Mode, FreeDOS 1.1)
        Borland C++ v3.1 (16-bit, FreeDOS 1.1)
          (NOTE: use "compact" or "large" memory model!)
        Microsoft Visual C++ 14.15 (see NMakefile)

   Has, in the Past, been Known to Compile Under:

        Metrowerks CodeWarrior (MacOS)

   ******************************************************************

   History:

   v1.0: Sep 2018, Chris Pressey
          handle trampoline at leftmost/topmost edges
            (thanks to https://github.com/serprex for this fix!)
          exit with non-zero error code on error
          remove unimplemented -q option from usage help text
          don't load invalid (past-EOF) bytes into playfield

   v0.94: Sep 2004, Chris Pressey
          cleanup only, no functional changes

   v0.93: Jul 2000, Chris Pressey
          added defines for Metrowerks CodeWarrior
            so that befprof will build on MacOS
          relicensed under BSD license

   v0.92: Mar 1998, Chris Pressey
          original Befunge-93 Profiler 'befprof' distribution.

   ****************************************************************** */

/********************************************************* #INCLUDE'S */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>

/********************************************************** #DEFINE'S */

#define LINEWIDTH    80
#define PAGEHEIGHT   25

#define cur pg[y * LINEWIDTH + x]
#define curbuf pgbuf[y * LINEWIDTH + x]
#define incprof(z) prof[z][y * LINEWIDTH + x]++;
#define curprof(z) prof[z][y * LINEWIDTH + x]

/********************************************************* STRUCTURES */

struct stack                    /* stack structure, for values on stack */
{
  signed long val;
  struct stack *next;
} *head;                        /* head of stack */

/*************************************************** GLOBAL VARIABLES */

char * pg;                       /* befunge 'page' of source */
char * pgbuf;                    /* where we load befunge 'page' of source */
int x = 0, y = 0;                /* x and y of the PC */
int dx = 1, dy = 0;              /* direction of the PC */
int infile = 0, ia;              /* flag : use input file, and assoc arg? */
int stringmode = 0;              /* flag : are we in string mode? */
int reps = 1;
int rep = 1;
int interactive = 0;
int rand_logic = 0;
unsigned long int curiter = 0;
unsigned long int maxiter = 500000;
int putoob = 0, unsupp = 0;

#define INGRESS_NORTH 0
#define INGRESS_SOUTH 1
#define INGRESS_EAST 2
#define INGRESS_WEST 3
#define EGRESS_NORTH 4
#define EGRESS_SOUTH 5
#define EGRESS_EAST 6
#define EGRESS_WEST 7
#define PUT_CELL 8
#define GET_CELL 9

#define PROFILE_HANDLES 10

unsigned long int * prof[PROFILE_HANDLES];

/********************************************************* PROTOTYPES */

void push (signed long val);
signed long pop (void);
int main (int, char **);

/******************************************************* MAIN PROGRAM */

int main (argc, argv)
    int argc;
    char **argv;
{
  FILE *f=NULL;
  FILE *fi=NULL;
  FILE *fp=NULL;
  int i;
  int flag_egress;
  char filename[128];
  char mapfilename[128];

#ifdef __MWERKS__
  argc = ccommand(&argv);
#endif /* __MWERKS__ */

  srand (time (0));

  pg = malloc(LINEWIDTH * PAGEHEIGHT);
  pgbuf = malloc(LINEWIDTH * PAGEHEIGHT);
  if ((pg == NULL) || (pgbuf == NULL))
  {
    printf ("Error: can't allocate %d bytes of memory.\n", LINEWIDTH * PAGEHEIGHT);
    exit (1);
  }
  memset(pg, ' ', LINEWIDTH * PAGEHEIGHT);
  memset(pgbuf, ' ', LINEWIDTH * PAGEHEIGHT);

  for(i=0;i<PROFILE_HANDLES;i++)
  {
    prof[i] = malloc(LINEWIDTH * PAGEHEIGHT * sizeof(long int));
    if (prof[i] == NULL)
    {
      printf ("Error: can't allocate %lu bytes of memory.\n", (long)(LINEWIDTH * PAGEHEIGHT * sizeof(long int)));
      exit (1);
    }
    memset(prof[i], 0, LINEWIDTH * PAGEHEIGHT * sizeof(long int));
  }

  if (argc < 2)
  {
    printf ("USAGE: befprof [-l] [-i] [-n count] [-r input] [-w foo.map] foo.bf\n");
    exit (1);
  }

  strcpy(filename, argv[argc - 1]);
  if (strchr(filename, '.') == NULL)
  {
    strcpy(mapfilename, filename);
    strcat(mapfilename, ".map");
    strcat(filename, ".bf");
  } else
  {
    char *xs;
    strcpy(mapfilename, filename);
    xs = strchr(mapfilename, '.');
    xs[0] = 0;
    strcat(mapfilename, ".map");
  }

  for (i = 1; i < argc; i++)
  {
    if (!strcmp(argv[i], "-r")) { infile = 1; ia = i + 1; }
    if (!strcmp(argv[i], "-i")) { interactive = 1; }
    if (!strcmp(argv[i], "-l")) { rand_logic = 1; }
    if (!strcmp(argv[i], "-n")) { reps = atoi(argv[i + 1]); }
    if (!strcmp(argv[i], "-w")) { strcpy(mapfilename, argv[i + 1]); }
  }

  printf ("Befunge-93 Profiler v1.0\n");

  if ((f = fopen (filename, "r")) != NULL)             /*** Input Phase */
  {
    int x = 0, y = 0;

    while (!feof (f))
    {
      curbuf = fgetc (f);
      if (feof (f))
      {
        curbuf = ' ';
        break;
      }
      if (curbuf == '\n')
      {
        curbuf = ' ';
        x = 0;
        y++;
        if (y >= PAGEHEIGHT) break;
      } else
      {
        x++;
        if (x >= LINEWIDTH)
        {
          x = 0;
          y++;
          if (y >= PAGEHEIGHT) break;
        }
      }
    }
    fclose (f);
  } else
  {
    printf ("Error: couldn't open '%s' for input.\n", filename);
    exit (1);
  }

  if (!(fp = fopen (mapfilename, "w")))
  {
    printf ("Error : couldn't open '%s' for output.\n", mapfilename);
    exit (1);
  }

  while (rep <= reps)
  {
    x = 0;
    y = 0;
    dx = 1;
    dy = 0;
    stringmode = 0;
    curiter = 0;

    memcpy(pg, pgbuf, LINEWIDTH * PAGEHEIGHT);

    if (infile)
    {
      if (!(fi = fopen (argv[ia], "r")))
      {
        printf ("Error : couldn't open '%s' for input.\n", argv[ia]);
        exit (1);
      }
    }

    while ((cur != '@') || (stringmode))          /*** Intepreting Phase */
    {
      flag_egress = 0;
      if ((dx == 1) && (dy == 0)) incprof(INGRESS_WEST);
      if ((dx == -1) && (dy == 0)) incprof(INGRESS_EAST);
      if ((dx == 0) && (dy == 1)) incprof(INGRESS_NORTH);
      if ((dx == 0) && (dy == -1)) incprof(INGRESS_SOUTH);
      if (stringmode && (cur != '"'))
      {
        push (cur);
      } else
      if (isdigit ((int)cur))
      {
        push (cur - '0');
      } else
      switch (cur)
      {
        case '>':            /* PC Right */
          dx = 1;
          dy = 0;
          break;
        case '<':            /* PC Left */
          dx = -1;
          dy = 0;
          break;
        case '^':            /* PC Up */
          dx = 0;
          dy = -1;
          break;
        case 'v':            /* PC Down */
          dx = 0;
          dy = 1;
          break;
        case '|':            /* Vertical 'If' */
        {
          long int pp = pop();
          dx = 0;
          if (rand_logic) pp = ((rand () >> 3) % 2);
          if (pp)
          {
            dy = -1;
          } else
          {
            dy = 1;
          }
          break;
        }
        case '_':            /* Horizontal 'If' */
        {
          long int pp = pop();
          dy = 0;
          if (rand_logic) pp = ((rand () >> 3) % 2);
          if (pp)
          {
            dx = -1;
          } else
          {
            dx = 1;
          }
          break;
        }
        case '+':            /* Add */
          push (pop () + pop ());
          break;
        case '-':            /* Subtract */
        {
          long a = pop();
          long b = pop();
          push(b - a);
          break;
        }
        case '*':            /* Multiply */
          push (pop () * pop ());
          break;
        case '/':            /* Integer Divide */
        {
          signed long a = pop ();
          signed long b = pop ();
          if (a == 0)
          {
            if (infile)
            {
              fscanf (fi, "%ld", &b);
              push (b);
            } else
            if (interactive)
            {
              printf("What do you want %ld/0 to be? ", b);
              scanf ("%ld", &b);
              push (b);
            } else
            {
              push (rand() - 32767);
            }
          } else
          {
            push (b / a);
          }
          break;
        }
        case '%':            /* Modulo */
        {
          signed long a = pop ();
          signed long b = pop ();
          push (b % a);
          break;
        }
        case '\\':           /* Swap */
        {
          signed long a = pop ();
          signed long b = pop ();
          push (a);
          push (b);
          break;
        }
        case '.':            /* Pop Out Integer */
        {
          if (interactive)
          {
            printf ("%ld ", pop ());
            fflush (stdout);
          } else pop();
          break;
        }
        case ',':            /* Pop Out ASCII */
        {
          if (interactive)
          {
            printf ("%c", (char)pop ());
            fflush (stdout);
          } else pop();
          break;
        }
        case '"':               /* Toggle String Mode */
          stringmode = !stringmode;
          break;
        case ':':            /* Duplicate */
        {
          signed long a = pop ();
          push (a);
          push (a);
          break;
        }
        case '!':            /* Negate */
        {
          if (pop())
          {
            push(0);
          } else
          {
            push(1);
          }
          break;
        }
        case '`':
        {
          signed long b = pop ();
          signed long a = pop ();
          if (a > b)
          {
            push(1);
          } else
          {
            push(0);
          }
          break;
        }
        case '#':            /* Bridge */
        {
          if ((dx == 1) && (dy == 0)) incprof(EGRESS_EAST);
          if ((dx == -1) && (dy == 0)) incprof(EGRESS_WEST);
          if ((dx == 0) && (dy == 1)) incprof(EGRESS_SOUTH);
          if ((dx == 0) && (dy == -1)) incprof(EGRESS_NORTH);
          x += dx;
          y += dy;
          flag_egress = 1;
          break;
        }
        case '$':            /* Pop and Discard */
        {
          pop ();
          break;
        }
        case '?':            /* Random Redirect */
        {
          switch ((rand () / 32) % 4)
          {
            case 0:
              dx = 1;
              dy = 0;
              break;
            case 1:
              dx = -1;
              dy = 0;
              break;
            case 2:
              dx = 0;
              dy = -1;
              break;
            case 3:
              dx = 0;
              dy = 1;
              break;
          }
          break;
        }
        case '&':            /* Input Integer */
        {
          signed long b;
          if (infile)
          {
            fscanf (fi, "%ld", &b);
            push (b);
          } else
          if (interactive)
          {
            scanf ("%ld", &b);
            push (b);
          } else
          {
            push (rand() - 32767);
          }
          break;
        }
        case '~':            /* Input ASCII */
        {
          char c;
          if (infile)
          {
            c = fgetc (fi);
            push (c);
          } else
          if (interactive)
          {
            c = fgetc (stdin);
            push (c);
          } else
          {
            c = ((rand() / 32) % 128);
            while (c < 32)
            {
              c = ((rand() / 32) % 128);
            }
            push (c);
          }
          break;
        }
        case 'g':            /* Get Value */
        {
          signed long y = pop (), x = pop ();
          incprof(GET_CELL);
          push (cur);
          break;
        }
        case 'p':            /* Put Value */
        {
          signed long y = pop (), x = pop ();
          if ((y < PAGEHEIGHT) && (y >= 0) && (x < LINEWIDTH) && (x >= 0))
          {
            incprof(PUT_CELL);
            cur = pop ();
          } else
          {
            /* fprintf(stderr, "p 'Put' instruction out of bounds (%d,%d)\n", x, y); */
            putoob++;
          }
          break;
        }
        case ' ':
          break;
        default:
        {
          unsupp++;
          break;
        }
      }
      if (!flag_egress)
      {
        if ((dx == 1) && (dy == 0)) incprof(EGRESS_EAST);
        if ((dx == -1) && (dy == 0)) incprof(EGRESS_WEST);
        if ((dx == 0) && (dy == 1)) incprof(EGRESS_SOUTH);
        if ((dx == 0) && (dy == -1)) incprof(EGRESS_NORTH);
      }
      x += dx;
      y += dy;
      if (x < 0)
      {
        x += LINEWIDTH;
      } else
      {
        x = x % LINEWIDTH;
      }
      if (y < 0)
      {
        y += PAGEHEIGHT;
      } else
      {
        y = y % PAGEHEIGHT;
      }

      curiter++;
      if (curiter > maxiter)
      {
        printf("more than %ld iterations\n", maxiter);
        break;
      }
    }

    while (head != NULL) { pop(); }
    if (fi != NULL)
    {
      fclose (fi);
      fi = NULL;
    }
    printf("Finished executing %d/%d (%ld ticks)\n", rep, reps, curiter-1);
    rep++;
  }

  if (fp != NULL)
  {
    /* print out results */
    int x, y, z; char c;
    unsigned long int maxprof;
    fprintf(fp, "BEGIN map 0; bf93prof - source file\n");
    for (y=0;y<PAGEHEIGHT;y++)
    {
      for (x=0;x<LINEWIDTH;x++)
      {
        fprintf(fp, "%c", cur);
      }
      fprintf(fp, "\n");
    }

    for(z=0;z<PROFILE_HANDLES;z++)
    {
      fprintf(fp, "BEGIN map %d; bf93prof %s ", z + 1, filename);
      switch(z)
      {
        case INGRESS_NORTH: fprintf(fp, "ip enters from north"); break;
        case INGRESS_SOUTH: fprintf(fp, "ip enters from south"); break;
        case INGRESS_EAST: fprintf(fp, "ip enters from east"); break;
        case INGRESS_WEST: fprintf(fp, "ip enters from west"); break;
        case EGRESS_NORTH: fprintf(fp, "ip exits to north"); break;
        case EGRESS_SOUTH: fprintf(fp, "ip exits to south"); break;
        case EGRESS_EAST: fprintf(fp, "ip exits to east"); break;
        case EGRESS_WEST: fprintf(fp, "ip exits to west"); break;
        case PUT_CELL: fprintf(fp, "g (get) addresses cell"); break;
        case GET_CELL: fprintf(fp, "p (put) addresses cell"); break;
      }
      fprintf(fp, "\n");
      maxprof = 0;
      {
        for (y=0;y<PAGEHEIGHT;y++)
        {
          for (x=0;x<LINEWIDTH;x++)
          {
            if (curprof(z) > maxprof) maxprof = curprof(z);
          }
        }
        for (y=0;y<PAGEHEIGHT;y++)
        {
          for (x=0;x<LINEWIDTH;x++)
          {
            if (maxprof > 0)
            {
              c = '0' + ((curprof(z) * 10) / maxprof);
              if (curprof(z) == 0) c = '.';
              if (curprof(z) == maxprof) c = '*';
              if ((c > '9') && (c != '*') && (c != '.'))
              {
                c = '*';
              }
              fprintf(fp, "%c", c);
            } else
            {
              fprintf(fp, ".");
            }
          }
          fprintf(fp, "\n");
        }
      }
      fprintf(fp, "END map %d\n", z + 1);
    }

    fprintf(fp, "BEGIN map %d; bf93prof %s ", PROFILE_HANDLES + 1, filename);
    fprintf(fp, "all possible ip ingresses (N8S4E2W1)\n");

    for (y=0;y<PAGEHEIGHT;y++)
    {
      for (x=0;x<LINEWIDTH;x++)
      {
        int b=0;
        if (curprof(INGRESS_NORTH) > 0) { b |= 0x08; }
        if (curprof(INGRESS_SOUTH) > 0) { b |= 0x04; }
        if (curprof(INGRESS_EAST) > 0) { b |= 0x02; }
        if (curprof(INGRESS_WEST) > 0) { b |= 0x01; }
        if (b)
        {
          fprintf(fp, "%x", b);
        } else
        {
          fprintf(fp, ".");
        }
      }
      fprintf(fp, "\n");
    }
    fprintf(fp, "END map %d\n", PROFILE_HANDLES + 1);

    fprintf(fp, "BEGIN map %d; bf93prof %s ", PROFILE_HANDLES + 2, filename);
    fprintf(fp, "all possible ip egresses (N8S4E2W1)\n");

    for (y=0;y<PAGEHEIGHT;y++)
    {
      for (x=0;x<LINEWIDTH;x++)
      {
        int b=0;
        if (curprof(EGRESS_NORTH) > 0) { b |= 0x08; }
        if (curprof(EGRESS_SOUTH) > 0) { b |= 0x04; }
        if (curprof(EGRESS_EAST) > 0) { b |= 0x02; }
        if (curprof(EGRESS_WEST) > 0) { b |= 0x01; }
        if (b)
        {
          fprintf(fp, "%x", b);
        } else
        {
          fprintf(fp, ".");
        }
      }
      fprintf(fp, "\n");
    }
    fprintf(fp, "END map %d\n", PROFILE_HANDLES + 2);

    fprintf(fp, "BEGIN map %d; bf93prof %s ", PROFILE_HANDLES + 3, filename);
    fprintf(fp, "cells by: get (4) put (2) execute (1)\n");

    for (y=0;y<PAGEHEIGHT;y++)
    {
      for (x=0;x<LINEWIDTH;x++)
      {
        int b=0;
        if ((curprof(EGRESS_NORTH) > 0) ||
            (curprof(EGRESS_SOUTH) > 0) ||
            (curprof(EGRESS_EAST) > 0) ||
            (curprof(EGRESS_WEST) > 0) ||
            (curprof(INGRESS_NORTH) > 0) ||
            (curprof(INGRESS_SOUTH) > 0) ||
            (curprof(INGRESS_EAST) > 0) ||
            (curprof(INGRESS_WEST) > 0))
        {
          b |= 0x01;
        }
        if (curprof(PUT_CELL) > 0)
        {
          b |= 0x02;
        }
        if (curprof(GET_CELL) > 0)
        {
          b |= 0x04;
        }
        if (b)
        {
          fprintf(fp, "%x", b);
        } else
        {
          fprintf(fp, ".");
        }
      }
      fprintf(fp, "\n");
    }
    fprintf(fp, "END map %d\n", PROFILE_HANDLES + 3);

    fclose (fp);
  }

  free (pg);
  free (pgbuf);
  for(i=0;i<PROFILE_HANDLES;i++)
  {
    free (prof[i]);
  }

  exit (0);
  return 0;
}

/*
 * pushes a value onto the stack.
 */
void push (val)
    signed long val;
{
  struct stack *s;
  s = (struct stack *) malloc (sizeof (struct stack));
  s->val = val;
  s->next = head;
  head = s;
}

/*
 * pops a value off the stack. returns 0 in case of underflow.
 */
signed long pop ()
{
  signed long v;
  struct stack *s = head;
  if (s)
  {
    v = head->val;
    head = head->next;
    free (s);
    return v;
  } else
  {
    return 0;
  }
}

/* END of befprof.c */