OSDN Git Service

2003-06-10 Dave Brolley <brolley@redhat.com>
[pf3gnuchains/pf3gnuchains4x.git] / cgen / desc.scm
1 ; General cpu info generator support.
2 ; Copyright (C) 2000 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: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                                 (elm-get self 'prefix) (car e) ; operand name
126                                 "\", "
127                                 (if (string? (cadr e))
128                                     (cadr e)
129                                     (number->string (cadr e))) ; value
130                                 ", {0, {0}}, 0, 0"
131                                 " },\n"
132                                 ))
133                              (elm-get self 'values)))
134     "\n};\n\n"
135     "CGEN_KEYWORD "
136     (gen-hw-asm-ref (elm-get self 'name))
137     " =\n{\n"
138     "  & " (gen-hw-asm-ref (elm-get self 'name)) "_entries[0],\n"
139     "  " (number->string (length (elm-get self 'values))) ",\n"
140     "  0, 0, 0, 0, \"\"\n"
141     "};\n\n"
142     )
143    )
144 )
145
146 ; Return a reference to a keyword table.
147
148 (method-make!
149  <keyword> 'gen-ref
150  (lambda (self) (string-append "& " (gen-hw-asm-ref (elm-get self 'name))))
151 )
152
153 (method-make!
154  <keyword> 'gen-table-entry
155  (lambda (self)
156    (string-append "CGEN_ASM_KEYWORD, (PTR) " (send self 'gen-ref) ", "))
157 )
158
159 ; Return the C code to initialize a keyword.
160 ; If the `hash' attr is present, the values are hashed.  Currently this is
161 ; done by calling back to GAS to have it add the registers to its symbol table.
162 ; FIXME: Currently unused.  Should be done either in the open routine or
163 ; lazily upon lookup.
164
165 (method-make!
166  <keyword> 'gen-init
167  (lambda (self)
168    (cond ((has-attr? self 'HASH)
169           (string-append
170            "  @arch@_cgen_asm_hash_keywords ("
171            (send self 'gen-ref)
172            ");\n"
173            ))
174          (else ""))
175    )
176 )
177 \f
178 ; Operand support.
179
180 ; Return a reference to the operand's attributes.
181
182 (method-make!
183  <operand> 'gen-attr-ref
184  (lambda (self)
185    (string-append "& CGEN_OPERAND_ATTRS (CGEN_SYM (operand_table)) "
186                   "[" (op-enum self) "]"))
187 )
188
189 ; Name of C variable that is a pointer to the fields struct.
190
191 (define ifields-var "fields")
192
193 ; Given FIELD, an `ifield' object, return an lvalue for the operand in
194 ; IFIELDS-VAR.
195
196 (define (gen-operand-result-var field)
197   (string-append ifields-var "->" (gen-sym field))
198 )
199 \f
200 ; Basic description init,finish,analyzer support.
201
202 ; Return a boolean indicating if all insns have a constant mnemonic
203 ; (ie: no $'s in insn's name in `syntax' field).
204 ; If constant, one can build the assembler hash table using the entire
205 ; mnemonic.
206
207 (define (constant-mnemonics?)
208   #f ; FIXME
209 )
210
211 ; Initialize any "desc" specific things before loading the .cpu file.
212 ; N.B. Since "desc" is always a part of another application, that
213 ; application's init! routine must call this one.
214
215 (define (desc-init!)
216   *UNSPECIFIED*
217 )
218
219 ; Finish any "desc" specific things after loading the .cpu file.
220 ; This is separate from analyze-data! as cpu-load performs some
221 ; consistency checks in between.
222 ; N.B. Since "desc" is always a part of another application, that
223 ; application's finish! routine must call this one.
224
225 (define (desc-finish!)
226   *UNSPECIFIED*
227 )
228
229 ; Compute various needed globals and assign any computed fields of
230 ; the various objects.  This is the standard routine that is called after
231 ; a .cpu file is loaded.
232 ; N.B. Since "desc" is always a part of another application, that
233 ; application's analyze! routine must call this one.
234
235 (define (desc-analyze!)
236   (set! strip-mnemonic? (constant-mnemonics?))
237
238   *UNSPECIFIED*
239 )