1 ; CPU family related simulator generator, excluding decoding and model support.
2 ; Copyright (C) 2000, 2002, 2003, 2005, 2006, 2009 Red Hat, Inc.
3 ; This file is part of CGEN.
9 (string-upcase (gen-c-symbol (caar (list-take -1
10 (gen-obj-list-enums (non-multi-insns (current-insn-list))))))))
12 ; Declare the attributes.
14 (define (/gen-attr-decls)
16 "// Insn attribute indices.\n\n"
17 (gen-attr-enum-decl "cgen_insn" (current-insn-attr-list))
19 (string-list-map gen-decl (current-attr-list))
23 ; Generate class to hold an instruction's attributes.
25 (define (/gen-insn-attr-decls)
26 (let ((attrs (current-insn-attr-list)))
28 "// Insn attributes.\n\n"
29 ; FIXME: maybe make class, but that'll require a constructor. Later.
30 "struct @arch@_insn_attr {\n"
31 " unsigned int bools;\n"
32 (string-map (lambda (attr)
38 (string-downcase (gen-sym attr))
42 (string-map (lambda (attr)
46 " get_" (string-downcase (gen-sym attr)) "_attr"
49 (string-append "(bools & "
50 (gen-attr-mask "cgen_insn" (obj:name attr))
52 (string-downcase (gen-sym attr)))
61 ; Emit a macro that specifies the word-bitsize for each machine.
62 (define (/gen-mach-params)
63 (string-map (lambda (mach)
65 "#define MACH_" (string-upcase (gen-sym mach)) "_INSN_CHUNK_BITSIZE "
66 (number->string (cpu-insn-chunk-bitsize (mach-cpu mach))) "\n"))
71 ; Generate <cpu>-desc.h.
74 (logit 1 "Generating " (gen-cpu-name) "-desc.h ...\n")
77 (gen-c-copyright "Misc. entries in the @arch@ description file."
78 copyright-red-hat package-red-hat-simulators)
83 #include \"opcode/cgen-bitset.h\"
89 (lambda () (string-map gen-decl (current-enum-list)))
96 } // end @arch@ namespace
98 #endif /* DESC_@ARCH@_H */\n"
105 ; Print out file containing elements to add to cpu class.
107 ; Get/set fns for hardware element HW.
109 (define (/gen-reg-access-defns hw)
110 (let ((scalar? (hw-scalar? hw))
112 (getter (hw-getter hw))
113 (setter (hw-setter hw))
114 (isas (bitset-attr->list (obj-attr-value hw 'ISA)))
115 (type (gen-type hw)))
116 (let ((get-code (if getter
117 (let ((mode (hw-mode hw))
119 (expr (cadr getter)))
125 (list (list (car args) 'UINT "regno")))
129 "return this->hardware."
131 (if scalar? "" "[regno]")
134 (let ((args (car setter))
135 (expr (cadr setter)))
137 VOID ; not `mode', sets have mode VOID
140 (list (list (car args) (hw-mode hw) "newval"))
141 (list (list (car args) 'UINT "regno")
142 (list (cadr args) (hw-mode hw) "newval")))
143 #:rtl-cover-fns? #t))
147 (if scalar? "" "[regno]")
151 (gen-reg-get-fun-name hw)
153 (if scalar? "" "UINT regno")
158 (gen-reg-set-fun-name hw)
160 (if scalar? "" "UINT regno, ")
166 ; Return a boolean indicating if hardware element HW needs storage allocated
167 ; for it in the SIM_CPU struct.
169 (define (hw-need-storage? hw)
171 (not (obj-has-attr? hw 'VIRTUAL)))
174 (define (hw-need-write-stack? hw)
175 (and (register? hw) (hw-used-in-delay-rtl? hw))
178 ; Subroutine of /gen-hardware-types to generate the struct containing
179 ; hardware elements of one isa.
181 (define (/gen-hardware-struct prefix hw-list)
183 ; If struct is empty, leave it out to simplify generated code.
187 (string-append " // Hardware elements for " prefix ".\n")
188 " // Hardware elements.\n")
190 (string-list-map gen-decl hw-list)
193 (string-append prefix "_")
199 ; Return C type declarations of all of the hardware elements.
200 ; The name of the type is prepended with the cpu family name.
202 (define (/gen-hardware-types)
204 "// CPU state information.\n\n"
205 (/gen-hardware-struct #f (find hw-need-storage? (current-hw-list))))
208 (define (/gen-hw-stream-and-destream-fns)
209 (let* ((sa string-append)
210 (regs (find hw-need-storage? (current-hw-list)))
211 (stack-regs (find hw-need-write-stack? (current-hw-list)))
213 (let ((dims (/hw-vector-dims r)))
214 (if (equal? 0 (length dims))
216 (number->string (car dims))))))
218 (map (lambda (n) (sa n "_writes"))
219 (append (map (lambda (r) (gen-c-symbol (obj:name r))) stack-regs)
220 (map (lambda (m) (sa (symbol->string m) "_memory")) write-stack-memory-mode-names))))
221 (stream-reg (lambda (r)
222 (let ((rname (sa "hardware." (gen-c-symbol (obj:name r)))))
224 (sa " ost << " rname " << ' ';\n")
225 (sa " for (int i = 0; i < " (reg-dim r)
226 "; i++)\n ost << " rname "[i] << ' ';\n")))))
227 (destream-reg (lambda (r)
228 (let ((rname (sa "hardware." (gen-c-symbol (obj:name r)))))
230 (sa " ist >> " rname ";\n")
231 (sa " for (int i = 0; i < " (reg-dim r)
232 "; i++)\n ist >> " rname "[i];\n")))))
233 (stream-stacks (lambda (s) (sa " stream_stacks ( stacks." s ", ost);\n")))
234 (destream-stacks (lambda (s) (sa " destream_stacks ( stacks." s ", ist);\n")))
237 " template <typename ST> \n"
238 " void stream_stacks (const ST &st, std::ostream &ost) const\n"
240 " for (int i = 0; i < @prefix@::pipe_sz; i++)\n"
242 " ost << st[i].t << ' ';\n"
243 " for (int j = 0; j <= st[i].t; j++)\n"
245 " ost << st[i].buf[j].pc << ' ';\n"
246 " ost << st[i].buf[j].val << ' ';\n"
247 " ost << st[i].buf[j].idx0 << ' ';\n"
252 " template <typename ST> \n"
253 " void destream_stacks (ST &st, std::istream &ist)\n"
255 " for (int i = 0; i < @prefix@::pipe_sz; i++)\n"
258 " for (int j = 0; j <= st[i].t; j++)\n"
260 " ist >> st[i].buf[j].pc;\n"
261 " ist >> st[i].buf[j].val;\n"
262 " ist >> st[i].buf[j].idx0;\n"
268 " void stream_cgen_hardware (std::ostream &ost) const \n {\n"
269 (string-map stream-reg regs)
271 " void destream_cgen_hardware (std::istream &ist) \n {\n"
272 (string-map destream-reg regs)
275 (sa stack-boilerplate
276 " void stream_cgen_write_stacks (std::ostream &ost, "
277 "const @prefix@::write_stacks &stacks) const \n {\n"
278 (string-map stream-stacks write-stacks)
280 " void destream_cgen_write_stacks (std::istream &ist, "
281 "@prefix@::write_stacks &stacks) \n {\n"
282 (string-map destream-stacks write-stacks)
287 ; Generate <cpu>-cpu.h
290 (logit 1 "Generating " (gen-cpu-name) "-cpu.h ...\n")
293 ; Turn parallel execution support on if cpu needs it.
294 (set-with-parallel?! (state-parallel-exec?))
296 ; Initialize rtl->c generation.
297 (rtl-c-config! #:rtl-cover-fns? #t)
300 (gen-c-copyright "CPU class elements for @cpu@."
301 copyright-red-hat package-red-hat-simulators)
303 // This file is included in the middle of the cpu class struct.
310 /gen-hw-stream-and-destream-fns
312 " // C++ register access function templates\n"
313 "#define current_cpu this\n\n"
315 (string-list-map /gen-reg-access-defns
316 (find register? (current-hw-list))))
317 "#undef current_cpu\n\n"
324 ; Print various parameters of the cpu family.
325 ; A "cpu family" here is a collection of variants of a particular architecture
326 ; that share sufficient commonality that they can be handled together.
328 (define (/gen-cpu-defines)
331 /* Maximum number of instructions that are fetched at a time.
332 This is for LIW type instructions sets (e.g. m32r). */\n"
333 "#define @CPU@_MAX_LIW_INSNS " (number->string (cpu-liw-insns (current-cpu))) "\n\n"
334 "/* Maximum number of instructions that can be executed in parallel. */\n"
335 "#define @CPU@_MAX_PARALLEL_INSNS " (number->string (cpu-parallel-insns (current-cpu))) "\n"
337 ; (gen-enum-decl '@prefix@_virtual
338 ; "@prefix@ virtual insns"
339 ; "@ARCH@_INSN_" ; not @CPU@ to match CGEN_INSN_TYPE in opc.h
341 ; (x-before -1) (x-after -2)
342 ; (x-begin -3) (x-chain -4) (x-cti-chain -5)))
346 ; Generate type of struct holding model state while executing.
348 (define (/gen-model-decls)
349 (logit 2 "Generating model decls ...\n")
355 (if (null? (model:state model))
357 (string-map (lambda (var)
359 (mode:c-type (mode:lookup (cadr var)))
361 (gen-c-symbol (car var))
363 (model:state model)))
365 (if (null? (model:state model)) "BLANK" "@CPU@") "_MODEL_DATA;\n\n"
367 (current-model-list))
369 typedef int (@CPU@_MODEL_FN) (struct @cpu@_cpu*, void*);
372 /* This is an integer that identifies this insn.
373 How this works is up to the target. */
376 /* Function to handle insn-specific profiling. */
377 @CPU@_MODEL_FN *model_fn;
379 /* Array of function units used by this insn. */
380 UNIT units[MAX_UNITS];
381 } @CPU@_INSN_TIMING;"
385 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
386 ;;; begin stack-based write schedule
387 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
389 (define write-stack-memory-mode-names '())
391 (define (/calculated-memory-write-buffer-size)
392 (let* ((is-mem? (lambda (op) (eq? (hw-sem-name (op:type op)) 'h-memory)))
394 (lambda (sfmt) (length (find is-mem? (sfmt-out-ops sfmt))))))
395 (apply max (append '(0) (map count-mem-writes (current-sfmt-list))))))
398 ;; note: this doesn't really correctly approximate the worst case. user-supplied functions
399 ;; might rewrite the pipeline extensively while it's running.
400 ;(define (/worst-case-number-of-writes-to hw-name)
401 ; (let* ((sfmts (current-sfmt-list))
402 ; (out-ops (map sfmt-out-ops sfmts))
403 ; (pred (lambda (op) (equal? hw-name (gen-c-symbol (obj:name (op:type op))))))
404 ; (filtered-ops (map (lambda (ops) (find pred ops)) out-ops)))
405 ; (apply max (cons 0 (map (lambda (ops) (length ops)) filtered-ops)))))
407 (define (/hw-gen-write-stack-decl nm mode)
409 ; for the time being, we're disabling this size-estimation stuff and just
410 ; requiring the user to supply a parameter WRITE_BUF_SZ before they include -defs.h
411 ; (pipe-sz (+ 1 (max-delay (cpu-max-delay (current-cpu)))))
412 ; (sz (* pipe-sz (/worst-case-number-of-writes-to nm))))
414 (mode-pad (spaces (- 4 (string-length (symbol->string mode)))))
415 (stack-name (string-append nm "_writes")))
417 " write_stack< write<" (symbol->string mode) "> >" mode-pad "\t" stack-name "\t[pipe_sz];\n")))
420 (define (/hw-gen-write-struct-decl)
421 (let* ((dims (/worst-case-index-dims))
425 (ctor (sa "write (PCADDR _pc, MODE _val"
426 (string-map (lambda (x) (sa ", USI _idx" (ns x) "=0")) idxs)
427 ") : pc(_pc), val(_val)"
428 (string-map (lambda (x) (sa ", idx" (ns x) "(_idx" (ns x) ")")) idxs)
430 (idx-fields (string-map (lambda (x) (sa " USI idx" (ns x) ";\n")) idxs)))
433 " template <typename MODE>\n"
443 (define (/hw-vector-dims hw) (elm-get (hw-type hw) 'dimensions))
444 (define (/worst-case-index-dims)
446 (append '(1) ; for memory accesses
447 (map (lambda (hw) (length (/hw-vector-dims hw)))
448 (find (lambda (hw) (not (scalar? hw))) (current-hw-list))))))
451 (define (/gen-writestacks)
452 (let* ((hw (find hw-need-write-stack? (current-hw-list)))
453 (modes write-stack-memory-mode-names)
454 (hw-pairs (map (lambda (h) (list (gen-c-symbol (obj:name h))
455 (obj:name (hw-mode h))))
457 (mem-pairs (map (lambda (m) (list (string-append (symbol->string m)
460 (all-pairs (append mem-pairs hw-pairs))
462 (h1 "\n\n// write stacks used in parallel execution\n\n struct write_stacks\n {\n // types of stacks\n\n")
464 "\n\n // unified writeback function (defined in @prefix@-write.cc)"
465 "\n void writeback (int tick, @cpu@::@cpu@_cpu* current_cpu);"
466 "\n // unified write-stack clearing function (defined in @prefix@-write.cc)"
467 "\n void reset ();"))
468 (zz "\n\n }; // end struct @prefix@::write_stacks \n\n"))
470 (/hw-gen-write-struct-decl)
471 (foldl (lambda (s pair) (string-append s (apply /hw-gen-write-stack-decl pair))) h1 all-pairs)
475 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
476 ;;; end stack-based write schedule
477 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
480 ; Generate the definition of the structure that holds register values, etc.
481 ; for use during parallel execution.
483 (define (gen-write-stack-structure)
484 (let ((membuf-sz (/calculated-memory-write-buffer-size))
485 (max-delay (cpu-max-delay (current-cpu))))
486 (logit 2 "Generating write stack structure ...\n")
488 " static const int max_delay = "
489 (number->string max-delay) ";\n"
490 " static const int pipe_sz = "
491 (number->string (+ 1 max-delay)) "; // max_delay + 1\n"
494 template <typename ELT>
499 ELT buf[WRITE_BUF_SZ];
501 write_stack () : t(-1), sz(WRITE_BUF_SZ) {}
502 inline bool empty () { return (t == -1); }
503 inline void clear () { t = -1; }
504 inline void pop () { if (t > -1) t--;}
505 inline void push (const ELT &e) { if (t+1 < sz) buf [++t] = e;}
506 inline ELT &top () { return buf [t>0 ? ( t<sz ? t : sz-1) : 0];}
509 // look ahead for latest write with index = idx, where time of write is
510 // <= dist steps from base (present) in write_stack array st.
511 // returning def if no scheduled write is found.
513 template <typename STKS, typename VAL>
514 inline VAL lookahead (int dist, int base, STKS &st, VAL def, int idx=0)
516 for (; dist > 0; --dist)
518 write_stack <VAL> &v = st [(base + dist) % pipe_sz];
519 for (int i = v.t; i > 0; --i)
520 if (v.buf [i].idx0 == idx) return v.buf [i];
530 ; Generate the TRACE_RECORD struct definition.
532 (define (/gen-trace-record-type)
535 /* Collection of various things for the trace handler to use. */
537 typedef struct @prefix@_trace_record {
540 } @CPU@_TRACE_RECORD;
545 ; Generate <cpu>-defs.h
547 (define (cgen-defs.h)
548 (logit 1 "Generating " (gen-cpu-name) "-defs.h ...\n")
551 ; Turn parallel execution support on if cpu needs it.
552 (set-with-parallel?! (state-parallel-exec?))
554 ; Initialize rtl->c generation.
555 (rtl-c-config! #:rtl-cover-fns? #t)
558 (gen-c-copyright "CPU family header for @cpu@ / @prefix@."
559 copyright-red-hat package-red-hat-simulators)
561 #ifndef DEFS_@PREFIX@_H
562 #define DEFS_@PREFIX@_H
568 #include \"cgen-types.h\"
570 // forward declaration\n\n
577 using namespace cgen;
580 gen-write-stack-structure
582 } // end @prefix@ namespace
586 #endif /* DEFS_@PREFIX@_H */\n"
593 ; This is the other way of implementing parallel execution support.
594 ; Instead of fetching all the input operands first, write all the output
595 ; operands and their addresses to holding variables, and then run a
596 ; post-processing pass to update the cpu state.
598 ; Return C code to fetch and save all output operands to instructions with
602 ; Generate <cpu>-write.cxx.
604 (define (/gen-register-writer nm mode dims)
607 (mode (symbol->string mode))
608 (idx-args (string-map (lambda (x) (sa "w.idx" (number->string x) ", "))
610 (sa pad "while (! " nm "_writes[tick].empty())\n"
612 pad " write<" mode "> &w = " nm "_writes[tick].top();\n"
613 pad " current_cpu->" nm "_set(" idx-args "w.val);\n"
614 pad " " nm "_writes[tick].pop();\n"
617 (define (/gen-memory-writer nm mode dims)
620 (mode (symbol->string mode))
621 (idx-args (string-map (lambda (x) (sa ", w.idx" (number->string x) ""))
623 (sa pad "while (! " nm "_writes[tick].empty())\n"
625 pad " write<" mode "> &w = " nm "_writes[tick].top();\n"
626 pad " current_cpu->SETMEM" mode " (w.pc" idx-args ", w.val);\n"
627 pad " " nm "_writes[tick].pop();\n"
631 (define (/gen-reset-fn)
632 (let* ((sa string-append)
633 (objs (append (map (lambda (h) (gen-c-symbol (obj:name h)))
634 (find hw-need-write-stack? (current-hw-list)))
635 (map (lambda (m) (sa (symbol->string m) "_memory"))
636 write-stack-memory-mode-names)))
637 (clr (lambda (elt) (sa " clear_stacks (" elt "_writes);\n"))))
639 " template <typename ST> \n"
640 " static void clear_stacks (ST &st)\n"
642 " for (int i = 0; i < @prefix@::pipe_sz; i++)\n"
645 " void @prefix@::write_stacks::reset ()\n {\n"
646 (string-map clr objs)
649 (define (/gen-unified-write-fn)
650 (let* ((hw (find hw-need-write-stack? (current-hw-list)))
651 (modes write-stack-memory-mode-names)
652 (hw-triples (map (lambda (h) (list (gen-c-symbol (obj:name h))
653 (obj:name (hw-mode h))
654 (length (/hw-vector-dims h))))
656 (mem-triples (map (lambda (m) (list (string-append (symbol->string m)
660 (logit 2 "Generating writer function ...\n")
663 void @prefix@::write_stacks::writeback (int tick, @cpu@::@cpu@_cpu* current_cpu)
666 "\n // register writeback loops\n"
667 (string-map (lambda (t) (apply /gen-register-writer t)) hw-triples)
668 "\n // memory writeback loops\n"
669 (string-map (lambda (t) (apply /gen-memory-writer t)) mem-triples)
674 (define (cgen-write.cxx)
675 (logit 1 "Generating " (gen-cpu-name) "-write.cxx ...\n")
680 ; Turn parallel execution support off.
681 (set-with-parallel?! #f)
683 ; Tell the rtx->c translator we are the simulator.
684 (rtl-c-config! #:rtl-cover-fns? #t)
687 (gen-c-copyright (string-append "Simulator instruction operand writer for "
688 (symbol->string (current-arch-name))
690 copyright-red-hat package-red-hat-simulators)
697 /gen-unified-write-fn
704 ; Return C code to perform the semantics of INSN.
706 (define (gen-semantic-code insn)
707 ; Indicate generating code for INSN.
708 ; Use the canonical form if available.
709 ; The case when they're not available is for virtual insns. (??? Still true?)
710 (cond ((insn-compiled-semantics insn)
712 (rtl-c++-parsed VOID sem nil
716 ((insn-canonical-semantics insn)
718 (rtl-c++-parsed VOID sem nil
723 (rtl-c++ VOID (insn-semantics insn) nil
729 ; Return definition of C function to perform INSN.
730 ; This version handles the with-scache case.
732 (define (/gen-scache-semantic-fn insn)
733 (logit 2 "Processing semantics for " (obj:name insn) ": \"" (insn-syntax insn) "\" ...\n")
734 (set! /with-profile? /with-profile-fn?)
735 (let ((cti? (insn-cti? insn))
736 (insn-len (insn-length-bytes insn)))
738 "// ********** " (obj:name insn) ": " (insn-syntax insn) "\n\n"
742 "@prefix@_sem_" (gen-sym insn)
744 (string-append " (@cpu@_cpu* current_cpu, @prefix@_scache* sem, const int tick, \n\t"
745 "@prefix@::write_stacks &buf)\n")
746 " (@cpu@_cpu* current_cpu, @prefix@_scache* sem)\n")
748 (gen-define-field-macro (insn-sfmt insn))
749 " sem_status status = SEM_STATUS_NORMAL;\n"
750 " @prefix@_scache* abuf = sem;\n"
751 ; Unconditionally written operands are not recorded here.
752 (if (or (with-profile?) (with-parallel-write?))
753 " unsigned long long written = 0;\n"
755 ; The address of this insn, needed by extraction and semantic code.
756 ; Note that the address recorded in the cpu state struct is not used.
757 ; For faster engines that copy will be out of date.
758 " PCADDR pc = abuf->addr;\n"
759 " PCADDR npc = pc + " (number->string insn-len) ";\n"
761 (gen-semantic-code insn)
763 ; Only update what's been written if some are conditionally written.
764 ; Otherwise we know they're all written so there's no point in
766 (if (or (with-profile?) (with-parallel-write?))
767 (if (/any-cond-written? (insn-sfmt insn))
768 " abuf->written = written;\n"
772 " current_cpu->done_cti_insn (npc, status);\n"
773 " current_cpu->done_insn (npc, status);\n")
777 (gen-undef-field-macro (insn-sfmt insn))
782 (define (/gen-all-semantic-fns)
783 (logit 2 "Processing semantics ...\n")
784 (let ((insns (scache-engine-insns)))
786 (string-write-map /gen-scache-semantic-fn insns)
787 (error "must specify `with-scache'")))
790 ; Generate <cpu>-sem.cxx.
791 ; Each instruction is implemented in its own function.
793 (define (cgen-semantics.cxx)
794 (logit 1 "Generating " (gen-cpu-name) "-semantics.cxx ...\n")
799 ; Turn parallel execution support on if cpu needs it.
800 (set-with-parallel?! (state-parallel-exec?))
802 ; Tell the rtx->c translator we are the simulator.
803 (rtl-c-config! #:rtl-cover-fns? #t)
805 ; Indicate we're currently not generating a pbb engine.
806 (set-current-pbb-engine?! #f)
809 (gen-c-copyright "Simulator instruction semantics for @prefix@."
810 copyright-red-hat package-red-hat-simulators)
814 #include \"config.h\"
818 using namespace @cpu@; // FIXME: namespace organization still wip\n")
821 using namespace @prefix@; // FIXME: namespace organization still wip\n"))
823 #define GET_ATTR(name) GET_ATTR_##name ()
827 /gen-all-semantic-fns
831 ; *******************
832 ; cgen-sem-switch.cxx
834 ; The semantic switch engine has two flavors: one case per insn, and one
835 ; case per "frag" (where each insn is split into one or more fragments).
837 ; Utility of /gen-sem-case to return the mask of operands always written
838 ; to in <sformat> SFMT.
839 ; ??? Not currently used.
841 (define (/uncond-written-mask sfmt)
842 (apply + (map (lambda (op)
845 (logsll 1 (op:num op))))
846 (sfmt-out-ops sfmt)))
849 ; Utility of /gen-sem-case to return #t if any operand in <sformat> SFMT is
850 ; conditionally written to.
852 (define (/any-cond-written? sfmt)
853 (any-true? (map op:cond? (sfmt-out-ops sfmt)))
856 ; One case per insn version.
858 ; Generate a switch case to perform INSN.
860 (define (/gen-sem-case insn parallel?)
861 (logit 2 "Processing "
862 (if parallel? "parallel " "")
863 "semantic switch case for \"" (insn-syntax insn) "\" ...\n")
864 (set! /with-profile? /with-profile-sw?)
865 (let ((cti? (insn-cti? insn))
866 (insn-len (insn-length-bytes insn)))
868 ; INSN_ is prepended here and not elsewhere to avoid name collisions
869 ; with symbols like AND, etc.
871 // ********** " (insn-syntax insn) "
873 CASE (INSN_" (if parallel? "PAR_" "") (string-upcase (gen-sym insn)) "):
875 @prefix@_scache* abuf = vpc;\n"
877 (gen-define-field-macro (insn-sfmt insn))
879 ; Unconditionally written operands are not recorded here.
880 (if (or (with-profile?) (with-parallel-write?))
881 " unsigned long long written = 0;\n"
883 ; The address of this insn, needed by extraction and semantic code.
884 ; Note that the address recorded in the cpu state struct is not used.
885 " PCADDR pc = abuf->addr;\n"
886 (if (and cti? (not parallel?))
887 (string-append " PCADDR npc;\n"
888 " branch_status br_status = BRANCH_UNTAKEN;\n")
890 (string-list " vpc = vpc + 1;\n")
891 ; Emit setup-semantics code for real insns.
892 (if (and (insn-real? insn)
893 (isa-setup-semantics (current-isa)))
896 (rtl-c++ VOID (isa-setup-semantics (current-isa)) nil
902 (gen-semantic-code insn)
904 ; Only update what's been written if some are conditionally written.
905 ; Otherwise we know they're all written so there's no point in
907 (if (or (with-profile?) (with-parallel-write?))
908 (if (/any-cond-written? (insn-sfmt insn))
909 " abuf->written = written;\n"
912 (if (and cti? (not parallel?))
913 (string-append " pbb_br_npc = npc;\n"
914 " pbb_br_status = br_status;\n")
917 (gen-undef-field-macro (insn-sfmt insn))
924 (define (/gen-sem-switch)
925 (logit 2 "Processing semantic switch ...\n")
926 ; Turn parallel execution support off.
927 (set-with-parallel?! #f)
928 (string-write-map (lambda (insn) (/gen-sem-case insn #f))
929 (non-multi-insns (non-alias-insns (current-insn-list))))
932 ; Generate the guts of a C switch statement to execute parallel instructions.
933 ; This switch is included after the non-parallel instructions in the semantic
936 ; ??? We duplicate the writeback case for each insn, even though we only need
937 ; one case per insn format. The former keeps the code for each insn
938 ; together and might improve cache usage. On the other hand the latter
939 ; reduces the amount of code, though it is believed that in this particular
940 ; instance the win isn't big enough.
942 (define (/gen-parallel-sem-switch)
943 (logit 2 "Processing parallel insn semantic switch ...\n")
944 ; Turn parallel execution support on.
945 (set-with-parallel?! #t)
946 (string-write-map (lambda (insn)
947 (string-list (/gen-sem-case insn #t)
948 (/gen-write-case (insn-sfmt insn) insn)))
949 (parallel-insns (current-insn-list)))
952 ; Return computed-goto engine.
954 (define (/gen-sem-switch-engine)
958 @cpu@_cpu::@prefix@_pbb_run ()
960 @cpu@_cpu* current_cpu = this;
961 @prefix@_scache* vpc;
962 // These two are used to pass data from cti insns to the cti-chain insn.
964 branch_status pbb_br_status;
968 static const struct sem_labels
970 enum @prefix@_insn_type insn;
977 (string-write-map (lambda (insn)
980 (string-upcase (gen-sym insn))
982 (string-upcase (gen-sym insn))
984 (non-multi-insns (non-alias-insns (current-insn-list)))))
986 (if (state-parallel-exec?)
988 (string-write-map (lambda (insn)
991 (string-upcase (gen-sym insn))
992 ", && case_INSN_PAR_"
993 (string-upcase (gen-sym insn))
996 "@PREFIX@_INSN_WRITE_"
997 (string-upcase (gen-sym insn))
998 ", && case_INSN_WRITE_"
999 (string-upcase (gen-sym insn))
1001 (parallel-insns (current-insn-list))))
1004 " { (@prefix@_insn_type) 0, 0 }
1007 if (! @prefix@_idesc::idesc_table_initialized_p)
1009 for (int i=0; labels[i].label != 0; i++)
1010 @prefix@_idesc::idesc_table[labels[i].insn].cgoto.label = labels[i].label;
1012 // confirm that table is all filled up
1013 for (int i = 0; i <= @PREFIX@_INSN_" (/last-insn) "; i++)
1014 assert (@prefix@_idesc::idesc_table[i].cgoto.label != 0);
1016 // Initialize the compiler virtual insn.
1017 current_cpu->@prefix@_engine.compile_begin_insn (current_cpu);
1019 @prefix@_idesc::idesc_table_initialized_p = true;
1025 #define CASE(X) case_##X
1026 // Branch to next handler without going around main loop.
1027 #define NEXT(vpc) goto * vpc->execute.cgoto.label;
1028 // Break out of threaded interpreter and return to \"main loop\".
1029 #define BREAK(vpc) goto end_switch
1031 #define CASE(X) case @PREFIX@_##X
1032 #define NEXT(vpc) goto restart
1033 #define BREAK(vpc) break
1036 // Get next insn to execute.
1037 vpc = current_cpu->@prefix@_engine.get_next_vpc (current_cpu->h_pc_get ());
1041 goto * vpc->execute.cgoto.label;
1043 switch (vpc->idesc->sem_index)
1051 (if (state-parallel-exec?)
1052 /gen-parallel-sem-switch
1063 // Save vpc for next time.
1064 current_cpu->@prefix@_engine.set_next_vpc (vpc);
1070 ; Semantic frag version.
1072 ; Return declaration of frag enum.
1074 (define (/gen-sfrag-enum-decl frag-list)
1075 (gen-enum-decl "@prefix@_frag_type"
1076 "semantic fragments in cpu family @prefix@"
1078 (append '((list-end))
1082 (atlist-attrs (obj-atlist i)))))
1087 ; Return header file decls for semantic frag threaded engine.
1089 (define (/gen-sfrag-engine-decls)
1091 "namespace @cpu@ {\n\n"
1093 ; FIXME: vector->list
1094 (/gen-sfrag-enum-decl (vector->list (sim-sfrag-frag-table)))
1097 struct @prefix@_insn_frag {
1098 @PREFIX@_INSN_TYPE itype;
1099 // 4: header+middle+trailer+delimiter
1100 @PREFIX@_FRAG_TYPE ftype[4];
1103 struct @prefix@_pbb_label {
1104 @PREFIX@_FRAG_TYPE frag;
1108 } // end @cpu@ namespace
1112 ; Return C code to perform the semantics of FRAG.
1113 ; LOCALS is a list of sequence locals made global to all frags.
1114 ; Each element is (symbol <mode> "c-var-name").
1116 (define (/gen-sfrag-code frag locals)
1117 ; Indicate generating code for FRAG.
1118 ; Use the compiled form if available.
1119 ; The case when they're not available is for virtual insns.
1120 (let ((sem (sfrag-compiled-semantics frag))
1121 ; If the frag has one owner, use it. Otherwise indicate the owner is
1122 ; unknown. In cases where the owner is needed by the semantics, the
1123 ; frag should have only one owner. In practice this means that frags
1124 ; with the ref,current-insn rtx cannot be used by multiple insns.
1125 (owner (if (= (length (sfrag-users frag)) 1)
1126 (car (sfrag-users frag))
1130 (rtl-c++-parsed VOID sem locals
1134 (rtl-c++ VOID (sfrag-semantics frag) locals
1140 ; Generate a switch case to perform FRAG.
1141 ; LOCALS is a list of sequence locals made global to all frags.
1142 ; Each element is (symbol <mode> "c-var-name").
1144 (define (/gen-sfrag-case frag locals)
1145 (set! /with-profile? /with-profile-sw?)
1146 (let ((cti? (sfmt-cti? (sfrag-sfmt frag)))
1147 (parallel? (sfrag-parallel? frag)))
1148 (logit 2 "Processing "
1149 (if parallel? "parallel " "")
1150 "semantic switch case for \"" (obj:name frag) "\" ...\n")
1152 ; FRAG_ is prepended here and not elsewhere to avoid name collisions
1153 ; with symbols like AND, etc.
1156 (if (= (length (sfrag-users frag)) 1)
1160 (string-map (lambda (user)
1161 (string-append ", " (obj:str-name user)))
1162 (sfrag-users frag)))
1165 CASE (FRAG_" (string-upcase (gen-sym frag)) "):
1167 (if (sfrag-header? frag)
1168 (string-append " abuf = vpc;\n"
1169 " vpc = vpc + 1;\n")
1171 (gen-define-field-macro (sfrag-sfmt frag))
1172 ; Unconditionally written operands are not recorded here.
1173 (if (or (with-profile?) (with-parallel-write?))
1174 " unsigned long long written = 0;\n"
1176 ; The address of this insn, needed by extraction and semantic code.
1177 ; Note that the address recorded in the cpu state struct is not used.
1178 " PCADDR pc = abuf->addr;\n"
1181 (sfrag-header? frag))
1182 (string-append ; " npc = 0;\n" ??? needed?
1183 " br_status = BRANCH_UNTAKEN;\n")
1185 ; Emit setup-semantics code for headers of real insns.
1186 (if (and (sfrag-header? frag)
1187 (not (obj-has-attr? frag 'VIRTUAL))
1188 (isa-setup-semantics (current-isa)))
1191 (rtl-c++ VOID (isa-setup-semantics (current-isa)) nil
1196 (/gen-sfrag-code frag locals)
1198 ; Only update what's been written if some are conditionally written.
1199 ; Otherwise we know they're all written so there's no point in
1201 (if (or (with-profile?) (with-parallel-write?))
1202 (if (/any-cond-written? (sfrag-sfmt frag))
1203 " abuf->written = written;\n"
1208 (sfrag-trailer? frag))
1209 (string-append " pbb_br_npc = npc;\n"
1210 " pbb_br_status = br_status;\n")
1212 (gen-undef-field-macro (sfrag-sfmt frag))
1214 (if (sfrag-trailer? frag)
1215 " NEXT_INSN (vpc, fragpc);\n"
1216 " NEXT_FRAG (fragpc);\n")
1221 ; Convert locals from form computed by sem-find-common-frags to that needed by
1222 ; /gen-sfrag-engine-code (and ultimately rtl-c++).
1224 (define (/frag-convert-c-locals locals)
1225 (map (lambda (local)
1226 (list (car local) (mode:lookup (cadr local))
1227 (gen-c-symbol (car local))))
1231 ; Return definition of insn frag usage table.
1233 (define (/gen-sfrag-engine-frag-table insn-list frag-table frag-usage)
1236 // Table of frags used by each insn.
1238 const @prefix@_insn_frag @prefix@_frag_usage[] = {\n"
1241 (for-each (lambda (insn frag-nums)
1244 (string-upcase (gen-sym insn))
1245 (string-map (lambda (frag-num)
1246 (string-append ", @PREFIX@_FRAG_"
1247 (string-upcase (gen-sym (vector-ref frag-table frag-num)))))
1249 ", @PREFIX@_FRAG_LIST_END },\n"))
1250 insn-list frag-usage)
1255 ; Return sfrag computed-goto engine.
1256 ; LOCALS is a list of sequence locals made global to all frags.
1257 ; Each element is (symbol <mode> "c-var-name").
1259 (define (/gen-sfrag-engine-fn frag-table locals)
1263 @cpu@_cpu::@prefix@_pbb_run ()
1265 @cpu@_cpu* current_cpu = this;
1266 @prefix@_scache* vpc;
1267 @prefix@_scache* abuf;
1271 ARM_FRAG_TYPE* fragpc;
1276 static const @prefix@_pbb_label labels[] =
1278 { @PREFIX@_FRAG_LIST_END, 0 },
1282 (string-write-map (lambda (frag)
1283 (string-append " { "
1285 (string-upcase (gen-sym frag))
1287 (string-upcase (gen-sym frag))
1289 ; FIXME: vector->list
1290 (vector->list frag-table)))
1293 { @PREFIX@_FRAG_MAX, 0 }
1296 if (! @prefix@_idesc::idesc_table_initialized_p)
1298 // Several tables are in play here:
1299 // idesc table: const table of misc things for each insn
1300 // frag usage table: const set of frags used by each insn
1301 // frag label table: same as frag usage table, but contains labels
1302 // selected insn frag table: table of pointers to either the frag usage
1303 // table (if !gnuc) or frag label table (if gnuc) for the currently
1304 // selected ISA. Insns not in the ISA are redirected to the `invalid'
1305 // insn handler. FIXME: This one isn't implemented yet.
1307 // Allocate frag label table and point idesc table entries at it.
1308 // FIXME: Temporary hack, to be redone.
1309 static void** frag_label_table;
1310 int max_insns = @PREFIX@_INSN_" (/last-insn) " + 1;
1311 int tabsize = max_insns * 4;
1312 frag_label_table = new void* [tabsize];
1313 memset (frag_label_table, 0, sizeof (void*) * tabsize);
1316 for (i = 0, v = frag_label_table; i < max_insns; ++i)
1318 @prefix@_idesc::idesc_table[@prefix@_frag_usage[i].itype].cgoto.frags = v;
1319 for (int j = 0; @prefix@_frag_usage[i].ftype[j] != @PREFIX@_FRAG_LIST_END; ++j)
1320 *v++ = labels[@prefix@_frag_usage[i].ftype[j]].label;
1323 // Initialize the compiler virtual insn.
1324 // FIXME: Also needed if !gnuc.
1325 current_cpu->@prefix@_engine.compile_begin_insn (current_cpu);
1327 @prefix@_idesc::idesc_table_initialized_p = true;
1333 #define CASE(X) case_##X
1334 // Branch to next handler without going around main loop.
1335 #define NEXT_INSN(vpc, fragpc) fragpc = vpc->execute.cgoto.frags; goto * *fragpc
1336 #define NEXT_FRAG(fragpc) ++fragpc; goto * *fragpc
1337 // Break out of threaded interpreter and return to \"main loop\".
1338 #define BREAK(vpc) goto end_switch
1340 #define CASE(X) case @PREFIX@_##X
1341 #define NEXT_INSN(vpc, fragpc) fragpc = vpc->idesc->frags; goto restart
1342 #define NEXT_FRAG(fragpc) ++fragpc; goto restart
1343 #define BREAK(vpc) break
1346 // Get next insn to execute.
1347 vpc = current_cpu->@prefix@_engine.get_next_vpc (current_cpu->h_pc_get ());
1350 // These two are used to pass data from cti insns to the cti-chain insn.
1352 branch_status pbb_br_status;
1353 // These two are used to build up values of the previous two.
1355 branch_status br_status;
1356 // Top level locals moved here so they're usable by multiple fragments.
1360 (string-write-map (lambda (local)
1362 (mode:c-type (cadr local))
1372 fragpc = vpc->execute.cgoto.frags;
1375 fragpc = vpc->idesc->frags;
1384 ; Turn parallel execution support off.
1386 (set-with-parallel?! #f)
1387 (string-write-map (lambda (frag)
1388 (/gen-sfrag-case frag locals))
1389 ; FIXME: vector->list
1390 (vector->list frag-table)))
1401 // Save vpc for next time.
1402 current_cpu->@prefix@_engine.set_next_vpc (vpc);
1407 (define (/gen-sfrag-engine)
1410 (/gen-sfrag-engine-frag-table (sim-sfrag-insn-list)
1411 (sim-sfrag-frag-table)
1412 (sim-sfrag-usage-table)))
1414 (/gen-sfrag-engine-fn (sim-sfrag-frag-table)
1415 (/frag-convert-c-locals (sim-sfrag-locals-list))))
1419 ; Generate sem-switch.cxx.
1421 (define (cgen-sem-switch.cxx)
1422 (logit 1 "Generating " (gen-cpu-name) "-sem-switch.cxx ...\n")
1424 (sim-analyze-insns!)
1425 (if (with-sem-frags?)
1426 (sim-sfrag-analyze-insns!))
1428 ; Turn parallel execution support off.
1429 ; It is later turned on/off when generating the actual semantic code.
1430 (set-with-parallel?! #f)
1432 ; Tell the rtx->c translator we are the simulator.
1433 (rtl-c-config! #:rtl-cover-fns? #t)
1435 ; Indicate we're currently generating a pbb engine.
1436 (set-current-pbb-engine?! #t)
1439 (gen-c-copyright "Simulator instruction semantics for @prefix@."
1440 copyright-red-hat package-red-hat-simulators)
1443 #include \"@cpu@.h\"
1445 using namespace @cpu@; // FIXME: namespace organization still wip
1447 #define GET_ATTR(name) GET_ATTR_##name ()
1451 (if (with-sem-frags?)
1452 /gen-sfrag-engine-decls
1455 (if (with-sem-frags?)
1457 /gen-sem-switch-engine)