1 /* CCL (Code Conversion Language) interpreter.
2 Copyright (C) 1995, 1997 Electrotechnical Laboratory, JAPAN.
3 Copyright (C) 2001, 2002 Free Software Foundation, Inc.
4 Licensed to the Free Software Foundation.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
32 /* This contains all code conversion map available to CCL. */
33 Lisp_Object Vcode_conversion_map_vector
;
35 /* Alist of fontname patterns vs corresponding CCL program. */
36 Lisp_Object Vfont_ccl_encoder_alist
;
38 /* This symbol is a property which assocates with ccl program vector.
39 Ex: (get 'ccl-big5-encoder 'ccl-program) returns ccl program vector. */
40 Lisp_Object Qccl_program
;
42 /* These symbols are properties which associate with code conversion
43 map and their ID respectively. */
44 Lisp_Object Qcode_conversion_map
;
45 Lisp_Object Qcode_conversion_map_id
;
47 /* Symbols of ccl program have this property, a value of the property
48 is an index for Vccl_protram_table. */
49 Lisp_Object Qccl_program_idx
;
51 /* Table of registered CCL programs. Each element is a vector of
52 NAME, CCL_PROG, RESOLVEDP, and UPDATEDP, where NAME (symbol) is the
53 name of the program, CCL_PROG (vector) is the compiled code of the
54 program, RESOLVEDP (t or nil) is the flag to tell if symbols in
55 CCL_PROG is already resolved to index numbers or not, UPDATEDP (t
56 or nil) is the flat to tell if the CCL program is updated after it
58 Lisp_Object Vccl_program_table
;
60 /* Vector of registered hash tables for translation. */
61 Lisp_Object Vtranslation_hash_table_vector
;
63 /* Return a hash table of id number ID. */
64 #define GET_HASH_TABLE(id) \
65 (XHASH_TABLE (XCDR(XVECTOR(Vtranslation_hash_table_vector)->contents[(id)])))
67 /* CCL (Code Conversion Language) is a simple language which has
68 operations on one input buffer, one output buffer, and 7 registers.
69 The syntax of CCL is described in `ccl.el'. Emacs Lisp function
70 `ccl-compile' compiles a CCL program and produces a CCL code which
71 is a vector of integers. The structure of this vector is as
72 follows: The 1st element: buffer-magnification, a factor for the
73 size of output buffer compared with the size of input buffer. The
74 2nd element: address of CCL code to be executed when encountered
75 with end of input stream. The 3rd and the remaining elements: CCL
78 /* Header of CCL compiled code */
79 #define CCL_HEADER_BUF_MAG 0
80 #define CCL_HEADER_EOF 1
81 #define CCL_HEADER_MAIN 2
83 /* CCL code is a sequence of 28-bit non-negative integers (i.e. the
84 MSB is always 0), each contains CCL command and/or arguments in the
87 |----------------- integer (28-bit) ------------------|
88 |------- 17-bit ------|- 3-bit --|- 3-bit --|- 5-bit -|
89 |--constant argument--|-register-|-register-|-command-|
90 ccccccccccccccccc RRR rrr XXXXX
92 |------- relative address -------|-register-|-command-|
93 cccccccccccccccccccc rrr XXXXX
95 |------------- constant or other args ----------------|
96 cccccccccccccccccccccccccccc
98 where, `cc...c' is a non-negative integer indicating constant value
99 (the left most `c' is always 0) or an absolute jump address, `RRR'
100 and `rrr' are CCL register number, `XXXXX' is one of the following
105 Each comment fields shows one or more lines for command syntax and
106 the following lines for semantics of the command. In semantics, IC
107 stands for Instruction Counter. */
109 #define CCL_SetRegister 0x00 /* Set register a register value:
110 1:00000000000000000RRRrrrXXXXX
111 ------------------------------
115 #define CCL_SetShortConst 0x01 /* Set register a short constant value:
116 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
117 ------------------------------
118 reg[rrr] = CCCCCCCCCCCCCCCCCCC;
121 #define CCL_SetConst 0x02 /* Set register a constant value:
122 1:00000000000000000000rrrXXXXX
124 ------------------------------
129 #define CCL_SetArray 0x03 /* Set register an element of array:
130 1:CCCCCCCCCCCCCCCCCRRRrrrXXXXX
134 ------------------------------
135 if (0 <= reg[RRR] < CC..C)
136 reg[rrr] = ELEMENT[reg[RRR]];
140 #define CCL_Jump 0x04 /* Jump:
141 1:A--D--D--R--E--S--S-000XXXXX
142 ------------------------------
146 /* Note: If CC..C is greater than 0, the second code is omitted. */
148 #define CCL_JumpCond 0x05 /* Jump conditional:
149 1:A--D--D--R--E--S--S-rrrXXXXX
150 ------------------------------
156 #define CCL_WriteRegisterJump 0x06 /* Write register and jump:
157 1:A--D--D--R--E--S--S-rrrXXXXX
158 ------------------------------
163 #define CCL_WriteRegisterReadJump 0x07 /* Write register, read, and jump:
164 1:A--D--D--R--E--S--S-rrrXXXXX
165 2:A--D--D--R--E--S--S-rrrYYYYY
166 -----------------------------
172 /* Note: If read is suspended, the resumed execution starts from the
173 second code (YYYYY == CCL_ReadJump). */
175 #define CCL_WriteConstJump 0x08 /* Write constant and jump:
176 1:A--D--D--R--E--S--S-000XXXXX
178 ------------------------------
183 #define CCL_WriteConstReadJump 0x09 /* Write constant, read, and jump:
184 1:A--D--D--R--E--S--S-rrrXXXXX
186 3:A--D--D--R--E--S--S-rrrYYYYY
187 -----------------------------
193 /* Note: If read is suspended, the resumed execution starts from the
194 second code (YYYYY == CCL_ReadJump). */
196 #define CCL_WriteStringJump 0x0A /* Write string and jump:
197 1:A--D--D--R--E--S--S-000XXXXX
199 3:0000STRIN[0]STRIN[1]STRIN[2]
201 ------------------------------
202 write_string (STRING, LENGTH);
206 #define CCL_WriteArrayReadJump 0x0B /* Write an array element, read, and jump:
207 1:A--D--D--R--E--S--S-rrrXXXXX
212 N:A--D--D--R--E--S--S-rrrYYYYY
213 ------------------------------
214 if (0 <= reg[rrr] < LENGTH)
215 write (ELEMENT[reg[rrr]]);
216 IC += LENGTH + 2; (... pointing at N+1)
220 /* Note: If read is suspended, the resumed execution starts from the
221 Nth code (YYYYY == CCL_ReadJump). */
223 #define CCL_ReadJump 0x0C /* Read and jump:
224 1:A--D--D--R--E--S--S-rrrYYYYY
225 -----------------------------
230 #define CCL_Branch 0x0D /* Jump by branch table:
231 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
232 2:A--D--D--R--E-S-S[0]000XXXXX
233 3:A--D--D--R--E-S-S[1]000XXXXX
235 ------------------------------
236 if (0 <= reg[rrr] < CC..C)
237 IC += ADDRESS[reg[rrr]];
239 IC += ADDRESS[CC..C];
242 #define CCL_ReadRegister 0x0E /* Read bytes into registers:
243 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
244 2:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
246 ------------------------------
251 #define CCL_WriteExprConst 0x0F /* write result of expression:
252 1:00000OPERATION000RRR000XXXXX
254 ------------------------------
255 write (reg[RRR] OPERATION CONSTANT);
259 /* Note: If the Nth read is suspended, the resumed execution starts
260 from the Nth code. */
262 #define CCL_ReadBranch 0x10 /* Read one byte into a register,
263 and jump by branch table:
264 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
265 2:A--D--D--R--E-S-S[0]000XXXXX
266 3:A--D--D--R--E-S-S[1]000XXXXX
268 ------------------------------
270 if (0 <= reg[rrr] < CC..C)
271 IC += ADDRESS[reg[rrr]];
273 IC += ADDRESS[CC..C];
276 #define CCL_WriteRegister 0x11 /* Write registers:
277 1:CCCCCCCCCCCCCCCCCCCrrrXXXXX
278 2:CCCCCCCCCCCCCCCCCCCrrrXXXXX
280 ------------------------------
286 /* Note: If the Nth write is suspended, the resumed execution
287 starts from the Nth code. */
289 #define CCL_WriteExprRegister 0x12 /* Write result of expression
290 1:00000OPERATIONRrrRRR000XXXXX
291 ------------------------------
292 write (reg[RRR] OPERATION reg[Rrr]);
295 #define CCL_Call 0x13 /* Call the CCL program whose ID is
297 1:CCCCCCCCCCCCCCCCCCCCFFFXXXXX
298 [2:00000000cccccccccccccccccccc]
299 ------------------------------
307 #define CCL_WriteConstString 0x14 /* Write a constant or a string:
308 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
309 [2:0000STRIN[0]STRIN[1]STRIN[2]]
311 -----------------------------
315 write_string (STRING, CC..C);
316 IC += (CC..C + 2) / 3;
319 #define CCL_WriteArray 0x15 /* Write an element of array:
320 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
324 ------------------------------
325 if (0 <= reg[rrr] < CC..C)
326 write (ELEMENT[reg[rrr]]);
330 #define CCL_End 0x16 /* Terminate:
331 1:00000000000000000000000XXXXX
332 ------------------------------
336 /* The following two codes execute an assignment arithmetic/logical
337 operation. The form of the operation is like REG OP= OPERAND. */
339 #define CCL_ExprSelfConst 0x17 /* REG OP= constant:
340 1:00000OPERATION000000rrrXXXXX
342 ------------------------------
343 reg[rrr] OPERATION= CONSTANT;
346 #define CCL_ExprSelfReg 0x18 /* REG1 OP= REG2:
347 1:00000OPERATION000RRRrrrXXXXX
348 ------------------------------
349 reg[rrr] OPERATION= reg[RRR];
352 /* The following codes execute an arithmetic/logical operation. The
353 form of the operation is like REG_X = REG_Y OP OPERAND2. */
355 #define CCL_SetExprConst 0x19 /* REG_X = REG_Y OP constant:
356 1:00000OPERATION000RRRrrrXXXXX
358 ------------------------------
359 reg[rrr] = reg[RRR] OPERATION CONSTANT;
363 #define CCL_SetExprReg 0x1A /* REG1 = REG2 OP REG3:
364 1:00000OPERATIONRrrRRRrrrXXXXX
365 ------------------------------
366 reg[rrr] = reg[RRR] OPERATION reg[Rrr];
369 #define CCL_JumpCondExprConst 0x1B /* Jump conditional according to
370 an operation on constant:
371 1:A--D--D--R--E--S--S-rrrXXXXX
374 -----------------------------
375 reg[7] = reg[rrr] OPERATION CONSTANT;
382 #define CCL_JumpCondExprReg 0x1C /* Jump conditional according to
383 an operation on register:
384 1:A--D--D--R--E--S--S-rrrXXXXX
387 -----------------------------
388 reg[7] = reg[rrr] OPERATION reg[RRR];
395 #define CCL_ReadJumpCondExprConst 0x1D /* Read and jump conditional according
396 to an operation on constant:
397 1:A--D--D--R--E--S--S-rrrXXXXX
400 -----------------------------
402 reg[7] = reg[rrr] OPERATION CONSTANT;
409 #define CCL_ReadJumpCondExprReg 0x1E /* Read and jump conditional according
410 to an operation on register:
411 1:A--D--D--R--E--S--S-rrrXXXXX
414 -----------------------------
416 reg[7] = reg[rrr] OPERATION reg[RRR];
423 #define CCL_Extension 0x1F /* Extended CCL code
424 1:ExtendedCOMMNDRrrRRRrrrXXXXX
427 ------------------------------
428 extended_command (rrr,RRR,Rrr,ARGS)
432 Here after, Extended CCL Instructions.
433 Bit length of extended command is 14.
434 Therefore, the instruction code range is 0..16384(0x3fff).
437 /* Read a multibyte characeter.
438 A code point is stored into reg[rrr]. A charset ID is stored into
441 #define CCL_ReadMultibyteChar2 0x00 /* Read Multibyte Character
442 1:ExtendedCOMMNDRrrRRRrrrXXXXX */
444 /* Write a multibyte character.
445 Write a character whose code point is reg[rrr] and the charset ID
448 #define CCL_WriteMultibyteChar2 0x01 /* Write Multibyte Character
449 1:ExtendedCOMMNDRrrRRRrrrXXXXX */
451 /* Translate a character whose code point is reg[rrr] and the charset
452 ID is reg[RRR] by a translation table whose ID is reg[Rrr].
454 A translated character is set in reg[rrr] (code point) and reg[RRR]
457 #define CCL_TranslateCharacter 0x02 /* Translate a multibyte character
458 1:ExtendedCOMMNDRrrRRRrrrXXXXX */
460 /* Translate a character whose code point is reg[rrr] and the charset
461 ID is reg[RRR] by a translation table whose ID is ARGUMENT.
463 A translated character is set in reg[rrr] (code point) and reg[RRR]
466 #define CCL_TranslateCharacterConstTbl 0x03 /* Translate a multibyte character
467 1:ExtendedCOMMNDRrrRRRrrrXXXXX
468 2:ARGUMENT(Translation Table ID)
471 /* Iterate looking up MAPs for reg[rrr] starting from the Nth (N =
472 reg[RRR]) MAP until some value is found.
474 Each MAP is a Lisp vector whose element is number, nil, t, or
476 If the element is nil, ignore the map and proceed to the next map.
477 If the element is t or lambda, finish without changing reg[rrr].
478 If the element is a number, set reg[rrr] to the number and finish.
480 Detail of the map structure is descibed in the comment for
481 CCL_MapMultiple below. */
483 #define CCL_IterateMultipleMap 0x10 /* Iterate multiple maps
484 1:ExtendedCOMMNDXXXRRRrrrXXXXX
491 /* Map the code in reg[rrr] by MAPs starting from the Nth (N =
494 MAPs are supplied in the succeeding CCL codes as follows:
496 When CCL program gives this nested structure of map to this command:
499 (MAP-ID121 MAP-ID122 MAP-ID123)
502 (MAP-ID211 (MAP-ID2111) MAP-ID212)
504 the compiled CCL codes has this sequence:
505 CCL_MapMultiple (CCL code of this command)
506 16 (total number of MAPs and SEPARATORs)
524 A value of each SEPARATOR follows this rule:
525 MAP-SET := SEPARATOR [(MAP-ID | MAP-SET)]+
526 SEPARATOR := -(number of MAP-IDs and SEPARATORs in the MAP-SET)
528 (*)....Nest level of MAP-SET must not be over than MAX_MAP_SET_LEVEL.
530 When some map fails to map (i.e. it doesn't have a value for
531 reg[rrr]), the mapping is treated as identity.
533 The mapping is iterated for all maps in each map set (set of maps
534 separated by SEPARATOR) except in the case that lambda is
535 encountered. More precisely, the mapping proceeds as below:
537 At first, VAL0 is set to reg[rrr], and it is translated by the
538 first map to VAL1. Then, VAL1 is translated by the next map to
539 VAL2. This mapping is iterated until the last map is used. The
540 result of the mapping is the last value of VAL?. When the mapping
541 process reached to the end of the map set, it moves to the next
542 map set. If the next does not exit, the mapping process terminates,
543 and regard the last value as a result.
545 But, when VALm is mapped to VALn and VALn is not a number, the
546 mapping proceed as below:
548 If VALn is nil, the lastest map is ignored and the mapping of VALm
549 proceed to the next map.
551 In VALn is t, VALm is reverted to reg[rrr] and the mapping of VALm
552 proceed to the next map.
554 If VALn is lambda, move to the next map set like reaching to the
555 end of the current map set.
557 If VALn is a symbol, call the CCL program refered by it.
558 Then, use reg[rrr] as a mapped value except for -1, -2 and -3.
559 Such special values are regarded as nil, t, and lambda respectively.
561 Each map is a Lisp vector of the following format (a) or (b):
562 (a)......[STARTPOINT VAL1 VAL2 ...]
563 (b)......[t VAL STARTPOINT ENDPOINT],
565 STARTPOINT is an offset to be used for indexing a map,
566 ENDPOINT is a maximum index number of a map,
567 VAL and VALn is a number, nil, t, or lambda.
569 Valid index range of a map of type (a) is:
570 STARTPOINT <= index < STARTPOINT + map_size - 1
571 Valid index range of a map of type (b) is:
572 STARTPOINT <= index < ENDPOINT */
574 #define CCL_MapMultiple 0x11 /* Mapping by multiple code conversion maps
575 1:ExtendedCOMMNDXXXRRRrrrXXXXX
587 #define MAX_MAP_SET_LEVEL 30
595 static tr_stack mapping_stack
[MAX_MAP_SET_LEVEL
];
596 static tr_stack
*mapping_stack_pointer
;
598 /* If this variable is non-zero, it indicates the stack_idx
599 of immediately called by CCL_MapMultiple. */
600 static int stack_idx_of_map_multiple
;
602 #define PUSH_MAPPING_STACK(restlen, orig) \
605 mapping_stack_pointer->rest_length = (restlen); \
606 mapping_stack_pointer->orig_val = (orig); \
607 mapping_stack_pointer++; \
611 #define POP_MAPPING_STACK(restlen, orig) \
614 mapping_stack_pointer--; \
615 (restlen) = mapping_stack_pointer->rest_length; \
616 (orig) = mapping_stack_pointer->orig_val; \
620 #define CCL_CALL_FOR_MAP_INSTRUCTION(symbol, ret_ic) \
623 struct ccl_program called_ccl; \
624 if (stack_idx >= 256 \
625 || (setup_ccl_program (&called_ccl, (symbol)) != 0)) \
629 ccl_prog = ccl_prog_stack_struct[0].ccl_prog; \
630 ic = ccl_prog_stack_struct[0].ic; \
631 eof_ic = ccl_prog_stack_struct[0].eof_ic; \
635 ccl_prog_stack_struct[stack_idx].ccl_prog = ccl_prog; \
636 ccl_prog_stack_struct[stack_idx].ic = (ret_ic); \
637 ccl_prog_stack_struct[stack_idx].eof_ic = eof_ic; \
639 ccl_prog = called_ccl.prog; \
640 ic = CCL_HEADER_MAIN; \
641 eof_ic = XFASTINT (ccl_prog[CCL_HEADER_EOF]); \
646 #define CCL_MapSingle 0x12 /* Map by single code conversion map
647 1:ExtendedCOMMNDXXXRRRrrrXXXXX
649 ------------------------------
650 Map reg[rrr] by MAP-ID.
651 If some valid mapping is found,
652 set reg[rrr] to the result,
657 #define CCL_LookupIntConstTbl 0x13 /* Lookup multibyte character by
658 integer key. Afterwards R7 set
659 to 1 iff lookup succeeded.
660 1:ExtendedCOMMNDRrrRRRXXXXXXXX
661 2:ARGUMENT(Hash table ID) */
663 #define CCL_LookupCharConstTbl 0x14 /* Lookup integer by multibyte
664 character key. Afterwards R7 set
665 to 1 iff lookup succeeded.
666 1:ExtendedCOMMNDRrrRRRrrrXXXXX
667 2:ARGUMENT(Hash table ID) */
669 /* CCL arithmetic/logical operators. */
670 #define CCL_PLUS 0x00 /* X = Y + Z */
671 #define CCL_MINUS 0x01 /* X = Y - Z */
672 #define CCL_MUL 0x02 /* X = Y * Z */
673 #define CCL_DIV 0x03 /* X = Y / Z */
674 #define CCL_MOD 0x04 /* X = Y % Z */
675 #define CCL_AND 0x05 /* X = Y & Z */
676 #define CCL_OR 0x06 /* X = Y | Z */
677 #define CCL_XOR 0x07 /* X = Y ^ Z */
678 #define CCL_LSH 0x08 /* X = Y << Z */
679 #define CCL_RSH 0x09 /* X = Y >> Z */
680 #define CCL_LSH8 0x0A /* X = (Y << 8) | Z */
681 #define CCL_RSH8 0x0B /* X = Y >> 8, r[7] = Y & 0xFF */
682 #define CCL_DIVMOD 0x0C /* X = Y / Z, r[7] = Y % Z */
683 #define CCL_LS 0x10 /* X = (X < Y) */
684 #define CCL_GT 0x11 /* X = (X > Y) */
685 #define CCL_EQ 0x12 /* X = (X == Y) */
686 #define CCL_LE 0x13 /* X = (X <= Y) */
687 #define CCL_GE 0x14 /* X = (X >= Y) */
688 #define CCL_NE 0x15 /* X = (X != Y) */
690 #define CCL_DECODE_SJIS 0x16 /* X = HIGHER_BYTE (DE-SJIS (Y, Z))
691 r[7] = LOWER_BYTE (DE-SJIS (Y, Z)) */
692 #define CCL_ENCODE_SJIS 0x17 /* X = HIGHER_BYTE (SJIS (Y, Z))
693 r[7] = LOWER_BYTE (SJIS (Y, Z) */
695 /* Terminate CCL program successfully. */
696 #define CCL_SUCCESS \
699 ccl->status = CCL_STAT_SUCCESS; \
704 /* Suspend CCL program because of reading from empty input buffer or
705 writing to full output buffer. When this program is resumed, the
706 same I/O command is executed. */
707 #define CCL_SUSPEND(stat) \
711 ccl->status = stat; \
716 /* Terminate CCL program because of invalid command. Should not occur
717 in the normal case. */
720 #define CCL_INVALID_CMD \
723 ccl->status = CCL_STAT_INVALID_CMD; \
724 goto ccl_error_handler; \
730 #define CCL_INVALID_CMD \
733 ccl_debug_hook (this_ic); \
734 ccl->status = CCL_STAT_INVALID_CMD; \
735 goto ccl_error_handler; \
741 /* Encode one character CH to multibyte form and write to the current
742 output buffer. If CH is less than 256, CH is written as is. */
743 #define CCL_WRITE_CHAR(ch) \
745 int bytes = SINGLE_BYTE_CHAR_P (ch) ? 1: CHAR_BYTES (ch); \
748 else if (dst + bytes + extra_bytes < (dst_bytes ? dst_end : src)) \
753 if (extra_bytes && (ch) >= 0x80 && (ch) < 0xA0) \
754 /* We may have to convert this eight-bit char to \
755 multibyte form later. */ \
758 else if (CHAR_VALID_P (ch, 0)) \
759 dst += CHAR_STRING (ch, dst); \
764 CCL_SUSPEND (CCL_STAT_SUSPEND_BY_DST); \
767 /* Encode one character CH to multibyte form and write to the current
768 output buffer. The output bytes always forms a valid multibyte
770 #define CCL_WRITE_MULTIBYTE_CHAR(ch) \
772 int bytes = CHAR_BYTES (ch); \
775 else if (dst + bytes + extra_bytes < (dst_bytes ? dst_end : src)) \
777 if (CHAR_VALID_P ((ch), 0)) \
778 dst += CHAR_STRING ((ch), dst); \
783 CCL_SUSPEND (CCL_STAT_SUSPEND_BY_DST); \
786 /* Write a string at ccl_prog[IC] of length LEN to the current output
788 #define CCL_WRITE_STRING(len) \
792 else if (dst + len <= (dst_bytes ? dst_end : src)) \
793 for (i = 0; i < len; i++) \
794 *dst++ = ((XFASTINT (ccl_prog[ic + (i / 3)])) \
795 >> ((2 - (i % 3)) * 8)) & 0xFF; \
797 CCL_SUSPEND (CCL_STAT_SUSPEND_BY_DST); \
800 /* Read one byte from the current input buffer into REGth register. */
801 #define CCL_READ_CHAR(REG) \
805 else if (src < src_end) \
809 && ccl->eol_type != CODING_EOL_LF) \
811 /* We are encoding. */ \
812 if (ccl->eol_type == CODING_EOL_CRLF) \
814 if (ccl->cr_consumed) \
815 ccl->cr_consumed = 0; \
818 ccl->cr_consumed = 1; \
826 if (REG == LEADING_CODE_8_BIT_CONTROL \
828 REG = *src++ - 0x20; \
830 else if (ccl->last_block) \
837 CCL_SUSPEND (CCL_STAT_SUSPEND_BY_SRC); \
841 /* Set C to the character code made from CHARSET and CODE. This is
842 like MAKE_CHAR but check the validity of CHARSET and CODE. If they
843 are not valid, set C to (CODE & 0xFF) because that is usually the
844 case that CCL_ReadMultibyteChar2 read an invalid code and it set
845 CODE to that invalid byte. */
847 #define CCL_MAKE_CHAR(charset, code, c) \
849 if (charset == CHARSET_ASCII) \
851 else if (CHARSET_DEFINED_P (charset) \
852 && (code & 0x7F) >= 32 \
853 && (code < 256 || ((code >> 7) & 0x7F) >= 32)) \
855 int c1 = code & 0x7F, c2 = 0; \
858 c2 = c1, c1 = (code >> 7) & 0x7F; \
859 c = MAKE_CHAR (charset, c1, c2); \
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_DEBUG_BACKTRACE_LEN
];
877 int ccl_backtrace_idx
;
880 ccl_debug_hook (int ic
)
887 struct ccl_prog_stack
889 Lisp_Object
*ccl_prog
; /* Pointer to an array of CCL code. */
890 int ic
; /* Instruction Counter. */
891 int eof_ic
; /* Instruction Counter to jump on EOF. */
894 /* For the moment, we only support depth 256 of stack. */
895 static struct ccl_prog_stack ccl_prog_stack_struct
[256];
898 ccl_driver (ccl
, source
, destination
, src_bytes
, dst_bytes
, consumed
)
899 struct ccl_program
*ccl
;
900 unsigned char *source
, *destination
;
901 int src_bytes
, dst_bytes
;
904 register int *reg
= ccl
->reg
;
905 register int ic
= ccl
->ic
;
906 register int code
= 0, field1
, field2
;
907 register Lisp_Object
*ccl_prog
= ccl
->prog
;
908 unsigned char *src
= source
, *src_end
= src
+ src_bytes
;
909 unsigned char *dst
= destination
, *dst_end
= dst
+ dst_bytes
;
912 int stack_idx
= ccl
->stack_idx
;
913 /* Instruction counter of the current CCL code. */
915 /* CCL_WRITE_CHAR will produce 8-bit code of range 0x80..0x9F. But,
916 each of them will be converted to multibyte form of 2-byte
917 sequence. For that conversion, we remember how many more bytes
918 we must keep in DESTINATION in this variable. */
919 int extra_bytes
= ccl
->eight_bit_control
;
920 int eof_ic
= ccl
->eof_ic
;
924 ic
= CCL_HEADER_MAIN
;
926 if (ccl
->buf_magnification
== 0) /* We can't produce any bytes. */
929 /* Set mapping stack pointer. */
930 mapping_stack_pointer
= mapping_stack
;
933 ccl_backtrace_idx
= 0;
940 ccl_backtrace_table
[ccl_backtrace_idx
++] = ic
;
941 if (ccl_backtrace_idx
>= CCL_DEBUG_BACKTRACE_LEN
)
942 ccl_backtrace_idx
= 0;
943 ccl_backtrace_table
[ccl_backtrace_idx
] = 0;
946 if (!NILP (Vquit_flag
) && NILP (Vinhibit_quit
))
948 /* We can't just signal Qquit, instead break the loop as if
949 the whole data is processed. Don't reset Vquit_flag, it
950 must be handled later at a safer place. */
952 src
= source
+ src_bytes
;
953 ccl
->status
= CCL_STAT_QUIT
;
958 code
= XINT (ccl_prog
[ic
]); ic
++;
960 field2
= (code
& 0xFF) >> 5;
963 #define RRR (field1 & 7)
964 #define Rrr ((field1 >> 3) & 7)
966 #define EXCMD (field1 >> 6)
970 case CCL_SetRegister
: /* 00000000000000000RRRrrrXXXXX */
974 case CCL_SetShortConst
: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
978 case CCL_SetConst
: /* 00000000000000000000rrrXXXXX */
979 reg
[rrr
] = XINT (ccl_prog
[ic
]);
983 case CCL_SetArray
: /* CCCCCCCCCCCCCCCCCCCCRRRrrrXXXXX */
986 if ((unsigned int) i
< j
)
987 reg
[rrr
] = XINT (ccl_prog
[ic
+ i
]);
991 case CCL_Jump
: /* A--D--D--R--E--S--S-000XXXXX */
995 case CCL_JumpCond
: /* A--D--D--R--E--S--S-rrrXXXXX */
1000 case CCL_WriteRegisterJump
: /* A--D--D--R--E--S--S-rrrXXXXX */
1006 case CCL_WriteRegisterReadJump
: /* A--D--D--R--E--S--S-rrrXXXXX */
1010 CCL_READ_CHAR (reg
[rrr
]);
1014 case CCL_WriteConstJump
: /* A--D--D--R--E--S--S-000XXXXX */
1015 i
= XINT (ccl_prog
[ic
]);
1020 case CCL_WriteConstReadJump
: /* A--D--D--R--E--S--S-rrrXXXXX */
1021 i
= XINT (ccl_prog
[ic
]);
1024 CCL_READ_CHAR (reg
[rrr
]);
1028 case CCL_WriteStringJump
: /* A--D--D--R--E--S--S-000XXXXX */
1029 j
= XINT (ccl_prog
[ic
]);
1031 CCL_WRITE_STRING (j
);
1035 case CCL_WriteArrayReadJump
: /* A--D--D--R--E--S--S-rrrXXXXX */
1037 j
= XINT (ccl_prog
[ic
]);
1038 if ((unsigned int) i
< j
)
1040 i
= XINT (ccl_prog
[ic
+ 1 + i
]);
1044 CCL_READ_CHAR (reg
[rrr
]);
1045 ic
+= ADDR
- (j
+ 2);
1048 case CCL_ReadJump
: /* A--D--D--R--E--S--S-rrrYYYYY */
1049 CCL_READ_CHAR (reg
[rrr
]);
1053 case CCL_ReadBranch
: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
1054 CCL_READ_CHAR (reg
[rrr
]);
1055 /* fall through ... */
1056 case CCL_Branch
: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
1057 if ((unsigned int) reg
[rrr
] < field1
)
1058 ic
+= XINT (ccl_prog
[ic
+ reg
[rrr
]]);
1060 ic
+= XINT (ccl_prog
[ic
+ field1
]);
1063 case CCL_ReadRegister
: /* CCCCCCCCCCCCCCCCCCCCrrXXXXX */
1066 CCL_READ_CHAR (reg
[rrr
]);
1068 code
= XINT (ccl_prog
[ic
]); ic
++;
1070 field2
= (code
& 0xFF) >> 5;
1074 case CCL_WriteExprConst
: /* 1:00000OPERATION000RRR000XXXXX */
1077 j
= XINT (ccl_prog
[ic
]);
1079 jump_address
= ic
+ 1;
1082 case CCL_WriteRegister
: /* CCCCCCCCCCCCCCCCCCCrrrXXXXX */
1088 code
= XINT (ccl_prog
[ic
]); ic
++;
1090 field2
= (code
& 0xFF) >> 5;
1094 case CCL_WriteExprRegister
: /* 1:00000OPERATIONRrrRRR000XXXXX */
1102 case CCL_Call
: /* 1:CCCCCCCCCCCCCCCCCCCCFFFXXXXX */
1107 /* If FFF is nonzero, the CCL program ID is in the
1111 prog_id
= XINT (ccl_prog
[ic
]);
1117 if (stack_idx
>= 256
1119 || prog_id
>= ASIZE (Vccl_program_table
)
1120 || (slot
= AREF (Vccl_program_table
, prog_id
), !VECTORP (slot
))
1121 || !VECTORP (AREF (slot
, 1)))
1125 ccl_prog
= ccl_prog_stack_struct
[0].ccl_prog
;
1126 ic
= ccl_prog_stack_struct
[0].ic
;
1127 eof_ic
= ccl_prog_stack_struct
[0].eof_ic
;
1132 ccl_prog_stack_struct
[stack_idx
].ccl_prog
= ccl_prog
;
1133 ccl_prog_stack_struct
[stack_idx
].ic
= ic
;
1134 ccl_prog_stack_struct
[stack_idx
].eof_ic
= eof_ic
;
1136 ccl_prog
= XVECTOR (AREF (slot
, 1))->contents
;
1137 ic
= CCL_HEADER_MAIN
;
1138 eof_ic
= XFASTINT (ccl_prog
[CCL_HEADER_EOF
]);
1142 case CCL_WriteConstString
: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
1144 CCL_WRITE_CHAR (field1
);
1147 CCL_WRITE_STRING (field1
);
1148 ic
+= (field1
+ 2) / 3;
1152 case CCL_WriteArray
: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
1154 if ((unsigned int) i
< field1
)
1156 j
= XINT (ccl_prog
[ic
+ i
]);
1162 case CCL_End
: /* 0000000000000000000000XXXXX */
1166 ccl_prog
= ccl_prog_stack_struct
[stack_idx
].ccl_prog
;
1167 ic
= ccl_prog_stack_struct
[stack_idx
].ic
;
1168 eof_ic
= ccl_prog_stack_struct
[stack_idx
].eof_ic
;
1175 /* ccl->ic should points to this command code again to
1176 suppress further processing. */
1180 case CCL_ExprSelfConst
: /* 00000OPERATION000000rrrXXXXX */
1181 i
= XINT (ccl_prog
[ic
]);
1186 case CCL_ExprSelfReg
: /* 00000OPERATION000RRRrrrXXXXX */
1193 case CCL_PLUS
: reg
[rrr
] += i
; break;
1194 case CCL_MINUS
: reg
[rrr
] -= i
; break;
1195 case CCL_MUL
: reg
[rrr
] *= i
; break;
1196 case CCL_DIV
: reg
[rrr
] /= i
; break;
1197 case CCL_MOD
: reg
[rrr
] %= i
; break;
1198 case CCL_AND
: reg
[rrr
] &= i
; break;
1199 case CCL_OR
: reg
[rrr
] |= i
; break;
1200 case CCL_XOR
: reg
[rrr
] ^= i
; break;
1201 case CCL_LSH
: reg
[rrr
] <<= i
; break;
1202 case CCL_RSH
: reg
[rrr
] >>= i
; break;
1203 case CCL_LSH8
: reg
[rrr
] <<= 8; reg
[rrr
] |= i
; break;
1204 case CCL_RSH8
: reg
[7] = reg
[rrr
] & 0xFF; reg
[rrr
] >>= 8; break;
1205 case CCL_DIVMOD
: reg
[7] = reg
[rrr
] % i
; reg
[rrr
] /= i
; break;
1206 case CCL_LS
: reg
[rrr
] = reg
[rrr
] < i
; break;
1207 case CCL_GT
: reg
[rrr
] = reg
[rrr
] > i
; break;
1208 case CCL_EQ
: reg
[rrr
] = reg
[rrr
] == i
; break;
1209 case CCL_LE
: reg
[rrr
] = reg
[rrr
] <= i
; break;
1210 case CCL_GE
: reg
[rrr
] = reg
[rrr
] >= i
; break;
1211 case CCL_NE
: reg
[rrr
] = reg
[rrr
] != i
; break;
1212 default: CCL_INVALID_CMD
;
1216 case CCL_SetExprConst
: /* 00000OPERATION000RRRrrrXXXXX */
1218 j
= XINT (ccl_prog
[ic
]);
1220 jump_address
= ++ic
;
1223 case CCL_SetExprReg
: /* 00000OPERATIONRrrRRRrrrXXXXX */
1230 case CCL_ReadJumpCondExprConst
: /* A--D--D--R--E--S--S-rrrXXXXX */
1231 CCL_READ_CHAR (reg
[rrr
]);
1232 case CCL_JumpCondExprConst
: /* A--D--D--R--E--S--S-rrrXXXXX */
1234 op
= XINT (ccl_prog
[ic
]);
1235 jump_address
= ic
++ + ADDR
;
1236 j
= XINT (ccl_prog
[ic
]);
1241 case CCL_ReadJumpCondExprReg
: /* A--D--D--R--E--S--S-rrrXXXXX */
1242 CCL_READ_CHAR (reg
[rrr
]);
1243 case CCL_JumpCondExprReg
:
1245 op
= XINT (ccl_prog
[ic
]);
1246 jump_address
= ic
++ + ADDR
;
1247 j
= reg
[XINT (ccl_prog
[ic
])];
1254 case CCL_PLUS
: reg
[rrr
] = i
+ j
; break;
1255 case CCL_MINUS
: reg
[rrr
] = i
- j
; break;
1256 case CCL_MUL
: reg
[rrr
] = i
* j
; break;
1257 case CCL_DIV
: reg
[rrr
] = i
/ j
; break;
1258 case CCL_MOD
: reg
[rrr
] = i
% j
; break;
1259 case CCL_AND
: reg
[rrr
] = i
& j
; break;
1260 case CCL_OR
: reg
[rrr
] = i
| j
; break;
1261 case CCL_XOR
: reg
[rrr
] = i
^ j
;; break;
1262 case CCL_LSH
: reg
[rrr
] = i
<< j
; break;
1263 case CCL_RSH
: reg
[rrr
] = i
>> j
; break;
1264 case CCL_LSH8
: reg
[rrr
] = (i
<< 8) | j
; break;
1265 case CCL_RSH8
: reg
[rrr
] = i
>> 8; reg
[7] = i
& 0xFF; break;
1266 case CCL_DIVMOD
: reg
[rrr
] = i
/ j
; reg
[7] = i
% j
; break;
1267 case CCL_LS
: reg
[rrr
] = i
< j
; break;
1268 case CCL_GT
: reg
[rrr
] = i
> j
; break;
1269 case CCL_EQ
: reg
[rrr
] = i
== j
; break;
1270 case CCL_LE
: reg
[rrr
] = i
<= j
; break;
1271 case CCL_GE
: reg
[rrr
] = i
>= j
; break;
1272 case CCL_NE
: reg
[rrr
] = i
!= j
; break;
1273 case CCL_DECODE_SJIS
: DECODE_SJIS (i
, j
, reg
[rrr
], reg
[7]); break;
1274 case CCL_ENCODE_SJIS
: ENCODE_SJIS (i
, j
, reg
[rrr
], reg
[7]); break;
1275 default: CCL_INVALID_CMD
;
1278 if (code
== CCL_WriteExprConst
|| code
== CCL_WriteExprRegister
)
1291 case CCL_ReadMultibyteChar2
:
1298 goto ccl_read_multibyte_character_suspend
;
1301 if (!ccl
->multibyte
)
1304 if (!UNIBYTE_STR_AS_MULTIBYTE_P (src
, src_end
- src
, bytes
))
1306 reg
[RRR
] = CHARSET_8_BIT_CONTROL
;
1312 if (i
== '\n' && ccl
->eol_type
!= CODING_EOL_LF
)
1314 /* We are encoding. */
1315 if (ccl
->eol_type
== CODING_EOL_CRLF
)
1317 if (ccl
->cr_consumed
)
1318 ccl
->cr_consumed
= 0;
1321 ccl
->cr_consumed
= 1;
1329 reg
[RRR
] = CHARSET_ASCII
;
1335 reg
[RRR
] = CHARSET_ASCII
;
1337 else if (i
<= MAX_CHARSET_OFFICIAL_DIMENSION2
)
1339 int dimension
= BYTES_BY_CHAR_HEAD (i
) - 1;
1343 /* `i' is a leading code for an undefined charset. */
1344 reg
[RRR
] = CHARSET_8_BIT_GRAPHIC
;
1347 else if (src
+ dimension
> src_end
)
1348 goto ccl_read_multibyte_character_suspend
;
1352 i
= (*src
++ & 0x7F);
1356 reg
[rrr
] = ((i
<< 7) | (*src
++ & 0x7F));
1359 else if ((i
== LEADING_CODE_PRIVATE_11
)
1360 || (i
== LEADING_CODE_PRIVATE_12
))
1362 if ((src
+ 1) >= src_end
)
1363 goto ccl_read_multibyte_character_suspend
;
1365 reg
[rrr
] = (*src
++ & 0x7F);
1367 else if ((i
== LEADING_CODE_PRIVATE_21
)
1368 || (i
== LEADING_CODE_PRIVATE_22
))
1370 if ((src
+ 2) >= src_end
)
1371 goto ccl_read_multibyte_character_suspend
;
1373 i
= (*src
++ & 0x7F);
1374 reg
[rrr
] = ((i
<< 7) | (*src
& 0x7F));
1377 else if (i
== LEADING_CODE_8_BIT_CONTROL
)
1380 goto ccl_read_multibyte_character_suspend
;
1381 reg
[RRR
] = CHARSET_8_BIT_CONTROL
;
1382 reg
[rrr
] = (*src
++ - 0x20);
1386 reg
[RRR
] = CHARSET_8_BIT_GRAPHIC
;
1391 /* INVALID CODE. Return a single byte character. */
1392 reg
[RRR
] = CHARSET_ASCII
;
1397 ccl_read_multibyte_character_suspend
:
1398 if (src
<= src_end
&& !ccl
->multibyte
&& ccl
->last_block
)
1400 reg
[RRR
] = CHARSET_8_BIT_CONTROL
;
1405 if (ccl
->last_block
)
1412 CCL_SUSPEND (CCL_STAT_SUSPEND_BY_SRC
);
1416 case CCL_WriteMultibyteChar2
:
1417 i
= reg
[RRR
]; /* charset */
1418 if (i
== CHARSET_ASCII
1419 || i
== CHARSET_8_BIT_CONTROL
1420 || i
== CHARSET_8_BIT_GRAPHIC
)
1421 i
= reg
[rrr
] & 0xFF;
1422 else if (CHARSET_DIMENSION (i
) == 1)
1423 i
= ((i
- 0x70) << 7) | (reg
[rrr
] & 0x7F);
1424 else if (i
< MIN_CHARSET_PRIVATE_DIMENSION2
)
1425 i
= ((i
- 0x8F) << 14) | reg
[rrr
];
1427 i
= ((i
- 0xE0) << 14) | reg
[rrr
];
1429 CCL_WRITE_MULTIBYTE_CHAR (i
);
1433 case CCL_TranslateCharacter
:
1434 CCL_MAKE_CHAR (reg
[RRR
], reg
[rrr
], i
);
1435 op
= translate_char (GET_TRANSLATION_TABLE (reg
[Rrr
]),
1437 SPLIT_CHAR (op
, reg
[RRR
], i
, j
);
1444 case CCL_TranslateCharacterConstTbl
:
1445 op
= XINT (ccl_prog
[ic
]); /* table */
1447 CCL_MAKE_CHAR (reg
[RRR
], reg
[rrr
], i
);
1448 op
= translate_char (GET_TRANSLATION_TABLE (op
), i
, -1, 0, 0);
1449 SPLIT_CHAR (op
, reg
[RRR
], i
, j
);
1456 case CCL_LookupIntConstTbl
:
1457 op
= XINT (ccl_prog
[ic
]); /* table */
1460 struct Lisp_Hash_Table
*h
= GET_HASH_TABLE (op
);
1462 op
= hash_lookup (h
, make_number (reg
[RRR
]), NULL
);
1466 opl
= HASH_VALUE (h
, op
);
1467 if (!CHAR_VALID_P (XINT (opl
), 0))
1469 SPLIT_CHAR (XINT (opl
), reg
[RRR
], i
, j
);
1473 reg
[7] = 1; /* r7 true for success */
1480 case CCL_LookupCharConstTbl
:
1481 op
= XINT (ccl_prog
[ic
]); /* table */
1483 CCL_MAKE_CHAR (reg
[RRR
], reg
[rrr
], i
);
1485 struct Lisp_Hash_Table
*h
= GET_HASH_TABLE (op
);
1487 op
= hash_lookup (h
, make_number (i
), NULL
);
1491 opl
= HASH_VALUE (h
, op
);
1492 if (!INTEGERP (opl
))
1494 reg
[RRR
] = XINT (opl
);
1495 reg
[7] = 1; /* r7 true for success */
1502 case CCL_IterateMultipleMap
:
1504 Lisp_Object map
, content
, attrib
, value
;
1505 int point
, size
, fin_ic
;
1507 j
= XINT (ccl_prog
[ic
++]); /* number of maps. */
1510 if ((j
> reg
[RRR
]) && (j
>= 0))
1525 size
= ASIZE (Vcode_conversion_map_vector
);
1526 point
= XINT (ccl_prog
[ic
++]);
1527 if (point
>= size
) continue;
1528 map
= AREF (Vcode_conversion_map_vector
, point
);
1530 /* Check map varidity. */
1531 if (!CONSP (map
)) continue;
1533 if (!VECTORP (map
)) continue;
1535 if (size
<= 1) continue;
1537 content
= AREF (map
, 0);
1540 [STARTPOINT VAL1 VAL2 ...] or
1541 [t ELELMENT STARTPOINT ENDPOINT] */
1542 if (NUMBERP (content
))
1544 point
= XUINT (content
);
1545 point
= op
- point
+ 1;
1546 if (!((point
>= 1) && (point
< size
))) continue;
1547 content
= AREF (map
, point
);
1549 else if (EQ (content
, Qt
))
1551 if (size
!= 4) continue;
1552 if ((op
>= XUINT (AREF (map
, 2)))
1553 && (op
< XUINT (AREF (map
, 3))))
1554 content
= AREF (map
, 1);
1563 else if (NUMBERP (content
))
1566 reg
[rrr
] = XINT(content
);
1569 else if (EQ (content
, Qt
) || EQ (content
, Qlambda
))
1574 else if (CONSP (content
))
1576 attrib
= XCAR (content
);
1577 value
= XCDR (content
);
1578 if (!NUMBERP (attrib
) || !NUMBERP (value
))
1581 reg
[rrr
] = XUINT (value
);
1584 else if (SYMBOLP (content
))
1585 CCL_CALL_FOR_MAP_INSTRUCTION (content
, fin_ic
);
1595 case CCL_MapMultiple
:
1597 Lisp_Object map
, content
, attrib
, value
;
1598 int point
, size
, map_vector_size
;
1599 int map_set_rest_length
, fin_ic
;
1600 int current_ic
= this_ic
;
1602 /* inhibit recursive call on MapMultiple. */
1603 if (stack_idx_of_map_multiple
> 0)
1605 if (stack_idx_of_map_multiple
<= stack_idx
)
1607 stack_idx_of_map_multiple
= 0;
1608 mapping_stack_pointer
= mapping_stack
;
1613 mapping_stack_pointer
= mapping_stack
;
1614 stack_idx_of_map_multiple
= 0;
1616 map_set_rest_length
=
1617 XINT (ccl_prog
[ic
++]); /* number of maps and separators. */
1618 fin_ic
= ic
+ map_set_rest_length
;
1621 if ((map_set_rest_length
> reg
[RRR
]) && (reg
[RRR
] >= 0))
1625 map_set_rest_length
-= i
;
1631 mapping_stack_pointer
= mapping_stack
;
1635 if (mapping_stack_pointer
<= (mapping_stack
+ 1))
1637 /* Set up initial state. */
1638 mapping_stack_pointer
= mapping_stack
;
1639 PUSH_MAPPING_STACK (0, op
);
1644 /* Recover after calling other ccl program. */
1647 POP_MAPPING_STACK (map_set_rest_length
, orig_op
);
1648 POP_MAPPING_STACK (map_set_rest_length
, reg
[rrr
]);
1652 /* Regard it as Qnil. */
1656 map_set_rest_length
--;
1659 /* Regard it as Qt. */
1663 map_set_rest_length
--;
1666 /* Regard it as Qlambda. */
1668 i
+= map_set_rest_length
;
1669 ic
+= map_set_rest_length
;
1670 map_set_rest_length
= 0;
1673 /* Regard it as normal mapping. */
1674 i
+= map_set_rest_length
;
1675 ic
+= map_set_rest_length
;
1676 POP_MAPPING_STACK (map_set_rest_length
, reg
[rrr
]);
1680 map_vector_size
= ASIZE (Vcode_conversion_map_vector
);
1683 for (;map_set_rest_length
> 0;i
++, ic
++, map_set_rest_length
--)
1685 point
= XINT(ccl_prog
[ic
]);
1688 /* +1 is for including separator. */
1690 if (mapping_stack_pointer
1691 >= &mapping_stack
[MAX_MAP_SET_LEVEL
])
1693 PUSH_MAPPING_STACK (map_set_rest_length
- point
,
1695 map_set_rest_length
= point
;
1700 if (point
>= map_vector_size
) continue;
1701 map
= AREF (Vcode_conversion_map_vector
, point
);
1703 /* Check map varidity. */
1704 if (!CONSP (map
)) continue;
1706 if (!VECTORP (map
)) continue;
1708 if (size
<= 1) continue;
1710 content
= AREF (map
, 0);
1713 [STARTPOINT VAL1 VAL2 ...] or
1714 [t ELEMENT STARTPOINT ENDPOINT] */
1715 if (NUMBERP (content
))
1717 point
= XUINT (content
);
1718 point
= op
- point
+ 1;
1719 if (!((point
>= 1) && (point
< size
))) continue;
1720 content
= AREF (map
, point
);
1722 else if (EQ (content
, Qt
))
1724 if (size
!= 4) continue;
1725 if ((op
>= XUINT (AREF (map
, 2))) &&
1726 (op
< XUINT (AREF (map
, 3))))
1727 content
= AREF (map
, 1);
1738 if (NUMBERP (content
))
1740 op
= XINT (content
);
1741 i
+= map_set_rest_length
- 1;
1742 ic
+= map_set_rest_length
- 1;
1743 POP_MAPPING_STACK (map_set_rest_length
, reg
[rrr
]);
1744 map_set_rest_length
++;
1746 else if (CONSP (content
))
1748 attrib
= XCAR (content
);
1749 value
= XCDR (content
);
1750 if (!NUMBERP (attrib
) || !NUMBERP (value
))
1753 i
+= map_set_rest_length
- 1;
1754 ic
+= map_set_rest_length
- 1;
1755 POP_MAPPING_STACK (map_set_rest_length
, reg
[rrr
]);
1756 map_set_rest_length
++;
1758 else if (EQ (content
, Qt
))
1762 else if (EQ (content
, Qlambda
))
1764 i
+= map_set_rest_length
;
1765 ic
+= map_set_rest_length
;
1768 else if (SYMBOLP (content
))
1770 if (mapping_stack_pointer
1771 >= &mapping_stack
[MAX_MAP_SET_LEVEL
])
1773 PUSH_MAPPING_STACK (map_set_rest_length
, reg
[rrr
]);
1774 PUSH_MAPPING_STACK (map_set_rest_length
, op
);
1775 stack_idx_of_map_multiple
= stack_idx
+ 1;
1776 CCL_CALL_FOR_MAP_INSTRUCTION (content
, current_ic
);
1781 if (mapping_stack_pointer
<= (mapping_stack
+ 1))
1783 POP_MAPPING_STACK (map_set_rest_length
, reg
[rrr
]);
1784 i
+= map_set_rest_length
;
1785 ic
+= map_set_rest_length
;
1786 POP_MAPPING_STACK (map_set_rest_length
, reg
[rrr
]);
1796 Lisp_Object map
, attrib
, value
, content
;
1798 j
= XINT (ccl_prog
[ic
++]); /* map_id */
1800 if (j
>= ASIZE (Vcode_conversion_map_vector
))
1805 map
= AREF (Vcode_conversion_map_vector
, j
);
1818 point
= XUINT (AREF (map
, 0));
1819 point
= op
- point
+ 1;
1822 (!((point
>= 1) && (point
< size
))))
1827 content
= AREF (map
, point
);
1830 else if (NUMBERP (content
))
1831 reg
[rrr
] = XINT (content
);
1832 else if (EQ (content
, Qt
));
1833 else if (CONSP (content
))
1835 attrib
= XCAR (content
);
1836 value
= XCDR (content
);
1837 if (!NUMBERP (attrib
) || !NUMBERP (value
))
1839 reg
[rrr
] = XUINT(value
);
1842 else if (SYMBOLP (content
))
1843 CCL_CALL_FOR_MAP_INSTRUCTION (content
, ic
);
1861 /* The suppress_error member is set when e.g. a CCL-based coding
1862 system is used for terminal output. */
1863 if (!ccl
->suppress_error
&& destination
)
1865 /* We can insert an error message only if DESTINATION is
1866 specified and we still have a room to store the message
1874 switch (ccl
->status
)
1876 case CCL_STAT_INVALID_CMD
:
1877 sprintf(msg
, "\nCCL: Invalid command %x (ccl_code = %x) at %d.",
1878 code
& 0x1F, code
, this_ic
);
1881 int i
= ccl_backtrace_idx
- 1;
1884 msglen
= strlen (msg
);
1885 if (dst
+ msglen
<= (dst_bytes
? dst_end
: src
))
1887 bcopy (msg
, dst
, msglen
);
1891 for (j
= 0; j
< CCL_DEBUG_BACKTRACE_LEN
; j
++, i
--)
1893 if (i
< 0) i
= CCL_DEBUG_BACKTRACE_LEN
- 1;
1894 if (ccl_backtrace_table
[i
] == 0)
1896 sprintf(msg
, " %d", ccl_backtrace_table
[i
]);
1897 msglen
= strlen (msg
);
1898 if (dst
+ msglen
> (dst_bytes
? dst_end
: src
))
1900 bcopy (msg
, dst
, msglen
);
1909 sprintf(msg
, "\nCCL: Quited.");
1913 sprintf(msg
, "\nCCL: Unknown error type (%d)", ccl
->status
);
1916 msglen
= strlen (msg
);
1917 if (dst
+ msglen
<= (dst_bytes
? dst_end
: src
))
1919 bcopy (msg
, dst
, msglen
);
1923 if (ccl
->status
== CCL_STAT_INVALID_CMD
)
1925 #if 0 /* If the remaining bytes contain 0x80..0x9F, copying them
1926 results in an invalid multibyte sequence. */
1928 /* Copy the remaining source data. */
1929 int i
= src_end
- src
;
1930 if (dst_bytes
&& (dst_end
- dst
) < i
)
1932 bcopy (src
, dst
, i
);
1936 /* Signal that we've consumed everything. */
1944 ccl
->stack_idx
= stack_idx
;
1945 ccl
->prog
= ccl_prog
;
1946 ccl
->eight_bit_control
= (extra_bytes
> 1);
1948 *consumed
= src
- source
;
1949 return (dst
? dst
- destination
: 0);
1952 /* Resolve symbols in the specified CCL code (Lisp vector). This
1953 function converts symbols of code conversion maps and character
1954 translation tables embeded in the CCL code into their ID numbers.
1956 The return value is a vector (CCL itself or a new vector in which
1957 all symbols are resolved), Qt if resolving of some symbol failed,
1958 or nil if CCL contains invalid data. */
1961 resolve_symbol_ccl_program (ccl
)
1964 int i
, veclen
, unresolved
= 0;
1965 Lisp_Object result
, contents
, val
;
1968 veclen
= ASIZE (result
);
1970 for (i
= 0; i
< veclen
; i
++)
1972 contents
= AREF (result
, i
);
1973 if (INTEGERP (contents
))
1975 else if (CONSP (contents
)
1976 && SYMBOLP (XCAR (contents
))
1977 && SYMBOLP (XCDR (contents
)))
1979 /* This is the new style for embedding symbols. The form is
1980 (SYMBOL . PROPERTY). (get SYMBOL PROPERTY) should give
1983 if (EQ (result
, ccl
))
1984 result
= Fcopy_sequence (ccl
);
1986 val
= Fget (XCAR (contents
), XCDR (contents
));
1988 AREF (result
, i
) = val
;
1993 else if (SYMBOLP (contents
))
1995 /* This is the old style for embedding symbols. This style
1996 may lead to a bug if, for instance, a translation table
1997 and a code conversion map have the same name. */
1998 if (EQ (result
, ccl
))
1999 result
= Fcopy_sequence (ccl
);
2001 val
= Fget (contents
, Qtranslation_table_id
);
2003 AREF (result
, i
) = val
;
2006 val
= Fget (contents
, Qcode_conversion_map_id
);
2008 AREF (result
, i
) = val
;
2011 val
= Fget (contents
, Qccl_program_idx
);
2013 AREF (result
, i
) = val
;
2023 return (unresolved
? Qt
: result
);
2026 /* Return the compiled code (vector) of CCL program CCL_PROG.
2027 CCL_PROG is a name (symbol) of the program or already compiled
2028 code. If necessary, resolve symbols in the compiled code to index
2029 numbers. If we failed to get the compiled code or to resolve
2030 symbols, return Qnil. */
2033 ccl_get_compiled_code (ccl_prog
, idx
)
2034 Lisp_Object ccl_prog
;
2037 Lisp_Object val
, slot
;
2039 if (VECTORP (ccl_prog
))
2041 val
= resolve_symbol_ccl_program (ccl_prog
);
2043 return (VECTORP (val
) ? val
: Qnil
);
2045 if (!SYMBOLP (ccl_prog
))
2048 val
= Fget (ccl_prog
, Qccl_program_idx
);
2050 || XINT (val
) >= ASIZE (Vccl_program_table
))
2052 slot
= AREF (Vccl_program_table
, XINT (val
));
2053 if (! VECTORP (slot
)
2054 || ASIZE (slot
) != 4
2055 || ! VECTORP (AREF (slot
, 1)))
2058 if (NILP (AREF (slot
, 2)))
2060 val
= resolve_symbol_ccl_program (AREF (slot
, 1));
2061 if (! VECTORP (val
))
2063 AREF (slot
, 1) = val
;
2064 AREF (slot
, 2) = Qt
;
2066 return AREF (slot
, 1);
2069 /* Setup fields of the structure pointed by CCL appropriately for the
2070 execution of CCL program CCL_PROG. CCL_PROG is the name (symbol)
2071 of the CCL program or the already compiled code (vector).
2072 Return 0 if we succeed this setup, else return -1.
2074 If CCL_PROG is nil, we just reset the structure pointed by CCL. */
2076 setup_ccl_program (ccl
, ccl_prog
)
2077 struct ccl_program
*ccl
;
2078 Lisp_Object ccl_prog
;
2082 if (! NILP (ccl_prog
))
2084 struct Lisp_Vector
*vp
;
2086 ccl_prog
= ccl_get_compiled_code (ccl_prog
, &ccl
->idx
);
2087 if (! VECTORP (ccl_prog
))
2089 vp
= XVECTOR (ccl_prog
);
2090 ccl
->size
= vp
->size
;
2091 ccl
->prog
= vp
->contents
;
2092 ccl
->eof_ic
= XINT (vp
->contents
[CCL_HEADER_EOF
]);
2093 ccl
->buf_magnification
= XINT (vp
->contents
[CCL_HEADER_BUF_MAG
]);
2098 slot
= AREF (Vccl_program_table
, ccl
->idx
);
2099 ASET (slot
, 3, Qnil
);
2102 ccl
->ic
= CCL_HEADER_MAIN
;
2103 for (i
= 0; i
< 8; i
++)
2105 ccl
->last_block
= 0;
2106 ccl
->private_state
= 0;
2109 ccl
->eol_type
= CODING_EOL_LF
;
2110 ccl
->suppress_error
= 0;
2111 ccl
->eight_bit_control
= 0;
2116 /* Check if CCL is updated or not. If not, re-setup members of CCL. */
2119 check_ccl_update (ccl
)
2120 struct ccl_program
*ccl
;
2122 struct Lisp_Vector
*vp
;
2123 Lisp_Object slot
, ccl_prog
;
2127 slot
= AREF (Vccl_program_table
, ccl
->idx
);
2128 if (NILP (AREF (slot
, 3)))
2130 ccl_prog
= ccl_get_compiled_code (AREF (slot
, 0), &ccl
->idx
);
2131 if (! VECTORP (ccl_prog
))
2133 ccl
->size
= ASIZE (ccl_prog
);
2134 ccl
->prog
= XVECTOR (ccl_prog
)->contents
;
2135 ccl
->eof_ic
= XINT (AREF (ccl_prog
, CCL_HEADER_EOF
));
2136 ccl
->buf_magnification
= XINT (AREF (ccl_prog
, CCL_HEADER_BUF_MAG
));
2137 ASET (slot
, 3, Qnil
);
2142 DEFUN ("ccl-program-p", Fccl_program_p
, Sccl_program_p
, 1, 1, 0,
2143 doc
: /* Return t if OBJECT is a CCL program name or a compiled CCL program code.
2144 See the documentation of `define-ccl-program' for the detail of CCL program. */)
2150 if (VECTORP (object
))
2152 val
= resolve_symbol_ccl_program (object
);
2153 return (VECTORP (val
) ? Qt
: Qnil
);
2155 if (!SYMBOLP (object
))
2158 val
= Fget (object
, Qccl_program_idx
);
2159 return ((! NATNUMP (val
)
2160 || XINT (val
) >= ASIZE (Vccl_program_table
))
2164 DEFUN ("ccl-execute", Fccl_execute
, Sccl_execute
, 2, 2, 0,
2165 doc
: /* Execute CCL-PROGRAM with registers initialized by REGISTERS.
2167 CCL-PROGRAM is a CCL program name (symbol)
2168 or compiled code generated by `ccl-compile' (for backward compatibility.
2169 In the latter case, the execution overhead is bigger than in the former).
2170 No I/O commands should appear in CCL-PROGRAM.
2172 REGISTERS is a vector of [R0 R1 ... R7] where RN is an initial value
2173 for the Nth register.
2175 As side effect, each element of REGISTERS holds the value of
2176 the corresponding register after the execution.
2178 See the documentation of `define-ccl-program' for a definition of CCL
2181 Lisp_Object ccl_prog
, reg
;
2183 struct ccl_program ccl
;
2186 if (setup_ccl_program (&ccl
, ccl_prog
) < 0)
2187 error ("Invalid CCL program");
2190 if (ASIZE (reg
) != 8)
2191 error ("Length of vector REGISTERS is not 8");
2193 for (i
= 0; i
< 8; i
++)
2194 ccl
.reg
[i
] = (INTEGERP (AREF (reg
, i
))
2195 ? XINT (AREF (reg
, i
))
2198 ccl_driver (&ccl
, (unsigned char *)0, (unsigned char *)0, 0, 0, (int *)0);
2200 if (ccl
.status
!= CCL_STAT_SUCCESS
)
2201 error ("Error in CCL program at %dth code", ccl
.ic
);
2203 for (i
= 0; i
< 8; i
++)
2204 XSETINT (AREF (reg
, i
), ccl
.reg
[i
]);
2208 DEFUN ("ccl-execute-on-string", Fccl_execute_on_string
, Sccl_execute_on_string
,
2210 doc
: /* Execute CCL-PROGRAM with initial STATUS on STRING.
2212 CCL-PROGRAM is a symbol registered by `register-ccl-program',
2213 or a compiled code generated by `ccl-compile' (for backward compatibility,
2214 in this case, the execution is slower).
2216 Read buffer is set to STRING, and write buffer is allocated automatically.
2218 STATUS is a vector of [R0 R1 ... R7 IC], where
2219 R0..R7 are initial values of corresponding registers,
2220 IC is the instruction counter specifying from where to start the program.
2221 If R0..R7 are nil, they are initialized to 0.
2222 If IC is nil, it is initialized to head of the CCL program.
2224 If optional 4th arg CONTINUE is non-nil, keep IC on read operation
2225 when read buffer is exausted, else, IC is always set to the end of
2226 CCL-PROGRAM on exit.
2228 It returns the contents of write buffer as a string,
2229 and as side effect, STATUS is updated.
2230 If the optional 5th arg UNIBYTE-P is non-nil, the returned string
2231 is a unibyte string. By default it is a multibyte string.
2233 See the documentation of `define-ccl-program' for the detail of CCL program.
2234 usage: (ccl-execute-on-string CCL-PROGRAM STATUS STRING &optional CONTINUE UNIBYTE-P) */)
2235 (ccl_prog
, status
, str
, contin
, unibyte_p
)
2236 Lisp_Object ccl_prog
, status
, str
, contin
, unibyte_p
;
2239 struct ccl_program ccl
;
2243 struct gcpro gcpro1
, gcpro2
;
2245 if (setup_ccl_program (&ccl
, ccl_prog
) < 0)
2246 error ("Invalid CCL program");
2248 CHECK_VECTOR (status
);
2249 if (ASIZE (status
) != 9)
2250 error ("Length of vector STATUS is not 9");
2253 GCPRO2 (status
, str
);
2255 for (i
= 0; i
< 8; i
++)
2257 if (NILP (AREF (status
, i
)))
2258 XSETINT (AREF (status
, i
), 0);
2259 if (INTEGERP (AREF (status
, i
)))
2260 ccl
.reg
[i
] = XINT (AREF (status
, i
));
2262 if (INTEGERP (AREF (status
, i
)))
2264 i
= XFASTINT (AREF (status
, 8));
2265 if (ccl
.ic
< i
&& i
< ccl
.size
)
2268 outbufsize
= SBYTES (str
) * ccl
.buf_magnification
+ 256;
2269 outbuf
= (char *) xmalloc (outbufsize
);
2270 ccl
.last_block
= NILP (contin
);
2271 ccl
.multibyte
= STRING_MULTIBYTE (str
);
2272 produced
= ccl_driver (&ccl
, SDATA (str
), outbuf
,
2273 SBYTES (str
), outbufsize
, (int *) 0);
2274 for (i
= 0; i
< 8; i
++)
2275 ASET (status
, i
, make_number (ccl
.reg
[i
]));
2276 ASET (status
, 8, make_number (ccl
.ic
));
2279 if (NILP (unibyte_p
))
2283 produced
= str_as_multibyte (outbuf
, outbufsize
, produced
, &nchars
);
2284 val
= make_multibyte_string (outbuf
, nchars
, produced
);
2287 val
= make_unibyte_string (outbuf
, produced
);
2290 if (ccl
.status
== CCL_STAT_SUSPEND_BY_DST
)
2291 error ("Output buffer for the CCL programs overflow");
2292 if (ccl
.status
!= CCL_STAT_SUCCESS
2293 && ccl
.status
!= CCL_STAT_SUSPEND_BY_SRC
)
2294 error ("Error in CCL program at %dth code", ccl
.ic
);
2299 DEFUN ("register-ccl-program", Fregister_ccl_program
, Sregister_ccl_program
,
2301 doc
: /* Register CCL program CCL-PROG as NAME in `ccl-program-table'.
2302 CCL-PROG should be a compiled CCL program (vector), or nil.
2303 If it is nil, just reserve NAME as a CCL program name.
2304 Return index number of the registered CCL program. */)
2306 Lisp_Object name
, ccl_prog
;
2308 int len
= ASIZE (Vccl_program_table
);
2310 Lisp_Object resolved
;
2312 CHECK_SYMBOL (name
);
2314 if (!NILP (ccl_prog
))
2316 CHECK_VECTOR (ccl_prog
);
2317 resolved
= resolve_symbol_ccl_program (ccl_prog
);
2318 if (NILP (resolved
))
2319 error ("Error in CCL program");
2320 if (VECTORP (resolved
))
2322 ccl_prog
= resolved
;
2329 for (idx
= 0; idx
< len
; idx
++)
2333 slot
= AREF (Vccl_program_table
, idx
);
2334 if (!VECTORP (slot
))
2335 /* This is the first unsed slot. Register NAME here. */
2338 if (EQ (name
, AREF (slot
, 0)))
2340 /* Update this slot. */
2341 ASET (slot
, 1, ccl_prog
);
2342 ASET (slot
, 2, resolved
);
2344 return make_number (idx
);
2350 /* Extend the table. */
2351 Lisp_Object new_table
;
2354 new_table
= Fmake_vector (make_number (len
* 2), Qnil
);
2355 for (j
= 0; j
< len
; j
++)
2356 ASET (new_table
, j
, AREF (Vccl_program_table
, j
));
2357 Vccl_program_table
= new_table
;
2363 elt
= Fmake_vector (make_number (4), Qnil
);
2364 ASET (elt
, 0, name
);
2365 ASET (elt
, 1, ccl_prog
);
2366 ASET (elt
, 2, resolved
);
2368 ASET (Vccl_program_table
, idx
, elt
);
2371 Fput (name
, Qccl_program_idx
, make_number (idx
));
2372 return make_number (idx
);
2375 /* Register code conversion map.
2376 A code conversion map consists of numbers, Qt, Qnil, and Qlambda.
2377 The first element is the start code point.
2378 The other elements are mapped numbers.
2379 Symbol t means to map to an original number before mapping.
2380 Symbol nil means that the corresponding element is empty.
2381 Symbol lambda means to terminate mapping here.
2384 DEFUN ("register-code-conversion-map", Fregister_code_conversion_map
,
2385 Sregister_code_conversion_map
,
2387 doc
: /* Register SYMBOL as code conversion map MAP.
2388 Return index number of the registered map. */)
2390 Lisp_Object symbol
, map
;
2392 int len
= ASIZE (Vcode_conversion_map_vector
);
2396 CHECK_SYMBOL (symbol
);
2399 for (i
= 0; i
< len
; i
++)
2401 Lisp_Object slot
= AREF (Vcode_conversion_map_vector
, i
);
2406 if (EQ (symbol
, XCAR (slot
)))
2408 index
= make_number (i
);
2409 XSETCDR (slot
, map
);
2410 Fput (symbol
, Qcode_conversion_map
, map
);
2411 Fput (symbol
, Qcode_conversion_map_id
, index
);
2418 Lisp_Object new_vector
= Fmake_vector (make_number (len
* 2), Qnil
);
2421 for (j
= 0; j
< len
; j
++)
2422 AREF (new_vector
, j
)
2423 = AREF (Vcode_conversion_map_vector
, j
);
2424 Vcode_conversion_map_vector
= new_vector
;
2427 index
= make_number (i
);
2428 Fput (symbol
, Qcode_conversion_map
, map
);
2429 Fput (symbol
, Qcode_conversion_map_id
, index
);
2430 AREF (Vcode_conversion_map_vector
, i
) = Fcons (symbol
, map
);
2438 staticpro (&Vccl_program_table
);
2439 Vccl_program_table
= Fmake_vector (make_number (32), Qnil
);
2441 Qccl_program
= intern ("ccl-program");
2442 staticpro (&Qccl_program
);
2444 Qccl_program_idx
= intern ("ccl-program-idx");
2445 staticpro (&Qccl_program_idx
);
2447 Qcode_conversion_map
= intern ("code-conversion-map");
2448 staticpro (&Qcode_conversion_map
);
2450 Qcode_conversion_map_id
= intern ("code-conversion-map-id");
2451 staticpro (&Qcode_conversion_map_id
);
2453 DEFVAR_LISP ("code-conversion-map-vector", &Vcode_conversion_map_vector
,
2454 doc
: /* Vector of code conversion maps. */);
2455 Vcode_conversion_map_vector
= Fmake_vector (make_number (16), Qnil
);
2457 DEFVAR_LISP ("font-ccl-encoder-alist", &Vfont_ccl_encoder_alist
,
2458 doc
: /* Alist of fontname patterns vs corresponding CCL program.
2459 Each element looks like (REGEXP . CCL-CODE),
2460 where CCL-CODE is a compiled CCL program.
2461 When a font whose name matches REGEXP is used for displaying a character,
2462 CCL-CODE is executed to calculate the code point in the font
2463 from the charset number and position code(s) of the character which are set
2464 in CCL registers R0, R1, and R2 before the execution.
2465 The code point in the font is set in CCL registers R1 and R2
2466 when the execution terminated.
2467 If the font is single-byte font, the register R2 is not used. */);
2468 Vfont_ccl_encoder_alist
= Qnil
;
2470 DEFVAR_LISP ("translation-hash-table-vector", &Vtranslation_hash_table_vector
,
2471 doc
: /* Vector containing all translation hash tables ever defined.
2472 Comprises pairs (SYMBOL . TABLE) where SYMBOL and TABLE were set up by calls
2473 to `define-translation-hash-table'. The vector is indexed by the table id
2475 Vtranslation_hash_table_vector
= Qnil
;
2477 defsubr (&Sccl_program_p
);
2478 defsubr (&Sccl_execute
);
2479 defsubr (&Sccl_execute_on_string
);
2480 defsubr (&Sregister_ccl_program
);
2481 defsubr (&Sregister_code_conversion_map
);
2484 /* arch-tag: bb9a37be-68ce-4576-8d3d-15d750e4a860
2485 (do not change this comment) */