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