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