OSDN Git Service

* iformat.scm (ifields-base-ifields): Simplify.
[pf3gnuchains/pf3gnuchains3x.git] / cgen / iformat.scm
1 ; Instruction formats.
2 ; Copyright (C) 2000, 2009 Red Hat, Inc.
3 ; This file is part of CGEN.
4 ; See file COPYING.CGEN for details.
5
6 ; Instruction formats are computed after the .cpu file has been read in.
7 ; ??? May also wish to allow programmer to specify formats, but not sure this
8 ; will complicate things more than it simplifies them, so it's defered.
9 ;
10 ; Two kinds of formats are defined here: iformat and sformat.
11 ; (pronounced "I-format" and "S-format")
12 ;
13 ; Iformats are the instruction format as specified by the instructions' fields,
14 ; and are the machine computed version of the generally known notion of an
15 ; "instruction format".  No semantic information is attributed to iformats.
16 ;
17 ; Sformats are the same as iformats except that semantics are used to
18 ; distinguish them.  For example, if an operand is refered to in one mode by
19 ; one instruction and in a different mode by another instruction, then these
20 ; two insns would have different sformats but the same iformat.  Sformats
21 ; are used in simulator extraction code to collapse the number of cases that
22 ; must be handled.  They can also be used to collapse the number of cases
23 ; in the modeling code.
24 ;
25 ; The "base length" is the length of the insn that is initially fetched for
26 ; decoding purposes.
27 ; Formats are fixed in length.  For variable instruction length architectures
28 ; there are separate formats for each insn's possible length.
29
30 (define <iformat>
31   (class-make '<iformat>
32               '(<ident>)
33                 ; From <ident>:
34                 ; - NAME is derived from number, but we might have user
35                 ;   specified formats someday [though I wouldn't add them
36                 ;   without a clear need].
37                 ; - COMMENT is the assembler syntax of an example insn that
38                 ;   uses the format.
39               '(
40                 ; Index into the iformat table.
41                 number
42
43                 ; Sort key, used to determine insns with identical formats.
44                 key
45
46                 ; List of <ifield> objects.
47                 ifields
48
49                 ; min (insn-length, base-insn-size)
50                 mask-length
51
52                 ; total length of insns with this format
53                 length
54
55                 ; mask of base part
56                 mask
57
58                 ; An example insn that uses the format.
59                 eg-insn
60                 )
61               nil)
62 )
63
64 ; Accessor fns.
65
66 (define-getters <iformat> ifmt
67   (number key ifields mask-length length mask eg-insn)
68 )
69
70 ; Traverse the ifield list to collect all base (non-derived) ifields
71 ; used in it.
72
73 (define (ifields-base-ifields ifld-list)
74   (collect ifld-base-ifields ifld-list)
75 )
76
77 ; Return enum cgen_fmt_type value for FMT.
78 ; ??? Not currently used.
79
80 (define (ifmt-enum fmt)
81   (string-append "@CPU@_" (string-upcase (gen-sym fmt)))
82 )
83 \f
84 ; Given FLD-LIST, compute the length of the insn in bits.
85 ; This is done by adding up all the field sizes.
86 ; All bits must be represent exactly once.
87
88 (define (compute-insn-length fld-list)
89   (apply + (map ifld-length (ifields-base-ifields fld-list)))
90 )
91
92 ; Given FLD-LIST, compute the base length in bits.
93 ;
94 ; For variable length instruction sets, or with cpus with multiple
95 ; instruction sets, compute the base appropriate for this set of
96 ; ifields.  Check that ifields are not shared among isas with
97 ; inconsistent base insn lengths.
98
99 (define (compute-insn-base-mask-length fld-list)
100   (let* ((isa-base-bitsizes
101           (remove-duplicates
102            (map isa-base-insn-bitsize
103                 (map current-isa-lookup
104                      (collect (lambda (ifld) 
105                                 (bitset-attr->list (atlist-attr-value (obj-atlist ifld) 'ISA #f)))
106                               fld-list))))))
107     (if (= 1 (length isa-base-bitsizes))
108         (min (car isa-base-bitsizes) (compute-insn-length fld-list))
109         (error "ifields have inconsistent isa/base-insn-size values:" isa-base-bitsizes)))
110 )
111
112 ; Given FLD-LIST, compute the bitmask of constant values in the base part
113 ; of the insn (i.e. the opcode field).
114 ;
115 ; FIXME: Need to add support for constant fields appearing outside the base
116 ; insn.  One way would be to record with each insn the value for each constant
117 ; field.  That would allow code to straightforwardly fetch it.  Another would
118 ; be to only record constant values appearing outside the base insn.
119 ;
120 ; See also (insn-value).
121 ;
122 (define (compute-insn-base-mask fld-list)
123   (let* ((mask-len (compute-insn-base-mask-length fld-list))
124          (lsb0? (ifld-lsb0? (car fld-list)))
125          (mask-bitrange (make <bitrange>
126                               0 ; word-offset
127                               (if lsb0? (- mask-len 1) 0) ; start
128                               mask-len ; length
129                               mask-len ; word-length
130                               lsb0?)))
131     (apply +
132            (map (lambda (fld) (ifld-mask fld mask-len mask-bitrange))
133                 ; Find the fields that have constant values.
134                 (find ifld-constant? (ifields-base-ifields fld-list)))))
135 )
136 \f
137 ; Return the <iformat> search key for a sorted field list.
138 ; This determines how iformats differ from each other.
139 ; It also speeds up searching as the search key can be anything
140 ; (though at present searching isn't as fast as it could be).
141 ; INSN is passed so that we can include its sanytize attribute, if present,
142 ; so sanytized sources work (needed formats don't disappear).
143
144 (define (/ifmt-search-key insn sorted-ifld-list)
145   (string-map (lambda (ifld)
146                 (string-append " ("
147                                (or (->string (obj-attr-value insn 'sanitize))
148                                    "-nosan-")
149                                " "
150                                (obj:str-name ifld)
151                                " "
152                                (ifld-ilk ifld)
153                                ")"))
154               sorted-ifld-list)
155 )
156
157 ; Create an <iformat> object for INSN.
158 ; INDEX is the ordinal to assign to the result or -1 if unknown.
159 ; SEARCH-KEY is the search key used to determine the iformat's uniqueness.
160 ; IFLDS is a sorted list of INSN's ifields.
161
162 (define (ifmt-build insn index search-key iflds)
163   (make <iformat>
164     (symbol-append 'ifmt- (obj:name insn))
165     (string-append "e.g. " (insn-syntax insn))
166     atlist-empty
167     index
168     search-key
169     iflds
170     (compute-insn-base-mask-length iflds)
171     (compute-insn-length iflds)
172     (compute-insn-base-mask iflds)
173     insn)
174 )
175 \f
176 ; Sformats.
177
178 (define <sformat>
179   (class-make '<sformat>
180               '(<ident>)
181               ; From <ident>:
182               ; - NAME is derived from number.
183               ; - COMMENT is the assembler syntax of an example insn that
184               ;   uses the format.
185               '(
186                 ; Index into the sformat table.
187                 number
188
189                 ; Sort key, used to determine insns with identical formats.
190                 key
191
192                 ; Non-#f if insns with this format are cti insns.
193                 cti?
194
195                 ; IN-OPS is a list of input operands.
196                 ; OUT-OPS is a list of output operands.
197                 ; These are used to distinguish the format from others,
198                 ; so that the extract and read operations can be based on the
199                 ; sformat.
200                 ; The extract fns use this data to record the necessary
201                 ; information for profiling [which isn't necessarily a property
202                 ; of the field list].  We could have one extraction function
203                 ; per instruction, but there's a *lot* of duplicated code, and
204                 ; the semantic operands rarely contribute to extra formats.
205                 ; The parallel execution support uses this data to record the
206                 ; input (or output) values based on the instruction format,
207                 ; again cutting down on duplicated code.
208                 in-ops
209                 out-ops
210
211                 ; Length of all insns with this format.
212                 ; Since insns with different iformats can have the same sformat
213                 ; we need to ensure ifield extraction works among the various
214                 ; iformats.  We do this by ensuring all insns with the same
215                 ; sformat have the same length.
216                 length
217
218                 ; Cached list of all ifields used.
219                 ; This can be derived from IN-OPS/OUT-OPS but is computed once
220                 ; and cached here for speed.
221                 iflds
222
223                 ; An example insn that uses the format.
224                 ; This is used for debugging purposes, but also to help get
225                 ; sanytization (spelled wrong on purpose) right.
226                 eg-insn
227
228                 ; <sformat-argbuf> entry
229                 ; FIXME: Temporary location, to be moved elsewhere
230                 (sbuf . #f)
231                 )
232               nil)
233 )
234
235 ; Accessor fns.
236
237 (define-getters <sformat> sfmt
238   (number key cti? in-ops out-ops length iflds eg-insn sbuf)
239 )
240
241 (define-setters <sformat> sfmt (sbuf))
242
243 (method-make-make! <sformat>
244                    '(name comment attrs
245                      number key cti? in-ops out-ops length iflds eg-insn)
246 )
247 \f
248 ; Return the <sformat> search key for a sorted field list and semantic
249 ; operands.
250 ; This determines how sformats differ from each other.
251 ; It also speeds up searching as the search key can be anything
252 ; (though at present searching isn't as fast as it could be).
253 ;
254 ; INSN is passed so that we can include its sanytize attribute, if present,
255 ; so sanytized sources work (needed formats don't disappear).
256 ; SORTED-USED-IFLDS is a sorted list of ifields used by SEM-{IN,OUT}-OPS.
257 ; Note that it is not the complete set of ifields used by INSN.
258 ;
259 ; We assume INSN's <iformat> has been recorded.
260 ;
261 ; Note: It's important to minimize the number of created sformats.  It keeps
262 ; the generated code smaller (and sometimes faster - more usable common
263 ; fragments in pbb simulators).  Don't cause spurious differences.
264
265 (define (/sfmt-search-key insn cti? sorted-used-iflds sem-in-ops sem-out-ops)
266   (let ((op-key (lambda (op)
267                   (string-append " ("
268                                  (or (->string (obj-attr-value insn 'sanitize))
269                                      "-nosan-")
270                                  " "
271                                  (obj:str-name op)
272                                  ; ??? Including memory operands currently
273                                  ; isn't necessary and it can account for some
274                                  ; spurious differences.  On the other hand
275                                  ; leaving it out doesn't seem like the right
276                                  ; thing to do.
277                                  (if (memory? (op:type op))
278                                      ""
279                                      (string-append " "
280                                                     (obj:str-name (op:mode op))))
281                                  ; CGEN_OPERAND_INSTANCE_COND_REF is stored
282                                  ; with the operand in the operand instance
283                                  ; table thus formats must be distinguished
284                                  ; by this.
285                                  (if (op:cond? op) " cond" "")
286                                  ")")))
287         )
288     (list
289      cti?
290      (insn-length insn)
291      (string-map (lambda (ifld)
292                    (string-append " (" (obj:str-name ifld) " " (ifld-ilk ifld) ")"))
293                  sorted-used-iflds)
294      (string-map op-key
295                  sem-in-ops)
296      (string-map op-key
297                  sem-out-ops)
298      ))
299 )
300
301 ; Create an <sformat> object for INSN.
302 ; INDEX is the ordinal to assign to the result or -1 if unknown.
303 ; SEARCH-KEY is the search key used to determine the sformat's uniqueness.
304 ; {IN,OUT}-OPS are lists of INSN's input/output operands.
305 ; SORTED-USED-IFLDS is a sorted list of ifields used by {IN,OUT}-OPS.
306 ; Note that it is not the complete set of ifields used by INSN.
307 ;
308 ; We assume INSN's <iformat> has already been recorded.
309
310 (define (sfmt-build insn index search-key cti? in-ops out-ops sorted-used-iflds)
311   (make <sformat>
312     (symbol-append 'sfmt- (obj:name insn))
313     (string-append "e.g. " (insn-syntax insn))
314     atlist-empty
315     index
316     search-key
317     cti?
318     in-ops
319     out-ops
320     (insn-length insn)
321     sorted-used-iflds
322     insn)
323 )
324
325 ; Sort IFLDS by dependencies and then by starting bit number.
326
327 (define (/sfmt-order-iflds iflds)
328   (let ((up? 
329          ; ??? Something like this is preferable.
330          ;(not (ifld-lsb0? (car ifld-list)))
331          (not (current-arch-insn-lsb0?))))
332     (let loop ((independent nil) (dependent nil) (iflds iflds))
333       (cond ((null? iflds)
334              (append (sort-ifield-list independent up?)
335                      (sort-ifield-list dependent up?)))
336             ; FIXME: quick hack.
337             ((multi-ifield? (car iflds))
338              (loop independent (cons (car iflds) dependent) (cdr iflds)))
339             (else
340              (loop (cons (car iflds) independent) dependent (cdr iflds))))))
341 )
342
343 ; Return a sorted list of ifields used by IN-OPS, OUT-OPS.
344 ; The ifields are sorted by dependencies and then by start bit.
345 ; The important points are to help distinguish sformat's by the ifields used
346 ; and to put ifields that others depend on first.
347
348 (define (/sfmt-used-iflds in-ops out-ops)
349   (let ((in-iflds (map op-iflds-used in-ops))
350         (out-iflds (map op-iflds-used out-ops)))
351     (let ((all-iflds (nub (append (apply append in-iflds)
352                                   (apply append out-iflds))
353                           obj:name)))
354       (/sfmt-order-iflds all-iflds)))
355 )
356 \f
357 ; The format descriptor is used to sort formats.
358 ; This is a utility class internal to this file.
359 ; There is one instance per insn.
360
361 (define <fmt-desc>
362   (class-make '<fmt-desc>
363               nil
364               '(
365                 ; #t if insn is a cti insn
366                 cti?
367
368                 ; sorted list of insn's ifields
369                 iflds
370
371                 ; computed set of input/output operands
372                 in-ops out-ops
373
374                 ; set of ifields used by IN-OPS,OUT-OPS.
375                 used-iflds
376
377                 ; computed set of attributes
378                 attrs
379                 )
380               nil)
381 )
382
383 ; Accessors.
384
385 (define-getters <fmt-desc> -fmt-desc
386   (cti? iflds in-ops out-ops used-iflds attrs)
387 )
388
389 ; Compute an iformat descriptor used to build an <iformat> object for INSN.
390 ;
391 ; If COMPUTE-SFORMAT? is #t compile the semantics and compute the semantic
392 ; format (same as instruction format except that operands are used to
393 ; distinguish insns).
394 ; Attributes derivable from the semantics are also computed.
395 ; This is all done at the same time to minimize the number of times the
396 ; semantic code is traversed.
397 ;
398 ; The result is (descriptor compiled-semantics attrs).
399 ; `descriptor' is #f for insns with an empty field list
400 ; (this happens for virtual insns).
401 ; `compiled-semantics' is #f if COMPUTE-SFORMAT? is #f.
402 ; `attrs' is an <attr-list> object of attributes derived from the semantics.
403 ;
404 ; ??? We never traverse the semantics of virtual insns.
405
406 (define (ifmt-analyze insn compute-sformat?)
407   ; First sort by starting bit number the list of fields in INSN.
408   (let ((sorted-ifields
409          (sort-ifield-list (insn-iflds insn)
410                            ; ??? Something like this is preferable, but
411                            ; if the first insn is a virtual insn there are
412                            ; no fields.
413                            ;(not (ifld-lsb0? (car (insn-iflds insn))))
414                            (not (current-arch-insn-lsb0?))
415                            )))
416
417     (if (null? sorted-ifields)
418
419         ; Field list is unspecified.
420         (list #f #f atlist-empty)
421
422         ; FIXME: error checking (e.g. missing or overlapping bits)
423         (let* ((sem (insn-semantics insn))
424                ; Compute list of input and output operands if asked for.
425                (sem-ops (if compute-sformat?
426                             (semantic-compile #f ; FIXME: context
427                                               insn sem)
428                             (csem-make #f #f #f
429                                        (if sem
430                                            (semantic-attrs #f ; FIXME: context
431                                                            insn sem)
432                                            atlist-empty))))
433                )
434           (let ((compiled-sem (csem-code sem-ops))
435                 (in-ops (csem-inputs sem-ops))
436                 (out-ops (csem-outputs sem-ops))
437                 (attrs (csem-attrs sem-ops))
438                 (cti? (or (atlist-cti? (csem-attrs sem-ops))
439                           (insn-cti? insn))))
440             (list (make <fmt-desc>
441                     cti? sorted-ifields in-ops out-ops
442                     (if (and in-ops out-ops)
443                         (/sfmt-used-iflds in-ops out-ops)
444                         #f)
445                     attrs)
446                   compiled-sem
447                   attrs)))))
448 )
449
450 ; Subroutine of ifmt-compute!, to simplify it.
451 ; Lookup INSN's iformat in IFMT-LIST and if not found add it.
452 ; FMT-DESC is INSN's <fmt-desc> object.
453 ; IFMT-LIST is append!'d to and the found iformat is stored in INSN.
454
455 (define (/ifmt-lookup-ifmt! insn fmt-desc ifmt-list)
456   (let* ((search-key (/ifmt-search-key insn (-fmt-desc-iflds fmt-desc)))
457          (ifmt (find-first (lambda (elm)
458                              (equal? (ifmt-key elm) search-key))
459                            ifmt-list)))
460
461     (if ifmt
462
463         ; Format was found, use it.
464         (begin
465           (logit 3 "Using iformat " (number->string (ifmt-number ifmt)) ".\n")
466           (insn-set-ifmt! insn ifmt)
467           )
468
469         ; Format wasn't found, create new entry.
470         (let* ((ifmt-index (length ifmt-list))
471                (ifmt (ifmt-build insn ifmt-index search-key
472                                  (ifields-base-ifields (-fmt-desc-iflds fmt-desc)))))
473           (logit 3 "Creating iformat " (number->string ifmt-index) ".\n")
474           (insn-set-ifmt! insn ifmt)
475           (append! ifmt-list (list ifmt))
476           )
477         ))
478
479   *UNSPECIFIED*
480 )
481
482 ; Subroutine of ifmt-compute!, to simplify it.
483 ; Lookup INSN's sformat in SFMT-LIST and if not found add it.
484 ; FMT-DESC is INSN's <fmt-desc> object.
485 ; SFMT-LIST is append!'d to and the found sformat is stored in INSN.
486 ;
487 ; We assume INSN's <iformat> has already been recorded.
488
489 (define (/ifmt-lookup-sfmt! insn fmt-desc sfmt-list)
490   (let* ((search-key (/sfmt-search-key insn (-fmt-desc-cti? fmt-desc)
491                                        (-fmt-desc-used-iflds fmt-desc)
492                                        (-fmt-desc-in-ops fmt-desc)
493                                        (-fmt-desc-out-ops fmt-desc)))
494          (sfmt (find-first (lambda (elm)
495                              (equal? (sfmt-key elm) search-key))
496                            sfmt-list)))
497
498     (if sfmt
499
500         ; Format was found, use it.
501         (begin
502           (logit 3 "Using sformat " (number->string (sfmt-number sfmt)) ".\n")
503           (insn-set-sfmt! insn sfmt)
504           )
505
506         ; Format wasn't found, create new entry.
507         (let* ((sfmt-index (length sfmt-list))
508                (sfmt (sfmt-build insn sfmt-index search-key
509                                  (-fmt-desc-cti? fmt-desc)
510                                  (-fmt-desc-in-ops fmt-desc)
511                                  (-fmt-desc-out-ops fmt-desc)
512                                  (ifields-base-ifields (-fmt-desc-used-iflds fmt-desc)))))
513           (logit 3 "Creating sformat " (number->string sfmt-index) ".\n")
514           (insn-set-sfmt! insn sfmt)
515           (append! sfmt-list (list sfmt))
516           )
517         ))
518
519   *UNSPECIFIED*
520 )
521 \f
522 ; Main entry point.
523
524 ; Given a list of insns, compute the set of instruction formats, semantic
525 ; formats, semantic attributes, and compiled semantics for each insn.
526 ;
527 ; The computed <iformat> object is stored in the `ifmt' field of each insn.
528 ;
529 ; Attributes derived from the semantic code are added to the insn's attributes,
530 ; but they don't override any prespecified values.
531 ;
532 ; If COMPUTE-SFORMAT? is #t, the computed <sformat> object is stored in the
533 ; `sfmt' field of each insn, and the processed semantic code is stored in the
534 ; `compiled-semantics' field of each insn.
535 ;
536 ; The `fmt-desc' field of each insn is used to store an <fmt-desc> object
537 ; which contains the search keys, sorted field list, input-operands, and
538 ; output-operands, and is not used outside this procedure.
539 ;
540 ; The result is a list of two lists: the set of computed iformats, and the
541 ; set of computed sformats.
542 ;
543 ; *** This is the most expensive calculation in CGEN.   ***
544 ; *** (mainly because of the detailed semantic parsing) ***
545
546 (define (ifmt-compute! insn-list compute-sformat?)
547   (logit 2 "Computing instruction formats and analyzing semantics ...\n")
548
549   ; First analyze each insn, storing the result in fmt-desc.
550   ; If asked to, convert the semantic code to a compiled form to simplify more
551   ; intelligent processing of it later.
552
553   (for-each (lambda (insn)
554               (logit 3 "Scanning operands of " (obj:name insn) ": "
555                      (insn-syntax insn) " ...\n")
556               (let ((sem-ops (ifmt-analyze insn compute-sformat?)))
557                 (insn-set-fmt-desc! insn (car sem-ops))
558                 (if (and compute-sformat? (cadr sem-ops))
559                     (let ((compiled-sem (cadr sem-ops)))
560                       (insn-set-compiled-semantics! insn compiled-sem)))
561                 (obj-set-atlist! insn
562                                  (atlist-append (obj-atlist insn)
563                                                 (caddr sem-ops)))
564                 ))
565             insn-list)
566
567   ; Now for each insn, look up the ifield list in the format table (and if not
568   ; found add it), and set the ifmt/sfmt elements of the insn.
569
570   (let* ((empty-ifmt (make <iformat>
571                           'ifmt-empty
572                           "empty iformat for unspecified field list"
573                           atlist-empty ; attrs
574                           -1 ; number
575                           #f ; key
576                           nil ; fields
577                           0 ; mask-length
578                           0 ; length
579                           0 ; mask
580                           #f)) ; eg-insn
581          (empty-sfmt (make <sformat>
582                           'sfmt-empty
583                           "empty sformat for unspecified field list"
584                           atlist-empty ; attrs
585                           -1 ; number
586                           #f ; key
587                           #f ; cti?
588                           nil ; sem-in-ops
589                           nil ; sem-out-ops
590                           0 ; length
591                           nil ; used iflds
592                           #f)) ; eg-insn
593          (ifmt-list (list empty-ifmt))
594          (sfmt-list (list empty-sfmt))
595          )
596
597     (for-each (lambda (insn)
598                 (logit 3 "Processing format for " (obj:name insn) ": "
599                        (insn-syntax insn) " ...\n")
600
601                 (let ((fmt-desc (insn-fmt-desc insn)))
602
603                   (if fmt-desc
604
605                       (begin
606                         ; Must compute <iformat> before <sformat>, the latter
607                         ; needs the former.
608                         (/ifmt-lookup-ifmt! insn fmt-desc ifmt-list)
609                         (if compute-sformat?
610                             (/ifmt-lookup-sfmt! insn fmt-desc sfmt-list)))
611
612                       ; No field list present, use empty format.
613                       (begin
614                         (insn-set-ifmt! insn empty-ifmt)
615                         (if compute-sformat?
616                             (insn-set-sfmt! insn empty-sfmt))))))
617
618               (non-multi-insns insn-list))
619
620     ; Done.  Return the computed iformat and sformat lists.
621     (list ifmt-list sfmt-list)
622     )
623 )