1 ;;; ccl.el --- CCL (Code Conversion Language) compiler
3 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
6 ;; Keywords: CCL, mule, multilingual, character set, coding-system
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
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.
27 ;; CCL (Code Conversion Language) is a simple programming language to
28 ;; be used for various kind of code conversion. CCL program is
29 ;; compiled to CCL code (vector of integers) and executed by CCL
30 ;; interpreter of Emacs.
32 ;; CCL is used for code conversion at process I/O and file I/O for
33 ;; non-standard coding-system. In addition, it is used for
34 ;; calculating a code point of X's font from a character code.
35 ;; However, since CCL is designed as a powerful programming language,
36 ;; it can be used for more generic calculation. For instance,
37 ;; combination of three or more arithmetic operations can be
38 ;; calculated faster than Emacs Lisp.
40 ;; Here's the syntax of CCL program in BNF notation.
43 ;; (BUFFER_MAGNIFICATION
47 ;; BUFFER_MAGNIFICATION := integer
48 ;; CCL_MAIN_BLOCK := CCL_BLOCK
49 ;; CCL_EOF_BLOCK := CCL_BLOCK
52 ;; STATEMENT | (STATEMENT [STATEMENT ...])
54 ;; SET | IF | BRANCH | LOOP | REPEAT | BREAK | READ | WRITE | CALL
58 ;; | (REG ASSIGNMENT_OPERATOR EXPRESSION)
61 ;; EXPRESSION := ARG | (EXPRESSION OPERATOR ARG)
63 ;; IF := (if EXPRESSION CCL_BLOCK CCL_BLOCK)
64 ;; BRANCH := (branch EXPRESSION CCL_BLOCK [CCL_BLOCK ...])
65 ;; LOOP := (loop STATEMENT [STATEMENT ...])
69 ;; | (write-repeat [REG | integer | string])
70 ;; | (write-read-repeat REG [integer | ARRAY])
73 ;; | (read-if (REG OPERATOR ARG) CCL_BLOCK CCL_BLOCK)
74 ;; | (read-branch REG CCL_BLOCK [CCL_BLOCK ...])
75 ;; | (read-multibyte-character REG {charset} REG {code-point})
78 ;; | (write EXPRESSION)
79 ;; | (write integer) | (write string) | (write REG ARRAY)
81 ;; | (write-multibyte-character REG(charset) REG(codepoint))
83 ;; (translate-character REG(table) REG(charset) REG(codepoint))
84 ;; | (translate-character SYMBOL REG(charset) REG(codepoint))
86 ;; (iterate-multiple-map REG REG MAP-IDs)
87 ;; | (map-multiple REG REG (MAP-SET))
88 ;; | (map-single REG REG MAP-ID)
89 ;; MAP-IDs := MAP-ID ...
90 ;; MAP-SET := MAP-IDs | (MAP-IDs) MAP-SET
93 ;; CALL := (call ccl-program-name)
96 ;; REG := r0 | r1 | r2 | r3 | r4 | r5 | r6 | r7
97 ;; ARG := REG | integer
99 ;; + | - | * | / | % | & | '|' | ^ | << | >> | <8 | >8 | //
100 ;; | < | > | == | <= | >= | != | de-sjis | en-sjis
101 ;; ASSIGNMENT_OPERATOR :=
102 ;; += | -= | *= | /= | %= | &= | '|=' | ^= | <<= | >>=
103 ;; ARRAY := '[' integer ... ']'
108 "CCL (Code Conversion Language) compiler."
112 (defconst ccl-command-table
113 [if branch loop break repeat write-repeat write-read-repeat
114 read read-if read-branch write call end
115 read-multibyte-character write-multibyte-character
117 iterate-multiple-map map-multiple map-single
]
118 "Vector of CCL commands (symbols).")
120 ;; Put a property to each symbol of CCL commands for the compiler.
121 (let (op (i 0) (len (length ccl-command-table
)))
123 (setq op
(aref ccl-command-table i
))
124 (put op
'ccl-compile-function
(intern (format "ccl-compile-%s" op
)))
127 (defconst ccl-code-table
135 write-register-read-jump
137 write-const-read-jump
139 write-array-read-jump
151 set-assign-expr-const
152 set-assign-expr-register
156 jump-cond-expr-register
157 read-jump-cond-expr-const
158 read-jump-cond-expr-register
161 "Vector of CCL compiled codes (symbols).")
163 (defconst ccl-extended-code-table
164 [read-multibyte-character
165 write-multibyte-character
167 translate-character-const-tbl
168 nil nil nil nil nil nil nil nil nil nil nil nil
; 0x04-0x0f
173 "Vector of CCL extended compiled codes (symbols).")
175 ;; Put a property to each symbol of CCL codes for the disassembler.
176 (let (code (i 0) (len (length ccl-code-table
)))
178 (setq code
(aref ccl-code-table i
))
179 (put code
'ccl-code i
)
180 (put code
'ccl-dump-function
(intern (format "ccl-dump-%s" code
)))
183 (let (code (i 0) (len (length ccl-extended-code-table
)))
185 (setq code
(aref ccl-extended-code-table i
))
188 (put code
'ccl-ex-code i
)
189 (put code
'ccl-dump-function
(intern (format "ccl-dump-%s" code
)))))
192 (defconst ccl-jump-code-list
193 '(jump jump-cond write-register-jump write-register-read-jump
194 write-const-jump write-const-read-jump write-string-jump
195 write-array-read-jump read-jump
))
197 ;; Put a property `jump-flag' to each CCL code which execute jump in
199 (let ((l ccl-jump-code-list
))
201 (put (car l
) 'jump-flag t
)
204 (defconst ccl-register-table
205 [r0 r1 r2 r3 r4 r5 r6 r7
]
206 "Vector of CCL registers (symbols).")
208 ;; Put a property to indicate register number to each symbol of CCL.
210 (let (reg (i 0) (len (length ccl-register-table
)))
212 (setq reg
(aref ccl-register-table i
))
213 (put reg
'ccl-register-number i
)
216 (defconst ccl-arith-table
217 [+ -
* / %
& | ^
<< >> <8 >8 // nil nil nil
218 < > == <= >= != de-sjis en-sjis
]
219 "Vector of CCL arithmetic/logical operators (symbols).")
221 ;; Put a property to each symbol of CCL operators for the compiler.
222 (let (arith (i 0) (len (length ccl-arith-table
)))
224 (setq arith
(aref ccl-arith-table i
))
225 (if arith
(put arith
'ccl-arith-code i
))
228 (defconst ccl-assign-arith-table
229 [+= -
= *= /= %
= &= |
= ^
= <<= >>= <8= >8= //=]
230 "Vector of CCL assignment operators (symbols).")
232 ;; Put a property to each symbol of CCL assignment operators for the compiler.
233 (let (arith (i 0) (len (length ccl-assign-arith-table
)))
235 (setq arith
(aref ccl-assign-arith-table i
))
236 (put arith
'ccl-self-arith-code i
)
239 (defvar ccl-program-vector nil
240 "Working vector of CCL codes produced by CCL compiler.")
241 (defvar ccl-current-ic
0
242 "The current index for `ccl-program-vector'.")
244 ;; Embed integer DATA in `ccl-program-vector' at `ccl-current-ic' and
245 ;; increment it. If IC is specified, embed DATA at IC.
246 (defun ccl-embed-data (data &optional ic
)
248 (aset ccl-program-vector ic data
)
249 (aset ccl-program-vector ccl-current-ic data
)
250 (setq ccl-current-ic
(1+ ccl-current-ic
))))
252 ;; Embed string STR of length LEN in `ccl-program-vector' at
254 (defun ccl-embed-string (len str
)
257 (ccl-embed-data (logior (ash (aref str i
) 16)
259 (ash (aref str
(1+ i
)) 8)
266 ;; Embed a relative jump address to `ccl-current-ic' in
267 ;; `ccl-program-vector' at IC without altering the other bit field.
268 (defun ccl-embed-current-address (ic)
269 (let ((relative (- ccl-current-ic
(1+ ic
))))
270 (aset ccl-program-vector ic
271 (logior (aref ccl-program-vector ic
) (ash relative
8)))))
273 ;; Embed CCL code for the operation OP and arguments REG and DATA in
274 ;; `ccl-program-vector' at `ccl-current-ic' in the following format.
275 ;; |----------------- integer (28-bit) ------------------|
276 ;; |------------ 20-bit ------------|- 3-bit --|- 5-bit -|
277 ;; |------------- DATA -------------|-- REG ---|-- OP ---|
278 ;; If REG2 is specified, embed a code in the following format.
279 ;; |------- 17-bit ------|- 3-bit --|- 3-bit --|- 5-bit -|
280 ;; |-------- DATA -------|-- REG2 --|-- REG ---|-- OP ---|
282 ;; If REG is a CCL register symbol (e.g. r0, r1...), the register
283 ;; number is embedded. If OP is one of unconditional jumps, DATA is
284 ;; changed to an relative jump address.
286 (defun ccl-embed-code (op reg data
&optional reg2
)
287 (if (and (> data
0) (get op
'jump-flag
))
288 ;; DATA is an absolute jump address. Make it relative to the
289 ;; next of jump code.
290 (setq data
(- data
(1+ ccl-current-ic
))))
291 (let ((code (logior (get op
'ccl-code
)
293 (if (symbolp reg
) (get reg
'ccl-register-number
) reg
) 5)
295 (logior (ash (get reg2
'ccl-register-number
) 8)
298 (aset ccl-program-vector ccl-current-ic code
)
299 (setq ccl-current-ic
(1+ ccl-current-ic
))))
301 ;; extended ccl command format
302 ;; |- 14-bit -|- 3-bit --|- 3-bit --|- 3-bit --|- 5-bit -|
303 ;; |- EX-OP --|-- REG3 --|-- REG2 --|-- REG ---|-- OP ---|
304 (defun ccl-embed-extended-command (ex-op reg reg2 reg3
)
305 (let ((data (logior (ash (get ex-op
'ccl-ex-code
) 3)
307 (get reg3
'ccl-register-number
)
309 (ccl-embed-code 'ex-cmd reg data reg2
)))
311 ;; Just advance `ccl-current-ic' by INC.
312 (defun ccl-increment-ic (inc)
313 (setq ccl-current-ic
(+ ccl-current-ic inc
)))
316 (defun ccl-program-p (obj)
317 "T if OBJECT is a valid CCL compiled code."
319 (let ((i 0) (len (length obj
)) (flag t
))
322 (while (and flag
(< i len
))
323 (setq flag
(integerp (aref obj i
)))
327 ;; If non-nil, index of the start of the current loop.
328 (defvar ccl-loop-head nil
)
329 ;; If non-nil, list of absolute addresses of the breaking points of
331 (defvar ccl-breaks nil
)
334 (defun ccl-compile (ccl-program)
335 "Return a compiled code of CCL-PROGRAM as a vector of integer."
336 (if (or (null (consp ccl-program
))
337 (null (integerp (car ccl-program
)))
338 (null (listp (car (cdr ccl-program
)))))
339 (error "CCL: Invalid CCL program: %s" ccl-program
))
340 (if (null (vectorp ccl-program-vector
))
341 (setq ccl-program-vector
(make-vector 8192 0)))
342 (setq ccl-loop-head nil ccl-breaks nil
)
343 (setq ccl-current-ic
0)
345 ;; The first element is the buffer magnification.
346 (ccl-embed-data (car ccl-program
))
348 ;; The second element is the address of the start CCL code for
349 ;; processing end of input buffer (we call it eof-processor). We
353 ;; Compile the main body of the CCL program.
354 (ccl-compile-1 (car (cdr ccl-program
)))
356 ;; Embed the address of eof-processor.
357 (ccl-embed-data ccl-current-ic
1)
359 ;; Then compile eof-processor.
360 (if (nth 2 ccl-program
)
361 (ccl-compile-1 (nth 2 ccl-program
)))
363 ;; At last, embed termination code.
364 (ccl-embed-code 'end
0 0)
366 (let ((vec (make-vector ccl-current-ic
0))
368 (while (< i ccl-current-ic
)
369 (aset vec i
(aref ccl-program-vector i
))
373 ;; Signal syntax error.
374 (defun ccl-syntax-error (cmd)
375 (error "CCL: Syntax error: %s" cmd
))
377 ;; Check if ARG is a valid CCL register.
378 (defun ccl-check-register (arg cmd
)
379 (if (get arg
'ccl-register-number
)
381 (error "CCL: Invalid register %s in %s." arg cmd
)))
383 ;; Check if ARG is a valid CCL command.
384 (defun ccl-check-compile-function (arg cmd
)
385 (or (get arg
'ccl-compile-function
)
386 (error "CCL: Invalid command: %s" cmd
)))
388 ;; In the following code, most ccl-compile-XXXX functions return t if
389 ;; they end with unconditional jump, else return nil.
391 ;; Compile CCL-BLOCK (see the syntax above).
392 (defun ccl-compile-1 (ccl-block)
393 (let (unconditional-jump
395 (if (or (integerp ccl-block
)
397 (and ccl-block
(symbolp (car ccl-block
))))
398 ;; This block consists of single statement.
399 (setq ccl-block
(list ccl-block
)))
401 ;; Now CCL-BLOCK is a list of statements. Compile them one by
404 (setq cmd
(car ccl-block
))
405 (setq unconditional-jump
406 (cond ((integerp cmd
)
407 ;; SET statement for the register 0.
408 (ccl-compile-set (list 'r0
'= cmd
)))
411 ;; WRITE statement of string argument.
412 (ccl-compile-write-string cmd
))
415 ;; The other statements.
416 (cond ((eq (nth 1 cmd
) '=)
417 ;; SET statement of the form `(REG = EXPRESSION)'.
418 (ccl-compile-set cmd
))
420 ((and (symbolp (nth 1 cmd
))
421 (get (nth 1 cmd
) 'ccl-self-arith-code
))
422 ;; SET statement with an assignment operation.
423 (ccl-compile-self-set cmd
))
426 (funcall (ccl-check-compile-function (car cmd
) cmd
)
430 (ccl-syntax-error cmd
))))
431 (setq ccl-block
(cdr ccl-block
)))
434 (defconst ccl-max-short-const
(ash 1 19))
435 (defconst ccl-min-short-const
(ash -
1 19))
437 ;; Compile SET statement.
438 (defun ccl-compile-set (cmd)
439 (let ((rrr (ccl-check-register (car cmd
) cmd
))
442 ;; CMD has the form `(RRR = (XXX OP YYY))'.
443 (ccl-compile-expression rrr right
))
446 ;; CMD has the form `(RRR = integer)'.
447 (if (and (<= right ccl-max-short-const
)
448 (>= right ccl-min-short-const
))
449 (ccl-embed-code 'set-short-const rrr right
)
450 (ccl-embed-code 'set-const rrr
0)
451 (ccl-embed-data right
)))
454 ;; CMD has the form `(RRR = rrr [ array ])'.
455 (ccl-check-register right cmd
)
456 (let ((ary (nth 3 cmd
)))
458 (let ((i 0) (len (length ary
)))
459 (ccl-embed-code 'set-array rrr len right
)
461 (ccl-embed-data (aref ary i
))
463 (ccl-embed-code 'set-register rrr
0 right
))))))
466 ;; Compile SET statement with ASSIGNMENT_OPERATOR.
467 (defun ccl-compile-self-set (cmd)
468 (let ((rrr (ccl-check-register (car cmd
) cmd
))
471 ;; CMD has the form `(RRR ASSIGN_OP (XXX OP YYY))', compile
472 ;; the right hand part as `(r7 = (XXX OP YYY))' (note: the
473 ;; register 7 can be used for storing temporary value).
475 (ccl-compile-expression 'r7 right
)
477 ;; Now CMD has the form `(RRR ASSIGN_OP ARG)'. Compile it as
478 ;; `(RRR = (RRR OP ARG))'.
479 (ccl-compile-expression
481 (list rrr
(intern (substring (symbol-name (nth 1 cmd
)) 0 -
1)) right
)))
484 ;; Compile SET statement of the form `(RRR = EXPR)'.
485 (defun ccl-compile-expression (rrr expr
)
486 (let ((left (car expr
))
487 (op (get (nth 1 expr
) 'ccl-arith-code
))
488 (right (nth 2 expr
)))
491 ;; EXPR has the form `((EXPR2 OP2 ARG) OP RIGHT)'. Compile
492 ;; the first term as `(r7 = (EXPR2 OP2 ARG)).'
493 (ccl-compile-expression 'r7 left
)
496 ;; Now EXPR has the form (LEFT OP RIGHT).
498 ;; Compile this SET statement as `(RRR OP= RIGHT)'.
501 (ccl-embed-code 'set-assign-expr-const rrr
(ash op
3) 'r0
)
502 (ccl-embed-data right
))
503 (ccl-check-register right expr
)
504 (ccl-embed-code 'set-assign-expr-register rrr
(ash op
3) right
))
506 ;; Compile this SET statement as `(RRR = (LEFT OP RIGHT))'.
509 (ccl-embed-code 'set-expr-const rrr
(ash op
3) left
)
510 (ccl-embed-data right
))
511 (ccl-check-register right expr
)
512 (ccl-embed-code 'set-expr-register
514 (logior (ash op
3) (get right
'ccl-register-number
))
517 ;; Compile WRITE statement with string argument.
518 (defun ccl-compile-write-string (str)
519 (let ((len (length str
)))
520 (ccl-embed-code 'write-const-string
1 len
)
521 (ccl-embed-string len str
))
524 ;; Compile IF statement of the form `(if CONDITION TRUE-PART FALSE-PART)'.
525 ;; If READ-FLAG is non-nil, this statement has the form
526 ;; `(read-if (REG OPERATOR ARG) TRUE-PART FALSE-PART)'.
527 (defun ccl-compile-if (cmd &optional read-flag
)
528 (if (and (/= (length cmd
) 3) (/= (length cmd
) 4))
529 (error "CCL: Invalid number of arguments: %s" cmd
))
530 (let ((condition (nth 1 cmd
))
531 (true-cmds (nth 2 cmd
))
532 (false-cmds (nth 3 cmd
))
535 (if (and (listp condition
)
536 (listp (car condition
)))
537 ;; If CONDITION is a nested expression, the inner expression
538 ;; should be compiled at first as SET statement, i.e.:
539 ;; `(if ((X OP2 Y) OP Z) ...)' is compiled into two statements:
540 ;; `(r7 = (X OP2 Y)) (if (r7 OP Z) ...)'.
542 (ccl-compile-expression 'r7
(car condition
))
543 (setq condition
(cons 'r7
(cdr condition
)))
544 (setq cmd
(cons (car cmd
)
545 (cons condition
(cdr (cdr cmd
)))))))
547 (setq jump-cond-address ccl-current-ic
)
548 ;; Compile CONDITION.
549 (if (symbolp condition
)
550 ;; CONDITION is a register.
552 (ccl-check-register condition cmd
)
553 (ccl-embed-code 'jump-cond condition
0))
554 ;; CONDITION is a simple expression of the form (RRR OP ARG).
555 (let ((rrr (car condition
))
556 (op (get (nth 1 condition
) 'ccl-arith-code
))
557 (arg (nth 2 condition
)))
558 (ccl-check-register rrr cmd
)
561 (ccl-embed-code (if read-flag
'read-jump-cond-expr-const
562 'jump-cond-expr-const
)
565 (ccl-embed-data arg
))
566 (ccl-check-register arg cmd
)
567 (ccl-embed-code (if read-flag
'read-jump-cond-expr-register
568 'jump-cond-expr-register
)
571 (ccl-embed-data (get arg
'ccl-register-number
)))))
573 ;; Compile TRUE-PART.
574 (let ((unconditional-jump (ccl-compile-1 true-cmds
)))
575 (if (null false-cmds
)
576 ;; This is the place to jump to if condition is false.
577 (ccl-embed-current-address jump-cond-address
)
578 (let (end-true-part-address)
579 (if (not unconditional-jump
)
581 ;; If TRUE-PART does not end with unconditional jump, we
582 ;; have to jump to the end of FALSE-PART from here.
583 (setq end-true-part-address ccl-current-ic
)
584 (ccl-embed-code 'jump
0 0)))
585 ;; This is the place to jump to if CONDITION is false.
586 (ccl-embed-current-address jump-cond-address
)
587 ;; Compile FALSE-PART.
588 (setq unconditional-jump
589 (and (ccl-compile-1 false-cmds
) unconditional-jump
))
590 (if end-true-part-address
591 ;; This is the place to jump to after the end of TRUE-PART.
592 (ccl-embed-current-address end-true-part-address
))))
593 unconditional-jump
)))
595 ;; Compile BRANCH statement.
596 (defun ccl-compile-branch (cmd)
597 (if (< (length cmd
) 3)
598 (error "CCL: Invalid number of arguments: %s" cmd
))
599 (ccl-compile-branch-blocks 'branch
600 (ccl-compile-branch-expression (nth 1 cmd
) cmd
)
603 ;; Compile READ statement of the form `(read-branch EXPR BLOCK0 BLOCK1 ...)'.
604 (defun ccl-compile-read-branch (cmd)
605 (if (< (length cmd
) 3)
606 (error "CCL: Invalid number of arguments: %s" cmd
))
607 (ccl-compile-branch-blocks 'read-branch
608 (ccl-compile-branch-expression (nth 1 cmd
) cmd
)
611 ;; Compile EXPRESSION part of BRANCH statement and return register
612 ;; which holds a value of the expression.
613 (defun ccl-compile-branch-expression (expr cmd
)
615 ;; EXPR has the form `(EXPR2 OP ARG)'. Compile it as SET
616 ;; statement of the form `(r7 = (EXPR2 OP ARG))'.
618 (ccl-compile-expression 'r7 expr
)
620 (ccl-check-register expr cmd
)))
622 ;; Compile BLOCKs of BRANCH statement. CODE is 'branch or 'read-branch.
623 ;; REG is a register which holds a value of EXPRESSION part. BLOCKs
624 ;; is a list of CCL-BLOCKs.
625 (defun ccl-compile-branch-blocks (code rrr blocks
)
626 (let ((branches (length blocks
))
628 jump-table-head-address
631 block-unconditional-jump
)
632 (ccl-embed-code code rrr branches
)
633 (setq jump-table-head-address ccl-current-ic
)
634 ;; The size of jump table is the number of blocks plus 1 (for the
635 ;; case RRR is out of range).
636 (ccl-increment-ic (1+ branches
))
637 (setq empty-block-indexes
(list branches
))
638 ;; Compile each block.
641 (if (null (car blocks
))
642 ;; This block is empty.
643 (setq empty-block-indexes
(cons branch-idx empty-block-indexes
)
644 block-unconditional-jump t
)
645 ;; This block is not empty.
646 (ccl-embed-data (- ccl-current-ic jump-table-head-address
)
647 (+ jump-table-head-address branch-idx
))
648 (setq block-unconditional-jump
(ccl-compile-1 (car blocks
)))
649 (if (not block-unconditional-jump
)
651 ;; Jump address of the end of branches are embedded later.
652 ;; For the moment, just remember where to embed them.
653 (setq block-tail-addresses
654 (cons ccl-current-ic block-tail-addresses
))
655 (ccl-embed-code 'jump
0 0))))
656 (setq branch-idx
(1+ branch-idx
))
657 (setq blocks
(cdr blocks
)))
658 (if (not block-unconditional-jump
)
659 ;; We don't need jump code at the end of the last block.
660 (setq block-tail-addresses
(cdr block-tail-addresses
)
661 ccl-current-ic
(1- ccl-current-ic
)))
662 ;; Embed jump address at the tailing jump commands of blocks.
663 (while block-tail-addresses
664 (ccl-embed-current-address (car block-tail-addresses
))
665 (setq block-tail-addresses
(cdr block-tail-addresses
)))
666 ;; For empty blocks, make entries in the jump table point directly here.
667 (while empty-block-indexes
668 (ccl-embed-data (- ccl-current-ic jump-table-head-address
)
669 (+ jump-table-head-address
(car empty-block-indexes
)))
670 (setq empty-block-indexes
(cdr empty-block-indexes
))))
671 ;; Branch command ends by unconditional jump if RRR is out of range.
674 ;; Compile LOOP statement.
675 (defun ccl-compile-loop (cmd)
676 (if (< (length cmd
) 2)
677 (error "CCL: Invalid number of arguments: %s" cmd
))
678 (let* ((ccl-loop-head ccl-current-ic
)
684 (setq unconditional-jump t
)
686 (setq unconditional-jump
687 (and (ccl-compile-1 (car cmd
)) unconditional-jump
))
688 (setq cmd
(cdr cmd
)))
691 ;; Embed jump address for break statements encountered in
694 (ccl-embed-current-address (car ccl-breaks
))
695 (setq ccl-breaks
(cdr ccl-breaks
))))
698 ;; Compile BREAK statement.
699 (defun ccl-compile-break (cmd)
700 (if (/= (length cmd
) 1)
701 (error "CCL: Invalid number of arguments: %s" cmd
))
702 (if (null ccl-loop-head
)
703 (error "CCL: No outer loop: %s" cmd
))
704 (setq ccl-breaks
(cons ccl-current-ic ccl-breaks
))
705 (ccl-embed-code 'jump
0 0)
708 ;; Compile REPEAT statement.
709 (defun ccl-compile-repeat (cmd)
710 (if (/= (length cmd
) 1)
711 (error "CCL: Invalid number of arguments: %s" cmd
))
712 (if (null ccl-loop-head
)
713 (error "CCL: No outer loop: %s" cmd
))
714 (ccl-embed-code 'jump
0 ccl-loop-head
)
717 ;; Compile WRITE-REPEAT statement.
718 (defun ccl-compile-write-repeat (cmd)
719 (if (/= (length cmd
) 2)
720 (error "CCL: Invalid number of arguments: %s" cmd
))
721 (if (null ccl-loop-head
)
722 (error "CCL: No outer loop: %s" cmd
))
723 (let ((arg (nth 1 cmd
)))
724 (cond ((integerp arg
)
725 (ccl-embed-code 'write-const-jump
0 ccl-loop-head
)
726 (ccl-embed-data arg
))
728 (let ((len (length arg
))
730 (ccl-embed-code 'write-string-jump
0 ccl-loop-head
)
732 (ccl-embed-string len arg
)))
734 (ccl-check-register arg cmd
)
735 (ccl-embed-code 'write-register-jump arg ccl-loop-head
))))
738 ;; Compile WRITE-READ-REPEAT statement.
739 (defun ccl-compile-write-read-repeat (cmd)
740 (if (or (< (length cmd
) 2) (> (length cmd
) 3))
741 (error "CCL: Invalid number of arguments: %s" cmd
))
742 (if (null ccl-loop-head
)
743 (error "CCL: No outer loop: %s" cmd
))
744 (let ((rrr (ccl-check-register (nth 1 cmd
) cmd
))
747 (ccl-embed-code 'write-register-read-jump rrr ccl-loop-head
))
749 (ccl-embed-code 'write-const-read-jump rrr arg ccl-loop-head
))
751 (let ((len (length arg
))
753 (ccl-embed-code 'write-array-read-jump rrr ccl-loop-head
)
756 (ccl-embed-data (aref arg i
))
759 (error "CCL: Invalid argument %s: %s" arg cmd
)))
760 (ccl-embed-code 'read-jump rrr ccl-loop-head
))
763 ;; Compile READ statement.
764 (defun ccl-compile-read (cmd)
765 (if (< (length cmd
) 2)
766 (error "CCL: Invalid number of arguments: %s" cmd
))
767 (let* ((args (cdr cmd
))
768 (i (1- (length args
))))
770 (let ((rrr (ccl-check-register (car args
) cmd
)))
771 (ccl-embed-code 'read-register rrr i
)
772 (setq args
(cdr args
) i
(1- i
)))))
775 ;; Compile READ-IF statement.
776 (defun ccl-compile-read-if (cmd)
777 (ccl-compile-if cmd
'read
))
779 ;; Compile WRITE statement.
780 (defun ccl-compile-write (cmd)
781 (if (< (length cmd
) 2)
782 (error "CCL: Invalid number of arguments: %s" cmd
))
783 (let ((rrr (nth 1 cmd
)))
784 (cond ((integerp rrr
)
785 (ccl-embed-code 'write-const-string
0 rrr
))
787 (ccl-compile-write-string rrr
))
788 ((and (symbolp rrr
) (vectorp (nth 2 cmd
)))
789 (ccl-check-register rrr cmd
)
790 ;; CMD has the form `(write REG ARRAY)'.
791 (let* ((arg (nth 2 cmd
))
794 (ccl-embed-code 'write-array rrr len
)
796 (if (not (integerp (aref arg i
)))
797 (error "CCL: Invalid argument %s: %s" arg cmd
))
798 (ccl-embed-data (aref arg i
))
802 ;; CMD has the form `(write REG ...)'.
803 (let* ((args (cdr cmd
))
804 (i (1- (length args
))))
806 (setq rrr
(ccl-check-register (car args
) cmd
))
807 (ccl-embed-code 'write-register rrr i
)
808 (setq args
(cdr args
) i
(1- i
)))))
811 ;; CMD has the form `(write (LEFT OP RIGHT))'.
812 (let ((left (car rrr
))
813 (op (get (nth 1 rrr
) 'ccl-arith-code
))
817 ;; RRR has the form `((EXPR OP2 ARG) OP RIGHT)'.
818 ;; Compile the first term as `(r7 = (EXPR OP2 ARG))'.
819 (ccl-compile-expression 'r7 left
)
821 ;; Now RRR has the form `(ARG OP RIGHT)'.
824 (ccl-embed-code 'write-expr-const
0 (ash op
3) left
)
825 (ccl-embed-data right
))
826 (ccl-check-register right rrr
)
827 (ccl-embed-code 'write-expr-register
0
829 (get right
'ccl-register-number
))))))
832 (error "CCL: Invalid argument: %s" cmd
))))
835 ;; Compile CALL statement.
836 (defun ccl-compile-call (cmd)
837 (if (/= (length cmd
) 2)
838 (error "CCL: Invalid number of arguments: %s" cmd
))
839 (if (not (symbolp (nth 1 cmd
)))
840 (error "CCL: Subroutine should be a symbol: %s" cmd
))
841 (let* ((name (nth 1 cmd
))
842 (idx (get name
'ccl-program-idx
)))
844 (error "CCL: Unknown subroutine name: %s" name
))
845 (ccl-embed-code 'call
0 idx
))
848 ;; Compile END statement.
849 (defun ccl-compile-end (cmd)
850 (if (/= (length cmd
) 1)
851 (error "CCL: Invalid number of arguments: %s" cmd
))
852 (ccl-embed-code 'end
0 0)
855 ;; Compile read-multibyte-character
856 (defun ccl-compile-read-multibyte-character (cmd)
857 (if (/= (length cmd
) 3)
858 (error "CCL: Invalid number of arguments: %s" cmd
))
859 (let ((RRR (nth 1 cmd
))
861 (ccl-check-register rrr cmd
)
862 (ccl-check-register RRR cmd
)
863 (ccl-embed-extended-command 'read-multibyte-character rrr RRR
0)))
865 ;; Compile write-multibyte-character
866 (defun ccl-compile-write-multibyte-character (cmd)
867 (if (/= (length cmd
) 3)
868 (error "CCL: Invalid number of arguments: %s" cmd
))
869 (let ((RRR (nth 1 cmd
))
871 (ccl-check-register rrr cmd
)
872 (ccl-check-register RRR cmd
)
873 (ccl-embed-extended-command 'write-multibyte-character rrr RRR
0)))
875 ;; Compile translate-character
876 (defun ccl-compile-translate-character (cmd)
877 (if (/= (length cmd
) 4)
878 (error "CCL: Invalid number of arguments: %s" cmd
))
879 (let ((Rrr (nth 1 cmd
))
882 (ccl-check-register rrr cmd
)
883 (ccl-check-register RRR cmd
)
885 (if (not (get Rrr
'translation-table
))
886 (error "CCL: Invalid translation table %s in %s" Rrr cmd
))
887 (ccl-embed-extended-command 'translate-character-const-tbl
889 (ccl-embed-data Rrr
))
891 (ccl-check-register Rrr cmd
)
892 (ccl-embed-extended-command 'translate-character rrr RRR Rrr
)))))
894 (defun ccl-compile-iterate-multiple-map (cmd)
895 (ccl-compile-multiple-map-function 'iterate-multiple-map cmd
))
897 (defun ccl-compile-map-multiple (cmd)
898 (if (/= (length cmd
) 4)
899 (error "CCL: Invalid number of arguments: %s" cmd
))
900 (let ((func '(lambda (arg mp
)
901 (let ((len 0) result add
)
903 (if (consp (car arg
))
904 (setq add
(funcall func
(car arg
) t
)
905 result
(append result add
)
906 add
(+ (-(car add
)) 1))
914 (cons (- len
) result
)
917 (setq arg
(append (list (nth 0 cmd
) (nth 1 cmd
) (nth 2 cmd
))
918 (funcall func
(nth 3 cmd
) nil
)))
919 (ccl-compile-multiple-map-function 'map-multiple arg
)))
921 (defun ccl-compile-map-single (cmd)
922 (if (/= (length cmd
) 4)
923 (error "CCL: Invalid number of arguments: %s" cmd
))
924 (let ((RRR (nth 1 cmd
))
928 (ccl-check-register rrr cmd
)
929 (ccl-check-register RRR cmd
)
930 (ccl-embed-extended-command 'map-single rrr RRR
0)
932 (if (get map
'code-conversion-map
)
934 (error "CCL: Invalid map: %s" map
)))
936 (error "CCL: Invalid type of arguments: %s" cmd
)))))
938 (defun ccl-compile-multiple-map-function (command cmd
)
939 (if (< (length cmd
) 4)
940 (error "CCL: Invalid number of arguments: %s" cmd
))
941 (let ((RRR (nth 1 cmd
))
943 (args (nthcdr 3 cmd
))
945 (ccl-check-register rrr cmd
)
946 (ccl-check-register RRR cmd
)
947 (ccl-embed-extended-command command rrr RRR
0)
948 (ccl-embed-data (length args
))
950 (setq map
(car args
))
952 (if (get map
'code-conversion-map
)
954 (error "CCL: Invalid map: %s" map
)))
956 (ccl-embed-data map
))
958 (error "CCL: Invalid type of arguments: %s" cmd
)))
959 (setq args
(cdr args
)))))
964 ;; To avoid byte-compiler warning.
968 (defun ccl-dump (ccl-code)
969 "Disassemble compiled CCL-CODE."
970 (let ((len (length ccl-code
))
971 (buffer-mag (aref ccl-code
0)))
972 (cond ((= buffer-mag
0)
973 (insert "Don't output anything.\n"))
975 (insert "Out-buffer must be as large as in-buffer.\n"))
978 (format "Out-buffer must be %d times bigger than in-buffer.\n"
980 (insert "Main-body:\n")
981 (setq ccl-current-ic
2)
982 (if (> (aref ccl-code
1) 0)
984 (while (< ccl-current-ic
(aref ccl-code
1))
986 (insert "At EOF:\n")))
987 (while (< ccl-current-ic len
)
991 ;; Return a CCL code in `ccl-code' at `ccl-current-ic'.
992 (defun ccl-get-next-code ()
994 (aref ccl-code ccl-current-ic
)
995 (setq ccl-current-ic
(1+ ccl-current-ic
))))
998 (let* ((code (ccl-get-next-code))
999 (cmd (aref ccl-code-table
(logand code
31)))
1000 (rrr (ash (logand code
255) -
5))
1002 (insert (format "%5d:[%s] " (1- ccl-current-ic
) cmd
))
1003 (funcall (get cmd
'ccl-dump-function
) rrr cc
)))
1005 (defun ccl-dump-set-register (rrr cc
)
1006 (insert (format "r%d = r%d\n" rrr cc
)))
1008 (defun ccl-dump-set-short-const (rrr cc
)
1009 (insert (format "r%d = %d\n" rrr cc
)))
1011 (defun ccl-dump-set-const (rrr ignore
)
1012 (insert (format "r%d = %d\n" rrr
(ccl-get-next-code))))
1014 (defun ccl-dump-set-array (rrr cc
)
1015 (let ((rrr2 (logand cc
7))
1018 (insert (format "r%d = array[r%d] of length %d\n\t"
1021 (insert (format "%d " (ccl-get-next-code)))
1025 (defun ccl-dump-jump (ignore cc
&optional address
)
1026 (insert (format "jump to %d(" (+ (or address ccl-current-ic
) cc
)))
1029 (insert (format "%d)\n" (1+ cc
))))
1031 (defun ccl-dump-jump-cond (rrr cc
)
1032 (insert (format "if (r%d == 0), " rrr
))
1033 (ccl-dump-jump nil cc
))
1035 (defun ccl-dump-write-register-jump (rrr cc
)
1036 (insert (format "write r%d, " rrr
))
1037 (ccl-dump-jump nil cc
))
1039 (defun ccl-dump-write-register-read-jump (rrr cc
)
1040 (insert (format "write r%d, read r%d, " rrr rrr
))
1041 (ccl-dump-jump nil cc
)
1042 (ccl-get-next-code) ; Skip dummy READ-JUMP
1045 (defun ccl-extract-arith-op (cc)
1046 (aref ccl-arith-table
(ash cc -
6)))
1048 (defun ccl-dump-write-expr-const (ignore cc
)
1049 (insert (format "write (r%d %s %d)\n"
1051 (ccl-extract-arith-op cc
)
1052 (ccl-get-next-code))))
1054 (defun ccl-dump-write-expr-register (ignore cc
)
1055 (insert (format "write (r%d %s r%d)\n"
1057 (ccl-extract-arith-op cc
)
1058 (logand (ash cc -
3) 7))))
1060 (defun ccl-dump-insert-char (cc)
1061 (cond ((= cc ?
\t) (insert " \"^I\""))
1062 ((= cc ?
\n) (insert " \"^J\""))
1063 (t (insert (format " \"%c\"" cc
)))))
1065 (defun ccl-dump-write-const-jump (ignore cc
)
1066 (let ((address ccl-current-ic
))
1067 (insert "write char")
1068 (ccl-dump-insert-char (ccl-get-next-code))
1070 (ccl-dump-jump nil cc address
)))
1072 (defun ccl-dump-write-const-read-jump (rrr cc
)
1073 (let ((address ccl-current-ic
))
1074 (insert "write char")
1075 (ccl-dump-insert-char (ccl-get-next-code))
1076 (insert (format ", read r%d, " rrr
))
1077 (ccl-dump-jump cc address
)
1078 (ccl-get-next-code) ; Skip dummy READ-JUMP
1081 (defun ccl-dump-write-string-jump (ignore cc
)
1082 (let ((address ccl-current-ic
)
1083 (len (ccl-get-next-code))
1087 (let ((code (ccl-get-next-code)))
1088 (insert (ash code -
16))
1089 (if (< (1+ i
) len
) (insert (logand (ash code -
8) 255)))
1090 (if (< (+ i
2) len
) (insert (logand code
255))))
1093 (ccl-dump-jump nil cc address
)))
1095 (defun ccl-dump-write-array-read-jump (rrr cc
)
1096 (let ((address ccl-current-ic
)
1097 (len (ccl-get-next-code))
1099 (insert (format "write array[r%d] of length %d,\n\t" rrr len
))
1101 (ccl-dump-insert-char (ccl-get-next-code))
1103 (insert (format "\n\tthen read r%d, " rrr
))
1104 (ccl-dump-jump nil cc address
)
1105 (ccl-get-next-code) ; Skip dummy READ-JUMP.
1108 (defun ccl-dump-read-jump (rrr cc
)
1109 (insert (format "read r%d, " rrr
))
1110 (ccl-dump-jump nil cc
))
1112 (defun ccl-dump-branch (rrr len
)
1113 (let ((jump-table-head ccl-current-ic
)
1115 (insert (format "jump to array[r%d] of length %d\n\t" rrr len
))
1117 (insert (format "%d " (+ jump-table-head
(ccl-get-next-code))))
1121 (defun ccl-dump-read-register (rrr cc
)
1122 (insert (format "read r%d (%d remaining)\n" rrr cc
)))
1124 (defun ccl-dump-read-branch (rrr len
)
1125 (insert (format "read r%d, " rrr
))
1126 (ccl-dump-branch rrr len
))
1128 (defun ccl-dump-write-register (rrr cc
)
1129 (insert (format "write r%d (%d remaining)\n" rrr cc
)))
1131 (defun ccl-dump-call (ignore cc
)
1132 (insert (format "call subroutine #%d\n" cc
)))
1134 (defun ccl-dump-write-const-string (rrr cc
)
1137 (insert "write char")
1138 (ccl-dump-insert-char cc
)
1144 (let ((code (ccl-get-next-code)))
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(%d) r%d r%d\n" tbl RRR rrr
))))
1249 (defun ccl-dump-iterate-multiple-map (rrr RRR Rrr
)
1250 (let ((notbl (ccl-get-next-code))
1252 (insert (format "iterate-multiple-map r%d r%d\n" RRR rrr
))
1253 (insert (format "\tnumber of maps is %d .\n\t [" notbl
))
1255 (setq id
(ccl-get-next-code))
1256 (insert (format "%S" id
))
1260 (defun ccl-dump-map-multiple (rrr RRR Rrr
)
1261 (let ((notbl (ccl-get-next-code))
1263 (insert (format "map-multiple r%d r%d\n" RRR rrr
))
1264 (insert (format "\tnumber of maps and separators is %d\n\t [" notbl
))
1266 (setq id
(ccl-get-next-code))
1269 (insert (format "%S " id
)))
1273 (defun ccl-dump-map-single (rrr RRR Rrr
)
1274 (let ((id (ccl-get-next-code)))
1275 (insert (format "map-single r%d r%d map(%S)\n" RRR rrr id
))))
1278 ;; CCL emulation staffs
1280 ;; Not yet implemented.
1282 ;; Auto-loaded functions.
1285 (defmacro declare-ccl-program
(name &optional vector
)
1286 "Declare NAME as a name of CCL program.
1288 To compile a CCL program which calls another CCL program not yet
1289 defined, it must be declared as a CCL program in advance.
1290 Optional arg VECTOR is a compiled CCL code of the CCL program."
1291 `(put ',name
'ccl-program-idx
(register-ccl-program ',name
,vector
)))
1294 (defmacro define-ccl-program
(name ccl-program
&optional doc
)
1295 "Set NAME the compiled code of CCL-PROGRAM.
1296 CCL-PROGRAM is `eval'ed before being handed to the CCL compiler `ccl-compile'.
1297 The compiled code is a vector of integers."
1298 `(let ((prog ,(ccl-compile (eval ccl-program
))))
1299 (defconst ,name prog
,doc
)
1300 (put ',name
'ccl-program-idx
(register-ccl-program ',name prog
))
1304 (defmacro check-ccl-program
(ccl-program &optional name
)
1305 "Check validity of CCL-PROGRAM.
1306 If CCL-PROGRAM is a symbol denoting a valid CCL program, return
1307 CCL-PROGRAM, else return nil.
1308 If CCL-PROGRAM is a vector and optional arg NAME (symbol) is supplied,
1309 register CCL-PROGRAM by name NAME, and return NAME."
1310 `(let ((result ,ccl-program
))
1311 (cond ((symbolp ,ccl-program
)
1312 (or (numberp (get ,ccl-program
'ccl-program-idx
))
1314 ((vectorp ,ccl-program
)
1316 (register-ccl-program result
,ccl-program
))
1322 (defun ccl-execute-with-args (ccl-prog &rest args
)
1323 "Execute CCL-PROGRAM with registers initialized by the remaining args.
1324 The return value is a vector of resulting CCL registers."
1325 (let ((reg (make-vector 8 0))
1327 (while (and args
(< i
8))
1328 (if (not (integerp (car args
)))
1329 (error "Arguments should be integer"))
1330 (aset reg i
(car args
))
1331 (setq args
(cdr args
) i
(1+ i
)))
1332 (ccl-execute ccl-prog reg
)