1 ; Operand instance support.
2 ; Copyright (C) 2000, 2009 Red Hat, Inc.
3 ; This file is part of CGEN.
5 ; Return C code to define one instance of operand object OP.
6 ; TYPE is one of "INPUT" or "OUTPUT".
8 (define (/gen-operand-instance op type)
9 (let ((index (op:index op)))
12 "\"" (gen-sym op) "\", "
13 (hw-enum (op:type op)) ", "
14 ; FIXME: Revisit CGEN_ prefix, use MODE (FOO) instead.
15 "CGEN_" (mode:enum (op:mode op)) ", "
16 ; FIXME: We don't handle memory properly yet. Later.
17 (cond ((memory? (op:type op))
19 ((has-attr? op 'SEM-ONLY)
21 ((eq? (hw-index:type index) 'ifield)
22 (if (= (ifld-length (hw-index:value index)) 0)
24 (string-append "OP_ENT ("
25 (string-upcase (gen-sym op))
27 ((eq? (hw-index:type index) 'constant)
29 (number->string (hw-index:value index))))
30 ((eq? (hw-index:type index) 'enum)
31 (let ((sym (hw-index-enum-name index))
32 (obj (hw-index-enum-obj index)))
34 (gen-enum-sym obj sym))))
36 ", " (if (op:cond? op) "COND_REF" "0")
40 ; Return C code to define arrays of operand instances read from and written
41 ; to by <sformat> SFMT.
42 ; This is based on the semantics of the instruction.
43 ; ??? All runtime chosen values (e.g. a particular register in a register bank)
44 ; is assumed to be selected statically by the instruction. When some cpu
45 ; violates this assumption (say because a previous instruction determines
46 ; which register(s) the next instruction operates on), this will need
49 (define (/gen-operand-instance-table sfmt)
50 (let ((ins (sfmt-in-ops sfmt))
51 (outs (sfmt-out-ops sfmt)))
52 ; This used to exclude outputing anything if there were no ins or outs.
54 (sfmt-eg-insn sfmt) ; sanitize based on the example insn
56 "static const CGEN_OPINST "
57 (gen-sym sfmt) "_ops[] ATTRIBUTE_UNUSED = {\n"
58 (string-map (lambda (op) (/gen-operand-instance op "INPUT"))
60 (string-map (lambda (op) (/gen-operand-instance op "OUTPUT"))
62 " { END, (const char *)0, (enum cgen_hw_type)0, (enum cgen_mode)0, (enum cgen_operand_type)0, 0, 0 }\n};\n\n")))
65 (define (/gen-operand-instance-tables)
68 /* Operand references. */
71 (gen-define-with-symcat "OP_ENT(op) @ARCH@_OPERAND_" "op")
73 #define INPUT CGEN_OPINST_INPUT
74 #define OUTPUT CGEN_OPINST_OUTPUT
75 #define END CGEN_OPINST_END
76 #define COND_REF CGEN_OPINST_COND_REF
79 (lambda () (string-write-map /gen-operand-instance-table (current-sfmt-list)))
91 ; Return C code for INSN's operand instance table.
93 (define (gen-operand-instance-ref insn)
94 (let* ((sfmt (insn-sfmt insn))
95 (ins (sfmt-in-ops sfmt))
96 (outs (sfmt-out-ops sfmt)))
97 (if (and (null? ins) (null? outs))
99 (string-append "& " (gen-sym sfmt) "_ops[0]")))
102 ; Return C code to define a table to lookup an insn's operand instance table.
104 (define (/gen-insn-opinst-lookup-table)
106 "/* Operand instance lookup table. */\n\n"
107 "static const CGEN_OPINST *@arch@_cgen_opinst_table[MAX_INSNS] = {\n"
108 " 0,\n" ; null first entry
113 (string-append " & " (gen-sym (insn-sfmt insn)) "_ops[0],\n")))
117 /* Function to call before using the operand instance table. */
120 @arch@_cgen_init_opinst_table (cd)
124 const CGEN_OPINST **oi = & @arch@_cgen_opinst_table[0];
125 CGEN_INSN *insns = (CGEN_INSN *) cd->insn_table.init_entries;
126 for (i = 0; i < MAX_INSNS; ++i)
127 insns[i].opinst = oi[i];
133 ; Return the maximum number of operand instances used by any insn.
134 ; If not generating the operand instance table, use a heuristic.
136 (define (max-operand-instances)
137 (if /opcodes-build-operand-instance-table?
140 (+ (length (sfmt-in-ops (insn-sfmt insn)))
141 (length (sfmt-out-ops (insn-sfmt insn)))))
142 (current-insn-list)))
146 ; Generate $arch-opinst.c.
148 (define (cgen-opinst.c)
149 (logit 1 "Generating " (current-arch-name) "-opinst.c ...\n")
151 ; If instruction semantics haven't been analyzed, do that now.
152 (if (not (arch-semantics-analyzed? CURRENT-ARCH))
154 (logit 1 "Instruction semantics weren't analyzed when .cpu file was loaded.\n")
155 (logit 1 "Doing so now ...\n")
156 (arch-analyze-insns! CURRENT-ARCH
158 #t) ; /opcodes-build-operand-instance-table?
162 (gen-c-copyright "Semantic operand instances for @arch@."
163 CURRENT-COPYRIGHT CURRENT-PACKAGE)
165 #include \"sysdep.h\"
166 #include \"ansidecl.h\"
168 #include \"symcat.h\"
169 #include \"@prefix@-desc.h\"
170 #include \"@prefix@-opc.h\"
172 /gen-operand-instance-tables
173 /gen-insn-opinst-lookup-table