1 ; Routines for instruction semantic analysis (including rtx-simplify).
2 ; Copyright (C) 2000 Red Hat, Inc.
3 ; This file is part of CGEN.
4 ; See file COPYING.CGEN for details.
6 ; Semantic expression compilation.
7 ; This is more involved than normal rtx compilation as we need to keep
8 ; track of the inputs and outputs. Various attributes that can be derived
9 ; from the code are also computed.
11 ; Subroutine of -simplify-expr-fn to compare two values for equality.
12 ; If both are constants and they're equal return #f/#t.
13 ; INVERT? = #f -> return #t if equal, #t -> return #f if equal.
14 ; Returns 'unknown if either argument is not a constant.
16 (define (rtx-const-equal arg0 arg1 invert?)
17 (if (and (rtx-constant? arg0)
20 (!= (rtx-constant-value arg0)
21 (rtx-constant-value arg1))
22 (= (rtx-constant-value arg0)
23 (rtx-constant-value arg1)))
27 ; Subroutine of -simplify-expr-fn to see if MAYBE-CONST is one of NUMBER-LIST.
28 ; NUMBER-LIST is a `number-list' rtx.
29 ; INVERT? is #t if looking for non-membership.
30 ; #f/#t is only returned for definitive answers.
32 ; - return #f if MAYBE-CONST is not in NUMBER-LIST
33 ; - return #t if MAYBE-CONST is in NUMBER-LIST and it has only one member
34 ; - return 'member if MAYBE-CONST is in NUMBER-LIST and it has many members
35 ; - otherwise return 'unknown
37 ; - return #t if MAYBE-CONST is not in NUMBER-LIST
38 ; - return #f if MAYBE-CONST is in NUMBER-LIST and it has only one member
39 ; - return 'member if MAYBE-CONST is in NUMBER-LIST and it has many members
40 ; - otherwise return 'unknown
42 (define (rtx-const-list-equal maybe-const number-list invert?)
43 (assert (rtx-kind? 'number-list number-list))
44 (if (rtx-constant? maybe-const)
45 (let ((values (rtx-number-list-values number-list)))
47 (if (memq (rtx-constant-value maybe-const) values)
48 (if (= (length values) 1)
52 (if (memq (rtx-constant-value maybe-const) values)
53 (if (= (length values) 1)
60 ; Subroutine of -simplify-expr-fn to simplify an eq-attr of (current-mach).
61 ; CONTEXT is a <context> object or #f if there is none.
63 (define (rtx-simplify-eq-attr-mach rtx context)
64 (let ((attr (rtx-eq-attr-attr rtx))
65 (value (rtx-eq-attr-value rtx)))
66 ; If all currently selected machs will yield the same value
67 ; for the attribute, we can simplify.
68 (let ((values (map (lambda (m)
69 (obj-attr-value m attr))
70 (current-mach-list))))
71 ; Ensure at least one mach is selected.
73 (context-error context "rtx simplification, no machs selected"
75 ; All values equal to the first one?
76 (if (all-true? (map (lambda (val)
77 (equal? val (car values)))
80 ; Convert internal boolean attribute value
81 ; #f/#t to external value FALSE/TRUE.
93 ; Subroutine of -simplify-expr-fn to simplify an eq-attr of (current-insn).
95 (define (rtx-simplify-eq-attr-insn rtx insn context)
96 (let ((attr (rtx-eq-attr-attr rtx))
97 (value (rtx-eq-attr-value rtx)))
98 (if (not (insn? insn))
99 (context-error context
100 "No current insn for `(current-insn)'"
102 (let ((attr-value (obj-attr-value insn attr)))
103 (if (eq? value attr-value)
108 ; Subroutine of rtx-simplify.
109 ; This is the EXPR-FN argument to rtx-traverse.
111 (define (-simplify-expr-fn rtx-obj expr mode parent-expr op-pos tstate appstuff)
113 ;(display "Processing ") (display (rtx-dump expr)) (newline)
115 (case (rtx-name expr)
118 (let* ((arg (-rtx-traverse (rtx-alu-op-arg expr 0)
120 (rtx-alu-op-mode expr)
121 expr 1 tstate appstuff))
122 (no-side-effects? (not (rtx-side-effects? arg))))
123 (cond ((and no-side-effects? (rtx-false? arg))
125 ((and no-side-effects? (rtx-true? arg))
127 (else (rtx-make 'not (rtx-alu-op-mode expr) arg)))))
130 (let ((arg0 (-rtx-traverse (rtx-boolif-op-arg expr 0)
131 'RTX 'DFLT expr 0 tstate appstuff))
132 (arg1 (-rtx-traverse (rtx-boolif-op-arg expr 1)
133 'RTX 'DFLT expr 1 tstate appstuff)))
134 (let ((no-side-effects-0? (not (rtx-side-effects? arg0)))
135 (no-side-effects-1? (not (rtx-side-effects? arg1))))
136 (cond ((and no-side-effects-0? (rtx-true? arg0))
138 ((and no-side-effects-0? (rtx-false? arg0))
139 (rtx-canonical-bool arg1))
140 ; Value of arg0 is unknown or has side-effects.
141 ((and no-side-effects-1? (rtx-true? arg1))
142 (if no-side-effects-0?
144 (rtx-make 'orif arg0 (rtx-true))))
145 ((and no-side-effects-1? (rtx-false? arg1))
148 (rtx-make 'orif arg0 arg1))))))
151 (let ((arg0 (-rtx-traverse (rtx-boolif-op-arg expr 0)
152 'RTX 'DFLT expr 0 tstate appstuff))
153 (arg1 (-rtx-traverse (rtx-boolif-op-arg expr 1)
154 'RTX 'DFLT expr 1 tstate appstuff)))
155 (let ((no-side-effects-0? (not (rtx-side-effects? arg0)))
156 (no-side-effects-1? (not (rtx-side-effects? arg1))))
157 (cond ((and no-side-effects-0? (rtx-false? arg0))
159 ((and no-side-effects-0? (rtx-true? arg0))
160 (rtx-canonical-bool arg1))
161 ; Value of arg0 is unknown or has side-effects.
162 ((and no-side-effects-1? (rtx-false? arg1))
163 (if no-side-effects-0?
165 (rtx-make 'andif arg0 (rtx-false))))
166 ((and no-side-effects-1? (rtx-true? arg1))
169 (rtx-make 'andif arg0 arg1))))))
171 ; Fold if's to their then or else part if we can determine the
172 ; result of the test.
175 ; ??? Was this but that calls rtx-traverse again which
176 ; resets the temp stack!
177 ; (rtx-simplify context (caddr expr))))
178 (-rtx-traverse (rtx-if-test expr) 'RTX 'DFLT expr 1 tstate appstuff)))
179 (cond ((rtx-true? test)
180 (-rtx-traverse (rtx-if-then expr) 'RTX mode expr 2 tstate appstuff))
182 (if (rtx-if-else expr)
183 (-rtx-traverse (rtx-if-else expr) 'RTX mode expr 3 tstate appstuff)
184 ; Sanity check, mode must be VOID.
185 (if (or (mode:eq? 'DFLT (rtx-mode expr))
186 (mode:eq? 'VOID (rtx-mode expr)))
188 (error "rtx-simplify: non-void-mode `if' missing `else' part" expr))))
190 ; We could traverse the then/else clauses here, but it's simpler
191 ; to have our caller do it. The cost is retraversing `test'.
195 (let ((name (rtx-name expr))
196 (cmp-mode (rtx-cmp-op-mode expr))
197 (arg0 (-rtx-traverse (rtx-cmp-op-arg expr 0) 'RTX
198 (rtx-cmp-op-mode expr)
199 expr 1 tstate appstuff))
200 (arg1 (-rtx-traverse (rtx-cmp-op-arg expr 1) 'RTX
201 (rtx-cmp-op-mode expr)
202 expr 2 tstate appstuff)))
203 (if (or (rtx-side-effects? arg0) (rtx-side-effects? arg1))
204 (rtx-make name cmp-mode arg0 arg1)
205 (case (rtx-const-equal arg0 arg1 (rtx-kind? 'ne expr))
209 ; That didn't work. See if we have an ifield/operand with a
210 ; known range of values.
211 (case (rtx-name arg0)
213 (let ((known-val (tstate-known-lookup tstate
214 (rtx-ifield-name arg0))))
215 (if (and known-val (rtx-kind? 'number-list known-val))
216 (case (rtx-const-list-equal arg1 known-val (rtx-kind? 'ne expr))
220 (rtx-make name cmp-mode arg0 arg1)))
221 (rtx-make name cmp-mode arg0 arg1))))
223 (let ((known-val (tstate-known-lookup tstate
224 (rtx-operand-name arg0))))
225 (if (and known-val (rtx-kind? 'number-list known-val))
226 (case (rtx-const-list-equal arg1 known-val (rtx-kind? 'ne expr))
230 (rtx-make name cmp-mode arg0 arg1)))
231 (rtx-make name cmp-mode arg0 arg1))))
233 (rtx-make name cmp-mode arg0 arg1))))))))
235 ; Recognize attribute requests of current-insn, current-mach.
237 (cond ((rtx-kind? 'current-mach (rtx-eq-attr-owner expr))
238 (rtx-simplify-eq-attr-mach expr (tstate-context tstate)))
239 ((rtx-kind? 'current-insn (rtx-eq-attr-owner expr))
240 (rtx-simplify-eq-attr-insn expr (tstate-owner tstate) (tstate-context tstate)))
244 (let ((known-val (tstate-known-lookup tstate (rtx-ifield-name expr))))
245 ; If the value is a single number, return that.
246 ; It can be one of several, represented as a number list.
247 (if (and known-val (rtx-constant? known-val))
248 known-val ; (rtx-make 'const 'INT known-val)
252 (let ((known-val (tstate-known-lookup tstate (rtx-operand-name expr))))
253 ; If the value is a single number, return that.
254 ; It can be one of several, represented as a number list.
255 (if (and known-val (rtx-constant? known-val))
256 known-val ; (rtx-make 'const 'INT known-val)
259 ; Leave EXPR unchanged and continue.
263 ; Simplify an rtl expression.
264 ; EXPR must be in source form.
265 ; The result is a possibly simplified EXPR, still in source form.
267 ; CONTEXT is a <context> object, used for error messages.
268 ; OWNER is the owner of the expression (e.g. <insn>) or #f if there is none.
270 ; KNOWN is an alist of known values. Each element is (name . value) where
271 ; NAME is an ifield/operand name and VALUE is a const/number-list rtx.
272 ; FIXME: Need ranges, later.
274 ; The following operations are performed:
275 ; - unselected machine dependent code is removed (eq-attr of (current-mach))
276 ; - if's are reduced to either then/else if we can determine that the test is
277 ; a compile-time constant
282 ; ??? Will become more intelligent as needed.
284 (define (rtx-simplify context owner expr known)
285 (-rtx-traverse expr #f 'DFLT #f 0
286 (tstate-make context owner
287 (/fastcall-make -simplify-expr-fn)
288 (rtx-env-empty-stack)
293 ; Utilities for equation solving.
294 ; ??? At the moment this is only focused on ifield assertions.
295 ; ??? That there exist more sophisticated versions than this one can take
296 ; as a given. This works for the task at hand and will evolve or be replaced
298 ; ??? This makes the simplifying assumption that no expr has side-effects.
300 ; Subroutine of rtx-solve.
301 ; This is the EXPR-FN argument to rtx-traverse.
303 (define (-solve-expr-fn rtx-obj expr mode parent-expr op-pos tstate appstuff)
307 ; Return a boolean indicating if {expr} equates to "true".
308 ; If the expression can't be reduced to #f/#t, return '?.
309 ; ??? Use rtx-eval instead of rtx-traverse?
311 ; EXPR must be in source form.
312 ; CONTEXT is a <context> object, used for error messages.
313 ; OWNER is the owner of the expression (e.g. <insn>) or #f if there is none.
314 ; KNOWN is an alist of known values. Each element is (name . value) where
315 ; NAME is an ifield/operand name and VALUE is a const/number-list rtx.
316 ; FIXME: Need ranges, later.
318 ; This is akin to rtx-simplify except it's geared towards solving ifield
319 ; assertions. It's not unreasonable to combine them. The worry is the
321 ; ??? Will become more intelligent as needed.
323 (define (rtx-solve context owner expr known)
324 ; First simplify, then solve.
325 (let* ((simplified-expr (rtx-simplify context owner expr known))
327 simplified-expr) ; FIXME: for now
328 ; (-rtx-traverse simplified-expr #f 'DFLT #f 0
329 ; (tstate-make context owner
330 ; (/fastcall-make -solve-expr-fn)
331 ; (rtx-env-empty-stack)
335 (cond ((rtx-true? maybe-solved-expr) #t)
336 ((rtx-false? maybe-solved-expr) #f)
340 ; Subroutine of -rtx-find-op to determine if two modes are equivalent.
341 ; Two modes are equivalent if they're equal, or if their sem-mode fields
344 (define (-rtx-mode-equiv? m1 m2)
346 (let ((mode1 (mode:lookup m1))
347 (mode2 (mode:lookup m2)))
348 (let ((s1 (mode:sem-mode mode1))
349 (s2 (mode:sem-mode mode2)))
350 (eq? (if s1 (obj:name s1) m1) (if s2 (obj:name s2) m2)))))
353 ; Subroutine of semantic-compile to find OP in OP-LIST.
354 ; OP-LIST is a list of operand expressions: (type expr mode name indx-sel).
355 ; The result is the list element or #f if not found.
356 ; TYPE is one of -op- reg mem.
357 ; EXPR is the constructed `xop' rtx expression for the operand,
358 ; ignored in the search.
359 ; MODE must match, as defined by -rtx-mode-equiv?.
360 ; NAME is the hardware element name, ifield name, or '-op-'.
361 ; INDX-SEL must match if present in either.
363 ; ??? Does this need to take "conditionally-referenced" into account?
365 (define (-rtx-find-op op op-list)
366 (let ((type (car op))
369 (indx-sel (car (cddddr op))))
370 ; The first cdr is to drop the dummy first arg.
371 (let loop ((op-list (cdr op-list)))
372 (cond ((null? op-list) #f)
373 ((eq? type (caar op-list))
374 (let ((try (car op-list)))
375 (if (and (eq? name (cadddr try))
376 (-rtx-mode-equiv? mode (caddr try))
377 (equal? indx-sel (car (cddddr try))))
379 (loop (cdr op-list)))))
380 (else (loop (cdr op-list))))))
383 ; Subroutine of semantic-compile to determine how the operand in
384 ; position OP-POS of EXPR is used.
385 ; The result is one of 'use, 'set, 'set-quiet.
386 ; "use" means "input operand".
388 (define (-rtx-ref-type expr op-pos)
389 ; operand 0 is the option list, operand 1 is the mode
390 ; (if you want to complain, fine, it's not like it would be unexpected)
394 ((set-quiet clobber) 'set-quiet)
399 ; Subroutine of semantic-compile:process-expr!, to simplify it.
400 ; Looks up the operand in the current set, returns it if found,
402 ; REF-TYPE is one of 'use, 'set, 'set-quiet.
403 ; Adds COND-CTI/UNCOND-CTI to SEM-ATTRS if the operand is a set of the pc.
405 (define (-build-operand! op-name op mode tstate ref-type op-list sem-attrs)
406 ;(display (list op-name mode ref-type)) (newline) (force-output)
407 (let* ((mode (mode-real-name (if (eq? mode 'DFLT)
410 ; The first #f is a placeholder for the object.
411 (try (list '-op- #f mode op-name #f))
412 (existing-op (-rtx-find-op try op-list)))
415 (memq ref-type '(set set-quiet)))
417 (list (if (tstate-cond? tstate) 'COND-CTI 'UNCOND-CTI))))
419 ; If already present, return the object, otherwise add it.
424 ; We can't set the operand number yet 'cus we don't know it.
425 ; However, when it's computed we'll need to set all associated
426 ; operands. This is done by creating shared rtx (a la gcc) - the
427 ; operand number then need only be updated in one place.
429 (let ((xop (op:new-mode op mode)))
430 (op:set-cond?! xop (tstate-cond? tstate))
431 ; Set the object rtx in `try', now that we have it.
432 (set-car! (cdr try) (rtx-make 'xop xop))
433 ; Add the operand to in/out-ops.
434 (append! op-list (list try))
438 ; Subroutine of semantic-compile:process-expr!, to simplify it.
440 (define (-build-reg-operand! expr tstate op-list)
441 (let* ((hw-name (rtx-reg-name expr))
442 (hw (current-hw-sem-lookup-1 hw-name)))
445 ; If the mode is DFLT, use the object's natural mode.
446 (let* ((mode (mode-real-name (if (eq? (rtx-mode expr) 'DFLT)
447 (obj:name (hw-mode hw))
449 (indx-sel (rtx-reg-index-sel expr))
450 ; #f is a place-holder for the object (filled in later)
451 (try (list 'reg #f mode hw-name indx-sel))
452 (existing-op (-rtx-find-op try op-list)))
454 ; If already present, return the object, otherwise add it.
459 (let ((xop (apply reg (cons (tstate->estate tstate)
461 (cons hw-name indx-sel))))))
462 (op:set-cond?! xop (tstate-cond? tstate))
463 ; Set the object rtx in `try', now that we have it.
464 (set-car! (cdr try) (rtx-make 'xop xop))
465 ; Add the operand to in/out-ops.
466 (append! op-list (list try))
469 (parse-error "FIXME" "unknown reg" expr)))
472 ; Subroutine of semantic-compile:process-expr!, to simplify it.
474 (define (-build-mem-operand! expr tstate op-list)
475 (let ((mode (rtx-mode expr))
476 (indx-sel (rtx-mem-index-sel expr)))
478 (if (memq mode '(DFLT VOID))
479 (parse-error "FIXME" "memory must have explicit mode" expr))
481 (let* ((try (list 'mem #f mode 'h-memory indx-sel))
482 (existing-op (-rtx-find-op try op-list)))
484 ; If already present, return the object, otherwise add it.
489 (let ((xop (apply mem (cons (tstate->estate tstate)
490 (cons mode indx-sel)))))
491 (op:set-cond?! xop (tstate-cond? tstate))
492 ; Set the object in `try', now that we have it.
493 (set-car! (cdr try) (rtx-make 'xop xop))
494 ; Add the operand to in/out-ops.
495 (append! op-list (list try))
499 ; Subroutine of semantic-compile:process-expr!, to simplify it.
501 (define (-build-ifield-operand! expr tstate op-list)
502 (let* ((f-name (rtx-ifield-name expr))
503 (f (current-ifld-lookup f-name)))
506 (parse-error "FIXME" "unknown ifield" f-name))
508 (let* ((mode (obj:name (ifld-mode f)))
509 (try (list '-op- #f mode f-name #f))
510 (existing-op (-rtx-find-op try op-list)))
512 ; If already present, return the object, otherwise add it.
517 (let ((xop (make <operand> f-name f-name
518 (atlist-cons (bool-attr-make 'SEM-ONLY #t)
520 (obj:name (ifld-hw-type f))
522 (make <hw-index> 'anonymous
523 'ifield (ifld-mode f) f)
525 (set-car! (cdr try) (rtx-make 'xop xop))
526 (append! op-list (list try))
530 ; Subroutine of semantic-compile:process-expr!, to simplify it.
532 ; ??? There are various optimizations (both space usage in ARGBUF and time
533 ; spent in semantic code) that can be done on code that uses index-of
534 ; (see i960's movq insn). Later.
536 (define (-build-index-of-operand! expr tstate op-list)
537 (if (not (and (rtx? (rtx-index-of-value expr))
538 (rtx-kind? 'operand (rtx-index-of-value expr))))
539 (parse-error "FIXME" "only `(index-of operand)' is currently supported"
542 (let ((op (rtx-operand-obj (rtx-index-of-value expr))))
543 (let ((indx (op:index op)))
544 (if (not (eq? (hw-index:type indx) 'ifield))
545 (parse-error "FIXME" "only ifield indices are currently supported"
547 (let* ((f (hw-index:value indx))
548 (f-name (obj:name f)))
549 ; The rest of this is identical to -build-ifield-operand!.
550 (let* ((mode (obj:name (ifld-mode f)))
551 (try (list '-op- #f mode f-name #f))
552 (existing-op (-rtx-find-op try op-list)))
554 ; If already present, return the object, otherwise add it.
559 (let ((xop (make <operand> f-name f-name
560 (atlist-cons (bool-attr-make 'SEM-ONLY #t)
562 (obj:name (ifld-hw-type f))
564 (make <hw-index> 'anonymous
567 ; (send (op:type op) 'get-index-mode)
570 (set-car! (cdr try) (rtx-make 'xop xop))
571 (append! op-list (list try))
575 ; Build the tstate known value list for INSN.
576 ; This built from the ifield-assertion list.
578 (define (insn-build-known-values insn)
579 (let ((expr (insn-ifield-assertion insn)))
581 (case (rtx-name expr)
583 (if (and (rtx-kind? 'ifield (rtx-cmp-op-arg expr 0))
584 (rtx-constant? (rtx-cmp-op-arg expr 1)))
585 (list (cons (rtx-ifield-name (rtx-cmp-op-arg expr 0))
586 (rtx-cmp-op-arg expr 1)))
589 (if (rtx-kind? 'ifield (rtx-member-value expr))
590 (list (cons (rtx-ifield-name (rtx-member-value expr))
591 (rtx-member-set expr)))
597 ; Structure to record the result of semantic-compile.
599 (define (csem-make compiled-code inputs outputs attributes)
600 (vector compiled-code inputs outputs attributes)
605 (define (csem-code csem) (vector-ref csem 0))
606 (define (csem-inputs csem) (vector-ref csem 1))
607 (define (csem-outputs csem) (vector-ref csem 2))
608 (define (csem-attrs csem) (vector-ref csem 3))
610 ; Traverse each element in SEM-CODE-LIST, converting them to canonical form,
611 ; and computing the input and output operands.
612 ; The result is an object of four elements (built with csem-make).
613 ; The first is a list of the canonical form of each element in SEM-CODE-LIST:
614 ; operand and ifield elements specified without `operand' or `ifield' have it
615 ; prepended, and operand numbers are computed for each operand.
616 ; Operand numbers are needed when emitting "write" handlers for LIW cpus.
617 ; Having the operand numbers available is also useful for efficient
618 ; modeling: recording operand references can be done with a bitmask (one host
619 ; insn), and the code to do the modeling can be kept out of the code that
621 ; The second is the list of input <operand> objects.
622 ; The third is the list of output <operand> objects.
623 ; The fourth is an <attr-list> object of attributes that can be computed from
625 ; The possibilities are: UNCOND-CTI, COND-CTI, SKIP-CTI, DELAY-SLOT.
626 ; ??? Combine *-CTI into an enum attribute.
628 ; CONTEXT is a <context> object or #f if there is none.
629 ; INSN is the <insn> object.
631 ; ??? Specifying operand ordinals in the source would simplify this and speed
632 ; it up. On the other hand that makes the source form more complex. Maybe the
633 ; complexity will prove necessary, but following the goal of "incremental
634 ; complication", we don't do this yet.
635 ; Another way to simplify this and speed it up would be to add lists of
636 ; input/output operands to the instruction description.
638 ; ??? This calls rtx-simplify which calls rtx-traverse as it's simpler to
639 ; simplify EXPR first, and then compile it. On the other hand it's slower
640 ; (two calls to rtx-traverse!).
642 ; FIXME: There's no need for sem-code-list to be a list.
643 ; The caller always passes (list (insn-semantics insn)).
645 (define (semantic-compile context insn sem-code-list)
646 (for-each (lambda (rtx) (assert (rtx? rtx)))
650 ; String for error messages.
651 ((errtxt "semantic compilation")
653 ; These record the result of traversing SEM-CODE-LIST.
654 ; They're lists of (type object mode name [args ...]).
655 ; TYPE is one of: -op- reg mem.
656 ; `-op-' is just something unique and is only used internally.
657 ; OBJECT is the constructed <operand> object.
658 ; The first element is just a dummy so that append! always works.
659 (in-ops (list (list #f)))
660 (out-ops (list (list #f)))
662 ; List of attributes computed from SEM-CODE-LIST.
663 ; The first element is just a dummy so that append! always works.
664 (sem-attrs (list #f))
666 ; Called for expressions encountered in SEM-CODE-LIST.
667 ; Don't waste cpu here, this is part of the slowest piece in CGEN.
669 (lambda (rtx-obj expr mode parent-expr op-pos tstate appstuff)
673 ((reg) (let ((ref-type (-rtx-ref-type parent-expr op-pos))
674 ; ??? could verify reg is a scalar
675 (regno (or (rtx-reg-number expr) 0)))
676 ; The register number is either a number or an
678 ; ??? This is a departure from GCC RTL that might have
679 ; significant ramifications. On the other hand in cases
680 ; where it matters the expression could always be
681 ; required to reduce to a constant (or some such).
682 (cond ((number? regno) #t)
684 (rtx-traverse-operands rtx-obj expr tstate appstuff))
685 (else (parse-error errtxt
686 "invalid register number"
688 (-build-reg-operand! expr tstate
689 (if (eq? ref-type 'use)
694 ((mem) (let ((ref-type (-rtx-ref-type parent-expr op-pos)))
695 (rtx-traverse-operands rtx-obj expr tstate appstuff)
696 (-build-mem-operand! expr tstate
697 (if (eq? ref-type 'use)
702 ((operand) (let ((op (rtx-operand-obj expr))
703 (ref-type (-rtx-ref-type parent-expr op-pos)))
704 (-build-operand! (obj:name op) op mode tstate ref-type
705 (if (eq? ref-type 'use)
710 ; Give operand new name.
711 ((name) (let ((result (-rtx-traverse (caddr expr) 'RTX mode
712 parent-expr op-pos tstate appstuff)))
713 (if (not (operand? result))
714 (error "name: invalid argument:" expr result))
715 (op:set-sem-name! result (cadr expr))
716 ; (op:set-num! result (caddr expr))
719 ; Specify a reference to a local variable
720 ((local) expr) ; nothing to do
722 ; Instruction fields.
723 ((ifield) (let ((ref-type (-rtx-ref-type parent-expr op-pos)))
724 (if (not (eq? ref-type 'use))
725 (parse-error errtxt "can't set an `ifield'" expr))
726 (-build-ifield-operand! expr tstate in-ops)))
729 ; For registers this is the register number.
730 ; For memory this is the address.
731 ; For constants, this is the constant.
732 ((index-of) (let ((ref-type (-rtx-ref-type parent-expr op-pos)))
733 (if (not (eq? ref-type 'use))
734 (parse-error errtxt "can't set an `index-of'" expr))
735 (-build-index-of-operand! expr tstate in-ops)))
737 ; Machine generate the SKIP-CTI attribute.
738 ((skip) (append! sem-attrs (list 'SKIP-CTI)) #f)
740 ; Machine generate the DELAY-SLOT attribute.
741 ((delay) (append! sem-attrs (list 'DELAY-SLOT)) #f)
743 ; If this is a syntax expression, the operands won't have been
744 ; processed, so tell our caller we want it to by returning #f.
745 ; We do the same for non-syntax expressions to keep things
746 ; simple. This requires collaboration with the traversal
747 ; handlers which are defined to do what we want if we return #f.
750 ; Whew. We're now ready to traverse the expression.
751 ; Traverse the expression recording the operands and building objects
752 ; for most elements in the source representation.
753 ; This also performs various simplifications.
754 ; In particular machine dependent code for non-selected machines
756 (compiled-exprs (map (lambda (expr)
760 (rtx-simplify context insn expr
761 (insn-build-known-values insn))
767 ;(display "in: ") (display in-ops) (newline)
768 ;(display "out: ") (display out-ops) (newline)
771 ; Now that we have the nub of all input and output operands,
772 ; we can assign operand numbers. Inputs and outputs are not defined
773 ; separately, output operand numbers follow inputs. This simplifies the
774 ; code which keeps track of such things: it can use one variable.
775 ; The assignment is defined to be arbitrary. If there comes a day
776 ; when we need to prespecify operand numbers, revisit.
777 ; The operand lists are sorted to avoid spurious differences in generated
778 ; code (for example unnecessary extra entries can be created in the
781 ; Drop dummy first arg and sort operand lists.
783 (alpha-sort-obj-list (map (lambda (op)
784 (rtx-xop-obj (cadr op)))
787 (alpha-sort-obj-list (map (lambda (op)
788 (rtx-xop-obj (cadr op)))
790 (sem-attrs (cdr sem-attrs)))
792 (let ((in-op-nums (iota (length sorted-ins)))
793 (out-op-nums (iota (length sorted-outs) (length sorted-ins))))
795 (for-each (lambda (op num) (op:set-num! op num))
796 sorted-ins in-op-nums)
797 (for-each (lambda (op num) (op:set-num! op num))
798 sorted-outs out-op-nums)
800 (let ((dump (lambda (op)
801 (string/symbol-append " "
804 (number->string (op:num op))
808 (map dump sorted-ins)
810 (map dump sorted-outs)
811 "End of operands.\n"))
813 (csem-make compiled-exprs sorted-ins sorted-outs
814 (atlist-parse sem-attrs "" "semantic attributes")))))
817 ; Traverse SEM-CODE-LIST, computing attributes derivable from it.
818 ; The result is an <attr-list> object of attributes that can be computed from
820 ; The possibilities are: UNCOND-CTI, COND-CTI, SKIP-CTI, DELAY-SLOT.
821 ; This computes the same values as semantic-compile, but for speed is
822 ; focused on attributes only.
823 ; ??? Combine *-CTI into an enum attribute.
825 ; CONTEXT is a <context> object or #f if there is none.
826 ; INSN is the <insn> object.
828 ; FIXME: There's no need for sem-code-list to be a list.
829 ; The caller always passes (list (insn-semantics insn)).
831 (define (semantic-attrs context insn sem-code-list)
832 (for-each (lambda (rtx) (assert (rtx? rtx)))
836 ; String for error messages.
837 ((errtxt "semantic attribute computation")
839 ; List of attributes computed from SEM-CODE-LIST.
840 ; The first element is just a dummy so that append! always works.
841 (sem-attrs (list #f))
843 ; Called for expressions encountered in SEM-CODE-LIST.
845 (lambda (rtx-obj expr mode parent-expr op-pos tstate appstuff)
848 ((operand) (if (and (eq? 'pc (obj:name (rtx-operand-obj expr)))
849 (memq (-rtx-ref-type parent-expr op-pos)
852 (if (tstate-cond? tstate)
853 ; Don't change these to '(FOO), since
856 (list 'UNCOND-CTI)))))
857 ((skip) (append! sem-attrs (list 'SKIP-CTI)) #f)
858 ((delay) (append! sem-attrs (list 'DELAY-SLOT)) #f)
860 ; If this is a syntax expression, the operands won't have been
861 ; processed, so tell our caller we want it to by returning #f.
862 ; We do the same for non-syntax expressions to keep things
863 ; simple. This requires collaboration with the traversal
864 ; handlers which are defined to do what we want if we return #f.
867 ; Traverse the expression recording the attributes.
868 (traversed-exprs (map (lambda (expr)
872 (rtx-simplify context insn expr
873 (insn-build-known-values insn))
880 ; Drop dummy first arg.
881 ((sem-attrs (cdr sem-attrs)))
882 (atlist-parse sem-attrs "" "semantic attributes")))