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 GNU Emacs.
7 GNU Emacs 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 2, or (at your option)
12 GNU Emacs 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 GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* Synched up with : FSF Emacs 21.0.90 except TranslateCharacter */
34 #include "character.h"
36 #include "file-coding.h"
42 #endif /* not emacs */
44 /* This contains all code conversion map available to CCL. */
45 Lisp_Object Vcode_conversion_map_vector;
47 /* Alist of fontname patterns vs corresponding CCL program. */
48 Lisp_Object Vfont_ccl_encoder_alist;
50 /* This symbol is a property which associates with ccl program vector.
51 Ex: (get 'ccl-big5-encoder 'ccl-program) returns ccl program vector. */
52 Lisp_Object Qccl_program;
54 /* These symbols are properties which associate with code conversion
55 map and their ID respectively. */
56 Lisp_Object Qcode_conversion_map;
57 Lisp_Object Qcode_conversion_map_id;
59 /* Symbols of ccl program have this property, a value of the property
60 is an index for Vccl_program_table. */
61 Lisp_Object Qccl_program_idx;
63 /* Table of registered CCL programs. Each element is a vector of
64 NAME, CCL_PROG, and RESOLVEDP where NAME (symbol) is the name of
65 the program, CCL_PROG (vector) is the compiled code of the program,
66 RESOLVEDP (t or nil) is the flag to tell if symbols in CCL_PROG is
67 already resolved to index numbers or not. */
68 Lisp_Object Vccl_program_table;
70 /* CCL (Code Conversion Language) is a simple language which has
71 operations on one input buffer, one output buffer, and 7 registers.
72 The syntax of CCL is described in `ccl.el'. Emacs Lisp function
73 `ccl-compile' compiles a CCL program and produces a CCL code which
74 is a vector of integers. The structure of this vector is as
75 follows: The 1st element: buffer-magnification, a factor for the
76 size of output buffer compared with the size of input buffer. The
77 2nd element: address of CCL code to be executed when encountered
78 with end of input stream. The 3rd and the remaining elements: CCL
81 /* Header of CCL compiled code */
82 #define CCL_HEADER_BUF_MAG 0
83 #define CCL_HEADER_EOF 1
84 #define CCL_HEADER_MAIN 2
86 /* CCL code is a sequence of 28-bit non-negative integers (i.e. the
87 MSB is always 0), each contains CCL command and/or arguments in the
90 |----------------- integer (28-bit) ------------------|
91 |------- 17-bit ------|- 3-bit --|- 3-bit --|- 5-bit -|
92 |--constant argument--|-register-|-register-|-command-|
93 ccccccccccccccccc RRR rrr XXXXX
95 |------- relative address -------|-register-|-command-|
96 cccccccccccccccccccc rrr XXXXX
98 |------------- constant or other args ----------------|
99 cccccccccccccccccccccccccccc
101 where, `cc...c' is a non-negative integer indicating constant value
102 (the left most `c' is always 0) or an absolute jump address, `RRR'
103 and `rrr' are CCL register number, `XXXXX' is one of the following
108 Each comment fields shows one or more lines for command syntax and
109 the following lines for semantics of the command. In semantics, IC
110 stands for Instruction Counter. */
112 #define CCL_SetRegister 0x00 /* Set register a register value:
113 1:00000000000000000RRRrrrXXXXX
114 ------------------------------
118 #define CCL_SetShortConst 0x01 /* Set register a short constant value:
119 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
120 ------------------------------
121 reg[rrr] = CCCCCCCCCCCCCCCCCCC;
124 #define CCL_SetConst 0x02 /* Set register a constant value:
125 1:00000000000000000000rrrXXXXX
127 ------------------------------
132 #define CCL_SetArray 0x03 /* Set register an element of array:
133 1:CCCCCCCCCCCCCCCCCRRRrrrXXXXX
137 ------------------------------
138 if (0 <= reg[RRR] < CC..C)
139 reg[rrr] = ELEMENT[reg[RRR]];
143 #define CCL_Jump 0x04 /* Jump:
144 1:A--D--D--R--E--S--S-000XXXXX
145 ------------------------------
149 /* Note: If CC..C is greater than 0, the second code is omitted. */
151 #define CCL_JumpCond 0x05 /* Jump conditional:
152 1:A--D--D--R--E--S--S-rrrXXXXX
153 ------------------------------
159 #define CCL_WriteRegisterJump 0x06 /* Write register and jump:
160 1:A--D--D--R--E--S--S-rrrXXXXX
161 ------------------------------
166 #define CCL_WriteRegisterReadJump 0x07 /* Write register, read, and jump:
167 1:A--D--D--R--E--S--S-rrrXXXXX
168 2:A--D--D--R--E--S--S-rrrYYYYY
169 -----------------------------
175 /* Note: If read is suspended, the resumed execution starts from the
176 second code (YYYYY == CCL_ReadJump). */
178 #define CCL_WriteConstJump 0x08 /* Write constant and jump:
179 1:A--D--D--R--E--S--S-000XXXXX
181 ------------------------------
186 #define CCL_WriteConstReadJump 0x09 /* Write constant, read, and jump:
187 1:A--D--D--R--E--S--S-rrrXXXXX
189 3:A--D--D--R--E--S--S-rrrYYYYY
190 -----------------------------
196 /* Note: If read is suspended, the resumed execution starts from the
197 second code (YYYYY == CCL_ReadJump). */
199 #define CCL_WriteStringJump 0x0A /* Write string and jump:
200 1:A--D--D--R--E--S--S-000XXXXX
202 3:0000STRIN[0]STRIN[1]STRIN[2]
204 ------------------------------
205 write_string (STRING, LENGTH);
209 #define CCL_WriteArrayReadJump 0x0B /* Write an array element, read, and jump:
210 1:A--D--D--R--E--S--S-rrrXXXXX
215 N:A--D--D--R--E--S--S-rrrYYYYY
216 ------------------------------
217 if (0 <= reg[rrr] < LENGTH)
218 write (ELEMENT[reg[rrr]]);
219 IC += LENGTH + 2; (... pointing at N+1)
223 /* Note: If read is suspended, the resumed execution starts from the
224 Nth code (YYYYY == CCL_ReadJump). */
226 #define CCL_ReadJump 0x0C /* Read and jump:
227 1:A--D--D--R--E--S--S-rrrYYYYY
228 -----------------------------
233 #define CCL_Branch 0x0D /* Jump by branch table:
234 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
235 2:A--D--D--R--E-S-S[0]000XXXXX
236 3:A--D--D--R--E-S-S[1]000XXXXX
238 ------------------------------
239 if (0 <= reg[rrr] < CC..C)
240 IC += ADDRESS[reg[rrr]];
242 IC += ADDRESS[CC..C];
245 #define CCL_ReadRegister 0x0E /* Read bytes into registers:
246 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
247 2:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
249 ------------------------------
254 #define CCL_WriteExprConst 0x0F /* write result of expression:
255 1:00000OPERATION000RRR000XXXXX
257 ------------------------------
258 write (reg[RRR] OPERATION CONSTANT);
262 /* Note: If the Nth read is suspended, the resumed execution starts
263 from the Nth code. */
265 #define CCL_ReadBranch 0x10 /* Read one byte into a register,
266 and jump by branch table:
267 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
268 2:A--D--D--R--E-S-S[0]000XXXXX
269 3:A--D--D--R--E-S-S[1]000XXXXX
271 ------------------------------
273 if (0 <= reg[rrr] < CC..C)
274 IC += ADDRESS[reg[rrr]];
276 IC += ADDRESS[CC..C];
279 #define CCL_WriteRegister 0x11 /* Write registers:
280 1:CCCCCCCCCCCCCCCCCCCrrrXXXXX
281 2:CCCCCCCCCCCCCCCCCCCrrrXXXXX
283 ------------------------------
289 /* Note: If the Nth write is suspended, the resumed execution
290 starts from the Nth code. */
292 #define CCL_WriteExprRegister 0x12 /* Write result of expression
293 1:00000OPERATIONRrrRRR000XXXXX
294 ------------------------------
295 write (reg[RRR] OPERATION reg[Rrr]);
298 #define CCL_Call 0x13 /* Call the CCL program whose ID is
300 1:CCCCCCCCCCCCCCCCCCCCFFFXXXXX
301 [2:00000000cccccccccccccccccccc]
302 ------------------------------
310 #define CCL_WriteConstString 0x14 /* Write a constant or a string:
311 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
312 [2:0000STRIN[0]STRIN[1]STRIN[2]]
314 -----------------------------
318 write_string (STRING, CC..C);
319 IC += (CC..C + 2) / 3;
322 #define CCL_WriteArray 0x15 /* Write an element of array:
323 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
327 ------------------------------
328 if (0 <= reg[rrr] < CC..C)
329 write (ELEMENT[reg[rrr]]);
333 #define CCL_End 0x16 /* Terminate:
334 1:00000000000000000000000XXXXX
335 ------------------------------
339 /* The following two codes execute an assignment arithmetic/logical
340 operation. The form of the operation is like REG OP= OPERAND. */
342 #define CCL_ExprSelfConst 0x17 /* REG OP= constant:
343 1:00000OPERATION000000rrrXXXXX
345 ------------------------------
346 reg[rrr] OPERATION= CONSTANT;
349 #define CCL_ExprSelfReg 0x18 /* REG1 OP= REG2:
350 1:00000OPERATION000RRRrrrXXXXX
351 ------------------------------
352 reg[rrr] OPERATION= reg[RRR];
355 /* The following codes execute an arithmetic/logical operation. The
356 form of the operation is like REG_X = REG_Y OP OPERAND2. */
358 #define CCL_SetExprConst 0x19 /* REG_X = REG_Y OP constant:
359 1:00000OPERATION000RRRrrrXXXXX
361 ------------------------------
362 reg[rrr] = reg[RRR] OPERATION CONSTANT;
366 #define CCL_SetExprReg 0x1A /* REG1 = REG2 OP REG3:
367 1:00000OPERATIONRrrRRRrrrXXXXX
368 ------------------------------
369 reg[rrr] = reg[RRR] OPERATION reg[Rrr];
372 #define CCL_JumpCondExprConst 0x1B /* Jump conditional according to
373 an operation on constant:
374 1:A--D--D--R--E--S--S-rrrXXXXX
377 -----------------------------
378 reg[7] = reg[rrr] OPERATION CONSTANT;
385 #define CCL_JumpCondExprReg 0x1C /* Jump conditional according to
386 an operation on register:
387 1:A--D--D--R--E--S--S-rrrXXXXX
390 -----------------------------
391 reg[7] = reg[rrr] OPERATION reg[RRR];
398 #define CCL_ReadJumpCondExprConst 0x1D /* Read and jump conditional according
399 to an operation on constant:
400 1:A--D--D--R--E--S--S-rrrXXXXX
403 -----------------------------
405 reg[7] = reg[rrr] OPERATION CONSTANT;
412 #define CCL_ReadJumpCondExprReg 0x1E /* Read and jump conditional according
413 to an operation on register:
414 1:A--D--D--R--E--S--S-rrrXXXXX
417 -----------------------------
419 reg[7] = reg[rrr] OPERATION reg[RRR];
426 #define CCL_Extension 0x1F /* Extended CCL code
427 1:ExtendedCOMMNDRrrRRRrrrXXXXX
430 ------------------------------
431 extended_command (rrr,RRR,Rrr,ARGS)
435 Here after, Extended CCL Instructions.
436 Bit length of extended command is 14.
437 Therefore, the instruction code range is 0..16384(0x3fff).
440 /* Read a multibyte characeter.
441 A code point is stored into reg[rrr]. A charset ID is stored into
444 #define CCL_ReadMultibyteChar2 0x00 /* Read Multibyte Character
445 1:ExtendedCOMMNDRrrRRRrrrXXXXX */
447 /* Write a multibyte character.
448 Write a character whose code point is reg[rrr] and the charset ID
451 #define CCL_WriteMultibyteChar2 0x01 /* Write Multibyte Character
452 1:ExtendedCOMMNDRrrRRRrrrXXXXX */
454 /* Translate a character whose code point is reg[rrr] and the charset
455 ID is reg[RRR] by a translation table whose ID is reg[Rrr].
457 A translated character is set in reg[rrr] (code point) and reg[RRR]
460 #define CCL_TranslateCharacter 0x02 /* Translate a multibyte character
461 1:ExtendedCOMMNDRrrRRRrrrXXXXX */
463 /* Translate a character whose code point is reg[rrr] and the charset
464 ID is reg[RRR] by a translation table whose ID is ARGUMENT.
466 A translated character is set in reg[rrr] (code point) and reg[RRR]
469 #define CCL_TranslateCharacterConstTbl 0x03 /* Translate a multibyte character
470 1:ExtendedCOMMNDRrrRRRrrrXXXXX
471 2:ARGUMENT(Translation Table ID)
474 /* Iterate looking up MAPs for reg[rrr] starting from the Nth (N =
475 reg[RRR]) MAP until some value is found.
477 Each MAP is a Lisp vector whose element is number, nil, t, or
479 If the element is nil, ignore the map and proceed to the next map.
480 If the element is t or lambda, finish without changing reg[rrr].
481 If the element is a number, set reg[rrr] to the number and finish.
483 Detail of the map structure is described in the comment for
484 CCL_MapMultiple below. */
486 #define CCL_IterateMultipleMap 0x10 /* Iterate multiple maps
487 1:ExtendedCOMMNDXXXRRRrrrXXXXX
494 /* Map the code in reg[rrr] by MAPs starting from the Nth (N =
497 MAPs are supplied in the succeeding CCL codes as follows:
499 When CCL program gives this nested structure of map to this command:
502 (MAP-ID121 MAP-ID122 MAP-ID123)
505 (MAP-ID211 (MAP-ID2111) MAP-ID212)
507 the compiled CCL code has this sequence:
508 CCL_MapMultiple (CCL code of this command)
509 16 (total number of MAPs and SEPARATORs)
527 A value of each SEPARATOR follows this rule:
528 MAP-SET := SEPARATOR [(MAP-ID | MAP-SET)]+
529 SEPARATOR := -(number of MAP-IDs and SEPARATORs in the MAP-SET)
531 (*)....Nest level of MAP-SET must not be over than MAX_MAP_SET_LEVEL.
533 When some map fails to map (i.e. it doesn't have a value for
534 reg[rrr]), the mapping is treated as identity.
536 The mapping is iterated for all maps in each map set (set of maps
537 separated by SEPARATOR) except in the case that lambda is
538 encountered. More precisely, the mapping proceeds as below:
540 At first, VAL0 is set to reg[rrr], and it is translated by the
541 first map to VAL1. Then, VAL1 is translated by the next map to
542 VAL2. This mapping is iterated until the last map is used. The
543 result of the mapping is the last value of VAL?. When the mapping
544 process reached to the end of the map set, it moves to the next
545 map set. If the next does not exit, the mapping process terminates,
546 and regard the last value as a result.
548 But, when VALm is mapped to VALn and VALn is not a number, the
549 mapping proceeds as follows:
551 If VALn is nil, the lastest map is ignored and the mapping of VALm
552 proceeds to the next map.
554 In VALn is t, VALm is reverted to reg[rrr] and the mapping of VALm
555 proceeds to the next map.
557 If VALn is lambda, move to the next map set like reaching to the
558 end of the current map set.
560 If VALn is a symbol, call the CCL program refered by it.
561 Then, use reg[rrr] as a mapped value except for -1, -2 and -3.
562 Such special values are regarded as nil, t, and lambda respectively.
564 Each map is a Lisp vector of the following format (a) or (b):
565 (a)......[STARTPOINT VAL1 VAL2 ...]
566 (b)......[t VAL STARTPOINT ENDPOINT],
568 STARTPOINT is an offset to be used for indexing a map,
569 ENDPOINT is a maximum index number of a map,
570 VAL and VALn is a number, nil, t, or lambda.
572 Valid index range of a map of type (a) is:
573 STARTPOINT <= index < STARTPOINT + map_size - 1
574 Valid index range of a map of type (b) is:
575 STARTPOINT <= index < ENDPOINT */
577 #define CCL_MapMultiple 0x11 /* Mapping by multiple code conversion maps
578 1:ExtendedCOMMNDXXXRRRrrrXXXXX
590 #define MAX_MAP_SET_LEVEL 30
598 static tr_stack mapping_stack[MAX_MAP_SET_LEVEL];
599 static tr_stack *mapping_stack_pointer;
601 /* If this variable is non-zero, it indicates the stack_idx
602 of immediately called by CCL_MapMultiple. */
603 static int stack_idx_of_map_multiple;
605 #define PUSH_MAPPING_STACK(restlen, orig) \
607 mapping_stack_pointer->rest_length = (restlen); \
608 mapping_stack_pointer->orig_val = (orig); \
609 mapping_stack_pointer++; \
612 #define POP_MAPPING_STACK(restlen, orig) \
614 mapping_stack_pointer--; \
615 (restlen) = mapping_stack_pointer->rest_length; \
616 (orig) = mapping_stack_pointer->orig_val; \
619 #define CCL_CALL_FOR_MAP_INSTRUCTION(symbol, ret_ic) \
621 struct ccl_program called_ccl; \
622 if (stack_idx >= 256 \
623 || (setup_ccl_program (&called_ccl, (symbol)) != 0)) \
627 ccl_prog = ccl_prog_stack_struct[0].ccl_prog; \
628 ic = ccl_prog_stack_struct[0].ic; \
632 ccl_prog_stack_struct[stack_idx].ccl_prog = ccl_prog; \
633 ccl_prog_stack_struct[stack_idx].ic = (ret_ic); \
635 ccl_prog = called_ccl.prog; \
636 ic = CCL_HEADER_MAIN; \
637 /* The "if (1)" prevents warning \
638 "end-of loop code not reached" */ \
639 if (1) goto ccl_repeat; \
642 #define CCL_MapSingle 0x12 /* Map by single code conversion map
643 1:ExtendedCOMMNDXXXRRRrrrXXXXX
645 ------------------------------
646 Map reg[rrr] by MAP-ID.
647 If some valid mapping is found,
648 set reg[rrr] to the result,
653 /* CCL arithmetic/logical operators. */
654 #define CCL_PLUS 0x00 /* X = Y + Z */
655 #define CCL_MINUS 0x01 /* X = Y - Z */
656 #define CCL_MUL 0x02 /* X = Y * Z */
657 #define CCL_DIV 0x03 /* X = Y / Z */
658 #define CCL_MOD 0x04 /* X = Y % Z */
659 #define CCL_AND 0x05 /* X = Y & Z */
660 #define CCL_OR 0x06 /* X = Y | Z */
661 #define CCL_XOR 0x07 /* X = Y ^ Z */
662 #define CCL_LSH 0x08 /* X = Y << Z */
663 #define CCL_RSH 0x09 /* X = Y >> Z */
664 #define CCL_LSH8 0x0A /* X = (Y << 8) | Z */
665 #define CCL_RSH8 0x0B /* X = Y >> 8, r[7] = Y & 0xFF */
666 #define CCL_DIVMOD 0x0C /* X = Y / Z, r[7] = Y % Z */
667 #define CCL_LS 0x10 /* X = (X < Y) */
668 #define CCL_GT 0x11 /* X = (X > Y) */
669 #define CCL_EQ 0x12 /* X = (X == Y) */
670 #define CCL_LE 0x13 /* X = (X <= Y) */
671 #define CCL_GE 0x14 /* X = (X >= Y) */
672 #define CCL_NE 0x15 /* X = (X != Y) */
674 #define CCL_DECODE_SJIS 0x16 /* X = HIGHER_BYTE (DE-SJIS (Y, Z))
675 r[7] = LOWER_BYTE (DE-SJIS (Y, Z)) */
676 #define CCL_ENCODE_SJIS 0x17 /* X = HIGHER_BYTE (SJIS (Y, Z))
677 r[7] = LOWER_BYTE (SJIS (Y, Z) */
679 /* Terminate CCL program successfully. */
680 #define CCL_SUCCESS \
682 ccl->status = CCL_STAT_SUCCESS; \
683 /* The "if (1)" inhibits the warning \
684 "end-of loop code not reached" */ \
685 if (1) goto ccl_finish; \
688 /* Suspend CCL program because of reading from empty input buffer or
689 writing to full output buffer. When this program is resumed, the
690 same I/O command is executed. */
691 #define CCL_SUSPEND(stat) \
694 ccl->status = (stat); \
695 /* The "if (1)" inhibits the warning \
696 "end-of loop code not reached" */ \
697 if (1) goto ccl_finish; \
700 /* Terminate CCL program because of invalid command. Should not occur
701 in the normal case. */
702 #define CCL_INVALID_CMD \
704 ccl->status = CCL_STAT_INVALID_CMD; \
705 /* The "if (1)" inhibits the warning \
706 "end-of loop code not reached" */ \
707 if (1) goto ccl_error_handler; \
710 /* Encode one character CH to multibyte form and write to the current
711 output buffer. At encoding time, if CH is less than 256, CH is
712 written as is. At decoding time, if CH cannot be regarded as an
713 ASCII character, write it in multibyte form. */
714 #define CCL_WRITE_CHAR(ch) \
718 if (conversion_mode == CCL_MODE_ENCODING) \
722 if (ccl->eol_type == CCL_CODING_EOL_CRLF) \
724 Dynarr_add (destination, '\r'); \
725 Dynarr_add (destination, '\n'); \
727 else if (ccl->eol_type == CCL_CODING_EOL_CR) \
728 Dynarr_add (destination, '\r'); \
730 Dynarr_add (destination, '\n'); \
732 else if ((ch) < 0x100) \
734 Dynarr_add (destination, ch); \
738 Bufbyte work[MAX_EMCHAR_LEN]; \
740 len = non_ascii_set_charptr_emchar (work, ch); \
741 Dynarr_add_many (destination, work, len); \
746 if (!CHAR_MULTIBYTE_P(ch)) \
748 Dynarr_add (destination, ch); \
752 Bufbyte work[MAX_EMCHAR_LEN]; \
754 len = non_ascii_set_charptr_emchar (work, ch); \
755 Dynarr_add_many (destination, work, len); \
760 /* Write a string at ccl_prog[IC] of length LEN to the current output
761 buffer. But this macro treat this string as a binary. Therefore,
762 cannot handle a multibyte string except for Control-1 characters. */
763 #define CCL_WRITE_STRING(len) \
765 Bufbyte work[MAX_EMCHAR_LEN]; \
769 else if (conversion_mode == CCL_MODE_ENCODING) \
771 for (i = 0; i < (len); i++) \
773 ch = ((XINT (ccl_prog[ic + (i / 3)])) \
774 >> ((2 - (i % 3)) * 8)) & 0xFF; \
777 if (ccl->eol_type == CCL_CODING_EOL_CRLF) \
779 Dynarr_add (destination, '\r'); \
780 Dynarr_add (destination, '\n'); \
782 else if (ccl->eol_type == CCL_CODING_EOL_CR) \
783 Dynarr_add (destination, '\r'); \
785 Dynarr_add (destination, '\n'); \
789 Dynarr_add (destination, ch); \
793 bytes = non_ascii_set_charptr_emchar (work, ch); \
794 Dynarr_add_many (destination, work, len); \
800 for (i = 0; i < (len); i++) \
802 ch = ((XINT (ccl_prog[ic + (i / 3)])) \
803 >> ((2 - (i % 3)) * 8)) & 0xFF; \
804 if (!CHAR_MULTIBYTE_P(ch)) \
806 Dynarr_add (destination, ch); \
810 bytes = non_ascii_set_charptr_emchar (work, ch); \
811 Dynarr_add_many (destination, work, len); \
817 /* Read one byte from the current input buffer into Rth register. */
818 #define CCL_READ_CHAR(r) \
826 if (ccl->last_block) \
832 CCL_SUSPEND (CCL_STAT_SUSPEND_BY_SRC); \
837 /* Set C to the character code made from CHARSET and CODE. This is
838 like MAKE_CHAR but check the validity of CHARSET and CODE. If they
839 are not valid, set C to (CODE & 0xFF) because that is usually the
840 case that CCL_ReadMultibyteChar2 read an invalid code and it set
841 CODE to that invalid byte. */
843 /* On XEmacs, TranslateCharacter is not supported. Thus, this
844 macro is not used. */
846 #define CCL_MAKE_CHAR(charset, code, c) \
848 if ((charset) == CHARSET_ASCII) \
849 (c) = (code) & 0xFF; \
850 else if (CHARSET_DEFINED_P (charset) \
851 && ((code) & 0x7F) >= 32 \
852 && ((code) < 256 || ((code >> 7) & 0x7F) >= 32)) \
854 int c1 = (code) & 0x7F, c2 = 0; \
857 c2 = c1, c1 = ((code) >> 7) & 0x7F; \
858 (c) = MAKE_CHAR (charset, c1, c2); \
861 (c) = (code) & 0xFF; \
866 /* Execute CCL code on SRC_BYTES length text at SOURCE. The resulting
867 text goes to a place pointed by DESTINATION, the length of which
868 should not exceed DST_BYTES. The bytes actually processed is
869 returned as *CONSUMED. The return value is the length of the
870 resulting text. As a side effect, the contents of CCL registers
871 are updated. If SOURCE or DESTINATION is NULL, only operations on
872 registers are permitted. */
875 #define CCL_DEBUG_BACKTRACE_LEN 256
876 int ccl_backtrace_table[CCL_BACKTRACE_TABLE];
877 int ccl_backtrace_idx;
880 struct ccl_prog_stack
882 Lisp_Object *ccl_prog; /* Pointer to an array of CCL code. */
883 int ic; /* Instruction Counter. */
886 /* For the moment, we only support depth 256 of stack. */
887 static struct ccl_prog_stack ccl_prog_stack_struct[256];
890 ccl_driver (struct ccl_program *ccl,
891 const unsigned char *source,
892 unsigned_char_dynarr *destination,
897 register int *reg = ccl->reg;
898 register int ic = ccl->ic;
899 register int code = -1;
900 register int field1, field2;
901 register Lisp_Object *ccl_prog = ccl->prog;
902 const unsigned char *src = source, *src_end = src + src_bytes;
905 int stack_idx = ccl->stack_idx;
906 /* Instruction counter of the current CCL code. */
909 if (ic >= ccl->eof_ic)
910 ic = CCL_HEADER_MAIN;
912 if (ccl->buf_magnification ==0) /* We can't produce any bytes. */
915 /* Set mapping stack pointer. */
916 mapping_stack_pointer = mapping_stack;
919 ccl_backtrace_idx = 0;
926 ccl_backtrace_table[ccl_backtrace_idx++] = ic;
927 if (ccl_backtrace_idx >= CCL_DEBUG_BACKTRACE_LEN)
928 ccl_backtrace_idx = 0;
929 ccl_backtrace_table[ccl_backtrace_idx] = 0;
932 if (!NILP (Vquit_flag) && NILP (Vinhibit_quit))
934 /* We can't just signal Qquit, instead break the loop as if
935 the whole data is processed. Don't reset Vquit_flag, it
936 must be handled later at a safer place. */
938 src = source + src_bytes;
939 ccl->status = CCL_STAT_QUIT;
944 code = XINT (ccl_prog[ic]); ic++;
946 field2 = (code & 0xFF) >> 5;
949 #define RRR (field1 & 7)
950 #define Rrr ((field1 >> 3) & 7)
952 #define EXCMD (field1 >> 6)
956 case CCL_SetRegister: /* 00000000000000000RRRrrrXXXXX */
960 case CCL_SetShortConst: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
964 case CCL_SetConst: /* 00000000000000000000rrrXXXXX */
965 reg[rrr] = XINT (ccl_prog[ic]);
969 case CCL_SetArray: /* CCCCCCCCCCCCCCCCCCCCRRRrrrXXXXX */
972 if ((unsigned int) i < j)
973 reg[rrr] = XINT (ccl_prog[ic + i]);
977 case CCL_Jump: /* A--D--D--R--E--S--S-000XXXXX */
981 case CCL_JumpCond: /* A--D--D--R--E--S--S-rrrXXXXX */
986 case CCL_WriteRegisterJump: /* A--D--D--R--E--S--S-rrrXXXXX */
992 case CCL_WriteRegisterReadJump: /* A--D--D--R--E--S--S-rrrXXXXX */
996 CCL_READ_CHAR (reg[rrr]);
1000 case CCL_WriteConstJump: /* A--D--D--R--E--S--S-000XXXXX */
1001 i = XINT (ccl_prog[ic]);
1006 case CCL_WriteConstReadJump: /* A--D--D--R--E--S--S-rrrXXXXX */
1007 i = XINT (ccl_prog[ic]);
1010 CCL_READ_CHAR (reg[rrr]);
1014 case CCL_WriteStringJump: /* A--D--D--R--E--S--S-000XXXXX */
1015 j = XINT (ccl_prog[ic]);
1017 CCL_WRITE_STRING (j);
1021 case CCL_WriteArrayReadJump: /* A--D--D--R--E--S--S-rrrXXXXX */
1023 j = XINT (ccl_prog[ic]);
1024 if ((unsigned int) i < j)
1026 i = XINT (ccl_prog[ic + 1 + i]);
1030 CCL_READ_CHAR (reg[rrr]);
1031 ic += ADDR - (j + 2);
1034 case CCL_ReadJump: /* A--D--D--R--E--S--S-rrrYYYYY */
1035 CCL_READ_CHAR (reg[rrr]);
1039 case CCL_ReadBranch: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
1040 CCL_READ_CHAR (reg[rrr]);
1041 /* fall through ... */
1042 case CCL_Branch: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
1043 if ((unsigned int) reg[rrr] < field1)
1044 ic += XINT (ccl_prog[ic + reg[rrr]]);
1046 ic += XINT (ccl_prog[ic + field1]);
1049 case CCL_ReadRegister: /* CCCCCCCCCCCCCCCCCCCCrrXXXXX */
1052 CCL_READ_CHAR (reg[rrr]);
1054 code = XINT (ccl_prog[ic]); ic++;
1056 field2 = (code & 0xFF) >> 5;
1060 case CCL_WriteExprConst: /* 1:00000OPERATION000RRR000XXXXX */
1063 j = XINT (ccl_prog[ic]);
1065 jump_address = ic + 1;
1068 case CCL_WriteRegister: /* CCCCCCCCCCCCCCCCCCCrrrXXXXX */
1074 code = XINT (ccl_prog[ic]); ic++;
1076 field2 = (code & 0xFF) >> 5;
1080 case CCL_WriteExprRegister: /* 1:00000OPERATIONRrrRRR000XXXXX */
1088 case CCL_Call: /* 1:CCCCCCCCCCCCCCCCCCCCFFFXXXXX */
1093 /* If FFF is nonzero, the CCL program ID is in the
1097 prog_id = XINT (ccl_prog[ic]);
1103 if (stack_idx >= 256
1105 || prog_id >= XVECTOR (Vccl_program_table)->size
1106 || (slot = XVECTOR (Vccl_program_table)->contents[prog_id],
1108 || !VECTORP (XVECTOR (slot)->contents[1]))
1112 ccl_prog = ccl_prog_stack_struct[0].ccl_prog;
1113 ic = ccl_prog_stack_struct[0].ic;
1118 ccl_prog_stack_struct[stack_idx].ccl_prog = ccl_prog;
1119 ccl_prog_stack_struct[stack_idx].ic = ic;
1121 ccl_prog = XVECTOR (XVECTOR (slot)->contents[1])->contents;
1122 ic = CCL_HEADER_MAIN;
1126 case CCL_WriteConstString: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
1128 CCL_WRITE_CHAR (field1);
1131 CCL_WRITE_STRING (field1);
1132 ic += (field1 + 2) / 3;
1136 case CCL_WriteArray: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
1138 if ((unsigned int) i < field1)
1140 j = XINT (ccl_prog[ic + i]);
1146 case CCL_End: /* 0000000000000000000000XXXXX */
1150 ccl_prog = ccl_prog_stack_struct[stack_idx].ccl_prog;
1151 ic = ccl_prog_stack_struct[stack_idx].ic;
1156 /* ccl->ic should points to this command code again to
1157 suppress further processing. */
1161 case CCL_ExprSelfConst: /* 00000OPERATION000000rrrXXXXX */
1162 i = XINT (ccl_prog[ic]);
1167 case CCL_ExprSelfReg: /* 00000OPERATION000RRRrrrXXXXX */
1174 case CCL_PLUS: reg[rrr] += i; break;
1175 case CCL_MINUS: reg[rrr] -= i; break;
1176 case CCL_MUL: reg[rrr] *= i; break;
1177 case CCL_DIV: reg[rrr] /= i; break;
1178 case CCL_MOD: reg[rrr] %= i; break;
1179 case CCL_AND: reg[rrr] &= i; break;
1180 case CCL_OR: reg[rrr] |= i; break;
1181 case CCL_XOR: reg[rrr] ^= i; break;
1182 case CCL_LSH: reg[rrr] <<= i; break;
1183 case CCL_RSH: reg[rrr] >>= i; break;
1184 case CCL_LSH8: reg[rrr] <<= 8; reg[rrr] |= i; break;
1185 case CCL_RSH8: reg[7] = reg[rrr] & 0xFF; reg[rrr] >>= 8; break;
1186 case CCL_DIVMOD: reg[7] = reg[rrr] % i; reg[rrr] /= i; break;
1187 case CCL_LS: reg[rrr] = reg[rrr] < i; break;
1188 case CCL_GT: reg[rrr] = reg[rrr] > i; break;
1189 case CCL_EQ: reg[rrr] = reg[rrr] == i; break;
1190 case CCL_LE: reg[rrr] = reg[rrr] <= i; break;
1191 case CCL_GE: reg[rrr] = reg[rrr] >= i; break;
1192 case CCL_NE: reg[rrr] = reg[rrr] != i; break;
1193 default: CCL_INVALID_CMD;
1197 case CCL_SetExprConst: /* 00000OPERATION000RRRrrrXXXXX */
1199 j = XINT (ccl_prog[ic]);
1201 jump_address = ++ic;
1204 case CCL_SetExprReg: /* 00000OPERATIONRrrRRRrrrXXXXX */
1211 case CCL_ReadJumpCondExprConst: /* A--D--D--R--E--S--S-rrrXXXXX */
1212 CCL_READ_CHAR (reg[rrr]);
1213 case CCL_JumpCondExprConst: /* A--D--D--R--E--S--S-rrrXXXXX */
1215 op = XINT (ccl_prog[ic]);
1216 jump_address = ic++ + ADDR;
1217 j = XINT (ccl_prog[ic]);
1222 case CCL_ReadJumpCondExprReg: /* A--D--D--R--E--S--S-rrrXXXXX */
1223 CCL_READ_CHAR (reg[rrr]);
1224 case CCL_JumpCondExprReg:
1226 op = XINT (ccl_prog[ic]);
1227 jump_address = ic++ + ADDR;
1228 j = reg[XINT (ccl_prog[ic])];
1235 case CCL_PLUS: reg[rrr] = i + j; break;
1236 case CCL_MINUS: reg[rrr] = i - j; break;
1237 case CCL_MUL: reg[rrr] = i * j; break;
1238 case CCL_DIV: reg[rrr] = i / j; break;
1239 case CCL_MOD: reg[rrr] = i % j; break;
1240 case CCL_AND: reg[rrr] = i & j; break;
1241 case CCL_OR: reg[rrr] = i | j; break;
1242 case CCL_XOR: reg[rrr] = i ^ j;; break;
1243 case CCL_LSH: reg[rrr] = i << j; break;
1244 case CCL_RSH: reg[rrr] = i >> j; break;
1245 case CCL_LSH8: reg[rrr] = (i << 8) | j; break;
1246 case CCL_RSH8: reg[rrr] = i >> 8; reg[7] = i & 0xFF; break;
1247 case CCL_DIVMOD: reg[rrr] = i / j; reg[7] = i % j; break;
1248 case CCL_LS: reg[rrr] = i < j; break;
1249 case CCL_GT: reg[rrr] = i > j; break;
1250 case CCL_EQ: reg[rrr] = i == j; break;
1251 case CCL_LE: reg[rrr] = i <= j; break;
1252 case CCL_GE: reg[rrr] = i >= j; break;
1253 case CCL_NE: reg[rrr] = i != j; break;
1254 case CCL_DECODE_SJIS:
1255 /* DECODE_SJIS set MSB for internal format
1256 as opposed to Emacs. */
1257 DECODE_SJIS (i, j, reg[rrr], reg[7]);
1261 case CCL_ENCODE_SJIS:
1262 /* ENCODE_SJIS assumes MSB of SJIS-char is set
1263 as opposed to Emacs. */
1264 ENCODE_SJIS (i | 0x80, j | 0x80, reg[rrr], reg[7]);
1266 default: CCL_INVALID_CMD;
1269 if (code == CCL_WriteExprConst || code == CCL_WriteExprRegister)
1283 case CCL_ReadMultibyteChar2:
1290 goto ccl_read_multibyte_character_suspend;
1298 reg[RRR] = LEADING_BYTE_ASCII;
1300 else if (i <= MAX_LEADING_BYTE_OFFICIAL_1)
1303 goto ccl_read_multibyte_character_suspend;
1305 reg[rrr] = (*src++ & 0x7F);
1307 else if (i <= MAX_LEADING_BYTE_OFFICIAL_2)
1309 if ((src + 1) >= src_end)
1310 goto ccl_read_multibyte_character_suspend;
1312 i = (*src++ & 0x7F);
1313 reg[rrr] = ((i << 7) | (*src & 0x7F));
1316 else if (i == PRE_LEADING_BYTE_PRIVATE_1)
1318 if ((src + 1) >= src_end)
1319 goto ccl_read_multibyte_character_suspend;
1321 reg[rrr] = (*src++ & 0x7F);
1323 else if (i == PRE_LEADING_BYTE_PRIVATE_2)
1325 if ((src + 2) >= src_end)
1326 goto ccl_read_multibyte_character_suspend;
1328 i = (*src++ & 0x7F);
1329 reg[rrr] = ((i << 7) | (*src & 0x7F));
1334 /* INVALID CODE. Return a single byte character. */
1335 reg[RRR] = LEADING_BYTE_ASCII;
1340 ccl_read_multibyte_character_suspend:
1342 if (ccl->last_block)
1348 CCL_SUSPEND (CCL_STAT_SUSPEND_BY_SRC);
1354 case CCL_WriteMultibyteChar2:
1355 i = reg[RRR]; /* charset */
1356 if (i == LEADING_BYTE_ASCII)
1357 i = reg[rrr] & 0xFF;
1358 else if (XCHARSET_DIMENSION (CHARSET_BY_LEADING_BYTE (i)) == 1)
1359 i = (((i - FIELD2_TO_OFFICIAL_LEADING_BYTE) << 7)
1360 | (reg[rrr] & 0x7F));
1361 else if (i < MAX_LEADING_BYTE_OFFICIAL_2)
1362 i = ((i - FIELD1_TO_OFFICIAL_LEADING_BYTE) << 14) | reg[rrr];
1364 i = ((i - FIELD1_TO_PRIVATE_LEADING_BYTE) << 14) | reg[rrr];
1371 case CCL_TranslateCharacter:
1373 /* XEmacs does not have translate_char, and its
1374 equivalent nor. We do nothing on this operation. */
1375 CCL_MAKE_CHAR (reg[RRR], reg[rrr], i);
1376 op = translate_char (GET_TRANSLATION_TABLE (reg[Rrr]),
1378 SPLIT_CHAR (op, reg[RRR], i, j);
1386 case CCL_TranslateCharacterConstTbl:
1388 /* XEmacs does not have translate_char, and its
1389 equivalent nor. We do nothing on this operation. */
1390 op = XINT (ccl_prog[ic]); /* table */
1392 CCL_MAKE_CHAR (reg[RRR], reg[rrr], i);
1393 op = translate_char (GET_TRANSLATION_TABLE (op), i, -1, 0, 0);
1394 SPLIT_CHAR (op, reg[RRR], i, j);
1402 case CCL_IterateMultipleMap:
1404 Lisp_Object map, content, attrib, value;
1405 int point, size, fin_ic;
1407 j = XINT (ccl_prog[ic++]); /* number of maps. */
1410 if ((j > reg[RRR]) && (j >= 0))
1425 size = XVECTOR (Vcode_conversion_map_vector)->size;
1426 point = XINT (ccl_prog[ic++]);
1427 if (point >= size) continue;
1429 XVECTOR (Vcode_conversion_map_vector)->contents[point];
1431 /* Check map validity. */
1432 if (!CONSP (map)) continue;
1434 if (!VECTORP (map)) continue;
1435 size = XVECTOR (map)->size;
1436 if (size <= 1) continue;
1438 content = XVECTOR (map)->contents[0];
1441 [STARTPOINT VAL1 VAL2 ...] or
1442 [t ELEMENT STARTPOINT ENDPOINT] */
1445 point = XUINT (content);
1446 point = op - point + 1;
1447 if (!((point >= 1) && (point < size))) continue;
1448 content = XVECTOR (map)->contents[point];
1450 else if (EQ (content, Qt))
1452 if (size != 4) continue;
1453 if ((op >= XUINT (XVECTOR (map)->contents[2]))
1454 && (op < XUINT (XVECTOR (map)->contents[3])))
1455 content = XVECTOR (map)->contents[1];
1464 else if (INTP (content))
1467 reg[rrr] = XINT(content);
1470 else if (EQ (content, Qt) || EQ (content, Qlambda))
1475 else if (CONSP (content))
1477 attrib = XCAR (content);
1478 value = XCDR (content);
1479 if (!INTP (attrib) || !INTP (value))
1482 reg[rrr] = XUINT (value);
1485 else if (SYMBOLP (content))
1486 CCL_CALL_FOR_MAP_INSTRUCTION (content, fin_ic);
1496 case CCL_MapMultiple:
1498 Lisp_Object map, content, attrib, value;
1499 int point, size, map_vector_size;
1500 int map_set_rest_length, fin_ic;
1501 int current_ic = this_ic;
1503 /* inhibit recursive call on MapMultiple. */
1504 if (stack_idx_of_map_multiple > 0)
1506 if (stack_idx_of_map_multiple <= stack_idx)
1508 stack_idx_of_map_multiple = 0;
1509 mapping_stack_pointer = mapping_stack;
1514 mapping_stack_pointer = mapping_stack;
1515 stack_idx_of_map_multiple = 0;
1517 map_set_rest_length =
1518 XINT (ccl_prog[ic++]); /* number of maps and separators. */
1519 fin_ic = ic + map_set_rest_length;
1522 if ((map_set_rest_length > reg[RRR]) && (reg[RRR] >= 0))
1526 map_set_rest_length -= i;
1532 mapping_stack_pointer = mapping_stack;
1536 if (mapping_stack_pointer <= (mapping_stack + 1))
1538 /* Set up initial state. */
1539 mapping_stack_pointer = mapping_stack;
1540 PUSH_MAPPING_STACK (0, op);
1545 /* Recover after calling other ccl program. */
1548 POP_MAPPING_STACK (map_set_rest_length, orig_op);
1549 POP_MAPPING_STACK (map_set_rest_length, reg[rrr]);
1553 /* Regard it as Qnil. */
1557 map_set_rest_length--;
1560 /* Regard it as Qt. */
1564 map_set_rest_length--;
1567 /* Regard it as Qlambda. */
1569 i += map_set_rest_length;
1570 ic += map_set_rest_length;
1571 map_set_rest_length = 0;
1574 /* Regard it as normal mapping. */
1575 i += map_set_rest_length;
1576 ic += map_set_rest_length;
1577 POP_MAPPING_STACK (map_set_rest_length, reg[rrr]);
1581 map_vector_size = XVECTOR (Vcode_conversion_map_vector)->size;
1584 for (;map_set_rest_length > 0;i++, ic++, map_set_rest_length--)
1586 point = XINT(ccl_prog[ic]);
1589 /* +1 is for including separator. */
1591 if (mapping_stack_pointer
1592 >= mapping_stack + countof (mapping_stack))
1594 PUSH_MAPPING_STACK (map_set_rest_length - point,
1596 map_set_rest_length = point;
1601 if (point >= map_vector_size) continue;
1602 map = (XVECTOR (Vcode_conversion_map_vector)
1605 /* Check map validity. */
1606 if (!CONSP (map)) continue;
1608 if (!VECTORP (map)) continue;
1609 size = XVECTOR (map)->size;
1610 if (size <= 1) continue;
1612 content = XVECTOR (map)->contents[0];
1615 [STARTPOINT VAL1 VAL2 ...] or
1616 [t ELEMENT STARTPOINT ENDPOINT] */
1619 point = XUINT (content);
1620 point = op - point + 1;
1621 if (!((point >= 1) && (point < size))) continue;
1622 content = XVECTOR (map)->contents[point];
1624 else if (EQ (content, Qt))
1626 if (size != 4) continue;
1627 if ((op >= XUINT (XVECTOR (map)->contents[2])) &&
1628 (op < XUINT (XVECTOR (map)->contents[3])))
1629 content = XVECTOR (map)->contents[1];
1642 op = XINT (content);
1643 i += map_set_rest_length - 1;
1644 ic += map_set_rest_length - 1;
1645 POP_MAPPING_STACK (map_set_rest_length, reg[rrr]);
1646 map_set_rest_length++;
1648 else if (CONSP (content))
1650 attrib = XCAR (content);
1651 value = XCDR (content);
1652 if (!INTP (attrib) || !INTP (value))
1655 i += map_set_rest_length - 1;
1656 ic += map_set_rest_length - 1;
1657 POP_MAPPING_STACK (map_set_rest_length, reg[rrr]);
1658 map_set_rest_length++;
1660 else if (EQ (content, Qt))
1664 else if (EQ (content, Qlambda))
1666 i += map_set_rest_length;
1667 ic += map_set_rest_length;
1670 else if (SYMBOLP (content))
1672 if (mapping_stack_pointer
1673 >= mapping_stack + countof (mapping_stack))
1675 PUSH_MAPPING_STACK (map_set_rest_length, reg[rrr]);
1676 PUSH_MAPPING_STACK (map_set_rest_length, op);
1677 stack_idx_of_map_multiple = stack_idx + 1;
1678 CCL_CALL_FOR_MAP_INSTRUCTION (content, current_ic);
1683 if (mapping_stack_pointer <= (mapping_stack + 1))
1685 POP_MAPPING_STACK (map_set_rest_length, reg[rrr]);
1686 i += map_set_rest_length;
1687 ic += map_set_rest_length;
1688 POP_MAPPING_STACK (map_set_rest_length, reg[rrr]);
1698 Lisp_Object map, attrib, value, content;
1700 j = XINT (ccl_prog[ic++]); /* map_id */
1702 if (j >= XVECTOR (Vcode_conversion_map_vector)->size)
1707 map = XVECTOR (Vcode_conversion_map_vector)->contents[j];
1719 size = XVECTOR (map)->size;
1720 point = XUINT (XVECTOR (map)->contents[0]);
1721 point = op - point + 1;
1724 (!((point >= 1) && (point < size))))
1729 content = XVECTOR (map)->contents[point];
1732 else if (INTP (content))
1733 reg[rrr] = XINT (content);
1734 else if (EQ (content, Qt));
1735 else if (CONSP (content))
1737 attrib = XCAR (content);
1738 value = XCDR (content);
1739 if (!INTP (attrib) || !INTP (value))
1741 reg[rrr] = XUINT(value);
1744 else if (SYMBOLP (content))
1745 CCL_CALL_FOR_MAP_INSTRUCTION (content, ic);
1765 /* We can insert an error message only if DESTINATION is
1766 specified and we still have a room to store the message
1770 switch (ccl->status)
1772 case CCL_STAT_INVALID_CMD:
1773 sprintf(msg, "\nCCL: Invalid command %x (ccl_code = %x) at %d.",
1774 code & 0x1F, code, this_ic);
1777 int i = ccl_backtrace_idx - 1;
1780 Dynarr_add_many (destination, (unsigned char *) msg, strlen (msg));
1782 for (j = 0; j < CCL_DEBUG_BACKTRACE_LEN; j++, i--)
1784 if (i < 0) i = CCL_DEBUG_BACKTRACE_LEN - 1;
1785 if (ccl_backtrace_table[i] == 0)
1787 sprintf(msg, " %d", ccl_backtrace_table[i]);
1788 Dynarr_add_many (destination, (unsigned char *) msg, strlen (msg));
1796 sprintf(msg, "\nCCL: Exited.");
1800 sprintf(msg, "\nCCL: Unknown error type (%d).", ccl->status);
1803 Dynarr_add_many (destination, (unsigned char *) msg, strlen (msg));
1808 ccl->stack_idx = stack_idx;
1809 ccl->prog = ccl_prog;
1810 if (consumed) *consumed = src - source;
1813 return Dynarr_length (destination);
1816 /* Resolve symbols in the specified CCL code (Lisp vector). This
1817 function converts symbols of code conversion maps and character
1818 translation tables embedded in the CCL code into their ID numbers.
1820 The return value is a vector (CCL itself or a new vector in which
1821 all symbols are resolved), Qt if resolving of some symbol failed,
1822 or nil if CCL contains invalid data. */
1825 resolve_symbol_ccl_program (Lisp_Object ccl)
1827 int i, veclen, unresolved = 0;
1828 Lisp_Object result, contents, val;
1831 veclen = XVECTOR (result)->size;
1833 for (i = 0; i < veclen; i++)
1835 contents = XVECTOR (result)->contents[i];
1836 if (INTP (contents))
1838 else if (CONSP (contents)
1839 && SYMBOLP (XCAR (contents))
1840 && SYMBOLP (XCDR (contents)))
1842 /* This is the new style for embedding symbols. The form is
1843 (SYMBOL . PROPERTY). (get SYMBOL PROPERTY) should give
1846 if (EQ (result, ccl))
1847 result = Fcopy_sequence (ccl);
1849 val = Fget (XCAR (contents), XCDR (contents), Qnil);
1851 XVECTOR (result)->contents[i] = val;
1856 else if (SYMBOLP (contents))
1858 /* This is the old style for embedding symbols. This style
1859 may lead to a bug if, for instance, a translation table
1860 and a code conversion map have the same name. */
1861 if (EQ (result, ccl))
1862 result = Fcopy_sequence (ccl);
1864 val = Fget (contents, Qcode_conversion_map_id, Qnil);
1866 XVECTOR (result)->contents[i] = val;
1869 val = Fget (contents, Qccl_program_idx, Qnil);
1871 XVECTOR (result)->contents[i] = val;
1880 return (unresolved ? Qt : result);
1883 /* Return the compiled code (vector) of CCL program CCL_PROG.
1884 CCL_PROG is a name (symbol) of the program or already compiled
1885 code. If necessary, resolve symbols in the compiled code to index
1886 numbers. If we failed to get the compiled code or to resolve
1887 symbols, return Qnil. */
1890 ccl_get_compiled_code (Lisp_Object ccl_prog)
1892 Lisp_Object val, slot;
1894 if (VECTORP (ccl_prog))
1896 val = resolve_symbol_ccl_program (ccl_prog);
1897 return (VECTORP (val) ? val : Qnil);
1899 if (!SYMBOLP (ccl_prog))
1902 val = Fget (ccl_prog, Qccl_program_idx, Qnil);
1904 || XINT (val) >= XVECTOR_LENGTH (Vccl_program_table))
1906 slot = XVECTOR_DATA (Vccl_program_table)[XINT (val)];
1907 if (! VECTORP (slot)
1908 || XVECTOR (slot)->size != 3
1909 || ! VECTORP (XVECTOR_DATA (slot)[1]))
1911 if (NILP (XVECTOR_DATA (slot)[2]))
1913 val = resolve_symbol_ccl_program (XVECTOR_DATA (slot)[1]);
1914 if (! VECTORP (val))
1916 XVECTOR_DATA (slot)[1] = val;
1917 XVECTOR_DATA (slot)[2] = Qt;
1919 return XVECTOR_DATA (slot)[1];
1922 /* Setup fields of the structure pointed by CCL appropriately for the
1923 execution of CCL program CCL_PROG. CCL_PROG is the name (symbol)
1924 of the CCL program or the already compiled code (vector).
1925 Return 0 if we succeed this setup, else return -1.
1927 If CCL_PROG is nil, we just reset the structure pointed by CCL. */
1929 setup_ccl_program (struct ccl_program *ccl, Lisp_Object ccl_prog)
1933 if (! NILP (ccl_prog))
1935 ccl_prog = ccl_get_compiled_code (ccl_prog);
1936 if (! VECTORP (ccl_prog))
1938 ccl->size = XVECTOR_LENGTH (ccl_prog);
1939 ccl->prog = XVECTOR_DATA (ccl_prog);
1940 ccl->eof_ic = XINT (XVECTOR_DATA (ccl_prog)[CCL_HEADER_EOF]);
1941 ccl->buf_magnification = XINT (XVECTOR_DATA (ccl_prog)[CCL_HEADER_BUF_MAG]);
1943 ccl->ic = CCL_HEADER_MAIN;
1944 for (i = 0; i < 8; i++)
1946 ccl->last_block = 0;
1947 ccl->private_state = 0;
1950 ccl->eol_type = CCL_CODING_EOL_LF;
1956 DEFUN ("ccl-program-p", Fccl_program_p, 1, 1, 0, /*
1957 Return t if OBJECT is a CCL program name or a compiled CCL program code.
1958 See the documentation of `define-ccl-program' for the detail of CCL program.
1964 if (VECTORP (object))
1966 val = resolve_symbol_ccl_program (object);
1967 return (VECTORP (val) ? Qt : Qnil);
1969 if (!SYMBOLP (object))
1972 val = Fget (object, Qccl_program_idx, Qnil);
1973 return ((! NATNUMP (val)
1974 || XINT (val) >= XVECTOR_LENGTH (Vccl_program_table))
1978 DEFUN ("ccl-execute", Fccl_execute, 2, 2, 0, /*
1979 Execute CCL-PROGRAM with registers initialized by REGISTERS.
1981 CCL-PROGRAM is a CCL program name (symbol)
1982 or a compiled code generated by `ccl-compile' (for backward compatibility,
1983 in this case, the overhead of the execution is bigger than the former case).
1984 No I/O commands should appear in CCL-PROGRAM.
1986 REGISTERS is a vector of [R0 R1 ... R7] where RN is an initial value
1989 As side effect, each element of REGISTERS holds the value of
1990 corresponding register after the execution.
1992 See the documentation of `define-ccl-program' for the detail of CCL program.
1996 struct ccl_program ccl;
1999 if (setup_ccl_program (&ccl, ccl_prog) < 0)
2000 error ("Invalid CCL program");
2003 if (XVECTOR_LENGTH (reg) != 8)
2004 error ("Length of vector REGISTERS is not 8");
2006 for (i = 0; i < 8; i++)
2007 ccl.reg[i] = (INTP (XVECTOR_DATA (reg)[i])
2008 ? XINT (XVECTOR_DATA (reg)[i])
2011 ccl_driver (&ccl, (const unsigned char *)0,
2012 (unsigned_char_dynarr *)0, 0, (int *)0,
2015 if (ccl.status != CCL_STAT_SUCCESS)
2016 error ("Error in CCL program at %dth code", ccl.ic);
2018 for (i = 0; i < 8; i++)
2019 XSETINT (XVECTOR (reg)->contents[i], ccl.reg[i]);
2023 DEFUN ("ccl-execute-on-string", Fccl_execute_on_string,
2025 Execute CCL-PROGRAM with initial STATUS on STRING.
2027 CCL-PROGRAM is a symbol registered by register-ccl-program,
2028 or a compiled code generated by `ccl-compile' (for backward compatibility,
2029 in this case, the execution is slower).
2031 Read buffer is set to STRING, and write buffer is allocated automatically.
2033 STATUS is a vector of [R0 R1 ... R7 IC], where
2034 R0..R7 are initial values of corresponding registers,
2035 IC is the instruction counter specifying from where to start the program.
2036 If R0..R7 are nil, they are initialized to 0.
2037 If IC is nil, it is initialized to head of the CCL program.
2039 If optional 4th arg CONTINUE is non-nil, keep IC on read operation
2040 when read buffer is exhausted, else, IC is always set to the end of
2041 CCL-PROGRAM on exit.
2043 It returns the contents of write buffer as a string,
2044 and as side effect, STATUS is updated.
2046 See the documentation of `define-ccl-program' for the detail of CCL program.
2048 (ccl_prog, status, string, continue_))
2051 struct ccl_program ccl;
2053 unsigned_char_dynarr *outbuf;
2054 struct gcpro gcpro1, gcpro2;
2056 if (setup_ccl_program (&ccl, ccl_prog) < 0)
2057 error ("Invalid CCL program");
2059 CHECK_VECTOR (status);
2060 if (XVECTOR (status)->size != 9)
2061 error ("Length of vector STATUS is not 9");
2062 CHECK_STRING (string);
2064 GCPRO2 (status, string);
2066 for (i = 0; i < 8; i++)
2068 if (NILP (XVECTOR_DATA (status)[i]))
2069 XSETINT (XVECTOR_DATA (status)[i], 0);
2070 if (INTP (XVECTOR_DATA (status)[i]))
2071 ccl.reg[i] = XINT (XVECTOR_DATA (status)[i]);
2073 if (INTP (XVECTOR (status)->contents[i]))
2075 i = XINT (XVECTOR_DATA (status)[8]);
2076 if (ccl.ic < i && i < ccl.size)
2079 outbuf = Dynarr_new (unsigned_char);
2080 ccl.last_block = NILP (continue_);
2081 produced = ccl_driver (&ccl, XSTRING_DATA (string), outbuf,
2082 XSTRING_LENGTH (string),
2085 for (i = 0; i < 8; i++)
2086 XSETINT (XVECTOR_DATA (status)[i], ccl.reg[i]);
2087 XSETINT (XVECTOR_DATA (status)[8], ccl.ic);
2090 val = make_string (Dynarr_atp (outbuf, 0), produced);
2091 Dynarr_free (outbuf);
2093 if (ccl.status == CCL_STAT_SUSPEND_BY_DST)
2094 error ("Output buffer for the CCL programs overflow");
2095 if (ccl.status != CCL_STAT_SUCCESS
2096 && ccl.status != CCL_STAT_SUSPEND_BY_SRC)
2097 error ("Error in CCL program at %dth code", ccl.ic);
2102 DEFUN ("register-ccl-program", Fregister_ccl_program,
2104 Register CCL program CCL-PROG as NAME in `ccl-program-table'.
2105 CCL-PROG should be a compiled CCL program (vector), or nil.
2106 If it is nil, just reserve NAME as a CCL program name.
2107 Return index number of the registered CCL program.
2111 int len = XVECTOR_LENGTH (Vccl_program_table);
2113 Lisp_Object resolved;
2115 CHECK_SYMBOL (name);
2117 if (!NILP (ccl_prog))
2119 CHECK_VECTOR (ccl_prog);
2120 resolved = resolve_symbol_ccl_program (ccl_prog);
2121 if (! NILP (resolved))
2123 ccl_prog = resolved;
2128 for (idx = 0; idx < len; idx++)
2132 slot = XVECTOR_DATA (Vccl_program_table)[idx];
2133 if (!VECTORP (slot))
2134 /* This is the first unused slot. Register NAME here. */
2137 if (EQ (name, XVECTOR_DATA (slot)[0]))
2139 /* Update this slot. */
2140 XVECTOR_DATA (slot)[1] = ccl_prog;
2141 XVECTOR_DATA (slot)[2] = resolved;
2142 return make_int (idx);
2148 /* Extend the table. */
2149 Lisp_Object new_table;
2152 new_table = Fmake_vector (make_int (len * 2), Qnil);
2153 for (j = 0; j < len; j++)
2154 XVECTOR_DATA (new_table)[j]
2155 = XVECTOR_DATA (Vccl_program_table)[j];
2156 Vccl_program_table = new_table;
2162 elt = Fmake_vector (make_int (3), Qnil);
2163 XVECTOR_DATA (elt)[0] = name;
2164 XVECTOR_DATA (elt)[1] = ccl_prog;
2165 XVECTOR_DATA (elt)[2] = resolved;
2166 XVECTOR_DATA (Vccl_program_table)[idx] = elt;
2169 Fput (name, Qccl_program_idx, make_int (idx));
2170 return make_int (idx);
2173 /* Register code conversion map.
2174 A code conversion map consists of numbers, Qt, Qnil, and Qlambda.
2175 The first element is start code point.
2176 The rest elements are mapped numbers.
2177 Symbol t means to map to an original number before mapping.
2178 Symbol nil means that the corresponding element is empty.
2179 Symbol lambda means to terminate mapping here.
2182 DEFUN ("register-code-conversion-map", Fregister_code_conversion_map,
2184 Register SYMBOL as code conversion map MAP.
2185 Return index number of the registered map.
2189 int len = XVECTOR_LENGTH (Vcode_conversion_map_vector);
2193 CHECK_SYMBOL (symbol);
2196 for (i = 0; i < len; i++)
2198 Lisp_Object slot = XVECTOR_DATA (Vcode_conversion_map_vector)[i];
2203 if (EQ (symbol, XCAR (slot)))
2207 Fput (symbol, Qcode_conversion_map, map);
2208 Fput (symbol, Qcode_conversion_map_id, idx);
2215 Lisp_Object new_vector = Fmake_vector (make_int (len * 2), Qnil);
2218 for (j = 0; j < len; j++)
2219 XVECTOR_DATA (new_vector)[j]
2220 = XVECTOR_DATA (Vcode_conversion_map_vector)[j];
2221 Vcode_conversion_map_vector = new_vector;
2225 Fput (symbol, Qcode_conversion_map, map);
2226 Fput (symbol, Qcode_conversion_map_id, idx);
2227 XVECTOR_DATA (Vcode_conversion_map_vector)[i] = Fcons (symbol, map);
2233 syms_of_mule_ccl (void)
2235 DEFSUBR (Fccl_program_p);
2236 DEFSUBR (Fccl_execute);
2237 DEFSUBR (Fccl_execute_on_string);
2238 DEFSUBR (Fregister_ccl_program);
2239 DEFSUBR (Fregister_code_conversion_map);
2243 vars_of_mule_ccl (void)
2245 staticpro (&Vccl_program_table);
2246 Vccl_program_table = Fmake_vector (make_int (32), Qnil);
2248 defsymbol (&Qccl_program, "ccl-program");
2249 defsymbol (&Qccl_program_idx, "ccl-program-idx");
2250 defsymbol (&Qcode_conversion_map, "code-conversion-map");
2251 defsymbol (&Qcode_conversion_map_id, "code-conversion-map-id");
2253 DEFVAR_LISP ("code-conversion-map-vector", &Vcode_conversion_map_vector /*
2254 Vector of code conversion maps.
2256 Vcode_conversion_map_vector = Fmake_vector (make_int (16), Qnil);
2258 DEFVAR_LISP ("font-ccl-encoder-alist", &Vfont_ccl_encoder_alist /*
2259 Alist of fontname patterns vs corresponding CCL program.
2260 Each element looks like (REGEXP . CCL-CODE),
2261 where CCL-CODE is a compiled CCL program.
2262 When a font whose name matches REGEXP is used for displaying a character,
2263 CCL-CODE is executed to calculate the code point in the font
2264 from the charset number and position code(s) of the character which are set
2265 in CCL registers R0, R1, and R2 before the execution.
2266 The code point in the font is set in CCL registers R1 and R2
2267 when the execution terminated.
2268 If the font is single-byte font, the register R2 is not used.
2270 Vfont_ccl_encoder_alist = Qnil;