1
;=============================================================================
2
; (c) copyright 1988 Kent State University kent, ohio 44242
5
; Authors: Paul S. Wang, Barbara Gates
6
; Permission to use this work for any purpose is granted provided that
7
; the copyright notice, author and support credits above are retained.
8
;=============================================================================
10
(include-if (null (getd 'wrs)) convmac.l)
12
(declare (special *gentran-dir tempvartype* tempvarname* tempvarnum* genstmtno*
13
genstmtincr* *symboltable* *instk* *stdin* *currin* *outstk*
14
*stdout* *currout* *outchanl* *lispdefops* *lisparithexpops*
15
*lisplogexpops* *lispstmtops* *lispstmtgpops*))
17
;; segmnt.l ;; segmentation module
20
(declare (special *gentranopt *gentranlang maxexpprintlen*))
23
;; 1. segmentation routines ;;
29
; +--> (assign assign ... assign exp ) ;
33
; stmtgp -----> stmtgp ;
35
(foreach f in forms collect
37
(cond ((toolongexpp f)
44
(cond ((toolongstmtgpp f)
49
(cond ((toolongdefp f)
57
; exp --> (assign assign ... assign exp ) ;
59
(reverse (segexp1 exp type)))
61
(de segexp1 (exp type)
62
; exp --> (exp assign assign ... assign ) ;
63
; (n) (n-1) (n-2) (1) ;
64
(prog (res tempvarname)
65
(setq tempvarname tempvarname*)
67
(setq tempvarname (stripdollar1 tempvarname))
68
(setq tempvarname* (explode2 tempvarname))
69
(setq tempvarname* (compress (cons (car tempvarname*)
71
(setq res (segexp2 exp type))
73
(setq tempvarname* tempvarname)
74
(cond ((equal (car res) (cadadr res))
77
(rplaca res (caddar res)))))
80
(de segexp2 (exp type)
81
; exp --> (exp assign assign ... assign ) ;
82
; (n) (n-1) (n-2) (1) ;
83
(prog (expn assigns newassigns unops op termlist var tmp)
85
(while (equal (length expn) 2)
87
(setq unops (cons (car expn) unops))
88
(setq expn (cadr expn))))
90
(foreach term in (cdr expn) do
92
(cond ((toolongexpp term)
94
(setq tmp (segexp2 term type))
96
(setq newassigns (cdr tmp))))
98
(setq newassigns 'nil)))
99
(cond ((and (toolongexpp (cons op (cons term termlist)))
101
(or (> (length termlist) 1)
102
(listp (car termlist))))
104
(recurunmark termlist)
105
(setq var (or var (tempvar type)))
109
(cond ((onep (length termlist))
112
(cons op termlist))))
114
(setq termlist (list var term))))
116
(setq termlist (aconc termlist term))))
117
(setq assigns (append newassigns assigns))))
118
(setq expn (cond ((onep (length termlist))
121
(cons op termlist))))
124
(setq expn (list (car unops) expn))
125
(setq unops (cdr unops))))
126
(cond ((equal expn exp)
129
(setq var (or var (tempvar type)))
131
(setq assigns (list (mkassign var expn)))
133
(return (cons expn assigns))))
136
; assign --+--> assign ;
142
; return --+--> return ;
144
(cond ((lispassignp stmt)
145
(cond ((toolongassignp stmt)
150
(cond ((toolongcondp stmt)
155
(cond ((toolongdop stmt)
160
(cond ((toolongreturnp stmt)
168
; assign --> stmtgp ;
170
(setq var (cadr stmt))
171
(setq type (getvartype var))
172
(setq exp (caddr stmt))
173
(setq stmt (segexp1 exp type))
174
(rplaca stmt (mkassign var (car stmt)))
175
(return (mkstmtgp 0 (reverse stmt)))))
180
(prog (tassigns res markedvars type)
181
(cond ((eq *gentranlang 'c)
184
(setq type 'logical)))
185
(while (setq cond (cdr cond))
187
(cond ((toolongexpp (setq exp (caar cond)))
189
(setq exp (segexp1 exp type))
190
(setq tassigns (append (cdr exp) tassigns))
193
(setq markedvars (cons exp markedvars)))))
194
(setq stmt (foreach st in (cdar cond) collect
196
(setq res (cons (cons exp stmt) res))))
197
(recurunmark markedvars)
198
(return (cond (tassigns
200
(reverse (cons (mkcond (reverse res))
203
(mkcond (reverse res)))))))
208
(prog (tassigns var initexp nextexp exitcond body markedvars type)
209
(setq body (cdddr stmt))
210
(cond ((setq var (cadr stmt))
212
(cond ((toolongexpp (setq initexp (cadar var)))
214
(setq type (getvartype (caar var)))
215
(setq initexp (segexp1 initexp type))
216
(setq tassigns (cdr initexp))
217
(setq initexp (car initexp))
219
(setq markedvars (cons initexp markedvars)))))
220
(cond ((toolongexpp (setq nextexp (caddar var)))
222
(setq type (getvartype (caar var)))
223
(setq nextexp (segexp1 nextexp type))
224
(setq body (append body (reverse (cdr nextexp))))
225
(setq nextexp (car nextexp))
227
(setq markedvars (cons nextexp markedvars)))))
228
(setq var (list (list (caar var) initexp nextexp))))))
229
(cond ((toolongexpp (car (setq exitcond (caddr stmt))))
231
(cond ((eq *gentranlang 'c)
234
(setq ltype 'logical)))
235
(setq texps (segexp1 (car exitcond) ltype))
236
(markvar (car texps))
237
(setq markedvars (cons (car texps) markedvars))
238
(rplaca exitcond (car texps))
239
(foreach texp in (reverse (cdr texps)) do
241
(setq texp (reverse texp))
243
(cons (cdr (reverse (cons (car texp)
246
(setq var (reverse var)))))
247
(setq body (foreach st in body collect (segstmt st)))
248
(recurunmark markedvars)
249
(return (cond (tassigns
250
(mkstmtgp 0 (reverse (cons (mkdo var exitcond body)
253
(mkdo var exitcond body))))))
256
; return --> stmtgp ;
258
(setq ret (segexp1 (cadr ret) 'unknown))
259
(rplaca ret (mkreturn (car ret)))
260
(mkstmtgp 0 (reverse ret))))
262
(de seggroup (stmtgp)
263
; stmtgp --> stmtgp ;
265
(cond ((equal (car stmtgp) 'prog)
267
(setq locvars (cadr stmtgp))
268
(setq stmtgp (cdr stmtgp))))
271
(while (setq stmtgp (cdr stmtgp))
272
(setq res (cons (segstmt (car stmtgp)) res)))
273
(return (mkstmtgp locvars (reverse res)))))
279
(foreach stmt in (cdddr def) collect (segstmt stmt))))
283
;; 2. long statement & expression predicates ;;
287
(de toolongexpp (exp)
288
(greaterp (numprintlen exp) maxexpprintlen*))
290
(de toolongstmtp (stmt)
291
(cond ((atom stmt) nil) ;; pwang 11/11/86
293
(cond ((lispcondp stmt)
296
(toolongassignp stmt))
298
(toolongreturnp stmt))
303
(foreach exp in stmt collect (toolongexpp exp)))))))
305
(toolongstmtgpp stmt))))
307
(de toolongassignp (assign)
308
(toolongexpp (caddr assign)))
310
(de toolongcondp (cond)
312
(while (setq cond (cdr cond))
313
(cond ((or (toolongexpp (caar cond))
314
(toolongstmtp (cadar cond)))
318
(de toolongdop (dostmt)
319
(cond ((greaterp (eval (cons 'plus (foreach exp in (caadr dostmt) collect
322
((toolongexpp (caaddr dostmt)) t)
323
((lispstmtgpp (cadddr dostmt)) (toolongstmtgpp (cadddr dostmt)))
324
(t (eval (cons 'or (foreach stmt in (cdddr dostmt) collect
325
(toolongstmtp stmt)))))))
327
(de toolongreturnp (ret)
328
(toolongexpp (cadr ret)))
330
(de toolongstmtgpp (stmtgp)
332
(foreach stmt in (cdr stmtgp) collect (toolongstmtp stmt)))))
334
(de toolongdefp (def)
335
(cond ((lispstmtgpp (cadddr def))
336
(toolongstmtgpp (cadddr def)))
339
(foreach stmt in (cdddr def) collect
340
(toolongstmtp stmt)))))))
344
;; 3. print length function ;;
348
(de numprintlen (exp)
350
(length (explode exp)))
352
(numprintlen (car exp)))
356
(foreach elt in (cdr exp) collect
357
(numprintlen elt))))))))