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 ; Traverse the ifield list to collect all base (non-derived) ifields
73 (define (ifields-base-ifields ifld-list)
74 (collect ifld-base-ifields ifld-list)
77 ; Return enum cgen_fmt_type value for FMT.
78 ; ??? Not currently used.
80 (define (ifmt-enum fmt)
81 (string-append "@CPU@_" (string-upcase (gen-sym fmt)))
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.
88 (define (compute-insn-length fld-list)
89 (apply + (map ifld-length (ifields-base-ifields fld-list)))
92 ; Given FLD-LIST, compute the base length in bits.
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.
99 (define (compute-insn-base-mask-length fld-list)
100 (let* ((isa-base-bitsizes
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)))
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)))
112 ; Given FLD-LIST, compute the bitmask of constant values in the base part
113 ; of the insn (i.e. the opcode field).
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.
120 ; See also (insn-value).
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>
127 (if lsb0? (- mask-len 1) 0) ; start
129 mask-len ; word-length
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)))))
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).
144 (define (/ifmt-search-key insn sorted-ifld-list)
145 (string-map (lambda (ifld)
147 (or (->string (obj-attr-value insn 'sanitize))
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.
162 (define (ifmt-build insn index search-key iflds)
164 (symbol-append 'ifmt- (obj:name insn))
165 (string-append "e.g. " (insn-syntax insn))
170 (compute-insn-base-mask-length iflds)
171 (compute-insn-length iflds)
172 (compute-insn-base-mask iflds)
179 (class-make '<sformat>
182 ; - NAME is derived from number.
183 ; - COMMENT is the assembler syntax of an example insn that
186 ; Index into the sformat table.
189 ; Sort key, used to determine insns with identical formats.
192 ; Non-#f if insns with this format are cti insns.
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
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.
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.
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.
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.
228 ; <sformat-argbuf> entry
229 ; FIXME: Temporary location, to be moved elsewhere
237 (define-getters <sformat> sfmt
238 (number key cti? in-ops out-ops length iflds eg-insn sbuf)
241 (define-setters <sformat> sfmt (sbuf))
243 (method-make-make! <sformat>
245 number key cti? in-ops out-ops length iflds eg-insn)
248 ; Return the <sformat> search key for a sorted field list and semantic
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).
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.
259 ; We assume INSN's <iformat> has been recorded.
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.
265 (define (/sfmt-search-key insn cti? sorted-used-iflds sem-in-ops sem-out-ops)
266 (let ((op-key (lambda (op)
268 (or (->string (obj-attr-value insn 'sanitize))
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
277 (if (memory? (op:type op))
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
285 (if (op:cond? op) " cond" "")
291 (string-map (lambda (ifld)
292 (string-append " (" (obj:str-name ifld) " " (ifld-ilk ifld) ")"))
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.
308 ; We assume INSN's <iformat> has already been recorded.
310 (define (sfmt-build insn index search-key cti? in-ops out-ops sorted-used-iflds)
312 (symbol-append 'sfmt- (obj:name insn))
313 (string-append "e.g. " (insn-syntax insn))
325 ; Sort IFLDS by dependencies and then by starting bit number.
327 (define (/sfmt-order-iflds iflds)
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))
334 (append (sort-ifield-list independent up?)
335 (sort-ifield-list dependent up?)))
337 ((multi-ifield? (car iflds))
338 (loop independent (cons (car iflds) dependent) (cdr iflds)))
340 (loop (cons (car iflds) independent) dependent (cdr iflds))))))
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.
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))
354 (/sfmt-order-iflds all-iflds)))
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.
362 (class-make '<fmt-desc>
365 ; #t if insn is a cti insn
368 ; sorted list of insn's ifields
371 ; computed set of input/output operands
374 ; set of ifields used by IN-OPS,OUT-OPS.
377 ; computed set of attributes
385 (define-getters <fmt-desc> -fmt-desc
386 (cti? iflds in-ops out-ops used-iflds attrs)
389 ; Compute an iformat descriptor used to build an <iformat> object for INSN.
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.
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.
404 ; ??? We never traverse the semantics of virtual insns.
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
413 ;(not (ifld-lsb0? (car (insn-iflds insn))))
414 (not (current-arch-insn-lsb0?))
417 (if (null? sorted-ifields)
419 ; Field list is unspecified.
420 (list #f #f atlist-empty)
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
430 (semantic-attrs #f ; FIXME: context
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))
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)
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.
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))
463 ; Format was found, use it.
465 (logit 3 "Using iformat " (number->string (ifmt-number ifmt)) ".\n")
466 (insn-set-ifmt! insn ifmt)
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))
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.
487 ; We assume INSN's <iformat> has already been recorded.
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))
500 ; Format was found, use it.
502 (logit 3 "Using sformat " (number->string (sfmt-number sfmt)) ".\n")
503 (insn-set-sfmt! insn sfmt)
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))
524 ; Given a list of insns, compute the set of instruction formats, semantic
525 ; formats, semantic attributes, and compiled semantics for each insn.
527 ; The computed <iformat> object is stored in the `ifmt' field of each insn.
529 ; Attributes derived from the semantic code are added to the insn's attributes,
530 ; but they don't override any prespecified values.
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.
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.
540 ; The result is a list of two lists: the set of computed iformats, and the
541 ; set of computed sformats.
543 ; *** This is the most expensive calculation in CGEN. ***
544 ; *** (mainly because of the detailed semantic parsing) ***
546 (define (ifmt-compute! insn-list compute-sformat?)
547 (logit 2 "Computing instruction formats and analyzing semantics ...\n")
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.
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)
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.
570 (let* ((empty-ifmt (make <iformat>
572 "empty iformat for unspecified field list"
581 (empty-sfmt (make <sformat>
583 "empty sformat for unspecified field list"
593 (ifmt-list (list empty-ifmt))
594 (sfmt-list (list empty-sfmt))
597 (for-each (lambda (insn)
598 (logit 3 "Processing format for " (obj:name insn) ": "
599 (insn-syntax insn) " ...\n")
601 (let ((fmt-desc (insn-fmt-desc insn)))
606 ; Must compute <iformat> before <sformat>, the latter
608 (/ifmt-lookup-ifmt! insn fmt-desc ifmt-list)
610 (/ifmt-lookup-sfmt! insn fmt-desc sfmt-list)))
612 ; No field list present, use empty format.
614 (insn-set-ifmt! insn empty-ifmt)
616 (insn-set-sfmt! insn empty-sfmt))))))
618 (non-multi-insns insn-list))
620 ; Done. Return the computed iformat and sformat lists.
621 (list ifmt-list sfmt-list)