/* This file is automatically produced by AEC.  Do NOT edit. */

/* aec-file-prologue.
   Boiler plate prologue code for files produced by AEC.
   Copyright (C) 1989, 1990 by James R. Larus (larus@cs.wisc.edu).

   AE and AEC are free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 1, or (at your option) any
   later version.

   AE and AEC are distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with GNU CC; see the file COPYING.  If not, write to James R.
   Larus, Computer Sciences Department, University of Wisconsin--Madison,
   1210 West Dayton Street, Madison, WI 53706, USA or to the Free
   Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */


/* $Header: /var/home/larus/AE/AEC/RCS/aec-file-prologue,v 2.0 90/02/09 17:23:39 larus Exp Locker: larus $ */


#include <sys/file.h>
#include <setjmp.h>
#include <stdio.h>
#include "aec.h"

static FILE *__ae_in_file;


#ifdef BYTES_BIG_ENDIAN
#define B_TO_H(b1, b2) (b2 | (b1 << 8))
#else
#define B_TO_H(b2, b1) (b2 | (b1 << 8))
#endif


#define GET_WORD(d) {d = getw (__ae_in_file); \
		     if (feof (__ae_in_file)) eof_err ();}

#define GET_HALF(d) {d = getc (__ae_in_file); \
		     d = B_TO_H (d, getc (__ae_in_file)); \
		     if (feof (__ae_in_file)) eof_err ();}

#define GET_BYTE(d) {d = getc (__ae_in_file); \
		     if (feof (__ae_in_file)) eof_err ();}


#define EAT_HALF() {getc (__ae_in_file); getc (__ae_in_file); \
		    if (feof (__ae_in_file)) eof_err ();}

#define EAT_BYTE() {getc (__ae_in_file); \
		    if (feof (__ae_in_file)) eof_err ();}



static void eof_err()
{
  fprintf(stderr, "Read beyond end of event file.\n");
  exit(-1);
}


static int temp;

static void (*out_func)();


#define ISSUE_INST(n) {out_func(INST_REF, PC, n); PC += 4 * n;}

#define READ_MEM(x) out_func(MEM_READ, x)

#define WRITE_MEM(x) out_func(MEM_WRITE, x)

#define LOOP_START(n) out_func(START_LOOP, n)

#define LOOP_CONT(n) out_func(CONT_LOOP, n)

#define LOOP_END(n) out_func(END_LOOP, n)


static void cjump_err()
{
  fprintf(stderr, "Target of jump unknown.\n");
  exit(-1);
}


static void func_err()
{
  fprintf(stderr, "Event PC does not match address of function.\n");
  exit(-1);
}


typedef int (*PFI)();
static PFI address_to_callee();


/* Exit returns to top level in AE_RECREATE. */

static jmp_buf __ae_top_level_env;

static int __ae_exit () {longjmp(__ae_top_level_env, 1);}

static int __ae__exit () {longjmp(__ae_top_level_env, 1);}


/* Used in debugging mode: */

#define SVI(var, value) {if (var != (int *) -1) \
			  *var = value; \
			else \
			  { \
			    var = (int *) malloc(sizeof(int)); \
			    *var = value; \
			  }}


#define SVF(var, value) {if (var != (int *) -1) \
			  *var = value; \
			else \
			  { \
			    var = (float *) malloc(sizeof(float)); \
			    *var = value; \
			  }}
