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 X 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)
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 ;; Synched up with: FSF 20.2
29 ;; CCL (Code Conversion Language) is a simple programming language to
30 ;; be used for various kind of code conversion. CCL program is
31 ;; compiled to CCL code (vector of integers) and executed by CCL
32 ;; interpreter of Emacs.
34 ;; CCL is used for code conversion at process I/O and file I/O for
35 ;; non-standard coding-system. In addition, it is used for
36 ;; calculating a code point of X's font from a character code.
37 ;; However, since CCL is designed as a powerful programming language,
38 ;; it can be used for more generic calculation. For instance,
39 ;; combination of three or more arithmetic operations can be
40 ;; calculated faster than Emacs Lisp.
42 ;; Here's the syntax of CCL program in BNF notation.
45 ;; (BUFFER_MAGNIFICATION
49 ;; BUFFER_MAGNIFICATION := integer
50 ;; CCL_MAIN_BLOCK := CCL_BLOCK
51 ;; CCL_EOF_BLOCK := CCL_BLOCK
54 ;; STATEMENT | (STATEMENT [STATEMENT ...])
56 ;; SET | IF | BRANCH | LOOP | REPEAT | BREAK | READ | WRITE | CALL
60 ;; | (REG ASSIGNMENT_OPERATOR EXPRESSION)
63 ;; EXPRESSION := ARG | (EXPRESSION OPERATOR ARG)
65 ;; IF := (if EXPRESSION CCL_BLOCK CCL_BLOCK)
66 ;; BRANCH := (branch EXPRESSION CCL_BLOCK [CCL_BLOCK ...])
67 ;; LOOP := (loop STATEMENT [STATEMENT ...])
71 ;; | (write-repeat [REG | integer | string])
72 ;; | (write-read-repeat REG [integer | ARRAY])
75 ;; | (read-if (REG OPERATOR ARG) CCL_BLOCK CCL_BLOCK)
76 ;; | (read-branch REG CCL_BLOCK [CCL_BLOCK ...])
77 ;; | (read-multibyte-character REG {charset} REG {code-point})
80 ;; | (write EXPRESSION)
81 ;; | (write integer) | (write string) | (write REG ARRAY)
83 ;; | (write-multibyte-character REG(charset) REG(codepoint))
84 ;; CALL := (call ccl-program-name)
87 ;; REG := r0 | r1 | r2 | r3 | r4 | r5 | r6 | r7
88 ;; ARG := REG | integer
90 ;; + | - | * | / | % | & | '|' | ^ | << | >> | <8 | >8 | //
91 ;; | < | > | == | <= | >= | != | de-sjis | en-sjis
92 ;; ASSIGNMENT_OPERATOR :=
93 ;; += | -= | *= | /= | %= | &= | '|=' | ^= | <<= | >>=
94 ;; ARRAY := '[' integer ... ']'
98 (defconst ccl-command-table
99 [if branch loop break repeat write-repeat write-read-repeat
100 read read-if read-branch write call end
101 read-multibyte-character write-multibyte-character]
102 "Vector of CCL commands (symbols).")
104 ;; Put a property to each symbol of CCL commands for the compiler.
105 (let (op (i 0) (len (length ccl-command-table)))
107 (setq op (aref ccl-command-table i))
108 (put op 'ccl-compile-function (intern (format "ccl-compile-%s" op)))
111 (defconst ccl-code-table
119 write-register-read-jump
121 write-const-read-jump
123 write-array-read-jump
135 set-assign-expr-const
136 set-assign-expr-register
140 jump-cond-expr-register
141 read-jump-cond-expr-const
142 read-jump-cond-expr-register
145 "Vector of CCL compiled codes (symbols).")
147 (defconst ccl-extended-code-table
148 [read-multibyte-character
149 write-multibyte-character
151 translate-character-const-tbl
152 nil nil nil nil nil nil nil nil nil nil nil nil ; 0x04-0x0f
157 "Vector of CCL extended compiled codes (symbols).")
159 ;; Put a property to each symbol of CCL codes for the disassembler.
160 (let (code (i 0) (len (length ccl-code-table)))
162 (setq code (aref ccl-code-table i))
163 (put code 'ccl-code i)
164 (put code 'ccl-dump-function (intern (format "ccl-dump-%s" code)))
167 (let (code (i 0) (len (length ccl-extended-code-table)))
169 (setq code (aref ccl-extended-code-table i))
172 (put code 'ccl-ex-code i)
173 (put code 'ccl-dump-function (intern (format "ccl-dump-%s" code)))))
176 (defconst ccl-jump-code-list
177 '(jump jump-cond write-register-jump write-register-read-jump
178 write-const-jump write-const-read-jump write-string-jump
179 write-array-read-jump read-jump))
181 ;; Put a property `jump-flag' to each CCL code which execute jump in
183 (let ((l ccl-jump-code-list))
185 (put (car l) 'jump-flag t)
188 (defconst ccl-register-table
189 [r0 r1 r2 r3 r4 r5 r6 r7]
190 "Vector of CCL registers (symbols).")
192 ;; Put a property to indicate register number to each symbol of CCL.
194 (let (reg (i 0) (len (length ccl-register-table)))
196 (setq reg (aref ccl-register-table i))
197 (put reg 'ccl-register-number i)
200 (defconst ccl-arith-table
201 [+ - * / % & | ^ << >> <8 >8 // nil nil nil
202 < > == <= >= != de-sjis en-sjis]
203 "Vector of CCL arithmetic/logical operators (symbols).")
205 ;; Put a property to each symbol of CCL operators for the compiler.
206 (let (arith (i 0) (len (length ccl-arith-table)))
208 (setq arith (aref ccl-arith-table i))
209 (if arith (put arith 'ccl-arith-code i))
212 (defconst ccl-assign-arith-table
213 [+= -= *= /= %= &= |= ^= <<= >>= <8= >8= //=]
214 "Vector of CCL assignment operators (symbols).")
216 ;; Put a property to each symbol of CCL assignment operators for the compiler.
217 (let (arith (i 0) (len (length ccl-assign-arith-table)))
219 (setq arith (aref ccl-assign-arith-table i))
220 (put arith 'ccl-self-arith-code i)
223 (defvar ccl-program-vector nil
224 "Working vector of CCL codes produced by CCL compiler.")
225 (defvar ccl-current-ic 0
226 "The current index for `ccl-program-vector'.")
228 ;; Embed integer DATA in `ccl-program-vector' at `ccl-current-ic' and
229 ;; increment it. If IC is specified, embed DATA at IC.
230 (defun ccl-embed-data (data &optional ic)
231 (let ((val (if (characterp data) (char-int data) data)))
233 (aset ccl-program-vector ic val)
234 (aset ccl-program-vector ccl-current-ic val)
235 (setq ccl-current-ic (1+ ccl-current-ic)))))
237 ;; Embed string STR of length LEN in `ccl-program-vector' at
239 (defun ccl-embed-string (len str)
242 (ccl-embed-data (logior (ash (aref str i) 16)
244 (ash (aref str (1+ i)) 8)
251 ;; Embed a relative jump address to `ccl-current-ic' in
252 ;; `ccl-program-vector' at IC without altering the other bit field.
253 (defun ccl-embed-current-address (ic)
254 (let ((relative (- ccl-current-ic (1+ ic))))
255 (aset ccl-program-vector ic
256 (logior (aref ccl-program-vector ic) (ash relative 8)))))
258 ;; Embed CCL code for the operation OP and arguments REG and DATA in
259 ;; `ccl-program-vector' at `ccl-current-ic' in the following format.
260 ;; |----------------- integer (28-bit) ------------------|
261 ;; |------------ 20-bit ------------|- 3-bit --|- 5-bit -|
262 ;; |------------- DATA -------------|-- REG ---|-- OP ---|
263 ;; If REG2 is specified, embed a code in the following format.
264 ;; |------- 17-bit ------|- 3-bit --|- 3-bit --|- 5-bit -|
265 ;; |-------- DATA -------|-- REG2 --|-- REG ---|-- OP ---|
267 ;; If REG is a CCL register symbol (e.g. r0, r1...), the register
268 ;; number is embedded. If OP is one of unconditional jumps, DATA is
269 ;; changed to an relative jump address.
271 (defun ccl-embed-code (op reg data &optional reg2)
272 (if (and (> data 0) (get op 'jump-flag))
273 ;; DATA is an absolute jump address. Make it relative to the
274 ;; next of jump code.
275 (setq data (- data (1+ ccl-current-ic))))
276 (let ((code (logior (get op 'ccl-code)
278 (if (symbolp reg) (get reg 'ccl-register-number) reg) 5)
280 (logior (ash (get reg2 'ccl-register-number) 8)
283 (aset ccl-program-vector ccl-current-ic code)
284 (setq ccl-current-ic (1+ ccl-current-ic))))
286 ;; extended ccl command format
287 ;; |- 14-bit -|- 3-bit --|- 3-bit --|- 3-bit --|- 5-bit -|
288 ;; |- EX-OP --|-- REG3 --|-- REG2 --|-- REG ---|-- OP ---|
289 (defun ccl-embed-extended-command (ex-op reg reg2 reg3)
290 (let ((data (logior (ash (get ex-op 'ccl-ex-code) 3)
292 (get reg3 'ccl-register-number)
294 (ccl-embed-code 'ex-cmd reg data reg2)))
296 ;; Just advance `ccl-current-ic' by INC.
297 (defun ccl-increment-ic (inc)
298 (setq ccl-current-ic (+ ccl-current-ic inc)))
301 (defun ccl-program-p (obj)
302 "Return t if OBJECT is a valid CCL compiled code."
304 (let ((i 0) (len (length obj)) (flag t))
307 (while (and flag (< i len))
308 (setq flag (integerp (aref obj i)))
312 ;; If non-nil, index of the start of the current loop.
313 (defvar ccl-loop-head nil)
314 ;; If non-nil, list of absolute addresses of the breaking points of
316 (defvar ccl-breaks nil)
319 (defun ccl-compile (ccl-program)
320 "Return a compiled code of CCL-PROGRAM as a vector of integer."
321 (if (or (null (consp ccl-program))
322 (null (integer-or-char-p (car ccl-program)))
323 (null (listp (car (cdr ccl-program)))))
324 (error "CCL: Invalid CCL program: %s" ccl-program))
325 (if (null (vectorp ccl-program-vector))
326 (setq ccl-program-vector (make-vector 8192 0)))
327 (setq ccl-loop-head nil ccl-breaks nil)
328 (setq ccl-current-ic 0)
330 ;; The first element is the buffer magnification.
331 (ccl-embed-data (car ccl-program))
333 ;; The second element is the address of the start CCL code for
334 ;; processing end of input buffer (we call it eof-processor). We
338 ;; Compile the main body of the CCL program.
339 (ccl-compile-1 (car (cdr ccl-program)))
341 ;; Embed the address of eof-processor.
342 (ccl-embed-data ccl-current-ic 1)
344 ;; Then compile eof-processor.
345 (if (nth 2 ccl-program)
346 (ccl-compile-1 (nth 2 ccl-program)))
348 ;; At last, embed termination code.
349 (ccl-embed-code 'end 0 0)
351 (let ((vec (make-vector ccl-current-ic 0))
353 (while (< i ccl-current-ic)
354 (aset vec i (aref ccl-program-vector i))
358 ;; Signal syntax error.
359 (defun ccl-syntax-error (cmd)
360 (error "CCL: Syntax error: %s" cmd))
362 ;; Check if ARG is a valid CCL register.
363 (defun ccl-check-register (arg cmd)
364 (if (get arg 'ccl-register-number)
366 (error "CCL: Invalid register %s in %s." arg cmd)))
368 ;; Check if ARG is a valid CCL command.
369 (defun ccl-check-compile-function (arg cmd)
370 (or (get arg 'ccl-compile-function)
371 (error "CCL: Invalid command: %s" cmd)))
373 ;; In the following code, most ccl-compile-XXXX functions return t if
374 ;; they end with unconditional jump, else return nil.
376 ;; Compile CCL-BLOCK (see the syntax above).
377 (defun ccl-compile-1 (ccl-block)
378 (let (unconditional-jump
380 (if (or (integer-or-char-p ccl-block)
382 (and ccl-block (symbolp (car ccl-block))))
383 ;; This block consists of single statement.
384 (setq ccl-block (list ccl-block)))
386 ;; Now CCL-BLOCK is a list of statements. Compile them one by
389 (setq cmd (car ccl-block))
390 (setq unconditional-jump
391 (cond ((integer-or-char-p cmd)
392 ;; SET statement for the register 0.
393 (ccl-compile-set (list 'r0 '= cmd)))
396 ;; WRITE statement of string argument.
397 (ccl-compile-write-string cmd))
400 ;; The other statements.
401 (cond ((eq (nth 1 cmd) '=)
402 ;; SET statement of the form `(REG = EXPRESSION)'.
403 (ccl-compile-set cmd))
405 ((and (symbolp (nth 1 cmd))
406 (get (nth 1 cmd) 'ccl-self-arith-code))
407 ;; SET statement with an assignment operation.
408 (ccl-compile-self-set cmd))
411 (funcall (ccl-check-compile-function (car cmd) cmd)
415 (ccl-syntax-error cmd))))
416 (setq ccl-block (cdr ccl-block)))
419 (defconst ccl-max-short-const (ash 1 19))
420 (defconst ccl-min-short-const (ash -1 19))
422 ;; Compile SET statement.
423 (defun ccl-compile-set (cmd)
424 (let ((rrr (ccl-check-register (car cmd) cmd))
427 ;; CMD has the form `(RRR = (XXX OP YYY))'.
428 (ccl-compile-expression rrr right))
430 ((integer-or-char-p right)
431 ;; CMD has the form `(RRR = integer)'.
432 (if (and (<= right ccl-max-short-const)
433 (>= right ccl-min-short-const))
434 (ccl-embed-code 'set-short-const rrr right)
435 (ccl-embed-code 'set-const rrr 0)
436 (ccl-embed-data right)))
439 ;; CMD has the form `(RRR = rrr [ array ])'.
440 (ccl-check-register right cmd)
441 (let ((ary (nth 3 cmd)))
443 (let ((i 0) (len (length ary)))
444 (ccl-embed-code 'set-array rrr len right)
446 (ccl-embed-data (aref ary i))
448 (ccl-embed-code 'set-register rrr 0 right))))))
451 ;; Compile SET statement with ASSIGNMENT_OPERATOR.
452 (defun ccl-compile-self-set (cmd)
453 (let ((rrr (ccl-check-register (car cmd) cmd))
456 ;; CMD has the form `(RRR ASSIGN_OP (XXX OP YYY))', compile
457 ;; the right hand part as `(r7 = (XXX OP YYY))' (note: the
458 ;; register 7 can be used for storing temporary value).
460 (ccl-compile-expression 'r7 right)
462 ;; Now CMD has the form `(RRR ASSIGN_OP ARG)'. Compile it as
463 ;; `(RRR = (RRR OP ARG))'.
464 (ccl-compile-expression
466 (list rrr (intern (substring (symbol-name (nth 1 cmd)) 0 -1)) right)))
469 ;; Compile SET statement of the form `(RRR = EXPR)'.
470 (defun ccl-compile-expression (rrr expr)
471 (let ((left (car expr))
472 (op (get (nth 1 expr) 'ccl-arith-code))
473 (right (nth 2 expr)))
476 ;; EXPR has the form `((EXPR2 OP2 ARG) OP RIGHT)'. Compile
477 ;; the first term as `(r7 = (EXPR2 OP2 ARG)).'
478 (ccl-compile-expression 'r7 left)
481 ;; Now EXPR has the form (LEFT OP RIGHT).
483 ;; Compile this SET statement as `(RRR OP= RIGHT)'.
484 (if (integer-or-char-p right)
486 (ccl-embed-code 'set-assign-expr-const rrr (ash op 3) 'r0)
487 (ccl-embed-data right))
488 (ccl-check-register right expr)
489 (ccl-embed-code 'set-assign-expr-register rrr (ash op 3) right))
491 ;; Compile this SET statement as `(RRR = (LEFT OP RIGHT))'.
492 (if (integer-or-char-p right)
494 (ccl-embed-code 'set-expr-const rrr (ash op 3) left)
495 (ccl-embed-data right))
496 (ccl-check-register right expr)
497 (ccl-embed-code 'set-expr-register
499 (logior (ash op 3) (get right 'ccl-register-number))
502 ;; Compile WRITE statement with string argument.
503 (defun ccl-compile-write-string (str)
504 (let ((len (length str)))
505 (ccl-embed-code 'write-const-string 1 len)
506 (ccl-embed-string len str))
509 ;; Compile IF statement of the form `(if CONDITION TRUE-PART FALSE-PART)'.
510 ;; If READ-FLAG is non-nil, this statement has the form
511 ;; `(read-if (REG OPERATOR ARG) TRUE-PART FALSE-PART)'.
512 (defun ccl-compile-if (cmd &optional read-flag)
513 (if (and (/= (length cmd) 3) (/= (length cmd) 4))
514 (error "CCL: Invalid number of arguments: %s" cmd))
515 (let ((condition (nth 1 cmd))
516 (true-cmds (nth 2 cmd))
517 (false-cmds (nth 3 cmd))
520 (if (and (listp condition)
521 (listp (car condition)))
522 ;; If CONDITION is a nested expression, the inner expression
523 ;; should be compiled at first as SET statement, i.e.:
524 ;; `(if ((X OP2 Y) OP Z) ...)' is compiled into two statements:
525 ;; `(r7 = (X OP2 Y)) (if (r7 OP Z) ...)'.
527 (ccl-compile-expression 'r7 (car condition))
528 (setq condition (cons 'r7 (cdr condition)))
529 (setq cmd (cons (car cmd)
530 (cons condition (cdr (cdr cmd)))))))
532 (setq jump-cond-address ccl-current-ic)
533 ;; Compile CONDITION.
534 (if (symbolp condition)
535 ;; CONDITION is a register.
537 (ccl-check-register condition cmd)
538 (ccl-embed-code 'jump-cond condition 0))
539 ;; CONDITION is a simple expression of the form (RRR OP ARG).
540 (let ((rrr (car condition))
541 (op (get (nth 1 condition) 'ccl-arith-code))
542 (arg (nth 2 condition)))
543 (ccl-check-register rrr cmd)
544 (if (integer-or-char-p arg)
546 (ccl-embed-code (if read-flag 'read-jump-cond-expr-const
547 'jump-cond-expr-const)
550 (ccl-embed-data arg))
551 (ccl-check-register arg cmd)
552 (ccl-embed-code (if read-flag 'read-jump-cond-expr-register
553 'jump-cond-expr-register)
556 (ccl-embed-data (get arg 'ccl-register-number)))))
558 ;; Compile TRUE-PART.
559 (let ((unconditional-jump (ccl-compile-1 true-cmds)))
560 (if (null false-cmds)
561 ;; This is the place to jump to if condition is false.
563 (ccl-embed-current-address jump-cond-address)
564 (setq unconditional-jump nil))
565 (let (end-true-part-address)
566 (if (not unconditional-jump)
568 ;; If TRUE-PART does not end with unconditional jump, we
569 ;; have to jump to the end of FALSE-PART from here.
570 (setq end-true-part-address ccl-current-ic)
571 (ccl-embed-code 'jump 0 0)))
572 ;; This is the place to jump to if CONDITION is false.
573 (ccl-embed-current-address jump-cond-address)
574 ;; Compile FALSE-PART.
575 (setq unconditional-jump
576 (and (ccl-compile-1 false-cmds) unconditional-jump))
577 (if end-true-part-address
578 ;; This is the place to jump to after the end of TRUE-PART.
579 (ccl-embed-current-address end-true-part-address))))
580 unconditional-jump)))
582 ;; Compile BRANCH statement.
583 (defun ccl-compile-branch (cmd)
584 (if (< (length cmd) 3)
585 (error "CCL: Invalid number of arguments: %s" cmd))
586 (ccl-compile-branch-blocks 'branch
587 (ccl-compile-branch-expression (nth 1 cmd) cmd)
590 ;; Compile READ statement of the form `(read-branch EXPR BLOCK0 BLOCK1 ...)'.
591 (defun ccl-compile-read-branch (cmd)
592 (if (< (length cmd) 3)
593 (error "CCL: Invalid number of arguments: %s" cmd))
594 (ccl-compile-branch-blocks 'read-branch
595 (ccl-compile-branch-expression (nth 1 cmd) cmd)
598 ;; Compile EXPRESSION part of BRANCH statement and return register
599 ;; which holds a value of the expression.
600 (defun ccl-compile-branch-expression (expr cmd)
602 ;; EXPR has the form `(EXPR2 OP ARG)'. Compile it as SET
603 ;; statement of the form `(r7 = (EXPR2 OP ARG))'.
605 (ccl-compile-expression 'r7 expr)
607 (ccl-check-register expr cmd)))
609 ;; Compile BLOCKs of BRANCH statement. CODE is 'branch or 'read-branch.
610 ;; REG is a register which holds a value of EXPRESSION part. BLOCKs
611 ;; is a list of CCL-BLOCKs.
612 (defun ccl-compile-branch-blocks (code rrr blocks)
613 (let ((branches (length blocks))
615 jump-table-head-address
618 block-unconditional-jump)
619 (ccl-embed-code code rrr branches)
620 (setq jump-table-head-address ccl-current-ic)
621 ;; The size of jump table is the number of blocks plus 1 (for the
622 ;; case RRR is out of range).
623 (ccl-increment-ic (1+ branches))
624 (setq empty-block-indexes (list branches))
625 ;; Compile each block.
628 (if (null (car blocks))
629 ;; This block is empty.
630 (setq empty-block-indexes (cons branch-idx empty-block-indexes)
631 block-unconditional-jump t)
632 ;; This block is not empty.
633 (ccl-embed-data (- ccl-current-ic jump-table-head-address)
634 (+ jump-table-head-address branch-idx))
635 (setq block-unconditional-jump (ccl-compile-1 (car blocks)))
636 (if (not block-unconditional-jump)
638 ;; Jump address of the end of branches are embedded later.
639 ;; For the moment, just remember where to embed them.
640 (setq block-tail-addresses
641 (cons ccl-current-ic block-tail-addresses))
642 (ccl-embed-code 'jump 0 0))))
643 (setq branch-idx (1+ branch-idx))
644 (setq blocks (cdr blocks)))
645 (if (not block-unconditional-jump)
646 ;; We don't need jump code at the end of the last block.
647 (setq block-tail-addresses (cdr block-tail-addresses)
648 ccl-current-ic (1- ccl-current-ic)))
649 ;; Embed jump address at the tailing jump commands of blocks.
650 (while block-tail-addresses
651 (ccl-embed-current-address (car block-tail-addresses))
652 (setq block-tail-addresses (cdr block-tail-addresses)))
653 ;; For empty blocks, make entries in the jump table point directly here.
654 (while empty-block-indexes
655 (ccl-embed-data (- ccl-current-ic jump-table-head-address)
656 (+ jump-table-head-address (car empty-block-indexes)))
657 (setq empty-block-indexes (cdr empty-block-indexes))))
658 ;; Branch command ends by unconditional jump if RRR is out of range.
661 ;; Compile LOOP statement.
662 (defun ccl-compile-loop (cmd)
663 (if (< (length cmd) 2)
664 (error "CCL: Invalid number of arguments: %s" cmd))
665 (let* ((ccl-loop-head ccl-current-ic)
671 (setq unconditional-jump t)
673 (setq unconditional-jump
674 (and (ccl-compile-1 (car cmd)) unconditional-jump))
675 (setq cmd (cdr cmd)))
678 ;; Embed jump address for break statements encountered in
681 (ccl-embed-current-address (car ccl-breaks))
682 (setq ccl-breaks (cdr ccl-breaks))))
685 ;; Compile BREAK statement.
686 (defun ccl-compile-break (cmd)
687 (if (/= (length cmd) 1)
688 (error "CCL: Invalid number of arguments: %s" cmd))
689 (if (null ccl-loop-head)
690 (error "CCL: No outer loop: %s" cmd))
691 (setq ccl-breaks (cons ccl-current-ic ccl-breaks))
692 (ccl-embed-code 'jump 0 0)
695 ;; Compile REPEAT statement.
696 (defun ccl-compile-repeat (cmd)
697 (if (/= (length cmd) 1)
698 (error "CCL: Invalid number of arguments: %s" cmd))
699 (if (null ccl-loop-head)
700 (error "CCL: No outer loop: %s" cmd))
701 (ccl-embed-code 'jump 0 ccl-loop-head)
704 ;; Compile WRITE-REPEAT statement.
705 (defun ccl-compile-write-repeat (cmd)
706 (if (/= (length cmd) 2)
707 (error "CCL: Invalid number of arguments: %s" cmd))
708 (if (null ccl-loop-head)
709 (error "CCL: No outer loop: %s" cmd))
710 (let ((arg (nth 1 cmd)))
711 (cond ((integer-or-char-p arg)
712 (ccl-embed-code 'write-const-jump 0 ccl-loop-head)
713 (ccl-embed-data arg))
715 (let ((len (length arg))
717 (ccl-embed-code 'write-string-jump 0 ccl-loop-head)
719 (ccl-embed-string len arg)))
721 (ccl-check-register arg cmd)
722 (ccl-embed-code 'write-register-jump arg ccl-loop-head))))
725 ;; Compile WRITE-READ-REPEAT statement.
726 (defun ccl-compile-write-read-repeat (cmd)
727 (if (or (< (length cmd) 2) (> (length cmd) 3))
728 (error "CCL: Invalid number of arguments: %s" cmd))
729 (if (null ccl-loop-head)
730 (error "CCL: No outer loop: %s" cmd))
731 (let ((rrr (ccl-check-register (nth 1 cmd) cmd))
734 (ccl-embed-code 'write-register-read-jump rrr ccl-loop-head))
735 ((integer-or-char-p arg)
736 (ccl-embed-code 'write-const-read-jump rrr arg ccl-loop-head))
738 (let ((len (length arg))
740 (ccl-embed-code 'write-array-read-jump rrr ccl-loop-head)
743 (ccl-embed-data (aref arg i))
746 (error "CCL: Invalid argument %s: %s" arg cmd)))
747 (ccl-embed-code 'read-jump rrr ccl-loop-head))
750 ;; Compile READ statement.
751 (defun ccl-compile-read (cmd)
752 (if (< (length cmd) 2)
753 (error "CCL: Invalid number of arguments: %s" cmd))
754 (let* ((args (cdr cmd))
755 (i (1- (length args))))
757 (let ((rrr (ccl-check-register (car args) cmd)))
758 (ccl-embed-code 'read-register rrr i)
759 (setq args (cdr args) i (1- i)))))
762 ;; Compile READ-IF statement.
763 (defun ccl-compile-read-if (cmd)
764 (ccl-compile-if cmd 'read))
766 ;; Compile WRITE statement.
767 (defun ccl-compile-write (cmd)
768 (if (< (length cmd) 2)
769 (error "CCL: Invalid number of arguments: %s" cmd))
770 (let ((rrr (nth 1 cmd)))
771 (cond ((integer-or-char-p rrr)
772 (ccl-embed-code 'write-const-string 0 rrr))
774 (ccl-compile-write-string rrr))
775 ((and (symbolp rrr) (vectorp (nth 2 cmd)))
776 (ccl-check-register rrr cmd)
777 ;; CMD has the form `(write REG ARRAY)'.
778 (let* ((arg (nth 2 cmd))
781 (ccl-embed-code 'write-array rrr len)
783 (if (not (integer-or-char-p (aref arg i)))
784 (error "CCL: Invalid argument %s: %s" arg cmd))
785 (ccl-embed-data (aref arg i))
789 ;; CMD has the form `(write REG ...)'.
790 (let* ((args (cdr cmd))
791 (i (1- (length args))))
793 (setq rrr (ccl-check-register (car args) cmd))
794 (ccl-embed-code 'write-register rrr i)
795 (setq args (cdr args) i (1- i)))))
798 ;; CMD has the form `(write (LEFT OP RIGHT))'.
799 (let ((left (car rrr))
800 (op (get (nth 1 rrr) 'ccl-arith-code))
804 ;; RRR has the form `((EXPR OP2 ARG) OP RIGHT)'.
805 ;; Compile the first term as `(r7 = (EXPR OP2 ARG))'.
806 (ccl-compile-expression 'r7 left)
808 ;; Now RRR has the form `(ARG OP RIGHT)'.
809 (if (integer-or-char-p right)
811 (ccl-embed-code 'write-expr-const 0 (ash op 3) left)
812 (ccl-embed-data right))
813 (ccl-check-register right rrr)
814 (ccl-embed-code 'write-expr-register 0
816 (get right 'ccl-register-number))))))
819 (error "CCL: Invalid argument: %s" cmd))))
822 ;; Compile CALL statement.
823 (defun ccl-compile-call (cmd)
824 (if (/= (length cmd) 2)
825 (error "CCL: Invalid number of arguments: %s" cmd))
826 (if (not (symbolp (nth 1 cmd)))
827 (error "CCL: Subroutine should be a symbol: %s" cmd))
828 (let* ((name (nth 1 cmd))
829 (idx (get name 'ccl-program-idx)))
831 (error "CCL: Unknown subroutine name: %s" name))
832 (ccl-embed-code 'call 0 idx))
835 ;; Compile END statement.
836 (defun ccl-compile-end (cmd)
837 (if (/= (length cmd) 1)
838 (error "CCL: Invalid number of arguments: %s" cmd))
839 (ccl-embed-code 'end 0 0)
842 ;; Compile read-multibyte-character
843 (defun ccl-compile-read-multibyte-character (cmd)
844 (if (/= (length cmd) 3)
845 (error "CCL: Invalid number of arguments: %s" cmd))
846 (let ((RRR (nth 1 cmd))
848 (ccl-check-register rrr cmd)
849 (ccl-check-register RRR cmd)
850 (ccl-embed-extended-command 'read-multibyte-character rrr RRR 0))
853 ;; Compile write-multibyte-character
854 (defun ccl-compile-write-multibyte-character (cmd)
855 (if (/= (length cmd) 3)
856 (error "CCL: Invalid number of arguments: %s" cmd))
857 (let ((RRR (nth 1 cmd))
859 (ccl-check-register rrr cmd)
860 (ccl-check-register RRR cmd)
861 (ccl-embed-extended-command 'write-multibyte-character rrr RRR 0))
864 ;; Compile translate-character
865 ;; (defun ccl-compile-translate-character (cmd)
866 ;; (if (/= (length cmd) 4)
867 ;; (error "CCL: Invalid number of arguments: %s" cmd))
868 ;; (let ((Rrr (nth 1 cmd))
870 ;; (rrr (nth 3 cmd)))
871 ;; (ccl-check-register rrr cmd)
872 ;; (ccl-check-register RRR cmd)
873 ;; (cond ((and (symbolp Rrr) (not (get Rrr 'ccl-register-number)))
874 ;; (if (not (get Rrr 'translation-table))
875 ;; (error "CCL: Invalid translation table %s in %s" Rrr cmd))
876 ;; (ccl-embed-extended-command 'translate-character-const-tbl
878 ;; (ccl-embed-data Rrr))
880 ;; (ccl-check-register Rrr cmd)
881 ;; (ccl-embed-extended-command 'translate-character rrr RRR Rrr))))
884 ;; (defun ccl-compile-iterate-multiple-map (cmd)
885 ;; (ccl-compile-multiple-map-function 'iterate-multiple-map cmd)
888 ;; (defun ccl-compile-map-multiple (cmd)
889 ;; (if (/= (length cmd) 4)
890 ;; (error "CCL: Invalid number of arguments: %s" cmd))
891 ;; (let ((func '(lambda (arg mp)
892 ;; (let ((len 0) result add)
894 ;; (if (consp (car arg))
895 ;; (setq add (funcall func (car arg) t)
896 ;; result (append result add)
897 ;; add (+ (-(car add)) 1))
902 ;; (setq arg (cdr arg)
905 ;; (cons (- len) result)
908 ;; (setq arg (append (list (nth 0 cmd) (nth 1 cmd) (nth 2 cmd))
909 ;; (funcall func (nth 3 cmd) nil)))
910 ;; (ccl-compile-multiple-map-function 'map-multiple arg))
913 ;; (defun ccl-compile-map-single (cmd)
914 ;; (if (/= (length cmd) 4)
915 ;; (error "CCL: Invalid number of arguments: %s" cmd))
916 ;; (let ((RRR (nth 1 cmd))
920 ;; (ccl-check-register rrr cmd)
921 ;; (ccl-check-register RRR cmd)
922 ;; (ccl-embed-extended-command 'map-single rrr RRR 0)
923 ;; (cond ((symbolp map)
924 ;; (if (get map 'code-conversion-map)
925 ;; (ccl-embed-data map)
926 ;; (error "CCL: Invalid map: %s" map)))
928 ;; (error "CCL: Invalid type of arguments: %s" cmd))))
931 ;; (defun ccl-compile-multiple-map-function (command cmd)
932 ;; (if (< (length cmd) 4)
933 ;; (error "CCL: Invalid number of arguments: %s" cmd))
934 ;; (let ((RRR (nth 1 cmd))
936 ;; (args (nthcdr 3 cmd))
938 ;; (ccl-check-register rrr cmd)
939 ;; (ccl-check-register RRR cmd)
940 ;; (ccl-embed-extended-command command rrr RRR 0)
941 ;; (ccl-embed-data (length args))
943 ;; (setq map (car args))
944 ;; (cond ((symbolp map)
945 ;; (if (get map 'code-conversion-map)
946 ;; (ccl-embed-data map)
947 ;; (error "CCL: Invalid map: %s" map)))
949 ;; (ccl-embed-data map))
951 ;; (error "CCL: Invalid type of arguments: %s" cmd)))
952 ;; (setq args (cdr args)))))
957 ;; To avoid byte-compiler warning.
961 (defun ccl-dump (ccl-code)
962 "Disassemble compiled CCL-CODE."
963 (let ((len (length ccl-code))
964 (buffer-mag (aref ccl-code 0)))
965 (cond ((= buffer-mag 0)
966 (insert "Don't output anything.\n"))
968 (insert "Out-buffer must be as large as in-buffer.\n"))
971 (format "Out-buffer must be %d times bigger than in-buffer.\n"
973 (insert "Main-body:\n")
974 (setq ccl-current-ic 2)
975 (if (> (aref ccl-code 1) 0)
977 (while (< ccl-current-ic (aref ccl-code 1))
979 (insert "At EOF:\n")))
980 (while (< ccl-current-ic len)
984 ;; Return a CCL code in `ccl-code' at `ccl-current-ic'.
985 (defun ccl-get-next-code ()
987 (aref ccl-code ccl-current-ic)
988 (setq ccl-current-ic (1+ ccl-current-ic))))
991 (let* ((code (ccl-get-next-code))
992 (cmd (aref ccl-code-table (logand code 31)))
993 (rrr (ash (logand code 255) -5))
995 (insert (format "%5d:[%s] " (1- ccl-current-ic) cmd))
996 (funcall (get cmd 'ccl-dump-function) rrr cc)))
998 (defun ccl-dump-set-register (rrr cc)
999 (insert (format "r%d = r%d\n" rrr cc)))
1001 (defun ccl-dump-set-short-const (rrr cc)
1002 (insert (format "r%d = %d\n" rrr cc)))
1004 (defun ccl-dump-set-const (rrr ignore)
1005 (insert (format "r%d = %d\n" rrr (ccl-get-next-code))))
1007 (defun ccl-dump-set-array (rrr cc)
1008 (let ((rrr2 (logand cc 7))
1011 (insert (format "r%d = array[r%d] of length %d\n\t"
1014 (insert (format "%d " (ccl-get-next-code)))
1018 (defun ccl-dump-jump (ignore cc &optional address)
1019 (insert (format "jump to %d(" (+ (or address ccl-current-ic) cc)))
1022 (insert (format "%d)\n" (1+ cc))))
1024 (defun ccl-dump-jump-cond (rrr cc)
1025 (insert (format "if (r%d == 0), " rrr))
1026 (ccl-dump-jump nil cc))
1028 (defun ccl-dump-write-register-jump (rrr cc)
1029 (insert (format "write r%d, " rrr))
1030 (ccl-dump-jump nil cc))
1032 (defun ccl-dump-write-register-read-jump (rrr cc)
1033 (insert (format "write r%d, read r%d, " rrr rrr))
1034 (ccl-dump-jump nil cc)
1035 (ccl-get-next-code) ; Skip dummy READ-JUMP
1038 (defun ccl-extract-arith-op (cc)
1039 (aref ccl-arith-table (ash cc -6)))
1041 (defun ccl-dump-write-expr-const (ignore cc)
1042 (insert (format "write (r%d %s %d)\n"
1044 (ccl-extract-arith-op cc)
1045 (ccl-get-next-code))))
1047 (defun ccl-dump-write-expr-register (ignore cc)
1048 (insert (format "write (r%d %s r%d)\n"
1050 (ccl-extract-arith-op cc)
1051 (logand (ash cc -3) 7))))
1053 (defun ccl-dump-insert-char (cc)
1054 (cond ((= cc ?\t) (insert " \"^I\""))
1055 ((= cc ?\n) (insert " \"^J\""))
1056 (t (insert (format " \"%c\"" cc)))))
1058 (defun ccl-dump-write-const-jump (ignore cc)
1059 (let ((address ccl-current-ic))
1060 (insert "write char")
1061 (ccl-dump-insert-char (ccl-get-next-code))
1063 (ccl-dump-jump nil cc address)))
1065 (defun ccl-dump-write-const-read-jump (rrr cc)
1066 (let ((address ccl-current-ic))
1067 (insert "write char")
1068 (ccl-dump-insert-char (ccl-get-next-code))
1069 (insert (format ", read r%d, " rrr))
1070 (ccl-dump-jump cc address)
1071 (ccl-get-next-code) ; Skip dummy READ-JUMP
1074 (defun ccl-dump-write-string-jump (ignore cc)
1075 (let ((address ccl-current-ic)
1076 (len (ccl-get-next-code))
1080 (let ((code (ccl-get-next-code)))
1081 (insert (ash code -16))
1082 (if (< (1+ i) len) (insert (logand (ash code -8) 255)))
1083 (if (< (+ i 2) len) (insert (logand code 255))))
1086 (ccl-dump-jump nil cc address)))
1088 (defun ccl-dump-write-array-read-jump (rrr cc)
1089 (let ((address ccl-current-ic)
1090 (len (ccl-get-next-code))
1092 (insert (format "write array[r%d] of length %d,\n\t" rrr len))
1094 (ccl-dump-insert-char (ccl-get-next-code))
1096 (insert (format "\n\tthen read r%d, " rrr))
1097 (ccl-dump-jump nil cc address)
1098 (ccl-get-next-code) ; Skip dummy READ-JUMP.
1101 (defun ccl-dump-read-jump (rrr cc)
1102 (insert (format "read r%d, " rrr))
1103 (ccl-dump-jump nil cc))
1105 (defun ccl-dump-branch (rrr len)
1106 (let ((jump-table-head ccl-current-ic)
1108 (insert (format "jump to array[r%d] of length %d\n\t" rrr len))
1110 (insert (format "%d " (+ jump-table-head (ccl-get-next-code))))
1114 (defun ccl-dump-read-register (rrr cc)
1115 (insert (format "read r%d (%d remaining)\n" rrr cc)))
1117 (defun ccl-dump-read-branch (rrr len)
1118 (insert (format "read r%d, " rrr))
1119 (ccl-dump-branch rrr len))
1121 (defun ccl-dump-write-register (rrr cc)
1122 (insert (format "write r%d (%d remaining)\n" rrr cc)))
1124 (defun ccl-dump-call (ignore cc)
1125 (insert (format "call subroutine #%d\n" cc)))
1127 (defun ccl-dump-write-const-string (rrr cc)
1130 (insert "write char")
1131 (ccl-dump-insert-char cc)
1137 (let ((code (ccl-get-next-code)))
1138 (insert (format "%c" (lsh code -16)))
1140 (insert (format "%c" (logand (lsh code -8) 255))))
1142 (insert (format "%c" (logand code 255))))
1146 (defun ccl-dump-write-array (rrr cc)
1148 (insert (format "write array[r%d] of length %d\n\t" rrr cc))
1150 (ccl-dump-insert-char (ccl-get-next-code))
1154 (defun ccl-dump-end (&rest ignore)
1157 (defun ccl-dump-set-assign-expr-const (rrr cc)
1158 (insert (format "r%d %s= %d\n"
1160 (ccl-extract-arith-op cc)
1161 (ccl-get-next-code))))
1163 (defun ccl-dump-set-assign-expr-register (rrr cc)
1164 (insert (format "r%d %s= r%d\n"
1166 (ccl-extract-arith-op cc)
1169 (defun ccl-dump-set-expr-const (rrr cc)
1170 (insert (format "r%d = r%d %s %d\n"
1173 (ccl-extract-arith-op cc)
1174 (ccl-get-next-code))))
1176 (defun ccl-dump-set-expr-register (rrr cc)
1177 (insert (format "r%d = r%d %s r%d\n"
1180 (ccl-extract-arith-op cc)
1181 (logand (ash cc -3) 7))))
1183 (defun ccl-dump-jump-cond-expr-const (rrr cc)
1184 (let ((address ccl-current-ic))
1185 (insert (format "if !(r%d %s %d), "
1187 (aref ccl-arith-table (ccl-get-next-code))
1188 (ccl-get-next-code)))
1189 (ccl-dump-jump nil cc address)))
1191 (defun ccl-dump-jump-cond-expr-register (rrr cc)
1192 (let ((address ccl-current-ic))
1193 (insert (format "if !(r%d %s r%d), "
1195 (aref ccl-arith-table (ccl-get-next-code))
1196 (ccl-get-next-code)))
1197 (ccl-dump-jump nil cc address)))
1199 (defun ccl-dump-read-jump-cond-expr-const (rrr cc)
1200 (insert (format "read r%d, " rrr))
1201 (ccl-dump-jump-cond-expr-const rrr cc))
1203 (defun ccl-dump-read-jump-cond-expr-register (rrr cc)
1204 (insert (format "read r%d, " rrr))
1205 (ccl-dump-jump-cond-expr-register rrr cc))
1207 (defun ccl-dump-binary (ccl-code)
1208 (let ((len (length ccl-code))
1211 (let ((code (aref ccl-code i))
1214 (insert (if (= (logand code (ash 1 j)) 0) ?0 ?1))
1216 (setq code (logand code 31))
1217 (if (< code (length ccl-code-table))
1218 (insert (format ":%s" (aref ccl-code-table code))))
1222 (defun ccl-dump-ex-cmd (rrr cc)
1223 (let* ((RRR (logand cc ?\x7))
1224 (Rrr (logand (ash cc -3) ?\x7))
1225 (ex-op (aref ccl-extended-code-table (logand (ash cc -6) ?\x3fff))))
1226 (insert (format "<%s> " ex-op))
1227 (funcall (get ex-op 'ccl-dump-function) rrr RRR Rrr)))
1229 (defun ccl-dump-read-multibyte-character (rrr RRR Rrr)
1230 (insert (format "read-multibyte-character r%d r%d\n" RRR rrr)))
1232 (defun ccl-dump-write-multibyte-character (rrr RRR Rrr)
1233 (insert (format "write-multibyte-character r%d r%d\n" RRR rrr)))
1235 ;; (defun ccl-dump-translate-character (rrr RRR Rrr)
1236 ;; (insert (format "translation table(r%d) r%d r%d\n" Rrr RRR rrr)))
1238 ;; (defun ccl-dump-translate-character-const-tbl (rrr RRR Rrr)
1239 ;; (let ((tbl (ccl-get-next-code)))
1240 ;; (insert (format "translation table(%S) r%d r%d\n" tbl RRR rrr))))
1242 ;; (defun ccl-dump-iterate-multiple-map (rrr RRR Rrr)
1243 ;; (let ((notbl (ccl-get-next-code))
1245 ;; (insert (format "iterate-multiple-map r%d r%d\n" RRR rrr))
1246 ;; (insert (format "\tnumber of maps is %d .\n\t [" notbl))
1247 ;; (while (< i notbl)
1248 ;; (setq id (ccl-get-next-code))
1249 ;; (insert (format "%S" id))
1253 ;; (defun ccl-dump-map-multiple (rrr RRR Rrr)
1254 ;; (let ((notbl (ccl-get-next-code))
1256 ;; (insert (format "map-multiple r%d r%d\n" RRR rrr))
1257 ;; (insert (format "\tnumber of maps and separators is %d\n\t [" notbl))
1258 ;; (while (< i notbl)
1259 ;; (setq id (ccl-get-next-code))
1261 ;; (insert "]\n\t [")
1262 ;; (insert (format "%S " id)))
1266 ;; (defun ccl-dump-map-single (rrr RRR Rrr)
1267 ;; (let ((id (ccl-get-next-code)))
1268 ;; (insert (format "map-single r%d r%d map(%S)\n" RRR rrr id))))
1271 ;; CCL emulation staffs
1273 ;; Not yet implemented.
1275 ;; Auto-loaded functions.
1278 (defmacro declare-ccl-program (name &optional vector)
1279 "Declare NAME as a name of CCL program.
1281 To compile a CCL program which calls another CCL program not yet
1282 defined, it must be declared as a CCL program in advance.
1283 Optional arg VECTOR is a compiled CCL code of the CCL program."
1284 `(put ',name 'ccl-program-idx (register-ccl-program ',name ,vector)))
1287 (defmacro define-ccl-program (name ccl-program &optional doc)
1288 "Set NAME the compiled code of CCL-PROGRAM.
1289 CCL-PROGRAM is `eval'ed before being handed to the CCL compiler `ccl-compile'.
1290 The compiled code is a vector of integers."
1291 `(let ((prog ,(ccl-compile (eval ccl-program))))
1292 (defconst ,name prog ,doc)
1293 (put ',name 'ccl-program-idx (register-ccl-program ',name prog))
1297 (defmacro check-ccl-program (ccl-program &optional name)
1298 "Check validity of CCL-PROGRAM.
1299 If CCL-PROGRAM is a symbol denoting a valid CCL program, return
1300 CCL-PROGRAM, else return nil.
1301 If CCL-PROGRAM is a vector and optional arg NAME (symbol) is supplied,
1302 register CCL-PROGRAM by name NAME, and return NAME."
1303 `(let ((result ,ccl-program))
1304 (cond ((symbolp ,ccl-program)
1305 (or (numberp (get ,ccl-program 'ccl-program-idx))
1307 ((vectorp ,ccl-program)
1309 (register-ccl-program result ,ccl-program))
1315 (defun ccl-execute-with-args (ccl-prog &rest args)
1316 "Execute CCL-PROGRAM with registers initialized by the remaining args.
1317 The return value is a vector of resulting CCL registers."
1318 (let ((reg (make-vector 8 0))
1320 (while (and args (< i 8))
1321 (if (not (integerp (car args)))
1322 (error "Arguments should be integer"))
1323 (aset reg i (car args))
1324 (setq args (cdr args) i (1+ i)))
1325 (ccl-execute ccl-prog reg)