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