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