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 ...])
77 ;; | (write EXPRESSION)
78 ;; | (write integer) | (write string) | (write REG ARRAY)
80 ;; CALL := (call ccl-program-name)
83 ;; REG := r0 | r1 | r2 | r3 | r4 | r5 | r6 | r7
84 ;; ARG := REG | integer
86 ;; + | - | * | / | % | & | '|' | ^ | << | >> | <8 | >8 | //
87 ;; | < | > | == | <= | >= | != | de-sjis | en-sjis
88 ;; ASSIGNMENT_OPERATOR :=
89 ;; += | -= | *= | /= | %= | &= | '|=' | ^= | <<= | >>=
90 ;; ARRAY := '[' interger ... ']'
94 (defconst ccl-command-table
95 [if branch loop break repeat write-repeat write-read-repeat
96 read read-if read-branch write call end]
97 "*Vector of CCL commands (symbols).")
99 ;; Put a property to each symbol of CCL commands for the compiler.
100 (let (op (i 0) (len (length ccl-command-table)))
102 (setq op (aref ccl-command-table i))
103 (put op 'ccl-compile-function (intern (format "ccl-compile-%s" op)))
106 (defconst ccl-code-table
114 write-register-read-jump
116 write-const-read-jump
118 write-array-read-jump
130 set-assign-expr-const
131 set-assign-expr-register
135 jump-cond-expr-register
136 read-jump-cond-expr-const
137 read-jump-cond-expr-register
139 "*Vector of CCL compiled codes (symbols).")
141 ;; Put a property to each symbol of CCL codes for the disassembler.
142 (let (code (i 0) (len (length ccl-code-table)))
144 (setq code (aref ccl-code-table i))
145 (put code 'ccl-code i)
146 (put code 'ccl-dump-function (intern (format "ccl-dump-%s" code)))
149 (defconst ccl-jump-code-list
150 '(jump jump-cond write-register-jump write-register-read-jump
151 write-const-jump write-const-read-jump write-string-jump
152 write-array-read-jump read-jump))
154 ;; Put a property `jump-flag' to each CCL code which execute jump in
156 (let ((l ccl-jump-code-list))
158 (put (car l) 'jump-flag t)
161 (defconst ccl-register-table
162 [r0 r1 r2 r3 r4 r5 r6 r7]
163 "*Vector of CCL registers (symbols).")
165 ;; Put a property to indicate register number to each symbol of CCL.
167 (let (reg (i 0) (len (length ccl-register-table)))
169 (setq reg (aref ccl-register-table i))
170 (put reg 'ccl-register-number i)
173 (defconst ccl-arith-table
174 [+ - * / % & | ^ << >> <8 >8 // nil nil nil
175 < > == <= >= != de-sjis en-sjis]
176 "*Vector of CCL arithmetic/logical operators (symbols).")
178 ;; Put a property to each symbol of CCL operators for the compiler.
179 (let (arith (i 0) (len (length ccl-arith-table)))
181 (setq arith (aref ccl-arith-table i))
182 (if arith (put arith 'ccl-arith-code i))
185 (defconst ccl-assign-arith-table
186 [+= -= *= /= %= &= |= ^= <<= >>= <8= >8= //=]
187 "*Vector of CCL assignment operators (symbols).")
189 ;; Put a property to each symbol of CCL assignment operators for the compiler.
190 (let (arith (i 0) (len (length ccl-assign-arith-table)))
192 (setq arith (aref ccl-assign-arith-table i))
193 (put arith 'ccl-self-arith-code i)
196 (defvar ccl-program-vector nil
197 "Working vector of CCL codes produced by CCL compiler.")
198 (defvar ccl-current-ic 0
199 "The current index for `ccl-program-vector'.")
201 ;; Embed integer DATA in `ccl-program-vector' at `ccl-current-ic' and
202 ;; increment it. If IC is specified, embed DATA at IC.
203 (defun ccl-embed-data (data &optional ic)
205 (aset ccl-program-vector ic data)
206 (aset ccl-program-vector ccl-current-ic data)
207 (setq ccl-current-ic (1+ ccl-current-ic))))
209 ;; Embed string STR of length LEN in `ccl-program-vector' at
211 (defun ccl-embed-string (len str)
214 (ccl-embed-data (logior (ash (aref str i) 16)
216 (ash (aref str (1+ i)) 8)
223 ;; Embed a relative jump address to `ccl-current-ic' in
224 ;; `ccl-program-vector' at IC without altering the other bit field.
225 (defun ccl-embed-current-address (ic)
226 (let ((relative (- ccl-current-ic (1+ ic))))
227 (aset ccl-program-vector ic
228 (logior (aref ccl-program-vector ic) (ash relative 8)))))
230 ;; Embed CCL code for the operation OP and arguments REG and DATA in
231 ;; `ccl-program-vector' at `ccl-current-ic' in the following format.
232 ;; |----------------- integer (28-bit) ------------------|
233 ;; |------------ 20-bit ------------|- 3-bit --|- 5-bit -|
234 ;; |------------- DATA -------------|-- REG ---|-- OP ---|
235 ;; If REG2 is specified, embed a code in the following format.
236 ;; |------- 17-bit ------|- 3-bit --|- 3-bit --|- 5-bit -|
237 ;; |-------- DATA -------|-- REG2 --|-- REG ---|-- OP ---|
239 ;; If REG is a CCL register symbol (e.g. r0, r1...), the register
240 ;; number is embedded. If OP is one of unconditional jumps, DATA is
241 ;; changed to an relative jump address.
243 (defun ccl-embed-code (op reg data &optional reg2)
244 (if (and (> data 0) (get op 'jump-flag))
245 ;; DATA is an absolute jump address. Make it relative to the
246 ;; next of jump code.
247 (setq data (- data (1+ ccl-current-ic))))
248 (let ((code (logior (get op 'ccl-code)
250 (if (symbolp reg) (get reg 'ccl-register-number) reg) 5)
252 (logior (ash (get reg2 'ccl-register-number) 8)
255 (aset ccl-program-vector ccl-current-ic code)
256 (setq ccl-current-ic (1+ ccl-current-ic))))
258 ;; Just advance `ccl-current-ic' by INC.
259 (defun ccl-increment-ic (inc)
260 (setq ccl-current-ic (+ ccl-current-ic inc)))
263 (defun ccl-program-p (obj)
264 "T if OBJECT is a valid CCL compiled code."
266 (let ((i 0) (len (length obj)) (flag t))
269 (while (and flag (< i len))
270 (setq flag (integerp (aref obj i)))
274 ;; If non-nil, index of the start of the current loop.
275 (defvar ccl-loop-head nil)
276 ;; If non-nil, list of absolute addresses of the breaking points of
278 (defvar ccl-breaks nil)
281 (defun ccl-compile (ccl-program)
282 "Return a comiled code of CCL-PROGRAM as a vector of integer."
283 (if (or (null (consp ccl-program))
284 (null (integerp (car ccl-program)))
285 (null (listp (car (cdr ccl-program)))))
286 (error "CCL: Invalid CCL program: %s" ccl-program))
287 (if (null (vectorp ccl-program-vector))
288 (setq ccl-program-vector (make-vector 8192 0)))
289 (setq ccl-loop-head nil ccl-breaks nil)
290 (setq ccl-current-ic 0)
292 ;; The first element is the buffer magnification.
293 (ccl-embed-data (car ccl-program))
295 ;; The second element is the address of the start CCL code for
296 ;; processing end of input buffer (we call it eof-processor). We
300 ;; Compile the main body of the CCL program.
301 (ccl-compile-1 (car (cdr ccl-program)))
303 ;; Embed the address of eof-processor.
304 (ccl-embed-data ccl-current-ic 1)
306 ;; Then compile eof-processor.
307 (if (nth 2 ccl-program)
308 (ccl-compile-1 (nth 2 ccl-program)))
310 ;; At last, embed termination code.
311 (ccl-embed-code 'end 0 0)
313 (let ((vec (make-vector ccl-current-ic 0))
315 (while (< i ccl-current-ic)
316 (aset vec i (aref ccl-program-vector i))
320 ;; Signal syntax error.
321 (defun ccl-syntax-error (cmd)
322 (error "CCL: Syntax error: %s" cmd))
324 ;; Check if ARG is a valid CCL register.
325 (defun ccl-check-register (arg cmd)
326 (if (get arg 'ccl-register-number)
328 (error "CCL: Invalid register %s in %s." arg cmd)))
330 ;; Check if ARG is a valid CCL command.
331 (defun ccl-check-compile-function (arg cmd)
332 (or (get arg 'ccl-compile-function)
333 (error "CCL: Invalid command: %s" cmd)))
335 ;; In the following code, most ccl-compile-XXXX functions return t if
336 ;; they end with unconditional jump, else return nil.
338 ;; Compile CCL-BLOCK (see the syntax above).
339 (defun ccl-compile-1 (ccl-block)
340 (let (unconditional-jump
342 (if (or (integerp ccl-block)
344 (and ccl-block (symbolp (car ccl-block))))
345 ;; This block consists of single statement.
346 (setq ccl-block (list ccl-block)))
348 ;; Now CCL-BLOCK is a list of statements. Compile them one by
351 (setq cmd (car ccl-block))
352 (setq unconditional-jump
353 (cond ((integerp cmd)
354 ;; SET statement for the register 0.
355 (ccl-compile-set (list 'r0 '= cmd)))
358 ;; WRITE statement of string argument.
359 (ccl-compile-write-string cmd))
362 ;; The other statements.
363 (cond ((eq (nth 1 cmd) '=)
364 ;; SET statement of the form `(REG = EXPRESSION)'.
365 (ccl-compile-set cmd))
367 ((and (symbolp (nth 1 cmd))
368 (get (nth 1 cmd) 'ccl-self-arith-code))
369 ;; SET statement with an assignment operation.
370 (ccl-compile-self-set cmd))
373 (funcall (ccl-check-compile-function (car cmd) cmd)
377 (ccl-syntax-error cmd))))
378 (setq ccl-block (cdr ccl-block)))
381 (defconst ccl-max-short-const (ash 1 19))
382 (defconst ccl-min-short-const (ash -1 19))
384 ;; Compile SET statement.
385 (defun ccl-compile-set (cmd)
386 (let ((rrr (ccl-check-register (car cmd) cmd))
389 ;; CMD has the form `(RRR = (XXX OP YYY))'.
390 (ccl-compile-expression rrr right))
393 ;; CMD has the form `(RRR = integer)'.
394 (if (and (<= right ccl-max-short-const)
395 (>= right ccl-min-short-const))
396 (ccl-embed-code 'set-short-const rrr right)
397 (ccl-embed-code 'set-const rrr 0)
398 (ccl-embed-data right)))
401 ;; CMD has the form `(RRR = rrr [ array ])'.
402 (ccl-check-register right cmd)
403 (let ((ary (nth 3 cmd)))
405 (let ((i 0) (len (length ary)))
406 (ccl-embed-code 'set-array rrr len right)
408 (ccl-embed-data (aref ary i))
410 (ccl-embed-code 'set-register rrr 0 right))))))
413 ;; Compile SET statement with ASSIGNMENT_OPERATOR.
414 (defun ccl-compile-self-set (cmd)
415 (let ((rrr (ccl-check-register (car cmd) cmd))
418 ;; CMD has the form `(RRR ASSIGN_OP (XXX OP YYY))', compile
419 ;; the right hand part as `(r7 = (XXX OP YYY))' (note: the
420 ;; register 7 can be used for storing temporary value).
422 (ccl-compile-expression 'r7 right)
424 ;; Now CMD has the form `(RRR ASSIGN_OP ARG)'. Compile it as
425 ;; `(RRR = (RRR OP ARG))'.
426 (ccl-compile-expression
428 (list rrr (intern (substring (symbol-name (nth 1 cmd)) 0 -1)) right)))
431 ;; Compile SET statement of the form `(RRR = EXPR)'.
432 (defun ccl-compile-expression (rrr expr)
433 (let ((left (car expr))
434 (op (get (nth 1 expr) 'ccl-arith-code))
435 (right (nth 2 expr)))
438 ;; EXPR has the form `((EXPR2 OP2 ARG) OP RIGHT)'. Compile
439 ;; the first term as `(r7 = (EXPR2 OP2 ARG)).'
440 (ccl-compile-expression 'r7 left)
443 ;; Now EXPR has the form (LEFT OP RIGHT).
445 ;; Compile this SET statement as `(RRR OP= RIGHT)'.
448 (ccl-embed-code 'set-assign-expr-const rrr (ash op 3) 'r0)
449 (ccl-embed-data right))
450 (ccl-check-register right expr)
451 (ccl-embed-code 'set-assign-expr-register rrr (ash op 3) right))
453 ;; Compile this SET statement as `(RRR = (LEFT OP RIGHT))'.
456 (ccl-embed-code 'set-expr-const rrr (ash op 3) left)
457 (ccl-embed-data right))
458 (ccl-check-register right expr)
459 (ccl-embed-code 'set-expr-register
461 (logior (ash op 3) (get right 'ccl-register-number))
464 ;; Compile WRITE statement with string argument.
465 (defun ccl-compile-write-string (str)
466 (let ((len (length str)))
467 (ccl-embed-code 'write-const-string 1 len)
468 (ccl-embed-string len str))
471 ;; Compile IF statement of the form `(if CONDITION TRUE-PART FALSE-PART)'.
472 ;; If READ-FLAG is non-nil, this statement has the form
473 ;; `(read-if (REG OPERATOR ARG) TRUE-PART FALSE-PART)'.
474 (defun ccl-compile-if (cmd &optional read-flag)
475 (if (and (/= (length cmd) 3) (/= (length cmd) 4))
476 (error "CCL: Invalid number of arguments: %s" cmd))
477 (let ((condition (nth 1 cmd))
478 (true-cmds (nth 2 cmd))
479 (false-cmds (nth 3 cmd))
482 (if (and (listp condition)
483 (listp (car condition)))
484 ;; If CONDITION is a nested expression, the inner expression
485 ;; should be compiled at first as SET statement, i.e.:
486 ;; `(if ((X OP2 Y) OP Z) ...)' is compiled into two statements:
487 ;; `(r7 = (X OP2 Y)) (if (r7 OP Z) ...)'.
489 (ccl-compile-expression 'r7 (car condition))
490 (setq condition (cons 'r7 (cdr condition)))
491 (setq cmd (cons (car cmd)
492 (cons condition (cdr (cdr cmd)))))))
494 (setq jump-cond-address ccl-current-ic)
495 ;; Compile CONDITION.
496 (if (symbolp condition)
497 ;; CONDITION is a register.
499 (ccl-check-register condition cmd)
500 (ccl-embed-code 'jump-cond condition 0))
501 ;; CONDITION is a simple expression of the form (RRR OP ARG).
502 (let ((rrr (car condition))
503 (op (get (nth 1 condition) 'ccl-arith-code))
504 (arg (nth 2 condition)))
505 (ccl-check-register rrr cmd)
508 (ccl-embed-code (if read-flag 'read-jump-cond-expr-const
509 'jump-cond-expr-const)
512 (ccl-embed-data arg))
513 (ccl-check-register arg cmd)
514 (ccl-embed-code (if read-flag 'read-jump-cond-expr-register
515 'jump-cond-expr-register)
518 (ccl-embed-data (get arg 'ccl-register-number)))))
520 ;; Compile TRUE-PART.
521 (let ((unconditional-jump (ccl-compile-1 true-cmds)))
522 (if (null false-cmds)
523 ;; This is the place to jump to if condition is false.
524 (ccl-embed-current-address jump-cond-address)
525 (let (end-true-part-address)
526 (if (not unconditional-jump)
528 ;; If TRUE-PART does not end with unconditional jump, we
529 ;; have to jump to the end of FALSE-PART from here.
530 (setq end-true-part-address ccl-current-ic)
531 (ccl-embed-code 'jump 0 0)))
532 ;; This is the place to jump to if CONDITION is false.
533 (ccl-embed-current-address jump-cond-address)
534 ;; Compile FALSE-PART.
535 (setq unconditional-jump
536 (and (ccl-compile-1 false-cmds) unconditional-jump))
537 (if end-true-part-address
538 ;; This is the place to jump to after the end of TRUE-PART.
539 (ccl-embed-current-address end-true-part-address))))
540 unconditional-jump)))
542 ;; Compile BRANCH statement.
543 (defun ccl-compile-branch (cmd)
544 (if (< (length cmd) 3)
545 (error "CCL: Invalid number of arguments: %s" cmd))
546 (ccl-compile-branch-blocks 'branch
547 (ccl-compile-branch-expression (nth 1 cmd) cmd)
550 ;; Compile READ statement of the form `(read-branch EXPR BLOCK0 BLOCK1 ...)'.
551 (defun ccl-compile-read-branch (cmd)
552 (if (< (length cmd) 3)
553 (error "CCL: Invalid number of arguments: %s" cmd))
554 (ccl-compile-branch-blocks 'read-branch
555 (ccl-compile-branch-expression (nth 1 cmd) cmd)
558 ;; Compile EXPRESSION part of BRANCH statement and return register
559 ;; which holds a value of the expression.
560 (defun ccl-compile-branch-expression (expr cmd)
562 ;; EXPR has the form `(EXPR2 OP ARG)'. Compile it as SET
563 ;; statement of the form `(r7 = (EXPR2 OP ARG))'.
565 (ccl-compile-expression 'r7 expr)
567 (ccl-check-register expr cmd)))
569 ;; Compile BLOCKs of BRANCH statement. CODE is 'branch or 'read-branch.
570 ;; REG is a register which holds a value of EXPRESSION part. BLOCKs
571 ;; is a list of CCL-BLOCKs.
572 (defun ccl-compile-branch-blocks (code rrr blocks)
573 (let ((branches (length blocks))
575 jump-table-head-address
578 block-unconditional-jump)
579 (ccl-embed-code code rrr branches)
580 (setq jump-table-head-address ccl-current-ic)
581 ;; The size of jump table is the number of blocks plus 1 (for the
582 ;; case RRR is out of range).
583 (ccl-increment-ic (1+ branches))
584 (setq empty-block-indexes (list branches))
585 ;; Compile each block.
588 (if (null (car blocks))
589 ;; This block is empty.
590 (setq empty-block-indexes (cons branch-idx empty-block-indexes)
591 block-unconditional-jump t)
592 ;; This block is not empty.
593 (ccl-embed-data (- ccl-current-ic jump-table-head-address)
594 (+ jump-table-head-address branch-idx))
595 (setq block-unconditional-jump (ccl-compile-1 (car blocks)))
596 (if (not block-unconditional-jump)
598 ;; Jump address of the end of branches are embedded later.
599 ;; For the moment, just remember where to embed them.
600 (setq block-tail-addresses
601 (cons ccl-current-ic block-tail-addresses))
602 (ccl-embed-code 'jump 0 0))))
603 (setq branch-idx (1+ branch-idx))
604 (setq blocks (cdr blocks)))
605 (if (not block-unconditional-jump)
606 ;; We don't need jump code at the end of the last block.
607 (setq block-tail-addresses (cdr block-tail-addresses)
608 ccl-current-ic (1- ccl-current-ic)))
609 ;; Embed jump address at the tailing jump commands of blocks.
610 (while block-tail-addresses
611 (ccl-embed-current-address (car block-tail-addresses))
612 (setq block-tail-addresses (cdr block-tail-addresses)))
613 ;; For empty blocks, make entries in the jump table point directly here.
614 (while empty-block-indexes
615 (ccl-embed-data (- ccl-current-ic jump-table-head-address)
616 (+ jump-table-head-address (car empty-block-indexes)))
617 (setq empty-block-indexes (cdr empty-block-indexes))))
618 ;; Branch command ends by unconditional jump if RRR is out of range.
621 ;; Compile LOOP statement.
622 (defun ccl-compile-loop (cmd)
623 (if (< (length cmd) 2)
624 (error "CCL: Invalid number of arguments: %s" cmd))
625 (let* ((ccl-loop-head ccl-current-ic)
631 (setq unconditional-jump t)
633 (setq unconditional-jump
634 (and (ccl-compile-1 (car cmd)) unconditional-jump))
635 (setq cmd (cdr cmd)))
638 ;; Embed jump address for break statements encountered in
641 (ccl-embed-current-address (car ccl-breaks))
642 (setq ccl-breaks (cdr ccl-breaks))))
645 ;; Compile BREAK statement.
646 (defun ccl-compile-break (cmd)
647 (if (/= (length cmd) 1)
648 (error "CCL: Invalid number of arguments: %s" cmd))
649 (if (null ccl-loop-head)
650 (error "CCL: No outer loop: %s" cmd))
651 (setq ccl-breaks (cons ccl-current-ic ccl-breaks))
652 (ccl-embed-code 'jump 0 0)
655 ;; Compile REPEAT statement.
656 (defun ccl-compile-repeat (cmd)
657 (if (/= (length cmd) 1)
658 (error "CCL: Invalid number of arguments: %s" cmd))
659 (if (null ccl-loop-head)
660 (error "CCL: No outer loop: %s" cmd))
661 (ccl-embed-code 'jump 0 ccl-loop-head)
664 ;; Compile WRITE-REPEAT statement.
665 (defun ccl-compile-write-repeat (cmd)
666 (if (/= (length cmd) 2)
667 (error "CCL: Invalid number of arguments: %s" cmd))
668 (if (null ccl-loop-head)
669 (error "CCL: No outer loop: %s" cmd))
670 (let ((arg (nth 1 cmd)))
671 (cond ((integerp arg)
672 (ccl-embed-code 'write-const-jump 0 ccl-loop-head)
673 (ccl-embed-data arg))
675 (let ((len (length arg))
677 (ccl-embed-code 'write-string-jump 0 ccl-loop-head)
679 (ccl-embed-string len arg)))
681 (ccl-check-register arg cmd)
682 (ccl-embed-code 'write-register-jump arg ccl-loop-head))))
685 ;; Compile WRITE-READ-REPEAT statement.
686 (defun ccl-compile-write-read-repeat (cmd)
687 (if (or (< (length cmd) 2) (> (length cmd) 3))
688 (error "CCL: Invalid number of arguments: %s" cmd))
689 (if (null ccl-loop-head)
690 (error "CCL: No outer loop: %s" cmd))
691 (let ((rrr (ccl-check-register (nth 1 cmd) cmd))
694 (ccl-embed-code 'write-register-read-jump rrr ccl-loop-head))
696 (ccl-embed-code 'write-const-read-jump rrr arg ccl-loop-head))
698 (let ((len (length arg))
700 (ccl-embed-code 'write-array-read-jump rrr ccl-loop-head)
703 (ccl-embed-data (aref arg i))
706 (error "CCL: Invalid argument %s: %s" arg cmd)))
707 (ccl-embed-code 'read-jump rrr ccl-loop-head))
710 ;; Compile READ statement.
711 (defun ccl-compile-read (cmd)
712 (if (< (length cmd) 2)
713 (error "CCL: Invalid number of arguments: %s" cmd))
714 (let* ((args (cdr cmd))
715 (i (1- (length args))))
717 (let ((rrr (ccl-check-register (car args) cmd)))
718 (ccl-embed-code 'read-register rrr i)
719 (setq args (cdr args) i (1- i)))))
722 ;; Compile READ-IF statement.
723 (defun ccl-compile-read-if (cmd)
724 (ccl-compile-if cmd 'read))
726 ;; Compile WRITE statement.
727 (defun ccl-compile-write (cmd)
728 (if (< (length cmd) 2)
729 (error "CCL: Invalid number of arguments: %s" cmd))
730 (let ((rrr (nth 1 cmd)))
731 (cond ((integerp rrr)
732 (ccl-embed-code 'write-const-string 0 rrr))
734 (ccl-compile-write-string rrr))
735 ((and (symbolp rrr) (vectorp (nth 2 cmd)))
736 (ccl-check-register rrr cmd)
737 ;; CMD has the form `(write REG ARRAY)'.
738 (let* ((arg (nth 2 cmd))
741 (ccl-embed-code 'write-array rrr len)
743 (if (not (integerp (aref arg i)))
744 (error "CCL: Invalid argument %s: %s" arg cmd))
745 (ccl-embed-data (aref arg i))
749 ;; CMD has the form `(write REG ...)'.
750 (let* ((args (cdr cmd))
751 (i (1- (length args))))
753 (setq rrr (ccl-check-register (car args) cmd))
754 (ccl-embed-code 'write-register rrr i)
755 (setq args (cdr args) i (1- i)))))
758 ;; CMD has the form `(write (LEFT OP RIGHT))'.
759 (let ((left (car rrr))
760 (op (get (nth 1 rrr) 'ccl-arith-code))
764 ;; RRR has the form `((EXPR OP2 ARG) OP RIGHT)'.
765 ;; Compile the first term as `(r7 = (EXPR OP2 ARG))'.
766 (ccl-compile-expression 'r7 left)
768 ;; Now RRR has the form `(ARG OP RIGHT)'.
771 (ccl-embed-code 'write-expr-const 0 (ash op 3) left)
772 (ccl-embed-data right))
773 (ccl-check-register right rrr)
774 (ccl-embed-code 'write-expr-register 0
776 (get right 'ccl-register-number))))))
779 (error "CCL: Invalid argument: %s" cmd))))
782 ;; Compile CALL statement.
783 (defun ccl-compile-call (cmd)
784 (if (/= (length cmd) 2)
785 (error "CCL: Invalid number of arguments: %s" cmd))
786 (if (not (symbolp (nth 1 cmd)))
787 (error "CCL: Subroutine should be a symbol: %s" cmd))
788 (let* ((name (nth 1 cmd))
789 (idx (get name 'ccl-program-idx)))
791 (error "CCL: Unknown subroutine name: %s" name))
792 (ccl-embed-code 'call 0 idx))
795 ;; Compile END statement.
796 (defun ccl-compile-end (cmd)
797 (if (/= (length cmd) 1)
798 (error "CCL: Invalid number of arguments: %s" cmd))
799 (ccl-embed-code 'end 0 0)
804 ;; To avoid byte-compiler warning.
808 (defun ccl-dump (ccl-code)
809 "Disassemble compiled CCL-CODE."
810 (let ((len (length ccl-code))
811 (buffer-mag (aref ccl-code 0)))
812 (cond ((= buffer-mag 0)
813 (insert "Don't output anything.\n"))
815 (insert "Out-buffer must be as large as in-buffer.\n"))
818 (format "Out-buffer must be %d times bigger than in-buffer.\n"
820 (insert "Main-body:\n")
821 (setq ccl-current-ic 2)
822 (if (> (aref ccl-code 1) 0)
824 (while (< ccl-current-ic (aref ccl-code 1))
826 (insert "At EOF:\n")))
827 (while (< ccl-current-ic len)
831 ;; Return a CCL code in `ccl-code' at `ccl-current-ic'.
832 (defun ccl-get-next-code ()
834 (aref ccl-code ccl-current-ic)
835 (setq ccl-current-ic (1+ ccl-current-ic))))
838 (let* ((code (ccl-get-next-code))
839 (cmd (aref ccl-code-table (logand code 31)))
840 (rrr (ash (logand code 255) -5))
842 (insert (format "%5d:[%s] " (1- ccl-current-ic) cmd))
843 (funcall (get cmd 'ccl-dump-function) rrr cc)))
845 (defun ccl-dump-set-register (rrr cc)
846 (insert (format "r%d = r%d\n" rrr cc)))
848 (defun ccl-dump-set-short-const (rrr cc)
849 (insert (format "r%d = %d\n" rrr cc)))
851 (defun ccl-dump-set-const (rrr ignore)
852 (insert (format "r%d = %d\n" rrr (ccl-get-next-code))))
854 (defun ccl-dump-set-array (rrr cc)
855 (let ((rrr2 (logand cc 7))
858 (insert (format "r%d = array[r%d] of length %d\n\t"
861 (insert (format "%d " (ccl-get-next-code)))
865 (defun ccl-dump-jump (ignore cc &optional address)
866 (insert (format "jump to %d(" (+ (or address ccl-current-ic) cc)))
869 (insert (format "%d)\n" (1+ cc))))
871 (defun ccl-dump-jump-cond (rrr cc)
872 (insert (format "if (r%d == 0), " rrr))
873 (ccl-dump-jump nil cc))
875 (defun ccl-dump-write-register-jump (rrr cc)
876 (insert (format "write r%d, " rrr))
877 (ccl-dump-jump nil cc))
879 (defun ccl-dump-write-register-read-jump (rrr cc)
880 (insert (format "write r%d, read r%d, " rrr rrr))
881 (ccl-dump-jump nil cc)
882 (ccl-get-next-code) ; Skip dummy READ-JUMP
885 (defun ccl-extract-arith-op (cc)
886 (aref ccl-arith-table (ash cc -6)))
888 (defun ccl-dump-write-expr-const (ignore cc)
889 (insert (format "write (r%d %s %d)\n"
891 (ccl-extract-arith-op cc)
892 (ccl-get-next-code))))
894 (defun ccl-dump-write-expr-register (ignore cc)
895 (insert (format "write (r%d %s r%d)\n"
897 (ccl-extract-arith-op cc)
898 (logand (ash cc -3) 7))))
900 (defun ccl-dump-insert-char (cc)
901 (cond ((= cc ?\t) (insert " \"^I\""))
902 ((= cc ?\n) (insert " \"^J\""))
903 (t (insert (format " \"%c\"" cc)))))
905 (defun ccl-dump-write-const-jump (ignore cc)
906 (let ((address ccl-current-ic))
907 (insert "write char")
908 (ccl-dump-insert-char (ccl-get-next-code))
910 (ccl-dump-jump nil cc address)))
912 (defun ccl-dump-write-const-read-jump (rrr cc)
913 (let ((address ccl-current-ic))
914 (insert "write char")
915 (ccl-dump-insert-char (ccl-get-next-code))
916 (insert (format ", read r%d, " rrr))
917 (ccl-dump-jump cc address)
918 (ccl-get-next-code) ; Skip dummy READ-JUMP
921 (defun ccl-dump-write-string-jump (ignore cc)
922 (let ((address ccl-current-ic)
923 (len (ccl-get-next-code))
927 (let ((code (ccl-get-next-code)))
928 (insert (ash code -16))
929 (if (< (1+ i) len) (insert (logand (ash code -8) 255)))
930 (if (< (+ i 2) len) (insert (logand code 255))))
933 (ccl-dump-jump nil cc address)))
935 (defun ccl-dump-write-array-read-jump (rrr cc)
936 (let ((address ccl-current-ic)
937 (len (ccl-get-next-code))
939 (insert (format "write array[r%d] of length %d,\n\t" rrr len))
941 (ccl-dump-insert-char (ccl-get-next-code))
943 (insert (format "\n\tthen read r%d, " rrr))
944 (ccl-dump-jump nil cc address)
945 (ccl-get-next-code) ; Skip dummy READ-JUMP.
948 (defun ccl-dump-read-jump (rrr cc)
949 (insert (format "read r%d, " rrr))
950 (ccl-dump-jump nil cc))
952 (defun ccl-dump-branch (rrr len)
953 (let ((jump-table-head ccl-current-ic)
955 (insert (format "jump to array[r%d] of length %d\n\t" rrr len))
957 (insert (format "%d " (+ jump-table-head (ccl-get-next-code))))
961 (defun ccl-dump-read-register (rrr cc)
962 (insert (format "read r%d (%d remaining)\n" rrr cc)))
964 (defun ccl-dump-read-branch (rrr len)
965 (insert (format "read r%d, " rrr))
966 (ccl-dump-branch rrr len))
968 (defun ccl-dump-write-register (rrr cc)
969 (insert (format "write r%d (%d remaining)\n" rrr cc)))
971 (defun ccl-dump-call (ignore cc)
972 (insert (format "call subroutine #%d\n" cc)))
974 (defun ccl-dump-write-const-string (rrr cc)
977 (insert "write char")
978 (ccl-dump-insert-char cc)
984 (let ((code (ccl-get-next-code)))
985 (insert (format "%c" (lsh code -16)))
987 (insert (format "%c" (logand (lsh code -8) 255))))
989 (insert (format "%c" (logand code 255))))
993 (defun ccl-dump-write-array (rrr cc)
995 (insert (format "write array[r%d] of length %d\n\t" rrr cc))
997 (ccl-dump-insert-char (ccl-get-next-code))
1001 (defun ccl-dump-end (&rest ignore)
1004 (defun ccl-dump-set-assign-expr-const (rrr cc)
1005 (insert (format "r%d %s= %d\n"
1007 (ccl-extract-arith-op cc)
1008 (ccl-get-next-code))))
1010 (defun ccl-dump-set-assign-expr-register (rrr cc)
1011 (insert (format "r%d %s= r%d\n"
1013 (ccl-extract-arith-op cc)
1016 (defun ccl-dump-set-expr-const (rrr cc)
1017 (insert (format "r%d = r%d %s %d\n"
1020 (ccl-extract-arith-op cc)
1021 (ccl-get-next-code))))
1023 (defun ccl-dump-set-expr-register (rrr cc)
1024 (insert (format "r%d = r%d %s r%d\n"
1027 (ccl-extract-arith-op cc)
1028 (logand (ash cc -3) 7))))
1030 (defun ccl-dump-jump-cond-expr-const (rrr cc)
1031 (let ((address ccl-current-ic))
1032 (insert (format "if !(r%d %s %d), "
1034 (aref ccl-arith-table (ccl-get-next-code))
1035 (ccl-get-next-code)))
1036 (ccl-dump-jump nil cc address)))
1038 (defun ccl-dump-jump-cond-expr-register (rrr cc)
1039 (let ((address ccl-current-ic))
1040 (insert (format "if !(r%d %s r%d), "
1042 (aref ccl-arith-table (ccl-get-next-code))
1043 (ccl-get-next-code)))
1044 (ccl-dump-jump nil cc address)))
1046 (defun ccl-dump-read-jump-cond-expr-const (rrr cc)
1047 (insert (format "read r%d, " rrr))
1048 (ccl-dump-jump-cond-expr-const rrr cc))
1050 (defun ccl-dump-read-jump-cond-expr-register (rrr cc)
1051 (insert (format "read r%d, " rrr))
1052 (ccl-dump-jump-cond-expr-register rrr cc))
1054 (defun ccl-dump-binary (ccl-code)
1055 (let ((len (length ccl-code))
1058 (let ((code (aref ccl-code i))
1061 (insert (if (= (logand code (ash 1 j)) 0) ?0 ?1))
1063 (setq code (logand code 31))
1064 (if (< code (length ccl-code-table))
1065 (insert (format ":%s" (aref ccl-code-table code))))
1069 ;; CCL emulation staffs
1071 ;; Not yet implemented.
1074 (defmacro declare-ccl-program (name)
1075 "Declare NAME as a name of CCL program.
1077 To compile a CCL program which calls another CCL program not yet
1078 defined, it must be declared as a CCL program in advance."
1079 `(put ',name 'ccl-program-idx (register-ccl-program ',name nil)))
1082 (defmacro define-ccl-program (name ccl-program &optional doc)
1083 "Set NAME the compiled code of CCL-PROGRAM.
1084 CCL-PROGRAM is `eval'ed before being handed to the CCL compiler `ccl-compile'.
1085 The compiled code is a vector of integers."
1086 `(let ((prog ,(ccl-compile (eval ccl-program))))
1087 (defconst ,name prog ,doc)
1088 (put ',name 'ccl-program-idx (register-ccl-program ',name prog))
1092 (defun ccl-execute-with-args (ccl-prog &rest args)
1093 "Execute CCL-PROGRAM with registers initialized by the remaining args.
1094 The return value is a vector of resulting CCL registeres."
1095 (let ((reg (make-vector 8 0))
1097 (while (and args (< i 8))
1098 (if (not (integerp (car args)))
1099 (error "Arguments should be integer"))
1100 (aset reg i (car args))
1101 (setq args (cdr args) i (1+ i)))
1102 (ccl-execute ccl-prog reg)