1 ; General cpu info generator support.
2 ; Copyright (C) 2000, 2003, 2009 Red Hat, Inc.
3 ; This file is part of CGEN.
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.
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.
15 (define strip-mnemonic? #f)
17 ; Attribute support code.
19 (define (gen-attr-table-defn type attr-list)
21 "const CGEN_ATTR_TABLE "
22 "@arch@_cgen_" type "_attr_table[] =\n{\n"
23 (string-map (lambda (attr)
28 (string-upcase (obj:str-name attr))
30 (if (class-instance? <boolean-attribute> attr)
31 "&bool_attr[0], &bool_attr[0]"
32 (string-append "& " (gen-sym attr)
42 (define (gen-attr-table-defns)
43 (logit 2 "Generating attribute table defns ...\n")
48 static const CGEN_ATTR_ENTRY bool_attr[] =
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
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))
67 ; HW-ASM is the base class for supporting hardware elements in the opcode table
68 ; (aka assembler/disassembler).
70 ; Return the C declaration.
71 ; It is up to a derived class to redefine this as necessary.
73 (method-make! <hw-asm> 'gen-decl (lambda (self) ""))
75 ; Return the C definition.
76 ; It is up to a derived class to redefine this as necessary.
78 (method-make! <hw-asm> 'gen-defn (lambda (self) ""))
80 (method-make! <hw-asm> 'gen-ref (lambda (self) "0"))
82 (method-make! <hw-asm> 'gen-init (lambda (self) ""))
84 (method-make! <hw-asm> 'gen-table-entry (lambda (self) "CGEN_ASM_NONE, 0, "))
86 ; Prefix of global variables describing operand values.
88 (define hw-asm-prefix "@arch@_cgen_opval_")
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.
94 (define (gen-hw-asm-ref name)
95 (string-append hw-asm-prefix (gen-c-symbol name))
101 ; Return the C declaration of a keyword list.
107 "extern CGEN_KEYWORD "
108 (gen-hw-asm-ref (elm-get self 'name))
112 ; Return the C definition of a keyword list.
118 "static CGEN_KEYWORD_ENTRY "
119 (gen-hw-asm-ref (elm-get self 'name)) "_entries"
121 (string-drop -2 ; Delete trailing ",\n" [don't want the ,]
122 (string-map (lambda (e)
125 (->string (elm-get self 'name-prefix))
126 (->string (car e)) ; operand name
128 (if (string? (cadr e))
130 (number->string (cadr e))) ; value
131 ", {0, {{{0, 0}}}}, 0, 0"
134 (elm-get self 'values)))
137 (gen-hw-asm-ref (elm-get self 'name))
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"
147 ; Return a reference to a keyword table.
151 (lambda (self) (string-append "& " (gen-hw-asm-ref (elm-get self 'name))))
155 <keyword> 'gen-table-entry
157 (string-append "CGEN_ASM_KEYWORD, (PTR) " (send self 'gen-ref) ", "))
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.
169 (cond ((has-attr? self 'HASH)
171 " @arch@_cgen_asm_hash_keywords ("
181 ; Return a reference to the operand's attributes.
184 <operand> 'gen-attr-ref
186 (string-append "& CGEN_OPERAND_ATTRS (CGEN_SYM (operand_table)) "
187 "[" (op-enum self) "]"))
190 ; Name of C variable that is a pointer to the fields struct.
192 (define ifields-var "fields")
194 ; Given FIELD, an `ifield' object, return an lvalue for the operand in
197 (define (gen-operand-result-var field)
198 (string-append ifields-var "->" (gen-sym field))
201 ; Basic description init,finish,analyzer support.
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
208 (define (constant-mnemonics?)
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.
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.
226 (define (desc-finish!)
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.
236 (define (desc-analyze!)
237 (set! strip-mnemonic? (constant-mnemonics?))