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"
|
70
|
33 #include "mule-coding.h"
|
|
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;
|
|
533 int code, field1, field2;
|
|
534 Lisp_Object *ccl_prog = ccl->prog;
|
|
535 unsigned char *src = source, *src_end = src + src_bytes;
|
|
536 int jump_address;
|
|
537 int i, j, op;
|
|
538 int stack_idx = 0;
|
|
539 /* For the moment, we only support depth 256 of stack. */
|
|
540 struct ccl_prog_stack ccl_prog_stack_struct[256];
|
70
|
541
|
213
|
542 if (ic >= ccl->eof_ic)
|
70
|
543 ic = CCL_HEADER_MAIN;
|
|
544
|
213
|
545 #ifdef CCL_DEBUG
|
|
546 ccl_backtrace_idx = 0;
|
|
547 #endif
|
70
|
548
|
213
|
549 for (;;)
|
70
|
550 {
|
213
|
551 #ifdef CCL_DEBUG
|
|
552 ccl_backtrace_table[ccl_backtrace_idx++] = ic;
|
|
553 if (ccl_backtrace_idx >= CCL_DEBUG_BACKTRACE_LEN)
|
|
554 ccl_backtrace_idx = 0;
|
|
555 ccl_backtrace_table[ccl_backtrace_idx] = 0;
|
|
556 #endif
|
70
|
557
|
213
|
558 if (!NILP (Vquit_flag) && NILP (Vinhibit_quit))
|
70
|
559 {
|
213
|
560 /* We can't just signal Qquit, instead break the loop as if
|
|
561 the whole data is processed. Don't reset Vquit_flag, it
|
|
562 must be handled later at a safer place. */
|
|
563 if (consumed)
|
|
564 src = source + src_bytes;
|
|
565 ccl->status = CCL_STAT_QUIT;
|
|
566 break;
|
|
567 }
|
|
568
|
|
569 code = XINT (ccl_prog[ic]); ic++;
|
|
570 field1 = code >> 8;
|
|
571 field2 = (code & 0xFF) >> 5;
|
|
572
|
|
573 #define rrr field2
|
|
574 #define RRR (field1 & 7)
|
|
575 #define Rrr ((field1 >> 3) & 7)
|
|
576 #define ADDR field1
|
|
577
|
|
578 switch (code & 0x1F)
|
|
579 {
|
|
580 case CCL_SetRegister: /* 00000000000000000RRRrrrXXXXX */
|
|
581 reg[rrr] = reg[RRR];
|
|
582 break;
|
|
583
|
|
584 case CCL_SetShortConst: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
|
|
585 reg[rrr] = field1;
|
|
586 break;
|
|
587
|
|
588 case CCL_SetConst: /* 00000000000000000000rrrXXXXX */
|
|
589 reg[rrr] = XINT (ccl_prog[ic]);
|
|
590 ic++;
|
|
591 break;
|
|
592
|
|
593 case CCL_SetArray: /* CCCCCCCCCCCCCCCCCCCCRRRrrrXXXXX */
|
|
594 i = reg[RRR];
|
|
595 j = field1 >> 3;
|
|
596 if ((unsigned int) i < j)
|
|
597 reg[rrr] = XINT (ccl_prog[ic + i]);
|
|
598 ic += j;
|
|
599 break;
|
|
600
|
|
601 case CCL_Jump: /* A--D--D--R--E--S--S-000XXXXX */
|
|
602 ic += ADDR;
|
|
603 break;
|
|
604
|
|
605 case CCL_JumpCond: /* A--D--D--R--E--S--S-rrrXXXXX */
|
70
|
606 if (!reg[rrr])
|
213
|
607 ic += ADDR;
|
|
608 break;
|
|
609
|
|
610 case CCL_WriteRegisterJump: /* A--D--D--R--E--S--S-rrrXXXXX */
|
|
611 i = reg[rrr];
|
|
612 CCL_WRITE_CHAR (i);
|
|
613 ic += ADDR;
|
|
614 break;
|
|
615
|
|
616 case CCL_WriteRegisterReadJump: /* A--D--D--R--E--S--S-rrrXXXXX */
|
|
617 i = reg[rrr];
|
|
618 CCL_WRITE_CHAR (i);
|
|
619 ic++;
|
70
|
620 CCL_READ_CHAR (reg[rrr]);
|
213
|
621 ic += ADDR - 1;
|
|
622 break;
|
|
623
|
|
624 case CCL_WriteConstJump: /* A--D--D--R--E--S--S-000XXXXX */
|
|
625 i = XINT (ccl_prog[ic]);
|
|
626 CCL_WRITE_CHAR (i);
|
|
627 ic += ADDR;
|
|
628 break;
|
|
629
|
|
630 case CCL_WriteConstReadJump: /* A--D--D--R--E--S--S-rrrXXXXX */
|
|
631 i = XINT (ccl_prog[ic]);
|
|
632 CCL_WRITE_CHAR (i);
|
|
633 ic++;
|
70
|
634 CCL_READ_CHAR (reg[rrr]);
|
213
|
635 ic += ADDR - 1;
|
|
636 break;
|
|
637
|
|
638 case CCL_WriteStringJump: /* A--D--D--R--E--S--S-000XXXXX */
|
|
639 j = XINT (ccl_prog[ic]);
|
|
640 ic++;
|
|
641 CCL_WRITE_STRING (j);
|
|
642 ic += ADDR - 1;
|
|
643 break;
|
|
644
|
|
645 case CCL_WriteArrayReadJump: /* A--D--D--R--E--S--S-rrrXXXXX */
|
|
646 i = reg[rrr];
|
|
647 j = XINT (ccl_prog[ic]);
|
|
648 if ((unsigned int) i < j)
|
70
|
649 {
|
213
|
650 i = XINT (ccl_prog[ic + 1 + i]);
|
|
651 CCL_WRITE_CHAR (i);
|
70
|
652 }
|
213
|
653 ic += j + 2;
|
70
|
654 CCL_READ_CHAR (reg[rrr]);
|
213
|
655 ic += ADDR - (j + 2);
|
|
656 break;
|
|
657
|
|
658 case CCL_ReadJump: /* A--D--D--R--E--S--S-rrrYYYYY */
|
70
|
659 CCL_READ_CHAR (reg[rrr]);
|
213
|
660 ic += ADDR;
|
|
661 break;
|
|
662
|
|
663 case CCL_ReadBranch: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
|
70
|
664 CCL_READ_CHAR (reg[rrr]);
|
213
|
665 /* fall through ... */
|
|
666 case CCL_Branch: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
|
|
667 if ((unsigned int) reg[rrr] < field1)
|
|
668 ic += XINT (ccl_prog[ic + reg[rrr]]);
|
|
669 else
|
|
670 ic += XINT (ccl_prog[ic + field1]);
|
|
671 break;
|
|
672
|
|
673 case CCL_ReadRegister: /* CCCCCCCCCCCCCCCCCCCCrrXXXXX */
|
|
674 while (1)
|
|
675 {
|
|
676 CCL_READ_CHAR (reg[rrr]);
|
|
677 if (!field1) break;
|
|
678 code = XINT (ccl_prog[ic]); ic++;
|
|
679 field1 = code >> 8;
|
|
680 field2 = (code & 0xFF) >> 5;
|
|
681 }
|
|
682 break;
|
|
683
|
|
684 case CCL_WriteExprConst: /* 1:00000OPERATION000RRR000XXXXX */
|
|
685 rrr = 7;
|
|
686 i = reg[RRR];
|
|
687 j = XINT (ccl_prog[ic]);
|
|
688 op = field1 >> 6;
|
|
689 ic++;
|
|
690 goto ccl_set_expr;
|
|
691
|
|
692 case CCL_WriteRegister: /* CCCCCCCCCCCCCCCCCCCrrrXXXXX */
|
|
693 while (1)
|
|
694 {
|
|
695 i = reg[rrr];
|
|
696 CCL_WRITE_CHAR (i);
|
|
697 if (!field1) break;
|
|
698 code = XINT (ccl_prog[ic]); ic++;
|
|
699 field1 = code >> 8;
|
|
700 field2 = (code & 0xFF) >> 5;
|
|
701 }
|
|
702 break;
|
|
703
|
|
704 case CCL_WriteExprRegister: /* 1:00000OPERATIONRrrRRR000XXXXX */
|
|
705 rrr = 7;
|
|
706 i = reg[RRR];
|
|
707 j = reg[Rrr];
|
|
708 op = field1 >> 6;
|
|
709 goto ccl_set_expr;
|
|
710
|
|
711 case CCL_Call: /* CCCCCCCCCCCCCCCCCCCC000XXXXX */
|
|
712 {
|
|
713 Lisp_Object slot;
|
|
714
|
|
715 if (stack_idx >= 256
|
|
716 || field1 < 0
|
|
717 || field1 >= XVECTOR_LENGTH (Vccl_program_table)
|
|
718 || (slot = XVECTOR_DATA (Vccl_program_table)[field1],
|
|
719 !CONSP (slot))
|
|
720 || !VECTORP (XCDR (slot)))
|
|
721 {
|
|
722 if (stack_idx > 0)
|
|
723 {
|
|
724 ccl_prog = ccl_prog_stack_struct[0].ccl_prog;
|
|
725 ic = ccl_prog_stack_struct[0].ic;
|
|
726 }
|
|
727 CCL_INVALID_CMD;
|
|
728 }
|
|
729
|
|
730 ccl_prog_stack_struct[stack_idx].ccl_prog = ccl_prog;
|
|
731 ccl_prog_stack_struct[stack_idx].ic = ic;
|
|
732 stack_idx++;
|
|
733 ccl_prog = XVECTOR_DATA (XCDR (slot));
|
|
734 ic = CCL_HEADER_MAIN;
|
|
735 }
|
|
736 break;
|
|
737
|
|
738 case CCL_WriteConstString: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
|
|
739 if (!rrr)
|
|
740 CCL_WRITE_CHAR (field1);
|
|
741 else
|
|
742 {
|
|
743 CCL_WRITE_STRING (field1);
|
|
744 ic += (field1 + 2) / 3;
|
|
745 }
|
|
746 break;
|
|
747
|
|
748 case CCL_WriteArray: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
|
|
749 i = reg[rrr];
|
|
750 if ((unsigned int) i < field1)
|
|
751 {
|
|
752 j = XINT (ccl_prog[ic + i]);
|
|
753 CCL_WRITE_CHAR (j);
|
|
754 }
|
|
755 ic += field1;
|
|
756 break;
|
|
757
|
|
758 case CCL_End: /* 0000000000000000000000XXXXX */
|
|
759 if (stack_idx-- > 0)
|
|
760 {
|
|
761 ccl_prog = ccl_prog_stack_struct[stack_idx].ccl_prog;
|
|
762 ic = ccl_prog_stack_struct[stack_idx].ic;
|
|
763 break;
|
|
764 }
|
70
|
765 CCL_SUCCESS;
|
213
|
766
|
|
767 case CCL_ExprSelfConst: /* 00000OPERATION000000rrrXXXXX */
|
|
768 i = XINT (ccl_prog[ic]);
|
|
769 ic++;
|
|
770 op = field1 >> 6;
|
|
771 goto ccl_expr_self;
|
|
772
|
|
773 case CCL_ExprSelfReg: /* 00000OPERATION000RRRrrrXXXXX */
|
|
774 i = reg[RRR];
|
|
775 op = field1 >> 6;
|
|
776
|
|
777 ccl_expr_self:
|
70
|
778 switch (op)
|
|
779 {
|
213
|
780 case CCL_PLUS: reg[rrr] += i; break;
|
|
781 case CCL_MINUS: reg[rrr] -= i; break;
|
|
782 case CCL_MUL: reg[rrr] *= i; break;
|
|
783 case CCL_DIV: reg[rrr] /= i; break;
|
|
784 case CCL_MOD: reg[rrr] %= i; break;
|
|
785 case CCL_AND: reg[rrr] &= i; break;
|
|
786 case CCL_OR: reg[rrr] |= i; break;
|
|
787 case CCL_XOR: reg[rrr] ^= i; break;
|
|
788 case CCL_LSH: reg[rrr] <<= i; break;
|
|
789 case CCL_RSH: reg[rrr] >>= i; break;
|
|
790 case CCL_LSH8: reg[rrr] <<= 8; reg[rrr] |= i; break;
|
|
791 case CCL_RSH8: reg[7] = reg[rrr] & 0xFF; reg[rrr] >>= 8; break;
|
|
792 case CCL_DIVMOD: reg[7] = reg[rrr] % i; reg[rrr] /= i; break;
|
|
793 case CCL_LS: reg[rrr] = reg[rrr] < i; break;
|
|
794 case CCL_GT: reg[rrr] = reg[rrr] > i; break;
|
|
795 case CCL_EQ: reg[rrr] = reg[rrr] == i; break;
|
|
796 case CCL_LE: reg[rrr] = reg[rrr] <= i; break;
|
|
797 case CCL_GE: reg[rrr] = reg[rrr] >= i; break;
|
|
798 case CCL_NE: reg[rrr] = reg[rrr] != i; break;
|
70
|
799 default: CCL_INVALID_CMD;
|
|
800 }
|
213
|
801 break;
|
|
802
|
|
803 case CCL_SetExprConst: /* 00000OPERATION000RRRrrrXXXXX */
|
|
804 i = reg[RRR];
|
|
805 j = XINT (ccl_prog[ic]);
|
|
806 op = field1 >> 6;
|
|
807 jump_address = ++ic;
|
70
|
808 goto ccl_set_expr;
|
213
|
809
|
|
810 case CCL_SetExprReg: /* 00000OPERATIONRrrRRRrrrXXXXX */
|
|
811 i = reg[RRR];
|
|
812 j = reg[Rrr];
|
|
813 op = field1 >> 6;
|
|
814 jump_address = ic;
|
70
|
815 goto ccl_set_expr;
|
213
|
816
|
|
817 case CCL_ReadJumpCondExprConst: /* A--D--D--R--E--S--S-rrrXXXXX */
|
70
|
818 CCL_READ_CHAR (reg[rrr]);
|
213
|
819 case CCL_JumpCondExprConst: /* A--D--D--R--E--S--S-rrrXXXXX */
|
70
|
820 i = reg[rrr];
|
213
|
821 op = XINT (ccl_prog[ic]);
|
|
822 jump_address = ic++ + ADDR;
|
|
823 j = XINT (ccl_prog[ic]);
|
|
824 ic++;
|
70
|
825 rrr = 7;
|
|
826 goto ccl_set_expr;
|
213
|
827
|
|
828 case CCL_ReadJumpCondExprReg: /* A--D--D--R--E--S--S-rrrXXXXX */
|
70
|
829 CCL_READ_CHAR (reg[rrr]);
|
213
|
830 case CCL_JumpCondExprReg:
|
70
|
831 i = reg[rrr];
|
213
|
832 op = XINT (ccl_prog[ic]);
|
|
833 jump_address = ic++ + ADDR;
|
|
834 j = reg[XINT (ccl_prog[ic])];
|
|
835 ic++;
|
70
|
836 rrr = 7;
|
213
|
837
|
|
838 ccl_set_expr:
|
70
|
839 switch (op)
|
|
840 {
|
213
|
841 case CCL_PLUS: reg[rrr] = i + j; break;
|
|
842 case CCL_MINUS: reg[rrr] = i - j; break;
|
|
843 case CCL_MUL: reg[rrr] = i * j; break;
|
|
844 case CCL_DIV: reg[rrr] = i / j; break;
|
|
845 case CCL_MOD: reg[rrr] = i % j; break;
|
|
846 case CCL_AND: reg[rrr] = i & j; break;
|
|
847 case CCL_OR: reg[rrr] = i | j; break;
|
|
848 case CCL_XOR: reg[rrr] = i ^ j;; break;
|
|
849 case CCL_LSH: reg[rrr] = i << j; break;
|
|
850 case CCL_RSH: reg[rrr] = i >> j; break;
|
|
851 case CCL_LSH8: reg[rrr] = (i << 8) | j; break;
|
|
852 case CCL_RSH8: reg[rrr] = i >> 8; reg[7] = i & 0xFF; break;
|
|
853 case CCL_DIVMOD: reg[rrr] = i / j; reg[7] = i % j; break;
|
|
854 case CCL_LS: reg[rrr] = i < j; break;
|
|
855 case CCL_GT: reg[rrr] = i > j; break;
|
|
856 case CCL_EQ: reg[rrr] = i == j; break;
|
|
857 case CCL_LE: reg[rrr] = i <= j; break;
|
|
858 case CCL_GE: reg[rrr] = i >= j; break;
|
|
859 case CCL_NE: reg[rrr] = i != j; break;
|
|
860 case CCL_ENCODE_SJIS: ENCODE_SJIS (i, j, reg[rrr], reg[7]); break;
|
|
861 case CCL_DECODE_SJIS: DECODE_SJIS (i, j, reg[rrr], reg[7]); break;
|
70
|
862 default: CCL_INVALID_CMD;
|
|
863 }
|
213
|
864 code &= 0x1F;
|
|
865 if (code == CCL_WriteExprConst || code == CCL_WriteExprRegister)
|
|
866 {
|
|
867 i = reg[rrr];
|
|
868 CCL_WRITE_CHAR (i);
|
|
869 }
|
|
870 else if (!reg[rrr])
|
|
871 ic = jump_address;
|
|
872 break;
|
|
873
|
70
|
874 default:
|
|
875 CCL_INVALID_CMD;
|
|
876 }
|
|
877 }
|
|
878
|
213
|
879 ccl_error_handler:
|
|
880 if (destination)
|
70
|
881 {
|
213
|
882 /* We can insert an error message only if DESTINATION is
|
|
883 specified and we still have a room to store the message
|
|
884 there. */
|
|
885 char msg[256];
|
|
886
|
70
|
887 switch (ccl->status)
|
|
888 {
|
|
889 case CCL_STAT_INVALID_CMD:
|
213
|
890 sprintf(msg, "\nCCL: Invalid command %x (ccl_code = %x) at %d.",
|
|
891 code & 0x1F, code, ic);
|
|
892 #ifdef CCL_DEBUG
|
|
893 {
|
|
894 int i = ccl_backtrace_idx - 1;
|
|
895 int j;
|
|
896
|
|
897 Dynarr_add_many (destination, (unsigned char *) msg, strlen (msg));
|
|
898
|
|
899 for (j = 0; j < CCL_DEBUG_BACKTRACE_LEN; j++, i--)
|
|
900 {
|
|
901 if (i < 0) i = CCL_DEBUG_BACKTRACE_LEN - 1;
|
|
902 if (ccl_backtrace_table[i] == 0)
|
|
903 break;
|
|
904 sprintf(msg, " %d", ccl_backtrace_table[i]);
|
|
905 Dynarr_add_many (destination, (unsigned char *) msg, strlen (msg));
|
|
906 }
|
|
907 }
|
|
908 #endif
|
|
909 goto ccl_finish;
|
|
910
|
|
911 case CCL_STAT_QUIT:
|
|
912 sprintf(msg, "\nCCL: Quited.");
|
70
|
913 break;
|
213
|
914
|
70
|
915 default:
|
213
|
916 sprintf(msg, "\nCCL: Unknown error type (%d).", ccl->status);
|
70
|
917 }
|
213
|
918
|
|
919 Dynarr_add_many (destination, (unsigned char *) msg, strlen (msg));
|
70
|
920 }
|
|
921
|
213
|
922 ccl_finish:
|
70
|
923 ccl->ic = ic;
|
213
|
924 if (consumed) *consumed = src - source;
|
|
925 if (destination)
|
|
926 return Dynarr_length (destination);
|
70
|
927 else
|
|
928 return 0;
|
|
929 }
|
|
930
|
213
|
931 /* Setup fields of the structure pointed by CCL appropriately for the
|
|
932 execution of compiled CCL code in VEC (vector of integer). */
|
70
|
933 void
|
243
|
934 setup_ccl_program (struct ccl_program *ccl, Lisp_Object vec)
|
70
|
935 {
|
|
936 int i;
|
|
937
|
213
|
938 ccl->size = XVECTOR_LENGTH (vec);
|
|
939 ccl->prog = XVECTOR_DATA (vec);
|
|
940 ccl->ic = CCL_HEADER_MAIN;
|
|
941 ccl->eof_ic = XINT (XVECTOR_DATA (vec)[CCL_HEADER_EOF]);
|
|
942 ccl->buf_magnification = XINT (XVECTOR_DATA (vec)[CCL_HEADER_BUF_MAG]);
|
|
943 for (i = 0; i < 8; i++)
|
70
|
944 ccl->reg[i] = 0;
|
213
|
945 ccl->last_block = 0;
|
70
|
946 ccl->status = 0;
|
|
947 }
|
|
948
|
|
949 #ifdef emacs
|
|
950
|
213
|
951 DEFUN ("ccl-execute", Fccl_execute, 2, 2, 0, /*
|
|
952 Execute CCL-PROGRAM with registers initialized by REGISTERS.
|
|
953 CCL-PROGRAM is a compiled code generated by `ccl-compile',
|
|
954 no I/O commands should appear in the CCL program.
|
|
955 REGISTERS is a vector of [R0 R1 ... R7] where RN is an initial value
|
|
956 of Nth register.
|
|
957 As side effect, each element of REGISTER holds the value of
|
|
958 corresponding register after the execution.
|
|
959 */
|
|
960 (ccl_prog, reg))
|
70
|
961 {
|
213
|
962 struct ccl_program ccl;
|
70
|
963 int i;
|
213
|
964
|
|
965 CHECK_VECTOR (ccl_prog);
|
|
966 CHECK_VECTOR (reg);
|
|
967 if (XVECTOR_LENGTH (reg) != 8)
|
241
|
968 signal_simple_error ("Vector should be of length 8", reg);
|
213
|
969
|
|
970 setup_ccl_program (&ccl, ccl_prog);
|
|
971 for (i = 0; i < 8; i++)
|
|
972 ccl.reg[i] = (INTP (XVECTOR_DATA (reg)[i])
|
|
973 ? XINT (XVECTOR_DATA (reg)[i])
|
|
974 : 0);
|
|
975
|
|
976 ccl_driver (&ccl, (CONST unsigned char *)0, (unsigned_char_dynarr *)0,
|
|
977 0, (int *)0);
|
|
978 QUIT;
|
|
979 if (ccl.status != CCL_STAT_SUCCESS)
|
|
980 error ("Error in CCL program at %dth code", ccl.ic);
|
|
981
|
|
982 for (i = 0; i < 8; i++)
|
|
983 XSETINT (XVECTOR_DATA (reg)[i], ccl.reg[i]);
|
|
984 return Qnil;
|
|
985 }
|
70
|
986
|
213
|
987 DEFUN ("ccl-execute-on-string", Fccl_execute_on_string, 3, 4, 0, /*
|
|
988 Execute CCL-PROGRAM with initial STATUS on STRING.
|
|
989 CCL-PROGRAM is a compiled code generated by `ccl-compile'.
|
|
990 Read buffer is set to STRING, and write buffer is allocated automatically.
|
|
991 STATUS is a vector of [R0 R1 ... R7 IC], where
|
|
992 R0..R7 are initial values of corresponding registers,
|
|
993 IC is the instruction counter specifying from where to start the program.
|
|
994 If R0..R7 are nil, they are initialized to 0.
|
|
995 If IC is nil, it is initialized to head of the CCL program.
|
|
996 Returns the contents of write buffer as a string,
|
|
997 and as side effect, STATUS is updated.
|
|
998 If optional 4th arg CONTINUE is non-nil, keep IC on read operation
|
|
999 when read buffer is exausted, else, IC is always set to the end of
|
|
1000 CCL-PROGRAM on exit.
|
|
1001 */
|
|
1002 (ccl_prog, status, str, contin))
|
|
1003 {
|
|
1004 Lisp_Object val;
|
|
1005 struct ccl_program ccl;
|
|
1006 int i, produced;
|
|
1007 unsigned_char_dynarr *outbuf;
|
|
1008 struct gcpro gcpro1, gcpro2, gcpro3;
|
|
1009
|
|
1010 CHECK_VECTOR (ccl_prog);
|
70
|
1011 CHECK_VECTOR (status);
|
213
|
1012 if (XVECTOR_LENGTH (status) != 9)
|
241
|
1013 signal_simple_error ("Vector should be of length 9", status);
|
213
|
1014 CHECK_STRING (str);
|
|
1015 GCPRO3 (ccl_prog, status, str);
|
70
|
1016
|
213
|
1017 setup_ccl_program (&ccl, ccl_prog);
|
70
|
1018 for (i = 0; i < 8; i++)
|
|
1019 {
|
213
|
1020 if (NILP (XVECTOR_DATA (status)[i]))
|
|
1021 XSETINT (XVECTOR_DATA (status)[i], 0);
|
|
1022 if (INTP (XVECTOR_DATA (status)[i]))
|
|
1023 ccl.reg[i] = XINT (XVECTOR_DATA (status)[i]);
|
|
1024 }
|
|
1025 if (INTP (XVECTOR_DATA (status)[8]))
|
|
1026 {
|
|
1027 i = XINT (XVECTOR_DATA (status)[8]);
|
|
1028 if (ccl.ic < i && i < ccl.size)
|
|
1029 ccl.ic = i;
|
|
1030 }
|
|
1031 outbuf = Dynarr_new (unsigned_char);
|
|
1032 ccl.last_block = NILP (contin);
|
|
1033 produced = ccl_driver (&ccl, XSTRING_DATA (str), outbuf,
|
|
1034 XSTRING_LENGTH (str), (int *)0);
|
|
1035 for (i = 0; i < 8; i++)
|
|
1036 XVECTOR_DATA (status)[i] = make_int(ccl.reg[i]);
|
|
1037 XSETINT (XVECTOR_DATA (status)[8], ccl.ic);
|
|
1038 UNGCPRO;
|
|
1039
|
|
1040 val = make_string (Dynarr_atp (outbuf, 0), produced);
|
241
|
1041 Dynarr_free (outbuf);
|
213
|
1042 QUIT;
|
|
1043 if (ccl.status != CCL_STAT_SUCCESS
|
|
1044 && ccl.status != CCL_STAT_SUSPEND)
|
|
1045 error ("Error in CCL program at %dth code", ccl.ic);
|
|
1046
|
|
1047 return val;
|
|
1048 }
|
|
1049
|
|
1050 DEFUN ("register-ccl-program", Fregister_ccl_program, 2, 2, 0, /*
|
|
1051 Register CCL program PROGRAM of NAME in `ccl-program-table'.
|
|
1052 PROGRAM should be a compiled code of CCL program, or nil.
|
|
1053 Return index number of the registered CCL program.
|
|
1054 */
|
|
1055 (name, ccl_prog))
|
|
1056 {
|
|
1057 int len = XVECTOR_LENGTH (Vccl_program_table);
|
|
1058 int i;
|
|
1059
|
|
1060 CHECK_SYMBOL (name);
|
|
1061 if (!NILP (ccl_prog))
|
|
1062 CHECK_VECTOR (ccl_prog);
|
|
1063
|
|
1064 for (i = 0; i < len; i++)
|
|
1065 {
|
|
1066 Lisp_Object slot = XVECTOR_DATA (Vccl_program_table)[i];
|
|
1067
|
|
1068 if (!CONSP (slot))
|
|
1069 break;
|
|
1070
|
|
1071 if (EQ (name, XCAR (slot)))
|
70
|
1072 {
|
213
|
1073 XCDR (slot) = ccl_prog;
|
|
1074 return make_int (i);
|
70
|
1075 }
|
|
1076 }
|
|
1077
|
213
|
1078 if (i == len)
|
|
1079 {
|
|
1080 Lisp_Object new_table = Fmake_vector (make_int (len * 2), Qnil);
|
|
1081 int j;
|
70
|
1082
|
213
|
1083 for (j = 0; j < len; j++)
|
|
1084 XVECTOR_DATA (new_table)[j]
|
|
1085 = XVECTOR_DATA (Vccl_program_table)[j];
|
|
1086 Vccl_program_table = new_table;
|
|
1087 }
|
70
|
1088
|
213
|
1089 XVECTOR_DATA (Vccl_program_table)[i] = Fcons (name, ccl_prog);
|
|
1090 return make_int (i);
|
70
|
1091 }
|
|
1092
|
|
1093 void
|
|
1094 syms_of_mule_ccl (void)
|
|
1095 {
|
213
|
1096 staticpro (&Vccl_program_table);
|
|
1097 Vccl_program_table = Fmake_vector (make_int (32), Qnil);
|
|
1098
|
|
1099 DEFVAR_LISP ("font-ccl-encoder-alist", &Vfont_ccl_encoder_alist /*
|
|
1100 Alist of fontname patterns vs corresponding CCL program.
|
|
1101 Each element looks like (REGEXP . CCL-CODE),
|
|
1102 where CCL-CODE is a compiled CCL program.
|
|
1103 When a font whose name matches REGEXP is used for displaying a character,
|
|
1104 CCL-CODE is executed to calculate the code point in the font
|
|
1105 from the charset number and position code(s) of the character which are set
|
|
1106 in CCL registers R0, R1, and R2 before the execution.
|
|
1107 The code point in the font is set in CCL registers R1 and R2
|
|
1108 when the execution terminated.
|
|
1109 If the font is single-byte font, the register R2 is not used.
|
|
1110 */ );
|
|
1111 Vfont_ccl_encoder_alist = Qnil;
|
|
1112
|
|
1113 DEFSUBR (Fccl_execute);
|
|
1114 DEFSUBR (Fccl_execute_on_string);
|
|
1115 DEFSUBR (Fregister_ccl_program);
|
70
|
1116 }
|
|
1117
|
213
|
1118 #endif /* emacs */
|