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