| 1 |
;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: C -*- |
| 2 |
;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab: |
| 3 |
|
| 4 |
;;;; |
| 5 |
;;;; Copyright (c) 2010, Juan Jose Garcia-Ripoll. |
| 6 |
;;;; |
| 7 |
;;;; See file 'LICENSE' for the copyright details. |
| 8 |
|
| 9 |
;;;; |
| 10 |
;;;; CMPC-WT -- Routines for writing code to C files. |
| 11 |
;;;; |
| 12 |
|
| 13 |
(in-package "COMPILER") |
| 14 |
|
| 15 |
(defun wt1 (form) |
| 16 |
(cond ((not (floatp form)) |
| 17 |
(typecase form |
| 18 |
((or INTEGER STRING CHARACTER) |
| 19 |
(princ form *compiler-output1*)) |
| 20 |
(VAR (wt-var form)) |
| 21 |
(t (wt-loc form)))) |
| 22 |
((ext:float-nan-p form) |
| 23 |
(format *compiler-output1* "NAN")) |
| 24 |
((ext:float-infinity-p form) |
| 25 |
(if (minusp form) |
| 26 |
(format *compiler-output1* "-INFINITY") |
| 27 |
(format *compiler-output1* "INFINITY"))) |
| 28 |
(T |
| 29 |
(typecase form |
| 30 |
(SINGLE-FLOAT |
| 31 |
(format *compiler-output1* "(float)~10,,,,,,'eG" form)) |
| 32 |
(DOUBLE-FLOAT |
| 33 |
(format *compiler-output1* "~10,,,,,,'eG" form)) |
| 34 |
(LONG-FLOAT |
| 35 |
(format *compiler-output1* "~,,,,,,'eEl" form)))))) |
| 36 |
|
| 37 |
(defun wt-h1 (form) |
| 38 |
(let ((*compiler-output1* *compiler-output2*)) |
| 39 |
(wt1 form))) |
| 40 |
|
| 41 |
(defun wt (&rest forms) |
| 42 |
(mapc #'wt1 forms)) |
| 43 |
|
| 44 |
(defun wt-h (&rest forms) |
| 45 |
(mapc #'wt-h1 forms)) |
| 46 |
|
| 47 |
(defun wt-nl-h (&rest forms) |
| 48 |
(terpri *compiler-output2*) |
| 49 |
(mapc #'wt-h1 forms)) |
| 50 |
|
| 51 |
(defun princ-h (form) |
| 52 |
(princ form *compiler-output2*)) |
| 53 |
|
| 54 |
(defun wt-nl (&rest forms) |
| 55 |
(wt-nl-indent) |
| 56 |
(mapc #'wt1 forms)) |
| 57 |
|
| 58 |
(defun wt-nl1 (&rest forms) |
| 59 |
(wt1 #\Newline) |
| 60 |
(mapc #'wt1 forms)) |
| 61 |
|
| 62 |
;;; Blocks beyond this value will not be indented |
| 63 |
(eval-when (:compile-toplevel :load-toplevel :execute) |
| 64 |
(defvar +max-depth+ 10)) |
| 65 |
|
| 66 |
(defvar +c-newline-indent-strings+ |
| 67 |
#.(coerce (let ((basis (make-array (1+ +max-depth+) |
| 68 |
:initial-element #\Space |
| 69 |
:element-type 'base-char))) |
| 70 |
(setf (aref basis 0) #\Newline) |
| 71 |
(loop for i from 0 to +max-depth+ |
| 72 |
collect (subseq basis 0 (1+ i)))) |
| 73 |
'vector)) |
| 74 |
|
| 75 |
(defun wt-nl-indent () |
| 76 |
(wt1 (aref +c-newline-indent-strings+ (min *opened-c-braces* +max-depth+)))) |
| 77 |
|
| 78 |
(defun wt-open-brace () |
| 79 |
(wt1 #\{) |
| 80 |
(incf *opened-c-braces*)) |
| 81 |
|
| 82 |
(defun wt-nl-open-brace () |
| 83 |
(wt-nl-indent) |
| 84 |
(wt-open-brace)) |
| 85 |
|
| 86 |
(defun wt-nl-close-many-braces (final-value) |
| 87 |
(let ((diff (- *opened-c-braces* final-value))) |
| 88 |
(when (minusp diff) |
| 89 |
(baboon :format-control "Mismatch in C blocks")) |
| 90 |
(loop for i from 0 below diff |
| 91 |
do (wt-nl-close-brace)))) |
| 92 |
|
| 93 |
(defun wt-nl-close-brace () |
| 94 |
(if (plusp *opened-c-braces*) |
| 95 |
(progn |
| 96 |
(decf *opened-c-braces*) |
| 97 |
(wt-nl-indent) |
| 98 |
(wt1 #\})) |
| 99 |
(baboon :format-control "Mismatch in C blocks"))) |
| 100 |
|
| 101 |
(defmacro with-indentation (&body body) |
| 102 |
`(let ((*opened-c-braces* (1+ *opened-c-braces*))) |
| 103 |
,@body)) |
| 104 |
|
| 105 |
;;; |
| 106 |
;;; LABELS AND JUMPS |
| 107 |
;;; |
| 108 |
|
| 109 |
(defun wt-nl-go (label) |
| 110 |
(wt-nl-indent) |
| 111 |
(wt-go label)) |
| 112 |
|
| 113 |
(defun wt-go (label) |
| 114 |
(setf (label-used-p label) t) |
| 115 |
(wt "goto L" (label-id label) ";")) |
| 116 |
|
| 117 |
(defun wt-label (label) |
| 118 |
(when (label-used-p label) |
| 119 |
(wt-nl1 "L" (label-id label) ":;"))) |
| 120 |
|
| 121 |
;;; |
| 122 |
;;; C/C++ COMMENTS |
| 123 |
;;; |
| 124 |
|
| 125 |
(defun wt-filtered-comment (text stream single-line) |
| 126 |
(declare (string text)) |
| 127 |
(if single-line |
| 128 |
(progn |
| 129 |
(fresh-line stream) |
| 130 |
(princ "/* " stream)) |
| 131 |
(format stream "~50T/* ")) |
| 132 |
(let* ((l (1- (length text)))) |
| 133 |
(declare (fixnum l)) |
| 134 |
(dotimes (n l) |
| 135 |
(let* ((c (schar text n)) |
| 136 |
(code (char-code c))) |
| 137 |
(cond |
| 138 |
((or (eq c #\Newline) (eq c #\Tab)) |
| 139 |
(princ c stream)) |
| 140 |
((or (< code 32) (> code 127)) |
| 141 |
(format stream "\u~x" code)) |
| 142 |
((and (char= c #\*) (char= (schar text (1+ n)) #\/)) |
| 143 |
(princ #\\ stream)) |
| 144 |
(t |
| 145 |
(princ c stream))))) |
| 146 |
(princ (schar text l) stream)) |
| 147 |
(format stream "~78T*/")) |
| 148 |
|
| 149 |
(defun do-wt-comment (message-or-format args single-line-p) |
| 150 |
(unless (and (symbolp message-or-format) (not (symbol-package message-or-format))) |
| 151 |
(wt-filtered-comment (if (stringp message-or-format) |
| 152 |
(if args |
| 153 |
(apply #'format nil message-or-format args) |
| 154 |
message-or-format) |
| 155 |
(princ-to-string message-or-format)) |
| 156 |
*compiler-output1* |
| 157 |
single-line-p))) |
| 158 |
|
| 159 |
(defun wt-comment (message &rest extra) |
| 160 |
(do-wt-comment message extra nil)) |
| 161 |
|
| 162 |
(defun wt-comment-nl (message &rest extra) |
| 163 |
(do-wt-comment message extra t)) |
| 164 |
|
| 165 |
;;; |
| 166 |
;;; STRINGS |
| 167 |
;;; |
| 168 |
;;; This routine converts lisp data into C-strings. We have to take |
| 169 |
;;; care of escaping special characteres with backslashes. We also have |
| 170 |
;;; to split long lines using the fact that multiple strings are joined |
| 171 |
;;; together by the compiler. |
| 172 |
;;; |
| 173 |
|
| 174 |
(defvar *wt-string-size* 0) |
| 175 |
|
| 176 |
(defun wt-filtered-data (string stream &key one-liner |
| 177 |
(external-format #-unicode :default #+unicode :utf-8)) |
| 178 |
(declare (ignorable external-format)) |
| 179 |
#+unicode |
| 180 |
(setf string (ext:string-to-octets string :external-format external-format |
| 181 |
:element-type 'base-char)) |
| 182 |
(let ((N (length string)) |
| 183 |
(wt-data-column 80)) |
| 184 |
(incf *wt-string-size* N) ; 1+ accounts for a blank space |
| 185 |
(format stream (if one-liner "\"" "~%\"")) |
| 186 |
(dotimes (i N) |
| 187 |
(decf wt-data-column) |
| 188 |
(when (< wt-data-column 0) |
| 189 |
(format stream "\"~% \"") |
| 190 |
(setq wt-data-column 79)) |
| 191 |
(let ((x (aref string i))) |
| 192 |
(cond |
| 193 |
((or (< (char-code x) 32) |
| 194 |
(> (char-code x) 127)) |
| 195 |
(case x |
| 196 |
; We avoid a trailing backslash+newline because some preprocessors |
| 197 |
; remove them. |
| 198 |
(#\Newline (princ "\\n" stream)) |
| 199 |
(#\Tab (princ "\\t" stream)) |
| 200 |
(t (format stream "\\~3,'0o" (char-code x))))) |
| 201 |
((char= x #\\) |
| 202 |
(princ "\\\\" stream)) |
| 203 |
((char= x #\") |
| 204 |
(princ "\\\"" stream)) |
| 205 |
(t (princ x stream))))) |
| 206 |
(princ "\"" stream) |
| 207 |
string)) |
| 208 |
|
| 209 |
(defun c-filtered-string (string &rest args) |
| 210 |
(with-output-to-string (aux-stream) |
| 211 |
(apply #'wt-filtered-data string aux-stream :one-liner t args))) |
| 212 |
|
| 213 |
(defun c-inline-safe-string (constant-string) |
| 214 |
;; Produce a text representation of a string that can be used in a C-INLINE |
| 215 |
;; form, without triggering the @ or # escape characters |
| 216 |
(c-filtered-string |
| 217 |
(concatenate 'string |
| 218 |
(loop for c across constant-string |
| 219 |
when (member c '(#\# #\@)) |
| 220 |
collect c |
| 221 |
collect c)))) |