Mercurial > hg > xemacs-beta
comparison lisp/mule/mule-ccl.el @ 392:1f50e6fe4f3f r21-2-11
Import from CVS: tag r21-2-11
author | cvs |
---|---|
date | Mon, 13 Aug 2007 11:10:50 +0200 |
parents | 4f79e16b1112 |
children | 74fd4e045ea6 |
comparison
equal
deleted
inserted
replaced
391:e50d8e68d7a5 | 392:1f50e6fe4f3f |
---|---|
1 ;;; ccl.el --- CCL (Code Conversion Language) compiler | |
2 | |
3 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN. | |
4 ;; Licensed to the Free Software Foundation. | |
5 | |
6 ;; Keywords: CCL, mule, multilingual, character set, coding-system | |
7 | |
8 ;; This file is part of X Emacs. | |
9 | |
10 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
11 ;; it under the terms of the GNU General Public License as published by | |
12 ;; the Free Software Foundation; either version 2, or (at your option) | |
13 ;; any later version. | |
14 | |
15 ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 ;; GNU General Public License for more details. | |
19 | |
20 ;; You should have received a copy of the GNU General Public License | |
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 ;; Boston, MA 02111-1307, USA. | |
24 | |
25 ;; Synched up with: FSF 20.2 | |
26 | |
27 ;;; Commentary: | |
28 | |
29 ;; CCL (Code Conversion Language) is a simple programming language to | |
30 ;; be used for various kind of code conversion. CCL program is | |
31 ;; compiled to CCL code (vector of integers) and executed by CCL | |
32 ;; interpreter of Emacs. | |
33 ;; | |
34 ;; CCL is used for code conversion at process I/O and file I/O for | |
35 ;; non-standard coding-system. In addition, it is used for | |
36 ;; calculating a code point of X's font from a character code. | |
37 ;; However, since CCL is designed as a powerful programming language, | |
38 ;; it can be used for more generic calculation. For instance, | |
39 ;; combination of three or more arithmetic operations can be | |
40 ;; calculated faster than Emacs Lisp. | |
41 ;; | |
42 ;; Here's the syntax of CCL program in BNF notation. | |
43 ;; | |
44 ;; CCL_PROGRAM := | |
45 ;; (BUFFER_MAGNIFICATION | |
46 ;; CCL_MAIN_BLOCK | |
47 ;; [ CCL_EOF_BLOCK ]) | |
48 ;; | |
49 ;; BUFFER_MAGNIFICATION := integer | |
50 ;; CCL_MAIN_BLOCK := CCL_BLOCK | |
51 ;; CCL_EOF_BLOCK := CCL_BLOCK | |
52 ;; | |
53 ;; CCL_BLOCK := | |
54 ;; STATEMENT | (STATEMENT [STATEMENT ...]) | |
55 ;; STATEMENT := | |
56 ;; SET | IF | BRANCH | LOOP | REPEAT | BREAK | READ | WRITE | CALL | |
57 ;; | |
58 ;; SET := | |
59 ;; (REG = EXPRESSION) | |
60 ;; | (REG ASSIGNMENT_OPERATOR EXPRESSION) | |
61 ;; | integer | |
62 ;; | |
63 ;; EXPRESSION := ARG | (EXPRESSION OPERATOR ARG) | |
64 ;; | |
65 ;; IF := (if EXPRESSION CCL_BLOCK CCL_BLOCK) | |
66 ;; BRANCH := (branch EXPRESSION CCL_BLOCK [CCL_BLOCK ...]) | |
67 ;; LOOP := (loop STATEMENT [STATEMENT ...]) | |
68 ;; BREAK := (break) | |
69 ;; REPEAT := | |
70 ;; (repeat) | |
71 ;; | (write-repeat [REG | integer | string]) | |
72 ;; | (write-read-repeat REG [integer | ARRAY]) | |
73 ;; READ := | |
74 ;; (read REG ...) | |
75 ;; | (read-if (REG OPERATOR ARG) CCL_BLOCK CCL_BLOCK) | |
76 ;; | (read-branch REG CCL_BLOCK [CCL_BLOCK ...]) | |
77 ;; WRITE := | |
78 ;; (write REG ...) | |
79 ;; | (write EXPRESSION) | |
80 ;; | (write integer) | (write string) | (write REG ARRAY) | |
81 ;; | string | |
82 ;; CALL := (call ccl-program-name) | |
83 ;; END := (end) | |
84 ;; | |
85 ;; REG := r0 | r1 | r2 | r3 | r4 | r5 | r6 | r7 | |
86 ;; ARG := REG | integer | |
87 ;; OPERATOR := | |
88 ;; + | - | * | / | % | & | '|' | ^ | << | >> | <8 | >8 | // | |
89 ;; | < | > | == | <= | >= | != | de-sjis | en-sjis | |
90 ;; ASSIGNMENT_OPERATOR := | |
91 ;; += | -= | *= | /= | %= | &= | '|=' | ^= | <<= | >>= | |
92 ;; ARRAY := '[' interger ... ']' | |
93 | |
94 ;;; Code: | |
95 | |
96 (defconst ccl-command-table | |
97 [if branch loop break repeat write-repeat write-read-repeat | |
98 read read-if read-branch write call end] | |
99 "*Vector of CCL commands (symbols).") | |
100 | |
101 ;; Put a property to each symbol of CCL commands for the compiler. | |
102 (let (op (i 0) (len (length ccl-command-table))) | |
103 (while (< i len) | |
104 (setq op (aref ccl-command-table i)) | |
105 (put op 'ccl-compile-function (intern (format "ccl-compile-%s" op))) | |
106 (setq i (1+ i)))) | |
107 | |
108 (defconst ccl-code-table | |
109 [set-register | |
110 set-short-const | |
111 set-const | |
112 set-array | |
113 jump | |
114 jump-cond | |
115 write-register-jump | |
116 write-register-read-jump | |
117 write-const-jump | |
118 write-const-read-jump | |
119 write-string-jump | |
120 write-array-read-jump | |
121 read-jump | |
122 branch | |
123 read-register | |
124 write-expr-const | |
125 read-branch | |
126 write-register | |
127 write-expr-register | |
128 call | |
129 write-const-string | |
130 write-array | |
131 end | |
132 set-assign-expr-const | |
133 set-assign-expr-register | |
134 set-expr-const | |
135 set-expr-register | |
136 jump-cond-expr-const | |
137 jump-cond-expr-register | |
138 read-jump-cond-expr-const | |
139 read-jump-cond-expr-register | |
140 ] | |
141 "*Vector of CCL compiled codes (symbols).") | |
142 | |
143 ;; Put a property to each symbol of CCL codes for the disassembler. | |
144 (let (code (i 0) (len (length ccl-code-table))) | |
145 (while (< i len) | |
146 (setq code (aref ccl-code-table i)) | |
147 (put code 'ccl-code i) | |
148 (put code 'ccl-dump-function (intern (format "ccl-dump-%s" code))) | |
149 (setq i (1+ i)))) | |
150 | |
151 (defconst ccl-jump-code-list | |
152 '(jump jump-cond write-register-jump write-register-read-jump | |
153 write-const-jump write-const-read-jump write-string-jump | |
154 write-array-read-jump read-jump)) | |
155 | |
156 ;; Put a property `jump-flag' to each CCL code which execute jump in | |
157 ;; some way. | |
158 (let ((l ccl-jump-code-list)) | |
159 (while l | |
160 (put (car l) 'jump-flag t) | |
161 (setq l (cdr l)))) | |
162 | |
163 (defconst ccl-register-table | |
164 [r0 r1 r2 r3 r4 r5 r6 r7] | |
165 "*Vector of CCL registers (symbols).") | |
166 | |
167 ;; Put a property to indicate register number to each symbol of CCL. | |
168 ;; registers. | |
169 (let (reg (i 0) (len (length ccl-register-table))) | |
170 (while (< i len) | |
171 (setq reg (aref ccl-register-table i)) | |
172 (put reg 'ccl-register-number i) | |
173 (setq i (1+ i)))) | |
174 | |
175 (defconst ccl-arith-table | |
176 [+ - * / % & | ^ << >> <8 >8 // nil nil nil | |
177 < > == <= >= != de-sjis en-sjis] | |
178 "*Vector of CCL arithmetic/logical operators (symbols).") | |
179 | |
180 ;; Put a property to each symbol of CCL operators for the compiler. | |
181 (let (arith (i 0) (len (length ccl-arith-table))) | |
182 (while (< i len) | |
183 (setq arith (aref ccl-arith-table i)) | |
184 (if arith (put arith 'ccl-arith-code i)) | |
185 (setq i (1+ i)))) | |
186 | |
187 (defconst ccl-assign-arith-table | |
188 [+= -= *= /= %= &= |= ^= <<= >>= <8= >8= //=] | |
189 "*Vector of CCL assignment operators (symbols).") | |
190 | |
191 ;; Put a property to each symbol of CCL assignment operators for the compiler. | |
192 (let (arith (i 0) (len (length ccl-assign-arith-table))) | |
193 (while (< i len) | |
194 (setq arith (aref ccl-assign-arith-table i)) | |
195 (put arith 'ccl-self-arith-code i) | |
196 (setq i (1+ i)))) | |
197 | |
198 (defvar ccl-program-vector nil | |
199 "Working vector of CCL codes produced by CCL compiler.") | |
200 (defvar ccl-current-ic 0 | |
201 "The current index for `ccl-program-vector'.") | |
202 | |
203 ;; Embed integer DATA in `ccl-program-vector' at `ccl-current-ic' and | |
204 ;; increment it. If IC is specified, embed DATA at IC. | |
205 (defun ccl-embed-data (data &optional ic) | |
206 (let ((val (if (characterp data) (char-int data) data))) | |
207 (if ic | |
208 (aset ccl-program-vector ic val) | |
209 (aset ccl-program-vector ccl-current-ic val) | |
210 (setq ccl-current-ic (1+ ccl-current-ic))))) | |
211 | |
212 ;; Embed string STR of length LEN in `ccl-program-vector' at | |
213 ;; `ccl-current-ic'. | |
214 (defun ccl-embed-string (len str) | |
215 (let ((i 0)) | |
216 (while (< i len) | |
217 (ccl-embed-data (logior (ash (aref str i) 16) | |
218 (if (< (1+ i) len) | |
219 (ash (aref str (1+ i)) 8) | |
220 0) | |
221 (if (< (+ i 2) len) | |
222 (aref str (+ i 2)) | |
223 0))) | |
224 (setq i (+ i 3))))) | |
225 | |
226 ;; Embed a relative jump address to `ccl-current-ic' in | |
227 ;; `ccl-program-vector' at IC without altering the other bit field. | |
228 (defun ccl-embed-current-address (ic) | |
229 (let ((relative (- ccl-current-ic (1+ ic)))) | |
230 (aset ccl-program-vector ic | |
231 (logior (aref ccl-program-vector ic) (ash relative 8))))) | |
232 | |
233 ;; Embed CCL code for the operation OP and arguments REG and DATA in | |
234 ;; `ccl-program-vector' at `ccl-current-ic' in the following format. | |
235 ;; |----------------- integer (28-bit) ------------------| | |
236 ;; |------------ 20-bit ------------|- 3-bit --|- 5-bit -| | |
237 ;; |------------- DATA -------------|-- REG ---|-- OP ---| | |
238 ;; If REG2 is specified, embed a code in the following format. | |
239 ;; |------- 17-bit ------|- 3-bit --|- 3-bit --|- 5-bit -| | |
240 ;; |-------- DATA -------|-- REG2 --|-- REG ---|-- OP ---| | |
241 | |
242 ;; If REG is a CCL register symbol (e.g. r0, r1...), the register | |
243 ;; number is embedded. If OP is one of unconditional jumps, DATA is | |
244 ;; changed to an relative jump address. | |
245 | |
246 (defun ccl-embed-code (op reg data &optional reg2) | |
247 (if (and (> data 0) (get op 'jump-flag)) | |
248 ;; DATA is an absolute jump address. Make it relative to the | |
249 ;; next of jump code. | |
250 (setq data (- data (1+ ccl-current-ic)))) | |
251 (let ((code (logior (get op 'ccl-code) | |
252 (ash | |
253 (if (symbolp reg) (get reg 'ccl-register-number) reg) 5) | |
254 (if reg2 | |
255 (logior (ash (get reg2 'ccl-register-number) 8) | |
256 (ash data 11)) | |
257 (ash data 8))))) | |
258 (aset ccl-program-vector ccl-current-ic code) | |
259 (setq ccl-current-ic (1+ ccl-current-ic)))) | |
260 | |
261 ;; Just advance `ccl-current-ic' by INC. | |
262 (defun ccl-increment-ic (inc) | |
263 (setq ccl-current-ic (+ ccl-current-ic inc))) | |
264 | |
265 ;;;###autoload | |
266 (defun ccl-program-p (obj) | |
267 "T if OBJECT is a valid CCL compiled code." | |
268 (and (vectorp obj) | |
269 (let ((i 0) (len (length obj)) (flag t)) | |
270 (if (> len 1) | |
271 (progn | |
272 (while (and flag (< i len)) | |
273 (setq flag (integerp (aref obj i))) | |
274 (setq i (1+ i))) | |
275 flag))))) | |
276 | |
277 ;; If non-nil, index of the start of the current loop. | |
278 (defvar ccl-loop-head nil) | |
279 ;; If non-nil, list of absolute addresses of the breaking points of | |
280 ;; the current loop. | |
281 (defvar ccl-breaks nil) | |
282 | |
283 ;;;###autoload | |
284 (defun ccl-compile (ccl-program) | |
285 "Return a compiled code of CCL-PROGRAM as a vector of integer." | |
286 (if (or (null (consp ccl-program)) | |
287 (null (integer-or-char-p (car ccl-program))) | |
288 (null (listp (car (cdr ccl-program))))) | |
289 (error "CCL: Invalid CCL program: %s" ccl-program)) | |
290 (if (null (vectorp ccl-program-vector)) | |
291 (setq ccl-program-vector (make-vector 8192 0))) | |
292 (setq ccl-loop-head nil ccl-breaks nil) | |
293 (setq ccl-current-ic 0) | |
294 | |
295 ;; The first element is the buffer magnification. | |
296 (ccl-embed-data (car ccl-program)) | |
297 | |
298 ;; The second element is the address of the start CCL code for | |
299 ;; processing end of input buffer (we call it eof-processor). We | |
300 ;; set it later. | |
301 (ccl-increment-ic 1) | |
302 | |
303 ;; Compile the main body of the CCL program. | |
304 (ccl-compile-1 (car (cdr ccl-program))) | |
305 | |
306 ;; Embed the address of eof-processor. | |
307 (ccl-embed-data ccl-current-ic 1) | |
308 | |
309 ;; Then compile eof-processor. | |
310 (if (nth 2 ccl-program) | |
311 (ccl-compile-1 (nth 2 ccl-program))) | |
312 | |
313 ;; At last, embed termination code. | |
314 (ccl-embed-code 'end 0 0) | |
315 | |
316 (let ((vec (make-vector ccl-current-ic 0)) | |
317 (i 0)) | |
318 (while (< i ccl-current-ic) | |
319 (aset vec i (aref ccl-program-vector i)) | |
320 (setq i (1+ i))) | |
321 vec)) | |
322 | |
323 ;; Signal syntax error. | |
324 (defun ccl-syntax-error (cmd) | |
325 (error "CCL: Syntax error: %s" cmd)) | |
326 | |
327 ;; Check if ARG is a valid CCL register. | |
328 (defun ccl-check-register (arg cmd) | |
329 (if (get arg 'ccl-register-number) | |
330 arg | |
331 (error "CCL: Invalid register %s in %s." arg cmd))) | |
332 | |
333 ;; Check if ARG is a valid CCL command. | |
334 (defun ccl-check-compile-function (arg cmd) | |
335 (or (get arg 'ccl-compile-function) | |
336 (error "CCL: Invalid command: %s" cmd))) | |
337 | |
338 ;; In the following code, most ccl-compile-XXXX functions return t if | |
339 ;; they end with unconditional jump, else return nil. | |
340 | |
341 ;; Compile CCL-BLOCK (see the syntax above). | |
342 (defun ccl-compile-1 (ccl-block) | |
343 (let (unconditional-jump | |
344 cmd) | |
345 (if (or (integer-or-char-p ccl-block) | |
346 (stringp ccl-block) | |
347 (and ccl-block (symbolp (car ccl-block)))) | |
348 ;; This block consists of single statement. | |
349 (setq ccl-block (list ccl-block))) | |
350 | |
351 ;; Now CCL-BLOCK is a list of statements. Compile them one by | |
352 ;; one. | |
353 (while ccl-block | |
354 (setq cmd (car ccl-block)) | |
355 (setq unconditional-jump | |
356 (cond ((integer-or-char-p cmd) | |
357 ;; SET statement for the register 0. | |
358 (ccl-compile-set (list 'r0 '= cmd))) | |
359 | |
360 ((stringp cmd) | |
361 ;; WRITE statement of string argument. | |
362 (ccl-compile-write-string cmd)) | |
363 | |
364 ((listp cmd) | |
365 ;; The other statements. | |
366 (cond ((eq (nth 1 cmd) '=) | |
367 ;; SET statement of the form `(REG = EXPRESSION)'. | |
368 (ccl-compile-set cmd)) | |
369 | |
370 ((and (symbolp (nth 1 cmd)) | |
371 (get (nth 1 cmd) 'ccl-self-arith-code)) | |
372 ;; SET statement with an assignment operation. | |
373 (ccl-compile-self-set cmd)) | |
374 | |
375 (t | |
376 (funcall (ccl-check-compile-function (car cmd) cmd) | |
377 cmd)))) | |
378 | |
379 (t | |
380 (ccl-syntax-error cmd)))) | |
381 (setq ccl-block (cdr ccl-block))) | |
382 unconditional-jump)) | |
383 | |
384 (defconst ccl-max-short-const (ash 1 19)) | |
385 (defconst ccl-min-short-const (ash -1 19)) | |
386 | |
387 ;; Compile SET statement. | |
388 (defun ccl-compile-set (cmd) | |
389 (let ((rrr (ccl-check-register (car cmd) cmd)) | |
390 (right (nth 2 cmd))) | |
391 (cond ((listp right) | |
392 ;; CMD has the form `(RRR = (XXX OP YYY))'. | |
393 (ccl-compile-expression rrr right)) | |
394 | |
395 ((integer-or-char-p right) | |
396 ;; CMD has the form `(RRR = integer)'. | |
397 (if (and (<= right ccl-max-short-const) | |
398 (>= right ccl-min-short-const)) | |
399 (ccl-embed-code 'set-short-const rrr right) | |
400 (ccl-embed-code 'set-const rrr 0) | |
401 (ccl-embed-data right))) | |
402 | |
403 (t | |
404 ;; CMD has the form `(RRR = rrr [ array ])'. | |
405 (ccl-check-register right cmd) | |
406 (let ((ary (nth 3 cmd))) | |
407 (if (vectorp ary) | |
408 (let ((i 0) (len (length ary))) | |
409 (ccl-embed-code 'set-array rrr len right) | |
410 (while (< i len) | |
411 (ccl-embed-data (aref ary i)) | |
412 (setq i (1+ i)))) | |
413 (ccl-embed-code 'set-register rrr 0 right)))))) | |
414 nil) | |
415 | |
416 ;; Compile SET statement with ASSIGNMENT_OPERATOR. | |
417 (defun ccl-compile-self-set (cmd) | |
418 (let ((rrr (ccl-check-register (car cmd) cmd)) | |
419 (right (nth 2 cmd))) | |
420 (if (listp right) | |
421 ;; CMD has the form `(RRR ASSIGN_OP (XXX OP YYY))', compile | |
422 ;; the right hand part as `(r7 = (XXX OP YYY))' (note: the | |
423 ;; register 7 can be used for storing temporary value). | |
424 (progn | |
425 (ccl-compile-expression 'r7 right) | |
426 (setq right 'r7))) | |
427 ;; Now CMD has the form `(RRR ASSIGN_OP ARG)'. Compile it as | |
428 ;; `(RRR = (RRR OP ARG))'. | |
429 (ccl-compile-expression | |
430 rrr | |
431 (list rrr (intern (substring (symbol-name (nth 1 cmd)) 0 -1)) right))) | |
432 nil) | |
433 | |
434 ;; Compile SET statement of the form `(RRR = EXPR)'. | |
435 (defun ccl-compile-expression (rrr expr) | |
436 (let ((left (car expr)) | |
437 (op (get (nth 1 expr) 'ccl-arith-code)) | |
438 (right (nth 2 expr))) | |
439 (if (listp left) | |
440 (progn | |
441 ;; EXPR has the form `((EXPR2 OP2 ARG) OP RIGHT)'. Compile | |
442 ;; the first term as `(r7 = (EXPR2 OP2 ARG)).' | |
443 (ccl-compile-expression 'r7 left) | |
444 (setq left 'r7))) | |
445 | |
446 ;; Now EXPR has the form (LEFT OP RIGHT). | |
447 (if (eq rrr left) | |
448 ;; Compile this SET statement as `(RRR OP= RIGHT)'. | |
449 (if (integer-or-char-p right) | |
450 (progn | |
451 (ccl-embed-code 'set-assign-expr-const rrr (ash op 3) 'r0) | |
452 (ccl-embed-data right)) | |
453 (ccl-check-register right expr) | |
454 (ccl-embed-code 'set-assign-expr-register rrr (ash op 3) right)) | |
455 | |
456 ;; Compile this SET statement as `(RRR = (LEFT OP RIGHT))'. | |
457 (if (integer-or-char-p right) | |
458 (progn | |
459 (ccl-embed-code 'set-expr-const rrr (ash op 3) left) | |
460 (ccl-embed-data right)) | |
461 (ccl-check-register right expr) | |
462 (ccl-embed-code 'set-expr-register | |
463 rrr | |
464 (logior (ash op 3) (get right 'ccl-register-number)) | |
465 left))))) | |
466 | |
467 ;; Compile WRITE statement with string argument. | |
468 (defun ccl-compile-write-string (str) | |
469 (let ((len (length str))) | |
470 (ccl-embed-code 'write-const-string 1 len) | |
471 (ccl-embed-string len str)) | |
472 nil) | |
473 | |
474 ;; Compile IF statement of the form `(if CONDITION TRUE-PART FALSE-PART)'. | |
475 ;; If READ-FLAG is non-nil, this statement has the form | |
476 ;; `(read-if (REG OPERATOR ARG) TRUE-PART FALSE-PART)'. | |
477 (defun ccl-compile-if (cmd &optional read-flag) | |
478 (if (and (/= (length cmd) 3) (/= (length cmd) 4)) | |
479 (error "CCL: Invalid number of arguments: %s" cmd)) | |
480 (let ((condition (nth 1 cmd)) | |
481 (true-cmds (nth 2 cmd)) | |
482 (false-cmds (nth 3 cmd)) | |
483 jump-cond-address | |
484 false-ic) | |
485 (if (and (listp condition) | |
486 (listp (car condition))) | |
487 ;; If CONDITION is a nested expression, the inner expression | |
488 ;; should be compiled at first as SET statement, i.e.: | |
489 ;; `(if ((X OP2 Y) OP Z) ...)' is compiled into two statements: | |
490 ;; `(r7 = (X OP2 Y)) (if (r7 OP Z) ...)'. | |
491 (progn | |
492 (ccl-compile-expression 'r7 (car condition)) | |
493 (setq condition (cons 'r7 (cdr condition))) | |
494 (setq cmd (cons (car cmd) | |
495 (cons condition (cdr (cdr cmd))))))) | |
496 | |
497 (setq jump-cond-address ccl-current-ic) | |
498 ;; Compile CONDITION. | |
499 (if (symbolp condition) | |
500 ;; CONDITION is a register. | |
501 (progn | |
502 (ccl-check-register condition cmd) | |
503 (ccl-embed-code 'jump-cond condition 0)) | |
504 ;; CONDITION is a simple expression of the form (RRR OP ARG). | |
505 (let ((rrr (car condition)) | |
506 (op (get (nth 1 condition) 'ccl-arith-code)) | |
507 (arg (nth 2 condition))) | |
508 (ccl-check-register rrr cmd) | |
509 (if (integer-or-char-p arg) | |
510 (progn | |
511 (ccl-embed-code (if read-flag 'read-jump-cond-expr-const | |
512 'jump-cond-expr-const) | |
513 rrr 0) | |
514 (ccl-embed-data op) | |
515 (ccl-embed-data arg)) | |
516 (ccl-check-register arg cmd) | |
517 (ccl-embed-code (if read-flag 'read-jump-cond-expr-register | |
518 'jump-cond-expr-register) | |
519 rrr 0) | |
520 (ccl-embed-data op) | |
521 (ccl-embed-data (get arg 'ccl-register-number))))) | |
522 | |
523 ;; Compile TRUE-PART. | |
524 (let ((unconditional-jump (ccl-compile-1 true-cmds))) | |
525 (if (null false-cmds) | |
526 ;; This is the place to jump to if condition is false. | |
527 (ccl-embed-current-address jump-cond-address) | |
528 (let (end-true-part-address) | |
529 (if (not unconditional-jump) | |
530 (progn | |
531 ;; If TRUE-PART does not end with unconditional jump, we | |
532 ;; have to jump to the end of FALSE-PART from here. | |
533 (setq end-true-part-address ccl-current-ic) | |
534 (ccl-embed-code 'jump 0 0))) | |
535 ;; This is the place to jump to if CONDITION is false. | |
536 (ccl-embed-current-address jump-cond-address) | |
537 ;; Compile FALSE-PART. | |
538 (setq unconditional-jump | |
539 (and (ccl-compile-1 false-cmds) unconditional-jump)) | |
540 (if end-true-part-address | |
541 ;; This is the place to jump to after the end of TRUE-PART. | |
542 (ccl-embed-current-address end-true-part-address)))) | |
543 unconditional-jump))) | |
544 | |
545 ;; Compile BRANCH statement. | |
546 (defun ccl-compile-branch (cmd) | |
547 (if (< (length cmd) 3) | |
548 (error "CCL: Invalid number of arguments: %s" cmd)) | |
549 (ccl-compile-branch-blocks 'branch | |
550 (ccl-compile-branch-expression (nth 1 cmd) cmd) | |
551 (cdr (cdr cmd)))) | |
552 | |
553 ;; Compile READ statement of the form `(read-branch EXPR BLOCK0 BLOCK1 ...)'. | |
554 (defun ccl-compile-read-branch (cmd) | |
555 (if (< (length cmd) 3) | |
556 (error "CCL: Invalid number of arguments: %s" cmd)) | |
557 (ccl-compile-branch-blocks 'read-branch | |
558 (ccl-compile-branch-expression (nth 1 cmd) cmd) | |
559 (cdr (cdr cmd)))) | |
560 | |
561 ;; Compile EXPRESSION part of BRANCH statement and return register | |
562 ;; which holds a value of the expression. | |
563 (defun ccl-compile-branch-expression (expr cmd) | |
564 (if (listp expr) | |
565 ;; EXPR has the form `(EXPR2 OP ARG)'. Compile it as SET | |
566 ;; statement of the form `(r7 = (EXPR2 OP ARG))'. | |
567 (progn | |
568 (ccl-compile-expression 'r7 expr) | |
569 'r7) | |
570 (ccl-check-register expr cmd))) | |
571 | |
572 ;; Compile BLOCKs of BRANCH statement. CODE is 'branch or 'read-branch. | |
573 ;; REG is a register which holds a value of EXPRESSION part. BLOCKs | |
574 ;; is a list of CCL-BLOCKs. | |
575 (defun ccl-compile-branch-blocks (code rrr blocks) | |
576 (let ((branches (length blocks)) | |
577 branch-idx | |
578 jump-table-head-address | |
579 empty-block-indexes | |
580 block-tail-addresses | |
581 block-unconditional-jump) | |
582 (ccl-embed-code code rrr branches) | |
583 (setq jump-table-head-address ccl-current-ic) | |
584 ;; The size of jump table is the number of blocks plus 1 (for the | |
585 ;; case RRR is out of range). | |
586 (ccl-increment-ic (1+ branches)) | |
587 (setq empty-block-indexes (list branches)) | |
588 ;; Compile each block. | |
589 (setq branch-idx 0) | |
590 (while blocks | |
591 (if (null (car blocks)) | |
592 ;; This block is empty. | |
593 (setq empty-block-indexes (cons branch-idx empty-block-indexes) | |
594 block-unconditional-jump t) | |
595 ;; This block is not empty. | |
596 (ccl-embed-data (- ccl-current-ic jump-table-head-address) | |
597 (+ jump-table-head-address branch-idx)) | |
598 (setq block-unconditional-jump (ccl-compile-1 (car blocks))) | |
599 (if (not block-unconditional-jump) | |
600 (progn | |
601 ;; Jump address of the end of branches are embedded later. | |
602 ;; For the moment, just remember where to embed them. | |
603 (setq block-tail-addresses | |
604 (cons ccl-current-ic block-tail-addresses)) | |
605 (ccl-embed-code 'jump 0 0)))) | |
606 (setq branch-idx (1+ branch-idx)) | |
607 (setq blocks (cdr blocks))) | |
608 (if (not block-unconditional-jump) | |
609 ;; We don't need jump code at the end of the last block. | |
610 (setq block-tail-addresses (cdr block-tail-addresses) | |
611 ccl-current-ic (1- ccl-current-ic))) | |
612 ;; Embed jump address at the tailing jump commands of blocks. | |
613 (while block-tail-addresses | |
614 (ccl-embed-current-address (car block-tail-addresses)) | |
615 (setq block-tail-addresses (cdr block-tail-addresses))) | |
616 ;; For empty blocks, make entries in the jump table point directly here. | |
617 (while empty-block-indexes | |
618 (ccl-embed-data (- ccl-current-ic jump-table-head-address) | |
619 (+ jump-table-head-address (car empty-block-indexes))) | |
620 (setq empty-block-indexes (cdr empty-block-indexes)))) | |
621 ;; Branch command ends by unconditional jump if RRR is out of range. | |
622 nil) | |
623 | |
624 ;; Compile LOOP statement. | |
625 (defun ccl-compile-loop (cmd) | |
626 (if (< (length cmd) 2) | |
627 (error "CCL: Invalid number of arguments: %s" cmd)) | |
628 (let* ((ccl-loop-head ccl-current-ic) | |
629 (ccl-breaks nil) | |
630 unconditional-jump) | |
631 (setq cmd (cdr cmd)) | |
632 (if cmd | |
633 (progn | |
634 (setq unconditional-jump t) | |
635 (while cmd | |
636 (setq unconditional-jump | |
637 (and (ccl-compile-1 (car cmd)) unconditional-jump)) | |
638 (setq cmd (cdr cmd))) | |
639 (if (not ccl-breaks) | |
640 unconditional-jump | |
641 ;; Embed jump address for break statements encountered in | |
642 ;; this loop. | |
643 (while ccl-breaks | |
644 (ccl-embed-current-address (car ccl-breaks)) | |
645 (setq ccl-breaks (cdr ccl-breaks)))) | |
646 nil)))) | |
647 | |
648 ;; Compile BREAK statement. | |
649 (defun ccl-compile-break (cmd) | |
650 (if (/= (length cmd) 1) | |
651 (error "CCL: Invalid number of arguments: %s" cmd)) | |
652 (if (null ccl-loop-head) | |
653 (error "CCL: No outer loop: %s" cmd)) | |
654 (setq ccl-breaks (cons ccl-current-ic ccl-breaks)) | |
655 (ccl-embed-code 'jump 0 0) | |
656 t) | |
657 | |
658 ;; Compile REPEAT statement. | |
659 (defun ccl-compile-repeat (cmd) | |
660 (if (/= (length cmd) 1) | |
661 (error "CCL: Invalid number of arguments: %s" cmd)) | |
662 (if (null ccl-loop-head) | |
663 (error "CCL: No outer loop: %s" cmd)) | |
664 (ccl-embed-code 'jump 0 ccl-loop-head) | |
665 t) | |
666 | |
667 ;; Compile WRITE-REPEAT statement. | |
668 (defun ccl-compile-write-repeat (cmd) | |
669 (if (/= (length cmd) 2) | |
670 (error "CCL: Invalid number of arguments: %s" cmd)) | |
671 (if (null ccl-loop-head) | |
672 (error "CCL: No outer loop: %s" cmd)) | |
673 (let ((arg (nth 1 cmd))) | |
674 (cond ((integer-or-char-p arg) | |
675 (ccl-embed-code 'write-const-jump 0 ccl-loop-head) | |
676 (ccl-embed-data arg)) | |
677 ((stringp arg) | |
678 (let ((len (length arg)) | |
679 (i 0)) | |
680 (ccl-embed-code 'write-string-jump 0 ccl-loop-head) | |
681 (ccl-embed-data len) | |
682 (ccl-embed-string len arg))) | |
683 (t | |
684 (ccl-check-register arg cmd) | |
685 (ccl-embed-code 'write-register-jump arg ccl-loop-head)))) | |
686 t) | |
687 | |
688 ;; Compile WRITE-READ-REPEAT statement. | |
689 (defun ccl-compile-write-read-repeat (cmd) | |
690 (if (or (< (length cmd) 2) (> (length cmd) 3)) | |
691 (error "CCL: Invalid number of arguments: %s" cmd)) | |
692 (if (null ccl-loop-head) | |
693 (error "CCL: No outer loop: %s" cmd)) | |
694 (let ((rrr (ccl-check-register (nth 1 cmd) cmd)) | |
695 (arg (nth 2 cmd))) | |
696 (cond ((null arg) | |
697 (ccl-embed-code 'write-register-read-jump rrr ccl-loop-head)) | |
698 ((integer-or-char-p arg) | |
699 (ccl-embed-code 'write-const-read-jump rrr arg ccl-loop-head)) | |
700 ((vectorp arg) | |
701 (let ((len (length arg)) | |
702 (i 0)) | |
703 (ccl-embed-code 'write-array-read-jump rrr ccl-loop-head) | |
704 (ccl-embed-data len) | |
705 (while (< i len) | |
706 (ccl-embed-data (aref arg i)) | |
707 (setq i (1+ i))))) | |
708 (t | |
709 (error "CCL: Invalid argument %s: %s" arg cmd))) | |
710 (ccl-embed-code 'read-jump rrr ccl-loop-head)) | |
711 t) | |
712 | |
713 ;; Compile READ statement. | |
714 (defun ccl-compile-read (cmd) | |
715 (if (< (length cmd) 2) | |
716 (error "CCL: Invalid number of arguments: %s" cmd)) | |
717 (let* ((args (cdr cmd)) | |
718 (i (1- (length args)))) | |
719 (while args | |
720 (let ((rrr (ccl-check-register (car args) cmd))) | |
721 (ccl-embed-code 'read-register rrr i) | |
722 (setq args (cdr args) i (1- i))))) | |
723 nil) | |
724 | |
725 ;; Compile READ-IF statement. | |
726 (defun ccl-compile-read-if (cmd) | |
727 (ccl-compile-if cmd 'read)) | |
728 | |
729 ;; Compile WRITE statement. | |
730 (defun ccl-compile-write (cmd) | |
731 (if (< (length cmd) 2) | |
732 (error "CCL: Invalid number of arguments: %s" cmd)) | |
733 (let ((rrr (nth 1 cmd))) | |
734 (cond ((integer-or-char-p rrr) | |
735 (ccl-embed-code 'write-const-string 0 rrr)) | |
736 ((stringp rrr) | |
737 (ccl-compile-write-string rrr)) | |
738 ((and (symbolp rrr) (vectorp (nth 2 cmd))) | |
739 (ccl-check-register rrr cmd) | |
740 ;; CMD has the form `(write REG ARRAY)'. | |
741 (let* ((arg (nth 2 cmd)) | |
742 (len (length arg)) | |
743 (i 0)) | |
744 (ccl-embed-code 'write-array rrr len) | |
745 (while (< i len) | |
746 (if (not (integer-or-char-p (aref arg i))) | |
747 (error "CCL: Invalid argument %s: %s" arg cmd)) | |
748 (ccl-embed-data (aref arg i)) | |
749 (setq i (1+ i))))) | |
750 | |
751 ((symbolp rrr) | |
752 ;; CMD has the form `(write REG ...)'. | |
753 (let* ((args (cdr cmd)) | |
754 (i (1- (length args)))) | |
755 (while args | |
756 (setq rrr (ccl-check-register (car args) cmd)) | |
757 (ccl-embed-code 'write-register rrr i) | |
758 (setq args (cdr args) i (1- i))))) | |
759 | |
760 ((listp rrr) | |
761 ;; CMD has the form `(write (LEFT OP RIGHT))'. | |
762 (let ((left (car rrr)) | |
763 (op (get (nth 1 rrr) 'ccl-arith-code)) | |
764 (right (nth 2 rrr))) | |
765 (if (listp left) | |
766 (progn | |
767 ;; RRR has the form `((EXPR OP2 ARG) OP RIGHT)'. | |
768 ;; Compile the first term as `(r7 = (EXPR OP2 ARG))'. | |
769 (ccl-compile-expression 'r7 left) | |
770 (setq left 'r7))) | |
771 ;; Now RRR has the form `(ARG OP RIGHT)'. | |
772 (if (integer-or-char-p right) | |
773 (progn | |
774 (ccl-embed-code 'write-expr-const 0 (ash op 3) left) | |
775 (ccl-embed-data right)) | |
776 (ccl-check-register right rrr) | |
777 (ccl-embed-code 'write-expr-register 0 | |
778 (logior (ash op 3) | |
779 (get right 'ccl-register-number)))))) | |
780 | |
781 (t | |
782 (error "CCL: Invalid argument: %s" cmd)))) | |
783 nil) | |
784 | |
785 ;; Compile CALL statement. | |
786 (defun ccl-compile-call (cmd) | |
787 (if (/= (length cmd) 2) | |
788 (error "CCL: Invalid number of arguments: %s" cmd)) | |
789 (if (not (symbolp (nth 1 cmd))) | |
790 (error "CCL: Subroutine should be a symbol: %s" cmd)) | |
791 (let* ((name (nth 1 cmd)) | |
792 (idx (get name 'ccl-program-idx))) | |
793 (if (not idx) | |
794 (error "CCL: Unknown subroutine name: %s" name)) | |
795 (ccl-embed-code 'call 0 idx)) | |
796 nil) | |
797 | |
798 ;; Compile END statement. | |
799 (defun ccl-compile-end (cmd) | |
800 (if (/= (length cmd) 1) | |
801 (error "CCL: Invalid number of arguments: %s" cmd)) | |
802 (ccl-embed-code 'end 0 0) | |
803 t) | |
804 | |
805 ;;; CCL dump staffs | |
806 | |
807 ;; To avoid byte-compiler warning. | |
808 (defvar ccl-code) | |
809 | |
810 ;;;###autoload | |
811 (defun ccl-dump (ccl-code) | |
812 "Disassemble compiled CCL-CODE." | |
813 (let ((len (length ccl-code)) | |
814 (buffer-mag (aref ccl-code 0))) | |
815 (cond ((= buffer-mag 0) | |
816 (insert "Don't output anything.\n")) | |
817 ((= buffer-mag 1) | |
818 (insert "Out-buffer must be as large as in-buffer.\n")) | |
819 (t | |
820 (insert | |
821 (format "Out-buffer must be %d times bigger than in-buffer.\n" | |
822 buffer-mag)))) | |
823 (insert "Main-body:\n") | |
824 (setq ccl-current-ic 2) | |
825 (if (> (aref ccl-code 1) 0) | |
826 (progn | |
827 (while (< ccl-current-ic (aref ccl-code 1)) | |
828 (ccl-dump-1)) | |
829 (insert "At EOF:\n"))) | |
830 (while (< ccl-current-ic len) | |
831 (ccl-dump-1)) | |
832 )) | |
833 | |
834 ;; Return a CCL code in `ccl-code' at `ccl-current-ic'. | |
835 (defun ccl-get-next-code () | |
836 (prog1 | |
837 (aref ccl-code ccl-current-ic) | |
838 (setq ccl-current-ic (1+ ccl-current-ic)))) | |
839 | |
840 (defun ccl-dump-1 () | |
841 (let* ((code (ccl-get-next-code)) | |
842 (cmd (aref ccl-code-table (logand code 31))) | |
843 (rrr (ash (logand code 255) -5)) | |
844 (cc (ash code -8))) | |
845 (insert (format "%5d:[%s] " (1- ccl-current-ic) cmd)) | |
846 (funcall (get cmd 'ccl-dump-function) rrr cc))) | |
847 | |
848 (defun ccl-dump-set-register (rrr cc) | |
849 (insert (format "r%d = r%d\n" rrr cc))) | |
850 | |
851 (defun ccl-dump-set-short-const (rrr cc) | |
852 (insert (format "r%d = %d\n" rrr cc))) | |
853 | |
854 (defun ccl-dump-set-const (rrr ignore) | |
855 (insert (format "r%d = %d\n" rrr (ccl-get-next-code)))) | |
856 | |
857 (defun ccl-dump-set-array (rrr cc) | |
858 (let ((rrr2 (logand cc 7)) | |
859 (len (ash cc -3)) | |
860 (i 0)) | |
861 (insert (format "r%d = array[r%d] of length %d\n\t" | |
862 rrr rrr2 len)) | |
863 (while (< i len) | |
864 (insert (format "%d " (ccl-get-next-code))) | |
865 (setq i (1+ i))) | |
866 (insert "\n"))) | |
867 | |
868 (defun ccl-dump-jump (ignore cc &optional address) | |
869 (insert (format "jump to %d(" (+ (or address ccl-current-ic) cc))) | |
870 (if (>= cc 0) | |
871 (insert "+")) | |
872 (insert (format "%d)\n" (1+ cc)))) | |
873 | |
874 (defun ccl-dump-jump-cond (rrr cc) | |
875 (insert (format "if (r%d == 0), " rrr)) | |
876 (ccl-dump-jump nil cc)) | |
877 | |
878 (defun ccl-dump-write-register-jump (rrr cc) | |
879 (insert (format "write r%d, " rrr)) | |
880 (ccl-dump-jump nil cc)) | |
881 | |
882 (defun ccl-dump-write-register-read-jump (rrr cc) | |
883 (insert (format "write r%d, read r%d, " rrr rrr)) | |
884 (ccl-dump-jump nil cc) | |
885 (ccl-get-next-code) ; Skip dummy READ-JUMP | |
886 ) | |
887 | |
888 (defun ccl-extract-arith-op (cc) | |
889 (aref ccl-arith-table (ash cc -6))) | |
890 | |
891 (defun ccl-dump-write-expr-const (ignore cc) | |
892 (insert (format "write (r%d %s %d)\n" | |
893 (logand cc 7) | |
894 (ccl-extract-arith-op cc) | |
895 (ccl-get-next-code)))) | |
896 | |
897 (defun ccl-dump-write-expr-register (ignore cc) | |
898 (insert (format "write (r%d %s r%d)\n" | |
899 (logand cc 7) | |
900 (ccl-extract-arith-op cc) | |
901 (logand (ash cc -3) 7)))) | |
902 | |
903 (defun ccl-dump-insert-char (cc) | |
904 (cond ((= cc ?\t) (insert " \"^I\"")) | |
905 ((= cc ?\n) (insert " \"^J\"")) | |
906 (t (insert (format " \"%c\"" cc))))) | |
907 | |
908 (defun ccl-dump-write-const-jump (ignore cc) | |
909 (let ((address ccl-current-ic)) | |
910 (insert "write char") | |
911 (ccl-dump-insert-char (ccl-get-next-code)) | |
912 (insert ", ") | |
913 (ccl-dump-jump nil cc address))) | |
914 | |
915 (defun ccl-dump-write-const-read-jump (rrr cc) | |
916 (let ((address ccl-current-ic)) | |
917 (insert "write char") | |
918 (ccl-dump-insert-char (ccl-get-next-code)) | |
919 (insert (format ", read r%d, " rrr)) | |
920 (ccl-dump-jump cc address) | |
921 (ccl-get-next-code) ; Skip dummy READ-JUMP | |
922 )) | |
923 | |
924 (defun ccl-dump-write-string-jump (ignore cc) | |
925 (let ((address ccl-current-ic) | |
926 (len (ccl-get-next-code)) | |
927 (i 0)) | |
928 (insert "write \"") | |
929 (while (< i len) | |
930 (let ((code (ccl-get-next-code))) | |
931 (insert (ash code -16)) | |
932 (if (< (1+ i) len) (insert (logand (ash code -8) 255))) | |
933 (if (< (+ i 2) len) (insert (logand code 255)))) | |
934 (setq i (+ i 3))) | |
935 (insert "\", ") | |
936 (ccl-dump-jump nil cc address))) | |
937 | |
938 (defun ccl-dump-write-array-read-jump (rrr cc) | |
939 (let ((address ccl-current-ic) | |
940 (len (ccl-get-next-code)) | |
941 (i 0)) | |
942 (insert (format "write array[r%d] of length %d,\n\t" rrr len)) | |
943 (while (< i len) | |
944 (ccl-dump-insert-char (ccl-get-next-code)) | |
945 (setq i (1+ i))) | |
946 (insert (format "\n\tthen read r%d, " rrr)) | |
947 (ccl-dump-jump nil cc address) | |
948 (ccl-get-next-code) ; Skip dummy READ-JUMP. | |
949 )) | |
950 | |
951 (defun ccl-dump-read-jump (rrr cc) | |
952 (insert (format "read r%d, " rrr)) | |
953 (ccl-dump-jump nil cc)) | |
954 | |
955 (defun ccl-dump-branch (rrr len) | |
956 (let ((jump-table-head ccl-current-ic) | |
957 (i 0)) | |
958 (insert (format "jump to array[r%d] of length %d\n\t" rrr len)) | |
959 (while (<= i len) | |
960 (insert (format "%d " (+ jump-table-head (ccl-get-next-code)))) | |
961 (setq i (1+ i))) | |
962 (insert "\n"))) | |
963 | |
964 (defun ccl-dump-read-register (rrr cc) | |
965 (insert (format "read r%d (%d remaining)\n" rrr cc))) | |
966 | |
967 (defun ccl-dump-read-branch (rrr len) | |
968 (insert (format "read r%d, " rrr)) | |
969 (ccl-dump-branch rrr len)) | |
970 | |
971 (defun ccl-dump-write-register (rrr cc) | |
972 (insert (format "write r%d (%d remaining)\n" rrr cc))) | |
973 | |
974 (defun ccl-dump-call (ignore cc) | |
975 (insert (format "call subroutine #%d\n" cc))) | |
976 | |
977 (defun ccl-dump-write-const-string (rrr cc) | |
978 (if (= rrr 0) | |
979 (progn | |
980 (insert "write char") | |
981 (ccl-dump-insert-char cc) | |
982 (newline)) | |
983 (let ((len cc) | |
984 (i 0)) | |
985 (insert "write \"") | |
986 (while (< i len) | |
987 (let ((code (ccl-get-next-code))) | |
988 (insert (format "%c" (lsh code -16))) | |
989 (if (< (1+ i) len) | |
990 (insert (format "%c" (logand (lsh code -8) 255)))) | |
991 (if (< (+ i 2) len) | |
992 (insert (format "%c" (logand code 255)))) | |
993 (setq i (+ i 3)))) | |
994 (insert "\"\n")))) | |
995 | |
996 (defun ccl-dump-write-array (rrr cc) | |
997 (let ((i 0)) | |
998 (insert (format "write array[r%d] of length %d\n\t" rrr cc)) | |
999 (while (< i cc) | |
1000 (ccl-dump-insert-char (ccl-get-next-code)) | |
1001 (setq i (1+ i))) | |
1002 (insert "\n"))) | |
1003 | |
1004 (defun ccl-dump-end (&rest ignore) | |
1005 (insert "end\n")) | |
1006 | |
1007 (defun ccl-dump-set-assign-expr-const (rrr cc) | |
1008 (insert (format "r%d %s= %d\n" | |
1009 rrr | |
1010 (ccl-extract-arith-op cc) | |
1011 (ccl-get-next-code)))) | |
1012 | |
1013 (defun ccl-dump-set-assign-expr-register (rrr cc) | |
1014 (insert (format "r%d %s= r%d\n" | |
1015 rrr | |
1016 (ccl-extract-arith-op cc) | |
1017 (logand cc 7)))) | |
1018 | |
1019 (defun ccl-dump-set-expr-const (rrr cc) | |
1020 (insert (format "r%d = r%d %s %d\n" | |
1021 rrr | |
1022 (logand cc 7) | |
1023 (ccl-extract-arith-op cc) | |
1024 (ccl-get-next-code)))) | |
1025 | |
1026 (defun ccl-dump-set-expr-register (rrr cc) | |
1027 (insert (format "r%d = r%d %s r%d\n" | |
1028 rrr | |
1029 (logand cc 7) | |
1030 (ccl-extract-arith-op cc) | |
1031 (logand (ash cc -3) 7)))) | |
1032 | |
1033 (defun ccl-dump-jump-cond-expr-const (rrr cc) | |
1034 (let ((address ccl-current-ic)) | |
1035 (insert (format "if !(r%d %s %d), " | |
1036 rrr | |
1037 (aref ccl-arith-table (ccl-get-next-code)) | |
1038 (ccl-get-next-code))) | |
1039 (ccl-dump-jump nil cc address))) | |
1040 | |
1041 (defun ccl-dump-jump-cond-expr-register (rrr cc) | |
1042 (let ((address ccl-current-ic)) | |
1043 (insert (format "if !(r%d %s r%d), " | |
1044 rrr | |
1045 (aref ccl-arith-table (ccl-get-next-code)) | |
1046 (ccl-get-next-code))) | |
1047 (ccl-dump-jump nil cc address))) | |
1048 | |
1049 (defun ccl-dump-read-jump-cond-expr-const (rrr cc) | |
1050 (insert (format "read r%d, " rrr)) | |
1051 (ccl-dump-jump-cond-expr-const rrr cc)) | |
1052 | |
1053 (defun ccl-dump-read-jump-cond-expr-register (rrr cc) | |
1054 (insert (format "read r%d, " rrr)) | |
1055 (ccl-dump-jump-cond-expr-register rrr cc)) | |
1056 | |
1057 (defun ccl-dump-binary (ccl-code) | |
1058 (let ((len (length ccl-code)) | |
1059 (i 2)) | |
1060 (while (< i len) | |
1061 (let ((code (aref ccl-code i)) | |
1062 (j 27)) | |
1063 (while (>= j 0) | |
1064 (insert (if (= (logand code (ash 1 j)) 0) ?0 ?1)) | |
1065 (setq j (1- j))) | |
1066 (setq code (logand code 31)) | |
1067 (if (< code (length ccl-code-table)) | |
1068 (insert (format ":%s" (aref ccl-code-table code)))) | |
1069 (insert "\n")) | |
1070 (setq i (1+ i))))) | |
1071 | |
1072 ;; CCL emulation staffs | |
1073 | |
1074 ;; Not yet implemented. | |
1075 | |
1076 ;;;###autoload | |
1077 (defmacro declare-ccl-program (name) | |
1078 "Declare NAME as a name of CCL program. | |
1079 | |
1080 To compile a CCL program which calls another CCL program not yet | |
1081 defined, it must be declared as a CCL program in advance." | |
1082 `(put ',name 'ccl-program-idx (register-ccl-program ',name nil))) | |
1083 | |
1084 ;;;###autoload | |
1085 (defmacro define-ccl-program (name ccl-program &optional doc) | |
1086 "Set NAME the compiled code of CCL-PROGRAM. | |
1087 CCL-PROGRAM is `eval'ed before being handed to the CCL compiler `ccl-compile'. | |
1088 The compiled code is a vector of integers." | |
1089 `(let ((prog ,(ccl-compile (eval ccl-program)))) | |
1090 (defconst ,name prog ,doc) | |
1091 (put ',name 'ccl-program-idx (register-ccl-program ',name prog)) | |
1092 nil)) | |
1093 | |
1094 ;;;###autoload | |
1095 (defun ccl-execute-with-args (ccl-prog &rest args) | |
1096 "Execute CCL-PROGRAM with registers initialized by the remaining args. | |
1097 The return value is a vector of resulting CCL registeres." | |
1098 (let ((reg (make-vector 8 0)) | |
1099 (i 0)) | |
1100 (while (and args (< i 8)) | |
1101 (if (not (integerp (car args))) | |
1102 (error "Arguments should be integer")) | |
1103 (aset reg i (car args)) | |
1104 (setq args (cdr args) i (1+ i))) | |
1105 (ccl-execute ccl-prog reg) | |
1106 reg)) | |
1107 | |
1108 (provide 'ccl) | |
1109 | |
1110 ;; ccl.el ends here |