pyasm 0.2 - dynamic x86 assembler for python

JanC usenet_spam at janc.invalid
Sun Mar 13 15:10:14 EST 2005


Stefan Behnel schreef:

> Meaning: Put the assembler into the doc-string of a function. Then 
> use a decorator to run the assembler on the function's __doc__ string 
> and build an assembly function that takes the same arguments to make 
> the assembly function directly callable.

That would 'disable' a programmer's ability to add useful documentation 
in the comments, but I played a bit further with the decorator idea.

The code below makes it possible to write assembler code for different 
architectures (in case pyasm ever supports that ;) and also a Python 
code version to use when running on a system where no assembler code 
can be used.  It prints:

  Hello x86 world!
  Hello python world!
  Hello i386 world!

Note: there is no serious error checking in this code and it's maybe 
not the best possible solution (this is my first decorator code), but 
it seems to work...  :-)

------------------------------
# supported_architectures should be defined in pyasm
# this is just to show how it could work...
supported_architectures = ['x86','i386']

# asm optimization decorator
def asm_optimise(*architectures):
    arch_iter = architectures.__iter__()
    try:
        arch = arch_iter.next()
        while arch not in supported_architectures:
            arch = arch_iter.next()

        def decorate(f):
            # x86 only at this moment  ;-)
            def optimized(self):
                from pyasm.x86asm import codePackageFromFile
                from pyasm.x86cpToMemory import CpToMemory
                from pyasm.pythonConstants import PythonConstants
                import cStringIO
                
                arch_asm = eval('self._asm_%s' % arch)
                cp = codePackageFromFile(cStringIO.StringIO(arch_asm),PythonConstants)
                mem = CpToMemory(cp)
                mem.MakeMemory()
                mem.BindPythonFunctions(globals())
                return __call__()
            return optimized
    except StopIteration:
        # no supported architecture found
        def decorate(f):
            return f

    return decorate


class hello_world(object):
    """Hello world with assembler optimisation for x86."""
    
    _asm_x86 = r"""
    !CHARS hello_str 'Hello x86 world!\n\0'

    !PROC __call__ PYTHON
    !ARG  self
    !ARG  args
        PUSH hello_str
        CALL PySys_WriteStdout
        ADD ESP, 0x4
        MOV EAX,PyNone
        ADD [EAX],1
    !ENDPROC
    """

    @asm_optimise('x86')
    def __call__(self):
        print 'Hello python world!'

test = hello_world()
test()


class hello_world2(object):
    """Hello world with assembler optimisation for unknown architecture."""

    # fake unknown architecture asm  :)
    _asm_unknown = r"""
    !CHARS hello_str 'Hello unknown world!\n\0'

    !PROC __call__ PYTHON
    !ARG  self
    !ARG  args
        PUSH hello_str
        CALL PySys_WriteStdout
        ADD ESP, 0x4
        MOV EAX,PyNone
        ADD [EAX],1
    !ENDPROC
    """

    @asm_optimise('unknown')
    def __call__(self):
        print 'Hello python world!'

test = hello_world2()
test()


class hello_world3(object):
    """Hello world with assembler optimisation for x86."""

    _asm_x86 = r"""
    !CHARS hello_str 'Hello x86 world!\n\0'

    !PROC __call__ PYTHON
    !ARG  self
    !ARG  args
        PUSH hello_str
        CALL PySys_WriteStdout
        ADD ESP, 0x4
        MOV EAX,PyNone
        ADD [EAX],1
    !ENDPROC
    """

    _asm_i386 = r"""
    !CHARS hello_str 'Hello i386 world!\n\0'

    !PROC __call__ PYTHON
    !ARG  self
    !ARG  args
        PUSH hello_str
        CALL PySys_WriteStdout
        ADD ESP, 0x4
        MOV EAX,PyNone
        ADD [EAX],1
    !ENDPROC
    """

    # i386 will be tried first, then x86
    @asm_optimise('i386','x86')
    def __call__(self):
        print 'Hello python world!'

test = hello_world3()
test()
------------------------------


-- 
JanC

"Be strict when sending and tolerant when receiving."
RFC 1958 - Architectural Principles of the Internet - section 3.9



More information about the Python-list mailing list