[e90b2f]: src / cmp / cmpc-wt.lsp Maximize Restore History

Download this file

cmpc-wt.lsp    173 lines (150 with data), 4.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
;;;; -*- Mode: Lisp; Syntax: Common-Lisp; Package: C -*-
;;;;
;;;; Copyright (c) 2010, Juan Jose Garcia-Ripoll.
;;;;
;;;; This program is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Library General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 2 of the License, or (at your option) any later version.
;;;;
;;;; See file '../Copyright' for full details.
;;;;
;;;; CMPC-WT -- Routines for writing code to C files.
;;;;
(in-package #-new-cmp "COMPILER" #+new-cmp "C-BACKEND")
(defun wt1 (form)
(typecase form
((or STRING INTEGER CHARACTER)
(princ form *compiler-output1*))
(SINGLE-FLOAT
(format *compiler-output1* "(float)~10,,,,,,'eG" form))
(DOUBLE-FLOAT
(format *compiler-output1* "~10,,,,,,'eG" form))
(LONG-FLOAT
(format *compiler-output1* "~,,,,,,'eEl" form))
(VAR (wt-var form))
(t (wt-loc form)))
nil)
(defun wt-h1 (form)
(let ((*compiler-output1* *compiler-output2*))
(wt1 form)))
(defun wt (&rest forms)
(mapc #'wt1 forms))
(defun wt-h (&rest forms)
(mapc #'wt-h1 forms))
(defun wt-nl-h (&rest forms)
(terpri *compiler-output2*)
(mapc #'wt-h1 forms))
(defun princ-h (form)
(princ form *compiler-output2*))
(defun wt-nl (&rest forms)
(wt1 #\Newline)
(wt1 #\Tab)
(mapc #'wt1 forms))
(defun wt-nl1 (&rest forms)
(wt1 #\Newline)
(mapc #'wt1 forms))
;;;
;;; LABELS AND JUMPS
;;;
(defun wt-go (label)
#-new-cmp
(setf (cdr label) t
label (car label))
(wt "goto L" label ";"))
(defun wt-label (label)
#-new-cmp
(when (cdr label) (wt-nl1 "L" (car label) ":;"))
#+new-cmp
(wt-nl1 "L" label ":;"))
;;;
;;; C/C++ COMMENTS
;;;
(defun wt-filtered-comment (text stream single-line)
(declare (string text))
(if single-line
(progn
(fresh-line stream)
(princ "/* " stream))
(format stream "~50T/* "))
(let* ((l (1- (length text))))
(declare (fixnum l))
(dotimes (n l)
(let* ((c (schar text n))
(code (char-code c)))
(cond
((or (eq c #\Newline) (eq c #\Tab))
(princ c stream))
((or (< code 32) (> code 127))
(format stream "\ux" code))
((and (char= c #\*) (char= (schar text (1+ n)) #\/))
(princ #\\ stream))
(t
(princ c stream)))))
(princ (schar text l) stream))
(format stream "~70T*/")
)
(defun do-wt-comment (message-or-format args single-line-p)
(unless (and (symbolp message-or-format) (not (symbol-package message-or-format)))
(wt-filtered-comment (if (stringp message-or-format)
(if args
(apply #'format nil message-or-format args)
message-or-format)
(princ-to-string message-or-format))
*compiler-output1*
single-line-p)))
(defun wt-comment (message &rest extra)
(do-wt-comment message extra nil))
(defun wt-comment-nl (message &rest extra)
(do-wt-comment message extra t))
;;;
;;; STRINGS
;;;
;;; This routine converts lisp data into C-strings. We have to take
;;; care of escaping special characteres with backslashes. We also have
;;; to split long lines using the fact that multiple strings are joined
;;; together by the compiler.
;;;
(defvar *wt-string-size* 0)
#+unicode
(defun encode-string (string format)
(let* ((output (make-array (round (* 1.2 (length string)))
:element-type 'base-char
:adjustable t
:fill-pointer 0))
(stream (make-sequence-output-stream output :external-format format)))
(write-string string stream)
output))
(defun wt-filtered-data (string stream &key one-liner
(external-format #-unicode :default #+unicode :utf-8))
#+unicode
(setf string (encode-string string external-format))
(let ((N (length string))
(wt-data-column 80))
(incf *wt-string-size* (1+ N)) ; 1+ accounts for a blank space
(format stream (if one-liner "\"" "~%\""))
(dotimes (i N)
(decf wt-data-column)
(when (< wt-data-column 0)
(format stream "\"~% \"")
(setq wt-data-column 79))
(let ((x (aref string i)))
(cond
((or (< (char-code x) 32)
(> (char-code x) 127))
(case x
; We avoid a trailing backslash+newline because some preprocessors
; remove them.
(#\Newline (princ "\\n" stream))
(#\Tab (princ "\\t" stream))
(t (format stream "\\~3,'0o" (char-code x)))))
((char= x #\\)
(princ "\\\\" stream))
((char= x #\")
(princ "\\\"" stream))
(t (princ x stream)))))
(princ (if one-liner "\"" " \"") stream)
string))
(defun c-filtered-string (string &rest args)
(with-output-to-string (aux-stream)
(apply #'wt-filtered-data string aux-stream :one-liner t args)))