/*
//////////////////////////////////////////////////////
//////                driver.c                  //////
//////////////////////////////////////////////////////
*/


/*
/////////////////////////////
//
// Author: Gayatri Krishnan
// Assignment # 8
// Internet Applications
// 
/////////////////////////////
*/


/*
////
// include files
////
*/
#include <stdio.h>
#include "common.h"

/*
 ** Function : read_bytes()
 */
int read_bytes(int num_of_bytes)
{
  char input1, input2;
  int count = 0;
  short value1,value2,value;
 
  while (count < num_of_bytes)
    {
      if ((input1=getc(stdin)) ==-1) 
      return(-1);
      while  ((input1 == '\n') ||(input1 == ' ')) 
	{
	  input1 = getc(stdin);
	}
      if (input1 == -1) 
     return(-1);
      if ((input2=getc(stdin))==-1) return(-1);
      while  ((input2 == '\n') ||(input2 == ' ')) 
	{
	  input2 = getc(stdin);
	}
      if (input2==-1) return(-1);

      /*      printf("%c %c\n",input1,input2); */

      value1 = convert(input1);
      value2 = convert(input2);

      value = value1<<4 | value2;
      byte[count] = value;
      
      count++;
    }
  return(0);
}

/*
 ** Function : convert()
 */
short convert(char input_char)
{
  if (input_char < 'a') return(input_char -'0');
   else return(input_char - 'a'+10);
}

/*
 ** Function : print_bytes()
 */
void print_bytes(int num_of_bytes)
{
  int count=0;
  
  
  while (count < num_of_bytes)
  {
    

    printf("%d ", byte[count]);
    count++;
  }
  printf("\n");
}

/*
 ** Function : print_opcodes()
 */
