generate-old-c-header.scm 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. ; Copyright (c) 1993-2008 by Richard Kelsey and Jonathan Rees. See file COPYING.
  2. ; This for compatibility with pre-0.53 code.
  3. ; [This is a kludge. Richard is loathe to include it in the
  4. ; distribution.]
  5. ; Reads arch.scm and data.scm and writes out a C .h file with constants
  6. ; and macros for dealing with Scheme 48 data structures.
  7. ; Needs Big Scheme.
  8. ; (make-c-header-file "scheme48.h" "vm/arch.scm" "vm/data.scm")
  9. (define (make-c-header-file c-file arch-file data-file)
  10. (receive (stob-list stob-data)
  11. (search-file arch-file
  12. '("stob enumeration" "(define stob-data ...)")
  13. (defines-enum? 'stob)
  14. enum-definition-list
  15. (lambda (x)
  16. (and (eq? (car x) 'define)
  17. (eq? (cadr x) 'stob-data)))
  18. (lambda (x) (cadr (caddr x))))
  19. (receive (tag-list immediate-list)
  20. (search-file data-file
  21. '("tag enumeration" "imm enumeration")
  22. (defines-enum? 'tag)
  23. enum-definition-list
  24. (defines-enum? 'imm)
  25. enum-definition-list)
  26. (with-output-to-file c-file
  27. (lambda ()
  28. (format #t "typedef long scheme_value;~%~%")
  29. (tag-stuff tag-list)
  30. (newline)
  31. (immediate-stuff immediate-list)
  32. (newline)
  33. (stob-stuff stob-list stob-data))))))
  34. (define (tag-stuff tag-list)
  35. (do ((tags tag-list (cdr tags))
  36. (i 0 (+ i 1)))
  37. ((null? tags))
  38. (let ((name (upcase (car tags))))
  39. (c-define "~A_TAG ~D" name i)
  40. (c-define "~AP(x) (((long)(x) & 3L) == ~A_TAG)" name name)))
  41. (newline)
  42. (c-define "ENTER_FIXNUM(n) ((scheme_value)((n) << 2))")
  43. (c-define "EXTRACT_FIXNUM(x) ((long)(x) >> 2)"))
  44. (define (immediate-stuff imm-list)
  45. (c-define "MISC_IMMEDIATE(n) (scheme_value)(IMMEDIATE_TAG | ((n) << 2))")
  46. (do ((imm imm-list (cdr imm))
  47. (i 0 (+ i 1)))
  48. ((null? imm))
  49. (let ((name (upcase (car imm))))
  50. (c-define "SCH~A MISC_IMMEDIATE(~D)" name i)))
  51. (c-define "UNDEFINED SCHUNDEFINED")
  52. (c-define "UNSPECIFIC SCHUNSPECIFIC")
  53. (newline)
  54. (c-define "ENTER_BOOLEAN(n) ((n) ? SCHTRUE : SCHFALSE)")
  55. (c-define "EXTRACT_BOOLEAN(x) ((x) != SCHFALSE)")
  56. (newline)
  57. (c-define "ENTER_CHAR(c) (SCHCHAR | ((c) << 8))")
  58. (c-define "EXTRACT_CHAR(x) ((x) >> 8)")
  59. (c-define "CHARP(x) ((((long) (x)) & 0xff) == SCHCHAR)"))
  60. (define (stob-stuff stob-list stob-data)
  61. (let ((type-mask (let ((len (length stob-list)))
  62. (do ((i 2 (* i 2)))
  63. ((>= i len) (- i 1))))))
  64. (c-define "ADDRESS_AFTER_HEADER(x, type) ((type *)((x) - STOB_TAG))")
  65. (c-define "STOB_REF(x, i) ((ADDRESS_AFTER_HEADER(x, long))[i])")
  66. (c-define "STOB_TYPE(x) ((STOB_HEADER(x)>>2)&~D)" type-mask)
  67. (c-define "STOB_HEADER(x) (STOB_REF((x),-1))")
  68. (c-define "STOB_BLENGTH(x) (STOB_HEADER(x) >> 8)")
  69. (c-define "STOB_LLENGTH(x) (STOB_HEADER(x) >> 10)")
  70. (newline)
  71. (do ((stob stob-list (cdr stob))
  72. (i 0 (+ i 1)))
  73. ((null? stob))
  74. (let ((name (upcase (if (eq? (car stob) 'byte-vector)
  75. 'code-vector
  76. (car stob)))))
  77. (c-define "STOBTYPE_~A ~D" name i)
  78. (c-define "~AP(x) (STOBP(x) && (STOB_TYPE(x) == STOBTYPE_~A))"
  79. name name)))
  80. (newline)
  81. (for-each (lambda (data)
  82. (do ((accs (cdddr data) (cdr accs))
  83. (i 0 (+ i 1)))
  84. ((null? accs))
  85. (let ((name (upcase (caar accs))))
  86. (c-define "~A(x) STOB_REF(x, ~D)" name i))))
  87. stob-data)
  88. (newline)
  89. (c-define "VECTOR_LENGTH(x) STOB_LLENGTH(x)")
  90. (c-define "VECTOR_REF(x, i) STOB_REF(x, i)")
  91. (c-define "CODE_VECTOR_LENGTH(x) STOB_BLENGTH(x)")
  92. (c-define "CODE_VECTOR_REF(x, i) (ADDRESS_AFTER_HEADER(x, unsigned char)[i])")
  93. (c-define "STRING_LENGTH(x) (STOB_BLENGTH(x)-1)")
  94. (c-define "STRING_REF(x, i) (ADDRESS_AFTER_HEADER(x, char)[i])")))
  95. ; - becomes _ > becomes TO_ (so -> turns into _TO_)
  96. ; ? becomes P
  97. (define (upcase symbol)
  98. (do ((chars (string->list (symbol->string symbol)) (cdr chars))
  99. (res '() (case (car chars)
  100. ((#\>) (append (string->list "_OT") res))
  101. ((#\-) (cons #\_ res))
  102. ((#\?) (cons #\P res))
  103. (else (cons (char-upcase (car chars)) res)))))
  104. ((null? chars)
  105. (list->string (reverse res)))))
  106. (define (c-define string . stuff)
  107. (format #t "#define ~?~%" string stuff))
  108. (define (defines-enum? name)
  109. (lambda (x)
  110. (and (eq? (car x) 'define-enumeration)
  111. (eq? (cadr x) name))))
  112. (define enum-definition-list caddr)
  113. ; STUFF is list of ((predicate . extract) . name). <name> is replaced
  114. ; with the value when it is found.
  115. (define (search-file file what-for . pred+extract)
  116. (let ((stuff (do ((p+e pred+extract (cddr p+e))
  117. (names what-for (cdr names))
  118. (ps '() (cons (cons (cons (car p+e) (cadr p+e))
  119. (car names))
  120. ps)))
  121. ((null? p+e) (reverse ps)))))
  122. (define (search next not-found)
  123. (let loop ((n-f not-found) (checked '()))
  124. (cond ((null? n-f)
  125. #f)
  126. (((caaar n-f) next)
  127. (set-cdr! (car n-f) ((cdaar n-f) next))
  128. (append (reverse checked) (cdr n-f)))
  129. (else
  130. (loop (cdr n-f) (cons (car n-f) checked))))))
  131. (with-input-from-file file
  132. (lambda ()
  133. (let loop ((not-found stuff))
  134. (let ((next (read)))
  135. (cond ((null? not-found)
  136. (apply values (map cdr stuff)))
  137. ((eof-object? next)
  138. (error "file ~S doesn't have ~A" file (cdar not-found)))
  139. (else
  140. (loop (or (and (pair? next)
  141. (search next not-found))
  142. not-found))))))))))