Change term translate-XXX-map to map-XXX
[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 := '[' interger ... ']'
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 comiled 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 'character-translation-table))
886 (error "CCL: Invalid character translation table %s in %s"
887 Rrr cmd))
888 (ccl-embed-extended-command 'translate-character-const-tbl
889 rrr RRR 0)
890 (ccl-embed-data Rrr))
891 (t
892 (ccl-check-register Rrr cmd)
893 (ccl-embed-extended-command 'translate-character rrr RRR Rrr)))))
894
895 (defun ccl-compile-iterate-multiple-map (cmd)
896 (ccl-compile-multiple-map-function 'iterate-multiple-map cmd))
897
898 (defun ccl-compile-map-multiple (cmd)
899 (if (/= (length cmd) 4)
900 (error "CCL: Invalid number of arguments: %s" cmd))
901 (let ((func '(lambda (arg mp)
902 (let ((len 0) result add)
903 (while arg
904 (if (consp (car arg))
905 (setq add (funcall func (car arg) t)
906 result (append result add)
907 add (+ (-(car add)) 1))
908 (setq result
909 (append result
910 (list (car arg)))
911 add 1))
912 (setq arg (cdr arg)
913 len (+ len add)))
914 (if mp
915 (cons (- len) result)
916 result))))
917 arg)
918 (setq arg (append (list (nth 0 cmd) (nth 1 cmd) (nth 2 cmd))
919 (funcall func (nth 3 cmd) nil)))
920 (ccl-compile-multiple-map-function 'map-multiple arg)))
921
922 (defun ccl-compile-map-single (cmd)
923 (if (/= (length cmd) 4)
924 (error "CCL: Invalid number of arguments: %s" cmd))
925 (let ((RRR (nth 1 cmd))
926 (rrr (nth 2 cmd))
927 (map (nth 3 cmd))
928 id)
929 (ccl-check-register rrr cmd)
930 (ccl-check-register RRR cmd)
931 (ccl-embed-extended-command 'map-single rrr RRR 0)
932 (cond ((symbolp map)
933 (if (get map 'code-conversion-map)
934 (ccl-embed-data map)
935 (error "CCL: Invalid map: %s" map)))
936 (t
937 (error "CCL: Invalid type of arguments: %s" cmd)))))
938
939 (defun ccl-compile-multiple-map-function (command cmd)
940 (if (< (length cmd) 4)
941 (error "CCL: Invalid number of arguments: %s" cmd))
942 (let ((RRR (nth 1 cmd))
943 (rrr (nth 2 cmd))
944 (args (nthcdr 3 cmd))
945 map)
946 (ccl-check-register rrr cmd)
947 (ccl-check-register RRR cmd)
948 (ccl-embed-extended-command command rrr RRR 0)
949 (ccl-embed-data (length args))
950 (while args
951 (setq map (car args))
952 (cond ((symbolp map)
953 (if (get map 'code-conversion-map)
954 (ccl-embed-data map)
955 (error "CCL: Invalid map: %s" map)))
956 ((numberp map)
957 (ccl-embed-data map))
958 (t
959 (error "CCL: Invalid type of arguments: %s" cmd)))
960 (setq args (cdr args)))))
961
962 \f
963 ;;; CCL dump staffs
964
965 ;; To avoid byte-compiler warning.
966 (defvar ccl-code)
967
968 ;;;###autoload
969 (defun ccl-dump (ccl-code)
970 "Disassemble compiled CCL-CODE."
971 (let ((len (length ccl-code))
972 (buffer-mag (aref ccl-code 0)))
973 (cond ((= buffer-mag 0)
974 (insert "Don't output anything.\n"))
975 ((= buffer-mag 1)
976 (insert "Out-buffer must be as large as in-buffer.\n"))
977 (t
978 (insert
979 (format "Out-buffer must be %d times bigger than in-buffer.\n"
980 buffer-mag))))
981 (insert "Main-body:\n")
982 (setq ccl-current-ic 2)
983 (if (> (aref ccl-code 1) 0)
984 (progn
985 (while (< ccl-current-ic (aref ccl-code 1))
986 (ccl-dump-1))
987 (insert "At EOF:\n")))
988 (while (< ccl-current-ic len)
989 (ccl-dump-1))
990 ))
991
992 ;; Return a CCL code in `ccl-code' at `ccl-current-ic'.
993 (defun ccl-get-next-code ()
994 (prog1
995 (aref ccl-code ccl-current-ic)
996 (setq ccl-current-ic (1+ ccl-current-ic))))
997
998 (defun ccl-dump-1 ()
999 (let* ((code (ccl-get-next-code))
1000 (cmd (aref ccl-code-table (logand code 31)))
1001 (rrr (ash (logand code 255) -5))
1002 (cc (ash code -8)))
1003 (insert (format "%5d:[%s] " (1- ccl-current-ic) cmd))
1004 (funcall (get cmd 'ccl-dump-function) rrr cc)))
1005
1006 (defun ccl-dump-set-register (rrr cc)
1007 (insert (format "r%d = r%d\n" rrr cc)))
1008
1009 (defun ccl-dump-set-short-const (rrr cc)
1010 (insert (format "r%d = %d\n" rrr cc)))
1011
1012 (defun ccl-dump-set-const (rrr ignore)
1013 (insert (format "r%d = %d\n" rrr (ccl-get-next-code))))
1014
1015 (defun ccl-dump-set-array (rrr cc)
1016 (let ((rrr2 (logand cc 7))
1017 (len (ash cc -3))
1018 (i 0))
1019 (insert (format "r%d = array[r%d] of length %d\n\t"
1020 rrr rrr2 len))
1021 (while (< i len)
1022 (insert (format "%d " (ccl-get-next-code)))
1023 (setq i (1+ i)))
1024 (insert "\n")))
1025
1026 (defun ccl-dump-jump (ignore cc &optional address)
1027 (insert (format "jump to %d(" (+ (or address ccl-current-ic) cc)))
1028 (if (>= cc 0)
1029 (insert "+"))
1030 (insert (format "%d)\n" (1+ cc))))
1031
1032 (defun ccl-dump-jump-cond (rrr cc)
1033 (insert (format "if (r%d == 0), " rrr))
1034 (ccl-dump-jump nil cc))
1035
1036 (defun ccl-dump-write-register-jump (rrr cc)
1037 (insert (format "write r%d, " rrr))
1038 (ccl-dump-jump nil cc))
1039
1040 (defun ccl-dump-write-register-read-jump (rrr cc)
1041 (insert (format "write r%d, read r%d, " rrr rrr))
1042 (ccl-dump-jump nil cc)
1043 (ccl-get-next-code) ; Skip dummy READ-JUMP
1044 )
1045
1046 (defun ccl-extract-arith-op (cc)
1047 (aref ccl-arith-table (ash cc -6)))
1048
1049 (defun ccl-dump-write-expr-const (ignore cc)
1050 (insert (format "write (r%d %s %d)\n"
1051 (logand cc 7)
1052 (ccl-extract-arith-op cc)
1053 (ccl-get-next-code))))
1054
1055 (defun ccl-dump-write-expr-register (ignore cc)
1056 (insert (format "write (r%d %s r%d)\n"
1057 (logand cc 7)
1058 (ccl-extract-arith-op cc)
1059 (logand (ash cc -3) 7))))
1060
1061 (defun ccl-dump-insert-char (cc)
1062 (cond ((= cc ?\t) (insert " \"^I\""))
1063 ((= cc ?\n) (insert " \"^J\""))
1064 (t (insert (format " \"%c\"" cc)))))
1065
1066 (defun ccl-dump-write-const-jump (ignore cc)
1067 (let ((address ccl-current-ic))
1068 (insert "write char")
1069 (ccl-dump-insert-char (ccl-get-next-code))
1070 (insert ", ")
1071 (ccl-dump-jump nil cc address)))
1072
1073 (defun ccl-dump-write-const-read-jump (rrr cc)
1074 (let ((address ccl-current-ic))
1075 (insert "write char")
1076 (ccl-dump-insert-char (ccl-get-next-code))
1077 (insert (format ", read r%d, " rrr))
1078 (ccl-dump-jump cc address)
1079 (ccl-get-next-code) ; Skip dummy READ-JUMP
1080 ))
1081
1082 (defun ccl-dump-write-string-jump (ignore cc)
1083 (let ((address ccl-current-ic)
1084 (len (ccl-get-next-code))
1085 (i 0))
1086 (insert "write \"")
1087 (while (< i len)
1088 (let ((code (ccl-get-next-code)))
1089 (insert (ash code -16))
1090 (if (< (1+ i) len) (insert (logand (ash code -8) 255)))
1091 (if (< (+ i 2) len) (insert (logand code 255))))
1092 (setq i (+ i 3)))
1093 (insert "\", ")
1094 (ccl-dump-jump nil cc address)))
1095
1096 (defun ccl-dump-write-array-read-jump (rrr cc)
1097 (let ((address ccl-current-ic)
1098 (len (ccl-get-next-code))
1099 (i 0))
1100 (insert (format "write array[r%d] of length %d,\n\t" rrr len))
1101 (while (< i len)
1102 (ccl-dump-insert-char (ccl-get-next-code))
1103 (setq i (1+ i)))
1104 (insert (format "\n\tthen read r%d, " rrr))
1105 (ccl-dump-jump nil cc address)
1106 (ccl-get-next-code) ; Skip dummy READ-JUMP.
1107 ))
1108
1109 (defun ccl-dump-read-jump (rrr cc)
1110 (insert (format "read r%d, " rrr))
1111 (ccl-dump-jump nil cc))
1112
1113 (defun ccl-dump-branch (rrr len)
1114 (let ((jump-table-head ccl-current-ic)
1115 (i 0))
1116 (insert (format "jump to array[r%d] of length %d\n\t" rrr len))
1117 (while (<= i len)
1118 (insert (format "%d " (+ jump-table-head (ccl-get-next-code))))
1119 (setq i (1+ i)))
1120 (insert "\n")))
1121
1122 (defun ccl-dump-read-register (rrr cc)
1123 (insert (format "read r%d (%d remaining)\n" rrr cc)))
1124
1125 (defun ccl-dump-read-branch (rrr len)
1126 (insert (format "read r%d, " rrr))
1127 (ccl-dump-branch rrr len))
1128
1129 (defun ccl-dump-write-register (rrr cc)
1130 (insert (format "write r%d (%d remaining)\n" rrr cc)))
1131
1132 (defun ccl-dump-call (ignore cc)
1133 (insert (format "call subroutine #%d\n" cc)))
1134
1135 (defun ccl-dump-write-const-string (rrr cc)
1136 (if (= rrr 0)
1137 (progn
1138 (insert "write char")
1139 (ccl-dump-insert-char cc)
1140 (newline))
1141 (let ((len cc)
1142 (i 0))
1143 (insert "write \"")
1144 (while (< i len)
1145 (let ((code (ccl-get-next-code)))
1146 (insert (format "%c" (lsh code -16)))
1147 (if (< (1+ i) len)
1148 (insert (format "%c" (logand (lsh code -8) 255))))
1149 (if (< (+ i 2) len)
1150 (insert (format "%c" (logand code 255))))
1151 (setq i (+ i 3))))
1152 (insert "\"\n"))))
1153
1154 (defun ccl-dump-write-array (rrr cc)
1155 (let ((i 0))
1156 (insert (format "write array[r%d] of length %d\n\t" rrr cc))
1157 (while (< i cc)
1158 (ccl-dump-insert-char (ccl-get-next-code))
1159 (setq i (1+ i)))
1160 (insert "\n")))
1161
1162 (defun ccl-dump-end (&rest ignore)
1163 (insert "end\n"))
1164
1165 (defun ccl-dump-set-assign-expr-const (rrr cc)
1166 (insert (format "r%d %s= %d\n"
1167 rrr
1168 (ccl-extract-arith-op cc)
1169 (ccl-get-next-code))))
1170
1171 (defun ccl-dump-set-assign-expr-register (rrr cc)
1172 (insert (format "r%d %s= r%d\n"
1173 rrr
1174 (ccl-extract-arith-op cc)
1175 (logand cc 7))))
1176
1177 (defun ccl-dump-set-expr-const (rrr cc)
1178 (insert (format "r%d = r%d %s %d\n"
1179 rrr
1180 (logand cc 7)
1181 (ccl-extract-arith-op cc)
1182 (ccl-get-next-code))))
1183
1184 (defun ccl-dump-set-expr-register (rrr cc)
1185 (insert (format "r%d = r%d %s r%d\n"
1186 rrr
1187 (logand cc 7)
1188 (ccl-extract-arith-op cc)
1189 (logand (ash cc -3) 7))))
1190
1191 (defun ccl-dump-jump-cond-expr-const (rrr cc)
1192 (let ((address ccl-current-ic))
1193 (insert (format "if !(r%d %s %d), "
1194 rrr
1195 (aref ccl-arith-table (ccl-get-next-code))
1196 (ccl-get-next-code)))
1197 (ccl-dump-jump nil cc address)))
1198
1199 (defun ccl-dump-jump-cond-expr-register (rrr cc)
1200 (let ((address ccl-current-ic))
1201 (insert (format "if !(r%d %s r%d), "
1202 rrr
1203 (aref ccl-arith-table (ccl-get-next-code))
1204 (ccl-get-next-code)))
1205 (ccl-dump-jump nil cc address)))
1206
1207 (defun ccl-dump-read-jump-cond-expr-const (rrr cc)
1208 (insert (format "read r%d, " rrr))
1209 (ccl-dump-jump-cond-expr-const rrr cc))
1210
1211 (defun ccl-dump-read-jump-cond-expr-register (rrr cc)
1212 (insert (format "read r%d, " rrr))
1213 (ccl-dump-jump-cond-expr-register rrr cc))
1214
1215 (defun ccl-dump-binary (ccl-code)
1216 (let ((len (length ccl-code))
1217 (i 2))
1218 (while (< i len)
1219 (let ((code (aref ccl-code i))
1220 (j 27))
1221 (while (>= j 0)
1222 (insert (if (= (logand code (ash 1 j)) 0) ?0 ?1))
1223 (setq j (1- j)))
1224 (setq code (logand code 31))
1225 (if (< code (length ccl-code-table))
1226 (insert (format ":%s" (aref ccl-code-table code))))
1227 (insert "\n"))
1228 (setq i (1+ i)))))
1229
1230 (defun ccl-dump-ex-cmd (rrr cc)
1231 (let* ((RRR (logand cc ?\x7))
1232 (Rrr (logand (ash cc -3) ?\x7))
1233 (ex-op (aref ccl-extended-code-table (logand (ash cc -6) ?\x3fff))))
1234 (insert (format "<%s> " ex-op))
1235 (funcall (get ex-op 'ccl-dump-function) rrr RRR Rrr)))
1236
1237 (defun ccl-dump-read-multibyte-character (rrr RRR Rrr)
1238 (insert (format "read-multibyte-character r%d r%d\n" RRR rrr)))
1239
1240 (defun ccl-dump-write-multibyte-character (rrr RRR Rrr)
1241 (insert (format "write-multibyte-character r%d r%d\n" RRR rrr)))
1242
1243 (defun ccl-dump-translate-character (rrr RRR Rrr)
1244 (insert (format "character translation table(r%d) r%d r%d\n" Rrr RRR rrr)))
1245
1246 (defun ccl-dump-translate-character-const-tbl (rrr RRR Rrr)
1247 (let ((tbl (ccl-get-next-code)))
1248 (insert (format "character translation table(%d) r%d r%d\n" tbl RRR rrr))))
1249
1250 (defun ccl-dump-iterate-multiple-map (rrr RRR Rrr)
1251 (let ((notbl (ccl-get-next-code))
1252 (i 0) id)
1253 (insert (format "iterate-multiple-map r%d r%d\n" RRR rrr))
1254 (insert (format "\tnumber of maps is %d .\n\t [" notbl))
1255 (while (< i notbl)
1256 (setq id (ccl-get-next-code))
1257 (insert (format "%S" id))
1258 (setq i (1+ i)))
1259 (insert "]\n")))
1260
1261 (defun ccl-dump-map-multiple (rrr RRR Rrr)
1262 (let ((notbl (ccl-get-next-code))
1263 (i 0) id)
1264 (insert (format "map-multiple r%d r%d\n" RRR rrr))
1265 (insert (format "\tnumber of maps and separators is %d\n\t [" notbl))
1266 (while (< i notbl)
1267 (setq id (ccl-get-next-code))
1268 (if (= id -1)
1269 (insert "]\n\t [")
1270 (insert (format "%S " id)))
1271 (setq i (1+ i)))
1272 (insert "]\n")))
1273
1274 (defun ccl-dump-map-single (rrr RRR Rrr)
1275 (let ((id (ccl-get-next-code)))
1276 (insert (format "map-single r%d r%d map(%S)\n" RRR rrr id))))
1277
1278 \f
1279 ;; CCL emulation staffs
1280
1281 ;; Not yet implemented.
1282 \f
1283 ;; Auto-loaded functions.
1284
1285 ;;;###autoload
1286 (defmacro declare-ccl-program (name &optional vector)
1287 "Declare NAME as a name of CCL program.
1288
1289 To compile a CCL program which calls another CCL program not yet
1290 defined, it must be declared as a CCL program in advance.
1291 Optional arg VECTOR is a compiled CCL code of the CCL program."
1292 `(put ',name 'ccl-program-idx (register-ccl-program ',name ,vector)))
1293
1294 ;;;###autoload
1295 (defmacro define-ccl-program (name ccl-program &optional doc)
1296 "Set NAME the compiled code of CCL-PROGRAM.
1297 CCL-PROGRAM is `eval'ed before being handed to the CCL compiler `ccl-compile'.
1298 The compiled code is a vector of integers."
1299 `(let ((prog ,(ccl-compile (eval ccl-program))))
1300 (defconst ,name prog ,doc)
1301 (put ',name 'ccl-program-idx (register-ccl-program ',name prog))
1302 nil))
1303
1304 ;;;###autoload
1305 (defmacro check-ccl-program (ccl-program &optional name)
1306 "Check validity of CCL-PROGRAM.
1307 If CCL-PROGRAM is a symbol denoting a valid CCL program, return
1308 CCL-PROGRAM, else return nil.
1309 If CCL-PROGRAM is a vector and optional arg NAME (symbol) is supplied,
1310 register CCL-PROGRAM by name NAME, and return NAME."
1311 `(let ((result ,ccl-program))
1312 (cond ((symbolp ,ccl-program)
1313 (or (numberp (get ,ccl-program 'ccl-program-idx))
1314 (setq result nil)))
1315 ((vectorp ,ccl-program)
1316 (setq result ,name)
1317 (register-ccl-program result ,ccl-program))
1318 (t
1319 (setq result nil)))
1320 result))
1321
1322 ;;;###autoload
1323 (defun ccl-execute-with-args (ccl-prog &rest args)
1324 "Execute CCL-PROGRAM with registers initialized by the remaining args.
1325 The return value is a vector of resulting CCL registeres."
1326 (let ((reg (make-vector 8 0))
1327 (i 0))
1328 (while (and args (< i 8))
1329 (if (not (integerp (car args)))
1330 (error "Arguments should be integer"))
1331 (aset reg i (car args))
1332 (setq args (cdr args) i (1+ i)))
1333 (ccl-execute ccl-prog reg)
1334 reg))
1335
1336 (provide 'ccl)
1337
1338 ;; ccl.el ends here