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-version) /CGEN-RTL-VERSION)
77 (define (cgen-rtl-major) (car /CGEN-RTL-VERSION))
78 (define (cgen-rtl-minor) (cadr /CGEN-RTL-VERSION))
80 ;; Utilities for testing the rtl version.
81 (define (rtl-version-equal? major minor)
82 (equal? (cgen-rtl-version) (list major minor))
84 (define (rtl-version-at-least? major minor)
85 (let ((rmajor (cgen-rtl-major))
86 (rminor (cgen-rtl-major)))
91 (define (rtl-version-older? major minor)
92 (not (rtl-version-at-least? major minor))
95 ;; List of supported versions
96 (define /supported-rtl-versions '((0 7) (0 8)))
98 (define (/cmd-define-rtl-version major minor)
99 (if (not (non-negative-integer? major))
100 (parse-error #f "Invalid major version number" major))
101 (if (not (non-negative-integer? minor))
102 (parse-error #f "Invalid minor version number" minor))
104 (let ((new-version (list major minor)))
105 (if (not (member new-version /supported-rtl-versions))
106 (parse-error #f "Unsupported/invalid rtl version" new-version))
107 (logit 1 "Setting RTL version to " major "." minor " ...\n")
108 (set! /CGEN-RTL-VERSION new-version))
111 ; Which application is in use (UNKNOWN, DESC, OPCODES, SIMULATOR, ???).
112 ; This is mostly for descriptive purposes.
113 (define APPLICATION 'UNKNOWN)
115 ; Things are organized so that files can be compiled with Hobbit for
116 ; experimentation. Thus we need one file that loads all the other files.
117 ; This is that file, though it would make sense to move the code in this
120 ; If a routine to initialize compiled-in code is defined, run it.
121 (if (defined? 'cgen-init-c) (cgen-init-c))
123 ; If this is set to #f, the file is always loaded.
124 ; Don't override any current setting, e.g. from dev.scm.
125 (if (not (defined? 'CHECK-LOADED?))
126 (define CHECK-LOADED? #t))
128 ; Unlink file if we're reloaded (say in an interactive session).
129 ; Dynamic loading is enabled by setting LIBCPU.SO to the pathname of the .so.
130 (if (and (defined? 'libcpu.so) (dynamic-object? libcpu.so))
131 (dynamic-unlink libcpu.so))
132 (define libcpu.so #f)
133 (if (and (defined? 'LIBCPU.SO)
134 (file-exists? LIBCPU.SO))
135 (set! libcpu.so (dynamic-link LIBCPU.SO))
138 ; List of loaded files.
140 (if (not (defined? '/loaded-file-list))
141 (define /loaded-file-list '()))
143 ; Return non-zero if FILE was loaded last time through.
145 (define (/loaded-file? file)
146 (->bool (memq (string->symbol file) /loaded-file-list))
149 ; Record FILE as compiled in.
151 (define (/loaded-file-record! file)
152 (let ((file (string->symbol file)))
153 (if (not (memq file /loaded-file-list))
154 (set! /loaded-file-list (cons file /loaded-file-list))))
157 ; Load FILE if SYM is not compiled in.
159 (define (maybe-load file init-func sym)
160 ; Return non-#f if FUNC is present in DYNOBJ.
161 (define (dynamic-func? func dynobj)
163 (lambda () (dynamic-func func dynobj))
167 (let ((init-func (string-append "init_" (if init-func init-func file))))
168 (cond ((and libcpu.so
169 (dynamic-func? init-func libcpu.so))
170 (dynamic-call init-func libcpu.so)
171 (display (string-append "Skipping " file ", dynamically loaded.\n")))
172 ((or (not CHECK-LOADED?)
174 (/loaded-file? file))
175 (/loaded-file-record! file)
178 (display (string-append "Skipping " file ", already loaded.\n")))))
181 (maybe-load "pmacros" #f 'define-pmacro)
182 (maybe-load "cos" #f 'make)
183 (maybe-load "slib/logical" #f 'logical:logand)
184 (maybe-load "slib/sort" #f 'sort)
185 ; Used to pretty-print debugging messages.
186 (maybe-load "slib/pp" #f 'pretty-print)
187 ; Used by pretty-print.
188 (maybe-load "slib/random" #f 'random)
189 (maybe-load "slib/genwrite" #f 'generic-write)
190 (maybe-load "utils" #f 'logit)
191 (maybe-load "utils-cgen" "utils_cgen" 'obj:name)
192 (maybe-load "attr" #f '<attribute>)
193 (maybe-load "enum" #f '<enum>)
194 (maybe-load "mach" #f '<mach>)
195 (maybe-load "model" #f '<model>)
196 (maybe-load "types" #f '<scalar>)
197 (maybe-load "mode" #f '<mode>)
198 (maybe-load "ifield" #f '<ifield>)
199 (maybe-load "iformat" #f '<iformat>)
200 (maybe-load "hardware" #f '<hardware-base>)
201 (maybe-load "operand" #f '<operand>)
202 (maybe-load "insn" #f '<insn>)
203 (maybe-load "minsn" #f '<macro-insn>)
204 (maybe-load "decode" #f 'decode-build-table)
205 (maybe-load "rtl" "rtl" '<rtx-func>)
206 (maybe-load "rtl-traverse" "rtl_traverse" 'rtx-traverse)
207 (maybe-load "rtl-xform" "rtx_simplify" 'rtx-simplify)
208 (maybe-load "rtx-funcs" "rtx_funcs" 'def-rtx-funcs)
209 (maybe-load "rtl-c" "rtl_c" '<c-expr>)
210 (maybe-load "semantics" #f 'semantic-compile)
211 (maybe-load "sem-frags" "sem_frags" 'gen-threaded-engine)
212 (maybe-load "utils-gen" "utils_gen" 'attr-gen-decl)
213 (maybe-load "pgmr-tools" "pgmr_tools" 'pgmr-pretty-print-insn-format)
216 ; All state regarding the reading of a .cpu file is kept in an object of
219 ; Class to record info for each top-level `command' (for lack of a better
220 ; word) in the description file.
221 ; Top level commands are things like define-*.
224 (class-make '<command>
227 ; argument spec to `lambda'
229 ; lambda that processes the entry
235 (define command-arg-spec (elm-make-getter <command> 'arg-spec))
236 (define command-handler (elm-make-getter <command> 'handler))
238 ; Return help text for COMMAND.
240 (define (command-help cmd)
244 (with-output-to-string (lambda () (write (command-arg-spec cmd))))
248 ; A pair of two lists: machs to keep, machs to drop.
249 ; The default is "keep all machs", "drop none".
251 (define /keep-all-machs '((all)))
253 ; Main reader state class.
256 (class-make '<reader>
259 ; Selected machs to keep.
260 ; A pair of two lists: the car lists the machs to keep, the cdr
261 ; lists the machs to drop. Two special entries are `all' and
262 ; `base'. Both are only valid in the keep list. `base' is a
263 ; place holder for objects that are common to all machine
264 ; variants in the architecture, it is the default value of the
265 ; MACH attribute. If `all' is present the drop list is still
267 (cons 'keep-mach /keep-all-machs)
269 ; Selected isas to keep or `all'.
272 ; Boolean indicating if command tracing is on.
273 (cons 'trace-commands? #f)
275 ; Boolean indicating if pmacro tracing is on.
276 (cons 'trace-pmacros? #f)
278 ; Currently select cpu family, computed from `keep-mach'.
279 ; Some applications don't care, and this is moderately
280 ; expensive to compute so we use delay/force.
283 ; Associative list of file entry commands
284 ; (e.g. define-insn, etc.).
285 ; Each entry is (name . command-object).
288 ; The current source location.
289 ; This is recorded here by the higher level reader and is
290 ; fetched by commands as necessary.
298 (define-getters <reader> reader
300 trace-commands? trace-pmacros?
301 current-cpu commands location))
302 (define-setters <reader> reader
304 trace-commands? trace-pmacros?
305 current-cpu commands location))
307 (define (reader-add-command! name comment attrs arg-spec handler)
308 (reader-set-commands! CURRENT-READER
310 (make <command> name comment attrs
312 (reader-commands CURRENT-READER)))
315 (define (/reader-lookup-command name)
316 (assq-ref (reader-commands CURRENT-READER) name)
319 ; Reader state for current .cpu file.
321 (define CURRENT-READER #f)
323 ; Return the current source location in readable form.
324 ; FIXME: Currently unused, keep for reference for awhile.
326 (define (/readable-current-location)
327 (let ((loc (current-reader-location)))
329 (location->string loc)
330 ;; Blech, we don't have a current reader location. That's odd.
331 ;; Fall back to the current input port's location.
332 (string-append (or (port-filename (current-input-port))
335 (number->string (port-line (current-input-port)))
339 ;;; Signal a parse error while reading a .cpu file.
340 ;;; If CONTEXT is #f, use a default context of the current reader location
341 ;;; and an empty prefix.
342 ;;; If MAYBE-HELP-TEXT is specified, elide the last trailing \n.
343 ;;; Multiple lines of help text need embedded newlines, and should be no longer
344 ;;; than 79 characters.
346 (define (parse-error context message expr . maybe-help-text)
348 (set! context (make <context> (current-reader-location) #f)))
350 (let* ((loc (or (context-location context) (unspecified-location)))
351 (top-sloc (location-top loc))
352 (intro "While reading description")
353 (prefix (or (context-prefix context) "Error"))
354 (text (string-append prefix ": " message)))
359 "\n~A:\n@ ~A:\n\n~A: ~A: ~S~A"
361 (location->string loc)
362 (single-location->simple-string top-sloc)
365 (if (null? maybe-help-text)
367 (string-append "\n\n" (car maybe-help-text))))))
370 ; Return the current source location.
372 ; If CURRENT-READER is uninitialized, return "unspecified" location.
373 ; This is done so that things like define-pmacro work in interactive mode.
375 (define (current-reader-location)
377 (reader-location CURRENT-READER)
378 (unspecified-location))
381 ; Process a macro-expanded entry.
383 (define (/reader-process-expanded-1! entry)
384 (let ((location (location-property entry)))
386 ;; Set the current source location for better diagnostics.
387 ;; Access with current-reader-location.
388 (reader-set-location! CURRENT-READER location)
390 (if (reader-trace-commands? CURRENT-READER)
391 (message "Processing command:\n @ "
392 (if location (location->string location) "location unknown")
394 (with-output-to-string (lambda () (pretty-print entry)))))
396 (let ((command (/reader-lookup-command (car entry)))
397 (context (make-current-context #f)))
401 (let* ((handler (command-handler command))
402 (arg-spec (command-arg-spec command))
403 (num-args (num-args arg-spec)))
405 ;; Variable number of trailing arguments.
406 (if (< (length (cdr entry)) (car num-args))
408 (string-append "Incorrect number of arguments to "
409 (symbol->string (car entry))
410 ", expecting at least "
411 (number->string (car num-args)))
413 (command-help command))
414 (apply handler (cdr entry)))
415 ;; Fixed number of arguments.
416 (if (!= (length (cdr entry)) (car num-args))
418 (string-append "Incorrect number of arguments to "
419 (symbol->string (car entry))
421 (number->string (car num-args)))
423 (command-help command))
424 (apply handler (cdr entry)))))
426 (parse-error context "unknown entry type" entry))))
431 ;; Process 1 or more macro-expanded entries.
432 ;; ENTRY is expected to have a location-property object property.
434 ;; NOTE: This is "public" so the .eval pmacro can use it.
435 ;; This is also used by /cmd-if.
437 (define (reader-process-expanded! entry)
438 ;; () is used to indicate a no-op
441 ;; `begin' is used to group a collection of entries into one,
442 ;; since pmacro can only return one expression (borrowed from
443 ;; Scheme of course).
444 ;; Recurse in case there are nested begins.
445 ((eq? (car entry) 'begin)
446 (for-each reader-process-expanded!
449 (/reader-process-expanded-1! entry)))
454 ; Process file entry ENTRY.
455 ; LOC is a <location> object for ENTRY.
457 (define (/reader-process! entry loc)
458 (if (not (form? entry))
459 (parse-error loc "improperly formed entry" entry))
461 ; First do macro expansion, but not if define-pmacro of course.
462 ; ??? Singling out define-pmacro this way seems a bit odd. The way to look
463 ; at it, I guess, is to think of define-pmacro as (currently) the only
464 ; "syntactic" command (it doesn't pre-evaluate its arguments).
465 (let ((expansion (if (eq? (car entry) 'define-pmacro)
466 (begin (location-property-set! entry loc) entry)
467 (if (reader-trace-pmacros? CURRENT-READER)
468 (pmacro-trace entry loc)
469 (pmacro-expand entry loc)))))
470 (reader-process-expanded! expansion))
475 ; Read in and process FILE.
477 ; It would be nice to get the line number of the beginning of the object,
478 ; but that's extra work, so for now we do the simple thing and use
479 ; port-line after we've read an entry.
481 (define (reader-read-file! file)
482 (let ((readit (lambda ()
483 (let loop ((entry (read)))
484 (if (eof-object? entry)
487 ;; ??? The location we pass here isn't ideal.
488 ;; Ideally we'd pass the start location of the
489 ;; expression, instead we currently pass the end
490 ;; location (it's easier).
491 ;; ??? Use source-properties of entry, and only if
492 ;; not present fall back on current-input-location.
493 (/reader-process! entry (current-input-location #t))
497 (with-input-from-file file readit))
502 ; Cpu data is recorded in an object of class <arch>.
503 ; This is necessary as we need to allow recording of multiple cpu descriptions
505 ; Class <arch> is defined in mach.scm.
507 ; Global containing all data of the currently selected architecture.
509 (define CURRENT-ARCH #f)
511 ; `keep-mach' processing.
513 ; Return the currently selected cpu family.
514 ; If a specific cpu family has been selected, each machine that is kept must
515 ; be in that cpu family [so there's no ambiguity in the result].
516 ; This is a moderately expensive computation so use delay/force.
518 (define (current-cpu) (force (reader-current-cpu CURRENT-READER)))
520 ; Return a boolean indicating if CPU-NAME is to be kept.
521 ; ??? Currently this is always true. Note that this doesn't necessarily apply
522 ; to machs in CPU-NAME.
524 (define (keep-cpu? cpu-name) #t)
526 ; Cover proc to set `keep-mach'.
527 ; MACH-NAME-LIST is a comma separated string of machines to keep and drop
528 ; (if prefixed with !).
530 (define (/keep-mach-set! mach-name-list)
531 (let* ((mach-name-list (string-cut mach-name-list #\,))
532 (keep (find (lambda (name) (not (char=? (string-ref name 0) #\!)))
534 (drop (map (lambda (name) (string->symbol (string-drop 1 name)))
535 (find (lambda (name) (char=? (string-ref name 0) #\!))
537 (reader-set-keep-mach! CURRENT-READER
538 (cons (map string->symbol keep)
539 (map string->symbol drop)))
541 (reader-set-current-cpu!
543 (delay (let ((selected-machs (find (lambda (mach)
544 (keep-mach? (list (obj:name mach))))
545 (current-mach-list))))
546 (if (= (length selected-machs) 0)
547 (error "no machs selected"))
548 (if (not (all-true? (map (lambda (mach)
549 (eq? (obj:name (mach-cpu mach))
550 (obj:name (mach-cpu (car selected-machs)))))
552 (error "machs from different cpu families selected"))
553 (mach-cpu (car selected-machs)))))
558 ; Validate the user-provided keep-mach list against the list of machs
559 ; specified in the .cpu file (in define-arch).
561 (define (keep-mach-validate!)
562 (let ((mach-names (cons 'all (current-arch-mach-name-list)))
563 (keep-mach (reader-keep-mach CURRENT-READER)))
564 (for-each (lambda (mach)
565 (if (not (memq mach mach-names))
566 (error "unknown mach to keep:" mach)))
568 (for-each (lambda (mach)
569 (if (not (memq mach mach-names))
570 (error "unknown mach to drop:" mach)))
576 ; Return #t if a machine in MACH-LIST, a list of symbols, is to be kept.
577 ; If any machine in MACH-LIST is to be kept, the result is #t.
578 ; If MACH-LIST is the empty list (no particular mach specified, thus the base
579 ; mach), the result is #t.
581 (define (keep-mach? mach-list)
582 (if (null? mach-list)
584 (let* ((keep-mach (reader-keep-mach CURRENT-READER))
585 (keep (cons 'base (car keep-mach)))
586 (drop (cdr keep-mach))
587 (keep? (map (lambda (m) (memq m keep)) mach-list))
588 (all? (memq 'all keep))
589 (drop? (map (lambda (m) (memq m drop)) mach-list)))
590 (any-true? (map (lambda (k d)
591 ; keep if K(ept) or ALL? and not D(ropped)
592 (->bool (and (or k all?) (not d))))
596 ; Return non-#f if the object containing ATLIST is to be kept.
597 ; OBJ is the container object or #f if there is none.
598 ; The object is kept if its attribute list specifies a `MACH' that is
599 ; kept (and not dropped) or does not have the `MACH' attribute (which means
600 ; it has the default value which means it's for use with all machines).
602 (define (keep-mach-atlist? atlist obj)
603 ; The MACH attribute is not created until the .cpu file is read in which
604 ; is too late for us [we will get called for builtin objects].
605 ; Thus we peek inside the attribute list directly.
606 ; ??? Maybe postpone creation of builtins until after define-arch?
607 (let ((machs (atlist-attr-value-no-default atlist 'MACH obj)))
610 (keep-mach? (bitset-attr->list machs))))
613 ; Return a boolean indicating if the object containing ATLIST is to be kept.
614 ; OBJ is the container object or #f if there is none.
615 ; The object is kept if both its isa and its mach are kept.
617 (define (keep-atlist? atlist obj)
618 (and (keep-mach-atlist? atlist obj)
619 (keep-isa-atlist? atlist obj))
622 ; Return a boolean indicating if multiple cpu families are being kept.
624 (define (keep-multiple?)
625 (let ((selected-machs (find (lambda (mach)
626 (keep-mach? (list (obj:name mach))))
627 (current-mach-list))))
628 (not (all-true? (map (lambda (mach)
629 (eq? (obj:name (mach-cpu mach))
630 (obj:name (mach-cpu (car selected-machs)))))
634 ; Return a boolean indicating if everything is kept.
637 (equal? (reader-keep-mach CURRENT-READER) /keep-all-machs)
640 ; Ensure all cpu families were kept, necessary for generating files that
641 ; encompass the entire architecture.
643 (define (assert-keep-all)
644 (if (not (keep-all?))
645 (error "no can do, all cpu families not selected"))
649 ; Ensure exactly one cpu family was kept, necessary for generating files that
650 ; are specific to one cpu family.
652 (define (assert-keep-one)
654 (error "no can do, multiple cpu families selected"))
658 ; `keep-isa' processing.
660 ; Cover proc to set `keep-isa'.
661 ; ISA-NAME-LIST is a comma separated string of isas to keep.
662 ; ??? We don't support the !drop notation of keep-mach processing.
663 ; Perhaps we should as otherwise there are two different styles the user
664 ; has to remember. On the other hand, !drop support is moderately complicated,
665 ; and it can be added in an upward compatible manner later.
667 (define (/keep-isa-set! isa-name-list)
668 (let ((isa-name-list (map string->symbol (string-cut isa-name-list #\,))))
669 (reader-set-keep-isa! CURRENT-READER isa-name-list)
674 ; Validate the user-provided keep-isa list against the list of isas
675 ; specified in the .cpu file (in define-arch).
677 (define (keep-isa-validate!)
678 (let ((isa-names (cons 'all (current-arch-isa-name-list)))
679 (keep-isa (reader-keep-isa CURRENT-READER)))
680 (for-each (lambda (isa)
681 (if (not (memq isa isa-names))
682 (error "unknown isa to keep:" isa)))
688 ; Return currently selected isa (there must be exactly one).
690 (define (current-isa)
691 (let ((keep-isa (reader-keep-isa CURRENT-READER)))
692 (if (equal? keep-isa '(all))
693 (let ((isas (current-isa-list)))
694 (if (= (length isas) 1)
696 (error "multiple isas selected" keep-isa)))
697 (if (= (length keep-isa) 1)
698 (current-isa-lookup (car keep-isa))
699 (error "multiple isas selected" keep-isa))))
702 ; Return #t if an isa in ISA-LIST, a list of symbols, is to be kept.
703 ; If any isa in ISA-LIST is to be kept, the result is #t.
704 ; If ISA-LIST is the empty list (no particular isa specified) use the default
707 (define (keep-isa? isa-list)
709 (set! isa-list (list (car (current-arch-isa-name-list)))))
710 (let* ((keep (reader-keep-isa CURRENT-READER))
711 (keep? (map (lambda (i)
718 ; Return #t if the object containing ATLIST is to be kept.
719 ; OBJ is the container object or #f if there is none.
720 ; The object is kept if its attribute list specifies an `ISA' that is
721 ; kept or does not have the `ISA' attribute (which means it has the default
722 ; value) and the default isa is being kept.
724 (define (keep-isa-atlist? atlist obj)
725 (let ((isas (atlist-attr-value atlist 'ISA obj)))
726 (keep-isa? (bitset-attr->list isas)))
729 ; Return non-#f if object OBJ is to be kept, according to its ISA attribute.
731 (define (keep-isa-obj? obj)
732 (keep-isa-atlist? (obj-atlist obj) obj)
735 ; Return a boolean indicating if multiple isas are being kept.
737 (define (keep-isa-multiple?)
738 (let ((keep (reader-keep-isa CURRENT-READER)))
739 (or (> (length keep) 1)
740 (and (memq 'all keep)
741 (> (length (current-arch-isa-name-list)) 1))))
744 ; Return list of isa names currently being kept.
746 (define (current-keep-isa-name-list)
747 (reader-keep-isa CURRENT-READER)
751 ;; This is akin to the "logit" support, but is for specific things that
752 ;; can be named (whereas logit support is based on a simple integer verbosity
755 ;;; Enable the specified tracing.
756 ;;; TRACE-OPTIONS is a comma-separated list of things to trace.
758 ;;; Currently supported tracing:
759 ;;; commands - trace invocation of description file commands (e.g. define-insn)
760 ;;; pmacros - trace pmacro expansion
761 ;;; all - trace everything
763 ;;; [If we later need to support disabling some tracing, one way is to
764 ;;; recognize an "-" in front of an option.]
766 (define (/set-trace-options! trace-options)
767 (let ((all (list "commands" "pmacros"))
768 (requests (string-cut trace-options #\,)))
769 (if (member "all" requests)
770 (append! requests all))
771 (for-each (lambda (item)
772 (cond ((string=? "commands" item)
773 (reader-set-trace-commands?! CURRENT-READER #t))
774 ((string=? "pmacros" item)
775 (reader-set-trace-pmacros?! CURRENT-READER #t))
776 ((string=? "all" item)
779 (cgen-usage 'unknown (string-append "-t " item)
786 ; If #f, treat reserved fields as operands and extract them with the insn.
787 ; Code can then be emitted in the extraction routines to validate them.
788 ; If #t, treat reserved fields as part of the opcode.
789 ; This complicates the decoding process as these fields have to be
793 (define option:reserved-as-opcode? #f)
795 ; Process options passed in on the command line.
796 ; OPTIONS is a space separated string of name=value values.
797 ; Each application is required to provide: option-init!, option-set!.
799 (define (set-cgen-options! options)
801 (for-each (lambda (opt)
803 #t ; ignore extraneous spaces
804 (let ((name (string->symbol (car opt)))
806 (logit 1 "Setting option `" name "' to \""
807 (apply string-append value) "\".\n")
808 (option-set! name value))))
809 (map (lambda (opt) (string-cut opt #\=))
810 (string-cut options #\space)))
813 ; Application specific object creation support.
815 ; Each entry in the .cpu file has a basic container class.
816 ; Each application adds functionality by subclassing the container
817 ; and registering with set-for-new! the proper class to create.
818 ; ??? Not sure this is the best way to handle this, but it does keep the
819 ; complexity down while not requiring as dynamic a language as I had before.
820 ; ??? Class local variables would provide a more efficient way to do this.
821 ; Assuming one wants to continue on this route.
823 (define /cpu-new-class-list nil)
825 (define (set-for-new! parent child)
826 (set! /cpu-new-class-list (acons parent child /cpu-new-class-list))
829 ; Lookup the class registered with set-for-new!
830 ; If none registered, return PARENT.
832 (define (lookup-for-new parent)
833 (let ((child (assq-ref /cpu-new-class-list parent)))
839 ; .cpu file loader support
841 ;; Initialize a new <reader> object.
842 ;; This doesn't add cgen-specific commands, leaving each element (ifield,
843 ;; hardware, etc.) to add their own.
844 ;; The "result" is stored in global CURRENT-READER.
846 (define (/init-reader!)
847 (set! CURRENT-READER (new <reader>))
849 (reader-add-command! 'define-rtl-version
850 "Specify the RTL version being used.\n"
851 nil '(major minor) /cmd-define-rtl-version)
853 (reader-add-command! 'include
855 nil '(file) /cmd-include)
856 (reader-add-command! 'if
857 "(if test then . else)\n"
858 nil '(test then . else) /cmd-if)
860 ; Rather than add cgen-internal specific stuff to pmacros.scm, we create
861 ; the pmacro commands here.
863 (reader-add-command! 'define-pmacro
865 Define a preprocessor-style macro.
867 nil '(name arg1 . arg-rest) define-pmacro)
872 ; Prepare to parse a .cpu file.
873 ; This initializes the application independent tables.
874 ; KEEP-MACH specifies what machs to keep.
875 ; KEEP-ISA specifies what isas to keep.
876 ; OPTIONS is a list of options to control code generation.
877 ; The values are application dependent.
879 (define (/init-parse-cpu! keep-mach keep-isa options)
880 (set! /cpu-new-class-list nil)
882 (set! CURRENT-ARCH (new <arch>))
883 (/keep-mach-set! keep-mach)
884 (/keep-isa-set! keep-isa)
885 (set-cgen-options! options)
887 ; The order here is important.
888 (arch-init!) ; Must be done first.
907 ; Install any builtin objects.
908 ; This is deferred until define-arch is read.
909 ; One reason is that attributes MACH and ISA don't exist until then.
911 (define (reader-install-builtin!)
912 ; The order here is important.
918 ; This is mainly for the insn attributes.
924 ; Do anything necessary for the application independent parts after parsing
926 ; The lists get cons'd in reverse order. One thing this does is change them
927 ; back to file order, it makes things easier for the human viewer.
929 (define (/finish-parse-cpu!)
930 ; The order here is generally the reverse of init-parse-cpu!.
943 (arch-finish!) ; Must be done last.
948 ; Perform a global error checking pass after the .cpu file has been read in.
950 (define (/global-error-checks)
953 ; - all hardware elements with same name must have same rank and
954 ; compatible modes (which for now means same float mode or all int modes)
958 ; .cpu file include mechanism
960 (define (/cmd-include file)
961 (logit 1 "Including file " (string-append arch-path "/" file) " ...\n")
962 (reader-read-file! (string-append arch-path "/" file))
963 (logit 2 "Resuming previous file ...\n")
966 ; Version of `if' invokable at the top level of a description file.
967 ; This is a work-in-progress. Its presence in the description file is ok,
968 ; but the implementation will need to evolve.
970 (define (/cmd-if test then . else)
971 (if (> (length else) 1)
973 "wrong number of arguments to `if'"
974 (cons 'if (cons test (cons then else)))))
976 (if (or (not (pair? test))
977 (not (memq (car test) '(keep-isa? keep-mach? application-is?))))
979 "only (if (keep-mach?|keep-isa?|application-is? ...) ...) are currently supported"
983 (if (keep-isa? (cadr test))
984 (reader-process-expanded! then)
987 (reader-process-expanded! (car else)))))
989 (if (keep-mach? (cadr test))
990 (reader-process-expanded! then)
993 (reader-process-expanded! (car else)))))
995 (if (eq? APPLICATION (cadr test))
996 (reader-process-expanded! then)
999 (reader-process-expanded! (car else))))))
1002 ; Top level routine for loading .cpu files.
1003 ; FILE is the name of the .cpu file to load.
1004 ; KEEP-MACH is a string of comma separated machines to keep
1005 ; (or not keep if prefixed with !).
1006 ; KEEP-ISA is a string of comma separated isas to keep.
1007 ; OPTIONS is the OPTIONS argument to -init-parse-cpu!.
1008 ; TRACE-OPTIONS is a random list of things to trace.
1009 ; APP-INITER! is an application specific zero argument proc (thunk)
1010 ; to call after -init-parse-cpu!
1011 ; APP-FINISHER! is an application specific zero argument proc to call after
1012 ; -finish-parse-cpu!
1013 ; ANALYZER! is a zero argument proc to call after loading the .cpu file.
1014 ; It is expected to set up various tables and things useful for the application
1017 ; This function isn't local because it's used by dev.scm.
1019 (define (cpu-load file keep-mach keep-isa options trace-options
1020 app-initer! app-finisher! analyzer!)
1022 (/init-parse-cpu! keep-mach keep-isa options)
1023 (/set-trace-options! trace-options)
1025 (logit 1 "Loading cpu description " file " ...\n")
1026 (set! arch-path (dirname file))
1027 (reader-read-file! file)
1028 (logit 2 "Processing cpu description " file " ...\n")
1029 (/finish-parse-cpu!)
1031 (/global-error-checks)
1036 ; Argument parsing utilities.
1038 ; Generate a usage message.
1039 ; ERRTYPE is one of 'help, 'unknown, 'missing.
1040 ; OPTION is the option that had the error or "" if ERRTYPE is 'help.
1042 (define (cgen-usage errtype option arguments)
1043 (let ((cep (current-error-port)))
1046 ((unknown) (display (string-append "Unknown option: " option "\n") cep))
1047 ((missing) (display (string-append "Missing argument: " option "\n") cep))
1048 (else (display "Unknown error!\n" cep)))
1049 (display "Usage: cgen arguments ...\n" cep)
1050 (for-each (lambda (arg)
1051 (display (string-append
1052 (let ((arg-str (string-append (car arg) " "
1053 (or (cadr arg) ""))))
1054 (if (< (string-length arg-str) 16)
1055 (string-take 16 arg-str)
1058 (apply string-append
1067 (display "...\n" cep)
1070 ((unknown missing) (quit 1))
1074 ; Poor man's getopt.
1075 ; [We don't know where to find the real one until we've parsed the args,
1076 ; and this isn't something we need to get too fancy about anyways.]
1077 ; The result is always ((a . b) . c).
1078 ; If the argument is valid, the result is ((opt-spec . arg) . remaining-argv),
1079 ; or (('unknown . option) . remaining-argv) if `option' isn't recognized,
1080 ; or (('missing . option) . remaining argv) if `option' is missing a required
1082 ; or ((#f . #f) . #f) if there are no more arguments.
1083 ; OPT-SPEC is a list of option specs.
1084 ; Each element is an alist of at least 3 elements: option argument help-text.
1085 ; `option' is a string or symbol naming the option. e.g. -a, --help, "-i".
1086 ; symbols are supported for backward compatibility, -i is a complex number.
1087 ; `argument' is a string naming the argument or #f if the option takes no
1089 ; `help-text' is a string that is printed with the usage information.
1090 ; Elements beyond `help-text' are ignored.
1092 (define (/getopt argv opt-spec)
1094 (cons (cons #f #f) #f)
1095 (let ((opt (assoc (car argv) opt-spec)))
1096 (cond ((not opt) (cons (cons 'unknown (car argv)) (cdr argv)))
1097 ((and (cadr opt) (null? (cdr argv)))
1098 (cons (cons 'missing (car argv)) (cdr argv)))
1099 ((cadr opt) (cons (cons opt (cadr argv)) (cddr argv)))
1100 (else ; must be option that doesn't take an argument
1101 (cons (cons opt #f) (cdr argv))))))
1104 ; Return (cadr args) or print a pretty error message if not possible.
1106 (define (option-arg args)
1107 (if (and (pair? args) (pair? (cdr args)))
1109 (parse-error (make-prefix-context "option processing")
1110 "missing argument to"
1114 ; List of common arguments.
1116 ; ??? Another useful arg would be one that says "do file generation with
1117 ; arguments specified up til now, then continue with next batch of args".
1119 (define common-arguments
1120 '(("-a" "arch-file" "specify path of .cpu file to load")
1121 ("-b" #f "use debugging evaluator, for backtraces")
1122 ("-d" #f "start interactive debugging session")
1123 ("-f" "flags" "specify a set of flags to control code generation")
1124 ("-h" #f "print usage information")
1125 ("--help" #f "print usage information")
1126 ("-i" "isa-list" "specify isa-list entries to keep")
1127 ("-m" "mach-list" "specify mach-list entries to keep")
1128 ("-s" "srcdir" "set srcdir")
1129 ("-t" "trace-options" "specify list of things to trace"
1131 "commands - trace cgen commands (e.g. define-insn)"
1132 "pmacros - trace pmacro expansion"
1133 "all - trace everything")
1134 ("-v" #f "increment verbosity level")
1135 ("--version" #f "print version info")
1139 ; Default place to look.
1140 ; This gets overridden to point to the directory of the loaded .cpu file.
1141 ; ??? Ideally this would be local to this file.
1143 (define arch-path (string-append srcdir "/cpu"))
1145 ; Accessors for application option specs
1147 (define (opt-get-first-pass opt)
1148 (or (list-ref opt 3) (lambda args #f)))
1149 (define (opt-get-second-pass opt)
1150 (or (list-ref opt 4) (lambda args #f)))
1152 ; Parse options and call generators.
1153 ; ARGS is a #:keyword delimited list of arguments.
1155 ; #:arg-spec optspec ; FIXME: rename to #:opt-spec
1156 ; #:init init-routine
1157 ; #:finish finish-routine
1158 ; #:analyze analysis-routine
1159 ; #:argv command-line-arguments
1161 ; ARGSPEC is a list of (option option-arg comment option-handler) elements.
1162 ; OPTION-HANDLER is either (lambda () ...) or (lambda (arg) ...) and
1163 ; processes the option.
1167 (let ((app-name "unknown")
1169 (app-init! (lambda () #f))
1170 (app-finish! (lambda () #f))
1171 (app-analyze! (lambda () #f))
1172 (argv (list "cgen"))
1174 (let loop ((args args))
1175 (if (not (null? args))
1177 ((#:app-name) (begin
1178 (set! app-name (option-arg args))
1179 (loop (cddr args))))
1180 ((#:arg-spec) (begin
1181 (set! opt-spec (option-arg args))
1182 (loop (cddr args))))
1184 (set! app-init! (option-arg args))
1185 (loop (cddr args))))
1187 (set! app-finish! (option-arg args))
1188 (loop (cddr args))))
1190 (set! app-analyze! (option-arg args))
1191 (loop (cddr args))))
1193 (set! argv (option-arg args))
1194 (loop (cddr args))))
1195 (else (error "cgen: unknown argument" (car args))))))
1197 ; ARGS has been processed, now we can process ARGV.
1200 (opt-spec (append common-arguments opt-spec))
1201 (app-args nil) ; application's args are queued here
1204 (keep-mach "all") ; default is all machs
1205 (keep-isa "all") ; default is all isas
1208 (debugging #f) ; default is off, for speed
1210 (cep (current-error-port))
1214 (let loop ((argv (cdr argv)))
1215 (let* ((new-argv (/getopt argv opt-spec))
1216 (opt (caar new-argv))
1217 (arg (cdar new-argv)))
1219 ((#f) (set! moreopts? #f))
1220 ((unknown) (cgen-usage 'unknown arg opt-spec))
1221 ((missing) (cgen-usage 'missing arg opt-spec))
1223 (cond ((str=? "-a" (car opt))
1224 (set! arch-file arg)
1226 ((str=? "-b" (car opt))
1229 ((str=? "-d" (car opt))
1230 (let ((prompt (string-append "cgen-" app-name "> ")))
1232 (set-repl-prompt! prompt)
1233 (if (feature? 'readline)
1234 (set-readline-prompt! prompt))
1236 ((str=? "-f" (car opt))
1239 ((str=? "-h" (car opt))
1240 (cgen-usage 'help "" opt-spec)
1242 ((str=? "--help" (car opt))
1243 (cgen-usage 'help "" opt-spec)
1245 ((str=? "-i" (car opt))
1248 ((str=? "-m" (car opt))
1249 (set! keep-mach arg)
1251 ((str=? "-s" (car opt))
1252 #f ; ignore, already processed by caller
1254 ((str=? "-t" (car opt))
1255 (set! trace-options arg)
1257 ((str=? "-v" (car opt))
1260 ((str=? "--version" (car opt))
1262 (display "Cpu tools GENerator version ")
1263 (display (cgen-major))
1265 (display (cgen-minor))
1267 (display (cgen-fixlevel))
1269 (display "RTL version ")
1270 (display (cgen-rtl-major))
1272 (display (cgen-rtl-minor))
1276 ; Else this is an application specific option.
1278 ; Record it for later processing. Note that they're
1279 ; recorded in reverse order (easier). This is undone
1281 (set! app-args (acons opt arg app-args)))
1283 (if moreopts? (loop (cdr new-argv)))
1287 ; All arguments have been parsed.
1289 (cgen-call-with-debugging
1294 (error "-a option missing, no architecture specified"))
1300 keep-mach keep-isa flags trace-options
1301 app-init! app-finish! app-analyze!)
1303 ;; Start another repl loop if -d.
1304 ;; Awkward. Both places are useful, though this is more useful.
1308 ;; Done with processing the arguments. Application arguments
1309 ;; are processed in two passes. This is because the app may
1310 ;; have arguments that specify things that affect file
1311 ;; generation (e.g. to specify another input file) and we
1312 ;; don't want to require an ordering of the options.
1313 (for-each (lambda (opt-arg)
1314 (let ((opt (car opt-arg))
1315 (arg (cdr opt-arg)))
1317 ((opt-get-first-pass opt) arg)
1318 ((opt-get-first-pass opt)))))
1321 (for-each (lambda (opt-arg)
1322 (let ((opt (car opt-arg))
1323 (arg (cdr opt-arg)))
1325 ((opt-get-second-pass opt) arg)
1326 ((opt-get-second-pass opt)))))
1327 (reverse app-args))))
1333 ; Main entry point called by application file generators.
1337 (cgen-debugging-stack-start /cgen args))