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