# 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
        if self.LocalOffset:
            a.AI("SUB ESP, %s" % self.LocalOffset)

    def EmitProcEndCode(self, a):
        """
        Restore settings and RETurn
        TODO: Do we need to handle a Return value here?
        """
        if self.LocalOffset:
            a.AI("ADD ESP, %s" % self.LocalOffset)

        #check for malformed stack
        #a.AI("CMP         EBP,ESP")
        #a.AI("CALL        __chkesp")
        
        a.AI("MOV ESP, EBP")
        a.AI("POP EBP")
        
        if self.Type == STDCALL and self.ArgOffset - 8:
            #HAD ARGS AND IS A STDCALL, CLEANUP
            a.AI("RET %s" % (self.ArgOffset - 8))
        else:
            a.AI("RET")

#
# assembler directive re's
#
strRe = re.compile("\s*" + symbolRe + "\s*" + stringRe + "?$",re.DOTALL)
procRe = re.compile("\s*" + symbolRe +"\s*(?P<TYPE>STDCALL|CDECL|PYTHON)?$")
varRe = re.compile("\s*" + symbolRe + "\s*(?P<NUM>" + Number[1:] + "?$")

# 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

        if self.LocalOffset:
            a.AI("SUB ESP, %s" % self.LocalOffset)

    def EmitProcEndCode(self, a):
        """
        Restore settings and RETurn
        TODO: Do we need to handle a Return value here?
        """
        if self.LocalOffset:
            a.AI("ADD ESP, %s" % self.LocalOffset)

        #check for malformed stack
        #a.AI("CMP         EBP,ESP")
        #a.AI("CALL        __chkesp")
        
        a.AI("MOV ESP, EBP")
        a.AI("POP EBP")
        
        if self.Type == STDCALL and self.ArgOffset - 8:
            #HAD ARGS AND IS A STDCALL, CLEANUP
            a.AI("RET %s" % (self.ArgOffset - 8))
        else:
            a.AI("RET")

#
# assembler directive re's
#
strRe = re.compile("\s*" + symbolRe + "\s*" + stringRe + "?$",re.DOTALL)
procRe = re.compile("\s*" + symbolRe +"\s*(?P<TYPE>STDCALL|CDECL|PYTHON)?$")
varRe = re.compile("\s*" + symbolRe + "\s*(?P<NUM>" + Number[1:] + "?$")

callRe = re.compile("\s*(%s|%s)\s*(?P<REST>.*)" % (symbolRe, stringRe))