This repository has been archived by the owner on Nov 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
disassembler.rkt
61 lines (54 loc) · 2.36 KB
/
disassembler.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#lang typed/racket
(require (submod "types.rkt" common))
(require (submod "types.rkt" evm-assembly))
(require "serializer.rkt")
(require "utils.rkt")
(require "globals.rkt")
(require (submod "typed.rkt" binaryio))
(require (submod "typed.rkt" dict))
(provide (all-defined-out))
(: disassemble-one (-> Bytes 0..∞ EthInstruction))
(define (disassemble-one bs i)
(let* ([ byte (cast (bytes-or-zero bs i 1) Byte)])
(if (hash-has-key? opcodes-by-byte byte)
(disassemble-opcode bs i (hash-ref opcodes-by-byte byte))
(evm-bytes (bytes byte)))))
(: disassemble-opcode (-> Bytes Integer opcode EthInstruction))
(define (disassemble-opcode bs i op)
(cond ((push-op? op) (disassemble-push bs i))
(else (evm-op (opcode-name op)))
))
(: disassemble-push (-> Bytes Integer EthInstruction))
(define (disassemble-push bs i)
(let ([ op (hash-ref opcodes-by-byte (bytes-ref bs i)) ])
(evm-push (op-extra-size op)
(bytes->integer bs
#f ; signed?
#t ; big-endian
(+ i 1) ; start position
(+ i 1 (op-extra-size op)))))) ; end
; Outputs 3 column TSV
(: print-disassembly (-> Bytes Void))
(define (print-disassembly bs)
(let ((reverse-symbol-table (invert-hash (*symbol-table*))))
(: loop (-> 0..∞ Void))
(define (loop n)
(if (>= n (- (bytes-length bs) 1))
(void)
(begin
(printf "~x" n)
(write-char #\tab)
(display (reverse-symbol-name reverse-symbol-table (assert-0..∞ (- n (*loader-size*)))))
;; (print `(,(bytes-ref bs n)
;; ,(push-op? (hash-ref opcodes-by-byte (bytes-ref bs n)))
;; ,(op-extra-size (hash-ref opcodes-by-byte (bytes-ref bs n)))))
(write-char #\tab)
(let ([ ethi (disassemble-one bs n) ])
(match ethi
[(struct evm-push (size value)) (printf "Push ~a 0x~x" size value )]
[(struct evm-op (sym) ) (write-string (symbol->string sym))]
[(struct evm-bytes (bs) ) (printf "BYTES ~a" bs )]
[_ (error "print-disassembly: Unknown ethi" ethi )])
(newline)
(loop (+ n (instruction-size ethi)))))))
(loop 0)))