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 #f)
76 (define /default-rtl-version '(0 7))
77 (define (cgen-rtl-version) /CGEN-RTL-VERSION)
78 (define (cgen-rtl-major) (car /CGEN-RTL-VERSION))
79 (define (cgen-rtl-minor) (cadr /CGEN-RTL-VERSION))
81 ;; Utilities for testing the rtl version.
82 (define (rtl-version-equal? major minor)
83 (equal? (cgen-rtl-version) (list major minor))
85 (define (rtl-version-at-least? major minor)
86 (let ((rmajor (cgen-rtl-major))
87 (rminor (cgen-rtl-major)))
92 (define (rtl-version-older? major minor)
93 (not (rtl-version-at-least? major minor))
96 ;; List of supported versions
97 (define /supported-rtl-versions '((0 7) (0 8)))
99 (define (/cmd-define-rtl-version major minor)
100 (if (not (non-negative-integer? major))
101 (parse-error #f "Invalid major version number" major))
102 (if (not (non-negative-integer? minor))
103 (parse-error #f "Invalid minor version number" minor))
105 (let ((new-version (list major minor)))
106 (if (not (member new-version /supported-rtl-versions))
107 (parse-error #f "Unsupported/invalid rtl version" new-version))
108 (if (not (equal? new-version /CGEN-RTL-VERSION))
109 (logit 1 "Setting RTL version to " major "." minor " ...\n"))
110 (set! /CGEN-RTL-VERSION new-version))
113 ; Which application is in use (UNKNOWN, DESC, OPCODES, SIMULATOR, ???).
114 ; This is mostly for descriptive purposes.
115 (define APPLICATION 'UNKNOWN)
117 ;; Load the base cgen files.
121 (load "slib/logical")
123 ; Used to pretty-print debugging messages.
125 ; Used by pretty-print.
127 (load "slib/genwrite")
144 (load "rtl-traverse")
154 ; All state regarding the reading of a .cpu file is kept in an object of
157 ; Class to record info for each top-level `command' (for lack of a better
158 ; word) in the description file.
159 ; Top level commands are things like define-*.
162 (class-make '<command>
165 ; argument spec to `lambda'
167 ; lambda that processes the entry
173 (define command-arg-spec (elm-make-getter <command> 'arg-spec))
174 (define command-handler (elm-make-getter <command> 'handler))
176 ; Return help text for COMMAND.
178 (define (command-help cmd)
182 (with-output-to-string (lambda () (write (command-arg-spec cmd))))
186 ; A pair of two lists: machs to keep, machs to drop.
187 ; The default is "keep all machs", "drop none".
189 (define /keep-all-machs '((all)))
191 ; Main reader state class.
194 (class-make '<reader>
197 ; Selected machs to keep.
198 ; A pair of two lists: the car lists the machs to keep, the cdr
199 ; lists the machs to drop. Two special entries are `all' and
200 ; `base'. Both are only valid in the keep list. `base' is a
201 ; place holder for objects that are common to all machine
202 ; variants in the architecture, it is the default value of the
203 ; MACH attribute. If `all' is present the drop list is still
205 (cons 'keep-mach /keep-all-machs)
207 ; Selected isas to keep or `all'.
210 ; Boolean indicating if command tracing is on.
211 (cons 'trace-commands? #f)
213 ; Boolean indicating if pmacro tracing is on.
214 (cons 'trace-pmacros? #f)
216 ; Issue diagnostics for instruction format issues.
217 (cons 'verify-iformat? #f)
219 ; Currently select cpu family, computed from `keep-mach'.
220 ; Some applications don't care, and this is moderately
221 ; expensive to compute so we use delay/force.
224 ; Associative list of file entry commands
225 ; (e.g. define-insn, etc.).
226 ; Each entry is (name . command-object).
229 ; The current source location.
230 ; This is recorded here by the higher level reader and is
231 ; fetched by commands as necessary.
239 (define-getters <reader> reader
241 trace-commands? trace-pmacros? verify-iformat?
242 current-cpu commands location))
243 (define-setters <reader> reader
245 trace-commands? trace-pmacros? verify-iformat?
246 current-cpu commands location))
248 (define (reader-add-command! name comment attrs arg-spec handler)
249 (reader-set-commands! CURRENT-READER
251 (make <command> name comment attrs
253 (reader-commands CURRENT-READER)))
256 (define (/reader-lookup-command name)
257 (assq-ref (reader-commands CURRENT-READER) name)
260 ; Reader state for current .cpu file.
262 (define CURRENT-READER #f)
264 ; Return the current source location in readable form.
265 ; FIXME: Currently unused, keep for reference for awhile.
267 (define (/readable-current-location)
268 (let ((loc (current-reader-location)))
270 (location->string loc)
271 ;; Blech, we don't have a current reader location. That's odd.
272 ;; Fall back to the current input port's location.
273 (string-append (or (port-filename (current-input-port))
276 (number->string (port-line (current-input-port)))
280 ;; Subroutine of parse-error, parse-warning to simplify them.
281 ;; Flag an error or a warning.
282 ;; EMITTER is a function of one argument, the message to print.
284 (define (/parse-diagnostic emitter context message expr maybe-help-text)
286 (set! context (make <context> (current-reader-location) #f)))
288 (let* ((loc (or (context-location context) (unspecified-location)))
289 (top-sloc (location-top loc))
290 (intro "While reading description")
291 (prefix (or (context-prefix context) "Error"))
292 (text (string-append prefix ": " message)))
297 "\n~A:\n@ ~A:\n\n~A: ~A: ~S~A"
299 (location->string loc)
300 (single-location->simple-string top-sloc)
304 (string-append "\n\n" maybe-help-text)
308 ;; Signal a parse error while reading a .cpu file.
309 ;; Processing stops immediately.
310 ;; If CONTEXT is #f, use a default context of the current reader location
311 ;; and an empty prefix.
312 ;; If MAYBE-HELP-TEXT is specified, elide the last trailing \n.
313 ;; Multiple lines of help text need embedded newlines, and should be no longer
314 ;; than 79 characters.
316 (define (parse-error context errmsg expr . maybe-help-text)
317 (/parse-diagnostic error
321 (if (null? maybe-help-text) "" (car maybe-help-text)))
324 ;; Same as parse-error, but continue processing.
326 (define (parse-error-continuable context errmsg expr . maybe-help-text)
327 (set! /continuable-error-found? #t)
328 (/parse-diagnostic (lambda (text) (message "Error: " text "\n"))
332 (if (null? maybe-help-text) #f (car maybe-help-text)))
335 ;; Signal a parse warning while reading a .cpu file.
336 ;; If CONTEXT is #f, use a default context of the current reader location
337 ;; and an empty prefix.
338 ;; If MAYBE-HELP-TEXT is specified, elide the last trailing \n.
339 ;; Multiple lines of help text need embedded newlines, and should be no longer
340 ;; than 79 characters.
342 (define (parse-warning context errmsg expr . maybe-help-text)
343 (/parse-diagnostic (lambda (text) (message "Warning: " text "\n"))
347 (if (null? maybe-help-text) #f (car maybe-help-text)))
350 ; Return the current source location.
352 ; If CURRENT-READER is uninitialized, return "unspecified" location.
353 ; This is done so that things like define-pmacro work in interactive mode.
355 (define (current-reader-location)
357 (reader-location CURRENT-READER)
358 (unspecified-location))
361 ; Process a macro-expanded entry.
363 (define (/reader-process-expanded-1! entry)
364 (let ((location (location-property entry)))
366 ;; Set the current source location for better diagnostics.
367 ;; Access with current-reader-location.
368 (reader-set-location! CURRENT-READER location)
370 (if (reader-trace-commands? CURRENT-READER)
371 (message "Processing command:\n @ "
372 (if location (location->string location) "location unknown")
374 (with-output-to-string (lambda () (pretty-print entry)))))
376 (let ((command (/reader-lookup-command (car entry)))
377 (context (make-current-context #f)))
381 (let* ((handler (command-handler command))
382 (arg-spec (command-arg-spec command))
383 (num-args (num-args arg-spec)))
385 ;; Variable number of trailing arguments.
386 (if (< (length (cdr entry)) (car num-args))
388 (string-append "Incorrect number of arguments to "
389 (symbol->string (car entry))
390 ", expecting at least "
391 (number->string (car num-args)))
393 (command-help command))
394 (apply handler (cdr entry)))
395 ;; Fixed number of arguments.
396 (if (!= (length (cdr entry)) (car num-args))
398 (string-append "Incorrect number of arguments to "
399 (symbol->string (car entry))
401 (number->string (car num-args)))
403 (command-help command))
404 (apply handler (cdr entry)))))
406 (parse-error context "unknown entry type" entry))))
411 ;; Process 1 or more macro-expanded entries.
412 ;; ENTRY is expected to have a location-property object property.
414 ;; NOTE: This is "public" so the .eval pmacro can use it.
415 ;; This is also used by /cmd-if.
417 (define (reader-process-expanded! entry)
418 ;; () is used to indicate a no-op
421 ;; `begin' is used to group a collection of entries into one,
422 ;; since pmacro can only return one expression (borrowed from
423 ;; Scheme of course).
424 ;; Recurse in case there are nested begins.
425 ((eq? (car entry) 'begin)
426 (for-each reader-process-expanded!
429 (/reader-process-expanded-1! entry)))
434 ; Process file entry ENTRY.
435 ; LOC is a <location> object for ENTRY.
437 (define (/reader-process! entry loc)
438 (if (not (form? entry))
439 (parse-error loc "improperly formed entry" entry))
441 ; First do macro expansion, but not if define-pmacro of course.
442 ; ??? Singling out define-pmacro this way seems a bit odd. The way to look
443 ; at it, I guess, is to think of define-pmacro as (currently) the only
444 ; "syntactic" command (it doesn't pre-evaluate its arguments).
445 (let ((expansion (if (eq? (car entry) 'define-pmacro)
446 (begin (location-property-set! entry loc) entry)
447 (if (reader-trace-pmacros? CURRENT-READER)
448 (pmacro-trace entry loc)
449 (pmacro-expand entry loc)))))
450 (reader-process-expanded! expansion))
455 ; Read in and process FILE.
457 ; It would be nice to get the line number of the beginning of the object,
458 ; but that's extra work, so for now we do the simple thing and use
459 ; port-line after we've read an entry.
461 (define (reader-read-file! file)
462 (let ((readit (lambda ()
463 (let loop ((entry (read)))
464 (if (eof-object? entry)
467 ;; ??? The location we pass here isn't ideal.
468 ;; Ideally we'd pass the start location of the
469 ;; expression, instead we currently pass the end
470 ;; location (it's easier).
471 ;; ??? Use source-properties of entry, and only if
472 ;; not present fall back on current-input-location.
473 (/reader-process! entry (current-input-location #t))
477 (with-input-from-file file readit))
482 ; Cpu data is recorded in an object of class <arch>.
483 ; This is necessary as we need to allow recording of multiple cpu descriptions
485 ; Class <arch> is defined in mach.scm.
487 ; Global containing all data of the currently selected architecture.
489 (define CURRENT-ARCH #f)
491 ; `keep-mach' processing.
493 ; Return the currently selected cpu family.
494 ; If a specific cpu family has been selected, each machine that is kept must
495 ; be in that cpu family [so there's no ambiguity in the result].
496 ; This is a moderately expensive computation so use delay/force.
498 (define (current-cpu) (force (reader-current-cpu CURRENT-READER)))
500 ; Return a boolean indicating if CPU-NAME is to be kept.
501 ; ??? Currently this is always true. Note that this doesn't necessarily apply
502 ; to machs in CPU-NAME.
504 (define (keep-cpu? cpu-name) #t)
506 ; Cover proc to set `keep-mach'.
507 ; MACH-NAME-LIST is a comma separated string of machines to keep and drop
508 ; (if prefixed with !).
510 (define (/keep-mach-set! mach-name-list)
511 (let* ((mach-name-list (string-cut mach-name-list #\,))
512 (keep (find (lambda (name) (not (char=? (string-ref name 0) #\!)))
514 (drop (map (lambda (name) (string->symbol (string-drop 1 name)))
515 (find (lambda (name) (char=? (string-ref name 0) #\!))
517 (reader-set-keep-mach! CURRENT-READER
518 (cons (map string->symbol keep)
519 (map string->symbol drop)))
521 (reader-set-current-cpu!
523 (delay (let ((selected-machs (find (lambda (mach)
524 (keep-mach? (list (obj:name mach))))
525 (current-mach-list))))
526 (if (= (length selected-machs) 0)
527 (error "no machs selected"))
528 (if (not (all-true? (map (lambda (mach)
529 (eq? (obj:name (mach-cpu mach))
530 (obj:name (mach-cpu (car selected-machs)))))
532 (error "machs from different cpu families selected"))
533 (mach-cpu (car selected-machs)))))
538 ; Validate the user-provided keep-mach list against the list of machs
539 ; specified in the .cpu file (in define-arch).
541 (define (keep-mach-validate!)
542 (let ((mach-names (cons 'all (current-arch-mach-name-list)))
543 (keep-mach (reader-keep-mach CURRENT-READER)))
544 (for-each (lambda (mach)
545 (if (not (memq mach mach-names))
546 (error "unknown mach to keep:" mach)))
548 (for-each (lambda (mach)
549 (if (not (memq mach mach-names))
550 (error "unknown mach to drop:" mach)))
556 ; Return #t if a machine in MACH-LIST, a list of symbols, is to be kept.
557 ; If any machine in MACH-LIST is to be kept, the result is #t.
558 ; If MACH-LIST is the empty list (no particular mach specified, thus the base
559 ; mach), the result is #t.
561 (define (keep-mach? mach-list)
562 (if (null? mach-list)
564 (let* ((keep-mach (reader-keep-mach CURRENT-READER))
565 (keep (cons 'base (car keep-mach)))
566 (drop (cdr keep-mach))
567 (keep? (map (lambda (m) (memq m keep)) mach-list))
568 (all? (memq 'all keep))
569 (drop? (map (lambda (m) (memq m drop)) mach-list)))
570 (any-true? (map (lambda (k d)
571 ; keep if K(ept) or ALL? and not D(ropped)
572 (->bool (and (or k all?) (not d))))
576 ; Return non-#f if the object containing ATLIST is to be kept.
577 ; OBJ is the container object or #f if there is none.
578 ; The object is kept if its attribute list specifies a `MACH' that is
579 ; kept (and not dropped) or does not have the `MACH' attribute (which means
580 ; it has the default value which means it's for use with all machines).
582 (define (keep-mach-atlist? atlist obj)
583 ; The MACH attribute is not created until the .cpu file is read in which
584 ; is too late for us [we will get called for builtin objects].
585 ; Thus we peek inside the attribute list directly.
586 ; ??? Maybe postpone creation of builtins until after define-arch?
587 (let ((machs (atlist-attr-value-no-default atlist 'MACH obj)))
593 ; Return a boolean indicating if the object containing ATLIST is to be kept.
594 ; OBJ is the container object or #f if there is none.
595 ; The object is kept if both its isa and its mach are kept.
597 (define (keep-atlist? atlist obj)
598 (and (keep-mach-atlist? atlist obj)
599 (keep-isa-atlist? atlist obj))
602 ; Return a boolean indicating if multiple cpu families are being kept.
604 (define (keep-multiple?)
605 (let ((selected-machs (find (lambda (mach)
606 (keep-mach? (list (obj:name mach))))
607 (current-mach-list))))
608 (not (all-true? (map (lambda (mach)
609 (eq? (obj:name (mach-cpu mach))
610 (obj:name (mach-cpu (car selected-machs)))))
614 ; Return a boolean indicating if everything is kept.
617 (equal? (reader-keep-mach CURRENT-READER) /keep-all-machs)
620 ; Ensure all cpu families were kept, necessary for generating files that
621 ; encompass the entire architecture.
623 (define (assert-keep-all)
624 (if (not (keep-all?))
625 (error "no can do, all cpu families not selected"))
629 ; Ensure exactly one cpu family was kept, necessary for generating files that
630 ; are specific to one cpu family.
632 (define (assert-keep-one)
634 (error "no can do, multiple cpu families selected"))
638 ; `keep-isa' processing.
640 ; Cover proc to set `keep-isa'.
641 ; ISA-NAME-LIST is a comma separated string of isas to keep.
642 ; ??? We don't support the !drop notation of keep-mach processing.
643 ; Perhaps we should as otherwise there are two different styles the user
644 ; has to remember. On the other hand, !drop support is moderately complicated,
645 ; and it can be added in an upward compatible manner later.
647 (define (/keep-isa-set! isa-name-list)
648 (let ((isa-name-list (map string->symbol (string-cut isa-name-list #\,))))
649 (reader-set-keep-isa! CURRENT-READER isa-name-list)
654 ; Validate the user-provided keep-isa list against the list of isas
655 ; specified in the .cpu file (in define-arch).
657 (define (keep-isa-validate!)
658 (let ((isa-names (cons 'all (current-arch-isa-name-list)))
659 (keep-isa (reader-keep-isa CURRENT-READER)))
660 (for-each (lambda (isa)
661 (if (not (memq isa isa-names))
662 (error "unknown isa to keep:" isa)))
668 ; Return currently selected isa (there must be exactly one).
670 (define (current-isa)
671 (let ((keep-isa (reader-keep-isa CURRENT-READER)))
672 (if (equal? keep-isa '(all))
673 (let ((isas (current-isa-list)))
674 (if (= (length isas) 1)
676 (error "multiple isas selected" keep-isa)))
677 (if (= (length keep-isa) 1)
678 (current-isa-lookup (car keep-isa))
679 (error "multiple isas selected" keep-isa))))
682 ; Return #t if an isa in ISA-LIST, a list of symbols, is to be kept.
683 ; If any isa in ISA-LIST is to be kept, the result is #t.
684 ; If ISA-LIST is the empty list (no particular isa specified) use the default
687 (define (keep-isa? isa-list)
688 ;; If unspecified, the default is the first one in the list.
690 (set! isa-list (list (car (current-arch-isa-name-list)))))
692 (let* ((keep (reader-keep-isa CURRENT-READER))
693 (keep? (map (lambda (i)
700 ; Return #t if the object containing ATLIST is to be kept.
701 ; OBJ is the container object or #f if there is none.
702 ; The object is kept if its attribute list specifies an `ISA' that is
703 ; kept or does not have the `ISA' attribute (which means it has the default
704 ; value) and the default isa is being kept.
706 (define (keep-isa-atlist? atlist obj)
707 (let ((isas (atlist-attr-value atlist 'ISA obj)))
711 ; Return non-#f if object OBJ is to be kept, according to its ISA attribute.
713 (define (keep-isa-obj? obj)
714 (keep-isa-atlist? (obj-atlist obj) obj)
717 ; Return a boolean indicating if multiple isas are being kept.
719 (define (keep-isa-multiple?)
720 (let ((keep (reader-keep-isa CURRENT-READER)))
721 (or (> (length keep) 1)
722 (and (memq 'all keep)
723 (> (length (current-arch-isa-name-list)) 1))))
726 ; Return list of isa names currently being kept.
728 (define (current-keep-isa-name-list)
729 (reader-keep-isa CURRENT-READER)
733 ;; This is akin to the "logit" support, but is for specific things that
734 ;; can be named (whereas logit support is based on a simple integer verbosity
737 ;;; Enable the specified tracing.
738 ;;; TRACE-OPTIONS is a comma-separated list of things to trace.
740 ;;; Currently supported tracing:
741 ;;; commands - trace invocation of description file commands (e.g. define-insn)
742 ;;; pmacros - trace pmacro expansion
743 ;;; all - trace everything
745 ;;; [If we later need to support disabling some tracing, one way is to
746 ;;; recognize an "-" in front of an option.]
748 (define (/set-trace-options! trace-options)
749 (let ((all (list "commands" "pmacros"))
750 (requests (string-cut trace-options #\,)))
751 (if (member "all" requests)
752 (append! requests all))
753 (for-each (lambda (item)
754 (cond ((string=? "commands" item)
755 (reader-set-trace-commands?! CURRENT-READER #t))
756 ((string=? "pmacros" item)
757 (reader-set-trace-pmacros?! CURRENT-READER #t))
758 ((string=? "all" item)
761 (cgen-usage 'unknown (string-append "-t " item)
768 ;; Diagnostic support.
770 ;;; Enable the specified diagnostics.
771 ;;; DIAGNOSTIC-OPTIONS is a comma-separated list of things to trace.
773 ;;; Currently supported diagnostics:
774 ;;; iformat - issue diagnostics for iformat issues
775 ;;; all - turn on all diagnostics
777 ;;; [If we later need to support disabling some diagnostic, one way is to
778 ;;; recognize an "-" in front of an option.]
780 (define (/set-diagnostic-options! diagnostic-options)
781 (let ((all (list "iformat"))
782 (requests (string-cut diagnostic-options #\,)))
783 (if (member "all" requests)
784 (append! requests all))
785 (for-each (lambda (item)
786 (cond ((string=? "iformat" item)
787 (reader-set-verify-iformat?! CURRENT-READER #t))
788 ((string=? "all" item)
791 (cgen-usage 'unknown (string-append "-w " item)
798 ; If #f, treat reserved fields as operands and extract them with the insn.
799 ; Code can then be emitted in the extraction routines to validate them.
800 ; If #t, treat reserved fields as part of the opcode.
801 ; This complicates the decoding process as these fields have to be
805 (define option:reserved-as-opcode? #f)
807 ; Process options passed in on the command line.
808 ; OPTIONS is a space separated string of name=value values.
809 ; Each application is required to provide: option-init!, option-set!.
811 (define (set-cgen-options! options)
813 (for-each (lambda (opt)
815 #t ; ignore extraneous spaces
816 (let ((name (string->symbol (car opt)))
818 (logit 1 "Setting option `" name "' to \""
819 (apply string-append value) "\".\n")
820 (option-set! name value))))
821 (map (lambda (opt) (string-cut opt #\=))
822 (string-cut options #\space)))
825 ; Application specific object creation support.
827 ; Each entry in the .cpu file has a basic container class.
828 ; Each application adds functionality by subclassing the container
829 ; and registering with set-for-new! the proper class to create.
830 ; ??? Not sure this is the best way to handle this, but it does keep the
831 ; complexity down while not requiring as dynamic a language as I had before.
832 ; ??? Class local variables would provide a more efficient way to do this.
833 ; Assuming one wants to continue on this route.
835 (define /cpu-new-class-list nil)
837 (define (set-for-new! parent child)
838 (set! /cpu-new-class-list (acons parent child /cpu-new-class-list))
841 ; Lookup the class registered with set-for-new!
842 ; If none registered, return PARENT.
844 (define (lookup-for-new parent)
845 (let ((child (assq-ref /cpu-new-class-list parent)))
851 ; .cpu file loader support
853 ;; #t if an error was found (but processing continued)
854 (define /continuable-error-found? #f)
856 ;; Initialize a new <reader> object.
857 ;; This doesn't add cgen-specific commands, leaving each element (ifield,
858 ;; hardware, etc.) to add their own.
859 ;; The "result" is stored in global CURRENT-READER.
861 (define (/init-reader!)
862 (set! CURRENT-READER (new <reader>))
864 (set! /CGEN-RTL-VERSION /default-rtl-version)
866 (set! /continuable-error-found? #f)
868 (reader-add-command! 'define-rtl-version
869 "Specify the RTL version being used.\n"
870 nil '(major minor) /cmd-define-rtl-version)
872 (reader-add-command! 'include
874 nil '(file) /cmd-include)
875 (reader-add-command! 'if
876 "(if test then . else)\n"
877 nil '(test then . else) /cmd-if)
879 ; Rather than add cgen-internal specific stuff to pmacros.scm, we create
880 ; the pmacro commands here.
882 (reader-add-command! 'define-pmacro
884 Define a preprocessor-style macro.
886 nil '(name arg1 . arg-rest) define-pmacro)
891 ;; Called at the end of .cpu file loading.
893 (define (/finish-reader! file)
894 (if /continuable-error-found?
895 (error (string-append "Error loading " file)))
899 ; Prepare to parse a .cpu file.
900 ; This initializes the application independent tables.
901 ; KEEP-MACH specifies what machs to keep.
902 ; KEEP-ISA specifies what isas to keep.
903 ; OPTIONS is a list of options to control code generation.
904 ; The values are application dependent.
906 (define (/init-parse-cpu! keep-mach keep-isa options)
907 (set! /cpu-new-class-list nil)
909 (set! CURRENT-ARCH (new <arch>))
910 (/keep-mach-set! keep-mach)
911 (/keep-isa-set! keep-isa)
912 (set-cgen-options! options)
914 ; The order here is important.
915 (arch-init!) ; Must be done first.
934 ; Install any builtin objects.
935 ; This is deferred until define-arch is read.
936 ; One reason is that attributes MACH and ISA don't exist until then.
938 (define (reader-install-builtin!)
939 ; The order here is important.
945 ; This is mainly for the insn attributes.
951 ; Do anything necessary for the application independent parts after parsing
953 ; The lists get cons'd in reverse order. One thing this does is change them
954 ; back to file order, it makes things easier for the human viewer.
956 (define (/finish-parse-cpu!)
957 ; The order here is generally the reverse of init-parse-cpu!.
970 (arch-finish!) ; Must be done last.
975 ; Perform a global error checking pass after the .cpu file has been read in.
977 (define (/global-error-checks)
980 ; - all hardware elements with same name must have same rank and
981 ; compatible modes (which for now means same float mode or all int modes)
985 ; .cpu file include mechanism
987 (define (/cmd-include file)
988 (logit 1 "Including file " (string-append arch-path "/" file) " ...\n")
989 (reader-read-file! (string-append arch-path "/" file))
990 (logit 2 "Resuming previous file ...\n")
993 ; Version of `if' invokable at the top level of a description file.
994 ; This is a work-in-progress. Its presence in the description file is ok,
995 ; but the implementation will need to evolve.
997 (define (/cmd-if test then . else)
998 (if (> (length else) 1)
1000 "wrong number of arguments to `if'"
1001 (cons 'if (cons test (cons then else)))))
1003 (if (or (not (pair? test))
1004 (not (memq (car test) '(keep-isa? keep-mach? application-is?))))
1006 "only (if (keep-mach?|keep-isa?|application-is? ...) ...) are currently supported"
1010 (if (keep-isa? (cadr test))
1011 (reader-process-expanded! then)
1014 (reader-process-expanded! (car else)))))
1016 (if (keep-mach? (cadr test))
1017 (reader-process-expanded! then)
1020 (reader-process-expanded! (car else)))))
1022 (if (eq? APPLICATION (cadr test))
1023 (reader-process-expanded! then)
1026 (reader-process-expanded! (car else))))))
1029 ; Top level routine for loading .cpu files.
1030 ; FILE is the name of the .cpu file to load.
1031 ; KEEP-MACH is a string of comma separated machines to keep
1032 ; (or not keep if prefixed with !).
1033 ; KEEP-ISA is a string of comma separated isas to keep.
1034 ; OPTIONS is the OPTIONS argument to -init-parse-cpu!.
1035 ; TRACE-OPTIONS is a random list of things to trace.
1036 ; DIAGNOSTIC-OPTIONS is a random list of things to warn/error about.
1037 ; APP-INITER! is an application specific zero argument proc (thunk)
1038 ; to call after -init-parse-cpu!
1039 ; APP-FINISHER! is an application specific zero argument proc to call after
1040 ; -finish-parse-cpu!
1041 ; ANALYZER! is a zero argument proc to call after loading the .cpu file.
1042 ; It is expected to set up various tables and things useful for the application
1045 ; This function isn't local because it's used by dev.scm.
1047 (define (cpu-load file keep-mach keep-isa options
1048 trace-options diagnostic-options
1049 app-initer! app-finisher! analyzer!)
1051 (/init-parse-cpu! keep-mach keep-isa options)
1052 (/set-trace-options! trace-options)
1053 (/set-diagnostic-options! diagnostic-options)
1055 (logit 1 "Loading cpu description " file " ...\n")
1056 (logit 1 "machs: " keep-mach "\n")
1057 (logit 1 "isas: " keep-isa "\n")
1058 (logit 1 "options: " options "\n")
1059 (logit 1 "trace: " trace-options "\n")
1060 (logit 1 "diags: " diagnostic-options "\n")
1061 (set! arch-path (dirname file))
1062 (reader-read-file! file)
1063 (/finish-parse-cpu!)
1064 (/finish-reader! file)
1065 (logit 1 "Processing cpu description " file " ...\n")
1067 (/global-error-checks)
1072 ; Argument parsing utilities.
1074 ; Generate a usage message.
1075 ; ERRTYPE is one of 'help, 'unknown, 'missing.
1076 ; OPTION is the option that had the error or "" if ERRTYPE is 'help.
1078 (define (cgen-usage errtype option arguments)
1079 (let ((cep (current-error-port)))
1082 ((unknown) (display (string-append "Unknown option: " option "\n") cep))
1083 ((missing) (display (string-append "Missing argument: " option "\n") cep))
1084 (else (display "Unknown error!\n" cep)))
1085 (display "Usage: cgen arguments ...\n" cep)
1086 (for-each (lambda (arg)
1087 (display (string-append
1088 (let ((arg-str (string-append (car arg) " "
1089 (or (cadr arg) ""))))
1090 (if (< (string-length arg-str) 16)
1091 (string-take 16 arg-str)
1094 (apply string-append
1103 (display "...\n" cep)
1106 ((unknown missing) (quit 1))
1110 ; Poor man's getopt.
1111 ; [We don't know where to find the real one until we've parsed the args,
1112 ; and this isn't something we need to get too fancy about anyways.]
1113 ; The result is always ((a . b) . c).
1114 ; If the argument is valid, the result is ((opt-spec . arg) . remaining-argv),
1115 ; or (('unknown . option) . remaining-argv) if `option' isn't recognized,
1116 ; or (('missing . option) . remaining argv) if `option' is missing a required
1118 ; or ((#f . #f) . #f) if there are no more arguments.
1119 ; OPT-SPEC is a list of option specs.
1120 ; Each element is an alist of at least 3 elements: option argument help-text.
1121 ; `option' is a string or symbol naming the option. e.g. -a, --help, "-i".
1122 ; symbols are supported for backward compatibility, -i is a complex number.
1123 ; `argument' is a string naming the argument or #f if the option takes no
1125 ; `help-text' is a string that is printed with the usage information.
1126 ; Elements beyond `help-text' are ignored.
1128 (define (/getopt argv opt-spec)
1130 (cons (cons #f #f) #f)
1131 (let ((opt (assoc (car argv) opt-spec)))
1132 (cond ((not opt) (cons (cons 'unknown (car argv)) (cdr argv)))
1133 ((and (cadr opt) (null? (cdr argv)))
1134 (cons (cons 'missing (car argv)) (cdr argv)))
1135 ((cadr opt) (cons (cons opt (cadr argv)) (cddr argv)))
1136 (else ; must be option that doesn't take an argument
1137 (cons (cons opt #f) (cdr argv))))))
1140 ; Return (cadr args) or print a pretty error message if not possible.
1142 (define (option-arg args)
1143 (if (and (pair? args) (pair? (cdr args)))
1145 (parse-error (make-prefix-context "option processing")
1146 "missing argument to"
1150 ; List of common arguments.
1152 ; ??? Another useful arg would be one that says "do file generation with
1153 ; arguments specified up til now, then continue with next batch of args".
1155 (define common-arguments
1156 '(("-a" "arch-file" "specify path of .cpu file to load")
1157 ("-b" #f "use debugging evaluator, for backtraces")
1158 ("-d" #f "start interactive debugging session")
1159 ("-f" "flags" "specify a set of flags to control code generation")
1160 ("-h" #f "print usage information")
1161 ("--help" #f "print usage information")
1162 ("-i" "isa-list" "specify isa-list entries to keep")
1163 ("-m" "mach-list" "specify mach-list entries to keep")
1164 ("-s" "srcdir" "set srcdir")
1165 ("-t" "trace-options" "specify list of things to trace"
1167 "commands - trace cgen commands (e.g. define-insn)"
1168 "pmacros - trace pmacro expansion"
1169 "all - trace everything")
1170 ("-v" #f "increment verbosity level")
1171 ("-w" "diagnostic-options" "specify list of things to issue diagnostics about"
1173 "iformat - verify instruction formats are valid"
1174 "all - turn on all diagnostics")
1176 ("--version" #f "print version info")
1180 ; Default place to look.
1181 ; This gets overridden to point to the directory of the loaded .cpu file.
1182 ; ??? Ideally this would be local to this file.
1184 (define arch-path (string-append srcdir "/cpu"))
1186 ; Accessors for application option specs
1188 (define (opt-get-first-pass opt)
1189 (or (list-ref opt 3) (lambda args #f)))
1190 (define (opt-get-second-pass opt)
1191 (or (list-ref opt 4) (lambda args #f)))
1193 ; Parse options and call generators.
1194 ; ARGS is a #:keyword delimited list of arguments.
1196 ; #:arg-spec optspec ; FIXME: rename to #:opt-spec
1197 ; #:init init-routine
1198 ; #:finish finish-routine
1199 ; #:analyze analysis-routine
1200 ; #:argv command-line-arguments
1202 ; ARGSPEC is a list of (option option-arg comment option-handler) elements.
1203 ; OPTION-HANDLER is either (lambda () ...) or (lambda (arg) ...) and
1204 ; processes the option.
1208 (let ((app-name "unknown")
1210 (app-init! (lambda () #f))
1211 (app-finish! (lambda () #f))
1212 (app-analyze! (lambda () #f))
1213 (argv (list "cgen"))
1215 (let loop ((args args))
1216 (if (not (null? args))
1218 ((#:app-name) (begin
1219 (set! app-name (option-arg args))
1220 (loop (cddr args))))
1221 ((#:arg-spec) (begin
1222 (set! opt-spec (option-arg args))
1223 (loop (cddr args))))
1225 (set! app-init! (option-arg args))
1226 (loop (cddr args))))
1228 (set! app-finish! (option-arg args))
1229 (loop (cddr args))))
1231 (set! app-analyze! (option-arg args))
1232 (loop (cddr args))))
1234 (set! argv (option-arg args))
1235 (loop (cddr args))))
1236 (else (error "cgen: unknown argument" (car args))))))
1238 ; ARGS has been processed, now we can process ARGV.
1241 (opt-spec (append common-arguments opt-spec))
1242 (app-args nil) ; application's args are queued here
1245 (keep-mach "all") ; default is all machs
1246 (keep-isa "all") ; default is all isas
1249 (debugging #f) ; default is off, for speed
1251 (diagnostic-options "")
1252 (cep (current-error-port))
1256 (let loop ((argv (cdr argv)))
1257 (let* ((new-argv (/getopt argv opt-spec))
1258 (opt (caar new-argv))
1259 (arg (cdar new-argv)))
1261 ((#f) (set! moreopts? #f))
1262 ((unknown) (cgen-usage 'unknown arg opt-spec))
1263 ((missing) (cgen-usage 'missing arg opt-spec))
1265 (cond ((str=? "-a" (car opt))
1266 (set! arch-file arg)
1268 ((str=? "-b" (car opt))
1271 ((str=? "-d" (car opt))
1272 (let ((prompt (string-append "cgen-" app-name "> ")))
1274 (set-repl-prompt! prompt)
1275 (if (feature? 'readline)
1276 (set-readline-prompt! prompt))
1278 ((str=? "-f" (car opt))
1281 ((str=? "-h" (car opt))
1282 (cgen-usage 'help "" opt-spec)
1284 ((str=? "--help" (car opt))
1285 (cgen-usage 'help "" opt-spec)
1287 ((str=? "-i" (car opt))
1290 ((str=? "-m" (car opt))
1291 (set! keep-mach arg)
1293 ((str=? "-s" (car opt))
1294 #f ; ignore, already processed by caller
1296 ((str=? "-t" (car opt))
1297 (set! trace-options arg)
1299 ((str=? "-v" (car opt))
1302 ((str=? "-w" (car opt))
1303 (set! diagnostic-options arg)
1305 ((str=? "--version" (car opt))
1307 (display "Cpu tools GENerator version ")
1308 (display (cgen-major))
1310 (display (cgen-minor))
1312 (display (cgen-fixlevel))
1314 (display "RTL version ")
1315 (display (cgen-rtl-major))
1317 (display (cgen-rtl-minor))
1321 ; Else this is an application specific option.
1323 ; Record it for later processing. Note that they're
1324 ; recorded in reverse order (easier). This is undone
1326 (set! app-args (acons opt arg app-args)))
1328 (if moreopts? (loop (cdr new-argv)))
1332 ; All arguments have been parsed.
1334 (cgen-call-with-debugging
1339 (error "-a option missing, no architecture specified"))
1345 keep-mach keep-isa flags
1346 trace-options diagnostic-options
1347 app-init! app-finish! app-analyze!)
1349 ;; Start another repl loop if -d.
1350 ;; Awkward. Both places are useful, though this is more useful.
1354 ;; Done with processing the arguments. Application arguments
1355 ;; are processed in two passes. This is because the app may
1356 ;; have arguments that specify things that affect file
1357 ;; generation (e.g. to specify another input file) and we
1358 ;; don't want to require an ordering of the options.
1359 (for-each (lambda (opt-arg)
1360 (let ((opt (car opt-arg))
1361 (arg (cdr opt-arg)))
1363 ((opt-get-first-pass opt) arg)
1364 ((opt-get-first-pass opt)))))
1367 (for-each (lambda (opt-arg)
1368 (let ((opt (car opt-arg))
1369 (arg (cdr opt-arg)))
1371 ((opt-get-second-pass opt) arg)
1372 ((opt-get-second-pass opt)))))
1373 (reverse app-args))))
1379 ; Main entry point called by application file generators.
1383 (cgen-debugging-stack-start /cgen args))