; CPU documentation generator, html output ; Copyright (C) 2003, 2009 Doug Evans ; This file is part of CGEN. See file COPYING.CGEN for details. ; ; TODO: ; - assumes names, comments, etc. don't interfere with html. ; Just like in generation of C there are routines to C-ize symbols, ; we need to pass output through an html-izer. ; - make generated html more readable, e.g. more indentation ; - should really print the semantics in pseudo-C, a much better form for ; the intended audience ; - registers that have multiple independent fields (like x86 eflags) ; need to be printed like instruction formats are ; - uses some deprecated html, use css at very least ; - multi-ifields ok? ; - mapping from operands to h/w isn't as clear as it needs to be ; - for insn formats, if field is large consider printing "n ... m", ; would want "n" left justified and "m" right justified though ; - for insn formats, consider printing them better, ; e.g. maybe generate image and include that instead ; - need ability to specify more prose for each architecture ; - assembler support ; - need to add docs to website that can be linked to here, rather than ; including generic cgen documentation here ; - function units, timing, etc. ; - instruction framing ; Global state variables. ; Specify which application. (set! APPLICATION 'DOC) ; String containing copyright text. (define CURRENT-COPYRIGHT #f) ; String containing text defining the package we're generating code for. (define CURRENT-PACKAGE #f) (define copyright-doc (cons "\ THIS FILE IS MACHINE GENERATED WITH CGEN. See the input .cpu file(s) for copyright information. " "\ ")) ; Initialize the options. (define (option-init!) (set! CURRENT-COPYRIGHT copyright-doc) (set! CURRENT-PACKAGE package-cgen) *UNSPECIFIED* ) ; Handle an option passed in from the command line. (define (option-set! name value) (case name ((copyright) (cond ((equal? value '("doc")) (set! CURRENT-COPYRIGHT copyright-doc)) (else (error "invalid copyright value" value)))) ((package) (cond ((equal? value '("cgen")) (set! CURRENT-PACKAGE package-cgen)) (else (error "invalid package value" value)))) (else (error "unknown option" name)) ) *UNSPECIFIED* ) ; Misc utilities. ; Return COPYRIGHT, with FILE-DESC as the first line ; and PACKAGE as the name of the package which the file belongs in. ; COPYRIGHT is a pair of (header . trailer). (define (gen-html-copyright file-desc copyright package) (string-append "\n\n") ) ; KIND is one of "Architecture" or "Instruction". ; TODO: Add author arg so all replies for this arch go to right person. (define (gen-html-header kind) (let* ((arch (symbol->string (current-arch-name))) (ARCH (string-upcase arch))) (string-list "\n" "\n" "\n" " \n" " \n" " \n" " \n" " \n" " " ARCH " " kind " Documentation\n" "\n" "\n" ) ) ) (define (gen-html-trailer) (string-list "\n" "


\n" "This documentation was machine generated from the cgen cpu description\n" "files for this architecture.\n" "
\n" "http://sources.redhat.com/cgen/\n" "\n" "\n" ) ) ; INSN-FILE is the name of the .html file containing instruction definitions. (define (gen-table-of-contents insn-file) (let ((ARCH (string-upcase (symbol->string (current-arch-name))))) (string-list "

\n" (string-append ARCH " Architecture Documentation") "

\n" "\n" "
\n" "DISCLAIMER: This documentation is derived from the cgen cpu description\n" "of this architecture, and does not represent official documentation\n" "of the chip maker.\n" "


\n" "\n" "

\n" "
\n" ; TODO: Move this to the cgen website, and include a link here. "In cgen-parlance, an architecture consists of machines and models.\n" "A `machine' is the specification of a variant of the architecture,\n" "and a `model' is the implementation of that specification.\n" "Typically there is a one-to-one correspondance between machine and model.\n" "The distinction allows for separation of what application programs see\n" "(the machine), and how to tune for the chip (what the compiler sees).\n" "
\n" "A \"cpu family\" is a cgen concoction to help organize the generated code.\n" "Chip variants that are quite dissimilar can be treated separately by the\n" "generated code even though they're both members of the same architecture.\n" )) ) ; Utility to print a list entry for NAME/COMMENT, kind KIND ; which is a link to the entry's description. ; KIND is one of "mach", "model", etc. (define (gen-list-entry name comment kind) (string-append "
  • " "string name) "\">" (->string name) " - " comment "\n" "
  • \n") ) ; Cover-fn to gen-list-entry for use with objects. (define (gen-obj-list-entry o kind) (gen-list-entry (obj:name o) (obj:comment o) kind) ) ; Utility to print the header for the description of TEXT. (define (gen-doc-header text anchor-name) (string-list "\n" "

    " text "

    \n" ) ) ; Cover-fn to gen-doc-header for use with objects. ; KIND is one of "mach", "model", etc. (define (gen-obj-doc-header o kind) (gen-doc-header (string-append (obj:str-name o) " - " (obj:comment o)) (string-append kind "-" (obj:str-name o))) ) ; Architecture page. (define (gen-cpu-intro cpu) (string-list "
  • \n" (obj:str-name cpu) " - " (obj:comment cpu) "\n" "
    \n" "
    \n" "Machines:\n" "\n" "
  • \n" "
    \n" ) ) (define (gen-mach-intro mach) (string-list "
  • \n" (obj:str-name mach) " - " (obj:comment mach) "\n" "
    \n" "
    \n" "Models:\n" "\n" "
  • \n" "
    \n" ) ) (define (gen-model-intro model) (string-list "
  • \n" (obj:str-name model) " - " (obj:comment model) "\n" "
    \n" "
  • \n" ) ) (define (gen-isa-intro isa) (string-list "
  • \n" (obj:str-name isa) " - " (obj:comment isa) "\n" "
    \n" ; FIXME: wip ; I'd like to include the .cpu file tag here, but using English text ; feels more appropriate. Having both is excessive. ; Pick one, and have a link to its description/tag. ; I'm leaning toward using the cgen tag here as we'll probably want ; access (via an html tag) to more than one-liner descriptions. "\n" "
  • \n" ) ) (define (gen-arch-intro) ; NOTE: This includes cpu families. (let ((ARCH (string-upcase (symbol->string (current-arch-name)))) (isas (current-isa-list)) (cpus (current-cpu-list)) ) (string-list "\n" "
    \n" "\n" "

    " ARCH " Architecture

    \n" "

    \n" "This section describes various things about the cgen description of\n" "the " ARCH " architecture. Familiarity with cgen cpu descriptions\n" "is assumed.\n" "

    \n" "Bit number orientation (arch.lsb0?): " (if (current-arch-insn-lsb0?) "lsb = 0" "msb = 0") "\n" "

    \n" "

    ISA description

    \n" ; NOTE: For the normal case there's only one isa, thus specifying it in ; a list is excessive. Later. "

    \n" "

    \n" "

    \n" "

    CPU Families

    \n" "\n" )) ) ; Machine page. (define (gen-machine-doc-1 mach) (string-list (gen-obj-doc-header mach "mach") "\n" ) ) (define (gen-machine-docs) (let ((machs (alpha-sort-obj-list (current-mach-list)))) (string-list "\n" "
    \n" "\n" "

    Machine variants

    \n" "\n" (string-list-map gen-machine-doc-1 machs) )) ) ; Model page. (define (gen-model-doc-1 model) (string-list (gen-obj-doc-header model "model") "\n" ) ) (define (gen-model-docs) (let ((models (alpha-sort-obj-list (current-model-list)))) (string-list "\n" "
    \n" "\n" "

    Model variants

    \n" "\n" (string-list-map gen-model-doc-1 models) )) ) ; Register page. ; ; TODO: Provide tables of regs for each mach. ; Subroutine of gen-reg-doc-1 to simplify it. ; Generate a list of names of registers in register array REG. ; The catch is that we want to shrink r0,r1,r2,...,r15 to r0...r15. (define (gen-pretty-reg-array-names reg) ; We currently only support arrays of rank 1 (vectors). (if (!= (hw-rank reg) 1) (error "gen-pretty-reg-array-names: unsupported rank" (hw-rank reg))) (let ((indices (hw-indices reg))) (if (class-instance? indices) (let ((values (kw-values indices))) (string-list "
    \n" "names:\n" "
    \n" "\n" "\n" (string-list-map (lambda (v) (string-list "\n" "\n" "\n" "\n")) values))) "")) ) (define (gen-reg-doc-1 reg) (string-list (gen-obj-doc-header reg "reg") "\n" ) ) (define (gen-register-docs) (let ((regs (alpha-sort-obj-list (find register? (current-hw-list))))) (string-list "\n" "
    \n" "\n" "

    Registers

    \n" "\n" (string-list-map gen-reg-doc-1 regs) )) ) ; Instruction page. ; Generate a diagram typically used to display instruction fields. ; OPERANDS is a list of numbers (for constant valued ifields) ; or operand names. (define (gen-iformat-table-1 bitnums names operands) (string-list "
    " (car v) "" (number->string (cadr v)) "
    \n" "\n" (string-list-map (lambda (b) (string-list "\n")) bitnums) "\n" "\n" (string-list-map (lambda (n) (string-list "\n")) names) "\n" "\n" (string-list-map (lambda (o) (string-list "\n")) operands) "\n" "
    \n" (string-map (lambda (n) (string-append " " (number->string n))) b) "\n" "
    \n" n "\n" "
    \n" (if (number? o) (string-append "0x" (number->string o 16)) o) "\n" "
    \n") ) ; Compute the list of field bit-numbers for each field. (define (get-ifield-bitnums widths lsb0?) (let* ((total-width (apply + widths)) (bitnums (iota total-width (if lsb0? (1- total-width) 0) (if lsb0? -1 1)))) (let loop ((result '()) (widths widths) (bitnums bitnums)) (if (null? widths) (reverse! result) (loop (cons (list-take (car widths) bitnums) result) (cdr widths) (list-drop (car widths) bitnums))))) ) ; Generate a diagram typically used to display instruction fields. (define (gen-iformat-table insn) (let* ((lsb0? (current-arch-insn-lsb0?)) (sorted-iflds (sort-ifield-list (insn-iflds insn) (not lsb0?)))) (let ((widths (map ifld-length sorted-iflds)) (names (map obj:name sorted-iflds)) (operands (map (lambda (f) (if (ifld-constant? f) (ifld-get-value f) (obj:name (ifld-get-value f)))) sorted-iflds))) (gen-iformat-table-1 (get-ifield-bitnums widths lsb0?) names operands))) ) (define (gen-insn-doc-1 insn) (string-list (gen-obj-doc-header insn "insn") "\n" ) ) (define (gen-insn-doc-list mach name comment insns) (string-list "
    \n" (gen-doc-header (string-append (obj:str-name mach) " " (->string name) (if (string=? comment "") "" (string-append " - " comment))) (string-append "mach-insns-" (obj:str-name mach) "-" (->string name))) "\n" ) ) ; Return boolean indicating if INSN sets the pc. (define (insn-sets-pc? insn) (or (obj-has-attr? insn 'COND-CTI) (obj-has-attr? insn 'UNCOND-CTI) (obj-has-attr? insn 'SKIP-CTI)) ) ; Traverse the semantics of INSN and return a list of symbols ; indicating various interesting properties we find. ; This is taken from `semantic-attrs' which does the same thing to find the ; CTI attributes. ; The result is list of properties computed from the semantics. ; The possibilities are: MEM, FPU. (define (get-insn-properties insn) (logit 2 "Collecting properties of insn " (obj:name insn) " ...\n") (let* ((context #f) ; ??? do we need a better context? ; List of attributes computed from SEM-CODE-LIST. ; The first element is just a dummy so that append! always works. (sem-attrs (list #f)) ; Called for expressions encountered in SEM-CODE-LIST. (process-expr! (lambda (rtx-obj expr parent-expr op-pos tstate appstuff) (case (car expr) ((operand) (if (memory? (op:type (current-op-lookup (rtx-arg1 expr) (obj-isa-list insn)))) ; Don't change to '(MEM), since we use append!. (append! sem-attrs (list 'MEM))) (if (mode-float? (mode:lookup (rtx-mode expr))) ; Don't change to '(FPU), since we use append!. (append! sem-attrs (list 'FPU))) ) ((mem) (append! sem-attrs (list 'MEM))) ; If this is a syntax expression, the operands won't have been ; processed, so tell our caller we want it to by returning #f. ; We do the same for non-syntax expressions to keep things ; simple. This requires collaboration with the traversal ; handlers which are defined to do what we want if we return #f. (else #f)))) ) ; Traverse the expression recording the attributes. ; We just want the side-effects of computing various properties ; so we discard the result. (rtx-traverse context insn ; Simplified semantics recorded in the `tmp' field. (insn-tmp insn) process-expr! #f) ; Drop dummy first arg and remove duplicates. (nub (cdr sem-attrs) identity)) ) ; Return boolean indicating if PROPS indicates INSN references memory. (define (insn-refs-mem? insn props) (->bool (memq 'MEM props)) ) ; Return boolean indicating if PROPS indicates INSN uses the fpu. (define (insn-uses-fpu? insn props) (->bool (memq 'FPU props)) ) ; Ensure INSN has attribute IDOC. ; If not specified, guess(?). (define (guess-insn-idoc-attr! insn) (if (not (obj-attr-present? insn 'IDOC)) (let ((attr #f) (props (get-insn-properties insn))) ; Try various heuristics. (if (and (not attr) (insn-sets-pc? insn)) (set! attr 'BR)) (if (and (not attr) (insn-refs-mem? insn props)) (set! attr 'MEM)) (if (and (not attr) (insn-uses-fpu? insn props)) (set! attr 'FPU)) ; If nothing else works, assume ALU. (if (not attr) (set! attr 'ALU)) (obj-cons-attr! insn (enum-attr-make 'IDOC attr)))) *UNSPECIFIED* ) ; Return subset of insns in IDOC category CAT-NAME. (define (get-insns-for-category insns cat-name) (find (lambda (insn) (obj-has-attr-value-no-default? insn 'IDOC cat-name)) insns) ) ; CATEGORIES is a list of "enum value" elements for each category. ; See for the definition. ; INSNS is already alphabetically sorted and selected for just MACH. (define (gen-categories-insn-lists mach categories insns) (string-list ; generate a table of insns for each category (string-list-map (lambda (c) (let ((cat-insns (get-insns-for-category insns (enum-val-name c))) (comment (enum-val-comment c))) (if (null? cat-insns) "" (gen-insn-doc-list mach (enum-val-name c) comment cat-insns)))) categories) ; lastly, the alphabetical list (gen-insn-doc-list mach (obj:name mach) (obj:comment mach) insns) ) ) ; CATEGORIES is a list of "enum value" elements for each category. ; See for the definition. ; INSNS is already alphabetically sorted and selected for just MACH. (define (gen-insn-categories mach categories insns) (string-list "\n" ) ) ; ??? There's an inefficiency here, we compute insns for each mach for each ; category twice. Left for later if warranted. (define (gen-insn-docs) ; First simplify the semantics, e.g. do constant folding. ; For insns built up from macros, often this will remove a lot of clutter. (for-each (lambda (insn) (logit 2 "Simplifying the rtl for insn " (obj:name insn) " ...\n") (insn-set-tmp! insn (rtx-simplify-insn #f insn))) (current-insn-list)) (let ((machs (current-mach-list)) (insns (alpha-sort-obj-list (current-insn-list))) (categories (attr-values (current-attr-lookup 'IDOC)))) ; First, install IDOC attributes for insns that don't specify one. (for-each guess-insn-idoc-attr! insns) (string-list "\n" "
    \n" "\n" "

    Instructions

    \n" "Instructions for each machine:\n" "
      \n" ; (string-map (lambda (o) ; (gen-obj-list-entry o "mach-insns")) ; machs) (string-list-map (lambda (m) (let ((mach-insns (find (lambda (insn) (mach-supports? m insn)) insns))) (string-list "
    • " (obj:str-name m) " - " (obj:comment m) "
    • \n" (gen-insn-categories m categories mach-insns) ))) machs) "
    \n" ; (string-list-map (lambda (m) ; (gen-insn-doc-list m insns)) ; machs) (string-list-map (lambda (m) (let ((mach-insns (find (lambda (insn) (mach-supports? m insn)) insns))) (gen-categories-insn-lists m categories mach-insns))) machs) "
    \n" "

    Individual instructions descriptions

    \n" "
    \n" (string-list-map gen-insn-doc-1 insns) )) ) ; Macro-instruction page. (define (gen-macro-insn-doc-1 minsn) (string-list (gen-obj-doc-header minsn "macro-insn") "
      \n" "
    • \n" "syntax: " "" (minsn-syntax minsn) "\n" "
    • \n" "
      \n" "
    • \n" "transformation:\n" "\n" "
      " ; no trailing newline here on purpose
         (with-output-to-string
           (lambda ()
             (pretty-print (minsn-expansions minsn))))
         "
      \n" "
    • \n" "
    \n" ) ) (define (gen-macro-insn-doc-list mach) (let ((minsns (find (lambda (minsn) (mach-supports? mach minsn)) (current-minsn-list)))) (string-list (gen-obj-doc-header mach "mach-macro-insns") "
      \n" (string-map (lambda (o) (gen-obj-list-entry o "macro-insn")) minsns) "
    \n" )) ) (define (gen-macro-insn-docs) (let ((machs (current-mach-list)) (minsns (alpha-sort-obj-list (current-minsn-list)))) (string-list "\n" "
    \n" "\n" "

    Macro Instructions

    \n" "Macro instructions for each machine:\n" "
      \n" (string-map (lambda (o) (gen-obj-list-entry o "mach-macro-insns")) machs) "
    \n" (string-list-map gen-macro-insn-doc-list machs) "

    \n" "

    Individual macro-instructions descriptions

    \n" "
    \n" (string-list-map gen-macro-insn-doc-1 minsns) )) ) ; Assembler page. (define (gen-asm-docs) (string-list "\n" "
    \n" "\n" "

    Assembler supplemental

    \n" ) ) ; Documentation init,finish,analyzer support. ; Initialize any doc specific things before loading the .cpu file. (define (doc-init!) (desc-init!) (mode-set-biggest-word-bitsizes!) *UNSPECIFIED* ) ; Finish any doc specific things after loading the .cpu file. ; This is separate from analyze-data! as cpu-load performs some ; consistency checks in between. (define (doc-finish!) (desc-finish!) *UNSPECIFIED* ) ; Compute various needed globals and assign any computed fields of ; the various objects. This is the standard routine that is called after ; a .cpu file is loaded. (define (doc-analyze!) (desc-analyze!) ; If the IDOC attribute isn't defined, provide a default one. (if (not (current-attr-lookup 'IDOC)) (define-attr '(for insn) '(type enum) '(name IDOC) '(comment "insn kind for documentation") '(attrs META) '(values (MEM - () "Memory") (ALU - () "ALU") (FPU - () "FPU") (BR - () "Branch") (MISC - () "Miscellaneous")))) ; Initialize the rtl->c translator. (rtl-c-config!) ; Only include semantic operands when computing the format tables if we're ; generating operand instance tables. ; ??? Actually, may always be able to exclude the semantic operands. ; Still need to traverse the semantics to derive machine computed attributes. (arch-analyze-insns! CURRENT-ARCH #t ; include aliases? #f) ; analyze semantics? *UNSPECIFIED* ) ; Top level C code generators ; Set by the -N argument. (define *insn-html-file-name* "unspecified.html") (define (cgen.html) (logit 1 "Generating " (current-arch-name) ".html ...\n") (string-write (gen-html-copyright (string-append "Architecture documentation for " (symbol->string (current-arch-name)) ".") CURRENT-COPYRIGHT CURRENT-PACKAGE) (gen-html-header "Architecture") (gen-table-of-contents *insn-html-file-name*) gen-arch-intro gen-machine-docs gen-model-docs gen-register-docs gen-asm-docs gen-html-trailer ) ) (define (cgen-insn.html) (logit 1 "Generating " (current-arch-name) "-insn.html ...\n") (string-write (gen-html-copyright (string-append "Instruction documentation for " (symbol->string (current-arch-name)) ".") CURRENT-COPYRIGHT CURRENT-PACKAGE) (gen-html-header "Instruction") gen-insn-docs gen-macro-insn-docs gen-html-trailer ) ) ; For debugging. (define (cgen-all) (string-write cgen.html cgen-insn.html ) )