1 ;;; ccl.el --- CCL (Code Conversion Language) compiler
3 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
5 ;; Copyright (C) 2002 Free Software Foundation, Inc.
7 ;; Keywords: CCL, mule, multilingual, character set, coding-system
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; CCL (Code Conversion Language) is a simple programming language to
29 ;; be used for various kind of code conversion. A CCL program is
30 ;; compiled to CCL code (vector of integers) and executed by the CCL
31 ;; interpreter in Emacs.
33 ;; CCL is used for code conversion at process I/O and file I/O for
34 ;; non-standard coding-systems. In addition, it is used for
35 ;; calculating code points of X fonts from character codes.
36 ;; However, since CCL is designed as a powerful programming language,
37 ;; it can be used for more generic calculation. For instance,
38 ;; combination of three or more arithmetic operations can be
39 ;; calculated faster than in Emacs Lisp.
41 ;; The syntax and semantics of CCL programs are described in the
42 ;; documentation of `define-ccl-program'.
47 "CCL (Code Conversion Language) compiler."
51 (defconst ccl-command-table
52 [if branch loop break repeat write-repeat write-read-repeat
53 read read-if read-branch write call end
54 read-multibyte-character write-multibyte-character
56 iterate-multiple-map map-multiple map-single lookup-integer
58 "Vector of CCL commands (symbols).")
60 ;; Put a property to each symbol of CCL commands for the compiler.
61 (let (op (i 0) (len (length ccl-command-table
)))
63 (setq op
(aref ccl-command-table i
))
64 (put op
'ccl-compile-function
(intern (format "ccl-compile-%s" op
)))
67 (defconst ccl-code-table
75 write-register-read-jump
92 set-assign-expr-register
96 jump-cond-expr-register
97 read-jump-cond-expr-const
98 read-jump-cond-expr-register
101 "Vector of CCL compiled codes (symbols).")
103 (defconst ccl-extended-code-table
104 [read-multibyte-character
105 write-multibyte-character
107 translate-character-const-tbl
108 nil nil nil nil nil nil nil nil nil nil nil nil
; 0x04-0x0f
113 lookup-char-const-tbl
115 "Vector of CCL extended compiled codes (symbols).")
117 ;; Put a property to each symbol of CCL codes for the disassembler.
118 (let (code (i 0) (len (length ccl-code-table
)))
120 (setq code
(aref ccl-code-table i
))
121 (put code
'ccl-code i
)
122 (put code
'ccl-dump-function
(intern (format "ccl-dump-%s" code
)))
125 (let (code (i 0) (len (length ccl-extended-code-table
)))
127 (setq code
(aref ccl-extended-code-table i
))
130 (put code
'ccl-ex-code i
)
131 (put code
'ccl-dump-function
(intern (format "ccl-dump-%s" code
)))))
134 (defconst ccl-jump-code-list
135 '(jump jump-cond write-register-jump write-register-read-jump
136 write-const-jump write-const-read-jump write-string-jump
137 write-array-read-jump read-jump
))
139 ;; Put a property `jump-flag' to each CCL code which execute jump in
141 (let ((l ccl-jump-code-list
))
143 (put (car l
) 'jump-flag t
)
146 (defconst ccl-register-table
147 [r0 r1 r2 r3 r4 r5 r6 r7
]
148 "Vector of CCL registers (symbols).")
150 ;; Put a property to indicate register number to each symbol of CCL.
152 (let (reg (i 0) (len (length ccl-register-table
)))
154 (setq reg
(aref ccl-register-table i
))
155 (put reg
'ccl-register-number i
)
158 (defconst ccl-arith-table
159 [+ -
* / %
& | ^
<< >> <8 >8 // nil nil nil
160 < > == <= >= != de-sjis en-sjis
]
161 "Vector of CCL arithmetic/logical operators (symbols).")
163 ;; Put a property to each symbol of CCL operators for the compiler.
164 (let (arith (i 0) (len (length ccl-arith-table
)))
166 (setq arith
(aref ccl-arith-table i
))
167 (if arith
(put arith
'ccl-arith-code i
))
170 (defconst ccl-assign-arith-table
171 [+= -
= *= /= %
= &= |
= ^
= <<= >>= <8= >8= //=]
172 "Vector of CCL assignment operators (symbols).")
174 ;; Put a property to each symbol of CCL assignment operators for the compiler.
175 (let (arith (i 0) (len (length ccl-assign-arith-table
)))
177 (setq arith
(aref ccl-assign-arith-table i
))
178 (put arith
'ccl-self-arith-code i
)
181 (defvar ccl-program-vector nil
182 "Working vector of CCL codes produced by CCL compiler.")
183 (defvar ccl-current-ic
0
184 "The current index for `ccl-program-vector'.")
186 ;; Embed integer DATA in `ccl-program-vector' at `ccl-current-ic' and
187 ;; increment it. If IC is specified, embed DATA at IC.
188 (defun ccl-embed-data (data &optional ic
)
190 (aset ccl-program-vector ic data
)
191 (let ((len (length ccl-program-vector
)))
192 (if (>= ccl-current-ic len
)
193 (let ((new (make-vector (* len
2) nil
)))
196 (aset new len
(aref ccl-program-vector len
)))
197 (setq ccl-program-vector new
))))
198 (aset ccl-program-vector ccl-current-ic data
)
199 (setq ccl-current-ic
(1+ ccl-current-ic
))))
201 ;; Embed pair of SYMBOL and PROP where (get SYMBOL PROP) should give
202 ;; proper index number for SYMBOL. PROP should be
203 ;; `translation-table-id', `translation-hash-table-id'
204 ;; `code-conversion-map-id', or `ccl-program-idx'.
205 (defun ccl-embed-symbol (symbol prop
)
206 (ccl-embed-data (cons symbol prop
)))
208 ;; Embed string STR of length LEN in `ccl-program-vector' at
210 (defun ccl-embed-string (len str
)
213 (ccl-embed-data (logior (ash (aref str i
) 16)
215 (ash (aref str
(1+ i
)) 8)
222 ;; Embed a relative jump address to `ccl-current-ic' in
223 ;; `ccl-program-vector' at IC without altering the other bit field.
224 (defun ccl-embed-current-address (ic)
225 (let ((relative (- ccl-current-ic
(1+ ic
))))
226 (aset ccl-program-vector ic
227 (logior (aref ccl-program-vector ic
) (ash relative
8)))))
229 ;; Embed CCL code for the operation OP and arguments REG and DATA in
230 ;; `ccl-program-vector' at `ccl-current-ic' in the following format.
231 ;; |----------------- integer (28-bit) ------------------|
232 ;; |------------ 20-bit ------------|- 3-bit --|- 5-bit -|
233 ;; |------------- DATA -------------|-- REG ---|-- OP ---|
234 ;; If REG2 is specified, embed a code in the following format.
235 ;; |------- 17-bit ------|- 3-bit --|- 3-bit --|- 5-bit -|
236 ;; |-------- DATA -------|-- REG2 --|-- REG ---|-- OP ---|
238 ;; If REG is a CCL register symbol (e.g. r0, r1...), the register
239 ;; number is embedded. If OP is one of unconditional jumps, DATA is
240 ;; changed to a relative jump address.
242 (defun ccl-embed-code (op reg data
&optional reg2
)
243 (if (and (> data
0) (get op
'jump-flag
))
244 ;; DATA is an absolute jump address. Make it relative to the
245 ;; next of jump code.
246 (setq data
(- data
(1+ ccl-current-ic
))))
247 (let ((code (logior (get op
'ccl-code
)
249 (if (symbolp reg
) (get reg
'ccl-register-number
) reg
) 5)
251 (logior (ash (get reg2
'ccl-register-number
) 8)
254 (ccl-embed-data code
)))
256 ;; extended ccl command format
257 ;; |- 14-bit -|- 3-bit --|- 3-bit --|- 3-bit --|- 5-bit -|
258 ;; |- EX-OP --|-- REG3 --|-- REG2 --|-- REG ---|-- OP ---|
259 (defun ccl-embed-extended-command (ex-op reg reg2 reg3
)
260 (let ((data (logior (ash (get ex-op
'ccl-ex-code
) 3)
262 (get reg3
'ccl-register-number
)
264 (ccl-embed-code 'ex-cmd reg data reg2
)))
266 ;; Just advance `ccl-current-ic' by INC.
267 (defun ccl-increment-ic (inc)
268 (setq ccl-current-ic
(+ ccl-current-ic inc
)))
270 ;; If non-nil, index of the start of the current loop.
271 (defvar ccl-loop-head nil
)
272 ;; If non-nil, list of absolute addresses of the breaking points of
274 (defvar ccl-breaks nil
)
277 (defun ccl-compile (ccl-program)
278 "Return the compiled code of CCL-PROGRAM as a vector of integers."
279 (if (or (null (consp ccl-program
))
280 (null (integerp (car ccl-program
)))
281 (null (listp (car (cdr ccl-program
)))))
282 (error "CCL: Invalid CCL program: %s" ccl-program
))
283 (if (null (vectorp ccl-program-vector
))
284 (setq ccl-program-vector
(make-vector 8192 0)))
285 (setq ccl-loop-head nil ccl-breaks nil
)
286 (setq ccl-current-ic
0)
288 ;; The first element is the buffer magnification.
289 (ccl-embed-data (car ccl-program
))
291 ;; The second element is the address of the start CCL code for
292 ;; processing end of input buffer (we call it eof-processor). We
296 ;; Compile the main body of the CCL program.
297 (ccl-compile-1 (car (cdr ccl-program
)))
299 ;; Embed the address of eof-processor.
300 (ccl-embed-data ccl-current-ic
1)
302 ;; Then compile eof-processor.
303 (if (nth 2 ccl-program
)
304 (ccl-compile-1 (nth 2 ccl-program
)))
306 ;; At last, embed termination code.
307 (ccl-embed-code 'end
0 0)
309 (let ((vec (make-vector ccl-current-ic
0))
311 (while (< i ccl-current-ic
)
312 (aset vec i
(aref ccl-program-vector i
))
316 ;; Signal syntax error.
317 (defun ccl-syntax-error (cmd)
318 (error "CCL: Syntax error: %s" cmd
))
320 ;; Check if ARG is a valid CCL register.
321 (defun ccl-check-register (arg cmd
)
322 (if (get arg
'ccl-register-number
)
324 (error "CCL: Invalid register %s in %s" arg cmd
)))
326 ;; Check if ARG is a valid CCL command.
327 (defun ccl-check-compile-function (arg cmd
)
328 (or (get arg
'ccl-compile-function
)
329 (error "CCL: Invalid command: %s" cmd
)))
331 ;; In the following code, most ccl-compile-XXXX functions return t if
332 ;; they end with unconditional jump, else return nil.
334 ;; Compile CCL-BLOCK (see the syntax above).
335 (defun ccl-compile-1 (ccl-block)
336 (let (unconditional-jump
338 (if (or (integerp ccl-block
)
340 (and ccl-block
(symbolp (car ccl-block
))))
341 ;; This block consists of single statement.
342 (setq ccl-block
(list ccl-block
)))
344 ;; Now CCL-BLOCK is a list of statements. Compile them one by
347 (setq cmd
(car ccl-block
))
348 (setq unconditional-jump
349 (cond ((integerp cmd
)
350 ;; SET statement for the register 0.
351 (ccl-compile-set (list 'r0
'= cmd
)))
354 ;; WRITE statement of string argument.
355 (ccl-compile-write-string cmd
))
358 ;; The other statements.
359 (cond ((eq (nth 1 cmd
) '=)
360 ;; SET statement of the form `(REG = EXPRESSION)'.
361 (ccl-compile-set cmd
))
363 ((and (symbolp (nth 1 cmd
))
364 (get (nth 1 cmd
) 'ccl-self-arith-code
))
365 ;; SET statement with an assignment operation.
366 (ccl-compile-self-set cmd
))
369 (funcall (ccl-check-compile-function (car cmd
) cmd
)
373 (ccl-syntax-error cmd
))))
374 (setq ccl-block
(cdr ccl-block
)))
377 (defconst ccl-max-short-const
(ash 1 19))
378 (defconst ccl-min-short-const
(ash -
1 19))
380 ;; Compile SET statement.
381 (defun ccl-compile-set (cmd)
382 (let ((rrr (ccl-check-register (car cmd
) cmd
))
385 ;; CMD has the form `(RRR = (XXX OP YYY))'.
386 (ccl-compile-expression rrr right
))
389 ;; CMD has the form `(RRR = integer)'.
390 (if (and (<= right ccl-max-short-const
)
391 (>= right ccl-min-short-const
))
392 (ccl-embed-code 'set-short-const rrr right
)
393 (ccl-embed-code 'set-const rrr
0)
394 (ccl-embed-data right
)))
397 ;; CMD has the form `(RRR = rrr [ array ])'.
398 (ccl-check-register right cmd
)
399 (let ((ary (nth 3 cmd
)))
401 (let ((i 0) (len (length ary
)))
402 (ccl-embed-code 'set-array rrr len right
)
404 (ccl-embed-data (aref ary i
))
406 (ccl-embed-code 'set-register rrr
0 right
))))))
409 ;; Compile SET statement with ASSIGNMENT_OPERATOR.
410 (defun ccl-compile-self-set (cmd)
411 (let ((rrr (ccl-check-register (car cmd
) cmd
))
414 ;; CMD has the form `(RRR ASSIGN_OP (XXX OP YYY))', compile
415 ;; the right hand part as `(r7 = (XXX OP YYY))' (note: the
416 ;; register 7 can be used for storing temporary value).
418 (ccl-compile-expression 'r7 right
)
420 ;; Now CMD has the form `(RRR ASSIGN_OP ARG)'. Compile it as
421 ;; `(RRR = (RRR OP ARG))'.
422 (ccl-compile-expression
424 (list rrr
(intern (substring (symbol-name (nth 1 cmd
)) 0 -
1)) right
)))
427 ;; Compile SET statement of the form `(RRR = EXPR)'.
428 (defun ccl-compile-expression (rrr expr
)
429 (let ((left (car expr
))
430 (op (get (nth 1 expr
) 'ccl-arith-code
))
431 (right (nth 2 expr
)))
434 ;; EXPR has the form `((EXPR2 OP2 ARG) OP RIGHT)'. Compile
435 ;; the first term as `(r7 = (EXPR2 OP2 ARG)).'
436 (ccl-compile-expression 'r7 left
)
439 ;; Now EXPR has the form (LEFT OP RIGHT).
440 (if (and (eq rrr left
)
441 (< op
(length ccl-assign-arith-table
)))
442 ;; Compile this SET statement as `(RRR OP= RIGHT)'.
445 (ccl-embed-code 'set-assign-expr-const rrr
(ash op
3) 'r0
)
446 (ccl-embed-data right
))
447 (ccl-check-register right expr
)
448 (ccl-embed-code 'set-assign-expr-register rrr
(ash op
3) right
))
450 ;; Compile this SET statement as `(RRR = (LEFT OP RIGHT))'.
453 (ccl-embed-code 'set-expr-const rrr
(ash op
3) left
)
454 (ccl-embed-data right
))
455 (ccl-check-register right expr
)
456 (ccl-embed-code 'set-expr-register
458 (logior (ash op
3) (get right
'ccl-register-number
))
461 ;; Compile WRITE statement with string argument.
462 (defun ccl-compile-write-string (str)
463 (setq str
(string-as-unibyte str
))
464 (let ((len (length str
)))
465 (ccl-embed-code 'write-const-string
1 len
)
466 (ccl-embed-string len str
))
469 ;; Compile IF statement of the form `(if CONDITION TRUE-PART FALSE-PART)'.
470 ;; If READ-FLAG is non-nil, this statement has the form
471 ;; `(read-if (REG OPERATOR ARG) TRUE-PART FALSE-PART)'.
472 (defun ccl-compile-if (cmd &optional read-flag
)
473 (if (and (/= (length cmd
) 3) (/= (length cmd
) 4))
474 (error "CCL: Invalid number of arguments: %s" cmd
))
475 (let ((condition (nth 1 cmd
))
476 (true-cmds (nth 2 cmd
))
477 (false-cmds (nth 3 cmd
))
480 (if (and (listp condition
)
481 (listp (car condition
)))
482 ;; If CONDITION is a nested expression, the inner expression
483 ;; should be compiled at first as SET statement, i.e.:
484 ;; `(if ((X OP2 Y) OP Z) ...)' is compiled into two statements:
485 ;; `(r7 = (X OP2 Y)) (if (r7 OP Z) ...)'.
487 (ccl-compile-expression 'r7
(car condition
))
488 (setq condition
(cons 'r7
(cdr condition
)))
489 (setq cmd
(cons (car cmd
)
490 (cons condition
(cdr (cdr cmd
)))))))
492 (setq jump-cond-address ccl-current-ic
)
493 ;; Compile CONDITION.
494 (if (symbolp condition
)
495 ;; CONDITION is a register.
497 (ccl-check-register condition cmd
)
498 (ccl-embed-code 'jump-cond condition
0))
499 ;; CONDITION is a simple expression of the form (RRR OP ARG).
500 (let ((rrr (car condition
))
501 (op (get (nth 1 condition
) 'ccl-arith-code
))
502 (arg (nth 2 condition
)))
503 (ccl-check-register rrr cmd
)
506 (ccl-embed-code (if read-flag
'read-jump-cond-expr-const
507 'jump-cond-expr-const
)
510 (ccl-embed-data arg
))
511 (ccl-check-register arg cmd
)
512 (ccl-embed-code (if read-flag
'read-jump-cond-expr-register
513 'jump-cond-expr-register
)
516 (ccl-embed-data (get arg
'ccl-register-number
)))))
518 ;; Compile TRUE-PART.
519 (let ((unconditional-jump (ccl-compile-1 true-cmds
)))
520 (if (null false-cmds
)
521 ;; This is the place to jump to if condition is false.
523 (ccl-embed-current-address jump-cond-address
)
524 (setq unconditional-jump nil
))
525 (let (end-true-part-address)
526 (if (not unconditional-jump
)
528 ;; If TRUE-PART does not end with unconditional jump, we
529 ;; have to jump to the end of FALSE-PART from here.
530 (setq end-true-part-address ccl-current-ic
)
531 (ccl-embed-code 'jump
0 0)))
532 ;; This is the place to jump to if CONDITION is false.
533 (ccl-embed-current-address jump-cond-address
)
534 ;; Compile FALSE-PART.
535 (setq unconditional-jump
536 (and (ccl-compile-1 false-cmds
) unconditional-jump
))
537 (if end-true-part-address
538 ;; This is the place to jump to after the end of TRUE-PART.
539 (ccl-embed-current-address end-true-part-address
))))
540 unconditional-jump
)))
542 ;; Compile BRANCH statement.
543 (defun ccl-compile-branch (cmd)
544 (if (< (length cmd
) 3)
545 (error "CCL: Invalid number of arguments: %s" cmd
))
546 (ccl-compile-branch-blocks 'branch
547 (ccl-compile-branch-expression (nth 1 cmd
) cmd
)
550 ;; Compile READ statement of the form `(read-branch EXPR BLOCK0 BLOCK1 ...)'.
551 (defun ccl-compile-read-branch (cmd)
552 (if (< (length cmd
) 3)
553 (error "CCL: Invalid number of arguments: %s" cmd
))
554 (ccl-compile-branch-blocks 'read-branch
555 (ccl-compile-branch-expression (nth 1 cmd
) cmd
)
558 ;; Compile EXPRESSION part of BRANCH statement and return register
559 ;; which holds a value of the expression.
560 (defun ccl-compile-branch-expression (expr cmd
)
562 ;; EXPR has the form `(EXPR2 OP ARG)'. Compile it as SET
563 ;; statement of the form `(r7 = (EXPR2 OP ARG))'.
565 (ccl-compile-expression 'r7 expr
)
567 (ccl-check-register expr cmd
)))
569 ;; Compile BLOCKs of BRANCH statement. CODE is 'branch or 'read-branch.
570 ;; REG is a register which holds a value of EXPRESSION part. BLOCKs
571 ;; is a list of CCL-BLOCKs.
572 (defun ccl-compile-branch-blocks (code rrr blocks
)
573 (let ((branches (length blocks
))
575 jump-table-head-address
578 block-unconditional-jump
)
579 (ccl-embed-code code rrr branches
)
580 (setq jump-table-head-address ccl-current-ic
)
581 ;; The size of jump table is the number of blocks plus 1 (for the
582 ;; case RRR is out of range).
583 (ccl-increment-ic (1+ branches
))
584 (setq empty-block-indexes
(list branches
))
585 ;; Compile each block.
588 (if (null (car blocks
))
589 ;; This block is empty.
590 (setq empty-block-indexes
(cons branch-idx empty-block-indexes
)
591 block-unconditional-jump t
)
592 ;; This block is not empty.
593 (ccl-embed-data (- ccl-current-ic jump-table-head-address
)
594 (+ jump-table-head-address branch-idx
))
595 (setq block-unconditional-jump
(ccl-compile-1 (car blocks
)))
596 (if (not block-unconditional-jump
)
598 ;; Jump address of the end of branches are embedded later.
599 ;; For the moment, just remember where to embed them.
600 (setq block-tail-addresses
601 (cons ccl-current-ic block-tail-addresses
))
602 (ccl-embed-code 'jump
0 0))))
603 (setq branch-idx
(1+ branch-idx
))
604 (setq blocks
(cdr blocks
)))
605 (if (not block-unconditional-jump
)
606 ;; We don't need jump code at the end of the last block.
607 (setq block-tail-addresses
(cdr block-tail-addresses
)
608 ccl-current-ic
(1- ccl-current-ic
)))
609 ;; Embed jump address at the tailing jump commands of blocks.
610 (while block-tail-addresses
611 (ccl-embed-current-address (car block-tail-addresses
))
612 (setq block-tail-addresses
(cdr block-tail-addresses
)))
613 ;; For empty blocks, make entries in the jump table point directly here.
614 (while empty-block-indexes
615 (ccl-embed-data (- ccl-current-ic jump-table-head-address
)
616 (+ jump-table-head-address
(car empty-block-indexes
)))
617 (setq empty-block-indexes
(cdr empty-block-indexes
))))
618 ;; Branch command ends by unconditional jump if RRR is out of range.
621 ;; Compile LOOP statement.
622 (defun ccl-compile-loop (cmd)
623 (if (< (length cmd
) 2)
624 (error "CCL: Invalid number of arguments: %s" cmd
))
625 (let* ((ccl-loop-head ccl-current-ic
)
631 (setq unconditional-jump t
)
633 (setq unconditional-jump
634 (and (ccl-compile-1 (car cmd
)) unconditional-jump
))
635 (setq cmd
(cdr cmd
)))
638 ;; Embed jump address for break statements encountered in
641 (ccl-embed-current-address (car ccl-breaks
))
642 (setq ccl-breaks
(cdr ccl-breaks
))))
645 ;; Compile BREAK statement.
646 (defun ccl-compile-break (cmd)
647 (if (/= (length cmd
) 1)
648 (error "CCL: Invalid number of arguments: %s" cmd
))
649 (if (null ccl-loop-head
)
650 (error "CCL: No outer loop: %s" cmd
))
651 (setq ccl-breaks
(cons ccl-current-ic ccl-breaks
))
652 (ccl-embed-code 'jump
0 0)
655 ;; Compile REPEAT statement.
656 (defun ccl-compile-repeat (cmd)
657 (if (/= (length cmd
) 1)
658 (error "CCL: Invalid number of arguments: %s" cmd
))
659 (if (null ccl-loop-head
)
660 (error "CCL: No outer loop: %s" cmd
))
661 (ccl-embed-code 'jump
0 ccl-loop-head
)
664 ;; Compile WRITE-REPEAT statement.
665 (defun ccl-compile-write-repeat (cmd)
666 (if (/= (length cmd
) 2)
667 (error "CCL: Invalid number of arguments: %s" cmd
))
668 (if (null ccl-loop-head
)
669 (error "CCL: No outer loop: %s" cmd
))
670 (let ((arg (nth 1 cmd
)))
671 (cond ((integerp arg
)
672 (ccl-embed-code 'write-const-jump
0 ccl-loop-head
)
673 (ccl-embed-data arg
))
675 (setq arg
(string-as-unibyte arg
))
676 (let ((len (length arg
))
678 (ccl-embed-code 'write-string-jump
0 ccl-loop-head
)
680 (ccl-embed-string len arg
)))
682 (ccl-check-register arg cmd
)
683 (ccl-embed-code 'write-register-jump arg ccl-loop-head
))))
686 ;; Compile WRITE-READ-REPEAT statement.
687 (defun ccl-compile-write-read-repeat (cmd)
688 (if (or (< (length cmd
) 2) (> (length cmd
) 3))
689 (error "CCL: Invalid number of arguments: %s" cmd
))
690 (if (null ccl-loop-head
)
691 (error "CCL: No outer loop: %s" cmd
))
692 (let ((rrr (ccl-check-register (nth 1 cmd
) cmd
))
695 (ccl-embed-code 'write-register-read-jump rrr ccl-loop-head
))
697 (ccl-embed-code 'write-const-read-jump rrr arg ccl-loop-head
))
699 (let ((len (length arg
))
701 (ccl-embed-code 'write-array-read-jump rrr ccl-loop-head
)
704 (ccl-embed-data (aref arg i
))
707 (error "CCL: Invalid argument %s: %s" arg cmd
)))
708 (ccl-embed-code 'read-jump rrr ccl-loop-head
))
711 ;; Compile READ statement.
712 (defun ccl-compile-read (cmd)
713 (if (< (length cmd
) 2)
714 (error "CCL: Invalid number of arguments: %s" cmd
))
715 (let* ((args (cdr cmd
))
716 (i (1- (length args
))))
718 (let ((rrr (ccl-check-register (car args
) cmd
)))
719 (ccl-embed-code 'read-register rrr i
)
720 (setq args
(cdr args
) i
(1- i
)))))
723 ;; Compile READ-IF statement.
724 (defun ccl-compile-read-if (cmd)
725 (ccl-compile-if cmd
'read
))
727 ;; Compile WRITE statement.
728 (defun ccl-compile-write (cmd)
729 (if (< (length cmd
) 2)
730 (error "CCL: Invalid number of arguments: %s" cmd
))
731 (let ((rrr (nth 1 cmd
)))
732 (cond ((integerp rrr
)
733 (ccl-embed-code 'write-const-string
0 rrr
))
735 (ccl-compile-write-string rrr
))
736 ((and (symbolp rrr
) (vectorp (nth 2 cmd
)))
737 (ccl-check-register rrr cmd
)
738 ;; CMD has the form `(write REG ARRAY)'.
739 (let* ((arg (nth 2 cmd
))
742 (ccl-embed-code 'write-array rrr len
)
744 (if (not (integerp (aref arg i
)))
745 (error "CCL: Invalid argument %s: %s" arg cmd
))
746 (ccl-embed-data (aref arg i
))
750 ;; CMD has the form `(write REG ...)'.
751 (let* ((args (cdr cmd
))
752 (i (1- (length args
))))
754 (setq rrr
(ccl-check-register (car args
) cmd
))
755 (ccl-embed-code 'write-register rrr i
)
756 (setq args
(cdr args
) i
(1- i
)))))
759 ;; CMD has the form `(write (LEFT OP RIGHT))'.
760 (let ((left (car rrr
))
761 (op (get (nth 1 rrr
) 'ccl-arith-code
))
765 ;; RRR has the form `((EXPR OP2 ARG) OP RIGHT)'.
766 ;; Compile the first term as `(r7 = (EXPR OP2 ARG))'.
767 (ccl-compile-expression 'r7 left
)
769 ;; Now RRR has the form `(ARG OP RIGHT)'.
772 (ccl-embed-code 'write-expr-const
0 (ash op
3) left
)
773 (ccl-embed-data right
))
774 (ccl-check-register right rrr
)
775 (ccl-embed-code 'write-expr-register
0
777 (get right
'ccl-register-number
))))))
780 (error "CCL: Invalid argument: %s" cmd
))))
783 ;; Compile CALL statement.
784 (defun ccl-compile-call (cmd)
785 (if (/= (length cmd
) 2)
786 (error "CCL: Invalid number of arguments: %s" cmd
))
787 (if (not (symbolp (nth 1 cmd
)))
788 (error "CCL: Subroutine should be a symbol: %s" cmd
))
789 (ccl-embed-code 'call
1 0)
790 (ccl-embed-symbol (nth 1 cmd
) 'ccl-program-idx
)
793 ;; Compile END statement.
794 (defun ccl-compile-end (cmd)
795 (if (/= (length cmd
) 1)
796 (error "CCL: Invalid number of arguments: %s" cmd
))
797 (ccl-embed-code 'end
0 0)
800 ;; Compile read-multibyte-character
801 (defun ccl-compile-read-multibyte-character (cmd)
802 (if (/= (length cmd
) 3)
803 (error "CCL: Invalid number of arguments: %s" cmd
))
804 (let ((RRR (nth 1 cmd
))
806 (ccl-check-register rrr cmd
)
807 (ccl-check-register RRR cmd
)
808 (ccl-embed-extended-command 'read-multibyte-character rrr RRR
0))
811 ;; Compile write-multibyte-character
812 (defun ccl-compile-write-multibyte-character (cmd)
813 (if (/= (length cmd
) 3)
814 (error "CCL: Invalid number of arguments: %s" cmd
))
815 (let ((RRR (nth 1 cmd
))
817 (ccl-check-register rrr cmd
)
818 (ccl-check-register RRR cmd
)
819 (ccl-embed-extended-command 'write-multibyte-character rrr RRR
0))
822 ;; Compile translate-character
823 (defun ccl-compile-translate-character (cmd)
824 (if (/= (length cmd
) 4)
825 (error "CCL: Invalid number of arguments: %s" cmd
))
826 (let ((Rrr (nth 1 cmd
))
829 (ccl-check-register rrr cmd
)
830 (ccl-check-register RRR cmd
)
831 (cond ((and (symbolp Rrr
) (not (get Rrr
'ccl-register-number
)))
832 (ccl-embed-extended-command 'translate-character-const-tbl
834 (ccl-embed-symbol Rrr
'translation-table-id
))
836 (ccl-check-register Rrr cmd
)
837 (ccl-embed-extended-command 'translate-character rrr RRR Rrr
))))
840 ;; Compile lookup-integer
841 (defun ccl-compile-lookup-integer (cmd)
842 (if (/= (length cmd
) 4)
843 (error "CCL: Invalid number of arguments: %s" cmd
))
844 (let ((Rrr (nth 1 cmd
))
847 (ccl-check-register RRR cmd
)
848 (ccl-check-register rrr cmd
)
849 (cond ((and (symbolp Rrr
) (not (get Rrr
'ccl-register-number
)))
850 (ccl-embed-extended-command 'lookup-int-const-tbl
852 (ccl-embed-symbol Rrr
'translation-hash-table-id
))
854 (error "CCL: non-constant table: %s" cmd
)
856 (ccl-check-register Rrr cmd
)
857 (ccl-embed-extended-command 'lookup-int rrr RRR
0))))
860 ;; Compile lookup-character
861 (defun ccl-compile-lookup-character (cmd)
862 (if (/= (length cmd
) 4)
863 (error "CCL: Invalid number of arguments: %s" cmd
))
864 (let ((Rrr (nth 1 cmd
))
867 (ccl-check-register RRR cmd
)
868 (ccl-check-register rrr cmd
)
869 (cond ((and (symbolp Rrr
) (not (get Rrr
'ccl-register-number
)))
870 (ccl-embed-extended-command 'lookup-char-const-tbl
872 (ccl-embed-symbol Rrr
'translation-hash-table-id
))
874 (error "CCL: non-constant table: %s" cmd
)
876 (ccl-check-register Rrr cmd
)
877 (ccl-embed-extended-command 'lookup-char rrr RRR
0))))
880 (defun ccl-compile-iterate-multiple-map (cmd)
881 (ccl-compile-multiple-map-function 'iterate-multiple-map cmd
)
884 (defun ccl-compile-map-multiple (cmd)
885 (if (/= (length cmd
) 4)
886 (error "CCL: Invalid number of arguments: %s" cmd
))
890 (let ((len 0) result add
)
892 (if (consp (car arg
))
893 (setq add
(funcall func
(car arg
) t
)
894 result
(append result add
)
895 add
(+ (- (car add
)) 1))
903 (cons (- len
) result
)
905 (setq arg
(append (list (nth 0 cmd
) (nth 1 cmd
) (nth 2 cmd
))
906 (funcall func
(nth 3 cmd
) nil
)))
907 (ccl-compile-multiple-map-function 'map-multiple arg
))
910 (defun ccl-compile-map-single (cmd)
911 (if (/= (length cmd
) 4)
912 (error "CCL: Invalid number of arguments: %s" cmd
))
913 (let ((RRR (nth 1 cmd
))
917 (ccl-check-register rrr cmd
)
918 (ccl-check-register RRR cmd
)
919 (ccl-embed-extended-command 'map-single rrr RRR
0)
921 (if (get map
'code-conversion-map
)
922 (ccl-embed-symbol map
'code-conversion-map-id
)
923 (error "CCL: Invalid map: %s" map
)))
925 (error "CCL: Invalid type of arguments: %s" cmd
))))
928 (defun ccl-compile-multiple-map-function (command cmd
)
929 (if (< (length cmd
) 4)
930 (error "CCL: Invalid number of arguments: %s" cmd
))
931 (let ((RRR (nth 1 cmd
))
933 (args (nthcdr 3 cmd
))
935 (ccl-check-register rrr cmd
)
936 (ccl-check-register RRR cmd
)
937 (ccl-embed-extended-command command rrr RRR
0)
938 (ccl-embed-data (length args
))
940 (setq map
(car args
))
942 (if (get map
'code-conversion-map
)
943 (ccl-embed-symbol map
'code-conversion-map-id
)
944 (error "CCL: Invalid map: %s" map
)))
946 (ccl-embed-data map
))
948 (error "CCL: Invalid type of arguments: %s" cmd
)))
949 (setq args
(cdr args
)))))
954 ;; To avoid byte-compiler warning.
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"))
965 (insert "Out-buffer must be as large as in-buffer.\n"))
968 (format "Out-buffer must be %d times bigger than in-buffer.\n"
970 (insert "Main-body:\n")
971 (setq ccl-current-ic
2)
972 (if (> (aref ccl-code
1) 0)
974 (while (< ccl-current-ic
(aref ccl-code
1))
976 (insert "At EOF:\n")))
977 (while (< ccl-current-ic len
)
981 ;; Return a CCL code in `ccl-code' at `ccl-current-ic'.
982 (defun ccl-get-next-code ()
984 (aref ccl-code ccl-current-ic
)
985 (setq ccl-current-ic
(1+ ccl-current-ic
))))
988 (let* ((code (ccl-get-next-code))
989 (cmd (aref ccl-code-table
(logand code
31)))
990 (rrr (ash (logand code
255) -
5))
992 (insert (format "%5d:[%s] " (1- ccl-current-ic
) cmd
))
993 (funcall (get cmd
'ccl-dump-function
) rrr cc
)))
995 (defun ccl-dump-set-register (rrr cc
)
996 (insert (format "r%d = r%d\n" rrr cc
)))
998 (defun ccl-dump-set-short-const (rrr cc
)
999 (insert (format "r%d = %d\n" rrr cc
)))
1001 (defun ccl-dump-set-const (rrr ignore
)
1002 (insert (format "r%d = %d\n" rrr
(ccl-get-next-code))))
1004 (defun ccl-dump-set-array (rrr cc
)
1005 (let ((rrr2 (logand cc
7))
1008 (insert (format "r%d = array[r%d] of length %d\n\t"
1011 (insert (format "%d " (ccl-get-next-code)))
1015 (defun ccl-dump-jump (ignore cc
&optional address
)
1016 (insert (format "jump to %d(" (+ (or address ccl-current-ic
) cc
)))
1019 (insert (format "%d)\n" (1+ cc
))))
1021 (defun ccl-dump-jump-cond (rrr cc
)
1022 (insert (format "if (r%d == 0), " rrr
))
1023 (ccl-dump-jump nil cc
))
1025 (defun ccl-dump-write-register-jump (rrr cc
)
1026 (insert (format "write r%d, " rrr
))
1027 (ccl-dump-jump nil cc
))
1029 (defun ccl-dump-write-register-read-jump (rrr cc
)
1030 (insert (format "write r%d, read r%d, " rrr rrr
))
1031 (ccl-dump-jump nil cc
)
1032 (ccl-get-next-code) ; Skip dummy READ-JUMP
1035 (defun ccl-extract-arith-op (cc)
1036 (aref ccl-arith-table
(ash cc -
6)))
1038 (defun ccl-dump-write-expr-const (ignore cc
)
1039 (insert (format "write (r%d %s %d)\n"
1041 (ccl-extract-arith-op cc
)
1042 (ccl-get-next-code))))
1044 (defun ccl-dump-write-expr-register (ignore cc
)
1045 (insert (format "write (r%d %s r%d)\n"
1047 (ccl-extract-arith-op cc
)
1048 (logand (ash cc -
3) 7))))
1050 (defun ccl-dump-insert-char (cc)
1051 (cond ((= cc ?
\t) (insert " \"^I\""))
1052 ((= cc ?
\n) (insert " \"^J\""))
1053 (t (insert (format " \"%c\"" cc
)))))
1055 (defun ccl-dump-write-const-jump (ignore cc
)
1056 (let ((address ccl-current-ic
))
1057 (insert "write char")
1058 (ccl-dump-insert-char (ccl-get-next-code))
1060 (ccl-dump-jump nil cc address
)))
1062 (defun ccl-dump-write-const-read-jump (rrr cc
)
1063 (let ((address ccl-current-ic
))
1064 (insert "write char")
1065 (ccl-dump-insert-char (ccl-get-next-code))
1066 (insert (format ", read r%d, " rrr
))
1067 (ccl-dump-jump cc address
)
1068 (ccl-get-next-code) ; Skip dummy READ-JUMP
1071 (defun ccl-dump-write-string-jump (ignore cc
)
1072 (let ((address ccl-current-ic
)
1073 (len (ccl-get-next-code))
1077 (let ((code (ccl-get-next-code)))
1078 (insert (ash code -
16))
1079 (if (< (1+ i
) len
) (insert (logand (ash code -
8) 255)))
1080 (if (< (+ i
2) len
) (insert (logand code
255))))
1083 (ccl-dump-jump nil cc address
)))
1085 (defun ccl-dump-write-array-read-jump (rrr cc
)
1086 (let ((address ccl-current-ic
)
1087 (len (ccl-get-next-code))
1089 (insert (format "write array[r%d] of length %d,\n\t" rrr len
))
1091 (ccl-dump-insert-char (ccl-get-next-code))
1093 (insert (format "\n\tthen read r%d, " rrr
))
1094 (ccl-dump-jump nil cc address
)
1095 (ccl-get-next-code) ; Skip dummy READ-JUMP.
1098 (defun ccl-dump-read-jump (rrr cc
)
1099 (insert (format "read r%d, " rrr
))
1100 (ccl-dump-jump nil cc
))
1102 (defun ccl-dump-branch (rrr len
)
1103 (let ((jump-table-head ccl-current-ic
)
1105 (insert (format "jump to array[r%d] of length %d\n\t" rrr len
))
1107 (insert (format "%d " (+ jump-table-head
(ccl-get-next-code))))
1111 (defun ccl-dump-read-register (rrr cc
)
1112 (insert (format "read r%d (%d remaining)\n" rrr cc
)))
1114 (defun ccl-dump-read-branch (rrr len
)
1115 (insert (format "read r%d, " rrr
))
1116 (ccl-dump-branch rrr len
))
1118 (defun ccl-dump-write-register (rrr cc
)
1119 (insert (format "write r%d (%d remaining)\n" rrr cc
)))
1121 (defun ccl-dump-call (ignore cc
)
1122 (insert (format "call subroutine #%d\n" cc
)))
1124 (defun ccl-dump-write-const-string (rrr cc
)
1127 (insert "write char")
1128 (ccl-dump-insert-char cc
)
1134 (let ((code (ccl-get-next-code)))
1135 (insert (format "%c" (lsh code -
16)))
1137 (insert (format "%c" (logand (lsh code -
8) 255))))
1139 (insert (format "%c" (logand code
255))))
1143 (defun ccl-dump-write-array (rrr cc
)
1145 (insert (format "write array[r%d] of length %d\n\t" rrr cc
))
1147 (ccl-dump-insert-char (ccl-get-next-code))
1151 (defun ccl-dump-end (&rest ignore
)
1154 (defun ccl-dump-set-assign-expr-const (rrr cc
)
1155 (insert (format "r%d %s= %d\n"
1157 (ccl-extract-arith-op cc
)
1158 (ccl-get-next-code))))
1160 (defun ccl-dump-set-assign-expr-register (rrr cc
)
1161 (insert (format "r%d %s= r%d\n"
1163 (ccl-extract-arith-op cc
)
1166 (defun ccl-dump-set-expr-const (rrr cc
)
1167 (insert (format "r%d = r%d %s %d\n"
1170 (ccl-extract-arith-op cc
)
1171 (ccl-get-next-code))))
1173 (defun ccl-dump-set-expr-register (rrr cc
)
1174 (insert (format "r%d = r%d %s r%d\n"
1177 (ccl-extract-arith-op cc
)
1178 (logand (ash cc -
3) 7))))
1180 (defun ccl-dump-jump-cond-expr-const (rrr cc
)
1181 (let ((address ccl-current-ic
))
1182 (insert (format "if !(r%d %s %d), "
1184 (aref ccl-arith-table
(ccl-get-next-code))
1185 (ccl-get-next-code)))
1186 (ccl-dump-jump nil cc address
)))
1188 (defun ccl-dump-jump-cond-expr-register (rrr cc
)
1189 (let ((address ccl-current-ic
))
1190 (insert (format "if !(r%d %s r%d), "
1192 (aref ccl-arith-table
(ccl-get-next-code))
1193 (ccl-get-next-code)))
1194 (ccl-dump-jump nil cc address
)))
1196 (defun ccl-dump-read-jump-cond-expr-const (rrr cc
)
1197 (insert (format "read r%d, " rrr
))
1198 (ccl-dump-jump-cond-expr-const rrr cc
))
1200 (defun ccl-dump-read-jump-cond-expr-register (rrr cc
)
1201 (insert (format "read r%d, " rrr
))
1202 (ccl-dump-jump-cond-expr-register rrr cc
))
1204 (defun ccl-dump-binary (ccl-code)
1205 (let ((len (length ccl-code
))
1208 (let ((code (aref ccl-code i
))
1211 (insert (if (= (logand code
(ash 1 j
)) 0) ?
0 ?
1))
1213 (setq code
(logand code
31))
1214 (if (< code
(length ccl-code-table
))
1215 (insert (format ":%s" (aref ccl-code-table code
))))
1219 (defun ccl-dump-ex-cmd (rrr cc
)
1220 (let* ((RRR (logand cc ?\x7
))
1221 (Rrr (logand (ash cc -
3) ?\x7
))
1222 (ex-op (aref ccl-extended-code-table
(logand (ash cc -
6) ?
\x3fff
))))
1223 (insert (format "<%s> " ex-op
))
1224 (funcall (get ex-op
'ccl-dump-function
) rrr RRR Rrr
)))
1226 (defun ccl-dump-read-multibyte-character (rrr RRR Rrr
)
1227 (insert (format "read-multibyte-character r%d r%d\n" RRR rrr
)))
1229 (defun ccl-dump-write-multibyte-character (rrr RRR Rrr
)
1230 (insert (format "write-multibyte-character r%d r%d\n" RRR rrr
)))
1232 (defun ccl-dump-translate-character (rrr RRR Rrr
)
1233 (insert (format "translation table(r%d) r%d r%d\n" Rrr RRR rrr
)))
1235 (defun ccl-dump-translate-character-const-tbl (rrr RRR Rrr
)
1236 (let ((tbl (ccl-get-next-code)))
1237 (insert (format "translation table(%S) r%d r%d\n" tbl RRR rrr
))))
1239 (defun ccl-dump-lookup-int-const-tbl (rrr RRR Rrr
)
1240 (let ((tbl (ccl-get-next-code)))
1241 (insert (format "hash table(%S) r%d r%d\n" tbl RRR rrr
))))
1243 (defun ccl-dump-lookup-char-const-tbl (rrr RRR Rrr
)
1244 (let ((tbl (ccl-get-next-code)))
1245 (insert (format "hash table(%S) r%d r%d\n" tbl RRR rrr
))))
1247 (defun ccl-dump-iterate-multiple-map (rrr RRR Rrr
)
1248 (let ((notbl (ccl-get-next-code))
1250 (insert (format "iterate-multiple-map r%d r%d\n" RRR rrr
))
1251 (insert (format "\tnumber of maps is %d .\n\t [" notbl
))
1253 (setq id
(ccl-get-next-code))
1254 (insert (format "%S" id
))
1258 (defun ccl-dump-map-multiple (rrr RRR Rrr
)
1259 (let ((notbl (ccl-get-next-code))
1261 (insert (format "map-multiple r%d r%d\n" RRR rrr
))
1262 (insert (format "\tnumber of maps and separators is %d\n\t [" notbl
))
1264 (setq id
(ccl-get-next-code))
1267 (insert (format "%S " id
)))
1271 (defun ccl-dump-map-single (rrr RRR Rrr
)
1272 (let ((id (ccl-get-next-code)))
1273 (insert (format "map-single r%d r%d map(%S)\n" RRR rrr id
))))
1276 ;; CCL emulation staffs
1278 ;; Not yet implemented.
1280 ;; Auto-loaded functions.
1283 (defmacro declare-ccl-program
(name &optional vector
)
1284 "Declare NAME as a name of CCL program.
1286 This macro exists for backward compatibility. In the old version of
1287 Emacs, to compile a CCL program which calls another CCL program not
1288 yet defined, it must be declared as a CCL program in advance. But,
1289 now CCL program names are resolved not at compile time but before
1292 Optional arg VECTOR is a compiled CCL code of the CCL program."
1293 `(put ',name
'ccl-program-idx
(register-ccl-program ',name
,vector
)))
1296 (defmacro define-ccl-program
(name ccl-program
&optional doc
)
1297 "Set NAME the compiled code of CCL-PROGRAM.
1299 CCL-PROGRAM has this form:
1300 (BUFFER_MAGNIFICATION
1304 BUFFER_MAGNIFICATION is an integer value specifying the approximate
1305 output buffer magnification size compared with the bytes of input data
1306 text. If the value is zero, the CCL program can't execute `read' and
1309 CCL_MAIN_CODE and CCL_EOF_CODE are CCL program codes. CCL_MAIN_CODE
1310 executed at first. If there's no more input data when `read' command
1311 is executed in CCL_MAIN_CODE, CCL_EOF_CODE is executed. If
1312 CCL_MAIN_CODE is terminated, CCL_EOF_CODE is not executed.
1314 Here's the syntax of CCL program code in BNF notation. The lines
1315 starting by two semicolons (and optional leading spaces) describe the
1318 CCL_MAIN_CODE := CCL_BLOCK
1320 CCL_EOF_CODE := CCL_BLOCK
1322 CCL_BLOCK := STATEMENT | (STATEMENT [STATEMENT ...])
1325 SET | IF | BRANCH | LOOP | REPEAT | BREAK | READ | WRITE | CALL
1326 | TRANSLATE | MAP | LOOKUP | END
1328 SET := (REG = EXPRESSION)
1329 | (REG ASSIGNMENT_OPERATOR EXPRESSION)
1330 ;; The following form is the same as (r0 = integer).
1333 EXPRESSION := ARG | (EXPRESSION OPERATOR ARG)
1335 ;; Evaluate EXPRESSION. If the result is nonzero, execute
1336 ;; CCL_BLOCK_0. Otherwise, execute CCL_BLOCK_1.
1337 IF := (if EXPRESSION CCL_BLOCK_0 CCL_BLOCK_1)
1339 ;; Evaluate EXPRESSION. Provided that the result is N, execute
1341 BRANCH := (branch EXPRESSION CCL_BLOCK_0 [CCL_BLOCK_1 ...])
1343 ;; Execute STATEMENTs until (break) or (end) is executed.
1344 LOOP := (loop STATEMENT [STATEMENT ...])
1346 ;; Terminate the most inner loop.
1350 ;; Jump to the head of the most inner loop.
1352 ;; Same as: ((write [REG | integer | string])
1354 | (write-repeat [REG | integer | string])
1355 ;; Same as: ((write REG [ARRAY])
1358 | (write-read-repeat REG [ARRAY])
1359 ;; Same as: ((write integer)
1362 | (write-read-repeat REG integer)
1364 READ := ;; Set REG_0 to a byte read from the input text, set REG_1
1365 ;; to the next byte read, and so on.
1366 (read REG_0 [REG_1 ...])
1367 ;; Same as: ((read REG)
1368 ;; (if (REG OPERATOR ARG) CCL_BLOCK_0 CCL_BLOCK_1))
1369 | (read-if (REG OPERATOR ARG) CCL_BLOCK_0 CCL_BLOCK_1)
1370 ;; Same as: ((read REG)
1371 ;; (branch REG CCL_BLOCK_0 [CCL_BLOCK_1 ...]))
1372 | (read-branch REG CCL_BLOCK_0 [CCL_BLOCK_1 ...])
1373 ;; Read a character from the input text while parsing
1374 ;; multibyte representation, set REG_0 to the charset ID of
1375 ;; the character, set REG_1 to the code point of the
1376 ;; character. If the dimension of charset is two, set REG_1
1377 ;; to ((CODE0 << 7) | CODE1), where CODE0 is the first code
1378 ;; point and CODE1 is the second code point.
1379 | (read-multibyte-character REG_0 REG_1)
1382 ;; Write REG_0, REG_1, ... to the output buffer. If REG_N is
1383 ;; a multibyte character, write the corresponding multibyte
1385 (write REG_0 [REG_1 ...])
1386 ;; Same as: ((r7 = EXPRESSION)
1388 | (write EXPRESSION)
1389 ;; Write the value of `integer' to the output buffer. If it
1390 ;; is a multibyte character, write the corresponding multibyte
1393 ;; Write the byte sequence of `string' as is to the output
1396 ;; Same as: (write string)
1398 ;; Provided that the value of REG is N, write Nth element of
1399 ;; ARRAY to the output buffer. If it is a multibyte
1400 ;; character, write the corresponding multibyte
1403 ;; Write a multibyte representation of a character whose
1404 ;; charset ID is REG_0 and code point is REG_1. If the
1405 ;; dimension of the charset is two, REG_1 should be ((CODE0 <<
1406 ;; 7) | CODE1), where CODE0 is the first code point and CODE1
1407 ;; is the second code point of the character.
1408 | (write-multibyte-character REG_0 REG_1)
1410 ;; Call CCL program whose name is ccl-program-name.
1411 CALL := (call ccl-program-name)
1413 ;; Terminate the CCL program.
1416 ;; CCL registers that can contain any integer value. As r7 is also
1417 ;; used by CCL interpreter, its value is changed unexpectedly.
1418 REG := r0 | r1 | r2 | r3 | r4 | r5 | r6 | r7
1420 ARG := REG | integer
1423 ;; Normal arithmethic operators (same meaning as C code).
1426 ;; Bitwize operators (same meaning as C code)
1429 ;; Shifting operators (same meaning as C code)
1432 ;; (REG = ARG_0 <8 ARG_1) means:
1433 ;; (REG = ((ARG_0 << 8) | ARG_1))
1436 ;; (REG = ARG_0 >8 ARG_1) means:
1437 ;; ((REG = (ARG_0 >> 8))
1438 ;; (r7 = (ARG_0 & 255)))
1441 ;; (REG = ARG_0 // ARG_1) means:
1442 ;; ((REG = (ARG_0 / ARG_1))
1443 ;; (r7 = (ARG_0 % ARG_1)))
1446 ;; Normal comparing operators (same meaning as C code)
1447 | < | > | == | <= | >= | !=
1449 ;; If ARG_0 and ARG_1 are higher and lower byte of Shift-JIS
1450 ;; code, and CHAR is the corresponding JISX0208 character,
1451 ;; (REG = ARG_0 de-sjis ARG_1) means:
1454 ;; where CODE0 is the first code point of CHAR, CODE1 is the
1455 ;; second code point of CHAR.
1458 ;; If ARG_0 and ARG_1 are the first and second code point of
1459 ;; JISX0208 character CHAR, and SJIS is the correponding
1461 ;; (REG = ARG_0 en-sjis ARG_1) means:
1464 ;; where HIGH is the higher byte of SJIS, LOW is the lower
1468 ASSIGNMENT_OPERATOR :=
1469 ;; Same meaning as C code
1470 += | -= | *= | /= | %= | &= | `|=' | ^= | <<= | >>=
1472 ;; (REG <8= ARG) is the same as:
1477 ;; (REG >8= ARG) is the same as:
1478 ;; ((r7 = (REG & 255))
1481 ;; (REG //= ARG) is the same as:
1482 ;; ((r7 = (REG % ARG))
1486 ARRAY := `[' integer ... `]'
1490 (translate-character REG(table) REG(charset) REG(codepoint))
1491 | (translate-character SYMBOL REG(charset) REG(codepoint))
1492 ;; SYMBOL must refer to a table defined by `define-translation-table'.
1494 (lookup-character SYMBOL REG(charset) REG(codepoint))
1495 | (lookup-integer SYMBOL REG(integer))
1496 ;; SYMBOL refers to a table defined by `define-hash-translation-table'.
1498 (iterate-multiple-map REG REG MAP-IDs)
1499 | (map-multiple REG REG (MAP-SET))
1500 | (map-single REG REG MAP-ID)
1501 MAP-IDs := MAP-ID ...
1502 MAP-SET := MAP-IDs | (MAP-IDs) MAP-SET
1505 `(let ((prog ,(ccl-compile (eval ccl-program
))))
1506 (defconst ,name prog
,doc
)
1507 (put ',name
'ccl-program-idx
(register-ccl-program ',name prog
))
1511 (defmacro check-ccl-program
(ccl-program &optional name
)
1512 "Check validity of CCL-PROGRAM.
1513 If CCL-PROGRAM is a symbol denoting a CCL program, return
1514 CCL-PROGRAM, else return nil.
1515 If CCL-PROGRAM is a vector and optional arg NAME (symbol) is supplied,
1516 register CCL-PROGRAM by name NAME, and return NAME."
1517 `(if (ccl-program-p ,ccl-program
)
1518 (if (vectorp ,ccl-program
)
1520 (register-ccl-program ,name
,ccl-program
)
1525 (defun ccl-execute-with-args (ccl-prog &rest args
)
1526 "Execute CCL-PROGRAM with registers initialized by the remaining args.
1527 The return value is a vector of resulting CCL registers.
1529 See the documentation of `define-ccl-program' for the detail of CCL program."
1530 (let ((reg (make-vector 8 0))
1532 (while (and args
(< i
8))
1533 (if (not (integerp (car args
)))
1534 (error "Arguments should be integer"))
1535 (aset reg i
(car args
))
1536 (setq args
(cdr args
) i
(1+ i
)))
1537 (ccl-execute ccl-prog reg
)
1542 ;;; ccl.el ends here