OSDN Git Service

cgen/ChangeLog:
[pf3gnuchains/pf3gnuchains3x.git] / cgen / opc-opinst.scm
1 ; Operand instance support.
2 ; Copyright (C) 2000 Red Hat, Inc.
3 ; This file is part of CGEN.
4
5 ; Return C code to define one instance of operand object OP.
6 ; TYPE is one of "INPUT" or "OUTPUT".
7
8 (define (-gen-operand-instance op type)
9   (let ((index (op:index op)))
10     (string-append "  { "
11                    type ", "
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))
18                           "0, 0")
19                          ((has-attr? op 'SEM-ONLY)
20                           "0, 0")
21                          ((eq? (hw-index:type index) 'ifield)
22                           (if (= (ifld-length (hw-index:value index)) 0)
23                               "0, 0"
24                               (string-append "OP_ENT ("
25                                              (string-upcase (gen-sym op))
26                                              "), 0")))
27                          ((eq? (hw-index:type index) 'constant)
28                           (string-append "0, "
29                                          (number->string (hw-index:value index))))
30                          (else "0, 0"))
31                    ", " (if (op:cond? op) "COND_REF" "0")
32                    " },\n"))
33 )
34
35 ; Return C code to define arrays of operand instances read from and written
36 ; to by <sformat> SFMT.
37 ; This is based on the semantics of the instruction.
38 ; ??? All runtime chosen values (e.g. a particular register in a register bank)
39 ; is assumed to be selected statically by the instruction.  When some cpu
40 ; violates this assumption (say because a previous instruction determines
41 ; which register(s) the next instruction operates on), this will need
42 ; additional support.
43
44 (define (-gen-operand-instance-table sfmt)
45   (let ((ins (sfmt-in-ops sfmt))
46         (outs (sfmt-out-ops sfmt)))
47     ; This used to exclude outputing anything if there were no ins or outs.
48     (gen-obj-sanitize
49      (sfmt-eg-insn sfmt) ; sanitize based on the example insn
50      (string-append
51       "static const CGEN_OPINST "
52       (gen-sym sfmt) "_ops[] = {\n"
53       (string-map (lambda (op) (-gen-operand-instance op "INPUT"))
54                   ins)
55       (string-map (lambda (op)  (-gen-operand-instance op "OUTPUT"))
56                   outs)
57       "  { END, (const char *)0, (enum cgen_hw_type)0, (enum cgen_mode)0, (enum cgen_operand_type)0, 0, 0 }\n};\n\n")))
58 )
59
60 (define (-gen-operand-instance-tables)
61   (string-write
62    "\
63 /* Operand references.  */
64
65 "
66    (gen-define-with-symcat "OP_ENT(op) @ARCH@_OPERAND_" "op")
67 "\
68 #define INPUT CGEN_OPINST_INPUT
69 #define OUTPUT CGEN_OPINST_OUTPUT
70 #define END CGEN_OPINST_END
71 #define COND_REF CGEN_OPINST_COND_REF
72
73 "
74    (lambda () (string-write-map -gen-operand-instance-table (current-sfmt-list)))
75    "\
76 #undef OP_ENT
77 #undef INPUT
78 #undef OUTPUT
79 #undef END
80 #undef COND_REF
81
82 "
83    )
84 )
85
86 ; Return C code for INSN's operand instance table.
87
88 (define (gen-operand-instance-ref insn)
89   (let* ((sfmt (insn-sfmt insn))
90          (ins (sfmt-in-ops sfmt))
91          (outs (sfmt-out-ops sfmt)))
92     (if (and (null? ins) (null? outs))
93         "0"
94         (string-append "& " (gen-sym sfmt) "_ops[0]")))
95 )
96
97 ; Return C code to define a table to lookup an insn's operand instance table.
98
99 (define (-gen-insn-opinst-lookup-table)
100   (string-list
101    "/* Operand instance lookup table.  */\n\n"
102    "static const CGEN_OPINST *@arch@_cgen_opinst_table[MAX_INSNS] = {\n"
103    "  0,\n" ; null first entry
104    (string-list-map
105     (lambda (insn)
106       (gen-obj-sanitize
107        insn
108        (string-append "  & " (gen-sym (insn-sfmt insn)) "_ops[0],\n")))
109     (current-insn-list))
110    "};\n\n"
111    "\
112 /* Function to call before using the operand instance table.  */
113
114 void
115 @arch@_cgen_init_opinst_table (cd)
116      CGEN_CPU_DESC cd;
117 {
118   int i;
119   const CGEN_OPINST **oi = & @arch@_cgen_opinst_table[0];
120   CGEN_INSN *insns = (CGEN_INSN *) cd->insn_table.init_entries;
121   for (i = 0; i < MAX_INSNS; ++i)
122     insns[i].opinst = oi[i];
123 }
124 "
125    )
126 )
127
128 ; Return the maximum number of operand instances used by any insn.
129 ; If not generating the operand instance table, use a heuristic.
130
131 (define (max-operand-instances)
132   (if -opcodes-build-operand-instance-table?
133       (apply max
134              (map (lambda (insn)
135                     (+ (length (sfmt-in-ops (insn-sfmt insn)))
136                        (length (sfmt-out-ops (insn-sfmt insn)))))
137                   (current-insn-list)))
138       8) ; FIXME: for now
139 )
140
141 ; Generate $arch-opinst.c.
142
143 (define (cgen-opinst.c)
144   (logit 1 "Generating " (current-arch-name) "-opinst.c ...\n")
145
146   ; If instruction semantics haven't been analyzed, do that now.
147   (if (not (arch-semantics-analyzed? CURRENT-ARCH))
148       (begin
149         (logit 1 "Instruction semantics weren't analyzed when .cpu file was loaded.\n")
150         (logit 1 "Doing so now ...\n")
151         (arch-analyze-insns! CURRENT-ARCH
152                              #t ; include aliases
153                              #t) ; -opcodes-build-operand-instance-table?
154         ))
155
156   (string-write
157    (gen-c-copyright "Semantic operand instances for @arch@."
158                   CURRENT-COPYRIGHT CURRENT-PACKAGE)
159    "\
160 #include \"sysdep.h\"
161 #include \"ansidecl.h\"
162 #include \"bfd.h\"
163 #include \"symcat.h\"
164 #include \"@prefix@-desc.h\"
165 #include \"@prefix@-opc.h\"
166 \n"
167    -gen-operand-instance-tables
168    -gen-insn-opinst-lookup-table
169    )
170 )