1 ; General cpu info generator support.
2 ; Copyright (C) 2000 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: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 (elm-get self 'prefix) (car e) ; operand name
127 (if (string? (cadr e))
129 (number->string (cadr e))) ; value
133 (elm-get self 'values)))
136 (gen-hw-asm-ref (elm-get self 'name))
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"
146 ; Return a reference to a keyword table.
150 (lambda (self) (string-append "& " (gen-hw-asm-ref (elm-get self 'name))))
154 <keyword> 'gen-table-entry
156 (string-append "CGEN_ASM_KEYWORD, (PTR) " (send self 'gen-ref) ", "))
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.
168 (cond ((has-attr? self 'HASH)
170 " @arch@_cgen_asm_hash_keywords ("
180 ; Return a reference to the operand's attributes.
183 <operand> 'gen-attr-ref
185 (string-append "& CGEN_OPERAND_ATTRS (CGEN_SYM (operand_table)) "
186 "[" (op-enum self) "]"))
189 ; Name of C variable that is a pointer to the fields struct.
191 (define ifields-var "fields")
193 ; Given FIELD, an `ifield' object, return an lvalue for the operand in
196 (define (gen-operand-result-var field)
197 (string-append ifields-var "->" (gen-sym field))
200 ; Basic description init,finish,analyzer support.
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
207 (define (constant-mnemonics?)
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.
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.
225 (define (desc-finish!)
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.
235 (define (desc-analyze!)
236 (set! strip-mnemonic? (constant-mnemonics?))