From: pviswana@uceng.UC.EDU (Prashanth V.)
Newsgroups: comp.lang.fortran
Subject: Low level routines to do logical shifts
Keywords: lshft,rshft etc.
Date: 18 Aug 92 17:47:15 GMT
Organization: College of Engineering, University of Cincinnati

Hello:


I am in a desparate need to translate the following program
into Fortran. The problem I am having is this:

THe parser that is available to translate the fortran code
to the instruction set of my choice does not have the
capability to understand any of the Fortran 90 commands like
shiftleft and shift right mod and so on.. 

I also do not know how to handle while loops in fortran. ( How
stupid of me :-)).

Is there anyone out there who can help me translate the following
complex code to simple fortran77 compatible code. Yes, I am
afraid we have to deal with pointers and stuff. I would also
be happy with a simple solutions like having to initialize
all datastructure elemets with identifiers.

I appreciate any help from any/all of you: The  code follows:
------------- TO BE CONVERTED TO FORTRAN ------------------
#include <fcntl.h>
#include <sys/time.h>
#include <errno.h>

#define false 0
#define true 1

#define ADD 0
#define SUB 1
#define MUL 2
#define DIV 3
#define AND 4
#define BAL 5
#define BEQ 6
#define HALT 7

#define NUMARGS 3

#define MAXPROCESSORS 1024
#define MAXINSTRUCTIONS 10
#define MAXDATA 512
#define MAXPROGRAMS MAXPROCESSORS
#define MAXDATAVAL 4096

typedef short Boolean;

typedef int data_t;

typedef struct prog{
  int num_inst;
  int num_data;
  int PC;
  long instructions[2*MAXINSTRUCTIONS];
  data_t data[MAXDATA];
}prog_t;

typedef struct prog_struct{
  int num_progs;
  prog_t program[MAXPROCESSORS];
}prog_struct_t;



Boolean sane(inst, arg, num_inst, num_data)
int inst, arg, num_inst, num_data;
{
Boolean toreturn;

  if ((inst < BEQ) || (arg < NUMARGS - 1))
    toreturn = ((arg >= 0) && (arg < num_data));
  else
    toreturn = ((arg >= 0) && (arg < num_inst));
  return(toreturn);
}

int RanInt(low, high)
int low, high;
{
long ret;
int toreturn;

  ret = my_random();
  ret = ret % (high - low + 1);
  ret = ret + low;
  toreturn = (int)ret;
  return(toreturn);
}

void PutData()
{
prog_struct_t programs;
Boolean worked;
int num_progs;
int fd;
int prog;
int inst, data, arg;
int loop1,loop2;
long instr,arg1,arg2,arg3;
short mode1,mode2,mode3;
long this_one;
Boolean inviolate[MAXINSTRUCTIONS];
#define current programs.program[prog]

  fd = open("programs",O_WRONLY|O_CREAT,0700);
  if (fd < 0)
    perror("open failed");
  programs.num_progs = MAXPROGRAMS;
  write(fd,&(programs.num_progs), sizeof(programs.num_progs));
  num_progs = programs.num_progs;
  for (prog = 0; prog < num_progs; prog++){
    for(loop1=0;loop1<MAXINSTRUCTIONS;++loop1)
      inviolate[loop1] = false;
  /* printf("Writing program %d\n",prog); */
    current.num_inst = MAXINSTRUCTIONS;
    write(fd, &(current.num_inst), sizeof(current.num_inst));
    current.num_data = MAXDATA;
    write(fd, &(current.num_data), sizeof(current.num_data));
    for (inst = 0; inst < current.num_inst; inst++){
      if (inst < current.num_inst - 1){
        instr = RanInt(ADD,BEQ);
	mode1 = RanInt(0,2);
	mode2 = RanInt(0,2);
	mode3 = RanInt(0,2);
        arg1  = RanInt(1,current.num_data - 1);
        arg2  = RanInt(1,current.num_data - 1);
        arg3  = RanInt(1,current.num_data - 1);
					
        if (instr == BEQ) {
          arg3 = inst + 1;
	  inviolate[inst+1] = true;
        }
 	else if (instr == BAL) {
          arg2 = inst + 1;
          inviolate[inst+1] = true;
 	}
      } else {
          instr = HALT;
      }
	instr <<= 8;
        instr |= mode1 << 4;
        instr |= mode2 << 2;
        instr |= mode3;
        current.instructions[2*inst] =     ((instr & 0xffff) << 16) | 
                                           (arg1  & 0xffff);
        current.instructions[2*inst + 1] = ((arg2  & 0xffff) << 16) | 
                                           (arg3  & 0xffff);
      }
      for (inst = 0; inst < current.num_inst; inst++){
        if (inviolate[inst]) {
          for(loop1=0;loop1<current.num_inst;loop1++) {
            instr = (current.instructions[2*loop1] >> 16) & 0xffff;
            arg1  = (current.instructions[2*loop1]) & 0xffff;
            arg2  = (current.instructions[2*loop1+1] >> 16) & 0xffff;
            arg3  = (current.instructions[2*loop1+1])       & 0xffff;
            this_one = instr >> 8;
	    if ((arg3 == inst) &&
	        (this_one != BEQ)) {
              arg3 = 0;
            }
            current.instructions[2*loop1] =     ((instr & 0xffff) << 16) | 
                                               (arg1  & 0xffff);
            current.instructions[2*loop1 + 1] = ((arg2  & 0xffff) << 16) | 
                                               (arg3  & 0xffff);
          }
	}
      }
      write(fd, current.instructions, current.num_inst*2*sizeof(long));
      for (data = 0; data < current.num_data; data++){
/*		*data_ptr = RanInt(0,MAXDATAVAL);*/
        current.data[data] = data;
      }
      write(fd, current.data,current.num_data*sizeof(data_t));
    }
    for (loop1 = 0;loop1 < 20;++loop1) {
      printf("Program %d:\n",loop1);
      for (loop2 = 0;loop2 < 10; ++loop2) {
        instr = (programs.program[loop1].instructions[2*loop2] >> 16) & 0xffff;
        arg1  = (programs.program[loop1].instructions[2*loop2]) & 0xffff;
        arg2 = (programs.program[loop1].instructions[2*loop2+1] >> 16) & 0xffff;
        arg3 = (programs.program[loop1].instructions[2*loop2+1])       & 0xffff;
        this_one = instr >> 8;
        mode1 = (instr & 0xff) >> 4;
        mode2 = (instr & 0xf) >> 2;
        mode3 = (instr & 0x3);
        printf("inst = %d\n",this_one);
        printf("mode1 = %d\n",mode1);
        printf("mode2 = %d\n",mode2);
        printf("mode3 = %d\n",mode3);
        printf("arg1 = %d\n",arg1);
        printf("arg2 = %d\n",arg2);
        printf("arg3 = %d\n",arg3);		
      }
    }
}


void InitRanSeed()
{
struct timeval tval;
struct timezone tzone;

  gettimeofday(&tval, &tzone);
  srandom(tval.tv_sec);
}

main() 
{
InitRanSeed();
PutData();
}
-- 
Prashanth Viswanath				 Elemental empathy
pviswana@uceng.uc.edu				 A change of synergy
pviswana@thor.ece.uc.edu			 Music making contact, 
					 	 Naturally