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 ...])
79 ;; | (write EXPRESSION)
80 ;; | (write integer) | (write string) | (write REG ARRAY)
82 ;; CALL := (call ccl-program-name)
85 ;; REG := r0 | r1 | r2 | r3 | r4 | r5 | r6 | r7
86 ;; ARG := REG | integer
88 ;; + | - | * | / | % | & | '|' | ^ | << | >> | <8 | >8 | //
89 ;; | < | > | == | <= | >= | != | de-sjis | en-sjis
90 ;; ASSIGNMENT_OPERATOR :=
91 ;; += | -= | *= | /= | %= | &= | '|=' | ^= | <<= | >>=
92 ;; ARRAY := '[' interger ... ']'
96 (defconst ccl-command-table
97 [if branch loop break repeat write-repeat write-read-repeat
98 read read-if read-branch write call end]
99 "*Vector of CCL commands (symbols).")
101 ;; Put a property to each symbol of CCL commands for the compiler.
102 (let (op (i 0) (len (length ccl-command-table)))
104 (setq op (aref ccl-command-table i))
105 (put op 'ccl-compile-function (intern (format "ccl-compile-%s" op)))
108 (defconst ccl-code-table
116 write-register-read-jump
118 write-const-read-jump
120 write-array-read-jump
132 set-assign-expr-const
133 set-assign-expr-register
137 jump-cond-expr-register
138 read-jump-cond-expr-const
139 read-jump-cond-expr-register
141 "*Vector of CCL compiled codes (symbols).")
143 ;; Put a property to each symbol of CCL codes for the disassembler.
144 (let (code (i 0) (len (length ccl-code-table)))
146 (setq code (aref ccl-code-table i))
147 (put code 'ccl-code i)
148 (put code 'ccl-dump-function (intern (format "ccl-dump-%s" code)))
151 (defconst ccl-jump-code-list
152 '(jump jump-cond write-register-jump write-register-read-jump
153 write-const-jump write-const-read-jump write-string-jump
154 write-array-read-jump read-jump))
156 ;; Put a property `jump-flag' to each CCL code which execute jump in
158 (let ((l ccl-jump-code-list))
160 (put (car l) 'jump-flag t)
163 (defconst ccl-register-table
164 [r0 r1 r2 r3 r4 r5 r6 r7]
165 "*Vector of CCL registers (symbols).")
167 ;; Put a property to indicate register number to each symbol of CCL.
169 (let (reg (i 0) (len (length ccl-register-table)))
171 (setq reg (aref ccl-register-table i))
172 (put reg 'ccl-register-number i)
175 (defconst ccl-arith-table
176 [+ - * / % & | ^ << >> <8 >8 // nil nil nil
177 < > == <= >= != de-sjis en-sjis]
178 "*Vector of CCL arithmetic/logical operators (symbols).")
180 ;; Put a property to each symbol of CCL operators for the compiler.
181 (let (arith (i 0) (len (length ccl-arith-table)))
183 (setq arith (aref ccl-arith-table i))
184 (if arith (put arith 'ccl-arith-code i))
187 (defconst ccl-assign-arith-table
188 [+= -= *= /= %= &= |= ^= <<= >>= <8= >8= //=]
189 "*Vector of CCL assignment operators (symbols).")
191 ;; Put a property to each symbol of CCL assignment operators for the compiler.
192 (let (arith (i 0) (len (length ccl-assign-arith-table)))
194 (setq arith (aref ccl-assign-arith-table i))
195 (put arith 'ccl-self-arith-code i)
198 (defvar ccl-program-vector nil
199 "Working vector of CCL codes produced by CCL compiler.")
200 (defvar ccl-current-ic 0
201 "The current index for `ccl-program-vector'.")
203 ;; Embed integer DATA in `ccl-program-vector' at `ccl-current-ic' and
204 ;; increment it. If IC is specified, embed DATA at IC.
205 (defun ccl-embed-data (data &optional ic)
206 (let ((val (if (characterp data) (char-int data) data)))
208 (aset ccl-program-vector ic val)
209 (aset ccl-program-vector ccl-current-ic val)
210 (setq ccl-current-ic (1+ ccl-current-ic)))))
212 ;; Embed string STR of length LEN in `ccl-program-vector' at
214 (defun ccl-embed-string (len str)
217 (ccl-embed-data (logior (ash (aref str i) 16)
219 (ash (aref str (1+ i)) 8)
226 ;; Embed a relative jump address to `ccl-current-ic' in
227 ;; `ccl-program-vector' at IC without altering the other bit field.
228 (defun ccl-embed-current-address (ic)
229 (let ((relative (- ccl-current-ic (1+ ic))))
230 (aset ccl-program-vector ic
231 (logior (aref ccl-program-vector ic) (ash relative 8)))))
233 ;; Embed CCL code for the operation OP and arguments REG and DATA in
234 ;; `ccl-program-vector' at `ccl-current-ic' in the following format.
235 ;; |----------------- integer (28-bit) ------------------|
236 ;; |------------ 20-bit ------------|- 3-bit --|- 5-bit -|
237 ;; |------------- DATA -------------|-- REG ---|-- OP ---|
238 ;; If REG2 is specified, embed a code in the following format.
239 ;; |------- 17-bit ------|- 3-bit --|- 3-bit --|- 5-bit -|
240 ;; |-------- DATA -------|-- REG2 --|-- REG ---|-- OP ---|
242 ;; If REG is a CCL register symbol (e.g. r0, r1...), the register
243 ;; number is embedded. If OP is one of unconditional jumps, DATA is
244 ;; changed to an relative jump address.
246 (defun ccl-embed-code (op reg data &optional reg2)
247 (if (and (> data 0) (get op 'jump-flag))
248 ;; DATA is an absolute jump address. Make it relative to the
249 ;; next of jump code.
250 (setq data (- data (1+ ccl-current-ic))))
251 (let ((code (logior (get op 'ccl-code)
253 (if (symbolp reg) (get reg 'ccl-register-number) reg) 5)
255 (logior (ash (get reg2 'ccl-register-number) 8)
258 (aset ccl-program-vector ccl-current-ic code)
259 (setq ccl-current-ic (1+ ccl-current-ic))))
261 ;; Just advance `ccl-current-ic' by INC.
262 (defun ccl-increment-ic (inc)
263 (setq ccl-current-ic (+ ccl-current-ic inc)))
266 (defun ccl-program-p (obj)
267 "T if OBJECT is a valid CCL compiled code."
269 (let ((i 0) (len (length obj)) (flag t))
272 (while (and flag (< i len))
273 (setq flag (integerp (aref obj i)))
277 ;; If non-nil, index of the start of the current loop.
278 (defvar ccl-loop-head nil)
279 ;; If non-nil, list of absolute addresses of the breaking points of
281 (defvar ccl-breaks nil)
284 (defun ccl-compile (ccl-program)
285 "Return a compiled code of CCL-PROGRAM as a vector of integer."
286 (if (or (null (consp ccl-program))
287 (null (integer-or-char-p (car ccl-program)))
288 (null (listp (car (cdr ccl-program)))))
289 (error "CCL: Invalid CCL program: %s" ccl-program))
290 (if (null (vectorp ccl-program-vector))
291 (setq ccl-program-vector (make-vector 8192 0)))
292 (setq ccl-loop-head nil ccl-breaks nil)
293 (setq ccl-current-ic 0)
295 ;; The first element is the buffer magnification.
296 (ccl-embed-data (car ccl-program))
298 ;; The second element is the address of the start CCL code for
299 ;; processing end of input buffer (we call it eof-processor). We
303 ;; Compile the main body of the CCL program.
304 (ccl-compile-1 (car (cdr ccl-program)))
306 ;; Embed the address of eof-processor.
307 (ccl-embed-data ccl-current-ic 1)
309 ;; Then compile eof-processor.
310 (if (nth 2 ccl-program)
311 (ccl-compile-1 (nth 2 ccl-program)))
313 ;; At last, embed termination code.
314 (ccl-embed-code 'end 0 0)
316 (let ((vec (make-vector ccl-current-ic 0))
318 (while (< i ccl-current-ic)
319 (aset vec i (aref ccl-program-vector i))
323 ;; Signal syntax error.
324 (defun ccl-syntax-error (cmd)
325 (error "CCL: Syntax error: %s" cmd))
327 ;; Check if ARG is a valid CCL register.
328 (defun ccl-check-register (arg cmd)
329 (if (get arg 'ccl-register-number)
331 (error "CCL: Invalid register %s in %s." arg cmd)))
333 ;; Check if ARG is a valid CCL command.
334 (defun ccl-check-compile-function (arg cmd)
335 (or (get arg 'ccl-compile-function)
336 (error "CCL: Invalid command: %s" cmd)))
338 ;; In the following code, most ccl-compile-XXXX functions return t if
339 ;; they end with unconditional jump, else return nil.
341 ;; Compile CCL-BLOCK (see the syntax above).
342 (defun ccl-compile-1 (ccl-block)
343 (let (unconditional-jump
345 (if (or (integer-or-char-p ccl-block)
347 (and ccl-block (symbolp (car ccl-block))))
348 ;; This block consists of single statement.
349 (setq ccl-block (list ccl-block)))
351 ;; Now CCL-BLOCK is a list of statements. Compile them one by
354 (setq cmd (car ccl-block))
355 (setq unconditional-jump
356 (cond ((integer-or-char-p cmd)
357 ;; SET statement for the register 0.
358 (ccl-compile-set (list 'r0 '= cmd)))
361 ;; WRITE statement of string argument.
362 (ccl-compile-write-string cmd))
365 ;; The other statements.
366 (cond ((eq (nth 1 cmd) '=)
367 ;; SET statement of the form `(REG = EXPRESSION)'.
368 (ccl-compile-set cmd))
370 ((and (symbolp (nth 1 cmd))
371 (get (nth 1 cmd) 'ccl-self-arith-code))
372 ;; SET statement with an assignment operation.
373 (ccl-compile-self-set cmd))
376 (funcall (ccl-check-compile-function (car cmd) cmd)
380 (ccl-syntax-error cmd))))
381 (setq ccl-block (cdr ccl-block)))
384 (defconst ccl-max-short-const (ash 1 19))
385 (defconst ccl-min-short-const (ash -1 19))
387 ;; Compile SET statement.
388 (defun ccl-compile-set (cmd)
389 (let ((rrr (ccl-check-register (car cmd) cmd))
392 ;; CMD has the form `(RRR = (XXX OP YYY))'.
393 (ccl-compile-expression rrr right))
395 ((integer-or-char-p right)
396 ;; CMD has the form `(RRR = integer)'.
397 (if (and (<= right ccl-max-short-const)
398 (>= right ccl-min-short-const))
399 (ccl-embed-code 'set-short-const rrr right)
400 (ccl-embed-code 'set-const rrr 0)
401 (ccl-embed-data right)))
404 ;; CMD has the form `(RRR = rrr [ array ])'.
405 (ccl-check-register right cmd)
406 (let ((ary (nth 3 cmd)))
408 (let ((i 0) (len (length ary)))
409 (ccl-embed-code 'set-array rrr len right)
411 (ccl-embed-data (aref ary i))
413 (ccl-embed-code 'set-register rrr 0 right))))))
416 ;; Compile SET statement with ASSIGNMENT_OPERATOR.
417 (defun ccl-compile-self-set (cmd)
418 (let ((rrr (ccl-check-register (car cmd) cmd))
421 ;; CMD has the form `(RRR ASSIGN_OP (XXX OP YYY))', compile
422 ;; the right hand part as `(r7 = (XXX OP YYY))' (note: the
423 ;; register 7 can be used for storing temporary value).
425 (ccl-compile-expression 'r7 right)
427 ;; Now CMD has the form `(RRR ASSIGN_OP ARG)'. Compile it as
428 ;; `(RRR = (RRR OP ARG))'.
429 (ccl-compile-expression
431 (list rrr (intern (substring (symbol-name (nth 1 cmd)) 0 -1)) right)))
434 ;; Compile SET statement of the form `(RRR = EXPR)'.
435 (defun ccl-compile-expression (rrr expr)
436 (let ((left (car expr))
437 (op (get (nth 1 expr) 'ccl-arith-code))
438 (right (nth 2 expr)))
441 ;; EXPR has the form `((EXPR2 OP2 ARG) OP RIGHT)'. Compile
442 ;; the first term as `(r7 = (EXPR2 OP2 ARG)).'
443 (ccl-compile-expression 'r7 left)
446 ;; Now EXPR has the form (LEFT OP RIGHT).
448 ;; Compile this SET statement as `(RRR OP= RIGHT)'.
449 (if (integer-or-char-p right)
451 (ccl-embed-code 'set-assign-expr-const rrr (ash op 3) 'r0)
452 (ccl-embed-data right))
453 (ccl-check-register right expr)
454 (ccl-embed-code 'set-assign-expr-register rrr (ash op 3) right))
456 ;; Compile this SET statement as `(RRR = (LEFT OP RIGHT))'.
457 (if (integer-or-char-p right)
459 (ccl-embed-code 'set-expr-const rrr (ash op 3) left)
460 (ccl-embed-data right))
461 (ccl-check-register right expr)
462 (ccl-embed-code 'set-expr-register
464 (logior (ash op 3) (get right 'ccl-register-number))
467 ;; Compile WRITE statement with string argument.
468 (defun ccl-compile-write-string (str)
469 (let ((len (length str)))
470 (ccl-embed-code 'write-const-string 1 len)
471 (ccl-embed-string len str))
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 (defun ccl-compile-if (cmd &optional read-flag)
478 (if (and (/= (length cmd) 3) (/= (length cmd) 4))
479 (error "CCL: Invalid number of arguments: %s" cmd))
480 (let ((condition (nth 1 cmd))
481 (true-cmds (nth 2 cmd))
482 (false-cmds (nth 3 cmd))
485 (if (and (listp condition)
486 (listp (car condition)))
487 ;; If CONDITION is a nested expression, the inner expression
488 ;; should be compiled at first as SET statement, i.e.:
489 ;; `(if ((X OP2 Y) OP Z) ...)' is compiled into two statements:
490 ;; `(r7 = (X OP2 Y)) (if (r7 OP Z) ...)'.
492 (ccl-compile-expression 'r7 (car condition))
493 (setq condition (cons 'r7 (cdr condition)))
494 (setq cmd (cons (car cmd)
495 (cons condition (cdr (cdr cmd)))))))
497 (setq jump-cond-address ccl-current-ic)
498 ;; Compile CONDITION.
499 (if (symbolp condition)
500 ;; CONDITION is a register.
502 (ccl-check-register condition cmd)
503 (ccl-embed-code 'jump-cond condition 0))
504 ;; CONDITION is a simple expression of the form (RRR OP ARG).
505 (let ((rrr (car condition))
506 (op (get (nth 1 condition) 'ccl-arith-code))
507 (arg (nth 2 condition)))
508 (ccl-check-register rrr cmd)
509 (if (integer-or-char-p arg)
511 (ccl-embed-code (if read-flag 'read-jump-cond-expr-const
512 'jump-cond-expr-const)
515 (ccl-embed-data arg))
516 (ccl-check-register arg cmd)
517 (ccl-embed-code (if read-flag 'read-jump-cond-expr-register
518 'jump-cond-expr-register)
521 (ccl-embed-data (get arg 'ccl-register-number)))))
523 ;; Compile TRUE-PART.
524 (let ((unconditional-jump (ccl-compile-1 true-cmds)))
525 (if (null false-cmds)
526 ;; This is the place to jump to if condition is false.
527 (ccl-embed-current-address jump-cond-address)
528 (let (end-true-part-address)
529 (if (not unconditional-jump)
531 ;; If TRUE-PART does not end with unconditional jump, we
532 ;; have to jump to the end of FALSE-PART from here.
533 (setq end-true-part-address ccl-current-ic)
534 (ccl-embed-code 'jump 0 0)))
535 ;; This is the place to jump to if CONDITION is false.
536 (ccl-embed-current-address jump-cond-address)
537 ;; Compile FALSE-PART.
538 (setq unconditional-jump
539 (and (ccl-compile-1 false-cmds) unconditional-jump))
540 (if end-true-part-address
541 ;; This is the place to jump to after the end of TRUE-PART.
542 (ccl-embed-current-address end-true-part-address))))
543 unconditional-jump)))
545 ;; Compile BRANCH statement.
546 (defun ccl-compile-branch (cmd)
547 (if (< (length cmd) 3)
548 (error "CCL: Invalid number of arguments: %s" cmd))
549 (ccl-compile-branch-blocks 'branch
550 (ccl-compile-branch-expression (nth 1 cmd) cmd)
553 ;; Compile READ statement of the form `(read-branch EXPR BLOCK0 BLOCK1 ...)'.
554 (defun ccl-compile-read-branch (cmd)
555 (if (< (length cmd) 3)
556 (error "CCL: Invalid number of arguments: %s" cmd))
557 (ccl-compile-branch-blocks 'read-branch
558 (ccl-compile-branch-expression (nth 1 cmd) cmd)
561 ;; Compile EXPRESSION part of BRANCH statement and return register
562 ;; which holds a value of the expression.
563 (defun ccl-compile-branch-expression (expr cmd)
565 ;; EXPR has the form `(EXPR2 OP ARG)'. Compile it as SET
566 ;; statement of the form `(r7 = (EXPR2 OP ARG))'.
568 (ccl-compile-expression 'r7 expr)
570 (ccl-check-register expr cmd)))
572 ;; Compile BLOCKs of BRANCH statement. CODE is 'branch or 'read-branch.
573 ;; REG is a register which holds a value of EXPRESSION part. BLOCKs
574 ;; is a list of CCL-BLOCKs.
575 (defun ccl-compile-branch-blocks (code rrr blocks)
576 (let ((branches (length blocks))
578 jump-table-head-address
581 block-unconditional-jump)
582 (ccl-embed-code code rrr branches)
583 (setq jump-table-head-address ccl-current-ic)
584 ;; The size of jump table is the number of blocks plus 1 (for the
585 ;; case RRR is out of range).
586 (ccl-increment-ic (1+ branches))
587 (setq empty-block-indexes (list branches))
588 ;; Compile each block.
591 (if (null (car blocks))
592 ;; This block is empty.
593 (setq empty-block-indexes (cons branch-idx empty-block-indexes)
594 block-unconditional-jump t)
595 ;; This block is not empty.
596 (ccl-embed-data (- ccl-current-ic jump-table-head-address)
597 (+ jump-table-head-address branch-idx))
598 (setq block-unconditional-jump (ccl-compile-1 (car blocks)))
599 (if (not block-unconditional-jump)
601 ;; Jump address of the end of branches are embedded later.
602 ;; For the moment, just remember where to embed them.
603 (setq block-tail-addresses
604 (cons ccl-current-ic block-tail-addresses))
605 (ccl-embed-code 'jump 0 0))))
606 (setq branch-idx (1+ branch-idx))
607 (setq blocks (cdr blocks)))
608 (if (not block-unconditional-jump)
609 ;; We don't need jump code at the end of the last block.
610 (setq block-tail-addresses (cdr block-tail-addresses)
611 ccl-current-ic (1- ccl-current-ic)))
612 ;; Embed jump address at the tailing jump commands of blocks.
613 (while block-tail-addresses
614 (ccl-embed-current-address (car block-tail-addresses))
615 (setq block-tail-addresses (cdr block-tail-addresses)))
616 ;; For empty blocks, make entries in the jump table point directly here.
617 (while empty-block-indexes
618 (ccl-embed-data (- ccl-current-ic jump-table-head-address)
619 (+ jump-table-head-address (car empty-block-indexes)))
620 (setq empty-block-indexes (cdr empty-block-indexes))))
621 ;; Branch command ends by unconditional jump if RRR is out of range.
624 ;; Compile LOOP statement.
625 (defun ccl-compile-loop (cmd)
626 (if (< (length cmd) 2)
627 (error "CCL: Invalid number of arguments: %s" cmd))
628 (let* ((ccl-loop-head ccl-current-ic)
634 (setq unconditional-jump t)
636 (setq unconditional-jump
637 (and (ccl-compile-1 (car cmd)) unconditional-jump))
638 (setq cmd (cdr cmd)))
641 ;; Embed jump address for break statements encountered in
644 (ccl-embed-current-address (car ccl-breaks))
645 (setq ccl-breaks (cdr ccl-breaks))))
648 ;; Compile BREAK statement.
649 (defun ccl-compile-break (cmd)
650 (if (/= (length cmd) 1)
651 (error "CCL: Invalid number of arguments: %s" cmd))
652 (if (null ccl-loop-head)
653 (error "CCL: No outer loop: %s" cmd))
654 (setq ccl-breaks (cons ccl-current-ic ccl-breaks))
655 (ccl-embed-code 'jump 0 0)
658 ;; Compile REPEAT statement.
659 (defun ccl-compile-repeat (cmd)
660 (if (/= (length cmd) 1)
661 (error "CCL: Invalid number of arguments: %s" cmd))
662 (if (null ccl-loop-head)
663 (error "CCL: No outer loop: %s" cmd))
664 (ccl-embed-code 'jump 0 ccl-loop-head)
667 ;; Compile WRITE-REPEAT statement.
668 (defun ccl-compile-write-repeat (cmd)
669 (if (/= (length cmd) 2)
670 (error "CCL: Invalid number of arguments: %s" cmd))
671 (if (null ccl-loop-head)
672 (error "CCL: No outer loop: %s" cmd))
673 (let ((arg (nth 1 cmd)))
674 (cond ((integer-or-char-p arg)
675 (ccl-embed-code 'write-const-jump 0 ccl-loop-head)
676 (ccl-embed-data arg))
678 (let ((len (length arg))
680 (ccl-embed-code 'write-string-jump 0 ccl-loop-head)
682 (ccl-embed-string len arg)))
684 (ccl-check-register arg cmd)
685 (ccl-embed-code 'write-register-jump arg ccl-loop-head))))
688 ;; Compile WRITE-READ-REPEAT statement.
689 (defun ccl-compile-write-read-repeat (cmd)
690 (if (or (< (length cmd) 2) (> (length cmd) 3))
691 (error "CCL: Invalid number of arguments: %s" cmd))
692 (if (null ccl-loop-head)
693 (error "CCL: No outer loop: %s" cmd))
694 (let ((rrr (ccl-check-register (nth 1 cmd) cmd))
697 (ccl-embed-code 'write-register-read-jump rrr ccl-loop-head))
698 ((integer-or-char-p arg)
699 (ccl-embed-code 'write-const-read-jump rrr arg ccl-loop-head))
701 (let ((len (length arg))
703 (ccl-embed-code 'write-array-read-jump rrr ccl-loop-head)
706 (ccl-embed-data (aref arg i))
709 (error "CCL: Invalid argument %s: %s" arg cmd)))
710 (ccl-embed-code 'read-jump rrr ccl-loop-head))
713 ;; Compile READ statement.
714 (defun ccl-compile-read (cmd)
715 (if (< (length cmd) 2)
716 (error "CCL: Invalid number of arguments: %s" cmd))
717 (let* ((args (cdr cmd))
718 (i (1- (length args))))
720 (let ((rrr (ccl-check-register (car args) cmd)))
721 (ccl-embed-code 'read-register rrr i)
722 (setq args (cdr args) i (1- i)))))
725 ;; Compile READ-IF statement.
726 (defun ccl-compile-read-if (cmd)
727 (ccl-compile-if cmd 'read))
729 ;; Compile WRITE statement.
730 (defun ccl-compile-write (cmd)
731 (if (< (length cmd) 2)
732 (error "CCL: Invalid number of arguments: %s" cmd))
733 (let ((rrr (nth 1 cmd)))
734 (cond ((integer-or-char-p rrr)
735 (ccl-embed-code 'write-const-string 0 rrr))
737 (ccl-compile-write-string rrr))
738 ((and (symbolp rrr) (vectorp (nth 2 cmd)))
739 (ccl-check-register rrr cmd)
740 ;; CMD has the form `(write REG ARRAY)'.
741 (let* ((arg (nth 2 cmd))
744 (ccl-embed-code 'write-array rrr len)
746 (if (not (integer-or-char-p (aref arg i)))
747 (error "CCL: Invalid argument %s: %s" arg cmd))
748 (ccl-embed-data (aref arg i))
752 ;; CMD has the form `(write REG ...)'.
753 (let* ((args (cdr cmd))
754 (i (1- (length args))))
756 (setq rrr (ccl-check-register (car args) cmd))
757 (ccl-embed-code 'write-register rrr i)
758 (setq args (cdr args) i (1- i)))))
761 ;; CMD has the form `(write (LEFT OP RIGHT))'.
762 (let ((left (car rrr))
763 (op (get (nth 1 rrr) 'ccl-arith-code))
767 ;; RRR has the form `((EXPR OP2 ARG) OP RIGHT)'.
768 ;; Compile the first term as `(r7 = (EXPR OP2 ARG))'.
769 (ccl-compile-expression 'r7 left)
771 ;; Now RRR has the form `(ARG OP RIGHT)'.
772 (if (integer-or-char-p right)
774 (ccl-embed-code 'write-expr-const 0 (ash op 3) left)
775 (ccl-embed-data right))
776 (ccl-check-register right rrr)
777 (ccl-embed-code 'write-expr-register 0
779 (get right 'ccl-register-number))))))
782 (error "CCL: Invalid argument: %s" cmd))))
785 ;; Compile CALL statement.
786 (defun ccl-compile-call (cmd)
787 (if (/= (length cmd) 2)
788 (error "CCL: Invalid number of arguments: %s" cmd))
789 (if (not (symbolp (nth 1 cmd)))
790 (error "CCL: Subroutine should be a symbol: %s" cmd))
791 (let* ((name (nth 1 cmd))
792 (idx (get name 'ccl-program-idx)))
794 (error "CCL: Unknown subroutine name: %s" name))
795 (ccl-embed-code 'call 0 idx))
798 ;; Compile END statement.
799 (defun ccl-compile-end (cmd)
800 (if (/= (length cmd) 1)
801 (error "CCL: Invalid number of arguments: %s" cmd))
802 (ccl-embed-code 'end 0 0)
807 ;; To avoid byte-compiler warning.
811 (defun ccl-dump (ccl-code)
812 "Disassemble compiled CCL-CODE."
813 (let ((len (length ccl-code))
814 (buffer-mag (aref ccl-code 0)))
815 (cond ((= buffer-mag 0)
816 (insert "Don't output anything.\n"))
818 (insert "Out-buffer must be as large as in-buffer.\n"))
821 (format "Out-buffer must be %d times bigger than in-buffer.\n"
823 (insert "Main-body:\n")
824 (setq ccl-current-ic 2)
825 (if (> (aref ccl-code 1) 0)
827 (while (< ccl-current-ic (aref ccl-code 1))
829 (insert "At EOF:\n")))
830 (while (< ccl-current-ic len)
834 ;; Return a CCL code in `ccl-code' at `ccl-current-ic'.
835 (defun ccl-get-next-code ()
837 (aref ccl-code ccl-current-ic)
838 (setq ccl-current-ic (1+ ccl-current-ic))))
841 (let* ((code (ccl-get-next-code))
842 (cmd (aref ccl-code-table (logand code 31)))
843 (rrr (ash (logand code 255) -5))
845 (insert (format "%5d:[%s] " (1- ccl-current-ic) cmd))
846 (funcall (get cmd 'ccl-dump-function) rrr cc)))
848 (defun ccl-dump-set-register (rrr cc)
849 (insert (format "r%d = r%d\n" rrr cc)))
851 (defun ccl-dump-set-short-const (rrr cc)
852 (insert (format "r%d = %d\n" rrr cc)))
854 (defun ccl-dump-set-const (rrr ignore)
855 (insert (format "r%d = %d\n" rrr (ccl-get-next-code))))
857 (defun ccl-dump-set-array (rrr cc)
858 (let ((rrr2 (logand cc 7))
861 (insert (format "r%d = array[r%d] of length %d\n\t"
864 (insert (format "%d " (ccl-get-next-code)))
868 (defun ccl-dump-jump (ignore cc &optional address)
869 (insert (format "jump to %d(" (+ (or address ccl-current-ic) cc)))
872 (insert (format "%d)\n" (1+ cc))))
874 (defun ccl-dump-jump-cond (rrr cc)
875 (insert (format "if (r%d == 0), " rrr))
876 (ccl-dump-jump nil cc))
878 (defun ccl-dump-write-register-jump (rrr cc)
879 (insert (format "write r%d, " rrr))
880 (ccl-dump-jump nil cc))
882 (defun ccl-dump-write-register-read-jump (rrr cc)
883 (insert (format "write r%d, read r%d, " rrr rrr))
884 (ccl-dump-jump nil cc)
885 (ccl-get-next-code) ; Skip dummy READ-JUMP
888 (defun ccl-extract-arith-op (cc)
889 (aref ccl-arith-table (ash cc -6)))
891 (defun ccl-dump-write-expr-const (ignore cc)
892 (insert (format "write (r%d %s %d)\n"
894 (ccl-extract-arith-op cc)
895 (ccl-get-next-code))))
897 (defun ccl-dump-write-expr-register (ignore cc)
898 (insert (format "write (r%d %s r%d)\n"
900 (ccl-extract-arith-op cc)
901 (logand (ash cc -3) 7))))
903 (defun ccl-dump-insert-char (cc)
904 (cond ((= cc ?\t) (insert " \"^I\""))
905 ((= cc ?\n) (insert " \"^J\""))
906 (t (insert (format " \"%c\"" cc)))))
908 (defun ccl-dump-write-const-jump (ignore cc)
909 (let ((address ccl-current-ic))
910 (insert "write char")
911 (ccl-dump-insert-char (ccl-get-next-code))
913 (ccl-dump-jump nil cc address)))
915 (defun ccl-dump-write-const-read-jump (rrr cc)
916 (let ((address ccl-current-ic))
917 (insert "write char")
918 (ccl-dump-insert-char (ccl-get-next-code))
919 (insert (format ", read r%d, " rrr))
920 (ccl-dump-jump cc address)
921 (ccl-get-next-code) ; Skip dummy READ-JUMP
924 (defun ccl-dump-write-string-jump (ignore cc)
925 (let ((address ccl-current-ic)
926 (len (ccl-get-next-code))
930 (let ((code (ccl-get-next-code)))
931 (insert (ash code -16))
932 (if (< (1+ i) len) (insert (logand (ash code -8) 255)))
933 (if (< (+ i 2) len) (insert (logand code 255))))
936 (ccl-dump-jump nil cc address)))
938 (defun ccl-dump-write-array-read-jump (rrr cc)
939 (let ((address ccl-current-ic)
940 (len (ccl-get-next-code))
942 (insert (format "write array[r%d] of length %d,\n\t" rrr len))
944 (ccl-dump-insert-char (ccl-get-next-code))
946 (insert (format "\n\tthen read r%d, " rrr))
947 (ccl-dump-jump nil cc address)
948 (ccl-get-next-code) ; Skip dummy READ-JUMP.
951 (defun ccl-dump-read-jump (rrr cc)
952 (insert (format "read r%d, " rrr))
953 (ccl-dump-jump nil cc))
955 (defun ccl-dump-branch (rrr len)
956 (let ((jump-table-head ccl-current-ic)
958 (insert (format "jump to array[r%d] of length %d\n\t" rrr len))
960 (insert (format "%d " (+ jump-table-head (ccl-get-next-code))))
964 (defun ccl-dump-read-register (rrr cc)
965 (insert (format "read r%d (%d remaining)\n" rrr cc)))
967 (defun ccl-dump-read-branch (rrr len)
968 (insert (format "read r%d, " rrr))
969 (ccl-dump-branch rrr len))
971 (defun ccl-dump-write-register (rrr cc)
972 (insert (format "write r%d (%d remaining)\n" rrr cc)))
974 (defun ccl-dump-call (ignore cc)
975 (insert (format "call subroutine #%d\n" cc)))
977 (defun ccl-dump-write-const-string (rrr cc)
980 (insert "write char")
981 (ccl-dump-insert-char cc)
987 (let ((code (ccl-get-next-code)))
988 (insert (format "%c" (lsh code -16)))
990 (insert (format "%c" (logand (lsh code -8) 255))))
992 (insert (format "%c" (logand code 255))))
996 (defun ccl-dump-write-array (rrr cc)
998 (insert (format "write array[r%d] of length %d\n\t" rrr cc))
1000 (ccl-dump-insert-char (ccl-get-next-code))
1004 (defun ccl-dump-end (&rest ignore)
1007 (defun ccl-dump-set-assign-expr-const (rrr cc)
1008 (insert (format "r%d %s= %d\n"
1010 (ccl-extract-arith-op cc)
1011 (ccl-get-next-code))))
1013 (defun ccl-dump-set-assign-expr-register (rrr cc)
1014 (insert (format "r%d %s= r%d\n"
1016 (ccl-extract-arith-op cc)
1019 (defun ccl-dump-set-expr-const (rrr cc)
1020 (insert (format "r%d = r%d %s %d\n"
1023 (ccl-extract-arith-op cc)
1024 (ccl-get-next-code))))
1026 (defun ccl-dump-set-expr-register (rrr cc)
1027 (insert (format "r%d = r%d %s r%d\n"
1030 (ccl-extract-arith-op cc)
1031 (logand (ash cc -3) 7))))
1033 (defun ccl-dump-jump-cond-expr-const (rrr cc)
1034 (let ((address ccl-current-ic))
1035 (insert (format "if !(r%d %s %d), "
1037 (aref ccl-arith-table (ccl-get-next-code))
1038 (ccl-get-next-code)))
1039 (ccl-dump-jump nil cc address)))
1041 (defun ccl-dump-jump-cond-expr-register (rrr cc)
1042 (let ((address ccl-current-ic))
1043 (insert (format "if !(r%d %s r%d), "
1045 (aref ccl-arith-table (ccl-get-next-code))
1046 (ccl-get-next-code)))
1047 (ccl-dump-jump nil cc address)))
1049 (defun ccl-dump-read-jump-cond-expr-const (rrr cc)
1050 (insert (format "read r%d, " rrr))
1051 (ccl-dump-jump-cond-expr-const rrr cc))
1053 (defun ccl-dump-read-jump-cond-expr-register (rrr cc)
1054 (insert (format "read r%d, " rrr))
1055 (ccl-dump-jump-cond-expr-register rrr cc))
1057 (defun ccl-dump-binary (ccl-code)
1058 (let ((len (length ccl-code))
1061 (let ((code (aref ccl-code i))
1064 (insert (if (= (logand code (ash 1 j)) 0) ?0 ?1))
1066 (setq code (logand code 31))
1067 (if (< code (length ccl-code-table))
1068 (insert (format ":%s" (aref ccl-code-table code))))
1072 ;; CCL emulation staffs
1074 ;; Not yet implemented.
1077 (defmacro declare-ccl-program (name)
1078 "Declare NAME as a name of CCL program.
1080 To compile a CCL program which calls another CCL program not yet
1081 defined, it must be declared as a CCL program in advance."
1082 `(put ',name 'ccl-program-idx (register-ccl-program ',name nil)))
1085 (defmacro define-ccl-program (name ccl-program &optional doc)
1086 "Set NAME the compiled code of CCL-PROGRAM.
1087 CCL-PROGRAM is `eval'ed before being handed to the CCL compiler `ccl-compile'.
1088 The compiled code is a vector of integers."
1089 `(let ((prog ,(ccl-compile (eval ccl-program))))
1090 (defconst ,name prog ,doc)
1091 (put ',name 'ccl-program-idx (register-ccl-program ',name prog))
1095 (defun ccl-execute-with-args (ccl-prog &rest args)
1096 "Execute CCL-PROGRAM with registers initialized by the remaining args.
1097 The return value is a vector of resulting CCL registeres."
1098 (let ((reg (make-vector 8 0))
1100 (while (and args (< i 8))
1101 (if (not (integerp (car args)))
1102 (error "Arguments should be integer"))
1103 (aset reg i (car args))
1104 (setq args (cdr args) i (1+ i)))
1105 (ccl-execute ccl-prog reg)