OSDN Git Service

* cpu/mep.opc (mep_cgen_insn_supported_asm): Change the test to a
[pf3gnuchains/pf3gnuchains3x.git] / cgen / read.scm
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.
5
6 ; This file [and its subordinates] contain no C code (well, as little as
7 ; possible).  That lives at a layer above us.
8
9 ; A .cpu file consists of several sections:
10 ;
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)
14 ; - attributes
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
21
22 ; TODO:
23 ; - memory access, layout, etc.
24 ; - floating point quirks
25 ; - ability to describe an ABI
26 ; - anything else that comes along
27
28 ; Notes:
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
31 ;   entry
32
33 ; Guidelines:
34 ; - Try to conform to R5RS, try to limit guile-ness.
35 ;   The current code is undoubtedly off in many places.
36
37 ; Conventions:
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.
42 ;
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.
62 \f
63 ; Variables representing misc. global constants.
64
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))
71
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))
79
80 ;; Utilities for testing the rtl version.
81 (define (rtl-version-equal? major minor)
82   (equal? (cgen-rtl-version) (list major minor))
83 )
84 (define (rtl-version-at-least? major minor)
85   (let ((rmajor (cgen-rtl-major))
86         (rminor (cgen-rtl-major)))
87     (or (> rmajor major)
88         (and (= rmajor major)
89              (>= rminor minor))))
90 )
91 (define (rtl-version-older? major minor)
92   (not (rtl-version-at-least? major minor))
93 )
94
95 ;; List of supported versions
96 (define /supported-rtl-versions '((0 7) (0 8)))
97
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))
103
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))
109 )
110
111 ; Which application is in use (UNKNOWN, DESC, OPCODES, SIMULATOR, ???).
112 ; This is mostly for descriptive purposes.
113 (define APPLICATION 'UNKNOWN)
114 \f
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
118 ; file to another.
119
120 ; If a routine to initialize compiled-in code is defined, run it.
121 (if (defined? 'cgen-init-c) (cgen-init-c))
122
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))
127
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))
136 )
137
138 ; List of loaded files.
139
140 (if (not (defined? '/loaded-file-list))
141     (define /loaded-file-list '()))
142
143 ; Return non-zero if FILE was loaded last time through.
144
145 (define (/loaded-file? file)
146   (->bool (memq (string->symbol file) /loaded-file-list))
147 )
148
149 ; Record FILE as compiled in.
150
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))))
155 )
156
157 ; Load FILE if SYM is not compiled in.
158
159 (define (maybe-load file init-func sym)
160   ; Return non-#f if FUNC is present in DYNOBJ.
161   (define (dynamic-func? func dynobj)
162     (catch #t
163            (lambda () (dynamic-func func dynobj))
164            (lambda args #f))
165     )
166
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?)
173                (not (defined? sym))
174                (/loaded-file? file))
175            (/loaded-file-record! file)
176            (load file))
177           (else
178            (display (string-append "Skipping " file ", already loaded.\n")))))
179 )
180
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)
214 \f
215 ; Reader state data.
216 ; All state regarding the reading of a .cpu file is kept in an object of
217 ; class <reader>.
218
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-*.
222
223 (define <command>
224   (class-make '<command>
225               '(<ident>)
226               '(
227                 ; argument spec to `lambda'
228                 arg-spec
229                 ; lambda that processes the entry
230                 handler
231                 )
232               nil)
233 )
234
235 (define command-arg-spec (elm-make-getter <command> 'arg-spec))
236 (define command-handler (elm-make-getter <command> 'handler))
237
238 ; Return help text for COMMAND.
239
240 (define (command-help cmd)
241   (string-append
242    (obj:comment cmd)
243    "Arguments: "
244    (with-output-to-string (lambda () (write (command-arg-spec cmd))))
245    "\n")
246 )
247
248 ; A pair of two lists: machs to keep, machs to drop.
249 ; The default is "keep all machs", "drop none".
250
251 (define /keep-all-machs '((all)))
252
253 ; Main reader state class.
254
255 (define <reader>
256   (class-make '<reader>
257               nil
258               (list
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
266                ; processed.
267                (cons 'keep-mach /keep-all-machs)
268
269                ; Selected isas to keep or `all'.
270                '(keep-isa . (all))
271
272                ; Boolean indicating if command tracing is on.
273                (cons 'trace-commands? #f)
274
275                ; Boolean indicating if pmacro tracing is on.
276                (cons 'trace-pmacros? #f)
277
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.
281                'current-cpu
282
283                ; Associative list of file entry commands
284                ; (e.g. define-insn, etc.).
285                ; Each entry is (name . command-object).
286                (cons 'commands nil)
287
288                ; The current source location.
289                ; This is recorded here by the higher level reader and is
290                ; fetched by commands as necessary.
291                'location
292                )
293               nil)
294 )
295
296 ; Accessors.
297
298 (define-getters <reader> reader
299   (keep-mach keep-isa
300    trace-commands? trace-pmacros?
301    current-cpu commands location))
302 (define-setters <reader> reader
303   (keep-mach keep-isa
304    trace-commands? trace-pmacros?
305    current-cpu commands location))
306
307 (define (reader-add-command! name comment attrs arg-spec handler)
308   (reader-set-commands! CURRENT-READER
309                         (acons name
310                                (make <command> name comment attrs
311                                      arg-spec handler)
312                                (reader-commands CURRENT-READER)))
313 )
314
315 (define (/reader-lookup-command name)
316   (assq-ref (reader-commands CURRENT-READER) name)
317 )
318
319 ; Reader state for current .cpu file.
320
321 (define CURRENT-READER #f)
322
323 ; Return the current source location in readable form.
324 ; FIXME: Currently unused, keep for reference for awhile.
325
326 (define (/readable-current-location)
327   (let ((loc (current-reader-location)))
328     (if loc
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))
333                             "<input>")
334                         ":"
335                         (number->string (port-line (current-input-port)))
336                         ":")))
337 )
338
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.
345
346 (define (parse-error context message expr . maybe-help-text)
347   (if (not context)
348       (set! context (make <context> (current-reader-location) #f)))
349
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)))
355
356     (error
357      (simple-format
358       #f
359       "\n~A:\n@ ~A:\n\n~A: ~A: ~S~A"
360       intro
361       (location->string loc)
362       (single-location->simple-string top-sloc)
363       text
364       expr
365       (if (null? maybe-help-text)
366           ""
367           (string-append "\n\n" (car maybe-help-text))))))
368 )
369
370 ; Return the current source location.
371 ;
372 ; If CURRENT-READER is uninitialized, return "unspecified" location.
373 ; This is done so that things like define-pmacro work in interactive mode.
374
375 (define (current-reader-location)
376   (if CURRENT-READER
377       (reader-location CURRENT-READER)
378       (unspecified-location))
379 )
380
381 ; Process a macro-expanded entry.
382
383 (define (/reader-process-expanded-1! entry)
384   (let ((location (location-property entry)))
385
386     ;; Set the current source location for better diagnostics.
387     ;; Access with current-reader-location.
388     (reader-set-location! CURRENT-READER location)
389
390     (if (reader-trace-commands? CURRENT-READER)
391         (message "Processing command:\n  @ "
392                  (if location (location->string location) "location unknown")
393                  "\n"
394                  (with-output-to-string (lambda () (pretty-print entry)))))
395
396     (let ((command (/reader-lookup-command (car entry)))
397           (context (make-current-context #f)))
398
399       (if command
400
401           (let* ((handler (command-handler command))
402                  (arg-spec (command-arg-spec command))
403                  (num-args (num-args arg-spec)))
404             (if (cdr num-args)
405                 ;; Variable number of trailing arguments.
406                 (if (< (length (cdr entry)) (car num-args))
407                     (parse-error context
408                                  (string-append "Incorrect number of arguments to "
409                                                 (symbol->string (car entry))
410                                                 ", expecting at least "
411                                                 (number->string (car num-args)))
412                                  entry
413                                  (command-help command))
414                     (apply handler (cdr entry)))
415                 ;; Fixed number of arguments.
416                 (if (!= (length (cdr entry)) (car num-args))
417                     (parse-error context
418                                  (string-append "Incorrect number of arguments to "
419                                                 (symbol->string (car entry))
420                                                 ", expecting "
421                                                 (number->string (car num-args)))
422                                  entry
423                                  (command-help command))
424                     (apply handler (cdr entry)))))
425
426           (parse-error context "unknown entry type" entry))))
427
428   *UNSPECIFIED*
429 )
430
431 ;; Process 1 or more macro-expanded entries.
432 ;; ENTRY is expected to have a location-property object property.
433
434 ;; NOTE: This is "public" so the .eval pmacro can use it.
435 ;; This is also used by /cmd-if.
436
437 (define (reader-process-expanded! entry)
438   ;; () is used to indicate a no-op
439   (cond ((null? entry)
440          #f) ;; nothing to do
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!
447                    (cdr entry)))
448         (else
449          (/reader-process-expanded-1! entry)))
450
451   *UNSPECIFIED*
452 )
453
454 ; Process file entry ENTRY.
455 ; LOC is a <location> object for ENTRY.
456
457 (define (/reader-process! entry loc)
458   (if (not (form? entry))
459       (parse-error loc "improperly formed entry" entry))
460
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))
471
472   *UNSPECIFIED*
473 )
474
475 ; Read in and process FILE.
476 ;
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.
480
481 (define (reader-read-file! file)
482   (let ((readit (lambda ()
483                   (let loop ((entry (read)))
484                     (if (eof-object? entry)
485                         #t ; done
486                         (begin
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))
494                           (loop (read)))))))
495         )
496
497     (with-input-from-file file readit))
498
499   *UNSPECIFIED*
500 )
501 \f
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
504 ; simultaneously.
505 ; Class <arch> is defined in mach.scm.
506
507 ; Global containing all data of the currently selected architecture.
508
509 (define CURRENT-ARCH #f)
510 \f
511 ; `keep-mach' processing.
512
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.
517
518 (define (current-cpu) (force (reader-current-cpu CURRENT-READER)))
519
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.
523
524 (define (keep-cpu? cpu-name) #t)
525
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 !).
529
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) #\!)))
533                      mach-name-list))
534          (drop (map (lambda (name) (string->symbol (string-drop 1 name)))
535                     (find (lambda (name) (char=? (string-ref name 0) #\!))
536                           mach-name-list))))
537     (reader-set-keep-mach! CURRENT-READER
538                            (cons (map string->symbol keep)
539                                  (map string->symbol drop)))
540     ; Reset current-cpu.
541     (reader-set-current-cpu!
542      CURRENT-READER
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)))))
551                                        selected-machs)))
552                   (error "machs from different cpu families selected"))
553               (mach-cpu (car selected-machs)))))
554
555     *UNSPECIFIED*)
556 )
557
558 ; Validate the user-provided keep-mach list against the list of machs
559 ; specified in the .cpu file (in define-arch).
560
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)))
567               (car keep-mach))
568     (for-each (lambda (mach)
569                 (if (not (memq mach mach-names))
570                     (error "unknown mach to drop:" mach)))
571               (cdr keep-mach))
572     )
573   *UNSPECIFIED*
574 )
575
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.
580
581 (define (keep-mach? mach-list)
582   (if (null? mach-list)
583       #t
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))))
593                         keep? drop?))))
594 )
595
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).
601
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)))
608     (if (null? machs)
609         #t
610         (keep-mach? (bitset-attr->list machs))))
611 )
612
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.
616
617 (define (keep-atlist? atlist obj)
618   (and (keep-mach-atlist? atlist obj)
619        (keep-isa-atlist? atlist obj))
620 )
621
622 ; Return a boolean indicating if multiple cpu families are being kept.
623
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)))))
631                          selected-machs))))
632 )
633
634 ; Return a boolean indicating if everything is kept.
635
636 (define (keep-all?)
637   (equal? (reader-keep-mach CURRENT-READER) /keep-all-machs)
638 )
639
640 ; Ensure all cpu families were kept, necessary for generating files that
641 ; encompass the entire architecture.
642
643 (define (assert-keep-all)
644   (if (not (keep-all?))
645       (error "no can do, all cpu families not selected"))
646   *UNSPECIFIED*
647 )
648
649 ; Ensure exactly one cpu family was kept, necessary for generating files that
650 ; are specific to one cpu family.
651
652 (define (assert-keep-one)
653   (if (keep-multiple?)
654       (error "no can do, multiple cpu families selected"))
655   *UNSPECIFIED*
656 )
657 \f
658 ; `keep-isa' processing.
659
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.
666
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)
670     )
671   *UNSPECIFIED*
672 )
673
674 ; Validate the user-provided keep-isa list against the list of isas
675 ; specified in the .cpu file (in define-arch).
676
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)))
683               keep-isa)
684     )
685   *UNSPECIFIED*
686 )
687
688 ; Return currently selected isa (there must be exactly one).
689
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)
695               (car isas)
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))))
700 )
701
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
705 ; isa.
706
707 (define (keep-isa? isa-list)
708   (if (null? 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)
712                        (or (memq i keep)
713                            (memq 'all keep)))
714                      isa-list)))
715     (any-true? keep?))
716 )
717
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.
723
724 (define (keep-isa-atlist? atlist obj)
725   (let ((isas (atlist-attr-value atlist 'ISA obj)))
726     (keep-isa? (bitset-attr->list isas)))
727 )
728
729 ; Return non-#f if object OBJ is to be kept, according to its ISA attribute.
730
731 (define (keep-isa-obj? obj)
732   (keep-isa-atlist? (obj-atlist obj) obj)
733 )
734
735 ; Return a boolean indicating if multiple isas are being kept.
736
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))))
742 )
743
744 ; Return list of isa names currently being kept.
745
746 (define (current-keep-isa-name-list)
747   (reader-keep-isa CURRENT-READER)
748 )
749 \f
750 ;; Tracing support.
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
753 ;; level).
754
755 ;;; Enable the specified tracing.
756 ;;; TRACE-OPTIONS is a comma-separated list of things to trace.
757 ;;;
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
762 ;;;
763 ;;; [If we later need to support disabling some tracing, one way is to
764 ;;; recognize an "-" in front of an option.]
765
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)
777                      #t) ;; handled above
778                     (else
779                      (cgen-usage 'unknown (string-append "-t " item)
780                                  common-arguments))))
781               requests))
782
783   *UNSPECIFIED*
784 )
785 \f
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
790 ; checked too.
791 ; ??? Unimplemented.
792
793 (define option:reserved-as-opcode? #f)
794
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!.
798
799 (define (set-cgen-options! options)
800   (option-init!)
801   (for-each (lambda (opt)
802               (if (null? opt)
803                   #t ; ignore extraneous spaces
804                   (let ((name (string->symbol (car opt)))
805                         (value (cdr 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)))
811 )
812 \f
813 ; Application specific object creation support.
814 ;
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.
822
823 (define /cpu-new-class-list nil)
824
825 (define (set-for-new! parent child)
826   (set! /cpu-new-class-list (acons parent child /cpu-new-class-list))
827 )
828
829 ; Lookup the class registered with set-for-new!
830 ; If none registered, return PARENT.
831
832 (define (lookup-for-new parent)
833   (let ((child (assq-ref /cpu-new-class-list parent)))
834     (if child
835         child
836         parent))
837 )
838 \f
839 ; .cpu file loader support
840
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.
845
846 (define (/init-reader!)
847   (set! CURRENT-READER (new <reader>))
848
849   (reader-add-command! 'define-rtl-version
850                        "Specify the RTL version being used.\n"
851                        nil '(major minor) /cmd-define-rtl-version)
852
853   (reader-add-command! 'include
854                        "Include a file.\n"
855                        nil '(file) /cmd-include)
856   (reader-add-command! 'if
857                        "(if test then . else)\n"
858                        nil '(test then . else) /cmd-if)
859
860   ; Rather than add cgen-internal specific stuff to pmacros.scm, we create
861   ; the pmacro commands here.
862   (pmacros-init!)
863   (reader-add-command! 'define-pmacro
864                        "\
865 Define a preprocessor-style macro.
866 "
867                        nil '(name arg1 . arg-rest) define-pmacro)
868
869   *UNSPECIFIED*
870 )
871
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.
878
879 (define (/init-parse-cpu! keep-mach keep-isa options)
880   (set! /cpu-new-class-list nil)
881
882   (set! CURRENT-ARCH (new <arch>))
883   (/keep-mach-set! keep-mach)
884   (/keep-isa-set! keep-isa)
885   (set-cgen-options! options)
886
887   ; The order here is important.
888   (arch-init!) ; Must be done first.
889   (enum-init!)
890   (attr-init!)
891   (types-init!)
892   (mach-init!)
893   (model-init!)
894   (mode-init!)
895   (ifield-init!)
896   (hardware-init!)
897   (operand-init!)
898   (insn-init!)
899   (minsn-init!)
900   (rtl-init!)
901   (rtl-c-init!)
902   (utils-init!)
903
904   *UNSPECIFIED*
905 )
906
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.
910
911 (define (reader-install-builtin!)
912   ; The order here is important.
913   (attr-builtin!)
914   (mode-builtin!)
915   (ifield-builtin!)
916   (hardware-builtin!)
917   (operand-builtin!)
918   ; This is mainly for the insn attributes.
919   (insn-builtin!)
920   (rtl-builtin!)
921   *UNSPECIFIED*
922 )
923
924 ; Do anything necessary for the application independent parts after parsing
925 ; a .cpu file.
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.
928
929 (define (/finish-parse-cpu!)
930   ; The order here is generally the reverse of init-parse-cpu!.
931   (rtl-finish!)
932   (minsn-finish!)
933   (insn-finish!)
934   (operand-finish!)
935   (hardware-finish!)
936   (ifield-finish!)
937   (mode-finish!)
938   (model-finish!)
939   (mach-finish!)
940   (types-finish!)
941   (attr-finish!)
942   (enum-finish!)
943   (arch-finish!) ; Must be done last.
944
945   *UNSPECIFIED*
946 )
947
948 ; Perform a global error checking pass after the .cpu file has been read in.
949
950 (define (/global-error-checks)
951   ; ??? None yet.
952   ; TODO:
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)
955   #f
956 )
957
958 ; .cpu file include mechanism
959
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")
964 )
965
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.
969
970 (define (/cmd-if test then . else)
971   (if (> (length else) 1)
972       (parse-error #f
973                    "wrong number of arguments to `if'"
974                    (cons 'if (cons test (cons then else)))))
975   ; ??? rtx-eval test
976   (if (or (not (pair? test))
977           (not (memq (car test) '(keep-isa? keep-mach? application-is?))))
978       (parse-error #f
979                    "only (if (keep-mach?|keep-isa?|application-is? ...) ...) are currently supported"
980                    test))
981   (case (car test)
982     ((keep-isa?)
983      (if (keep-isa? (cadr test))
984          (reader-process-expanded! then)
985          (if (null? else)
986              #f
987              (reader-process-expanded! (car else)))))
988     ((keep-mach?)
989      (if (keep-mach? (cadr test))
990          (reader-process-expanded! then)
991          (if (null? else)
992              #f
993              (reader-process-expanded! (car else)))))
994     ((application-is?)
995      (if (eq? APPLICATION (cadr test))
996          (reader-process-expanded! then)
997          (if (null? else)
998              #f
999              (reader-process-expanded! (car else))))))
1000 )
1001
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
1015 ; in question.
1016 ;
1017 ; This function isn't local because it's used by dev.scm.
1018
1019 (define (cpu-load file keep-mach keep-isa options trace-options
1020                   app-initer! app-finisher! analyzer!)
1021   (/init-reader!)
1022   (/init-parse-cpu! keep-mach keep-isa options)
1023   (/set-trace-options! trace-options)
1024   (app-initer!)
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!)
1030   (app-finisher!)
1031   (/global-error-checks)
1032   (analyzer!)
1033   *UNSPECIFIED*
1034 )
1035 \f
1036 ; Argument parsing utilities.
1037
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.
1041
1042 (define (cgen-usage errtype option arguments)
1043   (let ((cep (current-error-port)))
1044     (case errtype
1045       ((help) #f)
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)
1056                                 arg-str))
1057                           "  - " (caddr arg)
1058                           (apply string-append
1059                                  (map (lambda (text)
1060                                         (string-append "\n"
1061                                                        (string-take 20 "")
1062                                                        text))
1063                                       (cdddr arg)))
1064                           "\n")
1065                          cep))
1066               arguments)
1067     (display "...\n" cep)
1068     (case errtype
1069       ((help) (quit 0))
1070       ((unknown missing) (quit 1))
1071       (else (quit 2))))
1072 )
1073
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
1081 ; argument,
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
1088 ; arguments.
1089 ; `help-text' is a string that is printed with the usage information.
1090 ; Elements beyond `help-text' are ignored.
1091
1092 (define (/getopt argv opt-spec)
1093   (if (null? argv)
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))))))
1102 )
1103
1104 ; Return (cadr args) or print a pretty error message if not possible.
1105
1106 (define (option-arg args)
1107   (if (and (pair? args) (pair? (cdr args)))
1108       (cadr args)
1109       (parse-error (make-prefix-context "option processing")
1110                    "missing argument to"
1111                    (car args)))
1112 )
1113
1114 ; List of common arguments.
1115 ;
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".
1118
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"
1130                        "Options:"
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")
1136     )
1137 )
1138
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.
1142
1143 (define arch-path (string-append srcdir "/cpu"))
1144
1145 ; Accessors for application option specs
1146
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)))
1151
1152 ; Parse options and call generators.
1153 ; ARGS is a #:keyword delimited list of arguments.
1154 ; #:app-name name
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
1160 ;
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.
1164
1165 (define /cgen
1166   (lambda args
1167     (let ((app-name "unknown")
1168           (opt-spec nil)
1169           (app-init! (lambda () #f))
1170           (app-finish! (lambda () #f))
1171           (app-analyze! (lambda () #f))
1172           (argv (list "cgen"))
1173           )
1174       (let loop ((args args))
1175         (if (not (null? args))
1176             (case (car 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))))
1183               ((#:init) (begin
1184                           (set! app-init! (option-arg args))
1185                           (loop (cddr args))))
1186               ((#:finish) (begin
1187                             (set! app-finish! (option-arg args))
1188                             (loop (cddr args))))
1189               ((#:analyze) (begin
1190                              (set! app-analyze! (option-arg args))
1191                              (loop (cddr args))))
1192               ((#:argv) (begin
1193                           (set! argv (option-arg args))
1194                           (loop (cddr args))))
1195               (else (error "cgen: unknown argument" (car args))))))
1196
1197       ; ARGS has been processed, now we can process ARGV.
1198
1199       (let (
1200             (opt-spec (append common-arguments opt-spec))
1201             (app-args nil)    ; application's args are queued here
1202             (repl? #f)
1203             (arch-file #f)
1204             (keep-mach "all") ; default is all machs
1205             (keep-isa "all")  ; default is all isas
1206             (flags "")
1207             (moreopts? #t)
1208             (debugging #f)    ; default is off, for speed
1209             (trace-options "")
1210             (cep (current-error-port))
1211             (str=? string=?)
1212             )
1213
1214         (let loop ((argv (cdr argv)))
1215           (let* ((new-argv (/getopt argv opt-spec))
1216                  (opt (caar new-argv))
1217                  (arg (cdar new-argv)))
1218             (case opt
1219               ((#f) (set! moreopts? #f))
1220               ((unknown) (cgen-usage 'unknown arg opt-spec))
1221               ((missing) (cgen-usage 'missing arg opt-spec))
1222               (else
1223                (cond ((str=? "-a" (car opt))
1224                       (set! arch-file arg)
1225                       )
1226                      ((str=? "-b" (car opt))
1227                       (set! debugging #t)
1228                       )
1229                      ((str=? "-d" (car opt))
1230                       (let ((prompt (string-append "cgen-" app-name "> ")))
1231                         (set! repl? #t)
1232                         (set-repl-prompt! prompt)
1233                         (if (feature? 'readline)
1234                             (set-readline-prompt! prompt))
1235                         ))
1236                      ((str=? "-f" (car opt))
1237                       (set! flags arg)
1238                       )
1239                      ((str=? "-h" (car opt))
1240                       (cgen-usage 'help "" opt-spec)
1241                       )
1242                      ((str=? "--help" (car opt))
1243                       (cgen-usage 'help "" opt-spec)
1244                       )
1245                      ((str=? "-i" (car opt))
1246                       (set! keep-isa arg)
1247                       )
1248                      ((str=? "-m" (car opt))
1249                       (set! keep-mach arg)
1250                       )
1251                      ((str=? "-s" (car opt))
1252                       #f ; ignore, already processed by caller
1253                       )
1254                      ((str=? "-t" (car opt))
1255                       (set! trace-options arg)
1256                       )
1257                      ((str=? "-v" (car opt))
1258                       (verbose-inc!)
1259                       )
1260                      ((str=? "--version" (car opt))
1261                       (begin
1262                         (display "Cpu tools GENerator version ")
1263                         (display (cgen-major))
1264                         (display ".")
1265                         (display (cgen-minor))
1266                         (display ".")
1267                         (display (cgen-fixlevel))
1268                         (newline)
1269                         (display "RTL version ")
1270                         (display (cgen-rtl-major))
1271                         (display ".")
1272                         (display (cgen-rtl-minor))
1273                         (newline)
1274                         (quit 0)
1275                         ))
1276                      ; Else this is an application specific option.
1277                      (else
1278                       ; Record it for later processing.  Note that they're
1279                       ; recorded in reverse order (easier).  This is undone
1280                       ; later.
1281                       (set! app-args (acons opt arg app-args)))
1282                      )))
1283             (if moreopts? (loop (cdr new-argv)))
1284             )
1285           ) ; end of loop
1286
1287         ; All arguments have been parsed.
1288
1289         (cgen-call-with-debugging
1290          debugging
1291          (lambda ()
1292
1293            (if (not arch-file)
1294                (error "-a option missing, no architecture specified"))
1295
1296            (if repl?
1297                (debug-repl nil))
1298
1299            (cpu-load arch-file
1300                      keep-mach keep-isa flags trace-options
1301                      app-init! app-finish! app-analyze!)
1302
1303            ;; Start another repl loop if -d.
1304            ;; Awkward.  Both places are useful, though this is more useful.
1305            (if repl?
1306                (debug-repl nil))
1307
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)))
1316                          (if (cadr opt)
1317                              ((opt-get-first-pass opt) arg)
1318                              ((opt-get-first-pass opt)))))
1319                      (reverse app-args))
1320
1321            (for-each (lambda (opt-arg)
1322                        (let ((opt (car opt-arg))
1323                              (arg (cdr opt-arg)))
1324                          (if (cadr opt)
1325                              ((opt-get-second-pass opt) arg)
1326                              ((opt-get-second-pass opt)))))
1327                      (reverse app-args))))
1328         )
1329       )
1330     #f) ; end of lambda
1331 )
1332
1333 ; Main entry point called by application file generators.
1334
1335 (define cgen
1336   (lambda args
1337     (cgen-debugging-stack-start /cgen args))
1338 )