* ccl.c: Improve comment. (Bug#8751)
[bpt/emacs.git] / src / ccl.c
CommitLineData
4ed46869 1/* CCL (Code Conversion Language) interpreter.
73b0cd50 2 Copyright (C) 2001-2011 Free Software Foundation, Inc.
7976eda0 3 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5df4f04c 4 2005, 2006, 2007, 2008, 2009, 2010, 2011
ce03bf76
KH
5 National Institute of Advanced Industrial Science and Technology (AIST)
6 Registration Number H14PRO021
8f924df7 7 Copyright (C) 2003
c10842ea
KH
8 National Institute of Advanced Industrial Science and Technology (AIST)
9 Registration Number H13PRO009
4ed46869 10
369314dc
KH
11This file is part of GNU Emacs.
12
9ec0b715 13GNU Emacs is free software: you can redistribute it and/or modify
369314dc 14it under the terms of the GNU General Public License as published by
9ec0b715
GM
15the Free Software Foundation, either version 3 of the License, or
16(at your option) any later version.
4ed46869 17
369314dc
KH
18GNU Emacs is distributed in the hope that it will be useful,
19but WITHOUT ANY WARRANTY; without even the implied warranty of
20MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21GNU General Public License for more details.
4ed46869 22
369314dc 23You should have received a copy of the GNU General Public License
9ec0b715 24along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
4ed46869 25
4ed46869 26#include <config.h>
dfcf069d 27
68c45bf0 28#include <stdio.h>
d7306fe6 29#include <setjmp.h>
61838453 30#include <limits.h>
68c45bf0 31
4ed46869 32#include "lisp.h"
c10842ea 33#include "character.h"
4ed46869
KH
34#include "charset.h"
35#include "ccl.h"
36#include "coding.h"
37
c10842ea
KH
38Lisp_Object Qccl, Qcclp;
39
737b5223 40/* This symbol is a property which associates with ccl program vector.
6ae21908 41 Ex: (get 'ccl-big5-encoder 'ccl-program) returns ccl program vector. */
955cbe7b 42static Lisp_Object Qccl_program;
e34b1164 43
8146262a
KH
44/* These symbols are properties which associate with code conversion
45 map and their ID respectively. */
955cbe7b
PE
46static Lisp_Object Qcode_conversion_map;
47static Lisp_Object Qcode_conversion_map_id;
e34b1164 48
6ae21908
KH
49/* Symbols of ccl program have this property, a value of the property
50 is an index for Vccl_protram_table. */
955cbe7b 51static Lisp_Object Qccl_program_idx;
6ae21908 52
5232fa7b 53/* Table of registered CCL programs. Each element is a vector of
2a69c66e
KH
54 NAME, CCL_PROG, RESOLVEDP, and UPDATEDP, where NAME (symbol) is the
55 name of the program, CCL_PROG (vector) is the compiled code of the
56 program, RESOLVEDP (t or nil) is the flag to tell if symbols in
57 CCL_PROG is already resolved to index numbers or not, UPDATEDP (t
58 or nil) is the flat to tell if the CCL program is updated after it
59 was once used. */
127198fd 60static Lisp_Object Vccl_program_table;
4ed46869 61
d80dc57e
DL
62/* Return a hash table of id number ID. */
63#define GET_HASH_TABLE(id) \
64 (XHASH_TABLE (XCDR(XVECTOR(Vtranslation_hash_table_vector)->contents[(id)])))
d80dc57e 65
4ed46869
KH
66/* CCL (Code Conversion Language) is a simple language which has
67 operations on one input buffer, one output buffer, and 7 registers.
68 The syntax of CCL is described in `ccl.el'. Emacs Lisp function
69 `ccl-compile' compiles a CCL program and produces a CCL code which
70 is a vector of integers. The structure of this vector is as
71 follows: The 1st element: buffer-magnification, a factor for the
72 size of output buffer compared with the size of input buffer. The
73 2nd element: address of CCL code to be executed when encountered
74 with end of input stream. The 3rd and the remaining elements: CCL
75 codes. */
76
77/* Header of CCL compiled code */
78#define CCL_HEADER_BUF_MAG 0
79#define CCL_HEADER_EOF 1
80#define CCL_HEADER_MAIN 2
81
e003a292
PE
82/* CCL code is a sequence of 28-bit integers. Each contains a CCL
83 command and/or arguments in the following format:
4ed46869
KH
84
85 |----------------- integer (28-bit) ------------------|
86 |------- 17-bit ------|- 3-bit --|- 3-bit --|- 5-bit -|
87 |--constant argument--|-register-|-register-|-command-|
88 ccccccccccccccccc RRR rrr XXXXX
89 or
90 |------- relative address -------|-register-|-command-|
91 cccccccccccccccccccc rrr XXXXX
92 or
93 |------------- constant or other args ----------------|
94 cccccccccccccccccccccccccccc
95
b9627cfb
PE
96 where `cc...c' is a 17-bit, 20-bit, or 28-bit integer indicating a
97 constant value or a relative/absolute jump address, `RRR'
4ed46869
KH
98 and `rrr' are CCL register number, `XXXXX' is one of the following
99 CCL commands. */
100
30569699 101#define CCL_CODE_MAX ((1 << (28 - 1)) - 1)
e003a292 102#define CCL_CODE_MIN (-1 - CCL_CODE_MAX)
30569699 103
4ed46869
KH
104/* CCL commands
105
106 Each comment fields shows one or more lines for command syntax and
107 the following lines for semantics of the command. In semantics, IC
108 stands for Instruction Counter. */
109
110#define CCL_SetRegister 0x00 /* Set register a register value:
111 1:00000000000000000RRRrrrXXXXX
112 ------------------------------
113 reg[rrr] = reg[RRR];
114 */
115
116#define CCL_SetShortConst 0x01 /* Set register a short constant value:
117 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
118 ------------------------------
119 reg[rrr] = CCCCCCCCCCCCCCCCCCC;
120 */
121
122#define CCL_SetConst 0x02 /* Set register a constant value:
123 1:00000000000000000000rrrXXXXX
124 2:CONSTANT
125 ------------------------------
126 reg[rrr] = CONSTANT;
127 IC++;
128 */
129
130#define CCL_SetArray 0x03 /* Set register an element of array:
131 1:CCCCCCCCCCCCCCCCCRRRrrrXXXXX
132 2:ELEMENT[0]
133 3:ELEMENT[1]
134 ...
135 ------------------------------
136 if (0 <= reg[RRR] < CC..C)
137 reg[rrr] = ELEMENT[reg[RRR]];
138 IC += CC..C;
139 */
140
141#define CCL_Jump 0x04 /* Jump:
142 1:A--D--D--R--E--S--S-000XXXXX
143 ------------------------------
144 IC += ADDRESS;
145 */
146
147/* Note: If CC..C is greater than 0, the second code is omitted. */
148
149#define CCL_JumpCond 0x05 /* Jump conditional:
150 1:A--D--D--R--E--S--S-rrrXXXXX
151 ------------------------------
152 if (!reg[rrr])
153 IC += ADDRESS;
154 */
155
156
157#define CCL_WriteRegisterJump 0x06 /* Write register and jump:
158 1:A--D--D--R--E--S--S-rrrXXXXX
159 ------------------------------
160 write (reg[rrr]);
161 IC += ADDRESS;
162 */
163
164#define CCL_WriteRegisterReadJump 0x07 /* Write register, read, and jump:
165 1:A--D--D--R--E--S--S-rrrXXXXX
166 2:A--D--D--R--E--S--S-rrrYYYYY
167 -----------------------------
168 write (reg[rrr]);
169 IC++;
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_WriteConstJump 0x08 /* Write constant and jump:
177 1:A--D--D--R--E--S--S-000XXXXX
178 2:CONST
179 ------------------------------
180 write (CONST);
181 IC += ADDRESS;
182 */
183
184#define CCL_WriteConstReadJump 0x09 /* Write constant, read, and jump:
185 1:A--D--D--R--E--S--S-rrrXXXXX
186 2:CONST
187 3:A--D--D--R--E--S--S-rrrYYYYY
188 -----------------------------
189 write (CONST);
190 IC += 2;
191 read (reg[rrr]);
192 IC += ADDRESS;
193 */
194/* Note: If read is suspended, the resumed execution starts from the
195 second code (YYYYY == CCL_ReadJump). */
196
197#define CCL_WriteStringJump 0x0A /* Write string and jump:
198 1:A--D--D--R--E--S--S-000XXXXX
199 2:LENGTH
c6589bbd 200 3:000MSTRIN[0]STRIN[1]STRIN[2]
4ed46869
KH
201 ...
202 ------------------------------
c6589bbd
KH
203 if (M)
204 write_multibyte_string (STRING, LENGTH);
205 else
206 write_string (STRING, LENGTH);
4ed46869
KH
207 IC += ADDRESS;
208 */
209
210#define CCL_WriteArrayReadJump 0x0B /* Write an array element, read, and jump:
211 1:A--D--D--R--E--S--S-rrrXXXXX
212 2:LENGTH
213 3:ELEMENET[0]
214 4:ELEMENET[1]
215 ...
216 N:A--D--D--R--E--S--S-rrrYYYYY
217 ------------------------------
218 if (0 <= reg[rrr] < LENGTH)
219 write (ELEMENT[reg[rrr]]);
220 IC += LENGTH + 2; (... pointing at N+1)
221 read (reg[rrr]);
222 IC += ADDRESS;
223 */
224/* Note: If read is suspended, the resumed execution starts from the
887bfbd7 225 Nth code (YYYYY == CCL_ReadJump). */
4ed46869
KH
226
227#define CCL_ReadJump 0x0C /* Read and jump:
228 1:A--D--D--R--E--S--S-rrrYYYYY
229 -----------------------------
230 read (reg[rrr]);
231 IC += ADDRESS;
232 */
233
234#define CCL_Branch 0x0D /* Jump by branch table:
235 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
236 2:A--D--D--R--E-S-S[0]000XXXXX
237 3:A--D--D--R--E-S-S[1]000XXXXX
238 ...
239 ------------------------------
240 if (0 <= reg[rrr] < CC..C)
241 IC += ADDRESS[reg[rrr]];
242 else
243 IC += ADDRESS[CC..C];
244 */
245
246#define CCL_ReadRegister 0x0E /* Read bytes into registers:
247 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
248 2:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
249 ...
250 ------------------------------
251 while (CCC--)
252 read (reg[rrr]);
253 */
254
255#define CCL_WriteExprConst 0x0F /* write result of expression:
256 1:00000OPERATION000RRR000XXXXX
257 2:CONSTANT
258 ------------------------------
259 write (reg[RRR] OPERATION CONSTANT);
260 IC++;
261 */
262
263/* Note: If the Nth read is suspended, the resumed execution starts
264 from the Nth code. */
265
266#define CCL_ReadBranch 0x10 /* Read one byte into a register,
267 and jump by branch table:
268 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
269 2:A--D--D--R--E-S-S[0]000XXXXX
270 3:A--D--D--R--E-S-S[1]000XXXXX
271 ...
272 ------------------------------
273 read (read[rrr]);
274 if (0 <= reg[rrr] < CC..C)
275 IC += ADDRESS[reg[rrr]];
276 else
277 IC += ADDRESS[CC..C];
278 */
279
280#define CCL_WriteRegister 0x11 /* Write registers:
281 1:CCCCCCCCCCCCCCCCCCCrrrXXXXX
282 2:CCCCCCCCCCCCCCCCCCCrrrXXXXX
283 ...
284 ------------------------------
285 while (CCC--)
286 write (reg[rrr]);
287 ...
288 */
289
290/* Note: If the Nth write is suspended, the resumed execution
291 starts from the Nth code. */
292
293#define CCL_WriteExprRegister 0x12 /* Write result of expression
294 1:00000OPERATIONRrrRRR000XXXXX
295 ------------------------------
296 write (reg[RRR] OPERATION reg[Rrr]);
297 */
298
e34b1164 299#define CCL_Call 0x13 /* Call the CCL program whose ID is
5232fa7b
KH
300 CC..C or cc..c.
301 1:CCCCCCCCCCCCCCCCCCCCFFFXXXXX
302 [2:00000000cccccccccccccccccccc]
4ed46869 303 ------------------------------
5232fa7b
KH
304 if (FFF)
305 call (cc..c)
306 IC++;
307 else
308 call (CC..C)
4ed46869
KH
309 */
310
311#define CCL_WriteConstString 0x14 /* Write a constant or a string:
312 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
c6589bbd 313 [2:000MSTRIN[0]STRIN[1]STRIN[2]]
4ed46869
KH
314 [...]
315 -----------------------------
316 if (!rrr)
317 write (CC..C)
318 else
c6589bbd
KH
319 if (M)
320 write_multibyte_string (STRING, CC..C);
321 else
322 write_string (STRING, CC..C);
4ed46869
KH
323 IC += (CC..C + 2) / 3;
324 */
325
326#define CCL_WriteArray 0x15 /* Write an element of array:
327 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
328 2:ELEMENT[0]
329 3:ELEMENT[1]
330 ...
331 ------------------------------
332 if (0 <= reg[rrr] < CC..C)
333 write (ELEMENT[reg[rrr]]);
334 IC += CC..C;
335 */
336
337#define CCL_End 0x16 /* Terminate:
338 1:00000000000000000000000XXXXX
339 ------------------------------
340 terminate ();
341 */
342
343/* The following two codes execute an assignment arithmetic/logical
344 operation. The form of the operation is like REG OP= OPERAND. */
345
346#define CCL_ExprSelfConst 0x17 /* REG OP= constant:
347 1:00000OPERATION000000rrrXXXXX
348 2:CONSTANT
349 ------------------------------
350 reg[rrr] OPERATION= CONSTANT;
351 */
352
353#define CCL_ExprSelfReg 0x18 /* REG1 OP= REG2:
354 1:00000OPERATION000RRRrrrXXXXX
355 ------------------------------
356 reg[rrr] OPERATION= reg[RRR];
357 */
358
359/* The following codes execute an arithmetic/logical operation. The
360 form of the operation is like REG_X = REG_Y OP OPERAND2. */
361
362#define CCL_SetExprConst 0x19 /* REG_X = REG_Y OP constant:
363 1:00000OPERATION000RRRrrrXXXXX
364 2:CONSTANT
365 ------------------------------
366 reg[rrr] = reg[RRR] OPERATION CONSTANT;
367 IC++;
368 */
369
370#define CCL_SetExprReg 0x1A /* REG1 = REG2 OP REG3:
371 1:00000OPERATIONRrrRRRrrrXXXXX
372 ------------------------------
373 reg[rrr] = reg[RRR] OPERATION reg[Rrr];
374 */
375
376#define CCL_JumpCondExprConst 0x1B /* Jump conditional according to
377 an operation on constant:
378 1:A--D--D--R--E--S--S-rrrXXXXX
379 2:OPERATION
380 3:CONSTANT
381 -----------------------------
382 reg[7] = reg[rrr] OPERATION CONSTANT;
383 if (!(reg[7]))
384 IC += ADDRESS;
385 else
386 IC += 2
387 */
388
389#define CCL_JumpCondExprReg 0x1C /* Jump conditional according to
390 an operation on register:
391 1:A--D--D--R--E--S--S-rrrXXXXX
392 2:OPERATION
393 3:RRR
394 -----------------------------
395 reg[7] = reg[rrr] OPERATION reg[RRR];
396 if (!reg[7])
397 IC += ADDRESS;
398 else
399 IC += 2;
400 */
401
402#define CCL_ReadJumpCondExprConst 0x1D /* Read and jump conditional according
403 to an operation on constant:
404 1:A--D--D--R--E--S--S-rrrXXXXX
405 2:OPERATION
406 3:CONSTANT
407 -----------------------------
408 read (reg[rrr]);
409 reg[7] = reg[rrr] OPERATION CONSTANT;
410 if (!reg[7])
411 IC += ADDRESS;
412 else
413 IC += 2;
414 */
415
416#define CCL_ReadJumpCondExprReg 0x1E /* Read and jump conditional according
417 to an operation on register:
418 1:A--D--D--R--E--S--S-rrrXXXXX
419 2:OPERATION
420 3:RRR
421 -----------------------------
422 read (reg[rrr]);
423 reg[7] = reg[rrr] OPERATION reg[RRR];
424 if (!reg[7])
425 IC += ADDRESS;
426 else
427 IC += 2;
428 */
429
450ed226 430#define CCL_Extension 0x1F /* Extended CCL code
4ed46869
KH
431 1:ExtendedCOMMNDRrrRRRrrrXXXXX
432 2:ARGUEMENT
433 3:...
434 ------------------------------
435 extended_command (rrr,RRR,Rrr,ARGS)
436 */
437
177c0ea7 438/*
6ae21908 439 Here after, Extended CCL Instructions.
e34b1164 440 Bit length of extended command is 14.
6ae21908 441 Therefore, the instruction code range is 0..16384(0x3fff).
e34b1164
KH
442 */
443
51e4f4a8 444/* Read a multibyte character.
6ae21908
KH
445 A code point is stored into reg[rrr]. A charset ID is stored into
446 reg[RRR]. */
447
448#define CCL_ReadMultibyteChar2 0x00 /* Read Multibyte Character
449 1:ExtendedCOMMNDRrrRRRrrrXXXXX */
450
451/* Write a multibyte character.
452 Write a character whose code point is reg[rrr] and the charset ID
453 is reg[RRR]. */
454
455#define CCL_WriteMultibyteChar2 0x01 /* Write Multibyte Character
456 1:ExtendedCOMMNDRrrRRRrrrXXXXX */
457
8146262a 458/* Translate a character whose code point is reg[rrr] and the charset
f967223b 459 ID is reg[RRR] by a translation table whose ID is reg[Rrr].
6ae21908 460
8146262a 461 A translated character is set in reg[rrr] (code point) and reg[RRR]
6ae21908
KH
462 (charset ID). */
463
8146262a 464#define CCL_TranslateCharacter 0x02 /* Translate a multibyte character
6ae21908
KH
465 1:ExtendedCOMMNDRrrRRRrrrXXXXX */
466
8146262a 467/* Translate a character whose code point is reg[rrr] and the charset
f967223b 468 ID is reg[RRR] by a translation table whose ID is ARGUMENT.
6ae21908 469
8146262a 470 A translated character is set in reg[rrr] (code point) and reg[RRR]
6ae21908
KH
471 (charset ID). */
472
8146262a
KH
473#define CCL_TranslateCharacterConstTbl 0x03 /* Translate a multibyte character
474 1:ExtendedCOMMNDRrrRRRrrrXXXXX
475 2:ARGUMENT(Translation Table ID)
476 */
6ae21908 477
8146262a
KH
478/* Iterate looking up MAPs for reg[rrr] starting from the Nth (N =
479 reg[RRR]) MAP until some value is found.
6ae21908 480
8146262a 481 Each MAP is a Lisp vector whose element is number, nil, t, or
6ae21908 482 lambda.
8146262a 483 If the element is nil, ignore the map and proceed to the next map.
6ae21908
KH
484 If the element is t or lambda, finish without changing reg[rrr].
485 If the element is a number, set reg[rrr] to the number and finish.
486
8146262a
KH
487 Detail of the map structure is descibed in the comment for
488 CCL_MapMultiple below. */
6ae21908 489
8146262a 490#define CCL_IterateMultipleMap 0x10 /* Iterate multiple maps
6ae21908 491 1:ExtendedCOMMNDXXXRRRrrrXXXXX
8146262a
KH
492 2:NUMBER of MAPs
493 3:MAP-ID1
494 4:MAP-ID2
6ae21908 495 ...
177c0ea7 496 */
6ae21908 497
8146262a
KH
498/* Map the code in reg[rrr] by MAPs starting from the Nth (N =
499 reg[RRR]) map.
6ae21908 500
9b27b20d 501 MAPs are supplied in the succeeding CCL codes as follows:
6ae21908 502
8146262a
KH
503 When CCL program gives this nested structure of map to this command:
504 ((MAP-ID11
505 MAP-ID12
506 (MAP-ID121 MAP-ID122 MAP-ID123)
507 MAP-ID13)
508 (MAP-ID21
509 (MAP-ID211 (MAP-ID2111) MAP-ID212)
510 MAP-ID22)),
6ae21908 511 the compiled CCL codes has this sequence:
8146262a 512 CCL_MapMultiple (CCL code of this command)
9b27b20d
KH
513 16 (total number of MAPs and SEPARATORs)
514 -7 (1st SEPARATOR)
8146262a
KH
515 MAP-ID11
516 MAP-ID12
9b27b20d 517 -3 (2nd SEPARATOR)
8146262a
KH
518 MAP-ID121
519 MAP-ID122
520 MAP-ID123
521 MAP-ID13
9b27b20d 522 -7 (3rd SEPARATOR)
8146262a 523 MAP-ID21
9b27b20d 524 -4 (4th SEPARATOR)
8146262a 525 MAP-ID211
9b27b20d 526 -1 (5th SEPARATOR)
8146262a
KH
527 MAP_ID2111
528 MAP-ID212
529 MAP-ID22
6ae21908 530
9b27b20d 531 A value of each SEPARATOR follows this rule:
8146262a
KH
532 MAP-SET := SEPARATOR [(MAP-ID | MAP-SET)]+
533 SEPARATOR := -(number of MAP-IDs and SEPARATORs in the MAP-SET)
6ae21908 534
8146262a 535 (*)....Nest level of MAP-SET must not be over than MAX_MAP_SET_LEVEL.
6ae21908 536
8146262a
KH
537 When some map fails to map (i.e. it doesn't have a value for
538 reg[rrr]), the mapping is treated as identity.
6ae21908 539
8146262a 540 The mapping is iterated for all maps in each map set (set of maps
9b27b20d
KH
541 separated by SEPARATOR) except in the case that lambda is
542 encountered. More precisely, the mapping proceeds as below:
543
544 At first, VAL0 is set to reg[rrr], and it is translated by the
545 first map to VAL1. Then, VAL1 is translated by the next map to
546 VAL2. This mapping is iterated until the last map is used. The
54fa5bc1
KH
547 result of the mapping is the last value of VAL?. When the mapping
548 process reached to the end of the map set, it moves to the next
549 map set. If the next does not exit, the mapping process terminates,
550 and regard the last value as a result.
9b27b20d
KH
551
552 But, when VALm is mapped to VALn and VALn is not a number, the
553 mapping proceed as below:
554
555 If VALn is nil, the lastest map is ignored and the mapping of VALm
556 proceed to the next map.
557
558 In VALn is t, VALm is reverted to reg[rrr] and the mapping of VALm
559 proceed to the next map.
560
54fa5bc1
KH
561 If VALn is lambda, move to the next map set like reaching to the
562 end of the current map set.
563
564 If VALn is a symbol, call the CCL program refered by it.
565 Then, use reg[rrr] as a mapped value except for -1, -2 and -3.
566 Such special values are regarded as nil, t, and lambda respectively.
6ae21908 567
8146262a 568 Each map is a Lisp vector of the following format (a) or (b):
6ae21908
KH
569 (a)......[STARTPOINT VAL1 VAL2 ...]
570 (b)......[t VAL STARTPOINT ENDPOINT],
571 where
8146262a 572 STARTPOINT is an offset to be used for indexing a map,
9b27b20d 573 ENDPOINT is a maximum index number of a map,
177c0ea7 574 VAL and VALn is a number, nil, t, or lambda.
6ae21908 575
8146262a
KH
576 Valid index range of a map of type (a) is:
577 STARTPOINT <= index < STARTPOINT + map_size - 1
578 Valid index range of a map of type (b) is:
9b27b20d 579 STARTPOINT <= index < ENDPOINT */
6ae21908 580
8146262a 581#define CCL_MapMultiple 0x11 /* Mapping by multiple code conversion maps
6ae21908
KH
582 1:ExtendedCOMMNDXXXRRRrrrXXXXX
583 2:N-2
584 3:SEPARATOR_1 (< 0)
8146262a
KH
585 4:MAP-ID_1
586 5:MAP-ID_2
6ae21908
KH
587 ...
588 M:SEPARATOR_x (< 0)
8146262a 589 M+1:MAP-ID_y
6ae21908
KH
590 ...
591 N:SEPARATOR_z (< 0)
592 */
593
54fa5bc1 594#define MAX_MAP_SET_LEVEL 30
6ae21908
KH
595
596typedef struct
597{
598 int rest_length;
599 int orig_val;
600} tr_stack;
601
8146262a
KH
602static tr_stack mapping_stack[MAX_MAP_SET_LEVEL];
603static tr_stack *mapping_stack_pointer;
6ae21908 604
54fa5bc1
KH
605/* If this variable is non-zero, it indicates the stack_idx
606 of immediately called by CCL_MapMultiple. */
be57900b 607static int stack_idx_of_map_multiple;
54fa5bc1
KH
608
609#define PUSH_MAPPING_STACK(restlen, orig) \
a89f435d
PJ
610do \
611 { \
54fa5bc1
KH
612 mapping_stack_pointer->rest_length = (restlen); \
613 mapping_stack_pointer->orig_val = (orig); \
614 mapping_stack_pointer++; \
a89f435d
PJ
615 } \
616while (0)
54fa5bc1
KH
617
618#define POP_MAPPING_STACK(restlen, orig) \
a89f435d
PJ
619do \
620 { \
54fa5bc1
KH
621 mapping_stack_pointer--; \
622 (restlen) = mapping_stack_pointer->rest_length; \
623 (orig) = mapping_stack_pointer->orig_val; \
a89f435d
PJ
624 } \
625while (0)
6ae21908 626
54fa5bc1 627#define CCL_CALL_FOR_MAP_INSTRUCTION(symbol, ret_ic) \
a89f435d 628do \
0ee1088b 629 { \
54fa5bc1
KH
630 struct ccl_program called_ccl; \
631 if (stack_idx >= 256 \
632 || (setup_ccl_program (&called_ccl, (symbol)) != 0)) \
633 { \
634 if (stack_idx > 0) \
635 { \
636 ccl_prog = ccl_prog_stack_struct[0].ccl_prog; \
637 ic = ccl_prog_stack_struct[0].ic; \
9eaa8e65 638 eof_ic = ccl_prog_stack_struct[0].eof_ic; \
54fa5bc1
KH
639 } \
640 CCL_INVALID_CMD; \
641 } \
642 ccl_prog_stack_struct[stack_idx].ccl_prog = ccl_prog; \
643 ccl_prog_stack_struct[stack_idx].ic = (ret_ic); \
9eaa8e65 644 ccl_prog_stack_struct[stack_idx].eof_ic = eof_ic; \
54fa5bc1
KH
645 stack_idx++; \
646 ccl_prog = called_ccl.prog; \
647 ic = CCL_HEADER_MAIN; \
9eaa8e65 648 eof_ic = XFASTINT (ccl_prog[CCL_HEADER_EOF]); \
54fa5bc1 649 goto ccl_repeat; \
0ee1088b 650 } \
a89f435d 651while (0)
6ae21908 652
8146262a 653#define CCL_MapSingle 0x12 /* Map by single code conversion map
6ae21908 654 1:ExtendedCOMMNDXXXRRRrrrXXXXX
8146262a 655 2:MAP-ID
6ae21908 656 ------------------------------
8146262a
KH
657 Map reg[rrr] by MAP-ID.
658 If some valid mapping is found,
6ae21908
KH
659 set reg[rrr] to the result,
660 else
661 set reg[RRR] to -1.
662 */
4ed46869 663
d80dc57e
DL
664#define CCL_LookupIntConstTbl 0x13 /* Lookup multibyte character by
665 integer key. Afterwards R7 set
e0f24100 666 to 1 if lookup succeeded.
d80dc57e
DL
667 1:ExtendedCOMMNDRrrRRRXXXXXXXX
668 2:ARGUMENT(Hash table ID) */
669
670#define CCL_LookupCharConstTbl 0x14 /* Lookup integer by multibyte
671 character key. Afterwards R7 set
e0f24100 672 to 1 if lookup succeeded.
d80dc57e
DL
673 1:ExtendedCOMMNDRrrRRRrrrXXXXX
674 2:ARGUMENT(Hash table ID) */
675
4ed46869
KH
676/* CCL arithmetic/logical operators. */
677#define CCL_PLUS 0x00 /* X = Y + Z */
678#define CCL_MINUS 0x01 /* X = Y - Z */
679#define CCL_MUL 0x02 /* X = Y * Z */
680#define CCL_DIV 0x03 /* X = Y / Z */
681#define CCL_MOD 0x04 /* X = Y % Z */
682#define CCL_AND 0x05 /* X = Y & Z */
683#define CCL_OR 0x06 /* X = Y | Z */
684#define CCL_XOR 0x07 /* X = Y ^ Z */
685#define CCL_LSH 0x08 /* X = Y << Z */
686#define CCL_RSH 0x09 /* X = Y >> Z */
687#define CCL_LSH8 0x0A /* X = (Y << 8) | Z */
688#define CCL_RSH8 0x0B /* X = Y >> 8, r[7] = Y & 0xFF */
689#define CCL_DIVMOD 0x0C /* X = Y / Z, r[7] = Y % Z */
690#define CCL_LS 0x10 /* X = (X < Y) */
691#define CCL_GT 0x11 /* X = (X > Y) */
692#define CCL_EQ 0x12 /* X = (X == Y) */
693#define CCL_LE 0x13 /* X = (X <= Y) */
694#define CCL_GE 0x14 /* X = (X >= Y) */
695#define CCL_NE 0x15 /* X = (X != Y) */
696
51520e8a 697#define CCL_DECODE_SJIS 0x16 /* X = HIGHER_BYTE (DE-SJIS (Y, Z))
4ed46869 698 r[7] = LOWER_BYTE (DE-SJIS (Y, Z)) */
51520e8a
KH
699#define CCL_ENCODE_SJIS 0x17 /* X = HIGHER_BYTE (SJIS (Y, Z))
700 r[7] = LOWER_BYTE (SJIS (Y, Z) */
4ed46869 701
4ed46869 702/* Terminate CCL program successfully. */
0ee1088b 703#define CCL_SUCCESS \
a89f435d 704do \
0ee1088b 705 { \
4ed46869 706 ccl->status = CCL_STAT_SUCCESS; \
0ee1088b
KH
707 goto ccl_finish; \
708 } \
a89f435d 709while(0)
4ed46869
KH
710
711/* Suspend CCL program because of reading from empty input buffer or
712 writing to full output buffer. When this program is resumed, the
713 same I/O command is executed. */
e34b1164 714#define CCL_SUSPEND(stat) \
a89f435d 715do \
0ee1088b 716 { \
e34b1164
KH
717 ic--; \
718 ccl->status = stat; \
719 goto ccl_finish; \
0ee1088b 720 } \
a89f435d 721while (0)
4ed46869
KH
722
723/* Terminate CCL program because of invalid command. Should not occur
724 in the normal case. */
9eaa8e65
KH
725#ifndef CCL_DEBUG
726
4ed46869 727#define CCL_INVALID_CMD \
a89f435d 728do \
0ee1088b 729 { \
4ed46869
KH
730 ccl->status = CCL_STAT_INVALID_CMD; \
731 goto ccl_error_handler; \
0ee1088b 732 } \
a89f435d 733while(0)
4ed46869 734
9eaa8e65
KH
735#else
736
4ed46869 737#define CCL_INVALID_CMD \
a89f435d 738do \
0ee1088b 739 { \
9eaa8e65 740 ccl_debug_hook (this_ic); \
4ed46869
KH
741 ccl->status = CCL_STAT_INVALID_CMD; \
742 goto ccl_error_handler; \
0ee1088b 743 } \
a89f435d 744while(0)
4ed46869 745
9eaa8e65
KH
746#endif
747
30569699
PE
748#define GET_CCL_RANGE(var, ccl_prog, ic, lo, hi) \
749 do \
750 { \
751 EMACS_INT prog_word = XINT ((ccl_prog)[ic]); \
752 if (! ((lo) <= prog_word && prog_word <= (hi))) \
753 CCL_INVALID_CMD; \
754 (var) = prog_word; \
755 } \
756 while (0)
757
758#define GET_CCL_CODE(code, ccl_prog, ic) \
e003a292 759 GET_CCL_RANGE (code, ccl_prog, ic, CCL_CODE_MIN, CCL_CODE_MAX)
30569699
PE
760
761#define GET_CCL_INT(var, ccl_prog, ic) \
762 GET_CCL_RANGE (var, ccl_prog, ic, INT_MIN, INT_MAX)
763
764#define IN_INT_RANGE(val) (INT_MIN <= (val) && (val) <= INT_MAX)
765
4ed46869 766/* Encode one character CH to multibyte form and write to the current
887bfbd7 767 output buffer. If CH is less than 256, CH is written as is. */
c10842ea
KH
768#define CCL_WRITE_CHAR(ch) \
769 do { \
770 if (! dst) \
771 CCL_INVALID_CMD; \
772 else if (dst < dst_end) \
773 *dst++ = (ch); \
774 else \
775 CCL_SUSPEND (CCL_STAT_SUSPEND_BY_DST); \
a8302ba3
KH
776 } while (0)
777
4ed46869
KH
778/* Write a string at ccl_prog[IC] of length LEN to the current output
779 buffer. */
c6589bbd
KH
780#define CCL_WRITE_STRING(len) \
781 do { \
fb90da1b 782 int ccli; \
c6589bbd
KH
783 if (!dst) \
784 CCL_INVALID_CMD; \
785 else if (dst + len <= dst_end) \
786 { \
787 if (XFASTINT (ccl_prog[ic]) & 0x1000000) \
fb90da1b
PE
788 for (ccli = 0; ccli < len; ccli++) \
789 *dst++ = XFASTINT (ccl_prog[ic + ccli]) & 0xFFFFFF; \
c6589bbd 790 else \
fb90da1b
PE
791 for (ccli = 0; ccli < len; ccli++) \
792 *dst++ = ((XFASTINT (ccl_prog[ic + (ccli / 3)])) \
793 >> ((2 - (ccli % 3)) * 8)) & 0xFF; \
c6589bbd
KH
794 } \
795 else \
796 CCL_SUSPEND (CCL_STAT_SUSPEND_BY_DST); \
4ed46869
KH
797 } while (0)
798
c10842ea
KH
799/* Read one byte from the current input buffer into Rth register. */
800#define CCL_READ_CHAR(r) \
801 do { \
802 if (! src) \
803 CCL_INVALID_CMD; \
804 else if (src < src_end) \
805 r = *src++; \
806 else if (ccl->last_block) \
807 { \
327719ee 808 r = -1; \
c10842ea
KH
809 ic = ccl->eof_ic; \
810 goto ccl_repeat; \
811 } \
812 else \
813 CCL_SUSPEND (CCL_STAT_SUSPEND_BY_SRC); \
814 } while (0)
815
bda731af
KH
816/* Decode CODE by a charset whose id is ID. If ID is 0, return CODE
817 as is for backward compatibility. Assume that we can use the
818 variable `charset'. */
819
820#define CCL_DECODE_CHAR(id, code) \
821 ((id) == 0 ? (code) \
822 : (charset = CHARSET_FROM_ID ((id)), DECODE_CHAR (charset, (code))))
823
bda731af
KH
824/* Encode character C by some of charsets in CHARSET_LIST. Set ID to
825 the id of the used charset, ENCODED to the resulf of encoding.
826 Assume that we can use the variable `charset'. */
827
8f924df7 828#define CCL_ENCODE_CHAR(c, charset_list, id, encoded) \
4ffd4870 829 do { \
fb90da1b 830 unsigned ncode; \
4ffd4870 831 \
fb90da1b 832 charset = char_charset ((c), (charset_list), &ncode); \
8f924df7 833 if (! charset && ! NILP (charset_list)) \
fb90da1b 834 charset = char_charset ((c), Qnil, &ncode); \
8f924df7
KH
835 if (charset) \
836 { \
837 (id) = CHARSET_ID (charset); \
fb90da1b 838 (encoded) = ncode; \
4ffd4870 839 } \
8f924df7 840 } while (0)
4ffd4870 841
c10842ea
KH
842/* Execute CCL code on characters at SOURCE (length SRC_SIZE). The
843 resulting text goes to a place pointed by DESTINATION, the length
844 of which should not exceed DST_SIZE. As a side effect, how many
845 characters are consumed and produced are recorded in CCL->consumed
846 and CCL->produced, and the contents of CCL registers are updated.
847 If SOURCE or DESTINATION is NULL, only operations on registers are
848 permitted. */
4ed46869
KH
849
850#ifdef CCL_DEBUG
851#define CCL_DEBUG_BACKTRACE_LEN 256
f9bd23fd 852int ccl_backtrace_table[CCL_DEBUG_BACKTRACE_LEN];
4ed46869 853int ccl_backtrace_idx;
9eaa8e65
KH
854
855int
856ccl_debug_hook (int ic)
857{
858 return ic;
859}
860
4ed46869
KH
861#endif
862
863struct ccl_prog_stack
864 {
a9f1cc19 865 Lisp_Object *ccl_prog; /* Pointer to an array of CCL code. */
4ed46869 866 int ic; /* Instruction Counter. */
9eaa8e65 867 int eof_ic; /* Instruction Counter to jump on EOF. */
4ed46869
KH
868 };
869
177c0ea7 870/* For the moment, we only support depth 256 of stack. */
c13362d8
KH
871static struct ccl_prog_stack ccl_prog_stack_struct[256];
872
c10842ea 873void
971de7fb 874ccl_driver (struct ccl_program *ccl, int *source, int *destination, int src_size, int dst_size, Lisp_Object charset_list)
4ed46869
KH
875{
876 register int *reg = ccl->reg;
877 register int ic = ccl->ic;
8a1ae4dd 878 register int code = 0, field1, field2;
e995085f 879 register Lisp_Object *ccl_prog = ccl->prog;
c10842ea
KH
880 int *src = source, *src_end = src + src_size;
881 int *dst = destination, *dst_end = dst + dst_size;
4ed46869 882 int jump_address;
8a1ae4dd 883 int i = 0, j, op;
c13362d8 884 int stack_idx = ccl->stack_idx;
519bf146 885 /* Instruction counter of the current CCL code. */
8a1ae4dd 886 int this_ic = 0;
c10842ea 887 struct charset *charset;
9eaa8e65
KH
888 int eof_ic = ccl->eof_ic;
889 int eof_hit = 0;
4ed46869 890
c10842ea 891 if (ccl->buf_magnification == 0) /* We can't read/produce any bytes. */
12abd7d1
KH
892 dst = NULL;
893
54fa5bc1
KH
894 /* Set mapping stack pointer. */
895 mapping_stack_pointer = mapping_stack;
896
4ed46869
KH
897#ifdef CCL_DEBUG
898 ccl_backtrace_idx = 0;
899#endif
900
901 for (;;)
902 {
4ccd0d4a 903 ccl_repeat:
4ed46869
KH
904#ifdef CCL_DEBUG
905 ccl_backtrace_table[ccl_backtrace_idx++] = ic;
906 if (ccl_backtrace_idx >= CCL_DEBUG_BACKTRACE_LEN)
907 ccl_backtrace_idx = 0;
908 ccl_backtrace_table[ccl_backtrace_idx] = 0;
909#endif
910
911 if (!NILP (Vquit_flag) && NILP (Vinhibit_quit))
912 {
913 /* We can't just signal Qquit, instead break the loop as if
914 the whole data is processed. Don't reset Vquit_flag, it
915 must be handled later at a safer place. */
c10842ea
KH
916 if (src)
917 src = source + src_size;
4ed46869
KH
918 ccl->status = CCL_STAT_QUIT;
919 break;
920 }
921
519bf146 922 this_ic = ic;
30569699 923 GET_CCL_CODE (code, ccl_prog, ic++);
4ed46869
KH
924 field1 = code >> 8;
925 field2 = (code & 0xFF) >> 5;
926
927#define rrr field2
928#define RRR (field1 & 7)
929#define Rrr ((field1 >> 3) & 7)
930#define ADDR field1
e34b1164 931#define EXCMD (field1 >> 6)
4ed46869
KH
932
933 switch (code & 0x1F)
934 {
935 case CCL_SetRegister: /* 00000000000000000RRRrrrXXXXX */
936 reg[rrr] = reg[RRR];
937 break;
938
939 case CCL_SetShortConst: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
940 reg[rrr] = field1;
941 break;
942
943 case CCL_SetConst: /* 00000000000000000000rrrXXXXX */
30569699 944 GET_CCL_INT (reg[rrr], ccl_prog, ic++);
4ed46869
KH
945 break;
946
947 case CCL_SetArray: /* CCCCCCCCCCCCCCCCCCCCRRRrrrXXXXX */
948 i = reg[RRR];
949 j = field1 >> 3;
519e1d69 950 if (0 <= i && i < j)
30569699 951 GET_CCL_INT (reg[rrr], ccl_prog, ic + i);
4ed46869
KH
952 ic += j;
953 break;
954
955 case CCL_Jump: /* A--D--D--R--E--S--S-000XXXXX */
956 ic += ADDR;
957 break;
958
959 case CCL_JumpCond: /* A--D--D--R--E--S--S-rrrXXXXX */
960 if (!reg[rrr])
961 ic += ADDR;
962 break;
963
964 case CCL_WriteRegisterJump: /* A--D--D--R--E--S--S-rrrXXXXX */
965 i = reg[rrr];
966 CCL_WRITE_CHAR (i);
967 ic += ADDR;
968 break;
969
970 case CCL_WriteRegisterReadJump: /* A--D--D--R--E--S--S-rrrXXXXX */
971 i = reg[rrr];
972 CCL_WRITE_CHAR (i);
973 ic++;
974 CCL_READ_CHAR (reg[rrr]);
975 ic += ADDR - 1;
976 break;
977
978 case CCL_WriteConstJump: /* A--D--D--R--E--S--S-000XXXXX */
30569699 979 GET_CCL_INT (i, ccl_prog, ic);
4ed46869
KH
980 CCL_WRITE_CHAR (i);
981 ic += ADDR;
982 break;
983
984 case CCL_WriteConstReadJump: /* A--D--D--R--E--S--S-rrrXXXXX */
30569699 985 GET_CCL_INT (i, ccl_prog, ic);
4ed46869
KH
986 CCL_WRITE_CHAR (i);
987 ic++;
988 CCL_READ_CHAR (reg[rrr]);
989 ic += ADDR - 1;
990 break;
991
992 case CCL_WriteStringJump: /* A--D--D--R--E--S--S-000XXXXX */
30569699 993 GET_CCL_INT (j, ccl_prog, ic++);
4ed46869
KH
994 CCL_WRITE_STRING (j);
995 ic += ADDR - 1;
996 break;
997
998 case CCL_WriteArrayReadJump: /* A--D--D--R--E--S--S-rrrXXXXX */
999 i = reg[rrr];
30569699 1000 GET_CCL_INT (j, ccl_prog, ic);
519e1d69 1001 if (0 <= i && i < j)
4ed46869 1002 {
30569699 1003 GET_CCL_INT (i, ccl_prog, ic + 1 + i);
4ed46869
KH
1004 CCL_WRITE_CHAR (i);
1005 }
887bfbd7 1006 ic += j + 2;
4ed46869
KH
1007 CCL_READ_CHAR (reg[rrr]);
1008 ic += ADDR - (j + 2);
1009 break;
1010
1011 case CCL_ReadJump: /* A--D--D--R--E--S--S-rrrYYYYY */
1012 CCL_READ_CHAR (reg[rrr]);
1013 ic += ADDR;
1014 break;
1015
1016 case CCL_ReadBranch: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
1017 CCL_READ_CHAR (reg[rrr]);
1018 /* fall through ... */
1019 case CCL_Branch: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
30569699
PE
1020 {
1021 int incr;
1022 GET_CCL_INT (incr, ccl_prog,
519e1d69 1023 ic + (0 <= reg[rrr] && reg[rrr] < field1
30569699
PE
1024 ? reg[rrr]
1025 : field1));
1026 ic += incr;
1027 }
4ed46869
KH
1028 break;
1029
1030 case CCL_ReadRegister: /* CCCCCCCCCCCCCCCCCCCCrrXXXXX */
1031 while (1)
1032 {
1033 CCL_READ_CHAR (reg[rrr]);
1034 if (!field1) break;
30569699 1035 GET_CCL_CODE (code, ccl_prog, ic++);
4ed46869
KH
1036 field1 = code >> 8;
1037 field2 = (code & 0xFF) >> 5;
1038 }
1039 break;
1040
1041 case CCL_WriteExprConst: /* 1:00000OPERATION000RRR000XXXXX */
1042 rrr = 7;
1043 i = reg[RRR];
30569699 1044 GET_CCL_INT (j, ccl_prog, ic);
4ed46869 1045 op = field1 >> 6;
25660570 1046 jump_address = ic + 1;
4ed46869
KH
1047 goto ccl_set_expr;
1048
1049 case CCL_WriteRegister: /* CCCCCCCCCCCCCCCCCCCrrrXXXXX */
1050 while (1)
1051 {
1052 i = reg[rrr];
1053 CCL_WRITE_CHAR (i);
1054 if (!field1) break;
30569699 1055 GET_CCL_CODE (code, ccl_prog, ic++);
4ed46869
KH
1056 field1 = code >> 8;
1057 field2 = (code & 0xFF) >> 5;
1058 }
1059 break;
1060
1061 case CCL_WriteExprRegister: /* 1:00000OPERATIONRrrRRR000XXXXX */
1062 rrr = 7;
1063 i = reg[RRR];
1064 j = reg[Rrr];
1065 op = field1 >> 6;
25660570 1066 jump_address = ic;
4ed46869
KH
1067 goto ccl_set_expr;
1068
5232fa7b 1069 case CCL_Call: /* 1:CCCCCCCCCCCCCCCCCCCCFFFXXXXX */
4ed46869
KH
1070 {
1071 Lisp_Object slot;
5232fa7b
KH
1072 int prog_id;
1073
1074 /* If FFF is nonzero, the CCL program ID is in the
1075 following code. */
1076 if (rrr)
30569699 1077 GET_CCL_INT (prog_id, ccl_prog, ic++);
5232fa7b
KH
1078 else
1079 prog_id = field1;
4ed46869
KH
1080
1081 if (stack_idx >= 256
5232fa7b 1082 || prog_id < 0
64ef2921
SM
1083 || prog_id >= ASIZE (Vccl_program_table)
1084 || (slot = AREF (Vccl_program_table, prog_id), !VECTORP (slot))
1085 || !VECTORP (AREF (slot, 1)))
4ed46869
KH
1086 {
1087 if (stack_idx > 0)
1088 {
1089 ccl_prog = ccl_prog_stack_struct[0].ccl_prog;
1090 ic = ccl_prog_stack_struct[0].ic;
9eaa8e65 1091 eof_ic = ccl_prog_stack_struct[0].eof_ic;
4ed46869
KH
1092 }
1093 CCL_INVALID_CMD;
1094 }
177c0ea7 1095
4ed46869
KH
1096 ccl_prog_stack_struct[stack_idx].ccl_prog = ccl_prog;
1097 ccl_prog_stack_struct[stack_idx].ic = ic;
9eaa8e65 1098 ccl_prog_stack_struct[stack_idx].eof_ic = eof_ic;
4ed46869 1099 stack_idx++;
64ef2921 1100 ccl_prog = XVECTOR (AREF (slot, 1))->contents;
4ed46869 1101 ic = CCL_HEADER_MAIN;
9eaa8e65 1102 eof_ic = XFASTINT (ccl_prog[CCL_HEADER_EOF]);
4ed46869
KH
1103 }
1104 break;
1105
1106 case CCL_WriteConstString: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
1107 if (!rrr)
1108 CCL_WRITE_CHAR (field1);
1109 else
1110 {
1111 CCL_WRITE_STRING (field1);
1112 ic += (field1 + 2) / 3;
1113 }
1114 break;
1115
1116 case CCL_WriteArray: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
1117 i = reg[rrr];
519e1d69 1118 if (0 <= i && i < field1)
4ed46869 1119 {
30569699 1120 GET_CCL_INT (j, ccl_prog, ic + i);
4ed46869
KH
1121 CCL_WRITE_CHAR (j);
1122 }
1123 ic += field1;
1124 break;
1125
1126 case CCL_End: /* 0000000000000000000000XXXXX */
d3a478e2 1127 if (stack_idx > 0)
4ed46869 1128 {
d3a478e2 1129 stack_idx--;
4ed46869
KH
1130 ccl_prog = ccl_prog_stack_struct[stack_idx].ccl_prog;
1131 ic = ccl_prog_stack_struct[stack_idx].ic;
9eaa8e65
KH
1132 eof_ic = ccl_prog_stack_struct[stack_idx].eof_ic;
1133 if (eof_hit)
1134 ic = eof_ic;
4ed46869
KH
1135 break;
1136 }
ad3d1b1d
KH
1137 if (src)
1138 src = src_end;
1139 /* ccl->ic should points to this command code again to
1140 suppress further processing. */
1141 ic--;
4ed46869
KH
1142 CCL_SUCCESS;
1143
1144 case CCL_ExprSelfConst: /* 00000OPERATION000000rrrXXXXX */
30569699 1145 GET_CCL_INT (i, ccl_prog, ic++);
4ed46869
KH
1146 op = field1 >> 6;
1147 goto ccl_expr_self;
1148
1149 case CCL_ExprSelfReg: /* 00000OPERATION000RRRrrrXXXXX */
1150 i = reg[RRR];
1151 op = field1 >> 6;
1152
1153 ccl_expr_self:
1154 switch (op)
1155 {
1156 case CCL_PLUS: reg[rrr] += i; break;
1157 case CCL_MINUS: reg[rrr] -= i; break;
1158 case CCL_MUL: reg[rrr] *= i; break;
1159 case CCL_DIV: reg[rrr] /= i; break;
1160 case CCL_MOD: reg[rrr] %= i; break;
1161 case CCL_AND: reg[rrr] &= i; break;
1162 case CCL_OR: reg[rrr] |= i; break;
1163 case CCL_XOR: reg[rrr] ^= i; break;
1164 case CCL_LSH: reg[rrr] <<= i; break;
1165 case CCL_RSH: reg[rrr] >>= i; break;
1166 case CCL_LSH8: reg[rrr] <<= 8; reg[rrr] |= i; break;
1167 case CCL_RSH8: reg[7] = reg[rrr] & 0xFF; reg[rrr] >>= 8; break;
1168 case CCL_DIVMOD: reg[7] = reg[rrr] % i; reg[rrr] /= i; break;
1169 case CCL_LS: reg[rrr] = reg[rrr] < i; break;
1170 case CCL_GT: reg[rrr] = reg[rrr] > i; break;
1171 case CCL_EQ: reg[rrr] = reg[rrr] == i; break;
1172 case CCL_LE: reg[rrr] = reg[rrr] <= i; break;
1173 case CCL_GE: reg[rrr] = reg[rrr] >= i; break;
1174 case CCL_NE: reg[rrr] = reg[rrr] != i; break;
1175 default: CCL_INVALID_CMD;
1176 }
1177 break;
1178
1179 case CCL_SetExprConst: /* 00000OPERATION000RRRrrrXXXXX */
1180 i = reg[RRR];
30569699 1181 GET_CCL_INT (j, ccl_prog, ic++);
4ed46869 1182 op = field1 >> 6;
30569699 1183 jump_address = ic;
4ed46869
KH
1184 goto ccl_set_expr;
1185
1186 case CCL_SetExprReg: /* 00000OPERATIONRrrRRRrrrXXXXX */
1187 i = reg[RRR];
1188 j = reg[Rrr];
1189 op = field1 >> 6;
1190 jump_address = ic;
1191 goto ccl_set_expr;
1192
1193 case CCL_ReadJumpCondExprConst: /* A--D--D--R--E--S--S-rrrXXXXX */
1194 CCL_READ_CHAR (reg[rrr]);
1195 case CCL_JumpCondExprConst: /* A--D--D--R--E--S--S-rrrXXXXX */
1196 i = reg[rrr];
30569699
PE
1197 jump_address = ic + ADDR;
1198 GET_CCL_INT (op, ccl_prog, ic++);
1199 GET_CCL_INT (j, ccl_prog, ic++);
4ed46869
KH
1200 rrr = 7;
1201 goto ccl_set_expr;
1202
1203 case CCL_ReadJumpCondExprReg: /* A--D--D--R--E--S--S-rrrXXXXX */
1204 CCL_READ_CHAR (reg[rrr]);
1205 case CCL_JumpCondExprReg:
1206 i = reg[rrr];
30569699
PE
1207 jump_address = ic + ADDR;
1208 GET_CCL_INT (op, ccl_prog, ic++);
1209 GET_CCL_RANGE (j, ccl_prog, ic++, 0, 7);
1210 j = reg[j];
4ed46869
KH
1211 rrr = 7;
1212
1213 ccl_set_expr:
1214 switch (op)
1215 {
1216 case CCL_PLUS: reg[rrr] = i + j; break;
1217 case CCL_MINUS: reg[rrr] = i - j; break;
1218 case CCL_MUL: reg[rrr] = i * j; break;
1219 case CCL_DIV: reg[rrr] = i / j; break;
1220 case CCL_MOD: reg[rrr] = i % j; break;
1221 case CCL_AND: reg[rrr] = i & j; break;
1222 case CCL_OR: reg[rrr] = i | j; break;
3b8c0c70 1223 case CCL_XOR: reg[rrr] = i ^ j; break;
4ed46869
KH
1224 case CCL_LSH: reg[rrr] = i << j; break;
1225 case CCL_RSH: reg[rrr] = i >> j; break;
1226 case CCL_LSH8: reg[rrr] = (i << 8) | j; break;
1227 case CCL_RSH8: reg[rrr] = i >> 8; reg[7] = i & 0xFF; break;
1228 case CCL_DIVMOD: reg[rrr] = i / j; reg[7] = i % j; break;
1229 case CCL_LS: reg[rrr] = i < j; break;
1230 case CCL_GT: reg[rrr] = i > j; break;
1231 case CCL_EQ: reg[rrr] = i == j; break;
1232 case CCL_LE: reg[rrr] = i <= j; break;
1233 case CCL_GE: reg[rrr] = i >= j; break;
1234 case CCL_NE: reg[rrr] = i != j; break;
c10842ea
KH
1235 case CCL_DECODE_SJIS:
1236 {
1237 i = (i << 8) | j;
1238 SJIS_TO_JIS (i);
1239 reg[rrr] = i >> 8;
1240 reg[7] = i & 0xFF;
1241 break;
1242 }
1243 case CCL_ENCODE_SJIS:
1244 {
1245 i = (i << 8) | j;
1246 JIS_TO_SJIS (i);
1247 reg[rrr] = i >> 8;
1248 reg[7] = i & 0xFF;
1249 break;
1250 }
4ed46869
KH
1251 default: CCL_INVALID_CMD;
1252 }
1253 code &= 0x1F;
1254 if (code == CCL_WriteExprConst || code == CCL_WriteExprRegister)
1255 {
1256 i = reg[rrr];
1257 CCL_WRITE_CHAR (i);
25660570 1258 ic = jump_address;
4ed46869
KH
1259 }
1260 else if (!reg[rrr])
1261 ic = jump_address;
1262 break;
1263
450ed226 1264 case CCL_Extension:
e34b1164
KH
1265 switch (EXCMD)
1266 {
6ae21908 1267 case CCL_ReadMultibyteChar2:
e34b1164
KH
1268 if (!src)
1269 CCL_INVALID_CMD;
c10842ea 1270 CCL_READ_CHAR (i);
bda731af 1271 CCL_ENCODE_CHAR (i, charset_list, reg[RRR], reg[rrr]);
e34b1164
KH
1272 break;
1273
6ae21908 1274 case CCL_WriteMultibyteChar2:
c10842ea
KH
1275 if (! dst)
1276 CCL_INVALID_CMD;
bda731af 1277 i = CCL_DECODE_CHAR (reg[RRR], reg[rrr]);
c10842ea 1278 CCL_WRITE_CHAR (i);
e34b1164
KH
1279 break;
1280
8146262a 1281 case CCL_TranslateCharacter:
bda731af 1282 i = CCL_DECODE_CHAR (reg[RRR], reg[rrr]);
c10842ea 1283 op = translate_char (GET_TRANSLATION_TABLE (reg[Rrr]), i);
bda731af 1284 CCL_ENCODE_CHAR (op, charset_list, reg[RRR], reg[rrr]);
e34b1164
KH
1285 break;
1286
8146262a 1287 case CCL_TranslateCharacterConstTbl:
30569699
PE
1288 {
1289 EMACS_INT eop;
1290 GET_CCL_RANGE (eop, ccl_prog, ic++, 0,
1291 (VECTORP (Vtranslation_table_vector)
1292 ? ASIZE (Vtranslation_table_vector)
1293 : -1));
1294 i = CCL_DECODE_CHAR (reg[RRR], reg[rrr]);
1295 op = translate_char (GET_TRANSLATION_TABLE (eop), i);
1296 CCL_ENCODE_CHAR (op, charset_list, reg[RRR], reg[rrr]);
1297 }
e34b1164
KH
1298 break;
1299
d80dc57e 1300 case CCL_LookupIntConstTbl:
177c0ea7 1301 {
30569699
PE
1302 EMACS_INT eop;
1303 struct Lisp_Hash_Table *h;
1304 GET_CCL_RANGE (eop, ccl_prog, ic++, 0,
1305 (VECTORP (Vtranslation_hash_table_vector)
1306 ? ASIZE (Vtranslation_hash_table_vector)
1307 : -1));
1308 h = GET_HASH_TABLE (eop);
d80dc57e
DL
1309
1310 op = hash_lookup (h, make_number (reg[RRR]), NULL);
1311 if (op >= 0)
1312 {
f9bd23fd
DL
1313 Lisp_Object opl;
1314 opl = HASH_VALUE (h, op);
0bc6bafd 1315 if (! CHARACTERP (opl))
d80dc57e 1316 CCL_INVALID_CMD;
bda731af
KH
1317 reg[RRR] = charset_unicode;
1318 reg[rrr] = op;
d80dc57e
DL
1319 reg[7] = 1; /* r7 true for success */
1320 }
1321 else
1322 reg[7] = 0;
1323 }
1324 break;
1325
1326 case CCL_LookupCharConstTbl:
177c0ea7 1327 {
30569699
PE
1328 EMACS_INT eop;
1329 struct Lisp_Hash_Table *h;
1330 GET_CCL_RANGE (eop, ccl_prog, ic++, 0,
1331 (VECTORP (Vtranslation_hash_table_vector)
1332 ? ASIZE (Vtranslation_hash_table_vector)
1333 : -1));
1334 i = CCL_DECODE_CHAR (reg[RRR], reg[rrr]);
1335 h = GET_HASH_TABLE (eop);
d80dc57e
DL
1336
1337 op = hash_lookup (h, make_number (i), NULL);
1338 if (op >= 0)
1339 {
f9bd23fd
DL
1340 Lisp_Object opl;
1341 opl = HASH_VALUE (h, op);
30569699 1342 if (! (INTEGERP (opl) && IN_INT_RANGE (XINT (opl))))
d80dc57e 1343 CCL_INVALID_CMD;
f9bd23fd 1344 reg[RRR] = XINT (opl);
d80dc57e
DL
1345 reg[7] = 1; /* r7 true for success */
1346 }
1347 else
1348 reg[7] = 0;
1349 }
1350 break;
1351
e34b1164
KH
1352 case CCL_IterateMultipleMap:
1353 {
8146262a 1354 Lisp_Object map, content, attrib, value;
30569699
PE
1355 EMACS_INT point, size;
1356 int fin_ic;
e34b1164 1357
30569699 1358 GET_CCL_INT (j, ccl_prog, ic++); /* number of maps. */
e34b1164
KH
1359 fin_ic = ic + j;
1360 op = reg[rrr];
1361 if ((j > reg[RRR]) && (j >= 0))
1362 {
1363 ic += reg[RRR];
1364 i = reg[RRR];
1365 }
1366 else
1367 {
1368 reg[RRR] = -1;
1369 ic = fin_ic;
1370 break;
1371 }
1372
1373 for (;i < j;i++)
1374 {
1375
64ef2921 1376 size = ASIZE (Vcode_conversion_map_vector);
d387866a 1377 point = XINT (ccl_prog[ic++]);
30569699 1378 if (! (0 <= point && point < size)) continue;
64ef2921 1379 map = AREF (Vcode_conversion_map_vector, point);
8146262a 1380
78edd3b7 1381 /* Check map validity. */
8146262a 1382 if (!CONSP (map)) continue;
03699b14 1383 map = XCDR (map);
8146262a 1384 if (!VECTORP (map)) continue;
64ef2921 1385 size = ASIZE (map);
e34b1164 1386 if (size <= 1) continue;
6ae21908 1387
64ef2921 1388 content = AREF (map, 0);
6ae21908 1389
8146262a 1390 /* check map type,
6ae21908 1391 [STARTPOINT VAL1 VAL2 ...] or
78edd3b7 1392 [t ELEMENT STARTPOINT ENDPOINT] */
30569699 1393 if (INTEGERP (content))
6ae21908 1394 {
30569699
PE
1395 point = XINT (content);
1396 if (!(point <= op && op - point + 1 < size)) continue;
1397 content = AREF (map, op - point + 1);
6ae21908
KH
1398 }
1399 else if (EQ (content, Qt))
1400 {
1401 if (size != 4) continue;
30569699
PE
1402 if (INTEGERP (AREF (map, 2))
1403 && XINT (AREF (map, 2)) <= op
1404 && INTEGERP (AREF (map, 3))
1405 && op < XINT (AREF (map, 3)))
64ef2921 1406 content = AREF (map, 1);
6ae21908
KH
1407 else
1408 continue;
1409 }
177c0ea7 1410 else
6ae21908 1411 continue;
e34b1164
KH
1412
1413 if (NILP (content))
1414 continue;
30569699 1415 else if (INTEGERP (content) && IN_INT_RANGE (XINT (content)))
e34b1164
KH
1416 {
1417 reg[RRR] = i;
6ae21908 1418 reg[rrr] = XINT(content);
e34b1164
KH
1419 break;
1420 }
1421 else if (EQ (content, Qt) || EQ (content, Qlambda))
1422 {
1423 reg[RRR] = i;
1424 break;
1425 }
1426 else if (CONSP (content))
1427 {
03699b14
KR
1428 attrib = XCAR (content);
1429 value = XCDR (content);
30569699
PE
1430 if (! (INTEGERP (attrib) && INTEGERP (value)
1431 && IN_INT_RANGE (XINT (value))))
e34b1164
KH
1432 continue;
1433 reg[RRR] = i;
30569699 1434 reg[rrr] = XINT (value);
e34b1164
KH
1435 break;
1436 }
54fa5bc1
KH
1437 else if (SYMBOLP (content))
1438 CCL_CALL_FOR_MAP_INSTRUCTION (content, fin_ic);
1439 else
1440 CCL_INVALID_CMD;
e34b1164
KH
1441 }
1442 if (i == j)
1443 reg[RRR] = -1;
1444 ic = fin_ic;
1445 }
1446 break;
177c0ea7 1447
8146262a 1448 case CCL_MapMultiple:
e34b1164 1449 {
8146262a
KH
1450 Lisp_Object map, content, attrib, value;
1451 int point, size, map_vector_size;
1452 int map_set_rest_length, fin_ic;
54fa5bc1
KH
1453 int current_ic = this_ic;
1454
1455 /* inhibit recursive call on MapMultiple. */
1456 if (stack_idx_of_map_multiple > 0)
1457 {
1458 if (stack_idx_of_map_multiple <= stack_idx)
1459 {
1460 stack_idx_of_map_multiple = 0;
1461 mapping_stack_pointer = mapping_stack;
1462 CCL_INVALID_CMD;
1463 }
1464 }
1465 else
1466 mapping_stack_pointer = mapping_stack;
1467 stack_idx_of_map_multiple = 0;
8146262a 1468
30569699
PE
1469 /* Get number of maps and separators. */
1470 GET_CCL_INT (map_set_rest_length, ccl_prog, ic++);
1471
8146262a 1472 fin_ic = ic + map_set_rest_length;
54fa5bc1
KH
1473 op = reg[rrr];
1474
8146262a 1475 if ((map_set_rest_length > reg[RRR]) && (reg[RRR] >= 0))
e34b1164
KH
1476 {
1477 ic += reg[RRR];
1478 i = reg[RRR];
8146262a 1479 map_set_rest_length -= i;
e34b1164
KH
1480 }
1481 else
1482 {
1483 ic = fin_ic;
1484 reg[RRR] = -1;
54fa5bc1 1485 mapping_stack_pointer = mapping_stack;
e34b1164
KH
1486 break;
1487 }
6ae21908 1488
54fa5bc1
KH
1489 if (mapping_stack_pointer <= (mapping_stack + 1))
1490 {
1491 /* Set up initial state. */
1492 mapping_stack_pointer = mapping_stack;
1493 PUSH_MAPPING_STACK (0, op);
1494 reg[RRR] = -1;
1495 }
1496 else
1497 {
1498 /* Recover after calling other ccl program. */
1499 int orig_op;
e34b1164 1500
54fa5bc1
KH
1501 POP_MAPPING_STACK (map_set_rest_length, orig_op);
1502 POP_MAPPING_STACK (map_set_rest_length, reg[rrr]);
1503 switch (op)
e34b1164 1504 {
54fa5bc1
KH
1505 case -1:
1506 /* Regard it as Qnil. */
1507 op = orig_op;
1508 i++;
1509 ic++;
1510 map_set_rest_length--;
1511 break;
1512 case -2:
1513 /* Regard it as Qt. */
e34b1164 1514 op = reg[rrr];
54fa5bc1
KH
1515 i++;
1516 ic++;
1517 map_set_rest_length--;
1518 break;
1519 case -3:
1520 /* Regard it as Qlambda. */
1521 op = orig_op;
1522 i += map_set_rest_length;
1523 ic += map_set_rest_length;
1524 map_set_rest_length = 0;
1525 break;
1526 default:
1527 /* Regard it as normal mapping. */
8146262a 1528 i += map_set_rest_length;
54fa5bc1 1529 ic += map_set_rest_length;
8146262a 1530 POP_MAPPING_STACK (map_set_rest_length, reg[rrr]);
6ae21908
KH
1531 break;
1532 }
e34b1164 1533 }
64ef2921 1534 map_vector_size = ASIZE (Vcode_conversion_map_vector);
177c0ea7 1535
54fa5bc1
KH
1536 do {
1537 for (;map_set_rest_length > 0;i++, ic++, map_set_rest_length--)
1538 {
30569699 1539 GET_CCL_INT (point, ccl_prog, ic);
54fa5bc1
KH
1540 if (point < 0)
1541 {
1542 /* +1 is for including separator. */
1543 point = -point + 1;
1544 if (mapping_stack_pointer
1545 >= &mapping_stack[MAX_MAP_SET_LEVEL])
1546 CCL_INVALID_CMD;
1547 PUSH_MAPPING_STACK (map_set_rest_length - point,
1548 reg[rrr]);
1549 map_set_rest_length = point;
1550 reg[rrr] = op;
1551 continue;
1552 }
1553
1554 if (point >= map_vector_size) continue;
64ef2921 1555 map = AREF (Vcode_conversion_map_vector, point);
54fa5bc1 1556
78edd3b7 1557 /* Check map validity. */
54fa5bc1
KH
1558 if (!CONSP (map)) continue;
1559 map = XCDR (map);
1560 if (!VECTORP (map)) continue;
64ef2921 1561 size = ASIZE (map);
54fa5bc1
KH
1562 if (size <= 1) continue;
1563
64ef2921 1564 content = AREF (map, 0);
54fa5bc1
KH
1565
1566 /* check map type,
1567 [STARTPOINT VAL1 VAL2 ...] or
1568 [t ELEMENT STARTPOINT ENDPOINT] */
30569699 1569 if (INTEGERP (content))
54fa5bc1 1570 {
30569699
PE
1571 point = XINT (content);
1572 if (!(point <= op && op - point + 1 < size)) continue;
1573 content = AREF (map, op - point + 1);
54fa5bc1
KH
1574 }
1575 else if (EQ (content, Qt))
1576 {
1577 if (size != 4) continue;
30569699
PE
1578 if (INTEGERP (AREF (map, 2))
1579 && XINT (AREF (map, 2)) <= op
1580 && INTEGERP (AREF (map, 3))
1581 && op < XINT (AREF (map, 3)))
64ef2921 1582 content = AREF (map, 1);
54fa5bc1
KH
1583 else
1584 continue;
1585 }
177c0ea7 1586 else
54fa5bc1
KH
1587 continue;
1588
1589 if (NILP (content))
1590 continue;
1591
1592 reg[RRR] = i;
30569699 1593 if (INTEGERP (content) && IN_INT_RANGE (XINT (content)))
54fa5bc1
KH
1594 {
1595 op = XINT (content);
1596 i += map_set_rest_length - 1;
1597 ic += map_set_rest_length - 1;
1598 POP_MAPPING_STACK (map_set_rest_length, reg[rrr]);
1599 map_set_rest_length++;
1600 }
1601 else if (CONSP (content))
1602 {
1603 attrib = XCAR (content);
1604 value = XCDR (content);
30569699
PE
1605 if (! (INTEGERP (attrib) && INTEGERP (value)
1606 && IN_INT_RANGE (XINT (value))))
54fa5bc1 1607 continue;
30569699 1608 op = XINT (value);
54fa5bc1
KH
1609 i += map_set_rest_length - 1;
1610 ic += map_set_rest_length - 1;
1611 POP_MAPPING_STACK (map_set_rest_length, reg[rrr]);
1612 map_set_rest_length++;
1613 }
1614 else if (EQ (content, Qt))
1615 {
1616 op = reg[rrr];
1617 }
1618 else if (EQ (content, Qlambda))
1619 {
1620 i += map_set_rest_length;
1621 ic += map_set_rest_length;
1622 break;
1623 }
1624 else if (SYMBOLP (content))
1625 {
1626 if (mapping_stack_pointer
1627 >= &mapping_stack[MAX_MAP_SET_LEVEL])
1628 CCL_INVALID_CMD;
1629 PUSH_MAPPING_STACK (map_set_rest_length, reg[rrr]);
1630 PUSH_MAPPING_STACK (map_set_rest_length, op);
1631 stack_idx_of_map_multiple = stack_idx + 1;
1632 CCL_CALL_FOR_MAP_INSTRUCTION (content, current_ic);
1633 }
1634 else
1635 CCL_INVALID_CMD;
1636 }
1637 if (mapping_stack_pointer <= (mapping_stack + 1))
1638 break;
1639 POP_MAPPING_STACK (map_set_rest_length, reg[rrr]);
1640 i += map_set_rest_length;
1641 ic += map_set_rest_length;
1642 POP_MAPPING_STACK (map_set_rest_length, reg[rrr]);
1643 } while (1);
1644
e34b1164
KH
1645 ic = fin_ic;
1646 }
1647 reg[rrr] = op;
1648 break;
1649
8146262a 1650 case CCL_MapSingle:
e34b1164 1651 {
8146262a 1652 Lisp_Object map, attrib, value, content;
30569699 1653 int point;
8146262a 1654 j = XINT (ccl_prog[ic++]); /* map_id */
e34b1164 1655 op = reg[rrr];
64ef2921 1656 if (j >= ASIZE (Vcode_conversion_map_vector))
e34b1164
KH
1657 {
1658 reg[RRR] = -1;
1659 break;
1660 }
64ef2921 1661 map = AREF (Vcode_conversion_map_vector, j);
8146262a 1662 if (!CONSP (map))
e34b1164
KH
1663 {
1664 reg[RRR] = -1;
1665 break;
1666 }
03699b14 1667 map = XCDR (map);
30569699
PE
1668 if (! (VECTORP (map)
1669 && INTEGERP (AREF (map, 0))
1670 && XINT (AREF (map, 0)) <= op
1671 && op - XINT (AREF (map, 0)) + 1 < ASIZE (map)))
e34b1164
KH
1672 {
1673 reg[RRR] = -1;
1674 break;
1675 }
30569699 1676 point = XINT (AREF (map, 0));
e34b1164
KH
1677 point = op - point + 1;
1678 reg[RRR] = 0;
30569699
PE
1679 content = AREF (map, point);
1680 if (NILP (content))
e34b1164 1681 reg[RRR] = -1;
30569699
PE
1682 else if (INTEGERP (content))
1683 reg[rrr] = XINT (content);
1684 else if (EQ (content, Qt));
1685 else if (CONSP (content))
e34b1164 1686 {
30569699
PE
1687 attrib = XCAR (content);
1688 value = XCDR (content);
1689 if (!INTEGERP (attrib) || !INTEGERP (value))
1690 continue;
1691 reg[rrr] = XINT(value);
1692 break;
e34b1164 1693 }
30569699
PE
1694 else if (SYMBOLP (content))
1695 CCL_CALL_FOR_MAP_INSTRUCTION (content, ic);
1696 else
1697 reg[RRR] = -1;
e34b1164
KH
1698 }
1699 break;
177c0ea7 1700
e34b1164
KH
1701 default:
1702 CCL_INVALID_CMD;
1703 }
1704 break;
1705
4ed46869
KH
1706 default:
1707 CCL_INVALID_CMD;
1708 }
1709 }
1710
1711 ccl_error_handler:
0fb94c7f
EZ
1712 /* The suppress_error member is set when e.g. a CCL-based coding
1713 system is used for terminal output. */
1714 if (!ccl->suppress_error && destination)
4ed46869
KH
1715 {
1716 /* We can insert an error message only if DESTINATION is
1717 specified and we still have a room to store the message
1718 there. */
1719 char msg[256];
1720 int msglen;
1721
12abd7d1
KH
1722 if (!dst)
1723 dst = destination;
1724
4ed46869
KH
1725 switch (ccl->status)
1726 {
1727 case CCL_STAT_INVALID_CMD:
1728 sprintf(msg, "\nCCL: Invalid command %x (ccl_code = %x) at %d.",
519bf146 1729 code & 0x1F, code, this_ic);
4ed46869
KH
1730#ifdef CCL_DEBUG
1731 {
1732 int i = ccl_backtrace_idx - 1;
1733 int j;
1734
1735 msglen = strlen (msg);
12abd7d1 1736 if (dst + msglen <= (dst_bytes ? dst_end : src))
4ed46869 1737 {
72af86bd 1738 memcpy (dst, msg, msglen);
4ed46869
KH
1739 dst += msglen;
1740 }
1741
1742 for (j = 0; j < CCL_DEBUG_BACKTRACE_LEN; j++, i--)
1743 {
1744 if (i < 0) i = CCL_DEBUG_BACKTRACE_LEN - 1;
1745 if (ccl_backtrace_table[i] == 0)
1746 break;
1747 sprintf(msg, " %d", ccl_backtrace_table[i]);
1748 msglen = strlen (msg);
12abd7d1 1749 if (dst + msglen > (dst_bytes ? dst_end : src))
4ed46869 1750 break;
72af86bd 1751 memcpy (dst, msg, msglen);
4ed46869
KH
1752 dst += msglen;
1753 }
12abd7d1 1754 goto ccl_finish;
4ed46869 1755 }
4ed46869 1756#endif
12abd7d1 1757 break;
4ed46869
KH
1758
1759 case CCL_STAT_QUIT:
74215b55
KH
1760 if (! ccl->quit_silently)
1761 sprintf(msg, "\nCCL: Quited.");
4ed46869
KH
1762 break;
1763
1764 default:
6b61353c 1765 sprintf(msg, "\nCCL: Unknown error type (%d)", ccl->status);
4ed46869
KH
1766 }
1767
1768 msglen = strlen (msg);
c10842ea 1769 if (dst + msglen <= dst_end)
4ed46869 1770 {
c10842ea
KH
1771 for (i = 0; i < msglen; i++)
1772 *dst++ = msg[i];
4ed46869 1773 }
177c0ea7 1774
31165028
KH
1775 if (ccl->status == CCL_STAT_INVALID_CMD)
1776 {
8a1ae4dd
GM
1777#if 0 /* If the remaining bytes contain 0x80..0x9F, copying them
1778 results in an invalid multibyte sequence. */
1779
31165028
KH
1780 /* Copy the remaining source data. */
1781 int i = src_end - src;
1782 if (dst_bytes && (dst_end - dst) < i)
1783 i = dst_end - dst;
72af86bd 1784 memcpy (dst, src, i);
31165028
KH
1785 src += i;
1786 dst += i;
8a1ae4dd
GM
1787#else
1788 /* Signal that we've consumed everything. */
1789 src = src_end;
1790#endif
31165028 1791 }
4ed46869
KH
1792 }
1793
1794 ccl_finish:
1795 ccl->ic = ic;
c13362d8
KH
1796 ccl->stack_idx = stack_idx;
1797 ccl->prog = ccl_prog;
c10842ea 1798 ccl->consumed = src - source;
4e3bb4f3
KH
1799 if (dst != NULL)
1800 ccl->produced = dst - destination;
1801 else
1802 ccl->produced = 0;
4ed46869
KH
1803}
1804
5232fa7b
KH
1805/* Resolve symbols in the specified CCL code (Lisp vector). This
1806 function converts symbols of code conversion maps and character
1807 translation tables embeded in the CCL code into their ID numbers.
1808
1809 The return value is a vector (CCL itself or a new vector in which
1810 all symbols are resolved), Qt if resolving of some symbol failed,
1811 or nil if CCL contains invalid data. */
1812
1813static Lisp_Object
971de7fb 1814resolve_symbol_ccl_program (Lisp_Object ccl)
5232fa7b
KH
1815{
1816 int i, veclen, unresolved = 0;
1817 Lisp_Object result, contents, val;
1818
1819 result = ccl;
64ef2921 1820 veclen = ASIZE (result);
5232fa7b
KH
1821
1822 for (i = 0; i < veclen; i++)
1823 {
64ef2921 1824 contents = AREF (result, i);
5232fa7b
KH
1825 if (INTEGERP (contents))
1826 continue;
1827 else if (CONSP (contents)
03699b14
KR
1828 && SYMBOLP (XCAR (contents))
1829 && SYMBOLP (XCDR (contents)))
5232fa7b
KH
1830 {
1831 /* This is the new style for embedding symbols. The form is
1832 (SYMBOL . PROPERTY). (get SYMBOL PROPERTY) should give
1833 an index number. */
1834
1835 if (EQ (result, ccl))
1836 result = Fcopy_sequence (ccl);
1837
03699b14 1838 val = Fget (XCAR (contents), XCDR (contents));
5232fa7b 1839 if (NATNUMP (val))
3ae565b3 1840 ASET (result, i, val);
5232fa7b
KH
1841 else
1842 unresolved = 1;
1843 continue;
1844 }
1845 else if (SYMBOLP (contents))
1846 {
1847 /* This is the old style for embedding symbols. This style
1848 may lead to a bug if, for instance, a translation table
1849 and a code conversion map have the same name. */
1850 if (EQ (result, ccl))
1851 result = Fcopy_sequence (ccl);
1852
1853 val = Fget (contents, Qtranslation_table_id);
1854 if (NATNUMP (val))
3ae565b3 1855 ASET (result, i, val);
5232fa7b
KH
1856 else
1857 {
1858 val = Fget (contents, Qcode_conversion_map_id);
1859 if (NATNUMP (val))
3ae565b3 1860 ASET (result, i, val);
5232fa7b
KH
1861 else
1862 {
1863 val = Fget (contents, Qccl_program_idx);
1864 if (NATNUMP (val))
3ae565b3 1865 ASET (result, i, val);
5232fa7b
KH
1866 else
1867 unresolved = 1;
1868 }
1869 }
1870 continue;
1871 }
1872 return Qnil;
1873 }
1874
1875 return (unresolved ? Qt : result);
1876}
1877
1878/* Return the compiled code (vector) of CCL program CCL_PROG.
1879 CCL_PROG is a name (symbol) of the program or already compiled
1880 code. If necessary, resolve symbols in the compiled code to index
1881 numbers. If we failed to get the compiled code or to resolve
1882 symbols, return Qnil. */
1883
1884static Lisp_Object
971de7fb 1885ccl_get_compiled_code (Lisp_Object ccl_prog, int *idx)
5232fa7b
KH
1886{
1887 Lisp_Object val, slot;
1888
1889 if (VECTORP (ccl_prog))
1890 {
1891 val = resolve_symbol_ccl_program (ccl_prog);
2a69c66e 1892 *idx = -1;
5232fa7b
KH
1893 return (VECTORP (val) ? val : Qnil);
1894 }
1895 if (!SYMBOLP (ccl_prog))
1896 return Qnil;
1897
1898 val = Fget (ccl_prog, Qccl_program_idx);
1899 if (! NATNUMP (val)
64ef2921 1900 || XINT (val) >= ASIZE (Vccl_program_table))
5232fa7b 1901 return Qnil;
64ef2921 1902 slot = AREF (Vccl_program_table, XINT (val));
5232fa7b 1903 if (! VECTORP (slot)
2a69c66e 1904 || ASIZE (slot) != 4
64ef2921 1905 || ! VECTORP (AREF (slot, 1)))
5232fa7b 1906 return Qnil;
2a69c66e 1907 *idx = XINT (val);
64ef2921 1908 if (NILP (AREF (slot, 2)))
5232fa7b 1909 {
64ef2921 1910 val = resolve_symbol_ccl_program (AREF (slot, 1));
5232fa7b
KH
1911 if (! VECTORP (val))
1912 return Qnil;
3ae565b3
SM
1913 ASET (slot, 1, val);
1914 ASET (slot, 2, Qt);
5232fa7b 1915 }
64ef2921 1916 return AREF (slot, 1);
5232fa7b
KH
1917}
1918
4ed46869 1919/* Setup fields of the structure pointed by CCL appropriately for the
5232fa7b
KH
1920 execution of CCL program CCL_PROG. CCL_PROG is the name (symbol)
1921 of the CCL program or the already compiled code (vector).
1922 Return 0 if we succeed this setup, else return -1.
1923
1924 If CCL_PROG is nil, we just reset the structure pointed by CCL. */
1925int
971de7fb 1926setup_ccl_program (struct ccl_program *ccl, Lisp_Object ccl_prog)
4ed46869
KH
1927{
1928 int i;
1929
5232fa7b 1930 if (! NILP (ccl_prog))
ad3d1b1d 1931 {
5232fa7b 1932 struct Lisp_Vector *vp;
ad3d1b1d 1933
2a69c66e 1934 ccl_prog = ccl_get_compiled_code (ccl_prog, &ccl->idx);
5232fa7b
KH
1935 if (! VECTORP (ccl_prog))
1936 return -1;
1937 vp = XVECTOR (ccl_prog);
eab3844f 1938 ccl->size = vp->header.size;
ad3d1b1d
KH
1939 ccl->prog = vp->contents;
1940 ccl->eof_ic = XINT (vp->contents[CCL_HEADER_EOF]);
1941 ccl->buf_magnification = XINT (vp->contents[CCL_HEADER_BUF_MAG]);
2a69c66e
KH
1942 if (ccl->idx >= 0)
1943 {
1944 Lisp_Object slot;
1945
1946 slot = AREF (Vccl_program_table, ccl->idx);
1947 ASET (slot, 3, Qnil);
1948 }
ad3d1b1d 1949 }
4ed46869 1950 ccl->ic = CCL_HEADER_MAIN;
4ed46869
KH
1951 for (i = 0; i < 8; i++)
1952 ccl->reg[i] = 0;
1953 ccl->last_block = 0;
e34b1164 1954 ccl->private_state = 0;
4ed46869 1955 ccl->status = 0;
c13362d8 1956 ccl->stack_idx = 0;
ae08ba36 1957 ccl->suppress_error = 0;
fd40a25f 1958 ccl->eight_bit_control = 0;
74215b55 1959 ccl->quit_silently = 0;
5232fa7b 1960 return 0;
4ed46869
KH
1961}
1962
2a69c66e 1963
a7ca3326 1964DEFUN ("ccl-program-p", Fccl_program_p, Sccl_program_p, 1, 1, 0,
fdb82f93 1965 doc: /* Return t if OBJECT is a CCL program name or a compiled CCL program code.
78edd3b7 1966See the documentation of `define-ccl-program' for the detail of CCL program. */)
6f704c76 1967 (Lisp_Object object)
6ae21908 1968{
5232fa7b 1969 Lisp_Object val;
6ae21908 1970
5232fa7b 1971 if (VECTORP (object))
6ae21908 1972 {
5232fa7b
KH
1973 val = resolve_symbol_ccl_program (object);
1974 return (VECTORP (val) ? Qt : Qnil);
6ae21908 1975 }
5232fa7b
KH
1976 if (!SYMBOLP (object))
1977 return Qnil;
6ae21908 1978
5232fa7b
KH
1979 val = Fget (object, Qccl_program_idx);
1980 return ((! NATNUMP (val)
64ef2921 1981 || XINT (val) >= ASIZE (Vccl_program_table))
5232fa7b 1982 ? Qnil : Qt);
6ae21908
KH
1983}
1984
4ed46869 1985DEFUN ("ccl-execute", Fccl_execute, Sccl_execute, 2, 2, 0,
fdb82f93
PJ
1986 doc: /* Execute CCL-PROGRAM with registers initialized by REGISTERS.
1987
1988CCL-PROGRAM is a CCL program name (symbol)
1989or compiled code generated by `ccl-compile' (for backward compatibility.
1990In the latter case, the execution overhead is bigger than in the former).
1991No I/O commands should appear in CCL-PROGRAM.
1992
1993REGISTERS is a vector of [R0 R1 ... R7] where RN is an initial value
1994for the Nth register.
1995
1996As side effect, each element of REGISTERS holds the value of
1997the corresponding register after the execution.
1998
1999See the documentation of `define-ccl-program' for a definition of CCL
2000programs. */)
6f704c76 2001 (Lisp_Object ccl_prog, Lisp_Object reg)
4ed46869
KH
2002{
2003 struct ccl_program ccl;
2004 int i;
2005
5232fa7b
KH
2006 if (setup_ccl_program (&ccl, ccl_prog) < 0)
2007 error ("Invalid CCL program");
6ae21908 2008
b7826503 2009 CHECK_VECTOR (reg);
64ef2921 2010 if (ASIZE (reg) != 8)
d7e1fe1f 2011 error ("Length of vector REGISTERS is not 8");
4ed46869 2012
4ed46869 2013 for (i = 0; i < 8; i++)
64ef2921
SM
2014 ccl.reg[i] = (INTEGERP (AREF (reg, i))
2015 ? XINT (AREF (reg, i))
4ed46869
KH
2016 : 0);
2017
bda731af 2018 ccl_driver (&ccl, NULL, NULL, 0, 0, Qnil);
4ed46869
KH
2019 QUIT;
2020 if (ccl.status != CCL_STAT_SUCCESS)
2021 error ("Error in CCL program at %dth code", ccl.ic);
2022
2023 for (i = 0; i < 8; i++)
3ae565b3 2024 ASET (reg, i, make_number (ccl.reg[i]));
4ed46869
KH
2025 return Qnil;
2026}
2027
2028DEFUN ("ccl-execute-on-string", Fccl_execute_on_string, Sccl_execute_on_string,
39a68837 2029 3, 5, 0,
fdb82f93
PJ
2030 doc: /* Execute CCL-PROGRAM with initial STATUS on STRING.
2031
2a0bd758 2032CCL-PROGRAM is a symbol registered by `register-ccl-program',
fdb82f93
PJ
2033or a compiled code generated by `ccl-compile' (for backward compatibility,
2034in this case, the execution is slower).
2035
2036Read buffer is set to STRING, and write buffer is allocated automatically.
2037
2038STATUS is a vector of [R0 R1 ... R7 IC], where
2039 R0..R7 are initial values of corresponding registers,
2040 IC is the instruction counter specifying from where to start the program.
2041If R0..R7 are nil, they are initialized to 0.
2042If IC is nil, it is initialized to head of the CCL program.
2043
2044If optional 4th arg CONTINUE is non-nil, keep IC on read operation
51e4f4a8 2045when read buffer is exhausted, else, IC is always set to the end of
fdb82f93
PJ
2046CCL-PROGRAM on exit.
2047
2048It returns the contents of write buffer as a string,
2049 and as side effect, STATUS is updated.
2050If the optional 5th arg UNIBYTE-P is non-nil, the returned string
2051is a unibyte string. By default it is a multibyte string.
2052
2a0bd758
JB
2053See the documentation of `define-ccl-program' for the detail of CCL program.
2054usage: (ccl-execute-on-string CCL-PROGRAM STATUS STRING &optional CONTINUE UNIBYTE-P) */)
6f704c76 2055 (Lisp_Object ccl_prog, Lisp_Object status, Lisp_Object str, Lisp_Object contin, Lisp_Object unibyte_p)
4ed46869
KH
2056{
2057 Lisp_Object val;
2058 struct ccl_program ccl;
c10842ea 2059 int i;
a53e2e89 2060 EMACS_INT outbufsize;
c10842ea 2061 unsigned char *outbuf, *outp;
ace1712c 2062 EMACS_INT str_chars, str_bytes;
c10842ea
KH
2063#define CCL_EXECUTE_BUF_SIZE 1024
2064 int source[CCL_EXECUTE_BUF_SIZE], destination[CCL_EXECUTE_BUF_SIZE];
ace1712c 2065 EMACS_INT consumed_chars, consumed_bytes, produced_chars;
6ae21908 2066
5232fa7b
KH
2067 if (setup_ccl_program (&ccl, ccl_prog) < 0)
2068 error ("Invalid CCL program");
4ed46869 2069
b7826503 2070 CHECK_VECTOR (status);
64ef2921 2071 if (ASIZE (status) != 9)
5232fa7b 2072 error ("Length of vector STATUS is not 9");
b7826503 2073 CHECK_STRING (str);
4ed46869 2074
8f924df7
KH
2075 str_chars = SCHARS (str);
2076 str_bytes = SBYTES (str);
5232fa7b 2077
4ed46869
KH
2078 for (i = 0; i < 8; i++)
2079 {
64ef2921 2080 if (NILP (AREF (status, i)))
3ae565b3 2081 ASET (status, i, make_number (0));
64ef2921
SM
2082 if (INTEGERP (AREF (status, i)))
2083 ccl.reg[i] = XINT (AREF (status, i));
4ed46869 2084 }
64ef2921 2085 if (INTEGERP (AREF (status, i)))
4ed46869 2086 {
64ef2921 2087 i = XFASTINT (AREF (status, 8));
4ed46869
KH
2088 if (ccl.ic < i && i < ccl.size)
2089 ccl.ic = i;
2090 }
4ed46869 2091
c10842ea
KH
2092 outbufsize = (ccl.buf_magnification
2093 ? str_bytes * ccl.buf_magnification + 256
2094 : str_bytes + 256);
2095 outp = outbuf = (unsigned char *) xmalloc (outbufsize);
2096
2097 consumed_chars = consumed_bytes = 0;
2098 produced_chars = 0;
99e293b5 2099 while (1)
a3d8fcf2 2100 {
8f924df7
KH
2101 const unsigned char *p = SDATA (str) + consumed_bytes;
2102 const unsigned char *endp = SDATA (str) + str_bytes;
fb90da1b 2103 int j = 0;
c10842ea
KH
2104 int *src, src_size;
2105
2106 if (endp - p == str_chars - consumed_chars)
fb90da1b
PE
2107 while (j < CCL_EXECUTE_BUF_SIZE && p < endp)
2108 source[j++] = *p++;
c10842ea 2109 else
fb90da1b
PE
2110 while (j < CCL_EXECUTE_BUF_SIZE && p < endp)
2111 source[j++] = STRING_CHAR_ADVANCE (p);
2112 consumed_chars += j;
8f924df7 2113 consumed_bytes = p - SDATA (str);
c10842ea
KH
2114
2115 if (consumed_bytes == str_bytes)
2116 ccl.last_block = NILP (contin);
2117 src = source;
fb90da1b 2118 src_size = j;
c10842ea
KH
2119 while (1)
2120 {
bda731af
KH
2121 ccl_driver (&ccl, src, destination, src_size, CCL_EXECUTE_BUF_SIZE,
2122 Qnil);
c10842ea
KH
2123 produced_chars += ccl.produced;
2124 if (NILP (unibyte_p))
2125 {
2126 if (outp - outbuf + MAX_MULTIBYTE_LENGTH * ccl.produced
2127 > outbufsize)
2128 {
ace1712c 2129 EMACS_INT offset = outp - outbuf;
c10842ea
KH
2130 outbufsize += MAX_MULTIBYTE_LENGTH * ccl.produced;
2131 outbuf = (unsigned char *) xrealloc (outbuf, outbufsize);
2132 outp = outbuf + offset;
2133 }
fb90da1b
PE
2134 for (j = 0; j < ccl.produced; j++)
2135 CHAR_STRING_ADVANCE (destination[j], outp);
c10842ea
KH
2136 }
2137 else
2138 {
2139 if (outp - outbuf + ccl.produced > outbufsize)
2140 {
ace1712c 2141 EMACS_INT offset = outp - outbuf;
c10842ea
KH
2142 outbufsize += ccl.produced;
2143 outbuf = (unsigned char *) xrealloc (outbuf, outbufsize);
2144 outp = outbuf + offset;
2145 }
fb90da1b
PE
2146 for (j = 0; j < ccl.produced; j++)
2147 *outp++ = destination[j];
c10842ea
KH
2148 }
2149 src += ccl.consumed;
2150 src_size -= ccl.consumed;
99e293b5
KH
2151 if (ccl.status != CCL_STAT_SUSPEND_BY_DST)
2152 break;
c10842ea 2153 }
a3d8fcf2 2154
edeef421
KH
2155 if (ccl.status != CCL_STAT_SUSPEND_BY_SRC
2156 || str_chars == consumed_chars)
c10842ea 2157 break;
a3d8fcf2 2158 }
a3d8fcf2 2159
edeef421 2160 if (ccl.status == CCL_STAT_INVALID_CMD)
4ed46869 2161 error ("Error in CCL program at %dth code", ccl.ic);
edeef421
KH
2162 if (ccl.status == CCL_STAT_QUIT)
2163 error ("CCL program interrupted at %dth code", ccl.ic);
4ed46869 2164
c10842ea 2165 for (i = 0; i < 8; i++)
c6589bbd
KH
2166 ASET (status, i, make_number (ccl.reg[i]));
2167 ASET (status, 8, make_number (ccl.ic));
c10842ea
KH
2168
2169 if (NILP (unibyte_p))
2170 val = make_multibyte_string ((char *) outbuf, produced_chars,
2171 outp - outbuf);
2172 else
2173 val = make_unibyte_string ((char *) outbuf, produced_chars);
2174 xfree (outbuf);
4ed46869
KH
2175
2176 return val;
2177}
2178
2179DEFUN ("register-ccl-program", Fregister_ccl_program, Sregister_ccl_program,
2180 2, 2, 0,
2a0bd758
JB
2181 doc: /* Register CCL program CCL-PROG as NAME in `ccl-program-table'.
2182CCL-PROG should be a compiled CCL program (vector), or nil.
fdb82f93
PJ
2183If it is nil, just reserve NAME as a CCL program name.
2184Return index number of the registered CCL program. */)
6f704c76 2185 (Lisp_Object name, Lisp_Object ccl_prog)
4ed46869 2186{
64ef2921 2187 int len = ASIZE (Vccl_program_table);
5232fa7b
KH
2188 int idx;
2189 Lisp_Object resolved;
4ed46869 2190
b7826503 2191 CHECK_SYMBOL (name);
5232fa7b 2192 resolved = Qnil;
4ed46869 2193 if (!NILP (ccl_prog))
6ae21908 2194 {
b7826503 2195 CHECK_VECTOR (ccl_prog);
5232fa7b 2196 resolved = resolve_symbol_ccl_program (ccl_prog);
4d247a1f
KH
2197 if (NILP (resolved))
2198 error ("Error in CCL program");
2199 if (VECTORP (resolved))
5232fa7b
KH
2200 {
2201 ccl_prog = resolved;
2202 resolved = Qt;
2203 }
4d247a1f
KH
2204 else
2205 resolved = Qnil;
6ae21908 2206 }
5232fa7b
KH
2207
2208 for (idx = 0; idx < len; idx++)
4ed46869 2209 {
5232fa7b 2210 Lisp_Object slot;
4ed46869 2211
64ef2921 2212 slot = AREF (Vccl_program_table, idx);
5232fa7b 2213 if (!VECTORP (slot))
78edd3b7 2214 /* This is the first unused slot. Register NAME here. */
4ed46869
KH
2215 break;
2216
64ef2921 2217 if (EQ (name, AREF (slot, 0)))
4ed46869 2218 {
5232fa7b 2219 /* Update this slot. */
2a69c66e
KH
2220 ASET (slot, 1, ccl_prog);
2221 ASET (slot, 2, resolved);
2222 ASET (slot, 3, Qt);
5232fa7b 2223 return make_number (idx);
4ed46869
KH
2224 }
2225 }
2226
5232fa7b 2227 if (idx == len)
1d153206
EZ
2228 /* Extend the table. */
2229 Vccl_program_table = larger_vector (Vccl_program_table, len * 2, Qnil);
4ed46869 2230
5232fa7b
KH
2231 {
2232 Lisp_Object elt;
2233
2a69c66e
KH
2234 elt = Fmake_vector (make_number (4), Qnil);
2235 ASET (elt, 0, name);
2236 ASET (elt, 1, ccl_prog);
2237 ASET (elt, 2, resolved);
2238 ASET (elt, 3, Qt);
2239 ASET (Vccl_program_table, idx, elt);
5232fa7b
KH
2240 }
2241
2242 Fput (name, Qccl_program_idx, make_number (idx));
2243 return make_number (idx);
4ed46869
KH
2244}
2245
8146262a
KH
2246/* Register code conversion map.
2247 A code conversion map consists of numbers, Qt, Qnil, and Qlambda.
d617f6df
DL
2248 The first element is the start code point.
2249 The other elements are mapped numbers.
8146262a
KH
2250 Symbol t means to map to an original number before mapping.
2251 Symbol nil means that the corresponding element is empty.
d617f6df 2252 Symbol lambda means to terminate mapping here.
e34b1164
KH
2253*/
2254
8146262a
KH
2255DEFUN ("register-code-conversion-map", Fregister_code_conversion_map,
2256 Sregister_code_conversion_map,
e34b1164 2257 2, 2, 0,
fdb82f93
PJ
2258 doc: /* Register SYMBOL as code conversion map MAP.
2259Return index number of the registered map. */)
6f704c76 2260 (Lisp_Object symbol, Lisp_Object map)
e34b1164 2261{
64ef2921 2262 int len = ASIZE (Vcode_conversion_map_vector);
e34b1164 2263 int i;
fb90da1b 2264 Lisp_Object idx;
e34b1164 2265
b7826503
PJ
2266 CHECK_SYMBOL (symbol);
2267 CHECK_VECTOR (map);
177c0ea7 2268
e34b1164
KH
2269 for (i = 0; i < len; i++)
2270 {
64ef2921 2271 Lisp_Object slot = AREF (Vcode_conversion_map_vector, i);
e34b1164
KH
2272
2273 if (!CONSP (slot))
2274 break;
2275
03699b14 2276 if (EQ (symbol, XCAR (slot)))
e34b1164 2277 {
fb90da1b 2278 idx = make_number (i);
f3fbd155 2279 XSETCDR (slot, map);
8146262a 2280 Fput (symbol, Qcode_conversion_map, map);
fb90da1b
PE
2281 Fput (symbol, Qcode_conversion_map_id, idx);
2282 return idx;
e34b1164
KH
2283 }
2284 }
2285
2286 if (i == len)
2a1aad57
EZ
2287 Vcode_conversion_map_vector = larger_vector (Vcode_conversion_map_vector,
2288 len * 2, Qnil);
e34b1164 2289
fb90da1b 2290 idx = make_number (i);
8146262a 2291 Fput (symbol, Qcode_conversion_map, map);
fb90da1b 2292 Fput (symbol, Qcode_conversion_map_id, idx);
3ae565b3 2293 ASET (Vcode_conversion_map_vector, i, Fcons (symbol, map));
fb90da1b 2294 return idx;
e34b1164
KH
2295}
2296
2297
dfcf069d 2298void
971de7fb 2299syms_of_ccl (void)
4ed46869
KH
2300{
2301 staticpro (&Vccl_program_table);
6703ac4f 2302 Vccl_program_table = Fmake_vector (make_number (32), Qnil);
4ed46869 2303
d67b4f80 2304 Qccl = intern_c_string ("ccl");
c10842ea
KH
2305 staticpro (&Qccl);
2306
d67b4f80 2307 Qcclp = intern_c_string ("cclp");
c10842ea
KH
2308 staticpro (&Qcclp);
2309
d67b4f80 2310 Qccl_program = intern_c_string ("ccl-program");
6ae21908
KH
2311 staticpro (&Qccl_program);
2312
d67b4f80 2313 Qccl_program_idx = intern_c_string ("ccl-program-idx");
6ae21908 2314 staticpro (&Qccl_program_idx);
e34b1164 2315
d67b4f80 2316 Qcode_conversion_map = intern_c_string ("code-conversion-map");
8146262a 2317 staticpro (&Qcode_conversion_map);
6ae21908 2318
d67b4f80 2319 Qcode_conversion_map_id = intern_c_string ("code-conversion-map-id");
8146262a 2320 staticpro (&Qcode_conversion_map_id);
6ae21908 2321
29208e82 2322 DEFVAR_LISP ("code-conversion-map-vector", Vcode_conversion_map_vector,
fdb82f93 2323 doc: /* Vector of code conversion maps. */);
8146262a 2324 Vcode_conversion_map_vector = Fmake_vector (make_number (16), Qnil);
e34b1164 2325
29208e82 2326 DEFVAR_LISP ("font-ccl-encoder-alist", Vfont_ccl_encoder_alist,
fdb82f93
PJ
2327 doc: /* Alist of fontname patterns vs corresponding CCL program.
2328Each element looks like (REGEXP . CCL-CODE),
2329 where CCL-CODE is a compiled CCL program.
2330When a font whose name matches REGEXP is used for displaying a character,
2331 CCL-CODE is executed to calculate the code point in the font
2332 from the charset number and position code(s) of the character which are set
2333 in CCL registers R0, R1, and R2 before the execution.
2334The code point in the font is set in CCL registers R1 and R2
2335 when the execution terminated.
2336 If the font is single-byte font, the register R2 is not used. */);
4ed46869
KH
2337 Vfont_ccl_encoder_alist = Qnil;
2338
29208e82 2339 DEFVAR_LISP ("translation-hash-table-vector", Vtranslation_hash_table_vector,
d80dc57e
DL
2340 doc: /* Vector containing all translation hash tables ever defined.
2341Comprises pairs (SYMBOL . TABLE) where SYMBOL and TABLE were set up by calls
2342to `define-translation-hash-table'. The vector is indexed by the table id
2343used by CCL. */);
2344 Vtranslation_hash_table_vector = Qnil;
2345
5232fa7b 2346 defsubr (&Sccl_program_p);
4ed46869
KH
2347 defsubr (&Sccl_execute);
2348 defsubr (&Sccl_execute_on_string);
2349 defsubr (&Sregister_ccl_program);
8146262a 2350 defsubr (&Sregister_code_conversion_map);
4ed46869 2351}