Skip to content

Commit

Permalink
Call gcc_jit_result_release; start documenting gccjit.Result
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmalcolm committed Jan 22, 2015
1 parent 3e15f20 commit 301c514
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
2 changes: 1 addition & 1 deletion doc/conf.py
Expand Up @@ -41,7 +41,7 @@

# General information about the project.
project = u'gccjit'
copyright = u'2014, David Malcolm'
copyright = u'2014-2015, David Malcolm'

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
Expand Down
33 changes: 30 additions & 3 deletions doc/results.rst
@@ -1,5 +1,5 @@
.. Copyright 2014 David Malcolm <dmalcolm@redhat.com>
Copyright 2014 Red Hat, Inc.
.. Copyright 2014-2015 David Malcolm <dmalcolm@redhat.com>
Copyright 2014-2015 Red Hat, Inc.
This is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
Expand All @@ -20,7 +20,34 @@ Compilation results

.. py:class:: gccjit.Result
A :py:class:`gccjit.Result` encapsulates the result of compiling a
:py:class:`gccjit.Context` in-memory, and the lifetimes of any
machine code functions or globals that are within the result.

.. py:method:: get_code(funcname)
.. TODO
Locate the given function within the built machine code.

Functions are looked up by name. For this to succeed, a function
with a name matching `funcname` must have been created on
`result`'s context (or a parent context) via a call to
:py:meth:`gccjit.Context.new_function` with `kind`
:py:data:`gccjit.FunctionKind.EXPORTED`.

.. error-handling?
The returned value is an
`int`, actually a pointer to the machine code within the
address space of the process. This will need to be wrapped up
with `ctypes` to be callable::

import ctypes

# "[int] -> int" functype:
int_int_func_type = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int)
code = int_int_func_type(jit_result.get_code(b"square"))
assert code(5) == 25

The code has the same lifetime as the :py:class:`gccjit.Result`
instance; the pointer becomes invalid when the result instance
is cleaned up.
3 changes: 3 additions & 0 deletions gccjit/gccjit.pyx
Expand Up @@ -415,6 +415,9 @@ cdef class Result:
def __cinit__(self):
self._c_result = NULL

def __dealloc__(self):
c_api.gcc_jit_result_release(self._c_result)

cdef _set_c_ptr(self, c_api.gcc_jit_result* c_result):
self._c_result = c_result

Expand Down

0 comments on commit 301c514

Please sign in to comment.