CoqToLeanAsm: x86 Macro Assembler in Lean 4

14.1. Preventing ESP as SIB Index

In x86, the ESP register cannot be used as an index in SIB addressing. The encoding index=100 (which would be ESP) instead means "no index register." Traditional assemblers catch this as a runtime error. We catch it at compile time:

The NonSPReg type is defined as a subtype of Reg that excludes ESP:

X86.NonSPReg : Type#check NonSPReg -- Registers excluding ESP X86.NonSPReg.EAX : NonSPReg#check NonSPReg.EAX -- Valid X86.NonSPReg.ECX : NonSPReg#check NonSPReg.ECX -- Valid X86.NonSPReg.EBX : NonSPReg#check NonSPReg.EBX -- Valid -- NonSPReg.ESP doesn't exist!

The MemSpec.regIdx constructor requires a NonSPReg for the index:

MemSpec.regIdx EBX NonSPReg.ECX Scale.S4 : MemSpec#check MemSpec.regIdx EBX (.ECX) Scale.S4 -- This would NOT compile: -- #check MemSpec.regIdx EBX (.ESP) Scale.S4 -- Error: NonSPReg has no constructor ESP