2 ; Copyright (C) 2000, 2009 Red Hat, Inc.
3 ; This file is part of CGEN.
4 ; See file COPYING.CGEN for details.
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.
10 ; Two kinds of formats are defined here: iformat and sformat.
11 ; (pronounced "I-format" and "S-format")
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.
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.
25 ; The "base length" is the length of the insn that is initially fetched for
27 ; Formats are fixed in length. For variable instruction length architectures
28 ; there are separate formats for each insn's possible length.
31 (class-make '<iformat>
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
40 ; Index into the iformat table.
43 ; Sort key, used to determine insns with identical formats.
46 ; List of <ifield> objects.
49 ; min (insn-length, base-insn-size)
52 ; total length of insns with this format
58 ; An example insn that uses the format.
66 (define-getters <iformat> ifmt
67 (number key ifields mask-length length mask eg-insn)
70 ; Return enum cgen_fmt_type value for FMT.
71 ; ??? Not currently used.
73 (define (ifmt-enum fmt)
74 (string-append "@CPU@_" (string-upcase (gen-sym fmt)))
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.
81 (define (compute-insn-length fld-list)
82 (apply + (map ifld-length (ifields-base-ifields fld-list)))
85 ; Given FLD-LIST, compute the base length in bits.
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.
92 ; ??? The algorithm here is a bit odd. [Though there is value in verifying
93 ; ifields are from consistent ISAs.]
95 (define (compute-insn-base-mask-length fld-list)
96 (let* ((isa-base-bitsizes
98 (map isa-base-insn-bitsize
99 (map current-isa-lookup
100 (collect (lambda (ifld)
101 (atlist-attr-value (obj-atlist ifld) 'ISA #f))
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)))
108 ; Given FLD-LIST, compute the bitmask of constant values in the base part
109 ; of the insn (i.e. the opcode field).
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.
116 ; See also (insn-value).
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>
123 (if lsb0? (- mask-len 1) 0) ; start
125 mask-len ; word-length
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)))))
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).
140 (define (/ifmt-search-key insn sorted-ifld-list)
141 (string-map (lambda (ifld)
143 (or (->string (obj-attr-value insn 'sanitize))
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.
158 (define (ifmt-build insn index search-key iflds)
160 (symbol-append 'ifmt- (obj:name insn))
161 (string-append "e.g. " (insn-syntax insn))
166 (compute-insn-base-mask-length iflds)
167 (compute-insn-length iflds)
168 (compute-insn-base-mask iflds)
175 (class-make '<sformat>
178 ; - NAME is derived from number.
179 ; - COMMENT is the assembler syntax of an example insn that
182 ; Index into the sformat table.
185 ; Sort key, used to determine insns with identical formats.
188 ; Non-#f if insns with this format are cti insns.
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
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.
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.
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.
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.
224 ; <sformat-argbuf> entry
225 ; FIXME: Temporary location, to be moved elsewhere
233 (define-getters <sformat> sfmt
234 (number key cti? in-ops out-ops length iflds eg-insn sbuf)
237 (define-setters <sformat> sfmt (sbuf))
239 (method-make-make! <sformat>
241 number key cti? in-ops out-ops length iflds eg-insn)
244 ; Return the <sformat> search key for a sorted field list and semantic
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).
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.
255 ; We assume INSN's <iformat> has been recorded.
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.
261 (define (/sfmt-search-key insn cti? sorted-used-iflds sem-in-ops sem-out-ops)
262 (let ((op-key (lambda (op)
264 (or (->string (obj-attr-value insn 'sanitize))
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
273 (if (memory? (op:type op))
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
281 (if (op:cond? op) " cond" "")
287 (string-map (lambda (ifld)
288 (string-append " (" (obj:str-name ifld) " " (ifld-ilk ifld) ")"))
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.
304 ; We assume INSN's <iformat> has already been recorded.
306 (define (sfmt-build insn index search-key cti? in-ops out-ops sorted-used-iflds)
308 (symbol-append 'sfmt- (obj:name insn))
309 (string-append "e.g. " (insn-syntax insn))
321 ; Sort IFLDS by dependencies and then by starting bit number.
323 (define (/sfmt-order-iflds iflds)
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))
330 (append (sort-ifield-list independent up?)
331 (sort-ifield-list dependent up?)))
333 ((multi-ifield? (car iflds))
334 (loop independent (cons (car iflds) dependent) (cdr iflds)))
336 (loop (cons (car iflds) independent) dependent (cdr iflds))))))
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.
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))
350 (/sfmt-order-iflds all-iflds)))
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.
358 (class-make '<fmt-desc>
361 ; #t if insn is a cti insn
364 ; sorted list of insn's ifields
367 ; computed set of input/output operands
370 ; set of ifields used by IN-OPS,OUT-OPS.
373 ; computed set of attributes
381 (define-getters <fmt-desc> -fmt-desc
382 (cti? iflds in-ops out-ops used-iflds attrs)
385 ; Compute an iformat descriptor used to build an <iformat> object for INSN.
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.
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.
401 ; ??? We never traverse the semantics of virtual insns.
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
410 ;(not (ifld-lsb0? (car (insn-iflds insn))))
411 (not (current-arch-insn-lsb0?))
414 (if (null? sorted-ifields)
416 ; Field list is unspecified.
417 (list #f #f atlist-empty)
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
426 (semantic-attrs #f ; FIXME: context
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))))
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)
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.
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))
460 ; Format was found, use it.
462 (logit 3 "Using iformat " (number->string (ifmt-number ifmt)) ".\n")
463 (insn-set-ifmt! insn ifmt)
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))
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.
484 ; We assume INSN's <iformat> has already been recorded.
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))
497 ; Format was found, use it.
499 (logit 3 "Using sformat " (number->string (sfmt-number sfmt)) ".\n")
500 (insn-set-sfmt! insn sfmt)
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))
521 ; Given a list of insns, compute the set of instruction formats, semantic
522 ; formats, semantic attributes, and compiled semantics for each insn.
524 ; The computed <iformat> object is stored in the `ifmt' field of each insn.
526 ; Attributes derived from the semantic code are added to the insn's attributes,
527 ; but they don't override any prespecified values.
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.
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.
537 ; The result is a list of two lists: the set of computed iformats, and the
538 ; set of computed sformats.
540 ; *** This is the most expensive calculation in CGEN. ***
541 ; *** (mainly because of the detailed semantic parsing) ***
543 (define (ifmt-compute! insn-list compute-sformat?)
544 (logit 2 "Computing instruction formats and analyzing semantics ...\n")
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.
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)
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.
567 (let* ((empty-ifmt (make <iformat>
569 "empty iformat for unspecified field list"
578 (empty-sfmt (make <sformat>
580 "empty sformat for unspecified field list"
590 (ifmt-list (list empty-ifmt))
591 (sfmt-list (list empty-sfmt))
594 (for-each (lambda (insn)
595 (logit 2 "Processing format for " (obj:name insn) ": "
596 (insn-syntax insn) " ...\n")
598 (let ((fmt-desc (insn-fmt-desc insn)))
603 ; Must compute <iformat> before <sformat>, the latter
605 (/ifmt-lookup-ifmt! insn fmt-desc ifmt-list)
607 (/ifmt-lookup-sfmt! insn fmt-desc sfmt-list)))
609 ; No field list present, use empty format.
611 (insn-set-ifmt! insn empty-ifmt)
613 (insn-set-sfmt! insn empty-sfmt))))))
615 (non-multi-insns insn-list))
617 ; Done. Return the computed iformat and sformat lists.
618 (list ifmt-list sfmt-list)