1 ;;; ccl.el --- CCL (Code Conversion Language) compiler
3 ;; Copyright (C) 1997, 1998, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
5 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
6 ;; 2005, 2006, 2007, 2008, 2009
7 ;; National Institute of Advanced Industrial Science and Technology (AIST)
8 ;; Registration Number H14PRO021
10 ;; Keywords: CCL, mule, multilingual, character set, coding-system
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;; CCL (Code Conversion Language) is a simple programming language to
30 ;; be used for various kind of code conversion. A CCL program is
31 ;; compiled to CCL code (vector of integers) and executed by the CCL
32 ;; interpreter in Emacs.
34 ;; CCL is used for code conversion at process I/O and file I/O for
35 ;; non-standard coding-systems. In addition, it is used for
36 ;; calculating code points of X fonts from character codes.
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 in Emacs Lisp.
42 ;; The syntax and semantics of CCL programs are described in the
43 ;; documentation of `define-ccl-program'.
48 "CCL (Code Conversion Language) compiler."
52 (defconst ccl-command-table
53 [if branch loop break repeat write-repeat write-read-repeat
54 read read-if read-branch write call end
55 read-multibyte-character write-multibyte-character
57 iterate-multiple-map map-multiple map-single lookup-integer
59 "Vector of CCL commands (symbols).")
61 ;; Put a property to each symbol of CCL commands for the compiler.
62 (let (op (i 0) (len (length ccl-command-table
)))
64 (setq op
(aref ccl-command-table i
))
65 (put op
'ccl-compile-function
(intern (format "ccl-compile-%s" op
)))
68 (defconst ccl-code-table
76 write-register-read-jump
93 set-assign-expr-register
97 jump-cond-expr-register
98 read-jump-cond-expr-const
99 read-jump-cond-expr-register
102 "Vector of CCL compiled codes (symbols).")
104 (defconst ccl-extended-code-table
105 [read-multibyte-character
106 write-multibyte-character
108 translate-character-const-tbl
109 nil nil nil nil nil nil nil nil nil nil nil nil
; 0x04-0x0f
114 lookup-char-const-tbl
116 "Vector of CCL extended compiled codes (symbols).")
118 ;; Put a property to each symbol of CCL codes for the disassembler.
119 (let (code (i 0) (len (length ccl-code-table
)))
121 (setq code
(aref ccl-code-table i
))
122 (put code
'ccl-code i
)
123 (put code
'ccl-dump-function
(intern (format "ccl-dump-%s" code
)))
126 (let (code (i 0) (len (length ccl-extended-code-table
)))
128 (setq code
(aref ccl-extended-code-table i
))
131 (put code
'ccl-ex-code i
)
132 (put code
'ccl-dump-function
(intern (format "ccl-dump-%s" code
)))))
135 (defconst ccl-jump-code-list
136 '(jump jump-cond write-register-jump write-register-read-jump
137 write-const-jump write-const-read-jump write-string-jump
138 write-array-read-jump read-jump
))
140 ;; Put a property `jump-flag' to each CCL code which execute jump in
142 (let ((l ccl-jump-code-list
))
144 (put (car l
) 'jump-flag t
)
147 (defconst ccl-register-table
148 [r0 r1 r2 r3 r4 r5 r6 r7
]
149 "Vector of CCL registers (symbols).")
151 ;; Put a property to indicate register number to each symbol of CCL.
153 (let (reg (i 0) (len (length ccl-register-table
)))
155 (setq reg
(aref ccl-register-table i
))
156 (put reg
'ccl-register-number i
)
159 (defconst ccl-arith-table
160 [+ -
* / %
& | ^
<< >> <8 >8 // nil nil nil
161 < > == <= >= != de-sjis en-sjis
]
162 "Vector of CCL arithmetic/logical operators (symbols).")
164 ;; Put a property to each symbol of CCL operators for the compiler.
165 (let (arith (i 0) (len (length ccl-arith-table
)))
167 (setq arith
(aref ccl-arith-table i
))
168 (if arith
(put arith
'ccl-arith-code i
))
171 (defconst ccl-assign-arith-table
172 [+= -
= *= /= %
= &= |
= ^
= <<= >>= <8= >8= //=]
173 "Vector of CCL assignment operators (symbols).")
175 ;; Put a property to each symbol of CCL assignment operators for the compiler.
176 (let (arith (i 0) (len (length ccl-assign-arith-table
)))
178 (setq arith
(aref ccl-assign-arith-table i
))
179 (put arith
'ccl-self-arith-code i
)
182 (defvar ccl-program-vector nil
183 "Working vector of CCL codes produced by CCL compiler.")
184 (defvar ccl-current-ic
0
185 "The current index for `ccl-program-vector'.")
187 (defun ccl-embed-data (data &optional ic
)
188 "Embed integer DATA in `ccl-program-vector' at `ccl-current-ic' and
189 increment it. If IC is specified, embed DATA at IC."
191 (aset ccl-program-vector ic data
)
192 (let ((len (length ccl-program-vector
)))
193 (if (>= ccl-current-ic len
)
194 (let ((new (make-vector (* len
2) nil
)))
197 (aset new len
(aref ccl-program-vector len
)))
198 (setq ccl-program-vector new
))))
199 (aset ccl-program-vector ccl-current-ic data
)
200 (setq ccl-current-ic
(1+ ccl-current-ic
))))
202 (defun ccl-embed-symbol (symbol prop
)
203 "Embed pair of SYMBOL and PROP where (get SYMBOL PROP) should give
204 proper index number for SYMBOL. PROP should be
205 `translation-table-id', `translation-hash-table-id'
206 `code-conversion-map-id', or `ccl-program-idx'."
207 (ccl-embed-data (cons symbol prop
)))
209 (defun ccl-embed-string (len str
)
210 "Embed string STR of length LEN in `ccl-program-vector' at
213 (error "CCL: String too long: %d" len
))
214 (if (> (string-bytes str
) len
)
216 (ccl-embed-data (logior #x1000000
(aref str i
))))
219 (ccl-embed-data (logior (ash (aref str i
) 16)
221 (ash (aref str
(1+ i
)) 8)
228 (defun ccl-embed-current-address (ic)
229 "Embed a relative jump address to `ccl-current-ic' in
230 `ccl-program-vector' at IC without altering the other bit field."
231 (let ((relative (- ccl-current-ic
(1+ ic
))))
232 (aset ccl-program-vector ic
233 (logior (aref ccl-program-vector ic
) (ash relative
8)))))
235 (defun ccl-embed-code (op reg data
&optional reg2
)
236 "Embed CCL code for the operation OP and arguments REG and DATA in
237 `ccl-program-vector' at `ccl-current-ic' in the following format.
238 |----------------- integer (28-bit) ------------------|
239 |------------ 20-bit ------------|- 3-bit --|- 5-bit -|
240 |------------- DATA -------------|-- REG ---|-- OP ---|
241 If REG2 is specified, embed a code in the following format.
242 |------- 17-bit ------|- 3-bit --|- 3-bit --|- 5-bit -|
243 |-------- DATA -------|-- REG2 --|-- REG ---|-- OP ---|
245 If REG is a CCL register symbol (e.g. r0, r1...), the register
246 number is embedded. If OP is one of unconditional jumps, DATA is
247 changed to a relative jump address."
248 (if (and (> data
0) (get op
'jump-flag
))
249 ;; DATA is an absolute jump address. Make it relative to the
250 ;; next of jump code.
251 (setq data
(- data
(1+ ccl-current-ic
))))
252 (let ((code (logior (get op
'ccl-code
)
254 (if (symbolp reg
) (get reg
'ccl-register-number
) reg
) 5)
256 (logior (ash (get reg2
'ccl-register-number
) 8)
259 (ccl-embed-data code
)))
261 (defun ccl-embed-extended-command (ex-op reg reg2 reg3
)
262 "extended ccl command format
263 |- 14-bit -|- 3-bit --|- 3-bit --|- 3-bit --|- 5-bit -|
264 |- EX-OP --|-- REG3 --|-- REG2 --|-- REG ---|-- OP ---|"
265 (let ((data (logior (ash (get ex-op
'ccl-ex-code
) 3)
267 (get reg3
'ccl-register-number
)
269 (ccl-embed-code 'ex-cmd reg data reg2
)))
271 (defun ccl-increment-ic (inc)
272 "Just advance `ccl-current-ic' by INC."
273 (setq ccl-current-ic
(+ ccl-current-ic inc
)))
275 (defvar ccl-loop-head nil
276 "If non-nil, index of the start of the current loop.")
277 (defvar ccl-breaks nil
278 "If non-nil, list of absolute addresses of the breaking points of
282 (defun ccl-compile (ccl-program)
283 "Return the compiled code of CCL-PROGRAM as a vector of integers."
284 (if (or (null (consp ccl-program
))
285 (null (integerp (car ccl-program
)))
286 (null (listp (car (cdr ccl-program
)))))
287 (error "CCL: Invalid CCL program: %s" ccl-program
))
288 (if (null (vectorp ccl-program-vector
))
289 (setq ccl-program-vector
(make-vector 8192 0)))
290 (setq ccl-loop-head nil ccl-breaks nil
)
291 (setq ccl-current-ic
0)
293 ;; The first element is the buffer magnification.
294 (ccl-embed-data (car ccl-program
))
296 ;; The second element is the address of the start CCL code for
297 ;; processing end of input buffer (we call it eof-processor). We
301 ;; Compile the main body of the CCL program.
302 (ccl-compile-1 (car (cdr ccl-program
)))
304 ;; Embed the address of eof-processor.
305 (ccl-embed-data ccl-current-ic
1)
307 ;; Then compile eof-processor.
308 (if (nth 2 ccl-program
)
309 (ccl-compile-1 (nth 2 ccl-program
)))
311 ;; At last, embed termination code.
312 (ccl-embed-code 'end
0 0)
314 (let ((vec (make-vector ccl-current-ic
0))
316 (while (< i ccl-current-ic
)
317 (aset vec i
(aref ccl-program-vector i
))
321 (defun ccl-syntax-error (cmd)
322 "Signal syntax error."
323 (error "CCL: Syntax error: %s" cmd
))
325 (defun ccl-check-register (arg cmd
)
326 "Check if ARG is a valid CCL register."
327 (if (get arg
'ccl-register-number
)
329 (error "CCL: Invalid register %s in %s" arg cmd
)))
331 (defun ccl-check-compile-function (arg cmd
)
332 "Check if ARG is a valid CCL command."
333 (or (get arg
'ccl-compile-function
)
334 (error "CCL: Invalid command: %s" cmd
)))
336 ;; In the following code, most ccl-compile-XXXX functions return t if
337 ;; they end with unconditional jump, else return nil.
339 (defun ccl-compile-1 (ccl-block)
340 "Compile CCL-BLOCK (see the syntax above)."
341 (let (unconditional-jump
343 (if (or (integerp ccl-block
)
345 (and ccl-block
(symbolp (car ccl-block
))))
346 ;; This block consists of single statement.
347 (setq ccl-block
(list ccl-block
)))
349 ;; Now CCL-BLOCK is a list of statements. Compile them one by
352 (setq cmd
(car ccl-block
))
353 (setq unconditional-jump
354 (cond ((integerp cmd
)
355 ;; SET statement for the register 0.
356 (ccl-compile-set (list 'r0
'= cmd
)))
359 ;; WRITE statement of string argument.
360 (ccl-compile-write-string cmd
))
363 ;; The other statements.
364 (cond ((eq (nth 1 cmd
) '=)
365 ;; SET statement of the form `(REG = EXPRESSION)'.
366 (ccl-compile-set cmd
))
368 ((and (symbolp (nth 1 cmd
))
369 (get (nth 1 cmd
) 'ccl-self-arith-code
))
370 ;; SET statement with an assignment operation.
371 (ccl-compile-self-set cmd
))
374 (funcall (ccl-check-compile-function (car cmd
) cmd
)
378 (ccl-syntax-error cmd
))))
379 (setq ccl-block
(cdr ccl-block
)))
382 (defconst ccl-max-short-const
(ash 1 19))
383 (defconst ccl-min-short-const
(ash -
1 19))
385 (defun ccl-compile-set (cmd)
386 "Compile SET statement."
387 (let ((rrr (ccl-check-register (car cmd
) cmd
))
390 ;; CMD has the form `(RRR = (XXX OP YYY))'.
391 (ccl-compile-expression rrr right
))
394 ;; CMD has the form `(RRR = integer)'.
395 (if (and (<= right ccl-max-short-const
)
396 (>= right ccl-min-short-const
))
397 (ccl-embed-code 'set-short-const rrr right
)
398 (ccl-embed-code 'set-const rrr
0)
399 (ccl-embed-data right
)))
402 ;; CMD has the form `(RRR = rrr [ array ])'.
403 (ccl-check-register right cmd
)
404 (let ((ary (nth 3 cmd
)))
406 (let ((i 0) (len (length ary
)))
407 (ccl-embed-code 'set-array rrr len right
)
409 (ccl-embed-data (aref ary i
))
411 (ccl-embed-code 'set-register rrr
0 right
))))))
414 (defun ccl-compile-self-set (cmd)
415 "Compile SET statement with ASSIGNMENT_OPERATOR."
416 (let ((rrr (ccl-check-register (car cmd
) cmd
))
419 ;; CMD has the form `(RRR ASSIGN_OP (XXX OP YYY))', compile
420 ;; the right hand part as `(r7 = (XXX OP YYY))' (note: the
421 ;; register 7 can be used for storing temporary value).
423 (ccl-compile-expression 'r7 right
)
425 ;; Now CMD has the form `(RRR ASSIGN_OP ARG)'. Compile it as
426 ;; `(RRR = (RRR OP ARG))'.
427 (ccl-compile-expression
429 (list rrr
(intern (substring (symbol-name (nth 1 cmd
)) 0 -
1)) right
)))
432 (defun ccl-compile-expression (rrr expr
)
433 "Compile SET statement of the form `(RRR = EXPR)'."
434 (let ((left (car expr
))
435 (op (get (nth 1 expr
) 'ccl-arith-code
))
436 (right (nth 2 expr
)))
439 ;; EXPR has the form `((EXPR2 OP2 ARG) OP RIGHT)'. Compile
440 ;; the first term as `(r7 = (EXPR2 OP2 ARG)).'
441 (ccl-compile-expression 'r7 left
)
444 ;; Now EXPR has the form (LEFT OP RIGHT).
445 (if (and (eq rrr left
)
446 (< op
(length ccl-assign-arith-table
)))
447 ;; Compile this SET statement as `(RRR OP= RIGHT)'.
450 (ccl-embed-code 'set-assign-expr-const rrr
(ash op
3) 'r0
)
451 (ccl-embed-data right
))
452 (ccl-check-register right expr
)
453 (ccl-embed-code 'set-assign-expr-register rrr
(ash op
3) right
))
455 ;; Compile this SET statement as `(RRR = (LEFT OP RIGHT))'.
458 (ccl-embed-code 'set-expr-const rrr
(ash op
3) left
)
459 (ccl-embed-data right
))
460 (ccl-check-register right expr
)
461 (ccl-embed-code 'set-expr-register
463 (logior (ash op
3) (get right
'ccl-register-number
))
466 (defun ccl-compile-write-string (str)
467 "Compile WRITE statement with string argument."
468 (let ((len (length str
)))
469 (ccl-embed-code 'write-const-string
1 len
)
470 (ccl-embed-string len str
))
473 (defun ccl-compile-if (cmd &optional read-flag
)
474 "Compile IF statement of the form `(if CONDITION TRUE-PART FALSE-PART)'.
475 If READ-FLAG is non-nil, this statement has the form
476 `(read-if (REG OPERATOR ARG) TRUE-PART FALSE-PART)'."
477 (if (and (/= (length cmd
) 3) (/= (length cmd
) 4))
478 (error "CCL: Invalid number of arguments: %s" cmd
))
479 (let ((condition (nth 1 cmd
))
480 (true-cmds (nth 2 cmd
))
481 (false-cmds (nth 3 cmd
))
484 (if (and (listp condition
)
485 (listp (car condition
)))
486 ;; If CONDITION is a nested expression, the inner expression
487 ;; should be compiled at first as SET statement, i.e.:
488 ;; `(if ((X OP2 Y) OP Z) ...)' is compiled into two statements:
489 ;; `(r7 = (X OP2 Y)) (if (r7 OP Z) ...)'.
491 (ccl-compile-expression 'r7
(car condition
))
492 (setq condition
(cons 'r7
(cdr condition
)))
493 (setq cmd
(cons (car cmd
)
494 (cons condition
(cdr (cdr cmd
)))))))
496 (setq jump-cond-address ccl-current-ic
)
497 ;; Compile CONDITION.
498 (if (symbolp condition
)
499 ;; CONDITION is a register.
501 (ccl-check-register condition cmd
)
502 (ccl-embed-code 'jump-cond condition
0))
503 ;; CONDITION is a simple expression of the form (RRR OP ARG).
504 (let ((rrr (car condition
))
505 (op (get (nth 1 condition
) 'ccl-arith-code
))
506 (arg (nth 2 condition
)))
507 (ccl-check-register rrr cmd
)
510 (ccl-embed-code (if read-flag
'read-jump-cond-expr-const
511 'jump-cond-expr-const
)
514 (ccl-embed-data arg
))
515 (ccl-check-register arg cmd
)
516 (ccl-embed-code (if read-flag
'read-jump-cond-expr-register
517 'jump-cond-expr-register
)
520 (ccl-embed-data (get arg
'ccl-register-number
)))))
522 ;; Compile TRUE-PART.
523 (let ((unconditional-jump (ccl-compile-1 true-cmds
)))
524 (if (null false-cmds
)
525 ;; This is the place to jump to if condition is false.
527 (ccl-embed-current-address jump-cond-address
)
528 (setq unconditional-jump nil
))
529 (let (end-true-part-address)
530 (if (not unconditional-jump
)
532 ;; If TRUE-PART does not end with unconditional jump, we
533 ;; have to jump to the end of FALSE-PART from here.
534 (setq end-true-part-address ccl-current-ic
)
535 (ccl-embed-code 'jump
0 0)))
536 ;; This is the place to jump to if CONDITION is false.
537 (ccl-embed-current-address jump-cond-address
)
538 ;; Compile FALSE-PART.
539 (setq unconditional-jump
540 (and (ccl-compile-1 false-cmds
) unconditional-jump
))
541 (if end-true-part-address
542 ;; This is the place to jump to after the end of TRUE-PART.
543 (ccl-embed-current-address end-true-part-address
))))
544 unconditional-jump
)))
546 (defun ccl-compile-branch (cmd)
547 "Compile BRANCH statement."
548 (if (< (length cmd
) 3)
549 (error "CCL: Invalid number of arguments: %s" cmd
))
550 (ccl-compile-branch-blocks 'branch
551 (ccl-compile-branch-expression (nth 1 cmd
) cmd
)
554 (defun ccl-compile-read-branch (cmd)
555 "Compile READ statement of the form `(read-branch EXPR BLOCK0 BLOCK1 ...)'."
556 (if (< (length cmd
) 3)
557 (error "CCL: Invalid number of arguments: %s" cmd
))
558 (ccl-compile-branch-blocks 'read-branch
559 (ccl-compile-branch-expression (nth 1 cmd
) cmd
)
562 (defun ccl-compile-branch-expression (expr cmd
)
563 "Compile EXPRESSION part of BRANCH statement and return register
564 which holds a value of the expression."
566 ;; EXPR has the form `(EXPR2 OP ARG)'. Compile it as SET
567 ;; statement of the form `(r7 = (EXPR2 OP ARG))'.
569 (ccl-compile-expression 'r7 expr
)
571 (ccl-check-register expr cmd
)))
573 (defun ccl-compile-branch-blocks (code rrr blocks
)
574 "Compile BLOCKs of BRANCH statement. CODE is 'branch or 'read-branch.
575 REG is a register which holds a value of EXPRESSION part. BLOCKs
576 is a list of CCL-BLOCKs."
577 (let ((branches (length blocks
))
579 jump-table-head-address
582 block-unconditional-jump
)
583 (ccl-embed-code code rrr branches
)
584 (setq jump-table-head-address ccl-current-ic
)
585 ;; The size of jump table is the number of blocks plus 1 (for the
586 ;; case RRR is out of range).
587 (ccl-increment-ic (1+ branches
))
588 (setq empty-block-indexes
(list branches
))
589 ;; Compile each block.
592 (if (null (car blocks
))
593 ;; This block is empty.
594 (setq empty-block-indexes
(cons branch-idx empty-block-indexes
)
595 block-unconditional-jump t
)
596 ;; This block is not empty.
597 (ccl-embed-data (- ccl-current-ic jump-table-head-address
)
598 (+ jump-table-head-address branch-idx
))
599 (setq block-unconditional-jump
(ccl-compile-1 (car blocks
)))
600 (if (not block-unconditional-jump
)
602 ;; Jump address of the end of branches are embedded later.
603 ;; For the moment, just remember where to embed them.
604 (setq block-tail-addresses
605 (cons ccl-current-ic block-tail-addresses
))
606 (ccl-embed-code 'jump
0 0))))
607 (setq branch-idx
(1+ branch-idx
))
608 (setq blocks
(cdr blocks
)))
609 (if (not block-unconditional-jump
)
610 ;; We don't need jump code at the end of the last block.
611 (setq block-tail-addresses
(cdr block-tail-addresses
)
612 ccl-current-ic
(1- ccl-current-ic
)))
613 ;; Embed jump address at the tailing jump commands of blocks.
614 (while block-tail-addresses
615 (ccl-embed-current-address (car block-tail-addresses
))
616 (setq block-tail-addresses
(cdr block-tail-addresses
)))
617 ;; For empty blocks, make entries in the jump table point directly here.
618 (while empty-block-indexes
619 (ccl-embed-data (- ccl-current-ic jump-table-head-address
)
620 (+ jump-table-head-address
(car empty-block-indexes
)))
621 (setq empty-block-indexes
(cdr empty-block-indexes
))))
622 ;; Branch command ends by unconditional jump if RRR is out of range.
625 (defun ccl-compile-loop (cmd)
626 "Compile LOOP statement."
627 (if (< (length cmd
) 2)
628 (error "CCL: Invalid number of arguments: %s" cmd
))
629 (let* ((ccl-loop-head ccl-current-ic
)
635 (setq unconditional-jump t
)
637 (setq unconditional-jump
638 (and (ccl-compile-1 (car cmd
)) unconditional-jump
))
639 (setq cmd
(cdr cmd
)))
642 ;; Embed jump address for break statements encountered in
645 (ccl-embed-current-address (car ccl-breaks
))
646 (setq ccl-breaks
(cdr ccl-breaks
))))
649 (defun ccl-compile-break (cmd)
650 "Compile BREAK statement."
651 (if (/= (length cmd
) 1)
652 (error "CCL: Invalid number of arguments: %s" cmd
))
653 (if (null ccl-loop-head
)
654 (error "CCL: No outer loop: %s" cmd
))
655 (setq ccl-breaks
(cons ccl-current-ic ccl-breaks
))
656 (ccl-embed-code 'jump
0 0)
659 (defun ccl-compile-repeat (cmd)
660 "Compile REPEAT statement."
661 (if (/= (length cmd
) 1)
662 (error "CCL: Invalid number of arguments: %s" cmd
))
663 (if (null ccl-loop-head
)
664 (error "CCL: No outer loop: %s" cmd
))
665 (ccl-embed-code 'jump
0 ccl-loop-head
)
668 (defun ccl-compile-write-repeat (cmd)
669 "Compile WRITE-REPEAT statement."
670 (if (/= (length cmd
) 2)
671 (error "CCL: Invalid number of arguments: %s" cmd
))
672 (if (null ccl-loop-head
)
673 (error "CCL: No outer loop: %s" cmd
))
674 (let ((arg (nth 1 cmd
)))
675 (cond ((integerp arg
)
676 (ccl-embed-code 'write-const-jump
0 ccl-loop-head
)
677 (ccl-embed-data arg
))
679 (let ((len (length arg
))
681 (ccl-embed-code 'write-string-jump
0 ccl-loop-head
)
683 (ccl-embed-string len arg
)))
685 (ccl-check-register arg cmd
)
686 (ccl-embed-code 'write-register-jump arg ccl-loop-head
))))
689 (defun ccl-compile-write-read-repeat (cmd)
690 "Compile WRITE-READ-REPEAT statement."
691 (if (or (< (length cmd
) 2) (> (length cmd
) 3))
692 (error "CCL: Invalid number of arguments: %s" cmd
))
693 (if (null ccl-loop-head
)
694 (error "CCL: No outer loop: %s" cmd
))
695 (let ((rrr (ccl-check-register (nth 1 cmd
) cmd
))
698 (ccl-embed-code 'write-register-read-jump rrr ccl-loop-head
))
700 (ccl-embed-code 'write-const-read-jump rrr arg ccl-loop-head
))
702 (let ((len (length arg
))
704 (ccl-embed-code 'write-array-read-jump rrr ccl-loop-head
)
707 (ccl-embed-data (aref arg i
))
710 (error "CCL: Invalid argument %s: %s" arg cmd
)))
711 (ccl-embed-code 'read-jump rrr ccl-loop-head
))
714 (defun ccl-compile-read (cmd)
715 "Compile READ statement."
716 (if (< (length cmd
) 2)
717 (error "CCL: Invalid number of arguments: %s" cmd
))
718 (let* ((args (cdr cmd
))
719 (i (1- (length args
))))
721 (let ((rrr (ccl-check-register (car args
) cmd
)))
722 (ccl-embed-code 'read-register rrr i
)
723 (setq args
(cdr args
) i
(1- i
)))))
726 (defun ccl-compile-read-if (cmd)
727 "Compile READ-IF statement."
728 (ccl-compile-if cmd
'read
))
730 (defun ccl-compile-write (cmd)
731 "Compile WRITE statement."
732 (if (< (length cmd
) 2)
733 (error "CCL: Invalid number of arguments: %s" cmd
))
734 (let ((rrr (nth 1 cmd
)))
735 (cond ((integerp rrr
)
737 (ccl-compile-write-string (string rrr
))
738 (ccl-embed-code 'write-const-string
0 rrr
)))
740 (ccl-compile-write-string rrr
))
741 ((and (symbolp rrr
) (vectorp (nth 2 cmd
)))
742 (ccl-check-register rrr cmd
)
743 ;; CMD has the form `(write REG ARRAY)'.
744 (let* ((arg (nth 2 cmd
))
747 (ccl-embed-code 'write-array rrr len
)
749 (if (not (integerp (aref arg i
)))
750 (error "CCL: Invalid argument %s: %s" arg cmd
))
751 (ccl-embed-data (aref arg i
))
755 ;; CMD has the form `(write REG ...)'.
756 (let* ((args (cdr cmd
))
757 (i (1- (length args
))))
759 (setq rrr
(ccl-check-register (car args
) cmd
))
760 (ccl-embed-code 'write-register rrr i
)
761 (setq args
(cdr args
) i
(1- i
)))))
764 ;; CMD has the form `(write (LEFT OP RIGHT))'.
765 (let ((left (car rrr
))
766 (op (get (nth 1 rrr
) 'ccl-arith-code
))
770 ;; RRR has the form `((EXPR OP2 ARG) OP RIGHT)'.
771 ;; Compile the first term as `(r7 = (EXPR OP2 ARG))'.
772 (ccl-compile-expression 'r7 left
)
774 ;; Now RRR has the form `(ARG OP RIGHT)'.
777 (ccl-embed-code 'write-expr-const
0 (ash op
3) left
)
778 (ccl-embed-data right
))
779 (ccl-check-register right rrr
)
780 (ccl-embed-code 'write-expr-register
0
782 (get right
'ccl-register-number
))
786 (error "CCL: Invalid argument: %s" cmd
))))
789 (defun ccl-compile-call (cmd)
790 "Compile CALL statement."
791 (if (/= (length cmd
) 2)
792 (error "CCL: Invalid number of arguments: %s" cmd
))
793 (if (not (symbolp (nth 1 cmd
)))
794 (error "CCL: Subroutine should be a symbol: %s" cmd
))
795 (ccl-embed-code 'call
1 0)
796 (ccl-embed-symbol (nth 1 cmd
) 'ccl-program-idx
)
799 (defun ccl-compile-end (cmd)
800 "Compile END statement."
801 (if (/= (length cmd
) 1)
802 (error "CCL: Invalid number of arguments: %s" cmd
))
803 (ccl-embed-code 'end
0 0)
806 (defun ccl-compile-read-multibyte-character (cmd)
807 "Compile read-multibyte-character"
808 (if (/= (length cmd
) 3)
809 (error "CCL: Invalid number of arguments: %s" cmd
))
810 (let ((RRR (nth 1 cmd
))
812 (ccl-check-register rrr cmd
)
813 (ccl-check-register RRR cmd
)
814 (ccl-embed-extended-command 'read-multibyte-character rrr RRR
0))
817 (defun ccl-compile-write-multibyte-character (cmd)
818 "Compile write-multibyte-character"
819 (if (/= (length cmd
) 3)
820 (error "CCL: Invalid number of arguments: %s" cmd
))
821 (let ((RRR (nth 1 cmd
))
823 (ccl-check-register rrr cmd
)
824 (ccl-check-register RRR cmd
)
825 (ccl-embed-extended-command 'write-multibyte-character rrr RRR
0))
828 (defun ccl-compile-translate-character (cmd)
829 "Compile translate-character."
830 (if (/= (length cmd
) 4)
831 (error "CCL: Invalid number of arguments: %s" cmd
))
832 (let ((Rrr (nth 1 cmd
))
835 (ccl-check-register rrr cmd
)
836 (ccl-check-register RRR cmd
)
837 (cond ((and (symbolp Rrr
) (not (get Rrr
'ccl-register-number
)))
838 (ccl-embed-extended-command 'translate-character-const-tbl
840 (ccl-embed-symbol Rrr
'translation-table-id
))
842 (ccl-check-register Rrr cmd
)
843 (ccl-embed-extended-command 'translate-character rrr RRR Rrr
))))
846 (defun ccl-compile-lookup-integer (cmd)
847 "Compile lookup-integer."
848 (if (/= (length cmd
) 4)
849 (error "CCL: Invalid number of arguments: %s" cmd
))
850 (let ((Rrr (nth 1 cmd
))
853 (ccl-check-register RRR cmd
)
854 (ccl-check-register rrr cmd
)
855 (cond ((and (symbolp Rrr
) (not (get Rrr
'ccl-register-number
)))
856 (ccl-embed-extended-command 'lookup-int-const-tbl
858 (ccl-embed-symbol Rrr
'translation-hash-table-id
))
860 (error "CCL: non-constant table: %s" cmd
)
862 (ccl-check-register Rrr cmd
)
863 (ccl-embed-extended-command 'lookup-int rrr RRR
0))))
866 (defun ccl-compile-lookup-character (cmd)
867 "Compile lookup-character."
868 (if (/= (length cmd
) 4)
869 (error "CCL: Invalid number of arguments: %s" cmd
))
870 (let ((Rrr (nth 1 cmd
))
873 (ccl-check-register RRR cmd
)
874 (ccl-check-register rrr cmd
)
875 (cond ((and (symbolp Rrr
) (not (get Rrr
'ccl-register-number
)))
876 (ccl-embed-extended-command 'lookup-char-const-tbl
878 (ccl-embed-symbol Rrr
'translation-hash-table-id
))
880 (error "CCL: non-constant table: %s" cmd
)
882 (ccl-check-register Rrr cmd
)
883 (ccl-embed-extended-command 'lookup-char rrr RRR
0))))
886 (defun ccl-compile-iterate-multiple-map (cmd)
887 (ccl-compile-multiple-map-function 'iterate-multiple-map cmd
)
890 (defun ccl-compile-map-multiple (cmd)
891 (if (/= (length cmd
) 4)
892 (error "CCL: Invalid number of arguments: %s" cmd
))
896 (let ((len 0) result add
)
898 (if (consp (car arg
))
899 (setq add
(funcall func
(car arg
) t
)
900 result
(append result add
)
901 add
(+ (- (car add
)) 1))
909 (cons (- len
) result
)
911 (setq arg
(append (list (nth 0 cmd
) (nth 1 cmd
) (nth 2 cmd
))
912 (funcall func
(nth 3 cmd
) nil
)))
913 (ccl-compile-multiple-map-function 'map-multiple arg
))
916 (defun ccl-compile-map-single (cmd)
917 (if (/= (length cmd
) 4)
918 (error "CCL: Invalid number of arguments: %s" cmd
))
919 (let ((RRR (nth 1 cmd
))
923 (ccl-check-register rrr cmd
)
924 (ccl-check-register RRR cmd
)
925 (ccl-embed-extended-command 'map-single rrr RRR
0)
927 (if (get map
'code-conversion-map
)
928 (ccl-embed-symbol map
'code-conversion-map-id
)
929 (error "CCL: Invalid map: %s" map
)))
931 (error "CCL: Invalid type of arguments: %s" cmd
))))
934 (defun ccl-compile-multiple-map-function (command cmd
)
935 (if (< (length cmd
) 4)
936 (error "CCL: Invalid number of arguments: %s" cmd
))
937 (let ((RRR (nth 1 cmd
))
939 (args (nthcdr 3 cmd
))
941 (ccl-check-register rrr cmd
)
942 (ccl-check-register RRR cmd
)
943 (ccl-embed-extended-command command rrr RRR
0)
944 (ccl-embed-data (length args
))
946 (setq map
(car args
))
948 (if (get map
'code-conversion-map
)
949 (ccl-embed-symbol map
'code-conversion-map-id
)
950 (error "CCL: Invalid map: %s" map
)))
952 (ccl-embed-data map
))
954 (error "CCL: Invalid type of arguments: %s" cmd
)))
955 (setq args
(cdr args
)))))
963 (defun ccl-dump (ccl-code)
964 "Disassemble compiled CCL-CODE."
965 (let ((len (length ccl-code
))
966 (buffer-mag (aref ccl-code
0)))
967 (cond ((= buffer-mag
0)
968 (insert "Don't output anything.\n"))
970 (insert "Out-buffer must be as large as in-buffer.\n"))
973 (format "Out-buffer must be %d times bigger than in-buffer.\n"
975 (insert "Main-body:\n")
976 (setq ccl-current-ic
2)
977 (if (> (aref ccl-code
1) 0)
979 (while (< ccl-current-ic
(aref ccl-code
1))
981 (insert "At EOF:\n")))
982 (while (< ccl-current-ic len
)
986 (defun ccl-get-next-code ()
987 "Return a CCL code in `ccl-code' at `ccl-current-ic'."
989 (aref ccl-code ccl-current-ic
)
990 (setq ccl-current-ic
(1+ ccl-current-ic
))))
993 (let* ((code (ccl-get-next-code))
994 (cmd (aref ccl-code-table
(logand code
31)))
995 (rrr (ash (logand code
255) -
5))
997 (insert (format "%5d:[%s] " (1- ccl-current-ic
) cmd
))
998 (funcall (get cmd
'ccl-dump-function
) rrr cc
)))
1000 (defun ccl-dump-set-register (rrr cc
)
1001 (insert (format "r%d = r%d\n" rrr cc
)))
1003 (defun ccl-dump-set-short-const (rrr cc
)
1004 (insert (format "r%d = %d\n" rrr cc
)))
1006 (defun ccl-dump-set-const (rrr ignore
)
1007 (insert (format "r%d = %d\n" rrr
(ccl-get-next-code))))
1009 (defun ccl-dump-set-array (rrr cc
)
1010 (let ((rrr2 (logand cc
7))
1013 (insert (format "r%d = array[r%d] of length %d\n\t"
1016 (insert (format "%d " (ccl-get-next-code)))
1020 (defun ccl-dump-jump (ignore cc
&optional address
)
1021 (insert (format "jump to %d(" (+ (or address ccl-current-ic
) cc
)))
1024 (insert (format "%d)\n" (1+ cc
))))
1026 (defun ccl-dump-jump-cond (rrr cc
)
1027 (insert (format "if (r%d == 0), " rrr
))
1028 (ccl-dump-jump nil cc
))
1030 (defun ccl-dump-write-register-jump (rrr cc
)
1031 (insert (format "write r%d, " rrr
))
1032 (ccl-dump-jump nil cc
))
1034 (defun ccl-dump-write-register-read-jump (rrr cc
)
1035 (insert (format "write r%d, read r%d, " rrr rrr
))
1036 (ccl-dump-jump nil cc
)
1037 (ccl-get-next-code) ; Skip dummy READ-JUMP
1040 (defun ccl-extract-arith-op (cc)
1041 (aref ccl-arith-table
(ash cc -
6)))
1043 (defun ccl-dump-write-expr-const (ignore cc
)
1044 (insert (format "write (r%d %s %d)\n"
1046 (ccl-extract-arith-op cc
)
1047 (ccl-get-next-code))))
1049 (defun ccl-dump-write-expr-register (ignore cc
)
1050 (insert (format "write (r%d %s r%d)\n"
1052 (ccl-extract-arith-op cc
)
1053 (logand (ash cc -
3) 7))))
1055 (defun ccl-dump-insert-char (cc)
1056 (cond ((= cc ?
\t) (insert " \"^I\""))
1057 ((= cc ?
\n) (insert " \"^J\""))
1058 (t (insert (format " \"%c\"" cc
)))))
1060 (defun ccl-dump-write-const-jump (ignore cc
)
1061 (let ((address ccl-current-ic
))
1062 (insert "write char")
1063 (ccl-dump-insert-char (ccl-get-next-code))
1065 (ccl-dump-jump nil cc address
)))
1067 (defun ccl-dump-write-const-read-jump (rrr cc
)
1068 (let ((address ccl-current-ic
))
1069 (insert "write char")
1070 (ccl-dump-insert-char (ccl-get-next-code))
1071 (insert (format ", read r%d, " rrr
))
1072 (ccl-dump-jump cc address
)
1073 (ccl-get-next-code) ; Skip dummy READ-JUMP
1076 (defun ccl-dump-write-string-jump (ignore cc
)
1077 (let ((address ccl-current-ic
)
1078 (len (ccl-get-next-code))
1082 (let ((code (ccl-get-next-code)))
1083 (insert (ash code -
16))
1084 (if (< (1+ i
) len
) (insert (logand (ash code -
8) 255)))
1085 (if (< (+ i
2) len
) (insert (logand code
255))))
1088 (ccl-dump-jump nil cc address
)))
1090 (defun ccl-dump-write-array-read-jump (rrr cc
)
1091 (let ((address ccl-current-ic
)
1092 (len (ccl-get-next-code))
1094 (insert (format "write array[r%d] of length %d,\n\t" rrr len
))
1096 (ccl-dump-insert-char (ccl-get-next-code))
1098 (insert (format "\n\tthen read r%d, " rrr
))
1099 (ccl-dump-jump nil cc address
)
1100 (ccl-get-next-code) ; Skip dummy READ-JUMP.
1103 (defun ccl-dump-read-jump (rrr cc
)
1104 (insert (format "read r%d, " rrr
))
1105 (ccl-dump-jump nil cc
))
1107 (defun ccl-dump-branch (rrr len
)
1108 (let ((jump-table-head ccl-current-ic
)
1110 (insert (format "jump to array[r%d] of length %d\n\t" rrr len
))
1112 (insert (format "%d " (+ jump-table-head
(ccl-get-next-code))))
1116 (defun ccl-dump-read-register (rrr cc
)
1117 (insert (format "read r%d (%d remaining)\n" rrr cc
)))
1119 (defun ccl-dump-read-branch (rrr len
)
1120 (insert (format "read r%d, " rrr
))
1121 (ccl-dump-branch rrr len
))
1123 (defun ccl-dump-write-register (rrr cc
)
1124 (insert (format "write r%d (%d remaining)\n" rrr cc
)))
1126 (defun ccl-dump-call (ignore cc
)
1127 (let ((subroutine (car (ccl-get-next-code))))
1128 (insert (format "call subroutine `%s'\n" subroutine
))))
1130 (defun ccl-dump-write-const-string (rrr cc
)
1133 (insert "write char")
1134 (ccl-dump-insert-char cc
)
1140 (let ((code (ccl-get-next-code)))
1141 (if (/= (logand code
#x1000000
) 0)
1143 (insert (logand code
#xFFFFFF
))
1145 (insert (format "%c" (lsh code -
16)))
1147 (insert (format "%c" (logand (lsh code -
8) 255))))
1149 (insert (format "%c" (logand code
255))))
1153 (defun ccl-dump-write-array (rrr cc
)
1155 (insert (format "write array[r%d] of length %d\n\t" rrr cc
))
1157 (ccl-dump-insert-char (ccl-get-next-code))
1161 (defun ccl-dump-end (&rest ignore
)
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
)
1176 (defun ccl-dump-set-expr-const (rrr cc
)
1177 (insert (format "r%d = r%d %s %d\n"
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"
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
))
1218 (let ((code (aref ccl-code i
))
1221 (insert (if (= (logand code
(ash 1 j
)) 0) ?
0 ?
1))
1223 (setq code
(logand code
31))
1224 (if (< code
(length ccl-code-table
))
1225 (insert (format ":%s" (aref ccl-code-table code
))))
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-lookup-int-const-tbl (rrr RRR Rrr
)
1250 (let ((tbl (ccl-get-next-code)))
1251 (insert (format "hash table(%S) r%d r%d\n" tbl RRR rrr
))))
1253 (defun ccl-dump-lookup-char-const-tbl (rrr RRR Rrr
)
1254 (let ((tbl (ccl-get-next-code)))
1255 (insert (format "hash table(%S) r%d r%d\n" tbl RRR rrr
))))
1257 (defun ccl-dump-iterate-multiple-map (rrr RRR Rrr
)
1258 (let ((notbl (ccl-get-next-code))
1260 (insert (format "iterate-multiple-map r%d r%d\n" RRR rrr
))
1261 (insert (format "\tnumber of maps is %d .\n\t [" notbl
))
1263 (setq id
(ccl-get-next-code))
1264 (insert (format "%S" id
))
1268 (defun ccl-dump-map-multiple (rrr RRR Rrr
)
1269 (let ((notbl (ccl-get-next-code))
1271 (insert (format "map-multiple r%d r%d\n" RRR rrr
))
1272 (insert (format "\tnumber of maps and separators is %d\n\t [" notbl
))
1274 (setq id
(ccl-get-next-code))
1277 (insert (format "%S " id
)))
1281 (defun ccl-dump-map-single (rrr RRR Rrr
)
1282 (let ((id (ccl-get-next-code)))
1283 (insert (format "map-single r%d r%d map(%S)\n" RRR rrr id
))))
1286 ;; CCL emulation staffs
1288 ;; Not yet implemented.
1290 ;; Auto-loaded functions.
1293 (defmacro declare-ccl-program
(name &optional vector
)
1294 "Declare NAME as a name of CCL program.
1296 This macro exists for backward compatibility. In the old version of
1297 Emacs, to compile a CCL program which calls another CCL program not
1298 yet defined, it must be declared as a CCL program in advance. But,
1299 now CCL program names are resolved not at compile time but before
1302 Optional arg VECTOR is a compiled CCL code of the CCL program."
1303 `(put ',name
'ccl-program-idx
(register-ccl-program ',name
,vector
)))
1306 (defmacro define-ccl-program
(name ccl-program
&optional doc
)
1307 "Set NAME the compiled code of CCL-PROGRAM.
1309 CCL-PROGRAM has this form:
1310 (BUFFER_MAGNIFICATION
1314 BUFFER_MAGNIFICATION is an integer value specifying the approximate
1315 output buffer magnification size compared with the bytes of input data
1316 text. It is assured that the actual output buffer has 256 bytes
1317 more than the size calculated by BUFFER_MAGNIFICATION.
1318 If the value is zero, the CCL program can't execute `read' and
1321 CCL_MAIN_CODE and CCL_EOF_CODE are CCL program codes. CCL_MAIN_CODE
1322 executed at first. If there's no more input data when `read' command
1323 is executed in CCL_MAIN_CODE, CCL_EOF_CODE is executed. If
1324 CCL_MAIN_CODE is terminated, CCL_EOF_CODE is not executed.
1326 Here's the syntax of CCL program code in BNF notation. The lines
1327 starting by two semicolons (and optional leading spaces) describe the
1330 CCL_MAIN_CODE := CCL_BLOCK
1332 CCL_EOF_CODE := CCL_BLOCK
1334 CCL_BLOCK := STATEMENT | (STATEMENT [STATEMENT ...])
1337 SET | IF | BRANCH | LOOP | REPEAT | BREAK | READ | WRITE | CALL
1338 | TRANSLATE | MAP | LOOKUP | END
1340 SET := (REG = EXPRESSION)
1341 | (REG ASSIGNMENT_OPERATOR EXPRESSION)
1342 ;; The following form is the same as (r0 = integer).
1345 EXPRESSION := ARG | (EXPRESSION OPERATOR ARG)
1347 ;; Evaluate EXPRESSION. If the result is nonzero, execute
1348 ;; CCL_BLOCK_0. Otherwise, execute CCL_BLOCK_1.
1349 IF := (if EXPRESSION CCL_BLOCK_0 CCL_BLOCK_1)
1351 ;; Evaluate EXPRESSION. Provided that the result is N, execute
1353 BRANCH := (branch EXPRESSION CCL_BLOCK_0 [CCL_BLOCK_1 ...])
1355 ;; Execute STATEMENTs until (break) or (end) is executed.
1356 LOOP := (loop STATEMENT [STATEMENT ...])
1358 ;; Terminate the most inner loop.
1362 ;; Jump to the head of the most inner loop.
1364 ;; Same as: ((write [REG | integer | string])
1366 | (write-repeat [REG | integer | string])
1367 ;; Same as: ((write REG [ARRAY])
1370 | (write-read-repeat REG [ARRAY])
1371 ;; Same as: ((write integer)
1374 | (write-read-repeat REG integer)
1376 READ := ;; Set REG_0 to a byte read from the input text, set REG_1
1377 ;; to the next byte read, and so on.
1378 (read REG_0 [REG_1 ...])
1379 ;; Same as: ((read REG)
1380 ;; (if (REG OPERATOR ARG) CCL_BLOCK_0 CCL_BLOCK_1))
1381 | (read-if (REG OPERATOR ARG) CCL_BLOCK_0 CCL_BLOCK_1)
1382 ;; Same as: ((read REG)
1383 ;; (branch REG CCL_BLOCK_0 [CCL_BLOCK_1 ...]))
1384 | (read-branch REG CCL_BLOCK_0 [CCL_BLOCK_1 ...])
1385 ;; Read a character from the input text while parsing
1386 ;; multibyte representation, set REG_0 to the charset ID of
1387 ;; the character, set REG_1 to the code point of the
1388 ;; character. If the dimension of charset is two, set REG_1
1389 ;; to ((CODE0 << 7) | CODE1), where CODE0 is the first code
1390 ;; point and CODE1 is the second code point.
1391 | (read-multibyte-character REG_0 REG_1)
1394 ;; Write REG_0, REG_1, ... to the output buffer. If REG_N is
1395 ;; a multibyte character, write the corresponding multibyte
1397 (write REG_0 [REG_1 ...])
1398 ;; Same as: ((r7 = EXPRESSION)
1400 | (write EXPRESSION)
1401 ;; Write the value of `integer' to the output buffer. If it
1402 ;; is a multibyte character, write the corresponding multibyte
1405 ;; Write the byte sequence of `string' as is to the output
1408 ;; Same as: (write string)
1410 ;; Provided that the value of REG is N, write Nth element of
1411 ;; ARRAY to the output buffer. If it is a multibyte
1412 ;; character, write the corresponding multibyte
1415 ;; Write a multibyte representation of a character whose
1416 ;; charset ID is REG_0 and code point is REG_1. If the
1417 ;; dimension of the charset is two, REG_1 should be ((CODE0 <<
1418 ;; 7) | CODE1), where CODE0 is the first code point and CODE1
1419 ;; is the second code point of the character.
1420 | (write-multibyte-character REG_0 REG_1)
1422 ;; Call CCL program whose name is ccl-program-name.
1423 CALL := (call ccl-program-name)
1425 ;; Terminate the CCL program.
1428 ;; CCL registers that can contain any integer value. As r7 is also
1429 ;; used by CCL interpreter, its value is changed unexpectedly.
1430 REG := r0 | r1 | r2 | r3 | r4 | r5 | r6 | r7
1432 ARG := REG | integer
1435 ;; Normal arithmethic operators (same meaning as C code).
1438 ;; Bitwize operators (same meaning as C code)
1441 ;; Shifting operators (same meaning as C code)
1444 ;; (REG = ARG_0 <8 ARG_1) means:
1445 ;; (REG = ((ARG_0 << 8) | ARG_1))
1448 ;; (REG = ARG_0 >8 ARG_1) means:
1449 ;; ((REG = (ARG_0 >> 8))
1450 ;; (r7 = (ARG_0 & 255)))
1453 ;; (REG = ARG_0 // ARG_1) means:
1454 ;; ((REG = (ARG_0 / ARG_1))
1455 ;; (r7 = (ARG_0 % ARG_1)))
1458 ;; Normal comparing operators (same meaning as C code)
1459 | < | > | == | <= | >= | !=
1461 ;; If ARG_0 and ARG_1 are higher and lower byte of Shift-JIS
1462 ;; code, and CHAR is the corresponding JISX0208 character,
1463 ;; (REG = ARG_0 de-sjis ARG_1) means:
1466 ;; where CODE0 is the first code point of CHAR, CODE1 is the
1467 ;; second code point of CHAR.
1470 ;; If ARG_0 and ARG_1 are the first and second code point of
1471 ;; JISX0208 character CHAR, and SJIS is the correponding
1473 ;; (REG = ARG_0 en-sjis ARG_1) means:
1476 ;; where HIGH is the higher byte of SJIS, LOW is the lower
1480 ASSIGNMENT_OPERATOR :=
1481 ;; Same meaning as C code
1482 += | -= | *= | /= | %= | &= | `|=' | ^= | <<= | >>=
1484 ;; (REG <8= ARG) is the same as:
1489 ;; (REG >8= ARG) is the same as:
1490 ;; ((r7 = (REG & 255))
1493 ;; (REG //= ARG) is the same as:
1494 ;; ((r7 = (REG % ARG))
1498 ARRAY := `[' integer ... `]'
1502 (translate-character REG(table) REG(charset) REG(codepoint))
1503 | (translate-character SYMBOL REG(charset) REG(codepoint))
1504 ;; SYMBOL must refer to a table defined by `define-translation-table'.
1506 (lookup-character SYMBOL REG(charset) REG(codepoint))
1507 | (lookup-integer SYMBOL REG(integer))
1508 ;; SYMBOL refers to a table defined by `define-translation-hash-table'.
1510 (iterate-multiple-map REG REG MAP-IDs)
1511 | (map-multiple REG REG (MAP-SET))
1512 | (map-single REG REG MAP-ID)
1513 MAP-IDs := MAP-ID ...
1514 MAP-SET := MAP-IDs | (MAP-IDs) MAP-SET
1517 (declare (doc-string 3))
1518 `(let ((prog ,(unwind-protect
1520 ;; To make ,(charset-id CHARSET) works well.
1521 (fset 'charset-id
'charset-id-internal
)
1522 (ccl-compile (eval ccl-program
)))
1523 (fmakunbound 'charset-id
))))
1524 (defconst ,name prog
,doc
)
1525 (put ',name
'ccl-program-idx
(register-ccl-program ',name prog
))
1529 (defmacro check-ccl-program
(ccl-program &optional name
)
1530 "Check validity of CCL-PROGRAM.
1531 If CCL-PROGRAM is a symbol denoting a CCL program, return
1532 CCL-PROGRAM, else return nil.
1533 If CCL-PROGRAM is a vector and optional arg NAME (symbol) is supplied,
1534 register CCL-PROGRAM by name NAME, and return NAME."
1535 `(if (ccl-program-p ,ccl-program
)
1536 (if (vectorp ,ccl-program
)
1538 (register-ccl-program ,name
,ccl-program
)
1543 (defun ccl-execute-with-args (ccl-prog &rest args
)
1544 "Execute CCL-PROGRAM with registers initialized by the remaining args.
1545 The return value is a vector of resulting CCL registers.
1547 See the documentation of `define-ccl-program' for the detail of CCL program."
1548 (let ((reg (make-vector 8 0))
1550 (while (and args
(< i
8))
1551 (if (not (integerp (car args
)))
1552 (error "Arguments should be integer"))
1553 (aset reg i
(car args
))
1554 (setq args
(cdr args
) i
(1+ i
)))
1555 (ccl-execute ccl-prog reg
)
1560 ;; arch-tag: 836bcd27-63a1-4a56-b232-1145ecf823fb
1561 ;;; ccl.el ends here