1 /* CCL (Code Conversion Language) interpreter.
2 Copyright (C) 1995, 1997 Electrotechnical Laboratory, JAPAN.
3 Licensed to the Free Software Foundation.
5 This file is part of SXEmacs
7 SXEmacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 SXEmacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 /* Synched up with : FSF Emacs 21.0.90 except TranslateCharacter */
33 #include "mule-charset.h"
35 #include "file-coding.h"
41 #endif /* not emacs */
43 /* This contains all code conversion map available to CCL. */
44 Lisp_Object Vcode_conversion_map_vector;
46 /* Alist of fontname patterns vs corresponding CCL program. */
47 Lisp_Object Vfont_ccl_encoder_alist;
49 /* This symbol is a property which associates with ccl program vector.
50 Ex: (get 'ccl-big5-encoder 'ccl-program) returns ccl program vector. */
51 Lisp_Object Qccl_program;
53 /* These symbols are properties which associate with code conversion
54 map and their ID respectively. */
55 Lisp_Object Qcode_conversion_map;
56 Lisp_Object Qcode_conversion_map_id;
58 /* Symbols of ccl program have this property, a value of the property
59 is an index for Vccl_program_table. */
60 Lisp_Object Qccl_program_idx;
62 /* Table of registered CCL programs. Each element is a vector of
63 NAME, CCL_PROG, and RESOLVEDP where NAME (symbol) is the name of
64 the program, CCL_PROG (vector) is the compiled code of the program,
65 RESOLVEDP (t or nil) is the flag to tell if symbols in CCL_PROG is
66 already resolved to index numbers or not. */
67 Lisp_Object Vccl_program_table;
69 /* CCL (Code Conversion Language) is a simple language which has
70 operations on one input buffer, one output buffer, and 7 registers.
71 The syntax of CCL is described in `ccl.el'. Emacs Lisp function
72 `ccl-compile' compiles a CCL program and produces a CCL code which
73 is a vector of integers. The structure of this vector is as
74 follows: The 1st element: buffer-magnification, a factor for the
75 size of output buffer compared with the size of input buffer. The
76 2nd element: address of CCL code to be executed when encountered
77 with end of input stream. The 3rd and the remaining elements: CCL
80 /* Header of CCL compiled code */
81 #define CCL_HEADER_BUF_MAG 0
82 #define CCL_HEADER_EOF 1
83 #define CCL_HEADER_MAIN 2
85 /* CCL code is a sequence of 28-bit non-negative integers (i.e. the
86 MSB is always 0), each contains CCL command and/or arguments in the
89 |----------------- integer (28-bit) ------------------|
90 |------- 17-bit ------|- 3-bit --|- 3-bit --|- 5-bit -|
91 |--constant argument--|-register-|-register-|-command-|
92 ccccccccccccccccc RRR rrr XXXXX
94 |------- relative address -------|-register-|-command-|
95 cccccccccccccccccccc rrr XXXXX
97 |------------- constant or other args ----------------|
98 cccccccccccccccccccccccccccc
100 where, `cc...c' is a non-negative integer indicating constant value
101 (the left most `c' is always 0) or an absolute jump address, `RRR'
102 and `rrr' are CCL register number, `XXXXX' is one of the following
107 Each comment fields shows one or more lines for command syntax and
108 the following lines for semantics of the command. In semantics, IC
109 stands for Instruction Counter. */
111 #define CCL_SetRegister 0x00 /* Set register a register value:
112 1:00000000000000000RRRrrrXXXXX
113 ------------------------------
117 #define CCL_SetShortConst 0x01 /* Set register a short constant value:
118 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
119 ------------------------------
120 reg[rrr] = CCCCCCCCCCCCCCCCCCC;
123 #define CCL_SetConst 0x02 /* Set register a constant value:
124 1:00000000000000000000rrrXXXXX
126 ------------------------------
131 #define CCL_SetArray 0x03 /* Set register an element of array:
132 1:CCCCCCCCCCCCCCCCCRRRrrrXXXXX
136 ------------------------------
137 if (0 <= reg[RRR] < CC..C)
138 reg[rrr] = ELEMENT[reg[RRR]];
142 #define CCL_Jump 0x04 /* Jump:
143 1:A--D--D--R--E--S--S-000XXXXX
144 ------------------------------
148 /* Note: If CC..C is greater than 0, the second code is omitted. */
150 #define CCL_JumpCond 0x05 /* Jump conditional:
151 1:A--D--D--R--E--S--S-rrrXXXXX
152 ------------------------------
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:0000STRIN[0]STRIN[1]STRIN[2]
202 ------------------------------
203 write_string (STRING, LENGTH);
207 #define CCL_WriteArrayReadJump 0x0B /* Write an array element, read, and jump:
208 1:A--D--D--R--E--S--S-rrrXXXXX
213 N:A--D--D--R--E--S--S-rrrYYYYY
214 ------------------------------
215 if (0 <= reg[rrr] < LENGTH)
216 write (ELEMENT[reg[rrr]]);
217 IC += LENGTH + 2; (... pointing at N+1)
221 /* Note: If read is suspended, the resumed execution starts from the
222 Nth code (YYYYY == CCL_ReadJump). */
224 #define CCL_ReadJump 0x0C /* Read and jump:
225 1:A--D--D--R--E--S--S-rrrYYYYY
226 -----------------------------
231 #define CCL_Branch 0x0D /* Jump by branch table:
232 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
233 2:A--D--D--R--E-S-S[0]000XXXXX
234 3:A--D--D--R--E-S-S[1]000XXXXX
236 ------------------------------
237 if (0 <= reg[rrr] < CC..C)
238 IC += ADDRESS[reg[rrr]];
240 IC += ADDRESS[CC..C];
243 #define CCL_ReadRegister 0x0E /* Read bytes into registers:
244 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
245 2:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
247 ------------------------------
252 #define CCL_WriteExprConst 0x0F /* write result of expression:
253 1:00000OPERATION000RRR000XXXXX
255 ------------------------------
256 write (reg[RRR] OPERATION CONSTANT);
260 /* Note: If the Nth read is suspended, the resumed execution starts
261 from the Nth code. */
263 #define CCL_ReadBranch 0x10 /* Read one byte into a register,
264 and jump by branch table:
265 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
266 2:A--D--D--R--E-S-S[0]000XXXXX
267 3:A--D--D--R--E-S-S[1]000XXXXX
269 ------------------------------
271 if (0 <= reg[rrr] < CC..C)
272 IC += ADDRESS[reg[rrr]];
274 IC += ADDRESS[CC..C];
277 #define CCL_WriteRegister 0x11 /* Write registers:
278 1:CCCCCCCCCCCCCCCCCCCrrrXXXXX
279 2:CCCCCCCCCCCCCCCCCCCrrrXXXXX
281 ------------------------------
287 /* Note: If the Nth write is suspended, the resumed execution
288 starts from the Nth code. */
290 #define CCL_WriteExprRegister 0x12 /* Write result of expression
291 1:00000OPERATIONRrrRRR000XXXXX
292 ------------------------------
293 write (reg[RRR] OPERATION reg[Rrr]);
296 #define CCL_Call 0x13 /* Call the CCL program whose ID is
298 1:CCCCCCCCCCCCCCCCCCCCFFFXXXXX
299 [2:00000000cccccccccccccccccccc]
300 ------------------------------
308 #define CCL_WriteConstString 0x14 /* Write a constant or a string:
309 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
310 [2:0000STRIN[0]STRIN[1]STRIN[2]]
312 -----------------------------
316 write_string (STRING, CC..C);
317 IC += (CC..C + 2) / 3;
320 #define CCL_WriteArray 0x15 /* Write an element of array:
321 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
325 ------------------------------
326 if (0 <= reg[rrr] < CC..C)
327 write (ELEMENT[reg[rrr]]);
331 #define CCL_End 0x16 /* Terminate:
332 1:00000000000000000000000XXXXX
333 ------------------------------
337 /* The following two codes execute an assignment arithmetic/logical
338 operation. The form of the operation is like REG OP= OPERAND. */
340 #define CCL_ExprSelfConst 0x17 /* REG OP= constant:
341 1:00000OPERATION000000rrrXXXXX
343 ------------------------------
344 reg[rrr] OPERATION= CONSTANT;
347 #define CCL_ExprSelfReg 0x18 /* REG1 OP= REG2:
348 1:00000OPERATION000RRRrrrXXXXX
349 ------------------------------
350 reg[rrr] OPERATION= reg[RRR];
353 /* The following codes execute an arithmetic/logical operation. The
354 form of the operation is like REG_X = REG_Y OP OPERAND2. */
356 #define CCL_SetExprConst 0x19 /* REG_X = REG_Y OP constant:
357 1:00000OPERATION000RRRrrrXXXXX
359 ------------------------------
360 reg[rrr] = reg[RRR] OPERATION CONSTANT;
364 #define CCL_SetExprReg 0x1A /* REG1 = REG2 OP REG3:
365 1:00000OPERATIONRrrRRRrrrXXXXX
366 ------------------------------
367 reg[rrr] = reg[RRR] OPERATION reg[Rrr];
370 #define CCL_JumpCondExprConst 0x1B /* Jump conditional according to
371 an operation on constant:
372 1:A--D--D--R--E--S--S-rrrXXXXX
375 -----------------------------
376 reg[7] = reg[rrr] OPERATION CONSTANT;
383 #define CCL_JumpCondExprReg 0x1C /* Jump conditional according to
384 an operation on register:
385 1:A--D--D--R--E--S--S-rrrXXXXX
388 -----------------------------
389 reg[7] = reg[rrr] OPERATION reg[RRR];
396 #define CCL_ReadJumpCondExprConst 0x1D /* Read and jump conditional according
397 to an operation on constant:
398 1:A--D--D--R--E--S--S-rrrXXXXX
401 -----------------------------
403 reg[7] = reg[rrr] OPERATION CONSTANT;
410 #define CCL_ReadJumpCondExprReg 0x1E /* Read and jump conditional according
411 to an operation on register:
412 1:A--D--D--R--E--S--S-rrrXXXXX
415 -----------------------------
417 reg[7] = reg[rrr] OPERATION reg[RRR];
424 #define CCL_Extension 0x1F /* Extended CCL code
425 1:ExtendedCOMMNDRrrRRRrrrXXXXX
428 ------------------------------
429 extended_command (rrr,RRR,Rrr,ARGS)
433 Here after, Extended CCL Instructions.
434 Bit length of extended command is 14.
435 Therefore, the instruction code range is 0..16384(0x3fff).
438 /* Read a multibyte characeter.
439 A code point is stored into reg[rrr]. A charset ID is stored into
442 #define CCL_ReadMultibyteChar2 0x00 /* Read Multibyte Character
443 1:ExtendedCOMMNDRrrRRRrrrXXXXX */
445 /* Write a multibyte character.
446 Write a character whose code point is reg[rrr] and the charset ID
449 #define CCL_WriteMultibyteChar2 0x01 /* Write Multibyte Character
450 1:ExtendedCOMMNDRrrRRRrrrXXXXX */
452 /* Translate a character whose code point is reg[rrr] and the charset
453 ID is reg[RRR] by a translation table whose ID is reg[Rrr].
455 A translated character is set in reg[rrr] (code point) and reg[RRR]
458 #define CCL_TranslateCharacter 0x02 /* Translate a 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 ARGUMENT.
464 A translated character is set in reg[rrr] (code point) and reg[RRR]
467 #define CCL_TranslateCharacterConstTbl 0x03 /* Translate a multibyte character
468 1:ExtendedCOMMNDRrrRRRrrrXXXXX
469 2:ARGUMENT(Translation Table ID)
472 /* Iterate looking up MAPs for reg[rrr] starting from the Nth (N =
473 reg[RRR]) MAP until some value is found.
475 Each MAP is a Lisp vector whose element is number, nil, t, or
477 If the element is nil, ignore the map and proceed to the next map.
478 If the element is t or lambda, finish without changing reg[rrr].
479 If the element is a number, set reg[rrr] to the number and finish.
481 Detail of the map structure is described in the comment for
482 CCL_MapMultiple below. */
484 #define CCL_IterateMultipleMap 0x10 /* Iterate multiple maps
485 1:ExtendedCOMMNDXXXRRRrrrXXXXX
492 /* Map the code in reg[rrr] by MAPs starting from the Nth (N =
495 MAPs are supplied in the succeeding CCL codes as follows:
497 When CCL program gives this nested structure of map to this command:
500 (MAP-ID121 MAP-ID122 MAP-ID123)
503 (MAP-ID211 (MAP-ID2111) MAP-ID212)
505 the compiled CCL code has this sequence:
506 CCL_MapMultiple (CCL code of this command)
507 16 (total number of MAPs and SEPARATORs)
525 A value of each SEPARATOR follows this rule:
526 MAP-SET := SEPARATOR [(MAP-ID | MAP-SET)]+
527 SEPARATOR := -(number of MAP-IDs and SEPARATORs in the MAP-SET)
529 (*)....Nest level of MAP-SET must not be over than MAX_MAP_SET_LEVEL.
531 When some map fails to map (i.e. it doesn't have a value for
532 reg[rrr]), the mapping is treated as identity.
534 The mapping is iterated for all maps in each map set (set of maps
535 separated by SEPARATOR) except in the case that lambda is
536 encountered. More precisely, the mapping proceeds as below:
538 At first, VAL0 is set to reg[rrr], and it is translated by the
539 first map to VAL1. Then, VAL1 is translated by the next map to
540 VAL2. This mapping is iterated until the last map is used. The
541 result of the mapping is the last value of VAL?. When the mapping
542 process reached to the end of the map set, it moves to the next
543 map set. If the next does not exit, the mapping process terminates,
544 and regard the last value as a result.
546 But, when VALm is mapped to VALn and VALn is not a number, the
547 mapping proceeds as follows:
549 If VALn is nil, the lastest map is ignored and the mapping of VALm
550 proceeds to the next map.
552 In VALn is t, VALm is reverted to reg[rrr] and the mapping of VALm
553 proceeds to the next map.
555 If VALn is lambda, move to the next map set like reaching to the
556 end of the current map set.
558 If VALn is a symbol, call the CCL program refered by it.
559 Then, use reg[rrr] as a mapped value except for -1, -2 and -3.
560 Such special values are regarded as nil, t, and lambda respectively.
562 Each map is a Lisp vector of the following format (a) or (b):
563 (a)......[STARTPOINT VAL1 VAL2 ...]
564 (b)......[t VAL STARTPOINT ENDPOINT],
566 STARTPOINT is an offset to be used for indexing a map,
567 ENDPOINT is a maximum index number of a map,
568 VAL and VALn is a number, nil, t, or lambda.
570 Valid index range of a map of type (a) is:
571 STARTPOINT <= index < STARTPOINT + map_size - 1
572 Valid index range of a map of type (b) is:
573 STARTPOINT <= index < ENDPOINT */
575 #define CCL_MapMultiple 0x11 /* Mapping by multiple code conversion maps
576 1:ExtendedCOMMNDXXXRRRrrrXXXXX
588 #define MAX_MAP_SET_LEVEL 30
595 static tr_stack mapping_stack[MAX_MAP_SET_LEVEL];
596 static tr_stack *mapping_stack_pointer;
598 /* If this variable is non-zero, it indicates the stack_idx
599 of immediately called by CCL_MapMultiple. */
600 static int stack_idx_of_map_multiple;
602 #define PUSH_MAPPING_STACK(restlen, orig) \
604 mapping_stack_pointer->rest_length = (restlen); \
605 mapping_stack_pointer->orig_val = (orig); \
606 mapping_stack_pointer++; \
609 #define POP_MAPPING_STACK(restlen, orig) \
611 mapping_stack_pointer--; \
612 (restlen) = mapping_stack_pointer->rest_length; \
613 (orig) = mapping_stack_pointer->orig_val; \
616 #define CCL_CALL_FOR_MAP_INSTRUCTION(symbol, ret_ic) \
618 struct ccl_program called_ccl; \
619 if (stack_idx >= 256 \
620 || (setup_ccl_program (&called_ccl, (symbol)) != 0)) \
624 ccl_prog = ccl_prog_stack_struct[0].ccl_prog; \
625 ic = ccl_prog_stack_struct[0].ic; \
629 ccl_prog_stack_struct[stack_idx].ccl_prog = ccl_prog; \
630 ccl_prog_stack_struct[stack_idx].ic = (ret_ic); \
632 ccl_prog = called_ccl.prog; \
633 ic = CCL_HEADER_MAIN; \
634 /* The "if (1)" prevents warning \
635 "end-of loop code not reached" */ \
636 if (1) goto ccl_repeat; \
639 #define CCL_MapSingle 0x12 /* Map by single code conversion map
640 1:ExtendedCOMMNDXXXRRRrrrXXXXX
642 ------------------------------
643 Map reg[rrr] by MAP-ID.
644 If some valid mapping is found,
645 set reg[rrr] to the result,
650 /* CCL arithmetic/logical operators. */
651 #define CCL_PLUS 0x00 /* X = Y + Z */
652 #define CCL_MINUS 0x01 /* X = Y - Z */
653 #define CCL_MUL 0x02 /* X = Y * Z */
654 #define CCL_DIV 0x03 /* X = Y / Z */
655 #define CCL_MOD 0x04 /* X = Y % Z */
656 #define CCL_AND 0x05 /* X = Y & Z */
657 #define CCL_OR 0x06 /* X = Y | Z */
658 #define CCL_XOR 0x07 /* X = Y ^ Z */
659 #define CCL_LSH 0x08 /* X = Y << Z */
660 #define CCL_RSH 0x09 /* X = Y >> Z */
661 #define CCL_LSH8 0x0A /* X = (Y << 8) | Z */
662 #define CCL_RSH8 0x0B /* X = Y >> 8, r[7] = Y & 0xFF */
663 #define CCL_DIVMOD 0x0C /* X = Y / Z, r[7] = Y % Z */
664 #define CCL_LS 0x10 /* X = (X < Y) */
665 #define CCL_GT 0x11 /* X = (X > Y) */
666 #define CCL_EQ 0x12 /* X = (X == Y) */
667 #define CCL_LE 0x13 /* X = (X <= Y) */
668 #define CCL_GE 0x14 /* X = (X >= Y) */
669 #define CCL_NE 0x15 /* X = (X != Y) */
671 #define CCL_DECODE_SJIS 0x16 /* X = HIGHER_BYTE (DE-SJIS (Y, Z))
672 r[7] = LOWER_BYTE (DE-SJIS (Y, Z)) */
673 #define CCL_ENCODE_SJIS 0x17 /* X = HIGHER_BYTE (SJIS (Y, Z))
674 r[7] = LOWER_BYTE (SJIS (Y, Z) */
676 /* Terminate CCL program successfully. */
677 #define CCL_SUCCESS \
679 ccl->status = CCL_STAT_SUCCESS; \
680 /* The "if (1)" inhibits the warning \
681 "end-of loop code not reached" */ \
682 if (1) goto ccl_finish; \
685 /* Suspend CCL program because of reading from empty input buffer or
686 writing to full output buffer. When this program is resumed, the
687 same I/O command is executed. */
688 #define CCL_SUSPEND(stat) \
691 ccl->status = (stat); \
692 /* The "if (1)" inhibits the warning \
693 "end-of loop code not reached" */ \
694 if (1) goto ccl_finish; \
697 /* Terminate CCL program because of invalid command. Should not occur
698 in the normal case. */
699 #define CCL_INVALID_CMD \
701 ccl->status = CCL_STAT_INVALID_CMD; \
702 /* The "if (1)" inhibits the warning \
703 "end-of loop code not reached" */ \
704 if (1) goto ccl_error_handler; \
707 /* Encode one character CH to multibyte form and write to the current
708 output buffer. At encoding time, if CH is less than 256, CH is
709 written as is. At decoding time, if CH cannot be regarded as an
710 ASCII character, write it in multibyte form. */
711 #define CCL_WRITE_CHAR(ch) \
715 if (conversion_mode == CCL_MODE_ENCODING) \
719 if (ccl->eol_type == CCL_CODING_EOL_CRLF) \
721 Dynarr_add (destination, '\r'); \
722 Dynarr_add (destination, '\n'); \
724 else if (ccl->eol_type == CCL_CODING_EOL_CR) \
725 Dynarr_add (destination, '\r'); \
727 Dynarr_add (destination, '\n'); \
729 else if ((ch) < 0x100) \
731 Dynarr_add (destination, ch); \
735 Bufbyte work[MAX_EMCHAR_LEN]; \
737 len = non_ascii_set_charptr_emchar (work, ch); \
738 Dynarr_add_many (destination, work, len); \
743 if (!CHAR_MULTIBYTE_P(ch)) \
745 Dynarr_add (destination, ch); \
749 Bufbyte work[MAX_EMCHAR_LEN]; \
751 len = non_ascii_set_charptr_emchar (work, ch); \
752 Dynarr_add_many (destination, work, len); \
757 /* Write a string at ccl_prog[IC] of length LEN to the current output
758 buffer. But this macro treat this string as a binary. Therefore,
759 cannot handle a multibyte string except for Control-1 characters. */
760 #define CCL_WRITE_STRING(len) \
762 Bufbyte work[MAX_EMCHAR_LEN]; \
766 else if (conversion_mode == CCL_MODE_ENCODING) \
768 for (i = 0; i < (len); i++) \
770 ch = ((XINT (ccl_prog[ic + (i / 3)])) \
771 >> ((2 - (i % 3)) * 8)) & 0xFF; \
774 if (ccl->eol_type == CCL_CODING_EOL_CRLF) \
776 Dynarr_add (destination, '\r'); \
777 Dynarr_add (destination, '\n'); \
779 else if (ccl->eol_type == CCL_CODING_EOL_CR) \
780 Dynarr_add (destination, '\r'); \
782 Dynarr_add (destination, '\n'); \
786 Dynarr_add (destination, ch); \
790 bytes = non_ascii_set_charptr_emchar (work, ch); \
791 Dynarr_add_many (destination, work, len); \
797 for (i = 0; i < (len); i++) \
799 ch = ((XINT (ccl_prog[ic + (i / 3)])) \
800 >> ((2 - (i % 3)) * 8)) & 0xFF; \
801 if (!CHAR_MULTIBYTE_P(ch)) \
803 Dynarr_add (destination, ch); \
807 bytes = non_ascii_set_charptr_emchar (work, ch); \
808 Dynarr_add_many (destination, work, len); \
814 /* Read one byte from the current input buffer into Rth register. */
815 #define CCL_READ_CHAR(r) \
823 if (ccl->last_block) \
829 CCL_SUSPEND (CCL_STAT_SUSPEND_BY_SRC); \
833 /* Set C to the character code made from CHARSET and CODE. This is
834 like MAKE_CHAR but check the validity of CHARSET and CODE. If they
835 are not valid, set C to (CODE & 0xFF) because that is usually the
836 case that CCL_ReadMultibyteChar2 read an invalid code and it set
837 CODE to that invalid byte. */
839 /* On XEmacs, TranslateCharacter is not supported. Thus, this
840 macro is not used. */
842 #define CCL_MAKE_CHAR(charset, code, c) \
844 if ((charset) == CHARSET_ASCII) \
845 (c) = (code) & 0xFF; \
846 else if (CHARSET_DEFINED_P (charset) \
847 && ((code) & 0x7F) >= 32 \
848 && ((code) < 256 || ((code >> 7) & 0x7F) >= 32)) \
850 int c1 = (code) & 0x7F, c2 = 0; \
853 c2 = c1, c1 = ((code) >> 7) & 0x7F; \
854 (c) = MAKE_CHAR (charset, c1, c2); \
857 (c) = (code) & 0xFF; \
861 /* Execute CCL code on SRC_BYTES length text at SOURCE. The resulting
862 text goes to a place pointed by DESTINATION, the length of which
863 should not exceed DST_BYTES. The bytes actually processed is
864 returned as *CONSUMED. The return value is the length of the
865 resulting text. As a side effect, the contents of CCL registers
866 are updated. If SOURCE or DESTINATION is NULL, only operations on
867 registers are permitted. */
870 #define CCL_DEBUG_BACKTRACE_LEN 256
871 int ccl_backtrace_table[CCL_BACKTRACE_TABLE];
872 int ccl_backtrace_idx;
875 struct ccl_prog_stack {
876 Lisp_Object *ccl_prog; /* Pointer to an array of CCL code. */
877 int ic; /* Instruction Counter. */
880 /* For the moment, we only support depth 256 of stack. */
881 static struct ccl_prog_stack ccl_prog_stack_struct[256];
884 ccl_driver(struct ccl_program *ccl,
885 const unsigned char *source,
886 unsigned_char_dynarr * destination,
887 int src_bytes, int *consumed, int conversion_mode)
889 register int *reg = ccl->reg;
890 register int ic = ccl->ic;
891 register int code = -1;
892 register int field1, field2;
893 register Lisp_Object *ccl_prog = ccl->prog;
894 const unsigned char *src = source, *src_end = src + src_bytes;
897 int stack_idx = ccl->stack_idx;
898 /* Instruction counter of the current CCL code. */
901 if (ic >= ccl->eof_ic)
902 ic = CCL_HEADER_MAIN;
904 if (ccl->buf_magnification == 0) /* We can't produce any bytes. */
907 /* Set mapping stack pointer. */
908 mapping_stack_pointer = mapping_stack;
911 ccl_backtrace_idx = 0;
917 ccl_backtrace_table[ccl_backtrace_idx++] = ic;
918 if (ccl_backtrace_idx >= CCL_DEBUG_BACKTRACE_LEN)
919 ccl_backtrace_idx = 0;
920 ccl_backtrace_table[ccl_backtrace_idx] = 0;
923 if (!NILP(Vquit_flag) && NILP(Vinhibit_quit)) {
924 /* We can't just signal Qquit, instead break the loop as if
925 the whole data is processed. Don't reset Vquit_flag, it
926 must be handled later at a safer place. */
928 src = source + src_bytes;
929 ccl->status = CCL_STAT_QUIT;
934 code = XINT(ccl_prog[ic]);
937 field2 = (code & 0xFF) >> 5;
940 #define RRR (field1 & 7)
941 #define Rrr ((field1 >> 3) & 7)
943 #define EXCMD (field1 >> 6)
945 switch (code & 0x1F) {
946 case CCL_SetRegister: /* 00000000000000000RRRrrrXXXXX */
950 case CCL_SetShortConst: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
954 case CCL_SetConst: /* 00000000000000000000rrrXXXXX */
955 reg[rrr] = XINT(ccl_prog[ic]);
959 case CCL_SetArray: /* CCCCCCCCCCCCCCCCCCCCRRRrrrXXXXX */
963 reg[rrr] = XINT(ccl_prog[ic + i]);
967 case CCL_Jump: /* A--D--D--R--E--S--S-000XXXXX */
971 case CCL_JumpCond: /* A--D--D--R--E--S--S-rrrXXXXX */
976 case CCL_WriteRegisterJump: /* A--D--D--R--E--S--S-rrrXXXXX */
982 case CCL_WriteRegisterReadJump: /* A--D--D--R--E--S--S-rrrXXXXX */
986 CCL_READ_CHAR(reg[rrr]);
990 case CCL_WriteConstJump: /* A--D--D--R--E--S--S-000XXXXX */
991 i = XINT(ccl_prog[ic]);
996 case CCL_WriteConstReadJump: /* A--D--D--R--E--S--S-rrrXXXXX */
997 i = XINT(ccl_prog[ic]);
1000 CCL_READ_CHAR(reg[rrr]);
1004 case CCL_WriteStringJump: /* A--D--D--R--E--S--S-000XXXXX */
1005 j = XINT(ccl_prog[ic]);
1007 CCL_WRITE_STRING(j);
1011 case CCL_WriteArrayReadJump: /* A--D--D--R--E--S--S-rrrXXXXX */
1013 j = XINT(ccl_prog[ic]);
1015 i = XINT(ccl_prog[ic + 1 + i]);
1019 CCL_READ_CHAR(reg[rrr]);
1020 ic += ADDR - (j + 2);
1023 case CCL_ReadJump: /* A--D--D--R--E--S--S-rrrYYYYY */
1024 CCL_READ_CHAR(reg[rrr]);
1028 case CCL_ReadBranch: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
1029 CCL_READ_CHAR(reg[rrr]);
1030 /* fall through ... */
1031 case CCL_Branch: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
1032 if (reg[rrr] < field1)
1033 ic += XINT(ccl_prog[ic + reg[rrr]]);
1035 ic += XINT(ccl_prog[ic + field1]);
1038 case CCL_ReadRegister: /* CCCCCCCCCCCCCCCCCCCCrrXXXXX */
1040 CCL_READ_CHAR(reg[rrr]);
1043 code = XINT(ccl_prog[ic]);
1046 field2 = (code & 0xFF) >> 5;
1050 case CCL_WriteExprConst: /* 1:00000OPERATION000RRR000XXXXX */
1053 j = XINT(ccl_prog[ic]);
1055 jump_address = ic + 1;
1058 case CCL_WriteRegister: /* CCCCCCCCCCCCCCCCCCCrrrXXXXX */
1064 code = XINT(ccl_prog[ic]);
1067 field2 = (code & 0xFF) >> 5;
1071 case CCL_WriteExprRegister: /* 1:00000OPERATIONRrrRRR000XXXXX */
1079 case CCL_Call: /* 1:CCCCCCCCCCCCCCCCCCCCFFFXXXXX */
1084 /* If FFF is nonzero, the CCL program ID is in the
1087 prog_id = XINT(ccl_prog[ic]);
1092 if (stack_idx >= 256
1095 XVECTOR(Vccl_program_table)->size
1097 XVECTOR(Vccl_program_table)->
1098 contents[prog_id], !VECTORP(slot))
1099 || !VECTORP(XVECTOR(slot)->contents[1])) {
1100 if (stack_idx > 0) {
1102 ccl_prog_stack_struct[0].
1104 ic = ccl_prog_stack_struct[0].
1110 ccl_prog_stack_struct[stack_idx].ccl_prog =
1112 ccl_prog_stack_struct[stack_idx].ic = ic;
1115 XVECTOR(XVECTOR(slot)->contents[1])->
1117 ic = CCL_HEADER_MAIN;
1121 case CCL_WriteConstString: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
1123 CCL_WRITE_CHAR(field1);
1125 CCL_WRITE_STRING(field1);
1126 ic += (field1 + 2) / 3;
1130 case CCL_WriteArray: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
1133 j = XINT(ccl_prog[ic + i]);
1139 case CCL_End: /* 0000000000000000000000XXXXX */
1140 if (stack_idx > 0) {
1143 ccl_prog_stack_struct[stack_idx].ccl_prog;
1144 ic = ccl_prog_stack_struct[stack_idx].ic;
1149 /* ccl->ic should points to this command code again to
1150 suppress further processing. */
1154 case CCL_ExprSelfConst: /* 00000OPERATION000000rrrXXXXX */
1155 i = XINT(ccl_prog[ic]);
1160 case CCL_ExprSelfReg: /* 00000OPERATION000RRRrrrXXXXX */
1201 reg[7] = reg[rrr] & 0xFF;
1205 reg[7] = reg[rrr] % i;
1209 reg[rrr] = reg[rrr] < i;
1212 reg[rrr] = reg[rrr] > i;
1215 reg[rrr] = reg[rrr] == i;
1218 reg[rrr] = reg[rrr] <= i;
1221 reg[rrr] = reg[rrr] >= i;
1224 reg[rrr] = reg[rrr] != i;
1231 case CCL_SetExprConst: /* 00000OPERATION000RRRrrrXXXXX */
1233 j = XINT(ccl_prog[ic]);
1235 jump_address = ++ic;
1238 case CCL_SetExprReg: /* 00000OPERATIONRrrRRRrrrXXXXX */
1245 case CCL_ReadJumpCondExprConst: /* A--D--D--R--E--S--S-rrrXXXXX */
1246 CCL_READ_CHAR(reg[rrr]);
1247 case CCL_JumpCondExprConst: /* A--D--D--R--E--S--S-rrrXXXXX */
1249 op = XINT(ccl_prog[ic]);
1250 jump_address = ic++ + ADDR;
1251 j = XINT(ccl_prog[ic]);
1256 case CCL_ReadJumpCondExprReg: /* A--D--D--R--E--S--S-rrrXXXXX */
1257 CCL_READ_CHAR(reg[rrr]);
1258 case CCL_JumpCondExprReg:
1260 op = XINT(ccl_prog[ic]);
1261 jump_address = ic++ + ADDR;
1262 j = reg[XINT(ccl_prog[ic])];
1299 reg[rrr] = (i << 8) | j;
1327 case CCL_DECODE_SJIS:
1328 /* DECODE_SJIS set MSB for internal format
1329 as opposed to Emacs. */
1330 DECODE_SJIS(i, j, reg[rrr], reg[7]);
1334 case CCL_ENCODE_SJIS:
1335 /* ENCODE_SJIS assumes MSB of SJIS-char is set
1336 as opposed to Emacs. */
1337 ENCODE_SJIS(i | 0x80, j | 0x80, reg[rrr],
1344 if (code == CCL_WriteExprConst
1345 || code == CCL_WriteExprRegister) {
1349 } else if (!reg[rrr])
1355 case CCL_ReadMultibyteChar2:
1359 if (src >= src_end) {
1361 goto ccl_read_multibyte_character_suspend;
1368 reg[RRR] = LEADING_BYTE_ASCII;
1369 } else if (i <= MAX_LEADING_BYTE_OFFICIAL_1) {
1371 goto ccl_read_multibyte_character_suspend;
1373 reg[rrr] = (*src++ & 0x7F);
1374 } else if (i <= MAX_LEADING_BYTE_OFFICIAL_2) {
1375 if ((src + 1) >= src_end)
1376 goto ccl_read_multibyte_character_suspend;
1378 i = (*src++ & 0x7F);
1379 reg[rrr] = ((i << 7) | (*src & 0x7F));
1381 } else if (i == PRE_LEADING_BYTE_PRIVATE_1) {
1382 if ((src + 1) >= src_end)
1383 goto ccl_read_multibyte_character_suspend;
1385 reg[rrr] = (*src++ & 0x7F);
1386 } else if (i == PRE_LEADING_BYTE_PRIVATE_2) {
1387 if ((src + 2) >= src_end)
1388 goto ccl_read_multibyte_character_suspend;
1390 i = (*src++ & 0x7F);
1391 reg[rrr] = ((i << 7) | (*src & 0x7F));
1394 /* INVALID CODE. Return a single byte character. */
1395 reg[RRR] = LEADING_BYTE_ASCII;
1400 ccl_read_multibyte_character_suspend:
1402 if (ccl->last_block) {
1406 CCL_SUSPEND(CCL_STAT_SUSPEND_BY_SRC);
1410 case CCL_WriteMultibyteChar2: {
1415 tmp = CHARSET_BY_LEADING_BYTE(i);
1417 if (i == LEADING_BYTE_ASCII) {
1418 i = reg[rrr] & 0xFF;
1419 } else if (XCHARSET_DIMENSION(tmp) == 1) {
1421 FIELD2_TO_OFFICIAL_LEADING_BYTE)
1423 | (reg[rrr] & 0x7F));
1424 } else if (i < MAX_LEADING_BYTE_OFFICIAL_2) {
1426 FIELD1_TO_OFFICIAL_LEADING_BYTE)
1430 FIELD1_TO_PRIVATE_LEADING_BYTE) <<
1437 case CCL_TranslateCharacter:
1439 /* XEmacs does not have translate_char, and its
1440 equivalent nor. We do nothing on this operation. */
1441 CCL_MAKE_CHAR(reg[RRR], reg[rrr], i);
1442 op = translate_char(GET_TRANSLATION_TABLE
1443 (reg[Rrr]), i, -1, 0, 0);
1444 SPLIT_CHAR(op, reg[RRR], i, j);
1452 case CCL_TranslateCharacterConstTbl:
1454 /* XEmacs does not have translate_char, and its
1455 equivalent nor. We do nothing on this operation. */
1456 op = XINT(ccl_prog[ic]); /* table */
1458 CCL_MAKE_CHAR(reg[RRR], reg[rrr], i);
1459 op = translate_char(GET_TRANSLATION_TABLE(op),
1461 SPLIT_CHAR(op, reg[RRR], i, j);
1469 case CCL_IterateMultipleMap:
1471 Lisp_Object map, content, attrib, value;
1472 int point, size, fin_ic;
1474 j = XINT(ccl_prog[ic++]); /* number of maps. */
1477 if ((j > reg[RRR]) && (j >= 0)) {
1486 for (; i < j; i++) {
1490 (Vcode_conversion_map_vector)->
1492 point = XINT(ccl_prog[ic++]);
1497 (Vcode_conversion_map_vector)->
1500 /* Check map validity. */
1506 size = XVECTOR(map)->size;
1511 XVECTOR(map)->contents[0];
1514 [STARTPOINT VAL1 VAL2 ...] or
1515 [t ELEMENT STARTPOINT ENDPOINT] */
1516 if (INTP(content)) {
1517 point = XUINT(content);
1518 point = op - point + 1;
1526 } else if (EQ(content, Qt)) {
1549 else if (INTP(content)) {
1554 } else if (EQ(content, Qt)
1559 } else if (CONSP(content)) {
1560 attrib = XCAR(content);
1561 value = XCDR(content);
1566 reg[rrr] = XUINT(value);
1568 } else if (SYMBOLP(content))
1569 CCL_CALL_FOR_MAP_INSTRUCTION
1580 case CCL_MapMultiple:
1582 Lisp_Object map, content, attrib, value;
1583 int point, size, map_vector_size;
1584 int map_set_rest_length, fin_ic;
1585 int current_ic = this_ic;
1587 /* inhibit recursive call on MapMultiple. */
1588 if (stack_idx_of_map_multiple > 0) {
1589 if (stack_idx_of_map_multiple <=
1591 stack_idx_of_map_multiple
1593 mapping_stack_pointer =
1598 mapping_stack_pointer =
1600 stack_idx_of_map_multiple = 0;
1602 map_set_rest_length = XINT(ccl_prog[ic++]); /* number of maps and separators. */
1603 fin_ic = ic + map_set_rest_length;
1606 if ((map_set_rest_length > reg[RRR])
1607 && (reg[RRR] >= 0)) {
1610 map_set_rest_length -= i;
1614 mapping_stack_pointer =
1619 if (mapping_stack_pointer <=
1620 (mapping_stack + 1)) {
1621 /* Set up initial state. */
1622 mapping_stack_pointer =
1624 PUSH_MAPPING_STACK(0, op);
1627 /* Recover after calling other ccl program. */
1631 (map_set_rest_length,
1634 (map_set_rest_length,
1638 /* Regard it as Qnil. */
1642 map_set_rest_length--;
1645 /* Regard it as Qt. */
1649 map_set_rest_length--;
1652 /* Regard it as Qlambda. */
1654 i += map_set_rest_length;
1656 map_set_rest_length;
1657 map_set_rest_length = 0;
1660 /* Regard it as normal mapping. */
1661 i += map_set_rest_length;
1663 map_set_rest_length;
1665 (map_set_rest_length,
1672 (Vcode_conversion_map_vector)->size;
1675 for (; map_set_rest_length > 0;
1677 map_set_rest_length--) {
1681 /* +1 is for including separator. */
1684 if (mapping_stack_pointer >= mapping_stack + countof(mapping_stack))
1687 (map_set_rest_length
1701 (Vcode_conversion_map_vector)
1704 /* Check map validity. */
1720 [STARTPOINT VAL1 VAL2 ...] or
1721 [t ELEMENT STARTPOINT ENDPOINT] */
1722 if (INTP(content)) {
1741 if (EQ(content, Qt))
1772 if (INTP(content)) {
1775 i += map_set_rest_length - 1;
1780 (map_set_rest_length,
1782 map_set_rest_length++;
1800 i += map_set_rest_length - 1;
1805 (map_set_rest_length,
1807 map_set_rest_length++;
1809 if (EQ(content, Qt))
1816 i += map_set_rest_length;
1818 map_set_rest_length;
1823 if (mapping_stack_pointer >= mapping_stack + countof(mapping_stack))
1826 (map_set_rest_length,
1829 (map_set_rest_length,
1831 stack_idx_of_map_multiple
1835 CCL_CALL_FOR_MAP_INSTRUCTION
1841 if (mapping_stack_pointer <=
1842 (mapping_stack + 1))
1845 (map_set_rest_length,
1847 i += map_set_rest_length;
1848 ic += map_set_rest_length;
1850 (map_set_rest_length,
1861 Lisp_Object map, attrib, value, content;
1863 j = XINT(ccl_prog[ic++]); /* map_id */
1867 (Vcode_conversion_map_vector)->
1874 (Vcode_conversion_map_vector)->
1881 if (!VECTORP(map)) {
1885 size = XVECTOR(map)->size;
1887 XUINT(XVECTOR(map)->contents[0]);
1888 point = op - point + 1;
1891 (!((point >= 1) && (point < size))))
1900 else if (INTP(content))
1903 else if (EQ(content, Qt)) ;
1904 else if (CONSP(content)) {
1905 attrib = XCAR(content);
1906 value = XCDR(content);
1910 reg[rrr] = XUINT(value);
1912 } else if (SYMBOLP(content))
1913 CCL_CALL_FOR_MAP_INSTRUCTION
1933 /* We can insert an error message only if DESTINATION is
1934 specified and we still have a room to store the message
1938 switch (ccl->status) {
1939 case CCL_STAT_INVALID_CMD:
1940 sz = snprintf(msg, sizeof(msg),
1941 "\nCCL: Invalid command %x (ccl_code = %x) at %d.",
1942 code & 0x1F, code, this_ic);
1943 assert(sz >= 0 && sz < sizeof(msg));
1946 int i = ccl_backtrace_idx - 1;
1949 Dynarr_add_many(destination,
1950 (unsigned char *)msg,
1953 for (j = 0; j < CCL_DEBUG_BACKTRACE_LEN;
1956 i = CCL_DEBUG_BACKTRACE_LEN - 1;
1957 if (ccl_backtrace_table[i] == 0)
1959 sz = snprintf(msg, sizeof(msg), " %d",
1960 ccl_backtrace_table[i]);
1961 assert(sz >= 0 && sz < sizeof(msg));
1962 Dynarr_add_many(destination,
1963 (unsigned char *)msg,
1972 sz = snprintf(msg, sizeof(msg), "\nCCL: Exited.");
1973 assert(sz >= 0 && sz < sizeof(msg));
1977 sz = snprintf(msg, sizeof(msg), "\nCCL: Unknown error type (%d).",
1979 assert(sz >= 0 && sz < sizeof(msg));
1982 Dynarr_add_many(destination, (unsigned char *)msg, strlen(msg));
1987 ccl->stack_idx = stack_idx;
1988 ccl->prog = ccl_prog;
1990 *consumed = src - source;
1993 return Dynarr_length(destination);
1996 /* Resolve symbols in the specified CCL code (Lisp vector). This
1997 function converts symbols of code conversion maps and character
1998 translation tables embedded in the CCL code into their ID numbers.
2000 The return value is a vector (CCL itself or a new vector in which
2001 all symbols are resolved), Qt if resolving of some symbol failed,
2002 or nil if CCL contains invalid data. */
2004 static Lisp_Object resolve_symbol_ccl_program(Lisp_Object ccl)
2006 int i, veclen, unresolved = 0;
2007 Lisp_Object result, contents, val;
2010 veclen = XVECTOR(result)->size;
2012 for (i = 0; i < veclen; i++) {
2013 contents = XVECTOR(result)->contents[i];
2016 else if (CONSP(contents)
2017 && SYMBOLP(XCAR(contents))
2018 && SYMBOLP(XCDR(contents))) {
2019 /* This is the new style for embedding symbols. The form is
2020 (SYMBOL . PROPERTY). (get SYMBOL PROPERTY) should give
2023 if (EQ(result, ccl))
2024 result = Fcopy_sequence(ccl);
2026 val = Fget(XCAR(contents), XCDR(contents), Qnil);
2028 XVECTOR(result)->contents[i] = val;
2032 } else if (SYMBOLP(contents)) {
2033 /* This is the old style for embedding symbols. This style
2034 may lead to a bug if, for instance, a translation table
2035 and a code conversion map have the same name. */
2036 if (EQ(result, ccl))
2037 result = Fcopy_sequence(ccl);
2039 val = Fget(contents, Qcode_conversion_map_id, Qnil);
2041 XVECTOR(result)->contents[i] = val;
2043 val = Fget(contents, Qccl_program_idx, Qnil);
2045 XVECTOR(result)->contents[i] = val;
2054 return (unresolved ? Qt : result);
2057 /* Return the compiled code (vector) of CCL program CCL_PROG.
2058 CCL_PROG is a name (symbol) of the program or already compiled
2059 code. If necessary, resolve symbols in the compiled code to index
2060 numbers. If we failed to get the compiled code or to resolve
2061 symbols, return Qnil. */
2063 static Lisp_Object ccl_get_compiled_code(Lisp_Object ccl_prog)
2065 Lisp_Object val, slot;
2067 if (VECTORP(ccl_prog)) {
2068 val = resolve_symbol_ccl_program(ccl_prog);
2069 return (VECTORP(val) ? val : Qnil);
2071 if (!SYMBOLP(ccl_prog))
2074 val = Fget(ccl_prog, Qccl_program_idx, Qnil);
2076 || XINT(val) >= XVECTOR_LENGTH(Vccl_program_table))
2078 slot = XVECTOR_DATA(Vccl_program_table)[XINT(val)];
2080 || XVECTOR(slot)->size != 3 || !VECTORP(XVECTOR_DATA(slot)[1]))
2082 if (NILP(XVECTOR_DATA(slot)[2])) {
2083 val = resolve_symbol_ccl_program(XVECTOR_DATA(slot)[1]);
2086 XVECTOR_DATA(slot)[1] = val;
2087 XVECTOR_DATA(slot)[2] = Qt;
2089 return XVECTOR_DATA(slot)[1];
2092 /* Setup fields of the structure pointed by CCL appropriately for the
2093 execution of CCL program CCL_PROG. CCL_PROG is the name (symbol)
2094 of the CCL program or the already compiled code (vector).
2095 Return 0 if we succeed this setup, else return -1.
2097 If CCL_PROG is nil, we just reset the structure pointed by CCL. */
2098 int setup_ccl_program(struct ccl_program *ccl, Lisp_Object ccl_prog)
2102 if (!NILP(ccl_prog)) {
2103 ccl_prog = ccl_get_compiled_code(ccl_prog);
2104 if (!VECTORP(ccl_prog))
2106 ccl->size = XVECTOR_LENGTH(ccl_prog);
2107 ccl->prog = XVECTOR_DATA(ccl_prog);
2108 ccl->eof_ic = XINT(XVECTOR_DATA(ccl_prog)[CCL_HEADER_EOF]);
2109 ccl->buf_magnification =
2110 XINT(XVECTOR_DATA(ccl_prog)[CCL_HEADER_BUF_MAG]);
2112 ccl->ic = CCL_HEADER_MAIN;
2113 for (i = 0; i < 8; i++)
2115 ccl->last_block = 0;
2116 ccl->private_state = 0;
2119 ccl->eol_type = CCL_CODING_EOL_LF;
2125 DEFUN("ccl-program-p", Fccl_program_p, 1, 1, 0, /*
2126 Return t if OBJECT is a CCL program name or a compiled CCL program code.
2127 See the documentation of `define-ccl-program' for the detail of CCL program.
2133 if (VECTORP(object)) {
2134 val = resolve_symbol_ccl_program(object);
2135 return (VECTORP(val) ? Qt : Qnil);
2137 if (!SYMBOLP(object))
2140 val = Fget(object, Qccl_program_idx, Qnil);
2141 return ((!NATNUMP(val)
2142 || XINT(val) >= XVECTOR_LENGTH(Vccl_program_table))
2146 DEFUN("ccl-execute", Fccl_execute, 2, 2, 0, /*
2147 Execute CCL-PROGRAM with registers initialized by REGISTERS.
2149 CCL-PROGRAM is a CCL program name (symbol)
2150 or a compiled code generated by `ccl-compile' (for backward compatibility,
2151 in this case, the overhead of the execution is bigger than the former case).
2152 No I/O commands should appear in CCL-PROGRAM.
2154 REGISTERS is a vector of [R0 R1 ... R7] where RN is an initial value
2157 As side effect, each element of REGISTERS holds the value of
2158 corresponding register after the execution.
2160 See the documentation of `define-ccl-program' for the detail of CCL program.
2164 struct ccl_program ccl;
2167 if (setup_ccl_program(&ccl, ccl_prog) < 0)
2168 error("Invalid CCL program");
2171 if (XVECTOR_LENGTH(reg) != 8)
2172 error("Length of vector REGISTERS is not 8");
2174 for (i = 0; i < 8; i++)
2175 ccl.reg[i] = (INTP(XVECTOR_DATA(reg)[i])
2176 ? XINT(XVECTOR_DATA(reg)[i])
2179 ccl_driver(&ccl, (const unsigned char *)0,
2180 (unsigned_char_dynarr *) 0, 0, (int *)0, CCL_MODE_ENCODING);
2182 if (ccl.status != CCL_STAT_SUCCESS)
2183 error("Error in CCL program at %dth code", ccl.ic);
2185 for (i = 0; i < 8; i++)
2186 XSETINT(XVECTOR(reg)->contents[i], ccl.reg[i]);
2190 DEFUN("ccl-execute-on-string", Fccl_execute_on_string, 3, 4, 0, /*
2191 Execute CCL-PROGRAM with initial STATUS on STRING.
2193 CCL-PROGRAM is a symbol registered by register-ccl-program,
2194 or a compiled code generated by `ccl-compile' (for backward compatibility,
2195 in this case, the execution is slower).
2197 Read buffer is set to STRING, and write buffer is allocated automatically.
2199 STATUS is a vector of [R0 R1 ... R7 IC], where
2200 R0..R7 are initial values of corresponding registers,
2201 IC is the instruction counter specifying from where to start the program.
2202 If R0..R7 are nil, they are initialized to 0.
2203 If IC is nil, it is initialized to head of the CCL program.
2205 If optional 4th arg CONTINUE is non-nil, keep IC on read operation
2206 when read buffer is exhausted, else, IC is always set to the end of
2207 CCL-PROGRAM on exit.
2209 It returns the contents of write buffer as a string,
2210 and as side effect, STATUS is updated.
2212 See the documentation of `define-ccl-program' for the detail of CCL program.
2214 (ccl_prog, status, string, continue_))
2217 struct ccl_program ccl;
2219 unsigned_char_dynarr *outbuf;
2220 struct gcpro gcpro1, gcpro2;
2222 if (setup_ccl_program(&ccl, ccl_prog) < 0)
2223 error("Invalid CCL program");
2225 CHECK_VECTOR(status);
2226 if (XVECTOR(status)->size != 9)
2227 error("Length of vector STATUS is not 9");
2228 CHECK_STRING(string);
2230 GCPRO2(status, string);
2232 for (i = 0; i < 8; i++) {
2233 if (NILP(XVECTOR_DATA(status)[i]))
2234 XSETINT(XVECTOR_DATA(status)[i], 0);
2235 if (INTP(XVECTOR_DATA(status)[i]))
2236 ccl.reg[i] = XINT(XVECTOR_DATA(status)[i]);
2238 if (INTP(XVECTOR(status)->contents[i])) {
2239 i = XINT(XVECTOR_DATA(status)[8]);
2240 if (ccl.ic < i && i < ccl.size)
2243 outbuf = Dynarr_new(unsigned_char);
2244 ccl.last_block = NILP(continue_);
2245 produced = ccl_driver(&ccl, XSTRING_DATA(string), outbuf,
2246 XSTRING_LENGTH(string),
2247 (int *)0, CCL_MODE_DECODING);
2248 for (i = 0; i < 8; i++)
2249 XSETINT(XVECTOR_DATA(status)[i], ccl.reg[i]);
2250 XSETINT(XVECTOR_DATA(status)[8], ccl.ic);
2253 val = make_string(Dynarr_atp(outbuf, 0), produced);
2254 Dynarr_free(outbuf);
2256 if (ccl.status == CCL_STAT_SUSPEND_BY_DST)
2257 error("Output buffer for the CCL programs overflow");
2258 if (ccl.status != CCL_STAT_SUCCESS
2259 && ccl.status != CCL_STAT_SUSPEND_BY_SRC)
2260 error("Error in CCL program at %dth code", ccl.ic);
2265 DEFUN("register-ccl-program", Fregister_ccl_program, 2, 2, 0, /*
2266 Register CCL program CCL-PROG as NAME in `ccl-program-table'.
2267 CCL-PROG should be a compiled CCL program (vector), or nil.
2268 If it is nil, just reserve NAME as a CCL program name.
2269 Return index number of the registered CCL program.
2273 int len = XVECTOR_LENGTH(Vccl_program_table);
2275 Lisp_Object resolved;
2279 if (!NILP(ccl_prog)) {
2280 CHECK_VECTOR(ccl_prog);
2281 resolved = resolve_symbol_ccl_program(ccl_prog);
2282 if (!NILP(resolved)) {
2283 ccl_prog = resolved;
2288 for (idx = 0; idx < len; idx++) {
2291 slot = XVECTOR_DATA(Vccl_program_table)[idx];
2293 /* This is the first unused slot. Register NAME here. */
2296 if (EQ(name, XVECTOR_DATA(slot)[0])) {
2297 /* Update this slot. */
2298 XVECTOR_DATA(slot)[1] = ccl_prog;
2299 XVECTOR_DATA(slot)[2] = resolved;
2300 return make_int(idx);
2305 /* Extend the table. */
2306 Lisp_Object new_table;
2309 new_table = Fmake_vector(make_int(len * 2), Qnil);
2310 for (j = 0; j < len; j++)
2311 XVECTOR_DATA(new_table)[j]
2312 = XVECTOR_DATA(Vccl_program_table)[j];
2313 Vccl_program_table = new_table;
2319 elt = Fmake_vector(make_int(3), Qnil);
2320 XVECTOR_DATA(elt)[0] = name;
2321 XVECTOR_DATA(elt)[1] = ccl_prog;
2322 XVECTOR_DATA(elt)[2] = resolved;
2323 XVECTOR_DATA(Vccl_program_table)[idx] = elt;
2326 Fput(name, Qccl_program_idx, make_int(idx));
2327 return make_int(idx);
2330 /* Register code conversion map.
2331 A code conversion map consists of numbers, Qt, Qnil, and Qlambda.
2332 The first element is start code point.
2333 The rest elements are mapped numbers.
2334 Symbol t means to map to an original number before mapping.
2335 Symbol nil means that the corresponding element is empty.
2336 Symbol lambda means to terminate mapping here.
2339 DEFUN("register-code-conversion-map", Fregister_code_conversion_map, 2, 2, 0, /*
2340 Register SYMBOL as code conversion map MAP.
2341 Return index number of the registered map.
2345 int len = XVECTOR_LENGTH(Vcode_conversion_map_vector);
2349 CHECK_SYMBOL(symbol);
2352 for (i = 0; i < len; i++) {
2353 Lisp_Object slot = XVECTOR_DATA(Vcode_conversion_map_vector)[i];
2358 if (EQ(symbol, XCAR(slot))) {
2361 Fput(symbol, Qcode_conversion_map, map);
2362 Fput(symbol, Qcode_conversion_map_id, idx);
2368 Lisp_Object new_vector = Fmake_vector(make_int(len * 2), Qnil);
2371 for (j = 0; j < len; j++)
2372 XVECTOR_DATA(new_vector)[j]
2373 = XVECTOR_DATA(Vcode_conversion_map_vector)[j];
2374 Vcode_conversion_map_vector = new_vector;
2378 Fput(symbol, Qcode_conversion_map, map);
2379 Fput(symbol, Qcode_conversion_map_id, idx);
2380 XVECTOR_DATA(Vcode_conversion_map_vector)[i] = Fcons(symbol, map);
2384 void syms_of_mule_ccl(void)
2386 DEFSUBR(Fccl_program_p);
2387 DEFSUBR(Fccl_execute);
2388 DEFSUBR(Fccl_execute_on_string);
2389 DEFSUBR(Fregister_ccl_program);
2390 DEFSUBR(Fregister_code_conversion_map);
2393 void vars_of_mule_ccl(void)
2395 staticpro(&Vccl_program_table);
2396 Vccl_program_table = Fmake_vector(make_int(32), Qnil);
2398 defsymbol(&Qccl_program, "ccl-program");
2399 defsymbol(&Qccl_program_idx, "ccl-program-idx");
2400 defsymbol(&Qcode_conversion_map, "code-conversion-map");
2401 defsymbol(&Qcode_conversion_map_id, "code-conversion-map-id");
2403 DEFVAR_LISP("code-conversion-map-vector", &Vcode_conversion_map_vector /*
2404 Vector of code conversion maps.
2406 Vcode_conversion_map_vector = Fmake_vector(make_int(16), Qnil);
2408 DEFVAR_LISP("font-ccl-encoder-alist", &Vfont_ccl_encoder_alist /*
2409 Alist of fontname patterns vs corresponding CCL program.
2410 Each element looks like (REGEXP . CCL-CODE),
2411 where CCL-CODE is a compiled CCL program.
2412 When a font whose name matches REGEXP is used for displaying a character,
2413 CCL-CODE is executed to calculate the code point in the font
2414 from the charset number and position code(s) of the character which are set
2415 in CCL registers R0, R1, and R2 before the execution.
2416 The code point in the font is set in CCL registers R1 and R2
2417 when the execution terminated.
2418 If the font is single-byte font, the register R2 is not used.
2420 Vfont_ccl_encoder_alist = Qnil;