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.0.tar.gz (5.9 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.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sfpy_asm-0.1.0.tar.gz
  • Upload date:
  • Size: 5.9 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.0.tar.gz
Algorithm Hash digest
SHA256 aa06309e22f6051881bbe8fa3488575bfbc55dc181567c5ae990fcdca1f2f497
MD5 f686510830f93de271d774b917c5c841
BLAKE2b-256 edd1d299358cd9b4bc56bf6f1e11606d7a4608d7543f2c0bc9295982c19710d4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sfpy_asm-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.7 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7b71d8f429bd49ccd64cd590cb36becec1c4e59c0bdd3102de878e63b79dee2f
MD5 7a59112c27ad9c8fb31ff265378df770
BLAKE2b-256 33a4bc6d93d11596e91c7d9b8dc05e26a37a65299c047057d3d122bec3d839b8

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