1 /* CCL (Code Conversion Language) interpreter.
2 Copyright (C) 2001-2011 Free Software Foundation, Inc.
3 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 2005, 2006, 2007, 2008, 2009, 2010, 2011
5 National Institute of Advanced Industrial Science and Technology (AIST)
6 Registration Number H14PRO021
8 National Institute of Advanced Industrial Science and Technology (AIST)
9 Registration Number H13PRO009
11 This file is part of GNU Emacs.
13 GNU Emacs is free software: you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
18 GNU Emacs is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
33 #include "character.h"
38 Lisp_Object Qccl
, Qcclp
;
40 /* This symbol is a property which associates with ccl program vector.
41 Ex: (get 'ccl-big5-encoder 'ccl-program) returns ccl program vector. */
42 static Lisp_Object Qccl_program
;
44 /* These symbols are properties which associate with code conversion
45 map and their ID respectively. */
46 static Lisp_Object Qcode_conversion_map
;
47 static Lisp_Object Qcode_conversion_map_id
;
49 /* Symbols of ccl program have this property, a value of the property
50 is an index for Vccl_protram_table. */
51 static Lisp_Object Qccl_program_idx
;
53 /* Table of registered CCL programs. Each element is a vector of
54 NAME, CCL_PROG, RESOLVEDP, and UPDATEDP, where NAME (symbol) is the
55 name of the program, CCL_PROG (vector) is the compiled code of the
56 program, RESOLVEDP (t or nil) is the flag to tell if symbols in
57 CCL_PROG is already resolved to index numbers or not, UPDATEDP (t
58 or nil) is the flat to tell if the CCL program is updated after it
60 static Lisp_Object Vccl_program_table
;
62 /* Return a hash table of id number ID. */
63 #define GET_HASH_TABLE(id) \
64 (XHASH_TABLE (XCDR(XVECTOR(Vtranslation_hash_table_vector)->contents[(id)])))
66 /* CCL (Code Conversion Language) is a simple language which has
67 operations on one input buffer, one output buffer, and 7 registers.
68 The syntax of CCL is described in `ccl.el'. Emacs Lisp function
69 `ccl-compile' compiles a CCL program and produces a CCL code which
70 is a vector of integers. The structure of this vector is as
71 follows: The 1st element: buffer-magnification, a factor for the
72 size of output buffer compared with the size of input buffer. The
73 2nd element: address of CCL code to be executed when encountered
74 with end of input stream. The 3rd and the remaining elements: CCL
77 /* Header of CCL compiled code */
78 #define CCL_HEADER_BUF_MAG 0
79 #define CCL_HEADER_EOF 1
80 #define CCL_HEADER_MAIN 2
82 /* CCL code is a sequence of 28-bit integers. Each contains a CCL
83 command and/or arguments in the following format:
85 |----------------- integer (28-bit) ------------------|
86 |------- 17-bit ------|- 3-bit --|- 3-bit --|- 5-bit -|
87 |--constant argument--|-register-|-register-|-command-|
88 ccccccccccccccccc RRR rrr XXXXX
90 |------- relative address -------|-register-|-command-|
91 cccccccccccccccccccc rrr XXXXX
93 |------------- constant or other args ----------------|
94 cccccccccccccccccccccccccccc
96 where `cc...c' is a 17-bit, 20-bit, or 28-bit integer indicating a
97 constant value or a relative/absolute jump address, `RRR'
98 and `rrr' are CCL register number, `XXXXX' is one of the following
101 #define CCL_CODE_MAX ((1 << (28 - 1)) - 1)
102 #define CCL_CODE_MIN (-1 - CCL_CODE_MAX)
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. */
110 #define CCL_SetRegister 0x00 /* Set register a register value:
111 1:00000000000000000RRRrrrXXXXX
112 ------------------------------
116 #define CCL_SetShortConst 0x01 /* Set register a short constant value:
117 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
118 ------------------------------
119 reg[rrr] = CCCCCCCCCCCCCCCCCCC;
122 #define CCL_SetConst 0x02 /* Set register a constant value:
123 1:00000000000000000000rrrXXXXX
125 ------------------------------
130 #define CCL_SetArray 0x03 /* Set register an element of array:
131 1:CCCCCCCCCCCCCCCCCRRRrrrXXXXX
135 ------------------------------
136 if (0 <= reg[RRR] < CC..C)
137 reg[rrr] = ELEMENT[reg[RRR]];
141 #define CCL_Jump 0x04 /* Jump:
142 1:A--D--D--R--E--S--S-000XXXXX
143 ------------------------------
147 /* Note: If CC..C is greater than 0, the second code is omitted. */
149 #define CCL_JumpCond 0x05 /* Jump conditional:
150 1:A--D--D--R--E--S--S-rrrXXXXX
151 ------------------------------
157 #define CCL_WriteRegisterJump 0x06 /* Write register and jump:
158 1:A--D--D--R--E--S--S-rrrXXXXX
159 ------------------------------
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 -----------------------------
173 /* Note: If read is suspended, the resumed execution starts from the
174 second code (YYYYY == CCL_ReadJump). */
176 #define CCL_WriteConstJump 0x08 /* Write constant and jump:
177 1:A--D--D--R--E--S--S-000XXXXX
179 ------------------------------
184 #define CCL_WriteConstReadJump 0x09 /* Write constant, read, and jump:
185 1:A--D--D--R--E--S--S-rrrXXXXX
187 3:A--D--D--R--E--S--S-rrrYYYYY
188 -----------------------------
194 /* Note: If read is suspended, the resumed execution starts from the
195 second code (YYYYY == CCL_ReadJump). */
197 #define CCL_WriteStringJump 0x0A /* Write string and jump:
198 1:A--D--D--R--E--S--S-000XXXXX
200 3:000MSTRIN[0]STRIN[1]STRIN[2]
202 ------------------------------
204 write_multibyte_string (STRING, LENGTH);
206 write_string (STRING, LENGTH);
210 #define CCL_WriteArrayReadJump 0x0B /* Write an array element, read, and jump:
211 1:A--D--D--R--E--S--S-rrrXXXXX
216 N:A--D--D--R--E--S--S-rrrYYYYY
217 ------------------------------
218 if (0 <= reg[rrr] < LENGTH)
219 write (ELEMENT[reg[rrr]]);
220 IC += LENGTH + 2; (... pointing at N+1)
224 /* Note: If read is suspended, the resumed execution starts from the
225 Nth code (YYYYY == CCL_ReadJump). */
227 #define CCL_ReadJump 0x0C /* Read and jump:
228 1:A--D--D--R--E--S--S-rrrYYYYY
229 -----------------------------
234 #define CCL_Branch 0x0D /* Jump by branch table:
235 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
236 2:A--D--D--R--E-S-S[0]000XXXXX
237 3:A--D--D--R--E-S-S[1]000XXXXX
239 ------------------------------
240 if (0 <= reg[rrr] < CC..C)
241 IC += ADDRESS[reg[rrr]];
243 IC += ADDRESS[CC..C];
246 #define CCL_ReadRegister 0x0E /* Read bytes into registers:
247 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
248 2:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
250 ------------------------------
255 #define CCL_WriteExprConst 0x0F /* write result of expression:
256 1:00000OPERATION000RRR000XXXXX
258 ------------------------------
259 write (reg[RRR] OPERATION CONSTANT);
263 /* Note: If the Nth read is suspended, the resumed execution starts
264 from the Nth code. */
266 #define CCL_ReadBranch 0x10 /* Read one byte into a register,
267 and jump by branch table:
268 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
269 2:A--D--D--R--E-S-S[0]000XXXXX
270 3:A--D--D--R--E-S-S[1]000XXXXX
272 ------------------------------
274 if (0 <= reg[rrr] < CC..C)
275 IC += ADDRESS[reg[rrr]];
277 IC += ADDRESS[CC..C];
280 #define CCL_WriteRegister 0x11 /* Write registers:
281 1:CCCCCCCCCCCCCCCCCCCrrrXXXXX
282 2:CCCCCCCCCCCCCCCCCCCrrrXXXXX
284 ------------------------------
290 /* Note: If the Nth write is suspended, the resumed execution
291 starts from the Nth code. */
293 #define CCL_WriteExprRegister 0x12 /* Write result of expression
294 1:00000OPERATIONRrrRRR000XXXXX
295 ------------------------------
296 write (reg[RRR] OPERATION reg[Rrr]);
299 #define CCL_Call 0x13 /* Call the CCL program whose ID is
301 1:CCCCCCCCCCCCCCCCCCCCFFFXXXXX
302 [2:00000000cccccccccccccccccccc]
303 ------------------------------
311 #define CCL_WriteConstString 0x14 /* Write a constant or a string:
312 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
313 [2:000MSTRIN[0]STRIN[1]STRIN[2]]
315 -----------------------------
320 write_multibyte_string (STRING, CC..C);
322 write_string (STRING, CC..C);
323 IC += (CC..C + 2) / 3;
326 #define CCL_WriteArray 0x15 /* Write an element of array:
327 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
331 ------------------------------
332 if (0 <= reg[rrr] < CC..C)
333 write (ELEMENT[reg[rrr]]);
337 #define CCL_End 0x16 /* Terminate:
338 1:00000000000000000000000XXXXX
339 ------------------------------
343 /* The following two codes execute an assignment arithmetic/logical
344 operation. The form of the operation is like REG OP= OPERAND. */
346 #define CCL_ExprSelfConst 0x17 /* REG OP= constant:
347 1:00000OPERATION000000rrrXXXXX
349 ------------------------------
350 reg[rrr] OPERATION= CONSTANT;
353 #define CCL_ExprSelfReg 0x18 /* REG1 OP= REG2:
354 1:00000OPERATION000RRRrrrXXXXX
355 ------------------------------
356 reg[rrr] OPERATION= reg[RRR];
359 /* The following codes execute an arithmetic/logical operation. The
360 form of the operation is like REG_X = REG_Y OP OPERAND2. */
362 #define CCL_SetExprConst 0x19 /* REG_X = REG_Y OP constant:
363 1:00000OPERATION000RRRrrrXXXXX
365 ------------------------------
366 reg[rrr] = reg[RRR] OPERATION CONSTANT;
370 #define CCL_SetExprReg 0x1A /* REG1 = REG2 OP REG3:
371 1:00000OPERATIONRrrRRRrrrXXXXX
372 ------------------------------
373 reg[rrr] = reg[RRR] OPERATION reg[Rrr];
376 #define CCL_JumpCondExprConst 0x1B /* Jump conditional according to
377 an operation on constant:
378 1:A--D--D--R--E--S--S-rrrXXXXX
381 -----------------------------
382 reg[7] = reg[rrr] OPERATION CONSTANT;
389 #define CCL_JumpCondExprReg 0x1C /* Jump conditional according to
390 an operation on register:
391 1:A--D--D--R--E--S--S-rrrXXXXX
394 -----------------------------
395 reg[7] = reg[rrr] OPERATION reg[RRR];
402 #define CCL_ReadJumpCondExprConst 0x1D /* Read and jump conditional according
403 to an operation on constant:
404 1:A--D--D--R--E--S--S-rrrXXXXX
407 -----------------------------
409 reg[7] = reg[rrr] OPERATION CONSTANT;
416 #define CCL_ReadJumpCondExprReg 0x1E /* Read and jump conditional according
417 to an operation on register:
418 1:A--D--D--R--E--S--S-rrrXXXXX
421 -----------------------------
423 reg[7] = reg[rrr] OPERATION reg[RRR];
430 #define CCL_Extension 0x1F /* Extended CCL code
431 1:ExtendedCOMMNDRrrRRRrrrXXXXX
434 ------------------------------
435 extended_command (rrr,RRR,Rrr,ARGS)
439 Here after, Extended CCL Instructions.
440 Bit length of extended command is 14.
441 Therefore, the instruction code range is 0..16384(0x3fff).
444 /* Read a multibyte character.
445 A code point is stored into reg[rrr]. A charset ID is stored into
448 #define CCL_ReadMultibyteChar2 0x00 /* Read Multibyte Character
449 1:ExtendedCOMMNDRrrRRRrrrXXXXX */
451 /* Write a multibyte character.
452 Write a character whose code point is reg[rrr] and the charset ID
455 #define CCL_WriteMultibyteChar2 0x01 /* Write Multibyte Character
456 1:ExtendedCOMMNDRrrRRRrrrXXXXX */
458 /* Translate a character whose code point is reg[rrr] and the charset
459 ID is reg[RRR] by a translation table whose ID is reg[Rrr].
461 A translated character is set in reg[rrr] (code point) and reg[RRR]
464 #define CCL_TranslateCharacter 0x02 /* Translate a multibyte character
465 1:ExtendedCOMMNDRrrRRRrrrXXXXX */
467 /* Translate a character whose code point is reg[rrr] and the charset
468 ID is reg[RRR] by a translation table whose ID is ARGUMENT.
470 A translated character is set in reg[rrr] (code point) and reg[RRR]
473 #define CCL_TranslateCharacterConstTbl 0x03 /* Translate a multibyte character
474 1:ExtendedCOMMNDRrrRRRrrrXXXXX
475 2:ARGUMENT(Translation Table ID)
478 /* Iterate looking up MAPs for reg[rrr] starting from the Nth (N =
479 reg[RRR]) MAP until some value is found.
481 Each MAP is a Lisp vector whose element is number, nil, t, or
483 If the element is nil, ignore the map and proceed to the next map.
484 If the element is t or lambda, finish without changing reg[rrr].
485 If the element is a number, set reg[rrr] to the number and finish.
487 Detail of the map structure is descibed in the comment for
488 CCL_MapMultiple below. */
490 #define CCL_IterateMultipleMap 0x10 /* Iterate multiple maps
491 1:ExtendedCOMMNDXXXRRRrrrXXXXX
498 /* Map the code in reg[rrr] by MAPs starting from the Nth (N =
501 MAPs are supplied in the succeeding CCL codes as follows:
503 When CCL program gives this nested structure of map to this command:
506 (MAP-ID121 MAP-ID122 MAP-ID123)
509 (MAP-ID211 (MAP-ID2111) MAP-ID212)
511 the compiled CCL codes has this sequence:
512 CCL_MapMultiple (CCL code of this command)
513 16 (total number of MAPs and SEPARATORs)
531 A value of each SEPARATOR follows this rule:
532 MAP-SET := SEPARATOR [(MAP-ID | MAP-SET)]+
533 SEPARATOR := -(number of MAP-IDs and SEPARATORs in the MAP-SET)
535 (*)....Nest level of MAP-SET must not be over than MAX_MAP_SET_LEVEL.
537 When some map fails to map (i.e. it doesn't have a value for
538 reg[rrr]), the mapping is treated as identity.
540 The mapping is iterated for all maps in each map set (set of maps
541 separated by SEPARATOR) except in the case that lambda is
542 encountered. More precisely, the mapping proceeds as below:
544 At first, VAL0 is set to reg[rrr], and it is translated by the
545 first map to VAL1. Then, VAL1 is translated by the next map to
546 VAL2. This mapping is iterated until the last map is used. The
547 result of the mapping is the last value of VAL?. When the mapping
548 process reached to the end of the map set, it moves to the next
549 map set. If the next does not exit, the mapping process terminates,
550 and regard the last value as a result.
552 But, when VALm is mapped to VALn and VALn is not a number, the
553 mapping proceed as below:
555 If VALn is nil, the lastest map is ignored and the mapping of VALm
556 proceed to the next map.
558 In VALn is t, VALm is reverted to reg[rrr] and the mapping of VALm
559 proceed to the next map.
561 If VALn is lambda, move to the next map set like reaching to the
562 end of the current map set.
564 If VALn is a symbol, call the CCL program refered by it.
565 Then, use reg[rrr] as a mapped value except for -1, -2 and -3.
566 Such special values are regarded as nil, t, and lambda respectively.
568 Each map is a Lisp vector of the following format (a) or (b):
569 (a)......[STARTPOINT VAL1 VAL2 ...]
570 (b)......[t VAL STARTPOINT ENDPOINT],
572 STARTPOINT is an offset to be used for indexing a map,
573 ENDPOINT is a maximum index number of a map,
574 VAL and VALn is a number, nil, t, or lambda.
576 Valid index range of a map of type (a) is:
577 STARTPOINT <= index < STARTPOINT + map_size - 1
578 Valid index range of a map of type (b) is:
579 STARTPOINT <= index < ENDPOINT */
581 #define CCL_MapMultiple 0x11 /* Mapping by multiple code conversion maps
582 1:ExtendedCOMMNDXXXRRRrrrXXXXX
594 #define MAX_MAP_SET_LEVEL 30
602 static tr_stack mapping_stack
[MAX_MAP_SET_LEVEL
];
603 static tr_stack
*mapping_stack_pointer
;
605 /* If this variable is non-zero, it indicates the stack_idx
606 of immediately called by CCL_MapMultiple. */
607 static int stack_idx_of_map_multiple
;
609 #define PUSH_MAPPING_STACK(restlen, orig) \
612 mapping_stack_pointer->rest_length = (restlen); \
613 mapping_stack_pointer->orig_val = (orig); \
614 mapping_stack_pointer++; \
618 #define POP_MAPPING_STACK(restlen, orig) \
621 mapping_stack_pointer--; \
622 (restlen) = mapping_stack_pointer->rest_length; \
623 (orig) = mapping_stack_pointer->orig_val; \
627 #define CCL_CALL_FOR_MAP_INSTRUCTION(symbol, ret_ic) \
630 struct ccl_program called_ccl; \
631 if (stack_idx >= 256 \
632 || (setup_ccl_program (&called_ccl, (symbol)) != 0)) \
636 ccl_prog = ccl_prog_stack_struct[0].ccl_prog; \
637 ic = ccl_prog_stack_struct[0].ic; \
638 eof_ic = ccl_prog_stack_struct[0].eof_ic; \
642 ccl_prog_stack_struct[stack_idx].ccl_prog = ccl_prog; \
643 ccl_prog_stack_struct[stack_idx].ic = (ret_ic); \
644 ccl_prog_stack_struct[stack_idx].eof_ic = eof_ic; \
646 ccl_prog = called_ccl.prog; \
647 ic = CCL_HEADER_MAIN; \
648 eof_ic = XFASTINT (ccl_prog[CCL_HEADER_EOF]); \
653 #define CCL_MapSingle 0x12 /* Map by single code conversion map
654 1:ExtendedCOMMNDXXXRRRrrrXXXXX
656 ------------------------------
657 Map reg[rrr] by MAP-ID.
658 If some valid mapping is found,
659 set reg[rrr] to the result,
664 #define CCL_LookupIntConstTbl 0x13 /* Lookup multibyte character by
665 integer key. Afterwards R7 set
666 to 1 if lookup succeeded.
667 1:ExtendedCOMMNDRrrRRRXXXXXXXX
668 2:ARGUMENT(Hash table ID) */
670 #define CCL_LookupCharConstTbl 0x14 /* Lookup integer by multibyte
671 character key. Afterwards R7 set
672 to 1 if lookup succeeded.
673 1:ExtendedCOMMNDRrrRRRrrrXXXXX
674 2:ARGUMENT(Hash table ID) */
676 /* CCL arithmetic/logical operators. */
677 #define CCL_PLUS 0x00 /* X = Y + Z */
678 #define CCL_MINUS 0x01 /* X = Y - Z */
679 #define CCL_MUL 0x02 /* X = Y * Z */
680 #define CCL_DIV 0x03 /* X = Y / Z */
681 #define CCL_MOD 0x04 /* X = Y % Z */
682 #define CCL_AND 0x05 /* X = Y & Z */
683 #define CCL_OR 0x06 /* X = Y | Z */
684 #define CCL_XOR 0x07 /* X = Y ^ Z */
685 #define CCL_LSH 0x08 /* X = Y << Z */
686 #define CCL_RSH 0x09 /* X = Y >> Z */
687 #define CCL_LSH8 0x0A /* X = (Y << 8) | Z */
688 #define CCL_RSH8 0x0B /* X = Y >> 8, r[7] = Y & 0xFF */
689 #define CCL_DIVMOD 0x0C /* X = Y / Z, r[7] = Y % Z */
690 #define CCL_LS 0x10 /* X = (X < Y) */
691 #define CCL_GT 0x11 /* X = (X > Y) */
692 #define CCL_EQ 0x12 /* X = (X == Y) */
693 #define CCL_LE 0x13 /* X = (X <= Y) */
694 #define CCL_GE 0x14 /* X = (X >= Y) */
695 #define CCL_NE 0x15 /* X = (X != Y) */
697 #define CCL_DECODE_SJIS 0x16 /* X = HIGHER_BYTE (DE-SJIS (Y, Z))
698 r[7] = LOWER_BYTE (DE-SJIS (Y, Z)) */
699 #define CCL_ENCODE_SJIS 0x17 /* X = HIGHER_BYTE (SJIS (Y, Z))
700 r[7] = LOWER_BYTE (SJIS (Y, Z) */
702 /* Terminate CCL program successfully. */
703 #define CCL_SUCCESS \
706 ccl->status = CCL_STAT_SUCCESS; \
711 /* Suspend CCL program because of reading from empty input buffer or
712 writing to full output buffer. When this program is resumed, the
713 same I/O command is executed. */
714 #define CCL_SUSPEND(stat) \
718 ccl->status = stat; \
723 /* Terminate CCL program because of invalid command. Should not occur
724 in the normal case. */
727 #define CCL_INVALID_CMD \
730 ccl->status = CCL_STAT_INVALID_CMD; \
731 goto ccl_error_handler; \
737 #define CCL_INVALID_CMD \
740 ccl_debug_hook (this_ic); \
741 ccl->status = CCL_STAT_INVALID_CMD; \
742 goto ccl_error_handler; \
748 #define GET_CCL_RANGE(var, ccl_prog, ic, lo, hi) \
751 EMACS_INT prog_word = XINT ((ccl_prog)[ic]); \
752 if (! ((lo) <= prog_word && prog_word <= (hi))) \
758 #define GET_CCL_CODE(code, ccl_prog, ic) \
759 GET_CCL_RANGE (code, ccl_prog, ic, CCL_CODE_MIN, CCL_CODE_MAX)
761 #define GET_CCL_INT(var, ccl_prog, ic) \
762 GET_CCL_RANGE (var, ccl_prog, ic, INT_MIN, INT_MAX)
764 #define IN_INT_RANGE(val) (INT_MIN <= (val) && (val) <= INT_MAX)
766 /* Encode one character CH to multibyte form and write to the current
767 output buffer. If CH is less than 256, CH is written as is. */
768 #define CCL_WRITE_CHAR(ch) \
772 else if (dst < dst_end) \
775 CCL_SUSPEND (CCL_STAT_SUSPEND_BY_DST); \
778 /* Write a string at ccl_prog[IC] of length LEN to the current output
780 #define CCL_WRITE_STRING(len) \
785 else if (dst + len <= dst_end) \
787 if (XFASTINT (ccl_prog[ic]) & 0x1000000) \
788 for (ccli = 0; ccli < len; ccli++) \
789 *dst++ = XFASTINT (ccl_prog[ic + ccli]) & 0xFFFFFF; \
791 for (ccli = 0; ccli < len; ccli++) \
792 *dst++ = ((XFASTINT (ccl_prog[ic + (ccli / 3)])) \
793 >> ((2 - (ccli % 3)) * 8)) & 0xFF; \
796 CCL_SUSPEND (CCL_STAT_SUSPEND_BY_DST); \
799 /* Read one byte from the current input buffer into Rth register. */
800 #define CCL_READ_CHAR(r) \
804 else if (src < src_end) \
806 else if (ccl->last_block) \
813 CCL_SUSPEND (CCL_STAT_SUSPEND_BY_SRC); \
816 /* Decode CODE by a charset whose id is ID. If ID is 0, return CODE
817 as is for backward compatibility. Assume that we can use the
818 variable `charset'. */
820 #define CCL_DECODE_CHAR(id, code) \
821 ((id) == 0 ? (code) \
822 : (charset = CHARSET_FROM_ID ((id)), DECODE_CHAR (charset, (code))))
824 /* Encode character C by some of charsets in CHARSET_LIST. Set ID to
825 the id of the used charset, ENCODED to the resulf of encoding.
826 Assume that we can use the variable `charset'. */
828 #define CCL_ENCODE_CHAR(c, charset_list, id, encoded) \
832 charset = char_charset ((c), (charset_list), &ncode); \
833 if (! charset && ! NILP (charset_list)) \
834 charset = char_charset ((c), Qnil, &ncode); \
837 (id) = CHARSET_ID (charset); \
842 /* Execute CCL code on characters at SOURCE (length SRC_SIZE). The
843 resulting text goes to a place pointed by DESTINATION, the length
844 of which should not exceed DST_SIZE. As a side effect, how many
845 characters are consumed and produced are recorded in CCL->consumed
846 and CCL->produced, and the contents of CCL registers are updated.
847 If SOURCE or DESTINATION is NULL, only operations on registers are
851 #define CCL_DEBUG_BACKTRACE_LEN 256
852 int ccl_backtrace_table
[CCL_DEBUG_BACKTRACE_LEN
];
853 int ccl_backtrace_idx
;
856 ccl_debug_hook (int ic
)
863 struct ccl_prog_stack
865 Lisp_Object
*ccl_prog
; /* Pointer to an array of CCL code. */
866 int ic
; /* Instruction Counter. */
867 int eof_ic
; /* Instruction Counter to jump on EOF. */
870 /* For the moment, we only support depth 256 of stack. */
871 static struct ccl_prog_stack ccl_prog_stack_struct
[256];
874 ccl_driver (struct ccl_program
*ccl
, int *source
, int *destination
, int src_size
, int dst_size
, Lisp_Object charset_list
)
876 register int *reg
= ccl
->reg
;
877 register int ic
= ccl
->ic
;
878 register int code
= 0, field1
, field2
;
879 register Lisp_Object
*ccl_prog
= ccl
->prog
;
880 int *src
= source
, *src_end
= src
+ src_size
;
881 int *dst
= destination
, *dst_end
= dst
+ dst_size
;
884 int stack_idx
= ccl
->stack_idx
;
885 /* Instruction counter of the current CCL code. */
887 struct charset
*charset
;
888 int eof_ic
= ccl
->eof_ic
;
891 if (ccl
->buf_magnification
== 0) /* We can't read/produce any bytes. */
894 /* Set mapping stack pointer. */
895 mapping_stack_pointer
= mapping_stack
;
898 ccl_backtrace_idx
= 0;
905 ccl_backtrace_table
[ccl_backtrace_idx
++] = ic
;
906 if (ccl_backtrace_idx
>= CCL_DEBUG_BACKTRACE_LEN
)
907 ccl_backtrace_idx
= 0;
908 ccl_backtrace_table
[ccl_backtrace_idx
] = 0;
911 if (!NILP (Vquit_flag
) && NILP (Vinhibit_quit
))
913 /* We can't just signal Qquit, instead break the loop as if
914 the whole data is processed. Don't reset Vquit_flag, it
915 must be handled later at a safer place. */
917 src
= source
+ src_size
;
918 ccl
->status
= CCL_STAT_QUIT
;
923 GET_CCL_CODE (code
, ccl_prog
, ic
++);
925 field2
= (code
& 0xFF) >> 5;
928 #define RRR (field1 & 7)
929 #define Rrr ((field1 >> 3) & 7)
931 #define EXCMD (field1 >> 6)
935 case CCL_SetRegister
: /* 00000000000000000RRRrrrXXXXX */
939 case CCL_SetShortConst
: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
943 case CCL_SetConst
: /* 00000000000000000000rrrXXXXX */
944 GET_CCL_INT (reg
[rrr
], ccl_prog
, ic
++);
947 case CCL_SetArray
: /* CCCCCCCCCCCCCCCCCCCCRRRrrrXXXXX */
951 GET_CCL_INT (reg
[rrr
], ccl_prog
, ic
+ i
);
955 case CCL_Jump
: /* A--D--D--R--E--S--S-000XXXXX */
959 case CCL_JumpCond
: /* A--D--D--R--E--S--S-rrrXXXXX */
964 case CCL_WriteRegisterJump
: /* A--D--D--R--E--S--S-rrrXXXXX */
970 case CCL_WriteRegisterReadJump
: /* A--D--D--R--E--S--S-rrrXXXXX */
974 CCL_READ_CHAR (reg
[rrr
]);
978 case CCL_WriteConstJump
: /* A--D--D--R--E--S--S-000XXXXX */
979 GET_CCL_INT (i
, ccl_prog
, ic
);
984 case CCL_WriteConstReadJump
: /* A--D--D--R--E--S--S-rrrXXXXX */
985 GET_CCL_INT (i
, ccl_prog
, ic
);
988 CCL_READ_CHAR (reg
[rrr
]);
992 case CCL_WriteStringJump
: /* A--D--D--R--E--S--S-000XXXXX */
993 GET_CCL_INT (j
, ccl_prog
, ic
++);
994 CCL_WRITE_STRING (j
);
998 case CCL_WriteArrayReadJump
: /* A--D--D--R--E--S--S-rrrXXXXX */
1000 GET_CCL_INT (j
, ccl_prog
, ic
);
1001 if (0 <= i
&& i
< j
)
1003 GET_CCL_INT (i
, ccl_prog
, ic
+ 1 + i
);
1007 CCL_READ_CHAR (reg
[rrr
]);
1008 ic
+= ADDR
- (j
+ 2);
1011 case CCL_ReadJump
: /* A--D--D--R--E--S--S-rrrYYYYY */
1012 CCL_READ_CHAR (reg
[rrr
]);
1016 case CCL_ReadBranch
: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
1017 CCL_READ_CHAR (reg
[rrr
]);
1018 /* fall through ... */
1019 case CCL_Branch
: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
1022 GET_CCL_INT (incr
, ccl_prog
,
1023 ic
+ (0 <= reg
[rrr
] && reg
[rrr
] < field1
1030 case CCL_ReadRegister
: /* CCCCCCCCCCCCCCCCCCCCrrXXXXX */
1033 CCL_READ_CHAR (reg
[rrr
]);
1035 GET_CCL_CODE (code
, ccl_prog
, ic
++);
1037 field2
= (code
& 0xFF) >> 5;
1041 case CCL_WriteExprConst
: /* 1:00000OPERATION000RRR000XXXXX */
1044 GET_CCL_INT (j
, ccl_prog
, ic
);
1046 jump_address
= ic
+ 1;
1049 case CCL_WriteRegister
: /* CCCCCCCCCCCCCCCCCCCrrrXXXXX */
1055 GET_CCL_CODE (code
, ccl_prog
, ic
++);
1057 field2
= (code
& 0xFF) >> 5;
1061 case CCL_WriteExprRegister
: /* 1:00000OPERATIONRrrRRR000XXXXX */
1069 case CCL_Call
: /* 1:CCCCCCCCCCCCCCCCCCCCFFFXXXXX */
1074 /* If FFF is nonzero, the CCL program ID is in the
1077 GET_CCL_INT (prog_id
, ccl_prog
, ic
++);
1081 if (stack_idx
>= 256
1083 || prog_id
>= ASIZE (Vccl_program_table
)
1084 || (slot
= AREF (Vccl_program_table
, prog_id
), !VECTORP (slot
))
1085 || !VECTORP (AREF (slot
, 1)))
1089 ccl_prog
= ccl_prog_stack_struct
[0].ccl_prog
;
1090 ic
= ccl_prog_stack_struct
[0].ic
;
1091 eof_ic
= ccl_prog_stack_struct
[0].eof_ic
;
1096 ccl_prog_stack_struct
[stack_idx
].ccl_prog
= ccl_prog
;
1097 ccl_prog_stack_struct
[stack_idx
].ic
= ic
;
1098 ccl_prog_stack_struct
[stack_idx
].eof_ic
= eof_ic
;
1100 ccl_prog
= XVECTOR (AREF (slot
, 1))->contents
;
1101 ic
= CCL_HEADER_MAIN
;
1102 eof_ic
= XFASTINT (ccl_prog
[CCL_HEADER_EOF
]);
1106 case CCL_WriteConstString
: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
1108 CCL_WRITE_CHAR (field1
);
1111 CCL_WRITE_STRING (field1
);
1112 ic
+= (field1
+ 2) / 3;
1116 case CCL_WriteArray
: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
1118 if (0 <= i
&& i
< field1
)
1120 GET_CCL_INT (j
, ccl_prog
, ic
+ i
);
1126 case CCL_End
: /* 0000000000000000000000XXXXX */
1130 ccl_prog
= ccl_prog_stack_struct
[stack_idx
].ccl_prog
;
1131 ic
= ccl_prog_stack_struct
[stack_idx
].ic
;
1132 eof_ic
= ccl_prog_stack_struct
[stack_idx
].eof_ic
;
1139 /* ccl->ic should points to this command code again to
1140 suppress further processing. */
1144 case CCL_ExprSelfConst
: /* 00000OPERATION000000rrrXXXXX */
1145 GET_CCL_INT (i
, ccl_prog
, ic
++);
1149 case CCL_ExprSelfReg
: /* 00000OPERATION000RRRrrrXXXXX */
1156 case CCL_PLUS
: reg
[rrr
] += i
; break;
1157 case CCL_MINUS
: reg
[rrr
] -= i
; break;
1158 case CCL_MUL
: reg
[rrr
] *= i
; break;
1159 case CCL_DIV
: reg
[rrr
] /= i
; break;
1160 case CCL_MOD
: reg
[rrr
] %= i
; break;
1161 case CCL_AND
: reg
[rrr
] &= i
; break;
1162 case CCL_OR
: reg
[rrr
] |= i
; break;
1163 case CCL_XOR
: reg
[rrr
] ^= i
; break;
1164 case CCL_LSH
: reg
[rrr
] <<= i
; break;
1165 case CCL_RSH
: reg
[rrr
] >>= i
; break;
1166 case CCL_LSH8
: reg
[rrr
] <<= 8; reg
[rrr
] |= i
; break;
1167 case CCL_RSH8
: reg
[7] = reg
[rrr
] & 0xFF; reg
[rrr
] >>= 8; break;
1168 case CCL_DIVMOD
: reg
[7] = reg
[rrr
] % i
; reg
[rrr
] /= i
; break;
1169 case CCL_LS
: reg
[rrr
] = reg
[rrr
] < i
; break;
1170 case CCL_GT
: reg
[rrr
] = reg
[rrr
] > i
; break;
1171 case CCL_EQ
: reg
[rrr
] = reg
[rrr
] == i
; break;
1172 case CCL_LE
: reg
[rrr
] = reg
[rrr
] <= i
; break;
1173 case CCL_GE
: reg
[rrr
] = reg
[rrr
] >= i
; break;
1174 case CCL_NE
: reg
[rrr
] = reg
[rrr
] != i
; break;
1175 default: CCL_INVALID_CMD
;
1179 case CCL_SetExprConst
: /* 00000OPERATION000RRRrrrXXXXX */
1181 GET_CCL_INT (j
, ccl_prog
, ic
++);
1186 case CCL_SetExprReg
: /* 00000OPERATIONRrrRRRrrrXXXXX */
1193 case CCL_ReadJumpCondExprConst
: /* A--D--D--R--E--S--S-rrrXXXXX */
1194 CCL_READ_CHAR (reg
[rrr
]);
1195 case CCL_JumpCondExprConst
: /* A--D--D--R--E--S--S-rrrXXXXX */
1197 jump_address
= ic
+ ADDR
;
1198 GET_CCL_INT (op
, ccl_prog
, ic
++);
1199 GET_CCL_INT (j
, ccl_prog
, ic
++);
1203 case CCL_ReadJumpCondExprReg
: /* A--D--D--R--E--S--S-rrrXXXXX */
1204 CCL_READ_CHAR (reg
[rrr
]);
1205 case CCL_JumpCondExprReg
:
1207 jump_address
= ic
+ ADDR
;
1208 GET_CCL_INT (op
, ccl_prog
, ic
++);
1209 GET_CCL_RANGE (j
, ccl_prog
, ic
++, 0, 7);
1216 case CCL_PLUS
: reg
[rrr
] = i
+ j
; break;
1217 case CCL_MINUS
: reg
[rrr
] = i
- j
; break;
1218 case CCL_MUL
: reg
[rrr
] = i
* j
; break;
1219 case CCL_DIV
: reg
[rrr
] = i
/ j
; break;
1220 case CCL_MOD
: reg
[rrr
] = i
% j
; break;
1221 case CCL_AND
: reg
[rrr
] = i
& j
; break;
1222 case CCL_OR
: reg
[rrr
] = i
| j
; break;
1223 case CCL_XOR
: reg
[rrr
] = i
^ j
; break;
1224 case CCL_LSH
: reg
[rrr
] = i
<< j
; break;
1225 case CCL_RSH
: reg
[rrr
] = i
>> j
; break;
1226 case CCL_LSH8
: reg
[rrr
] = (i
<< 8) | j
; break;
1227 case CCL_RSH8
: reg
[rrr
] = i
>> 8; reg
[7] = i
& 0xFF; break;
1228 case CCL_DIVMOD
: reg
[rrr
] = i
/ j
; reg
[7] = i
% j
; break;
1229 case CCL_LS
: reg
[rrr
] = i
< j
; break;
1230 case CCL_GT
: reg
[rrr
] = i
> j
; break;
1231 case CCL_EQ
: reg
[rrr
] = i
== j
; break;
1232 case CCL_LE
: reg
[rrr
] = i
<= j
; break;
1233 case CCL_GE
: reg
[rrr
] = i
>= j
; break;
1234 case CCL_NE
: reg
[rrr
] = i
!= j
; break;
1235 case CCL_DECODE_SJIS
:
1243 case CCL_ENCODE_SJIS
:
1251 default: CCL_INVALID_CMD
;
1254 if (code
== CCL_WriteExprConst
|| code
== CCL_WriteExprRegister
)
1267 case CCL_ReadMultibyteChar2
:
1271 CCL_ENCODE_CHAR (i
, charset_list
, reg
[RRR
], reg
[rrr
]);
1274 case CCL_WriteMultibyteChar2
:
1277 i
= CCL_DECODE_CHAR (reg
[RRR
], reg
[rrr
]);
1281 case CCL_TranslateCharacter
:
1282 i
= CCL_DECODE_CHAR (reg
[RRR
], reg
[rrr
]);
1283 op
= translate_char (GET_TRANSLATION_TABLE (reg
[Rrr
]), i
);
1284 CCL_ENCODE_CHAR (op
, charset_list
, reg
[RRR
], reg
[rrr
]);
1287 case CCL_TranslateCharacterConstTbl
:
1290 GET_CCL_RANGE (eop
, ccl_prog
, ic
++, 0,
1291 (VECTORP (Vtranslation_table_vector
)
1292 ? ASIZE (Vtranslation_table_vector
)
1294 i
= CCL_DECODE_CHAR (reg
[RRR
], reg
[rrr
]);
1295 op
= translate_char (GET_TRANSLATION_TABLE (eop
), i
);
1296 CCL_ENCODE_CHAR (op
, charset_list
, reg
[RRR
], reg
[rrr
]);
1300 case CCL_LookupIntConstTbl
:
1303 struct Lisp_Hash_Table
*h
;
1304 GET_CCL_RANGE (eop
, ccl_prog
, ic
++, 0,
1305 (VECTORP (Vtranslation_hash_table_vector
)
1306 ? ASIZE (Vtranslation_hash_table_vector
)
1308 h
= GET_HASH_TABLE (eop
);
1310 eop
= hash_lookup (h
, make_number (reg
[RRR
]), NULL
);
1314 opl
= HASH_VALUE (h
, eop
);
1315 if (! (IN_INT_RANGE (eop
) && CHARACTERP (opl
)))
1317 reg
[RRR
] = charset_unicode
;
1319 reg
[7] = 1; /* r7 true for success */
1326 case CCL_LookupCharConstTbl
:
1329 struct Lisp_Hash_Table
*h
;
1330 GET_CCL_RANGE (eop
, ccl_prog
, ic
++, 0,
1331 (VECTORP (Vtranslation_hash_table_vector
)
1332 ? ASIZE (Vtranslation_hash_table_vector
)
1334 i
= CCL_DECODE_CHAR (reg
[RRR
], reg
[rrr
]);
1335 h
= GET_HASH_TABLE (eop
);
1337 eop
= hash_lookup (h
, make_number (i
), NULL
);
1341 opl
= HASH_VALUE (h
, eop
);
1342 if (! (INTEGERP (opl
) && IN_INT_RANGE (XINT (opl
))))
1344 reg
[RRR
] = XINT (opl
);
1345 reg
[7] = 1; /* r7 true for success */
1352 case CCL_IterateMultipleMap
:
1354 Lisp_Object map
, content
, attrib
, value
;
1355 EMACS_INT point
, size
;
1358 GET_CCL_INT (j
, ccl_prog
, ic
++); /* number of maps. */
1361 if ((j
> reg
[RRR
]) && (j
>= 0))
1376 size
= ASIZE (Vcode_conversion_map_vector
);
1377 point
= XINT (ccl_prog
[ic
++]);
1378 if (! (0 <= point
&& point
< size
)) continue;
1379 map
= AREF (Vcode_conversion_map_vector
, point
);
1381 /* Check map validity. */
1382 if (!CONSP (map
)) continue;
1384 if (!VECTORP (map
)) continue;
1386 if (size
<= 1) continue;
1388 content
= AREF (map
, 0);
1391 [STARTPOINT VAL1 VAL2 ...] or
1392 [t ELEMENT STARTPOINT ENDPOINT] */
1393 if (INTEGERP (content
))
1395 point
= XINT (content
);
1396 if (!(point
<= op
&& op
- point
+ 1 < size
)) continue;
1397 content
= AREF (map
, op
- point
+ 1);
1399 else if (EQ (content
, Qt
))
1401 if (size
!= 4) continue;
1402 if (INTEGERP (AREF (map
, 2))
1403 && XINT (AREF (map
, 2)) <= op
1404 && INTEGERP (AREF (map
, 3))
1405 && op
< XINT (AREF (map
, 3)))
1406 content
= AREF (map
, 1);
1415 else if (INTEGERP (content
) && IN_INT_RANGE (XINT (content
)))
1418 reg
[rrr
] = XINT(content
);
1421 else if (EQ (content
, Qt
) || EQ (content
, Qlambda
))
1426 else if (CONSP (content
))
1428 attrib
= XCAR (content
);
1429 value
= XCDR (content
);
1430 if (! (INTEGERP (attrib
) && INTEGERP (value
)
1431 && IN_INT_RANGE (XINT (value
))))
1434 reg
[rrr
] = XINT (value
);
1437 else if (SYMBOLP (content
))
1438 CCL_CALL_FOR_MAP_INSTRUCTION (content
, fin_ic
);
1448 case CCL_MapMultiple
:
1450 Lisp_Object map
, content
, attrib
, value
;
1451 int point
, size
, map_vector_size
;
1452 int map_set_rest_length
, fin_ic
;
1453 int current_ic
= this_ic
;
1455 /* inhibit recursive call on MapMultiple. */
1456 if (stack_idx_of_map_multiple
> 0)
1458 if (stack_idx_of_map_multiple
<= stack_idx
)
1460 stack_idx_of_map_multiple
= 0;
1461 mapping_stack_pointer
= mapping_stack
;
1466 mapping_stack_pointer
= mapping_stack
;
1467 stack_idx_of_map_multiple
= 0;
1469 /* Get number of maps and separators. */
1470 GET_CCL_INT (map_set_rest_length
, ccl_prog
, ic
++);
1472 fin_ic
= ic
+ map_set_rest_length
;
1475 if ((map_set_rest_length
> reg
[RRR
]) && (reg
[RRR
] >= 0))
1479 map_set_rest_length
-= i
;
1485 mapping_stack_pointer
= mapping_stack
;
1489 if (mapping_stack_pointer
<= (mapping_stack
+ 1))
1491 /* Set up initial state. */
1492 mapping_stack_pointer
= mapping_stack
;
1493 PUSH_MAPPING_STACK (0, op
);
1498 /* Recover after calling other ccl program. */
1501 POP_MAPPING_STACK (map_set_rest_length
, orig_op
);
1502 POP_MAPPING_STACK (map_set_rest_length
, reg
[rrr
]);
1506 /* Regard it as Qnil. */
1510 map_set_rest_length
--;
1513 /* Regard it as Qt. */
1517 map_set_rest_length
--;
1520 /* Regard it as Qlambda. */
1522 i
+= map_set_rest_length
;
1523 ic
+= map_set_rest_length
;
1524 map_set_rest_length
= 0;
1527 /* Regard it as normal mapping. */
1528 i
+= map_set_rest_length
;
1529 ic
+= map_set_rest_length
;
1530 POP_MAPPING_STACK (map_set_rest_length
, reg
[rrr
]);
1534 map_vector_size
= ASIZE (Vcode_conversion_map_vector
);
1537 for (;map_set_rest_length
> 0;i
++, ic
++, map_set_rest_length
--)
1539 GET_CCL_INT (point
, ccl_prog
, ic
);
1542 /* +1 is for including separator. */
1544 if (mapping_stack_pointer
1545 >= &mapping_stack
[MAX_MAP_SET_LEVEL
])
1547 PUSH_MAPPING_STACK (map_set_rest_length
- point
,
1549 map_set_rest_length
= point
;
1554 if (point
>= map_vector_size
) continue;
1555 map
= AREF (Vcode_conversion_map_vector
, point
);
1557 /* Check map validity. */
1558 if (!CONSP (map
)) continue;
1560 if (!VECTORP (map
)) continue;
1562 if (size
<= 1) continue;
1564 content
= AREF (map
, 0);
1567 [STARTPOINT VAL1 VAL2 ...] or
1568 [t ELEMENT STARTPOINT ENDPOINT] */
1569 if (INTEGERP (content
))
1571 point
= XINT (content
);
1572 if (!(point
<= op
&& op
- point
+ 1 < size
)) continue;
1573 content
= AREF (map
, op
- point
+ 1);
1575 else if (EQ (content
, Qt
))
1577 if (size
!= 4) continue;
1578 if (INTEGERP (AREF (map
, 2))
1579 && XINT (AREF (map
, 2)) <= op
1580 && INTEGERP (AREF (map
, 3))
1581 && op
< XINT (AREF (map
, 3)))
1582 content
= AREF (map
, 1);
1593 if (INTEGERP (content
) && IN_INT_RANGE (XINT (content
)))
1595 op
= XINT (content
);
1596 i
+= map_set_rest_length
- 1;
1597 ic
+= map_set_rest_length
- 1;
1598 POP_MAPPING_STACK (map_set_rest_length
, reg
[rrr
]);
1599 map_set_rest_length
++;
1601 else if (CONSP (content
))
1603 attrib
= XCAR (content
);
1604 value
= XCDR (content
);
1605 if (! (INTEGERP (attrib
) && INTEGERP (value
)
1606 && IN_INT_RANGE (XINT (value
))))
1609 i
+= map_set_rest_length
- 1;
1610 ic
+= map_set_rest_length
- 1;
1611 POP_MAPPING_STACK (map_set_rest_length
, reg
[rrr
]);
1612 map_set_rest_length
++;
1614 else if (EQ (content
, Qt
))
1618 else if (EQ (content
, Qlambda
))
1620 i
+= map_set_rest_length
;
1621 ic
+= map_set_rest_length
;
1624 else if (SYMBOLP (content
))
1626 if (mapping_stack_pointer
1627 >= &mapping_stack
[MAX_MAP_SET_LEVEL
])
1629 PUSH_MAPPING_STACK (map_set_rest_length
, reg
[rrr
]);
1630 PUSH_MAPPING_STACK (map_set_rest_length
, op
);
1631 stack_idx_of_map_multiple
= stack_idx
+ 1;
1632 CCL_CALL_FOR_MAP_INSTRUCTION (content
, current_ic
);
1637 if (mapping_stack_pointer
<= (mapping_stack
+ 1))
1639 POP_MAPPING_STACK (map_set_rest_length
, reg
[rrr
]);
1640 i
+= map_set_rest_length
;
1641 ic
+= map_set_rest_length
;
1642 POP_MAPPING_STACK (map_set_rest_length
, reg
[rrr
]);
1652 Lisp_Object map
, attrib
, value
, content
;
1654 j
= XINT (ccl_prog
[ic
++]); /* map_id */
1656 if (j
>= ASIZE (Vcode_conversion_map_vector
))
1661 map
= AREF (Vcode_conversion_map_vector
, j
);
1668 if (! (VECTORP (map
)
1669 && INTEGERP (AREF (map
, 0))
1670 && XINT (AREF (map
, 0)) <= op
1671 && op
- XINT (AREF (map
, 0)) + 1 < ASIZE (map
)))
1676 point
= XINT (AREF (map
, 0));
1677 point
= op
- point
+ 1;
1679 content
= AREF (map
, point
);
1682 else if (INTEGERP (content
))
1683 reg
[rrr
] = XINT (content
);
1684 else if (EQ (content
, Qt
));
1685 else if (CONSP (content
))
1687 attrib
= XCAR (content
);
1688 value
= XCDR (content
);
1689 if (!INTEGERP (attrib
) || !INTEGERP (value
))
1691 reg
[rrr
] = XINT(value
);
1694 else if (SYMBOLP (content
))
1695 CCL_CALL_FOR_MAP_INSTRUCTION (content
, ic
);
1712 /* The suppress_error member is set when e.g. a CCL-based coding
1713 system is used for terminal output. */
1714 if (!ccl
->suppress_error
&& destination
)
1716 /* We can insert an error message only if DESTINATION is
1717 specified and we still have a room to store the message
1725 switch (ccl
->status
)
1727 case CCL_STAT_INVALID_CMD
:
1728 sprintf(msg
, "\nCCL: Invalid command %x (ccl_code = %x) at %d.",
1729 code
& 0x1F, code
, this_ic
);
1732 int i
= ccl_backtrace_idx
- 1;
1735 msglen
= strlen (msg
);
1736 if (dst
+ msglen
<= (dst_bytes
? dst_end
: src
))
1738 memcpy (dst
, msg
, msglen
);
1742 for (j
= 0; j
< CCL_DEBUG_BACKTRACE_LEN
; j
++, i
--)
1744 if (i
< 0) i
= CCL_DEBUG_BACKTRACE_LEN
- 1;
1745 if (ccl_backtrace_table
[i
] == 0)
1747 sprintf(msg
, " %d", ccl_backtrace_table
[i
]);
1748 msglen
= strlen (msg
);
1749 if (dst
+ msglen
> (dst_bytes
? dst_end
: src
))
1751 memcpy (dst
, msg
, msglen
);
1760 if (! ccl
->quit_silently
)
1761 sprintf(msg
, "\nCCL: Quited.");
1765 sprintf(msg
, "\nCCL: Unknown error type (%d)", ccl
->status
);
1768 msglen
= strlen (msg
);
1769 if (dst
+ msglen
<= dst_end
)
1771 for (i
= 0; i
< msglen
; i
++)
1775 if (ccl
->status
== CCL_STAT_INVALID_CMD
)
1777 #if 0 /* If the remaining bytes contain 0x80..0x9F, copying them
1778 results in an invalid multibyte sequence. */
1780 /* Copy the remaining source data. */
1781 int i
= src_end
- src
;
1782 if (dst_bytes
&& (dst_end
- dst
) < i
)
1784 memcpy (dst
, src
, i
);
1788 /* Signal that we've consumed everything. */
1796 ccl
->stack_idx
= stack_idx
;
1797 ccl
->prog
= ccl_prog
;
1798 ccl
->consumed
= src
- source
;
1800 ccl
->produced
= dst
- destination
;
1805 /* Resolve symbols in the specified CCL code (Lisp vector). This
1806 function converts symbols of code conversion maps and character
1807 translation tables embeded in the CCL code into their ID numbers.
1809 The return value is a vector (CCL itself or a new vector in which
1810 all symbols are resolved), Qt if resolving of some symbol failed,
1811 or nil if CCL contains invalid data. */
1814 resolve_symbol_ccl_program (Lisp_Object ccl
)
1816 int i
, veclen
, unresolved
= 0;
1817 Lisp_Object result
, contents
, val
;
1820 veclen
= ASIZE (result
);
1822 for (i
= 0; i
< veclen
; i
++)
1824 contents
= AREF (result
, i
);
1825 if (INTEGERP (contents
))
1827 else if (CONSP (contents
)
1828 && SYMBOLP (XCAR (contents
))
1829 && SYMBOLP (XCDR (contents
)))
1831 /* This is the new style for embedding symbols. The form is
1832 (SYMBOL . PROPERTY). (get SYMBOL PROPERTY) should give
1835 if (EQ (result
, ccl
))
1836 result
= Fcopy_sequence (ccl
);
1838 val
= Fget (XCAR (contents
), XCDR (contents
));
1840 ASET (result
, i
, val
);
1845 else if (SYMBOLP (contents
))
1847 /* This is the old style for embedding symbols. This style
1848 may lead to a bug if, for instance, a translation table
1849 and a code conversion map have the same name. */
1850 if (EQ (result
, ccl
))
1851 result
= Fcopy_sequence (ccl
);
1853 val
= Fget (contents
, Qtranslation_table_id
);
1855 ASET (result
, i
, val
);
1858 val
= Fget (contents
, Qcode_conversion_map_id
);
1860 ASET (result
, i
, val
);
1863 val
= Fget (contents
, Qccl_program_idx
);
1865 ASET (result
, i
, val
);
1875 return (unresolved
? Qt
: result
);
1878 /* Return the compiled code (vector) of CCL program CCL_PROG.
1879 CCL_PROG is a name (symbol) of the program or already compiled
1880 code. If necessary, resolve symbols in the compiled code to index
1881 numbers. If we failed to get the compiled code or to resolve
1882 symbols, return Qnil. */
1885 ccl_get_compiled_code (Lisp_Object ccl_prog
, int *idx
)
1887 Lisp_Object val
, slot
;
1889 if (VECTORP (ccl_prog
))
1891 val
= resolve_symbol_ccl_program (ccl_prog
);
1893 return (VECTORP (val
) ? val
: Qnil
);
1895 if (!SYMBOLP (ccl_prog
))
1898 val
= Fget (ccl_prog
, Qccl_program_idx
);
1900 || XINT (val
) >= ASIZE (Vccl_program_table
))
1902 slot
= AREF (Vccl_program_table
, XINT (val
));
1903 if (! VECTORP (slot
)
1904 || ASIZE (slot
) != 4
1905 || ! VECTORP (AREF (slot
, 1)))
1908 if (NILP (AREF (slot
, 2)))
1910 val
= resolve_symbol_ccl_program (AREF (slot
, 1));
1911 if (! VECTORP (val
))
1913 ASET (slot
, 1, val
);
1916 return AREF (slot
, 1);
1919 /* Setup fields of the structure pointed by CCL appropriately for the
1920 execution of CCL program CCL_PROG. CCL_PROG is the name (symbol)
1921 of the CCL program or the already compiled code (vector).
1922 Return 0 if we succeed this setup, else return -1.
1924 If CCL_PROG is nil, we just reset the structure pointed by CCL. */
1926 setup_ccl_program (struct ccl_program
*ccl
, Lisp_Object ccl_prog
)
1930 if (! NILP (ccl_prog
))
1932 struct Lisp_Vector
*vp
;
1934 ccl_prog
= ccl_get_compiled_code (ccl_prog
, &ccl
->idx
);
1935 if (! VECTORP (ccl_prog
))
1937 vp
= XVECTOR (ccl_prog
);
1938 ccl
->size
= vp
->header
.size
;
1939 ccl
->prog
= vp
->contents
;
1940 ccl
->eof_ic
= XINT (vp
->contents
[CCL_HEADER_EOF
]);
1941 ccl
->buf_magnification
= XINT (vp
->contents
[CCL_HEADER_BUF_MAG
]);
1946 slot
= AREF (Vccl_program_table
, ccl
->idx
);
1947 ASET (slot
, 3, Qnil
);
1950 ccl
->ic
= CCL_HEADER_MAIN
;
1951 for (i
= 0; i
< 8; i
++)
1953 ccl
->last_block
= 0;
1954 ccl
->private_state
= 0;
1957 ccl
->suppress_error
= 0;
1958 ccl
->eight_bit_control
= 0;
1959 ccl
->quit_silently
= 0;
1964 DEFUN ("ccl-program-p", Fccl_program_p
, Sccl_program_p
, 1, 1, 0,
1965 doc
: /* Return t if OBJECT is a CCL program name or a compiled CCL program code.
1966 See the documentation of `define-ccl-program' for the detail of CCL program. */)
1967 (Lisp_Object object
)
1971 if (VECTORP (object
))
1973 val
= resolve_symbol_ccl_program (object
);
1974 return (VECTORP (val
) ? Qt
: Qnil
);
1976 if (!SYMBOLP (object
))
1979 val
= Fget (object
, Qccl_program_idx
);
1980 return ((! NATNUMP (val
)
1981 || XINT (val
) >= ASIZE (Vccl_program_table
))
1985 DEFUN ("ccl-execute", Fccl_execute
, Sccl_execute
, 2, 2, 0,
1986 doc
: /* Execute CCL-PROGRAM with registers initialized by REGISTERS.
1988 CCL-PROGRAM is a CCL program name (symbol)
1989 or compiled code generated by `ccl-compile' (for backward compatibility.
1990 In the latter case, the execution overhead is bigger than in the former).
1991 No I/O commands should appear in CCL-PROGRAM.
1993 REGISTERS is a vector of [R0 R1 ... R7] where RN is an initial value
1994 for the Nth register.
1996 As side effect, each element of REGISTERS holds the value of
1997 the corresponding register after the execution.
1999 See the documentation of `define-ccl-program' for a definition of CCL
2001 (Lisp_Object ccl_prog
, Lisp_Object reg
)
2003 struct ccl_program ccl
;
2006 if (setup_ccl_program (&ccl
, ccl_prog
) < 0)
2007 error ("Invalid CCL program");
2010 if (ASIZE (reg
) != 8)
2011 error ("Length of vector REGISTERS is not 8");
2013 for (i
= 0; i
< 8; i
++)
2014 ccl
.reg
[i
] = (INTEGERP (AREF (reg
, i
))
2015 ? XINT (AREF (reg
, i
))
2018 ccl_driver (&ccl
, NULL
, NULL
, 0, 0, Qnil
);
2020 if (ccl
.status
!= CCL_STAT_SUCCESS
)
2021 error ("Error in CCL program at %dth code", ccl
.ic
);
2023 for (i
= 0; i
< 8; i
++)
2024 ASET (reg
, i
, make_number (ccl
.reg
[i
]));
2028 DEFUN ("ccl-execute-on-string", Fccl_execute_on_string
, Sccl_execute_on_string
,
2030 doc
: /* Execute CCL-PROGRAM with initial STATUS on STRING.
2032 CCL-PROGRAM is a symbol registered by `register-ccl-program',
2033 or a compiled code generated by `ccl-compile' (for backward compatibility,
2034 in this case, the execution is slower).
2036 Read buffer is set to STRING, and write buffer is allocated automatically.
2038 STATUS is a vector of [R0 R1 ... R7 IC], where
2039 R0..R7 are initial values of corresponding registers,
2040 IC is the instruction counter specifying from where to start the program.
2041 If R0..R7 are nil, they are initialized to 0.
2042 If IC is nil, it is initialized to head of the CCL program.
2044 If optional 4th arg CONTINUE is non-nil, keep IC on read operation
2045 when read buffer is exhausted, else, IC is always set to the end of
2046 CCL-PROGRAM on exit.
2048 It returns the contents of write buffer as a string,
2049 and as side effect, STATUS is updated.
2050 If the optional 5th arg UNIBYTE-P is non-nil, the returned string
2051 is a unibyte string. By default it is a multibyte string.
2053 See the documentation of `define-ccl-program' for the detail of CCL program.
2054 usage: (ccl-execute-on-string CCL-PROGRAM STATUS STRING &optional CONTINUE UNIBYTE-P) */)
2055 (Lisp_Object ccl_prog
, Lisp_Object status
, Lisp_Object str
, Lisp_Object contin
, Lisp_Object unibyte_p
)
2058 struct ccl_program ccl
;
2060 EMACS_INT outbufsize
;
2061 unsigned char *outbuf
, *outp
;
2062 EMACS_INT str_chars
, str_bytes
;
2063 #define CCL_EXECUTE_BUF_SIZE 1024
2064 int source
[CCL_EXECUTE_BUF_SIZE
], destination
[CCL_EXECUTE_BUF_SIZE
];
2065 EMACS_INT consumed_chars
, consumed_bytes
, produced_chars
;
2067 if (setup_ccl_program (&ccl
, ccl_prog
) < 0)
2068 error ("Invalid CCL program");
2070 CHECK_VECTOR (status
);
2071 if (ASIZE (status
) != 9)
2072 error ("Length of vector STATUS is not 9");
2075 str_chars
= SCHARS (str
);
2076 str_bytes
= SBYTES (str
);
2078 for (i
= 0; i
< 8; i
++)
2080 if (NILP (AREF (status
, i
)))
2081 ASET (status
, i
, make_number (0));
2082 if (INTEGERP (AREF (status
, i
)))
2083 ccl
.reg
[i
] = XINT (AREF (status
, i
));
2085 if (INTEGERP (AREF (status
, i
)))
2087 i
= XFASTINT (AREF (status
, 8));
2088 if (ccl
.ic
< i
&& i
< ccl
.size
)
2092 outbufsize
= (ccl
.buf_magnification
2093 ? str_bytes
* ccl
.buf_magnification
+ 256
2095 outp
= outbuf
= (unsigned char *) xmalloc (outbufsize
);
2097 consumed_chars
= consumed_bytes
= 0;
2101 const unsigned char *p
= SDATA (str
) + consumed_bytes
;
2102 const unsigned char *endp
= SDATA (str
) + str_bytes
;
2106 if (endp
- p
== str_chars
- consumed_chars
)
2107 while (j
< CCL_EXECUTE_BUF_SIZE
&& p
< endp
)
2110 while (j
< CCL_EXECUTE_BUF_SIZE
&& p
< endp
)
2111 source
[j
++] = STRING_CHAR_ADVANCE (p
);
2112 consumed_chars
+= j
;
2113 consumed_bytes
= p
- SDATA (str
);
2115 if (consumed_bytes
== str_bytes
)
2116 ccl
.last_block
= NILP (contin
);
2121 ccl_driver (&ccl
, src
, destination
, src_size
, CCL_EXECUTE_BUF_SIZE
,
2123 produced_chars
+= ccl
.produced
;
2124 if (NILP (unibyte_p
))
2126 if (outp
- outbuf
+ MAX_MULTIBYTE_LENGTH
* ccl
.produced
2129 EMACS_INT offset
= outp
- outbuf
;
2130 outbufsize
+= MAX_MULTIBYTE_LENGTH
* ccl
.produced
;
2131 outbuf
= (unsigned char *) xrealloc (outbuf
, outbufsize
);
2132 outp
= outbuf
+ offset
;
2134 for (j
= 0; j
< ccl
.produced
; j
++)
2135 CHAR_STRING_ADVANCE (destination
[j
], outp
);
2139 if (outp
- outbuf
+ ccl
.produced
> outbufsize
)
2141 EMACS_INT offset
= outp
- outbuf
;
2142 outbufsize
+= ccl
.produced
;
2143 outbuf
= (unsigned char *) xrealloc (outbuf
, outbufsize
);
2144 outp
= outbuf
+ offset
;
2146 for (j
= 0; j
< ccl
.produced
; j
++)
2147 *outp
++ = destination
[j
];
2149 src
+= ccl
.consumed
;
2150 src_size
-= ccl
.consumed
;
2151 if (ccl
.status
!= CCL_STAT_SUSPEND_BY_DST
)
2155 if (ccl
.status
!= CCL_STAT_SUSPEND_BY_SRC
2156 || str_chars
== consumed_chars
)
2160 if (ccl
.status
== CCL_STAT_INVALID_CMD
)
2161 error ("Error in CCL program at %dth code", ccl
.ic
);
2162 if (ccl
.status
== CCL_STAT_QUIT
)
2163 error ("CCL program interrupted at %dth code", ccl
.ic
);
2165 for (i
= 0; i
< 8; i
++)
2166 ASET (status
, i
, make_number (ccl
.reg
[i
]));
2167 ASET (status
, 8, make_number (ccl
.ic
));
2169 if (NILP (unibyte_p
))
2170 val
= make_multibyte_string ((char *) outbuf
, produced_chars
,
2173 val
= make_unibyte_string ((char *) outbuf
, produced_chars
);
2179 DEFUN ("register-ccl-program", Fregister_ccl_program
, Sregister_ccl_program
,
2181 doc
: /* Register CCL program CCL-PROG as NAME in `ccl-program-table'.
2182 CCL-PROG should be a compiled CCL program (vector), or nil.
2183 If it is nil, just reserve NAME as a CCL program name.
2184 Return index number of the registered CCL program. */)
2185 (Lisp_Object name
, Lisp_Object ccl_prog
)
2187 int len
= ASIZE (Vccl_program_table
);
2189 Lisp_Object resolved
;
2191 CHECK_SYMBOL (name
);
2193 if (!NILP (ccl_prog
))
2195 CHECK_VECTOR (ccl_prog
);
2196 resolved
= resolve_symbol_ccl_program (ccl_prog
);
2197 if (NILP (resolved
))
2198 error ("Error in CCL program");
2199 if (VECTORP (resolved
))
2201 ccl_prog
= resolved
;
2208 for (idx
= 0; idx
< len
; idx
++)
2212 slot
= AREF (Vccl_program_table
, idx
);
2213 if (!VECTORP (slot
))
2214 /* This is the first unused slot. Register NAME here. */
2217 if (EQ (name
, AREF (slot
, 0)))
2219 /* Update this slot. */
2220 ASET (slot
, 1, ccl_prog
);
2221 ASET (slot
, 2, resolved
);
2223 return make_number (idx
);
2228 /* Extend the table. */
2229 Vccl_program_table
= larger_vector (Vccl_program_table
, len
* 2, Qnil
);
2234 elt
= Fmake_vector (make_number (4), Qnil
);
2235 ASET (elt
, 0, name
);
2236 ASET (elt
, 1, ccl_prog
);
2237 ASET (elt
, 2, resolved
);
2239 ASET (Vccl_program_table
, idx
, elt
);
2242 Fput (name
, Qccl_program_idx
, make_number (idx
));
2243 return make_number (idx
);
2246 /* Register code conversion map.
2247 A code conversion map consists of numbers, Qt, Qnil, and Qlambda.
2248 The first element is the start code point.
2249 The other elements are mapped numbers.
2250 Symbol t means to map to an original number before mapping.
2251 Symbol nil means that the corresponding element is empty.
2252 Symbol lambda means to terminate mapping here.
2255 DEFUN ("register-code-conversion-map", Fregister_code_conversion_map
,
2256 Sregister_code_conversion_map
,
2258 doc
: /* Register SYMBOL as code conversion map MAP.
2259 Return index number of the registered map. */)
2260 (Lisp_Object symbol
, Lisp_Object map
)
2262 int len
= ASIZE (Vcode_conversion_map_vector
);
2266 CHECK_SYMBOL (symbol
);
2269 for (i
= 0; i
< len
; i
++)
2271 Lisp_Object slot
= AREF (Vcode_conversion_map_vector
, i
);
2276 if (EQ (symbol
, XCAR (slot
)))
2278 idx
= make_number (i
);
2279 XSETCDR (slot
, map
);
2280 Fput (symbol
, Qcode_conversion_map
, map
);
2281 Fput (symbol
, Qcode_conversion_map_id
, idx
);
2287 Vcode_conversion_map_vector
= larger_vector (Vcode_conversion_map_vector
,
2290 idx
= make_number (i
);
2291 Fput (symbol
, Qcode_conversion_map
, map
);
2292 Fput (symbol
, Qcode_conversion_map_id
, idx
);
2293 ASET (Vcode_conversion_map_vector
, i
, Fcons (symbol
, map
));
2301 staticpro (&Vccl_program_table
);
2302 Vccl_program_table
= Fmake_vector (make_number (32), Qnil
);
2304 Qccl
= intern_c_string ("ccl");
2307 Qcclp
= intern_c_string ("cclp");
2310 Qccl_program
= intern_c_string ("ccl-program");
2311 staticpro (&Qccl_program
);
2313 Qccl_program_idx
= intern_c_string ("ccl-program-idx");
2314 staticpro (&Qccl_program_idx
);
2316 Qcode_conversion_map
= intern_c_string ("code-conversion-map");
2317 staticpro (&Qcode_conversion_map
);
2319 Qcode_conversion_map_id
= intern_c_string ("code-conversion-map-id");
2320 staticpro (&Qcode_conversion_map_id
);
2322 DEFVAR_LISP ("code-conversion-map-vector", Vcode_conversion_map_vector
,
2323 doc
: /* Vector of code conversion maps. */);
2324 Vcode_conversion_map_vector
= Fmake_vector (make_number (16), Qnil
);
2326 DEFVAR_LISP ("font-ccl-encoder-alist", Vfont_ccl_encoder_alist
,
2327 doc
: /* Alist of fontname patterns vs corresponding CCL program.
2328 Each element looks like (REGEXP . CCL-CODE),
2329 where CCL-CODE is a compiled CCL program.
2330 When a font whose name matches REGEXP is used for displaying a character,
2331 CCL-CODE is executed to calculate the code point in the font
2332 from the charset number and position code(s) of the character which are set
2333 in CCL registers R0, R1, and R2 before the execution.
2334 The code point in the font is set in CCL registers R1 and R2
2335 when the execution terminated.
2336 If the font is single-byte font, the register R2 is not used. */);
2337 Vfont_ccl_encoder_alist
= Qnil
;
2339 DEFVAR_LISP ("translation-hash-table-vector", Vtranslation_hash_table_vector
,
2340 doc
: /* Vector containing all translation hash tables ever defined.
2341 Comprises pairs (SYMBOL . TABLE) where SYMBOL and TABLE were set up by calls
2342 to `define-translation-hash-table'. The vector is indexed by the table id
2344 Vtranslation_hash_table_vector
= Qnil
;
2346 defsubr (&Sccl_program_p
);
2347 defsubr (&Sccl_execute
);
2348 defsubr (&Sccl_execute_on_string
);
2349 defsubr (&Sregister_ccl_program
);
2350 defsubr (&Sregister_code_conversion_map
);