Coverity: Assert side effect: CID 2
[sxemacs] / lisp / mule / ccl.el
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 SXEmacs.
9
10 ;; SXEmacs 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 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; SXEmacs 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 this program.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;; Synched up with: FSF 21.0.90
24
25 ;;; Commentary:
26
27 ;; CCL (Code Conversion Language) is a simple programming language to
28 ;; be used for various kind of code conversion.  CCL program is
29 ;; compiled to CCL code (vector of integers) and executed by CCL
30 ;; interpreter of Emacs.
31 ;;
32 ;; CCL is used for code conversion at process I/O and file I/O for
33 ;; non-standard coding-system.  In addition, it is used for
34 ;; calculating a code point of X's font from a character code.
35 ;; However, since CCL is designed as a powerful programming language,
36 ;; it can be used for more generic calculation.  For instance,
37 ;; combination of three or more arithmetic operations can be
38 ;; calculated faster than Emacs Lisp.
39 ;;
40 ;; Syntax and semantics of CCL program is described in the
41 ;; documentation of `define-ccl-program'.
42
43 ;;; Code:
44 (eval-when-compile
45   (globally-declare-boundp 'ff))
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     (if (and (listp condition)
475              (listp (car condition)))
476         ;; If CONDITION is a nested expression, the inner expression
477         ;; should be compiled at first as SET statement, i.e.:
478         ;; `(if ((X OP2 Y) OP Z) ...)' is compiled into two statements:
479         ;; `(r7 = (X OP2 Y)) (if (r7 OP Z) ...)'.
480         (progn
481           (ccl-compile-expression 'r7 (car condition))
482           (setq condition (cons 'r7 (cdr condition)))
483           (setq cmd (cons (car cmd)
484                           (cons condition (cdr (cdr cmd)))))))
485
486     (setq jump-cond-address ccl-current-ic)
487     ;; Compile CONDITION.
488     (if (symbolp condition)
489         ;; CONDITION is a register.
490         (progn
491           (ccl-check-register condition cmd)
492           (ccl-embed-code 'jump-cond condition 0))
493       ;; CONDITION is a simple expression of the form (RRR OP ARG).
494       (let ((rrr (car condition))
495             (op (get (nth 1 condition) 'ccl-arith-code))
496             (arg (nth 2 condition)))
497         (ccl-check-register rrr cmd)
498         (if (integer-or-char-p arg)
499             (progn
500               (ccl-embed-code (if read-flag 'read-jump-cond-expr-const
501                                 'jump-cond-expr-const)
502                               rrr 0)
503               (ccl-embed-data op)
504               (ccl-embed-data arg))
505           (ccl-check-register arg cmd)
506           (ccl-embed-code (if read-flag 'read-jump-cond-expr-register 
507                             'jump-cond-expr-register)
508                           rrr 0)
509           (ccl-embed-data op)
510           (ccl-embed-data (get arg 'ccl-register-number)))))
511
512     ;; Compile TRUE-PART.
513     (let ((unconditional-jump (ccl-compile-1 true-cmds)))
514       (if (null false-cmds)
515           ;; This is the place to jump to if condition is false.
516           (progn
517             (ccl-embed-current-address jump-cond-address)
518             (setq unconditional-jump nil))
519         (let (end-true-part-address)
520           (if (not unconditional-jump)
521               (progn
522                 ;; If TRUE-PART does not end with unconditional jump, we
523                 ;; have to jump to the end of FALSE-PART from here.
524                 (setq end-true-part-address ccl-current-ic)
525                 (ccl-embed-code 'jump 0 0)))
526           ;; This is the place to jump to if CONDITION is false.
527           (ccl-embed-current-address jump-cond-address)
528           ;; Compile FALSE-PART.
529           (setq unconditional-jump
530                 (and (ccl-compile-1 false-cmds) unconditional-jump))
531           (if end-true-part-address
532               ;; This is the place to jump to after the end of TRUE-PART.
533               (ccl-embed-current-address end-true-part-address))))
534       unconditional-jump)))
535
536 ;; Compile BRANCH statement.
537 (defun ccl-compile-branch (cmd)
538   (if (< (length cmd) 3)
539       (error "CCL: Invalid number of arguments: %s" cmd))
540   (ccl-compile-branch-blocks 'branch
541                              (ccl-compile-branch-expression (nth 1 cmd) cmd)
542                              (cdr (cdr cmd))))
543
544 ;; Compile READ statement of the form `(read-branch EXPR BLOCK0 BLOCK1 ...)'.
545 (defun ccl-compile-read-branch (cmd)
546   (if (< (length cmd) 3)
547       (error "CCL: Invalid number of arguments: %s" cmd))
548   (ccl-compile-branch-blocks 'read-branch
549                              (ccl-compile-branch-expression (nth 1 cmd) cmd)
550                              (cdr (cdr cmd))))
551
552 ;; Compile EXPRESSION part of BRANCH statement and return register
553 ;; which holds a value of the expression.
554 (defun ccl-compile-branch-expression (expr cmd)
555   (if (listp expr)
556       ;; EXPR has the form `(EXPR2 OP ARG)'.  Compile it as SET
557       ;; statement of the form `(r7 = (EXPR2 OP ARG))'.
558       (progn
559         (ccl-compile-expression 'r7 expr)
560         'r7)
561     (ccl-check-register expr cmd)))
562
563 ;; Compile BLOCKs of BRANCH statement.  CODE is 'branch or 'read-branch.
564 ;; REG is a register which holds a value of EXPRESSION part.  BLOCKs
565 ;; is a list of CCL-BLOCKs.
566 (defun ccl-compile-branch-blocks (code rrr blocks)
567   (let ((branches (length blocks))
568         branch-idx
569         jump-table-head-address
570         empty-block-indexes
571         block-tail-addresses
572         block-unconditional-jump)
573     (ccl-embed-code code rrr branches)
574     (setq jump-table-head-address ccl-current-ic)
575     ;; The size of jump table is the number of blocks plus 1 (for the
576     ;; case RRR is out of range).
577     (ccl-increment-ic (1+ branches))
578     (setq empty-block-indexes (list branches))
579     ;; Compile each block.
580     (setq branch-idx 0)
581     (while blocks
582       (if (null (car blocks))
583           ;; This block is empty.
584           (setq empty-block-indexes (cons branch-idx empty-block-indexes)
585                 block-unconditional-jump t)
586         ;; This block is not empty.
587         (ccl-embed-data (- ccl-current-ic jump-table-head-address)
588                         (+ jump-table-head-address branch-idx))
589         (setq block-unconditional-jump (ccl-compile-1 (car blocks)))
590         (if (not block-unconditional-jump)
591             (progn
592               ;; Jump address of the end of branches are embedded later.
593               ;; For the moment, just remember where to embed them.
594               (setq block-tail-addresses
595                     (cons ccl-current-ic block-tail-addresses))
596               (ccl-embed-code 'jump 0 0))))
597       (setq branch-idx (1+ branch-idx))
598       (setq blocks (cdr blocks)))
599     (if (not block-unconditional-jump)
600         ;; We don't need jump code at the end of the last block.
601         (setq block-tail-addresses (cdr block-tail-addresses)
602               ccl-current-ic (1- ccl-current-ic)))
603     ;; Embed jump address at the tailing jump commands of blocks.
604     (while block-tail-addresses
605       (ccl-embed-current-address (car block-tail-addresses))
606       (setq block-tail-addresses (cdr block-tail-addresses)))
607     ;; For empty blocks, make entries in the jump table point directly here.
608     (while empty-block-indexes
609       (ccl-embed-data (- ccl-current-ic jump-table-head-address)
610                       (+ jump-table-head-address (car empty-block-indexes)))
611       (setq empty-block-indexes (cdr empty-block-indexes))))
612   ;; Branch command ends by unconditional jump if RRR is out of range.
613   nil)
614
615 ;; Compile LOOP statement.
616 (defun ccl-compile-loop (cmd)
617   (if (< (length cmd) 2)
618       (error "CCL: Invalid number of arguments: %s" cmd))
619   (let* ((ccl-loop-head ccl-current-ic)
620          (ccl-breaks nil)
621          unconditional-jump)
622     (setq cmd (cdr cmd))
623     (if cmd
624         (progn
625           (setq unconditional-jump t)
626           (while cmd
627             (setq unconditional-jump
628                   (and (ccl-compile-1 (car cmd)) unconditional-jump))
629             (setq cmd (cdr cmd)))
630           (if (not ccl-breaks)
631               unconditional-jump
632             ;; Embed jump address for break statements encountered in
633             ;; this loop.
634             (while ccl-breaks
635               (ccl-embed-current-address (car ccl-breaks))
636               (setq ccl-breaks (cdr ccl-breaks))))
637           nil))))
638
639 ;; Compile BREAK statement.
640 (defun ccl-compile-break (cmd)
641   (if (/= (length cmd) 1)
642       (error "CCL: Invalid number of arguments: %s" cmd))
643   (if (null ccl-loop-head)
644       (error "CCL: No outer loop: %s" cmd))
645   (setq ccl-breaks (cons ccl-current-ic ccl-breaks))
646   (ccl-embed-code 'jump 0 0)
647   t)
648
649 ;; Compile REPEAT statement.
650 (defun ccl-compile-repeat (cmd)
651   (if (/= (length cmd) 1)
652       (error "CCL: Invalid number of arguments: %s" cmd))
653   (if (null ccl-loop-head)
654       (error "CCL: No outer loop: %s" cmd))
655   (ccl-embed-code 'jump 0 ccl-loop-head)
656   t)
657
658 ;; Compile WRITE-REPEAT statement.
659 (defun ccl-compile-write-repeat (cmd)
660   (if (/= (length cmd) 2)
661       (error "CCL: Invalid number of arguments: %s" cmd))
662   (if (null ccl-loop-head)
663       (error "CCL: No outer loop: %s" cmd))
664   (let ((arg (nth 1 cmd)))
665     (cond ((integer-or-char-p arg)
666            (ccl-embed-code 'write-const-jump 0 ccl-loop-head)
667            (ccl-embed-data arg))
668           ((stringp arg)
669            (setq arg (encode-coding-string arg 'binary))
670            (let ((len (length arg)))
671              (ccl-embed-code 'write-string-jump 0 ccl-loop-head)
672              (ccl-embed-data len)
673              (ccl-embed-string len arg)))
674           (t
675            (ccl-check-register arg cmd)
676            (ccl-embed-code 'write-register-jump arg ccl-loop-head))))
677   t)
678
679 ;; Compile WRITE-READ-REPEAT statement.
680 (defun ccl-compile-write-read-repeat (cmd)
681   (if (or (< (length cmd) 2) (> (length cmd) 3))
682       (error "CCL: Invalid number of arguments: %s" cmd))
683   (if (null ccl-loop-head)
684       (error "CCL: No outer loop: %s" cmd))
685   (let ((rrr (ccl-check-register (nth 1 cmd) cmd))
686         (arg (nth 2 cmd)))
687     (cond ((null arg)
688            (ccl-embed-code 'write-register-read-jump rrr ccl-loop-head))
689           ((integer-or-char-p arg)
690            (ccl-embed-code 'write-const-read-jump rrr arg ccl-loop-head))
691           ((vectorp arg)
692            (let ((len (length arg))
693                  (i 0))
694              (ccl-embed-code 'write-array-read-jump rrr ccl-loop-head)
695              (ccl-embed-data len)
696              (while (< i len)
697                (ccl-embed-data (aref arg i))
698                (setq i (1+ i)))))
699           (t
700            (error "CCL: Invalid argument %s: %s" arg cmd)))
701     (ccl-embed-code 'read-jump rrr ccl-loop-head))
702   t)
703                             
704 ;; Compile READ statement.
705 (defun ccl-compile-read (cmd)
706   (if (< (length cmd) 2)
707       (error "CCL: Invalid number of arguments: %s" cmd))
708   (let* ((args (cdr cmd))
709          (i (1- (length args))))
710     (while args
711       (let ((rrr (ccl-check-register (car args) cmd)))
712         (ccl-embed-code 'read-register rrr i)
713         (setq args (cdr args) i (1- i)))))
714   nil)
715
716 ;; Compile READ-IF statement.
717 (defun ccl-compile-read-if (cmd)
718   (ccl-compile-if cmd 'read))
719
720 ;; Compile WRITE statement.
721 (defun ccl-compile-write (cmd)
722   (if (< (length cmd) 2)
723       (error "CCL: Invalid number of arguments: %s" cmd))
724   (let ((rrr (nth 1 cmd)))
725     (cond ((integer-or-char-p rrr)
726            (ccl-embed-code 'write-const-string 0 rrr))
727           ((stringp rrr)
728            (ccl-compile-write-string rrr))
729           ((and (symbolp rrr) (vectorp (nth 2 cmd)))
730            (ccl-check-register rrr cmd)
731            ;; CMD has the form `(write REG ARRAY)'.
732            (let* ((arg (nth 2 cmd))
733                   (len (length arg))
734                   (i 0))
735              (ccl-embed-code 'write-array rrr len)
736              (while (< i len)
737                (if (not (integer-or-char-p (aref arg i)))
738                    (error "CCL: Invalid argument %s: %s" arg cmd))
739                (ccl-embed-data (aref arg i))
740                (setq i (1+ i)))))
741
742           ((symbolp rrr)
743            ;; CMD has the form `(write REG ...)'.
744            (let* ((args (cdr cmd))
745                   (i (1- (length args))))
746              (while args
747                (setq rrr (ccl-check-register (car args) cmd))
748                (ccl-embed-code 'write-register rrr i)
749                (setq args (cdr args) i (1- i)))))
750
751           ((listp rrr)
752            ;; CMD has the form `(write (LEFT OP RIGHT))'.
753            (let ((left (car rrr))
754                  (op (get (nth 1 rrr) 'ccl-arith-code))
755                  (right (nth 2 rrr)))
756              (if (listp left)
757                  (progn
758                    ;; RRR has the form `((EXPR OP2 ARG) OP RIGHT)'.
759                    ;; Compile the first term as `(r7 = (EXPR OP2 ARG))'.
760                    (ccl-compile-expression 'r7 left)
761                    (setq left 'r7)))
762              ;; Now RRR has the form `(ARG OP RIGHT)'.
763              (if (integer-or-char-p right)
764                  (progn
765                    (ccl-embed-code 'write-expr-const 0 (ash op 3) left)
766                    (ccl-embed-data right))
767                (ccl-check-register right rrr)
768                (ccl-embed-code 'write-expr-register 0
769                                (logior (ash op 3)
770                                        (get right 'ccl-register-number))))))
771
772           (t
773            (error "CCL: Invalid argument: %s" cmd))))
774   nil)
775
776 ;; Compile CALL statement.
777 (defun ccl-compile-call (cmd)
778   (if (/= (length cmd) 2)
779       (error "CCL: Invalid number of arguments: %s" cmd))
780   (if (not (symbolp (nth 1 cmd)))
781       (error "CCL: Subroutine should be a symbol: %s" cmd))
782   (ccl-embed-code 'call 1 0)
783   (ccl-embed-symbol (nth 1 cmd) 'ccl-program-idx)
784   nil)
785
786 ;; Compile END statement.
787 (defun ccl-compile-end (cmd)
788   (if (/= (length cmd) 1)
789       (error "CCL: Invalid number of arguments: %s" cmd))
790   (ccl-embed-code 'end 0 0)
791   t)
792
793 ;; Compile read-multibyte-character
794 (defun ccl-compile-read-multibyte-character (cmd)
795   (if (/= (length cmd) 3)
796       (error "CCL: Invalid number of arguments: %s" cmd))
797   (let ((RRR (nth 1 cmd))
798         (rrr (nth 2 cmd)))
799     (ccl-check-register rrr cmd)
800     (ccl-check-register RRR cmd)
801     (ccl-embed-extended-command 'read-multibyte-character rrr RRR 0))
802   nil)
803
804 ;; Compile write-multibyte-character
805 (defun ccl-compile-write-multibyte-character (cmd)
806   (if (/= (length cmd) 3)
807       (error "CCL: Invalid number of arguments: %s" cmd))
808   (let ((RRR (nth 1 cmd))
809         (rrr (nth 2 cmd)))
810     (ccl-check-register rrr cmd)
811     (ccl-check-register RRR cmd)
812     (ccl-embed-extended-command 'write-multibyte-character rrr RRR 0))
813   nil)
814
815 ;; Compile translate-character
816 (defun ccl-compile-translate-character (cmd)
817   (if (/= (length cmd) 4)
818       (error "CCL: Invalid number of arguments: %s" cmd))
819   (let ((Rrr (nth 1 cmd))
820         (RRR (nth 2 cmd))
821         (rrr (nth 3 cmd)))
822     (ccl-check-register rrr cmd)
823     (ccl-check-register RRR cmd)
824     (cond ((and (symbolp Rrr) (not (get Rrr 'ccl-register-number)))
825            (ccl-embed-extended-command 'translate-character-const-tbl
826                                        rrr RRR 0)
827            (ccl-embed-symbol Rrr 'translation-table-id))
828           (t
829            (ccl-check-register Rrr cmd)
830            (ccl-embed-extended-command 'translate-character rrr RRR Rrr))))
831   nil)
832
833 (defun ccl-compile-iterate-multiple-map (cmd)
834   (ccl-compile-multiple-map-function 'iterate-multiple-map cmd)
835   nil)
836
837 (defun ccl-compile-map-multiple (cmd)
838   (if (/= (length cmd) 4)
839       (error "CCL: Invalid number of arguments: %s" cmd))
840   (let (func arg)
841     (setq func
842           (lambda (arg mp)
843             (let ((len 0) result add)
844               (while arg
845                 (if (consp (car arg))
846                     (setq add (funcall func (car arg) t)
847                           result (append result add)
848                           add (+ (- (car add)) 1))
849                   (setq result
850                         (append result
851                                 (list (car arg)))
852                         add 1))
853                 (setq arg (cdr arg)
854                       len (+ len add)))
855               (if mp 
856                   (cons (- len) result)
857                 result))))
858     (setq arg (append (list (nth 0 cmd) (nth 1 cmd) (nth 2 cmd))
859                       (funcall func (nth 3 cmd) nil)))
860     (ccl-compile-multiple-map-function 'map-multiple arg))
861   nil)
862
863 (defun ccl-compile-map-single (cmd)
864   (if (/= (length cmd) 4)
865       (error "CCL: Invalid number of arguments: %s" cmd))
866   (let ((RRR (nth 1 cmd))
867         (rrr (nth 2 cmd))
868         (map (nth 3 cmd)))
869     (ccl-check-register rrr cmd)
870     (ccl-check-register RRR cmd)
871     (ccl-embed-extended-command 'map-single rrr RRR 0)
872     (cond ((symbolp map)
873            (if (get map 'code-conversion-map)
874                (ccl-embed-symbol map 'code-conversion-map-id)
875              (error "CCL: Invalid map: %s" map)))
876           (t
877            (error "CCL: Invalid type of arguments: %s" cmd))))
878   nil)
879
880 (defun ccl-compile-multiple-map-function (command cmd)
881   (if (< (length cmd) 4)
882       (error "CCL: Invalid number of arguments: %s" cmd))
883   (let ((RRR (nth 1 cmd))
884         (rrr (nth 2 cmd))
885         (args (nthcdr 3 cmd))
886         map)
887     (ccl-check-register rrr cmd)
888     (ccl-check-register RRR cmd)
889     (ccl-embed-extended-command command rrr RRR 0)
890     (ccl-embed-data (length args))
891     (while args
892       (setq map (car args))
893       (cond ((symbolp map)
894              (if (get map 'code-conversion-map)
895                  (ccl-embed-symbol map 'code-conversion-map-id)
896                (error "CCL: Invalid map: %s" map)))
897             ((numberp map)
898              (ccl-embed-data map))
899             (t
900              (error "CCL: Invalid type of arguments: %s" cmd)))
901       (setq args (cdr args)))))
902
903 \f
904 ;;; CCL dump staffs
905
906 ;; To avoid byte-compiler warning.
907 (defvar ccl-code)
908
909 ;;;###autoload
910 (defun ccl-dump (ccl-code)
911   "Disassemble compiled CCL-CODE."
912   (let ((len (length ccl-code))
913         (buffer-mag (aref ccl-code 0)))
914     (cond ((= buffer-mag 0)
915            (insert "Don't output anything.\n"))
916           ((= buffer-mag 1)
917            (insert "Out-buffer must be as large as in-buffer.\n"))
918           (t
919            (insert
920             (format "Out-buffer must be %d times bigger than in-buffer.\n"
921                     buffer-mag))))
922     (insert "Main-body:\n")
923     (setq ccl-current-ic 2)
924     (if (> (aref ccl-code 1) 0)
925         (progn
926           (while (< ccl-current-ic (aref ccl-code 1))
927             (ccl-dump-1))
928           (insert "At EOF:\n")))
929     (while (< ccl-current-ic len)
930       (ccl-dump-1))
931     ))
932
933 ;; Return a CCL code in `ccl-code' at `ccl-current-ic'.
934 (defun ccl-get-next-code ()
935   (prog1
936       (aref ccl-code ccl-current-ic)
937     (setq ccl-current-ic (1+ ccl-current-ic))))
938
939 (defun ccl-dump-1 ()
940   (let* ((code (ccl-get-next-code))
941          (cmd (aref ccl-code-table (logand code 31)))
942          (rrr (ash (logand code 255) -5))
943          (cc (ash code -8)))
944     (insert (format "%5d:[%s] " (1- ccl-current-ic) cmd))
945     (funcall (get cmd 'ccl-dump-function) rrr cc))) 
946
947 (defun ccl-dump-set-register (rrr cc)
948   (insert (format "r%d = r%d\n" rrr cc)))
949
950 (defun ccl-dump-set-short-const (rrr cc)
951   (insert (format "r%d = %d\n" rrr cc)))
952
953 (defun ccl-dump-set-const (rrr ignore)
954   (insert (format "r%d = %d\n" rrr (ccl-get-next-code))))
955
956 (defun ccl-dump-set-array (rrr cc)
957   (let ((rrr2 (logand cc 7))
958         (len (ash cc -3))
959         (i 0))
960     (insert (format "r%d = array[r%d] of length %d\n\t"
961                     rrr rrr2 len))
962     (while (< i len)
963       (insert (format "%d " (ccl-get-next-code)))
964       (setq i (1+ i)))
965     (insert "\n")))
966
967 (defun ccl-dump-jump (ignore cc &optional address)
968   (insert (format "jump to %d(" (+ (or address ccl-current-ic) cc)))
969   (if (>= cc 0)
970       (insert "+"))
971   (insert (format "%d)\n" (1+ cc))))
972
973 (defun ccl-dump-jump-cond (rrr cc)
974   (insert (format "if (r%d == 0), " rrr))
975   (ccl-dump-jump nil cc))
976
977 (defun ccl-dump-write-register-jump (rrr cc)
978   (insert (format "write r%d, " rrr))
979   (ccl-dump-jump nil cc))
980
981 (defun ccl-dump-write-register-read-jump (rrr cc)
982   (insert (format "write r%d, read r%d, " rrr rrr))
983   (ccl-dump-jump nil cc)
984   (ccl-get-next-code)                   ; Skip dummy READ-JUMP
985   )
986
987 (defun ccl-extract-arith-op (cc)
988   (aref ccl-arith-table (ash cc -6)))
989
990 (defun ccl-dump-write-expr-const (ignore cc)
991   (insert (format "write (r%d %s %d)\n"
992                   (logand cc 7)
993                   (ccl-extract-arith-op cc)
994                   (ccl-get-next-code))))
995
996 (defun ccl-dump-write-expr-register (ignore cc)
997   (insert (format "write (r%d %s r%d)\n"
998                   (logand cc 7)
999                   (ccl-extract-arith-op cc)
1000                   (logand (ash cc -3) 7))))
1001
1002 (defun ccl-dump-insert-char (cc)
1003   (cond ((= cc ?\t) (insert " \"^I\""))
1004         ((= cc ?\n) (insert " \"^J\""))
1005         (t (insert (format " \"%c\"" cc)))))
1006
1007 (defun ccl-dump-write-const-jump (ignore cc)
1008   (let ((address ccl-current-ic))
1009     (insert "write char")
1010     (ccl-dump-insert-char (ccl-get-next-code))
1011     (insert ", ")
1012     (ccl-dump-jump nil cc address)))
1013
1014 (defun ccl-dump-write-const-read-jump (rrr cc)
1015   (let ((address ccl-current-ic))
1016     (insert "write char")
1017     (ccl-dump-insert-char (ccl-get-next-code))
1018     (insert (format ", read r%d, " rrr))
1019     (ccl-dump-jump cc address)
1020     (ccl-get-next-code)                 ; Skip dummy READ-JUMP
1021     ))
1022
1023 (defun ccl-dump-write-string-jump (ignore cc)
1024   (let ((address ccl-current-ic)
1025         (len (ccl-get-next-code))
1026         (i 0))
1027     (insert "write \"")
1028     (while (< i len)
1029       (let ((code (ccl-get-next-code)))
1030         (insert (ash code -16))
1031         (if (< (1+ i) len) (insert (logand (ash code -8) 255)))
1032         (if (< (+ i 2) len) (insert (logand code 255))))
1033       (setq i (+ i 3)))
1034     (insert "\", ")
1035     (ccl-dump-jump nil cc address)))
1036
1037 (defun ccl-dump-write-array-read-jump (rrr cc)
1038   (let ((address ccl-current-ic)
1039         (len (ccl-get-next-code))
1040         (i 0))
1041     (insert (format "write array[r%d] of length %d,\n\t" rrr len))
1042     (while (< i len)
1043       (ccl-dump-insert-char (ccl-get-next-code))
1044       (setq i (1+ i)))
1045     (insert (format "\n\tthen read r%d, " rrr))
1046     (ccl-dump-jump nil cc address)
1047     (ccl-get-next-code)                 ; Skip dummy READ-JUMP.
1048     ))
1049
1050 (defun ccl-dump-read-jump (rrr cc)
1051   (insert (format "read r%d, " rrr))
1052   (ccl-dump-jump nil cc))
1053
1054 (defun ccl-dump-branch (rrr len)
1055   (let ((jump-table-head ccl-current-ic)
1056         (i 0))
1057     (insert (format "jump to array[r%d] of length %d\n\t" rrr len))
1058     (while (<= i len)
1059       (insert (format "%d " (+ jump-table-head (ccl-get-next-code))))
1060       (setq i (1+ i)))
1061     (insert "\n")))
1062
1063 (defun ccl-dump-read-register (rrr cc)
1064   (insert (format "read r%d (%d remaining)\n" rrr cc)))
1065
1066 (defun ccl-dump-read-branch (rrr len)
1067   (insert (format "read r%d, " rrr))
1068   (ccl-dump-branch rrr len))
1069
1070 (defun ccl-dump-write-register (rrr cc)
1071   (insert (format "write r%d (%d remaining)\n" rrr cc)))
1072
1073 (defun ccl-dump-call (ignore cc)
1074   (insert (format "call subroutine #%d\n" cc)))
1075
1076 (defun ccl-dump-write-const-string (rrr cc)
1077   (if (= rrr 0)
1078       (progn
1079         (insert "write char")
1080         (ccl-dump-insert-char cc)
1081         (newline))
1082     (let ((len cc)
1083           (i 0))
1084       (insert "write \"")
1085       (while (< i len)
1086         (let ((code (ccl-get-next-code)))
1087           (insert (format "%c" (lsh code -16)))
1088           (if (< (1+ i) len)
1089               (insert (format "%c" (logand (lsh code -8) 255))))
1090           (if (< (+ i 2) len)
1091               (insert (format "%c" (logand code 255))))
1092           (setq i (+ i 3))))
1093       (insert "\"\n"))))
1094
1095 (defun ccl-dump-write-array (rrr cc)
1096   (let ((i 0))
1097     (insert (format "write array[r%d] of length %d\n\t" rrr cc))
1098     (while (< i cc)
1099       (ccl-dump-insert-char (ccl-get-next-code))
1100       (setq i (1+ i)))
1101     (insert "\n")))
1102
1103 (defun ccl-dump-end (&rest ignore)
1104   (insert "end\n"))
1105
1106 (defun ccl-dump-set-assign-expr-const (rrr cc)
1107   (insert (format "r%d %s= %d\n"
1108                   rrr
1109                   (ccl-extract-arith-op cc)
1110                   (ccl-get-next-code))))
1111
1112 (defun ccl-dump-set-assign-expr-register (rrr cc)
1113   (insert (format "r%d %s= r%d\n"
1114                   rrr
1115                   (ccl-extract-arith-op cc)
1116                   (logand cc 7))))
1117
1118 (defun ccl-dump-set-expr-const (rrr cc)
1119   (insert (format "r%d = r%d %s %d\n"
1120                   rrr
1121                   (logand cc 7)
1122                   (ccl-extract-arith-op cc)
1123                   (ccl-get-next-code))))
1124
1125 (defun ccl-dump-set-expr-register (rrr cc)
1126   (insert (format "r%d = r%d %s r%d\n"
1127                   rrr
1128                   (logand cc 7)
1129                   (ccl-extract-arith-op cc)
1130                   (logand (ash cc -3) 7))))
1131
1132 (defun ccl-dump-jump-cond-expr-const (rrr cc)
1133   (let ((address ccl-current-ic))
1134     (insert (format "if !(r%d %s %d), "
1135                     rrr
1136                     (aref ccl-arith-table (ccl-get-next-code))
1137                     (ccl-get-next-code)))
1138     (ccl-dump-jump nil cc address)))
1139
1140 (defun ccl-dump-jump-cond-expr-register (rrr cc)
1141   (let ((address ccl-current-ic))
1142     (insert (format "if !(r%d %s r%d), "
1143                     rrr
1144                     (aref ccl-arith-table (ccl-get-next-code))
1145                     (ccl-get-next-code)))
1146     (ccl-dump-jump nil cc address)))
1147
1148 (defun ccl-dump-read-jump-cond-expr-const (rrr cc)
1149   (insert (format "read r%d, " rrr))
1150   (ccl-dump-jump-cond-expr-const rrr cc))
1151
1152 (defun ccl-dump-read-jump-cond-expr-register (rrr cc)
1153   (insert (format "read r%d, " rrr))
1154   (ccl-dump-jump-cond-expr-register rrr cc))
1155
1156 (defun ccl-dump-binary (ccl-code)
1157   (let ((len (length ccl-code))
1158         (i 2))
1159     (while (< i len)
1160       (let ((code (aref ccl-code i))
1161             (j 27))
1162         (while (>= j 0)
1163           (insert (if (= (logand code (ash 1 j)) 0) ?0 ?1))
1164           (setq j (1- j)))
1165         (setq code (logand code 31))
1166         (if (< code (length ccl-code-table))
1167             (insert (format ":%s" (aref ccl-code-table code))))
1168         (insert "\n"))
1169       (setq i (1+ i)))))
1170
1171 (defun ccl-dump-ex-cmd (rrr cc)
1172   (let* ((RRR (logand cc ?\x7))
1173          (Rrr (logand (ash cc -3) ?\x7))
1174          (ex-op (aref ccl-extended-code-table (logand (ash cc -6) ?\x3fff))))
1175     (insert (format "<%s> " ex-op))
1176     (funcall (get ex-op 'ccl-dump-function) rrr RRR Rrr)))
1177
1178 (defun ccl-dump-read-multibyte-character (rrr RRR Rrr)
1179   (insert (format "read-multibyte-character r%d r%d\n" RRR rrr)))
1180
1181 (defun ccl-dump-write-multibyte-character (rrr RRR Rrr)
1182   (insert (format "write-multibyte-character r%d r%d\n" RRR rrr)))
1183
1184 (defun ccl-dump-translate-character (rrr RRR Rrr)
1185   (insert (format "translation table(r%d) r%d r%d\n" Rrr RRR rrr)))
1186
1187 (defun ccl-dump-translate-character-const-tbl (rrr RRR Rrr)
1188   (let ((tbl (ccl-get-next-code)))
1189     (insert (format "translation table(%S) r%d r%d\n" tbl RRR rrr))))
1190
1191 (defun ccl-dump-iterate-multiple-map (rrr RRR Rrr)
1192   (let ((notbl (ccl-get-next-code))
1193         (i 0) id)
1194     (insert (format "iterate-multiple-map r%d r%d\n" RRR rrr))
1195     (insert (format "\tnumber of maps is %d .\n\t [" notbl))
1196     (while (< i notbl)
1197       (setq id (ccl-get-next-code))
1198       (insert (format "%S" id))
1199       (setq i (1+ i)))
1200     (insert "]\n")))
1201
1202 (defun ccl-dump-map-multiple (rrr RRR Rrr)
1203   (let ((notbl (ccl-get-next-code))
1204         (i 0) id)
1205     (insert (format "map-multiple r%d r%d\n" RRR rrr))
1206     (insert (format "\tnumber of maps and separators is %d\n\t [" notbl))
1207     (while (< i notbl)
1208       (setq id (ccl-get-next-code))
1209       (if (= id -1)
1210           (insert "]\n\t [")
1211         (insert (format "%S " id)))
1212       (setq i (1+ i)))
1213     (insert "]\n")))
1214
1215 (defun ccl-dump-map-single (rrr RRR Rrr)
1216   (let ((id (ccl-get-next-code)))
1217     (insert (format "map-single r%d r%d map(%S)\n" RRR rrr id))))
1218
1219 \f
1220 ;; CCL emulation staffs 
1221
1222 ;; Not yet implemented.
1223 \f
1224 ;; Auto-loaded functions.
1225
1226 ;;;###autoload
1227 (defmacro declare-ccl-program (name &optional vector)
1228   "Declare NAME as a name of CCL program.
1229
1230 This macro exists for backward compatibility.  In the old version of
1231 Emacs, to compile a CCL program which calls another CCL program not
1232 yet defined, it must be declared as a CCL program in advance.  But,
1233 now CCL program names are resolved not at compile time but before
1234 execution.
1235
1236 Optional arg VECTOR is a compiled CCL code of the CCL program."
1237   `(put ',name 'ccl-program-idx (register-ccl-program ',name ,vector)))
1238
1239 ;;;###autoload
1240 (defmacro define-ccl-program (name ccl-program &optional doc)
1241   "Set NAME the compiled code of CCL-PROGRAM.
1242
1243 CCL-PROGRAM has this form:
1244         (BUFFER_MAGNIFICATION
1245          CCL_MAIN_CODE
1246          [ CCL_EOF_CODE ])
1247
1248 BUFFER_MAGNIFICATION is an integer value specifying the approximate
1249 output buffer magnification size compared with the bytes of input data
1250 text.  If the value is zero, the CCL program can't execute `read' and
1251 `write' commands.
1252
1253 CCL_MAIN_CODE and CCL_EOF_CODE are CCL program codes.  CCL_MAIN_CODE
1254 executed at first.  If there's no more input data when `read' command
1255 is executed in CCL_MAIN_CODE, CCL_EOF_CODE is executed.  If
1256 CCL_MAIN_CODE is terminated, CCL_EOF_CODE is not executed.
1257
1258 Here's the syntax of CCL program code in BNF notation.  The lines
1259 starting by two semicolons (and optional leading spaces) describe the
1260 semantics.
1261
1262 CCL_MAIN_CODE := CCL_BLOCK
1263
1264 CCL_EOF_CODE := CCL_BLOCK
1265
1266 CCL_BLOCK := STATEMENT | (STATEMENT [STATEMENT ...])
1267
1268 STATEMENT :=
1269         SET | IF | BRANCH | LOOP | REPEAT | BREAK | READ | WRITE | CALL
1270         | TRANSLATE | END
1271
1272 SET :=  (REG = EXPRESSION)
1273         | (REG ASSIGNMENT_OPERATOR EXPRESSION)
1274         ;; The following form is the same as (r0 = integer).
1275         | integer
1276
1277 EXPRESSION := ARG | (EXPRESSION OPERATOR ARG)
1278
1279 ;; Evaluate EXPRESSION.  If the result is nonzeor, execute
1280 ;; CCL_BLOCK_0.  Otherwise, execute CCL_BLOCK_1.
1281 IF :=   (if EXPRESSION CCL_BLOCK_0 CCL_BLOCK_1)
1282
1283 ;; Evaluate EXPRESSION.  Provided that the result is N, execute
1284 ;; CCL_BLOCK_N.
1285 BRANCH := (branch EXPRESSION CCL_BLOCK_0 [CCL_BLOCK_1 ...])
1286
1287 ;; Execute STATEMENTs until (break) or (end) is executed.
1288 LOOP := (loop STATEMENT [STATEMENT ...])
1289
1290 ;; Terminate the most inner loop.
1291 BREAK := (break)
1292
1293 REPEAT :=
1294         ;; Jump to the head of the most inner loop.
1295         (repeat)
1296         ;; Same as: ((write [REG | integer | string])
1297         ;;           (repeat))
1298         | (write-repeat [REG | integer | string])
1299         ;; Same as: ((write REG [ARRAY])
1300         ;;           (read REG)
1301         ;;           (repeat))
1302         | (write-read-repeat REG [ARRAY])
1303         ;; Same as: ((write integer)
1304         ;;           (read REG)
1305         ;;           (repeat))
1306         | (write-read-repeat REG integer)
1307
1308 READ := ;; Set REG_0 to a byte read from the input text, set REG_1
1309         ;; to the next byte read, and so on.
1310         (read REG_0 [REG_1 ...])
1311         ;; Same as: ((read REG)
1312         ;;           (if (REG OPERATOR ARG) CCL_BLOCK_0 CCL_BLOCK_1))
1313         | (read-if (REG OPERATOR ARG) CCL_BLOCK_0 CCL_BLOCK_1)
1314         ;; Same as: ((read REG)
1315         ;;           (branch REG CCL_BLOCK_0 [CCL_BLOCK_1 ...]))
1316         | (read-branch REG CCL_BLOCK_0 [CCL_BLOCK_1 ...])
1317         ;; Read a character from the input text while parsing
1318         ;; multibyte representation, set REG_0 to the charset ID of
1319         ;; the character, set REG_1 to the code point of the
1320         ;; character.  If the dimension of charset is two, set REG_1
1321         ;; to ((CODE0 << 8) | CODE1), where CODE0 is the first code
1322         ;; point and CODE1 is the second code point.
1323         | (read-multibyte-character REG_0 REG_1)
1324
1325 WRITE :=
1326         ;; Write REG_0, REG_1, ... to the output buffer.  If REG_N is
1327         ;; a multibyte character, write the corresponding multibyte
1328         ;; representation.
1329         (write REG_0 [REG_1 ...])
1330         ;; Same as: ((r7 = EXPRESSION)
1331         ;;           (write r7))
1332         | (write EXPRESSION)
1333         ;; Write the value of `integer' to the output buffer.  If it
1334         ;; is a multibyte character, write the corresponding multibyte
1335         ;; representation.
1336         | (write integer)
1337         ;; Write the byte sequence of `string' as is to the output
1338         ;; buffer.  It is encoded by binary coding system, thus,
1339         ;; by this operation, you cannot write multibyte string
1340         ;; as it is.
1341         | (write string)
1342         ;; Same as: (write string)
1343         | string
1344         ;; Provided that the value of REG is N, write Nth element of
1345         ;; ARRAY to the output buffer.  If it is a multibyte
1346         ;; character, write the corresponding multibyte
1347         ;; representation.
1348         | (write REG ARRAY)
1349         ;; Write a multibyte representation of a character whose
1350         ;; charset ID is REG_0 and code point is REG_1.  If the
1351         ;; dimension of the charset is two, REG_1 should be ((CODE0 <<
1352         ;; 8) | CODE1), where CODE0 is the first code point and CODE1
1353         ;; is the second code point of the character.
1354         | (write-multibyte-character REG_0 REG_1)
1355
1356 ;; Call CCL program whose name is ccl-program-name.
1357 CALL := (call ccl-program-name)
1358
1359 ;; Terminate the CCL program.
1360 END := (end)
1361
1362 ;; CCL registers that can contain any integer value.  As r7 is also
1363 ;; used by CCL interpreter, its value is changed unexpectedly.
1364 REG := r0 | r1 | r2 | r3 | r4 | r5 | r6 | r7
1365
1366 ARG := REG | integer
1367
1368 OPERATOR :=
1369         ;; Normal arithmethic operators (same meaning as C code).
1370         + | - | * | / | %
1371
1372         ;; Bitwize operators (same meaning as C code)
1373         | & | `|' | ^
1374
1375         ;; Shifting operators (same meaning as C code)
1376         | << | >>
1377
1378         ;; (REG = ARG_0 <8 ARG_1) means:
1379         ;;      (REG = ((ARG_0 << 8) | ARG_1))
1380         | <8
1381
1382         ;; (REG = ARG_0 >8 ARG_1) means:
1383         ;;      ((REG = (ARG_0 >> 8))
1384         ;;       (r7 = (ARG_0 & 255)))
1385         | >8
1386
1387         ;; (REG = ARG_0 // ARG_1) means:
1388         ;;      ((REG = (ARG_0 / ARG_1))
1389         ;;       (r7 = (ARG_0 % ARG_1)))
1390         | //
1391
1392         ;; Normal comparing operators (same meaning as C code)
1393         | < | > | == | <= | >= | !=
1394
1395         ;; If ARG_0 and ARG_1 are higher and lower byte of Shift-JIS
1396         ;; code, and CHAR is the corresponding JISX0208 character,
1397         ;; (REG = ARG_0 de-sjis ARG_1) means:
1398         ;;      ((REG = CODE0)
1399         ;;       (r7 = CODE1))
1400         ;; where CODE0 is the first code point of CHAR, CODE1 is the
1401         ;; second code point of CHAR.
1402         | de-sjis
1403
1404         ;; If ARG_0 and ARG_1 are the first and second code point of
1405         ;; JISX0208 character CHAR, and SJIS is the correponding
1406         ;; Shift-JIS code,
1407         ;; (REG = ARG_0 en-sjis ARG_1) means:
1408         ;;      ((REG = HIGH)
1409         ;;       (r7 = LOW))
1410         ;; where HIGH is the higher byte of SJIS, LOW is the lower
1411         ;; byte of SJIS.
1412         | en-sjis
1413
1414 ASSIGNMENT_OPERATOR :=
1415         ;; Same meaning as C code
1416         += | -= | *= | /= | %= | &= | `|=' | ^= | <<= | >>=
1417
1418         ;; (REG <8= ARG) is the same as:
1419         ;;      ((REG <<= 8)
1420         ;;       (REG |= ARG))
1421         | <8= 
1422
1423         ;; (REG >8= ARG) is the same as:
1424         ;;      ((r7 = (REG & 255))
1425         ;;       (REG >>= 8))
1426
1427         ;; (REG //= ARG) is the same as:
1428         ;;      ((r7 = (REG % ARG))
1429         ;;       (REG /= ARG))
1430         | //=
1431
1432 ARRAY := `[' integer ... `]'
1433
1434
1435 TRANSLATE :=
1436         (translate-character REG(table) REG(charset) REG(codepoint))
1437         | (translate-character SYMBOL REG(charset) REG(codepoint))
1438 MAP :=
1439      (iterate-multiple-map REG REG MAP-IDs)
1440      | (map-multiple REG REG (MAP-SET))
1441      | (map-single REG REG MAP-ID)
1442 MAP-IDs := MAP-ID ...
1443 MAP-SET := MAP-IDs | (MAP-IDs) MAP-SET
1444 MAP-ID := integer
1445 "
1446   `(let ((prog ,(ccl-compile (eval ccl-program))))
1447      (defconst ,name prog ,doc)
1448      (put ',name 'ccl-program-idx (register-ccl-program ',name prog))
1449      nil))
1450
1451 ;;;###autoload
1452 (defmacro check-ccl-program (ccl-program &optional name)
1453   "Check validity of CCL-PROGRAM.
1454 If CCL-PROGRAM is a symbol denoting a CCL program, return
1455 CCL-PROGRAM, else return nil.
1456 If CCL-PROGRAM is a vector and optional arg NAME (symbol) is supplied,
1457 register CCL-PROGRAM by name NAME, and return NAME."
1458   `(if (ccl-program-p ,ccl-program)
1459        (if (vectorp ,ccl-program)
1460            (progn
1461              (register-ccl-program ,name ,ccl-program)
1462              ,name)
1463          ,ccl-program)))
1464
1465 ;;;###autoload
1466 (defun ccl-execute-with-args (ccl-prog &rest args)
1467   "Execute CCL-PROGRAM with registers initialized by the remaining args.
1468 The return value is a vector of resulting CCL registers.
1469
1470 See the documentation of `define-ccl-program' for the detail of CCL program."
1471   (let ((reg (make-vector 8 0))
1472         (i 0))
1473     (while (and args (< i 8))
1474       (if (not (integerp (car args)))
1475           (error "Arguments should be integer"))
1476       (aset reg i (car args))
1477       (setq args (cdr args) i (1+ i)))
1478     (ccl-execute ccl-prog reg)
1479     reg))
1480
1481 (provide 'ccl)
1482
1483 ;; ccl.el ends here