comparison lisp/mule/mule-ccl.el @ 213:78f53ef88e17 r20-4b5

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