1# Python bindings for Yasm: Main Pyrex input file 2# 3# Copyright (C) 2006 Michael Urman, Peter Johnson 4# 5# Redistribution and use in source and binary forms, with or without 6# modification, are permitted provided that the following conditions 7# are met: 8# 1. Redistributions of source code must retain the above copyright 9# notice, this list of conditions and the following disclaimer. 10# 2. Redistributions in binary form must reproduce the above copyright 11# notice, this list of conditions and the following disclaimer in the 12# documentation and/or other materials provided with the distribution. 13# 14# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS'' 15# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE 18# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 19# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 20# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 21# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 22# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 23# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24# POSSIBILITY OF SUCH DAMAGE. 25"""Interface to the Yasm library. 26 27The Yasm library (aka libyasm) provides the core functionality of the Yasm 28assembler. Classes in this library provide for manipulation of machine 29instructions and object file constructs such as symbol tables and sections. 30 31Expression objects encapsulate complex expressions containing registers, 32symbols, and operations such as SEG. 33 34Bytecode objects encapsulate data or code objects such as data, reserve, 35align, or instructions. 36 37Section objects encapsulate an object file section, including the section 38name, any Bytecode objects contained within that section, and other 39information. 40 41""" 42 43cdef extern from "Python.h": 44 cdef object PyCObject_FromVoidPtr(void *cobj, void (*destr)(void *)) 45 cdef object PyCObject_FromVoidPtrAndDesc(void *cobj, void *desc, 46 void (*destr)(void *, void *)) 47 cdef int PyType_Check(object) 48 cdef int PyCObject_Check(object) 49 cdef void *PyCObject_AsVoidPtr(object) 50 cdef void *PyCObject_GetDesc(object) 51 52 cdef object _PyLong_FromByteArray(unsigned char *bytes, unsigned int n, 53 int little_endian, int is_signed) 54 cdef int _PyLong_AsByteArray(object v, unsigned char *bytes, unsigned int n, 55 int little_endian, int is_signed) except -1 56 57 cdef void Py_INCREF(object o) 58 cdef void Py_DECREF(object o) 59 60 cdef void PyErr_SetString(object type, char *message) 61 cdef object PyErr_Format(object type, char *format, ...) 62 63cdef extern from "stdlib.h": 64 cdef void *malloc(int n) 65 cdef void free(void *p) 66 67include "_yasm.pxi" 68 69cdef object __pass_voidp(void *obj, object forclass): 70 return PyCObject_FromVoidPtrAndDesc(obj, <void *>forclass, NULL) 71 72cdef void *__get_voidp(object obj, object forclass) except NULL: 73 cdef void* desc 74 75 if not PyCObject_Check(obj): 76 msg = "obj %r is not a CObject" % obj 77 PyErr_SetString(TypeError, msg) 78 return NULL 79 80 desc = PyCObject_GetDesc(obj) 81 82 if desc != <void *>forclass: 83 if desc == NULL: 84 msg = "CObject type is not set (expecting %s)" % forclass 85 elif PyType_Check(<object>desc): 86 msg = "CObject is for %s not %s" % (<object>desc, forclass) 87 else: 88 msg = "CObject is incorrect (expecting %s)" % forclass 89 PyErr_SetString(TypeError, msg) 90 return NULL 91 92 return PyCObject_AsVoidPtr(obj) 93 94# 95# Link to associated data mechanism to keep Python references paired with 96# yasm objects. 97# 98cdef class __assoc_data_callback: 99 cdef yasm_assoc_data_callback *cb 100 def __cinit__(self, destroy, print_): 101 self.cb = <yasm_assoc_data_callback *>malloc(sizeof(yasm_assoc_data_callback)) 102 self.cb.destroy = <void (*) (void *)>PyCObject_AsVoidPtr(destroy) 103 #self.cb.print_ = <void (*) (void *, FILE *, int)>PyCObject_AsVoidPtr(print_) 104 def __dealloc__(self): 105 free(self.cb) 106 107 108cdef class Register: 109 cdef unsigned long reg 110 def __cinit__(self, reg): 111 self.reg = reg 112 113include "errwarn.pxi" 114include "intnum.pxi" 115include "floatnum.pxi" 116include "expr.pxi" 117include "symrec.pxi" 118include "value.pxi" 119 120include "bytecode.pxi" 121 122cdef __initialize(): 123 BitVector_Boot() 124 yasm_intnum_initialize() 125 yasm_floatnum_initialize() 126 yasm_errwarn_initialize() 127 128def __cleanup(): 129 yasm_floatnum_cleanup() 130 yasm_intnum_cleanup() 131 yasm_errwarn_cleanup() 132 BitVector_Shutdown() 133 134__initialize() 135import atexit 136atexit.register(__cleanup) 137 138