OSDN Git Service

Merge branch 'master' of git://github.com/monaka/binutils
[pf3gnuchains/pf3gnuchains4x.git] / cgen / desc.scm
1 ; General cpu info generator support.
2 ; Copyright (C) 2000, 2003, 2009 Red Hat, Inc.
3 ; This file is part of CGEN.
4 ;
5 ; This file generates C versions of the more salient parts of the description
6 ; file.  It's currently part of opcodes or simulator support,
7 ; and doesn't exist as its own "application" (i.e. user of cgen),
8 ; though that's not precluded.
9
10 ; strip-mnemonic?: If each mnemonic is constant, the insn table doesn't need
11 ; to record them in the syntax field as the mnemonic field also contains it.
12 ; Furthermore, the insn table can be hashed on complete mnemonic.
13 ; ??? Should live in <derived-arch-data> or some such.
14
15 (define strip-mnemonic? #f)
16 \f
17 ; Attribute support code.
18
19 (define (gen-attr-table-defn type attr-list)
20   (string-append
21    "const CGEN_ATTR_TABLE "
22    "@arch@_cgen_" type "_attr_table[] =\n{\n"
23    (string-map (lambda (attr)
24                  (gen-obj-sanitize
25                   attr
26                   (string-append "  { "
27                                  "\""
28                                  (string-upcase (obj:str-name attr))
29                                  "\", "
30                                  (if (class-instance? <boolean-attribute> attr)
31                                      "&bool_attr[0], &bool_attr[0]"
32                                      (string-append "& " (gen-sym attr)
33                                                     "_attr[0], & "
34                                                     (gen-sym attr)
35                                                     "_attr[0]"))
36                                  " },\n")))
37                attr-list)
38    "  { 0, 0, 0 }\n"
39    "};\n\n")
40 )
41
42 (define (gen-attr-table-defns)
43   (logit 2 "Generating attribute table defns ...\n")
44   (string-append
45    "\
46 /* Attributes.  */
47
48 static const CGEN_ATTR_ENTRY bool_attr[] =
49 {
50   { \"#f\", 0 },
51   { \"#t\", 1 },
52   { 0, 0 }
53 };
54
55 "
56    ; Generate tables mapping names to values for all the non-boolean attrs.
57    (string-map gen-defn (current-attr-list))
58    ; Generate tables for each domain (ifld, insn, etc.) mapping attribute type
59    ; to index.
60    (gen-attr-table-defn "ifield" (current-ifld-attr-list))
61    (gen-attr-table-defn "hardware" (current-hw-attr-list))
62    (gen-attr-table-defn "operand" (current-op-attr-list))
63    (gen-attr-table-defn "insn" (current-insn-attr-list))
64    )
65 )
66 \f
67 ; HW-ASM is the base class for supporting hardware elements in the opcode table
68 ; (aka assembler/disassembler).
69
70 ; Return the C declaration.
71 ; It is up to a derived class to redefine this as necessary.
72
73 (method-make! <hw-asm> 'gen-decl (lambda (self) ""))
74
75 ; Return the C definition.
76 ; It is up to a derived class to redefine this as necessary.
77
78 (method-make! <hw-asm> 'gen-defn (lambda (self) ""))
79
80 (method-make! <hw-asm> 'gen-ref (lambda (self) "0"))
81
82 (method-make! <hw-asm> 'gen-init (lambda (self) ""))
83
84 (method-make! <hw-asm> 'gen-table-entry (lambda (self) "CGEN_ASM_NONE, 0, "))
85
86 ; Prefix of global variables describing operand values.
87
88 (define hw-asm-prefix "@arch@_cgen_opval_")
89
90 ; Emit a C reference to a value operand.
91 ; Usually the operand's details are stored in a struct so in the default
92 ; case return that struct (?correct?).  The caller must add the "&" if desired.
93
94 (define (gen-hw-asm-ref name)
95   (string-append hw-asm-prefix (gen-c-symbol name))
96 )
97 \f
98 ; Keyword support.
99
100 ; Keyword operands.
101 ; Return the C declaration of a keyword list.
102
103 (method-make!
104  <keyword> 'gen-decl
105  (lambda (self)
106    (string-append
107     "extern CGEN_KEYWORD "
108     (gen-hw-asm-ref (elm-get self 'name))
109     ";\n"))
110 )
111
112 ; Return the C definition of a keyword list.
113
114 (method-make!
115  <keyword> 'gen-defn
116  (lambda (self)
117    (string-append
118     "static CGEN_KEYWORD_ENTRY "
119     (gen-hw-asm-ref (elm-get self 'name)) "_entries"
120     "[] =\n{\n"
121     (string-drop -2 ; Delete trailing ",\n" [don't want the ,]
122                  (string-map (lambda (e)
123                                (string-append
124                                 "  { \""
125                                 (->string (elm-get self 'name-prefix))
126                                 (->string (car e)) ; operand name
127                                 "\", "
128                                 (if (string? (cadr e))
129                                     (cadr e)
130                                     (number->string (cadr e))) ; value
131                                 ", {0, {{{0, 0}}}}, 0, 0"
132                                 " },\n"
133                                 ))
134                              (elm-get self 'values)))
135     "\n};\n\n"
136     "CGEN_KEYWORD "
137     (gen-hw-asm-ref (elm-get self 'name))
138     " =\n{\n"
139     "  & " (gen-hw-asm-ref (elm-get self 'name)) "_entries[0],\n"
140     "  " (number->string (length (elm-get self 'values))) ",\n"
141     "  0, 0, 0, 0, \"\"\n"
142     "};\n\n"
143     )
144    )
145 )
146
147 ; Return a reference to a keyword table.
148
149 (method-make!
150  <keyword> 'gen-ref
151  (lambda (self) (string-append "& " (gen-hw-asm-ref (elm-get self 'name))))
152 )
153
154 (method-make!
155  <keyword> 'gen-table-entry
156  (lambda (self)
157    (string-append "CGEN_ASM_KEYWORD, (PTR) " (send self 'gen-ref) ", "))
158 )
159
160 ; Return the C code to initialize a keyword.
161 ; If the `hash' attr is present, the values are hashed.  Currently this is
162 ; done by calling back to GAS to have it add the registers to its symbol table.
163 ; FIXME: Currently unused.  Should be done either in the open routine or
164 ; lazily upon lookup.
165
166 (method-make!
167  <keyword> 'gen-init
168  (lambda (self)
169    (cond ((has-attr? self 'HASH)
170           (string-append
171            "  @arch@_cgen_asm_hash_keywords ("
172            (send self 'gen-ref)
173            ");\n"
174            ))
175          (else ""))
176    )
177 )
178 \f
179 ; Operand support.
180
181 ; Return a reference to the operand's attributes.
182
183 (method-make!
184  <operand> 'gen-attr-ref
185  (lambda (self)
186    (string-append "& CGEN_OPERAND_ATTRS (CGEN_SYM (operand_table)) "
187                   "[" (op-enum self) "]"))
188 )
189
190 ; Name of C variable that is a pointer to the fields struct.
191
192 (define ifields-var "fields")
193
194 ; Given FIELD, an `ifield' object, return an lvalue for the operand in
195 ; IFIELDS-VAR.
196
197 (define (gen-operand-result-var field)
198   (string-append ifields-var "->" (gen-sym field))
199 )
200 \f
201 ; Basic description init,finish,analyzer support.
202
203 ; Return a boolean indicating if all insns have a constant mnemonic
204 ; (ie: no $'s in insn's name in `syntax' field).
205 ; If constant, one can build the assembler hash table using the entire
206 ; mnemonic.
207
208 (define (constant-mnemonics?)
209   #f ; FIXME
210 )
211
212 ; Initialize any "desc" specific things before loading the .cpu file.
213 ; N.B. Since "desc" is always a part of another application, that
214 ; application's init! routine must call this one.
215
216 (define (desc-init!)
217   *UNSPECIFIED*
218 )
219
220 ; Finish any "desc" specific things after loading the .cpu file.
221 ; This is separate from analyze-data! as cpu-load performs some
222 ; consistency checks in between.
223 ; N.B. Since "desc" is always a part of another application, that
224 ; application's finish! routine must call this one.
225
226 (define (desc-finish!)
227   *UNSPECIFIED*
228 )
229
230 ; Compute various needed globals and assign any computed fields of
231 ; the various objects.  This is the standard routine that is called after
232 ; a .cpu file is loaded.
233 ; N.B. Since "desc" is always a part of another application, that
234 ; application's analyze! routine must call this one.
235
236 (define (desc-analyze!)
237   (set! strip-mnemonic? (constant-mnemonics?))
238
239   *UNSPECIFIED*
240 )