# Path: x86tokenizer.py
# class tokenizeError(Exception):pass
# REGISTER,OPCODE,COMMA,OPERAND,LBRACKET,RBRACKET,NUMBER,SYMBOL,STRING = range(1,10)
# def tokenizeString(s,reToProcess):
# def tokenizeInstDef(s):
# def tokenizeInst(s):
# 
# Path: x86inst.py
# class OpcodeTooShort(Exception):pass
# class OpcodeNeedsModRM(Exception):pass # for /d info and SIB calculation
# class x86instError(Exception):pass
# class OpcodeDict(dict):
# class MnemonicDict(dict):
# class ModRM:
# class instruction:
# class instructionInstance:
#     def __getitem__(self,key):
#     def __setitem__(self,key,value):
#     def GetOp(self,opcode,modRM=None):
#     def __setitem__(self,key,val):
# def longToBytes(long, bytes=4):
# def longToString(long, bytes=4):
# def longToBytesRepr(long,bytes=4):
#     def __init__(self,byte=None):
#     def LoadFromByte(self,byte):
#     def SaveToByte(self):
#     def HasSIB(self):
#     def RegOpString(self,typ):
#     def RMString(self,typ=None):
#     def GetDisplacementSize(self):
#     def __init__(self,opstr,inststr,desc):
#     def loadRBWD(self,plus,reg):
#     def setOpcodeAndFlags(self):
#     def setHasFlags(self):
#     def GetInstance(self):
#     def __str__(self):
#     def __init__(self,inst):
#     def GetSymbolPatchins(self,modrm=None):
#     def GetSuffixSize(self,modrm=None):
#     def GetInstructionSize(self):
#     def NextInstructionLoc(self):
#     def LoadData(self, data):
#     def DataText(self,data,size,skip=False):
#     def LoadConcreteValues(self, toks):
#     def OpDataAsString(self):
#     def OpText(self):
#             OS = self.OpcodeString
#             IS = self.InstructionString
#             ID = self.Description
#             OS = OS.replace(plus,plus[1:])
#             OS = OS.replace("%X" % self.Opcode[0], '%X' % (self.Opcode[0] + i))
#             IS = IS.replace(reg, regOpcode[reg][i])
# RELATIVE,DIRECT = range(1,3)


from x86tokenizer import (tokenizeInst,
                          REGISTER,OPCODE,COMMA,OPERAND,
                          LBRACKET,RBRACKET,NUMBER,SYMBOL,STRING,
                          symbolRe,stringRe)
from x86inst import mnemonicDict, rb, rw, rd, instructionInstance
from tokenize import Number
from pyasm.loggers import x86asmLogger, x86sourceLogger, x86apiLogger
import types, re


class x86asmError(Exception): pass

###########################################################
## Find right instruction def based on concrete instruction
###########################################################

def possibleDefault(*toks):
    "By default, a token will just yield itself."
    first,rest = toks[0],toks[1:]
    if not rest:
        yield [first]
    else:
        i = 1
        possibleLookup = getProperLookup(*rest)
        for restMatches in possibleLookup(*rest):
            i += 1
            yldVal = [first]
            yldVal.extend(restMatches)
            yield yldVal

def possibleImmediateOrRelative(*toks):
    # TODO: can we narrow down which one this should be?
    immVals = [(OPERAND,'imm32'),(OPERAND,'imm16')]
    relVals = [(OPERAND,'rel32'),(OPERAND,'rel16')]
    first,rest = toks[0],toks[1:]
    vals = []
    
    #if it's 8 bit, try to grab smaller opcode

# Path: x86tokenizer.py
# class tokenizeError(Exception):pass
# REGISTER,OPCODE,COMMA,OPERAND,LBRACKET,RBRACKET,NUMBER,SYMBOL,STRING = range(1,10)
# def tokenizeString(s,reToProcess):
# def tokenizeInstDef(s):
# def tokenizeInst(s):
# 
# Path: x86inst.py
# class OpcodeTooShort(Exception):pass
# class OpcodeNeedsModRM(Exception):pass # for /d info and SIB calculation
# class x86instError(Exception):pass
# class OpcodeDict(dict):
# class MnemonicDict(dict):
# class ModRM:
# class instruction:
# class instructionInstance:
#     def __getitem__(self,key):
#     def __setitem__(self,key,value):
#     def GetOp(self,opcode,modRM=None):
#     def __setitem__(self,key,val):
# def longToBytes(long, bytes=4):
# def longToString(long, bytes=4):
# def longToBytesRepr(long,bytes=4):
#     def __init__(self,byte=None):
#     def LoadFromByte(self,byte):
#     def SaveToByte(self):
#     def HasSIB(self):
#     def RegOpString(self,typ):
#     def RMString(self,typ=None):
#     def GetDisplacementSize(self):
#     def __init__(self,opstr,inststr,desc):
#     def loadRBWD(self,plus,reg):
#     def setOpcodeAndFlags(self):
#     def setHasFlags(self):
#     def GetInstance(self):
#     def __str__(self):
#     def __init__(self,inst):
#     def GetSymbolPatchins(self,modrm=None):
#     def GetSuffixSize(self,modrm=None):
#     def GetInstructionSize(self):
#     def NextInstructionLoc(self):
#     def LoadData(self, data):
#     def DataText(self,data,size,skip=False):
#     def LoadConcreteValues(self, toks):
#     def OpDataAsString(self):
#     def OpText(self):
#             OS = self.OpcodeString
#             IS = self.InstructionString
#             ID = self.Description
#             OS = OS.replace(plus,plus[1:])
#             OS = OS.replace("%X" % self.Opcode[0], '%X' % (self.Opcode[0] + i))
#             IS = IS.replace(reg, regOpcode[reg][i])
# RELATIVE,DIRECT = range(1,3)


# Path: x86asm.py
from x86tokenizer import (tokenizeInst,
                          REGISTER,OPCODE,COMMA,OPERAND,
                          LBRACKET,RBRACKET,NUMBER,SYMBOL,STRING,
                          symbolRe,stringRe)
from x86inst import mnemonicDict, rb, rw, rd, instructionInstance
from tokenize import Number
from pyasm.loggers import x86asmLogger, x86sourceLogger, x86apiLogger
import types, re



class x86asmError(Exception): pass

###########################################################
## Find right instruction def based on concrete instruction
###########################################################

def possibleDefault(*toks):
    "By default, a token will just yield itself."
    first,rest = toks[0],toks[1:]
    if not rest:
        yield [first]
    else:
        i = 1
        possibleLookup = getProperLookup(*rest)
        for restMatches in possibleLookup(*rest):
            i += 1
            yldVal = [first]
            yldVal.extend(restMatches)
            yield yldVal

def possibleImmediateOrRelative(*toks):
    # TODO: can we narrow down which one this should be?
    immVals = [(OPERAND,'imm32'),(OPERAND,'imm16')]
    relVals = [(OPERAND,'rel32'),(OPERAND,'rel16')]
    first,rest = toks[0],toks[1:]
    vals = []
    
    #if it's 8 bit, try to grab smaller opcode

    if first[0] == NUMBER: