summaryrefslogtreecommitdiff
path: root/mblock/src/scheme/gnuradio/compile-mbh.scm
blob: a16e14c585be74719b16995a2f9b196ab6f76b6b (plain)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/guile \
-e main -s
!#
;; -*-scheme-*-
;;
;; Copyright 2007 Free Software Foundation, Inc.
;; 
;; This file is part of GNU Radio
;; 
;; GNU Radio is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; 
;; GNU Radio is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;; 
;; You should have received a copy of the GNU General Public License along
;; with this program; if not, write to the Free Software Foundation, Inc.,
;; 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
;;

;; usage: compile-mbh <input-file> <output-file>

(use-modules (ice-9 getopt-long))
(use-modules (ice-9 format))
(use-modules (ice-9 pretty-print))
;(use-modules (ice-9 slib))
(use-modules (gnuradio pmt-serialize))
(use-modules (gnuradio macros-etc))

(debug-enable 'backtrace)

;; ----------------------------------------------------------------

(define (main args)

  (define (usage)
    (format 0 "usage: ~a input-file output-file~%" (car args)))

  (when (not (= (length args) 3))
	(usage)
	(exit 1))
      
  (let ((input-filename (cadr args))
	(output-filename (caddr args)))
      (if (compile-mbh-file input-filename output-filename)
	  (exit 0)
	  (exit 1))))


;; ----------------------------------------------------------------
;; constructor and accessors for protocol-class

(define %protocol-class-tag (string->symbol "[PROTOCOL-CLASS-TAG]"))

(define (make-protocol-class name incoming outgoing)
  (vector %protocol-class-tag name incoming outgoing))

(define (protocol-class? obj)
  (and (vector? obj) (eq? %protocol-class-tag (vector-ref obj 0))))

(define (protocol-class-name pc)
  (vector-ref pc 1))

(define (protocol-class-incoming pc)
  (vector-ref pc 2))

(define (protocol-class-outgoing pc)
  (vector-ref pc 3))


;; ----------------------------------------------------------------

(define (syntax-error msg e)
  (throw 'syntax-error msg e))

(define (unrecognized-form form)
  (syntax-error "Unrecognized form" form))


(define (mbh-chk-length= e y n)
  (cond ((and (null? y)(zero? n))
	 #f)
        ((null? y)
         (syntax-error "Expression has too few subexpressions" e))
        ((atom? y)
         (syntax-error (if (atom? e)
                           "List expected"
                           "Expression ends with `dotted' atom")
                       e))
        ((zero? n)
         (syntax-error "Expression has too many subexpressions" e))
        (else
          (mbh-chk-length= e (cdr y) (- n 1)))))

(define (mbh-chk-length>= e y n)
  (cond ((and (null? y)(< n 1))
	 #f)
        ((atom? y)
         (mbh-chk-length= e y -1))
        (else
          (mbh-chk-length>= e (cdr y) (- n 1)))))


(define (compile-mbh-file input-filename output-filename)
  (let ((i-port (open-input-file input-filename))
	(o-port (open-output-file output-filename)))

    (letrec
      ((protocol-classes '())		; alist

       (lookup-protocol-class		; returns protocol-class or #f
	(lambda (name)
	  (cond ((assq name protocol-classes) => cdr)
		(else #f))))

       (register-protocol-class
	(lambda (pc)
	  (set! protocol-classes (acons (protocol-class-name pc)
					  pc protocol-classes))
	  pc))
					  
       (parse-top-level-form
	(lambda (form)
	  (mbh-chk-length>= form form 1)
	  (case (car form)
	    ((define-protocol-class) (parse-define-protocol-class form))
	    (else (syntax-error form)))))

       (parse-define-protocol-class
	(lambda (form)               
	  (mbh-chk-length>= form form 2)
	    ;; form => (define-protocol-class name
	    ;;           (:include protocol-class-name)
	    ;;           (:incoming list-of-msgs)
	    ;;           (:outgoing list-of-msgs))
	    (let ((name (cadr form))
		  (incoming '())
		  (outgoing '()))
	      (if (lookup-protocol-class name)
		  (syntax-error "Duplicate protocol-class name" name))
	      (for-each
	       (lambda (sub-form)
		 (mbh-chk-length>= sub-form sub-form 1)
		 (case (car sub-form)
		   ((:include)
		    (mbh-chk-length>= sub-form sub-form 2)
		    (cond ((lookup-protocol-class (cadr sub-form)) =>
			   (lambda (pc)
			     (set! incoming (append incoming (protocol-class-incoming pc)))
			     (set! outgoing (append outgoing (protocol-class-outgoing pc)))))
			  (else
			   (syntax-error "Unknown protocol-class-name" (cadr sub-form)))))
		   ((:incoming)
		    (set! incoming (append incoming (cdr sub-form))))
		   ((:outgoing)
		    (set! outgoing (append outgoing (cdr sub-form))))
		   (else
		    (unrecognized-form (car sub-form)))))
	       (cddr form))
	      
	      (register-protocol-class (make-protocol-class name incoming outgoing)))))

       ) ; end of bindings

      (for-each-in-file i-port parse-top-level-form)

      ;; generate the output here...

      (letrec ((classes (map cdr protocol-classes))
	       (so-stream (make-serial-output-stream))
	       (format-output-for-c++
		(lambda (output)
		  (format o-port "//~%")
		  (format o-port "// Machine generated by compile-mbh from ~a~%" input-filename)
		  (format o-port "//~%")
		  (format o-port "// protocol-classes: ~{~a ~}~%" (map car protocol-classes))
		  (format o-port "//~%")

		  (format o-port "#include <mb_protocol_class.h>~%")
		  (format o-port "#include <unistd.h>~%")
		  (format o-port
			  "static const char~%protocol_class_init_data[~d] = {~%  "
			  (length output))

		  (do ((lst output (cdr lst))
		       (i 0 (+ i 1)))
		      ((null? lst) #t)
		    (format o-port "~a, " (car lst))
		    (when (= 15 (modulo i 16))
			  (format o-port "~%  ")))

		  (format o-port "~&};~%")
		  (format o-port "static mb_protocol_class_init _init_(protocol_class_init_data, sizeof(protocol_class_init_data));~%")
		  )))
		  
		  
	(map (lambda (pc)
	       (let ((obj-to-dump
		      (list (protocol-class-name pc)			; class name
			    (map car (protocol-class-incoming pc))	; incoming msg names
			    (map car (protocol-class-outgoing pc))	; outgoing msg names
			    ;;(protocol-class-incoming pc)		; full incoming msg descriptions
			    ;;(protocol-class-outgoing pc)		; full outgoing msg descriptions
			    )))	
		 ;;(pretty-print obj-to-dump)  
		 (pmt-serialize obj-to-dump (so-stream 'put-byte))))
	     classes)

	(format-output-for-c++ ((so-stream 'get-output)))

	#t))))


(define (make-serial-output-stream)
  (letrec ((output '())
	   (put-byte
	    (lambda (byte)
	      (set! output (cons byte output))))
	   (get-output
	    (lambda ()
	      (reverse output))))
    (lambda (key)
      (case key
	((put-byte) put-byte)
	((get-output) get-output)
	(else (error "Unknown key" key))))))