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 (define -CGEN-VERSION '(1 1 0))
67 (define (cgen-major) (car -CGEN-VERSION))
68 (define (cgen-minor) (cadr -CGEN-VERSION))
69 (define (cgen-fixlevel) (caddr -CGEN-VERSION))
71 ; A list of three numbers designating the description language version.
72 ; Note that this is different from -CGEN-VERSION.
73 (define -CGEN-LANG-VERSION '(0 7 2))
74 (define (cgen-lang-major) (car -CGEN-LANG-VERSION))
75 (define (cgen-lang-minor) (cadr -CGEN-LANG-VERSION))
76 (define (cgen-lang-fixlevel) (caddr -CGEN-LANG-VERSION))
78 ; Which application is in use (UNKNOWN, DESC, OPCODES, SIMULATOR, ???).
79 ; This is mostly for descriptive purposes.
80 (define APPLICATION 'UNKNOWN)
82 ; Things are organized so that files can be compiled with Hobbit for
83 ; experimentation. Thus we need one file that loads all the other files.
84 ; This is that file, though it would make sense to move the code in this
87 ; If a routine to initialize compiled-in code is defined, run it.
88 (if (defined? 'cgen-init-c) (cgen-init-c))
90 ; If this is set to #f, the file is always loaded.
91 ; Don't override any current setting, e.g. from dev.scm.
92 (if (not (defined? 'CHECK-LOADED?))
93 (define CHECK-LOADED? #t))
95 ; Unlink file if we're reloaded (say in an interactive session).
96 ; Dynamic loading is enabled by setting LIBCPU.SO to the pathname of the .so.
97 (if (and (defined? 'libcpu.so) (dynamic-object? libcpu.so))
98 (dynamic-unlink libcpu.so))
100 (if (and (defined? 'LIBCPU.SO)
101 (file-exists? LIBCPU.SO))
102 (set! libcpu.so (dynamic-link LIBCPU.SO))
105 ; List of loaded files.
107 (if (not (defined? '-loaded-file-list))
108 (define -loaded-file-list '()))
110 ; Return non-zero if FILE was loaded last time through.
112 (define (-loaded-file? file)
113 (->bool (memq (string->symbol file) -loaded-file-list))
116 ; Record FILE as compiled in.
118 (define (-loaded-file-record! file)
119 (let ((file (string->symbol file)))
120 (if (not (memq file -loaded-file-list))
121 (set! -loaded-file-list (cons file -loaded-file-list))))
124 ; Load FILE if SYM is not compiled in.
126 (define (maybe-load file init-func sym)
127 ; Return non-#f if FUNC is present in DYNOBJ.
128 (define (dynamic-func? func dynobj)
130 (lambda () (dynamic-func func dynobj))
134 (let ((init-func (string-append "init_" (if init-func init-func file))))
135 (cond ((and libcpu.so
136 (dynamic-func? init-func libcpu.so))
137 (dynamic-call init-func libcpu.so)
138 (display (string-append "Skipping " file ", dynamically loaded.\n")))
139 ((or (not CHECK-LOADED?)
141 (-loaded-file? file))
142 (-loaded-file-record! file)
145 (display (string-append "Skipping " file ", already loaded.\n")))))
148 (maybe-load "pmacros" #f 'define-pmacro)
149 (maybe-load "cos" #f 'make)
150 (maybe-load "slib/logical" #f 'logical:logand)
151 (maybe-load "slib/sort" #f 'sort)
152 ; Used to pretty-print debugging messages.
153 (maybe-load "slib/pp" #f 'pretty-print)
154 ; Used by pretty-print.
155 (maybe-load "slib/random" #f 'random)
156 (maybe-load "slib/genwrite" #f 'generic-write)
157 (maybe-load "utils" #f 'logit)
158 (maybe-load "utils-cgen" "utils_cgen" 'obj:name)
159 (maybe-load "attr" #f '<attribute>)
160 (maybe-load "enum" #f '<enum>)
161 (maybe-load "mach" #f '<mach>)
162 (maybe-load "model" #f '<model>)
163 (maybe-load "types" #f '<scalar>)
164 (maybe-load "mode" #f '<mode>)
165 (maybe-load "ifield" #f '<ifield>)
166 (maybe-load "iformat" #f '<iformat>)
167 (maybe-load "hardware" #f '<hardware-base>)
168 (maybe-load "operand" #f '<operand>)
169 (maybe-load "insn" #f '<insn>)
170 (maybe-load "minsn" #f '<macro-insn>)
171 (maybe-load "decode" #f 'decode-build-table)
172 (maybe-load "rtl" "rtl" '<rtx-func>)
173 (maybe-load "rtl-traverse" "rtl_traverse" 'rtx-traverse)
174 (maybe-load "rtl-xform" "rtx_simplify" 'rtx-simplify)
175 (maybe-load "rtx-funcs" "rtx_funcs" 'def-rtx-funcs)
176 (maybe-load "rtl-c" "rtl_c" '<c-expr>)
177 (maybe-load "semantics" #f 'semantic-compile)
178 (maybe-load "sem-frags" "sem_frags" 'gen-threaded-engine)
179 (maybe-load "utils-gen" "utils_gen" 'attr-gen-decl)
180 (maybe-load "pgmr-tools" "pgmr_tools" 'pgmr-pretty-print-insn-format)
183 ; All state regarding the reading of a .cpu file is kept in an object of
186 ; Class to record info for each top-level `command' (for lack of a better
187 ; word) in the description file.
188 ; Top level commands are things like define-*.
191 (class-make '<command>
194 ; argument spec to `lambda'
196 ; lambda that processes the entry
202 (define command-arg-spec (elm-make-getter <command> 'arg-spec))
203 (define command-handler (elm-make-getter <command> 'handler))
205 ; Return help text for COMMAND.
207 (define (command-help cmd)
211 (with-output-to-string (lambda () (write (command-arg-spec cmd))))
215 ; A pair of two lists: machs to keep, machs to drop.
216 ; Keep all machs, drop none.
218 (define -keep-all-machs '((all)))
220 ; Main reader state class.
223 (class-make '<reader>
226 ; Selected machs to keep.
227 ; A pair of two lists: the car lists the machs to keep, the cdr
228 ; lists the machs to drop. Two special entries are `all' and
229 ; `base'. Both are only valid in the keep list. `base' is a
230 ; place holder for objects that are common to all machine
231 ; variants in the architecture, it is the default value of the
232 ; MACH attribute. If `all' is present the drop list is still
234 (cons 'keep-mach -keep-all-machs)
236 ; Selected isas to keep or `all'.
239 ; Currently select cpu family, computed from `keep-mach'.
240 ; Some applications don't care, and this is moderately
241 ; expensive to compute so we use delay/force.
244 ; Associative list of file entry commands
245 ; (e.g. define-insn, etc.).
246 ; Each entry is (name . command-object).
254 (define-getters <reader> reader (keep-mach keep-isa current-cpu commands))
255 (define-setters <reader> reader (keep-mach keep-isa current-cpu commands))
257 (define (reader-add-command! name comment attrs arg-spec handler)
258 (reader-set-commands! CURRENT-READER
260 (make <command> name comment attrs
262 (reader-commands CURRENT-READER)))
265 (define (reader-lookup-command name)
266 (assq-ref (reader-commands CURRENT-READER) name)
269 ; Reader state for current .cpu file.
271 (define CURRENT-READER #f)
273 ; Signal an error while reading a .cpu file.
275 (define (reader-error msg expr help-text)
277 (string-append (or (port-filename (current-input-port))
280 (number->string (port-line (current-input-port)))
284 (error (string-append errmsg "\n" help-text)
288 ; Signal a parse error while reading a .cpu file.
290 (define (parse-error errtxt message . args)
291 (reader-error (string-append errtxt ": " message ":") args "")
294 ; Process a macro-expanded entry.
296 (define (-reader-process-expanded-1 entry)
297 (logit 4 (with-output-to-string (lambda () (pretty-print entry))))
298 (let ((command (reader-lookup-command (car entry))))
300 (let* ((handler (command-handler command))
301 (arg-spec (command-arg-spec command))
302 (num-args (num-args arg-spec)))
304 ; Variable number of trailing arguments.
305 (if (< (length (cdr entry)) (car num-args))
306 (reader-error (string-append "Incorrect number of arguments to "
308 ", expecting at least "
309 (number->string (car num-args)))
311 (command-help command))
312 (apply handler (cdr entry)))
313 ; Fixed number of arguments.
314 (if (!= (length (cdr entry)) (car num-args))
315 (reader-error (string-append "Incorrect number of arguments to "
318 (number->string (car num-args)))
320 (command-help command))
321 (apply handler (cdr entry)))))
322 (reader-error "unknown entry type" entry "")))
326 ;; Process 1 or more macro-expanded entries.
328 (define (reader-process-expanded entry)
329 ;; () is used to indicate a no-op
332 ;; `begin' is used to group a collection of entries into one,
333 ;; since pmacro can only return one expression (borrowed from
334 ;; Scheme of course).
335 ;; Recurse in case there are nested begins.
336 ((eq? (car entry) 'begin)
337 (for-each reader-process-expanded
340 (-reader-process-expanded-1 entry)))
343 ; Process file entry ENTRY.
345 (define (reader-process entry)
346 (if (not (form? entry))
347 (reader-error "improperly formed entry" entry ""))
349 ; First do macro expansion, but not if define-pmacro of course.
350 (let ((expansion (if (eq? (car entry) 'define-pmacro)
352 (pmacro-expand entry))))
353 (reader-process-expanded expansion))
356 ; Read in and process FILE.
358 ; It would be nice to get the line number of the beginning of the object,
359 ; but that's extra work, so for now we do the simple thing and use
360 ; port-line after we've read an entry.
362 (define (reader-read-file! file)
363 (let ((readit (lambda ()
364 (let loop ((entry (read)))
365 (if (eof-object? entry)
368 (reader-process entry)
372 (with-input-from-file file readit)
376 ; Cpu data is recorded in an object of class <arch>.
377 ; This is necessary as we need to allow recording of multiple cpu descriptions
379 ; Class <arch> is defined in mach.scm.
381 ; Global containing all data of the currently selected architecture.
383 (define CURRENT-ARCH #f)
385 ; `keep-mach' processing.
387 ; Return the currently selected cpu family.
388 ; If a specific cpu family has been selected, each machine that is kept must
389 ; be in that cpu family [so there's no ambiguity in the result].
390 ; This is a moderately expensive computation so use delay/force.
392 (define (current-cpu) (force (reader-current-cpu CURRENT-READER)))
394 ; Return a boolean indicating if CPU-NAME is to be kept.
395 ; ??? Currently this is always true. Note that this doesn't necessarily apply
396 ; to machs in CPU-NAME.
398 (define (keep-cpu? cpu-name) #t)
400 ; Cover proc to set `keep-mach'.
401 ; MACH-NAME-LIST is a comma separated string of machines to keep and drop
402 ; (if prefixed with !).
404 (define (-keep-mach-set! mach-name-list)
405 (let* ((mach-name-list (string-cut mach-name-list #\,))
406 (keep (find (lambda (name) (not (char=? (string-ref name 0) #\!)))
408 (drop (map (lambda (name) (string->symbol (string-drop 1 name)))
409 (find (lambda (name) (char=? (string-ref name 0) #\!))
411 (reader-set-keep-mach! CURRENT-READER
412 (cons (map string->symbol keep)
413 (map string->symbol drop)))
415 (reader-set-current-cpu!
417 (delay (let ((selected-machs (find (lambda (mach)
418 (keep-mach? (list (obj:name mach))))
419 (current-mach-list))))
420 (if (= (length selected-machs) 0)
421 (error "no machs selected"))
422 (if (not (all-true? (map (lambda (mach)
423 (eq? (obj:name (mach-cpu mach))
424 (obj:name (mach-cpu (car selected-machs)))))
426 (error "machs from different cpu families selected"))
427 (mach-cpu (car selected-machs)))))
432 ; Validate the user-provided keep-mach list against the list of machs
433 ; specified in the .cpu file (in define-arch).
435 (define (keep-mach-validate!)
436 (let ((mach-names (cons 'all (current-arch-mach-name-list)))
437 (keep-mach (reader-keep-mach CURRENT-READER)))
438 (for-each (lambda (mach)
439 (if (not (memq mach mach-names))
440 (error "unknown mach to keep:" mach)))
442 (for-each (lambda (mach)
443 (if (not (memq mach mach-names))
444 (error "unknown mach to drop:" mach)))
450 ; Return #t if a machine in MACH-LIST, a list of symbols, is to be kept.
451 ; If any machine in MACH-LIST is to be kept, the result is #t.
452 ; If MACH-LIST is the empty list (no particular mach specified, thus the base
453 ; mach), the result is #t.
455 (define (keep-mach? mach-list)
456 (if (null? mach-list)
458 (let* ((keep-mach (reader-keep-mach CURRENT-READER))
459 (keep (cons 'base (car keep-mach)))
460 (drop (cdr keep-mach))
461 (keep? (map (lambda (m) (memq m keep)) mach-list))
462 (all? (memq 'all keep))
463 (drop? (map (lambda (m) (memq m drop)) mach-list)))
464 (any-true? (map (lambda (k d)
465 ; keep if K(ept) or ALL? and not D(ropped)
466 (->bool (and (or k all?) (not d))))
470 ; Return non-#f if the object containing ATLIST is to be kept.
471 ; OBJ is the container object or #f if there is none.
472 ; The object is kept if its attribute list specifies a `MACH' that is
473 ; kept (and not dropped) or does not have the `MACH' attribute (which means
474 ; it has the default value which means it's for use with all machines).
476 (define (keep-mach-atlist? atlist obj)
477 ; The MACH attribute is not created until the .cpu file is read in which
478 ; is too late for us [we will get called for builtin objects].
479 ; Thus we peek inside the attribute list directly.
480 ; ??? Maybe postpone creation of builtins until after define-arch?
481 (let ((machs (atlist-attr-value-no-default atlist 'MACH obj)))
484 (keep-mach? (bitset-attr->list machs))))
487 ; Return a boolean indicating if the object containing ATLIST is to be kept.
488 ; OBJ is the container object or #f if there is none.
489 ; The object is kept if both its isa and its mach are kept.
491 (define (keep-atlist? atlist obj)
492 (and (keep-mach-atlist? atlist obj)
493 (keep-isa-atlist? atlist obj))
496 ; Return a boolean indicating if multiple cpu families are being kept.
498 (define (keep-multiple?)
499 (let ((selected-machs (find (lambda (mach)
500 (keep-mach? (list (obj:name mach))))
501 (current-mach-list))))
502 (not (all-true? (map (lambda (mach)
503 (eq? (obj:name (mach-cpu mach))
504 (obj:name (mach-cpu (car selected-machs)))))
508 ; Return a boolean indicating if everything is kept.
511 (equal? (reader-keep-mach CURRENT-READER) -keep-all-machs)
514 ; Ensure all cpu families were kept, necessary for generating files that
515 ; encompass the entire architecture.
517 (define (assert-keep-all)
518 (if (not (keep-all?))
519 (error "no can do, all cpu families not selected"))
523 ; Ensure exactly one cpu family was kept, necessary for generating files that
524 ; are specific to one cpu family.
526 (define (assert-keep-one)
528 (error "no can do, multiple cpu families selected"))
532 ; `keep-isa' processing.
534 ; Cover proc to set `keep-isa'.
535 ; ISA-NAME-LIST is a comma separated string of isas to keep.
536 ; ??? We don't support the !drop notation of keep-mach processing.
537 ; Perhaps we should as otherwise there are two different styles the user
538 ; has to remember. On the other hand, !drop support is moderately complicated,
539 ; and it can be added in an upward compatible manner later.
541 (define (-keep-isa-set! isa-name-list)
542 (let ((isa-name-list (map string->symbol (string-cut isa-name-list #\,))))
543 (reader-set-keep-isa! CURRENT-READER isa-name-list)
548 ; Validate the user-provided keep-isa list against the list of isas
549 ; specified in the .cpu file (in define-arch).
551 (define (keep-isa-validate!)
552 (let ((isa-names (cons 'all (current-arch-isa-name-list)))
553 (keep-isa (reader-keep-isa CURRENT-READER)))
554 (for-each (lambda (isa)
555 (if (not (memq isa isa-names))
556 (error "unknown isa to keep:" isa)))
562 ; Return currently selected isa (there must be exactly one).
564 (define (current-isa)
565 (let ((keep-isa (reader-keep-isa CURRENT-READER)))
566 (if (equal? keep-isa '(all))
567 (let ((isas (current-isa-list)))
568 (if (= (length isas) 1)
570 (error "multiple isas selected" keep-isa)))
571 (if (= (length keep-isa) 1)
572 (current-isa-lookup (car keep-isa))
573 (error "multiple isas selected" keep-isa))))
576 ; Return #t if an isa in ISA-LIST, a list of symbols, is to be kept.
577 ; If any isa in ISA-LIST is to be kept, the result is #t.
578 ; If ISA-LIST is the empty list (no particular isa specified) use the default
581 (define (keep-isa? isa-list)
583 (set! isa-list (list (car (current-arch-isa-name-list)))))
584 (let* ((keep (reader-keep-isa CURRENT-READER))
585 (keep? (map (lambda (i)
592 ; Return #t if the object containing ATLIST is to be kept.
593 ; OBJ is the container object or #f if there is none.
594 ; The object is kept if its attribute list specifies an `ISA' that is
595 ; kept or does not have the `ISA' attribute (which means it has the default
596 ; value) and the default isa is being kept.
598 (define (keep-isa-atlist? atlist obj)
599 (let ((isas (atlist-attr-value atlist 'ISA obj)))
600 (keep-isa? (bitset-attr->list isas)))
603 ; Return non-#f if object OBJ is to be kept, according to its ISA attribute.
605 (define (keep-isa-obj? obj)
606 (keep-isa-atlist? (obj-atlist obj) obj)
609 ; Return a boolean indicating if multiple isas are being kept.
611 (define (keep-isa-multiple?)
612 (let ((keep (reader-keep-isa CURRENT-READER)))
613 (or (> (length keep) 1)
614 (and (memq 'all keep)
615 (> (length (current-arch-isa-name-list)) 1))))
618 ; Return list of isa names currently being kept.
620 (define (current-keep-isa-name-list)
621 (reader-keep-isa CURRENT-READER)
624 ; If #f, treat reserved fields as operands and extract them with the insn.
625 ; Code can then be emitted in the extraction routines to validate them.
626 ; If #t, treat reserved fields as part of the opcode.
627 ; This complicates the decoding process as these fields have to be
631 (define option:reserved-as-opcode? #f)
633 ; Process options passed in on the command line.
634 ; OPTIONS is a space separated string of name=value values.
635 ; Each application is required to provide: option-init!, option-set!.
637 (define (set-cgen-options! options)
639 (for-each (lambda (opt)
641 #t ; ignore extraneous spaces
642 (let ((name (string->symbol (car opt)))
644 (logit 1 "Setting option `" name "' to \""
645 (apply string-append value) "\".\n")
646 (option-set! name value))))
647 (map (lambda (opt) (string-cut opt #\=))
648 (string-cut options #\space)))
651 ; Application specific object creation support.
653 ; Each entry in the .cpu file has a basic container class.
654 ; Each application adds functionality by subclassing the container
655 ; and registering with set-for-new! the proper class to create.
656 ; ??? Not sure this is the best way to handle this, but it does keep the
657 ; complexity down while not requiring as dynamic a language as I had before.
658 ; ??? Class local variables would provide a more efficient way to do this.
659 ; Assuming one wants to continue on this route.
661 (define -cpu-new-class-list nil)
663 (define (set-for-new! parent child)
664 (set! -cpu-new-class-list (acons parent child -cpu-new-class-list))
667 ; Lookup the class registered with set-for-new!
668 ; If none registered, return PARENT.
670 (define (lookup-for-new parent)
671 (let ((child (assq-ref -cpu-new-class-list parent)))
677 ; .cpu file loader support
679 ; Prepare to parse a .cpu file.
680 ; This initializes the application independent tables.
681 ; KEEP-MACH specifies what machs to keep.
682 ; KEEP-ISA specifies what isas to keep.
683 ; OPTIONS is a list of options to control code generation.
684 ; The values are application dependent.
686 (define (-init-parse-cpu! keep-mach keep-isa options)
687 (set! -cpu-new-class-list nil)
689 (set! CURRENT-READER (new <reader>))
690 (set! CURRENT-ARCH (new <arch>))
691 (-keep-mach-set! keep-mach)
692 (-keep-isa-set! keep-isa)
693 (set-cgen-options! options)
695 (reader-add-command! 'include
699 (reader-add-command! 'if
700 "(if test then . else)\n"
701 nil '(test then . else) cmd-if
704 ; Rather than add cgen-internal specific stuff to pmacros.scm, we create
705 ; the pmacro commands here.
707 (reader-add-command! 'define-pmacro
709 Define a preprocessor-style macro.
711 nil '(name arg1 . arg-rest) define-pmacro)
713 ; The order here is important.
714 (arch-init!) ; Must be done first.
733 ; Install any builtin objects.
734 ; This is deferred until define-arch is read.
735 ; One reason is that attributes MACH and ISA don't exist until then.
737 (define (reader-install-builtin!)
738 ; The order here is important.
744 ; This is mainly for the insn attributes.
750 ; Do anything necessary for the application independent parts after parsing
752 ; The lists get cons'd in reverse order. One thing this does is change them
753 ; back to file order, it makes things easier for the human viewer.
755 (define (-finish-parse-cpu!)
756 ; The order here is generally the reverse of init-parse-cpu!.
769 (arch-finish!) ; Must be done last.
774 ; Perform a global error checking pass after the .cpu file has been read in.
776 (define (-global-error-checks)
779 ; - all hardware elements with same name must have same rank and
780 ; compatible modes (which for now means same float mode or all int modes)
784 ; .cpu file include mechanism
786 (define (include file)
787 (logit 1 "Including file " (string-append arch-path "/" file) " ...\n")
788 (reader-read-file! (string-append arch-path "/" file))
789 (logit 2 "Resuming previous file ...\n")
792 ; Version of `if' invokable at the top level of a description file.
793 ; This is a work-in-progress. Its presence in the description file is ok,
794 ; but the implementation will need to evolve.
796 (define (cmd-if test then . else)
797 (if (> (length else) 1)
798 (reader-error "wrong number of arguments to `if'"
799 (cons 'if (cons test (cons then else)))
802 (if (not (memq (car test) '(keep-isa? keep-mach? application-is?)))
803 (reader-error "only (if (keep-mach?|keep-isa?|application-is? ...) ...) are currently supported" test ""))
806 (if (keep-isa? (cadr test))
810 (eval1 (car else)))))
812 (if (keep-mach? (cadr test))
816 (eval1 (car else)))))
818 (if (eq? APPLICATION (cadr test))
822 (eval1 (car else))))))
825 ; Top level routine for loading .cpu files.
826 ; FILE is the name of the .cpu file to load.
827 ; KEEP-MACH is a string of comma separated machines to keep
828 ; (or not keep if prefixed with !).
829 ; KEEP-ISA is a string of comma separated isas to keep.
830 ; OPTIONS is the OPTIONS argument to -init-parse-cpu!.
831 ; APP-INITER! is an application specific zero argument proc (thunk)
832 ; to call after -init-parse-cpu!
833 ; APP-FINISHER! is an application specific zero argument proc to call after
835 ; ANALYZER! is a zero argument proc to call after loading the .cpu file.
836 ; It is expected to set up various tables and things useful for the application
839 ; This function isn't local because it's used by dev.scm.
841 (define (cpu-load file keep-mach keep-isa options
842 app-initer! app-finisher! analyzer!)
843 (-init-parse-cpu! keep-mach keep-isa options)
845 (logit 1 "Loading cpu description " file " ...\n")
846 (set! arch-path (dirname file))
847 (reader-read-file! file)
848 (logit 2 "Processing cpu description " file " ...\n")
851 (-global-error-checks)
856 ; Argument parsing utilities.
858 ; Generate a usage message.
859 ; ERRTYPE is one of 'help, 'unknown, 'missing.
860 ; OPTION is the option that had the error or "" if ERRTYPE is 'help.
862 (define (cgen-usage errtype option arguments)
863 (let ((cep (current-error-port)))
866 ((unknown) (display (string-append "Unknown option: " option "\n") cep))
867 ((missing) (display (string-append "Missing argument: " option "\n") cep))
868 (else (display "Unknown error!\n" cep)))
869 (display "Usage: cgen arguments ...\n" cep)
870 (for-each (lambda (arg)
871 (display (string-append (car arg)
872 " " (if (cadr arg) (cadr arg) "")
877 (display "...\n" cep)
880 ((unknown missing) (quit 1))
885 ; [We don't know where to find the real one until we've parsed the args,
886 ; and this isn't something we need to get too fancy about anyways.]
887 ; The result is always ((a . b) . c).
888 ; If the argument is valid, the result is ((opt-spec . arg) . remaining-argv),
889 ; or (('unknown . option) . remaining-argv) if `option' isn't recognized,
890 ; or (('missing . option) . remaining argv) if `option' is missing a required
892 ; or ((#f . #f) . #f) if there are no more arguments.
893 ; OPT-SPEC is a list of option specs.
894 ; Each element is an alist of at least 3 elements: option argument help-text.
895 ; `option' is a string or symbol naming the option. e.g. -a, --help, "-i".
896 ; symbols are supported for backward compatibility, -i is a complex number.
897 ; `argument' is a string naming the argument or #f if the option takes no
899 ; `help-text' is a string that is printed with the usage information.
900 ; Elements beyond `help-text' are ignored.
902 (define (-getopt argv opt-spec)
904 (cons (cons #f #f) #f)
905 (let ((opt (assoc (car argv) opt-spec)))
906 (cond ((not opt) (cons (cons 'unknown (car argv)) (cdr argv)))
907 ((and (cadr opt) (null? (cdr argv)))
908 (cons (cons 'missing (car argv)) (cdr argv)))
909 ((cadr opt) (cons (cons opt (cadr argv)) (cddr argv)))
910 (else ; must be option that doesn't take an argument
911 (cons (cons opt #f) (cdr argv))))))
914 ; Return (cadr args) or print a pretty error message if not possible.
916 (define (option-arg args)
917 (if (and (pair? args) (pair? (cdr args)))
919 (parse-error "option processing" "missing argument to" (car args)))
922 ; Record of arguments passed to debug-repl, so they can be accessed in
925 (define debug-env #f)
927 ; Return list of recorded variables for debugging.
929 (define (debug-var-names) (map car debug-env))
931 ; Return value of recorded var NAME.
933 (define (debug-var name) (assq-ref debug-env name))
935 ; A handle on /dev/tty, so we can be sure we're talking with the user.
936 ; We open this the first time we actually need it.
937 (define debug-tty #f)
939 ; Return the port we should use for interacting with the user,
940 ; opening it if necessary.
941 (define (debug-tty-port)
943 (set! debug-tty (open-file "/dev/tty" "r+")))
946 ; Enter a repl loop for debugging purposes.
947 ; Use (quit) to exit cgen completely.
948 ; Use (debug-quit) or (quit 0) to exit the debugging session and
949 ; resume argument processing.
951 ; ENV-ALIST can be anything, but it is intended to be an alist of values
952 ; the caller will want to be able to access in the repl loop.
953 ; It is stored in global `debug-env'.
955 ; FIXME: Move to utils.scm.
957 (define (debug-repl env-alist)
958 (with-input-and-output-to
961 (set! debug-env env-alist)
963 (let ((rc (top-repl)))
965 (quit 1)) ; indicate error to `make'
966 (if (not (equal? rc '(0)))
970 ; Utility for debug-repl.
973 ; Keep around for later debugging.
979 ; Macro to simplify calling debug-repl.
980 ; Usage: (debug-repl-env var-name1 var-name2 ...)
982 (defmacro debug-repl-env var-names
983 (let ((env (map (lambda (var-name)
984 (list 'cons (list 'quote var-name) var-name))
986 (list 'debug-repl (cons 'list env)))
989 ; List of common arguments.
991 ; ??? Another useful arg would be one that says "do file generation with
992 ; arguments specified up til now, then continue with next batch of args".
994 (define common-arguments
995 '(("-a" "arch-file" "specify path of .cpu file to load")
996 ("-b" #f "use debugging evaluator, for backtraces")
997 ("-d" #f "start interactive debugging session")
998 ("-f" "flags" "specify a set of flags to control code generation")
999 ("-h" #f "print usage information")
1000 ("--help" #f "print usage information")
1001 ("-i" "isa-list" "specify isa-list entries to keep")
1002 ("-m" "mach-list" "specify mach-list entries to keep")
1003 ("-s" "srcdir" "set srcdir")
1004 ("-v" #f "increment verbosity level")
1005 ("--version" #f "print version info")
1009 ; Default place to look.
1010 ; This gets overridden to point to the directory of the loaded .cpu file.
1011 ; ??? Ideally this would be local to this file.
1012 (define arch-path (string-append srcdir "/cpu"))
1014 ; Accessors for application option specs
1015 (define (opt-get-first-pass opt)
1016 (or (list-ref opt 3) (lambda args #f)))
1017 (define (opt-get-second-pass opt)
1018 (or (list-ref opt 4) (lambda args #f)))
1020 ; Parse options and call generators.
1021 ; ARGS is a #:keyword delimited list of arguments.
1023 ; #:arg-spec optspec ; FIXME: rename to #:opt-spec
1024 ; #:init init-routine
1025 ; #:finish finish-routine
1026 ; #:analyze analysis-routine
1027 ; #:argv command-line-arguments
1029 ; ARGSPEC is a list of (option option-arg comment option-handler) elements.
1030 ; OPTION-HANDLER is either (lambda () ...) or (lambda (arg) ...) and
1031 ; processes the option.
1035 (let ((app-name "unknown")
1037 (app-init! (lambda () #f))
1038 (app-finish! (lambda () #f))
1039 (app-analyze! (lambda () #f))
1040 (argv (list "cgen"))
1042 (let loop ((args args))
1043 (if (not (null? args))
1045 ((#:app-name) (begin
1046 (set! app-name (option-arg args))
1047 (loop (cddr args))))
1048 ((#:arg-spec) (begin
1049 (set! opt-spec (option-arg args))
1050 (loop (cddr args))))
1052 (set! app-init! (option-arg args))
1053 (loop (cddr args))))
1055 (set! app-finish! (option-arg args))
1056 (loop (cddr args))))
1058 (set! app-analyze! (option-arg args))
1059 (loop (cddr args))))
1061 (set! argv (option-arg args))
1062 (loop (cddr args))))
1063 (else (error "cgen: unknown argument" (car args))))))
1065 ; ARGS has been processed, now we can process ARGV.
1068 (opt-spec (append common-arguments opt-spec))
1069 (app-args nil) ; application's args are queued here
1072 (keep-mach "all") ; default is all machs
1073 (keep-isa "all") ; default is all isas
1076 (debugging #f) ; default is off, for speed
1077 (cep (current-error-port))
1081 (let loop ((argv (cdr argv)))
1082 (let* ((new-argv (-getopt argv opt-spec))
1083 (opt (caar new-argv))
1084 (arg (cdar new-argv)))
1086 ((#f) (set! moreopts? #f))
1087 ((unknown) (cgen-usage 'unknown arg opt-spec))
1088 ((missing) (cgen-usage 'missing arg opt-spec))
1090 (cond ((str=? "-a" (car opt))
1091 (set! arch-file arg)
1093 ((str=? "-b" (car opt))
1096 ((str=? "-d" (car opt))
1097 (let ((prompt (string-append "cgen-" app-name "> ")))
1099 (set-repl-prompt! prompt)
1100 (if (feature? 'readline)
1101 (set-readline-prompt! prompt))
1103 ((str=? "-f" (car opt))
1106 ((str=? "-h" (car opt))
1107 (cgen-usage 'help "" opt-spec)
1109 ((str=? "--help" (car opt))
1110 (cgen-usage 'help "" opt-spec)
1112 ((str=? "-i" (car opt))
1115 ((str=? "-m" (car opt))
1116 (set! keep-mach arg)
1118 ((str=? "-s" (car opt))
1119 #f ; ignore, already processed by caller
1121 ((str=? "-v" (car opt))
1124 ((str=? "--version" (car opt))
1126 (display "Cpu tools GENerator version ")
1127 (display (cgen-major))
1129 (display (cgen-minor))
1131 (display (cgen-fixlevel))
1135 ; Else this is an application specific option.
1137 ; Record it for later processing. Note that they're
1138 ; recorded in reverse order (easier). This is undone
1140 (set! app-args (acons opt arg app-args)))
1142 (if moreopts? (loop (cdr new-argv)))
1146 ; All arguments have been parsed.
1148 (cgen-call-with-debugging
1153 (error "-a option missing, no architecture specified"))
1158 keep-mach keep-isa flags
1159 app-init! app-finish! app-analyze!)
1161 ;; Start another repl loop if -d.
1162 ;; Awkward. Both places are useful, though this is more useful.
1166 ;; Done with processing the arguments. Application arguments
1167 ;; are processed in two passes. This is because the app may
1168 ;; have arguments that specify things that affect file
1169 ;; generation (e.g. to specify another input file) and we
1170 ;; don't want to require an ordering of the options.
1171 (for-each (lambda (opt-arg)
1172 (let ((opt (car opt-arg))
1173 (arg (cdr opt-arg)))
1175 ((opt-get-first-pass opt) arg)
1176 ((opt-get-first-pass opt)))))
1179 (for-each (lambda (opt-arg)
1180 (let ((opt (car opt-arg))
1181 (arg (cdr opt-arg)))
1183 ((opt-get-second-pass opt) arg)
1184 ((opt-get-second-pass opt)))))
1185 (reverse app-args))))
1191 ; Main entry point called by application file generators.
1195 (cgen-debugging-stack-start -cgen args))