1 /* CCL (Code Conversion Language) interpreter.
2 Copyright (C) 1995, 1997 Electrotechnical Laboratory, JAPAN.
3 Copyright (C) 2001 Free Software Foundation, Inc.
4 Licensed to the Free Software Foundation.
5 Copyright (C) 2001, 2002
6 National Institute of Advanced Industrial Science and Technology (AIST)
7 Registration Number H13PRO009
9 This file is part of GNU Emacs.
11 GNU Emacs is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2, or (at your option)
16 GNU Emacs is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with GNU Emacs; see the file COPYING. If not, write to
23 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 Boston, MA 02111-1307, USA. */
31 #include "character.h"
36 Lisp_Object Qccl
, Qcclp
;
38 /* This contains all code conversion map available to CCL. */
39 Lisp_Object Vcode_conversion_map_vector
;
41 /* Alist of fontname patterns vs corresponding CCL program. */
42 Lisp_Object Vfont_ccl_encoder_alist
;
44 /* This symbol is a property which assocates with ccl program vector.
45 Ex: (get 'ccl-big5-encoder 'ccl-program) returns ccl program vector. */
46 Lisp_Object Qccl_program
;
48 /* These symbols are properties which associate with code conversion
49 map and their ID respectively. */
50 Lisp_Object Qcode_conversion_map
;
51 Lisp_Object Qcode_conversion_map_id
;
53 /* Symbols of ccl program have this property, a value of the property
54 is an index for Vccl_protram_table. */
55 Lisp_Object Qccl_program_idx
;
57 /* Table of registered CCL programs. Each element is a vector of
58 NAME, CCL_PROG, and RESOLVEDP where NAME (symbol) is the name of
59 the program, CCL_PROG (vector) is the compiled code of the program,
60 RESOLVEDP (t or nil) is the flag to tell if symbols in CCL_PROG is
61 already resolved to index numbers or not. */
62 Lisp_Object Vccl_program_table
;
64 /* Vector of registered hash tables for translation. */
65 Lisp_Object Vtranslation_hash_table_vector
;
67 /* Return a hash table of id number ID. */
68 #define GET_HASH_TABLE(id) \
69 (XHASH_TABLE (XCDR(XVECTOR(Vtranslation_hash_table_vector)->contents[(id)])))
70 /* Copied from fns.c. */
71 #define HASH_VALUE(H, IDX) AREF ((H)->key_and_value, 2 * (IDX) + 1)
73 extern int charset_unicode
;
75 /* CCL (Code Conversion Language) is a simple language which has
76 operations on one input buffer, one output buffer, and 7 registers.
77 The syntax of CCL is described in `ccl.el'. Emacs Lisp function
78 `ccl-compile' compiles a CCL program and produces a CCL code which
79 is a vector of integers. The structure of this vector is as
80 follows: The 1st element: buffer-magnification, a factor for the
81 size of output buffer compared with the size of input buffer. The
82 2nd element: address of CCL code to be executed when encountered
83 with end of input stream. The 3rd and the remaining elements: CCL
86 /* Header of CCL compiled code */
87 #define CCL_HEADER_BUF_MAG 0
88 #define CCL_HEADER_EOF 1
89 #define CCL_HEADER_MAIN 2
91 /* CCL code is a sequence of 28-bit non-negative integers (i.e. the
92 MSB is always 0), each contains CCL command and/or arguments in the
95 |----------------- integer (28-bit) ------------------|
96 |------- 17-bit ------|- 3-bit --|- 3-bit --|- 5-bit -|
97 |--constant argument--|-register-|-register-|-command-|
98 ccccccccccccccccc RRR rrr XXXXX
100 |------- relative address -------|-register-|-command-|
101 cccccccccccccccccccc rrr XXXXX
103 |------------- constant or other args ----------------|
104 cccccccccccccccccccccccccccc
106 where, `cc...c' is a non-negative integer indicating constant value
107 (the left most `c' is always 0) or an absolute jump address, `RRR'
108 and `rrr' are CCL register number, `XXXXX' is one of the following
113 Each comment fields shows one or more lines for command syntax and
114 the following lines for semantics of the command. In semantics, IC
115 stands for Instruction Counter. */
117 #define CCL_SetRegister 0x00 /* Set register a register value:
118 1:00000000000000000RRRrrrXXXXX
119 ------------------------------
123 #define CCL_SetShortConst 0x01 /* Set register a short constant value:
124 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
125 ------------------------------
126 reg[rrr] = CCCCCCCCCCCCCCCCCCC;
129 #define CCL_SetConst 0x02 /* Set register a constant value:
130 1:00000000000000000000rrrXXXXX
132 ------------------------------
137 #define CCL_SetArray 0x03 /* Set register an element of array:
138 1:CCCCCCCCCCCCCCCCCRRRrrrXXXXX
142 ------------------------------
143 if (0 <= reg[RRR] < CC..C)
144 reg[rrr] = ELEMENT[reg[RRR]];
148 #define CCL_Jump 0x04 /* Jump:
149 1:A--D--D--R--E--S--S-000XXXXX
150 ------------------------------
154 /* Note: If CC..C is greater than 0, the second code is omitted. */
156 #define CCL_JumpCond 0x05 /* Jump conditional:
157 1:A--D--D--R--E--S--S-rrrXXXXX
158 ------------------------------
164 #define CCL_WriteRegisterJump 0x06 /* Write register and jump:
165 1:A--D--D--R--E--S--S-rrrXXXXX
166 ------------------------------
171 #define CCL_WriteRegisterReadJump 0x07 /* Write register, read, and jump:
172 1:A--D--D--R--E--S--S-rrrXXXXX
173 2:A--D--D--R--E--S--S-rrrYYYYY
174 -----------------------------
180 /* Note: If read is suspended, the resumed execution starts from the
181 second code (YYYYY == CCL_ReadJump). */
183 #define CCL_WriteConstJump 0x08 /* Write constant and jump:
184 1:A--D--D--R--E--S--S-000XXXXX
186 ------------------------------
191 #define CCL_WriteConstReadJump 0x09 /* Write constant, read, and jump:
192 1:A--D--D--R--E--S--S-rrrXXXXX
194 3:A--D--D--R--E--S--S-rrrYYYYY
195 -----------------------------
201 /* Note: If read is suspended, the resumed execution starts from the
202 second code (YYYYY == CCL_ReadJump). */
204 #define CCL_WriteStringJump 0x0A /* Write string and jump:
205 1:A--D--D--R--E--S--S-000XXXXX
207 3:0000STRIN[0]STRIN[1]STRIN[2]
209 ------------------------------
210 write_string (STRING, LENGTH);
214 #define CCL_WriteArrayReadJump 0x0B /* Write an array element, read, and jump:
215 1:A--D--D--R--E--S--S-rrrXXXXX
220 N:A--D--D--R--E--S--S-rrrYYYYY
221 ------------------------------
222 if (0 <= reg[rrr] < LENGTH)
223 write (ELEMENT[reg[rrr]]);
224 IC += LENGTH + 2; (... pointing at N+1)
228 /* Note: If read is suspended, the resumed execution starts from the
229 Nth code (YYYYY == CCL_ReadJump). */
231 #define CCL_ReadJump 0x0C /* Read and jump:
232 1:A--D--D--R--E--S--S-rrrYYYYY
233 -----------------------------
238 #define CCL_Branch 0x0D /* Jump by branch table:
239 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
240 2:A--D--D--R--E-S-S[0]000XXXXX
241 3:A--D--D--R--E-S-S[1]000XXXXX
243 ------------------------------
244 if (0 <= reg[rrr] < CC..C)
245 IC += ADDRESS[reg[rrr]];
247 IC += ADDRESS[CC..C];
250 #define CCL_ReadRegister 0x0E /* Read bytes into registers:
251 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
252 2:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
254 ------------------------------
259 #define CCL_WriteExprConst 0x0F /* write result of expression:
260 1:00000OPERATION000RRR000XXXXX
262 ------------------------------
263 write (reg[RRR] OPERATION CONSTANT);
267 /* Note: If the Nth read is suspended, the resumed execution starts
268 from the Nth code. */
270 #define CCL_ReadBranch 0x10 /* Read one byte into a register,
271 and jump by branch table:
272 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
273 2:A--D--D--R--E-S-S[0]000XXXXX
274 3:A--D--D--R--E-S-S[1]000XXXXX
276 ------------------------------
278 if (0 <= reg[rrr] < CC..C)
279 IC += ADDRESS[reg[rrr]];
281 IC += ADDRESS[CC..C];
284 #define CCL_WriteRegister 0x11 /* Write registers:
285 1:CCCCCCCCCCCCCCCCCCCrrrXXXXX
286 2:CCCCCCCCCCCCCCCCCCCrrrXXXXX
288 ------------------------------
294 /* Note: If the Nth write is suspended, the resumed execution
295 starts from the Nth code. */
297 #define CCL_WriteExprRegister 0x12 /* Write result of expression
298 1:00000OPERATIONRrrRRR000XXXXX
299 ------------------------------
300 write (reg[RRR] OPERATION reg[Rrr]);
303 #define CCL_Call 0x13 /* Call the CCL program whose ID is
305 1:CCCCCCCCCCCCCCCCCCCCFFFXXXXX
306 [2:00000000cccccccccccccccccccc]
307 ------------------------------
315 #define CCL_WriteConstString 0x14 /* Write a constant or a string:
316 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
317 [2:0000STRIN[0]STRIN[1]STRIN[2]]
319 -----------------------------
323 write_string (STRING, CC..C);
324 IC += (CC..C + 2) / 3;
327 #define CCL_WriteArray 0x15 /* Write an element of array:
328 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
332 ------------------------------
333 if (0 <= reg[rrr] < CC..C)
334 write (ELEMENT[reg[rrr]]);
338 #define CCL_End 0x16 /* Terminate:
339 1:00000000000000000000000XXXXX
340 ------------------------------
344 /* The following two codes execute an assignment arithmetic/logical
345 operation. The form of the operation is like REG OP= OPERAND. */
347 #define CCL_ExprSelfConst 0x17 /* REG OP= constant:
348 1:00000OPERATION000000rrrXXXXX
350 ------------------------------
351 reg[rrr] OPERATION= CONSTANT;
354 #define CCL_ExprSelfReg 0x18 /* REG1 OP= REG2:
355 1:00000OPERATION000RRRrrrXXXXX
356 ------------------------------
357 reg[rrr] OPERATION= reg[RRR];
360 /* The following codes execute an arithmetic/logical operation. The
361 form of the operation is like REG_X = REG_Y OP OPERAND2. */
363 #define CCL_SetExprConst 0x19 /* REG_X = REG_Y OP constant:
364 1:00000OPERATION000RRRrrrXXXXX
366 ------------------------------
367 reg[rrr] = reg[RRR] OPERATION CONSTANT;
371 #define CCL_SetExprReg 0x1A /* REG1 = REG2 OP REG3:
372 1:00000OPERATIONRrrRRRrrrXXXXX
373 ------------------------------
374 reg[rrr] = reg[RRR] OPERATION reg[Rrr];
377 #define CCL_JumpCondExprConst 0x1B /* Jump conditional according to
378 an operation on constant:
379 1:A--D--D--R--E--S--S-rrrXXXXX
382 -----------------------------
383 reg[7] = reg[rrr] OPERATION CONSTANT;
390 #define CCL_JumpCondExprReg 0x1C /* Jump conditional according to
391 an operation on register:
392 1:A--D--D--R--E--S--S-rrrXXXXX
395 -----------------------------
396 reg[7] = reg[rrr] OPERATION reg[RRR];
403 #define CCL_ReadJumpCondExprConst 0x1D /* Read and jump conditional according
404 to an operation on constant:
405 1:A--D--D--R--E--S--S-rrrXXXXX
408 -----------------------------
410 reg[7] = reg[rrr] OPERATION CONSTANT;
417 #define CCL_ReadJumpCondExprReg 0x1E /* Read and jump conditional according
418 to an operation on register:
419 1:A--D--D--R--E--S--S-rrrXXXXX
422 -----------------------------
424 reg[7] = reg[rrr] OPERATION reg[RRR];
431 #define CCL_Extension 0x1F /* Extended CCL code
432 1:ExtendedCOMMNDRrrRRRrrrXXXXX
435 ------------------------------
436 extended_command (rrr,RRR,Rrr,ARGS)
440 Here after, Extended CCL Instructions.
441 Bit length of extended command is 14.
442 Therefore, the instruction code range is 0..16384(0x3fff).
445 /* Read a multibyte characeter.
446 A code point is stored into reg[rrr]. A charset ID is stored into
449 #define CCL_ReadMultibyteChar2 0x00 /* Read Multibyte Character
450 1:ExtendedCOMMNDRrrRRRrrrXXXXX */
452 /* Write a multibyte character.
453 Write a character whose code point is reg[rrr] and the charset ID
456 #define CCL_WriteMultibyteChar2 0x01 /* Write Multibyte Character
457 1:ExtendedCOMMNDRrrRRRrrrXXXXX */
459 /* Translate a character whose code point is reg[rrr] and the charset
460 ID is reg[RRR] by a translation table whose ID is reg[Rrr].
462 A translated character is set in reg[rrr] (code point) and reg[RRR]
465 #define CCL_TranslateCharacter 0x02 /* Translate a multibyte character
466 1:ExtendedCOMMNDRrrRRRrrrXXXXX */
468 /* Translate a character whose code point is reg[rrr] and the charset
469 ID is reg[RRR] by a translation table whose ID is ARGUMENT.
471 A translated character is set in reg[rrr] (code point) and reg[RRR]
474 #define CCL_TranslateCharacterConstTbl 0x03 /* Translate a multibyte character
475 1:ExtendedCOMMNDRrrRRRrrrXXXXX
476 2:ARGUMENT(Translation Table ID)
479 /* Iterate looking up MAPs for reg[rrr] starting from the Nth (N =
480 reg[RRR]) MAP until some value is found.
482 Each MAP is a Lisp vector whose element is number, nil, t, or
484 If the element is nil, ignore the map and proceed to the next map.
485 If the element is t or lambda, finish without changing reg[rrr].
486 If the element is a number, set reg[rrr] to the number and finish.
488 Detail of the map structure is descibed in the comment for
489 CCL_MapMultiple below. */
491 #define CCL_IterateMultipleMap 0x10 /* Iterate multiple maps
492 1:ExtendedCOMMNDXXXRRRrrrXXXXX
499 /* Map the code in reg[rrr] by MAPs starting from the Nth (N =
502 MAPs are supplied in the succeeding CCL codes as follows:
504 When CCL program gives this nested structure of map to this command:
507 (MAP-ID121 MAP-ID122 MAP-ID123)
510 (MAP-ID211 (MAP-ID2111) MAP-ID212)
512 the compiled CCL codes has this sequence:
513 CCL_MapMultiple (CCL code of this command)
514 16 (total number of MAPs and SEPARATORs)
532 A value of each SEPARATOR follows this rule:
533 MAP-SET := SEPARATOR [(MAP-ID | MAP-SET)]+
534 SEPARATOR := -(number of MAP-IDs and SEPARATORs in the MAP-SET)
536 (*)....Nest level of MAP-SET must not be over than MAX_MAP_SET_LEVEL.
538 When some map fails to map (i.e. it doesn't have a value for
539 reg[rrr]), the mapping is treated as identity.
541 The mapping is iterated for all maps in each map set (set of maps
542 separated by SEPARATOR) except in the case that lambda is
543 encountered. More precisely, the mapping proceeds as below:
545 At first, VAL0 is set to reg[rrr], and it is translated by the
546 first map to VAL1. Then, VAL1 is translated by the next map to
547 VAL2. This mapping is iterated until the last map is used. The
548 result of the mapping is the last value of VAL?. When the mapping
549 process reached to the end of the map set, it moves to the next
550 map set. If the next does not exit, the mapping process terminates,
551 and regard the last value as a result.
553 But, when VALm is mapped to VALn and VALn is not a number, the
554 mapping proceed as below:
556 If VALn is nil, the lastest map is ignored and the mapping of VALm
557 proceed to the next map.
559 In VALn is t, VALm is reverted to reg[rrr] and the mapping of VALm
560 proceed to the next map.
562 If VALn is lambda, move to the next map set like reaching to the
563 end of the current map set.
565 If VALn is a symbol, call the CCL program refered by it.
566 Then, use reg[rrr] as a mapped value except for -1, -2 and -3.
567 Such special values are regarded as nil, t, and lambda respectively.
569 Each map is a Lisp vector of the following format (a) or (b):
570 (a)......[STARTPOINT VAL1 VAL2 ...]
571 (b)......[t VAL STARTPOINT ENDPOINT],
573 STARTPOINT is an offset to be used for indexing a map,
574 ENDPOINT is a maximum index number of a map,
575 VAL and VALn is a number, nil, t, or lambda.
577 Valid index range of a map of type (a) is:
578 STARTPOINT <= index < STARTPOINT + map_size - 1
579 Valid index range of a map of type (b) is:
580 STARTPOINT <= index < ENDPOINT */
582 #define CCL_MapMultiple 0x11 /* Mapping by multiple code conversion maps
583 1:ExtendedCOMMNDXXXRRRrrrXXXXX
595 #define MAX_MAP_SET_LEVEL 30
603 static tr_stack mapping_stack
[MAX_MAP_SET_LEVEL
];
604 static tr_stack
*mapping_stack_pointer
;
606 /* If this variable is non-zero, it indicates the stack_idx
607 of immediately called by CCL_MapMultiple. */
608 static int stack_idx_of_map_multiple
;
610 #define PUSH_MAPPING_STACK(restlen, orig) \
613 mapping_stack_pointer->rest_length = (restlen); \
614 mapping_stack_pointer->orig_val = (orig); \
615 mapping_stack_pointer++; \
619 #define POP_MAPPING_STACK(restlen, orig) \
622 mapping_stack_pointer--; \
623 (restlen) = mapping_stack_pointer->rest_length; \
624 (orig) = mapping_stack_pointer->orig_val; \
628 #define CCL_CALL_FOR_MAP_INSTRUCTION(symbol, ret_ic) \
631 struct ccl_program called_ccl; \
632 if (stack_idx >= 256 \
633 || (setup_ccl_program (&called_ccl, (symbol)) != 0)) \
637 ccl_prog = ccl_prog_stack_struct[0].ccl_prog; \
638 ic = ccl_prog_stack_struct[0].ic; \
642 ccl_prog_stack_struct[stack_idx].ccl_prog = ccl_prog; \
643 ccl_prog_stack_struct[stack_idx].ic = (ret_ic); \
645 ccl_prog = called_ccl.prog; \
646 ic = CCL_HEADER_MAIN; \
651 #define CCL_MapSingle 0x12 /* Map by single code conversion map
652 1:ExtendedCOMMNDXXXRRRrrrXXXXX
654 ------------------------------
655 Map reg[rrr] by MAP-ID.
656 If some valid mapping is found,
657 set reg[rrr] to the result,
662 #define CCL_LookupIntConstTbl 0x13 /* Lookup multibyte character by
663 integer key. Afterwards R7 set
664 to 1 iff lookup succeeded.
665 1:ExtendedCOMMNDRrrRRRXXXXXXXX
666 2:ARGUMENT(Hash table ID) */
668 #define CCL_LookupCharConstTbl 0x14 /* Lookup integer by multibyte
669 character key. Afterwards R7 set
670 to 1 iff lookup succeeded.
671 1:ExtendedCOMMNDRrrRRRrrrXXXXX
672 2:ARGUMENT(Hash table ID) */
674 /* CCL arithmetic/logical operators. */
675 #define CCL_PLUS 0x00 /* X = Y + Z */
676 #define CCL_MINUS 0x01 /* X = Y - Z */
677 #define CCL_MUL 0x02 /* X = Y * Z */
678 #define CCL_DIV 0x03 /* X = Y / Z */
679 #define CCL_MOD 0x04 /* X = Y % Z */
680 #define CCL_AND 0x05 /* X = Y & Z */
681 #define CCL_OR 0x06 /* X = Y | Z */
682 #define CCL_XOR 0x07 /* X = Y ^ Z */
683 #define CCL_LSH 0x08 /* X = Y << Z */
684 #define CCL_RSH 0x09 /* X = Y >> Z */
685 #define CCL_LSH8 0x0A /* X = (Y << 8) | Z */
686 #define CCL_RSH8 0x0B /* X = Y >> 8, r[7] = Y & 0xFF */
687 #define CCL_DIVMOD 0x0C /* X = Y / Z, r[7] = Y % Z */
688 #define CCL_LS 0x10 /* X = (X < Y) */
689 #define CCL_GT 0x11 /* X = (X > Y) */
690 #define CCL_EQ 0x12 /* X = (X == Y) */
691 #define CCL_LE 0x13 /* X = (X <= Y) */
692 #define CCL_GE 0x14 /* X = (X >= Y) */
693 #define CCL_NE 0x15 /* X = (X != Y) */
695 #define CCL_DECODE_SJIS 0x16 /* X = HIGHER_BYTE (DE-SJIS (Y, Z))
696 r[7] = LOWER_BYTE (DE-SJIS (Y, Z)) */
697 #define CCL_ENCODE_SJIS 0x17 /* X = HIGHER_BYTE (SJIS (Y, Z))
698 r[7] = LOWER_BYTE (SJIS (Y, Z) */
700 /* Terminate CCL program successfully. */
701 #define CCL_SUCCESS \
704 ccl->status = CCL_STAT_SUCCESS; \
709 /* Suspend CCL program because of reading from empty input buffer or
710 writing to full output buffer. When this program is resumed, the
711 same I/O command is executed. */
712 #define CCL_SUSPEND(stat) \
716 ccl->status = stat; \
721 /* Terminate CCL program because of invalid command. Should not occur
722 in the normal case. */
723 #define CCL_INVALID_CMD \
726 ccl->status = CCL_STAT_INVALID_CMD; \
727 goto ccl_error_handler; \
731 /* Encode one character CH to multibyte form and write to the current
732 output buffer. If CH is less than 256, CH is written as is. */
733 #define CCL_WRITE_CHAR(ch) \
737 else if (dst < dst_end) \
740 CCL_SUSPEND (CCL_STAT_SUSPEND_BY_DST); \
743 /* Write a string at ccl_prog[IC] of length LEN to the current output
745 #define CCL_WRITE_STRING(len) \
750 else if (dst + len <= dst_end) \
751 for (i = 0; i < len; i++) \
752 *dst++ = ((XFASTINT (ccl_prog[ic + (i / 3)])) \
753 >> ((2 - (i % 3)) * 8)) & 0xFF; \
755 CCL_SUSPEND (CCL_STAT_SUSPEND_BY_DST); \
758 /* Read one byte from the current input buffer into Rth register. */
759 #define CCL_READ_CHAR(r) \
763 else if (src < src_end) \
765 else if (ccl->last_block) \
771 CCL_SUSPEND (CCL_STAT_SUSPEND_BY_SRC); \
774 /* Decode CODE by a charset whose id is ID. If ID is 0, return CODE
775 as is for backward compatibility. Assume that we can use the
776 variable `charset'. */
778 #define CCL_DECODE_CHAR(id, code) \
779 ((id) == 0 ? (code) \
780 : (charset = CHARSET_FROM_ID ((id)), DECODE_CHAR (charset, (code))))
783 /* Encode character C by some of charsets in CHARSET_LIST. Set ID to
784 the id of the used charset, ENCODED to the resulf of encoding.
785 Assume that we can use the variable `charset'. */
787 #define CCL_ENCODE_CHAR(c, charset_list, id, encoded) \
791 charset = char_charset ((c), (charset_list), &code); \
792 if (! charset && ! NILP (charset_list)) \
793 charset = char_charset ((c), Qnil, &code); \
796 (id) = CHARSET_ID (charset); \
803 /* Execute CCL code on characters at SOURCE (length SRC_SIZE). The
804 resulting text goes to a place pointed by DESTINATION, the length
805 of which should not exceed DST_SIZE. As a side effect, how many
806 characters are consumed and produced are recorded in CCL->consumed
807 and CCL->produced, and the contents of CCL registers are updated.
808 If SOURCE or DESTINATION is NULL, only operations on registers are
812 #define CCL_DEBUG_BACKTRACE_LEN 256
813 int ccl_backtrace_table
[CCL_BACKTRACE_TABLE
];
814 int ccl_backtrace_idx
;
817 struct ccl_prog_stack
819 Lisp_Object
*ccl_prog
; /* Pointer to an array of CCL code. */
820 int ic
; /* Instruction Counter. */
823 /* For the moment, we only support depth 256 of stack. */
824 static struct ccl_prog_stack ccl_prog_stack_struct
[256];
827 ccl_driver (ccl
, source
, destination
, src_size
, dst_size
, charset_list
)
828 struct ccl_program
*ccl
;
829 int *source
, *destination
;
830 int src_size
, dst_size
;
831 Lisp_Object charset_list
;
833 register int *reg
= ccl
->reg
;
834 register int ic
= ccl
->ic
;
835 register int code
= 0, field1
, field2
;
836 register Lisp_Object
*ccl_prog
= ccl
->prog
;
837 int *src
= source
, *src_end
= src
+ src_size
;
838 int *dst
= destination
, *dst_end
= dst
+ dst_size
;
841 int stack_idx
= ccl
->stack_idx
;
842 /* Instruction counter of the current CCL code. */
844 struct charset
*charset
;
846 if (ic
>= ccl
->eof_ic
)
847 ic
= CCL_HEADER_MAIN
;
849 if (ccl
->buf_magnification
== 0) /* We can't read/produce any bytes. */
852 /* Set mapping stack pointer. */
853 mapping_stack_pointer
= mapping_stack
;
856 ccl_backtrace_idx
= 0;
863 ccl_backtrace_table
[ccl_backtrace_idx
++] = ic
;
864 if (ccl_backtrace_idx
>= CCL_DEBUG_BACKTRACE_LEN
)
865 ccl_backtrace_idx
= 0;
866 ccl_backtrace_table
[ccl_backtrace_idx
] = 0;
869 if (!NILP (Vquit_flag
) && NILP (Vinhibit_quit
))
871 /* We can't just signal Qquit, instead break the loop as if
872 the whole data is processed. Don't reset Vquit_flag, it
873 must be handled later at a safer place. */
875 src
= source
+ src_size
;
876 ccl
->status
= CCL_STAT_QUIT
;
881 code
= XINT (ccl_prog
[ic
]); ic
++;
883 field2
= (code
& 0xFF) >> 5;
886 #define RRR (field1 & 7)
887 #define Rrr ((field1 >> 3) & 7)
889 #define EXCMD (field1 >> 6)
893 case CCL_SetRegister
: /* 00000000000000000RRRrrrXXXXX */
897 case CCL_SetShortConst
: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
901 case CCL_SetConst
: /* 00000000000000000000rrrXXXXX */
902 reg
[rrr
] = XINT (ccl_prog
[ic
]);
906 case CCL_SetArray
: /* CCCCCCCCCCCCCCCCCCCCRRRrrrXXXXX */
909 if ((unsigned int) i
< j
)
910 reg
[rrr
] = XINT (ccl_prog
[ic
+ i
]);
914 case CCL_Jump
: /* A--D--D--R--E--S--S-000XXXXX */
918 case CCL_JumpCond
: /* A--D--D--R--E--S--S-rrrXXXXX */
923 case CCL_WriteRegisterJump
: /* A--D--D--R--E--S--S-rrrXXXXX */
929 case CCL_WriteRegisterReadJump
: /* A--D--D--R--E--S--S-rrrXXXXX */
933 CCL_READ_CHAR (reg
[rrr
]);
937 case CCL_WriteConstJump
: /* A--D--D--R--E--S--S-000XXXXX */
938 i
= XINT (ccl_prog
[ic
]);
943 case CCL_WriteConstReadJump
: /* A--D--D--R--E--S--S-rrrXXXXX */
944 i
= XINT (ccl_prog
[ic
]);
947 CCL_READ_CHAR (reg
[rrr
]);
951 case CCL_WriteStringJump
: /* A--D--D--R--E--S--S-000XXXXX */
952 j
= XINT (ccl_prog
[ic
]);
954 CCL_WRITE_STRING (j
);
958 case CCL_WriteArrayReadJump
: /* A--D--D--R--E--S--S-rrrXXXXX */
960 j
= XINT (ccl_prog
[ic
]);
961 if ((unsigned int) i
< j
)
963 i
= XINT (ccl_prog
[ic
+ 1 + i
]);
967 CCL_READ_CHAR (reg
[rrr
]);
968 ic
+= ADDR
- (j
+ 2);
971 case CCL_ReadJump
: /* A--D--D--R--E--S--S-rrrYYYYY */
972 CCL_READ_CHAR (reg
[rrr
]);
976 case CCL_ReadBranch
: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
977 CCL_READ_CHAR (reg
[rrr
]);
978 /* fall through ... */
979 case CCL_Branch
: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
980 if ((unsigned int) reg
[rrr
] < field1
)
981 ic
+= XINT (ccl_prog
[ic
+ reg
[rrr
]]);
983 ic
+= XINT (ccl_prog
[ic
+ field1
]);
986 case CCL_ReadRegister
: /* CCCCCCCCCCCCCCCCCCCCrrXXXXX */
989 CCL_READ_CHAR (reg
[rrr
]);
991 code
= XINT (ccl_prog
[ic
]); ic
++;
993 field2
= (code
& 0xFF) >> 5;
997 case CCL_WriteExprConst
: /* 1:00000OPERATION000RRR000XXXXX */
1000 j
= XINT (ccl_prog
[ic
]);
1002 jump_address
= ic
+ 1;
1005 case CCL_WriteRegister
: /* CCCCCCCCCCCCCCCCCCCrrrXXXXX */
1011 code
= XINT (ccl_prog
[ic
]); ic
++;
1013 field2
= (code
& 0xFF) >> 5;
1017 case CCL_WriteExprRegister
: /* 1:00000OPERATIONRrrRRR000XXXXX */
1025 case CCL_Call
: /* 1:CCCCCCCCCCCCCCCCCCCCFFFXXXXX */
1030 /* If FFF is nonzero, the CCL program ID is in the
1034 prog_id
= XINT (ccl_prog
[ic
]);
1040 if (stack_idx
>= 256
1042 || prog_id
>= ASIZE (Vccl_program_table
)
1043 || (slot
= AREF (Vccl_program_table
, prog_id
), !VECTORP (slot
))
1044 || !VECTORP (AREF (slot
, 1)))
1048 ccl_prog
= ccl_prog_stack_struct
[0].ccl_prog
;
1049 ic
= ccl_prog_stack_struct
[0].ic
;
1054 ccl_prog_stack_struct
[stack_idx
].ccl_prog
= ccl_prog
;
1055 ccl_prog_stack_struct
[stack_idx
].ic
= ic
;
1057 ccl_prog
= XVECTOR (AREF (slot
, 1))->contents
;
1058 ic
= CCL_HEADER_MAIN
;
1062 case CCL_WriteConstString
: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
1064 CCL_WRITE_CHAR (field1
);
1067 CCL_WRITE_STRING (field1
);
1068 ic
+= (field1
+ 2) / 3;
1072 case CCL_WriteArray
: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
1074 if ((unsigned int) i
< field1
)
1076 j
= XINT (ccl_prog
[ic
+ i
]);
1082 case CCL_End
: /* 0000000000000000000000XXXXX */
1086 ccl_prog
= ccl_prog_stack_struct
[stack_idx
].ccl_prog
;
1087 ic
= ccl_prog_stack_struct
[stack_idx
].ic
;
1092 /* ccl->ic should points to this command code again to
1093 suppress further processing. */
1097 case CCL_ExprSelfConst
: /* 00000OPERATION000000rrrXXXXX */
1098 i
= XINT (ccl_prog
[ic
]);
1103 case CCL_ExprSelfReg
: /* 00000OPERATION000RRRrrrXXXXX */
1110 case CCL_PLUS
: reg
[rrr
] += i
; break;
1111 case CCL_MINUS
: reg
[rrr
] -= i
; break;
1112 case CCL_MUL
: reg
[rrr
] *= i
; break;
1113 case CCL_DIV
: reg
[rrr
] /= i
; break;
1114 case CCL_MOD
: reg
[rrr
] %= i
; break;
1115 case CCL_AND
: reg
[rrr
] &= i
; break;
1116 case CCL_OR
: reg
[rrr
] |= i
; break;
1117 case CCL_XOR
: reg
[rrr
] ^= i
; break;
1118 case CCL_LSH
: reg
[rrr
] <<= i
; break;
1119 case CCL_RSH
: reg
[rrr
] >>= i
; break;
1120 case CCL_LSH8
: reg
[rrr
] <<= 8; reg
[rrr
] |= i
; break;
1121 case CCL_RSH8
: reg
[7] = reg
[rrr
] & 0xFF; reg
[rrr
] >>= 8; break;
1122 case CCL_DIVMOD
: reg
[7] = reg
[rrr
] % i
; reg
[rrr
] /= i
; break;
1123 case CCL_LS
: reg
[rrr
] = reg
[rrr
] < i
; break;
1124 case CCL_GT
: reg
[rrr
] = reg
[rrr
] > i
; break;
1125 case CCL_EQ
: reg
[rrr
] = reg
[rrr
] == i
; break;
1126 case CCL_LE
: reg
[rrr
] = reg
[rrr
] <= i
; break;
1127 case CCL_GE
: reg
[rrr
] = reg
[rrr
] >= i
; break;
1128 case CCL_NE
: reg
[rrr
] = reg
[rrr
] != i
; break;
1129 default: CCL_INVALID_CMD
;
1133 case CCL_SetExprConst
: /* 00000OPERATION000RRRrrrXXXXX */
1135 j
= XINT (ccl_prog
[ic
]);
1137 jump_address
= ++ic
;
1140 case CCL_SetExprReg
: /* 00000OPERATIONRrrRRRrrrXXXXX */
1147 case CCL_ReadJumpCondExprConst
: /* A--D--D--R--E--S--S-rrrXXXXX */
1148 CCL_READ_CHAR (reg
[rrr
]);
1149 case CCL_JumpCondExprConst
: /* A--D--D--R--E--S--S-rrrXXXXX */
1151 op
= XINT (ccl_prog
[ic
]);
1152 jump_address
= ic
++ + ADDR
;
1153 j
= XINT (ccl_prog
[ic
]);
1158 case CCL_ReadJumpCondExprReg
: /* A--D--D--R--E--S--S-rrrXXXXX */
1159 CCL_READ_CHAR (reg
[rrr
]);
1160 case CCL_JumpCondExprReg
:
1162 op
= XINT (ccl_prog
[ic
]);
1163 jump_address
= ic
++ + ADDR
;
1164 j
= reg
[XINT (ccl_prog
[ic
])];
1171 case CCL_PLUS
: reg
[rrr
] = i
+ j
; break;
1172 case CCL_MINUS
: reg
[rrr
] = i
- j
; break;
1173 case CCL_MUL
: reg
[rrr
] = i
* j
; break;
1174 case CCL_DIV
: reg
[rrr
] = i
/ j
; break;
1175 case CCL_MOD
: reg
[rrr
] = i
% j
; break;
1176 case CCL_AND
: reg
[rrr
] = i
& j
; break;
1177 case CCL_OR
: reg
[rrr
] = i
| j
; break;
1178 case CCL_XOR
: reg
[rrr
] = i
^ j
;; break;
1179 case CCL_LSH
: reg
[rrr
] = i
<< j
; break;
1180 case CCL_RSH
: reg
[rrr
] = i
>> j
; break;
1181 case CCL_LSH8
: reg
[rrr
] = (i
<< 8) | j
; break;
1182 case CCL_RSH8
: reg
[rrr
] = i
>> 8; reg
[7] = i
& 0xFF; break;
1183 case CCL_DIVMOD
: reg
[rrr
] = i
/ j
; reg
[7] = i
% j
; break;
1184 case CCL_LS
: reg
[rrr
] = i
< j
; break;
1185 case CCL_GT
: reg
[rrr
] = i
> j
; break;
1186 case CCL_EQ
: reg
[rrr
] = i
== j
; break;
1187 case CCL_LE
: reg
[rrr
] = i
<= j
; break;
1188 case CCL_GE
: reg
[rrr
] = i
>= j
; break;
1189 case CCL_NE
: reg
[rrr
] = i
!= j
; break;
1190 case CCL_DECODE_SJIS
:
1198 case CCL_ENCODE_SJIS
:
1206 default: CCL_INVALID_CMD
;
1209 if (code
== CCL_WriteExprConst
|| code
== CCL_WriteExprRegister
)
1222 case CCL_ReadMultibyteChar2
:
1226 CCL_ENCODE_CHAR (i
, charset_list
, reg
[RRR
], reg
[rrr
]);
1229 case CCL_WriteMultibyteChar2
:
1232 i
= CCL_DECODE_CHAR (reg
[RRR
], reg
[rrr
]);
1236 case CCL_TranslateCharacter
:
1237 i
= CCL_DECODE_CHAR (reg
[RRR
], reg
[rrr
]);
1238 op
= translate_char (GET_TRANSLATION_TABLE (reg
[Rrr
]), i
);
1239 CCL_ENCODE_CHAR (op
, charset_list
, reg
[RRR
], reg
[rrr
]);
1242 case CCL_TranslateCharacterConstTbl
:
1243 op
= XINT (ccl_prog
[ic
]); /* table */
1245 i
= CCL_DECODE_CHAR (reg
[RRR
], reg
[rrr
]);
1246 op
= translate_char (GET_TRANSLATION_TABLE (op
), i
);
1247 CCL_ENCODE_CHAR (op
, charset_list
, reg
[RRR
], reg
[rrr
]);
1250 case CCL_LookupIntConstTbl
:
1251 op
= XINT (ccl_prog
[ic
]); /* table */
1254 struct Lisp_Hash_Table
*h
= GET_HASH_TABLE (op
);
1256 op
= hash_lookup (h
, make_number (reg
[RRR
]), NULL
);
1260 opl
= HASH_VALUE (h
, op
);
1261 if (!CHARACTERP (opl
))
1263 reg
[RRR
] = charset_unicode
;
1265 reg
[7] = 1; /* r7 true for success */
1272 case CCL_LookupCharConstTbl
:
1273 op
= XINT (ccl_prog
[ic
]); /* table */
1275 i
= CCL_DECODE_CHAR (reg
[RRR
], reg
[rrr
]);
1277 struct Lisp_Hash_Table
*h
= GET_HASH_TABLE (op
);
1279 op
= hash_lookup (h
, make_number (i
), NULL
);
1283 opl
= HASH_VALUE (h
, op
);
1284 if (!INTEGERP (opl
))
1286 reg
[RRR
] = XINT (opl
);
1287 reg
[7] = 1; /* r7 true for success */
1294 case CCL_IterateMultipleMap
:
1296 Lisp_Object map
, content
, attrib
, value
;
1297 int point
, size
, fin_ic
;
1299 j
= XINT (ccl_prog
[ic
++]); /* number of maps. */
1302 if ((j
> reg
[RRR
]) && (j
>= 0))
1317 size
= ASIZE (Vcode_conversion_map_vector
);
1318 point
= XINT (ccl_prog
[ic
++]);
1319 if (point
>= size
) continue;
1320 map
= AREF (Vcode_conversion_map_vector
, point
);
1322 /* Check map varidity. */
1323 if (!CONSP (map
)) continue;
1325 if (!VECTORP (map
)) continue;
1327 if (size
<= 1) continue;
1329 content
= AREF (map
, 0);
1332 [STARTPOINT VAL1 VAL2 ...] or
1333 [t ELELMENT STARTPOINT ENDPOINT] */
1334 if (NUMBERP (content
))
1336 point
= XUINT (content
);
1337 point
= op
- point
+ 1;
1338 if (!((point
>= 1) && (point
< size
))) continue;
1339 content
= AREF (map
, point
);
1341 else if (EQ (content
, Qt
))
1343 if (size
!= 4) continue;
1344 if ((op
>= XUINT (AREF (map
, 2)))
1345 && (op
< XUINT (AREF (map
, 3))))
1346 content
= AREF (map
, 1);
1355 else if (NUMBERP (content
))
1358 reg
[rrr
] = XINT(content
);
1361 else if (EQ (content
, Qt
) || EQ (content
, Qlambda
))
1366 else if (CONSP (content
))
1368 attrib
= XCAR (content
);
1369 value
= XCDR (content
);
1370 if (!NUMBERP (attrib
) || !NUMBERP (value
))
1373 reg
[rrr
] = XUINT (value
);
1376 else if (SYMBOLP (content
))
1377 CCL_CALL_FOR_MAP_INSTRUCTION (content
, fin_ic
);
1387 case CCL_MapMultiple
:
1389 Lisp_Object map
, content
, attrib
, value
;
1390 int point
, size
, map_vector_size
;
1391 int map_set_rest_length
, fin_ic
;
1392 int current_ic
= this_ic
;
1394 /* inhibit recursive call on MapMultiple. */
1395 if (stack_idx_of_map_multiple
> 0)
1397 if (stack_idx_of_map_multiple
<= stack_idx
)
1399 stack_idx_of_map_multiple
= 0;
1400 mapping_stack_pointer
= mapping_stack
;
1405 mapping_stack_pointer
= mapping_stack
;
1406 stack_idx_of_map_multiple
= 0;
1408 map_set_rest_length
=
1409 XINT (ccl_prog
[ic
++]); /* number of maps and separators. */
1410 fin_ic
= ic
+ map_set_rest_length
;
1413 if ((map_set_rest_length
> reg
[RRR
]) && (reg
[RRR
] >= 0))
1417 map_set_rest_length
-= i
;
1423 mapping_stack_pointer
= mapping_stack
;
1427 if (mapping_stack_pointer
<= (mapping_stack
+ 1))
1429 /* Set up initial state. */
1430 mapping_stack_pointer
= mapping_stack
;
1431 PUSH_MAPPING_STACK (0, op
);
1436 /* Recover after calling other ccl program. */
1439 POP_MAPPING_STACK (map_set_rest_length
, orig_op
);
1440 POP_MAPPING_STACK (map_set_rest_length
, reg
[rrr
]);
1444 /* Regard it as Qnil. */
1448 map_set_rest_length
--;
1451 /* Regard it as Qt. */
1455 map_set_rest_length
--;
1458 /* Regard it as Qlambda. */
1460 i
+= map_set_rest_length
;
1461 ic
+= map_set_rest_length
;
1462 map_set_rest_length
= 0;
1465 /* Regard it as normal mapping. */
1466 i
+= map_set_rest_length
;
1467 ic
+= map_set_rest_length
;
1468 POP_MAPPING_STACK (map_set_rest_length
, reg
[rrr
]);
1472 map_vector_size
= ASIZE (Vcode_conversion_map_vector
);
1475 for (;map_set_rest_length
> 0;i
++, ic
++, map_set_rest_length
--)
1477 point
= XINT(ccl_prog
[ic
]);
1480 /* +1 is for including separator. */
1482 if (mapping_stack_pointer
1483 >= &mapping_stack
[MAX_MAP_SET_LEVEL
])
1485 PUSH_MAPPING_STACK (map_set_rest_length
- point
,
1487 map_set_rest_length
= point
;
1492 if (point
>= map_vector_size
) continue;
1493 map
= AREF (Vcode_conversion_map_vector
, point
);
1495 /* Check map varidity. */
1496 if (!CONSP (map
)) continue;
1498 if (!VECTORP (map
)) continue;
1500 if (size
<= 1) continue;
1502 content
= AREF (map
, 0);
1505 [STARTPOINT VAL1 VAL2 ...] or
1506 [t ELEMENT STARTPOINT ENDPOINT] */
1507 if (NUMBERP (content
))
1509 point
= XUINT (content
);
1510 point
= op
- point
+ 1;
1511 if (!((point
>= 1) && (point
< size
))) continue;
1512 content
= AREF (map
, point
);
1514 else if (EQ (content
, Qt
))
1516 if (size
!= 4) continue;
1517 if ((op
>= XUINT (AREF (map
, 2))) &&
1518 (op
< XUINT (AREF (map
, 3))))
1519 content
= AREF (map
, 1);
1530 if (NUMBERP (content
))
1532 op
= XINT (content
);
1533 i
+= map_set_rest_length
- 1;
1534 ic
+= map_set_rest_length
- 1;
1535 POP_MAPPING_STACK (map_set_rest_length
, reg
[rrr
]);
1536 map_set_rest_length
++;
1538 else if (CONSP (content
))
1540 attrib
= XCAR (content
);
1541 value
= XCDR (content
);
1542 if (!NUMBERP (attrib
) || !NUMBERP (value
))
1545 i
+= map_set_rest_length
- 1;
1546 ic
+= map_set_rest_length
- 1;
1547 POP_MAPPING_STACK (map_set_rest_length
, reg
[rrr
]);
1548 map_set_rest_length
++;
1550 else if (EQ (content
, Qt
))
1554 else if (EQ (content
, Qlambda
))
1556 i
+= map_set_rest_length
;
1557 ic
+= map_set_rest_length
;
1560 else if (SYMBOLP (content
))
1562 if (mapping_stack_pointer
1563 >= &mapping_stack
[MAX_MAP_SET_LEVEL
])
1565 PUSH_MAPPING_STACK (map_set_rest_length
, reg
[rrr
]);
1566 PUSH_MAPPING_STACK (map_set_rest_length
, op
);
1567 stack_idx_of_map_multiple
= stack_idx
+ 1;
1568 CCL_CALL_FOR_MAP_INSTRUCTION (content
, current_ic
);
1573 if (mapping_stack_pointer
<= (mapping_stack
+ 1))
1575 POP_MAPPING_STACK (map_set_rest_length
, reg
[rrr
]);
1576 i
+= map_set_rest_length
;
1577 ic
+= map_set_rest_length
;
1578 POP_MAPPING_STACK (map_set_rest_length
, reg
[rrr
]);
1588 Lisp_Object map
, attrib
, value
, content
;
1590 j
= XINT (ccl_prog
[ic
++]); /* map_id */
1592 if (j
>= ASIZE (Vcode_conversion_map_vector
))
1597 map
= AREF (Vcode_conversion_map_vector
, j
);
1610 point
= XUINT (AREF (map
, 0));
1611 point
= op
- point
+ 1;
1614 (!((point
>= 1) && (point
< size
))))
1619 content
= AREF (map
, point
);
1622 else if (NUMBERP (content
))
1623 reg
[rrr
] = XINT (content
);
1624 else if (EQ (content
, Qt
));
1625 else if (CONSP (content
))
1627 attrib
= XCAR (content
);
1628 value
= XCDR (content
);
1629 if (!NUMBERP (attrib
) || !NUMBERP (value
))
1631 reg
[rrr
] = XUINT(value
);
1634 else if (SYMBOLP (content
))
1635 CCL_CALL_FOR_MAP_INSTRUCTION (content
, ic
);
1653 /* The suppress_error member is set when e.g. a CCL-based coding
1654 system is used for terminal output. */
1655 if (!ccl
->suppress_error
&& destination
)
1657 /* We can insert an error message only if DESTINATION is
1658 specified and we still have a room to store the message
1666 switch (ccl
->status
)
1668 case CCL_STAT_INVALID_CMD
:
1669 sprintf(msg
, "\nCCL: Invalid command %x (ccl_code = %x) at %d.",
1670 code
& 0x1F, code
, this_ic
);
1673 int i
= ccl_backtrace_idx
- 1;
1676 msglen
= strlen (msg
);
1677 if (dst
+ msglen
<= (dst_bytes
? dst_end
: src
))
1679 bcopy (msg
, dst
, msglen
);
1683 for (j
= 0; j
< CCL_DEBUG_BACKTRACE_LEN
; j
++, i
--)
1685 if (i
< 0) i
= CCL_DEBUG_BACKTRACE_LEN
- 1;
1686 if (ccl_backtrace_table
[i
] == 0)
1688 sprintf(msg
, " %d", ccl_backtrace_table
[i
]);
1689 msglen
= strlen (msg
);
1690 if (dst
+ msglen
> (dst_bytes
? dst_end
: src
))
1692 bcopy (msg
, dst
, msglen
);
1701 sprintf(msg
, "\nCCL: Quited.");
1705 sprintf(msg
, "\nCCL: Unknown error type (%d).", ccl
->status
);
1708 msglen
= strlen (msg
);
1709 if (dst
+ msglen
<= dst_end
)
1711 for (i
= 0; i
< msglen
; i
++)
1718 ccl
->stack_idx
= stack_idx
;
1719 ccl
->prog
= ccl_prog
;
1720 ccl
->consumed
= src
- source
;
1721 ccl
->produced
= dst
- destination
;
1724 /* Resolve symbols in the specified CCL code (Lisp vector). This
1725 function converts symbols of code conversion maps and character
1726 translation tables embeded in the CCL code into their ID numbers.
1728 The return value is a vector (CCL itself or a new vector in which
1729 all symbols are resolved), Qt if resolving of some symbol failed,
1730 or nil if CCL contains invalid data. */
1733 resolve_symbol_ccl_program (ccl
)
1736 int i
, veclen
, unresolved
= 0;
1737 Lisp_Object result
, contents
, val
;
1740 veclen
= ASIZE (result
);
1742 for (i
= 0; i
< veclen
; i
++)
1744 contents
= AREF (result
, i
);
1745 if (INTEGERP (contents
))
1747 else if (CONSP (contents
)
1748 && SYMBOLP (XCAR (contents
))
1749 && SYMBOLP (XCDR (contents
)))
1751 /* This is the new style for embedding symbols. The form is
1752 (SYMBOL . PROPERTY). (get SYMBOL PROPERTY) should give
1755 if (EQ (result
, ccl
))
1756 result
= Fcopy_sequence (ccl
);
1758 val
= Fget (XCAR (contents
), XCDR (contents
));
1760 AREF (result
, i
) = val
;
1765 else if (SYMBOLP (contents
))
1767 /* This is the old style for embedding symbols. This style
1768 may lead to a bug if, for instance, a translation table
1769 and a code conversion map have the same name. */
1770 if (EQ (result
, ccl
))
1771 result
= Fcopy_sequence (ccl
);
1773 val
= Fget (contents
, Qtranslation_table_id
);
1775 AREF (result
, i
) = val
;
1778 val
= Fget (contents
, Qcode_conversion_map_id
);
1780 AREF (result
, i
) = val
;
1783 val
= Fget (contents
, Qccl_program_idx
);
1785 AREF (result
, i
) = val
;
1795 return (unresolved
? Qt
: result
);
1798 /* Return the compiled code (vector) of CCL program CCL_PROG.
1799 CCL_PROG is a name (symbol) of the program or already compiled
1800 code. If necessary, resolve symbols in the compiled code to index
1801 numbers. If we failed to get the compiled code or to resolve
1802 symbols, return Qnil. */
1805 ccl_get_compiled_code (ccl_prog
)
1806 Lisp_Object ccl_prog
;
1808 Lisp_Object val
, slot
;
1810 if (VECTORP (ccl_prog
))
1812 val
= resolve_symbol_ccl_program (ccl_prog
);
1813 return (VECTORP (val
) ? val
: Qnil
);
1815 if (!SYMBOLP (ccl_prog
))
1818 val
= Fget (ccl_prog
, Qccl_program_idx
);
1820 || XINT (val
) >= ASIZE (Vccl_program_table
))
1822 slot
= AREF (Vccl_program_table
, XINT (val
));
1823 if (! VECTORP (slot
)
1824 || ASIZE (slot
) != 3
1825 || ! VECTORP (AREF (slot
, 1)))
1827 if (NILP (AREF (slot
, 2)))
1829 val
= resolve_symbol_ccl_program (AREF (slot
, 1));
1830 if (! VECTORP (val
))
1832 AREF (slot
, 1) = val
;
1833 AREF (slot
, 2) = Qt
;
1835 return AREF (slot
, 1);
1838 /* Setup fields of the structure pointed by CCL appropriately for the
1839 execution of CCL program CCL_PROG. CCL_PROG is the name (symbol)
1840 of the CCL program or the already compiled code (vector).
1841 Return 0 if we succeed this setup, else return -1.
1843 If CCL_PROG is nil, we just reset the structure pointed by CCL. */
1845 setup_ccl_program (ccl
, ccl_prog
)
1846 struct ccl_program
*ccl
;
1847 Lisp_Object ccl_prog
;
1851 if (! NILP (ccl_prog
))
1853 struct Lisp_Vector
*vp
;
1855 ccl_prog
= ccl_get_compiled_code (ccl_prog
);
1856 if (! VECTORP (ccl_prog
))
1858 vp
= XVECTOR (ccl_prog
);
1859 ccl
->size
= vp
->size
;
1860 ccl
->prog
= vp
->contents
;
1861 ccl
->eof_ic
= XINT (vp
->contents
[CCL_HEADER_EOF
]);
1862 ccl
->buf_magnification
= XINT (vp
->contents
[CCL_HEADER_BUF_MAG
]);
1864 ccl
->ic
= CCL_HEADER_MAIN
;
1865 for (i
= 0; i
< 8; i
++)
1867 ccl
->last_block
= 0;
1868 ccl
->private_state
= 0;
1871 ccl
->suppress_error
= 0;
1875 DEFUN ("ccl-program-p", Fccl_program_p
, Sccl_program_p
, 1, 1, 0,
1876 doc
: /* Return t if OBJECT is a CCL program name or a compiled CCL program code.
1877 See the documentation of `define-ccl-program' for the detail of CCL program. */)
1883 if (VECTORP (object
))
1885 val
= resolve_symbol_ccl_program (object
);
1886 return (VECTORP (val
) ? Qt
: Qnil
);
1888 if (!SYMBOLP (object
))
1891 val
= Fget (object
, Qccl_program_idx
);
1892 return ((! NATNUMP (val
)
1893 || XINT (val
) >= ASIZE (Vccl_program_table
))
1897 DEFUN ("ccl-execute", Fccl_execute
, Sccl_execute
, 2, 2, 0,
1898 doc
: /* Execute CCL-PROGRAM with registers initialized by REGISTERS.
1900 CCL-PROGRAM is a CCL program name (symbol)
1901 or compiled code generated by `ccl-compile' (for backward compatibility.
1902 In the latter case, the execution overhead is bigger than in the former).
1903 No I/O commands should appear in CCL-PROGRAM.
1905 REGISTERS is a vector of [R0 R1 ... R7] where RN is an initial value
1906 for the Nth register.
1908 As side effect, each element of REGISTERS holds the value of
1909 the corresponding register after the execution.
1911 See the documentation of `define-ccl-program' for a definition of CCL
1914 Lisp_Object ccl_prog
, reg
;
1916 struct ccl_program ccl
;
1919 if (setup_ccl_program (&ccl
, ccl_prog
) < 0)
1920 error ("Invalid CCL program");
1923 if (ASIZE (reg
) != 8)
1924 error ("Length of vector REGISTERS is not 8");
1926 for (i
= 0; i
< 8; i
++)
1927 ccl
.reg
[i
] = (INTEGERP (AREF (reg
, i
))
1928 ? XINT (AREF (reg
, i
))
1931 ccl_driver (&ccl
, NULL
, NULL
, 0, 0, Qnil
);
1933 if (ccl
.status
!= CCL_STAT_SUCCESS
)
1934 error ("Error in CCL program at %dth code", ccl
.ic
);
1936 for (i
= 0; i
< 8; i
++)
1937 XSETINT (AREF (reg
, i
), ccl
.reg
[i
]);
1941 DEFUN ("ccl-execute-on-string", Fccl_execute_on_string
, Sccl_execute_on_string
,
1943 doc
: /* Execute CCL-PROGRAM with initial STATUS on STRING.
1945 CCL-PROGRAM is a symbol registered by register-ccl-program,
1946 or a compiled code generated by `ccl-compile' (for backward compatibility,
1947 in this case, the execution is slower).
1949 Read buffer is set to STRING, and write buffer is allocated automatically.
1951 STATUS is a vector of [R0 R1 ... R7 IC], where
1952 R0..R7 are initial values of corresponding registers,
1953 IC is the instruction counter specifying from where to start the program.
1954 If R0..R7 are nil, they are initialized to 0.
1955 If IC is nil, it is initialized to head of the CCL program.
1957 If optional 4th arg CONTINUE is non-nil, keep IC on read operation
1958 when read buffer is exausted, else, IC is always set to the end of
1959 CCL-PROGRAM on exit.
1961 It returns the contents of write buffer as a string,
1962 and as side effect, STATUS is updated.
1963 If the optional 5th arg UNIBYTE-P is non-nil, the returned string
1964 is a unibyte string. By default it is a multibyte string.
1966 See the documentation of `define-ccl-program' for the detail of CCL program. */)
1967 (ccl_prog
, status
, str
, contin
, unibyte_p
)
1968 Lisp_Object ccl_prog
, status
, str
, contin
, unibyte_p
;
1971 struct ccl_program ccl
;
1974 unsigned char *outbuf
, *outp
;
1975 int str_chars
, str_bytes
;
1976 #define CCL_EXECUTE_BUF_SIZE 1024
1977 int source
[CCL_EXECUTE_BUF_SIZE
], destination
[CCL_EXECUTE_BUF_SIZE
];
1978 int consumed_chars
, consumed_bytes
, produced_chars
;
1980 if (setup_ccl_program (&ccl
, ccl_prog
) < 0)
1981 error ("Invalid CCL program");
1983 CHECK_VECTOR (status
);
1984 if (ASIZE (status
) != 9)
1985 error ("Length of vector STATUS is not 9");
1987 str_chars
= XSTRING (str
)->size
;
1988 str_bytes
= STRING_BYTES (XSTRING (str
));
1990 for (i
= 0; i
< 8; i
++)
1992 if (NILP (AREF (status
, i
)))
1993 XSETINT (AREF (status
, i
), 0);
1994 if (INTEGERP (AREF (status
, i
)))
1995 ccl
.reg
[i
] = XINT (AREF (status
, i
));
1997 if (INTEGERP (AREF (status
, i
)))
1999 i
= XFASTINT (AREF (status
, 8));
2000 if (ccl
.ic
< i
&& i
< ccl
.size
)
2004 outbufsize
= (ccl
.buf_magnification
2005 ? str_bytes
* ccl
.buf_magnification
+ 256
2007 outp
= outbuf
= (unsigned char *) xmalloc (outbufsize
);
2009 consumed_chars
= consumed_bytes
= 0;
2011 while (consumed_bytes
< str_bytes
)
2013 const unsigned char *p
= XSTRING (str
)->data
+ consumed_bytes
;
2014 const unsigned char *endp
= XSTRING (str
)->data
+ str_bytes
;
2018 if (endp
- p
== str_chars
- consumed_chars
)
2019 while (i
< CCL_EXECUTE_BUF_SIZE
&& p
< endp
)
2022 while (i
< CCL_EXECUTE_BUF_SIZE
&& p
< endp
)
2023 source
[i
++] = STRING_CHAR_ADVANCE (p
);
2024 consumed_chars
+= i
;
2025 consumed_bytes
= p
- XSTRING (str
)->data
;
2027 if (consumed_bytes
== str_bytes
)
2028 ccl
.last_block
= NILP (contin
);
2033 ccl_driver (&ccl
, src
, destination
, src_size
, CCL_EXECUTE_BUF_SIZE
,
2035 if (ccl
.status
!= CCL_STAT_SUSPEND_BY_DST
)
2037 produced_chars
+= ccl
.produced
;
2038 if (NILP (unibyte_p
))
2040 if (outp
- outbuf
+ MAX_MULTIBYTE_LENGTH
* ccl
.produced
2043 int offset
= outp
- outbuf
;
2044 outbufsize
+= MAX_MULTIBYTE_LENGTH
* ccl
.produced
;
2045 outbuf
= (unsigned char *) xrealloc (outbuf
, outbufsize
);
2046 outp
= outbuf
+ offset
;
2048 for (i
= 0; i
< ccl
.produced
; i
++)
2049 CHAR_STRING_ADVANCE (destination
[i
], outp
);
2053 if (outp
- outbuf
+ ccl
.produced
> outbufsize
)
2055 int offset
= outp
- outbuf
;
2056 outbufsize
+= ccl
.produced
;
2057 outbuf
= (unsigned char *) xrealloc (outbuf
, outbufsize
);
2058 outp
= outbuf
+ offset
;
2060 for (i
= 0; i
< ccl
.produced
; i
++)
2061 *outp
++ = destination
[i
];
2063 src
+= ccl
.consumed
;
2064 src_size
-= ccl
.consumed
;
2067 if (ccl
.status
!= CCL_STAT_SUSPEND_BY_SRC
)
2071 if (ccl
.status
!= CCL_STAT_SUCCESS
2072 && ccl
.status
!= CCL_STAT_SUSPEND_BY_SRC
)
2073 error ("Error in CCL program at %dth code", ccl
.ic
);
2075 for (i
= 0; i
< 8; i
++)
2076 XSET (XVECTOR (status
)->contents
[i
], Lisp_Int
, ccl
.reg
[i
]);
2077 XSETINT (XVECTOR (status
)->contents
[8], ccl
.ic
);
2079 if (NILP (unibyte_p
))
2080 val
= make_multibyte_string ((char *) outbuf
, produced_chars
,
2083 val
= make_unibyte_string ((char *) outbuf
, produced_chars
);
2089 DEFUN ("register-ccl-program", Fregister_ccl_program
, Sregister_ccl_program
,
2091 doc
: /* Register CCL program CCL_PROG as NAME in `ccl-program-table'.
2092 CCL_PROG should be a compiled CCL program (vector), or nil.
2093 If it is nil, just reserve NAME as a CCL program name.
2094 Return index number of the registered CCL program. */)
2096 Lisp_Object name
, ccl_prog
;
2098 int len
= ASIZE (Vccl_program_table
);
2100 Lisp_Object resolved
;
2102 CHECK_SYMBOL (name
);
2104 if (!NILP (ccl_prog
))
2106 CHECK_VECTOR (ccl_prog
);
2107 resolved
= resolve_symbol_ccl_program (ccl_prog
);
2108 if (NILP (resolved
))
2109 error ("Error in CCL program");
2110 if (VECTORP (resolved
))
2112 ccl_prog
= resolved
;
2119 for (idx
= 0; idx
< len
; idx
++)
2123 slot
= AREF (Vccl_program_table
, idx
);
2124 if (!VECTORP (slot
))
2125 /* This is the first unsed slot. Register NAME here. */
2128 if (EQ (name
, AREF (slot
, 0)))
2130 /* Update this slot. */
2131 AREF (slot
, 1) = ccl_prog
;
2132 AREF (slot
, 2) = resolved
;
2133 return make_number (idx
);
2139 /* Extend the table. */
2140 Lisp_Object new_table
;
2143 new_table
= Fmake_vector (make_number (len
* 2), Qnil
);
2144 for (j
= 0; j
< len
; j
++)
2146 = AREF (Vccl_program_table
, j
);
2147 Vccl_program_table
= new_table
;
2153 elt
= Fmake_vector (make_number (3), Qnil
);
2154 AREF (elt
, 0) = name
;
2155 AREF (elt
, 1) = ccl_prog
;
2156 AREF (elt
, 2) = resolved
;
2157 AREF (Vccl_program_table
, idx
) = elt
;
2160 Fput (name
, Qccl_program_idx
, make_number (idx
));
2161 return make_number (idx
);
2164 /* Register code conversion map.
2165 A code conversion map consists of numbers, Qt, Qnil, and Qlambda.
2166 The first element is the start code point.
2167 The other elements are mapped numbers.
2168 Symbol t means to map to an original number before mapping.
2169 Symbol nil means that the corresponding element is empty.
2170 Symbol lambda means to terminate mapping here.
2173 DEFUN ("register-code-conversion-map", Fregister_code_conversion_map
,
2174 Sregister_code_conversion_map
,
2176 doc
: /* Register SYMBOL as code conversion map MAP.
2177 Return index number of the registered map. */)
2179 Lisp_Object symbol
, map
;
2181 int len
= ASIZE (Vcode_conversion_map_vector
);
2185 CHECK_SYMBOL (symbol
);
2188 for (i
= 0; i
< len
; i
++)
2190 Lisp_Object slot
= AREF (Vcode_conversion_map_vector
, i
);
2195 if (EQ (symbol
, XCAR (slot
)))
2197 index
= make_number (i
);
2198 XSETCDR (slot
, map
);
2199 Fput (symbol
, Qcode_conversion_map
, map
);
2200 Fput (symbol
, Qcode_conversion_map_id
, index
);
2207 Lisp_Object new_vector
= Fmake_vector (make_number (len
* 2), Qnil
);
2210 for (j
= 0; j
< len
; j
++)
2211 AREF (new_vector
, j
)
2212 = AREF (Vcode_conversion_map_vector
, j
);
2213 Vcode_conversion_map_vector
= new_vector
;
2216 index
= make_number (i
);
2217 Fput (symbol
, Qcode_conversion_map
, map
);
2218 Fput (symbol
, Qcode_conversion_map_id
, index
);
2219 AREF (Vcode_conversion_map_vector
, i
) = Fcons (symbol
, map
);
2227 staticpro (&Vccl_program_table
);
2228 Vccl_program_table
= Fmake_vector (make_number (32), Qnil
);
2230 Qccl
= intern ("ccl");
2233 Qcclp
= intern ("cclp");
2236 Qccl_program
= intern ("ccl-program");
2237 staticpro (&Qccl_program
);
2239 Qccl_program_idx
= intern ("ccl-program-idx");
2240 staticpro (&Qccl_program_idx
);
2242 Qcode_conversion_map
= intern ("code-conversion-map");
2243 staticpro (&Qcode_conversion_map
);
2245 Qcode_conversion_map_id
= intern ("code-conversion-map-id");
2246 staticpro (&Qcode_conversion_map_id
);
2248 DEFVAR_LISP ("code-conversion-map-vector", &Vcode_conversion_map_vector
,
2249 doc
: /* Vector of code conversion maps. */);
2250 Vcode_conversion_map_vector
= Fmake_vector (make_number (16), Qnil
);
2252 DEFVAR_LISP ("font-ccl-encoder-alist", &Vfont_ccl_encoder_alist
,
2253 doc
: /* Alist of fontname patterns vs corresponding CCL program.
2254 Each element looks like (REGEXP . CCL-CODE),
2255 where CCL-CODE is a compiled CCL program.
2256 When a font whose name matches REGEXP is used for displaying a character,
2257 CCL-CODE is executed to calculate the code point in the font
2258 from the charset number and position code(s) of the character which are set
2259 in CCL registers R0, R1, and R2 before the execution.
2260 The code point in the font is set in CCL registers R1 and R2
2261 when the execution terminated.
2262 If the font is single-byte font, the register R2 is not used. */);
2263 Vfont_ccl_encoder_alist
= Qnil
;
2265 DEFVAR_LISP ("translation-hash-table-vector", &Vtranslation_hash_table_vector
,
2266 doc
: /* Vector containing all translation hash tables ever defined.
2267 Comprises pairs (SYMBOL . TABLE) where SYMBOL and TABLE were set up by calls
2268 to `define-translation-hash-table'. The vector is indexed by the table id
2270 Vtranslation_hash_table_vector
= Qnil
;
2272 defsubr (&Sccl_program_p
);
2273 defsubr (&Sccl_execute
);
2274 defsubr (&Sccl_execute_on_string
);
2275 defsubr (&Sregister_ccl_program
);
2276 defsubr (&Sregister_code_conversion_map
);