Qwen3-0.6B inference (12 KB), bare-metal bootable OS (22 KB), and from-scratch Mini-GPT trainer with live Braille TUI (32 KB) in 100% pure x86-64 NASM assembly. Zero C, zero libc, zero dependencies.
#!/usr/bin/env python3
"""Scan a built binary for instruction forms outside a baseline ISA profile.
Usage: python3 scripts/check_isa.py [--profile avx1|sse2] build/asm-llm [build/asm-llm-os.elf ...]
--profile avx1 (default): the AVX1 (Sandy Bridge) build, `make AVX1=1`.
--profile sse2 : the x86-64 baseline build, `make SSE2=1 os`. Every VEX form, SSE3 and
later (SSSE3, SSE4.x, popcnt, crc32), XSAVE/XGETBV, BMI/ADX and
FMA/F16C are rejected; only SSE2 and older remain.
avx1 profile: flags the forms listed below, not every possible extension:
- FMA and F16C;
- AVX2: 256-bit integer ops (vp* on ymm, vmovntdqa / vmpsadbw on ymm),
vpbroadcast*, register-source vbroadcastss/sd, lane-crossing permutes,
gathers, variable shifts, masked moves, vpblendd;
- AVX-512: any zmm or mask (k0..k7) register;
- BMI1/BMI2/LZCNT and other post-Sandy-Bridge scalar extensions
(movbe, rdrand, rdseed, adcx/adox, rd/wr fs/gs base, invpcid, clflushopt, clwb).
sse2 profile: a deny list over the mnemonics, so any v* instruction fails.
Exit status 1 if anything is found.
"""
import re
import subprocess
import sys
AVX1_YMM_VP_OK = {"vpermilps", "vpermilpd", "vperm2f128", "vptest"}
AVX2_ON_YMM = {"vmovntdqa", "vmpsadbw"}
ALWAYS_BAD = re.compile(
r"^(vfn?m(add|sub)(sub)?\d{3}[ps][sd]|vfmaddsub\d{3}p[sd]|vfmsubadd\d{3}p[sd]"
r"|vcvtph2ps|vcvtps2ph"
r"|vpbroadcast[bwdq]|vbroadcasti128|vinserti128|vextracti128|vperm2i128"
r"|vpermd|vpermq|vpermps|vpermpd|vp?gather\w+|vpmaskmov[dq]"
r"|vps[lr]lv[dq]|vpsravd|vpblendd"
r"|andn|bextr|blsi|blsmsk|blsr|bzhi|mulx|pdep|pext|rorx|sarx|shlx|shrx|lzcnt|tzcnt"
r"|movbe|rdrand|rdseed|adcx|adox|rd[fg]sbase|wr[fg]sbase|invpcid|clflushopt|clwb)$"
)
AVX512_REG = re.compile(r"\b(zmm\d+|k[0-7])\b")
# sse2 profile: everything newer than SSE2, by mnemonic
SSE2_DENY = re.compile(
r"^(v[a-z0-9]+|xgetbv|xsetbv|xsave\w*|xrstor\w*"
r"|haddp[sd]|hsubp[sd]|addsubp[sd]|movddup|movshdup|movsldup|lddqu|fisttp|monitor|mwait"
r"|pshufb|phadd\w*|phsub\w*|pmaddubsw|pmulhrsw|psign\w*|pabs[bwd]|palignr"
r"|blendp[sd]|blendvp[sd]|pblend\w*|round[ps][sd]|pmovsx\w*|pmovzx\w*"
r"|pm(in|ax)(sb|uw|ud|sd)|pextr[bdq]|pinsr[bdq]|ptest|pcmpeqq|pcmpgtq|pmulld|pmuldq|packusdw"
r"|dpp[sd]|insertps|extractps|movntdqa|popcnt|crc32|pcmp[ei]str\w*|mpsadbw|phminposuw"
r"|andn|bextr|blsi|blsmsk|blsr|bzhi|tzcnt|lzcnt|mulx|rorx|sarx|shlx|shrx|adcx|adox|pdep|pext"
r"|movbe|rdrand|rdseed|rd[fg]sbase|wr[fg]sbase|invpcid|clflushopt|clwb)$"
)
PREFIXES = {"lock", "rep", "repz", "repnz", "bnd", "notrack", "data16", "cs", "ds", "es"}
def disassemble(path):
head = subprocess.run(["objdump", "-f", path], capture_output=True, text=True, check=True).stdout
# The Multiboot kernel is an ELF32 wrapper around 64-bit code: decode it as x86-64.
# Its small 32-bit entry stub may decode as noise; it holds no SIMD code.
arch = ["-m", "i386:x86-64"] if "elf32" in head else []
return subprocess.run(
["objdump", "-d", "-M", "intel", *arch, "--no-show-raw-insn", path],
capture_output=True, text=True, check=True,
).stdout
def scan(path, profile="avx1"):
bad = []
count = 0
for line in disassemble(path).splitlines():
m = re.match(r"\s*([0-9a-f]+):\s+(\S+)\s*(.*)", line)
if not m:
continue
addr, mnem, ops = m.groups()
if profile == "sse2":
# a prefix may be the first token: the instruction is the next one
while mnem in PREFIXES and ops:
mnem, _, ops = ops.partition(" ")
ops = ops.strip()
if mnem == "(bad)": # the 32-bit entry stub, decoded as 64-bit
continue
count += 1
if SSE2_DENY.match(mnem):
bad.append(f"{path}: {addr}: {mnem} {ops} [beyond SSE2]")
continue
reason = None
if ALWAYS_BAD.match(mnem):
reason = "FMA/F16C/AVX2/BMI or later"
elif AVX512_REG.search(ops):
reason = "AVX-512 register"
elif "ymm" in ops and (mnem in AVX2_ON_YMM or (mnem.startswith("vp") and mnem not in AVX1_YMM_VP_OK)):
reason = "AVX2 256-bit integer"
elif mnem in ("vbroadcastss", "vbroadcastsd") and re.search(r",\s*xmm\d+\s*$", ops):
reason = "AVX2 register-source broadcast"
if reason:
bad.append(f"{path}: {addr}: {mnem} {ops} [{reason}]")
return (bad, count) if profile == "sse2" else bad
def main():
args = sys.argv[1:]
profile = "avx1"
if args[:1] == ["--profile"]:
if len(args) < 2 or args[1] not in ("avx1", "sse2"):
sys.exit("usage: check_isa.py [--profile avx1|sse2] binary...")
profile = args[1]
args = args[2:]
paths = args or ["build/asm-llm"]
bad = []
for p in paths:
if profile == "sse2":
found, count = scan(p, "sse2")
print(f"{p}: {count} instructions, {len(found)} beyond SSE2")
else:
found = scan(p)
print(f"{p}: {len(found)} non-AVX1 instruction(s)")
bad += found
for b in bad[:200]:
print(" " + b)
sys.exit(1 if bad else 0)
if __name__ == "__main__":
main()