Skip to main content

Experimental GCC/GAS inline assembly bridge for Python

Project description

sfpy-asm(Segmentation Fault in Python)

Experimental Python bridge for GCC/GAS-style extended inline assembly.

from sfpy import __asm__

result = __asm__(
    "addq %1, %0",
    [("+r", 40)],
    [("r", 2)],
    ["cc"],
)

assert result == 42

Only __asm__ is exported from sfpy.

API

__asm__(code: str, outputs=None, inputs=None, clobbers=None)
  • code: GCC extended inline assembly template. GAS syntax is accepted by GCC.
  • outputs: list of (constraint, value) output operands.
  • inputs: list of (constraint, value) input operands.
  • clobbers: list of register/clobber names such as ["rax", "memory", "cc"].

Return value:

  • no outputs: None
  • one output: that value
  • multiple outputs: tuple of values

Supported operand values:

  • int, bool: native long long
  • float: native double
  • str: UTF-8 const char *
  • bytes, bytearray: byte buffer pointer
  • any other object: raw PyObject *
  • Python callables used as inputs: native no-argument callback pointer returning long long

Python variables cannot be mutated through GCC output operands, so output operands are returned instead. For read/write constraints such as "+r", the supplied output value is used as the initial native value.

Callable operands are plain native function pointers. Call them from assembly with call *%N. Because GCC cannot see that call, include the usual caller-saved register clobbers yourself:

def answer():
    return 42

result = __asm__(
    "call *%1\nmovq %%rax, %0",
    [("=r", 0)],
    [("r", answer)],
    ["rax", "rcx", "rdx", "rsi", "rdi", "r8", "r9", "r10", "r11", "memory", "cc"],
)

This package is Linux/WSL-only and requires gcc plus Python development headers at runtime because __asm__ compiles a small shared object for each assembly signature.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sfpy_asm-0.1.1.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sfpy_asm-0.1.1-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file sfpy_asm-0.1.1.tar.gz.

File metadata

  • Download URL: sfpy_asm-0.1.1.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for sfpy_asm-0.1.1.tar.gz
Algorithm Hash digest
SHA256 db1fbc55819283c3cfe2cc211e1b48a29e7899f21f297bb88aeb564036cb5f2f
MD5 3410239ea90007b01d5d0a4bd8c5d3ad
BLAKE2b-256 c4572885188b82735700a2c3f9be676439554a6c6b00dc5fad447822c6d8043a

See more details on using hashes here.

File details

Details for the file sfpy_asm-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: sfpy_asm-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for sfpy_asm-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6b3b014fbaedf1af87b3bf87fa3602d97609772e1e190e15541ff410353ba916
MD5 6caa978d4a38516539c5bee712e797df
BLAKE2b-256 9b208b8cd12496012a75b8449257110c2b6c46380ef1db48880e122e9177179d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page