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