]> code.delx.au - gnu-emacs/blob - lisp/international/ccl.el
*** empty log message ***
[gnu-emacs] / lisp / international / ccl.el
1 ;;; ccl.el --- CCL (Code Conversion Language) compiler
2
3 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
5
6 ;; Keywords: CCL, mule, multilingual, character set, coding-system
7
8 ;; This file is part of GNU Emacs.
9
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)
13 ;; any later version.
14
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.
19
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.
24
25 ;;; Commentary:
26
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.
31 ;;
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.
39 ;;
40 ;; Here's the syntax of CCL program in BNF notation.
41 ;;
42 ;; CCL_PROGRAM :=
43 ;; (BUFFER_MAGNIFICATION
44 ;; CCL_MAIN_BLOCK
45 ;; [ CCL_EOF_BLOCK ])
46 ;;
47 ;; BUFFER_MAGNIFICATION := integer
48 ;; CCL_MAIN_BLOCK := CCL_BLOCK
49 ;; CCL_EOF_BLOCK := CCL_BLOCK
50 ;;
51 ;; CCL_BLOCK :=
52 ;; STATEMENT | (STATEMENT [STATEMENT ...])
53 ;; STATEMENT :=
54 ;; SET | IF | BRANCH | LOOP | REPEAT | BREAK | READ | WRITE | CALL
55 ;;
56 ;; SET :=
57 ;; (REG = EXPRESSION)
58 ;; | (REG ASSIGNMENT_OPERATOR EXPRESSION)
59 ;; | integer
60 ;;
61 ;; EXPRESSION := ARG | (EXPRESSION OPERATOR ARG)
62 ;;
63 ;; IF := (if EXPRESSION CCL_BLOCK CCL_BLOCK)
64 ;; BRANCH := (branch EXPRESSION CCL_BLOCK [CCL_BLOCK ...])
65 ;; LOOP := (loop STATEMENT [STATEMENT ...])
66 ;; BREAK := (break)
67 ;; REPEAT :=
68 ;; (repeat)
69 ;; | (write-repeat [REG | integer | string])
70 ;; | (write-read-repeat REG [integer | ARRAY])
71 ;; READ :=
72 ;; (read REG ...)
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})
76 ;; WRITE :=
77 ;; (write REG ...)
78 ;; | (write EXPRESSION)
79 ;; | (write integer) | (write string) | (write REG ARRAY)
80 ;; | string
81 ;; | (write-multibyte-character REG(charset) REG(codepoint))
82 ;; UNIFY :=
83 ;; (unify-char REG(table) REG(charset) REG(codepoint))
84 ;; | (unify-char SYMBOL REG(charset) REG(codepoint))
85 ;; TRANSLATE :=
86 ;; (iterate-multiple-map REG REG TABLE-IDs)
87 ;; | (translate-multiple-map REG REG (TABLE-SET))
88 ;; | (translate-single-map REG REG TABLE-ID)
89 ;; TABLE-IDs := TABLE-ID ...
90 ;; TABLE-SET := TABLE-IDs | (TABLE-IDs) TABLE-SET
91 ;; TABLE-ID := integer
92 ;;
93 ;; CALL := (call ccl-program-name)
94 ;; END := (end)
95 ;;
96 ;; REG := r0 | r1 | r2 | r3 | r4 | r5 | r6 | r7
97 ;; ARG := REG | integer
98 ;; OPERATOR :=
99 ;; + | - | * | / | % | & | '|' | ^ | << | >> | <8 | >8 | //
100 ;; | < | > | == | <= | >= | != | de-sjis | en-sjis
101 ;; ASSIGNMENT_OPERATOR :=
102 ;; += | -= | *= | /= | %= | &= | '|=' | ^= | <<= | >>=
103 ;; ARRAY := '[' interger ... ']'
104
105 ;;; Code:
106
107 (defgroup ccl nil
108 "CCL (Code Conversion Language) compiler."
109 :prefix "ccl-"
110 :group 'i18n)
111
112 (defcustom 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
116 unify-character
117 iterate-multiple-map translate-multiple-map translate-single-map]
118 "*Vector of CCL commands (symbols)."
119 :type '(vector (repeat :inline t symbol))
120 :group 'ccl)
121
122 ;; Put a property to each symbol of CCL commands for the compiler.
123 (let (op (i 0) (len (length ccl-command-table)))
124 (while (< i len)
125 (setq op (aref ccl-command-table i))
126 (put op 'ccl-compile-function (intern (format "ccl-compile-%s" op)))
127 (setq i (1+ i))))
128
129 (defcustom ccl-code-table
130 [set-register
131 set-short-const
132 set-const
133 set-array
134 jump
135 jump-cond
136 write-register-jump
137 write-register-read-jump
138 write-const-jump
139 write-const-read-jump
140 write-string-jump
141 write-array-read-jump
142 read-jump
143 branch
144 read-register
145 write-expr-const
146 read-branch
147 write-register
148 write-expr-register
149 call
150 write-const-string
151 write-array
152 end
153 set-assign-expr-const
154 set-assign-expr-register
155 set-expr-const
156 set-expr-register
157 jump-cond-expr-const
158 jump-cond-expr-register
159 read-jump-cond-expr-const
160 read-jump-cond-expr-register
161 ex-cmd
162 ]
163 "*Vector of CCL compiled codes (symbols)."
164 :type '(vector (repeat :inline t symbol))
165 :group 'ccl)
166
167 (defcustom ccl-extended-code-table
168 [read-multibyte-character
169 write-multibyte-character
170 unify-character
171 unify-character-const-tbl
172 nil nil nil nil nil nil nil nil nil nil nil nil ; 0x04-0x0f
173 iterate-multiple-map
174 translate-multiple-map
175 translate-single-map
176 ]
177 "Vector of CCL extended compiled codes (symbols)."
178 :type '(vector (repeat :inline t symbol))
179 :group 'ccl
180 :version "20.3")
181
182 ;; Put a property to each symbol of CCL codes for the disassembler.
183 (let (code (i 0) (len (length ccl-code-table)))
184 (while (< i len)
185 (setq code (aref ccl-code-table i))
186 (put code 'ccl-code i)
187 (put code 'ccl-dump-function (intern (format "ccl-dump-%s" code)))
188 (setq i (1+ i))))
189
190 (let (code (i 0) (len (length ccl-extended-code-table)))
191 (while (< i len)
192 (setq code (aref ccl-extended-code-table i))
193 (if code
194 (progn
195 (put code 'ccl-ex-code i)
196 (put code 'ccl-dump-function (intern (format "ccl-dump-%s" code)))))
197 (setq i (1+ i))))
198
199 (defconst ccl-jump-code-list
200 '(jump jump-cond write-register-jump write-register-read-jump
201 write-const-jump write-const-read-jump write-string-jump
202 write-array-read-jump read-jump))
203
204 ;; Put a property `jump-flag' to each CCL code which execute jump in
205 ;; some way.
206 (let ((l ccl-jump-code-list))
207 (while l
208 (put (car l) 'jump-flag t)
209 (setq l (cdr l))))
210
211 (defcustom ccl-register-table
212 [r0 r1 r2 r3 r4 r5 r6 r7]
213 "*Vector of CCL registers (symbols)."
214 :type '(vector (repeat :inline t symbol))
215 :group 'ccl)
216
217 ;; Put a property to indicate register number to each symbol of CCL.
218 ;; registers.
219 (let (reg (i 0) (len (length ccl-register-table)))
220 (while (< i len)
221 (setq reg (aref ccl-register-table i))
222 (put reg 'ccl-register-number i)
223 (setq i (1+ i))))
224
225 (defcustom ccl-arith-table
226 [+ - * / % & | ^ << >> <8 >8 // nil nil nil
227 < > == <= >= != de-sjis en-sjis]
228 "*Vector of CCL arithmetic/logical operators (symbols)."
229 :type '(vector (repeat :inline t symbol))
230 :group 'ccl)
231
232 ;; Put a property to each symbol of CCL operators for the compiler.
233 (let (arith (i 0) (len (length ccl-arith-table)))
234 (while (< i len)
235 (setq arith (aref ccl-arith-table i))
236 (if arith (put arith 'ccl-arith-code i))
237 (setq i (1+ i))))
238
239 (defcustom ccl-assign-arith-table
240 [+= -= *= /= %= &= |= ^= <<= >>= <8= >8= //=]
241 "*Vector of CCL assignment operators (symbols)."
242 :type '(vector (repeat :inline t symbol))
243 :group 'ccl)
244
245 ;; Put a property to each symbol of CCL assignment operators for the compiler.
246 (let (arith (i 0) (len (length ccl-assign-arith-table)))
247 (while (< i len)
248 (setq arith (aref ccl-assign-arith-table i))
249 (put arith 'ccl-self-arith-code i)
250 (setq i (1+ i))))
251
252 (defvar ccl-program-vector nil
253 "Working vector of CCL codes produced by CCL compiler.")
254 (defvar ccl-current-ic 0
255 "The current index for `ccl-program-vector'.")
256
257 ;; Embed integer DATA in `ccl-program-vector' at `ccl-current-ic' and
258 ;; increment it. If IC is specified, embed DATA at IC.
259 (defun ccl-embed-data (data &optional ic)
260 (if ic
261 (aset ccl-program-vector ic data)
262 (aset ccl-program-vector ccl-current-ic data)
263 (setq ccl-current-ic (1+ ccl-current-ic))))
264
265 ;; Embed string STR of length LEN in `ccl-program-vector' at
266 ;; `ccl-current-ic'.
267 (defun ccl-embed-string (len str)
268 (let ((i 0))
269 (while (< i len)
270 (ccl-embed-data (logior (ash (aref str i) 16)
271 (if (< (1+ i) len)
272 (ash (aref str (1+ i)) 8)
273 0)
274 (if (< (+ i 2) len)
275 (aref str (+ i 2))
276 0)))
277 (setq i (+ i 3)))))
278
279 ;; Embed a relative jump address to `ccl-current-ic' in
280 ;; `ccl-program-vector' at IC without altering the other bit field.
281 (defun ccl-embed-current-address (ic)
282 (let ((relative (- ccl-current-ic (1+ ic))))
283 (aset ccl-program-vector ic
284 (logior (aref ccl-program-vector ic) (ash relative 8)))))
285
286 ;; Embed CCL code for the operation OP and arguments REG and DATA in
287 ;; `ccl-program-vector' at `ccl-current-ic' in the following format.
288 ;; |----------------- integer (28-bit) ------------------|
289 ;; |------------ 20-bit ------------|- 3-bit --|- 5-bit -|
290 ;; |------------- DATA -------------|-- REG ---|-- OP ---|
291 ;; If REG2 is specified, embed a code in the following format.
292 ;; |------- 17-bit ------|- 3-bit --|- 3-bit --|- 5-bit -|
293 ;; |-------- DATA -------|-- REG2 --|-- REG ---|-- OP ---|
294
295 ;; If REG is a CCL register symbol (e.g. r0, r1...), the register
296 ;; number is embedded. If OP is one of unconditional jumps, DATA is
297 ;; changed to an relative jump address.
298
299 (defun ccl-embed-code (op reg data &optional reg2)
300 (if (and (> data 0) (get op 'jump-flag))
301 ;; DATA is an absolute jump address. Make it relative to the
302 ;; next of jump code.
303 (setq data (- data (1+ ccl-current-ic))))
304 (let ((code (logior (get op 'ccl-code)
305 (ash
306 (if (symbolp reg) (get reg 'ccl-register-number) reg) 5)
307 (if reg2
308 (logior (ash (get reg2 'ccl-register-number) 8)
309 (ash data 11))
310 (ash data 8)))))
311 (aset ccl-program-vector ccl-current-ic code)
312 (setq ccl-current-ic (1+ ccl-current-ic))))
313
314 ;; extended ccl command format
315 ;; |- 14-bit -|- 3-bit --|- 3-bit --|- 3-bit --|- 5-bit -|
316 ;; |- EX-OP --|-- REG3 --|-- REG2 --|-- REG ---|-- OP ---|
317 (defun ccl-embed-extended-command (ex-op reg reg2 reg3)
318 (let ((data (logior (ash (get ex-op 'ccl-ex-code) 3)
319 (if (symbolp reg3)
320 (get reg3 'ccl-register-number)
321 0))))
322 (ccl-embed-code 'ex-cmd reg data reg2)))
323
324 ;; Just advance `ccl-current-ic' by INC.
325 (defun ccl-increment-ic (inc)
326 (setq ccl-current-ic (+ ccl-current-ic inc)))
327
328 ;;;###autoload
329 (defun ccl-program-p (obj)
330 "T if OBJECT is a valid CCL compiled code."
331 (and (vectorp obj)
332 (let ((i 0) (len (length obj)) (flag t))
333 (if (> len 1)
334 (progn
335 (while (and flag (< i len))
336 (setq flag (integerp (aref obj i)))
337 (setq i (1+ i)))
338 flag)))))
339
340 ;; If non-nil, index of the start of the current loop.
341 (defvar ccl-loop-head nil)
342 ;; If non-nil, list of absolute addresses of the breaking points of
343 ;; the current loop.
344 (defvar ccl-breaks nil)
345
346 ;;;###autoload
347 (defun ccl-compile (ccl-program)
348 "Return a comiled code of CCL-PROGRAM as a vector of integer."
349 (if (or (null (consp ccl-program))
350 (null (integerp (car ccl-program)))
351 (null (listp (car (cdr ccl-program)))))
352 (error "CCL: Invalid CCL program: %s" ccl-program))
353 (if (null (vectorp ccl-program-vector))
354 (setq ccl-program-vector (make-vector 8192 0)))
355 (setq ccl-loop-head nil ccl-breaks nil)
356 (setq ccl-current-ic 0)
357
358 ;; The first element is the buffer magnification.
359 (ccl-embed-data (car ccl-program))
360
361 ;; The second element is the address of the start CCL code for
362 ;; processing end of input buffer (we call it eof-processor). We
363 ;; set it later.
364 (ccl-increment-ic 1)
365
366 ;; Compile the main body of the CCL program.
367 (ccl-compile-1 (car (cdr ccl-program)))
368
369 ;; Embed the address of eof-processor.
370 (ccl-embed-data ccl-current-ic 1)
371
372 ;; Then compile eof-processor.
373 (if (nth 2 ccl-program)
374 (ccl-compile-1 (nth 2 ccl-program)))
375
376 ;; At last, embed termination code.
377 (ccl-embed-code 'end 0 0)
378
379 (let ((vec (make-vector ccl-current-ic 0))
380 (i 0))
381 (while (< i ccl-current-ic)
382 (aset vec i (aref ccl-program-vector i))
383 (setq i (1+ i)))
384 vec))
385
386 ;; Signal syntax error.
387 (defun ccl-syntax-error (cmd)
388 (error "CCL: Syntax error: %s" cmd))
389
390 ;; Check if ARG is a valid CCL register.
391 (defun ccl-check-register (arg cmd)
392 (if (get arg 'ccl-register-number)
393 arg
394 (error "CCL: Invalid register %s in %s." arg cmd)))
395
396 ;; Check if ARG is a valid CCL command.
397 (defun ccl-check-compile-function (arg cmd)
398 (or (get arg 'ccl-compile-function)
399 (error "CCL: Invalid command: %s" cmd)))
400
401 ;; In the following code, most ccl-compile-XXXX functions return t if
402 ;; they end with unconditional jump, else return nil.
403
404 ;; Compile CCL-BLOCK (see the syntax above).
405 (defun ccl-compile-1 (ccl-block)
406 (let (unconditional-jump
407 cmd)
408 (if (or (integerp ccl-block)
409 (stringp ccl-block)
410 (and ccl-block (symbolp (car ccl-block))))
411 ;; This block consists of single statement.
412 (setq ccl-block (list ccl-block)))
413
414 ;; Now CCL-BLOCK is a list of statements. Compile them one by
415 ;; one.
416 (while ccl-block
417 (setq cmd (car ccl-block))
418 (setq unconditional-jump
419 (cond ((integerp cmd)
420 ;; SET statement for the register 0.
421 (ccl-compile-set (list 'r0 '= cmd)))
422
423 ((stringp cmd)
424 ;; WRITE statement of string argument.
425 (ccl-compile-write-string cmd))
426
427 ((listp cmd)
428 ;; The other statements.
429 (cond ((eq (nth 1 cmd) '=)
430 ;; SET statement of the form `(REG = EXPRESSION)'.
431 (ccl-compile-set cmd))
432
433 ((and (symbolp (nth 1 cmd))
434 (get (nth 1 cmd) 'ccl-self-arith-code))
435 ;; SET statement with an assignment operation.
436 (ccl-compile-self-set cmd))
437
438 (t
439 (funcall (ccl-check-compile-function (car cmd) cmd)
440 cmd))))
441
442 (t
443 (ccl-syntax-error cmd))))
444 (setq ccl-block (cdr ccl-block)))
445 unconditional-jump))
446
447 (defconst ccl-max-short-const (ash 1 19))
448 (defconst ccl-min-short-const (ash -1 19))
449
450 ;; Compile SET statement.
451 (defun ccl-compile-set (cmd)
452 (let ((rrr (ccl-check-register (car cmd) cmd))
453 (right (nth 2 cmd)))
454 (cond ((listp right)
455 ;; CMD has the form `(RRR = (XXX OP YYY))'.
456 (ccl-compile-expression rrr right))
457
458 ((integerp right)
459 ;; CMD has the form `(RRR = integer)'.
460 (if (and (<= right ccl-max-short-const)
461 (>= right ccl-min-short-const))
462 (ccl-embed-code 'set-short-const rrr right)
463 (ccl-embed-code 'set-const rrr 0)
464 (ccl-embed-data right)))
465
466 (t
467 ;; CMD has the form `(RRR = rrr [ array ])'.
468 (ccl-check-register right cmd)
469 (let ((ary (nth 3 cmd)))
470 (if (vectorp ary)
471 (let ((i 0) (len (length ary)))
472 (ccl-embed-code 'set-array rrr len right)
473 (while (< i len)
474 (ccl-embed-data (aref ary i))
475 (setq i (1+ i))))
476 (ccl-embed-code 'set-register rrr 0 right))))))
477 nil)
478
479 ;; Compile SET statement with ASSIGNMENT_OPERATOR.
480 (defun ccl-compile-self-set (cmd)
481 (let ((rrr (ccl-check-register (car cmd) cmd))
482 (right (nth 2 cmd)))
483 (if (listp right)
484 ;; CMD has the form `(RRR ASSIGN_OP (XXX OP YYY))', compile
485 ;; the right hand part as `(r7 = (XXX OP YYY))' (note: the
486 ;; register 7 can be used for storing temporary value).
487 (progn
488 (ccl-compile-expression 'r7 right)
489 (setq right 'r7)))
490 ;; Now CMD has the form `(RRR ASSIGN_OP ARG)'. Compile it as
491 ;; `(RRR = (RRR OP ARG))'.
492 (ccl-compile-expression
493 rrr
494 (list rrr (intern (substring (symbol-name (nth 1 cmd)) 0 -1)) right)))
495 nil)
496
497 ;; Compile SET statement of the form `(RRR = EXPR)'.
498 (defun ccl-compile-expression (rrr expr)
499 (let ((left (car expr))
500 (op (get (nth 1 expr) 'ccl-arith-code))
501 (right (nth 2 expr)))
502 (if (listp left)
503 (progn
504 ;; EXPR has the form `((EXPR2 OP2 ARG) OP RIGHT)'. Compile
505 ;; the first term as `(r7 = (EXPR2 OP2 ARG)).'
506 (ccl-compile-expression 'r7 left)
507 (setq left 'r7)))
508
509 ;; Now EXPR has the form (LEFT OP RIGHT).
510 (if (eq rrr left)
511 ;; Compile this SET statement as `(RRR OP= RIGHT)'.
512 (if (integerp right)
513 (progn
514 (ccl-embed-code 'set-assign-expr-const rrr (ash op 3) 'r0)
515 (ccl-embed-data right))
516 (ccl-check-register right expr)
517 (ccl-embed-code 'set-assign-expr-register rrr (ash op 3) right))
518
519 ;; Compile this SET statement as `(RRR = (LEFT OP RIGHT))'.
520 (if (integerp right)
521 (progn
522 (ccl-embed-code 'set-expr-const rrr (ash op 3) left)
523 (ccl-embed-data right))
524 (ccl-check-register right expr)
525 (ccl-embed-code 'set-expr-register
526 rrr
527 (logior (ash op 3) (get right 'ccl-register-number))
528 left)))))
529
530 ;; Compile WRITE statement with string argument.
531 (defun ccl-compile-write-string (str)
532 (let ((len (length str)))
533 (ccl-embed-code 'write-const-string 1 len)
534 (ccl-embed-string len str))
535 nil)
536
537 ;; Compile IF statement of the form `(if CONDITION TRUE-PART FALSE-PART)'.
538 ;; If READ-FLAG is non-nil, this statement has the form
539 ;; `(read-if (REG OPERATOR ARG) TRUE-PART FALSE-PART)'.
540 (defun ccl-compile-if (cmd &optional read-flag)
541 (if (and (/= (length cmd) 3) (/= (length cmd) 4))
542 (error "CCL: Invalid number of arguments: %s" cmd))
543 (let ((condition (nth 1 cmd))
544 (true-cmds (nth 2 cmd))
545 (false-cmds (nth 3 cmd))
546 jump-cond-address
547 false-ic)
548 (if (and (listp condition)
549 (listp (car condition)))
550 ;; If CONDITION is a nested expression, the inner expression
551 ;; should be compiled at first as SET statement, i.e.:
552 ;; `(if ((X OP2 Y) OP Z) ...)' is compiled into two statements:
553 ;; `(r7 = (X OP2 Y)) (if (r7 OP Z) ...)'.
554 (progn
555 (ccl-compile-expression 'r7 (car condition))
556 (setq condition (cons 'r7 (cdr condition)))
557 (setq cmd (cons (car cmd)
558 (cons condition (cdr (cdr cmd)))))))
559
560 (setq jump-cond-address ccl-current-ic)
561 ;; Compile CONDITION.
562 (if (symbolp condition)
563 ;; CONDITION is a register.
564 (progn
565 (ccl-check-register condition cmd)
566 (ccl-embed-code 'jump-cond condition 0))
567 ;; CONDITION is a simple expression of the form (RRR OP ARG).
568 (let ((rrr (car condition))
569 (op (get (nth 1 condition) 'ccl-arith-code))
570 (arg (nth 2 condition)))
571 (ccl-check-register rrr cmd)
572 (if (integerp arg)
573 (progn
574 (ccl-embed-code (if read-flag 'read-jump-cond-expr-const
575 'jump-cond-expr-const)
576 rrr 0)
577 (ccl-embed-data op)
578 (ccl-embed-data arg))
579 (ccl-check-register arg cmd)
580 (ccl-embed-code (if read-flag 'read-jump-cond-expr-register
581 'jump-cond-expr-register)
582 rrr 0)
583 (ccl-embed-data op)
584 (ccl-embed-data (get arg 'ccl-register-number)))))
585
586 ;; Compile TRUE-PART.
587 (let ((unconditional-jump (ccl-compile-1 true-cmds)))
588 (if (null false-cmds)
589 ;; This is the place to jump to if condition is false.
590 (ccl-embed-current-address jump-cond-address)
591 (let (end-true-part-address)
592 (if (not unconditional-jump)
593 (progn
594 ;; If TRUE-PART does not end with unconditional jump, we
595 ;; have to jump to the end of FALSE-PART from here.
596 (setq end-true-part-address ccl-current-ic)
597 (ccl-embed-code 'jump 0 0)))
598 ;; This is the place to jump to if CONDITION is false.
599 (ccl-embed-current-address jump-cond-address)
600 ;; Compile FALSE-PART.
601 (setq unconditional-jump
602 (and (ccl-compile-1 false-cmds) unconditional-jump))
603 (if end-true-part-address
604 ;; This is the place to jump to after the end of TRUE-PART.
605 (ccl-embed-current-address end-true-part-address))))
606 unconditional-jump)))
607
608 ;; Compile BRANCH statement.
609 (defun ccl-compile-branch (cmd)
610 (if (< (length cmd) 3)
611 (error "CCL: Invalid number of arguments: %s" cmd))
612 (ccl-compile-branch-blocks 'branch
613 (ccl-compile-branch-expression (nth 1 cmd) cmd)
614 (cdr (cdr cmd))))
615
616 ;; Compile READ statement of the form `(read-branch EXPR BLOCK0 BLOCK1 ...)'.
617 (defun ccl-compile-read-branch (cmd)
618 (if (< (length cmd) 3)
619 (error "CCL: Invalid number of arguments: %s" cmd))
620 (ccl-compile-branch-blocks 'read-branch
621 (ccl-compile-branch-expression (nth 1 cmd) cmd)
622 (cdr (cdr cmd))))
623
624 ;; Compile EXPRESSION part of BRANCH statement and return register
625 ;; which holds a value of the expression.
626 (defun ccl-compile-branch-expression (expr cmd)
627 (if (listp expr)
628 ;; EXPR has the form `(EXPR2 OP ARG)'. Compile it as SET
629 ;; statement of the form `(r7 = (EXPR2 OP ARG))'.
630 (progn
631 (ccl-compile-expression 'r7 expr)
632 'r7)
633 (ccl-check-register expr cmd)))
634
635 ;; Compile BLOCKs of BRANCH statement. CODE is 'branch or 'read-branch.
636 ;; REG is a register which holds a value of EXPRESSION part. BLOCKs
637 ;; is a list of CCL-BLOCKs.
638 (defun ccl-compile-branch-blocks (code rrr blocks)
639 (let ((branches (length blocks))
640 branch-idx
641 jump-table-head-address
642 empty-block-indexes
643 block-tail-addresses
644 block-unconditional-jump)
645 (ccl-embed-code code rrr branches)
646 (setq jump-table-head-address ccl-current-ic)
647 ;; The size of jump table is the number of blocks plus 1 (for the
648 ;; case RRR is out of range).
649 (ccl-increment-ic (1+ branches))
650 (setq empty-block-indexes (list branches))
651 ;; Compile each block.
652 (setq branch-idx 0)
653 (while blocks
654 (if (null (car blocks))
655 ;; This block is empty.
656 (setq empty-block-indexes (cons branch-idx empty-block-indexes)
657 block-unconditional-jump t)
658 ;; This block is not empty.
659 (ccl-embed-data (- ccl-current-ic jump-table-head-address)
660 (+ jump-table-head-address branch-idx))
661 (setq block-unconditional-jump (ccl-compile-1 (car blocks)))
662 (if (not block-unconditional-jump)
663 (progn
664 ;; Jump address of the end of branches are embedded later.
665 ;; For the moment, just remember where to embed them.
666 (setq block-tail-addresses
667 (cons ccl-current-ic block-tail-addresses))
668 (ccl-embed-code 'jump 0 0))))
669 (setq branch-idx (1+ branch-idx))
670 (setq blocks (cdr blocks)))
671 (if (not block-unconditional-jump)
672 ;; We don't need jump code at the end of the last block.
673 (setq block-tail-addresses (cdr block-tail-addresses)
674 ccl-current-ic (1- ccl-current-ic)))
675 ;; Embed jump address at the tailing jump commands of blocks.
676 (while block-tail-addresses
677 (ccl-embed-current-address (car block-tail-addresses))
678 (setq block-tail-addresses (cdr block-tail-addresses)))
679 ;; For empty blocks, make entries in the jump table point directly here.
680 (while empty-block-indexes
681 (ccl-embed-data (- ccl-current-ic jump-table-head-address)
682 (+ jump-table-head-address (car empty-block-indexes)))
683 (setq empty-block-indexes (cdr empty-block-indexes))))
684 ;; Branch command ends by unconditional jump if RRR is out of range.
685 nil)
686
687 ;; Compile LOOP statement.
688 (defun ccl-compile-loop (cmd)
689 (if (< (length cmd) 2)
690 (error "CCL: Invalid number of arguments: %s" cmd))
691 (let* ((ccl-loop-head ccl-current-ic)
692 (ccl-breaks nil)
693 unconditional-jump)
694 (setq cmd (cdr cmd))
695 (if cmd
696 (progn
697 (setq unconditional-jump t)
698 (while cmd
699 (setq unconditional-jump
700 (and (ccl-compile-1 (car cmd)) unconditional-jump))
701 (setq cmd (cdr cmd)))
702 (if (not ccl-breaks)
703 unconditional-jump
704 ;; Embed jump address for break statements encountered in
705 ;; this loop.
706 (while ccl-breaks
707 (ccl-embed-current-address (car ccl-breaks))
708 (setq ccl-breaks (cdr ccl-breaks))))
709 nil))))
710
711 ;; Compile BREAK statement.
712 (defun ccl-compile-break (cmd)
713 (if (/= (length cmd) 1)
714 (error "CCL: Invalid number of arguments: %s" cmd))
715 (if (null ccl-loop-head)
716 (error "CCL: No outer loop: %s" cmd))
717 (setq ccl-breaks (cons ccl-current-ic ccl-breaks))
718 (ccl-embed-code 'jump 0 0)
719 t)
720
721 ;; Compile REPEAT statement.
722 (defun ccl-compile-repeat (cmd)
723 (if (/= (length cmd) 1)
724 (error "CCL: Invalid number of arguments: %s" cmd))
725 (if (null ccl-loop-head)
726 (error "CCL: No outer loop: %s" cmd))
727 (ccl-embed-code 'jump 0 ccl-loop-head)
728 t)
729
730 ;; Compile WRITE-REPEAT statement.
731 (defun ccl-compile-write-repeat (cmd)
732 (if (/= (length cmd) 2)
733 (error "CCL: Invalid number of arguments: %s" cmd))
734 (if (null ccl-loop-head)
735 (error "CCL: No outer loop: %s" cmd))
736 (let ((arg (nth 1 cmd)))
737 (cond ((integerp arg)
738 (ccl-embed-code 'write-const-jump 0 ccl-loop-head)
739 (ccl-embed-data arg))
740 ((stringp arg)
741 (let ((len (length arg))
742 (i 0))
743 (ccl-embed-code 'write-string-jump 0 ccl-loop-head)
744 (ccl-embed-data len)
745 (ccl-embed-string len arg)))
746 (t
747 (ccl-check-register arg cmd)
748 (ccl-embed-code 'write-register-jump arg ccl-loop-head))))
749 t)
750
751 ;; Compile WRITE-READ-REPEAT statement.
752 (defun ccl-compile-write-read-repeat (cmd)
753 (if (or (< (length cmd) 2) (> (length cmd) 3))
754 (error "CCL: Invalid number of arguments: %s" cmd))
755 (if (null ccl-loop-head)
756 (error "CCL: No outer loop: %s" cmd))
757 (let ((rrr (ccl-check-register (nth 1 cmd) cmd))
758 (arg (nth 2 cmd)))
759 (cond ((null arg)
760 (ccl-embed-code 'write-register-read-jump rrr ccl-loop-head))
761 ((integerp arg)
762 (ccl-embed-code 'write-const-read-jump rrr arg ccl-loop-head))
763 ((vectorp arg)
764 (let ((len (length arg))
765 (i 0))
766 (ccl-embed-code 'write-array-read-jump rrr ccl-loop-head)
767 (ccl-embed-data len)
768 (while (< i len)
769 (ccl-embed-data (aref arg i))
770 (setq i (1+ i)))))
771 (t
772 (error "CCL: Invalid argument %s: %s" arg cmd)))
773 (ccl-embed-code 'read-jump rrr ccl-loop-head))
774 t)
775
776 ;; Compile READ statement.
777 (defun ccl-compile-read (cmd)
778 (if (< (length cmd) 2)
779 (error "CCL: Invalid number of arguments: %s" cmd))
780 (let* ((args (cdr cmd))
781 (i (1- (length args))))
782 (while args
783 (let ((rrr (ccl-check-register (car args) cmd)))
784 (ccl-embed-code 'read-register rrr i)
785 (setq args (cdr args) i (1- i)))))
786 nil)
787
788 ;; Compile READ-IF statement.
789 (defun ccl-compile-read-if (cmd)
790 (ccl-compile-if cmd 'read))
791
792 ;; Compile WRITE statement.
793 (defun ccl-compile-write (cmd)
794 (if (< (length cmd) 2)
795 (error "CCL: Invalid number of arguments: %s" cmd))
796 (let ((rrr (nth 1 cmd)))
797 (cond ((integerp rrr)
798 (ccl-embed-code 'write-const-string 0 rrr))
799 ((stringp rrr)
800 (ccl-compile-write-string rrr))
801 ((and (symbolp rrr) (vectorp (nth 2 cmd)))
802 (ccl-check-register rrr cmd)
803 ;; CMD has the form `(write REG ARRAY)'.
804 (let* ((arg (nth 2 cmd))
805 (len (length arg))
806 (i 0))
807 (ccl-embed-code 'write-array rrr len)
808 (while (< i len)
809 (if (not (integerp (aref arg i)))
810 (error "CCL: Invalid argument %s: %s" arg cmd))
811 (ccl-embed-data (aref arg i))
812 (setq i (1+ i)))))
813
814 ((symbolp rrr)
815 ;; CMD has the form `(write REG ...)'.
816 (let* ((args (cdr cmd))
817 (i (1- (length args))))
818 (while args
819 (setq rrr (ccl-check-register (car args) cmd))
820 (ccl-embed-code 'write-register rrr i)
821 (setq args (cdr args) i (1- i)))))
822
823 ((listp rrr)
824 ;; CMD has the form `(write (LEFT OP RIGHT))'.
825 (let ((left (car rrr))
826 (op (get (nth 1 rrr) 'ccl-arith-code))
827 (right (nth 2 rrr)))
828 (if (listp left)
829 (progn
830 ;; RRR has the form `((EXPR OP2 ARG) OP RIGHT)'.
831 ;; Compile the first term as `(r7 = (EXPR OP2 ARG))'.
832 (ccl-compile-expression 'r7 left)
833 (setq left 'r7)))
834 ;; Now RRR has the form `(ARG OP RIGHT)'.
835 (if (integerp right)
836 (progn
837 (ccl-embed-code 'write-expr-const 0 (ash op 3) left)
838 (ccl-embed-data right))
839 (ccl-check-register right rrr)
840 (ccl-embed-code 'write-expr-register 0
841 (logior (ash op 3)
842 (get right 'ccl-register-number))))))
843
844 (t
845 (error "CCL: Invalid argument: %s" cmd))))
846 nil)
847
848 ;; Compile CALL statement.
849 (defun ccl-compile-call (cmd)
850 (if (/= (length cmd) 2)
851 (error "CCL: Invalid number of arguments: %s" cmd))
852 (if (not (symbolp (nth 1 cmd)))
853 (error "CCL: Subroutine should be a symbol: %s" cmd))
854 (let* ((name (nth 1 cmd))
855 (idx (get name 'ccl-program-idx)))
856 (if (not idx)
857 (error "CCL: Unknown subroutine name: %s" name))
858 (ccl-embed-code 'call 0 idx))
859 nil)
860
861 ;; Compile END statement.
862 (defun ccl-compile-end (cmd)
863 (if (/= (length cmd) 1)
864 (error "CCL: Invalid number of arguments: %s" cmd))
865 (ccl-embed-code 'end 0 0)
866 t)
867
868 ;; Compile read-multibyte-character
869 (defun ccl-compile-read-multibyte-character (cmd)
870 (if (/= (length cmd) 3)
871 (error "CCL: Invalid number of arguments: %s" cmd))
872 (let ((RRR (nth 1 cmd))
873 (rrr (nth 2 cmd)))
874 (ccl-check-register rrr cmd)
875 (ccl-check-register RRR cmd)
876 (ccl-embed-extended-command 'read-multibyte-character rrr RRR 0)))
877
878 ;; Compile write-multibyte-character
879 (defun ccl-compile-write-multibyte-character (cmd)
880 (if (/= (length cmd) 3)
881 (error "CCL: Invalid number of arguments: %s" cmd))
882 (let ((RRR (nth 1 cmd))
883 (rrr (nth 2 cmd)))
884 (ccl-check-register rrr cmd)
885 (ccl-check-register RRR cmd)
886 (ccl-embed-extended-command 'write-multibyte-character rrr RRR 0)))
887
888 ;; Compile unify-character
889 (defun ccl-compile-unify-character (cmd)
890 (if (/= (length cmd) 4)
891 (error "CCL: Invalid number of arguments: %s" cmd))
892 (let ((Rrr (nth 1 cmd))
893 (RRR (nth 2 cmd))
894 (rrr (nth 3 cmd)))
895 (ccl-check-register rrr cmd)
896 (ccl-check-register RRR cmd)
897 (cond ((symbolp Rrr)
898 (if (not (get Rrr 'unification-table))
899 (error "CCL: Invalid unification-table %s in %s" Rrr cmd))
900 (ccl-embed-extended-command 'unify-character-const-tbl rrr RRR 0)
901 (ccl-embed-data Rrr))
902 (t
903 (ccl-check-register Rrr cmd)
904 (ccl-embed-extended-command 'unify-character rrr RRR Rrr)))))
905
906 (defun ccl-compile-iterate-multiple-map (cmd)
907 (ccl-compile-multiple-map-function 'iterate-multiple-map cmd))
908
909 (defun ccl-compile-translate-multiple-map (cmd)
910 (if (/= (length cmd) 4)
911 (error "CCL: Invalid number of arguments: %s" cmd))
912 (let ((func '(lambda (arg mp)
913 (let ((len 0) result add)
914 (while arg
915 (if (consp (car arg))
916 (setq add (funcall func (car arg) t)
917 result (append result add)
918 add (+ (-(car add)) 1))
919 (setq result
920 (append result
921 (list (car arg)))
922 add 1))
923 (setq arg (cdr arg)
924 len (+ len add)))
925 (if mp
926 (cons (- len) result)
927 result))))
928 arg)
929 (setq arg (append (list (nth 0 cmd) (nth 1 cmd) (nth 2 cmd))
930 (funcall func (nth 3 cmd) nil)))
931 (ccl-compile-multiple-map-function 'translate-multiple-map arg)))
932
933 (defun ccl-compile-translate-single-map (cmd)
934 (if (/= (length cmd) 4)
935 (error "CCL: Invalid number of arguments: %s" cmd))
936 (let ((RRR (nth 1 cmd))
937 (rrr (nth 2 cmd))
938 (table (nth 3 cmd))
939 id)
940 (ccl-check-register rrr cmd)
941 (ccl-check-register RRR cmd)
942 (ccl-embed-extended-command 'translate-single-map rrr RRR 0)
943 (cond ((symbolp table)
944 (if (get table 'ccl-translation-table)
945 (ccl-embed-data table)
946 (error "CCL: Invalid table: %s" table)))
947 (t
948 (error "CCL: Invalid type of arguments: %s" cmd)))))
949
950 (defun ccl-compile-multiple-map-function (command cmd)
951 (if (< (length cmd) 4)
952 (error "CCL: Invalid number of arguments: %s" cmd))
953 (let ((RRR (nth 1 cmd))
954 (rrr (nth 2 cmd))
955 (args (nthcdr 3 cmd))
956 table)
957 (ccl-check-register rrr cmd)
958 (ccl-check-register RRR cmd)
959 (ccl-embed-extended-command command rrr RRR 0)
960 (ccl-embed-data (length args))
961 (while args
962 (setq table (car args))
963 (cond ((symbolp table)
964 (if (get table 'ccl-translation-table)
965 (ccl-embed-data table)
966 (error "CCL: Invalid table: %s" table)))
967 ((numberp table)
968 (ccl-embed-data table))
969 (t
970 (error "CCL: Invalid type of arguments: %s" cmd)))
971 (setq args (cdr args)))))
972
973 \f
974 ;;; CCL dump staffs
975
976 ;; To avoid byte-compiler warning.
977 (defvar ccl-code)
978
979 ;;;###autoload
980 (defun ccl-dump (ccl-code)
981 "Disassemble compiled CCL-CODE."
982 (let ((len (length ccl-code))
983 (buffer-mag (aref ccl-code 0)))
984 (cond ((= buffer-mag 0)
985 (insert "Don't output anything.\n"))
986 ((= buffer-mag 1)
987 (insert "Out-buffer must be as large as in-buffer.\n"))
988 (t
989 (insert
990 (format "Out-buffer must be %d times bigger than in-buffer.\n"
991 buffer-mag))))
992 (insert "Main-body:\n")
993 (setq ccl-current-ic 2)
994 (if (> (aref ccl-code 1) 0)
995 (progn
996 (while (< ccl-current-ic (aref ccl-code 1))
997 (ccl-dump-1))
998 (insert "At EOF:\n")))
999 (while (< ccl-current-ic len)
1000 (ccl-dump-1))
1001 ))
1002
1003 ;; Return a CCL code in `ccl-code' at `ccl-current-ic'.
1004 (defun ccl-get-next-code ()
1005 (prog1
1006 (aref ccl-code ccl-current-ic)
1007 (setq ccl-current-ic (1+ ccl-current-ic))))
1008
1009 (defun ccl-dump-1 ()
1010 (let* ((code (ccl-get-next-code))
1011 (cmd (aref ccl-code-table (logand code 31)))
1012 (rrr (ash (logand code 255) -5))
1013 (cc (ash code -8)))
1014 (insert (format "%5d:[%s] " (1- ccl-current-ic) cmd))
1015 (funcall (get cmd 'ccl-dump-function) rrr cc)))
1016
1017 (defun ccl-dump-set-register (rrr cc)
1018 (insert (format "r%d = r%d\n" rrr cc)))
1019
1020 (defun ccl-dump-set-short-const (rrr cc)
1021 (insert (format "r%d = %d\n" rrr cc)))
1022
1023 (defun ccl-dump-set-const (rrr ignore)
1024 (insert (format "r%d = %d\n" rrr (ccl-get-next-code))))
1025
1026 (defun ccl-dump-set-array (rrr cc)
1027 (let ((rrr2 (logand cc 7))
1028 (len (ash cc -3))
1029 (i 0))
1030 (insert (format "r%d = array[r%d] of length %d\n\t"
1031 rrr rrr2 len))
1032 (while (< i len)
1033 (insert (format "%d " (ccl-get-next-code)))
1034 (setq i (1+ i)))
1035 (insert "\n")))
1036
1037 (defun ccl-dump-jump (ignore cc &optional address)
1038 (insert (format "jump to %d(" (+ (or address ccl-current-ic) cc)))
1039 (if (>= cc 0)
1040 (insert "+"))
1041 (insert (format "%d)\n" (1+ cc))))
1042
1043 (defun ccl-dump-jump-cond (rrr cc)
1044 (insert (format "if (r%d == 0), " rrr))
1045 (ccl-dump-jump nil cc))
1046
1047 (defun ccl-dump-write-register-jump (rrr cc)
1048 (insert (format "write r%d, " rrr))
1049 (ccl-dump-jump nil cc))
1050
1051 (defun ccl-dump-write-register-read-jump (rrr cc)
1052 (insert (format "write r%d, read r%d, " rrr rrr))
1053 (ccl-dump-jump nil cc)
1054 (ccl-get-next-code) ; Skip dummy READ-JUMP
1055 )
1056
1057 (defun ccl-extract-arith-op (cc)
1058 (aref ccl-arith-table (ash cc -6)))
1059
1060 (defun ccl-dump-write-expr-const (ignore cc)
1061 (insert (format "write (r%d %s %d)\n"
1062 (logand cc 7)
1063 (ccl-extract-arith-op cc)
1064 (ccl-get-next-code))))
1065
1066 (defun ccl-dump-write-expr-register (ignore cc)
1067 (insert (format "write (r%d %s r%d)\n"
1068 (logand cc 7)
1069 (ccl-extract-arith-op cc)
1070 (logand (ash cc -3) 7))))
1071
1072 (defun ccl-dump-insert-char (cc)
1073 (cond ((= cc ?\t) (insert " \"^I\""))
1074 ((= cc ?\n) (insert " \"^J\""))
1075 (t (insert (format " \"%c\"" cc)))))
1076
1077 (defun ccl-dump-write-const-jump (ignore cc)
1078 (let ((address ccl-current-ic))
1079 (insert "write char")
1080 (ccl-dump-insert-char (ccl-get-next-code))
1081 (insert ", ")
1082 (ccl-dump-jump nil cc address)))
1083
1084 (defun ccl-dump-write-const-read-jump (rrr cc)
1085 (let ((address ccl-current-ic))
1086 (insert "write char")
1087 (ccl-dump-insert-char (ccl-get-next-code))
1088 (insert (format ", read r%d, " rrr))
1089 (ccl-dump-jump cc address)
1090 (ccl-get-next-code) ; Skip dummy READ-JUMP
1091 ))
1092
1093 (defun ccl-dump-write-string-jump (ignore cc)
1094 (let ((address ccl-current-ic)
1095 (len (ccl-get-next-code))
1096 (i 0))
1097 (insert "write \"")
1098 (while (< i len)
1099 (let ((code (ccl-get-next-code)))
1100 (insert (ash code -16))
1101 (if (< (1+ i) len) (insert (logand (ash code -8) 255)))
1102 (if (< (+ i 2) len) (insert (logand code 255))))
1103 (setq i (+ i 3)))
1104 (insert "\", ")
1105 (ccl-dump-jump nil cc address)))
1106
1107 (defun ccl-dump-write-array-read-jump (rrr cc)
1108 (let ((address ccl-current-ic)
1109 (len (ccl-get-next-code))
1110 (i 0))
1111 (insert (format "write array[r%d] of length %d,\n\t" rrr len))
1112 (while (< i len)
1113 (ccl-dump-insert-char (ccl-get-next-code))
1114 (setq i (1+ i)))
1115 (insert (format "\n\tthen read r%d, " rrr))
1116 (ccl-dump-jump nil cc address)
1117 (ccl-get-next-code) ; Skip dummy READ-JUMP.
1118 ))
1119
1120 (defun ccl-dump-read-jump (rrr cc)
1121 (insert (format "read r%d, " rrr))
1122 (ccl-dump-jump nil cc))
1123
1124 (defun ccl-dump-branch (rrr len)
1125 (let ((jump-table-head ccl-current-ic)
1126 (i 0))
1127 (insert (format "jump to array[r%d] of length %d\n\t" rrr len))
1128 (while (<= i len)
1129 (insert (format "%d " (+ jump-table-head (ccl-get-next-code))))
1130 (setq i (1+ i)))
1131 (insert "\n")))
1132
1133 (defun ccl-dump-read-register (rrr cc)
1134 (insert (format "read r%d (%d remaining)\n" rrr cc)))
1135
1136 (defun ccl-dump-read-branch (rrr len)
1137 (insert (format "read r%d, " rrr))
1138 (ccl-dump-branch rrr len))
1139
1140 (defun ccl-dump-write-register (rrr cc)
1141 (insert (format "write r%d (%d remaining)\n" rrr cc)))
1142
1143 (defun ccl-dump-call (ignore cc)
1144 (insert (format "call subroutine #%d\n" cc)))
1145
1146 (defun ccl-dump-write-const-string (rrr cc)
1147 (if (= rrr 0)
1148 (progn
1149 (insert "write char")
1150 (ccl-dump-insert-char cc)
1151 (newline))
1152 (let ((len cc)
1153 (i 0))
1154 (insert "write \"")
1155 (while (< i len)
1156 (let ((code (ccl-get-next-code)))
1157 (insert (format "%c" (lsh code -16)))
1158 (if (< (1+ i) len)
1159 (insert (format "%c" (logand (lsh code -8) 255))))
1160 (if (< (+ i 2) len)
1161 (insert (format "%c" (logand code 255))))
1162 (setq i (+ i 3))))
1163 (insert "\"\n"))))
1164
1165 (defun ccl-dump-write-array (rrr cc)
1166 (let ((i 0))
1167 (insert (format "write array[r%d] of length %d\n\t" rrr cc))
1168 (while (< i cc)
1169 (ccl-dump-insert-char (ccl-get-next-code))
1170 (setq i (1+ i)))
1171 (insert "\n")))
1172
1173 (defun ccl-dump-end (&rest ignore)
1174 (insert "end\n"))
1175
1176 (defun ccl-dump-set-assign-expr-const (rrr cc)
1177 (insert (format "r%d %s= %d\n"
1178 rrr
1179 (ccl-extract-arith-op cc)
1180 (ccl-get-next-code))))
1181
1182 (defun ccl-dump-set-assign-expr-register (rrr cc)
1183 (insert (format "r%d %s= r%d\n"
1184 rrr
1185 (ccl-extract-arith-op cc)
1186 (logand cc 7))))
1187
1188 (defun ccl-dump-set-expr-const (rrr cc)
1189 (insert (format "r%d = r%d %s %d\n"
1190 rrr
1191 (logand cc 7)
1192 (ccl-extract-arith-op cc)
1193 (ccl-get-next-code))))
1194
1195 (defun ccl-dump-set-expr-register (rrr cc)
1196 (insert (format "r%d = r%d %s r%d\n"
1197 rrr
1198 (logand cc 7)
1199 (ccl-extract-arith-op cc)
1200 (logand (ash cc -3) 7))))
1201
1202 (defun ccl-dump-jump-cond-expr-const (rrr cc)
1203 (let ((address ccl-current-ic))
1204 (insert (format "if !(r%d %s %d), "
1205 rrr
1206 (aref ccl-arith-table (ccl-get-next-code))
1207 (ccl-get-next-code)))
1208 (ccl-dump-jump nil cc address)))
1209
1210 (defun ccl-dump-jump-cond-expr-register (rrr cc)
1211 (let ((address ccl-current-ic))
1212 (insert (format "if !(r%d %s r%d), "
1213 rrr
1214 (aref ccl-arith-table (ccl-get-next-code))
1215 (ccl-get-next-code)))
1216 (ccl-dump-jump nil cc address)))
1217
1218 (defun ccl-dump-read-jump-cond-expr-const (rrr cc)
1219 (insert (format "read r%d, " rrr))
1220 (ccl-dump-jump-cond-expr-const rrr cc))
1221
1222 (defun ccl-dump-read-jump-cond-expr-register (rrr cc)
1223 (insert (format "read r%d, " rrr))
1224 (ccl-dump-jump-cond-expr-register rrr cc))
1225
1226 (defun ccl-dump-binary (ccl-code)
1227 (let ((len (length ccl-code))
1228 (i 2))
1229 (while (< i len)
1230 (let ((code (aref ccl-code i))
1231 (j 27))
1232 (while (>= j 0)
1233 (insert (if (= (logand code (ash 1 j)) 0) ?0 ?1))
1234 (setq j (1- j)))
1235 (setq code (logand code 31))
1236 (if (< code (length ccl-code-table))
1237 (insert (format ":%s" (aref ccl-code-table code))))
1238 (insert "\n"))
1239 (setq i (1+ i)))))
1240
1241 (defun ccl-dump-ex-cmd (rrr cc)
1242 (let* ((RRR (logand cc ?\x7))
1243 (Rrr (logand (ash cc -3) ?\x7))
1244 (ex-op (aref ccl-extended-code-table (logand (ash cc -6) ?\x3fff))))
1245 (insert (format "<%s> " ex-op))
1246 (funcall (get ex-op 'ccl-dump-function) rrr RRR Rrr)))
1247
1248 (defun ccl-dump-read-multibyte-character (rrr RRR Rrr)
1249 (insert (format "read-multibyte-character r%d r%d\n" RRR rrr)))
1250
1251 (defun ccl-dump-write-multibyte-character (rrr RRR Rrr)
1252 (insert (format "write-multibyte-character r%d r%d\n" RRR rrr)))
1253
1254 (defun ccl-dump-unify-character (rrr RRR Rrr)
1255 (insert (format "unify-character table(r%d) r%d r%d\n" Rrr RRR rrr)))
1256
1257 (defun ccl-dump-unify-character-const-tbl (rrr RRR Rrr)
1258 (let ((tbl (ccl-get-next-code)))
1259 (insert (format "unify-character table(%d) r%d r%d\n" tbl RRR rrr))))
1260
1261 (defun ccl-dump-iterate-multiple-map (rrr RRR Rrr)
1262 (let ((notbl (ccl-get-next-code))
1263 (i 0) id)
1264 (insert (format "iterate-multiple-map r%d r%d\n" RRR rrr))
1265 (insert (format "\tnumber of tables is %d .\n\t [" notbl))
1266 (while (< i notbl)
1267 (setq id (ccl-get-next-code))
1268 (insert (format "%S" id))
1269 (setq i (1+ i)))
1270 (insert "]\n")))
1271
1272 (defun ccl-dump-translate-multiple-map (rrr RRR Rrr)
1273 (let ((notbl (ccl-get-next-code))
1274 (i 0) id)
1275 (insert (format "translate-multiple-map r%d r%d\n" RRR rrr))
1276 (insert (format "\tnumber of tables and separators is %d\n\t [" notbl))
1277 (while (< i notbl)
1278 (setq id (ccl-get-next-code))
1279 (if (= id -1)
1280 (insert "]\n\t [")
1281 (insert (format "%S " id)))
1282 (setq i (1+ i)))
1283 (insert "]\n")))
1284
1285 (defun ccl-dump-translate-single-map (rrr RRR Rrr)
1286 (let ((id (ccl-get-next-code)))
1287 (insert (format "translate-single-map r%d r%d table(%S)\n" RRR rrr id))))
1288
1289 \f
1290 ;; CCL emulation staffs
1291
1292 ;; Not yet implemented.
1293 \f
1294 ;; Auto-loaded functions.
1295
1296 ;;;###autoload
1297 (defmacro declare-ccl-program (name &optional vector)
1298 "Declare NAME as a name of CCL program.
1299
1300 To compile a CCL program which calls another CCL program not yet
1301 defined, it must be declared as a CCL program in advance.
1302 Optional arg VECTOR is a compiled CCL code of the CCL program."
1303 `(put ',name 'ccl-program-idx (register-ccl-program ',name ,vector)))
1304
1305 ;;;###autoload
1306 (defmacro define-ccl-program (name ccl-program &optional doc)
1307 "Set NAME the compiled code of CCL-PROGRAM.
1308 CCL-PROGRAM is `eval'ed before being handed to the CCL compiler `ccl-compile'.
1309 The compiled code is a vector of integers."
1310 `(let ((prog ,(ccl-compile (eval ccl-program))))
1311 (defconst ,name prog ,doc)
1312 (put ',name 'ccl-program-idx (register-ccl-program ',name prog))
1313 nil))
1314
1315 ;;;###autoload
1316 (defmacro check-ccl-program (ccl-program &optional name)
1317 "Check validity of CCL-PROGRAM.
1318 If CCL-PROGRAM is a symbol denoting a valid CCL program, return
1319 CCL-PROGRAM, else return nil.
1320 If CCL-PROGRAM is a vector and optional arg NAME (symbol) is supplied,
1321 register CCL-PROGRAM by name NAME, and return NAME."
1322 `(let ((result ,ccl-program))
1323 (cond ((symbolp ,ccl-program)
1324 (or (numberp (get ,ccl-program 'ccl-program-idx))
1325 (setq result nil)))
1326 ((vectorp ,ccl-program)
1327 (setq result ,name)
1328 (register-ccl-program result ,ccl-program))
1329 (t
1330 (setq result nil)))
1331 result))
1332
1333 ;;;###autoload
1334 (defun ccl-execute-with-args (ccl-prog &rest args)
1335 "Execute CCL-PROGRAM with registers initialized by the remaining args.
1336 The return value is a vector of resulting CCL registeres."
1337 (let ((reg (make-vector 8 0))
1338 (i 0))
1339 (while (and args (< i 8))
1340 (if (not (integerp (car args)))
1341 (error "Arguments should be integer"))
1342 (aset reg i (car args))
1343 (setq args (cdr args) i (1+ i)))
1344 (ccl-execute ccl-prog reg)
1345 reg))
1346
1347 (provide 'ccl)
1348
1349 ;; ccl.el ends here