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