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