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