kasi-compiler 0.2.0__tar.gz
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.
- kasi_compiler-0.2.0/LICENSE +21 -0
- kasi_compiler-0.2.0/MANIFEST.in +9 -0
- kasi_compiler-0.2.0/PKG-INFO +330 -0
- kasi_compiler-0.2.0/README.md +304 -0
- kasi_compiler-0.2.0/examples/dynamic.py +10 -0
- kasi_compiler-0.2.0/examples/features.py +22 -0
- kasi_compiler-0.2.0/examples/fib.py +10 -0
- kasi_compiler-0.2.0/examples/lists.py +12 -0
- kasi_compiler-0.2.0/examples/objects.py +25 -0
- kasi_compiler-0.2.0/kasi/__init__.py +6 -0
- kasi_compiler-0.2.0/kasi/__main__.py +321 -0
- kasi_compiler-0.2.0/kasi/bench.py +82 -0
- kasi_compiler-0.2.0/kasi/codegen.py +1327 -0
- kasi_compiler-0.2.0/kasi/comptime.py +265 -0
- kasi_compiler-0.2.0/kasi/diagnostics.py +126 -0
- kasi_compiler-0.2.0/kasi/driver.py +574 -0
- kasi_compiler-0.2.0/kasi/explain.py +142 -0
- kasi_compiler-0.2.0/kasi/infer.py +631 -0
- kasi_compiler-0.2.0/kasi/ktypes.py +84 -0
- kasi_compiler-0.2.0/kasi/profile.py +89 -0
- kasi_compiler-0.2.0/kasi/runtime/__init__.py +1 -0
- kasi_compiler-0.2.0/kasi/runtime/kasi_runtime.c +975 -0
- kasi_compiler-0.2.0/kasi/runtime/kasi_runtime.h +264 -0
- kasi_compiler-0.2.0/kasi/tune.py +146 -0
- kasi_compiler-0.2.0/kasi/version.py +3 -0
- kasi_compiler-0.2.0/kasi_compiler.egg-info/PKG-INFO +330 -0
- kasi_compiler-0.2.0/kasi_compiler.egg-info/SOURCES.txt +34 -0
- kasi_compiler-0.2.0/kasi_compiler.egg-info/dependency_links.txt +1 -0
- kasi_compiler-0.2.0/kasi_compiler.egg-info/entry_points.txt +2 -0
- kasi_compiler-0.2.0/kasi_compiler.egg-info/requires.txt +3 -0
- kasi_compiler-0.2.0/kasi_compiler.egg-info/top_level.txt +1 -0
- kasi_compiler-0.2.0/pyproject.toml +43 -0
- kasi_compiler-0.2.0/setup.cfg +4 -0
- kasi_compiler-0.2.0/tests/conftest.py +10 -0
- kasi_compiler-0.2.0/tests/test_compiler.py +186 -0
- kasi_compiler-0.2.0/tests/test_features.py +245 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 KASI Compiler contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: kasi-compiler
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Ahead-of-time Python compiler: AST, type inference, C, native executable
|
|
5
|
+
Author: KASI Compiler contributors
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Keywords: compiler,python,aot,native,codegen,type-inference
|
|
8
|
+
Classifier: Development Status :: 3 - Alpha
|
|
9
|
+
Classifier: Environment :: Console
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
12
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
13
|
+
Classifier: Operating System :: MacOS
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
19
|
+
Classifier: Topic :: Software Development :: Compilers
|
|
20
|
+
Requires-Python: >=3.11
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
License-File: LICENSE
|
|
23
|
+
Provides-Extra: zig
|
|
24
|
+
Requires-Dist: ziglang; extra == "zig"
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
|
|
27
|
+
# KASI Compiler
|
|
28
|
+
|
|
29
|
+
**Python source → inferred types → C → native executable.**
|
|
30
|
+
|
|
31
|
+
KASI is an ahead-of-time compiler for a useful subset of Python. You write
|
|
32
|
+
normal Python; KASI parses it with `ast`, infers types (no annotations
|
|
33
|
+
required), emits C with native machine types for everything it can prove, and
|
|
34
|
+
compiles it with Zig/Clang into a standalone executable — no Python runtime,
|
|
35
|
+
no CPython ABI, no annotations.
|
|
36
|
+
|
|
37
|
+
## Installation
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
python -m pip install kasi-compiler
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
KASI needs a native C compiler at build time. Either install the optional Zig
|
|
44
|
+
wheel, or have `cc`, `gcc`, or `clang` on `PATH`:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
python -m pip install "kasi-compiler[zig]"
|
|
48
|
+
kasi compiler # show which compiler KASI found
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
The installed `kasi` command and `python -m kasi` are equivalent.
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
python -m kasi compile examples/fib.py -o fib.exe --emit-c fib.c
|
|
55
|
+
fib.bat # Windows launcher (fib.sh on POSIX)
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Compilation writes a small launcher beside the native binary: `.bat` on
|
|
59
|
+
Windows, `.sh` on POSIX. It forwards arguments and starts the compiled program,
|
|
60
|
+
so you can run the launcher instead of the `.exe`.
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
python -m kasi run examples/lists.py
|
|
64
|
+
python -m kasi compiler # show the native compiler being used
|
|
65
|
+
python -m kasi bench # compare CPython vs KASI (module: kasi.bench)
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
## What makes KASI different (v0.2.0)
|
|
71
|
+
|
|
72
|
+
### 1. Speculative specialization with runtime guards + PGO
|
|
73
|
+
|
|
74
|
+
Inference guesses types; guards make a wrong guess **slow instead of wrong**.
|
|
75
|
+
Each specialized function gets a boxed entry point that checks observed tags
|
|
76
|
+
and dispatches to the native fast path when they match, otherwise to a generic
|
|
77
|
+
`KValue` body.
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
python -m kasi compile app.py -o app.exe --guards
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Profile-guided AOT: build an instrumented binary, run it on representative
|
|
84
|
+
input, feed the observed `KASI_TYPE:` events back into the final build:
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
python -m kasi compile app.py -o app.exe --train input.txt
|
|
88
|
+
# writes app.kprofile, then rebuilds using the observed parameter types
|
|
89
|
+
python -m kasi compile app.py -o app.exe --profile app.kprofile
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### 2. `comptime` — execute Python at compile time
|
|
93
|
+
|
|
94
|
+
Pure constant expressions are folded automatically, and functions marked
|
|
95
|
+
`@kasi.comptime` run during compilation; constant calls are replaced by their
|
|
96
|
+
results and the function disappears from the binary. Think Zig-style comptime
|
|
97
|
+
without annotations.
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
@kasi.comptime
|
|
101
|
+
def table(n):
|
|
102
|
+
return n * n + 1
|
|
103
|
+
|
|
104
|
+
x = table(7) # becomes the constant 50
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Folding can be disabled with `--no-comptime`.
|
|
108
|
+
|
|
109
|
+
### 3. WASM and embedded targets
|
|
110
|
+
|
|
111
|
+
The bump allocator means no GC and deterministic memory, which suits browsers
|
|
112
|
+
and microcontrollers. `--target` accepts Zig triples and friendly aliases such
|
|
113
|
+
as `thumbv7m-none-eabi`:
|
|
114
|
+
|
|
115
|
+
```bash
|
|
116
|
+
python -m kasi compile app.py -o app.wasm --target wasm32-wasi
|
|
117
|
+
python -m kasi compile app.py -o app.wasm --target wasm32-freestanding
|
|
118
|
+
python -m kasi compile app.py -o firmware.elf --target thumbv7m-none-eabi
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Freestanding targets get no-op I/O and a trap on exit; WASI links against libc.
|
|
122
|
+
The `.sh` launcher looks for `wasmtime`, `wasmer`, or `wasmedge`.
|
|
123
|
+
|
|
124
|
+
### 4. Explainable speed map
|
|
125
|
+
|
|
126
|
+
`--explain` writes a self-contained HTML report: green = native, amber =
|
|
127
|
+
dynamic, red = degraded. Hover an expression to see its inferred type and the
|
|
128
|
+
rule that produced it.
|
|
129
|
+
|
|
130
|
+
```bash
|
|
131
|
+
python -m kasi compile app.py -o app.exe --explain speed.html
|
|
132
|
+
python -m kasi explain app.py -o speed.html
|
|
133
|
+
python -m kasi types app.py # per-line types + rules
|
|
134
|
+
python -m kasi types app.py --json # machine-readable provenance
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### 5. Compile-time diagnostics
|
|
138
|
+
|
|
139
|
+
Inference powers `-Wall`-style checks that are alien to normal Python tooling:
|
|
140
|
+
constant division by zero, int64 overflow, out-of-range literal indexing,
|
|
141
|
+
unreachable code, and every dynamic/degraded fallback.
|
|
142
|
+
|
|
143
|
+
```bash
|
|
144
|
+
python -m kasi compile app.py -o app.exe -Wall
|
|
145
|
+
python -m kasi lint app.py # just the diagnostics
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Codes: `KASI001` division by zero, `KASI002` int overflow, `KASI003` bad literal
|
|
149
|
+
index, `KASI004` unreachable code, `KASI005` degradation.
|
|
150
|
+
|
|
151
|
+
### 6. `kasi info` and reproducible builds
|
|
152
|
+
|
|
153
|
+
Every binary embeds a source hash, KASI version, target, and flags:
|
|
154
|
+
|
|
155
|
+
```bash
|
|
156
|
+
python -m kasi compile app.py -o app.exe --repro
|
|
157
|
+
python -m kasi info app.exe
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
`--repro` uses a fixed build name, records the source basename, and drops
|
|
161
|
+
`-mcpu native` so identical inputs produce identical binaries.
|
|
162
|
+
|
|
163
|
+
### 7. Library output
|
|
164
|
+
|
|
165
|
+
Produce a `.a` / `.so` / `.dll` with exported native-ABI functions taking plain
|
|
166
|
+
`int64_t`, `double`, and pointers — embeddable from C, Rust, or Go without any
|
|
167
|
+
Python runtime. Call the exported `kasi_init()` once to register methods.
|
|
168
|
+
|
|
169
|
+
```bash
|
|
170
|
+
python -m kasi compile app.py -o libapp.a --emit-lib static
|
|
171
|
+
python -m kasi compile app.py -o libapp.so --emit-lib shared
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### 8. Native module ABI without `PyObject`
|
|
175
|
+
|
|
176
|
+
Skip the Python C API entirely. Annotate a stub with `@kasi.native("header.h")`;
|
|
177
|
+
KASI emits the include and calls the external C function with zero boxing.
|
|
178
|
+
|
|
179
|
+
```python
|
|
180
|
+
@kasi.native("mymath.h")
|
|
181
|
+
def twice(x: int) -> int:
|
|
182
|
+
return 0 # body ignored; the C header supplies it
|
|
183
|
+
|
|
184
|
+
print(twice(3))
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
Add header locations with `--include-dir DIR`.
|
|
188
|
+
|
|
189
|
+
### 9. Autotuning
|
|
190
|
+
|
|
191
|
+
The compiler benchmarks itself for your hardware: compile several flag
|
|
192
|
+
variants, run each on your input, and install the fastest.
|
|
193
|
+
|
|
194
|
+
```bash
|
|
195
|
+
python -m kasi tune app.py -o app.exe --reps 5 --input sample.txt
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### 10. Deterministic mode
|
|
199
|
+
|
|
200
|
+
Expose the arena size and make any remaining dynamic path a compile error —
|
|
201
|
+
ideal for latency-sensitive strategies where allocation surprises are not an
|
|
202
|
+
option.
|
|
203
|
+
|
|
204
|
+
```bash
|
|
205
|
+
python -m kasi compile strategy.py -o strategy.exe --arena-size 64M --deterministic
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
### 11. First-class transpiler output
|
|
209
|
+
|
|
210
|
+
Readable, commented C you can inspect and ship:
|
|
211
|
+
|
|
212
|
+
```bash
|
|
213
|
+
python -m kasi transpile app.py -o app.c
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
---
|
|
217
|
+
|
|
218
|
+
## Pipeline
|
|
219
|
+
|
|
220
|
+
1. **Parse** — `ast.parse`, no custom syntax.
|
|
221
|
+
2. **Type inference** — `int`, `float`, `bool`, `str`, `list[T]`, `dict[T]`.
|
|
222
|
+
Unannotated function parameters are inferred from their call sites:
|
|
223
|
+
`fib(30)` makes `n` an `int`. Unknown expressions become `dynamic`
|
|
224
|
+
instead of failing compilation. Every expression records its type, source
|
|
225
|
+
position, and the rule that inferred it.
|
|
226
|
+
3. **Compile-time execution** — constant folding and `@kasi.comptime` inlining.
|
|
227
|
+
4. **IR / C generation** — typed expressions use `int64_t`, `double`,
|
|
228
|
+
`bool`, `KStr *`, `KList *`, `KDict *`. Dynamic expressions use a tagged
|
|
229
|
+
`KValue` runtime. C is the primary portable IR. `--emit-llvm` needs an
|
|
230
|
+
LLVM-enabled Zig/Clang; the `ziglang` pip wheel omits LLVM extensions, so
|
|
231
|
+
KASI reports `LLVM IR not available; use --emit-c`.
|
|
232
|
+
5. **Native compile** — the runtime and generated program are compiled as a
|
|
233
|
+
single C translation unit with Zig's `build-exe -O ReleaseFast` (or a system
|
|
234
|
+
C compiler). One TU plus `static inline` hot helpers gives full inlining
|
|
235
|
+
without needing LTO. Speed flags (`-mcpu native` on native builds, strip,
|
|
236
|
+
no frame pointers/unwind tables/stack checks) are on by default.
|
|
237
|
+
6. **Run** — no Python interpreter is required for compiled programs.
|
|
238
|
+
|
|
239
|
+
### Runtime platforms
|
|
240
|
+
|
|
241
|
+
The runtime has a small platform layer:
|
|
242
|
+
|
|
243
|
+
- **Windows** — `WriteFile`/`GetStdHandle` and `ExitProcess` (no CRT needed).
|
|
244
|
+
- **Linux x86_64 / aarch64** — direct `write`/`exit_group` syscalls, no libc.
|
|
245
|
+
- **Other POSIX targets (macOS, BSD, WASI, …)** — libc `write`/`_exit`.
|
|
246
|
+
- **WASM freestanding / Cortex-M** — no host I/O, trap on exit.
|
|
247
|
+
|
|
248
|
+
Use `--target` with a Zig target triple (or a common Rust-style alias). Plain
|
|
249
|
+
Linux targets such as `x86_64-linux-musl` need no libc; non-Linux POSIX targets
|
|
250
|
+
do.
|
|
251
|
+
|
|
252
|
+
## Status and honest expectations
|
|
253
|
+
|
|
254
|
+
This is a focused AOT compiler, not a complete Python implementation. Native
|
|
255
|
+
speedups (10–50×) apply to **statically inferred loops and arithmetic**; dynamic
|
|
256
|
+
fallback code is slower than CPython because it is a small tagged-value
|
|
257
|
+
interpreter, not CPython's runtime.
|
|
258
|
+
|
|
259
|
+
### Speed work
|
|
260
|
+
|
|
261
|
+
Native speed is the default; there is no "fast mode" to enable. The main
|
|
262
|
+
techniques are:
|
|
263
|
+
|
|
264
|
+
- `int`/`float` values stay as `int64_t`/`double` end-to-end — no boxing in
|
|
265
|
+
arithmetic or typed loops.
|
|
266
|
+
- `range` loops lower to native `for` loops with hoisted bounds, not a
|
|
267
|
+
materialised list.
|
|
268
|
+
- Typed list iteration goes straight to `KList->items[i]`; dict iteration walks
|
|
269
|
+
`KDict->entries[i]` directly.
|
|
270
|
+
- Hot boxing/unboxing helpers (`k_from_int64`, `k_as_int64`, `k_truthy`, …) are
|
|
271
|
+
`static inline` in `kasi_runtime.h`, so box/unbox pairs fold away.
|
|
272
|
+
- Runtime + program compile as one translation unit, letting the optimiser
|
|
273
|
+
inline across both without LTO.
|
|
274
|
+
- Host-tuned, stripped, frame-pointer-free release builds.
|
|
275
|
+
|
|
276
|
+
`python -m kasi bench` reports representative figures (Device Guard on some
|
|
277
|
+
Windows machines can skip individual native runs):
|
|
278
|
+
|
|
279
|
+
| Example | CPython | KASI | Speedup |
|
|
280
|
+
| --- | --- | --- | --- |
|
|
281
|
+
| `fib.py` | ~360 ms | ~20 ms | ~18× |
|
|
282
|
+
| `loop.py` | ~430 ms | ~15 ms | ~29× |
|
|
283
|
+
| `lists.py` | ~70 ms | ~15 ms | ~4.7× |
|
|
284
|
+
|
|
285
|
+
### Native fast paths
|
|
286
|
+
|
|
287
|
+
Constants, names, arithmetic, comparisons, `if`/`while`/`for`, `range`,
|
|
288
|
+
lists, strings, f-strings, `print`, `len`, `int`, `float`, `str`, `bool`,
|
|
289
|
+
list/dict literals, list/dict comprehensions, `in`/`not in`, user functions,
|
|
290
|
+
recursion, keyword arguments, `*args`, `**kwargs`, and `break`/`continue`.
|
|
291
|
+
|
|
292
|
+
### Dynamic/degraded features (always compile, with warnings)
|
|
293
|
+
|
|
294
|
+
- **Classes and methods** — instances are tagged runtime objects; methods are
|
|
295
|
+
registered in a runtime method table and dispatched dynamically via
|
|
296
|
+
`KValue`-boxed arguments. `__init__` is invoked on construction.
|
|
297
|
+
- **Imports / C-API extensions** — imports yield dynamic module objects; use
|
|
298
|
+
`@kasi.native` for a real zero-boxing C ABI instead.
|
|
299
|
+
- **Exceptions** — `try` bodies are compiled; handlers are skipped with a
|
|
300
|
+
warning. `raise` compiles to a runtime error.
|
|
301
|
+
- **Generators and closures** — `yield`/generator expressions and nested
|
|
302
|
+
functions degrade to dynamic values; closures cannot capture outer locals.
|
|
303
|
+
- **`with` statements** — only the body is compiled.
|
|
304
|
+
|
|
305
|
+
Warnings are emitted during compilation, returned in `CompileResult.warnings`,
|
|
306
|
+
and surfaced as `KASI005` diagnostics with `-Wall` / `kasi lint`.
|
|
307
|
+
|
|
308
|
+
## Development
|
|
309
|
+
|
|
310
|
+
From the directory containing `pyproject.toml`:
|
|
311
|
+
|
|
312
|
+
```bash
|
|
313
|
+
python -m pip install -e ".[zig]"
|
|
314
|
+
python -m pip install pytest
|
|
315
|
+
python -m pytest -q
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
Examples that show the dynamic feature set:
|
|
319
|
+
|
|
320
|
+
```bash
|
|
321
|
+
python -m kasi compile examples/features.py -o features.exe # dicts, comprehensions, imports, try
|
|
322
|
+
python -m kasi compile examples/objects.py -o objects.exe # classes, methods, kwargs, *args/**kwargs
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
On machines where Application Control/Device Guard blocks newly built `.exe`
|
|
326
|
+
files, the execution tests skip automatically; compile-only tests still verify
|
|
327
|
+
the full native link. Set `KASI_SKIP_NATIVE_RUN=1` to force that behavior.
|
|
328
|
+
|
|
329
|
+
This package is self-contained: the C runtime sources ship inside the wheel,
|
|
330
|
+
and no Python interpreter is needed to run compiled programs.
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
# KASI Compiler
|
|
2
|
+
|
|
3
|
+
**Python source → inferred types → C → native executable.**
|
|
4
|
+
|
|
5
|
+
KASI is an ahead-of-time compiler for a useful subset of Python. You write
|
|
6
|
+
normal Python; KASI parses it with `ast`, infers types (no annotations
|
|
7
|
+
required), emits C with native machine types for everything it can prove, and
|
|
8
|
+
compiles it with Zig/Clang into a standalone executable — no Python runtime,
|
|
9
|
+
no CPython ABI, no annotations.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
python -m pip install kasi-compiler
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
KASI needs a native C compiler at build time. Either install the optional Zig
|
|
18
|
+
wheel, or have `cc`, `gcc`, or `clang` on `PATH`:
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
python -m pip install "kasi-compiler[zig]"
|
|
22
|
+
kasi compiler # show which compiler KASI found
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
The installed `kasi` command and `python -m kasi` are equivalent.
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
python -m kasi compile examples/fib.py -o fib.exe --emit-c fib.c
|
|
29
|
+
fib.bat # Windows launcher (fib.sh on POSIX)
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Compilation writes a small launcher beside the native binary: `.bat` on
|
|
33
|
+
Windows, `.sh` on POSIX. It forwards arguments and starts the compiled program,
|
|
34
|
+
so you can run the launcher instead of the `.exe`.
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
python -m kasi run examples/lists.py
|
|
38
|
+
python -m kasi compiler # show the native compiler being used
|
|
39
|
+
python -m kasi bench # compare CPython vs KASI (module: kasi.bench)
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
## What makes KASI different (v0.2.0)
|
|
45
|
+
|
|
46
|
+
### 1. Speculative specialization with runtime guards + PGO
|
|
47
|
+
|
|
48
|
+
Inference guesses types; guards make a wrong guess **slow instead of wrong**.
|
|
49
|
+
Each specialized function gets a boxed entry point that checks observed tags
|
|
50
|
+
and dispatches to the native fast path when they match, otherwise to a generic
|
|
51
|
+
`KValue` body.
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
python -m kasi compile app.py -o app.exe --guards
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Profile-guided AOT: build an instrumented binary, run it on representative
|
|
58
|
+
input, feed the observed `KASI_TYPE:` events back into the final build:
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
python -m kasi compile app.py -o app.exe --train input.txt
|
|
62
|
+
# writes app.kprofile, then rebuilds using the observed parameter types
|
|
63
|
+
python -m kasi compile app.py -o app.exe --profile app.kprofile
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### 2. `comptime` — execute Python at compile time
|
|
67
|
+
|
|
68
|
+
Pure constant expressions are folded automatically, and functions marked
|
|
69
|
+
`@kasi.comptime` run during compilation; constant calls are replaced by their
|
|
70
|
+
results and the function disappears from the binary. Think Zig-style comptime
|
|
71
|
+
without annotations.
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
@kasi.comptime
|
|
75
|
+
def table(n):
|
|
76
|
+
return n * n + 1
|
|
77
|
+
|
|
78
|
+
x = table(7) # becomes the constant 50
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Folding can be disabled with `--no-comptime`.
|
|
82
|
+
|
|
83
|
+
### 3. WASM and embedded targets
|
|
84
|
+
|
|
85
|
+
The bump allocator means no GC and deterministic memory, which suits browsers
|
|
86
|
+
and microcontrollers. `--target` accepts Zig triples and friendly aliases such
|
|
87
|
+
as `thumbv7m-none-eabi`:
|
|
88
|
+
|
|
89
|
+
```bash
|
|
90
|
+
python -m kasi compile app.py -o app.wasm --target wasm32-wasi
|
|
91
|
+
python -m kasi compile app.py -o app.wasm --target wasm32-freestanding
|
|
92
|
+
python -m kasi compile app.py -o firmware.elf --target thumbv7m-none-eabi
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Freestanding targets get no-op I/O and a trap on exit; WASI links against libc.
|
|
96
|
+
The `.sh` launcher looks for `wasmtime`, `wasmer`, or `wasmedge`.
|
|
97
|
+
|
|
98
|
+
### 4. Explainable speed map
|
|
99
|
+
|
|
100
|
+
`--explain` writes a self-contained HTML report: green = native, amber =
|
|
101
|
+
dynamic, red = degraded. Hover an expression to see its inferred type and the
|
|
102
|
+
rule that produced it.
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
python -m kasi compile app.py -o app.exe --explain speed.html
|
|
106
|
+
python -m kasi explain app.py -o speed.html
|
|
107
|
+
python -m kasi types app.py # per-line types + rules
|
|
108
|
+
python -m kasi types app.py --json # machine-readable provenance
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### 5. Compile-time diagnostics
|
|
112
|
+
|
|
113
|
+
Inference powers `-Wall`-style checks that are alien to normal Python tooling:
|
|
114
|
+
constant division by zero, int64 overflow, out-of-range literal indexing,
|
|
115
|
+
unreachable code, and every dynamic/degraded fallback.
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
python -m kasi compile app.py -o app.exe -Wall
|
|
119
|
+
python -m kasi lint app.py # just the diagnostics
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Codes: `KASI001` division by zero, `KASI002` int overflow, `KASI003` bad literal
|
|
123
|
+
index, `KASI004` unreachable code, `KASI005` degradation.
|
|
124
|
+
|
|
125
|
+
### 6. `kasi info` and reproducible builds
|
|
126
|
+
|
|
127
|
+
Every binary embeds a source hash, KASI version, target, and flags:
|
|
128
|
+
|
|
129
|
+
```bash
|
|
130
|
+
python -m kasi compile app.py -o app.exe --repro
|
|
131
|
+
python -m kasi info app.exe
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
`--repro` uses a fixed build name, records the source basename, and drops
|
|
135
|
+
`-mcpu native` so identical inputs produce identical binaries.
|
|
136
|
+
|
|
137
|
+
### 7. Library output
|
|
138
|
+
|
|
139
|
+
Produce a `.a` / `.so` / `.dll` with exported native-ABI functions taking plain
|
|
140
|
+
`int64_t`, `double`, and pointers — embeddable from C, Rust, or Go without any
|
|
141
|
+
Python runtime. Call the exported `kasi_init()` once to register methods.
|
|
142
|
+
|
|
143
|
+
```bash
|
|
144
|
+
python -m kasi compile app.py -o libapp.a --emit-lib static
|
|
145
|
+
python -m kasi compile app.py -o libapp.so --emit-lib shared
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### 8. Native module ABI without `PyObject`
|
|
149
|
+
|
|
150
|
+
Skip the Python C API entirely. Annotate a stub with `@kasi.native("header.h")`;
|
|
151
|
+
KASI emits the include and calls the external C function with zero boxing.
|
|
152
|
+
|
|
153
|
+
```python
|
|
154
|
+
@kasi.native("mymath.h")
|
|
155
|
+
def twice(x: int) -> int:
|
|
156
|
+
return 0 # body ignored; the C header supplies it
|
|
157
|
+
|
|
158
|
+
print(twice(3))
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
Add header locations with `--include-dir DIR`.
|
|
162
|
+
|
|
163
|
+
### 9. Autotuning
|
|
164
|
+
|
|
165
|
+
The compiler benchmarks itself for your hardware: compile several flag
|
|
166
|
+
variants, run each on your input, and install the fastest.
|
|
167
|
+
|
|
168
|
+
```bash
|
|
169
|
+
python -m kasi tune app.py -o app.exe --reps 5 --input sample.txt
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
### 10. Deterministic mode
|
|
173
|
+
|
|
174
|
+
Expose the arena size and make any remaining dynamic path a compile error —
|
|
175
|
+
ideal for latency-sensitive strategies where allocation surprises are not an
|
|
176
|
+
option.
|
|
177
|
+
|
|
178
|
+
```bash
|
|
179
|
+
python -m kasi compile strategy.py -o strategy.exe --arena-size 64M --deterministic
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
### 11. First-class transpiler output
|
|
183
|
+
|
|
184
|
+
Readable, commented C you can inspect and ship:
|
|
185
|
+
|
|
186
|
+
```bash
|
|
187
|
+
python -m kasi transpile app.py -o app.c
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
---
|
|
191
|
+
|
|
192
|
+
## Pipeline
|
|
193
|
+
|
|
194
|
+
1. **Parse** — `ast.parse`, no custom syntax.
|
|
195
|
+
2. **Type inference** — `int`, `float`, `bool`, `str`, `list[T]`, `dict[T]`.
|
|
196
|
+
Unannotated function parameters are inferred from their call sites:
|
|
197
|
+
`fib(30)` makes `n` an `int`. Unknown expressions become `dynamic`
|
|
198
|
+
instead of failing compilation. Every expression records its type, source
|
|
199
|
+
position, and the rule that inferred it.
|
|
200
|
+
3. **Compile-time execution** — constant folding and `@kasi.comptime` inlining.
|
|
201
|
+
4. **IR / C generation** — typed expressions use `int64_t`, `double`,
|
|
202
|
+
`bool`, `KStr *`, `KList *`, `KDict *`. Dynamic expressions use a tagged
|
|
203
|
+
`KValue` runtime. C is the primary portable IR. `--emit-llvm` needs an
|
|
204
|
+
LLVM-enabled Zig/Clang; the `ziglang` pip wheel omits LLVM extensions, so
|
|
205
|
+
KASI reports `LLVM IR not available; use --emit-c`.
|
|
206
|
+
5. **Native compile** — the runtime and generated program are compiled as a
|
|
207
|
+
single C translation unit with Zig's `build-exe -O ReleaseFast` (or a system
|
|
208
|
+
C compiler). One TU plus `static inline` hot helpers gives full inlining
|
|
209
|
+
without needing LTO. Speed flags (`-mcpu native` on native builds, strip,
|
|
210
|
+
no frame pointers/unwind tables/stack checks) are on by default.
|
|
211
|
+
6. **Run** — no Python interpreter is required for compiled programs.
|
|
212
|
+
|
|
213
|
+
### Runtime platforms
|
|
214
|
+
|
|
215
|
+
The runtime has a small platform layer:
|
|
216
|
+
|
|
217
|
+
- **Windows** — `WriteFile`/`GetStdHandle` and `ExitProcess` (no CRT needed).
|
|
218
|
+
- **Linux x86_64 / aarch64** — direct `write`/`exit_group` syscalls, no libc.
|
|
219
|
+
- **Other POSIX targets (macOS, BSD, WASI, …)** — libc `write`/`_exit`.
|
|
220
|
+
- **WASM freestanding / Cortex-M** — no host I/O, trap on exit.
|
|
221
|
+
|
|
222
|
+
Use `--target` with a Zig target triple (or a common Rust-style alias). Plain
|
|
223
|
+
Linux targets such as `x86_64-linux-musl` need no libc; non-Linux POSIX targets
|
|
224
|
+
do.
|
|
225
|
+
|
|
226
|
+
## Status and honest expectations
|
|
227
|
+
|
|
228
|
+
This is a focused AOT compiler, not a complete Python implementation. Native
|
|
229
|
+
speedups (10–50×) apply to **statically inferred loops and arithmetic**; dynamic
|
|
230
|
+
fallback code is slower than CPython because it is a small tagged-value
|
|
231
|
+
interpreter, not CPython's runtime.
|
|
232
|
+
|
|
233
|
+
### Speed work
|
|
234
|
+
|
|
235
|
+
Native speed is the default; there is no "fast mode" to enable. The main
|
|
236
|
+
techniques are:
|
|
237
|
+
|
|
238
|
+
- `int`/`float` values stay as `int64_t`/`double` end-to-end — no boxing in
|
|
239
|
+
arithmetic or typed loops.
|
|
240
|
+
- `range` loops lower to native `for` loops with hoisted bounds, not a
|
|
241
|
+
materialised list.
|
|
242
|
+
- Typed list iteration goes straight to `KList->items[i]`; dict iteration walks
|
|
243
|
+
`KDict->entries[i]` directly.
|
|
244
|
+
- Hot boxing/unboxing helpers (`k_from_int64`, `k_as_int64`, `k_truthy`, …) are
|
|
245
|
+
`static inline` in `kasi_runtime.h`, so box/unbox pairs fold away.
|
|
246
|
+
- Runtime + program compile as one translation unit, letting the optimiser
|
|
247
|
+
inline across both without LTO.
|
|
248
|
+
- Host-tuned, stripped, frame-pointer-free release builds.
|
|
249
|
+
|
|
250
|
+
`python -m kasi bench` reports representative figures (Device Guard on some
|
|
251
|
+
Windows machines can skip individual native runs):
|
|
252
|
+
|
|
253
|
+
| Example | CPython | KASI | Speedup |
|
|
254
|
+
| --- | --- | --- | --- |
|
|
255
|
+
| `fib.py` | ~360 ms | ~20 ms | ~18× |
|
|
256
|
+
| `loop.py` | ~430 ms | ~15 ms | ~29× |
|
|
257
|
+
| `lists.py` | ~70 ms | ~15 ms | ~4.7× |
|
|
258
|
+
|
|
259
|
+
### Native fast paths
|
|
260
|
+
|
|
261
|
+
Constants, names, arithmetic, comparisons, `if`/`while`/`for`, `range`,
|
|
262
|
+
lists, strings, f-strings, `print`, `len`, `int`, `float`, `str`, `bool`,
|
|
263
|
+
list/dict literals, list/dict comprehensions, `in`/`not in`, user functions,
|
|
264
|
+
recursion, keyword arguments, `*args`, `**kwargs`, and `break`/`continue`.
|
|
265
|
+
|
|
266
|
+
### Dynamic/degraded features (always compile, with warnings)
|
|
267
|
+
|
|
268
|
+
- **Classes and methods** — instances are tagged runtime objects; methods are
|
|
269
|
+
registered in a runtime method table and dispatched dynamically via
|
|
270
|
+
`KValue`-boxed arguments. `__init__` is invoked on construction.
|
|
271
|
+
- **Imports / C-API extensions** — imports yield dynamic module objects; use
|
|
272
|
+
`@kasi.native` for a real zero-boxing C ABI instead.
|
|
273
|
+
- **Exceptions** — `try` bodies are compiled; handlers are skipped with a
|
|
274
|
+
warning. `raise` compiles to a runtime error.
|
|
275
|
+
- **Generators and closures** — `yield`/generator expressions and nested
|
|
276
|
+
functions degrade to dynamic values; closures cannot capture outer locals.
|
|
277
|
+
- **`with` statements** — only the body is compiled.
|
|
278
|
+
|
|
279
|
+
Warnings are emitted during compilation, returned in `CompileResult.warnings`,
|
|
280
|
+
and surfaced as `KASI005` diagnostics with `-Wall` / `kasi lint`.
|
|
281
|
+
|
|
282
|
+
## Development
|
|
283
|
+
|
|
284
|
+
From the directory containing `pyproject.toml`:
|
|
285
|
+
|
|
286
|
+
```bash
|
|
287
|
+
python -m pip install -e ".[zig]"
|
|
288
|
+
python -m pip install pytest
|
|
289
|
+
python -m pytest -q
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
Examples that show the dynamic feature set:
|
|
293
|
+
|
|
294
|
+
```bash
|
|
295
|
+
python -m kasi compile examples/features.py -o features.exe # dicts, comprehensions, imports, try
|
|
296
|
+
python -m kasi compile examples/objects.py -o objects.exe # classes, methods, kwargs, *args/**kwargs
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
On machines where Application Control/Device Guard blocks newly built `.exe`
|
|
300
|
+
files, the execution tests skip automatically; compile-only tests still verify
|
|
301
|
+
the full native link. Set `KASI_SKIP_NATIVE_RUN=1` to force that behavior.
|
|
302
|
+
|
|
303
|
+
This package is self-contained: the C runtime sources ship inside the wheel,
|
|
304
|
+
and no Python interpreter is needed to run compiled programs.
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"""Dynamic runtime features: dicts, comprehensions, imports and degraded statements."""
|
|
2
|
+
|
|
3
|
+
scores = {"Mike": 10, "Lerato": 20}
|
|
4
|
+
scores["Sipho"] = 30
|
|
5
|
+
print(scores["Lerato"])
|
|
6
|
+
print(len(scores))
|
|
7
|
+
print("Mike" in scores)
|
|
8
|
+
|
|
9
|
+
values = [x * 2 for x in range(5) if x % 2 == 0]
|
|
10
|
+
print(values)
|
|
11
|
+
|
|
12
|
+
lookup = {str(i): i * i for i in range(3)}
|
|
13
|
+
print(lookup["2"])
|
|
14
|
+
|
|
15
|
+
try:
|
|
16
|
+
print("safe")
|
|
17
|
+
except Exception:
|
|
18
|
+
print("bad")
|
|
19
|
+
|
|
20
|
+
import math
|
|
21
|
+
|
|
22
|
+
print(math.sqrt)
|