Skip to main content

Mantis 7 Python Compiler & Multi-File Source Manager

Project description

Mantis 7 — README Deterministic Native Compiler + Runtime + Loader

Mantis 7 is a minimal, deterministic, zero‑copy compiler toolchain. It takes Mantis source code (.mt), compiles it into portable bytecode (.mtn), translates that bytecode into native machine code (x86‑64 or ARM64), embeds a position‑independent string runtime, and executes it directly from RWX memory.

The entire pipeline is designed to be:

  • deterministic
  • architecture‑aware
  • zero‑copy
  • allocation‑safe
  • fully native (no interpreter fallback)
  • minimal and transparent

1. Architecture Overview

Mantis 7 consists of four major components:

1. Compiler (compiler.py)

  • Parses .mt source files
  • Emits MTN1 bytecode (portable ISA)
  • Embeds a string‑blob table for literals

2. Backends (backend_x64.py / backend_arm64.py)

  • Translate MTN1 bytecode → native machine code
  • Map builtin calls (-2 = concat, -3 = to_string) to runtime offsets
  • Produce a pure native function with correct calling convention

3. String Runtime (string_runtime.py)

  • Fully position‑independent machine‑code blob
  • Contains: __mantis_strlen __mantis_memcpy __mantis_string_concat __mantis_format_i64
  • Provides a bump‑allocated heap for string operations
  • build() returns (blob, offsets)

4. Loader (loader.py)

  • Loads .mtn or .mtnb bundles
  • Extracts string blob for dispatcher
  • Builds runtime blob + offsets
  • Calls backend with offsets
  • Appends runtime to native code
  • Executes the result directly from RWX memory

2. File Types

.mt — Mantis source file
.mtn — Single‑module bytecode file
.mtnb — Multi‑module bundle (MTNB format)
Contains modules + assets + offsets

3. Bytecode Format (MTN1)

Header:

    [magic "MTN1"]
    [fn_count u32]
    For each function:
        [code_len u32]
        code_len × instruction:
            [op u8][a i32][b i32][c i32]
    [string_blob_size u32]
    [string_blob raw bytes]

Instruction size: 13 bytes
All integers: little‑endian
All strings: UTF‑8, null‑terminated in runtime

4. Builtin Calls

The compiler emits negative function indices for builtins:

-1  → print(...)          (not yet mapped natively)
-2  → concat(a, b)        → __mantis_string_concat
-3  → to_string(i64)      → __mantis_format_i64

Backends resolve these using runtime offsets provided by build().

5. Native Execution Model

The loader:

  1. Reads bytecode
  2. Extracts string blob for dispatcher
  3. Builds runtime blob + offset table
  4. Calls backend:
    • emit_x64(bytecode, rt_offsets)
    • emit_arm64(bytecode, rt_offsets)
  5. Appends runtime blob to native code
  6. Allocates RWX memory
  7. Copies native code into executable memory
  8. Calls the entry function as: int64_t entry(void)

6. CLI Usage

The CLI entry point is:

    python cli.py {run|bench|build} {file.mt|project/}

Commands:

run

Compiles and immediately executes a single .mt file or a project.

Examples:

    python cli.py run hello.mt
    python cli.py run project/

Behavior:

  • Compiles source → .mtn
  • Loads .mtn via loader
  • Executes native code
  • Prints return value
bench

Runs a benchmark loop over the compiled program.

Examples:

python cli.py bench fib.mt
python cli.py bench project/

Behavior:

  • Compiles once
  • Executes N times (configurable)
  • Prints timing statistics
build

Compiles a file or project into a standalone .mtn or .mtnb bundle.

Examples:

python cli.py build hello.mt
python cli.py build project/

Behavior:

  • Produces .mtn for single file
  • Produces .mtnb for multi‑module project
  • Does NOT execute the result

7. Runtime Functions

__mantis_strlen(rdi)
#    Returns length of null‑terminated string.

__mantis_memcpy(rdi, rsi, rdx)
#    Copies rdx bytes from rsi → rdi.
#    Returns rdi.

__mantis_string_concat(a, b)
#    Allocates new buffer from bump heap.
#    Copies a, then b.
#    Null‑terminates.
#    Returns pointer.

__mantis_format_i64(value)
#    Converts signed 64‑bit integer to ASCII.
#    Writes digits in reverse, handles sign, null‑terminates.
#    Returns pointer.

8. Determinism Guarantees

  • No dynamic dispatch
  • No reflection
  • No GC
  • No OS‑dependent behavior
  • No heap fragmentation (bump allocator only)
  • All code generation is pure and reproducible
  • All runtime functions are pure machine code with fixed offsets

9. Supported Architectures

x86‑64 (System V ABI) ARM64 (AArch64, Linux / macOS / iOS)

  1. Example

hello.mt:

x = 41
print("Result: {x + 1}")

CLI:

python cli.py run hello.mt

Output:

Result: 42

11. License

MIT

12 Package Index

Mantis Package Index Note: Uploaded files are renamed to bin, rename and extract to use and please try to upload only zip files, if it is possible for your purpose.

END OF README

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

mantis_lang-1.0.3.tar.gz (22.2 kB view details)

Uploaded Source

Built Distribution

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

mantis_lang-1.0.3-py3-none-any.whl (24.1 kB view details)

Uploaded Python 3

File details

Details for the file mantis_lang-1.0.3.tar.gz.

File metadata

  • Download URL: mantis_lang-1.0.3.tar.gz
  • Upload date:
  • Size: 22.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.1

File hashes

Hashes for mantis_lang-1.0.3.tar.gz
Algorithm Hash digest
SHA256 a7e08fa4255f1445463f620a3c0bab11f492168b58064daf7a7c823530a80998
MD5 7da848d52674d00b62c319c22d405531
BLAKE2b-256 eb485b7177330fb2bfd07b08a0eee7cc75b5eb96c8c1824cb77dd6975d7794f9

See more details on using hashes here.

File details

Details for the file mantis_lang-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: mantis_lang-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 24.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.1

File hashes

Hashes for mantis_lang-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 8dee8a7ce39fc336d5fd2093c1a87581c9ee6ba00744559e71ea855fad0e642e
MD5 be6bd05f5159b9a8d7ddcaea475fa65b
BLAKE2b-256 fd9976e2b7370e66b349b09e3d478f5d25f5c2510430f0796d5fe0caa23defe9

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