1 |
2 |
tarookumic |
; Konrad Eisele <eiselekd@web.de>
|
2 |
|
|
; cdef_lib_i1.el: Some c printing functions
|
3 |
|
|
;-----------------------------------------------------------------------
|
4 |
|
|
|
5 |
|
|
; binary list to hex string
|
6 |
|
|
; (conv-hex-c '(0 0 1 1 1 ))
|
7 |
|
|
; (conv-hex-c (make-list 10 1))
|
8 |
|
|
(defun conv-hex-c (k)
|
9 |
|
|
(let ((e 0)
|
10 |
|
|
(i 1)
|
11 |
|
|
(v 0))
|
12 |
|
|
(dolist (e k v)
|
13 |
|
|
(if (eq e 1)
|
14 |
|
|
(setq v (+ v i))
|
15 |
|
|
)
|
16 |
|
|
(setq i (* i 2))
|
17 |
|
|
)
|
18 |
|
|
(format "0x%x" v)
|
19 |
|
|
)
|
20 |
|
|
)
|
21 |
|
|
|
22 |
|
|
(defun print-decode-c-hash (dec depth)
|
23 |
|
|
(cond
|
24 |
|
|
((hash-table-p dec)
|
25 |
|
|
(let ((p ""))
|
26 |
|
|
( maphash (function (lambda (k v)
|
27 |
|
|
(let ((p2 ""))
|
28 |
|
|
(setq p2 (concat p2 (space-string (- depth 1) " ") "case " (conv-hex-c k) ":\n" (space-string (- depth 1) " ") "{" ))
|
29 |
|
|
(if (is-decoder v)
|
30 |
|
|
(setq p2 (concat p2 "\n" (print-decoder-c v depth)))
|
31 |
|
|
(setq p2 (concat p2 "return init_" (mapconcat 'symbol-name v "," ) "(insn,s);\n"))
|
32 |
|
|
)
|
33 |
|
|
(setq p (concat p p2 (space-string (- depth 1) " ") "};break;\n"))
|
34 |
|
|
))) dec )
|
35 |
|
|
`,p
|
36 |
|
|
))
|
37 |
|
|
((listp dec)
|
38 |
|
|
(mapconcat 'print-list dec " ")
|
39 |
|
|
)
|
40 |
|
|
((t) ('"?")))
|
41 |
|
|
)
|
42 |
|
|
|
43 |
|
|
(defun print-insnrange-c (l r)
|
44 |
|
|
(concat "((insn>>" (number-to-string r) ")&" (conv-hex-c (make-list (+ (- l r) 1) 1)) ")")
|
45 |
|
|
)
|
46 |
|
|
|
47 |
|
|
(defun print-decoder-c (d depth)
|
48 |
|
|
(let ((l (- 31 (nth 1 d)))
|
49 |
|
|
(r (+ (- 31 (nth 2 d)) 1))
|
50 |
|
|
(dec (nth 3 d))
|
51 |
|
|
(def (nth 4 d))
|
52 |
|
|
(p ""))
|
53 |
|
|
(if (not (and (eq l 31) (eq r 32)))
|
54 |
|
|
(progn
|
55 |
|
|
(setq p (concat p (space-string depth " ") "{\n" (space-string depth " ") "switch (" (print-insnrange-c l r) ") { \n" ))
|
56 |
|
|
(setq p (concat p (print-decode-c-hash dec (+ 1 depth) ) (space-string depth " ") "}}\n" ))
|
57 |
|
|
)
|
58 |
|
|
)
|
59 |
|
|
(if (eq (length def) 5)
|
60 |
|
|
(if (and (eq (nth 0 def) 0) (eq (nth 1 def) 0) (> (length (nth 4 def)) 0))
|
61 |
|
|
(setq p (concat p (space-string depth " ") "/*default:*/ return init_" (mapconcat 'symbol-name (nth 4 def) " ") "(insn,s);\n" ))
|
62 |
|
|
(setq p (concat p (space-string depth " ") "/*default:*/\n" (print-decoder-c def depth)))
|
63 |
|
|
)
|
64 |
|
|
)
|
65 |
|
|
`,p
|
66 |
|
|
)
|
67 |
|
|
)
|
68 |
|
|
|
69 |
|
|
(defun print-decoder-c-pre (d)
|
70 |
|
|
(let ((v ""))
|
71 |
|
|
(setq v (print-decoder-c d 0))
|
72 |
|
|
(setq v (concat "unsigned int decode(unsigned int insn, insn_union *s) {\n" v "\n};"))
|
73 |
|
|
`,v
|
74 |
|
|
)
|
75 |
|
|
)
|
76 |
|
|
|
77 |
|
|
;-----------------------------------------------------------------------
|
78 |
|
|
|
79 |
|
|
(defun retrive-help (insn-help entry)
|
80 |
|
|
(let ((v "")
|
81 |
|
|
(e '()))
|
82 |
|
|
(dolist (e insn-help v)
|
83 |
|
|
(if (eq (symbol-name (nth 0 e)) entry)
|
84 |
|
|
(setq v (nth 1 e))
|
85 |
|
|
)
|
86 |
|
|
)
|
87 |
|
|
)
|
88 |
|
|
)
|
89 |
|
|
|
90 |
|
|
(defun print-decoder-c-struct-rec (a)
|
91 |
|
|
(if (> (length a) 0)
|
92 |
|
|
(let ((n (count-bits a))
|
93 |
|
|
(e (nth 0 a)))
|
94 |
|
|
(append (print-decoder-c-struct-rec (nthcdr n a)) `((,n ,e)))
|
95 |
|
|
)
|
96 |
|
|
'()
|
97 |
|
|
)
|
98 |
|
|
)
|
99 |
|
|
|
100 |
|
|
(defun print-decoder-c-structs (insn insn-help)
|
101 |
|
|
(let ((a 0)
|
102 |
|
|
(v1 "")
|
103 |
|
|
(v2 "")
|
104 |
|
|
(v3 "typedef union _insn_union {\n")
|
105 |
|
|
(v4 ""))
|
106 |
|
|
(dolist (a insn)
|
107 |
|
|
(let ((e 0)
|
108 |
|
|
(v '())
|
109 |
|
|
(l 31)
|
110 |
|
|
(r 32))
|
111 |
|
|
(setq v (reverse (print-decoder-c-struct-rec (symbol-value a))))
|
112 |
|
|
(setq v1 (concat v1 "typedef struct _" (symbol-name a) "_struct {\n/*" (retrive-help insn-help (symbol-name a)) "*/\n int (*func) (struct _" (symbol-name a) "_struct *s, proc_state *state);\n"))
|
113 |
|
|
(setq v2 (concat v2 "unsigned int init_" (symbol-name a) "(unsigned int insn, insn_union *s) {\n" "s->"(symbol-name a) ".func=func_" (symbol-name a) ";\n" ))
|
114 |
|
|
(setq v3 (concat v3 " " (symbol-name a) "_struct " (symbol-name a) ";\n"))
|
115 |
|
|
(setq v4 (concat v4 "int func_" (symbol-name a) " (struct _" (symbol-name a) "_struct *s, proc_state *state) {\n/*" (retrive-help insn-help (symbol-name a)) "*/\n};\n"))
|
116 |
|
|
(dolist (e v)
|
117 |
|
|
(setq r (- r (nth 0 e)))
|
118 |
|
|
(if (not (or (eq (nth 1 e) 1) (eq (nth 1 e) 0)))
|
119 |
|
|
(progn
|
120 |
|
|
(setq v1 (concat v1 " unsigned int " (print-list (nth 1 e)) ": " (number-to-string (+ (- l r) 2)) "; /*" (retrive-help insn-help (print-list (nth 1 e))) "*/\n" ))
|
121 |
|
|
(setq v2 (concat v2 "s->"(symbol-name a) "." (print-list (nth 1 e)) "=" (print-insnrange-c l r ) ";\n" ))
|
122 |
|
|
)
|
123 |
|
|
)
|
124 |
|
|
(setq l (- r 1));
|
125 |
|
|
)
|
126 |
|
|
(setq v1 (concat v1 "} " (symbol-name a) "_struct;\n"))
|
127 |
|
|
(setq v2 (concat v2 " return 1;};\n "))
|
128 |
|
|
)
|
129 |
|
|
)
|
130 |
|
|
|
131 |
|
|
|
132 |
|
|
(setq v3 (concat v3 "} insn_union;\n "))
|
133 |
|
|
(concat "typedef struct _proc_state {} proc_state;\n" v1 v3 v4 v2 )
|
134 |
|
|
)
|
135 |
|
|
)
|
136 |
|
|
|
137 |
|
|
|
138 |
|
|
;-----------------------------------------------------------------------
|
139 |
|
|
|