comparison lisp/mule/mule-ccl.el @ 428:3ecd8885ac67 r21-2-22

Import from CVS: tag r21-2-22
author cvs
date Mon, 13 Aug 2007 11:28:15 +0200
parents
children abe6d1db359e
comparison
equal deleted inserted replaced
427:0a0253eac470 428:3ecd8885ac67
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 ;; | (read-multibyte-character REG {charset} REG {code-point})
78 ;; WRITE :=
79 ;; (write REG ...)
80 ;; | (write EXPRESSION)
81 ;; | (write integer) | (write string) | (write REG ARRAY)
82 ;; | string
83 ;; | (write-multibyte-character REG(charset) REG(codepoint))
84 ;; CALL := (call ccl-program-name)
85 ;; END := (end)
86 ;;
87 ;; REG := r0 | r1 | r2 | r3 | r4 | r5 | r6 | r7
88 ;; ARG := REG | integer
89 ;; OPERATOR :=
90 ;; + | - | * | / | % | & | '|' | ^ | << | >> | <8 | >8 | //
91 ;; | < | > | == | <= | >= | != | de-sjis | en-sjis
92 ;; ASSIGNMENT_OPERATOR :=
93 ;; += | -= | *= | /= | %= | &= | '|=' | ^= | <<= | >>=
94 ;; ARRAY := '[' integer ... ']'
95
96 ;;; Code:
97
98 (defconst ccl-command-table
99 [if branch loop break repeat write-repeat write-read-repeat
100 read read-if read-branch write call end
101 read-multibyte-character write-multibyte-character]
102 "Vector of CCL commands (symbols).")
103
104 ;; Put a property to each symbol of CCL commands for the compiler.
105 (let (op (i 0) (len (length ccl-command-table)))
106 (while (< i len)
107 (setq op (aref ccl-command-table i))
108 (put op 'ccl-compile-function (intern (format "ccl-compile-%s" op)))
109 (setq i (1+ i))))
110
111 (defconst ccl-code-table
112 [set-register
113 set-short-const
114 set-const
115 set-array
116 jump
117 jump-cond
118 write-register-jump
119 write-register-read-jump
120 write-const-jump
121 write-const-read-jump
122 write-string-jump
123 write-array-read-jump
124 read-jump
125 branch
126 read-register
127 write-expr-const
128 read-branch
129 write-register
130 write-expr-register
131 call
132 write-const-string
133 write-array
134 end
135 set-assign-expr-const
136 set-assign-expr-register
137 set-expr-const
138 set-expr-register
139 jump-cond-expr-const
140 jump-cond-expr-register
141 read-jump-cond-expr-const
142 read-jump-cond-expr-register
143 ex-cmd
144 ]
145 "Vector of CCL compiled codes (symbols).")
146
147 (defconst ccl-extended-code-table
148 [read-multibyte-character
149 write-multibyte-character
150 translate-character
151 translate-character-const-tbl
152 nil nil nil nil nil nil nil nil nil nil nil nil ; 0x04-0x0f
153 iterate-multiple-map
154 map-multiple
155 map-single
156 ]
157 "Vector of CCL extended compiled codes (symbols).")
158
159 ;; Put a property to each symbol of CCL codes for the disassembler.
160 (let (code (i 0) (len (length ccl-code-table)))
161 (while (< i len)
162 (setq code (aref ccl-code-table i))
163 (put code 'ccl-code i)
164 (put code 'ccl-dump-function (intern (format "ccl-dump-%s" code)))
165 (setq i (1+ i))))
166
167 (let (code (i 0) (len (length ccl-extended-code-table)))
168 (while (< i len)
169 (setq code (aref ccl-extended-code-table i))
170 (if code
171 (progn
172 (put code 'ccl-ex-code i)
173 (put code 'ccl-dump-function (intern (format "ccl-dump-%s" code)))))
174 (setq i (1+ i))))
175
176 (defconst ccl-jump-code-list
177 '(jump jump-cond write-register-jump write-register-read-jump
178 write-const-jump write-const-read-jump write-string-jump
179 write-array-read-jump read-jump))
180
181 ;; Put a property `jump-flag' to each CCL code which execute jump in
182 ;; some way.
183 (let ((l ccl-jump-code-list))
184 (while l
185 (put (car l) 'jump-flag t)
186 (setq l (cdr l))))
187
188 (defconst ccl-register-table
189 [r0 r1 r2 r3 r4 r5 r6 r7]
190 "Vector of CCL registers (symbols).")
191
192 ;; Put a property to indicate register number to each symbol of CCL.
193 ;; registers.
194 (let (reg (i 0) (len (length ccl-register-table)))
195 (while (< i len)
196 (setq reg (aref ccl-register-table i))
197 (put reg 'ccl-register-number i)
198 (setq i (1+ i))))
199
200 (defconst ccl-arith-table
201 [+ - * / % & | ^ << >> <8 >8 // nil nil nil
202 < > == <= >= != de-sjis en-sjis]
203 "Vector of CCL arithmetic/logical operators (symbols).")
204
205 ;; Put a property to each symbol of CCL operators for the compiler.
206 (let (arith (i 0) (len (length ccl-arith-table)))
207 (while (< i len)
208 (setq arith (aref ccl-arith-table i))
209 (if arith (put arith 'ccl-arith-code i))
210 (setq i (1+ i))))
211
212 (defconst ccl-assign-arith-table
213 [+= -= *= /= %= &= |= ^= <<= >>= <8= >8= //=]
214 "Vector of CCL assignment operators (symbols).")
215
216 ;; Put a property to each symbol of CCL assignment operators for the compiler.
217 (let (arith (i 0) (len (length ccl-assign-arith-table)))
218 (while (< i len)
219 (setq arith (aref ccl-assign-arith-table i))
220 (put arith 'ccl-self-arith-code i)
221 (setq i (1+ i))))
222
223 (defvar ccl-program-vector nil
224 "Working vector of CCL codes produced by CCL compiler.")
225 (defvar ccl-current-ic 0
226 "The current index for `ccl-program-vector'.")
227
228 ;; Embed integer DATA in `ccl-program-vector' at `ccl-current-ic' and
229 ;; increment it. If IC is specified, embed DATA at IC.
230 (defun ccl-embed-data (data &optional ic)
231 (let ((val (if (characterp data) (char-int data) data)))
232 (if ic
233 (aset ccl-program-vector ic val)
234 (aset ccl-program-vector ccl-current-ic val)
235 (setq ccl-current-ic (1+ ccl-current-ic)))))
236
237 ;; Embed string STR of length LEN in `ccl-program-vector' at
238 ;; `ccl-current-ic'.
239 (defun ccl-embed-string (len str)
240 (let ((i 0))
241 (while (< i len)
242 (ccl-embed-data (logior (ash (aref str i) 16)
243 (if (< (1+ i) len)
244 (ash (aref str (1+ i)) 8)
245 0)
246 (if (< (+ i 2) len)
247 (aref str (+ i 2))
248 0)))
249 (setq i (+ i 3)))))
250
251 ;; Embed a relative jump address to `ccl-current-ic' in
252 ;; `ccl-program-vector' at IC without altering the other bit field.
253 (defun ccl-embed-current-address (ic)
254 (let ((relative (- ccl-current-ic (1+ ic))))
255 (aset ccl-program-vector ic
256 (logior (aref ccl-program-vector ic) (ash relative 8)))))
257
258 ;; Embed CCL code for the operation OP and arguments REG and DATA in
259 ;; `ccl-program-vector' at `ccl-current-ic' in the following format.
260 ;; |----------------- integer (28-bit) ------------------|
261 ;; |------------ 20-bit ------------|- 3-bit --|- 5-bit -|
262 ;; |------------- DATA -------------|-- REG ---|-- OP ---|
263 ;; If REG2 is specified, embed a code in the following format.
264 ;; |------- 17-bit ------|- 3-bit --|- 3-bit --|- 5-bit -|
265 ;; |-------- DATA -------|-- REG2 --|-- REG ---|-- OP ---|
266
267 ;; If REG is a CCL register symbol (e.g. r0, r1...), the register
268 ;; number is embedded. If OP is one of unconditional jumps, DATA is
269 ;; changed to an relative jump address.
270
271 (defun ccl-embed-code (op reg data &optional reg2)
272 (if (and (> data 0) (get op 'jump-flag))
273 ;; DATA is an absolute jump address. Make it relative to the
274 ;; next of jump code.
275 (setq data (- data (1+ ccl-current-ic))))
276 (let ((code (logior (get op 'ccl-code)
277 (ash
278 (if (symbolp reg) (get reg 'ccl-register-number) reg) 5)
279 (if reg2
280 (logior (ash (get reg2 'ccl-register-number) 8)
281 (ash data 11))
282 (ash data 8)))))
283 (aset ccl-program-vector ccl-current-ic code)
284 (setq ccl-current-ic (1+ ccl-current-ic))))
285
286 ;; extended ccl command format
287 ;; |- 14-bit -|- 3-bit --|- 3-bit --|- 3-bit --|- 5-bit -|
288 ;; |- EX-OP --|-- REG3 --|-- REG2 --|-- REG ---|-- OP ---|
289 (defun ccl-embed-extended-command (ex-op reg reg2 reg3)
290 (let ((data (logior (ash (get ex-op 'ccl-ex-code) 3)
291 (if (symbolp reg3)
292 (get reg3 'ccl-register-number)
293 0))))
294 (ccl-embed-code 'ex-cmd reg data reg2)))
295
296 ;; Just advance `ccl-current-ic' by INC.
297 (defun ccl-increment-ic (inc)
298 (setq ccl-current-ic (+ ccl-current-ic inc)))
299
300 ;;;###autoload
301 (defun ccl-program-p (obj)
302 "Return t if OBJECT is a valid CCL compiled code."
303 (and (vectorp obj)
304 (let ((i 0) (len (length obj)) (flag t))
305 (if (> len 1)
306 (progn
307 (while (and flag (< i len))
308 (setq flag (integerp (aref obj i)))
309 (setq i (1+ i)))
310 flag)))))
311
312 ;; If non-nil, index of the start of the current loop.
313 (defvar ccl-loop-head nil)
314 ;; If non-nil, list of absolute addresses of the breaking points of
315 ;; the current loop.
316 (defvar ccl-breaks nil)
317
318 ;;;###autoload
319 (defun ccl-compile (ccl-program)
320 "Return a compiled code of CCL-PROGRAM as a vector of integer."
321 (if (or (null (consp ccl-program))
322 (null (integer-or-char-p (car ccl-program)))
323 (null (listp (car (cdr ccl-program)))))
324 (error "CCL: Invalid CCL program: %s" ccl-program))
325 (if (null (vectorp ccl-program-vector))
326 (setq ccl-program-vector (make-vector 8192 0)))
327 (setq ccl-loop-head nil ccl-breaks nil)
328 (setq ccl-current-ic 0)
329
330 ;; The first element is the buffer magnification.
331 (ccl-embed-data (car ccl-program))
332
333 ;; The second element is the address of the start CCL code for
334 ;; processing end of input buffer (we call it eof-processor). We
335 ;; set it later.
336 (ccl-increment-ic 1)
337
338 ;; Compile the main body of the CCL program.
339 (ccl-compile-1 (car (cdr ccl-program)))
340
341 ;; Embed the address of eof-processor.
342 (ccl-embed-data ccl-current-ic 1)
343
344 ;; Then compile eof-processor.
345 (if (nth 2 ccl-program)
346 (ccl-compile-1 (nth 2 ccl-program)))
347
348 ;; At last, embed termination code.
349 (ccl-embed-code 'end 0 0)
350
351 (let ((vec (make-vector ccl-current-ic 0))
352 (i 0))
353 (while (< i ccl-current-ic)
354 (aset vec i (aref ccl-program-vector i))
355 (setq i (1+ i)))
356 vec))
357
358 ;; Signal syntax error.
359 (defun ccl-syntax-error (cmd)
360 (error "CCL: Syntax error: %s" cmd))
361
362 ;; Check if ARG is a valid CCL register.
363 (defun ccl-check-register (arg cmd)
364 (if (get arg 'ccl-register-number)
365 arg
366 (error "CCL: Invalid register %s in %s." arg cmd)))
367
368 ;; Check if ARG is a valid CCL command.
369 (defun ccl-check-compile-function (arg cmd)
370 (or (get arg 'ccl-compile-function)
371 (error "CCL: Invalid command: %s" cmd)))
372
373 ;; In the following code, most ccl-compile-XXXX functions return t if
374 ;; they end with unconditional jump, else return nil.
375
376 ;; Compile CCL-BLOCK (see the syntax above).
377 (defun ccl-compile-1 (ccl-block)
378 (let (unconditional-jump
379 cmd)
380 (if (or (integer-or-char-p ccl-block)
381 (stringp ccl-block)
382 (and ccl-block (symbolp (car ccl-block))))
383 ;; This block consists of single statement.
384 (setq ccl-block (list ccl-block)))
385
386 ;; Now CCL-BLOCK is a list of statements. Compile them one by
387 ;; one.
388 (while ccl-block
389 (setq cmd (car ccl-block))
390 (setq unconditional-jump
391 (cond ((integer-or-char-p cmd)
392 ;; SET statement for the register 0.
393 (ccl-compile-set (list 'r0 '= cmd)))
394
395 ((stringp cmd)
396 ;; WRITE statement of string argument.
397 (ccl-compile-write-string cmd))
398
399 ((listp cmd)
400 ;; The other statements.
401 (cond ((eq (nth 1 cmd) '=)
402 ;; SET statement of the form `(REG = EXPRESSION)'.
403 (ccl-compile-set cmd))
404
405 ((and (symbolp (nth 1 cmd))
406 (get (nth 1 cmd) 'ccl-self-arith-code))
407 ;; SET statement with an assignment operation.
408 (ccl-compile-self-set cmd))
409
410 (t
411 (funcall (ccl-check-compile-function (car cmd) cmd)
412 cmd))))
413
414 (t
415 (ccl-syntax-error cmd))))
416 (setq ccl-block (cdr ccl-block)))
417 unconditional-jump))
418
419 (defconst ccl-max-short-const (ash 1 19))
420 (defconst ccl-min-short-const (ash -1 19))
421
422 ;; Compile SET statement.
423 (defun ccl-compile-set (cmd)
424 (let ((rrr (ccl-check-register (car cmd) cmd))
425 (right (nth 2 cmd)))
426 (cond ((listp right)
427 ;; CMD has the form `(RRR = (XXX OP YYY))'.
428 (ccl-compile-expression rrr right))
429
430 ((integer-or-char-p right)
431 ;; CMD has the form `(RRR = integer)'.
432 (if (and (<= right ccl-max-short-const)
433 (>= right ccl-min-short-const))
434 (ccl-embed-code 'set-short-const rrr right)
435 (ccl-embed-code 'set-const rrr 0)
436 (ccl-embed-data right)))
437
438 (t
439 ;; CMD has the form `(RRR = rrr [ array ])'.
440 (ccl-check-register right cmd)
441 (let ((ary (nth 3 cmd)))
442 (if (vectorp ary)
443 (let ((i 0) (len (length ary)))
444 (ccl-embed-code 'set-array rrr len right)
445 (while (< i len)
446 (ccl-embed-data (aref ary i))
447 (setq i (1+ i))))
448 (ccl-embed-code 'set-register rrr 0 right))))))
449 nil)
450
451 ;; Compile SET statement with ASSIGNMENT_OPERATOR.
452 (defun ccl-compile-self-set (cmd)
453 (let ((rrr (ccl-check-register (car cmd) cmd))
454 (right (nth 2 cmd)))
455 (if (listp right)
456 ;; CMD has the form `(RRR ASSIGN_OP (XXX OP YYY))', compile
457 ;; the right hand part as `(r7 = (XXX OP YYY))' (note: the
458 ;; register 7 can be used for storing temporary value).
459 (progn
460 (ccl-compile-expression 'r7 right)
461 (setq right 'r7)))
462 ;; Now CMD has the form `(RRR ASSIGN_OP ARG)'. Compile it as
463 ;; `(RRR = (RRR OP ARG))'.
464 (ccl-compile-expression
465 rrr
466 (list rrr (intern (substring (symbol-name (nth 1 cmd)) 0 -1)) right)))
467 nil)
468
469 ;; Compile SET statement of the form `(RRR = EXPR)'.
470 (defun ccl-compile-expression (rrr expr)
471 (let ((left (car expr))
472 (op (get (nth 1 expr) 'ccl-arith-code))
473 (right (nth 2 expr)))
474 (if (listp left)
475 (progn
476 ;; EXPR has the form `((EXPR2 OP2 ARG) OP RIGHT)'. Compile
477 ;; the first term as `(r7 = (EXPR2 OP2 ARG)).'
478 (ccl-compile-expression 'r7 left)
479 (setq left 'r7)))
480
481 ;; Now EXPR has the form (LEFT OP RIGHT).
482 (if (eq rrr left)
483 ;; Compile this SET statement as `(RRR OP= RIGHT)'.
484 (if (integer-or-char-p right)
485 (progn
486 (ccl-embed-code 'set-assign-expr-const rrr (ash op 3) 'r0)
487 (ccl-embed-data right))
488 (ccl-check-register right expr)
489 (ccl-embed-code 'set-assign-expr-register rrr (ash op 3) right))
490
491 ;; Compile this SET statement as `(RRR = (LEFT OP RIGHT))'.
492 (if (integer-or-char-p right)
493 (progn
494 (ccl-embed-code 'set-expr-const rrr (ash op 3) left)
495 (ccl-embed-data right))
496 (ccl-check-register right expr)
497 (ccl-embed-code 'set-expr-register
498 rrr
499 (logior (ash op 3) (get right 'ccl-register-number))
500 left)))))
501
502 ;; Compile WRITE statement with string argument.
503 (defun ccl-compile-write-string (str)
504 (let ((len (length str)))
505 (ccl-embed-code 'write-const-string 1 len)
506 (ccl-embed-string len str))
507 nil)
508
509 ;; Compile IF statement of the form `(if CONDITION TRUE-PART FALSE-PART)'.
510 ;; If READ-FLAG is non-nil, this statement has the form
511 ;; `(read-if (REG OPERATOR ARG) TRUE-PART FALSE-PART)'.
512 (defun ccl-compile-if (cmd &optional read-flag)
513 (if (and (/= (length cmd) 3) (/= (length cmd) 4))
514 (error "CCL: Invalid number of arguments: %s" cmd))
515 (let ((condition (nth 1 cmd))
516 (true-cmds (nth 2 cmd))
517 (false-cmds (nth 3 cmd))
518 jump-cond-address
519 false-ic)
520 (if (and (listp condition)
521 (listp (car condition)))
522 ;; If CONDITION is a nested expression, the inner expression
523 ;; should be compiled at first as SET statement, i.e.:
524 ;; `(if ((X OP2 Y) OP Z) ...)' is compiled into two statements:
525 ;; `(r7 = (X OP2 Y)) (if (r7 OP Z) ...)'.
526 (progn
527 (ccl-compile-expression 'r7 (car condition))
528 (setq condition (cons 'r7 (cdr condition)))
529 (setq cmd (cons (car cmd)
530 (cons condition (cdr (cdr cmd)))))))
531
532 (setq jump-cond-address ccl-current-ic)
533 ;; Compile CONDITION.
534 (if (symbolp condition)
535 ;; CONDITION is a register.
536 (progn
537 (ccl-check-register condition cmd)
538 (ccl-embed-code 'jump-cond condition 0))
539 ;; CONDITION is a simple expression of the form (RRR OP ARG).
540 (let ((rrr (car condition))
541 (op (get (nth 1 condition) 'ccl-arith-code))
542 (arg (nth 2 condition)))
543 (ccl-check-register rrr cmd)
544 (if (integer-or-char-p arg)
545 (progn
546 (ccl-embed-code (if read-flag 'read-jump-cond-expr-const
547 'jump-cond-expr-const)
548 rrr 0)
549 (ccl-embed-data op)
550 (ccl-embed-data arg))
551 (ccl-check-register arg cmd)
552 (ccl-embed-code (if read-flag 'read-jump-cond-expr-register
553 'jump-cond-expr-register)
554 rrr 0)
555 (ccl-embed-data op)
556 (ccl-embed-data (get arg 'ccl-register-number)))))
557
558 ;; Compile TRUE-PART.
559 (let ((unconditional-jump (ccl-compile-1 true-cmds)))
560 (if (null false-cmds)
561 ;; This is the place to jump to if condition is false.
562 (progn
563 (ccl-embed-current-address jump-cond-address)
564 (setq unconditional-jump nil))
565 (let (end-true-part-address)
566 (if (not unconditional-jump)
567 (progn
568 ;; If TRUE-PART does not end with unconditional jump, we
569 ;; have to jump to the end of FALSE-PART from here.
570 (setq end-true-part-address ccl-current-ic)
571 (ccl-embed-code 'jump 0 0)))
572 ;; This is the place to jump to if CONDITION is false.
573 (ccl-embed-current-address jump-cond-address)
574 ;; Compile FALSE-PART.
575 (setq unconditional-jump
576 (and (ccl-compile-1 false-cmds) unconditional-jump))
577 (if end-true-part-address
578 ;; This is the place to jump to after the end of TRUE-PART.
579 (ccl-embed-current-address end-true-part-address))))
580 unconditional-jump)))
581
582 ;; Compile BRANCH statement.
583 (defun ccl-compile-branch (cmd)
584 (if (< (length cmd) 3)
585 (error "CCL: Invalid number of arguments: %s" cmd))
586 (ccl-compile-branch-blocks 'branch
587 (ccl-compile-branch-expression (nth 1 cmd) cmd)
588 (cdr (cdr cmd))))
589
590 ;; Compile READ statement of the form `(read-branch EXPR BLOCK0 BLOCK1 ...)'.
591 (defun ccl-compile-read-branch (cmd)
592 (if (< (length cmd) 3)
593 (error "CCL: Invalid number of arguments: %s" cmd))
594 (ccl-compile-branch-blocks 'read-branch
595 (ccl-compile-branch-expression (nth 1 cmd) cmd)
596 (cdr (cdr cmd))))
597
598 ;; Compile EXPRESSION part of BRANCH statement and return register
599 ;; which holds a value of the expression.
600 (defun ccl-compile-branch-expression (expr cmd)
601 (if (listp expr)
602 ;; EXPR has the form `(EXPR2 OP ARG)'. Compile it as SET
603 ;; statement of the form `(r7 = (EXPR2 OP ARG))'.
604 (progn
605 (ccl-compile-expression 'r7 expr)
606 'r7)
607 (ccl-check-register expr cmd)))
608
609 ;; Compile BLOCKs of BRANCH statement. CODE is 'branch or 'read-branch.
610 ;; REG is a register which holds a value of EXPRESSION part. BLOCKs
611 ;; is a list of CCL-BLOCKs.
612 (defun ccl-compile-branch-blocks (code rrr blocks)
613 (let ((branches (length blocks))
614 branch-idx
615 jump-table-head-address
616 empty-block-indexes
617 block-tail-addresses
618 block-unconditional-jump)
619 (ccl-embed-code code rrr branches)
620 (setq jump-table-head-address ccl-current-ic)
621 ;; The size of jump table is the number of blocks plus 1 (for the
622 ;; case RRR is out of range).
623 (ccl-increment-ic (1+ branches))
624 (setq empty-block-indexes (list branches))
625 ;; Compile each block.
626 (setq branch-idx 0)
627 (while blocks
628 (if (null (car blocks))
629 ;; This block is empty.
630 (setq empty-block-indexes (cons branch-idx empty-block-indexes)
631 block-unconditional-jump t)
632 ;; This block is not empty.
633 (ccl-embed-data (- ccl-current-ic jump-table-head-address)
634 (+ jump-table-head-address branch-idx))
635 (setq block-unconditional-jump (ccl-compile-1 (car blocks)))
636 (if (not block-unconditional-jump)
637 (progn
638 ;; Jump address of the end of branches are embedded later.
639 ;; For the moment, just remember where to embed them.
640 (setq block-tail-addresses
641 (cons ccl-current-ic block-tail-addresses))
642 (ccl-embed-code 'jump 0 0))))
643 (setq branch-idx (1+ branch-idx))
644 (setq blocks (cdr blocks)))
645 (if (not block-unconditional-jump)
646 ;; We don't need jump code at the end of the last block.
647 (setq block-tail-addresses (cdr block-tail-addresses)
648 ccl-current-ic (1- ccl-current-ic)))
649 ;; Embed jump address at the tailing jump commands of blocks.
650 (while block-tail-addresses
651 (ccl-embed-current-address (car block-tail-addresses))
652 (setq block-tail-addresses (cdr block-tail-addresses)))
653 ;; For empty blocks, make entries in the jump table point directly here.
654 (while empty-block-indexes
655 (ccl-embed-data (- ccl-current-ic jump-table-head-address)
656 (+ jump-table-head-address (car empty-block-indexes)))
657 (setq empty-block-indexes (cdr empty-block-indexes))))
658 ;; Branch command ends by unconditional jump if RRR is out of range.
659 nil)
660
661 ;; Compile LOOP statement.
662 (defun ccl-compile-loop (cmd)
663 (if (< (length cmd) 2)
664 (error "CCL: Invalid number of arguments: %s" cmd))
665 (let* ((ccl-loop-head ccl-current-ic)
666 (ccl-breaks nil)
667 unconditional-jump)
668 (setq cmd (cdr cmd))
669 (if cmd
670 (progn
671 (setq unconditional-jump t)
672 (while cmd
673 (setq unconditional-jump
674 (and (ccl-compile-1 (car cmd)) unconditional-jump))
675 (setq cmd (cdr cmd)))
676 (if (not ccl-breaks)
677 unconditional-jump
678 ;; Embed jump address for break statements encountered in
679 ;; this loop.
680 (while ccl-breaks
681 (ccl-embed-current-address (car ccl-breaks))
682 (setq ccl-breaks (cdr ccl-breaks))))
683 nil))))
684
685 ;; Compile BREAK statement.
686 (defun ccl-compile-break (cmd)
687 (if (/= (length cmd) 1)
688 (error "CCL: Invalid number of arguments: %s" cmd))
689 (if (null ccl-loop-head)
690 (error "CCL: No outer loop: %s" cmd))
691 (setq ccl-breaks (cons ccl-current-ic ccl-breaks))
692 (ccl-embed-code 'jump 0 0)
693 t)
694
695 ;; Compile REPEAT statement.
696 (defun ccl-compile-repeat (cmd)
697 (if (/= (length cmd) 1)
698 (error "CCL: Invalid number of arguments: %s" cmd))
699 (if (null ccl-loop-head)
700 (error "CCL: No outer loop: %s" cmd))
701 (ccl-embed-code 'jump 0 ccl-loop-head)
702 t)
703
704 ;; Compile WRITE-REPEAT statement.
705 (defun ccl-compile-write-repeat (cmd)
706 (if (/= (length cmd) 2)
707 (error "CCL: Invalid number of arguments: %s" cmd))
708 (if (null ccl-loop-head)
709 (error "CCL: No outer loop: %s" cmd))
710 (let ((arg (nth 1 cmd)))
711 (cond ((integer-or-char-p arg)
712 (ccl-embed-code 'write-const-jump 0 ccl-loop-head)
713 (ccl-embed-data arg))
714 ((stringp arg)
715 (let ((len (length arg))
716 (i 0))
717 (ccl-embed-code 'write-string-jump 0 ccl-loop-head)
718 (ccl-embed-data len)
719 (ccl-embed-string len arg)))
720 (t
721 (ccl-check-register arg cmd)
722 (ccl-embed-code 'write-register-jump arg ccl-loop-head))))
723 t)
724
725 ;; Compile WRITE-READ-REPEAT statement.
726 (defun ccl-compile-write-read-repeat (cmd)
727 (if (or (< (length cmd) 2) (> (length cmd) 3))
728 (error "CCL: Invalid number of arguments: %s" cmd))
729 (if (null ccl-loop-head)
730 (error "CCL: No outer loop: %s" cmd))
731 (let ((rrr (ccl-check-register (nth 1 cmd) cmd))
732 (arg (nth 2 cmd)))
733 (cond ((null arg)
734 (ccl-embed-code 'write-register-read-jump rrr ccl-loop-head))
735 ((integer-or-char-p arg)
736 (ccl-embed-code 'write-const-read-jump rrr arg ccl-loop-head))
737 ((vectorp arg)
738 (let ((len (length arg))
739 (i 0))
740 (ccl-embed-code 'write-array-read-jump rrr ccl-loop-head)
741 (ccl-embed-data len)
742 (while (< i len)
743 (ccl-embed-data (aref arg i))
744 (setq i (1+ i)))))
745 (t
746 (error "CCL: Invalid argument %s: %s" arg cmd)))
747 (ccl-embed-code 'read-jump rrr ccl-loop-head))
748 t)
749
750 ;; Compile READ statement.
751 (defun ccl-compile-read (cmd)
752 (if (< (length cmd) 2)
753 (error "CCL: Invalid number of arguments: %s" cmd))
754 (let* ((args (cdr cmd))
755 (i (1- (length args))))
756 (while args
757 (let ((rrr (ccl-check-register (car args) cmd)))
758 (ccl-embed-code 'read-register rrr i)
759 (setq args (cdr args) i (1- i)))))
760 nil)
761
762 ;; Compile READ-IF statement.
763 (defun ccl-compile-read-if (cmd)
764 (ccl-compile-if cmd 'read))
765
766 ;; Compile WRITE statement.
767 (defun ccl-compile-write (cmd)
768 (if (< (length cmd) 2)
769 (error "CCL: Invalid number of arguments: %s" cmd))
770 (let ((rrr (nth 1 cmd)))
771 (cond ((integer-or-char-p rrr)
772 (ccl-embed-code 'write-const-string 0 rrr))
773 ((stringp rrr)
774 (ccl-compile-write-string rrr))
775 ((and (symbolp rrr) (vectorp (nth 2 cmd)))
776 (ccl-check-register rrr cmd)
777 ;; CMD has the form `(write REG ARRAY)'.
778 (let* ((arg (nth 2 cmd))
779 (len (length arg))
780 (i 0))
781 (ccl-embed-code 'write-array rrr len)
782 (while (< i len)
783 (if (not (integer-or-char-p (aref arg i)))
784 (error "CCL: Invalid argument %s: %s" arg cmd))
785 (ccl-embed-data (aref arg i))
786 (setq i (1+ i)))))
787
788 ((symbolp rrr)
789 ;; CMD has the form `(write REG ...)'.
790 (let* ((args (cdr cmd))
791 (i (1- (length args))))
792 (while args
793 (setq rrr (ccl-check-register (car args) cmd))
794 (ccl-embed-code 'write-register rrr i)
795 (setq args (cdr args) i (1- i)))))
796
797 ((listp rrr)
798 ;; CMD has the form `(write (LEFT OP RIGHT))'.
799 (let ((left (car rrr))
800 (op (get (nth 1 rrr) 'ccl-arith-code))
801 (right (nth 2 rrr)))
802 (if (listp left)
803 (progn
804 ;; RRR has the form `((EXPR OP2 ARG) OP RIGHT)'.
805 ;; Compile the first term as `(r7 = (EXPR OP2 ARG))'.
806 (ccl-compile-expression 'r7 left)
807 (setq left 'r7)))
808 ;; Now RRR has the form `(ARG OP RIGHT)'.
809 (if (integer-or-char-p right)
810 (progn
811 (ccl-embed-code 'write-expr-const 0 (ash op 3) left)
812 (ccl-embed-data right))
813 (ccl-check-register right rrr)
814 (ccl-embed-code 'write-expr-register 0
815 (logior (ash op 3)
816 (get right 'ccl-register-number))))))
817
818 (t
819 (error "CCL: Invalid argument: %s" cmd))))
820 nil)
821
822 ;; Compile CALL statement.
823 (defun ccl-compile-call (cmd)
824 (if (/= (length cmd) 2)
825 (error "CCL: Invalid number of arguments: %s" cmd))
826 (if (not (symbolp (nth 1 cmd)))
827 (error "CCL: Subroutine should be a symbol: %s" cmd))
828 (let* ((name (nth 1 cmd))
829 (idx (get name 'ccl-program-idx)))
830 (if (not idx)
831 (error "CCL: Unknown subroutine name: %s" name))
832 (ccl-embed-code 'call 0 idx))
833 nil)
834
835 ;; Compile END statement.
836 (defun ccl-compile-end (cmd)
837 (if (/= (length cmd) 1)
838 (error "CCL: Invalid number of arguments: %s" cmd))
839 (ccl-embed-code 'end 0 0)
840 t)
841
842 ;; Compile read-multibyte-character
843 (defun ccl-compile-read-multibyte-character (cmd)
844 (if (/= (length cmd) 3)
845 (error "CCL: Invalid number of arguments: %s" cmd))
846 (let ((RRR (nth 1 cmd))
847 (rrr (nth 2 cmd)))
848 (ccl-check-register rrr cmd)
849 (ccl-check-register RRR cmd)
850 (ccl-embed-extended-command 'read-multibyte-character rrr RRR 0))
851 nil)
852
853 ;; Compile write-multibyte-character
854 (defun ccl-compile-write-multibyte-character (cmd)
855 (if (/= (length cmd) 3)
856 (error "CCL: Invalid number of arguments: %s" cmd))
857 (let ((RRR (nth 1 cmd))
858 (rrr (nth 2 cmd)))
859 (ccl-check-register rrr cmd)
860 (ccl-check-register RRR cmd)
861 (ccl-embed-extended-command 'write-multibyte-character rrr RRR 0))
862 nil)
863
864 ;; Compile translate-character
865 ;; (defun ccl-compile-translate-character (cmd)
866 ;; (if (/= (length cmd) 4)
867 ;; (error "CCL: Invalid number of arguments: %s" cmd))
868 ;; (let ((Rrr (nth 1 cmd))
869 ;; (RRR (nth 2 cmd))
870 ;; (rrr (nth 3 cmd)))
871 ;; (ccl-check-register rrr cmd)
872 ;; (ccl-check-register RRR cmd)
873 ;; (cond ((and (symbolp Rrr) (not (get Rrr 'ccl-register-number)))
874 ;; (if (not (get Rrr 'translation-table))
875 ;; (error "CCL: Invalid translation table %s in %s" Rrr cmd))
876 ;; (ccl-embed-extended-command 'translate-character-const-tbl
877 ;; rrr RRR 0)
878 ;; (ccl-embed-data Rrr))
879 ;; (t
880 ;; (ccl-check-register Rrr cmd)
881 ;; (ccl-embed-extended-command 'translate-character rrr RRR Rrr))))
882 ;; nil)
883
884 ;; (defun ccl-compile-iterate-multiple-map (cmd)
885 ;; (ccl-compile-multiple-map-function 'iterate-multiple-map cmd)
886 ;; nil)
887
888 ;; (defun ccl-compile-map-multiple (cmd)
889 ;; (if (/= (length cmd) 4)
890 ;; (error "CCL: Invalid number of arguments: %s" cmd))
891 ;; (let ((func '(lambda (arg mp)
892 ;; (let ((len 0) result add)
893 ;; (while arg
894 ;; (if (consp (car arg))
895 ;; (setq add (funcall func (car arg) t)
896 ;; result (append result add)
897 ;; add (+ (-(car add)) 1))
898 ;; (setq result
899 ;; (append result
900 ;; (list (car arg)))
901 ;; add 1))
902 ;; (setq arg (cdr arg)
903 ;; len (+ len add)))
904 ;; (if mp
905 ;; (cons (- len) result)
906 ;; result))))
907 ;; arg)
908 ;; (setq arg (append (list (nth 0 cmd) (nth 1 cmd) (nth 2 cmd))
909 ;; (funcall func (nth 3 cmd) nil)))
910 ;; (ccl-compile-multiple-map-function 'map-multiple arg))
911 ;; nil)
912
913 ;; (defun ccl-compile-map-single (cmd)
914 ;; (if (/= (length cmd) 4)
915 ;; (error "CCL: Invalid number of arguments: %s" cmd))
916 ;; (let ((RRR (nth 1 cmd))
917 ;; (rrr (nth 2 cmd))
918 ;; (map (nth 3 cmd))
919 ;; id)
920 ;; (ccl-check-register rrr cmd)
921 ;; (ccl-check-register RRR cmd)
922 ;; (ccl-embed-extended-command 'map-single rrr RRR 0)
923 ;; (cond ((symbolp map)
924 ;; (if (get map 'code-conversion-map)
925 ;; (ccl-embed-data map)
926 ;; (error "CCL: Invalid map: %s" map)))
927 ;; (t
928 ;; (error "CCL: Invalid type of arguments: %s" cmd))))
929 ;; nil)
930
931 ;; (defun ccl-compile-multiple-map-function (command cmd)
932 ;; (if (< (length cmd) 4)
933 ;; (error "CCL: Invalid number of arguments: %s" cmd))
934 ;; (let ((RRR (nth 1 cmd))
935 ;; (rrr (nth 2 cmd))
936 ;; (args (nthcdr 3 cmd))
937 ;; map)
938 ;; (ccl-check-register rrr cmd)
939 ;; (ccl-check-register RRR cmd)
940 ;; (ccl-embed-extended-command command rrr RRR 0)
941 ;; (ccl-embed-data (length args))
942 ;; (while args
943 ;; (setq map (car args))
944 ;; (cond ((symbolp map)
945 ;; (if (get map 'code-conversion-map)
946 ;; (ccl-embed-data map)
947 ;; (error "CCL: Invalid map: %s" map)))
948 ;; ((numberp map)
949 ;; (ccl-embed-data map))
950 ;; (t
951 ;; (error "CCL: Invalid type of arguments: %s" cmd)))
952 ;; (setq args (cdr args)))))
953
954
955 ;;; CCL dump staffs
956
957 ;; To avoid byte-compiler warning.
958 (defvar ccl-code)
959
960 ;;;###autoload
961 (defun ccl-dump (ccl-code)
962 "Disassemble compiled CCL-CODE."
963 (let ((len (length ccl-code))
964 (buffer-mag (aref ccl-code 0)))
965 (cond ((= buffer-mag 0)
966 (insert "Don't output anything.\n"))
967 ((= buffer-mag 1)
968 (insert "Out-buffer must be as large as in-buffer.\n"))
969 (t
970 (insert
971 (format "Out-buffer must be %d times bigger than in-buffer.\n"
972 buffer-mag))))
973 (insert "Main-body:\n")
974 (setq ccl-current-ic 2)
975 (if (> (aref ccl-code 1) 0)
976 (progn
977 (while (< ccl-current-ic (aref ccl-code 1))
978 (ccl-dump-1))
979 (insert "At EOF:\n")))
980 (while (< ccl-current-ic len)
981 (ccl-dump-1))
982 ))
983
984 ;; Return a CCL code in `ccl-code' at `ccl-current-ic'.
985 (defun ccl-get-next-code ()
986 (prog1
987 (aref ccl-code ccl-current-ic)
988 (setq ccl-current-ic (1+ ccl-current-ic))))
989
990 (defun ccl-dump-1 ()
991 (let* ((code (ccl-get-next-code))
992 (cmd (aref ccl-code-table (logand code 31)))
993 (rrr (ash (logand code 255) -5))
994 (cc (ash code -8)))
995 (insert (format "%5d:[%s] " (1- ccl-current-ic) cmd))
996 (funcall (get cmd 'ccl-dump-function) rrr cc)))
997
998 (defun ccl-dump-set-register (rrr cc)
999 (insert (format "r%d = r%d\n" rrr cc)))
1000
1001 (defun ccl-dump-set-short-const (rrr cc)
1002 (insert (format "r%d = %d\n" rrr cc)))
1003
1004 (defun ccl-dump-set-const (rrr ignore)
1005 (insert (format "r%d = %d\n" rrr (ccl-get-next-code))))
1006
1007 (defun ccl-dump-set-array (rrr cc)
1008 (let ((rrr2 (logand cc 7))
1009 (len (ash cc -3))
1010 (i 0))
1011 (insert (format "r%d = array[r%d] of length %d\n\t"
1012 rrr rrr2 len))
1013 (while (< i len)
1014 (insert (format "%d " (ccl-get-next-code)))
1015 (setq i (1+ i)))
1016 (insert "\n")))
1017
1018 (defun ccl-dump-jump (ignore cc &optional address)
1019 (insert (format "jump to %d(" (+ (or address ccl-current-ic) cc)))
1020 (if (>= cc 0)
1021 (insert "+"))
1022 (insert (format "%d)\n" (1+ cc))))
1023
1024 (defun ccl-dump-jump-cond (rrr cc)
1025 (insert (format "if (r%d == 0), " rrr))
1026 (ccl-dump-jump nil cc))
1027
1028 (defun ccl-dump-write-register-jump (rrr cc)
1029 (insert (format "write r%d, " rrr))
1030 (ccl-dump-jump nil cc))
1031
1032 (defun ccl-dump-write-register-read-jump (rrr cc)
1033 (insert (format "write r%d, read r%d, " rrr rrr))
1034 (ccl-dump-jump nil cc)
1035 (ccl-get-next-code) ; Skip dummy READ-JUMP
1036 )
1037
1038 (defun ccl-extract-arith-op (cc)
1039 (aref ccl-arith-table (ash cc -6)))
1040
1041 (defun ccl-dump-write-expr-const (ignore cc)
1042 (insert (format "write (r%d %s %d)\n"
1043 (logand cc 7)
1044 (ccl-extract-arith-op cc)
1045 (ccl-get-next-code))))
1046
1047 (defun ccl-dump-write-expr-register (ignore cc)
1048 (insert (format "write (r%d %s r%d)\n"
1049 (logand cc 7)
1050 (ccl-extract-arith-op cc)
1051 (logand (ash cc -3) 7))))
1052
1053 (defun ccl-dump-insert-char (cc)
1054 (cond ((= cc ?\t) (insert " \"^I\""))
1055 ((= cc ?\n) (insert " \"^J\""))
1056 (t (insert (format " \"%c\"" cc)))))
1057
1058 (defun ccl-dump-write-const-jump (ignore cc)
1059 (let ((address ccl-current-ic))
1060 (insert "write char")
1061 (ccl-dump-insert-char (ccl-get-next-code))
1062 (insert ", ")
1063 (ccl-dump-jump nil cc address)))
1064
1065 (defun ccl-dump-write-const-read-jump (rrr cc)
1066 (let ((address ccl-current-ic))
1067 (insert "write char")
1068 (ccl-dump-insert-char (ccl-get-next-code))
1069 (insert (format ", read r%d, " rrr))
1070 (ccl-dump-jump cc address)
1071 (ccl-get-next-code) ; Skip dummy READ-JUMP
1072 ))
1073
1074 (defun ccl-dump-write-string-jump (ignore cc)
1075 (let ((address ccl-current-ic)
1076 (len (ccl-get-next-code))
1077 (i 0))
1078 (insert "write \"")
1079 (while (< i len)
1080 (let ((code (ccl-get-next-code)))
1081 (insert (ash code -16))
1082 (if (< (1+ i) len) (insert (logand (ash code -8) 255)))
1083 (if (< (+ i 2) len) (insert (logand code 255))))
1084 (setq i (+ i 3)))
1085 (insert "\", ")
1086 (ccl-dump-jump nil cc address)))
1087
1088 (defun ccl-dump-write-array-read-jump (rrr cc)
1089 (let ((address ccl-current-ic)
1090 (len (ccl-get-next-code))
1091 (i 0))
1092 (insert (format "write array[r%d] of length %d,\n\t" rrr len))
1093 (while (< i len)
1094 (ccl-dump-insert-char (ccl-get-next-code))
1095 (setq i (1+ i)))
1096 (insert (format "\n\tthen read r%d, " rrr))
1097 (ccl-dump-jump nil cc address)
1098 (ccl-get-next-code) ; Skip dummy READ-JUMP.
1099 ))
1100
1101 (defun ccl-dump-read-jump (rrr cc)
1102 (insert (format "read r%d, " rrr))
1103 (ccl-dump-jump nil cc))
1104
1105 (defun ccl-dump-branch (rrr len)
1106 (let ((jump-table-head ccl-current-ic)
1107 (i 0))
1108 (insert (format "jump to array[r%d] of length %d\n\t" rrr len))
1109 (while (<= i len)
1110 (insert (format "%d " (+ jump-table-head (ccl-get-next-code))))
1111 (setq i (1+ i)))
1112 (insert "\n")))
1113
1114 (defun ccl-dump-read-register (rrr cc)
1115 (insert (format "read r%d (%d remaining)\n" rrr cc)))
1116
1117 (defun ccl-dump-read-branch (rrr len)
1118 (insert (format "read r%d, " rrr))
1119 (ccl-dump-branch rrr len))
1120
1121 (defun ccl-dump-write-register (rrr cc)
1122 (insert (format "write r%d (%d remaining)\n" rrr cc)))
1123
1124 (defun ccl-dump-call (ignore cc)
1125 (insert (format "call subroutine #%d\n" cc)))
1126
1127 (defun ccl-dump-write-const-string (rrr cc)
1128 (if (= rrr 0)
1129 (progn
1130 (insert "write char")
1131 (ccl-dump-insert-char cc)
1132 (newline))
1133 (let ((len cc)
1134 (i 0))
1135 (insert "write \"")
1136 (while (< i len)
1137 (let ((code (ccl-get-next-code)))
1138 (insert (format "%c" (lsh code -16)))
1139 (if (< (1+ i) len)
1140 (insert (format "%c" (logand (lsh code -8) 255))))
1141 (if (< (+ i 2) len)
1142 (insert (format "%c" (logand code 255))))
1143 (setq i (+ i 3))))
1144 (insert "\"\n"))))
1145
1146 (defun ccl-dump-write-array (rrr cc)
1147 (let ((i 0))
1148 (insert (format "write array[r%d] of length %d\n\t" rrr cc))
1149 (while (< i cc)
1150 (ccl-dump-insert-char (ccl-get-next-code))
1151 (setq i (1+ i)))
1152 (insert "\n")))
1153
1154 (defun ccl-dump-end (&rest ignore)
1155 (insert "end\n"))
1156
1157 (defun ccl-dump-set-assign-expr-const (rrr cc)
1158 (insert (format "r%d %s= %d\n"
1159 rrr
1160 (ccl-extract-arith-op cc)
1161 (ccl-get-next-code))))
1162
1163 (defun ccl-dump-set-assign-expr-register (rrr cc)
1164 (insert (format "r%d %s= r%d\n"
1165 rrr
1166 (ccl-extract-arith-op cc)
1167 (logand cc 7))))
1168
1169 (defun ccl-dump-set-expr-const (rrr cc)
1170 (insert (format "r%d = r%d %s %d\n"
1171 rrr
1172 (logand cc 7)
1173 (ccl-extract-arith-op cc)
1174 (ccl-get-next-code))))
1175
1176 (defun ccl-dump-set-expr-register (rrr cc)
1177 (insert (format "r%d = r%d %s r%d\n"
1178 rrr
1179 (logand cc 7)
1180 (ccl-extract-arith-op cc)
1181 (logand (ash cc -3) 7))))
1182
1183 (defun ccl-dump-jump-cond-expr-const (rrr cc)
1184 (let ((address ccl-current-ic))
1185 (insert (format "if !(r%d %s %d), "
1186 rrr
1187 (aref ccl-arith-table (ccl-get-next-code))
1188 (ccl-get-next-code)))
1189 (ccl-dump-jump nil cc address)))
1190
1191 (defun ccl-dump-jump-cond-expr-register (rrr cc)
1192 (let ((address ccl-current-ic))
1193 (insert (format "if !(r%d %s r%d), "
1194 rrr
1195 (aref ccl-arith-table (ccl-get-next-code))
1196 (ccl-get-next-code)))
1197 (ccl-dump-jump nil cc address)))
1198
1199 (defun ccl-dump-read-jump-cond-expr-const (rrr cc)
1200 (insert (format "read r%d, " rrr))
1201 (ccl-dump-jump-cond-expr-const rrr cc))
1202
1203 (defun ccl-dump-read-jump-cond-expr-register (rrr cc)
1204 (insert (format "read r%d, " rrr))
1205 (ccl-dump-jump-cond-expr-register rrr cc))
1206
1207 (defun ccl-dump-binary (ccl-code)
1208 (let ((len (length ccl-code))
1209 (i 2))
1210 (while (< i len)
1211 (let ((code (aref ccl-code i))
1212 (j 27))
1213 (while (>= j 0)
1214 (insert (if (= (logand code (ash 1 j)) 0) ?0 ?1))
1215 (setq j (1- j)))
1216 (setq code (logand code 31))
1217 (if (< code (length ccl-code-table))
1218 (insert (format ":%s" (aref ccl-code-table code))))
1219 (insert "\n"))
1220 (setq i (1+ i)))))
1221
1222 (defun ccl-dump-ex-cmd (rrr cc)
1223 (let* ((RRR (logand cc ?\x7))
1224 (Rrr (logand (ash cc -3) ?\x7))
1225 (ex-op (aref ccl-extended-code-table (logand (ash cc -6) ?\x3fff))))
1226 (insert (format "<%s> " ex-op))
1227 (funcall (get ex-op 'ccl-dump-function) rrr RRR Rrr)))
1228
1229 (defun ccl-dump-read-multibyte-character (rrr RRR Rrr)
1230 (insert (format "read-multibyte-character r%d r%d\n" RRR rrr)))
1231
1232 (defun ccl-dump-write-multibyte-character (rrr RRR Rrr)
1233 (insert (format "write-multibyte-character r%d r%d\n" RRR rrr)))
1234
1235 ;; (defun ccl-dump-translate-character (rrr RRR Rrr)
1236 ;; (insert (format "translation table(r%d) r%d r%d\n" Rrr RRR rrr)))
1237
1238 ;; (defun ccl-dump-translate-character-const-tbl (rrr RRR Rrr)
1239 ;; (let ((tbl (ccl-get-next-code)))
1240 ;; (insert (format "translation table(%S) r%d r%d\n" tbl RRR rrr))))
1241
1242 ;; (defun ccl-dump-iterate-multiple-map (rrr RRR Rrr)
1243 ;; (let ((notbl (ccl-get-next-code))
1244 ;; (i 0) id)
1245 ;; (insert (format "iterate-multiple-map r%d r%d\n" RRR rrr))
1246 ;; (insert (format "\tnumber of maps is %d .\n\t [" notbl))
1247 ;; (while (< i notbl)
1248 ;; (setq id (ccl-get-next-code))
1249 ;; (insert (format "%S" id))
1250 ;; (setq i (1+ i)))
1251 ;; (insert "]\n")))
1252
1253 ;; (defun ccl-dump-map-multiple (rrr RRR Rrr)
1254 ;; (let ((notbl (ccl-get-next-code))
1255 ;; (i 0) id)
1256 ;; (insert (format "map-multiple r%d r%d\n" RRR rrr))
1257 ;; (insert (format "\tnumber of maps and separators is %d\n\t [" notbl))
1258 ;; (while (< i notbl)
1259 ;; (setq id (ccl-get-next-code))
1260 ;; (if (= id -1)
1261 ;; (insert "]\n\t [")
1262 ;; (insert (format "%S " id)))
1263 ;; (setq i (1+ i)))
1264 ;; (insert "]\n")))
1265
1266 ;; (defun ccl-dump-map-single (rrr RRR Rrr)
1267 ;; (let ((id (ccl-get-next-code)))
1268 ;; (insert (format "map-single r%d r%d map(%S)\n" RRR rrr id))))
1269
1270
1271 ;; CCL emulation staffs
1272
1273 ;; Not yet implemented.
1274
1275 ;; Auto-loaded functions.
1276
1277 ;;;###autoload
1278 (defmacro declare-ccl-program (name &optional vector)
1279 "Declare NAME as a name of CCL program.
1280
1281 To compile a CCL program which calls another CCL program not yet
1282 defined, it must be declared as a CCL program in advance.
1283 Optional arg VECTOR is a compiled CCL code of the CCL program."
1284 `(put ',name 'ccl-program-idx (register-ccl-program ',name ,vector)))
1285
1286 ;;;###autoload
1287 (defmacro define-ccl-program (name ccl-program &optional doc)
1288 "Set NAME the compiled code of CCL-PROGRAM.
1289 CCL-PROGRAM is `eval'ed before being handed to the CCL compiler `ccl-compile'.
1290 The compiled code is a vector of integers."
1291 `(let ((prog ,(ccl-compile (eval ccl-program))))
1292 (defconst ,name prog ,doc)
1293 (put ',name 'ccl-program-idx (register-ccl-program ',name prog))
1294 nil))
1295
1296 ;;;###autoload
1297 (defmacro check-ccl-program (ccl-program &optional name)
1298 "Check validity of CCL-PROGRAM.
1299 If CCL-PROGRAM is a symbol denoting a valid CCL program, return
1300 CCL-PROGRAM, else return nil.
1301 If CCL-PROGRAM is a vector and optional arg NAME (symbol) is supplied,
1302 register CCL-PROGRAM by name NAME, and return NAME."
1303 `(let ((result ,ccl-program))
1304 (cond ((symbolp ,ccl-program)
1305 (or (numberp (get ,ccl-program 'ccl-program-idx))
1306 (setq result nil)))
1307 ((vectorp ,ccl-program)
1308 (setq result ,name)
1309 (register-ccl-program result ,ccl-program))
1310 (t
1311 (setq result nil)))
1312 result))
1313
1314 ;;;###autoload
1315 (defun ccl-execute-with-args (ccl-prog &rest args)
1316 "Execute CCL-PROGRAM with registers initialized by the remaining args.
1317 The return value is a vector of resulting CCL registers."
1318 (let ((reg (make-vector 8 0))
1319 (i 0))
1320 (while (and args (< i 8))
1321 (if (not (integerp (car args)))
1322 (error "Arguments should be integer"))
1323 (aset reg i (car args))
1324 (setq args (cdr args) i (1+ i)))
1325 (ccl-execute ccl-prog reg)
1326 reg))
1327
1328 (provide 'ccl)
1329
1330 ;; ccl.el ends here