1 ; Top level file for reading and recording .cpu file contents.
2 ; Copyright (C) 2000, 2001, 2006, 2009 Red Hat, Inc.
3 ; This file is part of CGEN.
4 ; See file COPYING.CGEN for details.
6 ; This file [and its subordinates] contain no C code (well, as little as
7 ; possible). That lives at a layer above us.
9 ; A .cpu file consists of several sections:
11 ; - basic definitions (e.g. cpu variants, word size, endianness, etc.)
12 ; - enums (enums are used throughout so by convention there is a special
13 ; section in which they're defined)
15 ; - instruction fields and formats
16 ; - hardware descriptions (e.g. registers, allowable immediate values)
17 ; - model descriptions (e.g. pipelines, latencies, etc.)
18 ; - instruction operands (mapping of insn fields to associated hardware)
19 ; - instruction definitions
20 ; - macro instruction definitions
23 ; - memory access, layout, etc.
24 ; - floating point quirks
25 ; - ability to describe an ABI
26 ; - anything else that comes along
29 ; - by convention most objects are subclasses of <ident> (having name, comment,
30 ; and attrs elements and they are the first three elements of any .cpu file
34 ; - Try to conform to R5RS, try to limit guile-ness.
35 ; The current code is undoubtedly off in many places.
38 ; [I want there to be a plethora of conventions and I want them strictly
39 ; adhered to. ??? There's probably a few violations here and there.
40 ; No big deal - fix them!]
41 ; These conventions are subject to revision.
43 ; - procs/vars local to a file are named "-foo"
44 ; - only routines that emit application code begin with "gen-"
45 ; - symbols beginning with "c-" are either variables containing C code
46 ; or procedures that generate C code, similarily for C++ and "c++-"
47 ; - variables containing C code begin with "c-"
48 ; - only routines that emit an entire file begin with "cgen-"
49 ; - all .cpu file elements shall have -foo-parse and -foo-read procedures
50 ; - global vars containing class definitions shall be named "<class-name>"
51 ; - procs related to a particular class shall be named "class-name-proc-name",
52 ; class-name may be abbreviated
53 ; - procs that test whether something is an object of a particular class
54 ; shall be named "class-name?"
55 ; - in keeping with Scheme conventions, predicates shall have a "?" suffix
56 ; - in keeping with Scheme conventions, methods and procedures that modify an
57 ; argument or have other side effects shall have a "!" suffix,
58 ; usually these procs return "*UNSPECIFIED*"
59 ; - all -foo-parse,parse-foo procs shall have `context' as the first arg
60 ; [FIXME: not all such procs have been converted]
61 ; - stay away from non-portable C symbols.
63 ; Variables representing misc. global constants.
65 ; A list of three numbers designating the cgen version: major minor fixlevel.
66 ; The "50" is a generic indicator that we're between 1.1 and 1.2.
67 (define -CGEN-VERSION '(1 1 50))
68 (define (cgen-major) (car -CGEN-VERSION))
69 (define (cgen-minor) (cadr -CGEN-VERSION))
70 (define (cgen-fixlevel) (caddr -CGEN-VERSION))
72 ; A list of two numbers designating the description language version.
73 ; Note that this is different from -CGEN-VERSION.
74 ; See section "RTL Versions" of the docs.
75 (define -CGEN-RTL-VERSION '(0 7))
76 (define (cgen-rtl-major) (car -CGEN-RTL-VERSION))
77 (define (cgen-rtl-minor) (cadr -CGEN-RTL-VERSION))
79 ;; List of supported versions
80 (define -supported-rtl-versions '((0 7)))
82 (define (-cmd-define-rtl-version major minor)
83 (if (not (non-negative-integer? major))
84 (parse-error #f "Invalid major version number" major))
85 (if (not (non-negative-integer? minor))
86 (parse-error #f "Invalid minor version number" minor))
88 (let ((new-version (list major minor)))
89 (if (not (member new-version -supported-rtl-versions))
90 (parse-error #f "Unsupported/invalid rtl version" new-version))
91 (logit 1 "Setting RTL version to " major "." minor " ...\n")
92 (set! -CGEN-RTL-VERSION new-version))
95 ; Which application is in use (UNKNOWN, DESC, OPCODES, SIMULATOR, ???).
96 ; This is mostly for descriptive purposes.
97 (define APPLICATION 'UNKNOWN)
99 ; Things are organized so that files can be compiled with Hobbit for
100 ; experimentation. Thus we need one file that loads all the other files.
101 ; This is that file, though it would make sense to move the code in this
104 ; If a routine to initialize compiled-in code is defined, run it.
105 (if (defined? 'cgen-init-c) (cgen-init-c))
107 ; If this is set to #f, the file is always loaded.
108 ; Don't override any current setting, e.g. from dev.scm.
109 (if (not (defined? 'CHECK-LOADED?))
110 (define CHECK-LOADED? #t))
112 ; Unlink file if we're reloaded (say in an interactive session).
113 ; Dynamic loading is enabled by setting LIBCPU.SO to the pathname of the .so.
114 (if (and (defined? 'libcpu.so) (dynamic-object? libcpu.so))
115 (dynamic-unlink libcpu.so))
116 (define libcpu.so #f)
117 (if (and (defined? 'LIBCPU.SO)
118 (file-exists? LIBCPU.SO))
119 (set! libcpu.so (dynamic-link LIBCPU.SO))
122 ; List of loaded files.
124 (if (not (defined? '-loaded-file-list))
125 (define -loaded-file-list '()))
127 ; Return non-zero if FILE was loaded last time through.
129 (define (-loaded-file? file)
130 (->bool (memq (string->symbol file) -loaded-file-list))
133 ; Record FILE as compiled in.
135 (define (-loaded-file-record! file)
136 (let ((file (string->symbol file)))
137 (if (not (memq file -loaded-file-list))
138 (set! -loaded-file-list (cons file -loaded-file-list))))
141 ; Load FILE if SYM is not compiled in.
143 (define (maybe-load file init-func sym)
144 ; Return non-#f if FUNC is present in DYNOBJ.
145 (define (dynamic-func? func dynobj)
147 (lambda () (dynamic-func func dynobj))
151 (let ((init-func (string-append "init_" (if init-func init-func file))))
152 (cond ((and libcpu.so
153 (dynamic-func? init-func libcpu.so))
154 (dynamic-call init-func libcpu.so)
155 (display (string-append "Skipping " file ", dynamically loaded.\n")))
156 ((or (not CHECK-LOADED?)
158 (-loaded-file? file))
159 (-loaded-file-record! file)
162 (display (string-append "Skipping " file ", already loaded.\n")))))
165 (maybe-load "pmacros" #f 'define-pmacro)
166 (maybe-load "cos" #f 'make)
167 (maybe-load "slib/logical" #f 'logical:logand)
168 (maybe-load "slib/sort" #f 'sort)
169 ; Used to pretty-print debugging messages.
170 (maybe-load "slib/pp" #f 'pretty-print)
171 ; Used by pretty-print.
172 (maybe-load "slib/random" #f 'random)
173 (maybe-load "slib/genwrite" #f 'generic-write)
174 (maybe-load "utils" #f 'logit)
175 (maybe-load "utils-cgen" "utils_cgen" 'obj:name)
176 (maybe-load "attr" #f '<attribute>)
177 (maybe-load "enum" #f '<enum>)
178 (maybe-load "mach" #f '<mach>)
179 (maybe-load "model" #f '<model>)
180 (maybe-load "types" #f '<scalar>)
181 (maybe-load "mode" #f '<mode>)
182 (maybe-load "ifield" #f '<ifield>)
183 (maybe-load "iformat" #f '<iformat>)
184 (maybe-load "hardware" #f '<hardware-base>)
185 (maybe-load "operand" #f '<operand>)
186 (maybe-load "insn" #f '<insn>)
187 (maybe-load "minsn" #f '<macro-insn>)
188 (maybe-load "decode" #f 'decode-build-table)
189 (maybe-load "rtl" "rtl" '<rtx-func>)
190 (maybe-load "rtl-traverse" "rtl_traverse" 'rtx-traverse)
191 (maybe-load "rtl-xform" "rtx_simplify" 'rtx-simplify)
192 (maybe-load "rtx-funcs" "rtx_funcs" 'def-rtx-funcs)
193 (maybe-load "rtl-c" "rtl_c" '<c-expr>)
194 (maybe-load "semantics" #f 'semantic-compile)
195 (maybe-load "sem-frags" "sem_frags" 'gen-threaded-engine)
196 (maybe-load "utils-gen" "utils_gen" 'attr-gen-decl)
197 (maybe-load "pgmr-tools" "pgmr_tools" 'pgmr-pretty-print-insn-format)
200 ; All state regarding the reading of a .cpu file is kept in an object of
203 ; Class to record info for each top-level `command' (for lack of a better
204 ; word) in the description file.
205 ; Top level commands are things like define-*.
208 (class-make '<command>
211 ; argument spec to `lambda'
213 ; lambda that processes the entry
219 (define command-arg-spec (elm-make-getter <command> 'arg-spec))
220 (define command-handler (elm-make-getter <command> 'handler))
222 ; Return help text for COMMAND.
224 (define (command-help cmd)
228 (with-output-to-string (lambda () (write (command-arg-spec cmd))))
232 ; A pair of two lists: machs to keep, machs to drop.
233 ; The default is "keep all machs", "drop none".
235 (define -keep-all-machs '((all)))
237 ; Main reader state class.
240 (class-make '<reader>
243 ; Selected machs to keep.
244 ; A pair of two lists: the car lists the machs to keep, the cdr
245 ; lists the machs to drop. Two special entries are `all' and
246 ; `base'. Both are only valid in the keep list. `base' is a
247 ; place holder for objects that are common to all machine
248 ; variants in the architecture, it is the default value of the
249 ; MACH attribute. If `all' is present the drop list is still
251 (cons 'keep-mach -keep-all-machs)
253 ; Selected isas to keep or `all'.
256 ; Boolean indicating if command tracing is on.
257 (cons 'trace-commands? #f)
259 ; Boolean indicating if pmacro tracing is on.
260 (cons 'trace-pmacros? #f)
262 ; Currently select cpu family, computed from `keep-mach'.
263 ; Some applications don't care, and this is moderately
264 ; expensive to compute so we use delay/force.
267 ; Associative list of file entry commands
268 ; (e.g. define-insn, etc.).
269 ; Each entry is (name . command-object).
272 ; The current source location.
273 ; This is recorded here by the higher level reader and is
274 ; fetched by commands as necessary.
282 (define-getters <reader> reader
284 trace-commands? trace-pmacros?
285 current-cpu commands location))
286 (define-setters <reader> reader
288 trace-commands? trace-pmacros?
289 current-cpu commands location))
291 (define (reader-add-command! name comment attrs arg-spec handler)
292 (reader-set-commands! CURRENT-READER
294 (make <command> name comment attrs
296 (reader-commands CURRENT-READER)))
299 (define (-reader-lookup-command name)
300 (assq-ref (reader-commands CURRENT-READER) name)
303 ; Reader state for current .cpu file.
305 (define CURRENT-READER #f)
307 ; Return the current source location in readable form.
308 ; FIXME: Currently unused, keep for reference for awhile.
310 (define (-readable-current-location)
311 (let ((loc (current-reader-location)))
313 (location->string loc)
314 ;; Blech, we don't have a current reader location. That's odd.
315 ;; Fall back to the current input port's location.
316 (string-append (or (port-filename (current-input-port))
319 (number->string (port-line (current-input-port)))
323 ;;; Signal a parse error while reading a .cpu file.
324 ;;; If CONTEXT is #f, use a default context of the current reader location
325 ;;; and an empty prefix.
326 ;;; If MAYBE-HELP-TEXT is specified, elide the last trailing \n.
327 ;;; Multiple lines of help text need embedded newlines, and should be no longer
328 ;;; than 79 characters.
330 (define (parse-error context message expr . maybe-help-text)
332 (set! context (make <context> (current-reader-location) #f)))
333 (let* ((loc (or (context-location context) (unspecified-location)))
334 (top-sloc (location-top loc))
335 (prefix (context-prefix context)))
339 "While reading description:\n~A: ~A:\n ~S\nReference chain:\n~A~A"
340 (single-location->simple-string top-sloc)
342 (string-append prefix ": " message)
345 (location->string loc)
346 (if (null? maybe-help-text)
348 (string-append "\n" (car maybe-help-text))))))
351 ; Return the current source location.
353 ; If CURRENT-READER is uninitialized, return "unspecified" location.
354 ; This is done so that things like define-pmacro work in interactive mode.
356 (define (current-reader-location)
358 (reader-location CURRENT-READER)
359 (unspecified-location))
362 ; Process a macro-expanded entry.
364 (define (-reader-process-expanded-1! entry)
365 (let ((location (location-property entry)))
367 ;; Set the current source location for better diagnostics.
368 ;; Access with current-reader-location.
369 (reader-set-location! CURRENT-READER location)
371 (if (reader-trace-commands? CURRENT-READER)
372 (message "Processing command:\n @ "
373 (if location (location->string location) "location unknown")
375 (with-output-to-string (lambda () (pretty-print entry)))))
377 (let ((command (-reader-lookup-command (car entry)))
378 (context (make-current-context #f)))
382 (let* ((handler (command-handler command))
383 (arg-spec (command-arg-spec command))
384 (num-args (num-args arg-spec)))
386 ;; Variable number of trailing arguments.
387 (if (< (length (cdr entry)) (car num-args))
389 (string-append "Incorrect number of arguments to "
390 (symbol->string (car entry))
391 ", expecting at least "
392 (number->string (car num-args)))
394 (command-help command))
395 (apply handler (cdr entry)))
396 ;; Fixed number of arguments.
397 (if (!= (length (cdr entry)) (car num-args))
399 (string-append "Incorrect number of arguments to "
400 (symbol->string (car entry))
402 (number->string (car num-args)))
404 (command-help command))
405 (apply handler (cdr entry)))))
407 (parse-error context "unknown entry type" entry))))
412 ;; Process 1 or more macro-expanded entries.
413 ;; ENTRY is expected to have a location-property object property.
415 ;; NOTE: This is "public" so the .eval pmacro can use it.
416 ;; This is also used by -cmd-if.
418 (define (reader-process-expanded! entry)
419 ;; () is used to indicate a no-op
422 ;; `begin' is used to group a collection of entries into one,
423 ;; since pmacro can only return one expression (borrowed from
424 ;; Scheme of course).
425 ;; Recurse in case there are nested begins.
426 ((eq? (car entry) 'begin)
427 (for-each reader-process-expanded!
430 (-reader-process-expanded-1! entry)))
435 ; Process file entry ENTRY.
436 ; LOC is a <location> object for ENTRY.
438 (define (-reader-process! entry loc)
439 (if (not (form? entry))
440 (parse-error loc "improperly formed entry" entry))
442 ; First do macro expansion, but not if define-pmacro of course.
443 ; ??? Singling out define-pmacro this way seems a bit odd. The way to look
444 ; at it, I guess, is to think of define-pmacro as (currently) the only
445 ; "syntactic" command (it doesn't pre-evaluate its arguments).
446 (let ((expansion (if (eq? (car entry) 'define-pmacro)
447 (begin (location-property-set! entry loc) entry)
448 (if (reader-trace-pmacros? CURRENT-READER)
449 (pmacro-trace entry loc)
450 (pmacro-expand entry loc)))))
451 (reader-process-expanded! expansion))
456 ; Read in and process FILE.
458 ; It would be nice to get the line number of the beginning of the object,
459 ; but that's extra work, so for now we do the simple thing and use
460 ; port-line after we've read an entry.
462 (define (reader-read-file! file)
463 (let ((readit (lambda ()
464 (let loop ((entry (read)))
465 (if (eof-object? entry)
468 ;; ??? The location we pass here isn't ideal.
469 ;; Ideally we'd pass the start location of the
470 ;; expression, instead we currently pass the end
471 ;; location (it's easier).
472 ;; ??? Use source-properties of entry, and only if
473 ;; not present fall back on current-input-location.
474 (-reader-process! entry (current-input-location #t))
478 (with-input-from-file file readit))
483 ; Cpu data is recorded in an object of class <arch>.
484 ; This is necessary as we need to allow recording of multiple cpu descriptions
486 ; Class <arch> is defined in mach.scm.
488 ; Global containing all data of the currently selected architecture.
490 (define CURRENT-ARCH #f)
492 ; `keep-mach' processing.
494 ; Return the currently selected cpu family.
495 ; If a specific cpu family has been selected, each machine that is kept must
496 ; be in that cpu family [so there's no ambiguity in the result].
497 ; This is a moderately expensive computation so use delay/force.
499 (define (current-cpu) (force (reader-current-cpu CURRENT-READER)))
501 ; Return a boolean indicating if CPU-NAME is to be kept.
502 ; ??? Currently this is always true. Note that this doesn't necessarily apply
503 ; to machs in CPU-NAME.
505 (define (keep-cpu? cpu-name) #t)
507 ; Cover proc to set `keep-mach'.
508 ; MACH-NAME-LIST is a comma separated string of machines to keep and drop
509 ; (if prefixed with !).
511 (define (-keep-mach-set! mach-name-list)
512 (let* ((mach-name-list (string-cut mach-name-list #\,))
513 (keep (find (lambda (name) (not (char=? (string-ref name 0) #\!)))
515 (drop (map (lambda (name) (string->symbol (string-drop 1 name)))
516 (find (lambda (name) (char=? (string-ref name 0) #\!))
518 (reader-set-keep-mach! CURRENT-READER
519 (cons (map string->symbol keep)
520 (map string->symbol drop)))
522 (reader-set-current-cpu!
524 (delay (let ((selected-machs (find (lambda (mach)
525 (keep-mach? (list (obj:name mach))))
526 (current-mach-list))))
527 (if (= (length selected-machs) 0)
528 (error "no machs selected"))
529 (if (not (all-true? (map (lambda (mach)
530 (eq? (obj:name (mach-cpu mach))
531 (obj:name (mach-cpu (car selected-machs)))))
533 (error "machs from different cpu families selected"))
534 (mach-cpu (car selected-machs)))))
539 ; Validate the user-provided keep-mach list against the list of machs
540 ; specified in the .cpu file (in define-arch).
542 (define (keep-mach-validate!)
543 (let ((mach-names (cons 'all (current-arch-mach-name-list)))
544 (keep-mach (reader-keep-mach CURRENT-READER)))
545 (for-each (lambda (mach)
546 (if (not (memq mach mach-names))
547 (error "unknown mach to keep:" mach)))
549 (for-each (lambda (mach)
550 (if (not (memq mach mach-names))
551 (error "unknown mach to drop:" mach)))
557 ; Return #t if a machine in MACH-LIST, a list of symbols, is to be kept.
558 ; If any machine in MACH-LIST is to be kept, the result is #t.
559 ; If MACH-LIST is the empty list (no particular mach specified, thus the base
560 ; mach), the result is #t.
562 (define (keep-mach? mach-list)
563 (if (null? mach-list)
565 (let* ((keep-mach (reader-keep-mach CURRENT-READER))
566 (keep (cons 'base (car keep-mach)))
567 (drop (cdr keep-mach))
568 (keep? (map (lambda (m) (memq m keep)) mach-list))
569 (all? (memq 'all keep))
570 (drop? (map (lambda (m) (memq m drop)) mach-list)))
571 (any-true? (map (lambda (k d)
572 ; keep if K(ept) or ALL? and not D(ropped)
573 (->bool (and (or k all?) (not d))))
577 ; Return non-#f if the object containing ATLIST is to be kept.
578 ; OBJ is the container object or #f if there is none.
579 ; The object is kept if its attribute list specifies a `MACH' that is
580 ; kept (and not dropped) or does not have the `MACH' attribute (which means
581 ; it has the default value which means it's for use with all machines).
583 (define (keep-mach-atlist? atlist obj)
584 ; The MACH attribute is not created until the .cpu file is read in which
585 ; is too late for us [we will get called for builtin objects].
586 ; Thus we peek inside the attribute list directly.
587 ; ??? Maybe postpone creation of builtins until after define-arch?
588 (let ((machs (atlist-attr-value-no-default atlist 'MACH obj)))
591 (keep-mach? (bitset-attr->list machs))))
594 ; Return a boolean indicating if the object containing ATLIST is to be kept.
595 ; OBJ is the container object or #f if there is none.
596 ; The object is kept if both its isa and its mach are kept.
598 (define (keep-atlist? atlist obj)
599 (and (keep-mach-atlist? atlist obj)
600 (keep-isa-atlist? atlist obj))
603 ; Return a boolean indicating if multiple cpu families are being kept.
605 (define (keep-multiple?)
606 (let ((selected-machs (find (lambda (mach)
607 (keep-mach? (list (obj:name mach))))
608 (current-mach-list))))
609 (not (all-true? (map (lambda (mach)
610 (eq? (obj:name (mach-cpu mach))
611 (obj:name (mach-cpu (car selected-machs)))))
615 ; Return a boolean indicating if everything is kept.
618 (equal? (reader-keep-mach CURRENT-READER) -keep-all-machs)
621 ; Ensure all cpu families were kept, necessary for generating files that
622 ; encompass the entire architecture.
624 (define (assert-keep-all)
625 (if (not (keep-all?))
626 (error "no can do, all cpu families not selected"))
630 ; Ensure exactly one cpu family was kept, necessary for generating files that
631 ; are specific to one cpu family.
633 (define (assert-keep-one)
635 (error "no can do, multiple cpu families selected"))
639 ; `keep-isa' processing.
641 ; Cover proc to set `keep-isa'.
642 ; ISA-NAME-LIST is a comma separated string of isas to keep.
643 ; ??? We don't support the !drop notation of keep-mach processing.
644 ; Perhaps we should as otherwise there are two different styles the user
645 ; has to remember. On the other hand, !drop support is moderately complicated,
646 ; and it can be added in an upward compatible manner later.
648 (define (-keep-isa-set! isa-name-list)
649 (let ((isa-name-list (map string->symbol (string-cut isa-name-list #\,))))
650 (reader-set-keep-isa! CURRENT-READER isa-name-list)
655 ; Validate the user-provided keep-isa list against the list of isas
656 ; specified in the .cpu file (in define-arch).
658 (define (keep-isa-validate!)
659 (let ((isa-names (cons 'all (current-arch-isa-name-list)))
660 (keep-isa (reader-keep-isa CURRENT-READER)))
661 (for-each (lambda (isa)
662 (if (not (memq isa isa-names))
663 (error "unknown isa to keep:" isa)))
669 ; Return currently selected isa (there must be exactly one).
671 (define (current-isa)
672 (let ((keep-isa (reader-keep-isa CURRENT-READER)))
673 (if (equal? keep-isa '(all))
674 (let ((isas (current-isa-list)))
675 (if (= (length isas) 1)
677 (error "multiple isas selected" keep-isa)))
678 (if (= (length keep-isa) 1)
679 (current-isa-lookup (car keep-isa))
680 (error "multiple isas selected" keep-isa))))
683 ; Return #t if an isa in ISA-LIST, a list of symbols, is to be kept.
684 ; If any isa in ISA-LIST is to be kept, the result is #t.
685 ; If ISA-LIST is the empty list (no particular isa specified) use the default
688 (define (keep-isa? isa-list)
690 (set! isa-list (list (car (current-arch-isa-name-list)))))
691 (let* ((keep (reader-keep-isa CURRENT-READER))
692 (keep? (map (lambda (i)
699 ; Return #t if the object containing ATLIST is to be kept.
700 ; OBJ is the container object or #f if there is none.
701 ; The object is kept if its attribute list specifies an `ISA' that is
702 ; kept or does not have the `ISA' attribute (which means it has the default
703 ; value) and the default isa is being kept.
705 (define (keep-isa-atlist? atlist obj)
706 (let ((isas (atlist-attr-value atlist 'ISA obj)))
707 (keep-isa? (bitset-attr->list isas)))
710 ; Return non-#f if object OBJ is to be kept, according to its ISA attribute.
712 (define (keep-isa-obj? obj)
713 (keep-isa-atlist? (obj-atlist obj) obj)
716 ; Return a boolean indicating if multiple isas are being kept.
718 (define (keep-isa-multiple?)
719 (let ((keep (reader-keep-isa CURRENT-READER)))
720 (or (> (length keep) 1)
721 (and (memq 'all keep)
722 (> (length (current-arch-isa-name-list)) 1))))
725 ; Return list of isa names currently being kept.
727 (define (current-keep-isa-name-list)
728 (reader-keep-isa CURRENT-READER)
732 ;; This is akin to the "logit" support, but is for specific things that
733 ;; can be named (whereas logit support is based on a simple integer verbosity
736 ;;; Enable the specified tracing.
737 ;;; TRACE-OPTIONS is a comma-separated list of things to trace.
739 ;;; Currently supported tracing:
740 ;;; commands - trace invocation of description file commands (e.g. define-insn)
741 ;;; pmacros - trace pmacro expansion
742 ;;; all - trace everything
744 ;;; [If we later need to support disabling some tracing, one way is to
745 ;;; recognize an "-" in front of an option.]
747 (define (-set-trace-options! trace-options)
748 (let ((all (list "commands" "pmacros"))
749 (requests (string-cut trace-options #\,)))
750 (if (member "all" requests)
751 (append! requests all))
752 (for-each (lambda (item)
753 (cond ((string=? "commands" item)
754 (reader-set-trace-commands?! CURRENT-READER #t))
755 ((string=? "pmacros" item)
756 (reader-set-trace-pmacros?! CURRENT-READER #t))
757 ((string=? "all" item)
760 (cgen-usage 'unknown (string-append "-t " item)
767 ; If #f, treat reserved fields as operands and extract them with the insn.
768 ; Code can then be emitted in the extraction routines to validate them.
769 ; If #t, treat reserved fields as part of the opcode.
770 ; This complicates the decoding process as these fields have to be
774 (define option:reserved-as-opcode? #f)
776 ; Process options passed in on the command line.
777 ; OPTIONS is a space separated string of name=value values.
778 ; Each application is required to provide: option-init!, option-set!.
780 (define (set-cgen-options! options)
782 (for-each (lambda (opt)
784 #t ; ignore extraneous spaces
785 (let ((name (string->symbol (car opt)))
787 (logit 1 "Setting option `" name "' to \""
788 (apply string-append value) "\".\n")
789 (option-set! name value))))
790 (map (lambda (opt) (string-cut opt #\=))
791 (string-cut options #\space)))
794 ; Application specific object creation support.
796 ; Each entry in the .cpu file has a basic container class.
797 ; Each application adds functionality by subclassing the container
798 ; and registering with set-for-new! the proper class to create.
799 ; ??? Not sure this is the best way to handle this, but it does keep the
800 ; complexity down while not requiring as dynamic a language as I had before.
801 ; ??? Class local variables would provide a more efficient way to do this.
802 ; Assuming one wants to continue on this route.
804 (define -cpu-new-class-list nil)
806 (define (set-for-new! parent child)
807 (set! -cpu-new-class-list (acons parent child -cpu-new-class-list))
810 ; Lookup the class registered with set-for-new!
811 ; If none registered, return PARENT.
813 (define (lookup-for-new parent)
814 (let ((child (assq-ref -cpu-new-class-list parent)))
820 ; .cpu file loader support
822 ;; Initialize a new <reader> object.
823 ;; This doesn't add cgen-specific commands, leaving each element (ifield,
824 ;; hardware, etc.) to add their own.
825 ;; The "result" is stored in global CURRENT-READER.
827 (define (-init-reader!)
828 (set! CURRENT-READER (new <reader>))
830 (reader-add-command! 'define-rtl-version
831 "Specify the RTL version being used.\n"
832 nil '(major minor) -cmd-define-rtl-version)
834 (reader-add-command! 'include
836 nil '(file) -cmd-include)
837 (reader-add-command! 'if
838 "(if test then . else)\n"
839 nil '(test then . else) -cmd-if)
841 ; Rather than add cgen-internal specific stuff to pmacros.scm, we create
842 ; the pmacro commands here.
844 (reader-add-command! 'define-pmacro
846 Define a preprocessor-style macro.
848 nil '(name arg1 . arg-rest) define-pmacro)
853 ; Prepare to parse a .cpu file.
854 ; This initializes the application independent tables.
855 ; KEEP-MACH specifies what machs to keep.
856 ; KEEP-ISA specifies what isas to keep.
857 ; OPTIONS is a list of options to control code generation.
858 ; The values are application dependent.
860 (define (-init-parse-cpu! keep-mach keep-isa options)
861 (set! -cpu-new-class-list nil)
863 (set! CURRENT-ARCH (new <arch>))
864 (-keep-mach-set! keep-mach)
865 (-keep-isa-set! keep-isa)
866 (set-cgen-options! options)
868 ; The order here is important.
869 (arch-init!) ; Must be done first.
888 ; Install any builtin objects.
889 ; This is deferred until define-arch is read.
890 ; One reason is that attributes MACH and ISA don't exist until then.
892 (define (reader-install-builtin!)
893 ; The order here is important.
899 ; This is mainly for the insn attributes.
905 ; Do anything necessary for the application independent parts after parsing
907 ; The lists get cons'd in reverse order. One thing this does is change them
908 ; back to file order, it makes things easier for the human viewer.
910 (define (-finish-parse-cpu!)
911 ; The order here is generally the reverse of init-parse-cpu!.
924 (arch-finish!) ; Must be done last.
929 ; Perform a global error checking pass after the .cpu file has been read in.
931 (define (-global-error-checks)
934 ; - all hardware elements with same name must have same rank and
935 ; compatible modes (which for now means same float mode or all int modes)
939 ; .cpu file include mechanism
941 (define (-cmd-include file)
942 (logit 1 "Including file " (string-append arch-path "/" file) " ...\n")
943 (reader-read-file! (string-append arch-path "/" file))
944 (logit 2 "Resuming previous file ...\n")
947 ; Version of `if' invokable at the top level of a description file.
948 ; This is a work-in-progress. Its presence in the description file is ok,
949 ; but the implementation will need to evolve.
951 (define (-cmd-if test then . else)
952 (if (> (length else) 1)
954 "wrong number of arguments to `if'"
955 (cons 'if (cons test (cons then else)))))
957 (if (or (not (pair? test))
958 (not (memq (car test) '(keep-isa? keep-mach? application-is?))))
960 "only (if (keep-mach?|keep-isa?|application-is? ...) ...) are currently supported"
964 (if (keep-isa? (cadr test))
965 (reader-process-expanded! then)
968 (reader-process-expanded! (car else)))))
970 (if (keep-mach? (cadr test))
971 (reader-process-expanded! then)
974 (reader-process-expanded! (car else)))))
976 (if (eq? APPLICATION (cadr test))
977 (reader-process-expanded! then)
980 (reader-process-expanded! (car else))))))
983 ; Top level routine for loading .cpu files.
984 ; FILE is the name of the .cpu file to load.
985 ; KEEP-MACH is a string of comma separated machines to keep
986 ; (or not keep if prefixed with !).
987 ; KEEP-ISA is a string of comma separated isas to keep.
988 ; OPTIONS is the OPTIONS argument to -init-parse-cpu!.
989 ; TRACE-OPTIONS is a random list of things to trace.
990 ; APP-INITER! is an application specific zero argument proc (thunk)
991 ; to call after -init-parse-cpu!
992 ; APP-FINISHER! is an application specific zero argument proc to call after
994 ; ANALYZER! is a zero argument proc to call after loading the .cpu file.
995 ; It is expected to set up various tables and things useful for the application
998 ; This function isn't local because it's used by dev.scm.
1000 (define (cpu-load file keep-mach keep-isa options trace-options
1001 app-initer! app-finisher! analyzer!)
1003 (-init-parse-cpu! keep-mach keep-isa options)
1004 (-set-trace-options! trace-options)
1006 (logit 1 "Loading cpu description " file " ...\n")
1007 (set! arch-path (dirname file))
1008 (reader-read-file! file)
1009 (logit 2 "Processing cpu description " file " ...\n")
1010 (-finish-parse-cpu!)
1012 (-global-error-checks)
1017 ; Argument parsing utilities.
1019 ; Generate a usage message.
1020 ; ERRTYPE is one of 'help, 'unknown, 'missing.
1021 ; OPTION is the option that had the error or "" if ERRTYPE is 'help.
1023 (define (cgen-usage errtype option arguments)
1024 (let ((cep (current-error-port)))
1027 ((unknown) (display (string-append "Unknown option: " option "\n") cep))
1028 ((missing) (display (string-append "Missing argument: " option "\n") cep))
1029 (else (display "Unknown error!\n" cep)))
1030 (display "Usage: cgen arguments ...\n" cep)
1031 (for-each (lambda (arg)
1032 (display (string-append
1033 (let ((arg-str (string-append (car arg) " "
1034 (or (cadr arg) ""))))
1035 (if (< (string-length arg-str) 16)
1036 (string-take 16 arg-str)
1039 (apply string-append
1048 (display "...\n" cep)
1051 ((unknown missing) (quit 1))
1055 ; Poor man's getopt.
1056 ; [We don't know where to find the real one until we've parsed the args,
1057 ; and this isn't something we need to get too fancy about anyways.]
1058 ; The result is always ((a . b) . c).
1059 ; If the argument is valid, the result is ((opt-spec . arg) . remaining-argv),
1060 ; or (('unknown . option) . remaining-argv) if `option' isn't recognized,
1061 ; or (('missing . option) . remaining argv) if `option' is missing a required
1063 ; or ((#f . #f) . #f) if there are no more arguments.
1064 ; OPT-SPEC is a list of option specs.
1065 ; Each element is an alist of at least 3 elements: option argument help-text.
1066 ; `option' is a string or symbol naming the option. e.g. -a, --help, "-i".
1067 ; symbols are supported for backward compatibility, -i is a complex number.
1068 ; `argument' is a string naming the argument or #f if the option takes no
1070 ; `help-text' is a string that is printed with the usage information.
1071 ; Elements beyond `help-text' are ignored.
1073 (define (-getopt argv opt-spec)
1075 (cons (cons #f #f) #f)
1076 (let ((opt (assoc (car argv) opt-spec)))
1077 (cond ((not opt) (cons (cons 'unknown (car argv)) (cdr argv)))
1078 ((and (cadr opt) (null? (cdr argv)))
1079 (cons (cons 'missing (car argv)) (cdr argv)))
1080 ((cadr opt) (cons (cons opt (cadr argv)) (cddr argv)))
1081 (else ; must be option that doesn't take an argument
1082 (cons (cons opt #f) (cdr argv))))))
1085 ; Return (cadr args) or print a pretty error message if not possible.
1087 (define (option-arg args)
1088 (if (and (pair? args) (pair? (cdr args)))
1090 (parse-error (make-prefix-context "option processing")
1091 "missing argument to"
1095 ; List of common arguments.
1097 ; ??? Another useful arg would be one that says "do file generation with
1098 ; arguments specified up til now, then continue with next batch of args".
1100 (define common-arguments
1101 '(("-a" "arch-file" "specify path of .cpu file to load")
1102 ("-b" #f "use debugging evaluator, for backtraces")
1103 ("-d" #f "start interactive debugging session")
1104 ("-f" "flags" "specify a set of flags to control code generation")
1105 ("-h" #f "print usage information")
1106 ("--help" #f "print usage information")
1107 ("-i" "isa-list" "specify isa-list entries to keep")
1108 ("-m" "mach-list" "specify mach-list entries to keep")
1109 ("-s" "srcdir" "set srcdir")
1110 ("-t" "trace-options" "specify list of things to trace"
1112 "commands - trace cgen commands (e.g. define-insn)"
1113 "pmacros - trace pmacro expansion"
1114 "all - trace everything")
1115 ("-v" #f "increment verbosity level")
1116 ("--version" #f "print version info")
1120 ; Default place to look.
1121 ; This gets overridden to point to the directory of the loaded .cpu file.
1122 ; ??? Ideally this would be local to this file.
1124 (define arch-path (string-append srcdir "/cpu"))
1126 ; Accessors for application option specs
1128 (define (opt-get-first-pass opt)
1129 (or (list-ref opt 3) (lambda args #f)))
1130 (define (opt-get-second-pass opt)
1131 (or (list-ref opt 4) (lambda args #f)))
1133 ; Parse options and call generators.
1134 ; ARGS is a #:keyword delimited list of arguments.
1136 ; #:arg-spec optspec ; FIXME: rename to #:opt-spec
1137 ; #:init init-routine
1138 ; #:finish finish-routine
1139 ; #:analyze analysis-routine
1140 ; #:argv command-line-arguments
1142 ; ARGSPEC is a list of (option option-arg comment option-handler) elements.
1143 ; OPTION-HANDLER is either (lambda () ...) or (lambda (arg) ...) and
1144 ; processes the option.
1148 (let ((app-name "unknown")
1150 (app-init! (lambda () #f))
1151 (app-finish! (lambda () #f))
1152 (app-analyze! (lambda () #f))
1153 (argv (list "cgen"))
1155 (let loop ((args args))
1156 (if (not (null? args))
1158 ((#:app-name) (begin
1159 (set! app-name (option-arg args))
1160 (loop (cddr args))))
1161 ((#:arg-spec) (begin
1162 (set! opt-spec (option-arg args))
1163 (loop (cddr args))))
1165 (set! app-init! (option-arg args))
1166 (loop (cddr args))))
1168 (set! app-finish! (option-arg args))
1169 (loop (cddr args))))
1171 (set! app-analyze! (option-arg args))
1172 (loop (cddr args))))
1174 (set! argv (option-arg args))
1175 (loop (cddr args))))
1176 (else (error "cgen: unknown argument" (car args))))))
1178 ; ARGS has been processed, now we can process ARGV.
1181 (opt-spec (append common-arguments opt-spec))
1182 (app-args nil) ; application's args are queued here
1185 (keep-mach "all") ; default is all machs
1186 (keep-isa "all") ; default is all isas
1189 (debugging #f) ; default is off, for speed
1191 (cep (current-error-port))
1195 (let loop ((argv (cdr argv)))
1196 (let* ((new-argv (-getopt argv opt-spec))
1197 (opt (caar new-argv))
1198 (arg (cdar new-argv)))
1200 ((#f) (set! moreopts? #f))
1201 ((unknown) (cgen-usage 'unknown arg opt-spec))
1202 ((missing) (cgen-usage 'missing arg opt-spec))
1204 (cond ((str=? "-a" (car opt))
1205 (set! arch-file arg)
1207 ((str=? "-b" (car opt))
1210 ((str=? "-d" (car opt))
1211 (let ((prompt (string-append "cgen-" app-name "> ")))
1213 (set-repl-prompt! prompt)
1214 (if (feature? 'readline)
1215 (set-readline-prompt! prompt))
1217 ((str=? "-f" (car opt))
1220 ((str=? "-h" (car opt))
1221 (cgen-usage 'help "" opt-spec)
1223 ((str=? "--help" (car opt))
1224 (cgen-usage 'help "" opt-spec)
1226 ((str=? "-i" (car opt))
1229 ((str=? "-m" (car opt))
1230 (set! keep-mach arg)
1232 ((str=? "-s" (car opt))
1233 #f ; ignore, already processed by caller
1235 ((str=? "-t" (car opt))
1236 (set! trace-options arg)
1238 ((str=? "-v" (car opt))
1241 ((str=? "--version" (car opt))
1243 (display "Cpu tools GENerator version ")
1244 (display (cgen-major))
1246 (display (cgen-minor))
1248 (display (cgen-fixlevel))
1250 (display "RTL version ")
1251 (display (cgen-rtl-major))
1253 (display (cgen-rtl-minor))
1257 ; Else this is an application specific option.
1259 ; Record it for later processing. Note that they're
1260 ; recorded in reverse order (easier). This is undone
1262 (set! app-args (acons opt arg app-args)))
1264 (if moreopts? (loop (cdr new-argv)))
1268 ; All arguments have been parsed.
1270 (cgen-call-with-debugging
1275 (error "-a option missing, no architecture specified"))
1281 keep-mach keep-isa flags trace-options
1282 app-init! app-finish! app-analyze!)
1284 ;; Start another repl loop if -d.
1285 ;; Awkward. Both places are useful, though this is more useful.
1289 ;; Done with processing the arguments. Application arguments
1290 ;; are processed in two passes. This is because the app may
1291 ;; have arguments that specify things that affect file
1292 ;; generation (e.g. to specify another input file) and we
1293 ;; don't want to require an ordering of the options.
1294 (for-each (lambda (opt-arg)
1295 (let ((opt (car opt-arg))
1296 (arg (cdr opt-arg)))
1298 ((opt-get-first-pass opt) arg)
1299 ((opt-get-first-pass opt)))))
1302 (for-each (lambda (opt-arg)
1303 (let ((opt (car opt-arg))
1304 (arg (cdr opt-arg)))
1306 ((opt-get-second-pass opt) arg)
1307 ((opt-get-second-pass opt)))))
1308 (reverse app-args))))
1314 ; Main entry point called by application file generators.
1318 (cgen-debugging-stack-start -cgen args))