1 ; Simulator generator support routines.
2 ; Copyright (C) 2000, 2001, 2002, 2006 Red Hat, Inc.
3 ; This file is part of CGEN.
5 ; One goal of this file is to provide cover functions for all methods.
6 ; i.e. this file fills in the missing pieces of the interface between
7 ; the application independent part of CGEN (i.e. the code loaded by read.scm)
8 ; and the application dependent part (i.e. sim-*.scm).
9 ; `send' is not intended to appear in sim-*.scm.
10 ; [It still does but that's to be fixed.]
12 ; Specify which application.
13 (set! APPLICATION 'SIMULATOR)
15 ; Cover functions for various methods.
17 ; Return the C type of something. This isn't always a mode.
19 (define (gen-type self) (send self 'gen-type))
21 ; Return the C type of an index's value or #f if not needed (scalar).
23 (define (gen-index-type op sfmt)
24 (let ((index-mode (send op 'get-index-mode)))
26 (mode:c-type index-mode)
32 ; Currently supported options:
34 ; generate code to use the scache
35 ; This is an all or nothing option, either scache is used or it's not.
37 ; generate code to do profiling in the semantic function
38 ; code (fn) or in the semantic switch (sw)
40 ; For architectures that have parallel execution.
41 ; Execute the semantics by recording the results in a generic buffer,
42 ; and doing a post-semantics writeback pass.
44 ; Only generate parallel versions of each insn.
46 ; Enable multiple-isa support (eg. arm+thumb).
47 ; copyright fsf|redhat
48 ; emit an FSF or Cygnus copyright (temporary, pending decision)
49 ; package gnusim|cygsim
50 ; indicate the software package
52 ; #t if the scache is being used
53 (define -with-scache? #f)
54 (define (with-scache?) -with-scache?)
56 ; #t if we're generating profiling code
57 ; Each of the function and switch semantic code can have profiling.
58 ; The options as passed are stored in -with-profile-{fn,sw}?, and
59 ; -with-profile? is set at code generation time.
60 (define -with-profile-fn? #f)
61 (define -with-profile-sw? #f)
62 (define -with-profile? #f)
63 (define (with-profile?) -with-profile?)
64 (define (with-any-profile?) (or -with-profile-fn? -with-profile-sw?))
66 ; #t if multiple isa support is enabled
67 (define -with-multiple-isa? #f)
68 (define (with-multiple-isa?) -with-multiple-isa?)
70 ; Handle parallel execution with generic writeback pass.
71 (define -with-generic-write? #f)
72 (define (with-generic-write?) -with-generic-write?)
74 ; Only generate parallel versions of each insn.
75 (define -with-parallel-only? #f)
76 (define (with-parallel-only?) -with-parallel-only?)
78 ; String containing copyright text.
79 (define CURRENT-COPYRIGHT #f)
81 ; String containing text defining the package we're generating code for.
82 (define CURRENT-PACKAGE #f)
84 ; Initialize the options.
86 (define (option-init!)
87 (set! -with-scache? #f)
88 (set! -with-profile-fn? #f)
89 (set! -with-profile-sw? #f)
90 (set! -with-multiple-isa? #f)
91 (set! -with-generic-write? #f)
92 (set! -with-parallel-only? #f)
93 (set! CURRENT-COPYRIGHT copyright-fsf)
94 (set! CURRENT-PACKAGE package-gnu-simulators)
98 ; Handle an option passed in from the command line.
100 (define (option-set! name value)
102 ((with-scache) (set! -with-scache? #t))
103 ((with-profile) (cond ((equal? value '("fn"))
104 (set! -with-profile-fn? #t))
105 ((equal? value '("sw"))
106 (set! -with-profile-sw? #t))
107 (else (error "invalid with-profile value" value))))
108 ((with-multiple-isa) (set! -with-multiple-isa? #t))
109 ((with-generic-write) (set! -with-generic-write? #t))
110 ((with-parallel-only) (set! -with-parallel-only? #t))
111 ((copyright) (cond ((equal? value '("fsf"))
112 (set! CURRENT-COPYRIGHT copyright-fsf))
113 ((equal? value '("redhat"))
114 (set! CURRENT-COPYRIGHT copyright-red-hat))
115 (else (error "invalid copyright value" value))))
116 ((package) (cond ((equal? value '("gnusim"))
117 (set! CURRENT-PACKAGE package-gnu-simulators))
118 ((equal? value '("cygsim"))
119 (set! CURRENT-PACKAGE package-red-hat-simulators))
120 (else (error "invalid package value" value))))
121 (else (error "unknown option" name))
126 ; #t if the cpu can execute insns parallely.
127 ; This one isn't passed on the command line, but we follow the convention
128 ; of prefixing these things with `with-'.
129 ; While processing operand reading (or writing), parallel execution support
130 ; needs to be turned off, so it is up to the appropriate cgen-foo.c proc to
131 ; set-with-parallel?! appropriately.
132 (define -with-parallel? #f)
133 (define (with-parallel?) -with-parallel?)
134 (define (set-with-parallel?! flag) (set! -with-parallel? flag))
136 ; Kind of parallel support.
137 ; If 'read, read pre-processing is done.
138 ; If 'write, write post-processing is done.
139 ; ??? At present we always use write post-processing, though the previous
140 ; version used read pre-processing. Not sure supporting both is useful
142 (define -with-parallel-kind 'write)
143 ; #t if parallel support is provided by read pre-processing.
144 (define (with-parallel-read?)
145 (and -with-parallel? (eq? -with-parallel-kind 'read))
147 ; #t if parallel support is provided by write post-processing.
148 (define (with-parallel-write?)
149 (and -with-parallel? (eq? -with-parallel-kind 'write))
154 ; All machine generated cpu elements are accessed through a cover macro
155 ; to hide the details of the underlying implementation.
157 (define c-cpu-macro "CPU")
159 (define (gen-cpu-ref sym)
160 (string-append c-cpu-macro " (" sym ")")
164 ; Return C code to fetch a value from instruction memory.
165 ; PC-VAR is the C expression containing the address of the start of the
167 ; ??? Aligned/unaligned support?
169 (define (gen-ifetch pc-var bitoffset bitsize)
170 (string-append "GETIMEM"
175 (else (error "bad bitsize argument to gen-ifetch" bitsize)))
177 pc-var " + " (number->string (quotient bitoffset 8))
181 ; Instruction field support code.
183 ; Return a <c-expr> object of the value of an ifield.
185 (define (-cxmake-ifld-val mode f)
187 ; ??? Perhaps a better way would be to defer evaluating the src of a
188 ; set until the method processing the dest.
189 (cx:make-with-atlist mode (gen-ifld-argbuf-ref f)
190 (atlist-make "" (bool-attr-make 'CACHED #t)))
191 (cx:make mode (gen-extracted-ifld-value f)))
197 ; gen-type - return C code representing the type
198 ; gen-sym-decl - generate decl using the provided symbol
199 ; gen-sym-get-macro - generate GET macro for accessing CPU elements
200 ; gen-sym-set-macro - generate SET macro for accessing CPU elements
206 (lambda (self) (mode:c-type (elm-get self 'mode)))
210 <scalar> 'gen-sym-decl
211 (lambda (self sym comment)
213 " /* " comment " */\n"
214 " " (send self 'gen-type) " "
215 (gen-c-symbol sym) ";\n"))
219 <scalar> 'gen-sym-get-macro
220 (lambda (self sym comment)
221 (let ((sym (gen-c-symbol sym)))
222 (gen-get-macro sym "" (gen-cpu-ref sym))))
226 <scalar> 'gen-sym-set-macro
227 (lambda (self sym comment)
228 (let ((sym (gen-c-symbol sym)))
229 (gen-set-macro sym "" (gen-cpu-ref sym))))
232 (method-make! <scalar> 'gen-ref (lambda (self sym index estate) sym))
238 (lambda (self) (mode:c-type (elm-get self 'mode)))
242 <array> 'gen-sym-decl
243 (lambda (self sym comment)
245 " /* " comment " */\n"
246 " " (send self 'gen-type) " "
248 (gen-array-ref (elm-get self 'dimensions))
254 <array> 'gen-sym-get-macro
255 (lambda (self sym comment)
256 (let ((sym (gen-c-symbol sym))
257 (rank (length (elm-get self 'dimensions))))
259 "#define GET_" (string-upcase sym)
260 "(" (string-drop 2 (gen-macro-args rank)) ") "
261 (gen-cpu-ref sym) (gen-array-ref (macro-args rank)) "\n"
266 <array> 'gen-sym-set-macro
267 (lambda (self sym comment)
268 (let ((sym (gen-c-symbol sym))
269 (rank (length (elm-get self 'dimensions))))
271 "#define SET_" (string-upcase sym)
272 "(" (string-drop 2 (gen-macro-args rank)) ", x) "
273 "(" (gen-cpu-ref sym) (gen-array-ref (macro-args rank))
278 ; Return a reference to the array.
279 ; SYM is the name of the array.
280 ; INDEX is either a single index object or a (possibly empty) list of objects,
281 ; one object per dimension.
285 (lambda (self sym index estate)
286 (let ((gen-index1 (lambda (idx)
288 (-gen-hw-index idx estate)
291 (cond ((list? index) (string-map gen-index1 index))
292 (else (gen-index1 index))))))
298 ; <integer> 'gen-type
300 ; (mode:c-type (mode-find (elm-get self 'bits)
301 ; (if (has-attr? self 'UNSIGNED)
306 ;(method-make! <integer> 'gen-sym-decl (lambda (self sym comment) ""))
307 ;(method-make! <integer> 'gen-sym-get-macro (lambda (self sym comment) ""))
308 ;(method-make! <integer> 'gen-sym-set-macro (lambda (self sym comment) ""))
310 ; Hardware descriptions support code.
312 ; Various operations are required for each h/w object to support the various
313 ; things the simulator will want to do with it.
317 ; gen-get-macro - Generate definition of the GET access macro.
318 ; gen-set-macro - Generate definition of the SET access macro.
319 ; gen-write - Same as gen-read except done on output operands
320 ; cxmake-get - Return a <c-expr> object to fetch the value.
321 ; gen-set-quiet - Set the value.
322 ; ??? Could just call this gen-set as there is no gen-set-trace
323 ; but for consistency with the messages passed to operands
325 ; gen-type - C type to use to record value, as a string.
326 ; ??? Delete and just use get-mode?
327 ; save-index? - return #t if an index needs to be saved for parallel
328 ; execution post-write processing
333 ; gen-sym-decl - Return a C declaration using the provided symbol.
334 ; gen-sym-get-macro - Generate default GET access macro.
335 ; gen-sym-set-macro - Generate default SET access macro.
336 ; gen-ref - Return a C reference to the object.
338 ; Generate CPU state struct entries.
341 <hardware-base> 'gen-decl
343 (send self 'gen-sym-decl (obj:name self) (obj:comment self)))
346 (method-make-virtual! <hardware-base> 'gen-sym-decl (lambda (self sym comment) ""))
348 ; Return a C reference to a hardware object.
350 (method-make! <hardware-base> 'gen-ref (lambda (self sym index estate) sym))
352 ; Each hardware type must provide its own gen-write method.
355 <hardware-base> 'gen-write
356 (lambda (self estate index mode sfmt op access-macro)
357 (error "gen-write method not overridden:" self))
360 ; gen-type handler, must be overridden
362 (method-make-virtual!
363 <hardware-base> 'gen-type
364 (lambda (self) (error "gen-type not overridden:" self))
367 (method-make! <hardware-base> 'gen-profile-decl (lambda (self) ""))
369 ; Default gen-record-profile method.
372 <hardware-base> 'gen-record-profile
373 (lambda (self index sfmt estate)
377 ; Default cxmake-get method.
378 ; Return a <c-expr> object of the value of SELF.
379 ; ESTATE is the current rtl evaluator state.
380 ; INDEX is a <hw-index> object. It must be an ifield.
381 ; SELECTOR is a hardware selector RTX.
384 <hardware-base> 'cxmake-get
385 (lambda (self estate mode index selector)
386 (if (not (eq? 'ifield (hw-index:type index)))
387 (error "not an ifield hw-index" index))
388 (-cxmake-ifld-val mode (hw-index:value index)))
391 ; Handle gen-get-macro/gen-set-macro.
394 <hardware-base> 'gen-get-macro
400 <hardware-base> 'gen-set-macro
407 ; 'gen-set-quiet helper for PC values.
408 ; NEWVAL is a <c-expr> object of the value to be assigned.
409 ; If OPTIONS contains #:direct, set the PC directly, bypassing semantic
410 ; code considerations.
411 ; ??? OPTIONS support wip. Probably want a new form (or extend existing form)
412 ; of rtx: that takes a variable number of named arguments.
413 ; ??? Another way to get #:direct might be (raw-reg h-pc).
415 (define (-hw-gen-set-quiet-pc self estate mode index selector newval . options)
416 (if (not (send self 'pc?)) (error "Not a PC:" self))
417 (cond ((memq #:direct options)
418 (-hw-gen-set-quiet self estate mode index selector newval))
419 ((has-attr? newval 'CACHED)
420 (string-append "SEM_BRANCH_VIA_CACHE (current_cpu, sem_arg, "
424 (string-append "SEM_BRANCH_VIA_ADDR (current_cpu, sem_arg, "
429 (method-make! <hw-pc> 'gen-set-quiet -hw-gen-set-quiet-pc)
431 ; Handle updates of the pc during parallel execution.
432 ; This is done in a post-processing pass after semantic evaluation.
433 ; SFMT is the <sformat>.
435 ; ACCESS-MACRO is the runtime C macro to use to fetch indices computed
436 ; during semantic evaluation.
438 ; ??? This wouldn't be necessary if gen-set-quiet were a virtual method.
439 ; At this point I'm reluctant to willy nilly make methods virtual.
443 (lambda (self estate index mode sfmt op access-macro)
445 (send self 'gen-set-quiet estate VOID index hw-selector-default
446 (cx:make DFLT (string-append access-macro
447 " (" (gen-sym op) ")")))))
452 (lambda (self estate yes?)
454 (string-append "if ("
457 " SEM_SKIP_INSN (current_cpu, sem_arg, vpc);\n")))
462 ; Forward these methods onto TYPE.
463 (method-make-virtual-forward! <hw-register> 'type '(gen-type gen-sym-decl))
464 (method-make-forward! <hw-register> 'type '(gen-ref
468 ; For parallel instructions supported by queueing outputs for later update,
469 ; return a boolean indicating if an index needs to be recorded.
470 ; An example of when the index isn't needed is if the index can be determined
474 <hw-register> 'save-index?
476 ; FIXME: Later handle case where register number is determined at runtime.
480 ; Handle updates of registers during parallel execution.
481 ; This is done in a post-processing pass after semantic evaluation.
482 ; SFMT is the <sformat>.
483 ; OP is the <operand>.
484 ; ACCESS-MACRO is the runtime C macro to use to fetch indices computed
485 ; during semantic evaluation.
486 ; FIXME: May need mode of OP.
489 <hw-register> 'gen-write
490 (lambda (self estate index mode sfmt op access-macro)
491 ; First get a hw-index object to use during indexing.
492 ; Some indices, e.g. memory addresses, are computed during semantic
493 ; evaluation. Others are computed during the extraction phase.
494 (let ((index (send index 'get-write-index self sfmt op access-macro)))
496 (send self 'gen-set-quiet estate mode index hw-selector-default
497 (cx:make DFLT (string-append access-macro
498 " (" (gen-sym op) ")"))))))
502 <hw-register> 'gen-profile-decl
505 " /* " (obj:comment self) " */\n"
506 " unsigned long " (gen-c-symbol (obj:name self)) ";\n"))
510 <hw-register> 'gen-record-profile
511 (lambda (self index sfmt estate)
512 ; FIXME: Need to handle scalars.
513 (-gen-hw-index-raw index estate))
517 <hw-register> 'gen-get-macro
519 (let ((getter (elm-get self 'get))
520 (mode (send self 'get-mode)))
522 (let ((args (car getter))
523 (expr (cadr getter)))
524 (gen-get-macro (gen-sym self)
525 (if (hw-scalar? self) "" "index")
527 (if (hw-scalar? self)
529 (list (list (car args) 'UINT "index")))
530 #:rtl-cover-fns? #t)))
531 (send self 'gen-sym-get-macro
532 (obj:name self) (obj:comment self)))))
536 <hw-register> 'gen-set-macro
538 (let ((setter (elm-get self 'set))
539 (mode (send self 'get-mode)))
541 (let ((args (car setter))
542 (expr (cadr setter)))
543 (gen-set-macro2 (gen-sym self)
544 (if (hw-scalar? self)
548 (rtl-c VOID ; not `mode', sets have mode VOID
550 (if (hw-scalar? self)
551 (list (list (car args) (hw-mode self) "(x)"))
552 (list (list (car args) 'UINT "(index)")
553 (list (cadr args) (hw-mode self) "(x)")))
554 #:rtl-cover-fns? #t #:macro? #t)))
555 (send self 'gen-sym-set-macro
556 (obj:name self) (obj:comment self)))))
559 ; Utility to build a <c-expr> object to fetch the value of a register.
561 (define (-hw-cxmake-get hw estate mode index selector)
562 (let ((mode (if (mode:eq? 'DFLT mode)
565 (getter (hw-getter hw)))
566 ; If the register is accessed via a cover function/macro, do it.
567 ; Otherwise fetch the value from the cached address or from the CPU struct.
570 (let ((scalar? (hw-scalar? hw))
571 (c-index (-gen-hw-index index estate)))
572 (string-append "GET_"
573 (string-upcase (gen-sym hw))
575 (if scalar? "" c-index)
577 ((and (hw-cache-addr? hw) ; FIXME: redo test
578 (eq? 'ifield (hw-index:type index)))
582 (gen-hw-index-argbuf-ref index)
583 (gen-hw-index-argbuf-name index))))
584 (else (gen-cpu-ref (send hw 'gen-ref
585 (gen-sym hw) index estate))))))
588 (method-make! <hw-register> 'cxmake-get -hw-cxmake-get)
591 ; ??? raw-reg: support is wip
594 <hw-register> 'cxmake-get-raw
595 (lambda (self estate mode index selector)
596 (let ((mode (if (mode:eq? 'DFLT mode)
597 (send self 'get-mode)
599 (cx:make mode (gen-cpu-ref (send self 'gen-ref
600 (gen-sym self) index estate)))))
603 ; Utilities to generate C code to assign a variable to a register.
605 (define (-hw-gen-set-quiet hw estate mode index selector newval)
606 (let ((setter (hw-setter hw)))
608 (let ((scalar? (hw-scalar? hw))
609 (c-index (-gen-hw-index index estate)))
610 (string-append "SET_"
611 (string-upcase (gen-sym hw))
613 (if scalar? "" (string-append c-index ", "))
616 ((and (hw-cache-addr? hw) ; FIXME: redo test
617 (eq? 'ifield (hw-index:type index)))
620 (gen-hw-index-argbuf-ref index)
621 (gen-hw-index-argbuf-name index))
622 " = " (cx:c newval) ";\n"))
623 (else (string-append (gen-cpu-ref (send hw 'gen-ref
624 (gen-sym hw) index estate))
625 " = " (cx:c newval) ";\n"))))
628 (method-make! <hw-register> 'gen-set-quiet -hw-gen-set-quiet)
634 <hw-register> 'gen-set-quiet-raw
635 (lambda (self estate mode index selector newval)
636 (string-append (gen-cpu-ref (send self 'gen-ref
637 (gen-sym self) index estate))
638 " = " (cx:c newval) ";\n"))
641 ; Return name of C access function for getting/setting a register.
643 (define (gen-reg-getter-fn hw prefix)
644 (string-append prefix "_" (gen-sym hw) "_get")
647 (define (gen-reg-setter-fn hw prefix)
648 (string-append prefix "_" (gen-sym hw) "_set")
651 ; Generate decls for access fns of register HW, beginning with
652 ; PREFIX, using C type TYPE.
653 ; SCALAR? is #t if the register is a scalar. Otherwise it is #f and the
654 ; register is a bank of registers.
656 (define (gen-reg-access-decl hw prefix type scalar?)
659 (gen-reg-getter-fn hw prefix)
661 (if scalar? "" ", UINT")
664 (gen-reg-setter-fn hw prefix)
666 (if scalar? "" "UINT, ")
671 ; Generate defns of access fns of register HW, beginning with
672 ; PREFIX, using C type TYPE.
673 ; SCALAR? is #t if the register is a scalar. Otherwise it is #f and the
674 ; register is a bank of registers.
675 ; GET/SET-CODE are C fragments to get/set the value.
676 ; ??? Inlining left for later.
678 (define (gen-reg-access-defn hw prefix type scalar? get-code set-code)
680 "/* Get the value of " (obj:str-name hw) ". */\n\n"
682 (gen-reg-getter-fn hw prefix)
683 " (SIM_CPU *current_cpu"
684 (if scalar? "" ", UINT regno")
688 "/* Set a value for " (obj:str-name hw) ". */\n\n"
690 (gen-reg-setter-fn hw prefix)
691 " (SIM_CPU *current_cpu, "
692 (if scalar? "" "UINT regno, ")
702 <hw-memory> 'cxmake-get
703 (lambda (self estate mode index selector)
704 (let ((mode (if (mode:eq? 'DFLT mode)
707 (default-selector? (hw-selector-default? selector)))
709 (string-append "GETMEM" (obj:str-name mode)
710 (if default-selector? "" "ASI")
713 (-gen-hw-index index estate)
714 (if default-selector?
717 (-gen-hw-selector selector)))
722 <hw-memory> 'gen-set-quiet
723 (lambda (self estate mode index selector newval)
724 (let ((mode (if (mode:eq? 'DFLT mode)
727 (default-selector? (hw-selector-default? selector)))
728 (string-append "SETMEM" (obj:str-name mode)
729 (if default-selector? "" "ASI")
732 (-gen-hw-index index estate)
733 (if default-selector?
736 (-gen-hw-selector selector)))
737 ", " (cx:c newval) ");\n")))
740 (method-make-virtual-forward! <hw-memory> 'type '(gen-type))
741 (method-make-virtual! <hw-memory> 'gen-sym-decl (lambda (self sym comment) ""))
742 (method-make! <hw-memory> 'gen-sym-get-macro (lambda (self sym comment) ""))
743 (method-make! <hw-memory> 'gen-sym-set-macro (lambda (self sym comment) ""))
745 ; For parallel instructions supported by queueing outputs for later update,
746 ; return the type of the index or #f if not needed.
749 <hw-memory> 'save-index?
751 ; In the case of the complete memory address being an immediate
752 ; argument, we can return #f (later).
757 <hw-memory> 'gen-write
758 (lambda (self estate index mode sfmt op access-macro)
759 (let ((index (send index 'get-write-index self sfmt op access-macro)))
761 (send self 'gen-set-quiet estate mode index
763 (cx:make DFLT (string-append access-macro " ("
768 ; Immediates, addresses.
770 ; Forward these methods onto TYPE.
771 (method-make-virtual-forward! <hw-immediate> 'type '(gen-type gen-sym-decl))
772 (method-make-forward! <hw-immediate> 'type '(gen-sym-get-macro
776 <hw-immediate> 'gen-write
777 (lambda (self estate index mode sfmt op access-macro)
778 (error "gen-write of <hw-immediate> shouldn't happen"))
782 (method-make-virtual! <hw-address> 'gen-type (lambda (self) "ADDR"))
783 (method-make-virtual! <hw-address> 'gen-sym-decl (lambda (self sym comment) ""))
784 (method-make! <hw-address> 'gen-sym-get-macro (lambda (self sym comment) ""))
785 (method-make! <hw-address> 'gen-sym-set-macro (lambda (self sym comment) ""))
787 ; Return a <c-expr> object of the value of SELF.
788 ; ESTATE is the current rtl evaluator state.
789 ; INDEX is a hw-index object. It must be an ifield.
790 ; Needed because we record our own copy of the ifield in ARGBUF.
791 ; SELECTOR is a hardware selector RTX.
794 <hw-address> 'cxmake-get
795 (lambda (self estate mode index selector)
796 (if (not (eq? 'ifield (hw-index:type index)))
797 (error "not an ifield hw-index" index))
799 (cx:make mode (gen-hw-index-argbuf-ref index))
800 (cx:make mode (gen-hw-index-argbuf-name index))))
804 <hw-address> 'gen-write
805 (lambda (self estate index mode sfmt op access-macro)
806 (error "gen-write of <hw-address> shouldn't happen"))
810 (method-make-virtual! <hw-iaddress> 'gen-type (lambda (self) "IADDR"))
812 ; Return a <c-expr> object of the value of SELF.
813 ; ESTATE is the current rtl evaluator state.
814 ; INDEX is a <hw-index> object. It must be an ifield.
815 ; Needed because we record our own copy of the ifield in ARGBUF,
816 ; *and* because we want to record in the result the 'CACHED attribute
817 ; since instruction addresses based on ifields are fixed [and thus cacheable].
818 ; SELECTOR is a hardware selector RTX.
821 <hw-iaddress> 'cxmake-get
822 (lambda (self estate mode index selector)
823 (if (not (eq? 'ifield (hw-index:type index)))
824 (error "not an ifield hw-index" index))
826 ; ??? Perhaps a better way would be to defer evaluating the src of a
827 ; set until the method processing the dest.
828 (cx:make-with-atlist mode (gen-hw-index-argbuf-ref index)
829 (atlist-make "" (bool-attr-make 'CACHED #t)))
830 (cx:make mode (gen-hw-index-argbuf-name index))))
833 ; Hardware index support code.
835 ; Return the index to use by the gen-write method.
836 ; In the cases where this is needed (the index isn't known until insn
837 ; execution time), the index is computed along with the value to be stored,
841 <hw-index> 'get-write-index
842 (lambda (self hw sfmt op access-macro)
843 (if (memq (hw-index:type self) '(scalar constant str-expr ifield))
845 (let ((index-mode (send hw 'get-index-mode)))
847 (make <hw-index> 'anonymous 'str-expr index-mode
848 (string-append access-macro " (" (-op-index-name op) ")"))
849 (hw-index-scalar)))))
852 ; Return the name of the PAREXEC structure member holding a hardware index
855 (define (-op-index-name op)
856 (string-append (gen-sym op) "_idx")
859 ; Cover fn to hardware indices to generate the actual C code.
860 ; INDEX is the hw-index object (i.e. op:index).
861 ; The result is a string of C code.
864 (define (-gen-hw-index-raw index estate)
865 (let ((type (hw-index:type index))
866 (mode (hw-index:mode index))
867 (value (hw-index:value index)))
870 ; special case UINT to cut down on unnecessary verbosity.
871 ; ??? May wish to handle more similarily.
872 ((constant) (if (mode:eq? 'UINT mode)
873 (number->string value)
874 (string-append "((" (mode:c-type mode) ") "
875 (number->string value)
878 ((rtx) (rtl-c-with-estate estate mode value))
879 ((ifield) (if (= (ifld-length value) 0)
881 (gen-extracted-ifld-value value)))
882 ((operand) (cx:c (send value 'cxmake-get estate mode (op:index value)
883 (op:selector value) #f)))
884 (else (error "-gen-hw-index-raw: invalid index:" index))))
887 ; Same as -gen-hw-index-raw except used where speedups are possible.
888 ; e.g. doing array index calcs at extraction time.
890 (define (-gen-hw-index index estate)
891 (let ((type (hw-index:type index))
892 (mode (hw-index:mode index))
893 (value (hw-index:value index)))
896 ((constant) (string-append "((" (mode:c-type mode) ") "
897 (number->string value)
900 ((rtx) (rtl-c-with-estate estate mode value))
901 ((ifield) (if (= (ifld-length value) 0)
903 (cx:c (-cxmake-ifld-val mode value))))
904 ((operand) (cx:c (send value 'cxmake-get estate mode (op:index value)
905 (op:selector value))))
906 (else (error "-gen-hw-index: invalid index:" index))))
909 ; Return address where HW is stored.
911 (define (-gen-hw-addr hw estate index)
912 (let ((setter (hw-setter hw)))
913 (cond ((and (hw-cache-addr? hw) ; FIXME: redo test
914 (eq? 'ifield (hw-index:type index)))
916 (gen-hw-index-argbuf-ref index)
917 (gen-hw-index-argbuf-name index)))
920 (gen-cpu-ref (send hw 'gen-ref
921 (gen-sym hw) index estate))))))
924 ; Return a <c-expr> object of the value of a hardware index.
927 <hw-index> 'cxmake-get
928 (lambda (self estate mode)
929 (let ((mode (if (mode:eq? 'DFLT mode) (elm-get self 'mode) mode)))
930 ; If MODE is VOID, abort.
931 (if (mode:eq? 'VOID mode)
932 (error "hw-index:cxmake-get: result needs a mode" self))
933 (cx:make (if (mode:host? mode)
934 ; FIXME: Temporary hack to generate same code as before.
935 (let ((xmode (object-copy-top mode)))
936 (obj-cons-attr! xmode (bool-attr-make 'FORCE-C #t))
939 (-gen-hw-index self estate))))
942 ; Hardware selector support code.
944 ; Generate C code for SEL.
946 (define (-gen-hw-selector sel)
950 ; Instruction operand support code.
953 ; gen-type - Return C type to use to hold operand's value.
954 ; gen-read - Record an operand's value prior to parallely executing
955 ; several instructions. Not used if gen-write used.
956 ; gen-write - Write back an operand's value after parallely executing
957 ; several instructions. Not used if gen-read used.
958 ; cxmake-get - Return C code to fetch the value of an operand.
959 ; gen-set-quiet - Return C code to set the value of an operand.
960 ; gen-set-trace - Return C code to set the value of an operand, and print
961 ; a result trace message. ??? Ideally this will go away when
962 ; trace record support is complete.
964 ; Return the C type of an operand.
965 ; Generally we forward things on to TYPE, but for the actual type we need to
966 ; use the get-mode method.
968 ;(method-make-forward! <operand> 'type '(gen-type))
972 ; First get the mode.
973 (let ((mode (send self 'get-mode)))
974 ; If it's VOID use the type's type.
975 (if (mode:eq? 'DFLT mode)
976 (send (op:type self) 'gen-type)
977 (mode:c-type mode))))
980 ; Extra pc operand methods.
984 (lambda (self estate mode index selector)
985 (let ((mode (if (mode:eq? 'DFLT mode)
986 (send self 'get-mode)
988 ; The enclosing function must set `pc' to the correct value.
989 (cx:make mode "pc")))
994 (lambda (self estate yes?)
995 (send (op:type self) 'cxmake-skip estate
996 (rtl-c INT yes? nil #:rtl-cover-fns? #t)))
999 ; For parallel write post-processing, we don't want to defer setting the pc.
1000 ; ??? Not sure anymore.
1002 ; <pc> 'gen-set-quiet
1003 ; (lambda (self estate mode index selector newval)
1004 ; (-op-gen-set-quiet self estate mode index selector newval)))
1006 ; <pc> 'gen-set-trace
1007 ; (lambda (self estate mode index selector newval)
1008 ; (-op-gen-set-trace self estate mode index selector newval)))
1010 ; Name of C macro to access parallel execution operand support.
1012 (define -par-operand-macro "OPRND")
1014 ; Return C code to fetch an operand's value and save it away for the
1015 ; semantic handler. This is used to handle parallel execution of several
1016 ; instructions where all inputs of all insns are read before any outputs are
1018 ; For operands, the word `read' is only used in this context.
1020 (define (op:read op sfmt)
1021 (let ((estate (estate-make-for-normal-rtl-c nil nil)))
1022 (send op 'gen-read estate sfmt -par-operand-macro))
1025 ; Return C code to write an operand's value.
1026 ; This is used to handle parallel execution of several instructions where all
1027 ; outputs are written to temporary spots first, and then a final
1028 ; post-processing pass is run to update cpu state.
1029 ; For operands, the word `write' is only used in this context.
1031 (define (op:write op sfmt)
1032 (let ((estate (estate-make-for-normal-rtl-c nil nil)))
1033 (send op 'gen-write estate sfmt -par-operand-macro))
1036 ; Default gen-read method.
1037 ; This is used to help support targets with parallel insns.
1038 ; Either this or gen-write (but not both) is used.
1042 (lambda (self estate sfmt access-macro)
1047 ; Pass #f for the index -> use the operand's builtin index.
1048 ; Ditto for the selector.
1049 (cx:c (send self 'cxmake-get estate DFLT #f #f))
1053 ; Forward gen-write onto the <hardware> object.
1056 <operand> 'gen-write
1057 (lambda (self estate sfmt access-macro)
1058 (let ((write-back-code (send (op:type self) 'gen-write estate
1059 (op:index self) (op:mode self)
1060 sfmt self access-macro)))
1061 ; If operand is conditionally written, we have to check that first.
1062 ; ??? If two (or more) operands are written based on the same condition,
1063 ; all the tests can be collapsed together. Not sure that's a big
1066 (string-append " if (written & (1 << "
1067 (number->string (op:num self))
1075 ; Return <c-expr> object to get the value of an operand.
1076 ; ESTATE is the current rtl evaluator state.
1077 ; If INDEX is non-#f use it, otherwise use (op:index self).
1078 ; This special handling of #f for INDEX is *only* supported for operands
1079 ; in cxmake-get, gen-set-quiet, and gen-set-trace.
1080 ; Ditto for SELECTOR.
1083 <operand> 'cxmake-get
1084 (lambda (self estate mode index selector)
1085 (let ((mode (if (mode:eq? 'DFLT mode)
1086 (send self 'get-mode)
1088 (index (if index index (op:index self)))
1089 (selector (if selector selector (op:selector self))))
1090 ; If the instruction could be parallely executed with others and we're
1091 ; doing read pre-processing, the operand has already been fetched, we
1092 ; just have to grab the cached value.
1093 ; ??? reg-raw: support wip
1094 (cond ((obj-has-attr? self 'RAW)
1095 (send (op:type self) 'cxmake-get-raw estate mode index selector))
1096 ((with-parallel-read?)
1097 (cx:make-with-atlist mode
1098 (string-append -par-operand-macro
1099 " (" (gen-sym self) ")")
1100 nil)) ; FIXME: want CACHED attr if present
1102 (let ((args (car (op:getter self)))
1103 (expr (cadr (op:getter self))))
1104 (rtl-c-expr mode expr
1105 (if (= (length args) 0)
1107 (list (list (car args) 'UINT index)))
1108 #:rtl-cover-fns? #t)))
1110 (send (op:type self) 'cxmake-get estate mode index selector)))))
1113 ; Utilities to implement gen-set-quiet/gen-set-trace.
1115 (define (-op-gen-set-quiet op estate mode index selector newval)
1116 (send (op:type op) 'gen-set-quiet estate mode index selector newval)
1119 ; Return C code to call the appropriate queued-write handler.
1122 (define (-op-gen-queued-write op estate mode index selector newval)
1123 (let* ((hw (op:type op))
1124 (setter (hw-setter hw))
1125 (sem-mode (mode:sem-mode mode)))
1129 ; FIXME: clean up (pc? op) vs (memory? hw)
1130 ; FIXME: (send 'pc?) is a temporary hack, (pc? op) didn't work
1131 (cond ((send hw 'pc?)
1147 (string-downcase (symbol->string (if sem-mode
1148 (mode-real-name sem-mode)
1149 (mode-real-name mode)))))))
1150 "_write (current_cpu"
1151 ; ??? May need to include h/w id some day.
1153 (string-append ", " (gen-reg-setter-fn hw "@cpu@"))
1155 (cond ((hw-scalar? hw)
1158 (string-append ", " (-gen-hw-index index estate)))
1160 (string-append ", " (-gen-hw-index index estate)))
1162 (string-append ", " (-gen-hw-addr (op:type op) estate index))))
1168 (define (-op-gen-set-quiet-parallel op estate mode index selector newval)
1169 (if (with-generic-write?)
1170 (-op-gen-queued-write op estate mode index selector (cx:c newval))
1172 (if (op-save-index? op)
1174 -par-operand-macro " (" (-op-index-name op) ")"
1175 " = " (-gen-hw-index index estate) ";\n")
1178 -par-operand-macro " (" (gen-sym op) ")"
1179 " = " (cx:c newval) ";\n"))
1182 (define (-op-gen-set-trace op estate mode index selector newval)
1185 " " (mode:c-type mode) " opval = " (cx:c newval) ";\n"
1186 ; Dispatch to setter code if appropriate
1189 (let ((args (car (op:setter op)))
1190 (expr (cadr (op:setter op))))
1192 (if (= (length args) 0)
1193 (list (list 'newval mode "opval"))
1194 (list (list (car args) 'UINT index)
1195 (list 'newval mode "opval")))
1196 #:rtl-cover-fns? #t))
1198 (send (op:type op) 'gen-set-quiet estate mode index selector
1199 (cx:make-with-atlist mode "opval" (cx:atlist newval))))
1201 (string-append " written |= (1 << "
1202 (number->string (op:num op))
1205 ; TRACE_RESULT_<MODE> (cpu, abuf, hwnum, opnum, value);
1206 ; For each insn record array of operand numbers [or indices into
1207 ; operand instance table].
1208 ; Could just scan the operand table for the operand or hardware number,
1209 ; assuming the operand number is stored in `op'.
1210 " TRACE_RESULT (current_cpu, abuf"
1211 ", " (send op 'gen-pretty-name mode)
1212 ", " (mode:printf-type mode)
1217 (define (-op-gen-set-trace-parallel op estate mode index selector newval)
1220 " " (mode:c-type mode) " opval = " (cx:c newval) ";\n"
1221 (if (with-generic-write?)
1222 (-op-gen-queued-write op estate mode index selector "opval")
1224 (if (op-save-index? op)
1226 -par-operand-macro " (" (-op-index-name op) ")"
1227 " = " (-gen-hw-index index estate) ";\n")
1229 " " -par-operand-macro " (" (gen-sym op) ")"
1232 (string-append " written |= (1 << "
1233 (number->string (op:num op))
1236 ; TRACE_RESULT_<MODE> (cpu, abuf, hwnum, opnum, value);
1237 ; For each insn record array of operand numbers [or indices into
1238 ; operand instance table].
1239 ; Could just scan the operand table for the operand or hardware number,
1240 ; assuming the operand number is stored in `op'.
1241 " TRACE_RESULT (current_cpu, abuf"
1242 ", " (send op 'gen-pretty-name mode)
1243 ", " (mode:printf-type mode)
1248 ; Return C code to set the value of an operand.
1249 ; NEWVAL is a <c-expr> object of the value to store.
1250 ; If INDEX is non-#f use it, otherwise use (op:index self).
1251 ; This special handling of #f for INDEX is *only* supported for operands
1252 ; in cxmake-get, gen-set-quiet, and gen-set-trace.
1253 ; Ditto for SELECTOR.
1256 <operand> 'gen-set-quiet
1257 (lambda (self estate mode index selector newval)
1258 (let ((mode (if (mode:eq? 'DFLT mode)
1259 (send self 'get-mode)
1261 (index (if index index (op:index self)))
1262 (selector (if selector selector (op:selector self))))
1263 ; ??? raw-reg: support wip
1264 (cond ((obj-has-attr? self 'RAW)
1265 (send (op:type self) 'gen-set-quiet-raw estate mode index selector newval))
1266 ((with-parallel-write?)
1267 (-op-gen-set-quiet-parallel self estate mode index selector newval))
1269 (-op-gen-set-quiet self estate mode index selector newval)))))
1272 ; Return C code to set the value of an operand and print TRACE_RESULT message.
1273 ; NEWVAL is a <c-expr> object of the value to store.
1274 ; If INDEX is non-#f use it, otherwise use (op:index self).
1275 ; This special handling of #f for INDEX is *only* supported for operands
1276 ; in cxmake-get, gen-set-quiet, and gen-set-trace.
1277 ; Ditto for SELECTOR.
1280 <operand> 'gen-set-trace
1281 (lambda (self estate mode index selector newval)
1282 (let ((mode (if (mode:eq? 'DFLT mode)
1283 (send self 'get-mode)
1285 (index (if index index (op:index self)))
1286 (selector (if selector selector (op:selector self))))
1287 ; ??? raw-reg: support wip
1288 (cond ((obj-has-attr? self 'RAW)
1289 (send (op:type self) 'gen-set-quiet-raw estate mode index selector newval))
1290 ((with-parallel-write?)
1291 (-op-gen-set-trace-parallel self estate mode index selector newval))
1293 (-op-gen-set-trace self estate mode index selector newval)))))
1296 ; Define and undefine C macros to tuck away details of instruction format used
1297 ; in the parallel execution functions. See gen-define-field-macro for a
1298 ; similar thing done for extraction/semantic functions.
1300 (define (gen-define-parallel-operand-macro sfmt)
1301 (string-append "#define " -par-operand-macro "(f) "
1302 "par_exec->operands."
1307 (define (gen-undef-parallel-operand-macro sfmt)
1308 (string-append "#undef " -par-operand-macro "\n")
1311 ; Operand profiling and parallel execution support.
1314 <operand> 'save-index?
1315 (lambda (self) (send (op:type self) 'save-index? self))
1318 ; Return boolean indicating if operand OP needs its index saved
1319 ; (for parallel write post-processing support).
1321 (define (op-save-index? op)
1322 (send op 'save-index?)
1325 ; Return C code to record profile data for modeling use.
1326 ; In the case of a register, this is usually the register's number.
1327 ; This shouldn't be called in the case of a scalar, the code should be
1328 ; smart enough to know there is no need.
1330 (define (op:record-profile op sfmt out?)
1331 (let ((estate (estate-make-for-normal-rtl-c nil nil)))
1332 (send op 'gen-record-profile sfmt out? estate))
1335 ; Return C code to record the data needed for profiling operand SELF.
1336 ; This is done during extraction.
1339 <operand> 'gen-record-profile
1340 (lambda (self sfmt out? estate)
1341 (if (hw-scalar? (op:type self))
1344 (gen-argbuf-ref (send self 'sbuf-profile-sym out?))
1346 (send (op:type self) 'gen-record-profile
1347 (op:index self) sfmt estate)
1351 ; Return C code to track profiling of operand SELF.
1352 ; This is usually called by the x-after handler.
1355 <operand> 'gen-profile-code
1356 (lambda (self insn out?)
1359 (if out? "set_" "get_")
1360 (gen-sym (op:type self))
1362 (if (hw-scalar? (op:type self))
1366 (send self 'sbuf-profile-sym out?))))
1370 ; CPU, mach, model support.
1372 ; Return the declaration of the cpu/insn enum.
1374 (define (gen-cpu-insn-enum-decl cpu insn-list)
1375 (gen-enum-decl "@prefix@_insn_type"
1376 "instructions in cpu family @cpu@"
1378 (append! (map (lambda (i)
1381 (atlist-attrs (obj-atlist i)))))
1383 (if (with-parallel?)
1387 (cons (symbol-append 'par- (obj:name i))
1389 (atlist-attrs (obj-atlist i))))
1390 (cons (symbol-append 'write- (obj:name i))
1392 (atlist-attrs (obj-atlist i))))))
1393 (parallel-insns insn-list)))
1398 ; Return the enum of INSN in cpu family CPU.
1399 ; In addition to CGEN_INSN_TYPE, an enum is created for each insn in each
1400 ; cpu family. This collapses the insn enum space for each cpu to increase
1401 ; cache efficiently (since the IDESC table is similarily collapsed).
1403 (define (gen-cpu-insn-enum cpu insn)
1404 (string-upcase (string-append "@PREFIX@_INSN_" (gen-sym insn)))
1407 ; Return C code to declare the machine data.
1409 (define (-gen-mach-decls)
1411 (string-map (lambda (mach)
1412 (gen-obj-sanitize mach
1413 (string-append "extern const MACH "
1416 (current-mach-list))
1420 ; Return C code to define the machine data.
1422 (define (-gen-mach-data)
1424 "const MACH *sim_machs[] =\n{\n"
1425 (string-map (lambda (mach)
1428 (string-append "#ifdef " (gen-have-cpu (mach-cpu mach)) "\n"
1429 " & " (gen-sym mach) "_mach,\n"
1431 (current-mach-list))
1437 ; Return C declarations of cpu model support stuff.
1438 ; ??? This goes in arch.h but a better place is each cpu.h.
1440 (define (-gen-arch-model-decls)
1442 (gen-enum-decl 'model_type "model types"
1444 (append (map (lambda (model)
1445 (cons (obj:name model)
1447 (atlist-attrs (obj-atlist model)))))
1448 (current-model-list))
1450 "#define MAX_MODELS ((int) MODEL_MAX)\n\n"
1451 (gen-enum-decl 'unit_type "unit types"
1455 ; "apply append" squeezes out nils.
1457 ; create <model_name>-<unit-name> for each unit
1458 (map (lambda (model)
1459 (let ((units (model:units model)))
1463 (cons (symbol-append (obj:name model) '-
1465 (cons '- (atlist-attrs (obj-atlist model)))))
1467 (current-model-list)))
1469 ; FIXME: revisit MAX_UNITS
1470 "#define MAX_UNITS ("
1473 (map (lambda (lengths) (apply max lengths))
1475 (let ((timing (insn-timing insn)))
1478 (map (lambda (insn-timing)
1479 (if (null? (cdr insn-timing))
1481 (length (timing:units (cdr insn-timing)))))
1483 (current-insn-list)))))
1490 (method-make! <unit> 'gen-decl (lambda (self) ""))
1492 ; Lookup operand named OP-NAME in INSN.
1493 ; Returns #f if OP-NAME is not an operand of INSN.
1494 ; IN-OUT is 'in to request an input operand, 'out to request an output operand,
1495 ; and 'in-out to request either (though if an operand is used for input and
1496 ; output then the input version is returned).
1497 ; FIXME: Move elsewhere.
1499 (define (insn-op-lookup op-name insn in-out)
1500 (letrec ((lookup (lambda (op-list)
1501 (cond ((null? op-list) #f)
1502 ((eq? op-name (op:sem-name (car op-list))) (car op-list))
1503 (else (lookup (cdr op-list)))))))
1505 ((in) (lookup (sfmt-in-ops (insn-sfmt insn))))
1506 ((out) (lookup (sfmt-out-ops (insn-sfmt insn))))
1507 ((in-out) (or (lookup (sfmt-in-ops (insn-sfmt insn)))
1508 (lookup (sfmt-out-ops (insn-sfmt insn)))))
1509 (else (error "insn-op-lookup: bad arg:" in-out))))
1512 ; Return C code to profile a unit's usage.
1513 ; UNIT-NUM is number of the unit in INSN.
1514 ; OVERRIDES is a list of (name value) pairs, where
1515 ; - NAME is a spec name, one of cycles, pred, in, out.
1516 ; The only ones we're concerned with are in,out. They map operand names
1517 ; as they appear in the semantic code to operand names as they appear in
1518 ; the function unit spec.
1519 ; - VALUE is the operand to NAME. For in,out it is (NAME VALUE) where
1520 ; - NAME is the name of an input/output arg of the unit.
1521 ; - VALUE is the name of the operand as it appears in semantic code.
1523 ; ??? This is a big sucker, though half of it is just the definitions
1527 <unit> 'gen-profile-code
1528 (lambda (self unit-num insn overrides cycles-var-name)
1530 (inputs (unit:inputs self))
1531 (outputs (unit:outputs self))
1533 ; Return C code to initialize UNIT-REFERENCED-VAR to be a bit mask
1534 ; of operands of UNIT that were read/written by INSN.
1535 ; INSN-REFERENCED-VAR is a bitmask of operands read/written by INSN.
1536 ; All we have to do is map INSN-REFERENCED-VAR to
1537 ; UNIT-REFERENCED-VAR.
1538 ; ??? For now we assume all input operands are read.
1539 (gen-ref-arg (lambda (arg num in-out)
1540 (let* ((op-name (assq-ref overrides (car arg)))
1541 (op (insn-op-lookup (if op-name
1545 (insn-referenced-var "insn_referenced")
1546 (unit-referenced-var "referenced"))
1553 (number->string (op:num op))
1557 (number->string num)
1562 (number->string num)
1566 ; Initialize unit argument ARG.
1567 ; OUT? is #f for input args, #t for output args.
1568 (gen-arg-init (lambda (arg out?)
1572 ; Ignore remapped arg, handled elsewhere.
1573 (assq (car arg) overrides)
1574 ; Ignore operands not in INSN.
1575 (not (insn-op-lookup (car arg) insn
1576 (if out? 'out 'in))))
1578 (let ((sym (gen-profile-sym (gen-c-symbol (car arg))
1583 (gen-argbuf-ref sym)
1586 ; Return C code to declare variable to hold unit argument ARG.
1587 ; OUT? is #f for input args, #t for output args.
1588 (gen-arg-decl (lambda (arg out?)
1589 (if (null? (cdr arg)) ; ignore scalars
1592 (mode:c-type (mode:lookup (cadr arg)))
1594 (gen-profile-sym (gen-c-symbol (car arg))
1597 (if (null? (cddr arg))
1599 (number->string (caddr arg)))
1602 ; Return C code to pass unit argument ARG to the handler.
1603 ; OUT? is #f for input args, #t for output args.
1604 (gen-arg-arg (lambda (arg out?)
1605 (if (null? (cdr arg)) ; ignore scalars
1608 (gen-profile-sym (gen-c-symbol (car arg))
1614 " int referenced = 0;\n"
1615 " int UNUSED insn_referenced = abuf->written;\n"
1616 ; Declare variables to hold unit arguments.
1617 (string-map (lambda (arg) (gen-arg-decl arg #f))
1619 (string-map (lambda (arg) (gen-arg-decl arg #t))
1621 ; Initialize 'em, being careful not to initialize an operand that
1623 (let (; Make a list of names of in/out overrides.
1624 (in-overrides (find-apply cadr
1625 (lambda (elm) (eq? (car elm) 'in))
1627 (out-overrides (find-apply cadr
1628 (lambda (elm) (eq? (car elm) 'out))
1631 (string-map (lambda (arg)
1632 (if (memq (car arg) in-overrides)
1634 (gen-arg-init arg #f)))
1636 (string-map (lambda (arg)
1637 (if (memq (car arg) out-overrides)
1639 (gen-arg-init arg #t)))
1641 (string-map (lambda (arg)
1648 (gen-profile-sym (gen-c-symbol (cadr arg)) #f)
1651 (gen-profile-sym (gen-c-symbol (caddr arg)) #f))
1657 (gen-profile-sym (gen-c-symbol (cadr arg)) #t)
1660 (gen-profile-sym (gen-c-symbol (caddr arg)) #t))
1664 (parse-error "insn function unit spec"
1665 "invalid spec" arg))))
1667 ; Create bitmask indicating which args were referenced.
1668 (string-map (lambda (arg num) (gen-ref-arg arg num 'in))
1670 (iota (length inputs)))
1671 (string-map (lambda (arg num) (gen-ref-arg arg num 'out))
1673 (iota (length outputs)
1675 ; Emit the call to the handler.
1676 " " cycles-var-name " += "
1677 (gen-model-unit-fn-name (unit:model self) self)
1678 " (current_cpu, idesc"
1679 ", " (number->string unit-num)
1681 (string-map (lambda (arg) (gen-arg-arg arg #f))
1683 (string-map (lambda (arg) (gen-arg-arg arg #t))
1690 ; Return C code to profile an insn-specific unit's usage.
1691 ; UNIT-NUM is number of the unit in INSN.
1694 <iunit> 'gen-profile-code
1695 (lambda (self unit-num insn cycles-var-name)
1696 (let ((args (iunit:args self))
1697 (unit (iunit:unit self)))
1698 (send unit 'gen-profile-code unit-num insn args cycles-var-name)))
1701 ; ARGBUF generation.
1702 ; ARGBUF support is put in cpuall.h, which doesn't depend on sim-cpu.scm,
1703 ; so this support is here.
1705 ; Utility of -gen-argbuf-fields-union to generate the definition for
1706 ; <sformat-abuf> SBUF.
1708 (define (-gen-argbuf-elm sbuf)
1709 (logit 2 "Processing sbuf format " (obj:name sbuf) " ...\n")
1711 " struct { /* " (obj:comment sbuf) " */\n"
1712 (let ((elms (sbuf-elms sbuf)))
1715 (string-list-map (lambda (elm)
1722 " } " (gen-sym sbuf) ";\n")
1725 ; Utility of gen-argbuf-type to generate the union of extracted ifields.
1727 (define (-gen-argbuf-fields-union)
1730 /* Instruction argument buffer. */
1732 union sem_fields {\n"
1733 (string-list-map -gen-argbuf-elm (current-sbuf-list))
1736 /* Writeback handler. */
1738 /* Pointer to argbuf entry for insn whose results need writing back. */
1739 const struct argbuf *abuf;
1741 /* x-before handler */
1743 /*const SCACHE *insns[MAX_PARALLEL_INSNS];*/
1746 /* x-after handler */
1750 /* This entry is used to terminate each pbb. */
1752 /* Number of insns in pbb. */
1754 /* Next pbb to execute. */
1756 SCACHE *branch_target;
1763 ; Generate the definition of the structure that records arguments.
1764 ; This is a union of structures with one structure for each insn format.
1765 ; It also includes hardware profiling information and miscellaneous
1767 ; CPU-DATA? is #t if data for the currently selected cpu is to be included.
1769 (define (gen-argbuf-type cpu-data?)
1770 (logit 2 "Generating ARGBUF type ...\n")
1772 (if (and cpu-data? (with-scache?))
1773 (-gen-argbuf-fields-union)
1775 (if cpu-data? "" "#ifndef WANT_CPU\n")
1777 /* The ARGBUF struct. */
1779 /* These are the baseclass definitions. */
1784 /* ??? Temporary hack for skip insns. */
1787 /* cpu specific data follows */\n"
1793 union sem_fields fields;\n"
1799 (if cpu-data? "" "#endif\n")
1804 ; Generate the definition of the structure that records a cached insn.
1805 ; This is cpu family specific (member `argbuf' is) so it is machine generated.
1806 ; CPU-DATA? is #t if data for the currently selected cpu is to be included.
1808 (define (gen-scache-type cpu-data?)
1809 (logit 2 "Generating SCACHE type ...\n")
1811 (if cpu-data? "" "#ifndef WANT_CPU\n")
1815 ??? SCACHE used to contain more than just argbuf. We could delete the
1816 type entirely and always just use ARGBUF, but for future concerns and as
1817 a level of abstraction it is left in. */
1820 struct argbuf argbuf;\n"
1821 (if (with-generic-write?) "\
1823 int last_insn_p;\n" "")
1825 (if cpu-data? "" "#endif\n")
1832 ; Generate a table of mode data.
1833 ; For now all we need is the names.
1835 (define (gen-mode-defs)
1837 "const char *mode_names[] = {\n"
1838 (string-map (lambda (m)
1839 (string-append " \"" (string-upcase (obj:str-name m)) "\",\n"))
1840 ; We don't treat aliases as being different from the real
1841 ; mode here, so ignore them.
1842 (mode-list-non-alias-values))
1847 ; Insn profiling support.
1849 ; Generate declarations for local variables needed for modelling code.
1852 <insn> 'gen-profile-locals
1853 (lambda (self model)
1854 ; (let ((cti? (or (has-attr? self 'UNCOND-CTI)
1855 ; (has-attr? self 'COND-CTI))))
1857 ; (if cti? " int UNUSED taken_p = 0;\n" "")
1862 ; Generate C code to profile INSN.
1865 <insn> 'gen-profile-code
1866 (lambda (self model cycles-var-name)
1868 (let ((timing (assq-ref (insn-timing self) (obj:name model))))
1870 (string-list-map (lambda (iunit unit-num)
1871 (send iunit 'gen-profile-code unit-num self cycles-var-name))
1872 (timing:units timing)
1873 (iota (length (timing:units timing))))
1874 (send (model-default-unit model) 'gen-profile-code 0 self nil cycles-var-name)))
1878 ; .cpu file loading support
1880 ; Only run sim-analyze-insns! once.
1881 (define -sim-insns-analyzed? #f)
1883 ; List of computed sformat argument buffers.
1884 (define -sim-sformat-abuf-list #f)
1885 (define (current-sbuf-list) -sim-sformat-abuf-list)
1887 ; Called before/after the .cpu file has been read in.
1890 (set! -sim-insns-analyzed? #f)
1891 (set! -sim-sformat-abuf-list #f)
1895 (define (sim-finish!)
1896 ; Add begin,chain,before,after,invalid handlers if not provided.
1897 ; The code generators should first look for x-foo-@prefix@, then for x-foo.
1898 ; ??? This is good enough for the first pass. Will eventually need to use
1899 ; less C and more RTL.
1901 (let ((all (stringize (current-arch-isa-name-list) ",")))
1903 (define-full-insn 'x-begin "pbb begin handler"
1904 `(VIRTUAL PBB (ISA ,all))
1905 "--begin--" '() '() '(c-code VOID "\
1907 #if WITH_SCACHE_PBB_@PREFIX@
1908 #if defined DEFINE_SWITCH || defined FAST_P
1909 /* In the switch case FAST_P is a constant, allowing several optimizations
1910 in any called inline functions. */
1911 vpc = @prefix@_pbb_begin (current_cpu, FAST_P);
1913 #if 0 /* cgen engine can't handle dynamic fast/full switching yet. */
1914 vpc = @prefix@_pbb_begin (current_cpu, STATE_RUN_FAST_P (CPU_STATE (current_cpu)));
1916 vpc = @prefix@_pbb_begin (current_cpu, 0);
1923 (define-full-insn 'x-chain "pbb chain handler"
1924 `(VIRTUAL PBB (ISA ,all))
1925 "--chain--" '() '() '(c-code VOID "\
1927 #if WITH_SCACHE_PBB_@PREFIX@
1928 vpc = @prefix@_pbb_chain (current_cpu, sem_arg);
1929 #ifdef DEFINE_SWITCH
1936 (define-full-insn 'x-cti-chain "pbb cti-chain handler"
1937 `(VIRTUAL PBB (ISA ,all))
1938 "--cti-chain--" '() '() '(c-code VOID "\
1940 #if WITH_SCACHE_PBB_@PREFIX@
1941 #ifdef DEFINE_SWITCH
1942 vpc = @prefix@_pbb_cti_chain (current_cpu, sem_arg,
1943 pbb_br_type, pbb_br_npc);
1946 /* FIXME: Allow provision of explicit ifmt spec in insn spec. */
1947 vpc = @prefix@_pbb_cti_chain (current_cpu, sem_arg,
1948 CPU_PBB_BR_TYPE (current_cpu),
1949 CPU_PBB_BR_NPC (current_cpu));
1955 (define-full-insn 'x-before "pbb begin handler"
1956 `(VIRTUAL PBB (ISA ,all))
1957 "--before--" '() '() '(c-code VOID "\
1959 #if WITH_SCACHE_PBB_@PREFIX@
1960 @prefix@_pbb_before (current_cpu, sem_arg);
1965 (define-full-insn 'x-after "pbb after handler"
1966 `(VIRTUAL PBB (ISA ,all))
1967 "--after--" '() '() '(c-code VOID "\
1969 #if WITH_SCACHE_PBB_@PREFIX@
1970 @prefix@_pbb_after (current_cpu, sem_arg);
1975 (define-full-insn 'x-invalid "invalid insn handler"
1976 `(VIRTUAL (ISA ,all))
1977 "--invalid--" '() '() (list 'c-code 'VOID (string-append "\
1979 /* Update the recorded pc in the cpu state struct.
1980 Only necessary for WITH_SCACHE case, but to avoid the
1981 conditional compilation .... */
1983 /* Virtual insns have zero size. Overwrite vpc with address of next insn
1984 using the default-insn-bitsize spec. When executing insns in parallel
1985 we may want to queue the fault and continue execution. */
1986 vpc = SEM_NEXT_VPC (sem_arg, pc, " (number->string (bits->bytes (state-default-insn-bitsize))) ");
1987 vpc = sim_engine_invalid_insn (current_cpu, pc, vpc);
1994 ; Called after file is read in and global error checks are done
1995 ; to initialize tables.
1997 (define (sim-analyze!)
2001 ; Scan insns, analyzing semantics and computing instruction formats.
2002 ; 'twould be nice to do this in sim-analyze! but it doesn't know whether this
2003 ; needs to be done or not (which is determined by what files are being
2004 ; generated). Since this is an expensive operation, we defer doing this
2005 ; to the files that need it.
2007 (define (sim-analyze-insns!)
2008 ; This can only be done if one isa and one cpu family is being kept.
2011 (if (not -sim-insns-analyzed?)
2014 (arch-analyze-insns! CURRENT-ARCH
2015 #f ; don't include aliases
2016 #t) ; do analyze the semantics
2018 ; Compute the set of sformat argument buffers.
2019 (set! -sim-sformat-abuf-list (compute-sformat-argbufs! (current-sfmt-list)))
2021 (set! -sim-insns-analyzed? #t)))
2023 ; Do our own error checking.
2024 (assert (current-insn-lookup 'x-invalid))
2031 (define (cgen-all-arch)
2042 (define (cgen-all-cpu)