1 /* CCL (Code Conversion Language) interpreter.
2 Copyright (C) 1995, 1997 Electrotechnical Laboratory, JAPAN.
3 Copyright (C) 2001, 2002 Free Software Foundation, Inc.
4 Licensed to the Free Software Foundation.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
40 #endif /* not emacs */
42 /* This contains all code conversion map available to CCL. */
43 Lisp_Object Vcode_conversion_map_vector
;
45 /* Alist of fontname patterns vs corresponding CCL program. */
46 Lisp_Object Vfont_ccl_encoder_alist
;
48 /* This symbol is a property which assocates with ccl program vector.
49 Ex: (get 'ccl-big5-encoder 'ccl-program) returns ccl program vector. */
50 Lisp_Object Qccl_program
;
52 /* These symbols are properties which associate with code conversion
53 map and their ID respectively. */
54 Lisp_Object Qcode_conversion_map
;
55 Lisp_Object Qcode_conversion_map_id
;
57 /* Symbols of ccl program have this property, a value of the property
58 is an index for Vccl_protram_table. */
59 Lisp_Object Qccl_program_idx
;
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. */
66 Lisp_Object Vccl_program_table
;
68 /* Vector of registered hash tables for translation. */
69 Lisp_Object Vtranslation_hash_table_vector
;
71 /* Return a hash table of id number ID. */
72 #define GET_HASH_TABLE(id) \
73 (XHASH_TABLE (XCDR(XVECTOR(Vtranslation_hash_table_vector)->contents[(id)])))
74 /* Copied from fns.c. */
75 #define HASH_VALUE(H, IDX) AREF ((H)->key_and_value, 2 * (IDX) + 1)
77 /* CCL (Code Conversion Language) is a simple language which has
78 operations on one input buffer, one output buffer, and 7 registers.
79 The syntax of CCL is described in `ccl.el'. Emacs Lisp function
80 `ccl-compile' compiles a CCL program and produces a CCL code which
81 is a vector of integers. The structure of this vector is as
82 follows: The 1st element: buffer-magnification, a factor for the
83 size of output buffer compared with the size of input buffer. The
84 2nd element: address of CCL code to be executed when encountered
85 with end of input stream. The 3rd and the remaining elements: CCL
88 /* Header of CCL compiled code */
89 #define CCL_HEADER_BUF_MAG 0
90 #define CCL_HEADER_EOF 1
91 #define CCL_HEADER_MAIN 2
93 /* CCL code is a sequence of 28-bit non-negative integers (i.e. the
94 MSB is always 0), each contains CCL command and/or arguments in the
97 |----------------- integer (28-bit) ------------------|
98 |------- 17-bit ------|- 3-bit --|- 3-bit --|- 5-bit -|
99 |--constant argument--|-register-|-register-|-command-|
100 ccccccccccccccccc RRR rrr XXXXX
102 |------- relative address -------|-register-|-command-|
103 cccccccccccccccccccc rrr XXXXX
105 |------------- constant or other args ----------------|
106 cccccccccccccccccccccccccccc
108 where, `cc...c' is a non-negative integer indicating constant value
109 (the left most `c' is always 0) or an absolute jump address, `RRR'
110 and `rrr' are CCL register number, `XXXXX' is one of the following
115 Each comment fields shows one or more lines for command syntax and
116 the following lines for semantics of the command. In semantics, IC
117 stands for Instruction Counter. */
119 #define CCL_SetRegister 0x00 /* Set register a register value:
120 1:00000000000000000RRRrrrXXXXX
121 ------------------------------
125 #define CCL_SetShortConst 0x01 /* Set register a short constant value:
126 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
127 ------------------------------
128 reg[rrr] = CCCCCCCCCCCCCCCCCCC;
131 #define CCL_SetConst 0x02 /* Set register a constant value:
132 1:00000000000000000000rrrXXXXX
134 ------------------------------
139 #define CCL_SetArray 0x03 /* Set register an element of array:
140 1:CCCCCCCCCCCCCCCCCRRRrrrXXXXX
144 ------------------------------
145 if (0 <= reg[RRR] < CC..C)
146 reg[rrr] = ELEMENT[reg[RRR]];
150 #define CCL_Jump 0x04 /* Jump:
151 1:A--D--D--R--E--S--S-000XXXXX
152 ------------------------------
156 /* Note: If CC..C is greater than 0, the second code is omitted. */
158 #define CCL_JumpCond 0x05 /* Jump conditional:
159 1:A--D--D--R--E--S--S-rrrXXXXX
160 ------------------------------
166 #define CCL_WriteRegisterJump 0x06 /* Write register and jump:
167 1:A--D--D--R--E--S--S-rrrXXXXX
168 ------------------------------
173 #define CCL_WriteRegisterReadJump 0x07 /* Write register, read, and jump:
174 1:A--D--D--R--E--S--S-rrrXXXXX
175 2:A--D--D--R--E--S--S-rrrYYYYY
176 -----------------------------
182 /* Note: If read is suspended, the resumed execution starts from the
183 second code (YYYYY == CCL_ReadJump). */
185 #define CCL_WriteConstJump 0x08 /* Write constant and jump:
186 1:A--D--D--R--E--S--S-000XXXXX
188 ------------------------------
193 #define CCL_WriteConstReadJump 0x09 /* Write constant, read, and jump:
194 1:A--D--D--R--E--S--S-rrrXXXXX
196 3:A--D--D--R--E--S--S-rrrYYYYY
197 -----------------------------
203 /* Note: If read is suspended, the resumed execution starts from the
204 second code (YYYYY == CCL_ReadJump). */
206 #define CCL_WriteStringJump 0x0A /* Write string and jump:
207 1:A--D--D--R--E--S--S-000XXXXX
209 3:0000STRIN[0]STRIN[1]STRIN[2]
211 ------------------------------
212 write_string (STRING, LENGTH);
216 #define CCL_WriteArrayReadJump 0x0B /* Write an array element, read, and jump:
217 1:A--D--D--R--E--S--S-rrrXXXXX
222 N:A--D--D--R--E--S--S-rrrYYYYY
223 ------------------------------
224 if (0 <= reg[rrr] < LENGTH)
225 write (ELEMENT[reg[rrr]]);
226 IC += LENGTH + 2; (... pointing at N+1)
230 /* Note: If read is suspended, the resumed execution starts from the
231 Nth code (YYYYY == CCL_ReadJump). */
233 #define CCL_ReadJump 0x0C /* Read and jump:
234 1:A--D--D--R--E--S--S-rrrYYYYY
235 -----------------------------
240 #define CCL_Branch 0x0D /* Jump by branch table:
241 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
242 2:A--D--D--R--E-S-S[0]000XXXXX
243 3:A--D--D--R--E-S-S[1]000XXXXX
245 ------------------------------
246 if (0 <= reg[rrr] < CC..C)
247 IC += ADDRESS[reg[rrr]];
249 IC += ADDRESS[CC..C];
252 #define CCL_ReadRegister 0x0E /* Read bytes into registers:
253 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
254 2:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
256 ------------------------------
261 #define CCL_WriteExprConst 0x0F /* write result of expression:
262 1:00000OPERATION000RRR000XXXXX
264 ------------------------------
265 write (reg[RRR] OPERATION CONSTANT);
269 /* Note: If the Nth read is suspended, the resumed execution starts
270 from the Nth code. */
272 #define CCL_ReadBranch 0x10 /* Read one byte into a register,
273 and jump by branch table:
274 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
275 2:A--D--D--R--E-S-S[0]000XXXXX
276 3:A--D--D--R--E-S-S[1]000XXXXX
278 ------------------------------
280 if (0 <= reg[rrr] < CC..C)
281 IC += ADDRESS[reg[rrr]];
283 IC += ADDRESS[CC..C];
286 #define CCL_WriteRegister 0x11 /* Write registers:
287 1:CCCCCCCCCCCCCCCCCCCrrrXXXXX
288 2:CCCCCCCCCCCCCCCCCCCrrrXXXXX
290 ------------------------------
296 /* Note: If the Nth write is suspended, the resumed execution
297 starts from the Nth code. */
299 #define CCL_WriteExprRegister 0x12 /* Write result of expression
300 1:00000OPERATIONRrrRRR000XXXXX
301 ------------------------------
302 write (reg[RRR] OPERATION reg[Rrr]);
305 #define CCL_Call 0x13 /* Call the CCL program whose ID is
307 1:CCCCCCCCCCCCCCCCCCCCFFFXXXXX
308 [2:00000000cccccccccccccccccccc]
309 ------------------------------
317 #define CCL_WriteConstString 0x14 /* Write a constant or a string:
318 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
319 [2:0000STRIN[0]STRIN[1]STRIN[2]]
321 -----------------------------
325 write_string (STRING, CC..C);
326 IC += (CC..C + 2) / 3;
329 #define CCL_WriteArray 0x15 /* Write an element of array:
330 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
334 ------------------------------
335 if (0 <= reg[rrr] < CC..C)
336 write (ELEMENT[reg[rrr]]);
340 #define CCL_End 0x16 /* Terminate:
341 1:00000000000000000000000XXXXX
342 ------------------------------
346 /* The following two codes execute an assignment arithmetic/logical
347 operation. The form of the operation is like REG OP= OPERAND. */
349 #define CCL_ExprSelfConst 0x17 /* REG OP= constant:
350 1:00000OPERATION000000rrrXXXXX
352 ------------------------------
353 reg[rrr] OPERATION= CONSTANT;
356 #define CCL_ExprSelfReg 0x18 /* REG1 OP= REG2:
357 1:00000OPERATION000RRRrrrXXXXX
358 ------------------------------
359 reg[rrr] OPERATION= reg[RRR];
362 /* The following codes execute an arithmetic/logical operation. The
363 form of the operation is like REG_X = REG_Y OP OPERAND2. */
365 #define CCL_SetExprConst 0x19 /* REG_X = REG_Y OP constant:
366 1:00000OPERATION000RRRrrrXXXXX
368 ------------------------------
369 reg[rrr] = reg[RRR] OPERATION CONSTANT;
373 #define CCL_SetExprReg 0x1A /* REG1 = REG2 OP REG3:
374 1:00000OPERATIONRrrRRRrrrXXXXX
375 ------------------------------
376 reg[rrr] = reg[RRR] OPERATION reg[Rrr];
379 #define CCL_JumpCondExprConst 0x1B /* Jump conditional according to
380 an operation on constant:
381 1:A--D--D--R--E--S--S-rrrXXXXX
384 -----------------------------
385 reg[7] = reg[rrr] OPERATION CONSTANT;
392 #define CCL_JumpCondExprReg 0x1C /* Jump conditional according to
393 an operation on register:
394 1:A--D--D--R--E--S--S-rrrXXXXX
397 -----------------------------
398 reg[7] = reg[rrr] OPERATION reg[RRR];
405 #define CCL_ReadJumpCondExprConst 0x1D /* Read and jump conditional according
406 to an operation on constant:
407 1:A--D--D--R--E--S--S-rrrXXXXX
410 -----------------------------
412 reg[7] = reg[rrr] OPERATION CONSTANT;
419 #define CCL_ReadJumpCondExprReg 0x1E /* Read and jump conditional according
420 to an operation on register:
421 1:A--D--D--R--E--S--S-rrrXXXXX
424 -----------------------------
426 reg[7] = reg[rrr] OPERATION reg[RRR];
433 #define CCL_Extension 0x1F /* Extended CCL code
434 1:ExtendedCOMMNDRrrRRRrrrXXXXX
437 ------------------------------
438 extended_command (rrr,RRR,Rrr,ARGS)
442 Here after, Extended CCL Instructions.
443 Bit length of extended command is 14.
444 Therefore, the instruction code range is 0..16384(0x3fff).
447 /* Read a multibyte characeter.
448 A code point is stored into reg[rrr]. A charset ID is stored into
451 #define CCL_ReadMultibyteChar2 0x00 /* Read Multibyte Character
452 1:ExtendedCOMMNDRrrRRRrrrXXXXX */
454 /* Write a multibyte character.
455 Write a character whose code point is reg[rrr] and the charset ID
458 #define CCL_WriteMultibyteChar2 0x01 /* Write Multibyte Character
459 1:ExtendedCOMMNDRrrRRRrrrXXXXX */
461 /* Translate a character whose code point is reg[rrr] and the charset
462 ID is reg[RRR] by a translation table whose ID is reg[Rrr].
464 A translated character is set in reg[rrr] (code point) and reg[RRR]
467 #define CCL_TranslateCharacter 0x02 /* Translate a multibyte character
468 1:ExtendedCOMMNDRrrRRRrrrXXXXX */
470 /* Translate a character whose code point is reg[rrr] and the charset
471 ID is reg[RRR] by a translation table whose ID is ARGUMENT.
473 A translated character is set in reg[rrr] (code point) and reg[RRR]
476 #define CCL_TranslateCharacterConstTbl 0x03 /* Translate a multibyte character
477 1:ExtendedCOMMNDRrrRRRrrrXXXXX
478 2:ARGUMENT(Translation Table ID)
481 /* Iterate looking up MAPs for reg[rrr] starting from the Nth (N =
482 reg[RRR]) MAP until some value is found.
484 Each MAP is a Lisp vector whose element is number, nil, t, or
486 If the element is nil, ignore the map and proceed to the next map.
487 If the element is t or lambda, finish without changing reg[rrr].
488 If the element is a number, set reg[rrr] to the number and finish.
490 Detail of the map structure is descibed in the comment for
491 CCL_MapMultiple below. */
493 #define CCL_IterateMultipleMap 0x10 /* Iterate multiple maps
494 1:ExtendedCOMMNDXXXRRRrrrXXXXX
501 /* Map the code in reg[rrr] by MAPs starting from the Nth (N =
504 MAPs are supplied in the succeeding CCL codes as follows:
506 When CCL program gives this nested structure of map to this command:
509 (MAP-ID121 MAP-ID122 MAP-ID123)
512 (MAP-ID211 (MAP-ID2111) MAP-ID212)
514 the compiled CCL codes has this sequence:
515 CCL_MapMultiple (CCL code of this command)
516 16 (total number of MAPs and SEPARATORs)
534 A value of each SEPARATOR follows this rule:
535 MAP-SET := SEPARATOR [(MAP-ID | MAP-SET)]+
536 SEPARATOR := -(number of MAP-IDs and SEPARATORs in the MAP-SET)
538 (*)....Nest level of MAP-SET must not be over than MAX_MAP_SET_LEVEL.
540 When some map fails to map (i.e. it doesn't have a value for
541 reg[rrr]), the mapping is treated as identity.
543 The mapping is iterated for all maps in each map set (set of maps
544 separated by SEPARATOR) except in the case that lambda is
545 encountered. More precisely, the mapping proceeds as below:
547 At first, VAL0 is set to reg[rrr], and it is translated by the
548 first map to VAL1. Then, VAL1 is translated by the next map to
549 VAL2. This mapping is iterated until the last map is used. The
550 result of the mapping is the last value of VAL?. When the mapping
551 process reached to the end of the map set, it moves to the next
552 map set. If the next does not exit, the mapping process terminates,
553 and regard the last value as a result.
555 But, when VALm is mapped to VALn and VALn is not a number, the
556 mapping proceed as below:
558 If VALn is nil, the lastest map is ignored and the mapping of VALm
559 proceed to the next map.
561 In VALn is t, VALm is reverted to reg[rrr] and the mapping of VALm
562 proceed to the next map.
564 If VALn is lambda, move to the next map set like reaching to the
565 end of the current map set.
567 If VALn is a symbol, call the CCL program refered by it.
568 Then, use reg[rrr] as a mapped value except for -1, -2 and -3.
569 Such special values are regarded as nil, t, and lambda respectively.
571 Each map is a Lisp vector of the following format (a) or (b):
572 (a)......[STARTPOINT VAL1 VAL2 ...]
573 (b)......[t VAL STARTPOINT ENDPOINT],
575 STARTPOINT is an offset to be used for indexing a map,
576 ENDPOINT is a maximum index number of a map,
577 VAL and VALn is a number, nil, t, or lambda.
579 Valid index range of a map of type (a) is:
580 STARTPOINT <= index < STARTPOINT + map_size - 1
581 Valid index range of a map of type (b) is:
582 STARTPOINT <= index < ENDPOINT */
584 #define CCL_MapMultiple 0x11 /* Mapping by multiple code conversion maps
585 1:ExtendedCOMMNDXXXRRRrrrXXXXX
597 #define MAX_MAP_SET_LEVEL 30
605 static tr_stack mapping_stack
[MAX_MAP_SET_LEVEL
];
606 static tr_stack
*mapping_stack_pointer
;
608 /* If this variable is non-zero, it indicates the stack_idx
609 of immediately called by CCL_MapMultiple. */
610 static int stack_idx_of_map_multiple
;
612 #define PUSH_MAPPING_STACK(restlen, orig) \
615 mapping_stack_pointer->rest_length = (restlen); \
616 mapping_stack_pointer->orig_val = (orig); \
617 mapping_stack_pointer++; \
621 #define POP_MAPPING_STACK(restlen, orig) \
624 mapping_stack_pointer--; \
625 (restlen) = mapping_stack_pointer->rest_length; \
626 (orig) = mapping_stack_pointer->orig_val; \
630 #define CCL_CALL_FOR_MAP_INSTRUCTION(symbol, ret_ic) \
633 struct ccl_program called_ccl; \
634 if (stack_idx >= 256 \
635 || (setup_ccl_program (&called_ccl, (symbol)) != 0)) \
639 ccl_prog = ccl_prog_stack_struct[0].ccl_prog; \
640 ic = ccl_prog_stack_struct[0].ic; \
644 ccl_prog_stack_struct[stack_idx].ccl_prog = ccl_prog; \
645 ccl_prog_stack_struct[stack_idx].ic = (ret_ic); \
647 ccl_prog = called_ccl.prog; \
648 ic = CCL_HEADER_MAIN; \
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 iff 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 iff 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. */
725 #define CCL_INVALID_CMD \
728 ccl->status = CCL_STAT_INVALID_CMD; \
729 goto ccl_error_handler; \
733 /* Encode one character CH to multibyte form and write to the current
734 output buffer. If CH is less than 256, CH is written as is. */
735 #define CCL_WRITE_CHAR(ch) \
737 int bytes = SINGLE_BYTE_CHAR_P (ch) ? 1: CHAR_BYTES (ch); \
740 else if (dst + bytes + extra_bytes < (dst_bytes ? dst_end : src)) \
745 if ((ch) >= 0x80 && (ch) < 0xA0) \
746 /* We may have to convert this eight-bit char to \
747 multibyte form later. */ \
750 else if (CHAR_VALID_P (ch, 0)) \
751 dst += CHAR_STRING (ch, dst); \
756 CCL_SUSPEND (CCL_STAT_SUSPEND_BY_DST); \
759 /* Encode one character CH to multibyte form and write to the current
760 output buffer. The output bytes always forms a valid multibyte
762 #define CCL_WRITE_MULTIBYTE_CHAR(ch) \
764 int bytes = CHAR_BYTES (ch); \
767 else if (dst + bytes + extra_bytes < (dst_bytes ? dst_end : src)) \
769 if (CHAR_VALID_P ((ch), 0)) \
770 dst += CHAR_STRING ((ch), dst); \
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) \
784 else if (dst + len <= (dst_bytes ? dst_end : src)) \
785 for (i = 0; i < len; i++) \
786 *dst++ = ((XFASTINT (ccl_prog[ic + (i / 3)])) \
787 >> ((2 - (i % 3)) * 8)) & 0xFF; \
789 CCL_SUSPEND (CCL_STAT_SUSPEND_BY_DST); \
792 /* Read one byte from the current input buffer into REGth register. */
793 #define CCL_READ_CHAR(REG) \
797 else if (src < src_end) \
801 && ccl->eol_type != CODING_EOL_LF) \
803 /* We are encoding. */ \
804 if (ccl->eol_type == CODING_EOL_CRLF) \
806 if (ccl->cr_consumed) \
807 ccl->cr_consumed = 0; \
810 ccl->cr_consumed = 1; \
818 if (REG == LEADING_CODE_8_BIT_CONTROL \
820 REG = *src++ - 0x20; \
822 else if (ccl->last_block) \
828 CCL_SUSPEND (CCL_STAT_SUSPEND_BY_SRC); \
832 /* Set C to the character code made from CHARSET and CODE. This is
833 like MAKE_CHAR but check the validity of CHARSET and CODE. If they
834 are not valid, set C to (CODE & 0xFF) because that is usually the
835 case that CCL_ReadMultibyteChar2 read an invalid code and it set
836 CODE to that invalid byte. */
838 #define CCL_MAKE_CHAR(charset, code, c) \
840 if (charset == CHARSET_ASCII) \
842 else if (CHARSET_DEFINED_P (charset) \
843 && (code & 0x7F) >= 32 \
844 && (code < 256 || ((code >> 7) & 0x7F) >= 32)) \
846 int c1 = code & 0x7F, c2 = 0; \
849 c2 = c1, c1 = (code >> 7) & 0x7F; \
850 c = MAKE_CHAR (charset, c1, c2); \
857 /* Execute CCL code on SRC_BYTES length text at SOURCE. The resulting
858 text goes to a place pointed by DESTINATION, the length of which
859 should not exceed DST_BYTES. The bytes actually processed is
860 returned as *CONSUMED. The return value is the length of the
861 resulting text. As a side effect, the contents of CCL registers
862 are updated. If SOURCE or DESTINATION is NULL, only operations on
863 registers are permitted. */
866 #define CCL_DEBUG_BACKTRACE_LEN 256
867 int ccl_backtrace_table
[CCL_BACKTRACE_TABLE
];
868 int ccl_backtrace_idx
;
871 struct ccl_prog_stack
873 Lisp_Object
*ccl_prog
; /* Pointer to an array of CCL code. */
874 int ic
; /* Instruction Counter. */
877 /* For the moment, we only support depth 256 of stack. */
878 static struct ccl_prog_stack ccl_prog_stack_struct
[256];
881 ccl_driver (ccl
, source
, destination
, src_bytes
, dst_bytes
, consumed
)
882 struct ccl_program
*ccl
;
883 unsigned char *source
, *destination
;
884 int src_bytes
, dst_bytes
;
887 register int *reg
= ccl
->reg
;
888 register int ic
= ccl
->ic
;
889 register int code
= 0, field1
, field2
;
890 register Lisp_Object
*ccl_prog
= ccl
->prog
;
891 unsigned char *src
= source
, *src_end
= src
+ src_bytes
;
892 unsigned char *dst
= destination
, *dst_end
= dst
+ dst_bytes
;
895 int stack_idx
= ccl
->stack_idx
;
896 /* Instruction counter of the current CCL code. */
898 /* CCL_WRITE_CHAR will produce 8-bit code of range 0x80..0x9F. But,
899 each of them will be converted to multibyte form of 2-byte
900 sequence. For that conversion, we remember how many more bytes
901 we must keep in DESTINATION in this variable. */
904 if (ic
>= ccl
->eof_ic
)
905 ic
= CCL_HEADER_MAIN
;
907 if (ccl
->buf_magnification
== 0) /* We can't produce any bytes. */
910 /* Set mapping stack pointer. */
911 mapping_stack_pointer
= mapping_stack
;
914 ccl_backtrace_idx
= 0;
921 ccl_backtrace_table
[ccl_backtrace_idx
++] = ic
;
922 if (ccl_backtrace_idx
>= CCL_DEBUG_BACKTRACE_LEN
)
923 ccl_backtrace_idx
= 0;
924 ccl_backtrace_table
[ccl_backtrace_idx
] = 0;
927 if (!NILP (Vquit_flag
) && NILP (Vinhibit_quit
))
929 /* We can't just signal Qquit, instead break the loop as if
930 the whole data is processed. Don't reset Vquit_flag, it
931 must be handled later at a safer place. */
933 src
= source
+ src_bytes
;
934 ccl
->status
= CCL_STAT_QUIT
;
939 code
= XINT (ccl_prog
[ic
]); ic
++;
941 field2
= (code
& 0xFF) >> 5;
944 #define RRR (field1 & 7)
945 #define Rrr ((field1 >> 3) & 7)
947 #define EXCMD (field1 >> 6)
951 case CCL_SetRegister
: /* 00000000000000000RRRrrrXXXXX */
955 case CCL_SetShortConst
: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
959 case CCL_SetConst
: /* 00000000000000000000rrrXXXXX */
960 reg
[rrr
] = XINT (ccl_prog
[ic
]);
964 case CCL_SetArray
: /* CCCCCCCCCCCCCCCCCCCCRRRrrrXXXXX */
967 if ((unsigned int) i
< j
)
968 reg
[rrr
] = XINT (ccl_prog
[ic
+ i
]);
972 case CCL_Jump
: /* A--D--D--R--E--S--S-000XXXXX */
976 case CCL_JumpCond
: /* A--D--D--R--E--S--S-rrrXXXXX */
981 case CCL_WriteRegisterJump
: /* A--D--D--R--E--S--S-rrrXXXXX */
987 case CCL_WriteRegisterReadJump
: /* A--D--D--R--E--S--S-rrrXXXXX */
991 CCL_READ_CHAR (reg
[rrr
]);
995 case CCL_WriteConstJump
: /* A--D--D--R--E--S--S-000XXXXX */
996 i
= XINT (ccl_prog
[ic
]);
1001 case CCL_WriteConstReadJump
: /* A--D--D--R--E--S--S-rrrXXXXX */
1002 i
= XINT (ccl_prog
[ic
]);
1005 CCL_READ_CHAR (reg
[rrr
]);
1009 case CCL_WriteStringJump
: /* A--D--D--R--E--S--S-000XXXXX */
1010 j
= XINT (ccl_prog
[ic
]);
1012 CCL_WRITE_STRING (j
);
1016 case CCL_WriteArrayReadJump
: /* A--D--D--R--E--S--S-rrrXXXXX */
1018 j
= XINT (ccl_prog
[ic
]);
1019 if ((unsigned int) i
< j
)
1021 i
= XINT (ccl_prog
[ic
+ 1 + i
]);
1025 CCL_READ_CHAR (reg
[rrr
]);
1026 ic
+= ADDR
- (j
+ 2);
1029 case CCL_ReadJump
: /* A--D--D--R--E--S--S-rrrYYYYY */
1030 CCL_READ_CHAR (reg
[rrr
]);
1034 case CCL_ReadBranch
: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
1035 CCL_READ_CHAR (reg
[rrr
]);
1036 /* fall through ... */
1037 case CCL_Branch
: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
1038 if ((unsigned int) reg
[rrr
] < field1
)
1039 ic
+= XINT (ccl_prog
[ic
+ reg
[rrr
]]);
1041 ic
+= XINT (ccl_prog
[ic
+ field1
]);
1044 case CCL_ReadRegister
: /* CCCCCCCCCCCCCCCCCCCCrrXXXXX */
1047 CCL_READ_CHAR (reg
[rrr
]);
1049 code
= XINT (ccl_prog
[ic
]); ic
++;
1051 field2
= (code
& 0xFF) >> 5;
1055 case CCL_WriteExprConst
: /* 1:00000OPERATION000RRR000XXXXX */
1058 j
= XINT (ccl_prog
[ic
]);
1060 jump_address
= ic
+ 1;
1063 case CCL_WriteRegister
: /* CCCCCCCCCCCCCCCCCCCrrrXXXXX */
1069 code
= XINT (ccl_prog
[ic
]); ic
++;
1071 field2
= (code
& 0xFF) >> 5;
1075 case CCL_WriteExprRegister
: /* 1:00000OPERATIONRrrRRR000XXXXX */
1083 case CCL_Call
: /* 1:CCCCCCCCCCCCCCCCCCCCFFFXXXXX */
1088 /* If FFF is nonzero, the CCL program ID is in the
1092 prog_id
= XINT (ccl_prog
[ic
]);
1098 if (stack_idx
>= 256
1100 || prog_id
>= ASIZE (Vccl_program_table
)
1101 || (slot
= AREF (Vccl_program_table
, prog_id
), !VECTORP (slot
))
1102 || !VECTORP (AREF (slot
, 1)))
1106 ccl_prog
= ccl_prog_stack_struct
[0].ccl_prog
;
1107 ic
= ccl_prog_stack_struct
[0].ic
;
1112 ccl_prog_stack_struct
[stack_idx
].ccl_prog
= ccl_prog
;
1113 ccl_prog_stack_struct
[stack_idx
].ic
= ic
;
1115 ccl_prog
= XVECTOR (AREF (slot
, 1))->contents
;
1116 ic
= CCL_HEADER_MAIN
;
1120 case CCL_WriteConstString
: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
1122 CCL_WRITE_CHAR (field1
);
1125 CCL_WRITE_STRING (field1
);
1126 ic
+= (field1
+ 2) / 3;
1130 case CCL_WriteArray
: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
1132 if ((unsigned int) i
< field1
)
1134 j
= XINT (ccl_prog
[ic
+ i
]);
1140 case CCL_End
: /* 0000000000000000000000XXXXX */
1144 ccl_prog
= ccl_prog_stack_struct
[stack_idx
].ccl_prog
;
1145 ic
= ccl_prog_stack_struct
[stack_idx
].ic
;
1150 /* ccl->ic should points to this command code again to
1151 suppress further processing. */
1155 case CCL_ExprSelfConst
: /* 00000OPERATION000000rrrXXXXX */
1156 i
= XINT (ccl_prog
[ic
]);
1161 case CCL_ExprSelfReg
: /* 00000OPERATION000RRRrrrXXXXX */
1168 case CCL_PLUS
: reg
[rrr
] += i
; break;
1169 case CCL_MINUS
: reg
[rrr
] -= i
; break;
1170 case CCL_MUL
: reg
[rrr
] *= i
; break;
1171 case CCL_DIV
: reg
[rrr
] /= i
; break;
1172 case CCL_MOD
: reg
[rrr
] %= i
; break;
1173 case CCL_AND
: reg
[rrr
] &= i
; break;
1174 case CCL_OR
: reg
[rrr
] |= i
; break;
1175 case CCL_XOR
: reg
[rrr
] ^= i
; break;
1176 case CCL_LSH
: reg
[rrr
] <<= i
; break;
1177 case CCL_RSH
: reg
[rrr
] >>= i
; break;
1178 case CCL_LSH8
: reg
[rrr
] <<= 8; reg
[rrr
] |= i
; break;
1179 case CCL_RSH8
: reg
[7] = reg
[rrr
] & 0xFF; reg
[rrr
] >>= 8; break;
1180 case CCL_DIVMOD
: reg
[7] = reg
[rrr
] % i
; reg
[rrr
] /= i
; break;
1181 case CCL_LS
: reg
[rrr
] = reg
[rrr
] < i
; break;
1182 case CCL_GT
: reg
[rrr
] = reg
[rrr
] > i
; break;
1183 case CCL_EQ
: reg
[rrr
] = reg
[rrr
] == i
; break;
1184 case CCL_LE
: reg
[rrr
] = reg
[rrr
] <= i
; break;
1185 case CCL_GE
: reg
[rrr
] = reg
[rrr
] >= i
; break;
1186 case CCL_NE
: reg
[rrr
] = reg
[rrr
] != i
; break;
1187 default: CCL_INVALID_CMD
;
1191 case CCL_SetExprConst
: /* 00000OPERATION000RRRrrrXXXXX */
1193 j
= XINT (ccl_prog
[ic
]);
1195 jump_address
= ++ic
;
1198 case CCL_SetExprReg
: /* 00000OPERATIONRrrRRRrrrXXXXX */
1205 case CCL_ReadJumpCondExprConst
: /* A--D--D--R--E--S--S-rrrXXXXX */
1206 CCL_READ_CHAR (reg
[rrr
]);
1207 case CCL_JumpCondExprConst
: /* A--D--D--R--E--S--S-rrrXXXXX */
1209 op
= XINT (ccl_prog
[ic
]);
1210 jump_address
= ic
++ + ADDR
;
1211 j
= XINT (ccl_prog
[ic
]);
1216 case CCL_ReadJumpCondExprReg
: /* A--D--D--R--E--S--S-rrrXXXXX */
1217 CCL_READ_CHAR (reg
[rrr
]);
1218 case CCL_JumpCondExprReg
:
1220 op
= XINT (ccl_prog
[ic
]);
1221 jump_address
= ic
++ + ADDR
;
1222 j
= reg
[XINT (ccl_prog
[ic
])];
1229 case CCL_PLUS
: reg
[rrr
] = i
+ j
; break;
1230 case CCL_MINUS
: reg
[rrr
] = i
- j
; break;
1231 case CCL_MUL
: reg
[rrr
] = i
* j
; break;
1232 case CCL_DIV
: reg
[rrr
] = i
/ j
; break;
1233 case CCL_MOD
: reg
[rrr
] = i
% j
; break;
1234 case CCL_AND
: reg
[rrr
] = i
& j
; break;
1235 case CCL_OR
: reg
[rrr
] = i
| j
; break;
1236 case CCL_XOR
: reg
[rrr
] = i
^ j
;; break;
1237 case CCL_LSH
: reg
[rrr
] = i
<< j
; break;
1238 case CCL_RSH
: reg
[rrr
] = i
>> j
; break;
1239 case CCL_LSH8
: reg
[rrr
] = (i
<< 8) | j
; break;
1240 case CCL_RSH8
: reg
[rrr
] = i
>> 8; reg
[7] = i
& 0xFF; break;
1241 case CCL_DIVMOD
: reg
[rrr
] = i
/ j
; reg
[7] = i
% j
; break;
1242 case CCL_LS
: reg
[rrr
] = i
< j
; break;
1243 case CCL_GT
: reg
[rrr
] = i
> j
; break;
1244 case CCL_EQ
: reg
[rrr
] = i
== j
; break;
1245 case CCL_LE
: reg
[rrr
] = i
<= j
; break;
1246 case CCL_GE
: reg
[rrr
] = i
>= j
; break;
1247 case CCL_NE
: reg
[rrr
] = i
!= j
; break;
1248 case CCL_DECODE_SJIS
: DECODE_SJIS (i
, j
, reg
[rrr
], reg
[7]); break;
1249 case CCL_ENCODE_SJIS
: ENCODE_SJIS (i
, j
, reg
[rrr
], reg
[7]); break;
1250 default: CCL_INVALID_CMD
;
1253 if (code
== CCL_WriteExprConst
|| code
== CCL_WriteExprRegister
)
1266 case CCL_ReadMultibyteChar2
:
1273 goto ccl_read_multibyte_character_suspend
;
1276 if (!ccl
->multibyte
)
1279 if (!UNIBYTE_STR_AS_MULTIBYTE_P (src
, src_end
- src
, bytes
))
1281 reg
[RRR
] = CHARSET_8_BIT_CONTROL
;
1287 if (i
== '\n' && ccl
->eol_type
!= CODING_EOL_LF
)
1289 /* We are encoding. */
1290 if (ccl
->eol_type
== CODING_EOL_CRLF
)
1292 if (ccl
->cr_consumed
)
1293 ccl
->cr_consumed
= 0;
1296 ccl
->cr_consumed
= 1;
1304 reg
[RRR
] = CHARSET_ASCII
;
1310 reg
[RRR
] = CHARSET_ASCII
;
1312 else if (i
<= MAX_CHARSET_OFFICIAL_DIMENSION2
)
1314 int dimension
= BYTES_BY_CHAR_HEAD (i
) - 1;
1318 /* `i' is a leading code for an undefined charset. */
1319 reg
[RRR
] = CHARSET_8_BIT_GRAPHIC
;
1322 else if (src
+ dimension
> src_end
)
1323 goto ccl_read_multibyte_character_suspend
;
1327 i
= (*src
++ & 0x7F);
1331 reg
[rrr
] = ((i
<< 7) | (*src
++ & 0x7F));
1334 else if ((i
== LEADING_CODE_PRIVATE_11
)
1335 || (i
== LEADING_CODE_PRIVATE_12
))
1337 if ((src
+ 1) >= src_end
)
1338 goto ccl_read_multibyte_character_suspend
;
1340 reg
[rrr
] = (*src
++ & 0x7F);
1342 else if ((i
== LEADING_CODE_PRIVATE_21
)
1343 || (i
== LEADING_CODE_PRIVATE_22
))
1345 if ((src
+ 2) >= src_end
)
1346 goto ccl_read_multibyte_character_suspend
;
1348 i
= (*src
++ & 0x7F);
1349 reg
[rrr
] = ((i
<< 7) | (*src
& 0x7F));
1352 else if (i
== LEADING_CODE_8_BIT_CONTROL
)
1355 goto ccl_read_multibyte_character_suspend
;
1356 reg
[RRR
] = CHARSET_8_BIT_CONTROL
;
1357 reg
[rrr
] = (*src
++ - 0x20);
1361 reg
[RRR
] = CHARSET_8_BIT_GRAPHIC
;
1366 /* INVALID CODE. Return a single byte character. */
1367 reg
[RRR
] = CHARSET_ASCII
;
1372 ccl_read_multibyte_character_suspend
:
1373 if (src
<= src_end
&& !ccl
->multibyte
&& ccl
->last_block
)
1375 reg
[RRR
] = CHARSET_8_BIT_CONTROL
;
1380 if (ccl
->last_block
)
1386 CCL_SUSPEND (CCL_STAT_SUSPEND_BY_SRC
);
1390 case CCL_WriteMultibyteChar2
:
1391 i
= reg
[RRR
]; /* charset */
1392 if (i
== CHARSET_ASCII
1393 || i
== CHARSET_8_BIT_CONTROL
1394 || i
== CHARSET_8_BIT_GRAPHIC
)
1395 i
= reg
[rrr
] & 0xFF;
1396 else if (CHARSET_DIMENSION (i
) == 1)
1397 i
= ((i
- 0x70) << 7) | (reg
[rrr
] & 0x7F);
1398 else if (i
< MIN_CHARSET_PRIVATE_DIMENSION2
)
1399 i
= ((i
- 0x8F) << 14) | reg
[rrr
];
1401 i
= ((i
- 0xE0) << 14) | reg
[rrr
];
1403 CCL_WRITE_MULTIBYTE_CHAR (i
);
1407 case CCL_TranslateCharacter
:
1408 CCL_MAKE_CHAR (reg
[RRR
], reg
[rrr
], i
);
1409 op
= translate_char (GET_TRANSLATION_TABLE (reg
[Rrr
]),
1411 SPLIT_CHAR (op
, reg
[RRR
], i
, j
);
1418 case CCL_TranslateCharacterConstTbl
:
1419 op
= XINT (ccl_prog
[ic
]); /* table */
1421 CCL_MAKE_CHAR (reg
[RRR
], reg
[rrr
], i
);
1422 op
= translate_char (GET_TRANSLATION_TABLE (op
), i
, -1, 0, 0);
1423 SPLIT_CHAR (op
, reg
[RRR
], i
, j
);
1430 case CCL_LookupIntConstTbl
:
1431 op
= XINT (ccl_prog
[ic
]); /* table */
1434 struct Lisp_Hash_Table
*h
= GET_HASH_TABLE (op
);
1436 op
= hash_lookup (h
, make_number (reg
[RRR
]), NULL
);
1439 op
= HASH_VALUE (h
, op
);
1440 if (!CHAR_VALID_P (op
, 0))
1442 SPLIT_CHAR (XINT (op
), reg
[RRR
], i
, j
);
1446 reg
[7] = 1; /* r7 true for success */
1453 case CCL_LookupCharConstTbl
:
1454 op
= XINT (ccl_prog
[ic
]); /* table */
1456 CCL_MAKE_CHAR (reg
[RRR
], reg
[rrr
], i
);
1458 struct Lisp_Hash_Table
*h
= GET_HASH_TABLE (op
);
1460 op
= hash_lookup (h
, make_number (i
), NULL
);
1463 op
= HASH_VALUE (h
, op
);
1466 reg
[RRR
] = XINT (op
);
1467 reg
[7] = 1; /* r7 true for success */
1474 case CCL_IterateMultipleMap
:
1476 Lisp_Object map
, content
, attrib
, value
;
1477 int point
, size
, fin_ic
;
1479 j
= XINT (ccl_prog
[ic
++]); /* number of maps. */
1482 if ((j
> reg
[RRR
]) && (j
>= 0))
1497 size
= ASIZE (Vcode_conversion_map_vector
);
1498 point
= XINT (ccl_prog
[ic
++]);
1499 if (point
>= size
) continue;
1500 map
= AREF (Vcode_conversion_map_vector
, point
);
1502 /* Check map varidity. */
1503 if (!CONSP (map
)) continue;
1505 if (!VECTORP (map
)) continue;
1507 if (size
<= 1) continue;
1509 content
= AREF (map
, 0);
1512 [STARTPOINT VAL1 VAL2 ...] or
1513 [t ELELMENT STARTPOINT ENDPOINT] */
1514 if (NUMBERP (content
))
1516 point
= XUINT (content
);
1517 point
= op
- point
+ 1;
1518 if (!((point
>= 1) && (point
< size
))) continue;
1519 content
= AREF (map
, point
);
1521 else if (EQ (content
, Qt
))
1523 if (size
!= 4) continue;
1524 if ((op
>= XUINT (AREF (map
, 2)))
1525 && (op
< XUINT (AREF (map
, 3))))
1526 content
= AREF (map
, 1);
1535 else if (NUMBERP (content
))
1538 reg
[rrr
] = XINT(content
);
1541 else if (EQ (content
, Qt
) || EQ (content
, Qlambda
))
1546 else if (CONSP (content
))
1548 attrib
= XCAR (content
);
1549 value
= XCDR (content
);
1550 if (!NUMBERP (attrib
) || !NUMBERP (value
))
1553 reg
[rrr
] = XUINT (value
);
1556 else if (SYMBOLP (content
))
1557 CCL_CALL_FOR_MAP_INSTRUCTION (content
, fin_ic
);
1567 case CCL_MapMultiple
:
1569 Lisp_Object map
, content
, attrib
, value
;
1570 int point
, size
, map_vector_size
;
1571 int map_set_rest_length
, fin_ic
;
1572 int current_ic
= this_ic
;
1574 /* inhibit recursive call on MapMultiple. */
1575 if (stack_idx_of_map_multiple
> 0)
1577 if (stack_idx_of_map_multiple
<= stack_idx
)
1579 stack_idx_of_map_multiple
= 0;
1580 mapping_stack_pointer
= mapping_stack
;
1585 mapping_stack_pointer
= mapping_stack
;
1586 stack_idx_of_map_multiple
= 0;
1588 map_set_rest_length
=
1589 XINT (ccl_prog
[ic
++]); /* number of maps and separators. */
1590 fin_ic
= ic
+ map_set_rest_length
;
1593 if ((map_set_rest_length
> reg
[RRR
]) && (reg
[RRR
] >= 0))
1597 map_set_rest_length
-= i
;
1603 mapping_stack_pointer
= mapping_stack
;
1607 if (mapping_stack_pointer
<= (mapping_stack
+ 1))
1609 /* Set up initial state. */
1610 mapping_stack_pointer
= mapping_stack
;
1611 PUSH_MAPPING_STACK (0, op
);
1616 /* Recover after calling other ccl program. */
1619 POP_MAPPING_STACK (map_set_rest_length
, orig_op
);
1620 POP_MAPPING_STACK (map_set_rest_length
, reg
[rrr
]);
1624 /* Regard it as Qnil. */
1628 map_set_rest_length
--;
1631 /* Regard it as Qt. */
1635 map_set_rest_length
--;
1638 /* Regard it as Qlambda. */
1640 i
+= map_set_rest_length
;
1641 ic
+= map_set_rest_length
;
1642 map_set_rest_length
= 0;
1645 /* Regard it as normal mapping. */
1646 i
+= map_set_rest_length
;
1647 ic
+= map_set_rest_length
;
1648 POP_MAPPING_STACK (map_set_rest_length
, reg
[rrr
]);
1652 map_vector_size
= ASIZE (Vcode_conversion_map_vector
);
1655 for (;map_set_rest_length
> 0;i
++, ic
++, map_set_rest_length
--)
1657 point
= XINT(ccl_prog
[ic
]);
1660 /* +1 is for including separator. */
1662 if (mapping_stack_pointer
1663 >= &mapping_stack
[MAX_MAP_SET_LEVEL
])
1665 PUSH_MAPPING_STACK (map_set_rest_length
- point
,
1667 map_set_rest_length
= point
;
1672 if (point
>= map_vector_size
) continue;
1673 map
= AREF (Vcode_conversion_map_vector
, point
);
1675 /* Check map varidity. */
1676 if (!CONSP (map
)) continue;
1678 if (!VECTORP (map
)) continue;
1680 if (size
<= 1) continue;
1682 content
= AREF (map
, 0);
1685 [STARTPOINT VAL1 VAL2 ...] or
1686 [t ELEMENT STARTPOINT ENDPOINT] */
1687 if (NUMBERP (content
))
1689 point
= XUINT (content
);
1690 point
= op
- point
+ 1;
1691 if (!((point
>= 1) && (point
< size
))) continue;
1692 content
= AREF (map
, point
);
1694 else if (EQ (content
, Qt
))
1696 if (size
!= 4) continue;
1697 if ((op
>= XUINT (AREF (map
, 2))) &&
1698 (op
< XUINT (AREF (map
, 3))))
1699 content
= AREF (map
, 1);
1710 if (NUMBERP (content
))
1712 op
= XINT (content
);
1713 i
+= map_set_rest_length
- 1;
1714 ic
+= map_set_rest_length
- 1;
1715 POP_MAPPING_STACK (map_set_rest_length
, reg
[rrr
]);
1716 map_set_rest_length
++;
1718 else if (CONSP (content
))
1720 attrib
= XCAR (content
);
1721 value
= XCDR (content
);
1722 if (!NUMBERP (attrib
) || !NUMBERP (value
))
1725 i
+= map_set_rest_length
- 1;
1726 ic
+= map_set_rest_length
- 1;
1727 POP_MAPPING_STACK (map_set_rest_length
, reg
[rrr
]);
1728 map_set_rest_length
++;
1730 else if (EQ (content
, Qt
))
1734 else if (EQ (content
, Qlambda
))
1736 i
+= map_set_rest_length
;
1737 ic
+= map_set_rest_length
;
1740 else if (SYMBOLP (content
))
1742 if (mapping_stack_pointer
1743 >= &mapping_stack
[MAX_MAP_SET_LEVEL
])
1745 PUSH_MAPPING_STACK (map_set_rest_length
, reg
[rrr
]);
1746 PUSH_MAPPING_STACK (map_set_rest_length
, op
);
1747 stack_idx_of_map_multiple
= stack_idx
+ 1;
1748 CCL_CALL_FOR_MAP_INSTRUCTION (content
, current_ic
);
1753 if (mapping_stack_pointer
<= (mapping_stack
+ 1))
1755 POP_MAPPING_STACK (map_set_rest_length
, reg
[rrr
]);
1756 i
+= map_set_rest_length
;
1757 ic
+= map_set_rest_length
;
1758 POP_MAPPING_STACK (map_set_rest_length
, reg
[rrr
]);
1768 Lisp_Object map
, attrib
, value
, content
;
1770 j
= XINT (ccl_prog
[ic
++]); /* map_id */
1772 if (j
>= ASIZE (Vcode_conversion_map_vector
))
1777 map
= AREF (Vcode_conversion_map_vector
, j
);
1790 point
= XUINT (AREF (map
, 0));
1791 point
= op
- point
+ 1;
1794 (!((point
>= 1) && (point
< size
))))
1799 content
= AREF (map
, point
);
1802 else if (NUMBERP (content
))
1803 reg
[rrr
] = XINT (content
);
1804 else if (EQ (content
, Qt
));
1805 else if (CONSP (content
))
1807 attrib
= XCAR (content
);
1808 value
= XCDR (content
);
1809 if (!NUMBERP (attrib
) || !NUMBERP (value
))
1811 reg
[rrr
] = XUINT(value
);
1814 else if (SYMBOLP (content
))
1815 CCL_CALL_FOR_MAP_INSTRUCTION (content
, ic
);
1833 /* The suppress_error member is set when e.g. a CCL-based coding
1834 system is used for terminal output. */
1835 if (!ccl
->suppress_error
&& destination
)
1837 /* We can insert an error message only if DESTINATION is
1838 specified and we still have a room to store the message
1846 switch (ccl
->status
)
1848 case CCL_STAT_INVALID_CMD
:
1849 sprintf(msg
, "\nCCL: Invalid command %x (ccl_code = %x) at %d.",
1850 code
& 0x1F, code
, this_ic
);
1853 int i
= ccl_backtrace_idx
- 1;
1856 msglen
= strlen (msg
);
1857 if (dst
+ msglen
<= (dst_bytes
? dst_end
: src
))
1859 bcopy (msg
, dst
, msglen
);
1863 for (j
= 0; j
< CCL_DEBUG_BACKTRACE_LEN
; j
++, i
--)
1865 if (i
< 0) i
= CCL_DEBUG_BACKTRACE_LEN
- 1;
1866 if (ccl_backtrace_table
[i
] == 0)
1868 sprintf(msg
, " %d", ccl_backtrace_table
[i
]);
1869 msglen
= strlen (msg
);
1870 if (dst
+ msglen
> (dst_bytes
? dst_end
: src
))
1872 bcopy (msg
, dst
, msglen
);
1881 sprintf(msg
, "\nCCL: Quited.");
1885 sprintf(msg
, "\nCCL: Unknown error type (%d).", ccl
->status
);
1888 msglen
= strlen (msg
);
1889 if (dst
+ msglen
<= (dst_bytes
? dst_end
: src
))
1891 bcopy (msg
, dst
, msglen
);
1895 if (ccl
->status
== CCL_STAT_INVALID_CMD
)
1897 #if 0 /* If the remaining bytes contain 0x80..0x9F, copying them
1898 results in an invalid multibyte sequence. */
1900 /* Copy the remaining source data. */
1901 int i
= src_end
- src
;
1902 if (dst_bytes
&& (dst_end
- dst
) < i
)
1904 bcopy (src
, dst
, i
);
1908 /* Signal that we've consumed everything. */
1916 ccl
->stack_idx
= stack_idx
;
1917 ccl
->prog
= ccl_prog
;
1918 ccl
->eight_bit_control
= (extra_bytes
> 0);
1920 *consumed
= src
- source
;
1921 return (dst
? dst
- destination
: 0);
1924 /* Resolve symbols in the specified CCL code (Lisp vector). This
1925 function converts symbols of code conversion maps and character
1926 translation tables embeded in the CCL code into their ID numbers.
1928 The return value is a vector (CCL itself or a new vector in which
1929 all symbols are resolved), Qt if resolving of some symbol failed,
1930 or nil if CCL contains invalid data. */
1933 resolve_symbol_ccl_program (ccl
)
1936 int i
, veclen
, unresolved
= 0;
1937 Lisp_Object result
, contents
, val
;
1940 veclen
= ASIZE (result
);
1942 for (i
= 0; i
< veclen
; i
++)
1944 contents
= AREF (result
, i
);
1945 if (INTEGERP (contents
))
1947 else if (CONSP (contents
)
1948 && SYMBOLP (XCAR (contents
))
1949 && SYMBOLP (XCDR (contents
)))
1951 /* This is the new style for embedding symbols. The form is
1952 (SYMBOL . PROPERTY). (get SYMBOL PROPERTY) should give
1955 if (EQ (result
, ccl
))
1956 result
= Fcopy_sequence (ccl
);
1958 val
= Fget (XCAR (contents
), XCDR (contents
));
1960 AREF (result
, i
) = val
;
1965 else if (SYMBOLP (contents
))
1967 /* This is the old style for embedding symbols. This style
1968 may lead to a bug if, for instance, a translation table
1969 and a code conversion map have the same name. */
1970 if (EQ (result
, ccl
))
1971 result
= Fcopy_sequence (ccl
);
1973 val
= Fget (contents
, Qtranslation_table_id
);
1975 AREF (result
, i
) = val
;
1978 val
= Fget (contents
, Qcode_conversion_map_id
);
1980 AREF (result
, i
) = val
;
1983 val
= Fget (contents
, Qccl_program_idx
);
1985 AREF (result
, i
) = val
;
1995 return (unresolved
? Qt
: result
);
1998 /* Return the compiled code (vector) of CCL program CCL_PROG.
1999 CCL_PROG is a name (symbol) of the program or already compiled
2000 code. If necessary, resolve symbols in the compiled code to index
2001 numbers. If we failed to get the compiled code or to resolve
2002 symbols, return Qnil. */
2005 ccl_get_compiled_code (ccl_prog
)
2006 Lisp_Object ccl_prog
;
2008 Lisp_Object val
, slot
;
2010 if (VECTORP (ccl_prog
))
2012 val
= resolve_symbol_ccl_program (ccl_prog
);
2013 return (VECTORP (val
) ? val
: Qnil
);
2015 if (!SYMBOLP (ccl_prog
))
2018 val
= Fget (ccl_prog
, Qccl_program_idx
);
2020 || XINT (val
) >= ASIZE (Vccl_program_table
))
2022 slot
= AREF (Vccl_program_table
, XINT (val
));
2023 if (! VECTORP (slot
)
2024 || ASIZE (slot
) != 3
2025 || ! VECTORP (AREF (slot
, 1)))
2027 if (NILP (AREF (slot
, 2)))
2029 val
= resolve_symbol_ccl_program (AREF (slot
, 1));
2030 if (! VECTORP (val
))
2032 AREF (slot
, 1) = val
;
2033 AREF (slot
, 2) = Qt
;
2035 return AREF (slot
, 1);
2038 /* Setup fields of the structure pointed by CCL appropriately for the
2039 execution of CCL program CCL_PROG. CCL_PROG is the name (symbol)
2040 of the CCL program or the already compiled code (vector).
2041 Return 0 if we succeed this setup, else return -1.
2043 If CCL_PROG is nil, we just reset the structure pointed by CCL. */
2045 setup_ccl_program (ccl
, ccl_prog
)
2046 struct ccl_program
*ccl
;
2047 Lisp_Object ccl_prog
;
2051 if (! NILP (ccl_prog
))
2053 struct Lisp_Vector
*vp
;
2055 ccl_prog
= ccl_get_compiled_code (ccl_prog
);
2056 if (! VECTORP (ccl_prog
))
2058 vp
= XVECTOR (ccl_prog
);
2059 ccl
->size
= vp
->size
;
2060 ccl
->prog
= vp
->contents
;
2061 ccl
->eof_ic
= XINT (vp
->contents
[CCL_HEADER_EOF
]);
2062 ccl
->buf_magnification
= XINT (vp
->contents
[CCL_HEADER_BUF_MAG
]);
2064 ccl
->ic
= CCL_HEADER_MAIN
;
2065 for (i
= 0; i
< 8; i
++)
2067 ccl
->last_block
= 0;
2068 ccl
->private_state
= 0;
2071 ccl
->eol_type
= CODING_EOL_LF
;
2072 ccl
->suppress_error
= 0;
2078 DEFUN ("ccl-program-p", Fccl_program_p
, Sccl_program_p
, 1, 1, 0,
2079 doc
: /* Return t if OBJECT is a CCL program name or a compiled CCL program code.
2080 See the documentation of `define-ccl-program' for the detail of CCL program. */)
2086 if (VECTORP (object
))
2088 val
= resolve_symbol_ccl_program (object
);
2089 return (VECTORP (val
) ? Qt
: Qnil
);
2091 if (!SYMBOLP (object
))
2094 val
= Fget (object
, Qccl_program_idx
);
2095 return ((! NATNUMP (val
)
2096 || XINT (val
) >= ASIZE (Vccl_program_table
))
2100 DEFUN ("ccl-execute", Fccl_execute
, Sccl_execute
, 2, 2, 0,
2101 doc
: /* Execute CCL-PROGRAM with registers initialized by REGISTERS.
2103 CCL-PROGRAM is a CCL program name (symbol)
2104 or compiled code generated by `ccl-compile' (for backward compatibility.
2105 In the latter case, the execution overhead is bigger than in the former).
2106 No I/O commands should appear in CCL-PROGRAM.
2108 REGISTERS is a vector of [R0 R1 ... R7] where RN is an initial value
2109 for the Nth register.
2111 As side effect, each element of REGISTERS holds the value of
2112 the corresponding register after the execution.
2114 See the documentation of `define-ccl-program' for a definition of CCL
2117 Lisp_Object ccl_prog
, reg
;
2119 struct ccl_program ccl
;
2122 if (setup_ccl_program (&ccl
, ccl_prog
) < 0)
2123 error ("Invalid CCL program");
2126 if (ASIZE (reg
) != 8)
2127 error ("Length of vector REGISTERS is not 8");
2129 for (i
= 0; i
< 8; i
++)
2130 ccl
.reg
[i
] = (INTEGERP (AREF (reg
, i
))
2131 ? XINT (AREF (reg
, i
))
2134 ccl_driver (&ccl
, (unsigned char *)0, (unsigned char *)0, 0, 0, (int *)0);
2136 if (ccl
.status
!= CCL_STAT_SUCCESS
)
2137 error ("Error in CCL program at %dth code", ccl
.ic
);
2139 for (i
= 0; i
< 8; i
++)
2140 XSETINT (AREF (reg
, i
), ccl
.reg
[i
]);
2144 DEFUN ("ccl-execute-on-string", Fccl_execute_on_string
, Sccl_execute_on_string
,
2146 doc
: /* Execute CCL-PROGRAM with initial STATUS on STRING.
2148 CCL-PROGRAM is a symbol registered by register-ccl-program,
2149 or a compiled code generated by `ccl-compile' (for backward compatibility,
2150 in this case, the execution is slower).
2152 Read buffer is set to STRING, and write buffer is allocated automatically.
2154 STATUS is a vector of [R0 R1 ... R7 IC], where
2155 R0..R7 are initial values of corresponding registers,
2156 IC is the instruction counter specifying from where to start the program.
2157 If R0..R7 are nil, they are initialized to 0.
2158 If IC is nil, it is initialized to head of the CCL program.
2160 If optional 4th arg CONTINUE is non-nil, keep IC on read operation
2161 when read buffer is exausted, else, IC is always set to the end of
2162 CCL-PROGRAM on exit.
2164 It returns the contents of write buffer as a string,
2165 and as side effect, STATUS is updated.
2166 If the optional 5th arg UNIBYTE-P is non-nil, the returned string
2167 is a unibyte string. By default it is a multibyte string.
2169 See the documentation of `define-ccl-program' for the detail of CCL program. */)
2170 (ccl_prog
, status
, str
, contin
, unibyte_p
)
2171 Lisp_Object ccl_prog
, status
, str
, contin
, unibyte_p
;
2174 struct ccl_program ccl
;
2178 struct gcpro gcpro1
, gcpro2
;
2180 if (setup_ccl_program (&ccl
, ccl_prog
) < 0)
2181 error ("Invalid CCL program");
2183 CHECK_VECTOR (status
);
2184 if (ASIZE (status
) != 9)
2185 error ("Length of vector STATUS is not 9");
2188 GCPRO2 (status
, str
);
2190 for (i
= 0; i
< 8; i
++)
2192 if (NILP (AREF (status
, i
)))
2193 XSETINT (AREF (status
, i
), 0);
2194 if (INTEGERP (AREF (status
, i
)))
2195 ccl
.reg
[i
] = XINT (AREF (status
, i
));
2197 if (INTEGERP (AREF (status
, i
)))
2199 i
= XFASTINT (AREF (status
, 8));
2200 if (ccl
.ic
< i
&& i
< ccl
.size
)
2203 outbufsize
= STRING_BYTES (XSTRING (str
)) * ccl
.buf_magnification
+ 256;
2204 outbuf
= (char *) xmalloc (outbufsize
);
2205 ccl
.last_block
= NILP (contin
);
2206 ccl
.multibyte
= STRING_MULTIBYTE (str
);
2207 produced
= ccl_driver (&ccl
, XSTRING (str
)->data
, outbuf
,
2208 STRING_BYTES (XSTRING (str
)), outbufsize
, (int *) 0);
2209 for (i
= 0; i
< 8; i
++)
2210 XSET (AREF (status
, i
), Lisp_Int
, ccl
.reg
[i
]);
2211 XSETINT (AREF (status
, 8), ccl
.ic
);
2214 if (NILP (unibyte_p
))
2218 produced
= str_as_multibyte (outbuf
, outbufsize
, produced
, &nchars
);
2219 val
= make_multibyte_string (outbuf
, nchars
, produced
);
2222 val
= make_unibyte_string (outbuf
, produced
);
2225 if (ccl
.status
== CCL_STAT_SUSPEND_BY_DST
)
2226 error ("Output buffer for the CCL programs overflow");
2227 if (ccl
.status
!= CCL_STAT_SUCCESS
2228 && ccl
.status
!= CCL_STAT_SUSPEND_BY_SRC
)
2229 error ("Error in CCL program at %dth code", ccl
.ic
);
2234 DEFUN ("register-ccl-program", Fregister_ccl_program
, Sregister_ccl_program
,
2236 doc
: /* Register CCL program CCL_PROG as NAME in `ccl-program-table'.
2237 CCL_PROG should be a compiled CCL program (vector), or nil.
2238 If it is nil, just reserve NAME as a CCL program name.
2239 Return index number of the registered CCL program. */)
2241 Lisp_Object name
, ccl_prog
;
2243 int len
= ASIZE (Vccl_program_table
);
2245 Lisp_Object resolved
;
2247 CHECK_SYMBOL (name
);
2249 if (!NILP (ccl_prog
))
2251 CHECK_VECTOR (ccl_prog
);
2252 resolved
= resolve_symbol_ccl_program (ccl_prog
);
2253 if (NILP (resolved
))
2254 error ("Error in CCL program");
2255 if (VECTORP (resolved
))
2257 ccl_prog
= resolved
;
2264 for (idx
= 0; idx
< len
; idx
++)
2268 slot
= AREF (Vccl_program_table
, idx
);
2269 if (!VECTORP (slot
))
2270 /* This is the first unsed slot. Register NAME here. */
2273 if (EQ (name
, AREF (slot
, 0)))
2275 /* Update this slot. */
2276 AREF (slot
, 1) = ccl_prog
;
2277 AREF (slot
, 2) = resolved
;
2278 return make_number (idx
);
2284 /* Extend the table. */
2285 Lisp_Object new_table
;
2288 new_table
= Fmake_vector (make_number (len
* 2), Qnil
);
2289 for (j
= 0; j
< len
; j
++)
2291 = AREF (Vccl_program_table
, j
);
2292 Vccl_program_table
= new_table
;
2298 elt
= Fmake_vector (make_number (3), Qnil
);
2299 AREF (elt
, 0) = name
;
2300 AREF (elt
, 1) = ccl_prog
;
2301 AREF (elt
, 2) = resolved
;
2302 AREF (Vccl_program_table
, idx
) = elt
;
2305 Fput (name
, Qccl_program_idx
, make_number (idx
));
2306 return make_number (idx
);
2309 /* Register code conversion map.
2310 A code conversion map consists of numbers, Qt, Qnil, and Qlambda.
2311 The first element is the start code point.
2312 The other elements are mapped numbers.
2313 Symbol t means to map to an original number before mapping.
2314 Symbol nil means that the corresponding element is empty.
2315 Symbol lambda means to terminate mapping here.
2318 DEFUN ("register-code-conversion-map", Fregister_code_conversion_map
,
2319 Sregister_code_conversion_map
,
2321 doc
: /* Register SYMBOL as code conversion map MAP.
2322 Return index number of the registered map. */)
2324 Lisp_Object symbol
, map
;
2326 int len
= ASIZE (Vcode_conversion_map_vector
);
2330 CHECK_SYMBOL (symbol
);
2333 for (i
= 0; i
< len
; i
++)
2335 Lisp_Object slot
= AREF (Vcode_conversion_map_vector
, i
);
2340 if (EQ (symbol
, XCAR (slot
)))
2342 index
= make_number (i
);
2343 XSETCDR (slot
, map
);
2344 Fput (symbol
, Qcode_conversion_map
, map
);
2345 Fput (symbol
, Qcode_conversion_map_id
, index
);
2352 Lisp_Object new_vector
= Fmake_vector (make_number (len
* 2), Qnil
);
2355 for (j
= 0; j
< len
; j
++)
2356 AREF (new_vector
, j
)
2357 = AREF (Vcode_conversion_map_vector
, j
);
2358 Vcode_conversion_map_vector
= new_vector
;
2361 index
= make_number (i
);
2362 Fput (symbol
, Qcode_conversion_map
, map
);
2363 Fput (symbol
, Qcode_conversion_map_id
, index
);
2364 AREF (Vcode_conversion_map_vector
, i
) = Fcons (symbol
, map
);
2372 staticpro (&Vccl_program_table
);
2373 Vccl_program_table
= Fmake_vector (make_number (32), Qnil
);
2375 Qccl_program
= intern ("ccl-program");
2376 staticpro (&Qccl_program
);
2378 Qccl_program_idx
= intern ("ccl-program-idx");
2379 staticpro (&Qccl_program_idx
);
2381 Qcode_conversion_map
= intern ("code-conversion-map");
2382 staticpro (&Qcode_conversion_map
);
2384 Qcode_conversion_map_id
= intern ("code-conversion-map-id");
2385 staticpro (&Qcode_conversion_map_id
);
2387 DEFVAR_LISP ("code-conversion-map-vector", &Vcode_conversion_map_vector
,
2388 doc
: /* Vector of code conversion maps. */);
2389 Vcode_conversion_map_vector
= Fmake_vector (make_number (16), Qnil
);
2391 DEFVAR_LISP ("font-ccl-encoder-alist", &Vfont_ccl_encoder_alist
,
2392 doc
: /* Alist of fontname patterns vs corresponding CCL program.
2393 Each element looks like (REGEXP . CCL-CODE),
2394 where CCL-CODE is a compiled CCL program.
2395 When a font whose name matches REGEXP is used for displaying a character,
2396 CCL-CODE is executed to calculate the code point in the font
2397 from the charset number and position code(s) of the character which are set
2398 in CCL registers R0, R1, and R2 before the execution.
2399 The code point in the font is set in CCL registers R1 and R2
2400 when the execution terminated.
2401 If the font is single-byte font, the register R2 is not used. */);
2402 Vfont_ccl_encoder_alist
= Qnil
;
2404 DEFVAR_LISP ("translation-hash-table-vector", &Vtranslation_hash_table_vector
,
2405 doc
: /* Vector containing all translation hash tables ever defined.
2406 Comprises pairs (SYMBOL . TABLE) where SYMBOL and TABLE were set up by calls
2407 to `define-translation-hash-table'. The vector is indexed by the table id
2409 Vtranslation_hash_table_vector
= Qnil
;
2411 defsubr (&Sccl_program_p
);
2412 defsubr (&Sccl_execute
);
2413 defsubr (&Sccl_execute_on_string
);
2414 defsubr (&Sregister_ccl_program
);
2415 defsubr (&Sregister_code_conversion_map
);