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