|
| 1 | +import Std.Data.HashMap |
| 2 | + |
| 3 | +open Lean Syntax Macro Std |
| 4 | + |
| 5 | +structure CPU where |
| 6 | + regs : Vector Int64 10 |
| 7 | + deriving Repr |
| 8 | + |
| 9 | +structure Program where |
| 10 | + labels : HashMap String Nat |
| 11 | + ops : Array (List String) |
| 12 | + |
| 13 | +private def getStr (id : TSyntax `ident) : String := |
| 14 | + id.getId.toString.toLower |
| 15 | + |
| 16 | +private def regIdx (reg : String) : Fin 10 := |
| 17 | + match reg with |
| 18 | + | "rdi" => 0 |
| 19 | + | "rsi" => 1 |
| 20 | + | "rdx" => 2 |
| 21 | + | "rcx" => 3 |
| 22 | + | "r8" => 4 |
| 23 | + | "r9" => 5 |
| 24 | + | "r10" => 6 |
| 25 | + | "r11" => 7 |
| 26 | + | "rax" => 8 |
| 27 | + | "rflags" => 9 |
| 28 | + | _ => panic! "invalid register" |
| 29 | + |
| 30 | +private def getVal (cpu : CPU) (reg : String) : Int64 := |
| 31 | + match reg.toInt? with |
| 32 | + | some n => n.toInt64 |
| 33 | + | none => cpu.regs[regIdx reg]! |
| 34 | + |
| 35 | +private def unFlag : Int64 := 1 |
| 36 | +private def eqFlag : Int64 := (1 : Int64) <<< 6 |
| 37 | +private def gtFlag : Int64 := (1 : Int64) <<< 5 |
| 38 | +private def ltFlag : Int64 := (1 : Int64) <<< 4 |
| 39 | + |
| 40 | +private def compareRegs (dest src : Int64) : Int64 := Id.run do |
| 41 | + let mut acc : Int64 := 0 |
| 42 | + acc := acc ||| ((dest == src).toInt64 <<< 6) -- equal |
| 43 | + acc := acc ||| ((decide (dest > src)).toInt64 <<< 5) -- greater |
| 44 | + acc := acc ||| ((decide (dest < src)).toInt64 <<< 4) -- lesser |
| 45 | + return acc |
| 46 | + |
| 47 | +private def dispatchCondJmp (cpu : CPU) (dest : String) (labels : Std.HashMap String Nat) (flag : Int64) (RIP : Nat) : Nat := |
| 48 | + if flag == unFlag || getVal cpu "rflags" &&& flag != 0 then |
| 49 | + if h₀: dest ∈ labels then |
| 50 | + labels[dest] + 1 |
| 51 | + else panic! "Invalid label" |
| 52 | + else RIP + 1 |
| 53 | + |
| 54 | +declare_syntax_cat x86_dest |
| 55 | +syntax ident : x86_dest |
| 56 | + |
| 57 | +declare_syntax_cat x86_src |
| 58 | +syntax ident : x86_src |
| 59 | +syntax num : x86_src |
| 60 | +syntax "-" num : x86_src |
| 61 | + |
| 62 | +declare_syntax_cat x86_inst |
| 63 | +syntax ident x86_dest "," x86_src : x86_inst |
| 64 | +syntax ident x86_dest : x86_inst |
| 65 | +syntax ident ":" : x86_inst |
| 66 | + |
| 67 | +syntax "assemble!(" (x86_inst)* ")" : term |
| 68 | +syntax term "(" (term),* ")" : term |
| 69 | + |
| 70 | +private def Program.evaluate (args : Array Int64) (program : Program) : Int64 := Id.run do |
| 71 | + let regs := (Array.finRange 6).foldl (init := Vector.replicate 10 0) fun acc i => |
| 72 | + acc.set i (args[i]?.getD 0) |
| 73 | + |
| 74 | + let mut acc : CPU := { regs } |
| 75 | + let mut i := 0 |
| 76 | + |
| 77 | + while h: i < program.ops.size do |
| 78 | + match program.ops[i] with |
| 79 | + | [op, dest, src] => |
| 80 | + i := i + 1 |
| 81 | + let srcVal := getVal acc src |
| 82 | + let destVal := getVal acc dest |
| 83 | + match op with |
| 84 | + | "mov" => acc := { acc with regs := acc.regs.set (regIdx dest) srcVal } |
| 85 | + | "add" => acc := { acc with regs := acc.regs.set (regIdx dest) (destVal + srcVal) } |
| 86 | + | "sub" => acc := { acc with regs := acc.regs.set (regIdx dest) (destVal - srcVal) } |
| 87 | + | "xor" => acc := { acc with regs := acc.regs.set (regIdx dest) (destVal ^^^ srcVal) } |
| 88 | + | "and" => acc := { acc with regs := acc.regs.set (regIdx dest) (destVal &&& srcVal) } |
| 89 | + | "or" => acc := { acc with regs := acc.regs.set (regIdx dest) (destVal ||| srcVal) } |
| 90 | + | "shl" => acc := { acc with regs := acc.regs.set (regIdx dest) (destVal <<< srcVal) } |
| 91 | + | "shr" => acc := { acc with regs := acc.regs.set (regIdx dest) (destVal >>> srcVal) } |
| 92 | + | "mul" => acc := { acc with regs := acc.regs.set (regIdx dest) (destVal * srcVal) } |
| 93 | + | "div" => acc := { acc with regs := acc.regs.set (regIdx dest) (destVal / srcVal) } |
| 94 | + | "cmp" => acc := { acc with regs := acc.regs.set (regIdx "rflags") (compareRegs destVal srcVal) } |
| 95 | + | _ => continue |
| 96 | + | [op, dest] => |
| 97 | + match op with |
| 98 | + | "jmp" => i := dispatchCondJmp acc dest program.labels unFlag i |
| 99 | + | "je" => i := dispatchCondJmp acc dest program.labels eqFlag i |
| 100 | + | "jl" => i := dispatchCondJmp acc dest program.labels ltFlag i |
| 101 | + | "jg" => i := dispatchCondJmp acc dest program.labels gtFlag i |
| 102 | + | _ => i := i + 1 |
| 103 | + | _ => i := i + 1 |
| 104 | + |
| 105 | + return acc.regs.get $ regIdx "rax" |
| 106 | + |
| 107 | +macro_rules |
| 108 | + | `(assemble!( $[$insts]* )) => do |
| 109 | + let mut labelNames : Array (TSyntax `term) := #[] |
| 110 | + let mut labelIndices : Array (TSyntax `term) := #[] |
| 111 | + let mut ops : Array (TSyntax `term) := #[] |
| 112 | + |
| 113 | + for i in [:insts.size] do |
| 114 | + match insts[i]! with |
| 115 | + | `(x86_inst| $opId:ident $dest:x86_dest , $src:x86_src) => |
| 116 | + match dest with |
| 117 | + | `(x86_dest| $destId:ident) => |
| 118 | + match src with |
| 119 | + | `(x86_src| $srcId:ident) => |
| 120 | + let opStr := mkStrLit $ getStr opId |
| 121 | + let destStr := mkStrLit $ getStr destId |
| 122 | + let srcStr := mkStrLit $ getStr srcId |
| 123 | + ops := ops.push (← `([$opStr, $destStr, $srcStr])) |
| 124 | + | `(x86_src| $srcN:num) => |
| 125 | + let opStr := mkStrLit $ getStr opId |
| 126 | + let destStr := mkStrLit $ getStr destId |
| 127 | + let srcStr := mkStrLit $ s!"{srcN.getNat}" |
| 128 | + ops := ops.push (← `([$opStr, $destStr, $srcStr])) |
| 129 | + | `(x86_src| -$srcN:num) => |
| 130 | + let opStr := mkStrLit $ getStr opId |
| 131 | + let destStr := mkStrLit $ getStr destId |
| 132 | + let srcStr := mkStrLit $ s!"-{srcN.getNat}" |
| 133 | + ops := ops.push (← `([$opStr, $destStr, $srcStr])) |
| 134 | + | _ => throwUnsupported |
| 135 | + | _ => throwUnsupported |
| 136 | + | `(x86_inst| $opId:ident $dest:x86_dest) => |
| 137 | + match dest with |
| 138 | + | `(x86_dest| $destId:ident) => |
| 139 | + let opStr := mkStrLit $ getStr opId |
| 140 | + let destStr := mkStrLit destId.getId.toString |
| 141 | + ops := ops.push (← `([$opStr, $destStr])) |
| 142 | + | _ => throwUnsupported |
| 143 | + | `(x86_inst| $labelId:ident :) => |
| 144 | + labelNames := labelNames.push (mkStrLit labelId.getId.toString) |
| 145 | + labelIndices := labelIndices.push (mkNumLit s!"{i}") |
| 146 | + ops := ops.push (← `([])) |
| 147 | + | _ => throwUnsupported |
| 148 | + |
| 149 | + `(({ |
| 150 | + ops := #[$[$ops],*], |
| 151 | + labels := HashMap.ofList [ $[ ($labelNames, $labelIndices) ],* ] |
| 152 | + } : Program) |
| 153 | + ) |
| 154 | + | `($program( $[$args],* )) => do |
| 155 | + let args' ← args.mapM fun a => `(($a : Int64)) |
| 156 | + let result ← `(Program.evaluate #[$[$args'],*] $program) |
| 157 | + `(($result)) |
0 commit comments