123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130(**************************************************************************)(* This file is part of BINSEC. *)(* *)(* Copyright (C) 2016-2026 *)(* CEA (Commissariat à l'énergie atomique et aux énergies *)(* alternatives) *)(* *)(* you can redistribute it and/or modify it under the terms of the GNU *)(* Lesser General Public License as published by the Free Software *)(* Foundation, version 2.1. *)(* *)(* It is distributed in the hope that it will be useful, *)(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)(* GNU Lesser General Public License for more details. *)(* *)(* See the GNU Lesser General Public License version 2.1 *)(* for more details (enclosed in the file licenses/LGPLv2.1). *)(* *)(**************************************************************************)typebitwidth=[`x16|`x32|`x64|`x128]typeendianness=Basic_types.endianness=LittleEndian|BigEndiantypethumb_mode=Basic_types.Ternary.t=False|True|Unknowntypeisa=|Unknown|ARMof{rev:[`v7ofthumb_mode|`v8];endianness:endianness}|PPCof{bits:[`x32|`x64];endianness:endianness}|RISCVof{bits:[`x32|`x64|`x128]}|SPARCof{rev:[`v8]}|X86of{bits:[`x32|`x64]}|Z80letunknown_msg="Machine ISA set to unknown. Aborting. Did you forget to set an -isa switch \
on the command line ?"(** Word size of the machine in bits *)moduleBitwidth=structtypet=bitwidthletbitsize=function|`x16->Size.Bit.bits16|`x32->Size.Bit.bits32|`x64->Size.Bit.bits64|`x128->Size.Bit.bits128letbytesizet=Size.Byte.of_bitsize(bitsizet)letppppft=Size.Bit.ppppf(bitsizet)letpp_print_hextppfx=matchtwith|`x16->Format.fprintfppf"%04x"x|`x32->Format.fprintfppf"%08x"x|`x64->Format.fprintfppf"%016x"x|`x128->Format.fprintfppf"%032x"xendmoduleISA=structtypet=isaletendianness=function|Unknown->failwithunknown_msg|ARM{endianness;_}->endianness|PPC{endianness;_}->endianness|RISCV_->LittleEndian|SPARC{rev=`v8}->BigEndian|X86_->LittleEndian|Z80->LittleEndianletbits=function|Unknown->failwithunknown_msg|ARM{rev=`v7_;_}->`x32|ARM{rev=`v8;_}->`x64|PPC{bits;_}->(bits:>bitwidth)|RISCV{bits;_}->(bits:>bitwidth)|SPARC{rev=`v8}->`x32|X86{bits;_}->(bits:>bitwidth)|Z80->`x16letword_sizet=Size.Bit.to_int(Bitwidth.bitsize(bitst))letstack_register=function|Unknown->failwithunknown_msg|ARM_->"sp"|PPC_->"r1"|RISCV_->"x2"|SPARC{rev=`v8}->"o6"|X86{bits=`x32}->"esp"|X86{bits=`x64}->"rsp"|Z80->"sp"letto_string=function|Unknown->"unknown"|ARM{rev=`v7_;_}->"armv7"|ARM{rev=`v8;_}->"armv8"|PPC_->"powerpc"|RISCV_->"risk-v"|SPARC{rev=`v8}->"sparcv8"|X86{bits=`x32}->"x86-32"|X86{bits=`x64}->"x86-64"|Z80->"Z80"letppppft=Format.pp_print_stringppf(to_stringt)endmoduleEndianness=structtypet=endiannessletppppf=function|LittleEndian->Format.fprintfppf"little endian"|BigEndian->Format.fprintfppf"big endian"endtypet=isaletamd64=X86{bits=`x64}letarmv7:?thumb:thumb_mode->endianness->t=fun?(thumb=False)endianness->ARM{rev=`v7thumb;endianness}letarmv8endianness=ARM{rev=`v8;endianness}letppc64endianness=PPC{bits=`x64;endianness}letriscvbits=RISCV{bits}letsparcv8=SPARC{rev=`v8}letx86=X86{bits=`x32}letz80=Z80letunknown=Unknownletpp=ISA.pp