# 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):


import logging
import struct
from pickle import decode_long, encode_long
from x86tokenizer import (tokenizeInstDef,tokenizeInst,
                          REGISTER,OPCODE,COMMA,OPERAND,
                          LBRACKET, RBRACKET,NUMBER,SYMBOL)
                #TODO: Can we handle this special case better?
                #      The special case is a m8/16/32 value that is constant
                #      and doesn't have an RM.
                if firstDef[0] == REGISTER and firstDef[1][0] == '[' and \
                   firstTok[0] == LBRACKET:
                    firstTok, restTok = restTok[0],restTok[1:]
                    if firstTok[1] == firstDef[1][1:-1]:
                        firstTok, restTok = restTok[0],restTok[1:]
                    else:
                        raise x86instError("TOKEN MISMATCH '%s' '%s'" % \
                                           (firstDef,firstTok))
                elif firstDef[0] != firstTok[0]:
                    raise x86instError("These should be equal '%s' '%s'" % \
                                       (firstDef, firstTok))
            elif firstDef[0] == NUMBER:
                if firstTok[0] != NUMBER or firstTok[1] != firstDef[1]:
                    raise x86instError("INVALID NUMBER '%s'" % repr(firstTok))
            elif firstDef[0] == OPERAND:
                if firstDef[1] in ('r/m32','r/m16','r/m8'):
                    #figure out r/m val
                    if firstTok[0] == REGISTER:
                        #figure out r val
                        registerName = firstTok[1]
                        registerType = firstDef[1][0] + firstDef[1][3:]
                        registerVal = regOpcode[registerType].index(registerName)
                        if registerVal < 0:
                            raise x86instError("Couldn't resolve register '%s'" % registerName)
                        else:
                            tmpModRM.Mode = 3
                            tmpModRM.RM = registerVal

# 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
import logging
import struct
from pickle import decode_long, encode_long
from x86tokenizer import (tokenizeInstDef,tokenizeInst,
                          REGISTER,OPCODE,COMMA,OPERAND,
                          LBRACKET, RBRACKET,NUMBER,SYMBOL)

                #TODO: Can we handle this special case better?
                #      The special case is a m8/16/32 value that is constant
                #      and doesn't have an RM.
                if firstDef[0] == REGISTER and firstDef[1][0] == '[' and \
                   firstTok[0] == LBRACKET:
                    firstTok, restTok = restTok[0],restTok[1:]
                    if firstTok[1] == firstDef[1][1:-1]:
                        firstTok, restTok = restTok[0],restTok[1:]
                    else:
                        raise x86instError("TOKEN MISMATCH '%s' '%s'" % \
                                           (firstDef,firstTok))
                elif firstDef[0] != firstTok[0]:
                    raise x86instError("These should be equal '%s' '%s'" % \
                                       (firstDef, firstTok))
            elif firstDef[0] == NUMBER:
                if firstTok[0] != NUMBER or firstTok[1] != firstDef[1]:
                    raise x86instError("INVALID NUMBER '%s'" % repr(firstTok))
            elif firstDef[0] == OPERAND:
                if firstDef[1] in ('r/m32','r/m16','r/m8'):
                    #figure out r/m val
                    if firstTok[0] == REGISTER:
                        #figure out r val
                        registerName = firstTok[1]
                        registerType = firstDef[1][0] + firstDef[1][3:]
                        registerVal = regOpcode[registerType].index(registerName)
                        if registerVal < 0:
                            raise x86instError("Couldn't resolve register '%s'" % registerName)
                        else:
                            tmpModRM.Mode = 3
                            tmpModRM.RM = registerVal

                    elif firstTok[0] == LBRACKET: