OSDN Git Service

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