struct sp_opcodes * print_opcodes(int num_of_bytes, int opcode_count)
{
  int count=0;
  int num;

char opcode[265][29] = {
{"nop"},
{"aconst_null"},
{"iconst_m1"},
{"iconst_0"},
{"iconst_1"},
{"iconst_2"},
{"iconst_3"},
{"iconst_4"},
{"iconst_5"},
{"lconst_0"},
{"lconst_1"},
{"fconst_0"},
{"fconst_1"},
{"fconst_2"},
{"dconst_0"},
{"dconst_1"},
{"bipush"},
{"sipush"},
{"ldc1"},
{"ldc2"},
{"ldc2w"},
{"iload"},
{"lload"},
{"fload"},
{"dload"},
{"aload"},
{"iload_0"},
{"iload_1"},
{"iload_2"},
{"iload_3"},
{"lload_0"},
{"lload_1"},
{"lload_2"},
{"lload_3"},
{"fload_0"},
{"fload_1"},
{"fload_2"},
{"fload_3"},
{"dload_0"},
{"dload_1"},
{"dload_2"},
{"dload_3"},
{"aload_0"},
{"aload_1"},
{"aload_2"},
{"aload_3"},
{"iaload"},
{"laload"},
{"faload"},
{"daload"},
{"aaload"},
{"baload"},
{"caload"},
{"saload"},
{"istore"},
{"lstore"},
{"fstore"},
{"dstore"},
{"astore"},
{"istore_0"},
{"istore_1"},
{"istore_2"},
{"istore_3"},
{"lstore_0"},
{"lstore_1"},
{"lstore_2"},
{"lstore_3"},
{"fstore_0"},
{"fstore_1"},
{"fstore_2"},
{"fstore_3"},
{"dstore_0"},
{"dstore_1"},
{"dstore_2"},
{"dstore_3"},
{"astore_0"},
{"astore_1"},
{"astore_2"},
{"astore_3"},
{"iastore"},
{"lastore"},
{"fastore"},
{"dastore"},
{"aastore"},
{"bastore"},
{"castore"},
{"sastore"},
{"pop"},
{"pop2"},
{"dup"},
{"dup_x1"},
{"dup_x2"},
{"dup2"},
{"dup2_x1"},
{"dup2_x2"},
{"swap"},
{"iadd"},
{"ladd"},
{"fadd"},
{"dadd"},
{"isub"},
{"lsub"},
{"fsub"},
{"dsub"},
{"imul"},
{"lmul"},
{"fmul"},
{"dmul"},
{"idiv"},
{"ldiv"},
{"fdiv"},
{"ddiv"},
{"imod"},
{"lmod"},
{"fmod"},
{"dmod"},
{"ineg"},
{"lneg"},
{"fneg"},
{"dneg"},
{"ishl"},
{"lshl"},
{"ishr"},
{"lshr"},
{"iushr"},
{"lushr"},
{"iand"},
{"land"},
{"ior"},
{"lor"},
{"ixor"},
{"lxor"},
{"iinc"},
{"i2l"},
{"i2f"},
{"i2d"},
{"l2i"},
{"l2f"},
{"l2d"},
{"f2i"},
{"f2l"},
{"f2d"},
{"d2i"},
{"d2l"},
{"d2f"},
{"int2byte"},
{"int2char"},
{"int2short"},
{"lcmp"},
{"fcmpl"},
{"fcmpg"},
{"dcmpl"},
{"dcmpg"},
{"ifeq"},
{"ifne"},
{"iflt"},
{"ifge"},
{"ifgt"},
{"ifle"},
{"if_icmpeq"},
{"if_icmpne"},
{"if_icmplt"},
{"if_icmpge"},
{"if_icmpgt"},
{"if_icmple"},
{"if_acmpeq"},
{"if_acmpne"},
{"goto"},
{"jsr"},
{"ret"},
{"tableswitch"},
{"lookupswitch"},
{"ireturn"},
{"lreturn"},
{"freturn"},
{"dreturn"},
{"areturn"},
{"return"},
{"getstatic"},
{"putstatic"},
{"getfield"},
{"putfield"},
{"invokevirtual"},
{"invokenonvirtual"},
{"invokestatic"},
{"invokeinterface"},
{"newfromname"},
{"new"},
{"newarray"},
{"anewarray"},
{"arraylength"},
{"athrow"},
{"checkcast"},
{"instanceof"},
{"monitorenter"},
{"monitorexit"},
{"wide"},
{"multianewarray"},
{"ifnull"},
{"ifnonull"},
{"goto_w"},
{"jsr_w"},
{"breakpoint"},
{"ldc1_quick"},
{"ldc2_quick"},
{"ldc2w_quick"},
{"anewarray_quick"},
{"multianewarray_quick"},
{"putfield_quick"},
{"putfield2_quick"},
{"getfield_quick"},
{"getfield2_quick"},
{"putstatic_quick"},
{"putstatic2_quick"},
{"getstatic_quick"},
{"getstatic2_quick"},
{"invokevirtual_quick"},
{"invokevirtualobject_quick"},
{"invokenonvirtual_quick"},
{"invokestatic_quick"},
{"invokeinterface_quick"},
{"new_quick"},
{"checkcast_quick"},
{"instanceof_quick"},
{""},
{""},
{""},
{""},
{""},
{""},
{""},
{""},
{""},
{""},
{""},
{""},
{""},
{""},
{""},
{""},
{""},
{""},
{""},
{""},
{""},
{""},
{""},
{""},
{""},
{""},
{""},
{""},
{""},
{""},
{""},
{"__254"},
{"__255"}
};

int numOfAttributes[265] = {
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1,
2,
1,
2,
2,
1,
1,
1,
1,
1,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1,
1,
1,
1,
1,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
2,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
1,
15,
11,
0,
0,
0,
0,
0,
0,
2,
2,
2,
2,
2,
2,
2,
4,
9,
2,
1,
2,
0,
0,
2,
2,
0,
0,
1,
3,
2,
2,
4,
4,
0,
1, 
2,
2,
2,
3,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
4,
2,
2,
2,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
};

 static struct sp_opcodes gotoif; 
      
  while (count < num_of_bytes) {
    num = byte[count];
  
    if (num > 255) {
      printf (" %d No corresponding Opcode \n",byte[count]);
      count ++;
    }
    else {
      printf("%d %s", opcode_count, opcode[num]);
      count++;
    }/* else */
  }/* while */
  gotoif.attr = numOfAttributes[num];
  gotoif.op_num = num;
  strcpy(gotoif.op_name, opcode[num]);
  return &gotoif;
}/* print_opcode */



