213
|
1 /* CCL (Code Conversion Language) interpreter.
|
|
2 Copyright (C) 1995, 1997 Electrotechnical Laboratory, JAPAN.
|
|
3 Licensed to the Free Software Foundation.
|
70
|
4
|
|
5 This file is part of XEmacs.
|
|
6
|
213
|
7 GNU Emacs is free software; you can redistribute it and/or modify
|
|
8 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)
|
|
10 any later version.
|
70
|
11
|
213
|
12 GNU Emacs is distributed in the hope that it will be useful,
|
|
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15 GNU General Public License for more details.
|
70
|
16
|
|
17 You should have received a copy of the GNU General Public License
|
213
|
18 along with GNU Emacs; see the file COPYING. If not, write to
|
70
|
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
20 Boston, MA 02111-1307, USA. */
|
|
21
|
213
|
22 /* Synched up with : FSF Emacs 20.2 */
|
|
23
|
|
24 #ifdef emacs
|
70
|
25
|
|
26 #include <config.h>
|
|
27 #include "lisp.h"
|
|
28 #include "buffer.h"
|
213
|
29 #include "mule-charset.h"
|
|
30 #include "mule-ccl.h"
|
259
|
31 #include "file-coding.h"
|
70
|
32
|
213
|
33 #else /* not emacs */
|
|
34
|
272
|
35 #include <stdio.h>
|
213
|
36 #include "mulelib.h"
|
|
37
|
|
38 #endif /* not emacs */
|
|
39
|
|
40 /* Alist of fontname patterns vs corresponding CCL program. */
|
|
41 Lisp_Object Vfont_ccl_encoder_alist;
|
70
|
42
|
213
|
43 /* Vector of CCL program names vs corresponding program data. */
|
|
44 Lisp_Object Vccl_program_table;
|
|
45
|
|
46 /* CCL (Code Conversion Language) is a simple language which has
|
|
47 operations on one input buffer, one output buffer, and 7 registers.
|
|
48 The syntax of CCL is described in `ccl.el'. Emacs Lisp function
|
|
49 `ccl-compile' compiles a CCL program and produces a CCL code which
|
|
50 is a vector of integers. The structure of this vector is as
|
|
51 follows: The 1st element: buffer-magnification, a factor for the
|
|
52 size of output buffer compared with the size of input buffer. The
|
|
53 2nd element: address of CCL code to be executed when encountered
|
|
54 with end of input stream. The 3rd and the remaining elements: CCL
|
|
55 codes. */
|
70
|
56
|
|
57 /* Header of CCL compiled code */
|
213
|
58 #define CCL_HEADER_BUF_MAG 0
|
|
59 #define CCL_HEADER_EOF 1
|
|
60 #define CCL_HEADER_MAIN 2
|
70
|
61
|
213
|
62 /* CCL code is a sequence of 28-bit non-negative integers (i.e. the
|
|
63 MSB is always 0), each contains CCL command and/or arguments in the
|
|
64 following format:
|
70
|
65
|
213
|
66 |----------------- integer (28-bit) ------------------|
|
|
67 |------- 17-bit ------|- 3-bit --|- 3-bit --|- 5-bit -|
|
|
68 |--constant argument--|-register-|-register-|-command-|
|
|
69 ccccccccccccccccc RRR rrr XXXXX
|
|
70 or
|
|
71 |------- relative address -------|-register-|-command-|
|
|
72 cccccccccccccccccccc rrr XXXXX
|
|
73 or
|
|
74 |------------- constant or other args ----------------|
|
|
75 cccccccccccccccccccccccccccc
|
|
76
|
|
77 where, `cc...c' is a non-negative integer indicating constant value
|
|
78 (the left most `c' is always 0) or an absolute jump address, `RRR'
|
|
79 and `rrr' are CCL register number, `XXXXX' is one of the following
|
|
80 CCL commands. */
|
|
81
|
|
82 /* CCL commands
|
|
83
|
|
84 Each comment fields shows one or more lines for command syntax and
|
|
85 the following lines for semantics of the command. In semantics, IC
|
|
86 stands for Instruction Counter. */
|
|
87
|
|
88 #define CCL_SetRegister 0x00 /* Set register a register value:
|
|
89 1:00000000000000000RRRrrrXXXXX
|
|
90 ------------------------------
|
|
91 reg[rrr] = reg[RRR];
|
|
92 */
|
70
|
93
|
213
|
94 #define CCL_SetShortConst 0x01 /* Set register a short constant value:
|
|
95 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
|
|
96 ------------------------------
|
|
97 reg[rrr] = CCCCCCCCCCCCCCCCCCC;
|
|
98 */
|
|
99
|
|
100 #define CCL_SetConst 0x02 /* Set register a constant value:
|
|
101 1:00000000000000000000rrrXXXXX
|
|
102 2:CONSTANT
|
|
103 ------------------------------
|
|
104 reg[rrr] = CONSTANT;
|
|
105 IC++;
|
|
106 */
|
70
|
107
|
213
|
108 #define CCL_SetArray 0x03 /* Set register an element of array:
|
|
109 1:CCCCCCCCCCCCCCCCCRRRrrrXXXXX
|
|
110 2:ELEMENT[0]
|
|
111 3:ELEMENT[1]
|
|
112 ...
|
|
113 ------------------------------
|
|
114 if (0 <= reg[RRR] < CC..C)
|
|
115 reg[rrr] = ELEMENT[reg[RRR]];
|
|
116 IC += CC..C;
|
|
117 */
|
70
|
118
|
213
|
119 #define CCL_Jump 0x04 /* Jump:
|
|
120 1:A--D--D--R--E--S--S-000XXXXX
|
|
121 ------------------------------
|
|
122 IC += ADDRESS;
|
|
123 */
|
|
124
|
|
125 /* Note: If CC..C is greater than 0, the second code is omitted. */
|
|
126
|
|
127 #define CCL_JumpCond 0x05 /* Jump conditional:
|
|
128 1:A--D--D--R--E--S--S-rrrXXXXX
|
|
129 ------------------------------
|
|
130 if (!reg[rrr])
|
|
131 IC += ADDRESS;
|
|
132 */
|
70
|
133
|
|
134
|
213
|
135 #define CCL_WriteRegisterJump 0x06 /* Write register and jump:
|
|
136 1:A--D--D--R--E--S--S-rrrXXXXX
|
|
137 ------------------------------
|
|
138 write (reg[rrr]);
|
|
139 IC += ADDRESS;
|
|
140 */
|
|
141
|
|
142 #define CCL_WriteRegisterReadJump 0x07 /* Write register, read, and jump:
|
|
143 1:A--D--D--R--E--S--S-rrrXXXXX
|
|
144 2:A--D--D--R--E--S--S-rrrYYYYY
|
|
145 -----------------------------
|
|
146 write (reg[rrr]);
|
|
147 IC++;
|
|
148 read (reg[rrr]);
|
|
149 IC += ADDRESS;
|
|
150 */
|
|
151 /* Note: If read is suspended, the resumed execution starts from the
|
|
152 second code (YYYYY == CCL_ReadJump). */
|
|
153
|
|
154 #define CCL_WriteConstJump 0x08 /* Write constant and jump:
|
|
155 1:A--D--D--R--E--S--S-000XXXXX
|
|
156 2:CONST
|
|
157 ------------------------------
|
|
158 write (CONST);
|
|
159 IC += ADDRESS;
|
|
160 */
|
|
161
|
|
162 #define CCL_WriteConstReadJump 0x09 /* Write constant, read, and jump:
|
|
163 1:A--D--D--R--E--S--S-rrrXXXXX
|
|
164 2:CONST
|
|
165 3:A--D--D--R--E--S--S-rrrYYYYY
|
|
166 -----------------------------
|
|
167 write (CONST);
|
|
168 IC += 2;
|
|
169 read (reg[rrr]);
|
|
170 IC += ADDRESS;
|
|
171 */
|
|
172 /* Note: If read is suspended, the resumed execution starts from the
|
|
173 second code (YYYYY == CCL_ReadJump). */
|
|
174
|
|
175 #define CCL_WriteStringJump 0x0A /* Write string and jump:
|
|
176 1:A--D--D--R--E--S--S-000XXXXX
|
|
177 2:LENGTH
|
|
178 3:0000STRIN[0]STRIN[1]STRIN[2]
|
|
179 ...
|
|
180 ------------------------------
|
|
181 write_string (STRING, LENGTH);
|
|
182 IC += ADDRESS;
|
|
183 */
|
|
184
|
|
185 #define CCL_WriteArrayReadJump 0x0B /* Write an array element, read, and jump:
|
|
186 1:A--D--D--R--E--S--S-rrrXXXXX
|
|
187 2:LENGTH
|
|
188 3:ELEMENET[0]
|
|
189 4:ELEMENET[1]
|
|
190 ...
|
|
191 N:A--D--D--R--E--S--S-rrrYYYYY
|
|
192 ------------------------------
|
|
193 if (0 <= reg[rrr] < LENGTH)
|
|
194 write (ELEMENT[reg[rrr]]);
|
|
195 IC += LENGTH + 2; (... pointing at N+1)
|
|
196 read (reg[rrr]);
|
|
197 IC += ADDRESS;
|
|
198 */
|
|
199 /* Note: If read is suspended, the resumed execution starts from the
|
|
200 Nth code (YYYYY == CCL_ReadJump). */
|
|
201
|
|
202 #define CCL_ReadJump 0x0C /* Read and jump:
|
|
203 1:A--D--D--R--E--S--S-rrrYYYYY
|
|
204 -----------------------------
|
|
205 read (reg[rrr]);
|
|
206 IC += ADDRESS;
|
|
207 */
|
|
208
|
|
209 #define CCL_Branch 0x0D /* Jump by branch table:
|
|
210 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
|
|
211 2:A--D--D--R--E-S-S[0]000XXXXX
|
|
212 3:A--D--D--R--E-S-S[1]000XXXXX
|
|
213 ...
|
|
214 ------------------------------
|
|
215 if (0 <= reg[rrr] < CC..C)
|
|
216 IC += ADDRESS[reg[rrr]];
|
|
217 else
|
|
218 IC += ADDRESS[CC..C];
|
|
219 */
|
|
220
|
|
221 #define CCL_ReadRegister 0x0E /* Read bytes into registers:
|
|
222 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
|
|
223 2:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
|
|
224 ...
|
|
225 ------------------------------
|
|
226 while (CCC--)
|
|
227 read (reg[rrr]);
|
|
228 */
|
|
229
|
|
230 #define CCL_WriteExprConst 0x0F /* write result of expression:
|
|
231 1:00000OPERATION000RRR000XXXXX
|
|
232 2:CONSTANT
|
|
233 ------------------------------
|
|
234 write (reg[RRR] OPERATION CONSTANT);
|
|
235 IC++;
|
|
236 */
|
|
237
|
|
238 /* Note: If the Nth read is suspended, the resumed execution starts
|
|
239 from the Nth code. */
|
|
240
|
|
241 #define CCL_ReadBranch 0x10 /* Read one byte into a register,
|
|
242 and jump by branch table:
|
|
243 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
|
|
244 2:A--D--D--R--E-S-S[0]000XXXXX
|
|
245 3:A--D--D--R--E-S-S[1]000XXXXX
|
|
246 ...
|
|
247 ------------------------------
|
|
248 read (read[rrr]);
|
|
249 if (0 <= reg[rrr] < CC..C)
|
|
250 IC += ADDRESS[reg[rrr]];
|
|
251 else
|
|
252 IC += ADDRESS[CC..C];
|
|
253 */
|
|
254
|
|
255 #define CCL_WriteRegister 0x11 /* Write registers:
|
|
256 1:CCCCCCCCCCCCCCCCCCCrrrXXXXX
|
|
257 2:CCCCCCCCCCCCCCCCCCCrrrXXXXX
|
|
258 ...
|
|
259 ------------------------------
|
|
260 while (CCC--)
|
|
261 write (reg[rrr]);
|
|
262 ...
|
|
263 */
|
|
264
|
|
265 /* Note: If the Nth write is suspended, the resumed execution
|
|
266 starts from the Nth code. */
|
|
267
|
|
268 #define CCL_WriteExprRegister 0x12 /* Write result of expression
|
|
269 1:00000OPERATIONRrrRRR000XXXXX
|
|
270 ------------------------------
|
|
271 write (reg[RRR] OPERATION reg[Rrr]);
|
|
272 */
|
|
273
|
|
274 #define CCL_Call 0x13 /* Write a constant:
|
|
275 1:CCCCCCCCCCCCCCCCCCCC000XXXXX
|
|
276 ------------------------------
|
|
277 call (CC..C)
|
|
278 */
|
|
279
|
|
280 #define CCL_WriteConstString 0x14 /* Write a constant or a string:
|
|
281 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
|
|
282 [2:0000STRIN[0]STRIN[1]STRIN[2]]
|
|
283 [...]
|
|
284 -----------------------------
|
|
285 if (!rrr)
|
|
286 write (CC..C)
|
|
287 else
|
|
288 write_string (STRING, CC..C);
|
|
289 IC += (CC..C + 2) / 3;
|
|
290 */
|
|
291
|
|
292 #define CCL_WriteArray 0x15 /* Write an element of array:
|
|
293 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
|
|
294 2:ELEMENT[0]
|
|
295 3:ELEMENT[1]
|
|
296 ...
|
|
297 ------------------------------
|
|
298 if (0 <= reg[rrr] < CC..C)
|
|
299 write (ELEMENT[reg[rrr]]);
|
|
300 IC += CC..C;
|
|
301 */
|
|
302
|
|
303 #define CCL_End 0x16 /* Terminate:
|
|
304 1:00000000000000000000000XXXXX
|
|
305 ------------------------------
|
|
306 terminate ();
|
|
307 */
|
|
308
|
|
309 /* The following two codes execute an assignment arithmetic/logical
|
|
310 operation. The form of the operation is like REG OP= OPERAND. */
|
|
311
|
|
312 #define CCL_ExprSelfConst 0x17 /* REG OP= constant:
|
|
313 1:00000OPERATION000000rrrXXXXX
|
|
314 2:CONSTANT
|
|
315 ------------------------------
|
|
316 reg[rrr] OPERATION= CONSTANT;
|
|
317 */
|
|
318
|
|
319 #define CCL_ExprSelfReg 0x18 /* REG1 OP= REG2:
|
|
320 1:00000OPERATION000RRRrrrXXXXX
|
|
321 ------------------------------
|
|
322 reg[rrr] OPERATION= reg[RRR];
|
|
323 */
|
|
324
|
|
325 /* The following codes execute an arithmetic/logical operation. The
|
|
326 form of the operation is like REG_X = REG_Y OP OPERAND2. */
|
70
|
327
|
213
|
328 #define CCL_SetExprConst 0x19 /* REG_X = REG_Y OP constant:
|
|
329 1:00000OPERATION000RRRrrrXXXXX
|
|
330 2:CONSTANT
|
|
331 ------------------------------
|
|
332 reg[rrr] = reg[RRR] OPERATION CONSTANT;
|
|
333 IC++;
|
|
334 */
|
|
335
|
|
336 #define CCL_SetExprReg 0x1A /* REG1 = REG2 OP REG3:
|
|
337 1:00000OPERATIONRrrRRRrrrXXXXX
|
|
338 ------------------------------
|
|
339 reg[rrr] = reg[RRR] OPERATION reg[Rrr];
|
|
340 */
|
|
341
|
|
342 #define CCL_JumpCondExprConst 0x1B /* Jump conditional according to
|
|
343 an operation on constant:
|
|
344 1:A--D--D--R--E--S--S-rrrXXXXX
|
|
345 2:OPERATION
|
|
346 3:CONSTANT
|
|
347 -----------------------------
|
|
348 reg[7] = reg[rrr] OPERATION CONSTANT;
|
|
349 if (!(reg[7]))
|
|
350 IC += ADDRESS;
|
|
351 else
|
|
352 IC += 2
|
|
353 */
|
|
354
|
|
355 #define CCL_JumpCondExprReg 0x1C /* Jump conditional according to
|
|
356 an operation on register:
|
|
357 1:A--D--D--R--E--S--S-rrrXXXXX
|
|
358 2:OPERATION
|
|
359 3:RRR
|
|
360 -----------------------------
|
|
361 reg[7] = reg[rrr] OPERATION reg[RRR];
|
|
362 if (!reg[7])
|
|
363 IC += ADDRESS;
|
|
364 else
|
|
365 IC += 2;
|
|
366 */
|
|
367
|
|
368 #define CCL_ReadJumpCondExprConst 0x1D /* Read and jump conditional according
|
|
369 to an operation on constant:
|
|
370 1:A--D--D--R--E--S--S-rrrXXXXX
|
|
371 2:OPERATION
|
|
372 3:CONSTANT
|
|
373 -----------------------------
|
|
374 read (reg[rrr]);
|
|
375 reg[7] = reg[rrr] OPERATION CONSTANT;
|
|
376 if (!reg[7])
|
|
377 IC += ADDRESS;
|
|
378 else
|
|
379 IC += 2;
|
|
380 */
|
|
381
|
|
382 #define CCL_ReadJumpCondExprReg 0x1E /* Read and jump conditional according
|
|
383 to an operation on register:
|
|
384 1:A--D--D--R--E--S--S-rrrXXXXX
|
|
385 2:OPERATION
|
|
386 3:RRR
|
|
387 -----------------------------
|
|
388 read (reg[rrr]);
|
|
389 reg[7] = reg[rrr] OPERATION reg[RRR];
|
|
390 if (!reg[7])
|
|
391 IC += ADDRESS;
|
|
392 else
|
|
393 IC += 2;
|
|
394 */
|
|
395
|
|
396 #define CCL_Extention 0x1F /* Extended CCL code
|
|
397 1:ExtendedCOMMNDRrrRRRrrrXXXXX
|
|
398 2:ARGUEMENT
|
|
399 3:...
|
|
400 ------------------------------
|
|
401 extended_command (rrr,RRR,Rrr,ARGS)
|
|
402 */
|
|
403
|
|
404
|
|
405 /* CCL arithmetic/logical operators. */
|
|
406 #define CCL_PLUS 0x00 /* X = Y + Z */
|
|
407 #define CCL_MINUS 0x01 /* X = Y - Z */
|
|
408 #define CCL_MUL 0x02 /* X = Y * Z */
|
|
409 #define CCL_DIV 0x03 /* X = Y / Z */
|
|
410 #define CCL_MOD 0x04 /* X = Y % Z */
|
|
411 #define CCL_AND 0x05 /* X = Y & Z */
|
|
412 #define CCL_OR 0x06 /* X = Y | Z */
|
|
413 #define CCL_XOR 0x07 /* X = Y ^ Z */
|
|
414 #define CCL_LSH 0x08 /* X = Y << Z */
|
|
415 #define CCL_RSH 0x09 /* X = Y >> Z */
|
|
416 #define CCL_LSH8 0x0A /* X = (Y << 8) | Z */
|
|
417 #define CCL_RSH8 0x0B /* X = Y >> 8, r[7] = Y & 0xFF */
|
|
418 #define CCL_DIVMOD 0x0C /* X = Y / Z, r[7] = Y % Z */
|
|
419 #define CCL_LS 0x10 /* X = (X < Y) */
|
|
420 #define CCL_GT 0x11 /* X = (X > Y) */
|
|
421 #define CCL_EQ 0x12 /* X = (X == Y) */
|
|
422 #define CCL_LE 0x13 /* X = (X <= Y) */
|
|
423 #define CCL_GE 0x14 /* X = (X >= Y) */
|
|
424 #define CCL_NE 0x15 /* X = (X != Y) */
|
|
425
|
|
426 #define CCL_ENCODE_SJIS 0x16 /* X = HIGHER_BYTE (SJIS (Y, Z))
|
|
427 r[7] = LOWER_BYTE (SJIS (Y, Z) */
|
|
428 #define CCL_DECODE_SJIS 0x17 /* X = HIGHER_BYTE (DE-SJIS (Y, Z))
|
|
429 r[7] = LOWER_BYTE (DE-SJIS (Y, Z)) */
|
|
430
|
|
431 /* Macros for exit status of CCL program. */
|
|
432 #define CCL_STAT_SUCCESS 0 /* Terminated successfully. */
|
|
433 #define CCL_STAT_SUSPEND 1 /* Terminated because of empty input
|
|
434 buffer or full output buffer. */
|
|
435 #define CCL_STAT_INVALID_CMD 2 /* Terminated because of invalid
|
|
436 command. */
|
|
437 #define CCL_STAT_QUIT 3 /* Terminated because of quit. */
|
|
438
|
|
439 /* Terminate CCL program successfully. */
|
|
440 #define CCL_SUCCESS \
|
|
441 do { \
|
|
442 ccl->status = CCL_STAT_SUCCESS; \
|
|
443 ccl->ic = CCL_HEADER_MAIN; \
|
|
444 goto ccl_finish; \
|
|
445 } while (0)
|
|
446
|
|
447 /* Suspend CCL program because of reading from empty input buffer or
|
|
448 writing to full output buffer. When this program is resumed, the
|
|
449 same I/O command is executed. */
|
|
450 #define CCL_SUSPEND \
|
|
451 do { \
|
|
452 ic--; \
|
|
453 ccl->status = CCL_STAT_SUSPEND; \
|
|
454 goto ccl_finish; \
|
|
455 } while (0)
|
|
456
|
|
457 /* Terminate CCL program because of invalid command. Should not occur
|
|
458 in the normal case. */
|
|
459 #define CCL_INVALID_CMD \
|
|
460 do { \
|
|
461 ccl->status = CCL_STAT_INVALID_CMD; \
|
|
462 goto ccl_error_handler; \
|
|
463 } while (0)
|
|
464
|
|
465 /* Encode one character CH to multibyte form and write to the current
|
|
466 output buffer. If CH is less than 256, CH is written as is. */
|
|
467 #define CCL_WRITE_CHAR(ch) \
|
|
468 do { \
|
|
469 if (!destination) \
|
|
470 CCL_INVALID_CMD; \
|
|
471 else \
|
|
472 { \
|
|
473 Bufbyte work[MAX_EMCHAR_LEN]; \
|
|
474 int len = set_charptr_emchar (work, ch); \
|
|
475 Dynarr_add_many (destination, work, len); \
|
|
476 } \
|
|
477 } while (0)
|
|
478
|
|
479 /* Write a string at ccl_prog[IC] of length LEN to the current output
|
|
480 buffer. */
|
|
481 #define CCL_WRITE_STRING(len) \
|
|
482 do { \
|
|
483 if (!destination) \
|
|
484 CCL_INVALID_CMD; \
|
|
485 else \
|
|
486 for (i = 0; i < len; i++) \
|
|
487 Dynarr_add(destination, (XINT (ccl_prog[ic + (i / 3)]) \
|
|
488 >> ((2 - (i % 3)) * 8)) & 0xFF); \
|
|
489 } while (0)
|
|
490
|
|
491 /* Read one byte from the current input buffer into Rth register. */
|
|
492 #define CCL_READ_CHAR(r) \
|
|
493 do { \
|
|
494 if (!src) \
|
|
495 CCL_INVALID_CMD; \
|
|
496 else if (src < src_end) \
|
|
497 r = *src++; \
|
|
498 else if (ccl->last_block) \
|
|
499 { \
|
|
500 ic = ccl->eof_ic; \
|
|
501 goto ccl_finish; \
|
|
502 } \
|
|
503 else \
|
|
504 CCL_SUSPEND; \
|
|
505 } while (0)
|
|
506
|
|
507
|
|
508 /* Execute CCL code on SRC_BYTES length text at SOURCE. The resulting
|
|
509 text goes to a place pointed by DESTINATION. The bytes actually
|
|
510 processed is returned as *CONSUMED. The return value is the length
|
|
511 of the resulting text. As a side effect, the contents of CCL registers
|
|
512 are updated. If SOURCE or DESTINATION is NULL, only operations on
|
|
513 registers are permitted. */
|
|
514
|
|
515 #ifdef CCL_DEBUG
|
|
516 #define CCL_DEBUG_BACKTRACE_LEN 256
|
|
517 int ccl_backtrace_table[CCL_BACKTRACE_TABLE];
|
|
518 int ccl_backtrace_idx;
|
|
519 #endif
|
|
520
|
|
521 struct ccl_prog_stack
|
|
522 {
|
|
523 Lisp_Object *ccl_prog; /* Pointer to an array of CCL code. */
|
|
524 int ic; /* Instruction Counter. */
|
|
525 };
|
70
|
526
|
|
527 int
|
213
|
528 ccl_driver (struct ccl_program *ccl, CONST unsigned char *source, unsigned_char_dynarr *destination, int src_bytes, int *consumed)
|
70
|
529 {
|
|
530 int *reg = ccl->reg;
|
213
|
531 int ic = ccl->ic;
|
249
|
532 int code = -1; /* init to illegal value, */
|
|
533 int field1, field2;
|
213
|
534 Lisp_Object *ccl_prog = ccl->prog;
|
251
|
535 CONST unsigned char *src = source, *src_end = src + src_bytes;
|
249
|
536 int jump_address = 0; /* shut up the compiler */
|
251
|
537
|
213
|
538 int i, j, op;
|
|
539 int stack_idx = 0;
|
272
|
540 /* For the moment, we only support depth 256 of stack. */
|
213
|
541 struct ccl_prog_stack ccl_prog_stack_struct[256];
|
70
|
542
|
213
|
543 if (ic >= ccl->eof_ic)
|
70
|
544 ic = CCL_HEADER_MAIN;
|
|
545
|
213
|
546 #ifdef CCL_DEBUG
|
|
547 ccl_backtrace_idx = 0;
|
|
548 #endif
|
70
|
549
|
213
|
550 for (;;)
|
70
|
551 {
|
213
|
552 #ifdef CCL_DEBUG
|
|
553 ccl_backtrace_table[ccl_backtrace_idx++] = ic;
|
|
554 if (ccl_backtrace_idx >= CCL_DEBUG_BACKTRACE_LEN)
|
|
555 ccl_backtrace_idx = 0;
|
|
556 ccl_backtrace_table[ccl_backtrace_idx] = 0;
|
|
557 #endif
|
70
|
558
|
213
|
559 if (!NILP (Vquit_flag) && NILP (Vinhibit_quit))
|
70
|
560 {
|
213
|
561 /* We can't just signal Qquit, instead break the loop as if
|
|
562 the whole data is processed. Don't reset Vquit_flag, it
|
|
563 must be handled later at a safer place. */
|
|
564 if (consumed)
|
|
565 src = source + src_bytes;
|
|
566 ccl->status = CCL_STAT_QUIT;
|
|
567 break;
|
|
568 }
|
|
569
|
|
570 code = XINT (ccl_prog[ic]); ic++;
|
|
571 field1 = code >> 8;
|
|
572 field2 = (code & 0xFF) >> 5;
|
|
573
|
|
574 #define rrr field2
|
|
575 #define RRR (field1 & 7)
|
|
576 #define Rrr ((field1 >> 3) & 7)
|
|
577 #define ADDR field1
|
|
578
|
|
579 switch (code & 0x1F)
|
|
580 {
|
|
581 case CCL_SetRegister: /* 00000000000000000RRRrrrXXXXX */
|
|
582 reg[rrr] = reg[RRR];
|
|
583 break;
|
|
584
|
|
585 case CCL_SetShortConst: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
|
|
586 reg[rrr] = field1;
|
|
587 break;
|
|
588
|
|
589 case CCL_SetConst: /* 00000000000000000000rrrXXXXX */
|
|
590 reg[rrr] = XINT (ccl_prog[ic]);
|
|
591 ic++;
|
|
592 break;
|
|
593
|
|
594 case CCL_SetArray: /* CCCCCCCCCCCCCCCCCCCCRRRrrrXXXXX */
|
|
595 i = reg[RRR];
|
|
596 j = field1 >> 3;
|
|
597 if ((unsigned int) i < j)
|
|
598 reg[rrr] = XINT (ccl_prog[ic + i]);
|
|
599 ic += j;
|
|
600 break;
|
|
601
|
|
602 case CCL_Jump: /* A--D--D--R--E--S--S-000XXXXX */
|
|
603 ic += ADDR;
|
|
604 break;
|
|
605
|
|
606 case CCL_JumpCond: /* A--D--D--R--E--S--S-rrrXXXXX */
|
70
|
607 if (!reg[rrr])
|
213
|
608 ic += ADDR;
|
|
609 break;
|
|
610
|
|
611 case CCL_WriteRegisterJump: /* A--D--D--R--E--S--S-rrrXXXXX */
|
|
612 i = reg[rrr];
|
|
613 CCL_WRITE_CHAR (i);
|
|
614 ic += ADDR;
|
|
615 break;
|
|
616
|
|
617 case CCL_WriteRegisterReadJump: /* A--D--D--R--E--S--S-rrrXXXXX */
|
|
618 i = reg[rrr];
|
|
619 CCL_WRITE_CHAR (i);
|
|
620 ic++;
|
70
|
621 CCL_READ_CHAR (reg[rrr]);
|
213
|
622 ic += ADDR - 1;
|
|
623 break;
|
|
624
|
|
625 case CCL_WriteConstJump: /* A--D--D--R--E--S--S-000XXXXX */
|
|
626 i = XINT (ccl_prog[ic]);
|
|
627 CCL_WRITE_CHAR (i);
|
|
628 ic += ADDR;
|
|
629 break;
|
|
630
|
|
631 case CCL_WriteConstReadJump: /* A--D--D--R--E--S--S-rrrXXXXX */
|
|
632 i = XINT (ccl_prog[ic]);
|
|
633 CCL_WRITE_CHAR (i);
|
|
634 ic++;
|
70
|
635 CCL_READ_CHAR (reg[rrr]);
|
213
|
636 ic += ADDR - 1;
|
|
637 break;
|
|
638
|
|
639 case CCL_WriteStringJump: /* A--D--D--R--E--S--S-000XXXXX */
|
|
640 j = XINT (ccl_prog[ic]);
|
|
641 ic++;
|
|
642 CCL_WRITE_STRING (j);
|
|
643 ic += ADDR - 1;
|
|
644 break;
|
|
645
|
|
646 case CCL_WriteArrayReadJump: /* A--D--D--R--E--S--S-rrrXXXXX */
|
|
647 i = reg[rrr];
|
|
648 j = XINT (ccl_prog[ic]);
|
|
649 if ((unsigned int) i < j)
|
70
|
650 {
|
213
|
651 i = XINT (ccl_prog[ic + 1 + i]);
|
|
652 CCL_WRITE_CHAR (i);
|
70
|
653 }
|
213
|
654 ic += j + 2;
|
70
|
655 CCL_READ_CHAR (reg[rrr]);
|
213
|
656 ic += ADDR - (j + 2);
|
|
657 break;
|
|
658
|
|
659 case CCL_ReadJump: /* A--D--D--R--E--S--S-rrrYYYYY */
|
70
|
660 CCL_READ_CHAR (reg[rrr]);
|
213
|
661 ic += ADDR;
|
|
662 break;
|
|
663
|
|
664 case CCL_ReadBranch: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
|
70
|
665 CCL_READ_CHAR (reg[rrr]);
|
213
|
666 /* fall through ... */
|
|
667 case CCL_Branch: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
|
|
668 if ((unsigned int) reg[rrr] < field1)
|
|
669 ic += XINT (ccl_prog[ic + reg[rrr]]);
|
|
670 else
|
|
671 ic += XINT (ccl_prog[ic + field1]);
|
|
672 break;
|
|
673
|
|
674 case CCL_ReadRegister: /* CCCCCCCCCCCCCCCCCCCCrrXXXXX */
|
|
675 while (1)
|
|
676 {
|
|
677 CCL_READ_CHAR (reg[rrr]);
|
|
678 if (!field1) break;
|
|
679 code = XINT (ccl_prog[ic]); ic++;
|
|
680 field1 = code >> 8;
|
|
681 field2 = (code & 0xFF) >> 5;
|
|
682 }
|
|
683 break;
|
|
684
|
|
685 case CCL_WriteExprConst: /* 1:00000OPERATION000RRR000XXXXX */
|
|
686 rrr = 7;
|
|
687 i = reg[RRR];
|
|
688 j = XINT (ccl_prog[ic]);
|
|
689 op = field1 >> 6;
|
|
690 ic++;
|
|
691 goto ccl_set_expr;
|
|
692
|
|
693 case CCL_WriteRegister: /* CCCCCCCCCCCCCCCCCCCrrrXXXXX */
|
|
694 while (1)
|
|
695 {
|
|
696 i = reg[rrr];
|
|
697 CCL_WRITE_CHAR (i);
|
|
698 if (!field1) break;
|
|
699 code = XINT (ccl_prog[ic]); ic++;
|
|
700 field1 = code >> 8;
|
|
701 field2 = (code & 0xFF) >> 5;
|
|
702 }
|
|
703 break;
|
|
704
|
|
705 case CCL_WriteExprRegister: /* 1:00000OPERATIONRrrRRR000XXXXX */
|
|
706 rrr = 7;
|
|
707 i = reg[RRR];
|
|
708 j = reg[Rrr];
|
|
709 op = field1 >> 6;
|
|
710 goto ccl_set_expr;
|
|
711
|
|
712 case CCL_Call: /* CCCCCCCCCCCCCCCCCCCC000XXXXX */
|
|
713 {
|
|
714 Lisp_Object slot;
|
|
715
|
|
716 if (stack_idx >= 256
|
|
717 || field1 < 0
|
|
718 || field1 >= XVECTOR_LENGTH (Vccl_program_table)
|
|
719 || (slot = XVECTOR_DATA (Vccl_program_table)[field1],
|
|
720 !CONSP (slot))
|
|
721 || !VECTORP (XCDR (slot)))
|
|
722 {
|
|
723 if (stack_idx > 0)
|
|
724 {
|
|
725 ccl_prog = ccl_prog_stack_struct[0].ccl_prog;
|
|
726 ic = ccl_prog_stack_struct[0].ic;
|
|
727 }
|
|
728 CCL_INVALID_CMD;
|
|
729 }
|
272
|
730
|
213
|
731 ccl_prog_stack_struct[stack_idx].ccl_prog = ccl_prog;
|
|
732 ccl_prog_stack_struct[stack_idx].ic = ic;
|
|
733 stack_idx++;
|
|
734 ccl_prog = XVECTOR_DATA (XCDR (slot));
|
|
735 ic = CCL_HEADER_MAIN;
|
|
736 }
|
|
737 break;
|
|
738
|
|
739 case CCL_WriteConstString: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
|
|
740 if (!rrr)
|
|
741 CCL_WRITE_CHAR (field1);
|
|
742 else
|
|
743 {
|
|
744 CCL_WRITE_STRING (field1);
|
|
745 ic += (field1 + 2) / 3;
|
|
746 }
|
|
747 break;
|
|
748
|
|
749 case CCL_WriteArray: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
|
|
750 i = reg[rrr];
|
|
751 if ((unsigned int) i < field1)
|
|
752 {
|
|
753 j = XINT (ccl_prog[ic + i]);
|
|
754 CCL_WRITE_CHAR (j);
|
|
755 }
|
|
756 ic += field1;
|
|
757 break;
|
|
758
|
|
759 case CCL_End: /* 0000000000000000000000XXXXX */
|
|
760 if (stack_idx-- > 0)
|
|
761 {
|
|
762 ccl_prog = ccl_prog_stack_struct[stack_idx].ccl_prog;
|
|
763 ic = ccl_prog_stack_struct[stack_idx].ic;
|
|
764 break;
|
|
765 }
|
70
|
766 CCL_SUCCESS;
|
213
|
767
|
|
768 case CCL_ExprSelfConst: /* 00000OPERATION000000rrrXXXXX */
|
|
769 i = XINT (ccl_prog[ic]);
|
|
770 ic++;
|
|
771 op = field1 >> 6;
|
|
772 goto ccl_expr_self;
|
|
773
|
|
774 case CCL_ExprSelfReg: /* 00000OPERATION000RRRrrrXXXXX */
|
|
775 i = reg[RRR];
|
|
776 op = field1 >> 6;
|
|
777
|
|
778 ccl_expr_self:
|
70
|
779 switch (op)
|
|
780 {
|
213
|
781 case CCL_PLUS: reg[rrr] += i; break;
|
|
782 case CCL_MINUS: reg[rrr] -= i; break;
|
|
783 case CCL_MUL: reg[rrr] *= i; break;
|
|
784 case CCL_DIV: reg[rrr] /= i; break;
|
|
785 case CCL_MOD: reg[rrr] %= i; break;
|
|
786 case CCL_AND: reg[rrr] &= i; break;
|
|
787 case CCL_OR: reg[rrr] |= i; break;
|
|
788 case CCL_XOR: reg[rrr] ^= i; break;
|
|
789 case CCL_LSH: reg[rrr] <<= i; break;
|
|
790 case CCL_RSH: reg[rrr] >>= i; break;
|
|
791 case CCL_LSH8: reg[rrr] <<= 8; reg[rrr] |= i; break;
|
|
792 case CCL_RSH8: reg[7] = reg[rrr] & 0xFF; reg[rrr] >>= 8; break;
|
|
793 case CCL_DIVMOD: reg[7] = reg[rrr] % i; reg[rrr] /= i; break;
|
|
794 case CCL_LS: reg[rrr] = reg[rrr] < i; break;
|
|
795 case CCL_GT: reg[rrr] = reg[rrr] > i; break;
|
|
796 case CCL_EQ: reg[rrr] = reg[rrr] == i; break;
|
|
797 case CCL_LE: reg[rrr] = reg[rrr] <= i; break;
|
|
798 case CCL_GE: reg[rrr] = reg[rrr] >= i; break;
|
|
799 case CCL_NE: reg[rrr] = reg[rrr] != i; break;
|
70
|
800 default: CCL_INVALID_CMD;
|
|
801 }
|
213
|
802 break;
|
|
803
|
|
804 case CCL_SetExprConst: /* 00000OPERATION000RRRrrrXXXXX */
|
|
805 i = reg[RRR];
|
|
806 j = XINT (ccl_prog[ic]);
|
|
807 op = field1 >> 6;
|
|
808 jump_address = ++ic;
|
70
|
809 goto ccl_set_expr;
|
213
|
810
|
|
811 case CCL_SetExprReg: /* 00000OPERATIONRrrRRRrrrXXXXX */
|
|
812 i = reg[RRR];
|
|
813 j = reg[Rrr];
|
|
814 op = field1 >> 6;
|
|
815 jump_address = ic;
|
70
|
816 goto ccl_set_expr;
|
213
|
817
|
|
818 case CCL_ReadJumpCondExprConst: /* A--D--D--R--E--S--S-rrrXXXXX */
|
70
|
819 CCL_READ_CHAR (reg[rrr]);
|
213
|
820 case CCL_JumpCondExprConst: /* A--D--D--R--E--S--S-rrrXXXXX */
|
70
|
821 i = reg[rrr];
|
213
|
822 op = XINT (ccl_prog[ic]);
|
|
823 jump_address = ic++ + ADDR;
|
|
824 j = XINT (ccl_prog[ic]);
|
|
825 ic++;
|
70
|
826 rrr = 7;
|
|
827 goto ccl_set_expr;
|
213
|
828
|
|
829 case CCL_ReadJumpCondExprReg: /* A--D--D--R--E--S--S-rrrXXXXX */
|
70
|
830 CCL_READ_CHAR (reg[rrr]);
|
213
|
831 case CCL_JumpCondExprReg:
|
70
|
832 i = reg[rrr];
|
213
|
833 op = XINT (ccl_prog[ic]);
|
|
834 jump_address = ic++ + ADDR;
|
|
835 j = reg[XINT (ccl_prog[ic])];
|
|
836 ic++;
|
70
|
837 rrr = 7;
|
213
|
838
|
|
839 ccl_set_expr:
|
70
|
840 switch (op)
|
|
841 {
|
213
|
842 case CCL_PLUS: reg[rrr] = i + j; break;
|
|
843 case CCL_MINUS: reg[rrr] = i - j; break;
|
|
844 case CCL_MUL: reg[rrr] = i * j; break;
|
|
845 case CCL_DIV: reg[rrr] = i / j; break;
|
|
846 case CCL_MOD: reg[rrr] = i % j; break;
|
|
847 case CCL_AND: reg[rrr] = i & j; break;
|
|
848 case CCL_OR: reg[rrr] = i | j; break;
|
|
849 case CCL_XOR: reg[rrr] = i ^ j;; break;
|
|
850 case CCL_LSH: reg[rrr] = i << j; break;
|
|
851 case CCL_RSH: reg[rrr] = i >> j; break;
|
|
852 case CCL_LSH8: reg[rrr] = (i << 8) | j; break;
|
|
853 case CCL_RSH8: reg[rrr] = i >> 8; reg[7] = i & 0xFF; break;
|
|
854 case CCL_DIVMOD: reg[rrr] = i / j; reg[7] = i % j; break;
|
|
855 case CCL_LS: reg[rrr] = i < j; break;
|
|
856 case CCL_GT: reg[rrr] = i > j; break;
|
|
857 case CCL_EQ: reg[rrr] = i == j; break;
|
|
858 case CCL_LE: reg[rrr] = i <= j; break;
|
|
859 case CCL_GE: reg[rrr] = i >= j; break;
|
|
860 case CCL_NE: reg[rrr] = i != j; break;
|
|
861 case CCL_ENCODE_SJIS: ENCODE_SJIS (i, j, reg[rrr], reg[7]); break;
|
|
862 case CCL_DECODE_SJIS: DECODE_SJIS (i, j, reg[rrr], reg[7]); break;
|
70
|
863 default: CCL_INVALID_CMD;
|
|
864 }
|
213
|
865 code &= 0x1F;
|
|
866 if (code == CCL_WriteExprConst || code == CCL_WriteExprRegister)
|
|
867 {
|
|
868 i = reg[rrr];
|
|
869 CCL_WRITE_CHAR (i);
|
|
870 }
|
|
871 else if (!reg[rrr])
|
|
872 ic = jump_address;
|
|
873 break;
|
|
874
|
70
|
875 default:
|
|
876 CCL_INVALID_CMD;
|
|
877 }
|
|
878 }
|
|
879
|
213
|
880 ccl_error_handler:
|
|
881 if (destination)
|
70
|
882 {
|
213
|
883 /* We can insert an error message only if DESTINATION is
|
|
884 specified and we still have a room to store the message
|
|
885 there. */
|
|
886 char msg[256];
|
|
887
|
70
|
888 switch (ccl->status)
|
|
889 {
|
|
890 case CCL_STAT_INVALID_CMD:
|
213
|
891 sprintf(msg, "\nCCL: Invalid command %x (ccl_code = %x) at %d.",
|
|
892 code & 0x1F, code, ic);
|
|
893 #ifdef CCL_DEBUG
|
|
894 {
|
|
895 int i = ccl_backtrace_idx - 1;
|
|
896 int j;
|
|
897
|
|
898 Dynarr_add_many (destination, (unsigned char *) msg, strlen (msg));
|
|
899
|
|
900 for (j = 0; j < CCL_DEBUG_BACKTRACE_LEN; j++, i--)
|
|
901 {
|
|
902 if (i < 0) i = CCL_DEBUG_BACKTRACE_LEN - 1;
|
|
903 if (ccl_backtrace_table[i] == 0)
|
|
904 break;
|
|
905 sprintf(msg, " %d", ccl_backtrace_table[i]);
|
|
906 Dynarr_add_many (destination, (unsigned char *) msg, strlen (msg));
|
|
907 }
|
|
908 }
|
|
909 #endif
|
|
910 goto ccl_finish;
|
|
911
|
|
912 case CCL_STAT_QUIT:
|
|
913 sprintf(msg, "\nCCL: Quited.");
|
70
|
914 break;
|
213
|
915
|
70
|
916 default:
|
213
|
917 sprintf(msg, "\nCCL: Unknown error type (%d).", ccl->status);
|
70
|
918 }
|
213
|
919
|
|
920 Dynarr_add_many (destination, (unsigned char *) msg, strlen (msg));
|
70
|
921 }
|
|
922
|
213
|
923 ccl_finish:
|
70
|
924 ccl->ic = ic;
|
213
|
925 if (consumed) *consumed = src - source;
|
|
926 if (destination)
|
|
927 return Dynarr_length (destination);
|
70
|
928 else
|
|
929 return 0;
|
|
930 }
|
|
931
|
213
|
932 /* Setup fields of the structure pointed by CCL appropriately for the
|
|
933 execution of compiled CCL code in VEC (vector of integer). */
|
70
|
934 void
|
243
|
935 setup_ccl_program (struct ccl_program *ccl, Lisp_Object vec)
|
70
|
936 {
|
|
937 int i;
|
|
938
|
213
|
939 ccl->size = XVECTOR_LENGTH (vec);
|
|
940 ccl->prog = XVECTOR_DATA (vec);
|
|
941 ccl->ic = CCL_HEADER_MAIN;
|
|
942 ccl->eof_ic = XINT (XVECTOR_DATA (vec)[CCL_HEADER_EOF]);
|
|
943 ccl->buf_magnification = XINT (XVECTOR_DATA (vec)[CCL_HEADER_BUF_MAG]);
|
|
944 for (i = 0; i < 8; i++)
|
70
|
945 ccl->reg[i] = 0;
|
213
|
946 ccl->last_block = 0;
|
70
|
947 ccl->status = 0;
|
|
948 }
|
|
949
|
|
950 #ifdef emacs
|
|
951
|
213
|
952 DEFUN ("ccl-execute", Fccl_execute, 2, 2, 0, /*
|
|
953 Execute CCL-PROGRAM with registers initialized by REGISTERS.
|
|
954 CCL-PROGRAM is a compiled code generated by `ccl-compile',
|
|
955 no I/O commands should appear in the CCL program.
|
|
956 REGISTERS is a vector of [R0 R1 ... R7] where RN is an initial value
|
|
957 of Nth register.
|
|
958 As side effect, each element of REGISTER holds the value of
|
|
959 corresponding register after the execution.
|
|
960 */
|
|
961 (ccl_prog, reg))
|
70
|
962 {
|
213
|
963 struct ccl_program ccl;
|
70
|
964 int i;
|
213
|
965
|
|
966 CHECK_VECTOR (ccl_prog);
|
|
967 CHECK_VECTOR (reg);
|
|
968 if (XVECTOR_LENGTH (reg) != 8)
|
241
|
969 signal_simple_error ("Vector should be of length 8", reg);
|
213
|
970
|
|
971 setup_ccl_program (&ccl, ccl_prog);
|
|
972 for (i = 0; i < 8; i++)
|
|
973 ccl.reg[i] = (INTP (XVECTOR_DATA (reg)[i])
|
|
974 ? XINT (XVECTOR_DATA (reg)[i])
|
|
975 : 0);
|
|
976
|
|
977 ccl_driver (&ccl, (CONST unsigned char *)0, (unsigned_char_dynarr *)0,
|
|
978 0, (int *)0);
|
|
979 QUIT;
|
|
980 if (ccl.status != CCL_STAT_SUCCESS)
|
|
981 error ("Error in CCL program at %dth code", ccl.ic);
|
|
982
|
|
983 for (i = 0; i < 8; i++)
|
|
984 XSETINT (XVECTOR_DATA (reg)[i], ccl.reg[i]);
|
|
985 return Qnil;
|
|
986 }
|
70
|
987
|
213
|
988 DEFUN ("ccl-execute-on-string", Fccl_execute_on_string, 3, 4, 0, /*
|
|
989 Execute CCL-PROGRAM with initial STATUS on STRING.
|
|
990 CCL-PROGRAM is a compiled code generated by `ccl-compile'.
|
|
991 Read buffer is set to STRING, and write buffer is allocated automatically.
|
|
992 STATUS is a vector of [R0 R1 ... R7 IC], where
|
|
993 R0..R7 are initial values of corresponding registers,
|
|
994 IC is the instruction counter specifying from where to start the program.
|
|
995 If R0..R7 are nil, they are initialized to 0.
|
|
996 If IC is nil, it is initialized to head of the CCL program.
|
|
997 Returns the contents of write buffer as a string,
|
|
998 and as side effect, STATUS is updated.
|
|
999 If optional 4th arg CONTINUE is non-nil, keep IC on read operation
|
|
1000 when read buffer is exausted, else, IC is always set to the end of
|
|
1001 CCL-PROGRAM on exit.
|
|
1002 */
|
|
1003 (ccl_prog, status, str, contin))
|
|
1004 {
|
|
1005 Lisp_Object val;
|
|
1006 struct ccl_program ccl;
|
|
1007 int i, produced;
|
|
1008 unsigned_char_dynarr *outbuf;
|
|
1009 struct gcpro gcpro1, gcpro2, gcpro3;
|
|
1010
|
|
1011 CHECK_VECTOR (ccl_prog);
|
70
|
1012 CHECK_VECTOR (status);
|
213
|
1013 if (XVECTOR_LENGTH (status) != 9)
|
241
|
1014 signal_simple_error ("Vector should be of length 9", status);
|
213
|
1015 CHECK_STRING (str);
|
|
1016 GCPRO3 (ccl_prog, status, str);
|
70
|
1017
|
213
|
1018 setup_ccl_program (&ccl, ccl_prog);
|
70
|
1019 for (i = 0; i < 8; i++)
|
|
1020 {
|
213
|
1021 if (NILP (XVECTOR_DATA (status)[i]))
|
|
1022 XSETINT (XVECTOR_DATA (status)[i], 0);
|
|
1023 if (INTP (XVECTOR_DATA (status)[i]))
|
|
1024 ccl.reg[i] = XINT (XVECTOR_DATA (status)[i]);
|
|
1025 }
|
|
1026 if (INTP (XVECTOR_DATA (status)[8]))
|
|
1027 {
|
|
1028 i = XINT (XVECTOR_DATA (status)[8]);
|
|
1029 if (ccl.ic < i && i < ccl.size)
|
|
1030 ccl.ic = i;
|
|
1031 }
|
|
1032 outbuf = Dynarr_new (unsigned_char);
|
|
1033 ccl.last_block = NILP (contin);
|
|
1034 produced = ccl_driver (&ccl, XSTRING_DATA (str), outbuf,
|
|
1035 XSTRING_LENGTH (str), (int *)0);
|
|
1036 for (i = 0; i < 8; i++)
|
|
1037 XVECTOR_DATA (status)[i] = make_int(ccl.reg[i]);
|
|
1038 XSETINT (XVECTOR_DATA (status)[8], ccl.ic);
|
|
1039 UNGCPRO;
|
|
1040
|
|
1041 val = make_string (Dynarr_atp (outbuf, 0), produced);
|
241
|
1042 Dynarr_free (outbuf);
|
213
|
1043 QUIT;
|
|
1044 if (ccl.status != CCL_STAT_SUCCESS
|
|
1045 && ccl.status != CCL_STAT_SUSPEND)
|
|
1046 error ("Error in CCL program at %dth code", ccl.ic);
|
|
1047
|
|
1048 return val;
|
|
1049 }
|
|
1050
|
|
1051 DEFUN ("register-ccl-program", Fregister_ccl_program, 2, 2, 0, /*
|
|
1052 Register CCL program PROGRAM of NAME in `ccl-program-table'.
|
|
1053 PROGRAM should be a compiled code of CCL program, or nil.
|
|
1054 Return index number of the registered CCL program.
|
|
1055 */
|
|
1056 (name, ccl_prog))
|
|
1057 {
|
|
1058 int len = XVECTOR_LENGTH (Vccl_program_table);
|
|
1059 int i;
|
|
1060
|
|
1061 CHECK_SYMBOL (name);
|
|
1062 if (!NILP (ccl_prog))
|
|
1063 CHECK_VECTOR (ccl_prog);
|
272
|
1064
|
213
|
1065 for (i = 0; i < len; i++)
|
|
1066 {
|
|
1067 Lisp_Object slot = XVECTOR_DATA (Vccl_program_table)[i];
|
|
1068
|
|
1069 if (!CONSP (slot))
|
|
1070 break;
|
|
1071
|
|
1072 if (EQ (name, XCAR (slot)))
|
70
|
1073 {
|
213
|
1074 XCDR (slot) = ccl_prog;
|
|
1075 return make_int (i);
|
70
|
1076 }
|
|
1077 }
|
|
1078
|
213
|
1079 if (i == len)
|
|
1080 {
|
|
1081 Lisp_Object new_table = Fmake_vector (make_int (len * 2), Qnil);
|
|
1082 int j;
|
70
|
1083
|
213
|
1084 for (j = 0; j < len; j++)
|
|
1085 XVECTOR_DATA (new_table)[j]
|
|
1086 = XVECTOR_DATA (Vccl_program_table)[j];
|
|
1087 Vccl_program_table = new_table;
|
|
1088 }
|
70
|
1089
|
213
|
1090 XVECTOR_DATA (Vccl_program_table)[i] = Fcons (name, ccl_prog);
|
|
1091 return make_int (i);
|
70
|
1092 }
|
|
1093
|
|
1094 void
|
|
1095 syms_of_mule_ccl (void)
|
|
1096 {
|
213
|
1097 staticpro (&Vccl_program_table);
|
|
1098 Vccl_program_table = Fmake_vector (make_int (32), Qnil);
|
|
1099
|
|
1100 DEFVAR_LISP ("font-ccl-encoder-alist", &Vfont_ccl_encoder_alist /*
|
|
1101 Alist of fontname patterns vs corresponding CCL program.
|
|
1102 Each element looks like (REGEXP . CCL-CODE),
|
|
1103 where CCL-CODE is a compiled CCL program.
|
|
1104 When a font whose name matches REGEXP is used for displaying a character,
|
|
1105 CCL-CODE is executed to calculate the code point in the font
|
|
1106 from the charset number and position code(s) of the character which are set
|
|
1107 in CCL registers R0, R1, and R2 before the execution.
|
|
1108 The code point in the font is set in CCL registers R1 and R2
|
|
1109 when the execution terminated.
|
|
1110 If the font is single-byte font, the register R2 is not used.
|
|
1111 */ );
|
|
1112 Vfont_ccl_encoder_alist = Qnil;
|
|
1113
|
|
1114 DEFSUBR (Fccl_execute);
|
|
1115 DEFSUBR (Fccl_execute_on_string);
|
|
1116 DEFSUBR (Fregister_ccl_program);
|
70
|
1117 }
|
|
1118
|
213
|
1119 #endif /* emacs */
|