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