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