/*

File: instruction_set.c
Author: Neil Cafferkey
Copyright (C) 1999-2001 Neil Cafferkey

This program is 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 2
of the License, or (at your option) any later version.

This program is 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA.

*/


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

#include "instruction_set_protos.h"
#include "memory_protos.h"
#include "instruction_protos.h"
#include "general_protos.h"
#include "string_protos.h"
#include "sequence_protos.h"
#include "address_protos.h"


/* Function: ReadInstructionSet
 * ============================
 */

InstructionSet ReadInstructionSet(StringSet file_names)
{
   InstructionSet instruction_set=CreateSet();
   TEXT *string,*substring;

   while(!IsEmptySet(file_names))
   {
      string=ReadString(TakeFromSet(file_names));

      /* Find instruction data */

      substring=string;

      while((substring=(TEXT *)strstr(substring,"@INSTRUCTION"))!=NULL)
      {
         substring=(TEXT *)strchr(substring,'\n')+1;
         PutInSet(instruction_set,ReadInstruction(substring));
      }

      KillString(string);
   }

   KillSet(file_names);

   return instruction_set;
}


/* Function: GetMatchingInstruction
 * ================================
 */

Instruction GetMatchingInstruction(InstructionSet set,Address address,
   SourceProgram src_prog)
{
   InstructionSetNode node=set->first;
   Instruction instruction;
   BOOL found;
   UBYTE *address_data=GetAddressContents(address,src_prog);

   for(found=FALSE;(node!=NULL)&&!found;node=node->next)
      found=IsSubsequence(GetInstructionPrefix(instruction=node->element),
      address_data);


/*   while(!IsEmptySet(temp_set)&&!found)
   {
      instruction=TakeFromSet(temp_set);
      found=IsSubsequence(GetInstructionPrefix(instruction),address_data);
   }*/

   if(found)
      return instruction;
   else
      return NULL;
}


#ifndef NDEBUG

/* Function: ShowInstructionSet
 * ============================
 * Prints out the contents of the instruction set for testing.
 */

VOID ShowInstructionSet(InstructionSet set)
{
   InstructionSet temp_set=DupSet(set);

   printf("----------------------------------------------------------\n");
   while(!IsEmptySet(temp_set))
      ShowInstruction(TakeFromSet(temp_set));

   KillSet(temp_set);

   return;
}

#endif


#ifdef TEST

#include <assert.h>


LONG main(VOID)
{
   InstructionSet set=CreateSet(),set2;

   PutInSet(set,7);
   PutInSet(set,4);
   PutInSet(set,18);

   assert(IsInSet(set,18));
   assert(IsInSet(set,18));
   assert(!IsInSet(set,0));
   assert(!IsEmptySet(set));

   set2=DupSet(set);

   printf("%d\n",TakeFromSet(set));
   printf("%d\n",TakeFromSet(set));
   printf("%d\n",TakeFromSet(set));

   ShowInstructionSet(set2);

   printf("%d\n",TakeFromSet(set2));
   printf("%d\n",TakeFromSet(set2));
   printf("%d\n",TakeFromSet(set2));

   assert(IsEmptySet(set2));
   assert(TakeFromSet(set)==NULL);
   assert(!IsInSet(set,18));

   KillSet(set);

   return EXIT_SUCCESS;
}

#endif


