qbepy 2026.2.1__cp312-cp312-macosx_14_0_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,359 @@
1
+ Metadata-Version: 2.4
2
+ Name: qbepy
3
+ Version: 2026.2.1
4
+ Summary: Python bindings for QBE (Quite Bare Engine) compiler backend
5
+ Author-email: Stefane Fermigier <sf@abilian.com>
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://git.sr.ht/~sfermigier/qbepy
8
+ Project-URL: Repository, https://git.sr.ht/~sfermigier/qbepy
9
+ Keywords: compiler,qbe,code-generation,assembly
10
+ Classifier: Development Status :: 3 - Alpha
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: Programming Language :: Python :: 3.12
13
+ Classifier: Programming Language :: Python :: 3.13
14
+ Classifier: Topic :: Software Development :: Compilers
15
+ Requires-Python: >=3.12
16
+ Description-Content-Type: text/markdown
17
+ Requires-Dist: cffi>=1.16.0
18
+
19
+ # qbepy
20
+
21
+ Python bindings for [QBE](https://c9x.me/compile/) (Quite Bare Engine), a minimalist compiler backend.
22
+
23
+ QBE is a small, fast compiler backend that takes an SSA-based intermediate language (IL) and produces native machine code for multiple architectures. qbepy provides Python bindings via CFFI, allowing you to compile QBE IL directly from Python without spawning subprocesses.
24
+
25
+ ## Features
26
+
27
+ - **Direct FFI bindings** - No subprocess overhead; QBE is compiled as a Python extension
28
+ - **Multiple targets** - Supports amd64 (System V and Apple ABIs), ARM64, and RISC-V 64
29
+ - **Pythonic IR builder** - Construct QBE IL programmatically with a clean API
30
+ - **Error handling** - QBE errors are raised as Python exceptions
31
+ - **Vendored QBE** - QBE source is included and built automatically during installation
32
+
33
+ ## Installation
34
+
35
+ ```bash
36
+ pip install qbepy
37
+ # or
38
+ uv add qbepy
39
+ ```
40
+
41
+ Or from source:
42
+
43
+ ```bash
44
+ git clone https://github.com/user/qbepy.git
45
+ cd qbepy
46
+ uv sync
47
+ ```
48
+
49
+ ## Quick Start
50
+
51
+ ### Compiling Raw IL
52
+
53
+ ```python
54
+ import qbepy
55
+
56
+ # QBE IL for a simple add function
57
+ il = """
58
+ export function w $add(w %a, w %b) {
59
+ @start
60
+ %r =w add %a, %b
61
+ ret %r
62
+ }
63
+ """
64
+
65
+ # Compile to assembly
66
+ asm = qbepy.compile_il(il)
67
+ print(asm)
68
+ ```
69
+
70
+ Output (ARM64 Apple):
71
+ ```asm
72
+ .text
73
+ .balign 4
74
+ .globl _add
75
+ _add:
76
+ hint #34
77
+ stp x29, x30, [sp, -16]!
78
+ mov x29, sp
79
+ add w0, w0, w1
80
+ ldp x29, x30, [sp], 16
81
+ ret
82
+ ```
83
+
84
+ ### Using the IR Builder
85
+
86
+ ```python
87
+ import qbepy
88
+ from qbepy import Module, Function, DataDef, W, L
89
+ from qbepy.ir import BinaryOp, Call, Return, Temporary, Global, IntConst
90
+
91
+ # Create a module
92
+ mod = Module()
93
+
94
+ # Add a string constant
95
+ mod.add_data(
96
+ DataDef("greeting")
97
+ .add_string("Hello, World!")
98
+ .add_bytes(0) # null terminator
99
+ )
100
+
101
+ # Create main function
102
+ func = Function("main", W, export=True)
103
+ block = func.add_block("start")
104
+
105
+ # Call puts($greeting)
106
+ r = func.new_temp("r")
107
+ block.instructions.append(
108
+ Call(Global("puts"), [(L, Global("greeting"))], r, W)
109
+ )
110
+ block.terminator = Return(IntConst(0))
111
+
112
+ mod.add_function(func)
113
+
114
+ # Compile to assembly
115
+ asm = qbepy.compile_module(mod)
116
+ print(asm)
117
+ ```
118
+
119
+ ### Specifying a Target
120
+
121
+ ```python
122
+ # Compile for x86-64 System V ABI
123
+ asm = qbepy.compile_il(il, target="amd64_sysv")
124
+
125
+ # Or use the Compiler class
126
+ compiler = qbepy.Compiler(target="arm64")
127
+ asm = compiler.compile(il)
128
+
129
+ # Available targets
130
+ print(qbepy.Compiler.get_available_targets())
131
+ # ['amd64_sysv', 'amd64_apple', 'arm64', 'arm64_apple', 'rv64']
132
+
133
+ # Get default target for current platform
134
+ print(qbepy.Compiler.get_default_target())
135
+ # 'arm64_apple' (on Apple Silicon)
136
+ ```
137
+
138
+ ## API Reference
139
+
140
+ ### Compiler
141
+
142
+ ```python
143
+ from qbepy import Compiler, compile_il, compile_module
144
+
145
+ # Create a compiler instance
146
+ compiler = Compiler(target=None) # None = platform default
147
+
148
+ # Compile IL string to assembly
149
+ asm = compiler.compile(il_string)
150
+
151
+ # Compile a Module object
152
+ asm = compiler.compile_module(module)
153
+
154
+ # Change target
155
+ compiler.set_target("amd64_sysv")
156
+
157
+ # Convenience functions
158
+ asm = compile_il(il_string, target=None)
159
+ asm = compile_module(module, target=None)
160
+ ```
161
+
162
+ ### Types
163
+
164
+ ```python
165
+ from qbepy import W, L, S, D, BaseType, ExtType, AggregateType
166
+
167
+ # Base types
168
+ W # word (32-bit integer)
169
+ L # long (64-bit integer)
170
+ S # single (32-bit float)
171
+ D # double (64-bit float)
172
+
173
+ # Extended types (for memory operations)
174
+ ExtType.BYTE, ExtType.HALF, ExtType.WORD, ExtType.LONG
175
+ ExtType.SINGLE, ExtType.DOUBLE
176
+
177
+ # Aggregate types (structs)
178
+ point = AggregateType("point", [
179
+ (ExtType.WORD, 1), # x: 1 word
180
+ (ExtType.WORD, 1), # y: 1 word
181
+ ])
182
+ ```
183
+
184
+ ### Values
185
+
186
+ ```python
187
+ from qbepy.ir import Temporary, Global, Label, IntConst, FloatConst
188
+
189
+ Temporary("x") # %x - SSA temporary
190
+ Global("main") # $main - global symbol
191
+ Label("loop") # @loop - block label
192
+ IntConst(42) # 42 - integer constant
193
+ FloatConst(3.14) # d_3.14 - double constant
194
+ FloatConst(3.14, is_single=True) # s_3.14 - float constant
195
+ ```
196
+
197
+ ### Instructions
198
+
199
+ ```python
200
+ from qbepy.ir import (
201
+ BinaryOp, # add, sub, mul, div, rem, or, xor, and, sar, shr, shl
202
+ UnaryOp, # neg, copy
203
+ Copy, # copy value
204
+ Load, # load from memory
205
+ Store, # store to memory
206
+ Alloc, # stack allocation
207
+ Call, # function call
208
+ Comparison, # ceqw, cnew, csltw, etc.
209
+ Conversion, # extsw, truncd, stosi, cast, etc.
210
+ Phi, # SSA phi node
211
+ )
212
+
213
+ # Examples
214
+ BinaryOp("add", Temporary("r"), W, Temporary("a"), Temporary("b"))
215
+ Load(Temporary("v"), W, Temporary("ptr"))
216
+ Store("storew", Temporary("v"), Temporary("ptr"))
217
+ Call(Global("printf"), [(L, Global("fmt"))], Temporary("r"), W)
218
+ ```
219
+
220
+ ### Control Flow
221
+
222
+ ```python
223
+ from qbepy.ir import Jump, Branch, Return, Halt
224
+
225
+ Jump(Label("next")) # jmp @next
226
+ Branch(Temporary("c"), Label("t"), Label("f")) # jnz %c, @t, @f
227
+ Return(Temporary("r")) # ret %r
228
+ Return(IntConst(0)) # ret 0
229
+ Return() # ret (void)
230
+ Halt() # hlt (unreachable)
231
+ ```
232
+
233
+ ### Building Modules
234
+
235
+ ```python
236
+ from qbepy import Module, Function, Block, DataDef
237
+
238
+ # Module - container for types, data, and functions
239
+ mod = Module()
240
+ mod.add_type(aggregate_type)
241
+ mod.add_data(data_def)
242
+ mod.add_function(function)
243
+
244
+ # Function
245
+ func = Function("name", return_type, params=[(W, "a"), (L, "b")], export=True)
246
+ block = func.add_block("start")
247
+ temp = func.new_temp("x") # creates unique temporary
248
+
249
+ # Block
250
+ block.instructions.append(instruction)
251
+ block.terminator = Return(value)
252
+
253
+ # DataDef
254
+ data = (DataDef("name", export=True, align=8)
255
+ .add_string("hello")
256
+ .add_bytes(0)
257
+ .add_words(1, 2, 3)
258
+ .add_longs(0x1234567890)
259
+ .add_zero(16))
260
+ ```
261
+
262
+ ## Supported Targets
263
+
264
+ | Target | Description |
265
+ |--------|-------------|
266
+ | `amd64_sysv` | x86-64 with System V ABI (Linux, BSD) |
267
+ | `amd64_apple` | x86-64 with Apple ABI (macOS Intel) |
268
+ | `arm64` | ARM64 with standard ABI (Linux) |
269
+ | `arm64_apple` | ARM64 with Apple ABI (macOS Apple Silicon) |
270
+ | `rv64` | RISC-V 64-bit |
271
+
272
+ ## QBE IL Reference
273
+
274
+ QBE uses a simple SSA-based intermediate language. For the complete specification, see the [QBE IL documentation](https://c9x.me/compile/doc/il.html).
275
+
276
+ ### Basic IL Structure
277
+
278
+ ```
279
+ # Type definitions
280
+ type :point = { w, w }
281
+
282
+ # Data definitions
283
+ data $message = { b "Hello", b 0 }
284
+
285
+ # Function definitions
286
+ export function w $main() {
287
+ @start
288
+ %x =w copy 42
289
+ ret %x
290
+ }
291
+ ```
292
+
293
+ ### IL Basics
294
+
295
+ - Temporaries: `%name` (SSA values)
296
+ - Globals: `$name` (functions and data)
297
+ - Labels: `@name` (basic blocks)
298
+ - Types: `w` (word), `l` (long), `s` (single), `d` (double)
299
+
300
+ ## Error Handling
301
+
302
+ ```python
303
+ from qbepy import CompilationError, compile_il
304
+
305
+ try:
306
+ asm = compile_il("invalid IL code")
307
+ except CompilationError as e:
308
+ print(f"Compilation failed: {e}")
309
+ ```
310
+
311
+ ## Project Structure
312
+
313
+ ```
314
+ qbepy/
315
+ ├── src/qbepy/
316
+ │ ├── __init__.py # Public API exports
317
+ │ ├── _ffi.py # Low-level CFFI bindings
318
+ │ ├── compiler.py # Compiler class
319
+ │ ├── errors.py # Exception types
320
+ │ └── ir/ # IR builder module
321
+ │ ├── types.py # Type definitions
322
+ │ ├── values.py # Value types
323
+ │ ├── instructions.py # Instructions
324
+ │ ├── control.py # Control flow
325
+ │ └── builder.py # Module, Function, Block, DataDef
326
+ ├── csrc/
327
+ │ ├── qbepy_wrapper.c # C wrapper with error handling
328
+ │ └── qbepy_wrapper.h
329
+ ├── vendor/qbe/ # Vendored QBE source
330
+ ├── build_ffi.py # CFFI build script
331
+ └── tests/ # Test suite
332
+ ```
333
+
334
+ ## How It Works
335
+
336
+ qbepy vendors the QBE compiler source and builds it as a Python extension using CFFI. The main challenge is that QBE's error handling uses `exit()`, which would terminate the Python process. qbepy solves this by:
337
+
338
+ 1. Redirecting QBE's `err()` function to a custom handler using preprocessor macros
339
+ 2. Using `setjmp`/`longjmp` to catch errors and return control to Python
340
+ 3. Converting errors to Python exceptions
341
+
342
+ This allows QBE to be used as a library rather than a standalone compiler.
343
+
344
+ ## License
345
+
346
+ qbepy is released under the MIT License.
347
+
348
+ QBE is developed by Quentin Carbonneaux and is also MIT licensed. See [vendor/qbe/LICENSE](vendor/qbe/LICENSE).
349
+
350
+ ## Credits
351
+
352
+ - [QBE](https://c9x.me/compile/) - The compiler backend by Quentin Carbonneaux
353
+ - [CFFI](https://cffi.readthedocs.io/) - C Foreign Function Interface for Python
354
+
355
+ ## See Also
356
+
357
+ - [QBE Documentation](https://c9x.me/compile/doc/il.html) - Complete IL specification
358
+ - [cproc](https://sr.ht/~mcf/cproc/) - A C11 compiler using QBE as backend
359
+ - [Hare](https://harelang.org/) - A systems programming language using QBE
@@ -0,0 +1,15 @@
1
+ qbepy/__init__.py,sha256=AuDwqEFlcaGu4DUTtlCby4AOK4YJ93nx4iXmCopDniU,1748
2
+ qbepy/_ffi.py,sha256=8RH3TTVZqKsGJhCcu50t9wDEm6eiAfWNsWSSqVGlPww,1772
3
+ qbepy/_qbe_ffi.abi3.so,sha256=PmCaTvZIw3FYfuMZpWWuBtMfHOLt6y9UyCuGXW8SE98,239376
4
+ qbepy/compiler.py,sha256=R4YCk8EVMZKNpijdt6ZIkk3Kz8TcjzW5OSCuyrvVwls,4434
5
+ qbepy/errors.py,sha256=VnAVWttiw88IRoNU0W9vXVhrH-E4vo8uoIk6QRDZxYc,479
6
+ qbepy/ir/__init__.py,sha256=pxLeIzfzkfF3Ip3EacrQSvlaxpzEjXI3eiNUG-tneGI,1264
7
+ qbepy/ir/builder.py,sha256=QZnZk-vacshWpKQJVtXUWIPSbzAuoTLMFAw-ZlT6Zmg,7307
8
+ qbepy/ir/control.py,sha256=g-4qlAGSpbtqTy4Ls2241JUqtZaB55-7xqE1xSFmyhE,1136
9
+ qbepy/ir/instructions.py,sha256=TjVqB7r1GAnd7CJNFgZzbepELlwEa0RYx_a4RCpZCUY,5402
10
+ qbepy/ir/types.py,sha256=n8jX6X5O9IsZA6tG4Fr2ZlXUOKDlHJhqDe0I0HexPvg,2151
11
+ qbepy/ir/values.py,sha256=AnDNM4mMDm2djUC97w5tn_9dZl8MiIVW3KR6XOWjgM4,1371
12
+ qbepy-2026.2.1.dist-info/METADATA,sha256=fSHnMPO1BDsUBSp0x_YANcewtCjMtkaPEAwG90YvNqQ,9476
13
+ qbepy-2026.2.1.dist-info/WHEEL,sha256=97kHCUEleITSucYBZu_0zWTbnOZIu_kBzOa1kxkgO4g,110
14
+ qbepy-2026.2.1.dist-info/top_level.txt,sha256=Z3hQHpqZ7fFFHrz3l7nFGPmyolMxcxHl1SuGUr3Kgqc,6
15
+ qbepy-2026.2.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.10.2)
3
+ Root-Is-Purelib: false
4
+ Tag: cp312-cp312-macosx_14_0_arm64
5
+
@@ -0,0 +1 @@
1
+ qbepy