1 ; RTL->C translation support.
2 ; Copyright (C) 2000, 2005, 2009, 2010 Red Hat, Inc.
3 ; This file is part of CGEN.
4 ; See file COPYING.CGEN for details.
6 ; Generating C from RTL
7 ; ---------------------
8 ; The main way to generate C code from an RTL expression is:
10 ; (rtl-c-parsed mode isa-name-list nil '(func mode ...))
13 ; (rtl-c-parsed SI (all) nil '(add () SI (const () SI 1) (const () SI 2)))
17 ; The expression is in source form and must be already canonicalized (with
18 ; rtx-canonicalize). There is also rtl-c for the occasions where the rtl
19 ; isn't already canonicalized.
21 ; The `set' rtx needs to be handled a little carefully.
22 ; Both the dest and src are processed first, and then code to perform the
23 ; assignment is computed. However, the dest may require more than a simple
24 ; C assignment. Therefore set dests are converted to the specified object
25 ; (e.g. a hardware operand) and then a message is sent to this object to
26 ; perform the actual code generation.
28 ; All interesting operands (e.g. regs, mem) are `operand' objects.
29 ; The following messages must be supported by operand objects.
30 ; - get-mode - return mode of operand
31 ; - cxmake-get - return <c-expr> object containing operand's value
32 ; - gen-set-quiet - return string of C code to set operand's value (no tracing)
33 ; - gen-set-trace - return string of C code to set operand's value
35 ; Instruction fields are refered to by name.
36 ; Instruction ifields must have these methods:
40 ; Conventions used in this file:
43 ; The <c-expr> object.
44 ; This is a fully translated expression (i.e. C code).
47 (class-make '<c-expr> nil
51 ; The translated C code.
53 ; The source expression, for debugging.
55 ; Attributes of the expression.
57 ; List of temporaries required to compute the expression.
58 ; ??? wip. These would be combined as the expression is
59 ; built up. Then in sets and other statements, the temporaries
68 (lambda (self mode c-code atlist)
69 ; FIXME: Extend COS to allow specifying member predicates.
71 (assert (string? c-code))
72 ;(assert (atlist? atlist)) ; FIXME: What should this be?
73 (elm-set! self 'mode mode)
74 (elm-set! self 'c-code c-code)
75 (elm-set! self 'atlist atlist)
81 (define cx:mode (elm-make-getter <c-expr> 'mode))
82 (define cx:c-code (elm-make-getter <c-expr> 'c-code))
83 (define cx:expr (elm-make-getter <c-expr> 'expr))
84 (define cx:atlist (elm-make-getter <c-expr> 'atlist))
85 ;(define cx:tmps (elm-make-getter <c-expr> 'tmps))
87 ; Any object with attributes requires the get-atlist method.
89 (method-make! <c-expr> 'get-atlist (lambda (self) (elm-get self 'atlist)))
91 ; Respond to 'get-mode messages.
93 (method-make! <c-expr> 'get-mode (lambda (self) (elm-get self 'mode)))
95 ; Respond to 'get-name messages for rtx-dump.
100 (string-append "(" (obj:str-name (elm-get self 'mode)) ") "
104 ; Return C code to perform an assignment.
105 ; NEWVAL is a <c-expr> object of the value to be assigned to SELF.
107 (method-make! <c-expr> 'gen-set-quiet
108 (lambda (self estate mode indx selector newval)
109 (string-append " " (cx:c self) " = " (cx:c newval) ";\n"))
112 (method-make! <c-expr> 'gen-set-trace
113 (lambda (self estate mode indx selector newval)
114 (string-append " " (cx:c self) " = " (cx:c newval) ";\n"))
117 ; Return the C code of CX.
118 ; ??? This used to handle lazy evaluation of the expression.
119 ; Maybe it will again, so it's left in, as a cover fn to cx:c-code.
125 ; Main routine to create a <c-expr> node object.
126 ; MODE is either the mode's symbol (e.g. 'QI) or a <mode> object.
127 ; CODE is a string of C code.
129 (define (cx:make mode code)
130 (make <c-expr> (mode-maybe-lookup mode) code nil)
133 ; Make copy of CX in new mode MODE.
134 ; MODE must be a <mode> object.
136 (define (cx-new-mode mode cx)
137 (make <c-expr> mode (cx:c cx) (cx:atlist cx))
140 ; Same as cx:make except with attributes.
142 (define (cx:make-with-atlist mode code atlist)
143 (make <c-expr> (mode-maybe-lookup mode) code atlist)
146 ; Return a boolean indicated if X is a <c-expr> object.
148 (define (c-expr? x) (class-instance? <c-expr> x))
150 ; RTX environment support.
153 <rtx-temp> 'cxmake-get
154 (lambda (self estate mode indx selector)
155 (cx:make mode (rtx-temp-value self)))
159 <rtx-temp> 'gen-set-quiet
160 (lambda (self estate mode indx selector src)
161 (string-append " " (rtx-temp-value self) " = " (cx:c src) ";\n"))
165 <rtx-temp> 'gen-set-trace
166 (lambda (self estate mode indx selector src)
167 (string-append " " (rtx-temp-value self) " = " (cx:c src) ";\n"))
170 (define (gen-temp-defs estate env)
171 (string-map (lambda (temp)
172 (let ((temp-obj (cdr temp)))
173 (string-append " " (mode:c-type (rtx-temp-mode temp-obj))
174 " " (rtx-temp-value temp-obj) ";\n")))
178 ; Top level routines to handle rtl->c translation.
180 ; rtl->c configuration parameters
182 ; #t -> emit calls to rtl cover fns, otherwise emit plain C where possible.
183 (define /rtl-c-rtl-cover-fns? #f)
185 ; Called before emitting code to configure the generator.
186 ; ??? I think this can go away now (since cover-fn specification is also
187 ; done at each call to rtl-c).
189 (define (rtl-c-config! . args)
190 (set! /rtl-c-rtl-cover-fns? #f)
191 (let loop ((args args))
197 (set! /rtl-c-rtl-cover-fns? (cadr args)))
198 (else (error "rtl-c-config: unknown option:" (car args))))
199 (loop (cddr args)))))
203 ; Subclass of <eval-state> to record additional things needed for rtl->c.
205 (define <rtl-c-eval-state>
206 (class-make '<rtl-c-eval-state> '(<eval-state>)
208 ; #t -> emit calls to rtl cover fns.
209 (rtl-cover-fns? . #f)
211 ; name of output language, "c" or "c++"
212 (output-language . "c")
214 ; #t if generating code for a macro.
215 ; Each newline is then preceeded with '\\'.
218 ; Boolean indicating if evaluation is for an instruction.
219 ; It's not always possible to look at OWNER, e.g. when we're
220 ; processing semantic fragments.
223 ; #f -> reference ifield values using FLD macro.
224 ; #t -> use C variables.
225 ; ??? This is only needed to get correct ifield references
226 ; in opcodes, decoder, and semantics. Maybe a better way to
227 ; go would be to specify the caller's name so there'd be just
228 ; one of these, rather than an increasing number. However,
229 ; for now either way is the same.
230 ; An alternative is to specify a callback to try first.
236 ; FIXME: involves upcasting.
237 (define-getters <rtl-c-eval-state> estate
238 (rtl-cover-fns? output-language macro? for-insn? ifield-var?)
241 ; Return booleans indicating if output language is C/C++.
243 (define (estate-output-language-c? estate)
244 (string=? (estate-output-language estate) "c")
246 (define (estate-output-language-c++? estate)
247 (string=? (estate-output-language estate) "c++")
251 <rtl-c-eval-state> 'vmake!
253 ; Initialize parent class first.
254 (let loop ((args (send-next self '<rtl-c-eval-state> 'vmake! args))
257 (reverse! unrecognized) ; ??? Could invoke method to initialize here.
261 (elm-set! self 'rtl-cover-fns? (cadr args)))
263 (elm-set! self 'output-language (cadr args)))
265 (elm-set! self 'macro? (cadr args)))
267 (elm-set! self 'for-insn? (cadr args)))
269 (elm-set! self 'ifield-var? (cadr args)))
271 ; Build in reverse order, as we reverse it back when we're done.
273 (cons (cadr args) (cons (car args) unrecognized)))))
274 (loop (cddr args) unrecognized)))))
277 ;; Build an estate for use in generating C.
278 ;; OVERRIDES is a #:keyword/value list of parameters to apply last.
280 (define (estate-make-for-rtl-c overrides)
285 #:expr-fn (lambda (rtx-obj expr mode estate)
286 (rtl-c-generator rtx-obj))
287 #:rtl-cover-fns? /rtl-c-rtl-cover-fns?)
291 ; Translate RTL expression EXPR to C.
292 ; ESTATE is the current rtx evaluation state.
293 ; MODE is a <mode> object.
295 (define (rtl-c-with-estate estate mode expr)
296 (cx:c (rtl-c-get estate mode (rtx-eval-with-estate expr mode estate)))
299 ; Translate parsed RTL expression X to a string of C code.
300 ; EXPR must have already been fed through rtx-canonicalize.
301 ; MODE is the desired mode of the value or DFLT for "natural mode".
302 ; MODE is a <mode> object.
303 ; OVERRIDES is a #:keyword/value list of arguments to build the eval state
306 (define (rtl-c-parsed mode expr . overrides)
307 ;; ??? If we're passed insn-compiled-semantics the output of xops is
308 ;; confusing. Fix by subclassing <operand> -> <xoperand>, and
309 ;; have <xoperand> provide original source expr.
310 (let ((estate (estate-make-for-rtl-c (cons #:outer-expr
311 (cons expr overrides)))))
312 (rtl-c-with-estate estate mode expr))
315 ; Same as rtl-c-parsed but EXPR is unparsed.
316 ; ISA-NAME-LIST is the list of ISA(s) in which to evaluate EXPR.
317 ; EXTRA-VARS-ALIST is an association list of extra (symbol <mode> value)
318 ; elements to be used during value lookup.
319 ; MODE is a <mode> object.
321 (define (rtl-c mode isa-name-list extra-vars-alist expr . overrides)
322 (let* ((canonical-rtl (rtx-canonicalize #f (obj:name mode)
323 isa-name-list extra-vars-alist expr))
324 (estate (estate-make-for-rtl-c (cons #:outer-expr
325 (cons canonical-rtl overrides)))))
326 (rtl-c-with-estate estate mode canonical-rtl))
329 ; Same as rtl-c-with-estate except return a <c-expr> object.
330 ; MODE is a <mode> object.
332 (define (rtl-c-expr-with-estate estate mode expr)
333 (rtl-c-get estate mode (rtx-eval-with-estate expr mode estate))
336 ; Same as rtl-c-parsed except return a <c-expr> object.
337 ; MODE is a <mode> object.
339 (define (rtl-c-expr-parsed mode expr . overrides)
340 ;; ??? If we're passed insn-compiled-semantics the output of xops is
341 ;; confusing. Fix by subclassing <operand> -> <xoperand>, and
342 ;; have <xoperand> provide original source expr.
343 (let ((estate (estate-make-for-rtl-c (cons #:outer-expr
344 (cons expr overrides)))))
345 (rtl-c-expr-with-estate estate mode expr))
348 ; Same as rtl-c-expr-parsed but EXPR is unparsed.
349 ; MODE is a <mode> object.
351 (define (rtl-c-expr mode isa-name-list extra-vars-alist expr . overrides)
352 (let* ((canonical-rtl (rtx-canonicalize #f (obj:name mode)
353 isa-name-list extra-vars-alist expr))
354 (estate (estate-make-for-rtl-c (cons #:outer-expr
355 (cons canonical-rtl overrides)))))
356 (rtl-c-expr-with-estate estate mode canonical-rtl))
359 ; C++ versions of rtl-c routines.
361 ; Build an estate for use in generating C++.
362 ; OVERRIDES is a #:keyword/value list of parameters to apply last.
364 (define (estate-make-for-rtl-c++ overrides)
365 (estate-make-for-rtl-c (cons #:output-language (cons "c++" overrides)))
368 ; Translate parsed RTL expression X to a string of C++ code.
369 ; EXPR must have already been fed through rtx-canonicalize.
370 ; MODE is the desired mode of the value or DFLT for "natural mode".
371 ; MODE is a <mode> object.
372 ; OVERRIDES is a #:keyword/value list of arguments to build the eval state
375 (define (rtl-c++-parsed mode expr . overrides)
376 ;; ??? If we're passed insn-compiled-semantics the output of xops is
377 ;; confusing. Fix by subclassing <operand> -> <xoperand>, and
378 ;; have <xoperand> provide original source expr.
379 (let ((estate (estate-make-for-rtl-c++ (cons #:outer-expr
380 (cons expr overrides)))))
381 (rtl-c-with-estate estate mode expr))
384 ; Same as rtl-c++-parsed but EXPR is unparsed.
385 ; MODE is a <mode> object.
387 (define (rtl-c++ mode isa-name-list extra-vars-alist expr . overrides)
388 (let* ((canonical-rtl (rtx-canonicalize #f (obj:name mode)
389 isa-name-list extra-vars-alist expr))
390 (estate (estate-make-for-rtl-c++ (cons #:outer-expr
391 (cons canonical-rtl overrides)))))
392 (rtl-c-with-estate estate mode canonical-rtl))
395 ; Top level routines for getting/setting values.
397 ; Return a <c-expr> node to get the value of SRC in mode MODE.
398 ; ESTATE is the current rtl evaluation state.
399 ; MODE is a <mode> object.
402 ; - rtl expression (e.g. '(add WI dr sr))
403 ; - sequence's local variable name
404 ; - sequence's local variable object
408 ; - a string of C code
409 ; FIXME: Reduce acceptable values of SRC.
410 ; The result has mode MODE, unless MODE is the "default mode indicator"
411 ; (DFLT) in which case the mode of the result is derived from SRC.
413 ; ??? mode compatibility checks are wip
415 (define (/rtl-c-get estate mode src)
416 (let ((mode mode)) ;;(mode:lookup mode)))
419 (cond ((or (mode:eq? 'VOID mode)
420 (mode:eq? 'DFLT mode)
421 (mode:eq? (cx:mode src) mode))
423 ((rtx-mode-compatible? mode (cx:mode src))
424 (cx-new-mode mode src))
428 (string-append "incompatible mode: "
429 "(" (obj:str-name (cx:mode src)) " vs "
430 (obj:str-name mode) ") in "
431 "\"" (cx:c src) "\"")
434 ; The recursive call to /rtl-c-get is in case the result of rtx-eval
435 ; is a hardware object, rtx-func object, or another rtl expression.
438 (let ((evald-src (rtx-eval-with-estate src mode estate)))
439 ; There must have been some change, otherwise we'll loop forever.
440 (assert (not (eq? src evald-src)))
441 (/rtl-c-get estate mode evald-src)))
443 ;; FIXME: Can we ever get a symbol here?
444 ((or (and (symbol? src) (current-op-lookup src))
448 (set! src (current-op-lookup src)))
449 (cond ((mode:eq? 'DFLT mode)
450 ; FIXME: Can we get called with 'DFLT anymore?
451 ; FIXME: If we fetch the mode here, operands can assume
452 ; they never get called with "default mode".
453 (send src 'cxmake-get estate mode #f #f))
454 ((rtx-mode-compatible? mode (op:mode src))
455 (let ((mode (op:mode src))) ;; FIXME: (rtx-sem-mode mode)))
456 (send src 'cxmake-get estate mode #f #f)))
458 ;; FIXME: canonicalization should have already caught this
461 (string-append "operand " (obj:str-name src)
462 " referenced in incompatible mode")
465 ;; FIXME: Can we ever get a symbol here?
466 ((or (and (symbol? src) (rtx-temp-lookup (estate-env-stack estate) src))
470 (set! src (rtx-temp-lookup (estate-env-stack estate) src)))
471 (cond ((mode:eq? 'DFLT mode)
472 (send src 'cxmake-get estate (rtx-temp-mode src) #f #f))
473 ((rtx-mode-compatible? mode (rtx-temp-mode src))
474 (let ((mode (rtx-temp-mode src))) ;; FIXME: (rtx-sem-mode mode)))
475 (send src 'cxmake-get estate mode #f #f)))
477 ;; FIXME: canonicalization should have already caught this
480 (string-append "sequence temp " (rtx-temp-name src)
481 " referenced in incompatible mode")
485 ; Default mode of integer argument is INT.
486 (if (or (mode:eq? 'DFLT mode) (mode:eq? 'VOID mode))
487 (cx:make INT (number->string src))
488 (cx:make mode (number->string src))))
491 ; Default mode of string argument is INT.
492 (if (or (mode:eq? 'DFLT mode) (mode:eq? 'VOID mode))
496 (else (estate-error estate "/rtl-c-get: invalid argument" src))))
499 ;; MODE is either a <mode> object or the mode name.
501 (define (rtl-c-get estate mode src)
502 (let ((mode (mode-maybe-lookup mode)))
503 (logit 4 (spaces (estate-depth estate))
504 "(rtl-c-get " (mode-real-name mode) " " (rtx-strdump src) ")\n")
505 (let ((result (/rtl-c-get estate mode src)))
506 (logit 4 (spaces (estate-depth estate))
507 "(rtl-c-get " (mode-real-name mode) " " (rtx-strdump src) ") => "
512 ; Return a <c-expr> object to set the value of DEST to SRC.
513 ; ESTATE is the current rtl evaluation state.
514 ; MODE is the mode of DEST or DFLT which means fetch the real mode from DEST.
515 ; MODE is either a <mode> object or the mode name.
518 ; - rtl expression (e.g. '(mem QI dr))
519 ; SRC is an RTX expression. It is important that we evaluate it, instead of
520 ; our caller, because only we know the mode of DEST (which we need to pass
521 ; when evaluating SRC if MODE is DFLT). ??? Can no longer get DFLT, but
522 ; it feels right to continue to evaluate SRC here.
523 ; The mode of the result is always VOID (void).
525 ; ??? One possible optimization is to pass the address of the result
526 ; to the computation of SRC. Seems dodgey though.
528 (define (rtl-c-set-quiet estate mode dest src)
529 ;(display (list 'rtl-c-set-quiet mode dest src)) (newline)
530 (let* ((mode (mode-maybe-lookup mode))
531 (xdest (cond ((c-expr? dest)
534 (rtx-eval-with-estate dest mode estate))
537 "rtl-c-set-quiet: invalid dest"
539 (assert (mode? mode))
540 (if (not (object? xdest))
541 (estate-error estate "rtl-c-set-quiet: invalid dest" dest))
542 (cx:make VOID (send xdest 'gen-set-quiet
544 (rtl-c-get estate mode src))))
547 ; Same as rtl-c-set-quiet except also print TRACE_RESULT message.
548 ; MODE is either a <mode> object or the mode name.
549 ; ??? One possible change is to defer the (rtl-c-get src) call to dest's
550 ; set handler. Such sources would be marked accordingly and rtl-c-get
551 ; would recognize them. This would allow, for example, passing the address
552 ; of the result to the computation.
554 (define (rtl-c-set-trace estate mode dest src)
555 ;(display (list 'rtl-c-set-trace mode dest src)) (newline)
556 (let* ((mode (mode-maybe-lookup mode))
557 (xdest (cond ((c-expr? dest)
560 (rtx-eval-with-estate dest mode estate))
563 "rtl-c-set-trace: invalid dest"
565 (assert (mode? mode))
566 (if (not (object? xdest))
567 (estate-error estate "rtl-c-set-trace: invalid dest" dest))
568 (cx:make VOID (send xdest 'gen-set-trace
570 (rtl-c-get estate mode src))))
573 ; Emit C code for each rtx function.
575 ; Table mapping rtx function to C generator.
577 (define /rtl-c-gen-table #f)
579 ; Return the C generator for <rtx-func> F.
581 (define (rtl-c-generator f)
582 (vector-ref /rtl-c-gen-table (rtx-num f))
585 ; Support for explicit C/C++ code.
586 ; MODE is the mode name.
587 ; ??? Actually, "support for explicit foreign language code".
588 ; s-c-call needs a better name but "unspec" seems like obfuscation.
589 ; ??? Need to distinguish owner of call (cpu, ???).
591 (define (s-c-call estate mode name . args)
594 (if (estate-output-language-c++? estate)
595 (string-append "current_cpu->" name " (")
596 ; FIXME: Prepend @cpu@_ to name here, and delete @cpu@_ from
598 (string-append name " (current_cpu"))
600 (string-map (lambda (arg)
603 (cx:c (rtl-c-get estate DFLT arg))))
605 (if (estate-output-language-c++? estate)
606 (string-drop 2 c-args)
608 ; If the mode is VOID, this is a statement.
609 ; Otherwise it's an expression.
610 ; ??? Bad assumption! VOID expressions may be used
611 ; within sequences without local vars, which are translated
612 ; to comma-expressions.
613 (if (or (mode:eq? 'DFLT mode) ;; FIXME: can't get DFLT anymore
614 (mode:eq? 'VOID mode))
620 ; Same as c-call except there is no particular owner of the call.
621 ; In general this means making a call to a non-member function,
622 ; whereas c-call makes calls to member functions (in C++ parlance).
623 ; MODE is the mode name.
625 (define (s-c-raw-call estate mode name . args)
630 (string-map (lambda (elm)
632 ", " (cx:c (rtl-c-get estate DFLT elm))))
634 ; If the mode is VOID, this is a statement.
635 ; Otherwise it's an expression.
636 ; ??? Bad assumption! VOID expressions may be used
637 ; within sequences without local vars, which are translated
638 ; to comma-expressions.
639 (if (or (mode:eq? 'DFLT mode) ;; FIXME: can't get DFLT anymore
640 (mode:eq? 'VOID mode))
646 ; Standard arithmetic operations.
648 ; Return a boolean indicating if a cover function/macro should be emitted
649 ; to perform an operation.
650 ; C-OP is a string containing the C operation or #f if there is none.
651 ; MODE is the mode of the operation.
653 (define (/rtx-use-sem-fn? estate c-op mode)
654 ; If no C operation has been provided, use a macro, or
655 ; if this is the simulator and MODE is not a host mode, use a macro.
657 ; (and (estate-rtl-cover-fns? estate)
658 ; (not (mode:host? mode))))
659 ; FIXME: The current definition is a temporary hack while host/target-ness
660 ; of INT/UINT is unresolved.
661 (and (not (obj-has-attr? mode 'FORCE-C))
663 (and (estate-rtl-cover-fns? estate)
664 ;; NOTE: We can't check (insn? (estate-owner estate)) here.
665 ;; It's not necessarily present for semantic fragments.
666 (or (estate-for-insn? estate)
667 (not (mode:host? mode))))))
670 ; One operand referenced, result is in same mode.
671 ; MODE is the mode name.
673 (define (s-unop estate name c-op mode src)
674 (let* ((val (rtl-c-get estate mode src))
675 ; Refetch mode in case it was DFLT and ensure unsigned->signed.
676 (mode (mode:lookup mode)) ;;(cx:mode val)) ;; FIXME: can't get DFLT anymore
677 (sem-mode (rtx-sem-mode mode)))
678 ; FIXME: Argument checking.
680 (if (/rtx-use-sem-fn? estate c-op mode)
681 (if (mode-float? mode)
683 (string-append "CGEN_CPU_FPU (current_cpu)->ops->"
684 (string-downcase name)
685 (string-downcase (obj:str-name sem-mode))
686 " (CGEN_CPU_FPU (current_cpu), "
689 (string-append name (obj:str-name sem-mode)
690 " (" (cx:c val) ")")))
691 (cx:make mode ; not sem-mode on purpose
692 (string-append "(" c-op " ("
696 ; Two operands referenced in the same mode producing a result in the same mode.
697 ; MODE is the mode name.
699 ; ??? Will eventually want to handle floating point modes specially. Since
700 ; bigger modes may get clumsily passed (there is no pass by reference in C) and
701 ; since we want to eventually handle lazy transformation, FP values could be
702 ; passed by reference. This is easy in C++. C requires more work and is
703 ; defered until it's warranted.
704 ; Implementing this should probably be via a new cxmake-get-ref method,
705 ; rather then complicating cxmake-get. Ditto for rtl-c-get-ref/rtl-c-get.
707 (define (s-binop estate name c-op mode src1 src2)
708 ;(display (list "binop " name ", mode " mode)) (newline)
709 (let* ((val1 (rtl-c-get estate mode src1))
710 ; Refetch mode in case it was DFLT and ensure unsigned->signed.
711 (mode (mode:lookup mode)) ;;(cx:mode val1)) ;; FIXME: can't get DFLT anymore
712 (sem-mode (rtx-sem-mode mode))
713 (val2 (rtl-c-get estate mode src2)))
714 ; FIXME: Argument checking.
716 (if (/rtx-use-sem-fn? estate c-op mode)
717 (if (mode-float? mode)
719 (string-append "CGEN_CPU_FPU (current_cpu)->ops->"
720 (string-downcase name)
721 (string-downcase (obj:str-name sem-mode))
722 " (CGEN_CPU_FPU (current_cpu), "
726 (string-append name (obj:str-name sem-mode)
727 " (" (cx:c val1) ", "
729 (cx:make mode ; not sem-mode on purpose
737 ; Same as s-binop except there's a third argument which is always one bit.
738 ; MODE is the mode name.
740 (define (s-binop-with-bit estate name mode src1 src2 src3)
741 (let* ((val1 (rtl-c-get estate mode src1))
742 ; Refetch mode in case it was DFLT and ensure unsigned->signed.
743 (mode (mode:lookup mode)) ;;(cx:mode val1)) ;; FIXME: can't get DFLT anymore
744 (sem-mode (rtx-sem-mode mode))
745 (val2 (rtl-c-get estate mode src2))
746 (val3 (rtl-c-get estate 'BI src3)))
747 ; FIXME: Argument checking.
750 (string-append name (obj:str-name sem-mode)
758 ; Shift operations are slightly different than binary operations:
759 ; the mode of src2 is any integral mode.
760 ; MODE is the mode name.
761 ; ??? Note that some cpus have a signed shift left that is semantically
762 ; different from a logical one. May need to create `sla' some day. Later.
764 (define (s-shop estate name c-op mode src1 src2)
765 ;(display (list "shop " name ", mode " mode)) (newline)
766 (let* ((val1 (rtl-c-get estate mode src1))
767 ; Refetch mode in case it was DFLT and ensure unsigned->signed
768 ; [sign of operation is determined from operation name, not mode].
769 (mode (mode:lookup mode)) ;;(cx:mode val1)) ;; FIXME: can't get DFLT anymore
770 (sem-mode (rtx-sem-mode mode))
771 (val2 (rtl-c-get estate mode src2)))
772 ; FIXME: Argument checking.
774 (if (/rtx-use-sem-fn? estate c-op mode)
776 (string-append name (obj:str-name sem-mode)
777 " (" (cx:c val1) ", "
779 (cx:make mode ; not sem-mode on purpose
781 ;; Ensure correct sign of shift.
782 (cond ((equal? name "SRL")
785 (cond ((mode-unsigned? mode) (mode:c-type mode))
786 ((mode:eq? mode 'INT) (mode:c-type UINT))
787 (else (mode:c-type (mode-find (mode:bits mode) 'UINT))))
792 (cond ((mode-signed? mode) (mode:c-type mode))
793 ((mode:eq? mode 'UINT) (mode:c-type INT))
794 (else (mode:c-type (mode-find (mode:bits mode) 'INT))))
796 ;; May wish to make this unsigned if not
801 " (" (cx:c val2) "))"))))
804 ; Process andif, orif.
805 ; SRC1 and SRC2 have any arithmetic mode.
806 ; MODE is the mode name.
807 ; The result has mode BI.
808 ; ??? May want to use INT as BI may introduce some slowness
809 ; in the generated code.
811 (define (s-boolifop estate name c-op src1 src2)
812 (let* ((val1 (rtl-c-get estate DFLT src1))
813 (val2 (rtl-c-get estate DFLT src2)))
814 ; FIXME: Argument checking.
816 ; If this is the simulator and MODE is not a host mode, use a macro.
817 ; ??? MODE here being the mode of SRC1. Maybe later.
818 (if (estate-rtl-cover-fns? estate)
819 (cx:make (mode:lookup 'BI)
820 (string-append name ; "BI", leave off mode, no need for it
821 " (" (cx:c val1) ", "
823 (cx:make (mode:lookup 'BI)
832 ; MODE is the mode name.
834 (define (s-convop estate name mode s1)
835 ; Get S1 in its normal mode, then convert.
836 (let ((s (rtl-c-get estate DFLT s1))
837 (mode (mode:lookup mode)))
838 (if (and (not (estate-rtl-cover-fns? estate))
839 (mode:host? (cx:mode s)))
841 (string-append "((" (obj:str-name mode) ")"
842 " (" (obj:str-name (cx:mode s)) ")"
844 (if (or (mode-float? mode)
845 (mode-float? (cx:mode s)))
847 (string-append "CGEN_CPU_FPU (current_cpu)->ops->"
848 (string-downcase name)
849 (string-downcase (obj:str-name (rtx-sem-mode (cx:mode s))))
850 (string-downcase (obj:str-name (rtx-sem-mode mode)))
851 " (CGEN_CPU_FPU (current_cpu), "
855 (obj:str-name (rtx-sem-mode (cx:mode s)))
856 (obj:str-name (rtx-sem-mode mode))
857 " (" (cx:c s) ")")))))
860 ; Compare SRC1 and SRC2 in mode MODE.
861 ; NAME is one of eq,ne,lt,le,gt,ge,ltu,leu,gtu,geu.
862 ; MODE is the mode name.
863 ; The result has mode BI.
864 ; ??? May want a host int mode result as BI may introduce some slowness
865 ; in the generated code.
867 (define (s-cmpop estate name c-op mode src1 src2)
868 (let* ((val1 (rtl-c-get estate mode src1))
869 ; Refetch mode in case it was DFLT.
870 (mode (mode:lookup mode)) ;;(cx:mode val1)) ;; FIXME: can't get DFLT anymore
871 (val2 (rtl-c-get estate mode src2)))
872 ; FIXME: Argument checking.
874 ; If no C operation has been provided, use a macro, or
875 ; if this is the simulator and MODE is not a host mode, use a macro.
876 (if (/rtx-use-sem-fn? estate c-op mode)
877 (if (mode-float? mode)
878 (cx:make (mode:lookup 'BI)
879 (string-append "CGEN_CPU_FPU (current_cpu)->ops->"
880 (string-downcase (symbol->string name))
881 (string-downcase (obj:str-name (rtx-sem-mode mode)))
882 " (CGEN_CPU_FPU (current_cpu), "
885 (cx:make (mode:lookup 'BI)
886 (string-append (string-upcase (symbol->string name))
887 (if (memq name '(eq ne))
888 (obj:str-name (rtx-sem-mode mode))
890 " (" (cx:c val1) ", "
892 (cx:make (mode:lookup 'BI)
900 ; Conditional execution.
902 ; `if' in RTL has a result, like ?: in C.
903 ; We support both: one with a result (non VOID mode), and one without (VOID mode).
904 ; The non-VOID case must have an else part.
905 ; MODE is the mode of the result, not the comparison.
906 ; MODE is the mode name.
907 ; The comparison is expected to return a zero/non-zero value.
908 ; ??? Perhaps this should be a syntax-expr. Later.
910 (define (s-if estate mode cond then . else)
911 (if (> (length else) 1)
912 (estate-error estate "if: too many elements in `else' part" else))
914 (if (or (mode:eq? 'DFLT mode) ;; FIXME: can't get DFLT anymore
915 (mode:eq? 'VOID mode))
917 (string-append "if (" (cx:c (rtl-c-get estate DFLT cond)) ")"
918 " {\n" (cx:c (rtl-c-get estate mode then)) "}"
919 (if (not (null? else))
920 (string-append " else {\n"
921 (cx:c (rtl-c-get estate mode (car else)))
925 (if (= (length else) 1)
928 (cx:c (rtl-c-get estate DFLT cond))
930 (cx:c (rtl-c-get estate mode then))
932 (cx:c (rtl-c-get estate mode (car else)))
934 (estate-error estate "non-void-mode `if' must have `else' part"))))
938 ; MODE is the mode name.
939 ; If MODE is VOID emit a series of if/else's.
940 ; If MODE is not VOID, emit a series of ?:'s.
941 ; COND-CODE-LIST is a list of lists, each sublist is a list of two elements:
942 ; condition, code. The condition part must return a zero/non-zero value, and
943 ; the code part is treated as a `sequence'.
944 ; This defer argument evaluation, the syntax
945 ; ((... condition ...) ... action ...)
946 ; needs special parsing.
947 ; FIXME: Need more error checking of arguments.
949 (define (s-cond estate mode . cond-code-list)
950 ;; FIXME: can't get DFLT anymore
951 (let ((vm? (or (mode:eq? 'DFLT mode) (mode:eq? 'VOID mode))))
952 (if (null? cond-code-list)
953 (estate-error estate "empty `cond'"))
954 (let ((if-part (if vm? "if (" "("))
955 (then-part (if vm? ") " ") ? "))
956 (elseif-part (if vm? " else if (" " : ("))
957 (else-part (if vm? " else " " : "))
958 (fi-part (if vm? "" ")")))
962 (cx:c (rtl-c-get estate DFLT (caar cond-code-list)))
964 (cx:c (apply s-sequence
968 (cdar cond-code-list))))))))
969 (ccl (cdr cond-code-list)))
970 (cond ((null? ccl) (cx:make mode result))
971 ((eq? (caar ccl) 'else)
976 (cx:c (apply s-sequence
981 (else (loop (string-append
984 (cx:c (rtl-c-get estate DFLT (caar ccl)))
986 (cx:c (apply s-sequence
994 ; Utility of s-case to print a case prefix (for lack of a better term).
996 (define (/gen-case-prefix val)
997 (string-append " case "
999 (number->string val))
1001 (string-upcase (gen-c-symbol val))) ; yes, upcase
1004 (parse-error (make-prefix-context "case:")
1009 ; Utility of s-case to handle a void result.
1011 (define (s-case-vm estate test case-list)
1016 (cx:c (rtl-c-get estate DFLT test))
1019 (string-map (lambda (case-entry)
1020 (let ((caseval (car case-entry))
1021 (code (cdr case-entry)))
1023 (cond ((list? caseval)
1024 (string-map /gen-case-prefix caseval))
1025 ((eq? 'else caseval)
1026 (string-append " default : "))
1028 (/gen-case-prefix caseval)))
1029 (cx:c (apply s-sequence
1030 (cons estate (cons VOID (cons nil code)))))
1036 ; Utility of s-case-non-vm to generate code to perform the test.
1037 ; MODE is the mode name.
1039 (define (/gen-non-vm-case-test estate mode test cases)
1040 (assert (not (null? cases)))
1041 (let loop ((result "") (cases cases))
1044 (let ((case (cond ((number? (car cases))
1046 ((symbol? (car cases))
1047 (if (enum-lookup-val (car cases))
1048 (rtx-make 'enum mode (car cases))
1049 (estate-error estate
1050 "symbol not an enum"
1053 (estate-error estate "invalid case" (car cases))))))
1054 (loop (string-append
1056 (if (= (string-length result) 0)
1059 (cx:c (rtl-c-get estate mode test))
1061 (cx:c (rtl-c-get estate mode case)))
1065 ; Utility of s-case to handle a non-void result.
1066 ; This is expanded as a series of ?:'s.
1067 ; MODE is the mode name.
1069 (define (s-case-non-vm estate mode test case-list)
1072 (elseif-part " : (")
1078 (/gen-non-vm-case-test estate mode test (caar case-list))
1080 (cx:c (apply s-sequence
1084 (cdar case-list))))))))
1085 (cl (cdr case-list)))
1086 (cond ((null? cl) (cx:make mode result))
1087 ((eq? (caar cl) 'else)
1092 (cx:c (apply s-sequence
1097 (else (loop (string-append
1100 (/gen-non-vm-case-test estate mode test (caar cl))
1102 (cx:c (apply s-sequence
1110 ; C switch statement
1111 ; To follow convention, MODE is the first arg.
1112 ; MODE is the mode name.
1113 ; FIXME: What to allow for case choices is wip.
1115 (define (s-case estate mode test . case-list)
1116 ;; FIXME: can't get DFLT anymore
1117 (if (or (mode:eq? 'DFLT mode) (mode:eq? 'VOID mode))
1118 (s-case-vm estate test case-list)
1119 (s-case-non-vm estate mode test case-list))
1122 ; Parallels and Sequences
1124 ; Temps for `parallel' are recorded differently than for `sequence'.
1125 ; ??? I believe this is because there was an interaction between the two.
1127 (define /par-temp-list nil)
1129 ; Record a temporary needed for a parallel in mode MODE.
1130 ; We just need to record the mode with a unique name so we use a <c-expr>
1131 ; object where the "expression" is the variable's name.
1133 (define (/par-new-temp! mode)
1134 (set! /par-temp-list
1135 (cons (cx:make mode (string-append "temp"
1137 (length /par-temp-list))))
1139 (car /par-temp-list)
1142 ; Return the next temp from the list, and leave the list pointing to the
1145 (define (/par-next-temp!)
1146 (let ((result (car /par-temp-list)))
1147 (set! /par-temp-list (cdr /par-temp-list))
1151 (define (/gen-par-temp-defns temp-list)
1152 ;(display temp-list) (newline)
1156 (string-map (lambda (temp) (string-append (obj:str-name (cx:mode temp))
1157 " " (cx:c temp) ";"))
1162 ;; Parallels are handled by converting them into two sequences. The first has
1163 ;; all set destinations replaced with temps, and the second has all set sources
1164 ;; replaced with those temps.
1166 ;; rtl-traverse expr-fn to replace the dest of sets with the parallel temp.
1168 (define (/par-replace-set-dest-expr-fn rtx-obj expr parent-expr op-pos
1172 (let ((name (rtx-name expr))
1173 (options (rtx-options expr))
1174 (mode (rtx-mode expr))
1175 (dest (rtx-set-dest expr))
1176 (src (rtx-set-src expr)))
1177 (list name options mode (/par-new-temp! mode) src)))
1181 ;; rtl-traverse expr-fn to replace the src of sets with the parallel temp.
1182 ;; This must process expressions in the same order as /par-replace-set-dests.
1184 (define (/par-replace-set-src-expr-fn rtx-obj expr parent-expr op-pos
1188 (let ((name (rtx-name expr))
1189 (options (rtx-options expr))
1190 (mode (rtx-mode expr))
1191 (dest (rtx-set-dest expr))
1192 (src (rtx-set-src expr)))
1193 (list name options mode dest (/par-next-temp!))))
1197 ;; Return a <c-expr> node for a `parallel'.
1199 (define (s-parallel estate . exprs)
1202 ;; Initialize /par-temp-list for /par-replace-set-dests.
1203 (set! /par-temp-list nil)
1205 (let* ((set-dest-exprs
1206 ;; Use map-in-order because we need temp creation and usage to
1207 ;; follow the same order.
1208 (map-in-order (lambda (expr)
1209 (rtx-traverse (estate-context estate)
1210 (estate-owner estate)
1212 /par-replace-set-dest-expr-fn
1215 (set-dests (string-map (lambda (expr)
1216 (rtl-c-with-estate estate VOID expr))
1218 (temps (reverse! /par-temp-list)))
1220 ;; Initialize /par-temp-list for /par-replace-set-srcs.
1221 (set! /par-temp-list temps)
1223 (let* ((set-src-exprs
1224 ;; Use map-in-order because we need temp creation and usage to
1225 ;; follow the same order.
1226 (map-in-order (lambda (expr)
1227 (rtx-traverse (estate-context estate)
1228 (estate-owner estate)
1230 /par-replace-set-src-expr-fn
1233 (set-srcs (string-map (lambda (expr)
1234 (rtl-c-with-estate estate VOID expr))
1239 ;; ??? do {} while (0); doesn't get "optimized out"
1240 ;; internally by gcc, meaning two labels and a loop are
1241 ;; created for it to have to process. We can generate pretty
1242 ;; big files and can cause gcc to require *lots* of memory.
1243 ;; So let's try just {} ...
1245 (/gen-par-temp-defns temps)
1252 ;; Subroutine of s-sequence to simplify it.
1253 ;; Return a boolean indicating if GCC's "statement expression" extension
1254 ;; is necessary to implement (sequence MODE ENV EXPR-LIST).
1255 ;; Only use GCC "statement expression" extension if necessary.
1257 ;; Avoid using statement expressions for
1258 ;; (sequence non-VOID-mode (error "mumble") expr).
1259 ;; Some targets, e.g. cris, use this.
1261 (define (/use-gcc-stmt-expr? mode env expr-list)
1262 (if (not (rtx-env-empty? env))
1264 (case (length expr-list)
1266 ((2) (if (eq? (rtx-name (car expr-list)) 'error)
1272 ;; Return a <c-expr> node for a `sequence'.
1273 ;; MODE is the mode name.
1275 (define (s-sequence estate mode env . exprs)
1276 (let* ((env (rtx-env-make-locals env)) ;; compile env
1277 (estate (estate-push-env estate env)))
1279 (if (or (mode:eq? 'DFLT mode) ;; FIXME: DFLT can't appear anymore
1280 (mode:eq? 'VOID mode))
1284 ;; ??? do {} while (0); doesn't get "optimized out"
1285 ;; internally by gcc, meaning two labels and a loop are
1286 ;; created for it to have to process. We can generate pretty
1287 ;; big files and can cause gcc to require *lots* of memory.
1288 ;; So let's try just {} ...
1290 (gen-temp-defs estate env)
1291 (string-map (lambda (e)
1292 (rtl-c-with-estate estate VOID e))
1296 (let ((use-stmt-expr? (/use-gcc-stmt-expr? mode env exprs)))
1299 (if use-stmt-expr? "({ " "(")
1300 (gen-temp-defs estate env)
1305 (if use-stmt-expr? "; " ", ")
1306 ;; Strip off gratuitous ";\n" at end of expressions that
1307 ;; misguessed themselves to be in statement context.
1308 ;; See s-c-call, s-c-call-raw above.
1309 (let ((substmt (rtl-c-with-estate estate DFLT e)))
1310 (if (and (not use-stmt-expr?)
1311 (string=? (string-take -2 substmt) ";\n"))
1312 (string-drop -2 substmt)
1315 (if use-stmt-expr? "; })" ")"))))))
1318 ; Return a <c-expr> node for a `do-count'.
1320 (define (s-do-count estate iter-var nr-times . exprs)
1321 (let* ((limit-var (rtx-make-iteration-limit-var iter-var))
1322 (env (rtx-env-make-iteration-locals iter-var))
1323 (estate (estate-push-env estate env))
1324 (temp-iter (rtx-temp-lookup (estate-env-stack estate) iter-var))
1325 (temp-limit (rtx-temp-lookup (estate-env-stack estate) limit-var))
1326 (c-iter-var (rtx-temp-value temp-iter))
1327 (c-limit-var (rtx-temp-value temp-limit)))
1331 (gen-temp-defs estate env)
1332 " " c-limit-var " = "
1333 (cx:c (rtl-c-get estate (rtx-temp-mode temp-limit) nr-times))
1335 " for (" c-iter-var " = 0;\n"
1336 " " c-iter-var " < " c-limit-var ";\n"
1337 " ++" c-iter-var ")\n"
1339 (string-map (lambda (e)
1340 (rtl-c-with-estate estate VOID e))
1347 ; *****************************************************************************
1349 ; RTL->C generators for each rtx function.
1351 ; Return code to set FN as the generator for RTX.
1353 (defmacro define-fn (rtx args expr . rest)
1355 (assert (rtx-lookup (quote ,rtx)))
1356 (vector-set! table (rtx-num (rtx-lookup (quote ,rtx)))
1357 (lambda ,args ,@(cons expr rest))))
1360 (define (rtl-c-init!)
1361 (set! /rtl-c-gen-table (/rtl-c-build-table))
1365 ; The rest of this file is one big function to return the rtl->c lookup table.
1366 ; For each of these functions, MODE is the name of the mode.
1368 (define (/rtl-c-build-table)
1369 (let ((table (make-vector (rtx-max-num) #f)))
1373 (define-fn error (*estate* options mode message)
1374 (let ((c-call (s-c-call *estate* mode "cgen_rtx_error"
1376 (backslash "\"" message)
1378 (if (mode:eq? mode VOID)
1380 (cx:make mode (string-append "(" (cx:c c-call) ", 0)"))))
1385 (define-fn enum (*estate* options mode name)
1386 (cx:make mode (string-upcase (gen-c-symbol name)))
1389 ; Instruction field support.
1390 ; ??? This should build an operand object like -build-ifield-operand! does
1393 (define-fn ifield (*estate* options mode ifld-name)
1394 (if (estate-ifield-var? *estate*)
1395 (cx:make mode (gen-c-symbol ifld-name))
1396 (cx:make mode (string-append "FLD (" (gen-c-symbol ifld-name) ")")))
1397 ; (let ((f (current-ifld-lookup ifld-name)))
1398 ; (make <operand> (obj-location f) ifld-name ifld-name
1399 ; (atlist-cons (bool-attr-make 'SEM-ONLY #t)
1401 ; (obj:name (ifld-hw-type f))
1402 ; (obj:name (ifld-mode f))
1403 ; (make <hw-index> 'anonymous
1404 ; 'ifield (ifld-mode f) f)
1410 (define-fn operand (*estate* options mode object-or-name)
1411 (cond ((operand? object-or-name)
1412 ;; FIXME: <operand> objects is what xop is for
1413 ;; mode checking to be done during canonicalization
1415 ((symbol? object-or-name)
1416 (let ((object (current-op-lookup object-or-name)))
1418 (estate-error *estate* "undefined operand" object-or-name))
1419 ;; mode checking to be done during canonicalization
1422 (estate-error *estate* "bad arg to `operand'" object-or-name)))
1425 (define-fn xop (*estate* options mode object)
1426 (let ((delayed (assoc '#:delay (estate-modifiers *estate*))))
1428 (equal? APPLICATION 'SID-SIMULATOR)
1430 ;; if we're looking at an operand inside a (delay ...) rtx, then we
1431 ;; are talking about a _delayed_ operand, which is a different
1432 ;; beast. rather than try to work out what context we were
1433 ;; constructed within, we just clone the operand instance and set
1434 ;; the new one to have a delayed value. the setters and getters
1435 ;; will work it out.
1436 (let ((obj (object-copy object))
1437 (amount (cadr delayed)))
1438 (op:set-delay! obj amount)
1440 ;; else return the normal object
1443 (define-fn local (*estate* options mode object-or-name)
1444 (cond ((rtx-temp? object-or-name)
1446 ((symbol? object-or-name)
1447 (let ((object (rtx-temp-lookup (estate-env-stack *estate*) object-or-name)))
1449 (estate-error *estate* "undefined local" object-or-name))
1452 (estate-error *estate* "bad arg to `local'" object-or-name)))
1455 (define-fn reg (*estate* options mode hw-elm . indx-sel)
1456 (let ((indx (or (list-maybe-ref indx-sel 0) 0))
1457 (sel (or (list-maybe-ref indx-sel 1) hw-selector-default)))
1458 (s-hw *estate* mode hw-elm indx sel))
1461 (define-fn raw-reg (*estate* options mode hw-elm . indx-sel)
1462 (let ((indx (or (list-maybe-ref indx-sel 0) 0))
1463 (sel (or (list-maybe-ref indx-sel 1) hw-selector-default)))
1464 (let ((result (s-hw *estate* mode hw-elm indx sel)))
1465 (obj-cons-attr! result (bool-attr-make 'RAW #t))
1469 (define-fn mem (*estate* options mode addr . sel)
1470 (s-hw *estate* mode 'h-memory addr
1471 (if (pair? sel) (car sel) hw-selector-default))
1474 ; ??? Hmmm... needed? The pc is usually specified as `pc' which is shorthand
1476 ;(define-fn pc (*estate* options mode)
1480 (define-fn ref (*estate* options mode name)
1481 (if (not (insn? (estate-owner *estate*)))
1482 (estate-error *estate* "ref: not processing an insn"
1483 (obj:name (estate-owner *estate*))))
1486 "(referenced & (1 << "
1488 (op:num (insn-lookup-op (estate-owner *estate*) name)))
1492 ; ??? Maybe this should return an operand object.
1493 (define-fn index-of (*estate* options mode op)
1494 (send (op:index (rtx-eval-with-estate op DFLT *estate*))
1495 'cxmake-get *estate* (mode:lookup mode))
1498 (define-fn clobber (*estate* options mode object)
1499 (cx:make VOID "; /*clobber*/\n")
1502 (define-fn delay (*estate* options mode num-node rtx)
1503 ;; FIXME: Try to move SID stuff into sid-foo.scm.
1506 (let* ((n (cadddr num-node))
1507 (old-delay (let ((old (assoc '#:delay (estate-modifiers *estate*))))
1508 (if old (cadr old) 0)))
1509 (new-delay (+ n old-delay)))
1511 ;; check for proper usage
1512 (if (let* ((hw (case (car rtx)
1513 ((operand) (op:type (current-op-lookup (rtx-arg1 rtx))))
1514 ((xop) (op:type (rtx-xop-obj rtx)))
1516 (not (and hw (or (pc? hw) (memory? hw) (register? hw)))))
1519 "(delay ...) rtx applied to wrong type of operand, should be pc, register or memory"
1521 ;; signal an error if we're delayed and not in a "parallel-insns" CPU
1522 (if (not (with-parallel?))
1523 (estate-error *estate* "delayed operand in a non-parallel cpu"
1525 ;; update cpu-global pipeline bound
1526 (cpu-set-max-delay! (current-cpu) (max (cpu-max-delay (current-cpu)) new-delay))
1527 ;; pass along new delay to embedded rtx
1528 (rtx-eval-with-estate rtx (mode:lookup mode)
1529 (estate-with-modifiers *estate* `((#:delay ,new-delay)))))))
1532 (else (s-sequence (estate-with-modifiers *estate* '((#:delay))) VOID '() rtx)))
1535 ; Gets expanded as a macro.
1536 ;(define-fn annul (*estate* yes?)
1537 ; (s-c-call *estate* 'VOID "SEM_ANNUL_INSN" "pc" yes?)
1540 (define-fn skip (*estate* options mode yes?)
1541 (send pc 'cxmake-skip *estate* yes?)
1542 ;(s-c-call *estate* 'VOID "SEM_SKIP_INSN" "pc" yes?)
1545 (define-fn eq-attr (*estate* options mode obj attr-name value)
1547 (string-append "(GET_ATTR ("
1548 (gen-c-symbol attr-name)
1550 (gen-c-symbol value)
1554 (define-fn int-attr (*estate* options mode owner attr-name)
1555 (cond ((or (equal? owner '(current-insn () DFLT)) ;; FIXME: delete in time
1556 (equal? owner '(current-insn () INSN)))
1557 (s-c-raw-call *estate* 'INT "GET_ATTR"
1558 (string-upcase (gen-c-symbol attr-name))))
1560 (estate-error *estate* "attr: unsupported object type" owner)))
1563 (define-fn const (*estate* options mode c)
1564 (assert (not (mode:eq? 'VOID mode)))
1565 (if (mode:eq? 'DFLT mode) ;; FIXME: can't get DFLT anymore
1567 (let ((mode (mode:lookup mode)))
1569 (cond ((or (mode:eq? 'DI mode)
1570 (mode:eq? 'UDI mode)
1573 (string-append "MAKEDI ("
1574 (gen-integer (high-part c)) ", "
1575 (gen-integer (low-part c))
1577 ((and (<= #x-80000000 c) (> #x80000000 c))
1579 ((and (<= #x80000000 c) (>= #xffffffff c))
1580 ; ??? GCC complains if not affixed with "U" but that's not k&r.
1581 ;(string-append (number->string val) "U"))
1582 (string-append "0x" (number->string c 16)))
1584 (else (number->string c)))))
1587 (define-fn join (*estate* options out-mode in-mode arg1 . arg-rest)
1588 ; FIXME: Endianness issues undecided.
1589 ; FIXME: Ensure correct number of args for in/out modes.
1590 ; Ensure compatible modes.
1591 (apply s-c-raw-call (cons *estate*
1593 (cons (stringsym-append "JOIN"
1596 (cons arg1 arg-rest)))))
1599 (define-fn subword (*estate* options mode value word-num)
1600 (let* ((mode (mode:lookup mode))
1601 (val (rtl-c-get *estate* DFLT value))
1602 (val-mode (cx:mode val)))
1604 (string-append "SUBWORD"
1605 (obj:str-name val-mode) (obj:str-name mode)
1607 (if (mode-bigger? val-mode mode)
1610 (if (number? word-num)
1611 (number->string word-num)
1612 (cx:c (rtl-c-get *estate* DFLT word-num))))
1617 (define-fn c-code (*estate* options mode text)
1621 (define-fn c-call (*estate* options mode name . args)
1622 (apply s-c-call (cons *estate* (cons mode (cons name args))))
1625 (define-fn c-raw-call (*estate* options mode name . args)
1626 (apply s-c-raw-call (cons *estate* (cons mode (cons name args))))
1629 (define-fn nop (*estate* options mode)
1630 (cx:make VOID "((void) 0); /*nop*/\n")
1633 (define-fn set (*estate* options mode dst src)
1634 (if (estate-for-insn? *estate*)
1635 (rtl-c-set-trace *estate* mode dst src)
1636 (rtl-c-set-quiet *estate* mode dst src))
1639 (define-fn set-quiet (*estate* options mode dst src)
1640 (rtl-c-set-quiet *estate* mode dst src)
1643 (define-fn neg (*estate* options mode s1)
1644 (s-unop *estate* "NEG" "-" mode s1)
1647 (define-fn abs (*estate* options mode s1)
1648 (s-unop *estate* "ABS" #f mode s1)
1651 (define-fn inv (*estate* options mode s1)
1652 (s-unop *estate* "INV" "~" mode s1)
1655 (define-fn not (*estate* options mode s1)
1656 (s-unop *estate* "NOT" "!" mode s1)
1659 (define-fn add (*estate* options mode s1 s2)
1660 (s-binop *estate* "ADD" "+" mode s1 s2)
1662 (define-fn sub (*estate* options mode s1 s2)
1663 (s-binop *estate* "SUB" "-" mode s1 s2)
1666 (define-fn addc (*estate* options mode s1 s2 s3)
1667 (s-binop-with-bit *estate* "ADDC" mode s1 s2 s3)
1669 ;; ??? Whether to rename ADDCF/ADDOF -> ADDCCF/ADDCOF is debatable.
1670 (define-fn addc-cflag (*estate* options mode s1 s2 s3)
1671 (s-binop-with-bit *estate* "ADDCF" mode s1 s2 s3)
1673 (define-fn addc-oflag (*estate* options mode s1 s2 s3)
1674 (s-binop-with-bit *estate* "ADDOF" mode s1 s2 s3)
1677 (define-fn subc (*estate* options mode s1 s2 s3)
1678 (s-binop-with-bit *estate* "SUBC" mode s1 s2 s3)
1680 ;; ??? Whether to rename SUBCF/SUBOF -> SUBCCF/SUBCOF is debatable.
1681 (define-fn subc-cflag (*estate* options mode s1 s2 s3)
1682 (s-binop-with-bit *estate* "SUBCF" mode s1 s2 s3)
1684 (define-fn subc-oflag (*estate* options mode s1 s2 s3)
1685 (s-binop-with-bit *estate* "SUBOF" mode s1 s2 s3)
1688 ;; ??? These are deprecated. Delete in time.
1689 (define-fn add-cflag (*estate* options mode s1 s2 s3)
1690 (s-binop-with-bit *estate* "ADDCF" mode s1 s2 s3)
1692 (define-fn add-oflag (*estate* options mode s1 s2 s3)
1693 (s-binop-with-bit *estate* "ADDOF" mode s1 s2 s3)
1695 (define-fn sub-cflag (*estate* options mode s1 s2 s3)
1696 (s-binop-with-bit *estate* "SUBCF" mode s1 s2 s3)
1698 (define-fn sub-oflag (*estate* options mode s1 s2 s3)
1699 (s-binop-with-bit *estate* "SUBOF" mode s1 s2 s3)
1702 ;(define-fn zflag (*estate* options mode value)
1703 ; (list 'eq mode value (list 'const mode 0))
1706 ;(define-fn nflag (*estate* options mode value)
1707 ; (list 'lt mode value (list 'const mode 0))
1710 (define-fn mul (*estate* options mode s1 s2)
1711 (s-binop *estate* "MUL" "*" mode s1 s2)
1713 (define-fn div (*estate* options mode s1 s2)
1714 (s-binop *estate* "DIV" "/" mode s1 s2)
1716 (define-fn udiv (*estate* options mode s1 s2)
1717 (s-binop *estate* "UDIV" "/" mode s1 s2)
1719 (define-fn mod (*estate* options mode s1 s2)
1720 (s-binop *estate* "MOD" "%" mode s1 s2)
1722 (define-fn umod (*estate* options mode s1 s2)
1723 (s-binop *estate* "UMOD" "%" mode s1 s2)
1726 (define-fn sqrt (*estate* options mode s1)
1727 (s-unop *estate* "SQRT" #f mode s1)
1729 (define-fn cos (*estate* options mode s1)
1730 (s-unop *estate* "COS" #f mode s1)
1732 (define-fn sin (*estate* options mode s1)
1733 (s-unop *estate* "SIN" #f mode s1)
1736 (define-fn min (*estate* options mode s1 s2)
1737 (s-binop *estate* "MIN" #f mode s1 s2)
1739 (define-fn max (*estate* options mode s1 s2)
1740 (s-binop *estate* "MAX" #f mode s1 s2)
1742 (define-fn umin (*estate* options mode s1 s2)
1743 (s-binop *estate* "UMIN" #f mode s1 s2)
1745 (define-fn umax (*estate* options mode s1 s2)
1746 (s-binop *estate* "UMAX" #f mode s1 s2)
1749 (define-fn and (*estate* options mode s1 s2)
1750 (s-binop *estate* "AND" "&" mode s1 s2)
1752 (define-fn or (*estate* options mode s1 s2)
1753 (s-binop *estate* "OR" "|" mode s1 s2)
1755 (define-fn xor (*estate* options mode s1 s2)
1756 (s-binop *estate* "XOR" "^" mode s1 s2)
1759 (define-fn sll (*estate* options mode s1 s2)
1760 (s-shop *estate* "SLL" "<<" mode s1 s2)
1762 (define-fn srl (*estate* options mode s1 s2)
1763 (s-shop *estate* "SRL" ">>" mode s1 s2)
1765 (define-fn sra (*estate* options mode s1 s2)
1766 (s-shop *estate* "SRA" ">>" mode s1 s2)
1768 (define-fn ror (*estate* options mode s1 s2)
1769 (s-shop *estate* "ROR" #f mode s1 s2)
1771 (define-fn rol (*estate* options mode s1 s2)
1772 (s-shop *estate* "ROL" #f mode s1 s2)
1775 (define-fn andif (*estate* options mode s1 s2)
1776 (s-boolifop *estate* "ANDIF" "&&" s1 s2)
1778 (define-fn orif (*estate* options mode s1 s2)
1779 (s-boolifop *estate* "ORIF" "||" s1 s2)
1782 (define-fn ext (*estate* options mode s1)
1783 (s-convop *estate* "EXT" mode s1)
1785 (define-fn zext (*estate* options mode s1)
1786 (s-convop *estate* "ZEXT" mode s1)
1788 (define-fn trunc (*estate* options mode s1)
1789 (s-convop *estate* "TRUNC" mode s1)
1791 (define-fn fext (*estate* options mode s1)
1792 (s-convop *estate* "FEXT" mode s1)
1794 (define-fn ftrunc (*estate* options mode s1)
1795 (s-convop *estate* "FTRUNC" mode s1)
1797 (define-fn float (*estate* options mode s1)
1798 (s-convop *estate* "FLOAT" mode s1)
1800 (define-fn ufloat (*estate* options mode s1)
1801 (s-convop *estate* "UFLOAT" mode s1)
1803 (define-fn fix (*estate* options mode s1)
1804 (s-convop *estate* "FIX" mode s1)
1806 (define-fn ufix (*estate* options mode s1)
1807 (s-convop *estate* "UFIX" mode s1)
1810 (define-fn eq (*estate* options mode s1 s2)
1811 (s-cmpop *estate* 'eq "==" mode s1 s2)
1813 (define-fn ne (*estate* options mode s1 s2)
1814 (s-cmpop *estate* 'ne "!=" mode s1 s2)
1817 (define-fn lt (*estate* options mode s1 s2)
1818 (s-cmpop *estate* 'lt "<" mode s1 s2)
1820 (define-fn le (*estate* options mode s1 s2)
1821 (s-cmpop *estate* 'le "<=" mode s1 s2)
1823 (define-fn gt (*estate* options mode s1 s2)
1824 (s-cmpop *estate* 'gt ">" mode s1 s2)
1826 (define-fn ge (*estate* options mode s1 s2)
1827 (s-cmpop *estate* 'ge ">=" mode s1 s2)
1830 (define-fn ltu (*estate* options mode s1 s2)
1831 (s-cmpop *estate* 'ltu "<" mode s1 s2)
1833 (define-fn leu (*estate* options mode s1 s2)
1834 (s-cmpop *estate* 'leu "<=" mode s1 s2)
1836 (define-fn gtu (*estate* options mode s1 s2)
1837 (s-cmpop *estate* 'gtu ">" mode s1 s2)
1839 (define-fn geu (*estate* options mode s1 s2)
1840 (s-cmpop *estate* 'geu ">=" mode s1 s2)
1843 (define-fn member (*estate* options mode value set)
1844 ;; NOTE: There are multiple evalutions of VALUE in the generated code.
1845 ;; It's probably ok, this comment is more for completeness sake.
1846 (let ((c-value (rtl-c-get *estate* mode value))
1847 (set (rtx-number-list-values set)))
1848 (let loop ((set (cdr set))
1849 (code (string-append "(" (cx:c c-value)
1851 (gen-integer (car set))
1854 (cx:make (mode:lookup 'BI) (string-append "(" code ")"))
1860 (gen-integer (car set))
1864 (define-fn if (*estate* options mode cond then . else)
1865 (apply s-if (append! (list *estate* mode cond then) else))
1868 (define-fn cond (*estate* options mode . cond-code-list)
1869 (apply s-cond (cons *estate* (cons mode cond-code-list)))
1872 (define-fn case (*estate* options mode test . case-list)
1873 (apply s-case (cons *estate* (cons mode (cons test case-list))))
1876 (define-fn parallel (*estate* options mode ignore expr . exprs)
1877 (apply s-parallel (cons *estate* (cons expr exprs)))
1880 (define-fn sequence (*estate* options mode locals expr . exprs)
1882 (cons *estate* (cons mode (cons locals (cons expr exprs)))))
1885 (define-fn do-count (*estate* options mode iter-var nr-times expr . exprs)
1887 (cons *estate* (cons iter-var (cons nr-times (cons expr exprs)))))
1890 (define-fn closure (*estate* options mode isa-name-list env-stack expr)
1891 (rtl-c-with-estate (estate-make-closure *estate* isa-name-list
1892 (rtx-make-env-stack env-stack))
1893 (mode:lookup mode) expr)
1896 ;; The result is the rtl->c generator table.
1897 ;; FIXME: verify all elements are filled
1901 )) ;; End of /rtl-c-build-table