]> code.delx.au - gnu-emacs/blob - lisp/international/ccl.el
(find-new-buffer-file-coding-system): Doc fix.
[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 ;; 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 (progn
578 (ccl-embed-current-address jump-cond-address)
579 (setq unconditional-jump nil))
580 (let (end-true-part-address)
581 (if (not unconditional-jump)
582 (progn
583 ;; If TRUE-PART does not end with unconditional jump, we
584 ;; have to jump to the end of FALSE-PART from here.
585 (setq end-true-part-address ccl-current-ic)
586 (ccl-embed-code 'jump 0 0)))
587 ;; This is the place to jump to if CONDITION is false.
588 (ccl-embed-current-address jump-cond-address)
589 ;; Compile FALSE-PART.
590 (setq unconditional-jump
591 (and (ccl-compile-1 false-cmds) unconditional-jump))
592 (if end-true-part-address
593 ;; This is the place to jump to after the end of TRUE-PART.
594 (ccl-embed-current-address end-true-part-address))))
595 unconditional-jump)))
596
597 ;; Compile BRANCH statement.
598 (defun ccl-compile-branch (cmd)
599 (if (< (length cmd) 3)
600 (error "CCL: Invalid number of arguments: %s" cmd))
601 (ccl-compile-branch-blocks 'branch
602 (ccl-compile-branch-expression (nth 1 cmd) cmd)
603 (cdr (cdr cmd))))
604
605 ;; Compile READ statement of the form `(read-branch EXPR BLOCK0 BLOCK1 ...)'.
606 (defun ccl-compile-read-branch (cmd)
607 (if (< (length cmd) 3)
608 (error "CCL: Invalid number of arguments: %s" cmd))
609 (ccl-compile-branch-blocks 'read-branch
610 (ccl-compile-branch-expression (nth 1 cmd) cmd)
611 (cdr (cdr cmd))))
612
613 ;; Compile EXPRESSION part of BRANCH statement and return register
614 ;; which holds a value of the expression.
615 (defun ccl-compile-branch-expression (expr cmd)
616 (if (listp expr)
617 ;; EXPR has the form `(EXPR2 OP ARG)'. Compile it as SET
618 ;; statement of the form `(r7 = (EXPR2 OP ARG))'.
619 (progn
620 (ccl-compile-expression 'r7 expr)
621 'r7)
622 (ccl-check-register expr cmd)))
623
624 ;; Compile BLOCKs of BRANCH statement. CODE is 'branch or 'read-branch.
625 ;; REG is a register which holds a value of EXPRESSION part. BLOCKs
626 ;; is a list of CCL-BLOCKs.
627 (defun ccl-compile-branch-blocks (code rrr blocks)
628 (let ((branches (length blocks))
629 branch-idx
630 jump-table-head-address
631 empty-block-indexes
632 block-tail-addresses
633 block-unconditional-jump)
634 (ccl-embed-code code rrr branches)
635 (setq jump-table-head-address ccl-current-ic)
636 ;; The size of jump table is the number of blocks plus 1 (for the
637 ;; case RRR is out of range).
638 (ccl-increment-ic (1+ branches))
639 (setq empty-block-indexes (list branches))
640 ;; Compile each block.
641 (setq branch-idx 0)
642 (while blocks
643 (if (null (car blocks))
644 ;; This block is empty.
645 (setq empty-block-indexes (cons branch-idx empty-block-indexes)
646 block-unconditional-jump t)
647 ;; This block is not empty.
648 (ccl-embed-data (- ccl-current-ic jump-table-head-address)
649 (+ jump-table-head-address branch-idx))
650 (setq block-unconditional-jump (ccl-compile-1 (car blocks)))
651 (if (not block-unconditional-jump)
652 (progn
653 ;; Jump address of the end of branches are embedded later.
654 ;; For the moment, just remember where to embed them.
655 (setq block-tail-addresses
656 (cons ccl-current-ic block-tail-addresses))
657 (ccl-embed-code 'jump 0 0))))
658 (setq branch-idx (1+ branch-idx))
659 (setq blocks (cdr blocks)))
660 (if (not block-unconditional-jump)
661 ;; We don't need jump code at the end of the last block.
662 (setq block-tail-addresses (cdr block-tail-addresses)
663 ccl-current-ic (1- ccl-current-ic)))
664 ;; Embed jump address at the tailing jump commands of blocks.
665 (while block-tail-addresses
666 (ccl-embed-current-address (car block-tail-addresses))
667 (setq block-tail-addresses (cdr block-tail-addresses)))
668 ;; For empty blocks, make entries in the jump table point directly here.
669 (while empty-block-indexes
670 (ccl-embed-data (- ccl-current-ic jump-table-head-address)
671 (+ jump-table-head-address (car empty-block-indexes)))
672 (setq empty-block-indexes (cdr empty-block-indexes))))
673 ;; Branch command ends by unconditional jump if RRR is out of range.
674 nil)
675
676 ;; Compile LOOP statement.
677 (defun ccl-compile-loop (cmd)
678 (if (< (length cmd) 2)
679 (error "CCL: Invalid number of arguments: %s" cmd))
680 (let* ((ccl-loop-head ccl-current-ic)
681 (ccl-breaks nil)
682 unconditional-jump)
683 (setq cmd (cdr cmd))
684 (if cmd
685 (progn
686 (setq unconditional-jump t)
687 (while cmd
688 (setq unconditional-jump
689 (and (ccl-compile-1 (car cmd)) unconditional-jump))
690 (setq cmd (cdr cmd)))
691 (if (not ccl-breaks)
692 unconditional-jump
693 ;; Embed jump address for break statements encountered in
694 ;; this loop.
695 (while ccl-breaks
696 (ccl-embed-current-address (car ccl-breaks))
697 (setq ccl-breaks (cdr ccl-breaks))))
698 nil))))
699
700 ;; Compile BREAK statement.
701 (defun ccl-compile-break (cmd)
702 (if (/= (length cmd) 1)
703 (error "CCL: Invalid number of arguments: %s" cmd))
704 (if (null ccl-loop-head)
705 (error "CCL: No outer loop: %s" cmd))
706 (setq ccl-breaks (cons ccl-current-ic ccl-breaks))
707 (ccl-embed-code 'jump 0 0)
708 t)
709
710 ;; Compile REPEAT statement.
711 (defun ccl-compile-repeat (cmd)
712 (if (/= (length cmd) 1)
713 (error "CCL: Invalid number of arguments: %s" cmd))
714 (if (null ccl-loop-head)
715 (error "CCL: No outer loop: %s" cmd))
716 (ccl-embed-code 'jump 0 ccl-loop-head)
717 t)
718
719 ;; Compile WRITE-REPEAT statement.
720 (defun ccl-compile-write-repeat (cmd)
721 (if (/= (length cmd) 2)
722 (error "CCL: Invalid number of arguments: %s" cmd))
723 (if (null ccl-loop-head)
724 (error "CCL: No outer loop: %s" cmd))
725 (let ((arg (nth 1 cmd)))
726 (cond ((integerp arg)
727 (ccl-embed-code 'write-const-jump 0 ccl-loop-head)
728 (ccl-embed-data arg))
729 ((stringp arg)
730 (let ((len (length arg))
731 (i 0))
732 (ccl-embed-code 'write-string-jump 0 ccl-loop-head)
733 (ccl-embed-data len)
734 (ccl-embed-string len arg)))
735 (t
736 (ccl-check-register arg cmd)
737 (ccl-embed-code 'write-register-jump arg ccl-loop-head))))
738 t)
739
740 ;; Compile WRITE-READ-REPEAT statement.
741 (defun ccl-compile-write-read-repeat (cmd)
742 (if (or (< (length cmd) 2) (> (length cmd) 3))
743 (error "CCL: Invalid number of arguments: %s" cmd))
744 (if (null ccl-loop-head)
745 (error "CCL: No outer loop: %s" cmd))
746 (let ((rrr (ccl-check-register (nth 1 cmd) cmd))
747 (arg (nth 2 cmd)))
748 (cond ((null arg)
749 (ccl-embed-code 'write-register-read-jump rrr ccl-loop-head))
750 ((integerp arg)
751 (ccl-embed-code 'write-const-read-jump rrr arg ccl-loop-head))
752 ((vectorp arg)
753 (let ((len (length arg))
754 (i 0))
755 (ccl-embed-code 'write-array-read-jump rrr ccl-loop-head)
756 (ccl-embed-data len)
757 (while (< i len)
758 (ccl-embed-data (aref arg i))
759 (setq i (1+ i)))))
760 (t
761 (error "CCL: Invalid argument %s: %s" arg cmd)))
762 (ccl-embed-code 'read-jump rrr ccl-loop-head))
763 t)
764
765 ;; Compile READ statement.
766 (defun ccl-compile-read (cmd)
767 (if (< (length cmd) 2)
768 (error "CCL: Invalid number of arguments: %s" cmd))
769 (let* ((args (cdr cmd))
770 (i (1- (length args))))
771 (while args
772 (let ((rrr (ccl-check-register (car args) cmd)))
773 (ccl-embed-code 'read-register rrr i)
774 (setq args (cdr args) i (1- i)))))
775 nil)
776
777 ;; Compile READ-IF statement.
778 (defun ccl-compile-read-if (cmd)
779 (ccl-compile-if cmd 'read))
780
781 ;; Compile WRITE statement.
782 (defun ccl-compile-write (cmd)
783 (if (< (length cmd) 2)
784 (error "CCL: Invalid number of arguments: %s" cmd))
785 (let ((rrr (nth 1 cmd)))
786 (cond ((integerp rrr)
787 (ccl-embed-code 'write-const-string 0 rrr))
788 ((stringp rrr)
789 (ccl-compile-write-string rrr))
790 ((and (symbolp rrr) (vectorp (nth 2 cmd)))
791 (ccl-check-register rrr cmd)
792 ;; CMD has the form `(write REG ARRAY)'.
793 (let* ((arg (nth 2 cmd))
794 (len (length arg))
795 (i 0))
796 (ccl-embed-code 'write-array rrr len)
797 (while (< i len)
798 (if (not (integerp (aref arg i)))
799 (error "CCL: Invalid argument %s: %s" arg cmd))
800 (ccl-embed-data (aref arg i))
801 (setq i (1+ i)))))
802
803 ((symbolp rrr)
804 ;; CMD has the form `(write REG ...)'.
805 (let* ((args (cdr cmd))
806 (i (1- (length args))))
807 (while args
808 (setq rrr (ccl-check-register (car args) cmd))
809 (ccl-embed-code 'write-register rrr i)
810 (setq args (cdr args) i (1- i)))))
811
812 ((listp rrr)
813 ;; CMD has the form `(write (LEFT OP RIGHT))'.
814 (let ((left (car rrr))
815 (op (get (nth 1 rrr) 'ccl-arith-code))
816 (right (nth 2 rrr)))
817 (if (listp left)
818 (progn
819 ;; RRR has the form `((EXPR OP2 ARG) OP RIGHT)'.
820 ;; Compile the first term as `(r7 = (EXPR OP2 ARG))'.
821 (ccl-compile-expression 'r7 left)
822 (setq left 'r7)))
823 ;; Now RRR has the form `(ARG OP RIGHT)'.
824 (if (integerp right)
825 (progn
826 (ccl-embed-code 'write-expr-const 0 (ash op 3) left)
827 (ccl-embed-data right))
828 (ccl-check-register right rrr)
829 (ccl-embed-code 'write-expr-register 0
830 (logior (ash op 3)
831 (get right 'ccl-register-number))))))
832
833 (t
834 (error "CCL: Invalid argument: %s" cmd))))
835 nil)
836
837 ;; Compile CALL statement.
838 (defun ccl-compile-call (cmd)
839 (if (/= (length cmd) 2)
840 (error "CCL: Invalid number of arguments: %s" cmd))
841 (if (not (symbolp (nth 1 cmd)))
842 (error "CCL: Subroutine should be a symbol: %s" cmd))
843 (let* ((name (nth 1 cmd))
844 (idx (get name 'ccl-program-idx)))
845 (if (not idx)
846 (error "CCL: Unknown subroutine name: %s" name))
847 (ccl-embed-code 'call 0 idx))
848 nil)
849
850 ;; Compile END statement.
851 (defun ccl-compile-end (cmd)
852 (if (/= (length cmd) 1)
853 (error "CCL: Invalid number of arguments: %s" cmd))
854 (ccl-embed-code 'end 0 0)
855 t)
856
857 ;; Compile read-multibyte-character
858 (defun ccl-compile-read-multibyte-character (cmd)
859 (if (/= (length cmd) 3)
860 (error "CCL: Invalid number of arguments: %s" cmd))
861 (let ((RRR (nth 1 cmd))
862 (rrr (nth 2 cmd)))
863 (ccl-check-register rrr cmd)
864 (ccl-check-register RRR cmd)
865 (ccl-embed-extended-command 'read-multibyte-character rrr RRR 0))
866 nil)
867
868 ;; Compile write-multibyte-character
869 (defun ccl-compile-write-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 'write-multibyte-character rrr RRR 0))
877 nil)
878
879 ;; Compile translate-character
880 (defun ccl-compile-translate-character (cmd)
881 (if (/= (length cmd) 4)
882 (error "CCL: Invalid number of arguments: %s" cmd))
883 (let ((Rrr (nth 1 cmd))
884 (RRR (nth 2 cmd))
885 (rrr (nth 3 cmd)))
886 (ccl-check-register rrr cmd)
887 (ccl-check-register RRR cmd)
888 (cond ((and (symbolp Rrr) (not (get Rrr 'ccl-register-number)))
889 (if (not (get Rrr 'translation-table))
890 (error "CCL: Invalid translation table %s in %s" Rrr cmd))
891 (ccl-embed-extended-command 'translate-character-const-tbl
892 rrr RRR 0)
893 (ccl-embed-data Rrr))
894 (t
895 (ccl-check-register Rrr cmd)
896 (ccl-embed-extended-command 'translate-character rrr RRR Rrr))))
897 nil)
898
899 (defun ccl-compile-iterate-multiple-map (cmd)
900 (ccl-compile-multiple-map-function 'iterate-multiple-map cmd)
901 nil)
902
903 (defun ccl-compile-map-multiple (cmd)
904 (if (/= (length cmd) 4)
905 (error "CCL: Invalid number of arguments: %s" cmd))
906 (let ((func '(lambda (arg mp)
907 (let ((len 0) result add)
908 (while arg
909 (if (consp (car arg))
910 (setq add (funcall func (car arg) t)
911 result (append result add)
912 add (+ (-(car add)) 1))
913 (setq result
914 (append result
915 (list (car arg)))
916 add 1))
917 (setq arg (cdr arg)
918 len (+ len add)))
919 (if mp
920 (cons (- len) result)
921 result))))
922 arg)
923 (setq arg (append (list (nth 0 cmd) (nth 1 cmd) (nth 2 cmd))
924 (funcall func (nth 3 cmd) nil)))
925 (ccl-compile-multiple-map-function 'map-multiple arg))
926 nil)
927
928 (defun ccl-compile-map-single (cmd)
929 (if (/= (length cmd) 4)
930 (error "CCL: Invalid number of arguments: %s" cmd))
931 (let ((RRR (nth 1 cmd))
932 (rrr (nth 2 cmd))
933 (map (nth 3 cmd))
934 id)
935 (ccl-check-register rrr cmd)
936 (ccl-check-register RRR cmd)
937 (ccl-embed-extended-command 'map-single rrr RRR 0)
938 (cond ((symbolp map)
939 (if (get map 'code-conversion-map)
940 (ccl-embed-data map)
941 (error "CCL: Invalid map: %s" map)))
942 (t
943 (error "CCL: Invalid type of arguments: %s" cmd))))
944 nil)
945
946 (defun ccl-compile-multiple-map-function (command cmd)
947 (if (< (length cmd) 4)
948 (error "CCL: Invalid number of arguments: %s" cmd))
949 (let ((RRR (nth 1 cmd))
950 (rrr (nth 2 cmd))
951 (args (nthcdr 3 cmd))
952 map)
953 (ccl-check-register rrr cmd)
954 (ccl-check-register RRR cmd)
955 (ccl-embed-extended-command command rrr RRR 0)
956 (ccl-embed-data (length args))
957 (while args
958 (setq map (car args))
959 (cond ((symbolp map)
960 (if (get map 'code-conversion-map)
961 (ccl-embed-data map)
962 (error "CCL: Invalid map: %s" map)))
963 ((numberp map)
964 (ccl-embed-data map))
965 (t
966 (error "CCL: Invalid type of arguments: %s" cmd)))
967 (setq args (cdr args)))))
968
969 \f
970 ;;; CCL dump staffs
971
972 ;; To avoid byte-compiler warning.
973 (defvar ccl-code)
974
975 ;;;###autoload
976 (defun ccl-dump (ccl-code)
977 "Disassemble compiled CCL-CODE."
978 (let ((len (length ccl-code))
979 (buffer-mag (aref ccl-code 0)))
980 (cond ((= buffer-mag 0)
981 (insert "Don't output anything.\n"))
982 ((= buffer-mag 1)
983 (insert "Out-buffer must be as large as in-buffer.\n"))
984 (t
985 (insert
986 (format "Out-buffer must be %d times bigger than in-buffer.\n"
987 buffer-mag))))
988 (insert "Main-body:\n")
989 (setq ccl-current-ic 2)
990 (if (> (aref ccl-code 1) 0)
991 (progn
992 (while (< ccl-current-ic (aref ccl-code 1))
993 (ccl-dump-1))
994 (insert "At EOF:\n")))
995 (while (< ccl-current-ic len)
996 (ccl-dump-1))
997 ))
998
999 ;; Return a CCL code in `ccl-code' at `ccl-current-ic'.
1000 (defun ccl-get-next-code ()
1001 (prog1
1002 (aref ccl-code ccl-current-ic)
1003 (setq ccl-current-ic (1+ ccl-current-ic))))
1004
1005 (defun ccl-dump-1 ()
1006 (let* ((code (ccl-get-next-code))
1007 (cmd (aref ccl-code-table (logand code 31)))
1008 (rrr (ash (logand code 255) -5))
1009 (cc (ash code -8)))
1010 (insert (format "%5d:[%s] " (1- ccl-current-ic) cmd))
1011 (funcall (get cmd 'ccl-dump-function) rrr cc)))
1012
1013 (defun ccl-dump-set-register (rrr cc)
1014 (insert (format "r%d = r%d\n" rrr cc)))
1015
1016 (defun ccl-dump-set-short-const (rrr cc)
1017 (insert (format "r%d = %d\n" rrr cc)))
1018
1019 (defun ccl-dump-set-const (rrr ignore)
1020 (insert (format "r%d = %d\n" rrr (ccl-get-next-code))))
1021
1022 (defun ccl-dump-set-array (rrr cc)
1023 (let ((rrr2 (logand cc 7))
1024 (len (ash cc -3))
1025 (i 0))
1026 (insert (format "r%d = array[r%d] of length %d\n\t"
1027 rrr rrr2 len))
1028 (while (< i len)
1029 (insert (format "%d " (ccl-get-next-code)))
1030 (setq i (1+ i)))
1031 (insert "\n")))
1032
1033 (defun ccl-dump-jump (ignore cc &optional address)
1034 (insert (format "jump to %d(" (+ (or address ccl-current-ic) cc)))
1035 (if (>= cc 0)
1036 (insert "+"))
1037 (insert (format "%d)\n" (1+ cc))))
1038
1039 (defun ccl-dump-jump-cond (rrr cc)
1040 (insert (format "if (r%d == 0), " rrr))
1041 (ccl-dump-jump nil cc))
1042
1043 (defun ccl-dump-write-register-jump (rrr cc)
1044 (insert (format "write r%d, " rrr))
1045 (ccl-dump-jump nil cc))
1046
1047 (defun ccl-dump-write-register-read-jump (rrr cc)
1048 (insert (format "write r%d, read r%d, " rrr rrr))
1049 (ccl-dump-jump nil cc)
1050 (ccl-get-next-code) ; Skip dummy READ-JUMP
1051 )
1052
1053 (defun ccl-extract-arith-op (cc)
1054 (aref ccl-arith-table (ash cc -6)))
1055
1056 (defun ccl-dump-write-expr-const (ignore cc)
1057 (insert (format "write (r%d %s %d)\n"
1058 (logand cc 7)
1059 (ccl-extract-arith-op cc)
1060 (ccl-get-next-code))))
1061
1062 (defun ccl-dump-write-expr-register (ignore cc)
1063 (insert (format "write (r%d %s r%d)\n"
1064 (logand cc 7)
1065 (ccl-extract-arith-op cc)
1066 (logand (ash cc -3) 7))))
1067
1068 (defun ccl-dump-insert-char (cc)
1069 (cond ((= cc ?\t) (insert " \"^I\""))
1070 ((= cc ?\n) (insert " \"^J\""))
1071 (t (insert (format " \"%c\"" cc)))))
1072
1073 (defun ccl-dump-write-const-jump (ignore cc)
1074 (let ((address ccl-current-ic))
1075 (insert "write char")
1076 (ccl-dump-insert-char (ccl-get-next-code))
1077 (insert ", ")
1078 (ccl-dump-jump nil cc address)))
1079
1080 (defun ccl-dump-write-const-read-jump (rrr cc)
1081 (let ((address ccl-current-ic))
1082 (insert "write char")
1083 (ccl-dump-insert-char (ccl-get-next-code))
1084 (insert (format ", read r%d, " rrr))
1085 (ccl-dump-jump cc address)
1086 (ccl-get-next-code) ; Skip dummy READ-JUMP
1087 ))
1088
1089 (defun ccl-dump-write-string-jump (ignore cc)
1090 (let ((address ccl-current-ic)
1091 (len (ccl-get-next-code))
1092 (i 0))
1093 (insert "write \"")
1094 (while (< i len)
1095 (let ((code (ccl-get-next-code)))
1096 (insert (ash code -16))
1097 (if (< (1+ i) len) (insert (logand (ash code -8) 255)))
1098 (if (< (+ i 2) len) (insert (logand code 255))))
1099 (setq i (+ i 3)))
1100 (insert "\", ")
1101 (ccl-dump-jump nil cc address)))
1102
1103 (defun ccl-dump-write-array-read-jump (rrr cc)
1104 (let ((address ccl-current-ic)
1105 (len (ccl-get-next-code))
1106 (i 0))
1107 (insert (format "write array[r%d] of length %d,\n\t" rrr len))
1108 (while (< i len)
1109 (ccl-dump-insert-char (ccl-get-next-code))
1110 (setq i (1+ i)))
1111 (insert (format "\n\tthen read r%d, " rrr))
1112 (ccl-dump-jump nil cc address)
1113 (ccl-get-next-code) ; Skip dummy READ-JUMP.
1114 ))
1115
1116 (defun ccl-dump-read-jump (rrr cc)
1117 (insert (format "read r%d, " rrr))
1118 (ccl-dump-jump nil cc))
1119
1120 (defun ccl-dump-branch (rrr len)
1121 (let ((jump-table-head ccl-current-ic)
1122 (i 0))
1123 (insert (format "jump to array[r%d] of length %d\n\t" rrr len))
1124 (while (<= i len)
1125 (insert (format "%d " (+ jump-table-head (ccl-get-next-code))))
1126 (setq i (1+ i)))
1127 (insert "\n")))
1128
1129 (defun ccl-dump-read-register (rrr cc)
1130 (insert (format "read r%d (%d remaining)\n" rrr cc)))
1131
1132 (defun ccl-dump-read-branch (rrr len)
1133 (insert (format "read r%d, " rrr))
1134 (ccl-dump-branch rrr len))
1135
1136 (defun ccl-dump-write-register (rrr cc)
1137 (insert (format "write r%d (%d remaining)\n" rrr cc)))
1138
1139 (defun ccl-dump-call (ignore cc)
1140 (insert (format "call subroutine #%d\n" cc)))
1141
1142 (defun ccl-dump-write-const-string (rrr cc)
1143 (if (= rrr 0)
1144 (progn
1145 (insert "write char")
1146 (ccl-dump-insert-char cc)
1147 (newline))
1148 (let ((len cc)
1149 (i 0))
1150 (insert "write \"")
1151 (while (< i len)
1152 (let ((code (ccl-get-next-code)))
1153 (insert (format "%c" (lsh code -16)))
1154 (if (< (1+ i) len)
1155 (insert (format "%c" (logand (lsh code -8) 255))))
1156 (if (< (+ i 2) len)
1157 (insert (format "%c" (logand code 255))))
1158 (setq i (+ i 3))))
1159 (insert "\"\n"))))
1160
1161 (defun ccl-dump-write-array (rrr cc)
1162 (let ((i 0))
1163 (insert (format "write array[r%d] of length %d\n\t" rrr cc))
1164 (while (< i cc)
1165 (ccl-dump-insert-char (ccl-get-next-code))
1166 (setq i (1+ i)))
1167 (insert "\n")))
1168
1169 (defun ccl-dump-end (&rest ignore)
1170 (insert "end\n"))
1171
1172 (defun ccl-dump-set-assign-expr-const (rrr cc)
1173 (insert (format "r%d %s= %d\n"
1174 rrr
1175 (ccl-extract-arith-op cc)
1176 (ccl-get-next-code))))
1177
1178 (defun ccl-dump-set-assign-expr-register (rrr cc)
1179 (insert (format "r%d %s= r%d\n"
1180 rrr
1181 (ccl-extract-arith-op cc)
1182 (logand cc 7))))
1183
1184 (defun ccl-dump-set-expr-const (rrr cc)
1185 (insert (format "r%d = r%d %s %d\n"
1186 rrr
1187 (logand cc 7)
1188 (ccl-extract-arith-op cc)
1189 (ccl-get-next-code))))
1190
1191 (defun ccl-dump-set-expr-register (rrr cc)
1192 (insert (format "r%d = r%d %s r%d\n"
1193 rrr
1194 (logand cc 7)
1195 (ccl-extract-arith-op cc)
1196 (logand (ash cc -3) 7))))
1197
1198 (defun ccl-dump-jump-cond-expr-const (rrr cc)
1199 (let ((address ccl-current-ic))
1200 (insert (format "if !(r%d %s %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-jump-cond-expr-register (rrr cc)
1207 (let ((address ccl-current-ic))
1208 (insert (format "if !(r%d %s r%d), "
1209 rrr
1210 (aref ccl-arith-table (ccl-get-next-code))
1211 (ccl-get-next-code)))
1212 (ccl-dump-jump nil cc address)))
1213
1214 (defun ccl-dump-read-jump-cond-expr-const (rrr cc)
1215 (insert (format "read r%d, " rrr))
1216 (ccl-dump-jump-cond-expr-const rrr cc))
1217
1218 (defun ccl-dump-read-jump-cond-expr-register (rrr cc)
1219 (insert (format "read r%d, " rrr))
1220 (ccl-dump-jump-cond-expr-register rrr cc))
1221
1222 (defun ccl-dump-binary (ccl-code)
1223 (let ((len (length ccl-code))
1224 (i 2))
1225 (while (< i len)
1226 (let ((code (aref ccl-code i))
1227 (j 27))
1228 (while (>= j 0)
1229 (insert (if (= (logand code (ash 1 j)) 0) ?0 ?1))
1230 (setq j (1- j)))
1231 (setq code (logand code 31))
1232 (if (< code (length ccl-code-table))
1233 (insert (format ":%s" (aref ccl-code-table code))))
1234 (insert "\n"))
1235 (setq i (1+ i)))))
1236
1237 (defun ccl-dump-ex-cmd (rrr cc)
1238 (let* ((RRR (logand cc ?\x7))
1239 (Rrr (logand (ash cc -3) ?\x7))
1240 (ex-op (aref ccl-extended-code-table (logand (ash cc -6) ?\x3fff))))
1241 (insert (format "<%s> " ex-op))
1242 (funcall (get ex-op 'ccl-dump-function) rrr RRR Rrr)))
1243
1244 (defun ccl-dump-read-multibyte-character (rrr RRR Rrr)
1245 (insert (format "read-multibyte-character r%d r%d\n" RRR rrr)))
1246
1247 (defun ccl-dump-write-multibyte-character (rrr RRR Rrr)
1248 (insert (format "write-multibyte-character r%d r%d\n" RRR rrr)))
1249
1250 (defun ccl-dump-translate-character (rrr RRR Rrr)
1251 (insert (format "translation table(r%d) r%d r%d\n" Rrr RRR rrr)))
1252
1253 (defun ccl-dump-translate-character-const-tbl (rrr RRR Rrr)
1254 (let ((tbl (ccl-get-next-code)))
1255 (insert (format "translation table(%S) r%d r%d\n" tbl RRR rrr))))
1256
1257 (defun ccl-dump-iterate-multiple-map (rrr RRR Rrr)
1258 (let ((notbl (ccl-get-next-code))
1259 (i 0) id)
1260 (insert (format "iterate-multiple-map r%d r%d\n" RRR rrr))
1261 (insert (format "\tnumber of maps is %d .\n\t [" notbl))
1262 (while (< i notbl)
1263 (setq id (ccl-get-next-code))
1264 (insert (format "%S" id))
1265 (setq i (1+ i)))
1266 (insert "]\n")))
1267
1268 (defun ccl-dump-map-multiple (rrr RRR Rrr)
1269 (let ((notbl (ccl-get-next-code))
1270 (i 0) id)
1271 (insert (format "map-multiple r%d r%d\n" RRR rrr))
1272 (insert (format "\tnumber of maps and separators is %d\n\t [" notbl))
1273 (while (< i notbl)
1274 (setq id (ccl-get-next-code))
1275 (if (= id -1)
1276 (insert "]\n\t [")
1277 (insert (format "%S " id)))
1278 (setq i (1+ i)))
1279 (insert "]\n")))
1280
1281 (defun ccl-dump-map-single (rrr RRR Rrr)
1282 (let ((id (ccl-get-next-code)))
1283 (insert (format "map-single r%d r%d map(%S)\n" RRR rrr id))))
1284
1285 \f
1286 ;; CCL emulation staffs
1287
1288 ;; Not yet implemented.
1289 \f
1290 ;; Auto-loaded functions.
1291
1292 ;;;###autoload
1293 (defmacro declare-ccl-program (name &optional vector)
1294 "Declare NAME as a name of CCL program.
1295
1296 To compile a CCL program which calls another CCL program not yet
1297 defined, it must be declared as a CCL program in advance.
1298 Optional arg VECTOR is a compiled CCL code of the CCL program."
1299 `(put ',name 'ccl-program-idx (register-ccl-program ',name ,vector)))
1300
1301 ;;;###autoload
1302 (defmacro define-ccl-program (name ccl-program &optional doc)
1303 "Set NAME the compiled code of CCL-PROGRAM.
1304 CCL-PROGRAM is `eval'ed before being handed to the CCL compiler `ccl-compile'.
1305 The compiled code is a vector of integers."
1306 `(let ((prog ,(ccl-compile (eval ccl-program))))
1307 (defconst ,name prog ,doc)
1308 (put ',name 'ccl-program-idx (register-ccl-program ',name prog))
1309 nil))
1310
1311 ;;;###autoload
1312 (defmacro check-ccl-program (ccl-program &optional name)
1313 "Check validity of CCL-PROGRAM.
1314 If CCL-PROGRAM is a symbol denoting a valid CCL program, return
1315 CCL-PROGRAM, else return nil.
1316 If CCL-PROGRAM is a vector and optional arg NAME (symbol) is supplied,
1317 register CCL-PROGRAM by name NAME, and return NAME."
1318 `(let ((result ,ccl-program))
1319 (cond ((symbolp ,ccl-program)
1320 (or (numberp (get ,ccl-program 'ccl-program-idx))
1321 (setq result nil)))
1322 ((vectorp ,ccl-program)
1323 (setq result ,name)
1324 (register-ccl-program result ,ccl-program))
1325 (t
1326 (setq result nil)))
1327 result))
1328
1329 ;;;###autoload
1330 (defun ccl-execute-with-args (ccl-prog &rest args)
1331 "Execute CCL-PROGRAM with registers initialized by the remaining args.
1332 The return value is a vector of resulting CCL registers."
1333 (let ((reg (make-vector 8 0))
1334 (i 0))
1335 (while (and args (< i 8))
1336 (if (not (integerp (car args)))
1337 (error "Arguments should be integer"))
1338 (aset reg i (car args))
1339 (setq args (cdr args) i (1+ i)))
1340 (ccl-execute ccl-prog reg)
1341 reg))
1342
1343 (provide 'ccl)
1344
1345 ;; ccl.el ends here