2 ; Copyright (C) 2000, 2009 Red Hat, Inc.
3 ; This file is part of CGEN.
4 ; See file COPYING.CGEN for details.
6 ; Enums having attribute PREFIX have their symbols prepended with
7 ; the enum class' name.
8 ; Member PREFIX is always prepended to the symbol names.
10 ; Enum values are looked up with `enum-lookup-val'. The value to search for
11 ; has PREFIX prepended.
13 ; Enums always have mode INT.
22 ; FIXME: this make! method is required by <insn-enum> for some reason.
26 (lambda (self name comment attrs prefix vals)
27 (elm-set! self 'name name)
28 (elm-set! self 'comment comment)
29 (elm-set! self 'attrs attrs)
30 (elm-set! self 'prefix prefix)
31 (elm-set! self 'vals vals)
35 (define enum-prefix (elm-make-getter <enum> 'prefix))
37 (method-make! <enum> 'enum-values (lambda (self) (elm-get self 'vals)))
39 ; Parse a list of enum name/value entries.
40 ; PREFIX is prepended to each name.
41 ; Elements are any of: symbol, (symbol), (symbol value)
42 ; (symbol - attrs), (symbol value attrs), (symbol - attrs comment),
43 ; (symbol value attrs comment).
44 ; The - or #f means "use the next value".
45 ; SYMBOL may be - which means "skip this value".
46 ; The result is the same list, except values are filled in where missing,
47 ; and each symbol is prepended with `prefix'.
49 (define (parse-enum-vals context prefix vals)
50 ; Scan the value list, building up RESULT as we go.
51 ; Each element's value is 1+ the previous, unless there's an explicit value.
52 (let loop ((result nil) (last -1) (remaining vals))
56 ; Compute the numeric value the next entry will have.
57 ((val (if (and (pair? (car remaining))
58 (not (null? (cdar remaining))))
59 (if (eq? '- (cadar remaining))
63 (if (eq? (car remaining) '-)
64 (loop result val (cdr remaining))
65 (let ((name (symbolstr-append prefix
66 (if (pair? (car remaining))
69 (attrs (if (and (pair? (car remaining))
70 (pair? (cdar remaining))
71 (pair? (cddar remaining)))
74 (comment (if (and (pair? (car remaining))
75 (pair? (cdar remaining))
76 (pair? (cddar remaining))
77 (pair? (cdddar remaining)))
78 (car (cdddar remaining))
80 (loop (cons (list name val attrs comment) result)
85 ; Accessors for the various elements of an enum val.
87 (define (enum-val-name ev) (list-ref ev 0))
88 (define (enum-val-value ev) (list-ref ev 1))
89 (define (enum-val-attrs ev) (list-ref ev 2))
90 (define (enum-val-comment ev) (list-ref ev 3))
92 ; Convert the names in the result of parse-enum-vals to uppercase.
94 (define (enum-vals-upcase vals)
96 (cons (symbol-upcase (car elm)) (cdr elm)))
100 ; Parse an enum definition.
102 ; Utility of /enum-parse to parse the prefix.
104 (define (/enum-parse-prefix context prefix)
106 (set! prefix (symbol->string prefix)))
108 (if (not (string? prefix))
109 (parse-error context "prefix is not a string" prefix))
111 ; Prefix must not contain lowercase chars (enforced style rule, sue me).
112 (if (any-true? (map char-lower-case? (string->list prefix)))
113 (parse-error context "prefix must be uppercase" prefix))
118 ; This is the main routine for building an enum object from a
119 ; description in the .cpu file.
120 ; All arguments are in raw (non-evaluated) form.
122 (define (/enum-parse context name comment attrs prefix vals)
123 (logit 2 "Processing enum " name " ...\n")
125 ;; Pick out name first to augment the error context.
126 (let* ((name (parse-name context name))
127 (context (context-append-name context name)))
131 (parse-comment context comment)
132 (atlist-parse context attrs "enum")
133 (/enum-parse-prefix context prefix)
134 (parse-enum-vals context prefix vals)))
137 ; Read an enum description
138 ; This is the main routine for analyzing enums in the .cpu file.
139 ; CONTEXT is a <context> object for error messages.
140 ; ARG-LIST is an associative list of field name and field value.
141 ; /enum-parse is invoked to create the `enum' object.
143 (define (/enum-read context . arg-list)
152 ; Loop over each element in ARG-LIST, recording what's found.
153 (let loop ((arg-list arg-list))
156 (let ((arg (car arg-list))
157 (elm-name (caar arg-list)))
159 ((name) (set! name (cadr arg)))
160 ((comment) (set! comment (cadr arg)))
161 ((attrs) (set! attrs (cdr arg)))
162 ((prefix) (set! prefix (cadr arg)))
163 ((values) (set! values (cadr arg)))
164 (else (parse-error context "invalid enum arg" arg)))
165 (loop (cdr arg-list)))))
167 ; Now that we've identified the elements, build the object.
168 (/enum-parse context name comment attrs prefix values))
171 ; Define an enum object, name/value pair list version.
175 (let ((e (apply /enum-read (cons (make-current-context "define-enum")
177 (current-enum-add! e)
181 ; Define an enum object, all arguments specified.
183 (define (define-full-enum name comment attrs prefix vals)
184 (let ((e (/enum-parse (make-current-context "define-full-enum")
185 name comment attrs prefix vals)))
186 (current-enum-add! e)
190 ; Lookup SYM in all recorded enums.
191 ; The result is (value . enum-obj) or #f if not found.
193 (define (enum-lookup-val name)
194 (let loop ((elist (current-enum-list)))
197 (let ((e (assq name (send (car elist) 'enum-values))))
198 ;(display e) (newline)
201 ; sanity check, ensure the enum has a value
202 (if (null? (cdr e)) (error "enum-lookup-val: enum missing value: " (car e)))
203 (cons (cadr e) (car elist)))
210 ; Enums support code.
212 ; Return #t if VALS is a sequential list of enum values.
213 ; VALS is a list of enums. e.g. ((sym1) (sym2 3) (sym3 - attr1 (attr2 4)))
214 ; FIXME: Doesn't handle gaps in specified values.
215 ; e.g. (sym1 val1) sym2 (sym3 val3)
217 (define (enum-sequential? vals)
218 (let loop ((last -1) (remaining vals))
219 (if (null? remaining)
221 (let ((val (if (and (pair? (car remaining))
222 (not (null? (cdar remaining))))
226 (loop (+ last 1) (cdr remaining))
227 (if (not (= val (+ last 1)))
229 (loop val (cdr remaining)))))))
232 ; Return C code to declare enum SYM with values VALS.
233 ; COMMENT is inserted in "/* Enum declaration for <...>. */".
234 ; PREFIX is added to each element of VALS.
235 ; All enum symbols are uppercase.
236 ; If the list of vals is sequential beginning at 0, don't output them.
237 ; This simplifies the output and is necessary for sanitized values where
238 ; some values may be cut out.
239 ; VALS may have '- for the value, signifying use the next value as in C.
241 (define (gen-enum-decl name comment prefix vals)
242 (logit 2 "Generating enum decl for " name " ...\n")
243 ; Build result up as a list and then flatten it into a string.
244 ; We could just return a string-list but that seems like too much to ask
249 "/* Enum declaration for " comment ". */\n"
251 (string-downcase (gen-c-symbol name))
253 (let loop ((n 0) ; `n' is used to track the number of entries per line only
254 (sequential? (enum-sequential? vals))
259 (let* ((e (car vals))
260 (attrs (if (null? (cdr e)) nil (cddr e)))
261 (san-code (attr-value attrs 'sanitize #f))
262 (san? (and san-code (not (eq? san-code 'none)))))
265 4 ; reset to beginning of line (but != 0)
274 (if include-sanitize-marker?
275 ; split string to avoid removal
276 (string-append "/* start-"
284 (if (and (not san?) (=? (remainder n 4) 0))
290 (gen-c-symbol prefix)
291 (gen-c-symbol (car e))
297 (if (number? (cadr e))
298 (number->string (cadr e))
301 (if (and san? include-sanitize-marker?)
302 ; split string to avoid removal
303 (string-append "\n/* end-"
304 "sanitize-" san-code " */")
308 (string-upcase (gen-c-symbol name))
313 ; Return a list of enum value definitions for gen-enum-decl.
314 ; OBJ-LIST is a list of objects that support obj:name, obj-atlist.
316 (define (gen-obj-list-enums obj-list)
318 (cons (obj:name o) (cons '- (atlist-attrs (obj-atlist o)))))
322 ; Return C code that declares[/defines] an enum.
327 (gen-enum-decl (elm-get self 'name)
328 (elm-get self 'comment)
329 (if (has-attr? self 'PREFIX)
330 (string-append (elm-get self 'name) "_")
332 (elm-get self 'vals)))
335 ; Return the C symbol of an enum value named VAL.
337 (define (gen-enum-sym enum-obj val)
338 (string-upcase (gen-c-symbol (string-append (enum-prefix enum-obj) val)))
341 ; Instruction code enums.
342 ; These associate an enum with an instruction field so that the enum values
343 ; can be used in instruction field lists.
345 (define <insn-enum> (class-make '<insn-enum> '(<enum>) '(fld) nil))
349 (lambda (self name comment attrs prefix fld vals)
350 (send (object-parent self <enum>) 'make! name comment attrs prefix vals)
351 (elm-set! self 'fld fld)
356 (define ienum:fld (elm-make-getter <insn-enum> 'fld))
358 ; Same as enum-lookup-val except returned enum must be an insn-enum.
360 (define (ienum-lookup-val name)
361 (let ((result (enum-lookup-val name)))
362 (if (and result (eq? (object-class-name (cdr result)) '<insn-enum>))
367 ; Define an insn enum, all arguments specified.
369 (define (define-full-insn-enum name comment attrs prefix fld vals)
370 (let* ((context (make-current-context "define-full-insn-enum"))
371 (atlist (atlist-parse context attrs "insn-enum"))
372 (fld-obj (current-ifld-lookup fld)))
374 (if (keep-isa-atlist? atlist #f)
377 (parse-error context "unknown insn field" fld))
378 ; Create enum object and add it to the list of enums.
379 (let ((e (make <insn-enum>
380 (parse-name context name)
381 (parse-comment context comment)
383 (/enum-parse-prefix context prefix)
385 (parse-enum-vals context prefix vals))))
386 (current-enum-add! e)
392 (reader-add-command! 'define-enum
394 Define an enum, name/value pair list version.
396 nil 'arg-list define-enum)
397 (reader-add-command! 'define-full-enum
399 Define an enum, all arguments specified.
401 nil '(name comment attrs prefix vals) define-full-enum)
402 (reader-add-command! 'define-full-insn-enum
404 Define an instruction opcode enum, all arguments specified.
406 nil '(name comment attrs prefix ifld vals)
407 define-full-insn-enum)
412 (define (enum-finish!)