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