145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# Python bindings for Yasm: Pyrex input file for value.h
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
2645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgcdef class Value:
2745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    cdef yasm_value value
28a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org    def __cinit__(self, value=None, size=None):
2945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        cdef unsigned int sz
3045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        if size is None:
3145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            sz = 0
3245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        else:
3345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            sz = size;
3445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
3545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        yasm_value_initialize(&self.value, NULL, sz)
3645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        if value is None:
3745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            pass
3845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        elif isinstance(value, Expression):
3945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            yasm_value_initialize(&self.value,
4045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                                  yasm_expr_copy((<Expression>value).expr), sz)
4145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        elif isinstance(value, Symbol):
4245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            yasm_value_init_sym(&self.value, (<Symbol>value).sym, sz)
4345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        else:
4445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            raise TypeError("Invalid value type '%s'" % type(value))
4545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
4645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    def __dealloc__(self):
4745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        yasm_value_delete(&self.value)
4845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
4945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    def finalize(self, precbc=None):
5045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        if precbc is None:
5145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            return yasm_value_finalize(&self.value, NULL)
5245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        elif isinstance(precbc, Bytecode):
5345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            return yasm_value_finalize(&self.value, (<Bytecode>precbc).bc)
5445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        else:
5545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            raise TypeError("Invalid precbc type '%s'" % type(precbc))
5645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
57