(ccl_coding_driver): Setup ccl->last_block properly.
[bpt/emacs.git] / src / ccl.c
CommitLineData
4ed46869 1/* CCL (Code Conversion Language) interpreter.
75c8c592
RS
2 Copyright (C) 1995, 1997 Electrotechnical Laboratory, JAPAN.
3 Licensed to the Free Software Foundation.
4ed46869 4
369314dc
KH
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
4ed46869 11
369314dc
KH
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
4ed46869 16
369314dc
KH
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
4ed46869
KH
21
22#include <stdio.h>
23
24#ifdef emacs
25
26#include <config.h>
dfcf069d
AS
27
28#ifdef STDC_HEADERS
29#include <stdlib.h>
30#endif
31
4ed46869
KH
32#include "lisp.h"
33#include "charset.h"
34#include "ccl.h"
35#include "coding.h"
36
37#else /* not emacs */
38
39#include "mulelib.h"
40
41#endif /* not emacs */
42
20398ea4 43/* This contains all code conversion map available to CCL. */
8146262a 44Lisp_Object Vcode_conversion_map_vector;
e34b1164 45
4ed46869
KH
46/* Alist of fontname patterns vs corresponding CCL program. */
47Lisp_Object Vfont_ccl_encoder_alist;
48
6ae21908
KH
49/* This symbol is a property which assocates with ccl program vector.
50 Ex: (get 'ccl-big5-encoder 'ccl-program) returns ccl program vector. */
e34b1164
KH
51Lisp_Object Qccl_program;
52
8146262a
KH
53/* These symbols are properties which associate with code conversion
54 map and their ID respectively. */
55Lisp_Object Qcode_conversion_map;
56Lisp_Object Qcode_conversion_map_id;
e34b1164 57
6ae21908
KH
58/* Symbols of ccl program have this property, a value of the property
59 is an index for Vccl_protram_table. */
60Lisp_Object Qccl_program_idx;
61
4ed46869
KH
62/* Vector of CCL program names vs corresponding program data. */
63Lisp_Object Vccl_program_table;
64
65/* CCL (Code Conversion Language) is a simple language which has
66 operations on one input buffer, one output buffer, and 7 registers.
67 The syntax of CCL is described in `ccl.el'. Emacs Lisp function
68 `ccl-compile' compiles a CCL program and produces a CCL code which
69 is a vector of integers. The structure of this vector is as
70 follows: The 1st element: buffer-magnification, a factor for the
71 size of output buffer compared with the size of input buffer. The
72 2nd element: address of CCL code to be executed when encountered
73 with end of input stream. The 3rd and the remaining elements: CCL
74 codes. */
75
76/* Header of CCL compiled code */
77#define CCL_HEADER_BUF_MAG 0
78#define CCL_HEADER_EOF 1
79#define CCL_HEADER_MAIN 2
80
81/* CCL code is a sequence of 28-bit non-negative integers (i.e. the
82 MSB is always 0), each contains CCL command and/or arguments in the
83 following format:
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
96 where, `cc...c' is a non-negative integer indicating constant value
97 (the left most `c' is always 0) or an absolute jump address, `RRR'
98 and `rrr' are CCL register number, `XXXXX' is one of the following
99 CCL commands. */
100
101/* CCL commands
102
103 Each comment fields shows one or more lines for command syntax and
104 the following lines for semantics of the command. In semantics, IC
105 stands for Instruction Counter. */
106
107#define CCL_SetRegister 0x00 /* Set register a register value:
108 1:00000000000000000RRRrrrXXXXX
109 ------------------------------
110 reg[rrr] = reg[RRR];
111 */
112
113#define CCL_SetShortConst 0x01 /* Set register a short constant value:
114 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
115 ------------------------------
116 reg[rrr] = CCCCCCCCCCCCCCCCCCC;
117 */
118
119#define CCL_SetConst 0x02 /* Set register a constant value:
120 1:00000000000000000000rrrXXXXX
121 2:CONSTANT
122 ------------------------------
123 reg[rrr] = CONSTANT;
124 IC++;
125 */
126
127#define CCL_SetArray 0x03 /* Set register an element of array:
128 1:CCCCCCCCCCCCCCCCCRRRrrrXXXXX
129 2:ELEMENT[0]
130 3:ELEMENT[1]
131 ...
132 ------------------------------
133 if (0 <= reg[RRR] < CC..C)
134 reg[rrr] = ELEMENT[reg[RRR]];
135 IC += CC..C;
136 */
137
138#define CCL_Jump 0x04 /* Jump:
139 1:A--D--D--R--E--S--S-000XXXXX
140 ------------------------------
141 IC += ADDRESS;
142 */
143
144/* Note: If CC..C is greater than 0, the second code is omitted. */
145
146#define CCL_JumpCond 0x05 /* Jump conditional:
147 1:A--D--D--R--E--S--S-rrrXXXXX
148 ------------------------------
149 if (!reg[rrr])
150 IC += ADDRESS;
151 */
152
153
154#define CCL_WriteRegisterJump 0x06 /* Write register and jump:
155 1:A--D--D--R--E--S--S-rrrXXXXX
156 ------------------------------
157 write (reg[rrr]);
158 IC += ADDRESS;
159 */
160
161#define CCL_WriteRegisterReadJump 0x07 /* Write register, read, and jump:
162 1:A--D--D--R--E--S--S-rrrXXXXX
163 2:A--D--D--R--E--S--S-rrrYYYYY
164 -----------------------------
165 write (reg[rrr]);
166 IC++;
167 read (reg[rrr]);
168 IC += ADDRESS;
169 */
170/* Note: If read is suspended, the resumed execution starts from the
171 second code (YYYYY == CCL_ReadJump). */
172
173#define CCL_WriteConstJump 0x08 /* Write constant and jump:
174 1:A--D--D--R--E--S--S-000XXXXX
175 2:CONST
176 ------------------------------
177 write (CONST);
178 IC += ADDRESS;
179 */
180
181#define CCL_WriteConstReadJump 0x09 /* Write constant, read, and jump:
182 1:A--D--D--R--E--S--S-rrrXXXXX
183 2:CONST
184 3:A--D--D--R--E--S--S-rrrYYYYY
185 -----------------------------
186 write (CONST);
187 IC += 2;
188 read (reg[rrr]);
189 IC += ADDRESS;
190 */
191/* Note: If read is suspended, the resumed execution starts from the
192 second code (YYYYY == CCL_ReadJump). */
193
194#define CCL_WriteStringJump 0x0A /* Write string and jump:
195 1:A--D--D--R--E--S--S-000XXXXX
196 2:LENGTH
197 3:0000STRIN[0]STRIN[1]STRIN[2]
198 ...
199 ------------------------------
200 write_string (STRING, LENGTH);
201 IC += ADDRESS;
202 */
203
204#define CCL_WriteArrayReadJump 0x0B /* Write an array element, read, and jump:
205 1:A--D--D--R--E--S--S-rrrXXXXX
206 2:LENGTH
207 3:ELEMENET[0]
208 4:ELEMENET[1]
209 ...
210 N:A--D--D--R--E--S--S-rrrYYYYY
211 ------------------------------
212 if (0 <= reg[rrr] < LENGTH)
213 write (ELEMENT[reg[rrr]]);
214 IC += LENGTH + 2; (... pointing at N+1)
215 read (reg[rrr]);
216 IC += ADDRESS;
217 */
218/* Note: If read is suspended, the resumed execution starts from the
887bfbd7 219 Nth code (YYYYY == CCL_ReadJump). */
4ed46869
KH
220
221#define CCL_ReadJump 0x0C /* Read and jump:
222 1:A--D--D--R--E--S--S-rrrYYYYY
223 -----------------------------
224 read (reg[rrr]);
225 IC += ADDRESS;
226 */
227
228#define CCL_Branch 0x0D /* Jump by branch table:
229 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
230 2:A--D--D--R--E-S-S[0]000XXXXX
231 3:A--D--D--R--E-S-S[1]000XXXXX
232 ...
233 ------------------------------
234 if (0 <= reg[rrr] < CC..C)
235 IC += ADDRESS[reg[rrr]];
236 else
237 IC += ADDRESS[CC..C];
238 */
239
240#define CCL_ReadRegister 0x0E /* Read bytes into registers:
241 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
242 2:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
243 ...
244 ------------------------------
245 while (CCC--)
246 read (reg[rrr]);
247 */
248
249#define CCL_WriteExprConst 0x0F /* write result of expression:
250 1:00000OPERATION000RRR000XXXXX
251 2:CONSTANT
252 ------------------------------
253 write (reg[RRR] OPERATION CONSTANT);
254 IC++;
255 */
256
257/* Note: If the Nth read is suspended, the resumed execution starts
258 from the Nth code. */
259
260#define CCL_ReadBranch 0x10 /* Read one byte into a register,
261 and jump by branch table:
262 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
263 2:A--D--D--R--E-S-S[0]000XXXXX
264 3:A--D--D--R--E-S-S[1]000XXXXX
265 ...
266 ------------------------------
267 read (read[rrr]);
268 if (0 <= reg[rrr] < CC..C)
269 IC += ADDRESS[reg[rrr]];
270 else
271 IC += ADDRESS[CC..C];
272 */
273
274#define CCL_WriteRegister 0x11 /* Write registers:
275 1:CCCCCCCCCCCCCCCCCCCrrrXXXXX
276 2:CCCCCCCCCCCCCCCCCCCrrrXXXXX
277 ...
278 ------------------------------
279 while (CCC--)
280 write (reg[rrr]);
281 ...
282 */
283
284/* Note: If the Nth write is suspended, the resumed execution
285 starts from the Nth code. */
286
287#define CCL_WriteExprRegister 0x12 /* Write result of expression
288 1:00000OPERATIONRrrRRR000XXXXX
289 ------------------------------
290 write (reg[RRR] OPERATION reg[Rrr]);
291 */
292
e34b1164
KH
293#define CCL_Call 0x13 /* Call the CCL program whose ID is
294 (CC..C).
4ed46869
KH
295 1:CCCCCCCCCCCCCCCCCCCC000XXXXX
296 ------------------------------
297 call (CC..C)
298 */
299
300#define CCL_WriteConstString 0x14 /* Write a constant or a string:
301 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
302 [2:0000STRIN[0]STRIN[1]STRIN[2]]
303 [...]
304 -----------------------------
305 if (!rrr)
306 write (CC..C)
307 else
308 write_string (STRING, CC..C);
309 IC += (CC..C + 2) / 3;
310 */
311
312#define CCL_WriteArray 0x15 /* Write an element of array:
313 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
314 2:ELEMENT[0]
315 3:ELEMENT[1]
316 ...
317 ------------------------------
318 if (0 <= reg[rrr] < CC..C)
319 write (ELEMENT[reg[rrr]]);
320 IC += CC..C;
321 */
322
323#define CCL_End 0x16 /* Terminate:
324 1:00000000000000000000000XXXXX
325 ------------------------------
326 terminate ();
327 */
328
329/* The following two codes execute an assignment arithmetic/logical
330 operation. The form of the operation is like REG OP= OPERAND. */
331
332#define CCL_ExprSelfConst 0x17 /* REG OP= constant:
333 1:00000OPERATION000000rrrXXXXX
334 2:CONSTANT
335 ------------------------------
336 reg[rrr] OPERATION= CONSTANT;
337 */
338
339#define CCL_ExprSelfReg 0x18 /* REG1 OP= REG2:
340 1:00000OPERATION000RRRrrrXXXXX
341 ------------------------------
342 reg[rrr] OPERATION= reg[RRR];
343 */
344
345/* The following codes execute an arithmetic/logical operation. The
346 form of the operation is like REG_X = REG_Y OP OPERAND2. */
347
348#define CCL_SetExprConst 0x19 /* REG_X = REG_Y OP constant:
349 1:00000OPERATION000RRRrrrXXXXX
350 2:CONSTANT
351 ------------------------------
352 reg[rrr] = reg[RRR] OPERATION CONSTANT;
353 IC++;
354 */
355
356#define CCL_SetExprReg 0x1A /* REG1 = REG2 OP REG3:
357 1:00000OPERATIONRrrRRRrrrXXXXX
358 ------------------------------
359 reg[rrr] = reg[RRR] OPERATION reg[Rrr];
360 */
361
362#define CCL_JumpCondExprConst 0x1B /* Jump conditional according to
363 an operation on constant:
364 1:A--D--D--R--E--S--S-rrrXXXXX
365 2:OPERATION
366 3:CONSTANT
367 -----------------------------
368 reg[7] = reg[rrr] OPERATION CONSTANT;
369 if (!(reg[7]))
370 IC += ADDRESS;
371 else
372 IC += 2
373 */
374
375#define CCL_JumpCondExprReg 0x1C /* Jump conditional according to
376 an operation on register:
377 1:A--D--D--R--E--S--S-rrrXXXXX
378 2:OPERATION
379 3:RRR
380 -----------------------------
381 reg[7] = reg[rrr] OPERATION reg[RRR];
382 if (!reg[7])
383 IC += ADDRESS;
384 else
385 IC += 2;
386 */
387
388#define CCL_ReadJumpCondExprConst 0x1D /* Read and jump conditional according
389 to an operation on constant:
390 1:A--D--D--R--E--S--S-rrrXXXXX
391 2:OPERATION
392 3:CONSTANT
393 -----------------------------
394 read (reg[rrr]);
395 reg[7] = reg[rrr] OPERATION CONSTANT;
396 if (!reg[7])
397 IC += ADDRESS;
398 else
399 IC += 2;
400 */
401
402#define CCL_ReadJumpCondExprReg 0x1E /* Read and jump conditional according
403 to an operation on register:
404 1:A--D--D--R--E--S--S-rrrXXXXX
405 2:OPERATION
406 3:RRR
407 -----------------------------
408 read (reg[rrr]);
409 reg[7] = reg[rrr] OPERATION reg[RRR];
410 if (!reg[7])
411 IC += ADDRESS;
412 else
413 IC += 2;
414 */
415
416#define CCL_Extention 0x1F /* Extended CCL code
417 1:ExtendedCOMMNDRrrRRRrrrXXXXX
418 2:ARGUEMENT
419 3:...
420 ------------------------------
421 extended_command (rrr,RRR,Rrr,ARGS)
422 */
423
e34b1164 424/*
6ae21908 425 Here after, Extended CCL Instructions.
e34b1164 426 Bit length of extended command is 14.
6ae21908 427 Therefore, the instruction code range is 0..16384(0x3fff).
e34b1164
KH
428 */
429
6ae21908
KH
430/* Read a multibyte characeter.
431 A code point is stored into reg[rrr]. A charset ID is stored into
432 reg[RRR]. */
433
434#define CCL_ReadMultibyteChar2 0x00 /* Read Multibyte Character
435 1:ExtendedCOMMNDRrrRRRrrrXXXXX */
436
437/* Write a multibyte character.
438 Write a character whose code point is reg[rrr] and the charset ID
439 is reg[RRR]. */
440
441#define CCL_WriteMultibyteChar2 0x01 /* Write Multibyte Character
442 1:ExtendedCOMMNDRrrRRRrrrXXXXX */
443
8146262a 444/* Translate a character whose code point is reg[rrr] and the charset
f967223b 445 ID is reg[RRR] by a translation table whose ID is reg[Rrr].
6ae21908 446
8146262a 447 A translated character is set in reg[rrr] (code point) and reg[RRR]
6ae21908
KH
448 (charset ID). */
449
8146262a 450#define CCL_TranslateCharacter 0x02 /* Translate a multibyte character
6ae21908
KH
451 1:ExtendedCOMMNDRrrRRRrrrXXXXX */
452
8146262a 453/* Translate a character whose code point is reg[rrr] and the charset
f967223b 454 ID is reg[RRR] by a translation table whose ID is ARGUMENT.
6ae21908 455
8146262a 456 A translated character is set in reg[rrr] (code point) and reg[RRR]
6ae21908
KH
457 (charset ID). */
458
8146262a
KH
459#define CCL_TranslateCharacterConstTbl 0x03 /* Translate a multibyte character
460 1:ExtendedCOMMNDRrrRRRrrrXXXXX
461 2:ARGUMENT(Translation Table ID)
462 */
6ae21908 463
8146262a
KH
464/* Iterate looking up MAPs for reg[rrr] starting from the Nth (N =
465 reg[RRR]) MAP until some value is found.
6ae21908 466
8146262a 467 Each MAP is a Lisp vector whose element is number, nil, t, or
6ae21908 468 lambda.
8146262a 469 If the element is nil, ignore the map and proceed to the next map.
6ae21908
KH
470 If the element is t or lambda, finish without changing reg[rrr].
471 If the element is a number, set reg[rrr] to the number and finish.
472
8146262a
KH
473 Detail of the map structure is descibed in the comment for
474 CCL_MapMultiple below. */
6ae21908 475
8146262a 476#define CCL_IterateMultipleMap 0x10 /* Iterate multiple maps
6ae21908 477 1:ExtendedCOMMNDXXXRRRrrrXXXXX
8146262a
KH
478 2:NUMBER of MAPs
479 3:MAP-ID1
480 4:MAP-ID2
6ae21908
KH
481 ...
482 */
483
8146262a
KH
484/* Map the code in reg[rrr] by MAPs starting from the Nth (N =
485 reg[RRR]) map.
6ae21908 486
9b27b20d 487 MAPs are supplied in the succeeding CCL codes as follows:
6ae21908 488
8146262a
KH
489 When CCL program gives this nested structure of map to this command:
490 ((MAP-ID11
491 MAP-ID12
492 (MAP-ID121 MAP-ID122 MAP-ID123)
493 MAP-ID13)
494 (MAP-ID21
495 (MAP-ID211 (MAP-ID2111) MAP-ID212)
496 MAP-ID22)),
6ae21908 497 the compiled CCL codes has this sequence:
8146262a 498 CCL_MapMultiple (CCL code of this command)
9b27b20d
KH
499 16 (total number of MAPs and SEPARATORs)
500 -7 (1st SEPARATOR)
8146262a
KH
501 MAP-ID11
502 MAP-ID12
9b27b20d 503 -3 (2nd SEPARATOR)
8146262a
KH
504 MAP-ID121
505 MAP-ID122
506 MAP-ID123
507 MAP-ID13
9b27b20d 508 -7 (3rd SEPARATOR)
8146262a 509 MAP-ID21
9b27b20d 510 -4 (4th SEPARATOR)
8146262a 511 MAP-ID211
9b27b20d 512 -1 (5th SEPARATOR)
8146262a
KH
513 MAP_ID2111
514 MAP-ID212
515 MAP-ID22
6ae21908 516
9b27b20d 517 A value of each SEPARATOR follows this rule:
8146262a
KH
518 MAP-SET := SEPARATOR [(MAP-ID | MAP-SET)]+
519 SEPARATOR := -(number of MAP-IDs and SEPARATORs in the MAP-SET)
6ae21908 520
8146262a 521 (*)....Nest level of MAP-SET must not be over than MAX_MAP_SET_LEVEL.
6ae21908 522
8146262a
KH
523 When some map fails to map (i.e. it doesn't have a value for
524 reg[rrr]), the mapping is treated as identity.
6ae21908 525
8146262a 526 The mapping is iterated for all maps in each map set (set of maps
9b27b20d
KH
527 separated by SEPARATOR) except in the case that lambda is
528 encountered. More precisely, the mapping proceeds as below:
529
530 At first, VAL0 is set to reg[rrr], and it is translated by the
531 first map to VAL1. Then, VAL1 is translated by the next map to
532 VAL2. This mapping is iterated until the last map is used. The
533 result of the mapping is the last value of VAL?.
534
535 But, when VALm is mapped to VALn and VALn is not a number, the
536 mapping proceed as below:
537
538 If VALn is nil, the lastest map is ignored and the mapping of VALm
539 proceed to the next map.
540
541 In VALn is t, VALm is reverted to reg[rrr] and the mapping of VALm
542 proceed to the next map.
543
544 If VALn is lambda, the whole mapping process terminates, and VALm
545 is the result of this mapping.
6ae21908 546
8146262a 547 Each map is a Lisp vector of the following format (a) or (b):
6ae21908
KH
548 (a)......[STARTPOINT VAL1 VAL2 ...]
549 (b)......[t VAL STARTPOINT ENDPOINT],
550 where
8146262a 551 STARTPOINT is an offset to be used for indexing a map,
9b27b20d 552 ENDPOINT is a maximum index number of a map,
6ae21908
KH
553 VAL and VALn is a number, nil, t, or lambda.
554
8146262a
KH
555 Valid index range of a map of type (a) is:
556 STARTPOINT <= index < STARTPOINT + map_size - 1
557 Valid index range of a map of type (b) is:
9b27b20d 558 STARTPOINT <= index < ENDPOINT */
6ae21908 559
8146262a 560#define CCL_MapMultiple 0x11 /* Mapping by multiple code conversion maps
6ae21908
KH
561 1:ExtendedCOMMNDXXXRRRrrrXXXXX
562 2:N-2
563 3:SEPARATOR_1 (< 0)
8146262a
KH
564 4:MAP-ID_1
565 5:MAP-ID_2
6ae21908
KH
566 ...
567 M:SEPARATOR_x (< 0)
8146262a 568 M+1:MAP-ID_y
6ae21908
KH
569 ...
570 N:SEPARATOR_z (< 0)
571 */
572
8146262a 573#define MAX_MAP_SET_LEVEL 20
6ae21908
KH
574
575typedef struct
576{
577 int rest_length;
578 int orig_val;
579} tr_stack;
580
8146262a
KH
581static tr_stack mapping_stack[MAX_MAP_SET_LEVEL];
582static tr_stack *mapping_stack_pointer;
6ae21908 583
8146262a 584#define PUSH_MAPPING_STACK(restlen, orig) \
6ae21908 585{ \
8146262a
KH
586 mapping_stack_pointer->rest_length = (restlen); \
587 mapping_stack_pointer->orig_val = (orig); \
588 mapping_stack_pointer++; \
6ae21908
KH
589}
590
8146262a 591#define POP_MAPPING_STACK(restlen, orig) \
6ae21908 592{ \
8146262a
KH
593 mapping_stack_pointer--; \
594 (restlen) = mapping_stack_pointer->rest_length; \
595 (orig) = mapping_stack_pointer->orig_val; \
6ae21908
KH
596} \
597
8146262a 598#define CCL_MapSingle 0x12 /* Map by single code conversion map
6ae21908 599 1:ExtendedCOMMNDXXXRRRrrrXXXXX
8146262a 600 2:MAP-ID
6ae21908 601 ------------------------------
8146262a
KH
602 Map reg[rrr] by MAP-ID.
603 If some valid mapping is found,
6ae21908
KH
604 set reg[rrr] to the result,
605 else
606 set reg[RRR] to -1.
607 */
4ed46869
KH
608
609/* CCL arithmetic/logical operators. */
610#define CCL_PLUS 0x00 /* X = Y + Z */
611#define CCL_MINUS 0x01 /* X = Y - Z */
612#define CCL_MUL 0x02 /* X = Y * Z */
613#define CCL_DIV 0x03 /* X = Y / Z */
614#define CCL_MOD 0x04 /* X = Y % Z */
615#define CCL_AND 0x05 /* X = Y & Z */
616#define CCL_OR 0x06 /* X = Y | Z */
617#define CCL_XOR 0x07 /* X = Y ^ Z */
618#define CCL_LSH 0x08 /* X = Y << Z */
619#define CCL_RSH 0x09 /* X = Y >> Z */
620#define CCL_LSH8 0x0A /* X = (Y << 8) | Z */
621#define CCL_RSH8 0x0B /* X = Y >> 8, r[7] = Y & 0xFF */
622#define CCL_DIVMOD 0x0C /* X = Y / Z, r[7] = Y % Z */
623#define CCL_LS 0x10 /* X = (X < Y) */
624#define CCL_GT 0x11 /* X = (X > Y) */
625#define CCL_EQ 0x12 /* X = (X == Y) */
626#define CCL_LE 0x13 /* X = (X <= Y) */
627#define CCL_GE 0x14 /* X = (X >= Y) */
628#define CCL_NE 0x15 /* X = (X != Y) */
629
630#define CCL_ENCODE_SJIS 0x16 /* X = HIGHER_BYTE (SJIS (Y, Z))
631 r[7] = LOWER_BYTE (SJIS (Y, Z) */
632#define CCL_DECODE_SJIS 0x17 /* X = HIGHER_BYTE (DE-SJIS (Y, Z))
633 r[7] = LOWER_BYTE (DE-SJIS (Y, Z)) */
634
4ed46869
KH
635/* Terminate CCL program successfully. */
636#define CCL_SUCCESS \
637 do { \
638 ccl->status = CCL_STAT_SUCCESS; \
639 ccl->ic = CCL_HEADER_MAIN; \
640 goto ccl_finish; \
641 } while (0)
642
643/* Suspend CCL program because of reading from empty input buffer or
644 writing to full output buffer. When this program is resumed, the
645 same I/O command is executed. */
e34b1164
KH
646#define CCL_SUSPEND(stat) \
647 do { \
648 ic--; \
649 ccl->status = stat; \
650 goto ccl_finish; \
4ed46869
KH
651 } while (0)
652
653/* Terminate CCL program because of invalid command. Should not occur
654 in the normal case. */
655#define CCL_INVALID_CMD \
656 do { \
657 ccl->status = CCL_STAT_INVALID_CMD; \
658 goto ccl_error_handler; \
659 } while (0)
660
661/* Encode one character CH to multibyte form and write to the current
887bfbd7 662 output buffer. If CH is less than 256, CH is written as is. */
e34b1164
KH
663#define CCL_WRITE_CHAR(ch) \
664 do { \
665 if (!dst) \
666 CCL_INVALID_CMD; \
667 else \
668 { \
669 unsigned char work[4], *str; \
670 int len = CHAR_STRING (ch, work, str); \
671 if (dst + len <= (dst_bytes ? dst_end : src)) \
672 { \
673 bcopy (str, dst, len); \
674 dst += len; \
675 } \
676 else \
677 CCL_SUSPEND (CCL_STAT_SUSPEND_BY_DST); \
678 } \
4ed46869
KH
679 } while (0)
680
681/* Write a string at ccl_prog[IC] of length LEN to the current output
682 buffer. */
683#define CCL_WRITE_STRING(len) \
684 do { \
685 if (!dst) \
686 CCL_INVALID_CMD; \
e34b1164 687 else if (dst + len <= (dst_bytes ? dst_end : src)) \
4ed46869
KH
688 for (i = 0; i < len; i++) \
689 *dst++ = ((XFASTINT (ccl_prog[ic + (i / 3)])) \
690 >> ((2 - (i % 3)) * 8)) & 0xFF; \
691 else \
e34b1164 692 CCL_SUSPEND (CCL_STAT_SUSPEND_BY_DST); \
4ed46869
KH
693 } while (0)
694
695/* Read one byte from the current input buffer into Rth register. */
e34b1164
KH
696#define CCL_READ_CHAR(r) \
697 do { \
698 if (!src) \
699 CCL_INVALID_CMD; \
700 else if (src < src_end) \
701 r = *src++; \
702 else if (ccl->last_block) \
703 { \
704 ic = ccl->eof_ic; \
705 goto ccl_finish; \
706 } \
707 else \
708 CCL_SUSPEND (CCL_STAT_SUSPEND_BY_SRC); \
4ed46869
KH
709 } while (0)
710
711
712/* Execute CCL code on SRC_BYTES length text at SOURCE. The resulting
713 text goes to a place pointed by DESTINATION, the length of which
714 should not exceed DST_BYTES. The bytes actually processed is
715 returned as *CONSUMED. The return value is the length of the
716 resulting text. As a side effect, the contents of CCL registers
717 are updated. If SOURCE or DESTINATION is NULL, only operations on
718 registers are permitted. */
719
720#ifdef CCL_DEBUG
721#define CCL_DEBUG_BACKTRACE_LEN 256
722int ccl_backtrace_table[CCL_BACKTRACE_TABLE];
723int ccl_backtrace_idx;
724#endif
725
726struct ccl_prog_stack
727 {
a9f1cc19 728 Lisp_Object *ccl_prog; /* Pointer to an array of CCL code. */
4ed46869
KH
729 int ic; /* Instruction Counter. */
730 };
731
dfcf069d 732int
4ed46869
KH
733ccl_driver (ccl, source, destination, src_bytes, dst_bytes, consumed)
734 struct ccl_program *ccl;
735 unsigned char *source, *destination;
736 int src_bytes, dst_bytes;
737 int *consumed;
738{
739 register int *reg = ccl->reg;
740 register int ic = ccl->ic;
741 register int code, field1, field2;
e995085f 742 register Lisp_Object *ccl_prog = ccl->prog;
4ed46869
KH
743 unsigned char *src = source, *src_end = src + src_bytes;
744 unsigned char *dst = destination, *dst_end = dst + dst_bytes;
745 int jump_address;
746 int i, j, op;
747 int stack_idx = 0;
748 /* For the moment, we only support depth 256 of stack. */
749 struct ccl_prog_stack ccl_prog_stack_struct[256];
750
751 if (ic >= ccl->eof_ic)
752 ic = CCL_HEADER_MAIN;
753
754#ifdef CCL_DEBUG
755 ccl_backtrace_idx = 0;
756#endif
757
758 for (;;)
759 {
760#ifdef CCL_DEBUG
761 ccl_backtrace_table[ccl_backtrace_idx++] = ic;
762 if (ccl_backtrace_idx >= CCL_DEBUG_BACKTRACE_LEN)
763 ccl_backtrace_idx = 0;
764 ccl_backtrace_table[ccl_backtrace_idx] = 0;
765#endif
766
767 if (!NILP (Vquit_flag) && NILP (Vinhibit_quit))
768 {
769 /* We can't just signal Qquit, instead break the loop as if
770 the whole data is processed. Don't reset Vquit_flag, it
771 must be handled later at a safer place. */
772 if (consumed)
773 src = source + src_bytes;
774 ccl->status = CCL_STAT_QUIT;
775 break;
776 }
777
778 code = XINT (ccl_prog[ic]); ic++;
779 field1 = code >> 8;
780 field2 = (code & 0xFF) >> 5;
781
782#define rrr field2
783#define RRR (field1 & 7)
784#define Rrr ((field1 >> 3) & 7)
785#define ADDR field1
e34b1164 786#define EXCMD (field1 >> 6)
4ed46869
KH
787
788 switch (code & 0x1F)
789 {
790 case CCL_SetRegister: /* 00000000000000000RRRrrrXXXXX */
791 reg[rrr] = reg[RRR];
792 break;
793
794 case CCL_SetShortConst: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
795 reg[rrr] = field1;
796 break;
797
798 case CCL_SetConst: /* 00000000000000000000rrrXXXXX */
799 reg[rrr] = XINT (ccl_prog[ic]);
800 ic++;
801 break;
802
803 case CCL_SetArray: /* CCCCCCCCCCCCCCCCCCCCRRRrrrXXXXX */
804 i = reg[RRR];
805 j = field1 >> 3;
806 if ((unsigned int) i < j)
807 reg[rrr] = XINT (ccl_prog[ic + i]);
808 ic += j;
809 break;
810
811 case CCL_Jump: /* A--D--D--R--E--S--S-000XXXXX */
812 ic += ADDR;
813 break;
814
815 case CCL_JumpCond: /* A--D--D--R--E--S--S-rrrXXXXX */
816 if (!reg[rrr])
817 ic += ADDR;
818 break;
819
820 case CCL_WriteRegisterJump: /* A--D--D--R--E--S--S-rrrXXXXX */
821 i = reg[rrr];
822 CCL_WRITE_CHAR (i);
823 ic += ADDR;
824 break;
825
826 case CCL_WriteRegisterReadJump: /* A--D--D--R--E--S--S-rrrXXXXX */
827 i = reg[rrr];
828 CCL_WRITE_CHAR (i);
829 ic++;
830 CCL_READ_CHAR (reg[rrr]);
831 ic += ADDR - 1;
832 break;
833
834 case CCL_WriteConstJump: /* A--D--D--R--E--S--S-000XXXXX */
835 i = XINT (ccl_prog[ic]);
836 CCL_WRITE_CHAR (i);
837 ic += ADDR;
838 break;
839
840 case CCL_WriteConstReadJump: /* A--D--D--R--E--S--S-rrrXXXXX */
841 i = XINT (ccl_prog[ic]);
842 CCL_WRITE_CHAR (i);
843 ic++;
844 CCL_READ_CHAR (reg[rrr]);
845 ic += ADDR - 1;
846 break;
847
848 case CCL_WriteStringJump: /* A--D--D--R--E--S--S-000XXXXX */
849 j = XINT (ccl_prog[ic]);
850 ic++;
851 CCL_WRITE_STRING (j);
852 ic += ADDR - 1;
853 break;
854
855 case CCL_WriteArrayReadJump: /* A--D--D--R--E--S--S-rrrXXXXX */
856 i = reg[rrr];
2e34157c 857 j = XINT (ccl_prog[ic]);
4ed46869
KH
858 if ((unsigned int) i < j)
859 {
887bfbd7 860 i = XINT (ccl_prog[ic + 1 + i]);
4ed46869
KH
861 CCL_WRITE_CHAR (i);
862 }
887bfbd7 863 ic += j + 2;
4ed46869
KH
864 CCL_READ_CHAR (reg[rrr]);
865 ic += ADDR - (j + 2);
866 break;
867
868 case CCL_ReadJump: /* A--D--D--R--E--S--S-rrrYYYYY */
869 CCL_READ_CHAR (reg[rrr]);
870 ic += ADDR;
871 break;
872
873 case CCL_ReadBranch: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
874 CCL_READ_CHAR (reg[rrr]);
875 /* fall through ... */
876 case CCL_Branch: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
877 if ((unsigned int) reg[rrr] < field1)
878 ic += XINT (ccl_prog[ic + reg[rrr]]);
879 else
880 ic += XINT (ccl_prog[ic + field1]);
881 break;
882
883 case CCL_ReadRegister: /* CCCCCCCCCCCCCCCCCCCCrrXXXXX */
884 while (1)
885 {
886 CCL_READ_CHAR (reg[rrr]);
887 if (!field1) break;
888 code = XINT (ccl_prog[ic]); ic++;
889 field1 = code >> 8;
890 field2 = (code & 0xFF) >> 5;
891 }
892 break;
893
894 case CCL_WriteExprConst: /* 1:00000OPERATION000RRR000XXXXX */
895 rrr = 7;
896 i = reg[RRR];
897 j = XINT (ccl_prog[ic]);
898 op = field1 >> 6;
899 ic++;
900 goto ccl_set_expr;
901
902 case CCL_WriteRegister: /* CCCCCCCCCCCCCCCCCCCrrrXXXXX */
903 while (1)
904 {
905 i = reg[rrr];
906 CCL_WRITE_CHAR (i);
907 if (!field1) break;
908 code = XINT (ccl_prog[ic]); ic++;
909 field1 = code >> 8;
910 field2 = (code & 0xFF) >> 5;
911 }
912 break;
913
914 case CCL_WriteExprRegister: /* 1:00000OPERATIONRrrRRR000XXXXX */
915 rrr = 7;
916 i = reg[RRR];
917 j = reg[Rrr];
918 op = field1 >> 6;
919 goto ccl_set_expr;
920
921 case CCL_Call: /* CCCCCCCCCCCCCCCCCCCC000XXXXX */
922 {
923 Lisp_Object slot;
924
925 if (stack_idx >= 256
926 || field1 < 0
927 || field1 >= XVECTOR (Vccl_program_table)->size
928 || (slot = XVECTOR (Vccl_program_table)->contents[field1],
929 !CONSP (slot))
930 || !VECTORP (XCONS (slot)->cdr))
931 {
932 if (stack_idx > 0)
933 {
934 ccl_prog = ccl_prog_stack_struct[0].ccl_prog;
935 ic = ccl_prog_stack_struct[0].ic;
936 }
937 CCL_INVALID_CMD;
938 }
939
940 ccl_prog_stack_struct[stack_idx].ccl_prog = ccl_prog;
941 ccl_prog_stack_struct[stack_idx].ic = ic;
942 stack_idx++;
943 ccl_prog = XVECTOR (XCONS (slot)->cdr)->contents;
944 ic = CCL_HEADER_MAIN;
945 }
946 break;
947
948 case CCL_WriteConstString: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
949 if (!rrr)
950 CCL_WRITE_CHAR (field1);
951 else
952 {
953 CCL_WRITE_STRING (field1);
954 ic += (field1 + 2) / 3;
955 }
956 break;
957
958 case CCL_WriteArray: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
959 i = reg[rrr];
960 if ((unsigned int) i < field1)
961 {
962 j = XINT (ccl_prog[ic + i]);
963 CCL_WRITE_CHAR (j);
964 }
965 ic += field1;
966 break;
967
968 case CCL_End: /* 0000000000000000000000XXXXX */
969 if (stack_idx-- > 0)
970 {
971 ccl_prog = ccl_prog_stack_struct[stack_idx].ccl_prog;
972 ic = ccl_prog_stack_struct[stack_idx].ic;
973 break;
974 }
975 CCL_SUCCESS;
976
977 case CCL_ExprSelfConst: /* 00000OPERATION000000rrrXXXXX */
978 i = XINT (ccl_prog[ic]);
979 ic++;
980 op = field1 >> 6;
981 goto ccl_expr_self;
982
983 case CCL_ExprSelfReg: /* 00000OPERATION000RRRrrrXXXXX */
984 i = reg[RRR];
985 op = field1 >> 6;
986
987 ccl_expr_self:
988 switch (op)
989 {
990 case CCL_PLUS: reg[rrr] += i; break;
991 case CCL_MINUS: reg[rrr] -= i; break;
992 case CCL_MUL: reg[rrr] *= i; break;
993 case CCL_DIV: reg[rrr] /= i; break;
994 case CCL_MOD: reg[rrr] %= i; break;
995 case CCL_AND: reg[rrr] &= i; break;
996 case CCL_OR: reg[rrr] |= i; break;
997 case CCL_XOR: reg[rrr] ^= i; break;
998 case CCL_LSH: reg[rrr] <<= i; break;
999 case CCL_RSH: reg[rrr] >>= i; break;
1000 case CCL_LSH8: reg[rrr] <<= 8; reg[rrr] |= i; break;
1001 case CCL_RSH8: reg[7] = reg[rrr] & 0xFF; reg[rrr] >>= 8; break;
1002 case CCL_DIVMOD: reg[7] = reg[rrr] % i; reg[rrr] /= i; break;
1003 case CCL_LS: reg[rrr] = reg[rrr] < i; break;
1004 case CCL_GT: reg[rrr] = reg[rrr] > i; break;
1005 case CCL_EQ: reg[rrr] = reg[rrr] == i; break;
1006 case CCL_LE: reg[rrr] = reg[rrr] <= i; break;
1007 case CCL_GE: reg[rrr] = reg[rrr] >= i; break;
1008 case CCL_NE: reg[rrr] = reg[rrr] != i; break;
1009 default: CCL_INVALID_CMD;
1010 }
1011 break;
1012
1013 case CCL_SetExprConst: /* 00000OPERATION000RRRrrrXXXXX */
1014 i = reg[RRR];
1015 j = XINT (ccl_prog[ic]);
1016 op = field1 >> 6;
1017 jump_address = ++ic;
1018 goto ccl_set_expr;
1019
1020 case CCL_SetExprReg: /* 00000OPERATIONRrrRRRrrrXXXXX */
1021 i = reg[RRR];
1022 j = reg[Rrr];
1023 op = field1 >> 6;
1024 jump_address = ic;
1025 goto ccl_set_expr;
1026
1027 case CCL_ReadJumpCondExprConst: /* A--D--D--R--E--S--S-rrrXXXXX */
1028 CCL_READ_CHAR (reg[rrr]);
1029 case CCL_JumpCondExprConst: /* A--D--D--R--E--S--S-rrrXXXXX */
1030 i = reg[rrr];
1031 op = XINT (ccl_prog[ic]);
1032 jump_address = ic++ + ADDR;
1033 j = XINT (ccl_prog[ic]);
1034 ic++;
1035 rrr = 7;
1036 goto ccl_set_expr;
1037
1038 case CCL_ReadJumpCondExprReg: /* A--D--D--R--E--S--S-rrrXXXXX */
1039 CCL_READ_CHAR (reg[rrr]);
1040 case CCL_JumpCondExprReg:
1041 i = reg[rrr];
1042 op = XINT (ccl_prog[ic]);
1043 jump_address = ic++ + ADDR;
1044 j = reg[XINT (ccl_prog[ic])];
1045 ic++;
1046 rrr = 7;
1047
1048 ccl_set_expr:
1049 switch (op)
1050 {
1051 case CCL_PLUS: reg[rrr] = i + j; break;
1052 case CCL_MINUS: reg[rrr] = i - j; break;
1053 case CCL_MUL: reg[rrr] = i * j; break;
1054 case CCL_DIV: reg[rrr] = i / j; break;
1055 case CCL_MOD: reg[rrr] = i % j; break;
1056 case CCL_AND: reg[rrr] = i & j; break;
1057 case CCL_OR: reg[rrr] = i | j; break;
1058 case CCL_XOR: reg[rrr] = i ^ j;; break;
1059 case CCL_LSH: reg[rrr] = i << j; break;
1060 case CCL_RSH: reg[rrr] = i >> j; break;
1061 case CCL_LSH8: reg[rrr] = (i << 8) | j; break;
1062 case CCL_RSH8: reg[rrr] = i >> 8; reg[7] = i & 0xFF; break;
1063 case CCL_DIVMOD: reg[rrr] = i / j; reg[7] = i % j; break;
1064 case CCL_LS: reg[rrr] = i < j; break;
1065 case CCL_GT: reg[rrr] = i > j; break;
1066 case CCL_EQ: reg[rrr] = i == j; break;
1067 case CCL_LE: reg[rrr] = i <= j; break;
1068 case CCL_GE: reg[rrr] = i >= j; break;
1069 case CCL_NE: reg[rrr] = i != j; break;
1070 case CCL_ENCODE_SJIS: ENCODE_SJIS (i, j, reg[rrr], reg[7]); break;
1071 case CCL_DECODE_SJIS: DECODE_SJIS (i, j, reg[rrr], reg[7]); break;
1072 default: CCL_INVALID_CMD;
1073 }
1074 code &= 0x1F;
1075 if (code == CCL_WriteExprConst || code == CCL_WriteExprRegister)
1076 {
1077 i = reg[rrr];
1078 CCL_WRITE_CHAR (i);
1079 }
1080 else if (!reg[rrr])
1081 ic = jump_address;
1082 break;
1083
e34b1164
KH
1084 case CCL_Extention:
1085 switch (EXCMD)
1086 {
6ae21908 1087 case CCL_ReadMultibyteChar2:
e34b1164
KH
1088 if (!src)
1089 CCL_INVALID_CMD;
1090 do {
1091 if (src >= src_end)
6ae21908
KH
1092 {
1093 src++;
1094 goto ccl_read_multibyte_character_suspend;
1095 }
e34b1164
KH
1096
1097 i = *src++;
1098 if (i == LEADING_CODE_COMPOSITION)
1099 {
1100 if (src >= src_end)
1101 goto ccl_read_multibyte_character_suspend;
1102 if (*src == 0xFF)
1103 {
1104 ccl->private_state = COMPOSING_WITH_RULE_HEAD;
1105 src++;
1106 }
1107 else
1108 ccl->private_state = COMPOSING_NO_RULE_HEAD;
1109 }
1110 if (ccl->private_state != 0)
1111 {
1112 /* composite character */
1113 if (*src < 0xA0)
1114 ccl->private_state = 0;
1115 else
1116 {
1117 if (i == 0xA0)
1118 {
1119 if (src >= src_end)
1120 goto ccl_read_multibyte_character_suspend;
1121 i = *src++ & 0x7F;
1122 }
1123 else
1124 i -= 0x20;
1125
1126 if (COMPOSING_WITH_RULE_RULE == ccl->private_state)
1127 {
1128 ccl->private_state = COMPOSING_WITH_RULE_HEAD;
1129 continue;
1130 }
1131 else if (COMPOSING_WITH_RULE_HEAD == ccl->private_state)
1132 ccl->private_state = COMPOSING_WITH_RULE_RULE;
1133 }
1134 }
1135 if (i < 0x80)
1136 {
1137 /* ASCII */
1138 reg[rrr] = i;
1139 reg[RRR] = CHARSET_ASCII;
1140 }
1141 else if (i <= MAX_CHARSET_OFFICIAL_DIMENSION1)
1142 {
1143 if (src >= src_end)
1144 goto ccl_read_multibyte_character_suspend;
1145 reg[RRR] = i;
1146 reg[rrr] = (*src++ & 0x7F);
1147 }
1148 else if (i <= MAX_CHARSET_OFFICIAL_DIMENSION2)
1149 {
1150 if ((src + 1) >= src_end)
1151 goto ccl_read_multibyte_character_suspend;
1152 reg[RRR] = i;
1153 i = (*src++ & 0x7F);
1154 reg[rrr] = ((i << 7) | (*src & 0x7F));
1155 src++;
1156 }
6ae21908
KH
1157 else if ((i == LEADING_CODE_PRIVATE_11)
1158 || (i == LEADING_CODE_PRIVATE_12))
e34b1164
KH
1159 {
1160 if ((src + 1) >= src_end)
1161 goto ccl_read_multibyte_character_suspend;
1162 reg[RRR] = *src++;
1163 reg[rrr] = (*src++ & 0x7F);
1164 }
6ae21908
KH
1165 else if ((i == LEADING_CODE_PRIVATE_21)
1166 || (i == LEADING_CODE_PRIVATE_22))
e34b1164
KH
1167 {
1168 if ((src + 2) >= src_end)
1169 goto ccl_read_multibyte_character_suspend;
1170 reg[RRR] = *src++;
1171 i = (*src++ & 0x7F);
1172 reg[rrr] = ((i << 7) | (*src & 0x7F));
1173 src++;
1174 }
1175 else
1176 {
6ae21908
KH
1177 /* INVALID CODE
1178 Returned charset is -1. */
e34b1164
KH
1179 reg[RRR] = -1;
1180 }
1181 } while (0);
1182 break;
1183
1184 ccl_read_multibyte_character_suspend:
1185 src--;
1186 if (ccl->last_block)
1187 {
1188 ic = ccl->eof_ic;
1189 goto ccl_finish;
1190 }
1191 else
1192 CCL_SUSPEND (CCL_STAT_SUSPEND_BY_SRC);
1193
1194 break;
1195
6ae21908 1196 case CCL_WriteMultibyteChar2:
e34b1164
KH
1197 i = reg[RRR]; /* charset */
1198 if (i == CHARSET_ASCII)
1199 i = reg[rrr] & 0x7F;
1200 else if (i == CHARSET_COMPOSITION)
1201 i = MAKE_COMPOSITE_CHAR (reg[rrr]);
1202 else if (CHARSET_DIMENSION (i) == 1)
1203 i = ((i - 0x70) << 7) | (reg[rrr] & 0x7F);
1204 else if (i < MIN_CHARSET_PRIVATE_DIMENSION2)
1205 i = ((i - 0x8F) << 14) | reg[rrr];
1206 else
1207 i = ((i - 0xE0) << 14) | reg[rrr];
1208
1209 CCL_WRITE_CHAR (i);
1210
1211 break;
1212
8146262a 1213 case CCL_TranslateCharacter:
e34b1164
KH
1214 i = reg[RRR]; /* charset */
1215 if (i == CHARSET_ASCII)
1216 i = reg[rrr] & 0x7F;
1217 else if (i == CHARSET_COMPOSITION)
1218 {
1219 reg[RRR] = -1;
1220 break;
1221 }
1222 else if (CHARSET_DIMENSION (i) == 1)
1223 i = ((i - 0x70) << 7) | (reg[rrr] & 0x7F);
1224 else if (i < MIN_CHARSET_PRIVATE_DIMENSION2)
1225 i = ((i - 0x8F) << 14) | (reg[rrr] & 0x3FFF);
1226 else
1227 i = ((i - 0xE0) << 14) | (reg[rrr] & 0x3FFF);
1228
8146262a
KH
1229 op = translate_char (GET_TRANSLATION_TABLE (reg[Rrr]),
1230 i, -1, 0, 0);
e34b1164
KH
1231 SPLIT_CHAR (op, reg[RRR], i, j);
1232 if (j != -1)
1233 i = (i << 7) | j;
1234
1235 reg[rrr] = i;
1236 break;
1237
8146262a 1238 case CCL_TranslateCharacterConstTbl:
e34b1164
KH
1239 op = XINT (ccl_prog[ic]); /* table */
1240 ic++;
1241 i = reg[RRR]; /* charset */
1242 if (i == CHARSET_ASCII)
1243 i = reg[rrr] & 0x7F;
1244 else if (i == CHARSET_COMPOSITION)
1245 {
1246 reg[RRR] = -1;
1247 break;
1248 }
1249 else if (CHARSET_DIMENSION (i) == 1)
1250 i = ((i - 0x70) << 7) | (reg[rrr] & 0x7F);
1251 else if (i < MIN_CHARSET_PRIVATE_DIMENSION2)
1252 i = ((i - 0x8F) << 14) | (reg[rrr] & 0x3FFF);
1253 else
1254 i = ((i - 0xE0) << 14) | (reg[rrr] & 0x3FFF);
1255
8146262a 1256 op = translate_char (GET_TRANSLATION_TABLE (op), i, -1, 0, 0);
e34b1164
KH
1257 SPLIT_CHAR (op, reg[RRR], i, j);
1258 if (j != -1)
1259 i = (i << 7) | j;
1260
1261 reg[rrr] = i;
1262 break;
1263
1264 case CCL_IterateMultipleMap:
1265 {
8146262a 1266 Lisp_Object map, content, attrib, value;
e34b1164
KH
1267 int point, size, fin_ic;
1268
8146262a 1269 j = XINT (ccl_prog[ic++]); /* number of maps. */
e34b1164
KH
1270 fin_ic = ic + j;
1271 op = reg[rrr];
1272 if ((j > reg[RRR]) && (j >= 0))
1273 {
1274 ic += reg[RRR];
1275 i = reg[RRR];
1276 }
1277 else
1278 {
1279 reg[RRR] = -1;
1280 ic = fin_ic;
1281 break;
1282 }
1283
1284 for (;i < j;i++)
1285 {
1286
8146262a 1287 size = XVECTOR (Vcode_conversion_map_vector)->size;
d387866a 1288 point = XINT (ccl_prog[ic++]);
e34b1164 1289 if (point >= size) continue;
8146262a
KH
1290 map =
1291 XVECTOR (Vcode_conversion_map_vector)->contents[point];
1292
1293 /* Check map varidity. */
1294 if (!CONSP (map)) continue;
1295 map = XCONS(map)->cdr;
1296 if (!VECTORP (map)) continue;
1297 size = XVECTOR (map)->size;
e34b1164 1298 if (size <= 1) continue;
6ae21908 1299
8146262a 1300 content = XVECTOR (map)->contents[0];
6ae21908 1301
8146262a 1302 /* check map type,
6ae21908
KH
1303 [STARTPOINT VAL1 VAL2 ...] or
1304 [t ELELMENT STARTPOINT ENDPOINT] */
1305 if (NUMBERP (content))
1306 {
1307 point = XUINT (content);
1308 point = op - point + 1;
1309 if (!((point >= 1) && (point < size))) continue;
8146262a 1310 content = XVECTOR (map)->contents[point];
6ae21908
KH
1311 }
1312 else if (EQ (content, Qt))
1313 {
1314 if (size != 4) continue;
8146262a
KH
1315 if ((op >= XUINT (XVECTOR (map)->contents[2]))
1316 && (op < XUINT (XVECTOR (map)->contents[3])))
1317 content = XVECTOR (map)->contents[1];
6ae21908
KH
1318 else
1319 continue;
1320 }
1321 else
1322 continue;
e34b1164
KH
1323
1324 if (NILP (content))
1325 continue;
1326 else if (NUMBERP (content))
1327 {
1328 reg[RRR] = i;
6ae21908 1329 reg[rrr] = XINT(content);
e34b1164
KH
1330 break;
1331 }
1332 else if (EQ (content, Qt) || EQ (content, Qlambda))
1333 {
1334 reg[RRR] = i;
1335 break;
1336 }
1337 else if (CONSP (content))
1338 {
1339 attrib = XCONS (content)->car;
1340 value = XCONS (content)->cdr;
1341 if (!NUMBERP (attrib) || !NUMBERP (value))
1342 continue;
1343 reg[RRR] = i;
6ae21908 1344 reg[rrr] = XUINT (value);
e34b1164
KH
1345 break;
1346 }
1347 }
1348 if (i == j)
1349 reg[RRR] = -1;
1350 ic = fin_ic;
1351 }
1352 break;
1353
8146262a 1354 case CCL_MapMultiple:
e34b1164 1355 {
8146262a
KH
1356 Lisp_Object map, content, attrib, value;
1357 int point, size, map_vector_size;
1358 int map_set_rest_length, fin_ic;
1359
1360 map_set_rest_length =
1361 XINT (ccl_prog[ic++]); /* number of maps and separators. */
1362 fin_ic = ic + map_set_rest_length;
1363 if ((map_set_rest_length > reg[RRR]) && (reg[RRR] >= 0))
e34b1164
KH
1364 {
1365 ic += reg[RRR];
1366 i = reg[RRR];
8146262a 1367 map_set_rest_length -= i;
e34b1164
KH
1368 }
1369 else
1370 {
1371 ic = fin_ic;
1372 reg[RRR] = -1;
1373 break;
1374 }
8146262a 1375 mapping_stack_pointer = mapping_stack;
e34b1164 1376 op = reg[rrr];
8146262a 1377 PUSH_MAPPING_STACK (0, op);
e34b1164 1378 reg[RRR] = -1;
8146262a
KH
1379 map_vector_size = XVECTOR (Vcode_conversion_map_vector)->size;
1380 for (;map_set_rest_length > 0;i++, map_set_rest_length--)
e34b1164 1381 {
6ae21908
KH
1382 point = XINT(ccl_prog[ic++]);
1383 if (point < 0)
e34b1164 1384 {
6ae21908 1385 point = -point;
8146262a
KH
1386 if (mapping_stack_pointer
1387 >= &mapping_stack[MAX_MAP_SET_LEVEL])
6ae21908
KH
1388 {
1389 CCL_INVALID_CMD;
1390 }
8146262a
KH
1391 PUSH_MAPPING_STACK (map_set_rest_length - point,
1392 reg[rrr]);
1393 map_set_rest_length = point + 1;
6ae21908 1394 reg[rrr] = op;
e34b1164
KH
1395 continue;
1396 }
6ae21908 1397
8146262a
KH
1398 if (point >= map_vector_size) continue;
1399 map = (XVECTOR (Vcode_conversion_map_vector)
1400 ->contents[point]);
6ae21908 1401
8146262a
KH
1402 /* Check map varidity. */
1403 if (!CONSP (map)) continue;
1404 map = XCONS (map)->cdr;
1405 if (!VECTORP (map)) continue;
1406 size = XVECTOR (map)->size;
e34b1164 1407 if (size <= 1) continue;
6ae21908 1408
8146262a 1409 content = XVECTOR (map)->contents[0];
6ae21908 1410
8146262a 1411 /* check map type,
6ae21908
KH
1412 [STARTPOINT VAL1 VAL2 ...] or
1413 [t ELEMENT STARTPOINT ENDPOINT] */
1414 if (NUMBERP (content))
1415 {
1416 point = XUINT (content);
1417 point = op - point + 1;
1418 if (!((point >= 1) && (point < size))) continue;
8146262a 1419 content = XVECTOR (map)->contents[point];
6ae21908
KH
1420 }
1421 else if (EQ (content, Qt))
1422 {
1423 if (size != 4) continue;
8146262a
KH
1424 if ((op >= XUINT (XVECTOR (map)->contents[2])) &&
1425 (op < XUINT (XVECTOR (map)->contents[3])))
1426 content = XVECTOR (map)->contents[1];
6ae21908
KH
1427 else
1428 continue;
1429 }
1430 else
1431 continue;
e34b1164
KH
1432
1433 if (NILP (content))
1434 continue;
1435 else if (NUMBERP (content))
1436 {
6ae21908 1437 op = XINT (content);
e34b1164 1438 reg[RRR] = i;
8146262a
KH
1439 i += map_set_rest_length;
1440 POP_MAPPING_STACK (map_set_rest_length, reg[rrr]);
e34b1164
KH
1441 }
1442 else if (CONSP (content))
1443 {
1444 attrib = XCONS (content)->car;
1445 value = XCONS (content)->cdr;
1446 if (!NUMBERP (attrib) || !NUMBERP (value))
1447 continue;
1448 reg[RRR] = i;
1449 op = XUINT (value);
8146262a
KH
1450 i += map_set_rest_length;
1451 POP_MAPPING_STACK (map_set_rest_length, reg[rrr]);
e34b1164
KH
1452 }
1453 else if (EQ (content, Qt))
1454 {
1455 reg[RRR] = i;
1456 op = reg[rrr];
8146262a
KH
1457 i += map_set_rest_length;
1458 POP_MAPPING_STACK (map_set_rest_length, reg[rrr]);
e34b1164
KH
1459 }
1460 else if (EQ (content, Qlambda))
6ae21908
KH
1461 {
1462 break;
1463 }
1464 else
1465 CCL_INVALID_CMD;
e34b1164
KH
1466 }
1467 ic = fin_ic;
1468 }
1469 reg[rrr] = op;
1470 break;
1471
8146262a 1472 case CCL_MapSingle:
e34b1164 1473 {
8146262a 1474 Lisp_Object map, attrib, value, content;
e34b1164 1475 int size, point;
8146262a 1476 j = XINT (ccl_prog[ic++]); /* map_id */
e34b1164 1477 op = reg[rrr];
8146262a 1478 if (j >= XVECTOR (Vcode_conversion_map_vector)->size)
e34b1164
KH
1479 {
1480 reg[RRR] = -1;
1481 break;
1482 }
8146262a
KH
1483 map = XVECTOR (Vcode_conversion_map_vector)->contents[j];
1484 if (!CONSP (map))
e34b1164
KH
1485 {
1486 reg[RRR] = -1;
1487 break;
1488 }
8146262a
KH
1489 map = XCONS(map)->cdr;
1490 if (!VECTORP (map))
e34b1164
KH
1491 {
1492 reg[RRR] = -1;
1493 break;
1494 }
8146262a
KH
1495 size = XVECTOR (map)->size;
1496 point = XUINT (XVECTOR (map)->contents[0]);
e34b1164
KH
1497 point = op - point + 1;
1498 reg[RRR] = 0;
1499 if ((size <= 1) ||
1500 (!((point >= 1) && (point < size))))
1501 reg[RRR] = -1;
1502 else
1503 {
8146262a 1504 content = XVECTOR (map)->contents[point];
e34b1164
KH
1505 if (NILP (content))
1506 reg[RRR] = -1;
1507 else if (NUMBERP (content))
6ae21908 1508 reg[rrr] = XINT (content);
e34b1164
KH
1509 else if (EQ (content, Qt))
1510 reg[RRR] = i;
1511 else if (CONSP (content))
1512 {
1513 attrib = XCONS (content)->car;
1514 value = XCONS (content)->cdr;
1515 if (!NUMBERP (attrib) || !NUMBERP (value))
1516 continue;
1517 reg[rrr] = XUINT(value);
1518 break;
1519 }
1520 else
1521 reg[RRR] = -1;
1522 }
1523 }
1524 break;
1525
1526 default:
1527 CCL_INVALID_CMD;
1528 }
1529 break;
1530
4ed46869
KH
1531 default:
1532 CCL_INVALID_CMD;
1533 }
1534 }
1535
1536 ccl_error_handler:
1537 if (destination)
1538 {
1539 /* We can insert an error message only if DESTINATION is
1540 specified and we still have a room to store the message
1541 there. */
1542 char msg[256];
1543 int msglen;
1544
1545 switch (ccl->status)
1546 {
1547 case CCL_STAT_INVALID_CMD:
1548 sprintf(msg, "\nCCL: Invalid command %x (ccl_code = %x) at %d.",
1549 code & 0x1F, code, ic);
1550#ifdef CCL_DEBUG
1551 {
1552 int i = ccl_backtrace_idx - 1;
1553 int j;
1554
1555 msglen = strlen (msg);
6ae21908 1556 if (dst + msglen <= dst_end)
4ed46869
KH
1557 {
1558 bcopy (msg, dst, msglen);
1559 dst += msglen;
1560 }
1561
1562 for (j = 0; j < CCL_DEBUG_BACKTRACE_LEN; j++, i--)
1563 {
1564 if (i < 0) i = CCL_DEBUG_BACKTRACE_LEN - 1;
1565 if (ccl_backtrace_table[i] == 0)
1566 break;
1567 sprintf(msg, " %d", ccl_backtrace_table[i]);
1568 msglen = strlen (msg);
6ae21908 1569 if (dst + msglen > dst_end)
4ed46869
KH
1570 break;
1571 bcopy (msg, dst, msglen);
1572 dst += msglen;
1573 }
1574 }
4ed46869 1575#endif
887bfbd7 1576 goto ccl_finish;
4ed46869
KH
1577
1578 case CCL_STAT_QUIT:
1579 sprintf(msg, "\nCCL: Quited.");
1580 break;
1581
1582 default:
1583 sprintf(msg, "\nCCL: Unknown error type (%d).", ccl->status);
1584 }
1585
1586 msglen = strlen (msg);
6ae21908 1587 if (dst + msglen <= dst_end)
4ed46869
KH
1588 {
1589 bcopy (msg, dst, msglen);
1590 dst += msglen;
1591 }
1592 }
1593
1594 ccl_finish:
1595 ccl->ic = ic;
1596 if (consumed) *consumed = src - source;
1597 return dst - destination;
1598}
1599
1600/* Setup fields of the structure pointed by CCL appropriately for the
1601 execution of compiled CCL code in VEC (vector of integer). */
07478155 1602void
4ed46869
KH
1603setup_ccl_program (ccl, vec)
1604 struct ccl_program *ccl;
1605 Lisp_Object vec;
1606{
1607 int i;
1608
1609 ccl->size = XVECTOR (vec)->size;
1610 ccl->prog = XVECTOR (vec)->contents;
1611 ccl->ic = CCL_HEADER_MAIN;
1612 ccl->eof_ic = XINT (XVECTOR (vec)->contents[CCL_HEADER_EOF]);
1613 ccl->buf_magnification = XINT (XVECTOR (vec)->contents[CCL_HEADER_BUF_MAG]);
1614 for (i = 0; i < 8; i++)
1615 ccl->reg[i] = 0;
1616 ccl->last_block = 0;
e34b1164 1617 ccl->private_state = 0;
4ed46869
KH
1618 ccl->status = 0;
1619}
1620
6ae21908 1621/* Resolve symbols in the specified CCL code (Lisp vector). This
8146262a
KH
1622 function converts symbols of code conversion maps and character
1623 translation tables embeded in the CCL code into their ID numbers. */
6ae21908
KH
1624
1625Lisp_Object
1626resolve_symbol_ccl_program (ccl)
1627 Lisp_Object ccl;
1628{
1629 int i, veclen;
1630 Lisp_Object result, contents, prop;
1631
1632 result = ccl;
1633 veclen = XVECTOR (result)->size;
1634
1635 /* Set CCL program's table ID */
1636 for (i = 0; i < veclen; i++)
1637 {
1638 contents = XVECTOR (result)->contents[i];
1639 if (SYMBOLP (contents))
1640 {
1641 if (EQ(result, ccl))
1642 result = Fcopy_sequence (ccl);
1643
f967223b 1644 prop = Fget (contents, Qtranslation_table_id);
6ae21908
KH
1645 if (NUMBERP (prop))
1646 {
1647 XVECTOR (result)->contents[i] = prop;
1648 continue;
1649 }
8146262a 1650 prop = Fget (contents, Qcode_conversion_map_id);
6ae21908
KH
1651 if (NUMBERP (prop))
1652 {
1653 XVECTOR (result)->contents[i] = prop;
1654 continue;
1655 }
1656 prop = Fget (contents, Qccl_program_idx);
1657 if (NUMBERP (prop))
1658 {
1659 XVECTOR (result)->contents[i] = prop;
1660 continue;
1661 }
1662 }
1663 }
1664
1665 return result;
1666}
1667
1668
4ed46869
KH
1669#ifdef emacs
1670
1671DEFUN ("ccl-execute", Fccl_execute, Sccl_execute, 2, 2, 0,
1672 "Execute CCL-PROGRAM with registers initialized by REGISTERS.\n\
6ae21908
KH
1673\n\
1674CCL-PROGRAM is a symbol registered by register-ccl-program,\n\
1675or a compiled code generated by `ccl-compile' (for backward compatibility,\n\
1676in this case, the execution is slower).\n\
1677No I/O commands should appear in CCL-PROGRAM.\n\
1678\n\
4ed46869
KH
1679REGISTERS is a vector of [R0 R1 ... R7] where RN is an initial value\n\
1680 of Nth register.\n\
6ae21908
KH
1681\n\
1682As side effect, each element of REGISTERS holds the value of\n\
4ed46869
KH
1683 corresponding register after the execution.")
1684 (ccl_prog, reg)
1685 Lisp_Object ccl_prog, reg;
1686{
1687 struct ccl_program ccl;
1688 int i;
6ae21908 1689 Lisp_Object ccl_id;
4ed46869 1690
6ae21908
KH
1691 if ((SYMBOLP (ccl_prog)) &&
1692 (!NILP (ccl_id = Fget (ccl_prog, Qccl_program_idx))))
1693 {
1694 ccl_prog = XVECTOR (Vccl_program_table)->contents[XUINT (ccl_id)];
1695 CHECK_LIST (ccl_prog, 0);
1696 ccl_prog = XCONS (ccl_prog)->cdr;
1697 CHECK_VECTOR (ccl_prog, 1);
1698 }
1699 else
1700 {
1701 CHECK_VECTOR (ccl_prog, 1);
1702 ccl_prog = resolve_symbol_ccl_program (ccl_prog);
1703 }
1704
1705 CHECK_VECTOR (reg, 2);
4ed46869
KH
1706 if (XVECTOR (reg)->size != 8)
1707 error ("Invalid length of vector REGISTERS");
1708
1709 setup_ccl_program (&ccl, ccl_prog);
1710 for (i = 0; i < 8; i++)
1711 ccl.reg[i] = (INTEGERP (XVECTOR (reg)->contents[i])
1712 ? XINT (XVECTOR (reg)->contents[i])
1713 : 0);
1714
1715 ccl_driver (&ccl, (char *)0, (char *)0, 0, 0, (int *)0);
1716 QUIT;
1717 if (ccl.status != CCL_STAT_SUCCESS)
1718 error ("Error in CCL program at %dth code", ccl.ic);
1719
1720 for (i = 0; i < 8; i++)
1721 XSETINT (XVECTOR (reg)->contents[i], ccl.reg[i]);
1722 return Qnil;
1723}
1724
1725DEFUN ("ccl-execute-on-string", Fccl_execute_on_string, Sccl_execute_on_string,
39a68837 1726 3, 5, 0,
4ed46869 1727 "Execute CCL-PROGRAM with initial STATUS on STRING.\n\
6ae21908
KH
1728\n\
1729CCL-PROGRAM is a symbol registered by register-ccl-program,\n\
1730or a compiled code generated by `ccl-compile' (for backward compatibility,\n\
1731in this case, the execution is slower).\n\
1732\n\
4ed46869 1733Read buffer is set to STRING, and write buffer is allocated automatically.\n\
6ae21908 1734\n\
4ed46869
KH
1735STATUS is a vector of [R0 R1 ... R7 IC], where\n\
1736 R0..R7 are initial values of corresponding registers,\n\
1737 IC is the instruction counter specifying from where to start the program.\n\
1738If R0..R7 are nil, they are initialized to 0.\n\
1739If IC is nil, it is initialized to head of the CCL program.\n\
39a68837 1740\n\
6ae21908 1741If optional 4th arg CONTINUE is non-nil, keep IC on read operation\n\
cb5373dd 1742when read buffer is exausted, else, IC is always set to the end of\n\
db6089c5 1743CCL-PROGRAM on exit.\n\
39a68837
KH
1744\n\
1745It returns the contents of write buffer as a string,\n\
6ae21908 1746 and as side effect, STATUS is updated.\n\
39a68837
KH
1747If the optional 5th arg UNIBYTE-P is non-nil, the returned string\n\
1748is a unibyte string. By default it is a multibyte string.")
1749 (ccl_prog, status, str, contin, unibyte_p)
1750 Lisp_Object ccl_prog, status, str, contin, unibyte_p;
4ed46869
KH
1751{
1752 Lisp_Object val;
1753 struct ccl_program ccl;
1754 int i, produced;
1755 int outbufsize;
1756 char *outbuf;
1757 struct gcpro gcpro1, gcpro2, gcpro3;
6ae21908
KH
1758 Lisp_Object ccl_id;
1759
1760 if ((SYMBOLP (ccl_prog)) &&
1761 (!NILP (ccl_id = Fget (ccl_prog, Qccl_program_idx))))
1762 {
1763 ccl_prog = XVECTOR (Vccl_program_table)->contents[XUINT (ccl_id)];
1764 CHECK_LIST (ccl_prog, 0);
1765 ccl_prog = XCONS (ccl_prog)->cdr;
1766 CHECK_VECTOR (ccl_prog, 1);
1767 }
1768 else
1769 {
1770 CHECK_VECTOR (ccl_prog, 1);
1771 ccl_prog = resolve_symbol_ccl_program (ccl_prog);
1772 }
4ed46869 1773
4ed46869
KH
1774 CHECK_VECTOR (status, 1);
1775 if (XVECTOR (status)->size != 9)
1776 error ("Invalid length of vector STATUS");
1777 CHECK_STRING (str, 2);
1778 GCPRO3 (ccl_prog, status, str);
1779
1780 setup_ccl_program (&ccl, ccl_prog);
1781 for (i = 0; i < 8; i++)
1782 {
1783 if (NILP (XVECTOR (status)->contents[i]))
1784 XSETINT (XVECTOR (status)->contents[i], 0);
1785 if (INTEGERP (XVECTOR (status)->contents[i]))
1786 ccl.reg[i] = XINT (XVECTOR (status)->contents[i]);
1787 }
1788 if (INTEGERP (XVECTOR (status)->contents[i]))
1789 {
1790 i = XFASTINT (XVECTOR (status)->contents[8]);
1791 if (ccl.ic < i && i < ccl.size)
1792 ccl.ic = i;
1793 }
fc932ac6 1794 outbufsize = STRING_BYTES (XSTRING (str)) * ccl.buf_magnification + 256;
4ed46869
KH
1795 outbuf = (char *) xmalloc (outbufsize);
1796 if (!outbuf)
1797 error ("Not enough memory");
cb5373dd 1798 ccl.last_block = NILP (contin);
4ed46869 1799 produced = ccl_driver (&ccl, XSTRING (str)->data, outbuf,
fc932ac6 1800 STRING_BYTES (XSTRING (str)), outbufsize, (int *)0);
4ed46869
KH
1801 for (i = 0; i < 8; i++)
1802 XSET (XVECTOR (status)->contents[i], Lisp_Int, ccl.reg[i]);
1803 XSETINT (XVECTOR (status)->contents[8], ccl.ic);
1804 UNGCPRO;
1805
39a68837
KH
1806 if (NILP (unibyte_p))
1807 val = make_string (outbuf, produced);
1808 else
1809 val = make_unibyte_string (outbuf, produced);
4ed46869
KH
1810 free (outbuf);
1811 QUIT;
1812 if (ccl.status != CCL_STAT_SUCCESS
e34b1164
KH
1813 && ccl.status != CCL_STAT_SUSPEND_BY_SRC
1814 && ccl.status != CCL_STAT_SUSPEND_BY_DST)
4ed46869
KH
1815 error ("Error in CCL program at %dth code", ccl.ic);
1816
1817 return val;
1818}
1819
1820DEFUN ("register-ccl-program", Fregister_ccl_program, Sregister_ccl_program,
1821 2, 2, 0,
7bce92a6
KH
1822 "Register CCL program PROGRAM of NAME in `ccl-program-table'.\n\
1823PROGRAM should be a compiled code of CCL program, or nil.\n\
4ed46869
KH
1824Return index number of the registered CCL program.")
1825 (name, ccl_prog)
1826 Lisp_Object name, ccl_prog;
1827{
1828 int len = XVECTOR (Vccl_program_table)->size;
e34b1164 1829 int i;
4ed46869
KH
1830
1831 CHECK_SYMBOL (name, 0);
1832 if (!NILP (ccl_prog))
6ae21908
KH
1833 {
1834 CHECK_VECTOR (ccl_prog, 1);
1835 ccl_prog = resolve_symbol_ccl_program (ccl_prog);
1836 }
4ed46869
KH
1837
1838 for (i = 0; i < len; i++)
1839 {
1840 Lisp_Object slot = XVECTOR (Vccl_program_table)->contents[i];
1841
1842 if (!CONSP (slot))
1843 break;
1844
1845 if (EQ (name, XCONS (slot)->car))
1846 {
1847 XCONS (slot)->cdr = ccl_prog;
1848 return make_number (i);
1849 }
1850 }
1851
1852 if (i == len)
1853 {
6703ac4f 1854 Lisp_Object new_table = Fmake_vector (make_number (len * 2), Qnil);
4ed46869
KH
1855 int j;
1856
1857 for (j = 0; j < len; j++)
1858 XVECTOR (new_table)->contents[j]
1859 = XVECTOR (Vccl_program_table)->contents[j];
1860 Vccl_program_table = new_table;
1861 }
1862
1863 XVECTOR (Vccl_program_table)->contents[i] = Fcons (name, ccl_prog);
b23c2440 1864 Fput (name, Qccl_program_idx, make_number (i));
4ed46869
KH
1865 return make_number (i);
1866}
1867
8146262a
KH
1868/* Register code conversion map.
1869 A code conversion map consists of numbers, Qt, Qnil, and Qlambda.
e34b1164 1870 The first element is start code point.
8146262a
KH
1871 The rest elements are mapped numbers.
1872 Symbol t means to map to an original number before mapping.
1873 Symbol nil means that the corresponding element is empty.
1874 Symbol lambda menas to terminate mapping here.
e34b1164
KH
1875*/
1876
8146262a
KH
1877DEFUN ("register-code-conversion-map", Fregister_code_conversion_map,
1878 Sregister_code_conversion_map,
e34b1164 1879 2, 2, 0,
8146262a
KH
1880 "Register SYMBOL as code conversion map MAP.\n\
1881Return index number of the registered map.")
1882 (symbol, map)
1883 Lisp_Object symbol, map;
e34b1164 1884{
8146262a 1885 int len = XVECTOR (Vcode_conversion_map_vector)->size;
e34b1164
KH
1886 int i;
1887 Lisp_Object index;
1888
1889 CHECK_SYMBOL (symbol, 0);
8146262a 1890 CHECK_VECTOR (map, 1);
e34b1164
KH
1891
1892 for (i = 0; i < len; i++)
1893 {
8146262a 1894 Lisp_Object slot = XVECTOR (Vcode_conversion_map_vector)->contents[i];
e34b1164
KH
1895
1896 if (!CONSP (slot))
1897 break;
1898
1899 if (EQ (symbol, XCONS (slot)->car))
1900 {
1901 index = make_number (i);
8146262a
KH
1902 XCONS (slot)->cdr = map;
1903 Fput (symbol, Qcode_conversion_map, map);
1904 Fput (symbol, Qcode_conversion_map_id, index);
e34b1164
KH
1905 return index;
1906 }
1907 }
1908
1909 if (i == len)
1910 {
1911 Lisp_Object new_vector = Fmake_vector (make_number (len * 2), Qnil);
1912 int j;
1913
1914 for (j = 0; j < len; j++)
1915 XVECTOR (new_vector)->contents[j]
8146262a
KH
1916 = XVECTOR (Vcode_conversion_map_vector)->contents[j];
1917 Vcode_conversion_map_vector = new_vector;
e34b1164
KH
1918 }
1919
1920 index = make_number (i);
8146262a
KH
1921 Fput (symbol, Qcode_conversion_map, map);
1922 Fput (symbol, Qcode_conversion_map_id, index);
1923 XVECTOR (Vcode_conversion_map_vector)->contents[i] = Fcons (symbol, map);
e34b1164
KH
1924 return index;
1925}
1926
1927
dfcf069d 1928void
4ed46869
KH
1929syms_of_ccl ()
1930{
1931 staticpro (&Vccl_program_table);
6703ac4f 1932 Vccl_program_table = Fmake_vector (make_number (32), Qnil);
4ed46869 1933
6ae21908
KH
1934 Qccl_program = intern ("ccl-program");
1935 staticpro (&Qccl_program);
1936
1937 Qccl_program_idx = intern ("ccl-program-idx");
1938 staticpro (&Qccl_program_idx);
e34b1164 1939
8146262a
KH
1940 Qcode_conversion_map = intern ("code-conversion-map");
1941 staticpro (&Qcode_conversion_map);
6ae21908 1942
8146262a
KH
1943 Qcode_conversion_map_id = intern ("code-conversion-map-id");
1944 staticpro (&Qcode_conversion_map_id);
6ae21908 1945
8146262a
KH
1946 DEFVAR_LISP ("code-conversion-map-vector", &Vcode_conversion_map_vector,
1947 "Vector of code conversion maps.");
1948 Vcode_conversion_map_vector = Fmake_vector (make_number (16), Qnil);
e34b1164 1949
4ed46869
KH
1950 DEFVAR_LISP ("font-ccl-encoder-alist", &Vfont_ccl_encoder_alist,
1951 "Alist of fontname patterns vs corresponding CCL program.\n\
1952Each element looks like (REGEXP . CCL-CODE),\n\
1953 where CCL-CODE is a compiled CCL program.\n\
1954When a font whose name matches REGEXP is used for displaying a character,\n\
1955 CCL-CODE is executed to calculate the code point in the font\n\
1956 from the charset number and position code(s) of the character which are set\n\
1957 in CCL registers R0, R1, and R2 before the execution.\n\
1958The code point in the font is set in CCL registers R1 and R2\n\
1959 when the execution terminated.\n\
1960If the font is single-byte font, the register R2 is not used.");
1961 Vfont_ccl_encoder_alist = Qnil;
1962
1963 defsubr (&Sccl_execute);
1964 defsubr (&Sccl_execute_on_string);
1965 defsubr (&Sregister_ccl_program);
8146262a 1966 defsubr (&Sregister_code_conversion_map);
4ed46869
KH
1967}
1968
1969#endif /* emacs */