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