comparison lisp/mule/ccl.el @ 5118:e0db3c197671 ben-lisp-object

merge up to latest default branch, doesn't compile yet
author Ben Wing <ben@xemacs.org>
date Sat, 26 Dec 2009 21:18:49 -0600
parents 476d0799d704
children f00192e1cd49 308d34e9f07d
comparison
equal deleted inserted replaced
5117:3742ea8250b5 5118:e0db3c197671
1 ;;; ccl.el --- CCL (Code Conversion Language) compiler -*- coding: iso-2022-7bit; -*-
2
3 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
5 ;; Copyright (C) 2002, 2007 Free Software Foundation, Inc.
6
7 ;; Keywords: CCL, mule, multilingual, character set, coding-system
8
9 ;; This file is part of XEmacs.
10
11 ;; XEmacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; XEmacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with XEmacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;; Synched up with: FSF 21.0.90
27
28 ;;; Commentary:
29
30 ;; CCL (Code Conversion Language) is a simple programming language to
31 ;; be used for various kind of code conversion. CCL program is
32 ;; compiled to CCL code (vector of integers) and executed by CCL
33 ;; interpreter of Emacs.
34 ;;
35 ;; CCL is used for code conversion at process I/O and file I/O for
36 ;; non-standard coding-system. In addition, it is used for
37 ;; calculating a code point of X's font from a character code.
38 ;; However, since CCL is designed as a powerful programming language,
39 ;; it can be used for more generic calculation. For instance,
40 ;; combination of three or more arithmetic operations can be
41 ;; calculated faster than Emacs Lisp.
42 ;;
43 ;; Syntax and semantics of CCL program is described in the
44 ;; documentation of `define-ccl-program'.
45
46 ;;; Code:
47
48 (defconst ccl-command-table
49 [if branch loop break repeat write-repeat write-read-repeat
50 read read-if read-branch write call end
51 read-multibyte-character write-multibyte-character
52 translate-character mule-to-unicode unicode-to-mule
53 iterate-multiple-map map-multiple map-single lookup-integer
54 lookup-character]
55 "Vector of CCL commands (symbols).")
56
57 ;; Put a property to each symbol of CCL commands for the compiler.
58 (let (op (i 0) (len (length ccl-command-table)))
59 (while (< i len)
60 (setq op (aref ccl-command-table i))
61 (put op 'ccl-compile-function (intern (format "ccl-compile-%s" op)))
62 (setq i (1+ i))))
63
64 (defconst ccl-code-table
65 [set-register
66 set-short-const
67 set-const
68 set-array
69 jump
70 jump-cond
71 write-register-jump
72 write-register-read-jump
73 write-const-jump
74 write-const-read-jump
75 write-string-jump
76 write-array-read-jump
77 read-jump
78 branch
79 read-register
80 write-expr-const
81 read-branch
82 write-register
83 write-expr-register
84 call
85 write-const-string
86 write-array
87 end
88 set-assign-expr-const
89 set-assign-expr-register
90 set-expr-const
91 set-expr-register
92 jump-cond-expr-const
93 jump-cond-expr-register
94 read-jump-cond-expr-const
95 read-jump-cond-expr-register
96 ex-cmd
97 ]
98 "Vector of CCL compiled codes (symbols).")
99
100 (defconst ccl-extended-code-table
101 [read-multibyte-character
102 write-multibyte-character
103 translate-character
104 translate-character-const-tbl
105 mule-to-unicode
106 unicode-to-mule
107 nil nil nil nil nil nil nil nil nil nil ; 0x06-0x0f
108 iterate-multiple-map
109 map-multiple
110 map-single
111 lookup-int-const-tbl
112 lookup-char-const-tbl
113 ]
114 "Vector of CCL extended compiled codes (symbols).")
115
116 ;; Put a property to each symbol of CCL codes for the disassembler.
117 (let (code (i 0) (len (length ccl-code-table)))
118 (while (< i len)
119 (setq code (aref ccl-code-table i))
120 (put code 'ccl-code i)
121 (put code 'ccl-dump-function (intern (format "ccl-dump-%s" code)))
122 (setq i (1+ i))))
123
124 (let (code (i 0) (len (length ccl-extended-code-table)))
125 (while (< i len)
126 (setq code (aref ccl-extended-code-table i))
127 (if code
128 (progn
129 (put code 'ccl-ex-code i)
130 (put code 'ccl-dump-function (intern (format "ccl-dump-%s" code)))))
131 (setq i (1+ i))))
132
133 (defconst ccl-jump-code-list
134 '(jump jump-cond write-register-jump write-register-read-jump
135 write-const-jump write-const-read-jump write-string-jump
136 write-array-read-jump read-jump))
137
138 ;; Put a property `jump-flag' to each CCL code which execute jump in
139 ;; some way.
140 (let ((l ccl-jump-code-list))
141 (while l
142 (put (car l) 'jump-flag t)
143 (setq l (cdr l))))
144
145 (defconst ccl-register-table
146 [r0 r1 r2 r3 r4 r5 r6 r7]
147 "Vector of CCL registers (symbols).")
148
149 ;; Put a property to indicate register number to each symbol of CCL.
150 ;; registers.
151 (let (reg (i 0) (len (length ccl-register-table)))
152 (while (< i len)
153 (setq reg (aref ccl-register-table i))
154 (put reg 'ccl-register-number i)
155 (setq i (1+ i))))
156
157 (defconst ccl-arith-table
158 [+ - * / % & | ^ << >> <8 >8 // nil nil nil
159 < > == <= >= != de-sjis en-sjis]
160 "Vector of CCL arithmetic/logical operators (symbols).")
161
162 ;; Put a property to each symbol of CCL operators for the compiler.
163 (let (arith (i 0) (len (length ccl-arith-table)))
164 (while (< i len)
165 (setq arith (aref ccl-arith-table i))
166 (if arith (put arith 'ccl-arith-code i))
167 (setq i (1+ i))))
168
169 (defconst ccl-assign-arith-table
170 [+= -= *= /= %= &= |= ^= <<= >>= <8= >8= //=]
171 "Vector of CCL assignment operators (symbols).")
172
173 ;; Put a property to each symbol of CCL assignment operators for the compiler.
174 (let (arith (i 0) (len (length ccl-assign-arith-table)))
175 (while (< i len)
176 (setq arith (aref ccl-assign-arith-table i))
177 (put arith 'ccl-self-arith-code i)
178 (setq i (1+ i))))
179
180 (defvar ccl-program-vector nil
181 "Working vector of CCL codes produced by CCL compiler.")
182 (defvar ccl-current-ic 0
183 "The current index for `ccl-program-vector'.")
184
185 ;; Embed integer DATA in `ccl-program-vector' at `ccl-current-ic' and
186 ;; increment it. If IC is specified, embed DATA at IC.
187 (defun ccl-embed-data (data &optional ic)
188 ;; XEmacs: Embed characters as characters, since their integer values vary at
189 ;; runtime.
190 ; (if (characterp data)
191 ; (setq data (char-int data)))
192 (if ic
193 (aset ccl-program-vector ic data)
194 (let ((len (length ccl-program-vector)))
195 (if (>= ccl-current-ic len)
196 (let ((new (make-vector (* len 2) nil)))
197 (while (> len 0)
198 (setq len (1- len))
199 (aset new len (aref ccl-program-vector len)))
200 (setq ccl-program-vector new))))
201 (aset ccl-program-vector ccl-current-ic data)
202 (setq ccl-current-ic (1+ ccl-current-ic))))
203
204 ;; Embed pair of SYMBOL and PROP where (get SYMBOL PROP) should give
205 ;; proper index number for SYMBOL. PROP should be
206 ;; `translation-table-id', `translation-hash-table-id'
207 ;; `code-conversion-map-id', or `ccl-program-idx'.
208 (defun ccl-embed-symbol (symbol prop)
209 (ccl-embed-data (cons symbol prop)))
210
211 ;; Embed string STR of length LEN in `ccl-program-vector' at
212 ;; `ccl-current-ic'.
213 (defun ccl-embed-string (len str)
214 (let ((i 0))
215 (while (< i len)
216 (ccl-embed-data (logior (ash (aref str i) 16)
217 (if (< (1+ i) len)
218 (ash (aref str (1+ i)) 8)
219 0)
220 (if (< (+ i 2) len)
221 (aref str (+ i 2))
222 0)))
223 (setq i (+ i 3)))))
224
225 ;; Embed a relative jump address to `ccl-current-ic' in
226 ;; `ccl-program-vector' at IC without altering the other bit field.
227 (defun ccl-embed-current-address (ic)
228 (let ((relative (- ccl-current-ic (1+ ic))))
229 (aset ccl-program-vector ic
230 (logior (aref ccl-program-vector ic) (ash relative 8)))))
231
232 ;; Embed CCL code for the operation OP and arguments REG and DATA in
233 ;; `ccl-program-vector' at `ccl-current-ic' in the following format.
234 ;; |----------------- integer (28-bit) ------------------|
235 ;; |------------ 20-bit ------------|- 3-bit --|- 5-bit -|
236 ;; |------------- DATA -------------|-- REG ---|-- OP ---|
237 ;; If REG2 is specified, embed a code in the following format.
238 ;; |------- 17-bit ------|- 3-bit --|- 3-bit --|- 5-bit -|
239 ;; |-------- DATA -------|-- REG2 --|-- REG ---|-- OP ---|
240
241 ;; If REG is a CCL register symbol (e.g. r0, r1...), the register
242 ;; number is embedded. If OP is one of unconditional jumps, DATA is
243 ;; changed to an relative jump address.
244
245 (defun ccl-embed-code (op reg data &optional reg2)
246 (if (and (> data 0) (get op 'jump-flag))
247 ;; DATA is an absolute jump address. Make it relative to the
248 ;; next of jump code.
249 (setq data (- data (1+ ccl-current-ic))))
250 (let ((code (logior (get op 'ccl-code)
251 (ash
252 (if (symbolp reg) (get reg 'ccl-register-number) reg) 5)
253 (if reg2
254 (logior (ash (get reg2 'ccl-register-number) 8)
255 (ash data 11))
256 (ash data 8)))))
257 (ccl-embed-data code)))
258
259 ;; extended ccl command format
260 ;; |- 14-bit -|- 3-bit --|- 3-bit --|- 3-bit --|- 5-bit -|
261 ;; |- EX-OP --|-- REG3 --|-- REG2 --|-- REG ---|-- OP ---|
262 (defun ccl-embed-extended-command (ex-op reg reg2 reg3)
263 (let ((data (logior (ash (get ex-op 'ccl-ex-code) 3)
264 (if (symbolp reg3)
265 (get reg3 'ccl-register-number)
266 0))))
267 (ccl-embed-code 'ex-cmd reg data reg2)))
268
269 ;; Just advance `ccl-current-ic' by INC.
270 (defun ccl-increment-ic (inc)
271 (setq ccl-current-ic (+ ccl-current-ic inc)))
272
273 ;; If non-nil, index of the start of the current loop.
274 (defvar ccl-loop-head nil)
275 ;; If non-nil, list of absolute addresses of the breaking points of
276 ;; the current loop.
277 (defvar ccl-breaks nil)
278
279 ;;;###autoload
280 (defun ccl-compile (ccl-program)
281 "Return a compiled code of CCL-PROGRAM as a vector of integer."
282 (if (or (null (consp ccl-program))
283 (null (integerp (car ccl-program)))
284 (null (listp (car (cdr ccl-program)))))
285 (error "CCL: Invalid CCL program: %s" ccl-program))
286 (if (null (vectorp ccl-program-vector))
287 (setq ccl-program-vector (make-vector 8192 0)))
288 (setq ccl-loop-head nil ccl-breaks nil)
289 (setq ccl-current-ic 0)
290
291 ;; The first element is the buffer magnification.
292 (ccl-embed-data (car ccl-program))
293
294 ;; The second element is the address of the start CCL code for
295 ;; processing end of input buffer (we call it eof-processor). We
296 ;; set it later.
297 (ccl-increment-ic 1)
298
299 ;; Compile the main body of the CCL program.
300 (ccl-compile-1 (car (cdr ccl-program)))
301
302 ;; Embed the address of eof-processor.
303 (ccl-embed-data ccl-current-ic 1)
304
305 ;; Then compile eof-processor.
306 (if (nth 2 ccl-program)
307 (ccl-compile-1 (nth 2 ccl-program)))
308
309 ;; At last, embed termination code.
310 (ccl-embed-code 'end 0 0)
311
312 (let ((vec (make-vector ccl-current-ic 0))
313 (i 0))
314 (while (< i ccl-current-ic)
315 (aset vec i (aref ccl-program-vector i))
316 (setq i (1+ i)))
317 vec))
318
319 ;; Signal syntax error.
320 (defun ccl-syntax-error (cmd)
321 (error "CCL: Syntax error: %s" cmd))
322
323 ;; Check if ARG is a valid CCL register.
324 (defun ccl-check-register (arg cmd)
325 (if (get arg 'ccl-register-number)
326 arg
327 (error "CCL: Invalid register %s in %s." arg cmd)))
328
329 ;; Check if ARG is a valid CCL command.
330 (defun ccl-check-compile-function (arg cmd)
331 (or (get arg 'ccl-compile-function)
332 (error "CCL: Invalid command: %s" cmd)))
333
334 ;; In the following code, most ccl-compile-XXXX functions return t if
335 ;; they end with unconditional jump, else return nil.
336
337 ;; Compile CCL-BLOCK (see the syntax above).
338 (defun ccl-compile-1 (ccl-block)
339 (let (unconditional-jump
340 cmd)
341 (if (or (integer-or-char-p ccl-block)
342 (stringp ccl-block)
343 (and ccl-block (symbolp (car ccl-block))))
344 ;; This block consists of single statement.
345 (setq ccl-block (list ccl-block)))
346
347 ;; Now CCL-BLOCK is a list of statements. Compile them one by
348 ;; one.
349 (while ccl-block
350 (setq cmd (car ccl-block))
351 (setq unconditional-jump
352 (cond ((integer-or-char-p cmd)
353 ;; SET statement for the register 0.
354 (ccl-compile-set (list 'r0 '= cmd)))
355
356 ((stringp cmd)
357 ;; WRITE statement of string argument.
358 (ccl-compile-write-string cmd))
359
360 ((listp cmd)
361 ;; The other statements.
362 (cond ((eq (nth 1 cmd) '=)
363 ;; SET statement of the form `(REG = EXPRESSION)'.
364 (ccl-compile-set cmd))
365
366 ((and (symbolp (nth 1 cmd))
367 (get (nth 1 cmd) 'ccl-self-arith-code))
368 ;; SET statement with an assignment operation.
369 (ccl-compile-self-set cmd))
370
371 (t
372 (funcall (ccl-check-compile-function (car cmd) cmd)
373 cmd))))
374
375 (t
376 (ccl-syntax-error cmd))))
377 (setq ccl-block (cdr ccl-block)))
378 unconditional-jump))
379
380 (defconst ccl-max-short-const (ash 1 19))
381 (defconst ccl-min-short-const (ash -1 19))
382
383 ;; Compile SET statement.
384 (defun ccl-compile-set (cmd)
385 (let ((rrr (ccl-check-register (car cmd) cmd))
386 (right (nth 2 cmd)))
387 (cond ((listp right)
388 ;; CMD has the form `(RRR = (XXX OP YYY))'.
389 (ccl-compile-expression rrr right))
390
391 ((integer-or-char-p right)
392 ;; CMD has the form `(RRR = integer)'.
393 (if (and (<= right ccl-max-short-const)
394 (>= right ccl-min-short-const))
395 (ccl-embed-code 'set-short-const rrr right)
396 (ccl-embed-code 'set-const rrr 0)
397 (ccl-embed-data right)))
398
399 (t
400 ;; CMD has the form `(RRR = rrr [ array ])'.
401 (ccl-check-register right cmd)
402 (let ((ary (nth 3 cmd)))
403 (if (vectorp ary)
404 (let ((i 0) (len (length ary)))
405 (ccl-embed-code 'set-array rrr len right)
406 (while (< i len)
407 (ccl-embed-data (aref ary i))
408 (setq i (1+ i))))
409 (ccl-embed-code 'set-register rrr 0 right))))))
410 nil)
411
412 ;; Compile SET statement with ASSIGNMENT_OPERATOR.
413 (defun ccl-compile-self-set (cmd)
414 (let ((rrr (ccl-check-register (car cmd) cmd))
415 (right (nth 2 cmd)))
416 (if (listp right)
417 ;; CMD has the form `(RRR ASSIGN_OP (XXX OP YYY))', compile
418 ;; the right hand part as `(r7 = (XXX OP YYY))' (note: the
419 ;; register 7 can be used for storing temporary value).
420 (progn
421 (ccl-compile-expression 'r7 right)
422 (setq right 'r7)))
423 ;; Now CMD has the form `(RRR ASSIGN_OP ARG)'. Compile it as
424 ;; `(RRR = (RRR OP ARG))'.
425 (ccl-compile-expression
426 rrr
427 (list rrr (intern (substring (symbol-name (nth 1 cmd)) 0 -1)) right)))
428 nil)
429
430 ;; Compile SET statement of the form `(RRR = EXPR)'.
431 (defun ccl-compile-expression (rrr expr)
432 (let ((left (car expr))
433 (op (get (nth 1 expr) 'ccl-arith-code))
434 (right (nth 2 expr)))
435 (if (listp left)
436 (progn
437 ;; EXPR has the form `((EXPR2 OP2 ARG) OP RIGHT)'. Compile
438 ;; the first term as `(r7 = (EXPR2 OP2 ARG)).'
439 (ccl-compile-expression 'r7 left)
440 (setq left 'r7)))
441
442 ;; Now EXPR has the form (LEFT OP RIGHT).
443 (if (and (eq rrr left)
444 (< op (length ccl-assign-arith-table)))
445 ;; Compile this SET statement as `(RRR OP= RIGHT)'.
446 (if (integer-or-char-p right)
447 (progn
448 (ccl-embed-code 'set-assign-expr-const rrr (ash op 3) 'r0)
449 (ccl-embed-data right))
450 (ccl-check-register right expr)
451 (ccl-embed-code 'set-assign-expr-register rrr (ash op 3) right))
452
453 ;; Compile this SET statement as `(RRR = (LEFT OP RIGHT))'.
454 (if (integer-or-char-p right)
455 (progn
456 (ccl-embed-code 'set-expr-const rrr (ash op 3) left)
457 (ccl-embed-data right))
458 (ccl-check-register right expr)
459 (ccl-embed-code 'set-expr-register
460 rrr
461 (logior (ash op 3) (get right 'ccl-register-number))
462 left)))))
463
464 ;; Compile WRITE statement with string argument.
465 (defun ccl-compile-write-string (str)
466 (setq str (encode-coding-string str 'binary))
467 (let ((len (length str)))
468 (ccl-embed-code 'write-const-string 1 len)
469 (ccl-embed-string len str))
470 nil)
471
472 ;; Compile IF statement of the form `(if CONDITION TRUE-PART FALSE-PART)'.
473 ;; If READ-FLAG is non-nil, this statement has the form
474 ;; `(read-if (REG OPERATOR ARG) TRUE-PART FALSE-PART)'.
475 (defun ccl-compile-if (cmd &optional read-flag)
476 (if (and (/= (length cmd) 3) (/= (length cmd) 4))
477 (error "CCL: Invalid number of arguments: %s" cmd))
478 (let ((condition (nth 1 cmd))
479 (true-cmds (nth 2 cmd))
480 (false-cmds (nth 3 cmd))
481 jump-cond-address)
482 (if (and (listp condition)
483 (listp (car condition)))
484 ;; If CONDITION is a nested expression, the inner expression
485 ;; should be compiled at first as SET statement, i.e.:
486 ;; `(if ((X OP2 Y) OP Z) ...)' is compiled into two statements:
487 ;; `(r7 = (X OP2 Y)) (if (r7 OP Z) ...)'.
488 (progn
489 (ccl-compile-expression 'r7 (car condition))
490 (setq condition (cons 'r7 (cdr condition)))
491 (setq cmd (cons (car cmd)
492 (cons condition (cdr (cdr cmd)))))))
493
494 (setq jump-cond-address ccl-current-ic)
495 ;; Compile CONDITION.
496 (if (symbolp condition)
497 ;; CONDITION is a register.
498 (progn
499 (ccl-check-register condition cmd)
500 (ccl-embed-code 'jump-cond condition 0))
501 ;; CONDITION is a simple expression of the form (RRR OP ARG).
502 (let ((rrr (car condition))
503 (op (get (nth 1 condition) 'ccl-arith-code))
504 (arg (nth 2 condition)))
505 (ccl-check-register rrr cmd)
506 (if (integer-or-char-p arg)
507 (progn
508 (ccl-embed-code (if read-flag 'read-jump-cond-expr-const
509 'jump-cond-expr-const)
510 rrr 0)
511 (ccl-embed-data op)
512 (ccl-embed-data arg))
513 (ccl-check-register arg cmd)
514 (ccl-embed-code (if read-flag 'read-jump-cond-expr-register
515 'jump-cond-expr-register)
516 rrr 0)
517 (ccl-embed-data op)
518 (ccl-embed-data (get arg 'ccl-register-number)))))
519
520 ;; Compile TRUE-PART.
521 (let ((unconditional-jump (ccl-compile-1 true-cmds)))
522 (if (null false-cmds)
523 ;; This is the place to jump to if condition is false.
524 (progn
525 (ccl-embed-current-address jump-cond-address)
526 (setq unconditional-jump nil))
527 (let (end-true-part-address)
528 (if (not unconditional-jump)
529 (progn
530 ;; If TRUE-PART does not end with unconditional jump, we
531 ;; have to jump to the end of FALSE-PART from here.
532 (setq end-true-part-address ccl-current-ic)
533 (ccl-embed-code 'jump 0 0)))
534 ;; This is the place to jump to if CONDITION is false.
535 (ccl-embed-current-address jump-cond-address)
536 ;; Compile FALSE-PART.
537 (setq unconditional-jump
538 (and (ccl-compile-1 false-cmds) unconditional-jump))
539 (if end-true-part-address
540 ;; This is the place to jump to after the end of TRUE-PART.
541 (ccl-embed-current-address end-true-part-address))))
542 unconditional-jump)))
543
544 ;; Compile BRANCH statement.
545 (defun ccl-compile-branch (cmd)
546 (if (< (length cmd) 3)
547 (error "CCL: Invalid number of arguments: %s" cmd))
548 (ccl-compile-branch-blocks 'branch
549 (ccl-compile-branch-expression (nth 1 cmd) cmd)
550 (cdr (cdr cmd))))
551
552 ;; Compile READ statement of the form `(read-branch EXPR BLOCK0 BLOCK1 ...)'.
553 (defun ccl-compile-read-branch (cmd)
554 (if (< (length cmd) 3)
555 (error "CCL: Invalid number of arguments: %s" cmd))
556 (ccl-compile-branch-blocks 'read-branch
557 (ccl-compile-branch-expression (nth 1 cmd) cmd)
558 (cdr (cdr cmd))))
559
560 ;; Compile EXPRESSION part of BRANCH statement and return register
561 ;; which holds a value of the expression.
562 (defun ccl-compile-branch-expression (expr cmd)
563 (if (listp expr)
564 ;; EXPR has the form `(EXPR2 OP ARG)'. Compile it as SET
565 ;; statement of the form `(r7 = (EXPR2 OP ARG))'.
566 (progn
567 (ccl-compile-expression 'r7 expr)
568 'r7)
569 (ccl-check-register expr cmd)))
570
571 ;; Compile BLOCKs of BRANCH statement. CODE is 'branch or 'read-branch.
572 ;; REG is a register which holds a value of EXPRESSION part. BLOCKs
573 ;; is a list of CCL-BLOCKs.
574 (defun ccl-compile-branch-blocks (code rrr blocks)
575 (let ((branches (length blocks))
576 branch-idx
577 jump-table-head-address
578 empty-block-indexes
579 block-tail-addresses
580 block-unconditional-jump)
581 (ccl-embed-code code rrr branches)
582 (setq jump-table-head-address ccl-current-ic)
583 ;; The size of jump table is the number of blocks plus 1 (for the
584 ;; case RRR is out of range).
585 (ccl-increment-ic (1+ branches))
586 (setq empty-block-indexes (list branches))
587 ;; Compile each block.
588 (setq branch-idx 0)
589 (while blocks
590 (if (null (car blocks))
591 ;; This block is empty.
592 (setq empty-block-indexes (cons branch-idx empty-block-indexes)
593 block-unconditional-jump t)
594 ;; This block is not empty.
595 (ccl-embed-data (- ccl-current-ic jump-table-head-address)
596 (+ jump-table-head-address branch-idx))
597 (setq block-unconditional-jump (ccl-compile-1 (car blocks)))
598 (if (not block-unconditional-jump)
599 (progn
600 ;; Jump address of the end of branches are embedded later.
601 ;; For the moment, just remember where to embed them.
602 (setq block-tail-addresses
603 (cons ccl-current-ic block-tail-addresses))
604 (ccl-embed-code 'jump 0 0))))
605 (setq branch-idx (1+ branch-idx))
606 (setq blocks (cdr blocks)))
607 (if (not block-unconditional-jump)
608 ;; We don't need jump code at the end of the last block.
609 (setq block-tail-addresses (cdr block-tail-addresses)
610 ccl-current-ic (1- ccl-current-ic)))
611 ;; Embed jump address at the tailing jump commands of blocks.
612 (while block-tail-addresses
613 (ccl-embed-current-address (car block-tail-addresses))
614 (setq block-tail-addresses (cdr block-tail-addresses)))
615 ;; For empty blocks, make entries in the jump table point directly here.
616 (while empty-block-indexes
617 (ccl-embed-data (- ccl-current-ic jump-table-head-address)
618 (+ jump-table-head-address (car empty-block-indexes)))
619 (setq empty-block-indexes (cdr empty-block-indexes))))
620 ;; Branch command ends by unconditional jump if RRR is out of range.
621 nil)
622
623 ;; Compile LOOP statement.
624 (defun ccl-compile-loop (cmd)
625 (if (< (length cmd) 2)
626 (error "CCL: Invalid number of arguments: %s" cmd))
627 (let* ((ccl-loop-head ccl-current-ic)
628 (ccl-breaks nil)
629 unconditional-jump)
630 (setq cmd (cdr cmd))
631 (if cmd
632 (progn
633 (setq unconditional-jump t)
634 (while cmd
635 (setq unconditional-jump
636 (and (ccl-compile-1 (car cmd)) unconditional-jump))
637 (setq cmd (cdr cmd)))
638 (if (not ccl-breaks)
639 unconditional-jump
640 ;; Embed jump address for break statements encountered in
641 ;; this loop.
642 (while ccl-breaks
643 (ccl-embed-current-address (car ccl-breaks))
644 (setq ccl-breaks (cdr ccl-breaks))))
645 nil))))
646
647 ;; Compile BREAK statement.
648 (defun ccl-compile-break (cmd)
649 (if (/= (length cmd) 1)
650 (error "CCL: Invalid number of arguments: %s" cmd))
651 (if (null ccl-loop-head)
652 (error "CCL: No outer loop: %s" cmd))
653 (setq ccl-breaks (cons ccl-current-ic ccl-breaks))
654 (ccl-embed-code 'jump 0 0)
655 t)
656
657 ;; Compile REPEAT statement.
658 (defun ccl-compile-repeat (cmd)
659 (if (/= (length cmd) 1)
660 (error "CCL: Invalid number of arguments: %s" cmd))
661 (if (null ccl-loop-head)
662 (error "CCL: No outer loop: %s" cmd))
663 (ccl-embed-code 'jump 0 ccl-loop-head)
664 t)
665
666 ;; Compile WRITE-REPEAT statement.
667 (defun ccl-compile-write-repeat (cmd)
668 (if (/= (length cmd) 2)
669 (error "CCL: Invalid number of arguments: %s" cmd))
670 (if (null ccl-loop-head)
671 (error "CCL: No outer loop: %s" cmd))
672 (let ((arg (nth 1 cmd)))
673 (cond ((integer-or-char-p arg)
674 (ccl-embed-code 'write-const-jump 0 ccl-loop-head)
675 (ccl-embed-data arg))
676 ((stringp arg)
677 (setq arg (encode-coding-string arg 'binary))
678 (let ((len (length arg)))
679 (ccl-embed-code 'write-string-jump 0 ccl-loop-head)
680 (ccl-embed-data len)
681 (ccl-embed-string len arg)))
682 (t
683 (ccl-check-register arg cmd)
684 (ccl-embed-code 'write-register-jump arg ccl-loop-head))))
685 t)
686
687 ;; Compile WRITE-READ-REPEAT statement.
688 (defun ccl-compile-write-read-repeat (cmd)
689 (if (or (< (length cmd) 2) (> (length cmd) 3))
690 (error "CCL: Invalid number of arguments: %s" cmd))
691 (if (null ccl-loop-head)
692 (error "CCL: No outer loop: %s" cmd))
693 (let ((rrr (ccl-check-register (nth 1 cmd) cmd))
694 (arg (nth 2 cmd)))
695 (cond ((null arg)
696 (ccl-embed-code 'write-register-read-jump rrr ccl-loop-head))
697 ((integer-or-char-p arg)
698 (ccl-embed-code 'write-const-read-jump rrr arg ccl-loop-head))
699 ((vectorp arg)
700 (let ((len (length arg))
701 (i 0))
702 (ccl-embed-code 'write-array-read-jump rrr ccl-loop-head)
703 (ccl-embed-data len)
704 (while (< i len)
705 (ccl-embed-data (aref arg i))
706 (setq i (1+ i)))))
707 (t
708 (error "CCL: Invalid argument %s: %s" arg cmd)))
709 (ccl-embed-code 'read-jump rrr ccl-loop-head))
710 t)
711
712 ;; Compile READ statement.
713 (defun ccl-compile-read (cmd)
714 (if (< (length cmd) 2)
715 (error "CCL: Invalid number of arguments: %s" cmd))
716 (let* ((args (cdr cmd))
717 (i (1- (length args))))
718 (while args
719 (let ((rrr (ccl-check-register (car args) cmd)))
720 (ccl-embed-code 'read-register rrr i)
721 (setq args (cdr args) i (1- i)))))
722 nil)
723
724 ;; Compile READ-IF statement.
725 (defun ccl-compile-read-if (cmd)
726 (ccl-compile-if cmd 'read))
727
728 ;; Compile WRITE statement.
729 (defun ccl-compile-write (cmd)
730 (if (< (length cmd) 2)
731 (error "CCL: Invalid number of arguments: %s" cmd))
732 (let ((rrr (nth 1 cmd)))
733 (cond ((integer-or-char-p rrr)
734 (ccl-embed-code 'write-const-string 0 rrr))
735 ((stringp rrr)
736 (ccl-compile-write-string rrr))
737 ((and (symbolp rrr) (vectorp (nth 2 cmd)))
738 (ccl-check-register rrr cmd)
739 ;; CMD has the form `(write REG ARRAY)'.
740 (let* ((arg (nth 2 cmd))
741 (len (length arg))
742 (i 0))
743 (ccl-embed-code 'write-array rrr len)
744 (while (< i len)
745 (if (not (integer-or-char-p (aref arg i)))
746 (error "CCL: Invalid argument %s: %s" arg cmd))
747 (ccl-embed-data (aref arg i))
748 (setq i (1+ i)))))
749
750 ((symbolp rrr)
751 ;; CMD has the form `(write REG ...)'.
752 (let* ((args (cdr cmd))
753 (i (1- (length args))))
754 (while args
755 (setq rrr (ccl-check-register (car args) cmd))
756 (ccl-embed-code 'write-register rrr i)
757 (setq args (cdr args) i (1- i)))))
758
759 ((listp rrr)
760 ;; CMD has the form `(write (LEFT OP RIGHT))'.
761 (let ((left (car rrr))
762 (op (get (nth 1 rrr) 'ccl-arith-code))
763 (right (nth 2 rrr)))
764 (if (listp left)
765 (progn
766 ;; RRR has the form `((EXPR OP2 ARG) OP RIGHT)'.
767 ;; Compile the first term as `(r7 = (EXPR OP2 ARG))'.
768 (ccl-compile-expression 'r7 left)
769 (setq left 'r7)))
770 ;; Now RRR has the form `(ARG OP RIGHT)'.
771 (if (integer-or-char-p right)
772 (progn
773 (ccl-embed-code 'write-expr-const 0 (ash op 3) left)
774 (ccl-embed-data right))
775 (ccl-check-register right rrr)
776 (ccl-embed-code 'write-expr-register 0
777 (logior (ash op 3)
778 (get right 'ccl-register-number))))))
779
780 (t
781 (error "CCL: Invalid argument: %s" cmd))))
782 nil)
783
784 ;; Compile CALL statement.
785 (defun ccl-compile-call (cmd)
786 (if (/= (length cmd) 2)
787 (error "CCL: Invalid number of arguments: %s" cmd))
788 (if (not (symbolp (nth 1 cmd)))
789 (error "CCL: Subroutine should be a symbol: %s" cmd))
790 (ccl-embed-code 'call 1 0)
791 (ccl-embed-symbol (nth 1 cmd) 'ccl-program-idx)
792 nil)
793
794 ;; Compile END statement.
795 (defun ccl-compile-end (cmd)
796 (if (/= (length cmd) 1)
797 (error "CCL: Invalid number of arguments: %s" cmd))
798 (ccl-embed-code 'end 0 0)
799 t)
800
801 ;; Compile read-multibyte-character
802 (defun ccl-compile-read-multibyte-character (cmd)
803 (if (/= (length cmd) 3)
804 (error "CCL: Invalid number of arguments: %s" cmd))
805 (let ((RRR (nth 1 cmd))
806 (rrr (nth 2 cmd)))
807 (ccl-check-register rrr cmd)
808 (ccl-check-register RRR cmd)
809 (ccl-embed-extended-command 'read-multibyte-character rrr RRR 0))
810 nil)
811
812 ;; Compile write-multibyte-character
813 (defun ccl-compile-write-multibyte-character (cmd)
814 (if (/= (length cmd) 3)
815 (error "CCL: Invalid number of arguments: %s" cmd))
816 (let ((RRR (nth 1 cmd))
817 (rrr (nth 2 cmd)))
818 (ccl-check-register rrr cmd)
819 (ccl-check-register RRR cmd)
820 (ccl-embed-extended-command 'write-multibyte-character rrr RRR 0))
821 nil)
822
823 ;; Compile translate-character
824 (defun ccl-compile-translate-character (cmd)
825 (if (/= (length cmd) 4)
826 (error "CCL: Invalid number of arguments: %s" cmd))
827 (let ((Rrr (nth 1 cmd))
828 (RRR (nth 2 cmd))
829 (rrr (nth 3 cmd)))
830 (ccl-check-register rrr cmd)
831 (ccl-check-register RRR cmd)
832 (cond ((and (symbolp Rrr) (not (get Rrr 'ccl-register-number)))
833 (ccl-embed-extended-command 'translate-character-const-tbl
834 rrr RRR 0)
835 (ccl-embed-symbol Rrr 'translation-table-id))
836 (t
837 (ccl-check-register Rrr cmd)
838 (ccl-embed-extended-command 'translate-character rrr RRR Rrr))))
839 nil)
840
841 ;; Compile mule-to-unicode
842 (defun ccl-compile-mule-to-unicode (cmd)
843 (if (/= (length cmd) 3)
844 (error "CCL: Invalid number of arguments: %s" cmd))
845 (let ((RRR (nth 1 cmd))
846 (rrr (nth 2 cmd)))
847 (ccl-check-register RRR cmd)
848 (ccl-check-register rrr cmd)
849 (ccl-embed-extended-command 'mule-to-unicode RRR rrr 0))
850 nil)
851
852 ;; Given a Unicode code point in register rrr, write the charset ID of the
853 ;; corresponding character in RRR, and the Mule-CCL form of its code in rrr.
854 (defun ccl-compile-unicode-to-mule (cmd)
855 (if (/= (length cmd) 3)
856 (error "CCL: Invalid number of arguments: %s" cmd))
857 (let ((rrr (nth 1 cmd))
858 (RRR (nth 2 cmd)))
859 (ccl-check-register rrr cmd)
860 (ccl-check-register RRR cmd)
861 (ccl-embed-extended-command 'unicode-to-mule rrr RRR 0))
862 nil)
863
864 ;; Compile lookup-integer
865 (defun ccl-compile-lookup-integer (cmd)
866 (if (/= (length cmd) 4)
867 (error "CCL: Invalid number of arguments: %s" cmd))
868 (let ((Rrr (nth 1 cmd))
869 (RRR (nth 2 cmd))
870 (rrr (nth 3 cmd)))
871 (ccl-check-register RRR cmd)
872 (ccl-check-register rrr cmd)
873 (cond ((and (symbolp Rrr) (not (get Rrr 'ccl-register-number)))
874 (ccl-embed-extended-command 'lookup-int-const-tbl
875 rrr RRR 0)
876 (ccl-embed-symbol Rrr 'translation-hash-table-id))
877 (t
878 (error "CCL: non-constant table: %s" cmd)
879 ;; not implemented:
880 (ccl-check-register Rrr cmd)
881 (ccl-embed-extended-command 'lookup-int rrr RRR 0))))
882 nil)
883
884 ;; Compile lookup-character
885 (defun ccl-compile-lookup-character (cmd)
886 (if (/= (length cmd) 4)
887 (error "CCL: Invalid number of arguments: %s" cmd))
888 (let ((Rrr (nth 1 cmd))
889 (RRR (nth 2 cmd))
890 (rrr (nth 3 cmd)))
891 (ccl-check-register RRR cmd)
892 (ccl-check-register rrr cmd)
893 (cond ((and (symbolp Rrr) (not (get Rrr 'ccl-register-number)))
894 (ccl-embed-extended-command 'lookup-char-const-tbl
895 rrr RRR 0)
896 (ccl-embed-symbol Rrr 'translation-hash-table-id))
897 (t
898 (error "CCL: non-constant table: %s" cmd)
899 ;; not implemented:
900 (ccl-check-register Rrr cmd)
901 (ccl-embed-extended-command 'lookup-char rrr RRR 0))))
902 nil)
903
904 (defun ccl-compile-iterate-multiple-map (cmd)
905 (ccl-compile-multiple-map-function 'iterate-multiple-map cmd)
906 nil)
907
908 (defun ccl-compile-map-multiple (cmd)
909 (if (/= (length cmd) 4)
910 (error "CCL: Invalid number of arguments: %s" cmd))
911 (let (func arg)
912 (setq func
913 (lambda (arg mp)
914 (let ((len 0) result add)
915 (while arg
916 (if (consp (car arg))
917 (setq add (funcall func (car arg) t)
918 result (append result add)
919 add (+ (- (car add)) 1))
920 (setq result
921 (append result
922 (list (car arg)))
923 add 1))
924 (setq arg (cdr arg)
925 len (+ len add)))
926 (if mp
927 (cons (- len) result)
928 result))))
929 (setq arg (append (list (nth 0 cmd) (nth 1 cmd) (nth 2 cmd))
930 (funcall func (nth 3 cmd) nil)))
931 (ccl-compile-multiple-map-function 'map-multiple arg))
932 nil)
933
934 (defun ccl-compile-map-single (cmd)
935 (if (/= (length cmd) 4)
936 (error "CCL: Invalid number of arguments: %s" cmd))
937 (let ((RRR (nth 1 cmd))
938 (rrr (nth 2 cmd))
939 (map (nth 3 cmd)))
940 (ccl-check-register rrr cmd)
941 (ccl-check-register RRR cmd)
942 (ccl-embed-extended-command 'map-single rrr RRR 0)
943 (cond ((symbolp map)
944 (if (get map 'code-conversion-map)
945 (ccl-embed-symbol map 'code-conversion-map-id)
946 (error "CCL: Invalid map: %s" map)))
947 (t
948 (error "CCL: Invalid type of arguments: %s" cmd))))
949 nil)
950
951 (defun ccl-compile-multiple-map-function (command cmd)
952 (if (< (length cmd) 4)
953 (error "CCL: Invalid number of arguments: %s" cmd))
954 (let ((RRR (nth 1 cmd))
955 (rrr (nth 2 cmd))
956 (args (nthcdr 3 cmd))
957 map)
958 (ccl-check-register rrr cmd)
959 (ccl-check-register RRR cmd)
960 (ccl-embed-extended-command command rrr RRR 0)
961 (ccl-embed-data (length args))
962 (while args
963 (setq map (car args))
964 (cond ((symbolp map)
965 (if (get map 'code-conversion-map)
966 (ccl-embed-symbol map 'code-conversion-map-id)
967 (error "CCL: Invalid map: %s" map)))
968 ((numberp map)
969 (ccl-embed-data map))
970 (t
971 (error "CCL: Invalid type of arguments: %s" cmd)))
972 (setq args (cdr args)))))
973
974
975 ;;; CCL dump staffs
976
977 ;; To avoid byte-compiler warning.
978 (defvar ccl-code)
979
980 ;;;###autoload
981 (defun ccl-dump (ccl-code)
982 "Disassemble compiled CCL-CODE."
983 (let ((len (length ccl-code))
984 (buffer-mag (aref ccl-code 0)))
985 (cond ((= buffer-mag 0)
986 (insert "Don't output anything.\n"))
987 ((= buffer-mag 1)
988 (insert "Out-buffer must be as large as in-buffer.\n"))
989 (t
990 (insert
991 (format "Out-buffer must be %d times bigger than in-buffer.\n"
992 buffer-mag))))
993 (insert "Main-body:\n")
994 (setq ccl-current-ic 2)
995 (if (> (aref ccl-code 1) 0)
996 (progn
997 (while (< ccl-current-ic (aref ccl-code 1))
998 (ccl-dump-1))
999 (insert "At EOF:\n")))
1000 (while (< ccl-current-ic len)
1001 (ccl-dump-1))
1002 ))
1003
1004 ;; Return a CCL code in `ccl-code' at `ccl-current-ic'.
1005 (defun ccl-get-next-code ()
1006 (prog1
1007 (aref ccl-code ccl-current-ic)
1008 (setq ccl-current-ic (1+ ccl-current-ic))))
1009
1010 (defun ccl-dump-1 ()
1011 (let* ((code (ccl-get-next-code))
1012 (cmd (aref ccl-code-table (logand code 31)))
1013 (rrr (ash (logand code 255) -5))
1014 (cc (ash code -8)))
1015 (insert (format "%5d:[%s] " (1- ccl-current-ic) cmd))
1016 (funcall (get cmd 'ccl-dump-function) rrr cc)))
1017
1018 (defun ccl-dump-set-register (rrr cc)
1019 (insert (format "r%d = r%d\n" rrr cc)))
1020
1021 (defun ccl-dump-set-short-const (rrr cc)
1022 (insert (format "r%d = %d\n" rrr cc)))
1023
1024 (defun ccl-dump-set-const (rrr ignore)
1025 (insert (format "r%d = %d\n" rrr (ccl-get-next-code))))
1026
1027 (defun ccl-dump-set-array (rrr cc)
1028 (let ((rrr2 (logand cc 7))
1029 (len (ash cc -3))
1030 (i 0))
1031 (insert (format "r%d = array[r%d] of length %d\n\t"
1032 rrr rrr2 len))
1033 (while (< i len)
1034 (insert (format "%d " (ccl-get-next-code)))
1035 (setq i (1+ i)))
1036 (insert "\n")))
1037
1038 (defun ccl-dump-jump (ignore cc &optional address)
1039 (insert (format "jump to %d(" (+ (or address ccl-current-ic) cc)))
1040 (if (>= cc 0)
1041 (insert "+"))
1042 (insert (format "%d)\n" (1+ cc))))
1043
1044 (defun ccl-dump-jump-cond (rrr cc)
1045 (insert (format "if (r%d == 0), " rrr))
1046 (ccl-dump-jump nil cc))
1047
1048 (defun ccl-dump-write-register-jump (rrr cc)
1049 (insert (format "write r%d, " rrr))
1050 (ccl-dump-jump nil cc))
1051
1052 (defun ccl-dump-write-register-read-jump (rrr cc)
1053 (insert (format "write r%d, read r%d, " rrr rrr))
1054 (ccl-dump-jump nil cc)
1055 (ccl-get-next-code) ; Skip dummy READ-JUMP
1056 )
1057
1058 (defun ccl-extract-arith-op (cc)
1059 (aref ccl-arith-table (ash cc -6)))
1060
1061 (defun ccl-dump-write-expr-const (ignore cc)
1062 (insert (format "write (r%d %s %d)\n"
1063 (logand cc 7)
1064 (ccl-extract-arith-op cc)
1065 (ccl-get-next-code))))
1066
1067 (defun ccl-dump-write-expr-register (ignore cc)
1068 (insert (format "write (r%d %s r%d)\n"
1069 (logand cc 7)
1070 (ccl-extract-arith-op cc)
1071 (logand (ash cc -3) 7))))
1072
1073 (defun ccl-dump-insert-char (cc)
1074 (cond ((= cc ?\t) (insert " \"^I\""))
1075 ((= cc ?\n) (insert " \"^J\""))
1076 (t (insert (format " \"%c\"" cc)))))
1077
1078 (defun ccl-dump-write-const-jump (ignore cc)
1079 (let ((address ccl-current-ic))
1080 (insert "write char")
1081 (ccl-dump-insert-char (ccl-get-next-code))
1082 (insert ", ")
1083 (ccl-dump-jump nil cc address)))
1084
1085 (defun ccl-dump-write-const-read-jump (rrr cc)
1086 (let ((address ccl-current-ic))
1087 (insert "write char")
1088 (ccl-dump-insert-char (ccl-get-next-code))
1089 (insert (format ", read r%d, " rrr))
1090 (ccl-dump-jump cc address)
1091 (ccl-get-next-code) ; Skip dummy READ-JUMP
1092 ))
1093
1094 (defun ccl-dump-write-string-jump (ignore cc)
1095 (let ((address ccl-current-ic)
1096 (len (ccl-get-next-code))
1097 (i 0))
1098 (insert "write \"")
1099 (while (< i len)
1100 (let ((code (ccl-get-next-code)))
1101 (insert (ash code -16))
1102 (if (< (1+ i) len) (insert (logand (ash code -8) 255)))
1103 (if (< (+ i 2) len) (insert (logand code 255))))
1104 (setq i (+ i 3)))
1105 (insert "\", ")
1106 (ccl-dump-jump nil cc address)))
1107
1108 (defun ccl-dump-write-array-read-jump (rrr cc)
1109 (let ((address ccl-current-ic)
1110 (len (ccl-get-next-code))
1111 (i 0))
1112 (insert (format "write array[r%d] of length %d,\n\t" rrr len))
1113 (while (< i len)
1114 (ccl-dump-insert-char (ccl-get-next-code))
1115 (setq i (1+ i)))
1116 (insert (format "\n\tthen read r%d, " rrr))
1117 (ccl-dump-jump nil cc address)
1118 (ccl-get-next-code) ; Skip dummy READ-JUMP.
1119 ))
1120
1121 (defun ccl-dump-read-jump (rrr cc)
1122 (insert (format "read r%d, " rrr))
1123 (ccl-dump-jump nil cc))
1124
1125 (defun ccl-dump-branch (rrr len)
1126 (let ((jump-table-head ccl-current-ic)
1127 (i 0))
1128 (insert (format "jump to array[r%d] of length %d\n\t" rrr len))
1129 (while (<= i len)
1130 (insert (format "%d " (+ jump-table-head (ccl-get-next-code))))
1131 (setq i (1+ i)))
1132 (insert "\n")))
1133
1134 (defun ccl-dump-read-register (rrr cc)
1135 (insert (format "read r%d (%d remaining)\n" rrr cc)))
1136
1137 (defun ccl-dump-read-branch (rrr len)
1138 (insert (format "read r%d, " rrr))
1139 (ccl-dump-branch rrr len))
1140
1141 (defun ccl-dump-write-register (rrr cc)
1142 (insert (format "write r%d (%d remaining)\n" rrr cc)))
1143
1144 (defun ccl-dump-call (ignore cc)
1145 (insert (format "call subroutine #%d\n" cc)))
1146
1147 (defun ccl-dump-write-const-string (rrr cc)
1148 (if (= rrr 0)
1149 (progn
1150 (insert "write char")
1151 (ccl-dump-insert-char cc)
1152 (newline))
1153 (let ((len cc)
1154 (i 0))
1155 (insert "write \"")
1156 (while (< i len)
1157 (let ((code (ccl-get-next-code)))
1158 (insert (format "%c" (lsh code -16)))
1159 (if (< (1+ i) len)
1160 (insert (format "%c" (logand (lsh code -8) 255))))
1161 (if (< (+ i 2) len)
1162 (insert (format "%c" (logand code 255))))
1163 (setq i (+ i 3))))
1164 (insert "\"\n"))))
1165
1166 (defun ccl-dump-write-array (rrr cc)
1167 (let ((i 0))
1168 (insert (format "write array[r%d] of length %d\n\t" rrr cc))
1169 (while (< i cc)
1170 (ccl-dump-insert-char (ccl-get-next-code))
1171 (setq i (1+ i)))
1172 (insert "\n")))
1173
1174 (defun ccl-dump-end (&rest ignore)
1175 (insert "end\n"))
1176
1177 (defun ccl-dump-set-assign-expr-const (rrr cc)
1178 (insert (format "r%d %s= %d\n"
1179 rrr
1180 (ccl-extract-arith-op cc)
1181 (ccl-get-next-code))))
1182
1183 (defun ccl-dump-set-assign-expr-register (rrr cc)
1184 (insert (format "r%d %s= r%d\n"
1185 rrr
1186 (ccl-extract-arith-op cc)
1187 (logand cc 7))))
1188
1189 (defun ccl-dump-set-expr-const (rrr cc)
1190 (insert (format "r%d = r%d %s %d\n"
1191 rrr
1192 (logand cc 7)
1193 (ccl-extract-arith-op cc)
1194 (ccl-get-next-code))))
1195
1196 (defun ccl-dump-set-expr-register (rrr cc)
1197 (insert (format "r%d = r%d %s r%d\n"
1198 rrr
1199 (logand cc 7)
1200 (ccl-extract-arith-op cc)
1201 (logand (ash cc -3) 7))))
1202
1203 (defun ccl-dump-jump-cond-expr-const (rrr cc)
1204 (let ((address ccl-current-ic))
1205 (insert (format "if !(r%d %s %d), "
1206 rrr
1207 (aref ccl-arith-table (ccl-get-next-code))
1208 (ccl-get-next-code)))
1209 (ccl-dump-jump nil cc address)))
1210
1211 (defun ccl-dump-jump-cond-expr-register (rrr cc)
1212 (let ((address ccl-current-ic))
1213 (insert (format "if !(r%d %s r%d), "
1214 rrr
1215 (aref ccl-arith-table (ccl-get-next-code))
1216 (ccl-get-next-code)))
1217 (ccl-dump-jump nil cc address)))
1218
1219 (defun ccl-dump-read-jump-cond-expr-const (rrr cc)
1220 (insert (format "read r%d, " rrr))
1221 (ccl-dump-jump-cond-expr-const rrr cc))
1222
1223 (defun ccl-dump-read-jump-cond-expr-register (rrr cc)
1224 (insert (format "read r%d, " rrr))
1225 (ccl-dump-jump-cond-expr-register rrr cc))
1226
1227 (defun ccl-dump-binary (ccl-code)
1228 (let ((len (length ccl-code))
1229 (i 2))
1230 (while (< i len)
1231 (let ((code (aref ccl-code i))
1232 (j 27))
1233 (while (>= j 0)
1234 (insert (if (= (logand code (ash 1 j)) 0) ?0 ?1))
1235 (setq j (1- j)))
1236 (setq code (logand code 31))
1237 (if (< code (length ccl-code-table))
1238 (insert (format ":%s" (aref ccl-code-table code))))
1239 (insert "\n"))
1240 (setq i (1+ i)))))
1241
1242 (defun ccl-dump-ex-cmd (rrr cc)
1243 (let* ((RRR (logand cc #x7))
1244 (Rrr (logand (ash cc -3) #x7))
1245 (ex-op (aref ccl-extended-code-table (logand (ash cc -6) #x3fff))))
1246 (insert (format "<%s> " ex-op))
1247 (funcall (get ex-op 'ccl-dump-function) rrr RRR Rrr)))
1248
1249 (defun ccl-dump-read-multibyte-character (rrr RRR Rrr)
1250 (insert (format "read-multibyte-character r%d r%d\n" RRR rrr)))
1251
1252 (defun ccl-dump-write-multibyte-character (rrr RRR Rrr)
1253 (insert (format "write-multibyte-character r%d r%d\n" RRR rrr)))
1254
1255 (defun ccl-dump-translate-character (rrr RRR Rrr)
1256 (insert (format "translation table(r%d) r%d r%d\n" Rrr RRR rrr)))
1257
1258 (defun ccl-dump-translate-character-const-tbl (rrr RRR Rrr)
1259 (let ((tbl (ccl-get-next-code)))
1260 (insert (format "translation table(%S) r%d r%d\n" tbl RRR rrr))))
1261
1262 (defun ccl-dump-lookup-int-const-tbl (rrr RRR Rrr)
1263 (let ((tbl (ccl-get-next-code)))
1264 (insert (format "hash table(%S) r%d r%d\n" tbl RRR rrr))))
1265
1266 (defun ccl-dump-lookup-char-const-tbl (rrr RRR Rrr)
1267 (let ((tbl (ccl-get-next-code)))
1268 (insert (format "hash table(%S) r%d r%d\n" tbl RRR rrr))))
1269
1270 (defun ccl-dump-mule-to-unicode (rrr RRR Rrr)
1271 (insert (format "change chars in r%d and r%d to unicode\n" RRR rrr)))
1272
1273 (defun ccl-dump-unicode-to-mule (rrr RRR Rrr)
1274 (insert (format "converter UCS code %d to a Mule char\n" rrr)))
1275
1276 (defun ccl-dump-iterate-multiple-map (rrr RRR Rrr)
1277 (let ((notbl (ccl-get-next-code))
1278 (i 0) id)
1279 (insert (format "iterate-multiple-map r%d r%d\n" RRR rrr))
1280 (insert (format "\tnumber of maps is %d .\n\t [" notbl))
1281 (while (< i notbl)
1282 (setq id (ccl-get-next-code))
1283 (insert (format "%S" id))
1284 (setq i (1+ i)))
1285 (insert "]\n")))
1286
1287 (defun ccl-dump-map-multiple (rrr RRR Rrr)
1288 (let ((notbl (ccl-get-next-code))
1289 (i 0) id)
1290 (insert (format "map-multiple r%d r%d\n" RRR rrr))
1291 (insert (format "\tnumber of maps and separators is %d\n\t [" notbl))
1292 (while (< i notbl)
1293 (setq id (ccl-get-next-code))
1294 (if (= id -1)
1295 (insert "]\n\t [")
1296 (insert (format "%S " id)))
1297 (setq i (1+ i)))
1298 (insert "]\n")))
1299
1300 (defun ccl-dump-map-single (rrr RRR Rrr)
1301 (let ((id (ccl-get-next-code)))
1302 (insert (format "map-single r%d r%d map(%S)\n" RRR rrr id))))
1303
1304
1305 ;; CCL emulation staffs
1306
1307 ;; Not yet implemented.
1308
1309 ;; Auto-loaded functions.
1310
1311 ;;;###autoload
1312 (defmacro declare-ccl-program (name &optional vector)
1313 "Declare NAME as a name of CCL program.
1314
1315 This macro exists for backward compatibility. In the old version of
1316 Emacs, to compile a CCL program which calls another CCL program not
1317 yet defined, it must be declared as a CCL program in advance. But,
1318 now CCL program names are resolved not at compile time but before
1319 execution.
1320
1321 Optional arg VECTOR is a compiled CCL code of the CCL program."
1322 `(put ',name 'ccl-program-idx (register-ccl-program ',name ,vector)))
1323
1324 ;;;###autoload
1325 (defmacro define-ccl-program (name ccl-program &optional doc)
1326 "Set NAME to be the compiled CCL code of CCL-PROGRAM.
1327
1328 CCL-PROGRAM has this form:
1329 (BUFFER_MAGNIFICATION
1330 CCL_MAIN_CODE
1331 [ CCL_EOF_CODE ])
1332
1333 BUFFER_MAGNIFICATION is an integer value specifying the approximate
1334 output buffer magnification size compared with the bytes of input data
1335 text. If the value is zero, the CCL program can't execute `read' and
1336 `write' commands.
1337
1338 CCL_MAIN_CODE and CCL_EOF_CODE are CCL program codes. CCL_MAIN_CODE is
1339 executed first. If there are no more input data when a `read' command is
1340 executed in CCL_MAIN_CODE, CCL_EOF_CODE is executed. If CCL_MAIN_CODE is
1341 terminated, CCL_EOF_CODE is not executed.
1342
1343 Here's the syntax of CCL program code in BNF notation. The lines starting
1344 with two semicolons (and optional leading spaces) describe the semantics.
1345
1346 CCL_MAIN_CODE := CCL_BLOCK
1347
1348 CCL_EOF_CODE := CCL_BLOCK
1349
1350 CCL_BLOCK := STATEMENT | (STATEMENT [STATEMENT ...])
1351
1352 STATEMENT :=
1353 SET | IF | BRANCH | LOOP | REPEAT | BREAK | READ | WRITE | CALL
1354 | TRANSLATE | MAP | LOOKUP | END
1355
1356 SET := (REG = EXPRESSION)
1357 | (REG ASSIGNMENT_OPERATOR EXPRESSION)
1358 ;; The following form is the same as (r0 = INT-OR-CHAR).
1359 | INT-OR-CHAR
1360
1361 EXPRESSION := ARG | (EXPRESSION OPERATOR ARG)
1362
1363 ;; Evaluate EXPRESSION. If the result is nonzero, execute
1364 ;; CCL_BLOCK_0. Otherwise, execute CCL_BLOCK_1.
1365 IF := (if EXPRESSION CCL_BLOCK_0 [CCL_BLOCK_1])
1366
1367 ;; Evaluate EXPRESSION. Provided that the result is N, execute
1368 ;; CCL_BLOCK_N.
1369 BRANCH := (branch EXPRESSION CCL_BLOCK_0 [CCL_BLOCK_1 ...])
1370
1371 ;; Execute STATEMENTs until (break) or (end) is executed.
1372 LOOP := (loop STATEMENT [STATEMENT ...])
1373
1374 ;; Terminate the innermost loop.
1375 BREAK := (break)
1376
1377 REPEAT :=
1378 ;; Jump to the head of the innermost loop.
1379 (repeat)
1380 ;; Same as: ((write [REG | INT-OR-CHAR | string])
1381 ;; (repeat))
1382 | (write-repeat [REG | INT-OR-CHAR | string])
1383 ;; Same as: ((write REG [ARRAY])
1384 ;; (read REG)
1385 ;; (repeat))
1386 | (write-read-repeat REG [ARRAY])
1387 ;; Same as: ((write INT-OR-CHAR)
1388 ;; (read REG)
1389 ;; (repeat))
1390 | (write-read-repeat REG INT-OR-CHAR)
1391
1392 READ := ;; Set REG_0 to a byte read from the input text, set REG_1
1393 ;; to the next byte read, and so on. Note that \"byte\" here means
1394 ;; \"some octet from XEmacs' internal representation\", which may
1395 ;; not be that useful to you when non-ASCII characters are involved.
1396 ;;
1397 ;; Yes, this is exactly the opposite of what (write ...) does.
1398 (read REG_0 [REG_1 ...])
1399 ;; Same as: ((read REG)
1400 ;; (if (REG OPERATOR ARG) CCL_BLOCK_0 CCL_BLOCK_1))
1401 | (read-if (REG OPERATOR ARG) CCL_BLOCK_0 [CCL_BLOCK_1])
1402 ;; Same as: ((read REG)
1403 ;; (branch REG CCL_BLOCK_0 [CCL_BLOCK_1 ...]))
1404 | (read-branch REG CCL_BLOCK_0 [CCL_BLOCK_1 ...])
1405 ;; Read a character from the input text, splitting it into its
1406 ;; multibyte representation. Set REG_0 to the charset ID of the
1407 ;; character, and set REG_1 to the code point of the character. If
1408 ;; the dimension of charset is two, set REG_1 to ((CODE0 << 7) |
1409 ;; CODE1), where CODE0 is the first code point and CODE1 is the
1410 ;; second code point.
1411 | (read-multibyte-character REG_0 REG_1)
1412
1413 WRITE :=
1414 ;; Write REG_0, REG_1, ... to the output buffer. If REG_N is
1415 ;; a multibyte character, write the corresponding multibyte
1416 ;; representation.
1417 (write REG_0 [REG_1 ...])
1418 ;; Same as: ((r7 = EXPRESSION)
1419 ;; (write r7))
1420 | (write EXPRESSION)
1421 ;; Write the value of `INT-OR-CHAR' to the output buffer. If it
1422 ;; is a multibyte character, write the corresponding multibyte
1423 ;; representation.
1424 | (write INT-OR-CHAR)
1425 ;; Write the byte sequence of `string' as is to the output
1426 ;; buffer. It is encoded by binary coding system, thus,
1427 ;; by this operation, you cannot write multibyte string
1428 ;; as it is.
1429 | (write string)
1430 ;; Same as: (write string)
1431 | string
1432 ;; Provided that the value of REG is N, write Nth element of
1433 ;; ARRAY to the output buffer. If it is a multibyte
1434 ;; character, write the corresponding multibyte
1435 ;; representation.
1436 | (write REG ARRAY)
1437 ;; Write a multibyte representation of a character whose
1438 ;; charset ID is REG_0 and code point is REG_1. If the
1439 ;; dimension of the charset is two, REG_1 should be ((CODE0 <<
1440 ;; 7) | CODE1), where CODE0 is the first code point and CODE1
1441 ;; is the second code point of the character.
1442 | (write-multibyte-character REG_0 REG_1)
1443
1444 ;; Call CCL program whose name is ccl-program-name.
1445 CALL := (call ccl-program-name)
1446
1447 TRANSLATE := ;; Not implemented under XEmacs, except mule-to-unicode and
1448 ;; unicode-to-mule.
1449 (translate-character REG(table) REG(charset) REG(codepoint))
1450 | (translate-character SYMBOL REG(charset) REG(codepoint))
1451 | (mule-to-unicode REG(charset) REG(codepoint))
1452 | (unicode-to-mule REG(unicode,code) REG(CHARSET))
1453
1454 LOOKUP :=
1455 (lookup-character SYMBOL REG(charset) REG(codepoint))
1456 | (lookup-integer SYMBOL REG(integer))
1457 ;; SYMBOL refers to a table defined by `define-hash-translation-table'.
1458
1459 MAP :=
1460 (iterate-multiple-map REG REG MAP-IDs)
1461 | (map-multiple REG REG (MAP-SET))
1462 | (map-single REG REG MAP-ID)
1463 MAP-IDs := MAP-ID ...
1464 MAP-SET := MAP-IDs | (MAP-IDs) MAP-SET
1465 MAP-ID := INT-OR-CHAR
1466
1467 ;; Terminate the CCL program.
1468 END := (end)
1469
1470 ;; CCL registers. These can contain any integer value. As r7 is used by the
1471 ;; CCL interpreter itself, its value can change unexpectedly.
1472 REG := r0 | r1 | r2 | r3 | r4 | r5 | r6 | r7
1473
1474 ARG := REG | INT-OR-CHAR
1475
1476 OPERATOR :=
1477 ;; Normal arithmetical operators (same meaning as C code).
1478 + | - | * | / | %
1479
1480 ;; Bitwise operators (same meaning as C code)
1481 | & | `|' | ^
1482
1483 ;; Shifting operators (same meaning as C code)
1484 | << | >>
1485
1486 ;; (REG = ARG_0 <8 ARG_1) means:
1487 ;; (REG = ((ARG_0 << 8) | ARG_1))
1488 | <8
1489
1490 ;; (REG = ARG_0 >8 ARG_1) means:
1491 ;; ((REG = (ARG_0 >> 8))
1492 ;; (r7 = (ARG_0 & 255)))
1493 | >8
1494
1495 ;; (REG = ARG_0 // ARG_1) means:
1496 ;; ((REG = (ARG_0 / ARG_1))
1497 ;; (r7 = (ARG_0 % ARG_1)))
1498 | //
1499
1500 ;; Normal comparing operators (same meaning as C code)
1501 | < | > | == | <= | >= | !=
1502
1503 ;; If ARG_0 and ARG_1 are higher and lower byte of Shift-JIS
1504 ;; code, and CHAR is the corresponding JISX0208 character,
1505 ;; (REG = ARG_0 de-sjis ARG_1) means:
1506 ;; ((REG = CODE0)
1507 ;; (r7 = CODE1))
1508 ;; where CODE0 is the first code point of CHAR, CODE1 is the
1509 ;; second code point of CHAR.
1510 | de-sjis
1511
1512 ;; If ARG_0 and ARG_1 are the first and second code point of
1513 ;; JISX0208 character CHAR, and SJIS is the correponding
1514 ;; Shift-JIS code,
1515 ;; (REG = ARG_0 en-sjis ARG_1) means:
1516 ;; ((REG = HIGH)
1517 ;; (r7 = LOW))
1518 ;; where HIGH is the higher byte of SJIS, LOW is the lower
1519 ;; byte of SJIS.
1520 | en-sjis
1521
1522 ASSIGNMENT_OPERATOR :=
1523 ;; Same meaning as C code
1524 += | -= | *= | /= | %= | &= | `|=' | ^= | <<= | >>=
1525
1526 ;; (REG <8= ARG) is the same as:
1527 ;; ((REG <<= 8)
1528 ;; (REG |= ARG))
1529 | <8=
1530
1531 ;; (REG >8= ARG) is the same as:
1532 ;; ((r7 = (REG & 255))
1533 ;; (REG >>= 8))
1534
1535 ;; (REG //= ARG) is the same as:
1536 ;; ((r7 = (REG % ARG))
1537 ;; (REG /= ARG))
1538 | //=
1539
1540 ARRAY := `[' INT-OR-CHAR ... `]'
1541
1542 INT-OR-CHAR := integer | character
1543 "
1544 `(let ((prog ,(ccl-compile (eval ccl-program))))
1545 (defconst ,name prog ,doc)
1546 (put ',name 'ccl-program-idx (register-ccl-program ',name prog))
1547 nil))
1548
1549 ;;;###autoload
1550 (defmacro check-ccl-program (ccl-program &optional name)
1551 "Check validity of CCL-PROGRAM.
1552 If CCL-PROGRAM is a symbol denoting a CCL program, return
1553 CCL-PROGRAM, else return nil.
1554 If CCL-PROGRAM is a vector and optional arg NAME (symbol) is supplied,
1555 register CCL-PROGRAM by name NAME, and return NAME."
1556 `(if (ccl-program-p ,ccl-program)
1557 (if (vectorp ,ccl-program)
1558 (progn
1559 (register-ccl-program ,name ,ccl-program)
1560 ,name)
1561 ,ccl-program)))
1562
1563 (provide 'ccl)
1564
1565 ;; ccl.el ends here