145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# Python bindings for Yasm: Main Pyrex input file 245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# 345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# Copyright (C) 2006 Michael Urman, Peter Johnson 445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# 545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# Redistribution and use in source and binary forms, with or without 645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# modification, are permitted provided that the following conditions 745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# are met: 845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# 1. Redistributions of source code must retain the above copyright 945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# notice, this list of conditions and the following disclaimer. 1045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# 2. Redistributions in binary form must reproduce the above copyright 1145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# notice, this list of conditions and the following disclaimer in the 1245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# documentation and/or other materials provided with the distribution. 1345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# 1445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS'' 1545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE 1845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 1945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# POSSIBILITY OF SUCH DAMAGE. 2545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org"""Interface to the Yasm library. 2645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 2745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgThe Yasm library (aka libyasm) provides the core functionality of the Yasm 2845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgassembler. Classes in this library provide for manipulation of machine 2945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orginstructions and object file constructs such as symbol tables and sections. 3045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 3145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgExpression objects encapsulate complex expressions containing registers, 3245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgsymbols, and operations such as SEG. 3345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 3445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgBytecode objects encapsulate data or code objects such as data, reserve, 3545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgalign, or instructions. 3645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 3745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgSection objects encapsulate an object file section, including the section 3845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgname, any Bytecode objects contained within that section, and other 3945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orginformation. 4045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 4145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org""" 4245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 4345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgcdef extern from "Python.h": 4445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org cdef object PyCObject_FromVoidPtr(void *cobj, void (*destr)(void *)) 4545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org cdef object PyCObject_FromVoidPtrAndDesc(void *cobj, void *desc, 4645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org void (*destr)(void *, void *)) 4745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org cdef int PyType_Check(object) 4845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org cdef int PyCObject_Check(object) 4945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org cdef void *PyCObject_AsVoidPtr(object) 5045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org cdef void *PyCObject_GetDesc(object) 5145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 5245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org cdef object _PyLong_FromByteArray(unsigned char *bytes, unsigned int n, 5345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org int little_endian, int is_signed) 5445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org cdef int _PyLong_AsByteArray(object v, unsigned char *bytes, unsigned int n, 5545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org int little_endian, int is_signed) except -1 5645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 5745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org cdef void Py_INCREF(object o) 5845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org cdef void Py_DECREF(object o) 5945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 6045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org cdef void PyErr_SetString(object type, char *message) 6145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org cdef object PyErr_Format(object type, char *format, ...) 6245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 6345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgcdef extern from "stdlib.h": 6445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org cdef void *malloc(int n) 6545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org cdef void free(void *p) 6645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 6745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orginclude "_yasm.pxi" 6845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 6945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgcdef object __pass_voidp(void *obj, object forclass): 7045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org return PyCObject_FromVoidPtrAndDesc(obj, <void *>forclass, NULL) 7145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 7245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgcdef void *__get_voidp(object obj, object forclass) except NULL: 7345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org cdef void* desc 7445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 7545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if not PyCObject_Check(obj): 7645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org msg = "obj %r is not a CObject" % obj 7745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org PyErr_SetString(TypeError, msg) 7845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org return NULL 7945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 8045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org desc = PyCObject_GetDesc(obj) 8145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 8245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if desc != <void *>forclass: 8345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if desc == NULL: 8445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org msg = "CObject type is not set (expecting %s)" % forclass 8545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org elif PyType_Check(<object>desc): 8645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org msg = "CObject is for %s not %s" % (<object>desc, forclass) 8745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org else: 8845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org msg = "CObject is incorrect (expecting %s)" % forclass 8945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org PyErr_SetString(TypeError, msg) 9045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org return NULL 9145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 9245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org return PyCObject_AsVoidPtr(obj) 9345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 9445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# 9545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# Link to associated data mechanism to keep Python references paired with 9645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# yasm objects. 9745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# 9845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgcdef class __assoc_data_callback: 9945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org cdef yasm_assoc_data_callback *cb 1008c35491afd495d828e8f43e3293ef9d9e145c751hbono@chromium.org def __cinit__(self, destroy, print_): 10145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org self.cb = <yasm_assoc_data_callback *>malloc(sizeof(yasm_assoc_data_callback)) 10245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org self.cb.destroy = <void (*) (void *)>PyCObject_AsVoidPtr(destroy) 10345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org #self.cb.print_ = <void (*) (void *, FILE *, int)>PyCObject_AsVoidPtr(print_) 10445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org def __dealloc__(self): 10545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org free(self.cb) 10645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 10745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 10845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgcdef class Register: 10945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org cdef unsigned long reg 1108c35491afd495d828e8f43e3293ef9d9e145c751hbono@chromium.org def __cinit__(self, reg): 11145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org self.reg = reg 11245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 11345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orginclude "errwarn.pxi" 11445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orginclude "intnum.pxi" 11545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orginclude "floatnum.pxi" 11645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orginclude "expr.pxi" 11745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orginclude "symrec.pxi" 11845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orginclude "value.pxi" 11945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 12045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orginclude "bytecode.pxi" 12145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 12245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgcdef __initialize(): 12345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org BitVector_Boot() 12445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org yasm_intnum_initialize() 12545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org yasm_floatnum_initialize() 12645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org yasm_errwarn_initialize() 12745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 12845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgdef __cleanup(): 12945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org yasm_floatnum_cleanup() 13045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org yasm_intnum_cleanup() 13145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org yasm_errwarn_cleanup() 13245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org BitVector_Shutdown() 13345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 13445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org__initialize() 13545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgimport atexit 13645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgatexit.register(__cleanup) 13745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 138