miaou — le blogv1.0.3

inter.inter

← Le code · src/amorce/inter.inter · 3894 lignes

; inter.inter : le compilateur du langage intermédiaire inter (docs/spec-inter.md), écrit en inter (phase 1c).
; Point fixe : inter.macos-arm64 < inter.inter | encodeur.macos-arm64 | signeur.macos-arm64 redonne
; inter.macos-arm64 au bit près. Première construction : encodeur.macos-arm64 < inter.mac | signeur.macos-arm64
; donne un binaire intermédiaire (inter.mac est l'image initiale), qui compile ce source en inter.macos-arm64.
; Il fait exactement ce que fait inter.mac : mêmes fautes, dans le même ordre, même texte produit.
;
; Zone de travail (0x32900000 octets, mmap, réservés sans être touchés), passée à chaque fonction sous le nom z (inter n'a pas de variable globale) :
;   0x0000000 état (champs de 8 octets, ci-dessous) ; 0x100 chiffres décimaux ; 0x1000 texte d'une faute ;
;   0x2000 en-tête produit ; 0x2400 signature produite ; 0x2800 correctifs ; 0x3000 texte de l'en-tête ;
;   0x4000 à 0x10000 zéros (cases d'empreinte) ;
;   0x0100000 noms locaux (8 192 × 64 octets) ; 0x0200000 pile des parenthèses (16 384 × 8 octets) ;
;   0x0400000 noms de tête (16 384 × 256 octets) ; 0x0800000 source (32 Mio) ; 0x2800000 jetons (8 388 608 × 64
;   octets) ; 0x22800000 texte produit (256 Mio, comme inter.mac) ; 0x32800000 chaîne décodée (1 Mio).
; Les capacités sont celles d'inter.mac (elles se voient : LIMITE) ; les dispositions, non : tout champ fait ici
; 8 octets, parce qu'inter n'écrit que 8 octets (store) ou 1 (store8).
; État : +0x00 longueur du source, +0x08 nombre de jetons, +0x28 pointeur d'écriture, +0x38 noms locaux visibles,
;   +0x48 noms de tête, +0x50 octets de code, +0x58 étiquettes produites, +0x60 renvois produits, +0x68 fonction en
;   cours (son entrée), +0x70 cases de la fonction, +0x78 taille de son cadre, +0x80 octets de données, +0x88 jeton
;   du nom dans le dernier (end …), +0x90 ligne de la fin du fichier, +0xb0 profondeur des parenthèses, +0xf0 et
;   +0xf8 les étiquettes des deux pièges de la fonction en cours (brk #1, brk #2, posés après elle).
; Jeton (64 octets) : +0 sorte (1 '(', 2 ')', 3 nom ou mot réservé, 4 entier, 5 chaîne, 6 type), +8 numéro du mot
;   réservé (1 à 51) ou du type (1 Ent, 2 Nat, 3 Bool, 4 Adr), +16 drapeaux d'entier (1 négatif, 2 trop grand),
;   +24 ligne, +32 début dans le source, +40 longueur, +48 grandeur, +56 pour '(' : l'index de sa fermante.
; Une adresse de zone ou de champ ne s'écrit en chiffres que dans sa fonction, ci-dessous : ailleurs, on la nomme.
 
; ---------------------------------------------------------------------------
; Zones
 
(fn zone_chiffres (params (z Adr)) (returns Adr)
  (let p Adr (offset z 0x100))
  (return p)
  (end fn zone_chiffres))
 
(fn zone_faute (params (z Adr)) (returns Adr)
  (let p Adr (offset z 0x1000))
  (return p)
  (end fn zone_faute))
 
(fn zone_parentheses (params (z Adr)) (returns Adr)
  (let p Adr (offset z 0x200000))
  (return p)
  (end fn zone_parentheses))
 
(fn zone_source (params (z Adr)) (returns Adr)
  (let p Adr (offset z 0x800000))
  (return p)
  (end fn zone_source))
 
(fn zone_jetons (params (z Adr)) (returns Adr)
  (let p Adr (offset z 0x2800000))
  (return p)
  (end fn zone_jetons))
 
; ---------------------------------------------------------------------------
; Champs de l'état
 
(fn e_longueur_source (params (z Adr)) (returns Adr)
  (let p Adr (offset z 0x00))
  (return p)
  (end fn e_longueur_source))
 
(fn e_nombre_jetons (params (z Adr)) (returns Adr)
  (let p Adr (offset z 0x08))
  (return p)
  (end fn e_nombre_jetons))
 
(fn e_ecriture (params (z Adr)) (returns Adr)
  (let p Adr (offset z 0x28))
  (return p)
  (end fn e_ecriture))
 
(fn e_ligne_fin (params (z Adr)) (returns Adr)
  (let p Adr (offset z 0x90))
  (return p)
  (end fn e_ligne_fin))
 
(fn e_profondeur (params (z Adr)) (returns Adr)
  (let p Adr (offset z 0xb0))
  (return p)
  (end fn e_profondeur))
 
; ---------------------------------------------------------------------------
; Champs d'un jeton (i : son index)
 
(fn jeton (params (z Adr) (i Nat)) (returns Adr)
  (let base Adr (call zone_jetons z))
  (let d Nat (mul i 64))
  (let t Adr (offset base d))
  (return t)
  (end fn jeton))
 
(fn j_sorte (params (z Adr) (i Nat)) (returns Adr)
  (let t Adr (call jeton z i))
  (return t)
  (end fn j_sorte))
 
(fn j_mot (params (z Adr) (i Nat)) (returns Adr)
  (let t Adr (call jeton z i))
  (let p Adr (offset t 8))
  (return p)
  (end fn j_mot))
 
(fn j_drapeaux (params (z Adr) (i Nat)) (returns Adr)
  (let t Adr (call jeton z i))
  (let p Adr (offset t 16))
  (return p)
  (end fn j_drapeaux))
 
(fn j_ligne (params (z Adr) (i Nat)) (returns Adr)
  (let t Adr (call jeton z i))
  (let p Adr (offset t 24))
  (return p)
  (end fn j_ligne))
 
(fn j_debut (params (z Adr) (i Nat)) (returns Adr)
  (let t Adr (call jeton z i))
  (let p Adr (offset t 32))
  (return p)
  (end fn j_debut))
 
(fn j_longueur (params (z Adr) (i Nat)) (returns Adr)
  (let t Adr (call jeton z i))
  (let p Adr (offset t 40))
  (return p)
  (end fn j_longueur))
 
(fn j_grandeur (params (z Adr) (i Nat)) (returns Adr)
  (let t Adr (call jeton z i))
  (let p Adr (offset t 48))
  (return p)
  (end fn j_grandeur))
 
(fn j_fermante (params (z Adr) (i Nat)) (returns Adr)
  (let t Adr (call jeton z i))
  (let p Adr (offset t 56))
  (return p)
  (end fn j_fermante))
 
; ---------------------------------------------------------------------------
; Écriture d'un texte au pointeur d'écriture de l'état
 
; mettre : l'octet c, puis le pointeur avance
(fn mettre (params (z Adr) (c Nat))
  (let e Adr (call e_ecriture z))
  (let p Adr (load Adr e))
  (store8 p c)
  (let q Adr (offset p 1))
  (store Adr e q)
  (end fn mettre))
 
; mettre_octets : les n octets de l'adresse a
(fn mettre_octets (params (z Adr) (a Adr) (n Nat))
  (var i Nat 0)
  (loop octets
    (let fini Bool (eq i n))
    (if fini (then (break octets)))
    (let p Adr (offset a i))
    (let c Nat (load8 p))
    (call mettre z c)
    (set i (add i 1))
    (end loop octets))
  (end fn mettre_octets))
 
; mettre_dec : v en décimal, sans zéro de tête
(fn mettre_dec (params (z Adr) (v Nat))
  (let t Adr (call zone_chiffres z))
  (var x Nat v)
  (var n Nat 0)
  (loop chiffres
    (let q Nat (div x 10))
    (let r Nat (mod x 10))
    (let c Nat (add r 48))
    (let p Adr (offset t n))
    (store8 p c)
    (set n (add n 1))
    (set x q)
    (let nul Bool (eq x 0))
    (if nul (then (break chiffres)))
    (end loop chiffres))
  (loop copie
    (set n (sub n 1))
    (let p Adr (offset t n))
    (let c Nat (load8 p))
    (call mettre z c)
    (let premier Bool (eq n 0))
    (if premier (then (break copie)))
    (end loop copie))
  (end fn mettre_dec))
 
; chiffre_hex : le chiffre hexadécimal minuscule de v (0 à 15)
(fn chiffre_hex (params (v Nat)) (returns Nat)
  (let petit Bool (lt v 10))
  (if petit (then
    (let c Nat (add v 48))
    (return c)))
  (let c Nat (add v 87))
  (return c)
  (end fn chiffre_hex))
 
; mettre_echappe : l'octet c, échappé pour une chaîne JSON (\" \\ ; \u00XX pour un caractère de contrôle)
(fn mettre_echappe (params (z Adr) (c Nat))
  (let guillemet Bool (eq c 34))
  (let barre Bool (eq c 92))
  (let special Bool (or guillemet barre))
  (if special (then
    (call mettre z 92)
    (call mettre z c)
    (return)))
  (let bas Bool (lt c 32))
  (let del Bool (eq c 127))
  (let controle Bool (or bas del))
  (if controle (then
    (call mettre z 92)
    (call mettre z 117)
    (call mettre z 48)
    (call mettre z 48)
    (let h Nat (shr c 4))
    (let ch Nat (call chiffre_hex h))
    (call mettre z ch)
    (let l Nat (band c 15))
    (let cl Nat (call chiffre_hex l))
    (call mettre z cl)
    (return)))
  (call mettre z c)
  (end fn mettre_echappe))
 
; ---------------------------------------------------------------------------
; Fautes : une ligne JSON sur la sortie d'erreur, puis la sortie (2, ou 3 pour LIMITE). Une faute ne rend jamais
; la main : ce qui suit son appel ne s'exécute pas.
 
(data t_lecture "LECTURE")
(data t_forme "FORME")
(data t_nom_inconnu "NOM_INCONNU")
(data t_nom_double "NOM_DOUBLE")
(data t_type "TYPE")
(data t_non_mutable "NON_MUTABLE")
(data t_hors_bornes "HORS_BORNES")
(data t_retour_manquant "RETOUR_MANQUANT")
(data t_limite "LIMITE")
(data t_code "{\"code\":\"")
(data t_ligne "\",\"ligne\":")
(data t_mot ",\"mot\":\"")
(data t_fin_faute "}
")
 
; mettre_code : le nom du code de faute (0 LECTURE … 8 LIMITE)
(fn mettre_code (params (z Adr) (code Nat))
  (let c0 Bool (eq code 0))
  (if c0 (then
    (let n Nat (size t_lecture))
    (call mettre_octets z t_lecture n)
    (return)))
  (let c1 Bool (eq code 1))
  (if c1 (then
    (let n Nat (size t_forme))
    (call mettre_octets z t_forme n)
    (return)))
  (let c2 Bool (eq code 2))
  (if c2 (then
    (let n Nat (size t_nom_inconnu))
    (call mettre_octets z t_nom_inconnu n)
    (return)))
  (let c3 Bool (eq code 3))
  (if c3 (then
    (let n Nat (size t_nom_double))
    (call mettre_octets z t_nom_double n)
    (return)))
  (let c4 Bool (eq code 4))
  (if c4 (then
    (let n Nat (size t_type))
    (call mettre_octets z t_type n)
    (return)))
  (let c5 Bool (eq code 5))
  (if c5 (then
    (let n Nat (size t_non_mutable))
    (call mettre_octets z t_non_mutable n)
    (return)))
  (let c6 Bool (eq code 6))
  (if c6 (then
    (let n Nat (size t_hors_bornes))
    (call mettre_octets z t_hors_bornes n)
    (return)))
  (let c7 Bool (eq code 7))
  (if c7 (then
    (let n Nat (size t_retour_manquant))
    (call mettre_octets z t_retour_manquant n)
    (return)))
  (let n Nat (size t_limite))
  (call mettre_octets z t_limite n)
  (end fn mettre_code))
 
; faute : code, ligne, adresse et longueur du mot cité (longueur 0 : pas de mot)
(fn faute (params (z Adr) (code Nat) (ligne Nat) (mot Adr) (long Nat))
  (let tampon Adr (call zone_faute z))
  (let e Adr (call e_ecriture z))
  (store Adr e tampon)
  (let n0 Nat (size t_code))
  (call mettre_octets z t_code n0)
  (call mettre_code z code)
  (let n1 Nat (size t_ligne))
  (call mettre_octets z t_ligne n1)
  (call mettre_dec z ligne)
  (let avec_mot Bool (ne long 0))
  (if avec_mot (then
    (let n2 Nat (size t_mot))
    (call mettre_octets z t_mot n2)
    (var i Nat 0)
    (loop mot_cite
      (let fini Bool (eq i long))
      (if fini (then (break mot_cite)))
      (let p Adr (offset mot i))
      (let c Nat (load8 p))
      (call mettre_echappe z c)
      (set i (add i 1))
      (end loop mot_cite))
    (call mettre z 34)))
  (let n3 Nat (size t_fin_faute))
  (call mettre_octets z t_fin_faute n3)
  (let fin Adr (load Adr e))
  (let longueur Nat (diff fin tampon))
  (let ecrit Ent (syscall 4 2 tampon longueur))
  (var sortie Nat 2)
  (let est_limite Bool (eq code 8))
  (if est_limite (then (set sortie 3)))
  (let r Ent (syscall 1 sortie))
  (end fn faute))
 
; echec_es : lecture ou écriture impossible (code 1, sans message)
(fn echec_es (params)
  (let r Ent (syscall 1 1))
  (end fn echec_es))
 
; ---------------------------------------------------------------------------
; Lecture : tout le source (32 Mio au plus) dans sa zone
 
; compter_sauts : nombre de sauts de ligne dans les n octets de l'adresse a
(fn compter_sauts (params (a Adr) (n Nat)) (returns Nat)
  (var i Nat 0)
  (var sauts Nat 0)
  (loop octets
    (let fini Bool (eq i n))
    (if fini (then (break octets)))
    (let p Adr (offset a i))
    (let c Nat (load8 p))
    (let saut Bool (eq c 10))
    (if saut (then (set sauts (add sauts 1))))
    (set i (add i 1))
    (end loop octets))
  (return sauts)
  (end fn compter_sauts))
 
; trop_long : le source dépasse 32 Mio : LIMITE à la ligne où tombe son premier octet de trop (les 32 Mio lus
; comptés ; le reste n'est pas lu)
(fn trop_long (params (z Adr) (lu Nat))
  (let source Adr (call zone_source z))
  (let sauts Nat (call compter_sauts source lu))
  (let ligne Nat (add sauts 1))
  (let nt Nat (size t_cap_texte))
  (call limite_a z ligne t_cap_texte nt 33554432)
  (end fn trop_long))
 
(fn lire_source (params (z Adr))
  (let source Adr (call zone_source z))
  (var lu Nat 0)
  (loop lecture
    (let place Nat (sub 33554432 lu))
    (let plein Bool (eq place 0))
    ; 32 Mio lus : un octet de plus, lu dans la zone des jetons (encore libre), dit s'il y a un reste
    (if plein (then
      (let tampon Adr (call zone_jetons z))
      (let r1 Ent (syscall 3 0 tampon 1))
      (let erreur1 Bool (lt r1 0))
      (if erreur1 (then (call echec_es)))
      (let fini Bool (eq r1 0))
      (if fini (then (break lecture)))
      (call trop_long z lu)))
    (let p Adr (offset source lu))
    (let r Ent (syscall 3 0 p place))
    (let erreur Bool (lt r 0))
    (if erreur (then (call echec_es)))
    (let vide Bool (eq r 0))
    (if vide (then (break lecture)))
    (let n Nat (to_nat r))
    (set lu (add lu n))
    (end loop lecture))
  (let a Adr (call e_longueur_source z))
  (store Nat a lu)
  (end fn lire_source))
 
; ---------------------------------------------------------------------------
; Jetons
 
 
; nouveau_jeton : un jeton de plus (8 388 608 au plus) ; rend son index
(fn nouveau_jeton (params (z Adr) (sorte Nat) (debut Nat) (long Nat) (ligne Nat)) (returns Nat)
  (let a Adr (call e_nombre_jetons z))
  (let i Nat (load Nat a))
  (let plein Bool (ge i 8388608))
  (if plein (then
    (let nj Nat (size t_cap_jetons))
    (call limite_a z ligne t_cap_jetons nj 8388608)))
  (let ps Adr (call j_sorte z i))
  (store Nat ps sorte)
  (let pm Adr (call j_mot z i))
  (store Nat pm 0)
  (let pd Adr (call j_drapeaux z i))
  (store Nat pd 0)
  (let pl Adr (call j_ligne z i))
  (store Nat pl ligne)
  (let pb Adr (call j_debut z i))
  (store Nat pb debut)
  (let pn Adr (call j_longueur z i))
  (store Nat pn long)
  (let pg Adr (call j_grandeur z i))
  (store Nat pg 0)
  (let pf Adr (call j_fermante z i))
  (store Nat pf 0)
  (let i_plus Nat (add i 1))
  (store Nat a i_plus)
  (return i)
  (end fn nouveau_jeton))
 
(fn est_chiffre (params (c Nat)) (returns Bool)
  (let bas Bool (ge c 48))
  (let haut Bool (le c 57))
  (let oui Bool (and bas haut))
  (return oui)
  (end fn est_chiffre))
 
(fn est_minuscule (params (c Nat)) (returns Bool)
  (let bas Bool (ge c 97))
  (let haut Bool (le c 122))
  (let oui Bool (and bas haut))
  (return oui)
  (end fn est_minuscule))
 
(fn est_majuscule (params (c Nat)) (returns Bool)
  (let bas Bool (ge c 65))
  (let haut Bool (le c 90))
  (let oui Bool (and bas haut))
  (return oui)
  (end fn est_majuscule))
 
; est_mot : c est dans [A-Za-z0-9_-]
(fn est_mot (params (c Nat)) (returns Bool)
  (let chiffre Bool (call est_chiffre c))
  (let minuscule Bool (call est_minuscule c))
  (let majuscule Bool (call est_majuscule c))
  (let souligne Bool (eq c 95))
  (let tiret Bool (eq c 45))
  (let lettre Bool (or minuscule majuscule))
  (let signe Bool (or souligne tiret))
  (let a Bool (or chiffre lettre))
  (let oui Bool (or a signe))
  (return oui)
  (end fn est_mot))
 
; memes_octets : les n octets de a et de b sont les mêmes
(fn memes_octets (params (a Adr) (b Adr) (n Nat)) (returns Bool)
  (var i Nat 0)
  (loop octets
    (let fini Bool (eq i n))
    (if fini (then (return true)))
    (let pa Adr (offset a i))
    (let ca Nat (load8 pa))
    (let pb Adr (offset b i))
    (let cb Nat (load8 pb))
    (let autre Bool (ne ca cb))
    (if autre (then (return false)))
    (set i (add i 1))
    (end loop octets))
  (end fn memes_octets))
 
; numero_dans_liste : la place (à partir de 1) du mot de n octets en a dans la liste de mots séparés par une
; espace (taille octets en liste) ; 0 s'il n'y est pas
(fn numero_dans_liste (params (liste Adr) (taille Nat) (a Adr) (n Nat)) (returns Nat)
  (var k Nat 0)
  (var numero Nat 1)
  (loop mots
    (let fini Bool (ge k taille))
    (if fini (then (return 0)))
    (var e Nat k)
    (loop lettres
      (let bout Bool (eq e taille))
      (if bout (then (break lettres)))
      (let pe Adr (offset liste e))
      (let c Nat (load8 pe))
      (let espace Bool (eq c 32))
      (if espace (then (break lettres)))
      (set e (add e 1))
      (end loop lettres))
    (let long Nat (sub e k))
    (let meme_long Bool (eq long n))
    (if meme_long (then
      (let pk Adr (offset liste k))
      (let meme Bool (call memes_octets pk a n))
      (if meme (then (return numero)))))
    (set k (add e 1))
    (set numero (add numero 1))
    (end loop mots))
  (end fn numero_dans_liste))
 
; les mots réservés, dans l'ordre de leur numéro (1 data … 51 false, 52 call_extern) ; les types (1 Ent … 4 Adr)
(data mots_reserves "data fn params returns end let var set if then else loop break continue return store store8 call add sub mul div mod neg eq ne lt le gt ge and or not band bor bxor bnot shl shr to_nat to_ent adr_of nat_of offset diff load load8 size syscall true false call_extern")
(data noms_types "Ent Nat Bool Adr")
 
; mot_fautif : LECTURE qui cite le mot entier
(fn mot_fautif (params (z Adr) (i Nat))
  (let source Adr (call zone_source z))
  (let pl Adr (call j_ligne z i))
  (let ligne Nat (load Nat pl))
  (let pb Adr (call j_debut z i))
  (let debut Nat (load Nat pb))
  (let pn Adr (call j_longueur z i))
  (let n Nat (load Nat pn))
  (let a Adr (offset source debut))
  (call faute z 0 ligne a n)
  (end fn mot_fautif))
 
; classer_nom : [a-z][a-z0-9_]*, 24 caractères au plus ; garde son numéro de mot réservé (0 pour un nom)
(fn classer_nom (params (z Adr) (i Nat) (a Adr) (n Nat))
  (let long Bool (gt n 24))
  (if long (then (call mot_fautif z i)))
  (var k Nat 0)
  (loop lettres
    (let fini Bool (eq k n))
    (if fini (then (break lettres)))
    (let p Adr (offset a k))
    (let c Nat (load8 p))
    (let minuscule Bool (call est_minuscule c))
    (let chiffre Bool (call est_chiffre c))
    (let souligne Bool (eq c 95))
    (let lettre Bool (or minuscule chiffre))
    (let bon Bool (or lettre souligne))
    (if bon (then) (else (call mot_fautif z i)))
    (set k (add k 1))
    (end loop lettres))
  (let taille Nat (size mots_reserves))
  (let numero Nat (call numero_dans_liste mots_reserves taille a n))
  (let pm Adr (call j_mot z i))
  (store Nat pm numero)
  (end fn classer_nom))
 
; classer_type : Ent, Nat, Bool, Adr (seuls mots à majuscule)
(fn classer_type (params (z Adr) (i Nat) (a Adr) (n Nat))
  (let taille Nat (size noms_types))
  (let numero Nat (call numero_dans_liste noms_types taille a n))
  (let inconnu Bool (eq numero 0))
  (if inconnu (then (call mot_fautif z i)))
  (let ps Adr (call j_sorte z i))
  (store Nat ps 6)
  (let pm Adr (call j_mot z i))
  (store Nat pm numero)
  (end fn classer_type))
 
; valeur_hex : la valeur du chiffre hexadécimal minuscule c, ou 16 s'il n'en est pas un
(fn valeur_hex (params (c Nat)) (returns Nat)
  (let chiffre Bool (call est_chiffre c))
  (if chiffre (then
    (let v Nat (sub c 48))
    (return v)))
  (let bas Bool (ge c 97))
  (let haut Bool (le c 102))
  (let lettre Bool (and bas haut))
  (if lettre (then
    (let v Nat (sub c 87))
    (return v)))
  (return 16)
  (end fn valeur_hex))
 
; classer_entier : -?[0-9]+ sans zéro de tête, ou 0x[0-9a-f]+ ; la grandeur est gardée et « trop grand » noté (la
; borne dépend du type, vue plus tard : HORS_BORNES). Une fois trop grand, la grandeur n'est plus calculée : elle
; ne sert plus (HORS_BORNES est décidé sur le drapeau seul).
(fn classer_entier (params (z Adr) (i Nat) (a Adr) (n Nat))
  (var k Nat 0)
  (var negatif Nat 0)
  (var trop Nat 0)
  (var g Nat 0)
  (let c0 Nat (load8 a))
  (let moins Bool (eq c0 45))
  (if moins (then
    (set negatif 1)
    (set k 1)
    (let seul Bool (ge k n))
    (if seul (then (call mot_fautif z i)))))
  (let pk Adr (offset a k))
  (let c Nat (load8 pk))
  (let zero Bool (eq c 48))
  (let k1 Nat (add k 1))
  (let apres Bool (lt k1 n))
  (let prefixe Bool (and zero apres))
  (if prefixe (then
    ; un 0 suivi d'autre chose : seulement 0x, sans signe, suivi d'au moins un chiffre
    (let px Adr (offset a k1))
    (let cx Nat (load8 px))
    (let x Bool (eq cx 120))
    (if x (then) (else (call mot_fautif z i)))
    (let signe Bool (eq negatif 1))
    (if signe (then (call mot_fautif z i)))
    (set k (add k 2))
    (let vide Bool (ge k n))
    (if vide (then (call mot_fautif z i)))
    (loop hex
      (let fini Bool (ge k n))
      (if fini (then (break hex)))
      (let p Adr (offset a k))
      (let d Nat (load8 p))
      (set k (add k 1))
      (let v Nat (call valeur_hex d))
      (let pas_chiffre Bool (eq v 16))
      (if pas_chiffre (then (call mot_fautif z i)))
      (let deja Bool (eq trop 2))
      (if deja (then (continue hex)))
      (let haut Nat (shr g 60))
      (let deborde Bool (ne haut 0))
      (if deborde (then
        (set trop 2)
        (continue hex)))
      (let decale Nat (shl g 4))
      (set g (add decale v))
      (end loop hex))
    (call ranger_entier z i negatif trop g)
    (return)))
  (loop decimal
    (let fini Bool (ge k n))
    (if fini (then (break decimal)))
    (let p Adr (offset a k))
    (let d Nat (load8 p))
    (set k (add k 1))
    (let chiffre Bool (call est_chiffre d))
    (if chiffre (then) (else (call mot_fautif z i)))
    (let deja Bool (eq trop 2))
    (if deja (then (continue decimal)))
    ; g × 10 + v déborde-t-il 2⁶⁴ − 1 ?
    (let v Nat (sub d 48))
    (let grand Bool (gt g 1844674407370955161))
    (if grand (then
      (set trop 2)
      (continue decimal)))
    (let dix Nat (mul g 10))
    (let marge Nat (sub 18446744073709551615 dix))
    (let deborde Bool (lt marge v))
    (if deborde (then
      (set trop 2)
      (continue decimal)))
    (set g (add dix v))
    (end loop decimal))
  (call ranger_entier z i negatif trop g)
  (end fn classer_entier))
 
(fn ranger_entier (params (z Adr) (i Nat) (negatif Nat) (trop Nat) (g Nat))
  (let ps Adr (call j_sorte z i))
  (store Nat ps 4)
  (let drapeaux Nat (bor negatif trop))
  (let pd Adr (call j_drapeaux z i))
  (store Nat pd drapeaux)
  (let pg Adr (call j_grandeur z i))
  (store Nat pg g)
  (end fn ranger_entier))
 
; classer_mot : le jeton i est un mot ; nom (avec son mot réservé), type ou entier ; sinon LECTURE qui le cite
(fn classer_mot (params (z Adr) (i Nat))
  (let source Adr (call zone_source z))
  (let pb Adr (call j_debut z i))
  (let debut Nat (load Nat pb))
  (let pn Adr (call j_longueur z i))
  (let n Nat (load Nat pn))
  (let a Adr (offset source debut))
  (let c Nat (load8 a))
  (let chiffre Bool (call est_chiffre c))
  (let moins Bool (eq c 45))
  (let entier Bool (or chiffre moins))
  (if entier (then
    (call classer_entier z i a n)
    (return)))
  (let minuscule Bool (call est_minuscule c))
  (if minuscule (then
    (call classer_nom z i a n)
    (return)))
  (let majuscule Bool (call est_majuscule c))
  (if majuscule (then
    (call classer_type z i a n)
    (return)))
  (call mot_fautif z i)
  (end fn classer_mot))
 
; lire_mot : un mot commence à debut ; rend la position qui le suit
(fn lire_mot (params (z Adr) (debut Nat) (ligne Nat)) (returns Nat)
  (let source Adr (call zone_source z))
  (let al Adr (call e_longueur_source z))
  (let total Nat (load Nat al))
  (var k Nat (add debut 1))
  (loop lettres
    (let fini Bool (ge k total))
    (if fini (then (break lettres)))
    (let p Adr (offset source k))
    (let c Nat (load8 p))
    (let encore Bool (call est_mot c))
    (if encore (then) (else (break lettres)))
    (set k (add k 1))
    (end loop lettres))
  (let n Nat (sub k debut))
  (let i Nat (call nouveau_jeton z 3 debut n ligne))
  (call classer_mot z i)
  (return k)
  (end fn lire_mot))
 
; lire_chaine : une chaîne commence à debut (son '"'), sur la ligne ligne ; échappements \\ et \" seulement ;
; octets 0x20 à 0x7e, saut de ligne, 0x80 et plus ; rend la position qui suit le '"' fermant. Une faute dans la
; chaîne est à sa première ligne, sans mot.
(fn lire_chaine (params (z Adr) (debut Nat) (ligne Nat)) (returns Nat)
  (let source Adr (call zone_source z))
  (let al Adr (call e_longueur_source z))
  (let total Nat (load Nat al))
  (var k Nat (add debut 1))
  (loop octets
    (let dehors Bool (ge k total))
    (if dehors (then (call faute z 0 ligne source 0)))
    (let p Adr (offset source k))
    (let c Nat (load8 p))
    (let ferme Bool (eq c 34))
    (if ferme (then (break octets)))
    (let barre Bool (eq c 92))
    (if barre (then
      (set k (add k 1))
      (let bout Bool (ge k total))
      (if bout (then (call faute z 0 ligne source 0)))
      (let q Adr (offset source k))
      (let e Nat (load8 q))
      (let e_barre Bool (eq e 92))
      (let e_guillemet Bool (eq e 34))
      (let connu Bool (or e_barre e_guillemet))
      (if connu (then) (else (call faute z 0 ligne source 0)))
      (set k (add k 1))
      (continue octets)))
    (let saut Bool (eq c 10))
    (let bas Bool (lt c 32))
    (let del Bool (eq c 127))
    (let pas_saut Bool (not saut))
    (let controle Bool (and bas pas_saut))
    (let refuse Bool (or controle del))
    (if refuse (then (call faute z 0 ligne source 0)))
    (set k (add k 1))
    (end loop octets))
  (let fin Nat (add k 1))
  (let n Nat (sub fin debut))
  (let i Nat (call nouveau_jeton z 5 debut n ligne))
  (return fin)
  (end fn lire_chaine))
 
; ouvrir : '(' à pos ; un jeton, et sa place sur la pile des parenthèses (16 384 ouvertes au plus)
(fn ouvrir (params (z Adr) (pos Nat) (ligne Nat))
  (let i Nat (call nouveau_jeton z 1 pos 1 ligne))
  (let ap Adr (call e_profondeur z))
  (let prof Nat (load Nat ap))
  (let plein Bool (ge prof 16384))
  (if plein (then
    (let np Nat (size t_cap_parentheses))
    (call limite_a z ligne t_cap_parentheses np 16384)))
  (let pile Adr (call zone_parentheses z))
  (let d Nat (mul prof 8))
  (let place Adr (offset pile d))
  (store Nat place i)
  (let suivante Nat (add prof 1))
  (store Nat ap suivante)
  (end fn ouvrir))
 
; fermer : ')' à pos ; ferme la dernière '(' ouverte, qui retient sa fermante ; sans '(' ouverte, LECTURE
(fn fermer (params (z Adr) (pos Nat) (ligne Nat))
  (let source Adr (call zone_source z))
  (let ap Adr (call e_profondeur z))
  (let prof Nat (load Nat ap))
  (let seule Bool (eq prof 0))
  (if seule (then
    (let p Adr (offset source pos))
    (call faute z 0 ligne p 1)))
  (let i Nat (call nouveau_jeton z 2 pos 1 ligne))
  (let haut Nat (sub prof 1))
  (store Nat ap haut)
  (let pile Adr (call zone_parentheses z))
  (let d Nat (mul haut 8))
  (let place Adr (offset pile d))
  (let o Nat (load Nat place))
  (let pf Adr (call j_fermante z o))
  (store Nat pf i)
  (end fn fermer))
 
; interdit : caractère interdit à pos ; la faute le cite en entier (1 à 4 octets d'UTF-8, selon son premier octet)
(fn interdit (params (z Adr) (pos Nat) (ligne Nat))
  (let source Adr (call zone_source z))
  (let al Adr (call e_longueur_source z))
  (let total Nat (load Nat al))
  (let p Adr (offset source pos))
  (let c Nat (load8 p))
  (var n Nat 1)
  (let deux_octets Bool (ge c 0xc0))
  (if deux_octets (then (set n 2)))
  (let trois_octets Bool (ge c 0xe0))
  (if trois_octets (then (set n 3)))
  (let quatre_octets Bool (ge c 0xf0))
  (if quatre_octets (then (set n 4)))
  (let reste Nat (sub total pos))
  (let coupe Bool (gt n reste))
  (if coupe (then (set n reste)))
  (call faute z 0 ligne p n)
  (end fn interdit))
 
; fin_de_ligne : la position du saut de ligne qui suit pos, ou la fin du texte
(fn fin_de_ligne (params (source Adr) (pos Nat) (total Nat)) (returns Nat)
  (var k Nat pos)
  (loop octets
    (set k (add k 1))
    (let fini Bool (ge k total))
    (if fini (then (break octets)))
    (let p Adr (offset source k))
    (let c Nat (load8 p))
    (let saut Bool (eq c 10))
    (if saut (then (break octets)))
    (end loop octets))
  (return k)
  (end fn fin_de_ligne))
 
; lire_jetons : tout le texte en jetons ; fautes LECTURE dans l'ordre du texte ; à la fin, toute '(' fermée
(fn lire_jetons (params (z Adr))
  (let source Adr (call zone_source z))
  (let al Adr (call e_longueur_source z))
  (let total Nat (load Nat al))
  (var pos Nat 0)
  (var ligne Nat 1)
  (loop lex
    (let fini Bool (ge pos total))
    (if fini (then (break lex)))
    (let pc Adr (offset source pos))
    (let c Nat (load8 pc))
    (let blanc Bool (eq c 32))
    (if blanc (then
      (set pos (add pos 1))
      (continue lex)))
    (let saut Bool (eq c 10))
    (if saut (then
      (set pos (add pos 1))
      (set ligne (add ligne 1))
      (continue lex)))
    ; commentaire : jusqu'au saut de ligne, qui est compté par la boucle
    (let commentaire Bool (eq c 59))
    (if commentaire (then
      (let fin Nat (call fin_de_ligne source pos total))
      (set pos fin)
      (continue lex)))
    (let ouvrante Bool (eq c 40))
    (if ouvrante (then
      (call ouvrir z pos ligne)
      (set pos (add pos 1))
      (continue lex)))
    (let fermante Bool (eq c 41))
    (if fermante (then
      (call fermer z pos ligne)
      (set pos (add pos 1))
      (continue lex)))
    ; chaîne : la ligne avance de ses sauts de ligne
    (let guillemet Bool (eq c 34))
    (if guillemet (then
      (let fin Nat (call lire_chaine z pos ligne))
      (let n Nat (sub fin pos))
      (let sauts Nat (call compter_sauts pc n))
      (set ligne (add ligne sauts))
      (set pos fin)
      (continue lex)))
    (let un_mot Bool (call est_mot c))
    (if un_mot (then
      (let fin Nat (call lire_mot z pos ligne))
      (set pos fin)
      (continue lex)))
    (call interdit z pos ligne)
    (end loop lex))
  (let af Adr (call e_ligne_fin z))
  (store Nat af ligne)
  (let ap Adr (call e_profondeur z))
  (let prof Nat (load Nat ap))
  (let ouverte Bool (ne prof 0))
  (if ouverte (then (call faute z 0 ligne source 0)))
  (end fn lire_jetons))
 
; ---------------------------------------------------------------------------
; Valeurs des champs d'un jeton
 
(fn sorte_de (params (z Adr) (i Nat)) (returns Nat)
  (let p Adr (call j_sorte z i))
  (let v Nat (load Nat p))
  (return v)
  (end fn sorte_de))
 
(fn mot_de (params (z Adr) (i Nat)) (returns Nat)
  (let p Adr (call j_mot z i))
  (let v Nat (load Nat p))
  (return v)
  (end fn mot_de))
 
(fn ligne_de (params (z Adr) (i Nat)) (returns Nat)
  (let p Adr (call j_ligne z i))
  (let v Nat (load Nat p))
  (return v)
  (end fn ligne_de))
 
(fn debut_de (params (z Adr) (i Nat)) (returns Nat)
  (let p Adr (call j_debut z i))
  (let v Nat (load Nat p))
  (return v)
  (end fn debut_de))
 
(fn longueur_de (params (z Adr) (i Nat)) (returns Nat)
  (let p Adr (call j_longueur z i))
  (let v Nat (load Nat p))
  (return v)
  (end fn longueur_de))
 
(fn fermante_de (params (z Adr) (i Nat)) (returns Nat)
  (let p Adr (call j_fermante z i))
  (let v Nat (load Nat p))
  (return v)
  (end fn fermante_de))
 
; texte_de : l'adresse du texte du jeton i dans le source
(fn texte_de (params (z Adr) (i Nat)) (returns Adr)
  (let source Adr (call zone_source z))
  (let d Nat (call debut_de z i))
  (let a Adr (offset source d))
  (return a)
  (end fn texte_de))
 
; ---------------------------------------------------------------------------
; Fautes sur un jeton
 
; faute_jeton : la faute cite le jeton i, à sa ligne
(fn faute_jeton (params (z Adr) (code Nat) (i Nat))
  (let ligne Nat (call ligne_de z i))
  (let a Adr (call texte_de z i))
  (let n Nat (call longueur_de z i))
  (call faute z code ligne a n)
  (end fn faute_jeton))
 
; faute_ici : si le jeton i ouvre une forme, la faute porte sur sa tête (et ainsi de suite)
(fn faute_ici (params (z Adr) (code Nat) (i Nat))
  (var k Nat i)
  (loop tetes
    (let s Nat (call sorte_de z k))
    (let ouvre Bool (eq s 1))
    (if ouvre (then) (else (break tetes)))
    (set k (add k 1))
    (end loop tetes))
  (call faute_jeton z code k)
  (end fn faute_ici))
 
; LIMITE (plan sans héritage, phase H1b, décision de papy cyber) : une capacité dépassée se dit par son nom, sa borne
; (le plus grand nombre admis) et la ligne où elle a été franchie : {"code":"LIMITE","ligne":L,"capacite":"…",
; "borne":N}, puis la sortie 3. Avant, toute LIMITE était donnée à la ligne de la fin du fichier, qui n'apprend rien.
(data t_limite_debut "{\"code\":\"LIMITE\",\"ligne\":")
(data t_capacite ",\"capacite\":\"")
(data t_borne "\",\"borne\":")
(data t_cap_texte "texte")
(data t_cap_jetons "jetons")
(data t_cap_parentheses "parentheses_ouvertes")
(data t_cap_noms_tete "noms_de_tete")
(data t_cap_data "texte_d_une_data")
(data t_cap_produit "texte_produit")
(data t_cap_locaux "noms_locaux")
(data t_cap_fonction "octets_d_une_fonction")
(data t_cap_cadre "cadre_d_une_fonction")
(data t_cap_binaire "octets_du_binaire")
(data t_cap_noms_chaine "noms_de_la_chaine")
(data t_cap_renvois "renvois")
(data t_cap_imports "symboles_externes")
 
; limite_a : la capacité cap (son nom, de capn octets), de borne borne, franchie à la ligne ligne
(fn limite_a (params (z Adr) (ligne Nat) (cap Adr) (capn Nat) (borne Nat))
  (let tampon Adr (call zone_faute z))
  (let e Adr (call e_ecriture z))
  (store Adr e tampon)
  (let n0 Nat (size t_limite_debut))
  (call mettre_octets z t_limite_debut n0)
  (call mettre_dec z ligne)
  (let n1 Nat (size t_capacite))
  (call mettre_octets z t_capacite n1)
  (call mettre_octets z cap capn)
  (let n2 Nat (size t_borne))
  (call mettre_octets z t_borne n2)
  (call mettre_dec z borne)
  (let n3 Nat (size t_fin_faute))
  (call mettre_octets z t_fin_faute n3)
  (let fin Adr (load Adr e))
  (let longueur Nat (diff fin tampon))
  (let ecrit Ent (syscall 4 2 tampon longueur))
  (let r Ent (syscall 1 3))
  (end fn limite_a))
 
; ligne_fonction : la ligne de la fonction en cours de traduction, ou celle de la fin du fichier s'il n'y en a pas
(fn ligne_fonction (params (z Adr)) (returns Nat)
  (let pf Adr (call e_fonction z))
  (let g Adr (load Adr pf))
  (let nul Adr (adr_of 0))
  (let sans Bool (eq g nul))
  (if sans (then
    (let af Adr (call e_ligne_fin z))
    (let lf Nat (load Nat af))
    (return lf)))
  (let pj Adr (call g_jeton g))
  (let j Nat (load Nat pj))
  (let lj Nat (call ligne_de z j))
  (return lj)
  (end fn ligne_fonction))
 
; controler_chaine : les limites de la chaîne (le fichier de moins de 0xff0000 octets pour le signeur ; 61 440 noms
; et 524 288 renvois pour l'encodeur), pour le texte produit jusqu'ici ; contrôlées après chaque fonction et chaque
; donnée, elles sont franchies à la ligne ligne, celle de la fonction ou de la donnée qui vient d'être écrite
(fn controler_chaine (params (z Adr) (ligne Nat))
  (let pco Adr (call e_octets_code z))
  (let code Nat (load Nat pco))
  (let pdo Adr (call e_octets_donnees z))
  (let donnees Nat (load Nat pdo))
  (let cd Nat (add code donnees))
  (let t Nat (add cd 0x260))
  (let t_haut Nat (add t 0x3fff))
  (let l Nat (band t_haut 0xffffffffffffc000))
  (let pages Nat (shr l 12))
  (let n Nat (add pages 1))
  (let cases Nat (mul n 32))
  (let s Nat (add cases 0x76))
  (let s_haut Nat (add s 15))
  (let sr Nat (band s_haut 0xfffffffffffffff0))
  (let l40 Nat (add l 0x40))
  (let taille Nat (add l40 sr))
  (let trop_gros Bool (ge taille 0xff0000))
  (if trop_gros (then
    (let nb Nat (size t_cap_binaire))
    (call limite_a z ligne t_cap_binaire nb 0xfeffff)))
  (let pe Adr (call e_etiquettes z))
  (let etiquettes Nat (load Nat pe))
  (let pg Adr (call e_nombre_globaux z))
  (let globaux Nat (load Nat pg))
  (let eg Nat (add etiquettes globaux))
  (let trop_noms Bool (gt eg 0xf000))
  (if trop_noms (then
    (let nn Nat (size t_cap_noms_chaine))
    (call limite_a z ligne t_cap_noms_chaine nn 0xf000)))
  (let pr Adr (call e_renvois z))
  (let renvois Nat (load Nat pr))
  (let trop_renvois Bool (gt renvois 0x80000))
  (if trop_renvois (then
    (let nr Nat (size t_cap_renvois))
    (call limite_a z ligne t_cap_renvois nr 0x80000)))
  (end fn controler_chaine))
 
; Contrôles de forme (une faute ne revient pas). i : jeton attendu ; f : fermante de la forme ; t : sa tête.
; Si la forme se ferme trop tôt, la faute (FORME) porte sur sa tête ; sinon sur le jeton fautif (sa tête si c'est
; une forme).
 
(fn exiger_present (params (z Adr) (i Nat) (f Nat) (t Nat))
  (let manque Bool (eq i f))
  (if manque (then (call faute_jeton z 1 t)))
  (end fn exiger_present))
 
(fn exiger_atome (params (z Adr) (i Nat) (f Nat) (t Nat))
  (call exiger_present z i f t)
  (let s Nat (call sorte_de z i))
  (let forme Bool (eq s 1))
  (if forme (then (call faute_ici z 1 i)))
  (end fn exiger_atome))
 
; exiger_nom : un nom, pas un mot réservé
(fn exiger_nom (params (z Adr) (i Nat) (f Nat) (t Nat))
  (call exiger_present z i f t)
  (let s Nat (call sorte_de z i))
  (let nom Bool (eq s 3))
  (if nom (then) (else (call faute_ici z 1 i)))
  (let m Nat (call mot_de z i))
  (let reserve Bool (ne m 0))
  (if reserve (then (call faute_ici z 1 i)))
  (end fn exiger_nom))
 
; exiger_type : un type ; rend son numéro (1 Ent, 2 Nat, 3 Bool, 4 Adr)
(fn exiger_type (params (z Adr) (i Nat) (f Nat) (t Nat)) (returns Nat)
  (call exiger_present z i f t)
  (let s Nat (call sorte_de z i))
  (let type Bool (eq s 6))
  (if type (then) (else (call faute_ici z 1 i)))
  (let m Nat (call mot_de z i))
  (return m)
  (end fn exiger_type))
 
; exiger_forme : une forme dont la tête est le mot réservé m
(fn exiger_forme (params (z Adr) (i Nat) (f Nat) (t Nat) (m Nat))
  (call exiger_present z i f t)
  (let s Nat (call sorte_de z i))
  (let ouvre Bool (eq s 1))
  (if ouvre (then) (else (call faute_ici z 1 i)))
  (let h Nat (add i 1))
  (let sh Nat (call sorte_de z h))
  (let nom Bool (eq sh 3))
  (if nom (then) (else (call faute_ici z 1 i)))
  (let mh Nat (call mot_de z h))
  (let bon Bool (eq mh m))
  (if bon (then) (else (call faute_ici z 1 i)))
  (end fn exiger_forme))
 
; exiger_fin : rien de plus dans la forme
(fn exiger_fin (params (z Adr) (i Nat) (f Nat))
  (let fini Bool (eq i f))
  (if fini (then) (else (call faute_ici z 1 i)))
  (end fn exiger_fin))
 
; ---------------------------------------------------------------------------
; Noms de tête (fonctions et data). Entrée de 256 octets, champs de 8 octets : +0 jeton du nom, +8 sorte (1 fn,
; 2 data), +16 nombre de paramètres, +24 returns (0 ou 1), +32 type rendu, +40 forme (sa '('), +48 forme params,
; +56 premier élément du corps, +64 1 pour main, +72 jeton de la chaîne (data), +80 nombre d'octets (data),
; +128 types des paramètres (8 × 8), +192 jetons des noms des paramètres (8 × 8). 16 384 entrées au plus.
 
(fn zone_globaux (params (z Adr)) (returns Adr)
  (let p Adr (offset z 0x400000))
  (return p)
  (end fn zone_globaux))
 
(fn zone_decodee (params (z Adr)) (returns Adr)
  (let p Adr (offset z 0x32800000))
  (return p)
  (end fn zone_decodee))
 
(fn e_nombre_globaux (params (z Adr)) (returns Adr)
  (let p Adr (offset z 0x48))
  (return p)
  (end fn e_nombre_globaux))
 
(fn global (params (z Adr) (k Nat)) (returns Adr)
  (let base Adr (call zone_globaux z))
  (let d Nat (mul k 256))
  (let g Adr (offset base d))
  (return g)
  (end fn global))
 
(fn g_jeton (params (g Adr)) (returns Adr)
  (return g)
  (end fn g_jeton))
 
(fn g_sorte (params (g Adr)) (returns Adr)
  (let p Adr (offset g 8))
  (return p)
  (end fn g_sorte))
 
(fn g_nombre_params (params (g Adr)) (returns Adr)
  (let p Adr (offset g 16))
  (return p)
  (end fn g_nombre_params))
 
(fn g_returns (params (g Adr)) (returns Adr)
  (let p Adr (offset g 24))
  (return p)
  (end fn g_returns))
 
(fn g_type_rendu (params (g Adr)) (returns Adr)
  (let p Adr (offset g 32))
  (return p)
  (end fn g_type_rendu))
 
(fn g_forme (params (g Adr)) (returns Adr)
  (let p Adr (offset g 40))
  (return p)
  (end fn g_forme))
 
(fn g_forme_params (params (g Adr)) (returns Adr)
  (let p Adr (offset g 48))
  (return p)
  (end fn g_forme_params))
 
(fn g_corps (params (g Adr)) (returns Adr)
  (let p Adr (offset g 56))
  (return p)
  (end fn g_corps))
 
(fn g_main (params (g Adr)) (returns Adr)
  (let p Adr (offset g 64))
  (return p)
  (end fn g_main))
 
(fn g_chaine (params (g Adr)) (returns Adr)
  (let p Adr (offset g 72))
  (return p)
  (end fn g_chaine))
 
(fn g_octets (params (g Adr)) (returns Adr)
  (let p Adr (offset g 80))
  (return p)
  (end fn g_octets))
 
; g_type_param, g_nom_param : le k-ième paramètre (0 à 7)
(fn g_type_param (params (g Adr) (k Nat)) (returns Adr)
  (let d Nat (mul k 8))
  (let e Nat (add d 128))
  (let p Adr (offset g e))
  (return p)
  (end fn g_type_param))
 
(fn g_nom_param (params (g Adr) (k Nat)) (returns Adr)
  (let d Nat (mul k 8))
  (let e Nat (add d 192))
  (let p Adr (offset g e))
  (return p)
  (end fn g_nom_param))
 
; lire_champ, ranger_champ : la valeur d'un champ dont on a l'adresse
(fn lire_champ (params (p Adr)) (returns Nat)
  (let v Nat (load Nat p))
  (return v)
  (end fn lire_champ))
 
(fn ranger_champ (params (p Adr) (v Nat))
  (store Nat p v)
  (end fn ranger_champ))
 
; memes_noms : les jetons a et b ont le même texte
(fn memes_noms (params (z Adr) (a Nat) (b Nat)) (returns Bool)
  (let na Nat (call longueur_de z a))
  (let nb Nat (call longueur_de z b))
  (let autre Bool (ne na nb))
  (if autre (then (return false)))
  (let ta Adr (call texte_de z a))
  (let tb Adr (call texte_de z b))
  (let meme Bool (call memes_octets ta tb na))
  (return meme)
  (end fn memes_noms))
 
; est_texte : le jeton i a pour texte les n octets de a
(fn est_texte (params (z Adr) (i Nat) (a Adr) (n Nat)) (returns Bool)
  (let ni Nat (call longueur_de z i))
  (let autre Bool (ne ni n))
  (if autre (then (return false)))
  (let ti Adr (call texte_de z i))
  (let meme Bool (call memes_octets ti a n))
  (return meme)
  (end fn est_texte))
 
; chercher_global : l'entrée globale du nom du jeton i, ou l'adresse 0 s'il n'y en a pas
(fn chercher_global (params (z Adr) (i Nat)) (returns Adr)
  (let an Adr (call e_nombre_globaux z))
  (let n Nat (load Nat an))
  (var k Nat 0)
  (loop globaux
    (let fini Bool (eq k n))
    (if fini (then (break globaux)))
    (let g Adr (call global z k))
    (let pj Adr (call g_jeton g))
    (let j Nat (load Nat pj))
    (let meme Bool (call memes_noms z i j))
    (if meme (then (return g)))
    (set k (add k 1))
    (end loop globaux))
  (let nul Adr (adr_of 0))
  (return nul)
  (end fn chercher_global))
 
; nouveau_global : le jeton i nomme une forme de tête ; NOM_DOUBLE s'il existe déjà ; rend sa nouvelle entrée
(fn nouveau_global (params (z Adr) (i Nat)) (returns Adr)
  (let deja Adr (call chercher_global z i))
  (let nul Adr (adr_of 0))
  (let double Bool (ne deja nul))
  (if double (then (call faute_jeton z 3 i)))
  (let an Adr (call e_nombre_globaux z))
  (let n Nat (load Nat an))
  (let plein Bool (ge n 16384))
  (if plein (then
    (let lg Nat (call ligne_de z i))
    (let ng Nat (size t_cap_noms_tete))
    (call limite_a z lg t_cap_noms_tete ng 16384)))
  (let g Adr (call global z n))
  (let n_plus Nat (add n 1))
  (store Nat an n_plus)
  (let pj Adr (call g_jeton g))
  (store Nat pj i)
  (return g)
  (end fn nouveau_global))
 
; decoder_chaine : les octets de la chaîne du jeton i, échappements défaits, écrits à dest ; rend leur nombre
(fn decoder_chaine (params (z Adr) (i Nat) (dest Adr)) (returns Nat)
  (let t Adr (call texte_de z i))
  (let a Adr (offset t 1))
  (let long Nat (call longueur_de z i))
  (let n Nat (sub long 2))
  (var k Nat 0)
  (var ecrits Nat 0)
  (loop octets
    (let fini Bool (ge k n))
    (if fini (then (break octets)))
    (let p Adr (offset a k))
    (var c Nat (load8 p))
    (set k (add k 1))
    (let barre Bool (eq c 92))
    (if barre (then
      (let q Adr (offset a k))
      (set c (load8 q))
      (set k (add k 1))))
    (let d Adr (offset dest ecrits))
    (store8 d c)
    (set ecrits (add ecrits 1))
    (end loop octets))
  (return ecrits)
  (end fn decoder_chaine))
 
; ---------------------------------------------------------------------------
; En-têtes : chaque forme de tête est (data nom "texte") ou (fn nom (params …) [(returns T)] …)
 
; entete_data : (data nom "texte") de i à f ; la chaîne décodée donne la taille (1 Mio au plus)
(fn entete_data (params (z Adr) (i Nat) (f Nat))
  (let t Nat (add i 1))
  (let n Nat (add i 2))
  (call exiger_nom z n f t)
  (let g Adr (call nouveau_global z n))
  (let ps Adr (call g_sorte g))
  (store Nat ps 2)
  (let pf Adr (call g_forme g))
  (store Nat pf i)
  (let c Nat (add i 3))
  (let manque Bool (eq c f))
  (if manque (then (call faute_jeton z 1 t)))
  (let sc Nat (call sorte_de z c))
  (let chaine Bool (eq sc 5))
  (if chaine (then) (else (call faute_ici z 1 c)))
  (let pc Adr (call g_chaine g))
  (store Nat pc c)
  (let long Nat (call longueur_de z c))
  (let trop Bool (gt long 1048576))
  (if trop (then
    (let lc Nat (call ligne_de z c))
    (let nd Nat (size t_cap_data))
    (call limite_a z lc t_cap_data nd 1048576)))
  (let d Nat (add i 4))
  (call exiger_fin z d f)
  (let dest Adr (call zone_decodee z))
  (let octets Nat (call decoder_chaine z c dest))
  (let po Adr (call g_octets g))
  (store Nat po octets)
  (end fn entete_data))
 
; entete_fn : (fn nom (params (p T) …) [(returns T)] …) de i à f ; au plus 8 paramètres
(fn entete_fn (params (z Adr) (i Nat) (f Nat))
  (let t Nat (add i 1))
  (let n Nat (add i 2))
  (call exiger_nom z n f t)
  (let g Adr (call nouveau_global z n))
  (let ps Adr (call g_sorte g))
  (store Nat ps 1)
  (let pf Adr (call g_forme g))
  (store Nat pf i)
  (let fp Nat (add i 3))
  (call exiger_forme z fp f t 3)
  (let pfp Adr (call g_forme_params g))
  (store Nat pfp fp)
  (let fin_params Nat (call fermante_de z fp))
  (let pnb Adr (call g_nombre_params g))
  (var k Nat (add i 5))
  (loop parametres
    (let fini Bool (eq k fin_params))
    (if fini (then (break parametres)))
    (let nb Nat (load Nat pnb))
    (let trop Bool (ge nb 8))
    (if trop (then (call faute_ici z 1 k)))
    (let sk Nat (call sorte_de z k))
    (let ouvre Bool (eq sk 1))
    (if ouvre (then) (else (call faute_ici z 1 k)))
    (let fk Nat (call fermante_de z k))
    (let k1 Nat (add k 1))
    (call exiger_nom z k1 fk k1)
    (let k2 Nat (add k 2))
    (let ty Nat (call exiger_type z k2 fk k1))
    (let pty Adr (call g_type_param g nb))
    (store Nat pty ty)
    (let pno Adr (call g_nom_param g nb))
    (store Nat pno k1)
    (let nb1 Nat (add nb 1))
    (store Nat pnb nb1)
    (let k3 Nat (add k 3))
    (call exiger_fin z k3 fk)
    (set k (add fk 1))
    (end loop parametres))
  (set k (add fin_params 1))
  ; (returns T) facultatif : une forme dont la tête est returns
  (let encore Bool (ne k f))
  (if encore (then
    (let sk Nat (call sorte_de z k))
    (let ouvre Bool (eq sk 1))
    (if ouvre (then
      (let h Nat (add k 1))
      (let sh Nat (call sorte_de z h))
      (let mh Nat (call mot_de z h))
      (let nom Bool (eq sh 3))
      (let mot_returns Bool (eq mh 4))
      (let est_returns Bool (and nom mot_returns))
      (if est_returns (then
        (let fr Nat (call fermante_de z k))
        (let k2 Nat (add k 2))
        (let ty Nat (call exiger_type z k2 fr h))
        (let pr Adr (call g_returns g))
        (store Nat pr 1)
        (let ptr Adr (call g_type_rendu g))
        (store Nat ptr ty)
        (let k3 Nat (add k 3))
        (call exiger_fin z k3 fr)
        (set k (add fr 1))))))))
  (let pc Adr (call g_corps g))
  (store Nat pc k)
  (end fn entete_fn))
 
(fn lire_entetes (params (z Adr))
  (let aj Adr (call e_nombre_jetons z))
  (let total Nat (load Nat aj))
  (var i Nat 0)
  (loop entetes
    (let fini Bool (ge i total))
    (if fini (then (break entetes)))
    (let s Nat (call sorte_de z i))
    (let ouvre Bool (eq s 1))
    (if ouvre (then) (else (call faute_ici z 1 i)))
    (let f Nat (call fermante_de z i))
    (let t Nat (add i 1))
    (let st Nat (call sorte_de z t))
    (let nom Bool (eq st 3))
    (if nom (then) (else (call faute_ici z 1 i)))
    (let m Nat (call mot_de z t))
    (let est_data Bool (eq m 1))
    (if est_data (then
      (call entete_data z i f)
      (set i (add f 1))
      (continue entetes)))
    (let est_fn Bool (eq m 2))
    (if est_fn (then
      (call entete_fn z i f)
      (set i (add f 1))
      (continue entetes)))
    (call faute_ici z 1 i)
    (end loop entetes))
  (end fn lire_entetes))
 
(data t_main "main")
(data t_argc "argc")
(data t_argv "argv")
(data t_envp "envp")
 
; verifier_main : main obligatoire (NOM_INCONNU à la ligne de la fin du fichier) ; une fn, (returns Nat), et
; (params) ou (params (argc Nat) (argv Adr) (envp Adr)) ; sinon FORME sur son nom
(fn verifier_main (params (z Adr))
  (let an Adr (call e_nombre_globaux z))
  (let n Nat (load Nat an))
  (var k Nat 0)
  (loop globaux
    (let fini Bool (eq k n))
    (if fini (then
      (let af Adr (call e_ligne_fin z))
      (let ligne Nat (load Nat af))
      (call faute z 2 ligne t_main 4)))
    (let g Adr (call global z k))
    (let pj Adr (call g_jeton g))
    (let j Nat (load Nat pj))
    (let est_main Bool (call est_texte z j t_main 4))
    (if est_main (then
      (call controler_main z g)
      (return)))
    (set k (add k 1))
    (end loop globaux))
  (end fn verifier_main))
 
(fn controler_main (params (z Adr) (g Adr))
  (let pj Adr (call g_jeton g))
  (let j Nat (load Nat pj))
  (let ps Adr (call g_sorte g))
  (let sorte Nat (load Nat ps))
  (let pas_fn Bool (ne sorte 1))
  (if pas_fn (then (call faute_jeton z 1 j)))
  (let pr Adr (call g_returns g))
  (let rend Nat (load Nat pr))
  (let sans_returns Bool (eq rend 0))
  (if sans_returns (then (call faute_jeton z 1 j)))
  (let ptr Adr (call g_type_rendu g))
  (let tr Nat (load Nat ptr))
  (let pas_nat Bool (ne tr 2))
  (if pas_nat (then (call faute_jeton z 1 j)))
  (let pnb Adr (call g_nombre_params g))
  (let nb Nat (load Nat pnb))
  (let aucun Bool (eq nb 0))
  (if aucun (then
    (call marquer_main g)
    (return)))
  (let pas_trois Bool (ne nb 3))
  (if pas_trois (then (call faute_jeton z 1 j)))
  (let bon_argc Bool (call param_vaut z g 0 t_argc 2))
  (if bon_argc (then) (else (call faute_jeton z 1 j)))
  (let bon_argv Bool (call param_vaut z g 1 t_argv 4))
  (if bon_argv (then) (else (call faute_jeton z 1 j)))
  (let bon_envp Bool (call param_vaut z g 2 t_envp 4))
  (if bon_envp (then) (else (call faute_jeton z 1 j)))
  (call marquer_main g)
  (end fn controler_main))
 
; param_vaut : le k-ième paramètre de g s'appelle comme les 4 octets de nom, et a le type ty
(fn param_vaut (params (z Adr) (g Adr) (k Nat) (nom Adr) (ty Nat)) (returns Bool)
  (let pno Adr (call g_nom_param g k))
  (let j Nat (load Nat pno))
  (let bon_nom Bool (call est_texte z j nom 4))
  (let pty Adr (call g_type_param g k))
  (let t Nat (load Nat pty))
  (let bon_type Bool (eq t ty))
  (let bon Bool (and bon_nom bon_type))
  (return bon)
  (end fn param_vaut))
 
(fn marquer_main (params (g Adr))
  (let pm Adr (call g_main g))
  (store Nat pm 1)
  (end fn marquer_main))
 
; ---------------------------------------------------------------------------
; Texte produit : zones, champs de l'état, émission d'une ligne
 
(fn zone_locaux (params (z Adr)) (returns Adr)
  (let p Adr (offset z 0x100000))
  (return p)
  (end fn zone_locaux))
 
(fn zone_texte (params (z Adr)) (returns Adr)
  (let p Adr (offset z 0x22800000))
  (return p)
  (end fn zone_texte))
 
; la fin permise du texte produit : 256 Mio après son début, comme dans inter.mac
(fn fin_texte (params (z Adr)) (returns Adr)
  (let p Adr (offset z 0x32800000))
  (return p)
  (end fn fin_texte))
 
(fn e_locaux (params (z Adr)) (returns Adr)
  (let p Adr (offset z 0x38))
  (return p)
  (end fn e_locaux))
 
(fn e_octets_code (params (z Adr)) (returns Adr)
  (let p Adr (offset z 0x50))
  (return p)
  (end fn e_octets_code))
 
(fn e_etiquettes (params (z Adr)) (returns Adr)
  (let p Adr (offset z 0x58))
  (return p)
  (end fn e_etiquettes))
 
(fn e_piege_un (params (z Adr)) (returns Adr)
  (let p Adr (offset z 0xf0))
  (return p)
  (end fn e_piege_un))
 
(fn e_piege_deux (params (z Adr)) (returns Adr)
  (let p Adr (offset z 0xf8))
  (return p)
  (end fn e_piege_deux))
 
(fn e_renvois (params (z Adr)) (returns Adr)
  (let p Adr (offset z 0x60))
  (return p)
  (end fn e_renvois))
 
(fn e_fonction (params (z Adr)) (returns Adr)
  (let p Adr (offset z 0x68))
  (return p)
  (end fn e_fonction))
 
(fn e_cases (params (z Adr)) (returns Adr)
  (let p Adr (offset z 0x70))
  (return p)
  (end fn e_cases))
 
(fn e_cadre (params (z Adr)) (returns Adr)
  (let p Adr (offset z 0x78))
  (return p)
  (end fn e_cadre))
 
(fn e_octets_donnees (params (z Adr)) (returns Adr)
  (let p Adr (offset z 0x80))
  (return p)
  (end fn e_octets_donnees))
 
(fn e_nom_fin (params (z Adr)) (returns Adr)
  (let p Adr (offset z 0x88))
  (return p)
  (end fn e_nom_fin))
 
; e_nb_imports : nombre de symboles externes distincts (phase J2) ; 0 = programme sans liaison (en-tête actuel)
(fn e_nb_imports (params (z Adr)) (returns Adr)
  (let p Adr (offset z 0xa0))
  (return p)
  (end fn e_nb_imports))
 
; zone_imports : table des symboles externes (z+0x10000, après les cases d'empreinte, avant les noms locaux).
; Une entrée de 64 octets par symbole : +0 longueur du nom, +8 octets du nom (56 au plus).
(fn zone_imports (params (z Adr)) (returns Adr)
  (let p Adr (offset z 0x10000))
  (return p)
  (end fn zone_imports))
 
; ajouter_import : l'index (à partir de 0) du symbole de n octets en a ; l'ajoute s'il est neuf. Une seule dylib
; (libSystem) dans cette version : l'index est aussi l'offset / 8 dans le __got. 4 096 imports au plus (LIMITE).
(fn ajouter_import (params (z Adr) (a Adr) (n Nat)) (returns Nat)
  (let pnb Adr (call e_nb_imports z))
  (let nb Nat (load Nat pnb))
  (let base Adr (call zone_imports z))
  (var k Nat 0)
  (loop cherche
    (let fini Bool (eq k nb))
    (if fini (then (break cherche)))
    (let d Nat (mul k 64))
    (let e Adr (offset base d))
    (let ln Nat (load Nat e))
    (let meme_long Bool (eq ln n))
    (if meme_long (then
      (let noms Adr (offset e 8))
      (var i Nat 0)
      (var egal Bool true)
      (loop octets
        (let bout Bool (eq i n))
        (if bout (then (break octets)))
        (let pa Adr (offset a i))
        (let pe Adr (offset noms i))
        (let ca Nat (load8 pa))
        (let ce Nat (load8 pe))
        (let differe Bool (ne ca ce))
        (if differe (then (set egal false) (break octets)))
        (set i (add i 1))
        (end loop octets))
      (if egal (then (return k)))))
    (set k (add k 1))
    (end loop cherche))
  (let plein Bool (ge nb 4096))
  (if plein (then
    (let af Adr (call e_ligne_fin z))
    (let lf Nat (load Nat af))
    (let nt Nat (size t_cap_imports))
    (call limite_a z lf t_cap_imports nt 4096)))
  (let dd Nat (mul nb 64))
  (let ee Adr (offset base dd))
  (store Nat ee n)
  (let noms_a Adr (offset ee 8))
  (var w Nat 0)
  (loop copie
    (let bout_a Bool (eq w n))
    (if bout_a (then (break copie)))
    (let pa_a Adr (offset a w))
    (let c Nat (load8 pa_a))
    (let pe_a Adr (offset noms_a w))
    (store8 pe_a c)
    (set w (add w 1))
    (end loop copie))
  (let apres_nb Nat (add nb 1))
  (store Nat pnb apres_nb)
  (return nb)
  (end fn ajouter_import))
 
; ajouter : le champ d'adresse p augmente de v
(fn ajouter (params (p Adr) (v Nat))
  (let x Nat (load Nat p))
  (let y Nat (add x v))
  (store Nat p y)
  (end fn ajouter))
 
; mettre_hex : v en hexadécimal minuscule, sans préfixe ni zéro de tête (0 s'écrit 0)
(fn mettre_hex (params (z Adr) (v Nat))
  (var s Nat 60)
  (loop zeros
    (let bas Bool (eq s 0))
    (if bas (then (break zeros)))
    (let x Nat (shr v s))
    (let d Nat (band x 15))
    (let chiffre Bool (ne d 0))
    (if chiffre (then (break zeros)))
    (set s (sub s 4))
    (end loop zeros))
  (loop chiffres
    (let x Nat (shr v s))
    (let d Nat (band x 15))
    (let c Nat (call chiffre_hex d))
    (call mettre z c)
    (let dernier Bool (eq s 0))
    (if dernier (then (break chiffres)))
    (set s (sub s 4))
    (end loop chiffres))
  (end fn mettre_hex))
 
; mettre_jeton : le texte du jeton i
(fn mettre_jeton (params (z Adr) (i Nat))
  (let a Adr (call texte_de z i))
  (let n Nat (call longueur_de z i))
  (call mettre_octets z a n)
  (end fn mettre_jeton))
 
; emettre : une ligne du texte produit, d'après le format f de n octets, et ses trous remplis par a, b, c dans
; l'ordre : $r décimal, $h 0x hexadécimal, $e étiquette e.N, $n texte d'un jeton. Une ligne qui commence par une
; minuscule est une instruction : 4 octets de code de plus. Chaque '@' ou '%' compte un renvoi. Le texte produit
; ne dépasse pas sa fin permise (LIMITE).
(fn emettre (params (z Adr) (f Adr) (n Nat) (a Nat) (b Nat) (c Nat))
  (let c0 Nat (load8 f))
  (let est_instruction Bool (call est_minuscule c0))
  (if est_instruction (then
    (let po Adr (call e_octets_code z))
    (call ajouter po 4)))
  (var k Nat 0)
  (var trou Nat 0)
  (loop octets
    (let fini Bool (eq k n))
    (if fini (then (break octets)))
    (let p Adr (offset f k))
    (let ch Nat (load8 p))
    (set k (add k 1))
    (let dollar Bool (eq ch 36))
    (if dollar (then
      (let q Adr (offset f k))
      (let genre Nat (load8 q))
      (set k (add k 1))
      (var v Nat a)
      (let deuxieme Bool (eq trou 1))
      (if deuxieme (then (set v b)))
      (let troisieme Bool (eq trou 2))
      (if troisieme (then (set v c)))
      (set trou (add trou 1))
      (let decimal Bool (eq genre 114))
      (if decimal (then
        (call mettre_dec z v)
        (continue octets)))
      (let hex Bool (eq genre 104))
      (if hex (then
        (call mettre z 48)
        (call mettre z 120)
        (call mettre_hex z v)
        (continue octets)))
      (let etiquette Bool (eq genre 101))
      (if etiquette (then
        (call mettre z 101)
        (call mettre z 46)
        (call mettre_hex z v)
        (continue octets)))
      (call mettre_jeton z v)
      (continue octets)))
    (let arobase Bool (eq ch 64))
    (let pourcent Bool (eq ch 37))
    (let renvoi Bool (or arobase pourcent))
    (if renvoi (then
      (let pr Adr (call e_renvois z))
      (call ajouter pr 1)))
    (call mettre z ch)
    (end loop octets))
  (call mettre z 10)
  (let e Adr (call e_ecriture z))
  (let ici Adr (load Adr e))
  (let fin Adr (call fin_texte z))
  (let plein Bool (ge ici fin))
  (if plein (then
    (let lf Nat (call ligne_fonction z))
    (let nt Nat (size t_cap_produit))
    (call limite_a z lf t_cap_produit nt 0xfffffff)))
  (end fn emettre))
 
; ---------------------------------------------------------------------------
; Les lignes produites, une fonction par format (les trous en paramètres)
 
(data f_fn ":f.$n")
(fn e_fn (params (z Adr) (j Nat))
  (let n Nat (size f_fn))
  (call emettre z f_fn n j 0 0)
  (end fn e_fn))
 
(data f_etiquette ":$e")
(fn e_etiquette (params (z Adr) (e Nat))
  (let n Nat (size f_etiquette))
  (call emettre z f_etiquette n e 0 0)
  (end fn e_etiquette))
 
(data f_data ":d.$n")
(fn e_data (params (z Adr) (j Nat))
  (let n Nat (size f_data))
  (call emettre z f_data n j 0 0)
  (end fn e_data))
 
(data f_sub_sp_haut "sub_sp,sp,#$h,lsl_#12")
(fn e_sub_sp_haut (params (z Adr) (h Nat))
  (let n Nat (size f_sub_sp_haut))
  (call emettre z f_sub_sp_haut n h 0 0)
  (end fn e_sub_sp_haut))
 
(data f_sub_sp "sub_sp,sp,#$h")
(fn e_sub_sp (params (z Adr) (h Nat))
  (let n Nat (size f_sub_sp))
  (call emettre z f_sub_sp n h 0 0)
  (end fn e_sub_sp))
 
(data f_add_sp_haut "add_sp,sp,#$h,lsl_#12")
(fn e_add_sp_haut (params (z Adr) (h Nat))
  (let n Nat (size f_add_sp_haut))
  (call emettre z f_add_sp_haut n h 0 0)
  (end fn e_add_sp_haut))
 
(data f_add_sp "add_sp,sp,#$h")
(fn e_add_sp (params (z Adr) (h Nat))
  (let n Nat (size f_add_sp))
  (call emettre z f_add_sp n h 0 0)
  (end fn e_add_sp))
 
(data f_str_case "str_x$r,[sp,#$h]")
(fn e_str_case (params (z Adr) (r Nat) (h Nat))
  (let n Nat (size f_str_case))
  (call emettre z f_str_case n r h 0)
  (end fn e_str_case))
 
(data f_ldr_case "ldr_x$r,[sp,#$h]")
(fn e_ldr_case (params (z Adr) (r Nat) (h Nat))
  (let n Nat (size f_ldr_case))
  (call emettre z f_ldr_case n r h 0)
  (end fn e_ldr_case))
 
; une donnée : sa page par adrp (portée ±4 Gio), puis ses 12 bits bas par add
(data f_adr_data "adrp_x$r %d.$n")
(data f_add_data "add_x$r,x$r,#0x0 %d.$n")
(fn e_adr_data (params (z Adr) (r Nat) (j Nat))
  (let n Nat (size f_adr_data))
  (call emettre z f_adr_data n r j 0)
  (let m Nat (size f_add_data))
  (call emettre z f_add_data m r r j)
  (end fn e_adr_data))
 
; appel externe (phase J2) : l'entrée du GOT du symbole d'index idx par adrp + add (comme une donnée), puis le
; pointeur chargé et appelé. L'étiquette :g.idx est posée dans le __got par la mise en page liée.
(data f_adr_got "adrp_x$r %g.$r")
(data f_add_got "add_x$r,x$r,#0x0 %g.$r")
(data f_ldr_indirect "ldr_x$r,[x$r]")
(data f_blr "blr_x$r")
(data f_etiq_got ":g.$r")
(fn e_adr_got (params (z Adr) (r Nat) (idx Nat))
  (let n Nat (size f_adr_got))
  (call emettre z f_adr_got n r idx 0)
  (let m Nat (size f_add_got))
  (call emettre z f_add_got m r r idx)
  (end fn e_adr_got))
(fn e_ldr_indirect (params (z Adr) (r Nat))
  (let n Nat (size f_ldr_indirect))
  (call emettre z f_ldr_indirect n r r 0)
  (end fn e_ldr_indirect))
(fn e_blr (params (z Adr) (r Nat))
  (let n Nat (size f_blr))
  (call emettre z f_blr n r 0 0)
  (end fn e_blr))
(fn e_etiq_got (params (z Adr) (idx Nat))
  (let n Nat (size f_etiq_got))
  (call emettre z f_etiq_got n idx 0 0)
  (end fn e_etiq_got))
 
(data f_mov "mov_x$r,#$h")
(fn e_mov (params (z Adr) (r Nat) (h Nat))
  (let n Nat (size f_mov))
  (call emettre z f_mov n r h 0)
  (end fn e_mov))
 
(data f_movk "movk_x$r,#$h,lsl_#$r")
(fn e_movk (params (z Adr) (r Nat) (h Nat) (s Nat))
  (let n Nat (size f_movk))
  (call emettre z f_movk n r h s)
  (end fn e_movk))
 
(data f_cbz "cbz_x0 @$e")
(fn e_cbz (params (z Adr) (e Nat))
  (let n Nat (size f_cbz))
  (call emettre z f_cbz n e 0 0)
  (end fn e_cbz))
 
(data f_b "b_ @$e")
(fn e_b (params (z Adr) (e Nat))
  (let n Nat (size f_b))
  (call emettre z f_b n e 0 0)
  (end fn e_b))
 
(data f_bl "bl @f.$n")
(fn e_bl (params (z Adr) (j Nat))
  (let n Nat (size f_bl))
  (call emettre z f_bl n j 0 0)
  (end fn e_bl))
 
(data f_mov16 "mov_x16,#$h")
(fn e_mov16 (params (z Adr) (h Nat))
  (let n Nat (size f_mov16))
  (call emettre z f_mov16 n h 0 0)
  (end fn e_mov16))
 
; lignes sans trou
(data f_b_main "b_ @f.main")
(fn e_b_main (params (z Adr)) (let n Nat (size f_b_main)) (call emettre z f_b_main n 0 0 0) (end fn e_b_main))
(data f_stp "stp_x29,x30,[sp]")
(fn e_stp (params (z Adr)) (let n Nat (size f_stp)) (call emettre z f_stp n 0 0 0) (end fn e_stp))
(data f_ldp "ldp_x29,x30,[sp]")
(fn e_ldp (params (z Adr)) (let n Nat (size f_ldp)) (call emettre z f_ldp n 0 0 0) (end fn e_ldp))
(data f_ret "ret")
(fn e_ret (params (z Adr)) (let n Nat (size f_ret)) (call emettre z f_ret n 0 0 0) (end fn e_ret))
(data f_cmp_ff "cmp_x0,#0xff")
(fn e_cmp_ff (params (z Adr)) (let n Nat (size f_cmp_ff)) (call emettre z f_cmp_ff n 0 0 0) (end fn e_cmp_ff))
(data f_bhi_piege "b.hi @$e")
(fn e_bhi_piege (params (z Adr)) (let n Nat (size f_bhi_piege)) (let pe Adr (call e_piege_un z)) (let e Nat (load Nat pe)) (call emettre z f_bhi_piege n e 0 0) (end fn e_bhi_piege))
(data f_str_x1 "str_x0,[x1]")
(fn e_str_x1 (params (z Adr)) (let n Nat (size f_str_x1)) (call emettre z f_str_x1 n 0 0 0) (end fn e_str_x1))
(data f_strb_x1 "strb_w0,[x1]")
(fn e_strb_x1 (params (z Adr)) (let n Nat (size f_strb_x1)) (call emettre z f_strb_x1 n 0 0 0) (end fn e_strb_x1))
(data f_adds "adds_x0,x0,x1")
(fn e_adds (params (z Adr)) (let n Nat (size f_adds)) (call emettre z f_adds n 0 0 0) (end fn e_adds))
(data f_bvs_piege "b.vs @$e")
(fn e_bvs_piege (params (z Adr)) (let n Nat (size f_bvs_piege)) (let pe Adr (call e_piege_un z)) (let e Nat (load Nat pe)) (call emettre z f_bvs_piege n e 0 0) (end fn e_bvs_piege))
(data f_bhs_piege "b.hs @$e")
(fn e_bhs_piege (params (z Adr)) (let n Nat (size f_bhs_piege)) (let pe Adr (call e_piege_un z)) (let e Nat (load Nat pe)) (call emettre z f_bhs_piege n e 0 0) (end fn e_bhs_piege))
(data f_subs "subs_x0,x0,x1")
(fn e_subs (params (z Adr)) (let n Nat (size f_subs)) (call emettre z f_subs n 0 0 0) (end fn e_subs))
(data f_blo_piege "b.lo @$e")
(fn e_blo_piege (params (z Adr)) (let n Nat (size f_blo_piege)) (let pe Adr (call e_piege_un z)) (let e Nat (load Nat pe)) (call emettre z f_blo_piege n e 0 0) (end fn e_blo_piege))
(data f_mul "mul_x2,x0,x1")
(fn e_mul (params (z Adr)) (let n Nat (size f_mul)) (call emettre z f_mul n 0 0 0) (end fn e_mul))
(data f_smulh "smulh_x3,x0,x1")
(fn e_smulh (params (z Adr)) (let n Nat (size f_smulh)) (call emettre z f_smulh n 0 0 0) (end fn e_smulh))
(data f_cmp_asr "cmp_x3,x2,asr_#63")
(fn e_cmp_asr (params (z Adr)) (let n Nat (size f_cmp_asr)) (call emettre z f_cmp_asr n 0 0 0) (end fn e_cmp_asr))
(data f_bne_piege "b.ne @$e")
(fn e_bne_piege (params (z Adr)) (let n Nat (size f_bne_piege)) (let pe Adr (call e_piege_un z)) (let e Nat (load Nat pe)) (call emettre z f_bne_piege n e 0 0) (end fn e_bne_piege))
(data f_umulh "umulh_x3,x0,x1")
(fn e_umulh (params (z Adr)) (let n Nat (size f_umulh)) (call emettre z f_umulh n 0 0 0) (end fn e_umulh))
(data f_cbnz3_piege "cbnz_x3 @$e")
(fn e_cbnz3_piege (params (z Adr)) (let n Nat (size f_cbnz3_piege)) (let pe Adr (call e_piege_un z)) (let e Nat (load Nat pe)) (call emettre z f_cbnz3_piege n e 0 0) (end fn e_cbnz3_piege))
(data f_mov_x0_x2 "mov_x0,x2")
(fn e_mov_x0_x2 (params (z Adr)) (let n Nat (size f_mov_x0_x2)) (call emettre z f_mov_x0_x2 n 0 0 0) (end fn e_mov_x0_x2))
(data f_cbz1_piege2 "cbz_x1 @$e")
(fn e_cbz1_piege2 (params (z Adr)) (let n Nat (size f_cbz1_piege2)) (let pe Adr (call e_piege_deux z)) (let e Nat (load Nat pe)) (call emettre z f_cbz1_piege2 n e 0 0) (end fn e_cbz1_piege2))
(data f_negs_x2 "negs_x2,x0")
(fn e_negs_x2 (params (z Adr)) (let n Nat (size f_negs_x2)) (call emettre z f_negs_x2 n 0 0 0) (end fn e_negs_x2))
(data f_cset_x2_vs "cset_x2,vs")
(fn e_cset_x2_vs (params (z Adr)) (let n Nat (size f_cset_x2_vs)) (call emettre z f_cset_x2_vs n 0 0 0) (end fn e_cset_x2_vs))
(data f_cmn "cmn_x1,#0x1")
(fn e_cmn (params (z Adr)) (let n Nat (size f_cmn)) (call emettre z f_cmn n 0 0 0) (end fn e_cmn))
(data f_cset_x3_eq "cset_x3,eq")
(fn e_cset_x3_eq (params (z Adr)) (let n Nat (size f_cset_x3_eq)) (call emettre z f_cset_x3_eq n 0 0 0) (end fn e_cset_x3_eq))
(data f_and_x2 "and_x2,x2,x3")
(fn e_and_x2 (params (z Adr)) (let n Nat (size f_and_x2)) (call emettre z f_and_x2 n 0 0 0) (end fn e_and_x2))
(data f_cbnz2_piege "cbnz_x2 @$e")
(fn e_cbnz2_piege (params (z Adr)) (let n Nat (size f_cbnz2_piege)) (let pe Adr (call e_piege_un z)) (let e Nat (load Nat pe)) (call emettre z f_cbnz2_piege n e 0 0) (end fn e_cbnz2_piege))
(data f_sdiv0 "sdiv_x0,x0,x1")
(fn e_sdiv0 (params (z Adr)) (let n Nat (size f_sdiv0)) (call emettre z f_sdiv0 n 0 0 0) (end fn e_sdiv0))
(data f_udiv0 "udiv_x0,x0,x1")
(fn e_udiv0 (params (z Adr)) (let n Nat (size f_udiv0)) (call emettre z f_udiv0 n 0 0 0) (end fn e_udiv0))
(data f_sdiv2 "sdiv_x2,x0,x1")
(fn e_sdiv2 (params (z Adr)) (let n Nat (size f_sdiv2)) (call emettre z f_sdiv2 n 0 0 0) (end fn e_sdiv2))
(data f_udiv2 "udiv_x2,x0,x1")
(fn e_udiv2 (params (z Adr)) (let n Nat (size f_udiv2)) (call emettre z f_udiv2 n 0 0 0) (end fn e_udiv2))
(data f_msub "msub_x0,x2,x1,x0")
(fn e_msub (params (z Adr)) (let n Nat (size f_msub)) (call emettre z f_msub n 0 0 0) (end fn e_msub))
(data f_negs_x0 "negs_x0,x0")
(fn e_negs_x0 (params (z Adr)) (let n Nat (size f_negs_x0)) (call emettre z f_negs_x0 n 0 0 0) (end fn e_negs_x0))
(data f_cmp_x0_x1 "cmp_x0,x1")
(fn e_cmp_x0_x1 (params (z Adr)) (let n Nat (size f_cmp_x0_x1)) (call emettre z f_cmp_x0_x1 n 0 0 0) (end fn e_cmp_x0_x1))
(data f_cset_eq "cset_x0,eq")
(fn e_cset_eq (params (z Adr)) (let n Nat (size f_cset_eq)) (call emettre z f_cset_eq n 0 0 0) (end fn e_cset_eq))
(data f_cset_ne "cset_x0,ne")
(fn e_cset_ne (params (z Adr)) (let n Nat (size f_cset_ne)) (call emettre z f_cset_ne n 0 0 0) (end fn e_cset_ne))
(data f_cset_lt "cset_x0,lt")
(fn e_cset_lt (params (z Adr)) (let n Nat (size f_cset_lt)) (call emettre z f_cset_lt n 0 0 0) (end fn e_cset_lt))
(data f_cset_le "cset_x0,le")
(fn e_cset_le (params (z Adr)) (let n Nat (size f_cset_le)) (call emettre z f_cset_le n 0 0 0) (end fn e_cset_le))
(data f_cset_gt "cset_x0,gt")
(fn e_cset_gt (params (z Adr)) (let n Nat (size f_cset_gt)) (call emettre z f_cset_gt n 0 0 0) (end fn e_cset_gt))
(data f_cset_ge "cset_x0,ge")
(fn e_cset_ge (params (z Adr)) (let n Nat (size f_cset_ge)) (call emettre z f_cset_ge n 0 0 0) (end fn e_cset_ge))
(data f_cset_lo "cset_x0,lo")
(fn e_cset_lo (params (z Adr)) (let n Nat (size f_cset_lo)) (call emettre z f_cset_lo n 0 0 0) (end fn e_cset_lo))
(data f_cset_ls "cset_x0,ls")
(fn e_cset_ls (params (z Adr)) (let n Nat (size f_cset_ls)) (call emettre z f_cset_ls n 0 0 0) (end fn e_cset_ls))
(data f_cset_hi "cset_x0,hi")
(fn e_cset_hi (params (z Adr)) (let n Nat (size f_cset_hi)) (call emettre z f_cset_hi n 0 0 0) (end fn e_cset_hi))
(data f_cset_hs "cset_x0,hs")
(fn e_cset_hs (params (z Adr)) (let n Nat (size f_cset_hs)) (call emettre z f_cset_hs n 0 0 0) (end fn e_cset_hs))
(data f_and "and_x0,x0,x1")
(fn e_and (params (z Adr)) (let n Nat (size f_and)) (call emettre z f_and n 0 0 0) (end fn e_and))
(data f_orr "orr_x0,x0,x1")
(fn e_orr (params (z Adr)) (let n Nat (size f_orr)) (call emettre z f_orr n 0 0 0) (end fn e_orr))
(data f_eor_un "eor_x0,x0,#0x1")
(fn e_eor_un (params (z Adr)) (let n Nat (size f_eor_un)) (call emettre z f_eor_un n 0 0 0) (end fn e_eor_un))
(data f_eor "eor_x0,x0,x1")
(fn e_eor (params (z Adr)) (let n Nat (size f_eor)) (call emettre z f_eor n 0 0 0) (end fn e_eor))
(data f_mvn "mvn_x0,x0")
(fn e_mvn (params (z Adr)) (let n Nat (size f_mvn)) (call emettre z f_mvn n 0 0 0) (end fn e_mvn))
(data f_cmp_40 "cmp_x1,#0x40")
(fn e_cmp_40 (params (z Adr)) (let n Nat (size f_cmp_40)) (call emettre z f_cmp_40 n 0 0 0) (end fn e_cmp_40))
(data f_lsl "lsl_x0,x0,x1")
(fn e_lsl (params (z Adr)) (let n Nat (size f_lsl)) (call emettre z f_lsl n 0 0 0) (end fn e_lsl))
(data f_lsr "lsr_x0,x0,x1")
(fn e_lsr (params (z Adr)) (let n Nat (size f_lsr)) (call emettre z f_lsr n 0 0 0) (end fn e_lsr))
(data f_cmp_0 "cmp_x0,#0x0")
(fn e_cmp_0 (params (z Adr)) (let n Nat (size f_cmp_0)) (call emettre z f_cmp_0 n 0 0 0) (end fn e_cmp_0))
(data f_blt_piege "b.lt @$e")
(fn e_blt_piege (params (z Adr)) (let n Nat (size f_blt_piege)) (let pe Adr (call e_piege_un z)) (let e Nat (load Nat pe)) (call emettre z f_blt_piege n e 0 0) (end fn e_blt_piege))
(data f_ldr_x0 "ldr_x0,[x0]")
(fn e_ldr_x0 (params (z Adr)) (let n Nat (size f_ldr_x0)) (call emettre z f_ldr_x0 n 0 0 0) (end fn e_ldr_x0))
(data f_ldrb_x0 "ldrb_w0,[x0]")
(fn e_ldrb_x0 (params (z Adr)) (let n Nat (size f_ldrb_x0)) (call emettre z f_ldrb_x0 n 0 0 0) (end fn e_ldrb_x0))
(data f_svc "svc_#0x80")
(fn e_svc (params (z Adr)) (let n Nat (size f_svc)) (call emettre z f_svc n 0 0 0) (end fn e_svc))
(data f_cset_x1_hs "cset_x1,hs")
(fn e_cset_x1_hs (params (z Adr)) (let n Nat (size f_cset_x1_hs)) (call emettre z f_cset_x1_hs n 0 0 0) (end fn e_cset_x1_hs))
(data f_neg_x1 "neg_x1,x1")
(fn e_neg_x1 (params (z Adr)) (let n Nat (size f_neg_x1)) (call emettre z f_neg_x1 n 0 0 0) (end fn e_neg_x1))
(data f_eor_x2 "eor_x2,x0,x1")
(fn e_eor_x2 (params (z Adr)) (let n Nat (size f_eor_x2)) (call emettre z f_eor_x2 n 0 0 0) (end fn e_eor_x2))
(data f_sub_x0 "sub_x0,x2,x1")
(fn e_sub_x0 (params (z Adr)) (let n Nat (size f_sub_x0)) (call emettre z f_sub_x0 n 0 0 0) (end fn e_sub_x0))
(data f_brk1 "brk_#0x1")
(fn e_brk1 (params (z Adr)) (let n Nat (size f_brk1)) (call emettre z f_brk1 n 0 0 0) (end fn e_brk1))
(data f_brk2 "brk_#0x2")
(fn e_brk2 (params (z Adr)) (let n Nat (size f_brk2)) (call emettre z f_brk2 n 0 0 0) (end fn e_brk2))
 
; ---------------------------------------------------------------------------
; Noms locaux (paramètres, let, var, boucles). Entrée de 64 octets, champs de 8 octets : +0 jeton, +8 sorte
; (1 paramètre, 2 let, 3 var, 4 boucle), +16 type, +24 break vu (boucle), +32 case (ou étiquette de début de
; boucle), +40 étiquette de fin de boucle. 8 192 visibles à la fois au plus.
 
(fn local (params (z Adr) (k Nat)) (returns Adr)
  (let base Adr (call zone_locaux z))
  (let d Nat (mul k 64))
  (let l Adr (offset base d))
  (return l)
  (end fn local))
 
(fn l_jeton (params (l Adr)) (returns Adr)
  (return l)
  (end fn l_jeton))
 
(fn l_sorte (params (l Adr)) (returns Adr)
  (let p Adr (offset l 8))
  (return p)
  (end fn l_sorte))
 
(fn l_type (params (l Adr)) (returns Adr)
  (let p Adr (offset l 16))
  (return p)
  (end fn l_type))
 
(fn l_break (params (l Adr)) (returns Adr)
  (let p Adr (offset l 24))
  (return p)
  (end fn l_break))
 
(fn l_case (params (l Adr)) (returns Adr)
  (let p Adr (offset l 32))
  (return p)
  (end fn l_case))
 
(fn l_fin (params (l Adr)) (returns Adr)
  (let p Adr (offset l 40))
  (return p)
  (end fn l_fin))
 
; ajouter_local : un nom local de plus ; rend son entrée
(fn ajouter_local (params (z Adr) (j Nat) (sorte Nat) (type Nat) (place Nat) (fin Nat)) (returns Adr)
  (let an Adr (call e_locaux z))
  (let n Nat (load Nat an))
  (let plein Bool (ge n 8192))
  (if plein (then
    (let lj Nat (call ligne_de z j))
    (let nl Nat (size t_cap_locaux))
    (call limite_a z lj t_cap_locaux nl 8192)))
  (let l Adr (call local z n))
  (let pj Adr (call l_jeton l))
  (store Nat pj j)
  (let ps Adr (call l_sorte l))
  (store Nat ps sorte)
  (let pt Adr (call l_type l))
  (store Nat pt type)
  (let pb Adr (call l_break l))
  (store Nat pb 0)
  (let pc Adr (call l_case l))
  (store Nat pc place)
  (let pf Adr (call l_fin l))
  (store Nat pf fin)
  (let n_plus Nat (add n 1))
  (store Nat an n_plus)
  (return l)
  (end fn ajouter_local))
 
; chercher_local : l'entrée locale visible du nom du jeton i, la plus récente ; l'adresse 0 s'il n'y en a pas
(fn chercher_local (params (z Adr) (i Nat)) (returns Adr)
  (let an Adr (call e_locaux z))
  (var k Nat (load Nat an))
  (loop locaux
    (let fini Bool (eq k 0))
    (if fini (then (break locaux)))
    (set k (sub k 1))
    (let l Adr (call local z k))
    (let pj Adr (call l_jeton l))
    (let j Nat (load Nat pj))
    (let meme Bool (call memes_noms z i j))
    (if meme (then (return l)))
    (end loop locaux))
  (let nul Adr (adr_of 0))
  (return nul)
  (end fn chercher_local))
 
; exiger_libre : le jeton i nomme un nom à déclarer ; NOM_DOUBLE si un nom visible (local ou de tête) a son texte
(fn exiger_libre (params (z Adr) (i Nat))
  (let nul Adr (adr_of 0))
  (let l Adr (call chercher_local z i))
  (let local_vu Bool (ne l nul))
  (if local_vu (then (call faute_jeton z 3 i)))
  (let g Adr (call chercher_global z i))
  (let global_vu Bool (ne g nul))
  (if global_vu (then (call faute_jeton z 3 i)))
  (end fn exiger_libre))
 
; fonction_appelee : le jeton i nomme la fonction appelée ; rend son entrée (NOM_INCONNU ; FORME si ce n'est pas
; une fn)
(fn fonction_appelee (params (z Adr) (i Nat)) (returns Adr)
  (let nul Adr (adr_of 0))
  (let l Adr (call chercher_local z i))
  (let local_vu Bool (ne l nul))
  (if local_vu (then (call faute_jeton z 1 i)))
  (let g Adr (call chercher_global z i))
  (let absent Bool (eq g nul))
  (if absent (then (call faute_jeton z 2 i)))
  (let ps Adr (call g_sorte g))
  (let sorte Nat (load Nat ps))
  (let pas_fn Bool (ne sorte 1))
  (if pas_fn (then (call faute_jeton z 1 i)))
  (return g)
  (end fn fonction_appelee))
 
; suivant : l'élément qui suit l'élément i (une forme se saute en entier)
(fn suivant (params (z Adr) (i Nat)) (returns Nat)
  (let s Nat (call sorte_de z i))
  (let ouvre Bool (eq s 1))
  (if ouvre (then
    (let f Nat (call fermante_de z i))
    (let apres Nat (add f 1))
    (return apres)))
  (let apres Nat (add i 1))
  (return apres)
  (end fn suivant))
 
(fn nouvelle_case (params (z Adr)) (returns Nat)
  (let pc Adr (call e_cases z))
  (let c Nat (load Nat pc))
  (let c1 Nat (add c 1))
  (store Nat pc c1)
  (return c)
  (end fn nouvelle_case))
 
(fn nouvelle_etiquette (params (z Adr)) (returns Nat)
  (let pe Adr (call e_etiquettes z))
  (let e Nat (load Nat pe))
  (let e1 Nat (add e 1))
  (store Nat pe e1)
  (return e1)
  (end fn nouvelle_etiquette))
 
; decalage_case : la place de la case c dans le cadre, 16 + 8 × c
(fn decalage_case (params (c Nat)) (returns Nat)
  (let d Nat (mul c 8))
  (let h Nat (add d 16))
  (return h)
  (end fn decalage_case))
 
; compter_cases : le nombre de (let …) et (var …) dans la forme de la fonction (sa '(' : forme)
(fn compter_cases (params (z Adr) (forme Nat)) (returns Nat)
  (let f Nat (call fermante_de z forme))
  (var k Nat forme)
  (var n Nat 0)
  (loop jetons
    (let fini Bool (ge k f))
    (if fini (then (break jetons)))
    (let s Nat (call sorte_de z k))
    (set k (add k 1))
    (let ouvre Bool (eq s 1))
    (if ouvre (then) (else (continue jetons)))
    (let sh Nat (call sorte_de z k))
    (let nom Bool (eq sh 3))
    (if nom (then) (else (continue jetons)))
    (let m Nat (call mot_de z k))
    (let est_let Bool (eq m 6))
    (let est_var Bool (eq m 7))
    (let liaison Bool (or est_let est_var))
    (if liaison (then (set n (add n 1))))
    (end loop jetons))
  (return n)
  (end fn compter_cases))
 
; ---------------------------------------------------------------------------
; Atomes
 
; type_atome : le type de l'atome i, 0 pour un entier littéral (son type viendra de sa place) ; FORME pour une
; fonction ou une étiquette de boucle employée comme atome
(fn type_atome (params (z Adr) (i Nat)) (returns Nat)
  (let s Nat (call sorte_de z i))
  (let entier Bool (eq s 4))
  (if entier (then (return 0)))
  (let nom Bool (eq s 3))
  (if nom (then) (else (call faute_ici z 1 i)))
  (let m Nat (call mot_de z i))
  (let vrai Bool (eq m 50))
  (let faux Bool (eq m 51))
  (let booleen Bool (or vrai faux))
  (if booleen (then (return 3)))
  (let reserve Bool (ne m 0))
  (if reserve (then (call faute_ici z 1 i)))
  (let nul Adr (adr_of 0))
  (let l Adr (call chercher_local z i))
  (let local_vu Bool (ne l nul))
  (if local_vu (then
    (let ps Adr (call l_sorte l))
    (let sorte Nat (load Nat ps))
    (let boucle Bool (eq sorte 4))
    (if boucle (then (call faute_ici z 1 i)))
    (let pt Adr (call l_type l))
    (let t Nat (load Nat pt))
    (return t)))
  (let g Adr (call chercher_global z i))
  (let absent Bool (eq g nul))
  (if absent (then (call faute_jeton z 2 i)))
  (let pg Adr (call g_sorte g))
  (let sorte_g Nat (load Nat pg))
  (let pas_data Bool (ne sorte_g 2))
  (if pas_data (then (call faute_ici z 1 i)))
  (return 4)
  (end fn type_atome))
 
; bornes : la valeur (64 bits) de l'entier littéral i dans le type t ; TYPE si t n'est ni Ent ni Nat,
; HORS_BORNES s'il n'y tient pas
(fn bornes (params (z Adr) (i Nat) (t Nat)) (returns Nat)
  (let ent Bool (eq t 1))
  (let nat Bool (eq t 2))
  (let nombre Bool (or ent nat))
  (if nombre (then) (else (call faute_jeton z 4 i)))
  (let pd Adr (call j_drapeaux z i))
  (let d Nat (load Nat pd))
  (let pg Adr (call j_grandeur z i))
  (let g Nat (load Nat pg))
  (let trop_bits Nat (band d 2))
  (let trop Bool (ne trop_bits 0))
  (if trop (then (call faute_jeton z 6 i)))
  (let signe Nat (band d 1))
  (let negatif Bool (ne signe 0))
  (if nat (then
    (let non_nul Bool (ne g 0))
    (let refuse Bool (and negatif non_nul))
    (if refuse (then (call faute_jeton z 6 i)))
    (return g)))
  (if negatif (then
    (let nul Bool (eq g 0))
    (if nul (then (return 0)))
    (let au_dela Bool (gt g 9223372036854775808))
    (if au_dela (then (call faute_jeton z 6 i)))
    ; −g sur 64 bits : (non g) + 1
    (let inverse Nat (bnot g))
    (let oppose Nat (add inverse 1))
    (return oppose)))
  (let au_dela Bool (ge g 9223372036854775808))
  (if au_dela (then (call faute_jeton z 6 i)))
  (return g)
  (end fn bornes))
 
; emettre_litteral : mov du morceau bas dans le registre r, puis un movk par morceau de 16 bits non nul
(fn emettre_litteral (params (z Adr) (r Nat) (v Nat))
  (let bas Nat (band v 0xffff))
  (call e_mov z r bas)
  (var s Nat 16)
  (loop morceaux
    (let fini Bool (eq s 64))
    (if fini (then (break morceaux)))
    (let x Nat (shr v s))
    (let morceau Nat (band x 0xffff))
    (let present Bool (ne morceau 0))
    (if present (then (call e_movk z r morceau s)))
    (set s (add s 16))
    (end loop morceaux))
  (end fn emettre_litteral))
 
; charger : met la valeur de l'atome i, de type t, dans le registre r
(fn charger (params (z Adr) (i Nat) (t Nat) (r Nat))
  (let s Nat (call sorte_de z i))
  (let entier Bool (eq s 4))
  (if entier (then
    (let v Nat (call bornes z i t))
    (call emettre_litteral z r v)
    (return)))
  (let m Nat (call mot_de z i))
  (let vrai Bool (eq m 50))
  (if vrai (then
    (call e_mov z r 1)
    (return)))
  (let faux Bool (eq m 51))
  (if faux (then
    (call e_mov z r 0)
    (return)))
  (let nul Adr (adr_of 0))
  (let l Adr (call chercher_local z i))
  (let local_vu Bool (ne l nul))
  (if local_vu (then
    (let pc Adr (call l_case l))
    (let c Nat (load Nat pc))
    (let h Nat (call decalage_case c))
    (call e_ldr_case z r h)
    (return)))
  (call e_adr_data z r i)
  (end fn charger))
 
; atome : charge l'atome i dans le registre r ; un littéral prend le type attendu ; rend le type de l'atome
(fn atome (params (z Adr) (i Nat) (attendu Nat) (r Nat)) (returns Nat)
  (var t Nat (call type_atome z i))
  (let litteral Bool (eq t 0))
  (if litteral (then (set t attendu)))
  (call charger z i t r)
  (return t)
  (end fn atome))
 
; ---------------------------------------------------------------------------
; Opérandes d'une opération (forme : sa '(')
 
; unaire : (op a), a du type t, dans x0
(fn unaire (params (z Adr) (forme Nat) (t Nat))
  (let f Nat (call fermante_de z forme))
  (let tete Nat (add forme 1))
  (let a Nat (add forme 2))
  (call exiger_atome z a f tete)
  (let fin Nat (add forme 3))
  (call exiger_fin z fin f)
  (let ta Nat (call atome z a t 0))
  (let mal Bool (ne ta t))
  (if mal (then (call faute_jeton z 4 a)))
  (end fn unaire))
 
; deux : (op a b), a du type ta dans x0, b du type tb dans x1
(fn deux (params (z Adr) (forme Nat) (ta Nat) (tb Nat))
  (let f Nat (call fermante_de z forme))
  (let tete Nat (add forme 1))
  (let a Nat (add forme 2))
  (call exiger_atome z a f tete)
  (let b Nat (add forme 3))
  (call exiger_atome z b f tete)
  (let fin Nat (add forme 4))
  (call exiger_fin z fin f)
  (let ra Nat (call atome z a ta 0))
  (let mal_a Bool (ne ra ta))
  (if mal_a (then (call faute_jeton z 4 a)))
  (let rb Nat (call atome z b tb 1))
  (let mal_b Bool (ne rb tb))
  (if mal_b (then (call faute_jeton z 4 b)))
  (end fn deux))
 
; permis : le type t est dans le masque (bit t)
(fn permis (params (masque Nat) (t Nat)) (returns Bool)
  (let bit Nat (shl 1 t))
  (let x Nat (band masque bit))
  (let oui Bool (ne x 0))
  (return oui)
  (end fn permis))
 
; binaire : (op a b) ; a dans x0, b dans x1 ; rend le type commun T. Faute TYPE : a s'il n'est pas permis, b s'il
; n'est pas permis ou diffère de a, le premier littéral si aucun type ne fixe les littéraux (defaut 0 : aucun).
(fn binaire (params (z Adr) (forme Nat) (masque Nat) (defaut Nat)) (returns Nat)
  (let f Nat (call fermante_de z forme))
  (let tete Nat (add forme 1))
  (let a Nat (add forme 2))
  (call exiger_atome z a f tete)
  (let b Nat (add forme 3))
  (call exiger_atome z b f tete)
  (let fin Nat (add forme 4))
  (call exiger_fin z fin f)
  (let ta Nat (call type_atome z a))
  (let tb Nat (call type_atome z b))
  (let a_type Bool (ne ta 0))
  (if a_type (then
    (let bon_a Bool (call permis masque ta))
    (if bon_a (then) (else (call faute_jeton z 4 a)))))
  (let b_type Bool (ne tb 0))
  (if b_type (then
    (let bon_b Bool (call permis masque tb))
    (if bon_b (then) (else (call faute_jeton z 4 b)))
    (if a_type (then
      (let autre Bool (ne tb ta))
      (if autre (then (call faute_jeton z 4 b)))))))
  (var t Nat ta)
  (let sans_a Bool (eq t 0))
  (if sans_a (then (set t tb)))
  (let sans_type Bool (eq t 0))
  (if sans_type (then
    (let aucun Bool (eq defaut 0))
    (if aucun (then (call faute_jeton z 4 a)))
    (let bon Bool (call permis masque defaut))
    (if bon (then) (else (call faute_jeton z 4 a)))
    (set t defaut)))
  (call charger z a t 0)
  (call charger z b t 1)
  (return t)
  (end fn binaire))
 
; arguments : chaque argument de l'appel de g (de k à la fermante f, tête t), un atome du type de son paramètre,
; dans x0 à x7
(fn arguments (params (z Adr) (g Adr) (k Nat) (f Nat) (t Nat))
  (let pnb Adr (call g_nombre_params g))
  (let nb Nat (load Nat pnb))
  (var p Nat 0)
  (var a Nat k)
  (loop args
    (let fini Bool (eq p nb))
    (if fini (then (break args)))
    (call exiger_atome z a f t)
    (let pty Adr (call g_type_param g p))
    (let ty Nat (load Nat pty))
    (let r Nat (call atome z a ty p))
    (let mal Bool (ne r ty))
    (if mal (then (call faute_jeton z 4 a)))
    (set a (add a 1))
    (set p (add p 1))
    (end loop args))
  (call exiger_fin z a f)
  (end fn arguments))
 
; ---------------------------------------------------------------------------
; Expressions : un atome ou une opération ; le résultat dans x0 ; rend son type. declare : le type déclaré par
; la liaison (pour un littéral seul)
 
(fn e_arith (params (z Adr) (i Nat) (m Nat) (declare Nat)) (returns Nat)
  (let t Nat (call binaire z i 6 declare))
  (let ent Bool (eq t 1))
  (let est_add Bool (eq m 19))
  (if est_add (then
    (call e_adds z)
    (if ent (then (call e_bvs_piege z)) (else (call e_bhs_piege z)))
    (return t)))
  (let est_sub Bool (eq m 20))
  (if est_sub (then
    (call e_subs z)
    (if ent (then (call e_bvs_piege z)) (else (call e_blo_piege z)))
    (return t)))
  (let est_mul Bool (eq m 21))
  (if est_mul (then
    (call e_mul z)
    (if ent (then
      (call e_smulh z)
      (call e_cmp_asr z)
      (call e_bne_piege z))
    (else
      (call e_umulh z)
      (call e_cbnz3_piege z)))
    (call e_mov_x0_x2 z)
    (return t)))
  (let est_div Bool (eq m 22))
  (if est_div (then
    (call e_cbz1_piege2 z)
    (if ent (then
      (call e_negs_x2 z)
      (call e_cset_x2_vs z)
      (call e_cmn z)
      (call e_cset_x3_eq z)
      (call e_and_x2 z)
      (call e_cbnz2_piege z)
      (call e_sdiv0 z))
    (else (call e_udiv0 z)))
    (return t)))
  ; mod : diviseur nul -> piège 2 ; reste = a - (a div b) × b (MIN mod -1 = 0)
  (call e_cbz1_piege2 z)
  (if ent (then (call e_sdiv2 z)) (else (call e_udiv2 z)))
  (call e_msub z)
  (return t)
  (end fn e_arith))
 
; e_ordre : lt, le, gt, ge ; signé pour Ent, non signé pour Nat et Adr
(fn e_ordre (params (z Adr) (i Nat) (m Nat)) (returns Nat)
  (let t Nat (call binaire z i 0x16 0))
  (call e_cmp_x0_x1 z)
  (let ent Bool (eq t 1))
  (let est_lt Bool (eq m 27))
  (let est_le Bool (eq m 28))
  (let est_gt Bool (eq m 29))
  (if ent (then
    (if est_lt (then (call e_cset_lt z) (return 3)))
    (if est_le (then (call e_cset_le z) (return 3)))
    (if est_gt (then (call e_cset_gt z) (return 3)))
    (call e_cset_ge z)
    (return 3)))
  (if est_lt (then (call e_cset_lo z) (return 3)))
  (if est_le (then (call e_cset_ls z) (return 3)))
  (if est_gt (then (call e_cset_hi z) (return 3)))
  (call e_cset_hs z)
  (return 3)
  (end fn e_ordre))
 
; e_load : (load T a) : T parmi Ent Nat Adr, a Adr ; 8 octets
(fn e_load (params (z Adr) (i Nat)) (returns Nat)
  (let f Nat (call fermante_de z i))
  (let tete Nat (add i 1))
  (let pt Nat (add i 2))
  (let t Nat (call exiger_type z pt f tete))
  (let booleen Bool (eq t 3))
  (if booleen (then (call faute_jeton z 1 pt)))
  (let a Nat (add i 3))
  (call exiger_atome z a f tete)
  (let fin Nat (add i 4))
  (call exiger_fin z fin f)
  (let ta Nat (call atome z a 4 0))
  (let mal Bool (ne ta 4))
  (if mal (then (call faute_jeton z 4 a)))
  (call e_ldr_x0 z)
  (return t)
  (end fn e_load))
 
; e_size : (size d) : d nom de data ; sa taille, connue à la compilation
(fn e_size (params (z Adr) (i Nat)) (returns Nat)
  (let f Nat (call fermante_de z i))
  (let tete Nat (add i 1))
  (let d Nat (add i 2))
  (call exiger_nom z d f tete)
  (let fin Nat (add i 3))
  (call exiger_fin z fin f)
  (let nul Adr (adr_of 0))
  (let l Adr (call chercher_local z d))
  (let local_vu Bool (ne l nul))
  (let g Adr (call chercher_global z d))
  (let global_vu Bool (ne g nul))
  (let connu Bool (or local_vu global_vu))
  (if connu (then) (else (call faute_jeton z 2 d)))
  (if local_vu (then (call faute_jeton z 4 d)))
  (let ps Adr (call g_sorte g))
  (let sorte Nat (load Nat ps))
  (let pas_data Bool (ne sorte 2))
  (if pas_data (then (call faute_jeton z 4 d)))
  (let po Adr (call g_octets g))
  (let octets Nat (load Nat po))
  (call emettre_litteral z 0 octets)
  (return 2)
  (end fn e_size))
 
; e_call : (call f a…) : fn avec returns ; résultat dans x0
(fn e_call (params (z Adr) (i Nat)) (returns Nat)
  (let f Nat (call fermante_de z i))
  (let tete Nat (add i 1))
  (let n Nat (add i 2))
  (call exiger_nom z n f tete)
  (let g Adr (call fonction_appelee z n))
  (let pr Adr (call g_returns g))
  (let rend Nat (load Nat pr))
  (let sans_valeur Bool (eq rend 0))
  (if sans_valeur (then (call faute_jeton z 1 n)))
  (let a Nat (add i 3))
  (call arguments z g a f tete)
  (let pj Adr (call g_jeton g))
  (let j Nat (load Nat pj))
  (call e_bl z j)
  (let ptr Adr (call g_type_rendu g))
  (let t Nat (load Nat ptr))
  (return t)
  (end fn e_call))
 
; e_syscall : (syscall n a…) : n littéral de 0 à 65535, au plus 6 atomes Ent, Nat ou Adr (un littéral est un
; Ent) ; svc, puis -errno si le système signale une erreur (retenue), sans étiquette
(fn e_syscall (params (z Adr) (i Nat)) (returns Nat)
  (let f Nat (call fermante_de z i))
  (let tete Nat (add i 1))
  (let n Nat (add i 2))
  (let manque Bool (eq n f))
  (if manque (then (call faute_jeton z 1 tete)))
  (let s Nat (call sorte_de z n))
  (let entier Bool (eq s 4))
  (if entier (then) (else (call faute_ici z 1 n)))
  (let pd Adr (call j_drapeaux z n))
  (let d Nat (load Nat pd))
  (let pg Adr (call j_grandeur z n))
  (let g Nat (load Nat pg))
  (let trop_bits Nat (band d 2))
  (let trop Bool (ne trop_bits 0))
  (if trop (then (call faute_jeton z 6 n)))
  (let signe Nat (band d 1))
  (let negatif Bool (ne signe 0))
  (let non_nul Bool (ne g 0))
  (let negatif_vrai Bool (and negatif non_nul))
  (if negatif_vrai (then (call faute_jeton z 6 n)))
  (let grand Bool (ge g 65536))
  (if grand (then (call faute_jeton z 6 n)))
  (var a Nat (add i 3))
  (var r Nat 0)
  (loop args
    (let fini Bool (eq a f))
    (if fini (then (break args)))
    (let sept Bool (ge r 6))
    (if sept (then (call faute_ici z 1 a)))
    (call exiger_atome z a f tete)
    (var t Nat (call type_atome z a))
    (let litteral Bool (eq t 0))
    (if litteral (then (set t 1)))
    (let booleen Bool (eq t 3))
    (if booleen (then (call faute_jeton z 4 a)))
    (call charger z a t r)
    (set a (add a 1))
    (set r (add r 1))
    (end loop args))
  (call e_mov16 z g)
  (call e_svc z)
  (call e_cset_x1_hs z)
  (call e_neg_x1 z)
  (call e_eor_x2 z)
  (call e_sub_x0 z)
  (return 1)
  (end fn e_syscall))
 
; e_call_extern : (call_extern "dylib" "sym" atome…) : deux chaînes littérales non vides (la dylib puis le
; symbole), puis au plus 8 atomes Ent, Nat ou Adr dans x0-x7 (convention AAPCS64), résultat Ent dans x0. C.2b :
; analyse et validation de la forme seulement ; l'abaissement (adr x8, got_sym ; ldr x8, [x8] ; blr x8 et la
; ligne de la section __miaou_imports) est fait en C.2c/C.2d. Tant que l'abaissement manque, une forme bien
; formée est refusée (FORME) : inter ne sait pas encore produire le binaire lié.
(fn e_call_extern (params (z Adr) (i Nat) (declare Nat)) (returns Nat)
  (let f Nat (call fermante_de z i))
  (let tete Nat (add i 1))
  (let dylib Nat (add i 2))
  (let manque1 Bool (eq dylib f))
  (if manque1 (then (call faute_jeton z 1 tete)))
  (let sd Nat (call sorte_de z dylib))
  (let chaine1 Bool (eq sd 5))
  (if chaine1 (then) (else (call faute_jeton z 1 dylib)))
  (let ld Nat (call longueur_de z dylib))
  (let vide1 Bool (le ld 2))
  (if vide1 (then (call faute_jeton z 1 dylib)))
  (let sym Nat (add i 3))
  (let manque2 Bool (eq sym f))
  (if manque2 (then (call faute_jeton z 1 tete)))
  (let ss Nat (call sorte_de z sym))
  (let chaine2 Bool (eq ss 5))
  (if chaine2 (then) (else (call faute_jeton z 1 sym)))
  (let ls Nat (call longueur_de z sym))
  (let vide2 Bool (le ls 2))
  (if vide2 (then (call faute_jeton z 1 sym)))
  (let a_sym_tot Adr (call texte_de z sym))
  (let a_sym Adr (offset a_sym_tot 1))
  (let n_sym Nat (sub ls 2))
  (let idx Nat (call ajouter_import z a_sym n_sym))
  (var a Nat (add i 4))
  (var r Nat 0)
  (loop args
    (let fini Bool (eq a f))
    (if fini (then (break args)))
    (let neuf Bool (ge r 8))
    (if neuf (then (call faute_ici z 1 a)))
    (call exiger_atome z a f tete)
    (var t Nat (call type_atome z a))
    (let litteral Bool (eq t 0))
    (if litteral (then (set t 1)))
    (let booleen Bool (eq t 3))
    (if booleen (then (call faute_jeton z 4 a)))
    (call charger z a t r)
    (set a (add a 1))
    (set r (add r 1))
    (end loop args))
  (call e_adr_got z 8 idx)
  (call e_ldr_indirect z 8)
  (call e_blr z 8)
  (return declare)
  (end fn e_call_extern))
 
(fn expression (params (z Adr) (i Nat) (declare Nat)) (returns Nat)
  (let s Nat (call sorte_de z i))
  (let forme Bool (eq s 1))
  (if forme (then) (else
    (let t Nat (call atome z i declare 0))
    (return t)))
  (let tete Nat (add i 1))
  (let sh Nat (call sorte_de z tete))
  (let nom Bool (eq sh 3))
  (if nom (then) (else (call faute_ici z 1 tete)))
  (let m Nat (call mot_de z tete))
  (let appel Bool (eq m 18))
  (if appel (then
    (let t Nat (call e_call z i))
    (return t)))
  (let extern Bool (eq m 52))
  (if extern (then
    (let t Nat (call e_call_extern z i declare))
    (return t)))
  (let avant Bool (lt m 19))
  (let apres Bool (gt m 49))
  (let inconnue Bool (or avant apres))
  (if inconnue (then (call faute_ici z 1 tete)))
  (let arith Bool (le m 23))
  (if arith (then
    (let t Nat (call e_arith z i m declare))
    (return t)))
  (let est_neg Bool (eq m 24))
  (if est_neg (then
    (call unaire z i 1)
    (call e_negs_x0 z)
    (call e_bvs_piege z)
    (return 1)))
  (let egal Bool (le m 26))
  (if egal (then
    (let t Nat (call binaire z i 0x1e 0))
    (call e_cmp_x0_x1 z)
    (let est_eq Bool (eq m 25))
    (if est_eq (then (call e_cset_eq z)) (else (call e_cset_ne z)))
    (return 3)))
  (let ordre Bool (le m 30))
  (if ordre (then
    (let t Nat (call e_ordre z i m))
    (return t)))
  (let logique Bool (le m 32))
  (if logique (then
    (let t Nat (call binaire z i 8 0))
    (let est_and Bool (eq m 31))
    (if est_and (then (call e_and z)) (else (call e_orr z)))
    (return 3)))
  (let est_not Bool (eq m 33))
  (if est_not (then
    (call unaire z i 3)
    (call e_eor_un z)
    (return 3)))
  (let bits Bool (le m 36))
  (if bits (then
    (let t Nat (call binaire z i 4 2))
    (let est_band Bool (eq m 34))
    (if est_band (then
      (call e_and z)
      (return 2)))
    (let est_bor Bool (eq m 35))
    (if est_bor (then
      (call e_orr z)
      (return 2)))
    (call e_eor z)
    (return 2)))
  (let est_bnot Bool (eq m 37))
  (if est_bnot (then
    (call unaire z i 2)
    (call e_mvn z)
    (return 2)))
  (let decalage Bool (le m 39))
  (if decalage (then
    (let t Nat (call binaire z i 4 2))
    (call e_cmp_40 z)
    (call e_bhs_piege z)
    (let est_shl Bool (eq m 38))
    (if est_shl (then (call e_lsl z)) (else (call e_lsr z)))
    (return 2)))
  (let est_to_nat Bool (eq m 40))
  (if est_to_nat (then
    (call unaire z i 1)
    (call e_cmp_0 z)
    (call e_blt_piege z)
    (return 2)))
  (let est_to_ent Bool (eq m 41))
  (if est_to_ent (then
    (call unaire z i 2)
    (call e_cmp_0 z)
    (call e_blt_piege z)
    (return 1)))
  (let est_adr_of Bool (eq m 42))
  (if est_adr_of (then
    (call unaire z i 2)
    (return 4)))
  (let est_nat_of Bool (eq m 43))
  (if est_nat_of (then
    (call unaire z i 4)
    (return 2)))
  (let est_offset Bool (eq m 44))
  (if est_offset (then
    (call deux z i 4 2)
    (call e_adds z)
    (call e_bhs_piege z)
    (return 4)))
  (let est_diff Bool (eq m 45))
  (if est_diff (then
    (let t Nat (call binaire z i 0x10 4))
    (call e_subs z)
    (call e_blo_piege z)
    (return 2)))
  (let est_load Bool (eq m 46))
  (if est_load (then
    (let t Nat (call e_load z i))
    (return t)))
  (let est_load8 Bool (eq m 47))
  (if est_load8 (then
    (call unaire z i 4)
    (call e_ldrb_x0 z)
    (return 2)))
  (let est_size Bool (eq m 48))
  (if est_size (then
    (let t Nat (call e_size z i))
    (return t)))
  (let t Nat (call e_syscall z i))
  (return t)
  (end fn expression))
 
; ---------------------------------------------------------------------------
; Instructions (i : la '(' de l'instruction ; f : sa fermante ; t : sa tête)
 
; i_let : (let nom T expr), (var nom T expr) ; le nom naît après le calcul de expr, dans une case neuve
(fn i_let (params (z Adr) (i Nat) (sorte Nat))
  (let f Nat (call fermante_de z i))
  (let t Nat (add i 1))
  (let n Nat (add i 2))
  (call exiger_nom z n f t)
  (call exiger_libre z n)
  (let pt Nat (add i 3))
  (let ty Nat (call exiger_type z pt f t))
  (let e Nat (add i 4))
  (call exiger_present z e f t)
  (let apres Nat (call suivant z e))
  (call exiger_fin z apres f)
  (let r Nat (call expression z e ty))
  (let mal Bool (ne r ty))
  (if mal (then (call faute_jeton z 4 n)))
  (let c Nat (call nouvelle_case z))
  (let l Adr (call ajouter_local z n sorte ty c 0))
  (let h Nat (call decalage_case c))
  (call e_str_case z 0 h)
  (end fn i_let))
 
; i_set : (set nom expr) : seulement sur un var
(fn i_set (params (z Adr) (i Nat))
  (let f Nat (call fermante_de z i))
  (let t Nat (add i 1))
  (let n Nat (add i 2))
  (call exiger_nom z n f t)
  (let e Nat (add i 3))
  (call exiger_present z e f t)
  (let apres Nat (call suivant z e))
  (call exiger_fin z apres f)
  (let nul Adr (adr_of 0))
  (let l Adr (call chercher_local z n))
  (let absent Bool (eq l nul))
  (if absent (then
    (let g Adr (call chercher_global z n))
    (let inconnu Bool (eq g nul))
    (if inconnu (then (call faute_jeton z 2 n)))
    (call faute_jeton z 5 n)))
  (let ps Adr (call l_sorte l))
  (let sorte Nat (load Nat ps))
  (let fixe Bool (ne sorte 3))
  (if fixe (then (call faute_jeton z 5 n)))
  (let pt Adr (call l_type l))
  (let ty Nat (load Nat pt))
  (let pc Adr (call l_case l))
  (let c Nat (load Nat pc))
  (let r Nat (call expression z e ty))
  (let mal Bool (ne r ty))
  (if mal (then (call faute_jeton z 4 n)))
  (let h Nat (call decalage_case c))
  (call e_str_case z 0 h)
  (end fn i_set))
 
; i_if : (if atome (then …) [(else …)]) : cbz vers l'étiquette du else (ou de la fin)
(fn i_if (params (z Adr) (i Nat))
  (let f Nat (call fermante_de z i))
  (let t Nat (add i 1))
  (let a Nat (add i 2))
  (call exiger_atome z a f t)
  (let ta Nat (call atome z a 3 0))
  (let mal Bool (ne ta 3))
  (if mal (then (call faute_jeton z 4 a)))
  (let th Nat (add i 3))
  (call exiger_forme z th f t 10)
  (let el Nat (call suivant z th))
  (let avec_else Bool (ne el f))
  (if avec_else (then
    (call exiger_forme z el f t 11)
    (let apres Nat (call suivant z el))
    (call exiger_fin z apres f)))
  (let l1 Nat (call nouvelle_etiquette z))
  (call e_cbz z l1)
  (let r1 Nat (call bloc_simple z th))
  (if avec_else (then) (else
    (call e_etiquette z l1)
    (return)))
  (let l2 Nat (call nouvelle_etiquette z))
  (call e_b z l2)
  (call e_etiquette z l1)
  (let r2 Nat (call bloc_simple z el))
  (call e_etiquette z l2)
  (end fn i_if))
 
; i_loop : (loop nom … (end loop nom)) : étiquettes de début et de fin ; le nom n'est visible que dans le corps.
; Rend 2 (fin de fonction) si aucun break ne vise la boucle, 0 sinon.
(fn i_loop (params (z Adr) (i Nat)) (returns Nat)
  (let f Nat (call fermante_de z i))
  (let t Nat (add i 1))
  (let n Nat (add i 2))
  (call exiger_nom z n f t)
  (call exiger_libre z n)
  (let l1 Nat (call nouvelle_etiquette z))
  (let l2 Nat (call nouvelle_etiquette z))
  (let entree Adr (call ajouter_local z n 4 0 l1 l2))
  (call e_etiquette z l1)
  (let premier Nat (add i 3))
  (let r Nat (call bloc z premier f 2 n t))
  (call e_b z l1)
  (call e_etiquette z l2)
  (let an Adr (call e_locaux z))
  (let nl Nat (load Nat an))
  (let nl1 Nat (sub nl 1))
  (store Nat an nl1)
  (let pb Adr (call l_break entree))
  (let vu Nat (load Nat pb))
  (let sans_break Bool (eq vu 0))
  (if sans_break (then (return 2)))
  (return 0)
  (end fn i_loop))
 
; i_saut : (break nom), (continue nom) (m : 13 ou 14) : nom d'une boucle visible
(fn i_saut (params (z Adr) (i Nat) (m Nat))
  (let f Nat (call fermante_de z i))
  (let t Nat (add i 1))
  (let n Nat (add i 2))
  (call exiger_nom z n f t)
  (let fin Nat (add i 3))
  (call exiger_fin z fin f)
  (let nul Adr (adr_of 0))
  (let l Adr (call chercher_local z n))
  (let absent Bool (eq l nul))
  (if absent (then
    (let g Adr (call chercher_global z n))
    (let inconnu Bool (eq g nul))
    (if inconnu (then (call faute_jeton z 2 n)))
    (call faute_jeton z 1 n)))
  (let ps Adr (call l_sorte l))
  (let sorte Nat (load Nat ps))
  (let pas_boucle Bool (ne sorte 4))
  (if pas_boucle (then (call faute_jeton z 1 n)))
  (let est_continue Bool (eq m 14))
  (if est_continue (then
    (let pc Adr (call l_case l))
    (let debut Nat (load Nat pc))
    (call e_b z debut)
    (return)))
  (let pb Adr (call l_break l))
  (store Nat pb 1)
  (let pf Adr (call l_fin l))
  (let sortie Nat (load Nat pf))
  (call e_b z sortie)
  (end fn i_saut))
 
; epilogue : x29, x30 repris, le cadre rendu, retour
(fn epilogue (params (z Adr))
  (call e_ldp z)
  (let pc Adr (call e_cadre z))
  (let cadre Nat (load Nat pc))
  (let haut Nat (shr cadre 12))
  (let avec_haut Bool (ne haut 0))
  (if avec_haut (then (call e_add_sp_haut z haut)))
  (let bas Nat (band cadre 0xfff))
  (let avec_bas Bool (ne bas 0))
  (if avec_bas (then (call e_add_sp z bas)))
  (call e_ret z)
  (end fn epilogue))
 
; i_return : (return [atome]) : atome si et seulement si la fn a un returns ; main contrôle sa valeur (255 au plus)
(fn i_return (params (z Adr) (i Nat))
  (let f Nat (call fermante_de z i))
  (let t Nat (add i 1))
  (let pf Adr (call e_fonction z))
  (let g Adr (load Adr pf))
  (let pr Adr (call g_returns g))
  (let rend Nat (load Nat pr))
  (let avec_valeur Bool (ne rend 0))
  (let a Nat (add i 2))
  (let vide Bool (eq a f))
  (if vide (then
    (if avec_valeur (then (call faute_jeton z 1 t)))
    (call epilogue z)
    (return)))
  (if avec_valeur (then) (else (call faute_ici z 1 a)))
  (call exiger_atome z a f t)
  (let fin Nat (add i 3))
  (call exiger_fin z fin f)
  (let ptr Adr (call g_type_rendu g))
  (let tr Nat (load Nat ptr))
  (let ta Nat (call atome z a tr 0))
  (let mal Bool (ne ta tr))
  (if mal (then (call faute_jeton z 4 a)))
  (let pm Adr (call g_main g))
  (let est_main Nat (load Nat pm))
  (let controle Bool (ne est_main 0))
  (if controle (then
    (call e_cmp_ff z)
    (call e_bhi_piege z)))
  (call epilogue z)
  (end fn i_return))
 
; i_store : (store T adresse atome) : T parmi Ent Nat Adr
(fn i_store (params (z Adr) (i Nat))
  (let f Nat (call fermante_de z i))
  (let t Nat (add i 1))
  (let pt Nat (add i 2))
  (let ty Nat (call exiger_type z pt f t))
  (let booleen Bool (eq ty 3))
  (if booleen (then (call faute_jeton z 1 pt)))
  (let a Nat (add i 3))
  (call exiger_atome z a f t)
  (let v Nat (add i 4))
  (call exiger_atome z v f t)
  (let fin Nat (add i 5))
  (call exiger_fin z fin f)
  (let ta Nat (call atome z a 4 1))
  (let mal_a Bool (ne ta 4))
  (if mal_a (then (call faute_jeton z 4 a)))
  (let tv Nat (call atome z v ty 0))
  (let mal_v Bool (ne tv ty))
  (if mal_v (then (call faute_jeton z 4 v)))
  (call e_str_x1 z)
  (end fn i_store))
 
; i_store8 : (store8 adresse atome) : un octet ; piège au-delà de 255
(fn i_store8 (params (z Adr) (i Nat))
  (let f Nat (call fermante_de z i))
  (let t Nat (add i 1))
  (let a Nat (add i 2))
  (call exiger_atome z a f t)
  (let v Nat (add i 3))
  (call exiger_atome z v f t)
  (let fin Nat (add i 4))
  (call exiger_fin z fin f)
  (let ta Nat (call atome z a 4 1))
  (let mal_a Bool (ne ta 4))
  (if mal_a (then (call faute_jeton z 4 a)))
  (let tv Nat (call atome z v 2 0))
  (let mal_v Bool (ne tv 2))
  (if mal_v (then (call faute_jeton z 4 v)))
  (call e_cmp_ff z)
  (call e_bhi_piege z)
  (call e_strb_x1 z)
  (end fn i_store8))
 
; i_call : (call f atome…) : seulement pour une fn sans returns (un résultat n'est jamais jeté)
(fn i_call (params (z Adr) (i Nat))
  (let f Nat (call fermante_de z i))
  (let t Nat (add i 1))
  (let n Nat (add i 2))
  (call exiger_nom z n f t)
  (let g Adr (call fonction_appelee z n))
  (let pr Adr (call g_returns g))
  (let rend Nat (load Nat pr))
  (let avec_valeur Bool (ne rend 0))
  (if avec_valeur (then (call faute_jeton z 1 n)))
  (let a Nat (add i 3))
  (call arguments z g a f t)
  (let pj Adr (call g_jeton g))
  (let j Nat (load Nat pj))
  (call e_bl z j)
  (end fn i_call))
 
; instruction : la vérifie et produit son code ; rend 1 si elle termine le bloc (return, break, continue), plus 2
; si elle compte comme fin de fonction (return, ou boucle sans break vers elle)
(fn instruction (params (z Adr) (i Nat)) (returns Nat)
  (let s Nat (call sorte_de z i))
  (let forme Bool (eq s 1))
  (if forme (then) (else (call faute_jeton z 1 i)))
  (let t Nat (add i 1))
  (let st Nat (call sorte_de z t))
  (let nom Bool (eq st 3))
  (if nom (then) (else (call faute_ici z 1 t)))
  (let m Nat (call mot_de z t))
  (let est_let Bool (eq m 6))
  (if est_let (then
    (call i_let z i 2)
    (return 0)))
  (let est_var Bool (eq m 7))
  (if est_var (then
    (call i_let z i 3)
    (return 0)))
  (let est_set Bool (eq m 8))
  (if est_set (then
    (call i_set z i)
    (return 0)))
  (let est_if Bool (eq m 9))
  (if est_if (then
    (call i_if z i)
    (return 0)))
  (let est_loop Bool (eq m 12))
  (if est_loop (then
    (let r Nat (call i_loop z i))
    (return r)))
  (let est_break Bool (eq m 13))
  (let est_continue Bool (eq m 14))
  (let saut Bool (or est_break est_continue))
  (if saut (then
    (call i_saut z i m)
    (return 1)))
  (let est_return Bool (eq m 15))
  (if est_return (then
    (call i_return z i)
    (return 3)))
  (let est_store Bool (eq m 16))
  (if est_store (then
    (call i_store z i)
    (return 0)))
  (let est_store8 Bool (eq m 17))
  (if est_store8 (then
    (call i_store8 z i)
    (return 0)))
  (let est_call Bool (eq m 18))
  (if est_call (then
    (call i_call z i)
    (return 0)))
  (call faute_ici z 1 t)
  (return 0)
  (end fn instruction))
 
; ---------------------------------------------------------------------------
; Blocs
 
; fin_de_bloc : (end fn nom) ou (end loop nom) en k, qui doit être le dernier élément du bloc (fermante f)
(fn fin_de_bloc (params (z Adr) (k Nat) (f Nat) (sorte Nat) (nom Nat))
  (let fe Nat (call fermante_de z k))
  (let t Nat (add k 1))
  (let a Nat (add k 2))
  (let manque_a Bool (eq a fe))
  (if manque_a (then (call faute_jeton z 1 t)))
  (let sa Nat (call sorte_de z a))
  (let nom_a Bool (eq sa 3))
  (if nom_a (then) (else (call faute_ici z 1 a)))
  (var attendu Nat 12)
  (let fonction Bool (eq sorte 1))
  (if fonction (then (set attendu 2)))
  (let ma Nat (call mot_de z a))
  (let bon_a Bool (eq ma attendu))
  (if bon_a (then) (else (call faute_ici z 1 a)))
  (let b Nat (add k 3))
  (let manque_b Bool (eq b fe))
  (if manque_b (then (call faute_jeton z 1 t)))
  (let sb Nat (call sorte_de z b))
  (let nom_b Bool (eq sb 3))
  (if nom_b (then) (else (call faute_ici z 1 b)))
  (let mb Nat (call mot_de z b))
  (let reserve Bool (ne mb 0))
  (if reserve (then (call faute_ici z 1 b)))
  (let meme Bool (call memes_noms z b nom))
  (if meme (then) (else (call faute_ici z 1 b)))
  (let apres_nom Nat (add k 4))
  (call exiger_fin z apres_nom fe)
  (let pn Adr (call e_nom_fin z))
  (store Nat pn b)
  (let apres Nat (call suivant z k))
  (call exiger_fin z apres f)
  (end fn fin_de_bloc))
 
; bloc : les éléments de premier à la fermante f ; sorte 0 then/else, 1 fn, 2 loop ; nom et tete : le jeton du nom
; et de la tête (fn, loop) ; une fn ou une boucle finit par (end fn|loop nom). Les noms déclarés dans le bloc
; cessent d'être visibles à sa fin. Rend 1 si sa dernière instruction compte comme fin de fonction.
(fn bloc (params (z Adr) (premier Nat) (f Nat) (sorte Nat) (nom Nat) (tete Nat)) (returns Nat)
  (let an Adr (call e_locaux z))
  (let garde Nat (load Nat an))
  (let avec_fin Bool (ne sorte 0))
  (var k Nat premier)
  (var fin Nat 0)
  (var mort Nat 0)
  (loop elements
    (let bout Bool (eq k f))
    (if bout (then
      (if avec_fin (then (call faute_jeton z 1 tete)))
      (break elements)))
    (let s Nat (call sorte_de z k))
    (let forme Bool (eq s 1))
    (let peut_finir Bool (and forme avec_fin))
    (if peut_finir (then
      (let h Nat (add k 1))
      (let sh Nat (call sorte_de z h))
      (let mh Nat (call mot_de z h))
      (let nom_h Bool (eq sh 3))
      (let mot_end Bool (eq mh 5))
      (let est_end Bool (and nom_h mot_end))
      (if est_end (then
        (call fin_de_bloc z k f sorte nom)
        (break elements)))))
    ; rien ne suit return, break ou continue dans le même bloc
    (let apres_saut Bool (ne mort 0))
    (if apres_saut (then (call faute_ici z 1 k)))
    (let r Nat (call instruction z k))
    (set mort (band r 1))
    (let r2 Nat (shr r 1))
    (set fin (band r2 1))
    (set k (call suivant z k))
    (end loop elements))
  (store Nat an garde)
  (return fin)
  (end fn bloc))
 
; bloc_simple : (then …) ou (else …) en forme ; ses instructions, sans (end …)
(fn bloc_simple (params (z Adr) (forme Nat)) (returns Nat)
  (let f Nat (call fermante_de z forme))
  (let premier Nat (add forme 2))
  (let r Nat (call bloc z premier f 0 0 0))
  (return r)
  (end fn bloc_simple))
 
; ---------------------------------------------------------------------------
; Fonctions. Cadre : x29, x30 en [sp], une case de 8 octets par paramètre, let et var en [sp, #16 + 8 × case] ;
; jamais réutilisée ; 32 Kio au plus.
 
; poser_pieges : les deux pièges de la fonction en cours, posés après elle sous leurs étiquettes : brk #1, brk #2
; (un contrôle saute au piège de sa fonction, à moins d'1 Mio, la portée d'un saut conditionnel)
; une fonction de plus de 0xff000 octets, pièges compris, donne LIMITE : ses sauts conditionnels et ses cbz
; ne portent qu'à ±1 Mio
(fn poser_pieges (params (z Adr) (debut Nat))
  (let pu Adr (call e_piege_un z))
  (let eu Nat (load Nat pu))
  (call e_etiquette z eu)
  (call e_brk1 z)
  (let pd Adr (call e_piege_deux z))
  (let ed Nat (load Nat pd))
  (call e_etiquette z ed)
  (call e_brk2 z)
  (let po Adr (call e_octets_code z))
  (let ici Nat (load Nat po))
  (let taille Nat (sub ici debut))
  (let trop Bool (gt taille 0xff000))
  (if trop (then
    (let lf Nat (call ligne_fonction z))
    (let nf Nat (size t_cap_fonction))
    (call limite_a z lf t_cap_fonction nf 0xff000)))
  (end fn poser_pieges))
 
(fn compiler_fonction (params (z Adr) (g Adr))
  (let pf Adr (call e_fonction z))
  (store Adr pf g)
  (let an Adr (call e_locaux z))
  (store Nat an 0)
  (let pc Adr (call e_cases z))
  (store Nat pc 0)
  (let piege_un Nat (call nouvelle_etiquette z))
  (let pu Adr (call e_piege_un z))
  (store Nat pu piege_un)
  (let piege_deux Nat (call nouvelle_etiquette z))
  (let pd Adr (call e_piege_deux z))
  (store Nat pd piege_deux)
  (let poc Adr (call e_octets_code z))
  (let debut Nat (load Nat poc))
  (let pfo Adr (call g_forme g))
  (let forme Nat (load Nat pfo))
  (let n Nat (call compter_cases z forme))
  (let pnb Adr (call g_nombre_params g))
  (let nb Nat (load Nat pnb))
  (let a Nat (add n nb))
  (let total Nat (add a 2))
  (let octets Nat (mul total 8))
  (let arrondi Nat (add octets 15))
  (let cadre Nat (band arrondi 0xfffffffffffffff0))
  (let trop Bool (gt cadre 0x8000))
  (if trop (then
    (let lf Nat (call ligne_fonction z))
    (let nc Nat (size t_cap_cadre))
    (call limite_a z lf t_cap_cadre nc 0x8000)))
  (let pca Adr (call e_cadre z))
  (store Nat pca cadre)
  (let pj Adr (call g_jeton g))
  (let j Nat (load Nat pj))
  (call e_fn z j)
  (let haut Nat (shr cadre 12))
  (let avec_haut Bool (ne haut 0))
  (if avec_haut (then (call e_sub_sp_haut z haut)))
  (let bas Nat (band cadre 0xfff))
  (let avec_bas Bool (ne bas 0))
  (if avec_bas (then (call e_sub_sp z bas)))
  (call e_stp z)
  (var p Nat 0)
  (loop parametres
    (let fini Bool (eq p nb))
    (if fini (then (break parametres)))
    (let pno Adr (call g_nom_param g p))
    (let jp Nat (load Nat pno))
    (call exiger_libre z jp)
    (let c Nat (call nouvelle_case z))
    (let pty Adr (call g_type_param g p))
    (let ty Nat (load Nat pty))
    (let l Adr (call ajouter_local z jp 1 ty c 0))
    (let h Nat (call decalage_case c))
    (call e_str_case z p h)
    (set p (add p 1))
    (end loop parametres))
  (let pco Adr (call g_corps g))
  (let premier Nat (load Nat pco))
  (let f Nat (call fermante_de z forme))
  (let tete Nat (add forme 1))
  (let fin Nat (call bloc z premier f 1 j tete))
  (let pr Adr (call g_returns g))
  (let rend Nat (load Nat pr))
  (let sans_returns Bool (eq rend 0))
  (if sans_returns (then
    (call epilogue z)
    (call poser_pieges z debut)
    (return)))
  (let manque Bool (eq fin 0))
  (if manque (then
    (let pn Adr (call e_nom_fin z))
    (let nf Nat (load Nat pn))
    (call faute_jeton z 7 nf)))
  (call poser_pieges z debut)
  (end fn compiler_fonction))
 
; ecrire_octets : les n octets de a, en hexadécimal, 16 par ligne
(fn ecrire_octets (params (z Adr) (a Adr) (n Nat))
  (var k Nat 0)
  (loop octets
    (let fini Bool (eq k n))
    (if fini (then (break octets)))
    (let p Adr (offset a k))
    (let c Nat (load8 p))
    (let haut Nat (shr c 4))
    (let ch Nat (call chiffre_hex haut))
    (call mettre z ch)
    (let bas Nat (band c 15))
    (let cb Nat (call chiffre_hex bas))
    (call mettre z cb)
    (set k (add k 1))
    (let colonne Nat (band k 15))
    (let fin_ligne Bool (eq colonne 0))
    (if fin_ligne (then (call mettre z 10)) (else (call mettre z 32)))
    (end loop octets))
  (let reste Nat (band n 15))
  (let ligne_ouverte Bool (ne reste 0))
  (if ligne_ouverte (then (call mettre z 10)))
  (end fn ecrire_octets))
 
; corps : l'entrée saute à main, puis chaque fonction dans l'ordre du texte, ses deux pièges après elle ; puis les données
(fn corps (params (z Adr))
  (call e_b_main z)
  (let an Adr (call e_nombre_globaux z))
  (let n Nat (load Nat an))
  (var k Nat 0)
  (loop fonctions
    (let fini Bool (eq k n))
    (if fini (then (break fonctions)))
    (let g Adr (call global z k))
    (let ps Adr (call g_sorte g))
    (let sorte Nat (load Nat ps))
    (let est_fn Bool (eq sorte 1))
    (if est_fn (then
      (call compiler_fonction z g)
      (let lf Nat (call ligne_fonction z))
      (call controler_chaine z lf)))
    (set k (add k 1))
    (end loop fonctions))
  (set k 0)
  (loop donnees
    (let fini Bool (eq k n))
    (if fini (then (break donnees)))
    (let g Adr (call global z k))
    (set k (add k 1))
    (let ps Adr (call g_sorte g))
    (let sorte Nat (load Nat ps))
    (let est_data Bool (eq sorte 2))
    (if est_data (then) (else (continue donnees)))
    (let pj Adr (call g_jeton g))
    (let j Nat (load Nat pj))
    (call e_data z j)
    (let pc Adr (call g_chaine g))
    (let c Nat (load Nat pc))
    (let dest Adr (call zone_decodee z))
    (let octets Nat (call decoder_chaine z c dest))
    (let pd Adr (call e_octets_donnees z))
    (call ajouter pd octets)
    (let e Adr (call e_ecriture z))
    (let ici Adr (load Adr e))
    (let bout Adr (call fin_texte z))
    (let place Nat (diff bout ici))
    (let quatre Nat (mul octets 4))
    (let besoin Nat (add quatre 0x40))
    (let manque Bool (lt place besoin))
    (let ldj Nat (call ligne_de z j))
    (if manque (then
      (let nt Nat (size t_cap_produit))
      (call limite_a z ldj t_cap_produit nt 0xfffffff)))
    (call ecrire_octets z dest octets)
    (call controler_chaine z ldj)
    (end loop donnees))
  (end fn corps))
 
; ecrire_tout : les n octets de a sur la sortie standard (les écritures peuvent être partielles)
(fn ecrire_tout (params (z Adr) (a Adr) (n Nat))
  (var p Adr a)
  (var reste Nat n)
  (loop ecriture
    (let fini Bool (eq reste 0))
    (if fini (then (break ecriture)))
    (let r Ent (syscall 4 1 p reste))
    (let echec Bool (le r 0))
    (if echec (then (call echec_es)))
    (let ecrit Nat (to_nat r))
    (set p (offset p ecrit))
    (set reste (sub reste ecrit))
    (end loop ecriture))
  (end fn ecrire_tout))
 
 
; ---------------------------------------------------------------------------
; Mise en page et signature. Les modèles d'inter.mac (en-tête Mach-O de 0x260 octets, correctifs chaînés vides et
; remplissage de 64 octets, signature de 118 octets suivie de n cases nulles), en texte hexadécimal : un data ne peut
; pas porter d'octet nul. Les champs entre crochets dans inter.mac sont posés par la mise en page.
 
(data modele_entete "cf fa ed fe 0c 00 00 01 00 00 00 00 02 00 00 00 0b 00 00 00 38 02 00 00 85 00 20 00 00 00 00 00 19 00 00 00 48 00 00 00 5f 5f 50 41 47 45 5a 45 52 4f 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 19 00 00 00 98 00 00 00 5f 5f 54 45 58 54 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 05 00 00 00 05 00 00 00 01 00 00 00 00 00 00 00 5f 5f 74 65 78 74 00 00 00 00 00 00 00 00 00 00 5f 5f 54 45 58 54 00 00 00 00 00 00 00 00 00 00 60 02 00 00 01 00 00 00 00 00 00 00 00 00 00 00 60 02 00 00 04 00 00 00 00 00 00 00 00 00 00 00 00 04 00 80 00 00 00 00 00 00 00 00 00 00 00 00 19 00 00 00 48 00 00 00 5f 5f 4c 49 4e 4b 45 44 49 54 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 34 00 00 80 10 00 00 00 00 00 00 00 38 00 00 00 02 00 00 00 18 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0b 00 00 00 50 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0e 00 00 00 20 00 00 00 0c 00 00 00 2f 75 73 72 2f 6c 69 62 2f 64 79 6c 64 00 00 00 00 00 00 00 28 00 00 80 18 00 00 00 60 02 00 00 00 00 00 00 00 00 00 20 00 00 00 00 0c 00 00 00 38 00 00 00 18 00 00 00 02 00 00 00 03 64 27 05 00 00 01 00 2f 75 73 72 2f 6c 69 62 2f 6c 69 62 53 79 73 74 65 6d 2e 42 2e 64 79 6c 69 62 00 00 00 00 00 00 32 00 00 00 18 00 00 00 01 00 00 00 00 00 0b 00 00 00 0b 00 00 00 00 00 1d 00 00 00 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00")
(data modele_entete_lie "cf fa ed fe 0c 00 00 01 00 00 00 00 02 00 00 00 0c 00 00 00 d0 02 00 00 85 00 20 00 00 00 00 00 19 00 00 00 48 00 00 00 5f 5f 50 41 47 45 5a 45 52 4f 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 19 00 00 00 98 00 00 00 5f 5f 54 45 58 54 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 40 00 00 00 00 00 00 05 00 00 00 05 00 00 00 01 00 00 00 00 00 00 00 5f 5f 74 65 78 74 00 00 00 00 00 00 00 00 00 00 5f 5f 54 45 58 54 00 00 00 00 00 00 00 00 00 00 60 02 00 00 01 00 00 00 00 00 00 00 00 00 00 00 60 02 00 00 04 00 00 00 00 00 00 00 00 00 00 00 00 04 00 80 00 00 00 00 00 00 00 00 00 00 00 00 19 00 00 00 98 00 00 00 5f 5f 44 41 54 41 5f 43 4f 4e 53 54 00 00 00 00 00 40 00 00 01 00 00 00 00 40 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 00 00 00 03 00 00 00 01 00 00 00 10 00 00 00 5f 5f 67 6f 74 00 00 00 00 00 00 00 00 00 00 00 5f 5f 44 41 54 41 5f 43 4f 4e 53 54 00 00 00 00 00 40 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 40 00 00 03 00 00 00 00 00 00 00 00 00 00 00 06 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 19 00 00 00 48 00 00 00 5f 5f 4c 49 4e 4b 45 44 49 54 00 00 00 00 00 00 00 80 00 00 01 00 00 00 00 40 00 00 00 00 00 00 00 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 34 00 00 80 10 00 00 00 00 80 00 00 38 00 00 00 02 00 00 00 18 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0b 00 00 00 50 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0e 00 00 00 20 00 00 00 0c 00 00 00 2f 75 73 72 2f 6c 69 62 2f 64 79 6c 64 00 00 00 00 00 00 00 28 00 00 80 18 00 00 00 f8 02 00 00 00 00 00 00 00 00 00 20 00 00 00 00 0c 00 00 00 38 00 00 00 18 00 00 00 02 00 00 00 03 64 27 05 00 00 01 00 2f 75 73 72 2f 6c 69 62 2f 6c 69 62 53 79 73 74 65 6d 2e 42 2e 64 79 6c 69 62 00 00 00 00 00 00 32 00 00 00 18 00 00 00 01 00 00 00 00 00 0b 00 00 00 0b 00 00 00 00 00 1d 00 00 00 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00")
(data modele_linkedit "00 00 00 00 20 00 00 00 30 00 00 00 30 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00")
(data modele_signature "fa de 0c c0 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 14 fa de 0c 02 00 00 00 00 00 02 04 00 00 02 00 02 00 00 00 62 00 00 00 58 00 00 00 00 00 00 00 00 00 00 00 00 20 02 00 0c 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 70 72 6f 67 72 61 6d 6d 65 00")
 
(fn zone_entete (params (z Adr)) (returns Adr)
  (let p Adr (offset z 0x2000))
  (return p)
  (end fn zone_entete))
 
(fn zone_signature (params (z Adr)) (returns Adr)
  (let p Adr (offset z 0x2400))
  (return p)
  (end fn zone_signature))
 
(fn zone_correctifs (params (z Adr)) (returns Adr)
  (let p Adr (offset z 0x2800))
  (return p)
  (end fn zone_correctifs))
 
(fn zone_texte_entete (params (z Adr)) (returns Adr)
  (let p Adr (offset z 0x3000))
  (return p)
  (end fn zone_texte_entete))
 
; les cases d'empreinte de la signature : des zéros, jamais écrits (jusqu'à 0x10000 : 1 536 cases)
(fn zone_zeros (params (z Adr)) (returns Adr)
  (let p Adr (offset z 0x4000))
  (return p)
  (end fn zone_zeros))
 
(data f_alignement_page "!4000")
(fn e_alignement_page (params (z Adr)) (let n Nat (size f_alignement_page)) (call emettre z f_alignement_page n 0 0 0) (end fn e_alignement_page))
(data f_alignement_fin "!10")
(fn e_alignement_fin (params (z Adr)) (let n Nat (size f_alignement_fin)) (call emettre z f_alignement_fin n 0 0 0) (end fn e_alignement_fin))
 
; lire_modele : les octets du texte hexadécimal t (n caractères, paires séparées par une espace) écrits à dest ;
; rend leur nombre
(fn lire_modele (params (t Adr) (n Nat) (dest Adr)) (returns Nat)
  (var k Nat 0)
  (var o Nat 0)
  (loop paires
    (let fini Bool (ge k n))
    (if fini (then (break paires)))
    (let ph Adr (offset t k))
    (let ch Nat (load8 ph))
    (let h Nat (call valeur_hex ch))
    (let k1 Nat (add k 1))
    (let pl Adr (offset t k1))
    (let cl Nat (load8 pl))
    (let l Nat (call valeur_hex cl))
    (let seize Nat (mul h 16))
    (let v Nat (add seize l))
    (let d Adr (offset dest o))
    (store8 d v)
    (set o (add o 1))
    (set k (add k 3))
    (end loop paires))
  (return o)
  (end fn lire_modele))
 
; octet_de : l'octet de rang r (0 le plus bas) de v
(fn octet_de (params (v Nat) (r Nat)) (returns Nat)
  (let s Nat (mul r 8))
  (let x Nat (shr v s))
  (let o Nat (band x 255))
  (return o)
  (end fn octet_de))
 
; ranger_petit : les 4 octets bas de v à l'adresse a, petit-boutistes (le Mach-O)
(fn ranger_petit (params (a Adr) (v Nat))
  (var r Nat 0)
  (loop octets
    (let fini Bool (eq r 4))
    (if fini (then (break octets)))
    (let o Nat (call octet_de v r))
    (let p Adr (offset a r))
    (store8 p o)
    (set r (add r 1))
    (end loop octets))
  (end fn ranger_petit))
 
; ranger_gros : les 4 octets bas de v à l'adresse a, gros-boutistes (la signature)
(fn ranger_gros (params (a Adr) (v Nat))
  (var r Nat 0)
  (loop octets
    (let fini Bool (eq r 4))
    (if fini (then (break octets)))
    (let rang Nat (sub 3 r))
    (let o Nat (call octet_de v rang))
    (let p Adr (offset a r))
    (store8 p o)
    (set r (add r 1))
    (end loop octets))
  (end fn ranger_gros))
 
; poser : ranger_petit ou ranger_gros au décalage d de la zone a
(fn poser_petit (params (a Adr) (d Nat) (v Nat))
  (let p Adr (offset a d))
  (call ranger_petit p v)
  (end fn poser_petit))
 
(fn poser_gros (params (a Adr) (d Nat) (v Nat))
  (let p Adr (offset a d))
  (call ranger_gros p v)
  (end fn poser_gros))
 
; mise_en_page : T = 0x260 + code + données (fin de __text) ; L = T arrondi à 16 Kio (__LINKEDIT) ;
; n = L / 4 Kio + 1 pages signées (jusqu'à L + 0x40) ; S = 118 + 32 n octets de signature, arrondis à 16 (Sr).
; Puis les limites de la chaîne (fichier de moins de 1 Mio pour le signeur ; 16 384 noms et 65 536 renvois pour
; l'encodeur), la fin du texte (alignement, correctifs, signature à cases nulles), l'en-tête, et l'écriture de tout.
; taille_noms_de : 1 + somme(longueur du nom + 1) pour les k symboles importés (table des chaînes \0_sym0\0…)
(fn taille_noms_de (params (z Adr) (k Nat)) (returns Nat)
  (let base Adr (call zone_imports z))
  (var total Nat 1)
  (var i Nat 0)
  (loop noms
    (let fini Bool (eq i k))
    (if fini (then (break noms)))
    (let d Nat (mul i 64))
    (let e Adr (offset base d))
    (let ln Nat (load Nat e))
    (let ln1 Nat (add ln 1))
    (set total (add total ln1))
    (set i (add i 1))
    (end loop noms))
  (return total)
  (end fn taille_noms_de))
 
; emettre_got : le __DATA_CONST,__got : une entrée de 8 octets par import (dyld_chained_ptr_64_bind), avec son
; étiquette :g.i. Entrée i : ordinal i, next 2 (sauf la dernière, 0), bind 1 : (i) | (next<<51) | (1<<63).
(fn emettre_got (params (z Adr) (k Nat))
  (let buf Adr (offset z 0x58000))
  (let p0 Adr (offset buf 0))
  (let p1 Adr (offset buf 1))
  (let p2 Adr (offset buf 2))
  (let p3 Adr (offset buf 3))
  (let p4 Adr (offset buf 4))
  (let p5 Adr (offset buf 5))
  (let p6 Adr (offset buf 6))
  (let p7 Adr (offset buf 7))
  (store8 p2 0)
  (store8 p3 0)
  (store8 p4 0)
  (store8 p5 0)
  (store8 p7 0x80)
  (let der Nat (sub k 1))
  (var i Nat 0)
  (loop entrees
    (let fini Bool (eq i k))
    (if fini (then (break entrees)))
    (call e_etiq_got z i)
    (let bas Nat (band i 0xff))
    (store8 p0 bas)
    (let i8 Nat (shr i 8))
    (let haut Nat (band i8 0xff))
    (store8 p1 haut)
    (let dernier Bool (eq i der))
    (var b6 Nat 0x10)
    (if dernier (then (set b6 0)))
    (store8 p6 b6)
    (call ecrire_octets z buf 8)
    (set i (add i 1))
    (end loop entrees))
  (end fn emettre_got))
 
; batir_fixups : construit le payload LC_DYLD_CHAINED_FIXUPS dans le tampon fxb (header, starts_in_image,
; starts_in_segment, imports, table des noms). k imports, 1 dylib (libSystem, lib_ordinal 1).
(fn batir_fixups (params (z Adr) (fxb Adr) (k Nat))
  (let base Adr (call zone_imports z))
  (let k4 Nat (mul k 4))
  (let sym_off Nat (add 80 k4))
  (call poser_petit fxb 0 0)
  (call poser_petit fxb 4 32)
  (call poser_petit fxb 8 80)
  (call poser_petit fxb 12 sym_off)
  (call poser_petit fxb 16 k)
  (call poser_petit fxb 20 1)
  (call poser_petit fxb 24 0)
  (call poser_petit fxb 32 4)
  (call poser_petit fxb 36 0)
  (call poser_petit fxb 40 0)
  (call poser_petit fxb 44 24)
  (call poser_petit fxb 48 0)
  (call poser_petit fxb 56 24)
  (let q60 Adr (offset fxb 60))
  (store8 q60 0x00)
  (let q61 Adr (offset fxb 61))
  (store8 q61 0x40)
  (let q62 Adr (offset fxb 62))
  (store8 q62 0x02)
  (let q63 Adr (offset fxb 63))
  (store8 q63 0x00)
  (call poser_petit fxb 64 0x4000)
  (call poser_petit fxb 68 0)
  (call poser_petit fxb 72 0)
  (let q76 Adr (offset fxb 76))
  (store8 q76 0x01)
  (let q77 Adr (offset fxb 77))
  (store8 q77 0x00)
  (let q78 Adr (offset fxb 78))
  (store8 q78 0x00)
  (let q79 Adr (offset fxb 79))
  (store8 q79 0x00)
  (let q_sym Adr (offset fxb sym_off))
  (store8 q_sym 0)
  (var pos Nat (add sym_off 1))
  (var noff Nat 1)
  (var j Nat 0)
  (loop imp
    (let fini Bool (eq j k))
    (if fini (then (break imp)))
    (let d Nat (mul j 64))
    (let e Adr (offset base d))
    (let ln Nat (load Nat e))
    (let noms_a Adr (offset e 8))
    (let dec Nat (shl noff 9))
    (let val Nat (bor 1 dec))
    (let j4 Nat (mul j 4))
    (let io Nat (add 80 j4))
    (call poser_petit fxb io val)
    (var w Nat 0)
    (loop cpn
      (let b Bool (eq w ln))
      (if b (then (break cpn)))
      (let pw Adr (offset noms_a w))
      (let c Nat (load8 pw))
      (let pf Adr (offset fxb pos))
      (store8 pf c)
      (set pos (add pos 1))
      (set w (add w 1))
      (end loop cpn))
    (let pz Adr (offset fxb pos))
    (store8 pz 0)
    (set pos (add pos 1))
    (let ln1 Nat (add ln 1))
    (set noff (add noff ln1))
    (set j (add j 1))
    (end loop imp))
  (end fn batir_fixups))
 
; batir_symtab : construit k nlist_64 (16 octets) puis la table des chaînes \0_sym0\0… dans stb. nlist = 16k.
(fn batir_symtab (params (z Adr) (stb Adr) (k Nat))
  (let base Adr (call zone_imports z))
  (let nlist Nat (mul k 16))
  (var noff Nat 1)
  (var j Nat 0)
  (loop nl
    (let fini Bool (eq j k))
    (if fini (then (break nl)))
    (let d Nat (mul j 64))
    (let e Adr (offset base d))
    (let ln Nat (load Nat e))
    (let o Nat (mul j 16))
    (call poser_petit stb o noff)
    (let o4 Nat (add o 4))
    (let q4 Adr (offset stb o4))
    (store8 q4 1)
    (let o5 Nat (add o 5))
    (let q5 Adr (offset stb o5))
    (store8 q5 0)
    (let o6 Nat (add o 6))
    (let q6 Adr (offset stb o6))
    (store8 q6 0)
    (let o7 Nat (add o 7))
    (let q7 Adr (offset stb o7))
    (store8 q7 1)
    (let ln1 Nat (add ln 1))
    (set noff (add noff ln1))
    (set j (add j 1))
    (end loop nl))
  (let q_nl Adr (offset stb nlist))
  (store8 q_nl 0)
  (var pos Nat (add nlist 1))
  (var m Nat 0)
  (loop st
    (let fin2 Bool (eq m k))
    (if fin2 (then (break st)))
    (let dd Nat (mul m 64))
    (let ee Adr (offset base dd))
    (let ln2 Nat (load Nat ee))
    (let noms_a Adr (offset ee 8))
    (var w Nat 0)
    (loop cpn
      (let b Bool (eq w ln2))
      (if b (then (break cpn)))
      (let pw Adr (offset noms_a w))
      (let c Nat (load8 pw))
      (let ps Adr (offset stb pos))
      (store8 ps c)
      (set pos (add pos 1))
      (set w (add w 1))
      (end loop cpn))
    (let pz Adr (offset stb pos))
    (store8 pz 0)
    (set pos (add pos 1))
    (set m (add m 1))
    (end loop st))
  (end fn batir_symtab))
 
; mise_en_page_lie : la mise en page d'un programme lié dynamiquement (au moins un call_extern). __TEXT en page 0
; (0x4000), __DATA_CONST,__got en page 0x4000, __LINKEDIT (fixups + symtab + signature) en page 0x8000.
(fn mise_en_page_lie (params (z Adr))
  (let pco Adr (call e_octets_code z))
  (let code Nat (load Nat pco))
  (let pk Adr (call e_nb_imports z))
  (let k Nat (load Nat pk))
  (let got_size Nat (mul k 8))
  (let noms Nat (call taille_noms_de z k))
  (let k4 Nat (mul k 4))
  (let base80 Nat (add 80 k4))
  (let fx_brut Nat (add base80 noms))
  (let fx_h Nat (add fx_brut 7))
  (let taille_fx Nat (band fx_h 0xfffffffffffffff8))
  (let nlist Nat (mul k 16))
  (let taille_st Nat (add nlist noms))
  (let symoff Nat (add 0x8000 taille_fx))
  (let stroff Nat (add symoff nlist))
  (let fx_st Nat (add taille_fx taille_st))
  (let apres Nat (add 0x8000 fx_st))
  (let cl_h Nat (add apres 15))
  (let codelimit Nat (band cl_h 0xfffffffffffffff0))
  (let pages Nat (shr codelimit 12))
  (let r12 Nat (band codelimit 0xfff))
  (let a_page Bool (ne r12 0))
  (var npg Nat pages)
  (if a_page (then (set npg (add npg 1))))
  (let cases Nat (mul npg 32))
  (let s Nat (add cases 0x76))
  (let s_h Nat (add s 15))
  (let sr Nat (band s_h 0xfffffffffffffff0))
  (let fin_sig Nat (add codelimit sr))
  (let linksize Nat (sub fin_sig 0x8000))
  (let vl_h Nat (add linksize 0x3fff))
  (let vmlink Nat (band vl_h 0xffffffffffffc000))
  (let afin Adr (call e_ligne_fin z))
  (let lfin Nat (load Nat afin))
  (call controler_chaine z lfin)
  ; suite du texte : __got, puis __LINKEDIT (fixups, symtab, signature)
  (call e_alignement_page z)
  (call emettre_got z k)
  (call e_alignement_page z)
  (let fxb Adr (offset z 0x60000))
  (call batir_fixups z fxb k)
  (call ecrire_octets z fxb taille_fx)
  (let stb Adr (offset z 0x70000))
  (call batir_symtab z stb k)
  (call ecrire_octets z stb taille_st)
  (call e_alignement_fin z)
  (let sig Adr (call zone_signature z))
  (let nsg Nat (size modele_signature))
  (let osg Nat (call lire_modele modele_signature nsg sig))
  (call poser_gros sig 0x4 s)
  (let s20 Nat (sub s 0x14))
  (call poser_gros sig 0x18 s20)
  (call poser_gros sig 0x30 npg)
  (call poser_gros sig 0x34 codelimit)
  (call poser_gros sig 0x60 0x4000)
  (call ecrire_octets z sig osg)
  (let zeros Adr (call zone_zeros z))
  (call ecrire_octets z zeros cases)
  (call e_alignement_fin z)
  ; en-tête lié, champs variables posés
  (let ent Adr (call zone_entete z))
  (let nel Nat (size modele_entete_lie))
  (let oe Nat (call lire_modele modele_entete_lie nel ent))
  (call poser_petit ent 0xd0 0x2f8)
  (call poser_petit ent 0xe0 0x2f8)
  (call poser_petit ent 0xd8 code)
  (call poser_petit ent 0x130 got_size)
  (call poser_petit ent 0x170 got_size)
  (call poser_petit ent 0x1b8 vmlink)
  (call poser_petit ent 0x1c8 linksize)
  (call poser_petit ent 0x1ec taille_fx)
  (call poser_petit ent 0x1f8 symoff)
  (call poser_petit ent 0x1fc k)
  (call poser_petit ent 0x200 stroff)
  (call poser_petit ent 0x204 noms)
  (call poser_petit ent 0x224 k)
  (call poser_petit ent 0x2e8 codelimit)
  (call poser_petit ent 0x2ec sr)
  (let e Adr (call e_ecriture z))
  (let fin_corps Adr (load Adr e))
  (let te Adr (call zone_texte_entete z))
  (store Adr e te)
  (call ecrire_octets z ent oe)
  (let fin_entete Adr (load Adr e))
  (let ne_texte Nat (diff fin_entete te))
  (call ecrire_tout z te ne_texte)
  (let texte Adr (call zone_texte z))
  (let nt Nat (diff fin_corps texte))
  (call ecrire_tout z texte nt)
  (end fn mise_en_page_lie))
 
(fn mise_en_page (params (z Adr))
  (let pnbi Adr (call e_nb_imports z))
  (let nbi Nat (load Nat pnbi))
  (let lie Bool (ne nbi 0))
  (if lie (then (call mise_en_page_lie z) (return)))
  (let pco Adr (call e_octets_code z))
  (let code Nat (load Nat pco))
  (let pdo Adr (call e_octets_donnees z))
  (let donnees Nat (load Nat pdo))
  (let cd Nat (add code donnees))
  (let t Nat (add cd 0x260))
  (let t_haut Nat (add t 0x3fff))
  (let l Nat (band t_haut 0xffffffffffffc000))
  (let pages Nat (shr l 12))
  (let n Nat (add pages 1))
  (let cases Nat (mul n 32))
  (let s Nat (add cases 0x76))
  (let s_haut Nat (add s 15))
  (let sr Nat (band s_haut 0xfffffffffffffff0))
  ; limites de la chaîne, contrôlées une dernière fois (les étiquettes de la fin du texte comprises), à la ligne
  ; de la fin du fichier ; celles qu'une fonction ou une donnée franchit l'ont été à sa ligne (corps)
  (let afin Adr (call e_ligne_fin z))
  (let lfin Nat (load Nat afin))
  (call controler_chaine z lfin)
  (let l40 Nat (add l 0x40))
  ; fin du texte : alignement de __LINKEDIT, correctifs vides, signature à cases nulles
  (call e_alignement_page z)
  (let correctifs Adr (call zone_correctifs z))
  (let nc Nat (size modele_linkedit))
  (let oc Nat (call lire_modele modele_linkedit nc correctifs))
  (call ecrire_octets z correctifs oc)
  (let sig Adr (call zone_signature z))
  (let ns Nat (size modele_signature))
  (let os Nat (call lire_modele modele_signature ns sig))
  (call poser_gros sig 0x4 s)
  (let s20 Nat (sub s 0x14))
  (call poser_gros sig 0x18 s20)
  (call poser_gros sig 0x30 n)
  (call poser_gros sig 0x34 l40)
  (call poser_gros sig 0x60 l)
  (call ecrire_octets z sig os)
  (let zeros Adr (call zone_zeros z))
  (call ecrire_octets z zeros cases)
  (call e_alignement_fin z)
  ; en-tête Mach-O : le modèle, et les champs que la mise en page fixe (petit-boutistes)
  (let ent Adr (call zone_entete z))
  (let n_entete Nat (size modele_entete))
  (let oe Nat (call lire_modele modele_entete n_entete ent))
  (call poser_petit ent 0x88 l)
  (call poser_petit ent 0x98 l)
  (let taille_text Nat (sub t 0x260))
  (call poser_petit ent 0xd8 taille_text)
  (call poser_petit ent 0x118 l)
  (call poser_petit ent 0x128 l)
  (let sr40 Nat (add sr 0x40))
  (call poser_petit ent 0x130 sr40)
  ; vmsize de __LINKEDIT : sa taille arrondie à 16 Kio (0x4000 du modèle tant que la signature tient en 16 Kio)
  (let sr40_haut Nat (add sr40 0x3fff))
  (let vl Nat (band sr40_haut 0xffffffffffffc000))
  (call poser_petit ent 0x120 vl)
  (call poser_petit ent 0x150 l)
  (let l38 Nat (add l 0x38))
  (call poser_petit ent 0x168 l38)
  (call poser_petit ent 0x250 l40)
  (call poser_petit ent 0x254 sr)
  ; le texte de l'en-tête à part, puis l'écriture : l'en-tête, puis tout le reste
  (let e Adr (call e_ecriture z))
  (let fin_corps Adr (load Adr e))
  (let te Adr (call zone_texte_entete z))
  (store Adr e te)
  (call ecrire_octets z ent oe)
  (let fin_entete Adr (load Adr e))
  (let ne_texte Nat (diff fin_entete te))
  (call ecrire_tout z te ne_texte)
  (let texte Adr (call zone_texte z))
  (let nt Nat (diff fin_corps texte))
  (call ecrire_tout z texte nt)
  (end fn mise_en_page))
 
; ---------------------------------------------------------------------------
; main : la zone de travail, mmap(0, 0x32900000, lecture et écriture, privée et anonyme, -1, 0), puis les étapes
 
(fn main (params) (returns Nat)
  (let r Ent (syscall 197 0 0x32900000 3 4098 -1 0))
  (let echec Bool (lt r 0))
  (if echec (then (return 1)))
  (let n Nat (to_nat r))
  (let z Adr (adr_of n))
  (let af Adr (call e_ligne_fin z))
  (store Nat af 1)
  (call lire_source z)
  (call lire_jetons z)
  (call lire_entetes z)
  (call verifier_main z)
  (let texte Adr (call zone_texte z))
  (let e Adr (call e_ecriture z))
  (store Adr e texte)
  (call corps z)
  (call mise_en_page z)
  (return 0)
  (end fn main))