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. */
35 #include "character.h"
44 #endif /* not emacs */
46 Lisp_Object Qccl
, Qcclp
;
48 /* This contains all code conversion map available to CCL. */
49 Lisp_Object Vcode_conversion_map_vector
;
51 /* Alist of fontname patterns vs corresponding CCL program. */
52 Lisp_Object Vfont_ccl_encoder_alist
;
54 /* This symbol is a property which assocates with ccl program vector.
55 Ex: (get 'ccl-big5-encoder 'ccl-program) returns ccl program vector. */
56 Lisp_Object Qccl_program
;
58 /* These symbols are properties which associate with code conversion
59 map and their ID respectively. */
60 Lisp_Object Qcode_conversion_map
;
61 Lisp_Object Qcode_conversion_map_id
;
63 /* Symbols of ccl program have this property, a value of the property
64 is an index for Vccl_protram_table. */
65 Lisp_Object Qccl_program_idx
;
67 /* Table of registered CCL programs. Each element is a vector of
68 NAME, CCL_PROG, and RESOLVEDP where NAME (symbol) is the name of
69 the program, CCL_PROG (vector) is the compiled code of the program,
70 RESOLVEDP (t or nil) is the flag to tell if symbols in CCL_PROG is
71 already resolved to index numbers or not. */
72 Lisp_Object Vccl_program_table
;
74 /* CCL (Code Conversion Language) is a simple language which has
75 operations on one input buffer, one output buffer, and 7 registers.
76 The syntax of CCL is described in `ccl.el'. Emacs Lisp function
77 `ccl-compile' compiles a CCL program and produces a CCL code which
78 is a vector of integers. The structure of this vector is as
79 follows: The 1st element: buffer-magnification, a factor for the
80 size of output buffer compared with the size of input buffer. The
81 2nd element: address of CCL code to be executed when encountered
82 with end of input stream. The 3rd and the remaining elements: CCL
85 /* Header of CCL compiled code */
86 #define CCL_HEADER_BUF_MAG 0
87 #define CCL_HEADER_EOF 1
88 #define CCL_HEADER_MAIN 2
90 /* CCL code is a sequence of 28-bit non-negative integers (i.e. the
91 MSB is always 0), each contains CCL command and/or arguments in the
94 |----------------- integer (28-bit) ------------------|
95 |------- 17-bit ------|- 3-bit --|- 3-bit --|- 5-bit -|
96 |--constant argument--|-register-|-register-|-command-|
97 ccccccccccccccccc RRR rrr XXXXX
99 |------- relative address -------|-register-|-command-|
100 cccccccccccccccccccc rrr XXXXX
102 |------------- constant or other args ----------------|
103 cccccccccccccccccccccccccccc
105 where, `cc...c' is a non-negative integer indicating constant value
106 (the left most `c' is always 0) or an absolute jump address, `RRR'
107 and `rrr' are CCL register number, `XXXXX' is one of the following
112 Each comment fields shows one or more lines for command syntax and
113 the following lines for semantics of the command. In semantics, IC
114 stands for Instruction Counter. */
116 #define CCL_SetRegister 0x00 /* Set register a register value:
117 1:00000000000000000RRRrrrXXXXX
118 ------------------------------
122 #define CCL_SetShortConst 0x01 /* Set register a short constant value:
123 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
124 ------------------------------
125 reg[rrr] = CCCCCCCCCCCCCCCCCCC;
128 #define CCL_SetConst 0x02 /* Set register a constant value:
129 1:00000000000000000000rrrXXXXX
131 ------------------------------
136 #define CCL_SetArray 0x03 /* Set register an element of array:
137 1:CCCCCCCCCCCCCCCCCRRRrrrXXXXX
141 ------------------------------
142 if (0 <= reg[RRR] < CC..C)
143 reg[rrr] = ELEMENT[reg[RRR]];
147 #define CCL_Jump 0x04 /* Jump:
148 1:A--D--D--R--E--S--S-000XXXXX
149 ------------------------------
153 /* Note: If CC..C is greater than 0, the second code is omitted. */
155 #define CCL_JumpCond 0x05 /* Jump conditional:
156 1:A--D--D--R--E--S--S-rrrXXXXX
157 ------------------------------
163 #define CCL_WriteRegisterJump 0x06 /* Write register and jump:
164 1:A--D--D--R--E--S--S-rrrXXXXX
165 ------------------------------
170 #define CCL_WriteRegisterReadJump 0x07 /* Write register, read, and jump:
171 1:A--D--D--R--E--S--S-rrrXXXXX
172 2:A--D--D--R--E--S--S-rrrYYYYY
173 -----------------------------
179 /* Note: If read is suspended, the resumed execution starts from the
180 second code (YYYYY == CCL_ReadJump). */
182 #define CCL_WriteConstJump 0x08 /* Write constant and jump:
183 1:A--D--D--R--E--S--S-000XXXXX
185 ------------------------------
190 #define CCL_WriteConstReadJump 0x09 /* Write constant, read, and jump:
191 1:A--D--D--R--E--S--S-rrrXXXXX
193 3:A--D--D--R--E--S--S-rrrYYYYY
194 -----------------------------
200 /* Note: If read is suspended, the resumed execution starts from the
201 second code (YYYYY == CCL_ReadJump). */
203 #define CCL_WriteStringJump 0x0A /* Write string and jump:
204 1:A--D--D--R--E--S--S-000XXXXX
206 3:0000STRIN[0]STRIN[1]STRIN[2]
208 ------------------------------
209 write_string (STRING, LENGTH);
213 #define CCL_WriteArrayReadJump 0x0B /* Write an array element, read, and jump:
214 1:A--D--D--R--E--S--S-rrrXXXXX
219 N:A--D--D--R--E--S--S-rrrYYYYY
220 ------------------------------
221 if (0 <= reg[rrr] < LENGTH)
222 write (ELEMENT[reg[rrr]]);
223 IC += LENGTH + 2; (... pointing at N+1)
227 /* Note: If read is suspended, the resumed execution starts from the
228 Nth code (YYYYY == CCL_ReadJump). */
230 #define CCL_ReadJump 0x0C /* Read and jump:
231 1:A--D--D--R--E--S--S-rrrYYYYY
232 -----------------------------
237 #define CCL_Branch 0x0D /* Jump by branch table:
238 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
239 2:A--D--D--R--E-S-S[0]000XXXXX
240 3:A--D--D--R--E-S-S[1]000XXXXX
242 ------------------------------
243 if (0 <= reg[rrr] < CC..C)
244 IC += ADDRESS[reg[rrr]];
246 IC += ADDRESS[CC..C];
249 #define CCL_ReadRegister 0x0E /* Read bytes into registers:
250 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
251 2:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
253 ------------------------------
258 #define CCL_WriteExprConst 0x0F /* write result of expression:
259 1:00000OPERATION000RRR000XXXXX
261 ------------------------------
262 write (reg[RRR] OPERATION CONSTANT);
266 /* Note: If the Nth read is suspended, the resumed execution starts
267 from the Nth code. */
269 #define CCL_ReadBranch 0x10 /* Read one byte into a register,
270 and jump by branch table:
271 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
272 2:A--D--D--R--E-S-S[0]000XXXXX
273 3:A--D--D--R--E-S-S[1]000XXXXX
275 ------------------------------
277 if (0 <= reg[rrr] < CC..C)
278 IC += ADDRESS[reg[rrr]];
280 IC += ADDRESS[CC..C];
283 #define CCL_WriteRegister 0x11 /* Write registers:
284 1:CCCCCCCCCCCCCCCCCCCrrrXXXXX
285 2:CCCCCCCCCCCCCCCCCCCrrrXXXXX
287 ------------------------------
293 /* Note: If the Nth write is suspended, the resumed execution
294 starts from the Nth code. */
296 #define CCL_WriteExprRegister 0x12 /* Write result of expression
297 1:00000OPERATIONRrrRRR000XXXXX
298 ------------------------------
299 write (reg[RRR] OPERATION reg[Rrr]);
302 #define CCL_Call 0x13 /* Call the CCL program whose ID is
304 1:CCCCCCCCCCCCCCCCCCCCFFFXXXXX
305 [2:00000000cccccccccccccccccccc]
306 ------------------------------
314 #define CCL_WriteConstString 0x14 /* Write a constant or a string:
315 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
316 [2:0000STRIN[0]STRIN[1]STRIN[2]]
318 -----------------------------
322 write_string (STRING, CC..C);
323 IC += (CC..C + 2) / 3;
326 #define CCL_WriteArray 0x15 /* Write an element of array:
327 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
331 ------------------------------
332 if (0 <= reg[rrr] < CC..C)
333 write (ELEMENT[reg[rrr]]);
337 #define CCL_End 0x16 /* Terminate:
338 1:00000000000000000000000XXXXX
339 ------------------------------
343 /* The following two codes execute an assignment arithmetic/logical
344 operation. The form of the operation is like REG OP= OPERAND. */
346 #define CCL_ExprSelfConst 0x17 /* REG OP= constant:
347 1:00000OPERATION000000rrrXXXXX
349 ------------------------------
350 reg[rrr] OPERATION= CONSTANT;
353 #define CCL_ExprSelfReg 0x18 /* REG1 OP= REG2:
354 1:00000OPERATION000RRRrrrXXXXX
355 ------------------------------
356 reg[rrr] OPERATION= reg[RRR];
359 /* The following codes execute an arithmetic/logical operation. The
360 form of the operation is like REG_X = REG_Y OP OPERAND2. */
362 #define CCL_SetExprConst 0x19 /* REG_X = REG_Y OP constant:
363 1:00000OPERATION000RRRrrrXXXXX
365 ------------------------------
366 reg[rrr] = reg[RRR] OPERATION CONSTANT;
370 #define CCL_SetExprReg 0x1A /* REG1 = REG2 OP REG3:
371 1:00000OPERATIONRrrRRRrrrXXXXX
372 ------------------------------
373 reg[rrr] = reg[RRR] OPERATION reg[Rrr];
376 #define CCL_JumpCondExprConst 0x1B /* Jump conditional according to
377 an operation on constant:
378 1:A--D--D--R--E--S--S-rrrXXXXX
381 -----------------------------
382 reg[7] = reg[rrr] OPERATION CONSTANT;
389 #define CCL_JumpCondExprReg 0x1C /* Jump conditional according to
390 an operation on register:
391 1:A--D--D--R--E--S--S-rrrXXXXX
394 -----------------------------
395 reg[7] = reg[rrr] OPERATION reg[RRR];
402 #define CCL_ReadJumpCondExprConst 0x1D /* Read and jump conditional according
403 to an operation on constant:
404 1:A--D--D--R--E--S--S-rrrXXXXX
407 -----------------------------
409 reg[7] = reg[rrr] OPERATION CONSTANT;
416 #define CCL_ReadJumpCondExprReg 0x1E /* Read and jump conditional according
417 to an operation on register:
418 1:A--D--D--R--E--S--S-rrrXXXXX
421 -----------------------------
423 reg[7] = reg[rrr] OPERATION reg[RRR];
430 #define CCL_Extension 0x1F /* Extended CCL code
431 1:ExtendedCOMMNDRrrRRRrrrXXXXX
434 ------------------------------
435 extended_command (rrr,RRR,Rrr,ARGS)
439 Here after, Extended CCL Instructions.
440 Bit length of extended command is 14.
441 Therefore, the instruction code range is 0..16384(0x3fff).
444 /* Read a multibyte characeter.
445 A code point is stored into reg[rrr]. A charset ID is stored into
448 #define CCL_ReadMultibyteChar2 0x00 /* Read Multibyte Character
449 1:ExtendedCOMMNDRrrRRRrrrXXXXX */
451 /* Write a multibyte character.
452 Write a character whose code point is reg[rrr] and the charset ID
455 #define CCL_WriteMultibyteChar2 0x01 /* Write Multibyte Character
456 1:ExtendedCOMMNDRrrRRRrrrXXXXX */
458 /* Translate a character whose code point is reg[rrr] and the charset
459 ID is reg[RRR] by a translation table whose ID is reg[Rrr].
461 A translated character is set in reg[rrr] (code point) and reg[RRR]
464 #define CCL_TranslateCharacter 0x02 /* Translate a multibyte character
465 1:ExtendedCOMMNDRrrRRRrrrXXXXX */
467 /* Translate a character whose code point is reg[rrr] and the charset
468 ID is reg[RRR] by a translation table whose ID is ARGUMENT.
470 A translated character is set in reg[rrr] (code point) and reg[RRR]
473 #define CCL_TranslateCharacterConstTbl 0x03 /* Translate a multibyte character
474 1:ExtendedCOMMNDRrrRRRrrrXXXXX
475 2:ARGUMENT(Translation Table ID)
478 /* Iterate looking up MAPs for reg[rrr] starting from the Nth (N =
479 reg[RRR]) MAP until some value is found.
481 Each MAP is a Lisp vector whose element is number, nil, t, or
483 If the element is nil, ignore the map and proceed to the next map.
484 If the element is t or lambda, finish without changing reg[rrr].
485 If the element is a number, set reg[rrr] to the number and finish.
487 Detail of the map structure is descibed in the comment for
488 CCL_MapMultiple below. */
490 #define CCL_IterateMultipleMap 0x10 /* Iterate multiple maps
491 1:ExtendedCOMMNDXXXRRRrrrXXXXX
498 /* Map the code in reg[rrr] by MAPs starting from the Nth (N =
501 MAPs are supplied in the succeeding CCL codes as follows:
503 When CCL program gives this nested structure of map to this command:
506 (MAP-ID121 MAP-ID122 MAP-ID123)
509 (MAP-ID211 (MAP-ID2111) MAP-ID212)
511 the compiled CCL codes has this sequence:
512 CCL_MapMultiple (CCL code of this command)
513 16 (total number of MAPs and SEPARATORs)
531 A value of each SEPARATOR follows this rule:
532 MAP-SET := SEPARATOR [(MAP-ID | MAP-SET)]+
533 SEPARATOR := -(number of MAP-IDs and SEPARATORs in the MAP-SET)
535 (*)....Nest level of MAP-SET must not be over than MAX_MAP_SET_LEVEL.
537 When some map fails to map (i.e. it doesn't have a value for
538 reg[rrr]), the mapping is treated as identity.
540 The mapping is iterated for all maps in each map set (set of maps
541 separated by SEPARATOR) except in the case that lambda is
542 encountered. More precisely, the mapping proceeds as below:
544 At first, VAL0 is set to reg[rrr], and it is translated by the
545 first map to VAL1. Then, VAL1 is translated by the next map to
546 VAL2. This mapping is iterated until the last map is used. The
547 result of the mapping is the last value of VAL?. When the mapping
548 process reached to the end of the map set, it moves to the next
549 map set. If the next does not exit, the mapping process terminates,
550 and regard the last value as a result.
552 But, when VALm is mapped to VALn and VALn is not a number, the
553 mapping proceed as below:
555 If VALn is nil, the lastest map is ignored and the mapping of VALm
556 proceed to the next map.
558 In VALn is t, VALm is reverted to reg[rrr] and the mapping of VALm
559 proceed to the next map.
561 If VALn is lambda, move to the next map set like reaching to the
562 end of the current map set.
564 If VALn is a symbol, call the CCL program refered by it.
565 Then, use reg[rrr] as a mapped value except for -1, -2 and -3.
566 Such special values are regarded as nil, t, and lambda respectively.
568 Each map is a Lisp vector of the following format (a) or (b):
569 (a)......[STARTPOINT VAL1 VAL2 ...]
570 (b)......[t VAL STARTPOINT ENDPOINT],
572 STARTPOINT is an offset to be used for indexing a map,
573 ENDPOINT is a maximum index number of a map,
574 VAL and VALn is a number, nil, t, or lambda.
576 Valid index range of a map of type (a) is:
577 STARTPOINT <= index < STARTPOINT + map_size - 1
578 Valid index range of a map of type (b) is:
579 STARTPOINT <= index < ENDPOINT */
581 #define CCL_MapMultiple 0x11 /* Mapping by multiple code conversion maps
582 1:ExtendedCOMMNDXXXRRRrrrXXXXX
594 #define MAX_MAP_SET_LEVEL 30
602 static tr_stack mapping_stack
[MAX_MAP_SET_LEVEL
];
603 static tr_stack
*mapping_stack_pointer
;
605 /* If this variable is non-zero, it indicates the stack_idx
606 of immediately called by CCL_MapMultiple. */
607 static int stack_idx_of_map_multiple
;
609 #define PUSH_MAPPING_STACK(restlen, orig) \
612 mapping_stack_pointer->rest_length = (restlen); \
613 mapping_stack_pointer->orig_val = (orig); \
614 mapping_stack_pointer++; \
618 #define POP_MAPPING_STACK(restlen, orig) \
621 mapping_stack_pointer--; \
622 (restlen) = mapping_stack_pointer->rest_length; \
623 (orig) = mapping_stack_pointer->orig_val; \
627 #define CCL_CALL_FOR_MAP_INSTRUCTION(symbol, ret_ic) \
630 struct ccl_program called_ccl; \
631 if (stack_idx >= 256 \
632 || (setup_ccl_program (&called_ccl, (symbol)) != 0)) \
636 ccl_prog = ccl_prog_stack_struct[0].ccl_prog; \
637 ic = ccl_prog_stack_struct[0].ic; \
641 ccl_prog_stack_struct[stack_idx].ccl_prog = ccl_prog; \
642 ccl_prog_stack_struct[stack_idx].ic = (ret_ic); \
644 ccl_prog = called_ccl.prog; \
645 ic = CCL_HEADER_MAIN; \
650 #define CCL_MapSingle 0x12 /* Map by single code conversion map
651 1:ExtendedCOMMNDXXXRRRrrrXXXXX
653 ------------------------------
654 Map reg[rrr] by MAP-ID.
655 If some valid mapping is found,
656 set reg[rrr] to the result,
661 /* CCL arithmetic/logical operators. */
662 #define CCL_PLUS 0x00 /* X = Y + Z */
663 #define CCL_MINUS 0x01 /* X = Y - Z */
664 #define CCL_MUL 0x02 /* X = Y * Z */
665 #define CCL_DIV 0x03 /* X = Y / Z */
666 #define CCL_MOD 0x04 /* X = Y % Z */
667 #define CCL_AND 0x05 /* X = Y & Z */
668 #define CCL_OR 0x06 /* X = Y | Z */
669 #define CCL_XOR 0x07 /* X = Y ^ Z */
670 #define CCL_LSH 0x08 /* X = Y << Z */
671 #define CCL_RSH 0x09 /* X = Y >> Z */
672 #define CCL_LSH8 0x0A /* X = (Y << 8) | Z */
673 #define CCL_RSH8 0x0B /* X = Y >> 8, r[7] = Y & 0xFF */
674 #define CCL_DIVMOD 0x0C /* X = Y / Z, r[7] = Y % Z */
675 #define CCL_LS 0x10 /* X = (X < Y) */
676 #define CCL_GT 0x11 /* X = (X > Y) */
677 #define CCL_EQ 0x12 /* X = (X == Y) */
678 #define CCL_LE 0x13 /* X = (X <= Y) */
679 #define CCL_GE 0x14 /* X = (X >= Y) */
680 #define CCL_NE 0x15 /* X = (X != Y) */
682 #define CCL_DECODE_SJIS 0x16 /* X = HIGHER_BYTE (DE-SJIS (Y, Z))
683 r[7] = LOWER_BYTE (DE-SJIS (Y, Z)) */
684 #define CCL_ENCODE_SJIS 0x17 /* X = HIGHER_BYTE (SJIS (Y, Z))
685 r[7] = LOWER_BYTE (SJIS (Y, Z) */
687 /* Terminate CCL program successfully. */
688 #define CCL_SUCCESS \
691 ccl->status = CCL_STAT_SUCCESS; \
696 /* Suspend CCL program because of reading from empty input buffer or
697 writing to full output buffer. When this program is resumed, the
698 same I/O command is executed. */
699 #define CCL_SUSPEND(stat) \
703 ccl->status = stat; \
708 /* Terminate CCL program because of invalid command. Should not occur
709 in the normal case. */
710 #define CCL_INVALID_CMD \
713 ccl->status = CCL_STAT_INVALID_CMD; \
714 goto ccl_error_handler; \
718 /* Encode one character CH to multibyte form and write to the current
719 output buffer. If CH is less than 256, CH is written as is. */
720 #define CCL_WRITE_CHAR(ch) \
724 else if (dst < dst_end) \
727 CCL_SUSPEND (CCL_STAT_SUSPEND_BY_DST); \
730 /* Write a string at ccl_prog[IC] of length LEN to the current output
732 #define CCL_WRITE_STRING(len) \
737 else if (dst + len <= dst_end) \
738 for (i = 0; i < len; i++) \
739 *dst++ = ((XFASTINT (ccl_prog[ic + (i / 3)])) \
740 >> ((2 - (i % 3)) * 8)) & 0xFF; \
742 CCL_SUSPEND (CCL_STAT_SUSPEND_BY_DST); \
745 /* Read one byte from the current input buffer into Rth register. */
746 #define CCL_READ_CHAR(r) \
750 else if (src < src_end) \
752 else if (ccl->last_block) \
758 CCL_SUSPEND (CCL_STAT_SUSPEND_BY_SRC); \
762 /* Execute CCL code on characters at SOURCE (length SRC_SIZE). The
763 resulting text goes to a place pointed by DESTINATION, the length
764 of which should not exceed DST_SIZE. As a side effect, how many
765 characters are consumed and produced are recorded in CCL->consumed
766 and CCL->produced, and the contents of CCL registers are updated.
767 If SOURCE or DESTINATION is NULL, only operations on registers are
771 #define CCL_DEBUG_BACKTRACE_LEN 256
772 int ccl_backtrace_table
[CCL_BACKTRACE_TABLE
];
773 int ccl_backtrace_idx
;
776 struct ccl_prog_stack
778 Lisp_Object
*ccl_prog
; /* Pointer to an array of CCL code. */
779 int ic
; /* Instruction Counter. */
782 /* For the moment, we only support depth 256 of stack. */
783 static struct ccl_prog_stack ccl_prog_stack_struct
[256];
786 ccl_driver (ccl
, source
, destination
, src_size
, dst_size
)
787 struct ccl_program
*ccl
;
788 int *source
, *destination
;
789 int src_size
, dst_size
;
791 register int *reg
= ccl
->reg
;
792 register int ic
= ccl
->ic
;
793 register int code
= 0, field1
, field2
;
794 register Lisp_Object
*ccl_prog
= ccl
->prog
;
795 int *src
= source
, *src_end
= src
+ src_size
;
796 int *dst
= destination
, *dst_end
= dst
+ dst_size
;
799 int stack_idx
= ccl
->stack_idx
;
800 /* Instruction counter of the current CCL code. */
802 struct charset
*charset
;
804 if (ic
>= ccl
->eof_ic
)
805 ic
= CCL_HEADER_MAIN
;
807 if (ccl
->buf_magnification
== 0) /* We can't read/produce any bytes. */
810 /* Set mapping stack pointer. */
811 mapping_stack_pointer
= mapping_stack
;
814 ccl_backtrace_idx
= 0;
821 ccl_backtrace_table
[ccl_backtrace_idx
++] = ic
;
822 if (ccl_backtrace_idx
>= CCL_DEBUG_BACKTRACE_LEN
)
823 ccl_backtrace_idx
= 0;
824 ccl_backtrace_table
[ccl_backtrace_idx
] = 0;
827 if (!NILP (Vquit_flag
) && NILP (Vinhibit_quit
))
829 /* We can't just signal Qquit, instead break the loop as if
830 the whole data is processed. Don't reset Vquit_flag, it
831 must be handled later at a safer place. */
833 src
= source
+ src_size
;
834 ccl
->status
= CCL_STAT_QUIT
;
839 code
= XINT (ccl_prog
[ic
]); ic
++;
841 field2
= (code
& 0xFF) >> 5;
844 #define RRR (field1 & 7)
845 #define Rrr ((field1 >> 3) & 7)
847 #define EXCMD (field1 >> 6)
851 case CCL_SetRegister
: /* 00000000000000000RRRrrrXXXXX */
855 case CCL_SetShortConst
: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
859 case CCL_SetConst
: /* 00000000000000000000rrrXXXXX */
860 reg
[rrr
] = XINT (ccl_prog
[ic
]);
864 case CCL_SetArray
: /* CCCCCCCCCCCCCCCCCCCCRRRrrrXXXXX */
867 if ((unsigned int) i
< j
)
868 reg
[rrr
] = XINT (ccl_prog
[ic
+ i
]);
872 case CCL_Jump
: /* A--D--D--R--E--S--S-000XXXXX */
876 case CCL_JumpCond
: /* A--D--D--R--E--S--S-rrrXXXXX */
881 case CCL_WriteRegisterJump
: /* A--D--D--R--E--S--S-rrrXXXXX */
887 case CCL_WriteRegisterReadJump
: /* A--D--D--R--E--S--S-rrrXXXXX */
891 CCL_READ_CHAR (reg
[rrr
]);
895 case CCL_WriteConstJump
: /* A--D--D--R--E--S--S-000XXXXX */
896 i
= XINT (ccl_prog
[ic
]);
901 case CCL_WriteConstReadJump
: /* A--D--D--R--E--S--S-rrrXXXXX */
902 i
= XINT (ccl_prog
[ic
]);
905 CCL_READ_CHAR (reg
[rrr
]);
909 case CCL_WriteStringJump
: /* A--D--D--R--E--S--S-000XXXXX */
910 j
= XINT (ccl_prog
[ic
]);
912 CCL_WRITE_STRING (j
);
916 case CCL_WriteArrayReadJump
: /* A--D--D--R--E--S--S-rrrXXXXX */
918 j
= XINT (ccl_prog
[ic
]);
919 if ((unsigned int) i
< j
)
921 i
= XINT (ccl_prog
[ic
+ 1 + i
]);
925 CCL_READ_CHAR (reg
[rrr
]);
926 ic
+= ADDR
- (j
+ 2);
929 case CCL_ReadJump
: /* A--D--D--R--E--S--S-rrrYYYYY */
930 CCL_READ_CHAR (reg
[rrr
]);
934 case CCL_ReadBranch
: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
935 CCL_READ_CHAR (reg
[rrr
]);
936 /* fall through ... */
937 case CCL_Branch
: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
938 if ((unsigned int) reg
[rrr
] < field1
)
939 ic
+= XINT (ccl_prog
[ic
+ reg
[rrr
]]);
941 ic
+= XINT (ccl_prog
[ic
+ field1
]);
944 case CCL_ReadRegister
: /* CCCCCCCCCCCCCCCCCCCCrrXXXXX */
947 CCL_READ_CHAR (reg
[rrr
]);
949 code
= XINT (ccl_prog
[ic
]); ic
++;
951 field2
= (code
& 0xFF) >> 5;
955 case CCL_WriteExprConst
: /* 1:00000OPERATION000RRR000XXXXX */
958 j
= XINT (ccl_prog
[ic
]);
960 jump_address
= ic
+ 1;
963 case CCL_WriteRegister
: /* CCCCCCCCCCCCCCCCCCCrrrXXXXX */
969 code
= XINT (ccl_prog
[ic
]); ic
++;
971 field2
= (code
& 0xFF) >> 5;
975 case CCL_WriteExprRegister
: /* 1:00000OPERATIONRrrRRR000XXXXX */
983 case CCL_Call
: /* 1:CCCCCCCCCCCCCCCCCCCCFFFXXXXX */
988 /* If FFF is nonzero, the CCL program ID is in the
992 prog_id
= XINT (ccl_prog
[ic
]);
1000 || prog_id
>= ASIZE (Vccl_program_table
)
1001 || (slot
= AREF (Vccl_program_table
, prog_id
), !VECTORP (slot
))
1002 || !VECTORP (AREF (slot
, 1)))
1006 ccl_prog
= ccl_prog_stack_struct
[0].ccl_prog
;
1007 ic
= ccl_prog_stack_struct
[0].ic
;
1012 ccl_prog_stack_struct
[stack_idx
].ccl_prog
= ccl_prog
;
1013 ccl_prog_stack_struct
[stack_idx
].ic
= ic
;
1015 ccl_prog
= XVECTOR (AREF (slot
, 1))->contents
;
1016 ic
= CCL_HEADER_MAIN
;
1020 case CCL_WriteConstString
: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
1022 CCL_WRITE_CHAR (field1
);
1025 CCL_WRITE_STRING (field1
);
1026 ic
+= (field1
+ 2) / 3;
1030 case CCL_WriteArray
: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
1032 if ((unsigned int) i
< field1
)
1034 j
= XINT (ccl_prog
[ic
+ i
]);
1040 case CCL_End
: /* 0000000000000000000000XXXXX */
1044 ccl_prog
= ccl_prog_stack_struct
[stack_idx
].ccl_prog
;
1045 ic
= ccl_prog_stack_struct
[stack_idx
].ic
;
1050 /* ccl->ic should points to this command code again to
1051 suppress further processing. */
1055 case CCL_ExprSelfConst
: /* 00000OPERATION000000rrrXXXXX */
1056 i
= XINT (ccl_prog
[ic
]);
1061 case CCL_ExprSelfReg
: /* 00000OPERATION000RRRrrrXXXXX */
1068 case CCL_PLUS
: reg
[rrr
] += i
; break;
1069 case CCL_MINUS
: reg
[rrr
] -= i
; break;
1070 case CCL_MUL
: reg
[rrr
] *= i
; break;
1071 case CCL_DIV
: reg
[rrr
] /= i
; break;
1072 case CCL_MOD
: reg
[rrr
] %= i
; break;
1073 case CCL_AND
: reg
[rrr
] &= i
; break;
1074 case CCL_OR
: reg
[rrr
] |= i
; break;
1075 case CCL_XOR
: reg
[rrr
] ^= i
; break;
1076 case CCL_LSH
: reg
[rrr
] <<= i
; break;
1077 case CCL_RSH
: reg
[rrr
] >>= i
; break;
1078 case CCL_LSH8
: reg
[rrr
] <<= 8; reg
[rrr
] |= i
; break;
1079 case CCL_RSH8
: reg
[7] = reg
[rrr
] & 0xFF; reg
[rrr
] >>= 8; break;
1080 case CCL_DIVMOD
: reg
[7] = reg
[rrr
] % i
; reg
[rrr
] /= i
; break;
1081 case CCL_LS
: reg
[rrr
] = reg
[rrr
] < i
; break;
1082 case CCL_GT
: reg
[rrr
] = reg
[rrr
] > i
; break;
1083 case CCL_EQ
: reg
[rrr
] = reg
[rrr
] == i
; break;
1084 case CCL_LE
: reg
[rrr
] = reg
[rrr
] <= i
; break;
1085 case CCL_GE
: reg
[rrr
] = reg
[rrr
] >= i
; break;
1086 case CCL_NE
: reg
[rrr
] = reg
[rrr
] != i
; break;
1087 default: CCL_INVALID_CMD
;
1091 case CCL_SetExprConst
: /* 00000OPERATION000RRRrrrXXXXX */
1093 j
= XINT (ccl_prog
[ic
]);
1095 jump_address
= ++ic
;
1098 case CCL_SetExprReg
: /* 00000OPERATIONRrrRRRrrrXXXXX */
1105 case CCL_ReadJumpCondExprConst
: /* A--D--D--R--E--S--S-rrrXXXXX */
1106 CCL_READ_CHAR (reg
[rrr
]);
1107 case CCL_JumpCondExprConst
: /* A--D--D--R--E--S--S-rrrXXXXX */
1109 op
= XINT (ccl_prog
[ic
]);
1110 jump_address
= ic
++ + ADDR
;
1111 j
= XINT (ccl_prog
[ic
]);
1116 case CCL_ReadJumpCondExprReg
: /* A--D--D--R--E--S--S-rrrXXXXX */
1117 CCL_READ_CHAR (reg
[rrr
]);
1118 case CCL_JumpCondExprReg
:
1120 op
= XINT (ccl_prog
[ic
]);
1121 jump_address
= ic
++ + ADDR
;
1122 j
= reg
[XINT (ccl_prog
[ic
])];
1129 case CCL_PLUS
: reg
[rrr
] = i
+ j
; break;
1130 case CCL_MINUS
: reg
[rrr
] = i
- j
; break;
1131 case CCL_MUL
: reg
[rrr
] = i
* j
; break;
1132 case CCL_DIV
: reg
[rrr
] = i
/ j
; break;
1133 case CCL_MOD
: reg
[rrr
] = i
% j
; break;
1134 case CCL_AND
: reg
[rrr
] = i
& j
; break;
1135 case CCL_OR
: reg
[rrr
] = i
| j
; break;
1136 case CCL_XOR
: reg
[rrr
] = i
^ j
;; break;
1137 case CCL_LSH
: reg
[rrr
] = i
<< j
; break;
1138 case CCL_RSH
: reg
[rrr
] = i
>> j
; break;
1139 case CCL_LSH8
: reg
[rrr
] = (i
<< 8) | j
; break;
1140 case CCL_RSH8
: reg
[rrr
] = i
>> 8; reg
[7] = i
& 0xFF; break;
1141 case CCL_DIVMOD
: reg
[rrr
] = i
/ j
; reg
[7] = i
% j
; break;
1142 case CCL_LS
: reg
[rrr
] = i
< j
; break;
1143 case CCL_GT
: reg
[rrr
] = i
> j
; break;
1144 case CCL_EQ
: reg
[rrr
] = i
== j
; break;
1145 case CCL_LE
: reg
[rrr
] = i
<= j
; break;
1146 case CCL_GE
: reg
[rrr
] = i
>= j
; break;
1147 case CCL_NE
: reg
[rrr
] = i
!= j
; break;
1148 case CCL_DECODE_SJIS
:
1156 case CCL_ENCODE_SJIS
:
1164 default: CCL_INVALID_CMD
;
1167 if (code
== CCL_WriteExprConst
|| code
== CCL_WriteExprRegister
)
1180 case CCL_ReadMultibyteChar2
:
1184 charset
= CHAR_CHARSET (i
);
1185 reg
[rrr
] = CHARSET_ID (charset
);
1186 reg
[RRR
] = ENCODE_CHAR (charset
, i
);
1189 case CCL_WriteMultibyteChar2
:
1192 charset
= CHARSET_FROM_ID (reg
[RRR
]);
1193 i
= DECODE_CHAR (charset
, reg
[rrr
]);
1197 case CCL_TranslateCharacter
:
1198 charset
= CHARSET_FROM_ID (reg
[RRR
]);
1199 i
= DECODE_CHAR (charset
, reg
[rrr
]);
1200 op
= translate_char (GET_TRANSLATION_TABLE (reg
[Rrr
]), i
);
1201 charset
= CHAR_CHARSET (op
);
1202 reg
[RRR
] = CHARSET_ID (charset
);
1203 reg
[rrr
] = ENCODE_CHAR (charset
, op
);
1206 case CCL_TranslateCharacterConstTbl
:
1207 op
= XINT (ccl_prog
[ic
]); /* table */
1209 charset
= CHARSET_FROM_ID (reg
[RRR
]);
1210 i
= DECODE_CHAR (charset
, reg
[rrr
]);
1211 op
= translate_char (GET_TRANSLATION_TABLE (op
), i
);
1212 charset
= CHAR_CHARSET (op
);
1213 reg
[RRR
] = CHARSET_ID (charset
);
1214 reg
[rrr
] = ENCODE_CHAR (charset
, op
);
1217 case CCL_IterateMultipleMap
:
1219 Lisp_Object map
, content
, attrib
, value
;
1220 int point
, size
, fin_ic
;
1222 j
= XINT (ccl_prog
[ic
++]); /* number of maps. */
1225 if ((j
> reg
[RRR
]) && (j
>= 0))
1240 size
= ASIZE (Vcode_conversion_map_vector
);
1241 point
= XINT (ccl_prog
[ic
++]);
1242 if (point
>= size
) continue;
1243 map
= AREF (Vcode_conversion_map_vector
, point
);
1245 /* Check map varidity. */
1246 if (!CONSP (map
)) continue;
1248 if (!VECTORP (map
)) continue;
1250 if (size
<= 1) continue;
1252 content
= AREF (map
, 0);
1255 [STARTPOINT VAL1 VAL2 ...] or
1256 [t ELELMENT STARTPOINT ENDPOINT] */
1257 if (NUMBERP (content
))
1259 point
= XUINT (content
);
1260 point
= op
- point
+ 1;
1261 if (!((point
>= 1) && (point
< size
))) continue;
1262 content
= AREF (map
, point
);
1264 else if (EQ (content
, Qt
))
1266 if (size
!= 4) continue;
1267 if ((op
>= XUINT (AREF (map
, 2)))
1268 && (op
< XUINT (AREF (map
, 3))))
1269 content
= AREF (map
, 1);
1278 else if (NUMBERP (content
))
1281 reg
[rrr
] = XINT(content
);
1284 else if (EQ (content
, Qt
) || EQ (content
, Qlambda
))
1289 else if (CONSP (content
))
1291 attrib
= XCAR (content
);
1292 value
= XCDR (content
);
1293 if (!NUMBERP (attrib
) || !NUMBERP (value
))
1296 reg
[rrr
] = XUINT (value
);
1299 else if (SYMBOLP (content
))
1300 CCL_CALL_FOR_MAP_INSTRUCTION (content
, fin_ic
);
1310 case CCL_MapMultiple
:
1312 Lisp_Object map
, content
, attrib
, value
;
1313 int point
, size
, map_vector_size
;
1314 int map_set_rest_length
, fin_ic
;
1315 int current_ic
= this_ic
;
1317 /* inhibit recursive call on MapMultiple. */
1318 if (stack_idx_of_map_multiple
> 0)
1320 if (stack_idx_of_map_multiple
<= stack_idx
)
1322 stack_idx_of_map_multiple
= 0;
1323 mapping_stack_pointer
= mapping_stack
;
1328 mapping_stack_pointer
= mapping_stack
;
1329 stack_idx_of_map_multiple
= 0;
1331 map_set_rest_length
=
1332 XINT (ccl_prog
[ic
++]); /* number of maps and separators. */
1333 fin_ic
= ic
+ map_set_rest_length
;
1336 if ((map_set_rest_length
> reg
[RRR
]) && (reg
[RRR
] >= 0))
1340 map_set_rest_length
-= i
;
1346 mapping_stack_pointer
= mapping_stack
;
1350 if (mapping_stack_pointer
<= (mapping_stack
+ 1))
1352 /* Set up initial state. */
1353 mapping_stack_pointer
= mapping_stack
;
1354 PUSH_MAPPING_STACK (0, op
);
1359 /* Recover after calling other ccl program. */
1362 POP_MAPPING_STACK (map_set_rest_length
, orig_op
);
1363 POP_MAPPING_STACK (map_set_rest_length
, reg
[rrr
]);
1367 /* Regard it as Qnil. */
1371 map_set_rest_length
--;
1374 /* Regard it as Qt. */
1378 map_set_rest_length
--;
1381 /* Regard it as Qlambda. */
1383 i
+= map_set_rest_length
;
1384 ic
+= map_set_rest_length
;
1385 map_set_rest_length
= 0;
1388 /* Regard it as normal mapping. */
1389 i
+= map_set_rest_length
;
1390 ic
+= map_set_rest_length
;
1391 POP_MAPPING_STACK (map_set_rest_length
, reg
[rrr
]);
1395 map_vector_size
= ASIZE (Vcode_conversion_map_vector
);
1398 for (;map_set_rest_length
> 0;i
++, ic
++, map_set_rest_length
--)
1400 point
= XINT(ccl_prog
[ic
]);
1403 /* +1 is for including separator. */
1405 if (mapping_stack_pointer
1406 >= &mapping_stack
[MAX_MAP_SET_LEVEL
])
1408 PUSH_MAPPING_STACK (map_set_rest_length
- point
,
1410 map_set_rest_length
= point
;
1415 if (point
>= map_vector_size
) continue;
1416 map
= AREF (Vcode_conversion_map_vector
, point
);
1418 /* Check map varidity. */
1419 if (!CONSP (map
)) continue;
1421 if (!VECTORP (map
)) continue;
1423 if (size
<= 1) continue;
1425 content
= AREF (map
, 0);
1428 [STARTPOINT VAL1 VAL2 ...] or
1429 [t ELEMENT STARTPOINT ENDPOINT] */
1430 if (NUMBERP (content
))
1432 point
= XUINT (content
);
1433 point
= op
- point
+ 1;
1434 if (!((point
>= 1) && (point
< size
))) continue;
1435 content
= AREF (map
, point
);
1437 else if (EQ (content
, Qt
))
1439 if (size
!= 4) continue;
1440 if ((op
>= XUINT (AREF (map
, 2))) &&
1441 (op
< XUINT (AREF (map
, 3))))
1442 content
= AREF (map
, 1);
1453 if (NUMBERP (content
))
1455 op
= XINT (content
);
1456 i
+= map_set_rest_length
- 1;
1457 ic
+= map_set_rest_length
- 1;
1458 POP_MAPPING_STACK (map_set_rest_length
, reg
[rrr
]);
1459 map_set_rest_length
++;
1461 else if (CONSP (content
))
1463 attrib
= XCAR (content
);
1464 value
= XCDR (content
);
1465 if (!NUMBERP (attrib
) || !NUMBERP (value
))
1468 i
+= map_set_rest_length
- 1;
1469 ic
+= map_set_rest_length
- 1;
1470 POP_MAPPING_STACK (map_set_rest_length
, reg
[rrr
]);
1471 map_set_rest_length
++;
1473 else if (EQ (content
, Qt
))
1477 else if (EQ (content
, Qlambda
))
1479 i
+= map_set_rest_length
;
1480 ic
+= map_set_rest_length
;
1483 else if (SYMBOLP (content
))
1485 if (mapping_stack_pointer
1486 >= &mapping_stack
[MAX_MAP_SET_LEVEL
])
1488 PUSH_MAPPING_STACK (map_set_rest_length
, reg
[rrr
]);
1489 PUSH_MAPPING_STACK (map_set_rest_length
, op
);
1490 stack_idx_of_map_multiple
= stack_idx
+ 1;
1491 CCL_CALL_FOR_MAP_INSTRUCTION (content
, current_ic
);
1496 if (mapping_stack_pointer
<= (mapping_stack
+ 1))
1498 POP_MAPPING_STACK (map_set_rest_length
, reg
[rrr
]);
1499 i
+= map_set_rest_length
;
1500 ic
+= map_set_rest_length
;
1501 POP_MAPPING_STACK (map_set_rest_length
, reg
[rrr
]);
1511 Lisp_Object map
, attrib
, value
, content
;
1513 j
= XINT (ccl_prog
[ic
++]); /* map_id */
1515 if (j
>= ASIZE (Vcode_conversion_map_vector
))
1520 map
= AREF (Vcode_conversion_map_vector
, j
);
1533 point
= XUINT (AREF (map
, 0));
1534 point
= op
- point
+ 1;
1537 (!((point
>= 1) && (point
< size
))))
1542 content
= AREF (map
, point
);
1545 else if (NUMBERP (content
))
1546 reg
[rrr
] = XINT (content
);
1547 else if (EQ (content
, Qt
));
1548 else if (CONSP (content
))
1550 attrib
= XCAR (content
);
1551 value
= XCDR (content
);
1552 if (!NUMBERP (attrib
) || !NUMBERP (value
))
1554 reg
[rrr
] = XUINT(value
);
1557 else if (SYMBOLP (content
))
1558 CCL_CALL_FOR_MAP_INSTRUCTION (content
, ic
);
1576 /* The suppress_error member is set when e.g. a CCL-based coding
1577 system is used for terminal output. */
1578 if (!ccl
->suppress_error
&& destination
)
1580 /* We can insert an error message only if DESTINATION is
1581 specified and we still have a room to store the message
1589 switch (ccl
->status
)
1591 case CCL_STAT_INVALID_CMD
:
1592 sprintf(msg
, "\nCCL: Invalid command %x (ccl_code = %x) at %d.",
1593 code
& 0x1F, code
, this_ic
);
1596 int i
= ccl_backtrace_idx
- 1;
1599 msglen
= strlen (msg
);
1600 if (dst
+ msglen
<= (dst_bytes
? dst_end
: src
))
1602 bcopy (msg
, dst
, msglen
);
1606 for (j
= 0; j
< CCL_DEBUG_BACKTRACE_LEN
; j
++, i
--)
1608 if (i
< 0) i
= CCL_DEBUG_BACKTRACE_LEN
- 1;
1609 if (ccl_backtrace_table
[i
] == 0)
1611 sprintf(msg
, " %d", ccl_backtrace_table
[i
]);
1612 msglen
= strlen (msg
);
1613 if (dst
+ msglen
> (dst_bytes
? dst_end
: src
))
1615 bcopy (msg
, dst
, msglen
);
1624 sprintf(msg
, "\nCCL: Quited.");
1628 sprintf(msg
, "\nCCL: Unknown error type (%d).", ccl
->status
);
1631 msglen
= strlen (msg
);
1632 if (dst
+ msglen
<= dst_end
)
1634 for (i
= 0; i
< msglen
; i
++)
1641 ccl
->stack_idx
= stack_idx
;
1642 ccl
->prog
= ccl_prog
;
1643 ccl
->consumed
= src
- source
;
1644 ccl
->produced
= dst
- destination
;
1647 /* Resolve symbols in the specified CCL code (Lisp vector). This
1648 function converts symbols of code conversion maps and character
1649 translation tables embeded in the CCL code into their ID numbers.
1651 The return value is a vector (CCL itself or a new vector in which
1652 all symbols are resolved), Qt if resolving of some symbol failed,
1653 or nil if CCL contains invalid data. */
1656 resolve_symbol_ccl_program (ccl
)
1659 int i
, veclen
, unresolved
= 0;
1660 Lisp_Object result
, contents
, val
;
1663 veclen
= ASIZE (result
);
1665 for (i
= 0; i
< veclen
; i
++)
1667 contents
= AREF (result
, i
);
1668 if (INTEGERP (contents
))
1670 else if (CONSP (contents
)
1671 && SYMBOLP (XCAR (contents
))
1672 && SYMBOLP (XCDR (contents
)))
1674 /* This is the new style for embedding symbols. The form is
1675 (SYMBOL . PROPERTY). (get SYMBOL PROPERTY) should give
1678 if (EQ (result
, ccl
))
1679 result
= Fcopy_sequence (ccl
);
1681 val
= Fget (XCAR (contents
), XCDR (contents
));
1683 AREF (result
, i
) = val
;
1688 else if (SYMBOLP (contents
))
1690 /* This is the old style for embedding symbols. This style
1691 may lead to a bug if, for instance, a translation table
1692 and a code conversion map have the same name. */
1693 if (EQ (result
, ccl
))
1694 result
= Fcopy_sequence (ccl
);
1696 val
= Fget (contents
, Qtranslation_table_id
);
1698 AREF (result
, i
) = val
;
1701 val
= Fget (contents
, Qcode_conversion_map_id
);
1703 AREF (result
, i
) = val
;
1706 val
= Fget (contents
, Qccl_program_idx
);
1708 AREF (result
, i
) = val
;
1718 return (unresolved
? Qt
: result
);
1721 /* Return the compiled code (vector) of CCL program CCL_PROG.
1722 CCL_PROG is a name (symbol) of the program or already compiled
1723 code. If necessary, resolve symbols in the compiled code to index
1724 numbers. If we failed to get the compiled code or to resolve
1725 symbols, return Qnil. */
1728 ccl_get_compiled_code (ccl_prog
)
1729 Lisp_Object ccl_prog
;
1731 Lisp_Object val
, slot
;
1733 if (VECTORP (ccl_prog
))
1735 val
= resolve_symbol_ccl_program (ccl_prog
);
1736 return (VECTORP (val
) ? val
: Qnil
);
1738 if (!SYMBOLP (ccl_prog
))
1741 val
= Fget (ccl_prog
, Qccl_program_idx
);
1743 || XINT (val
) >= ASIZE (Vccl_program_table
))
1745 slot
= AREF (Vccl_program_table
, XINT (val
));
1746 if (! VECTORP (slot
)
1747 || ASIZE (slot
) != 3
1748 || ! VECTORP (AREF (slot
, 1)))
1750 if (NILP (AREF (slot
, 2)))
1752 val
= resolve_symbol_ccl_program (AREF (slot
, 1));
1753 if (! VECTORP (val
))
1755 AREF (slot
, 1) = val
;
1756 AREF (slot
, 2) = Qt
;
1758 return AREF (slot
, 1);
1761 /* Setup fields of the structure pointed by CCL appropriately for the
1762 execution of CCL program CCL_PROG. CCL_PROG is the name (symbol)
1763 of the CCL program or the already compiled code (vector).
1764 Return 0 if we succeed this setup, else return -1.
1766 If CCL_PROG is nil, we just reset the structure pointed by CCL. */
1768 setup_ccl_program (ccl
, ccl_prog
)
1769 struct ccl_program
*ccl
;
1770 Lisp_Object ccl_prog
;
1774 if (! NILP (ccl_prog
))
1776 struct Lisp_Vector
*vp
;
1778 ccl_prog
= ccl_get_compiled_code (ccl_prog
);
1779 if (! VECTORP (ccl_prog
))
1781 vp
= XVECTOR (ccl_prog
);
1782 ccl
->size
= vp
->size
;
1783 ccl
->prog
= vp
->contents
;
1784 ccl
->eof_ic
= XINT (vp
->contents
[CCL_HEADER_EOF
]);
1785 ccl
->buf_magnification
= XINT (vp
->contents
[CCL_HEADER_BUF_MAG
]);
1787 ccl
->ic
= CCL_HEADER_MAIN
;
1788 for (i
= 0; i
< 8; i
++)
1790 ccl
->last_block
= 0;
1791 ccl
->private_state
= 0;
1794 ccl
->suppress_error
= 0;
1800 DEFUN ("ccl-program-p", Fccl_program_p
, Sccl_program_p
, 1, 1, 0,
1801 doc
: /* Return t if OBJECT is a CCL program name or a compiled CCL program code.
1802 See the documentation of `define-ccl-program' for the detail of CCL program. */)
1808 if (VECTORP (object
))
1810 val
= resolve_symbol_ccl_program (object
);
1811 return (VECTORP (val
) ? Qt
: Qnil
);
1813 if (!SYMBOLP (object
))
1816 val
= Fget (object
, Qccl_program_idx
);
1817 return ((! NATNUMP (val
)
1818 || XINT (val
) >= ASIZE (Vccl_program_table
))
1822 DEFUN ("ccl-execute", Fccl_execute
, Sccl_execute
, 2, 2, 0,
1823 doc
: /* Execute CCL-PROGRAM with registers initialized by REGISTERS.
1825 CCL-PROGRAM is a CCL program name (symbol)
1826 or compiled code generated by `ccl-compile' (for backward compatibility.
1827 In the latter case, the execution overhead is bigger than in the former).
1828 No I/O commands should appear in CCL-PROGRAM.
1830 REGISTERS is a vector of [R0 R1 ... R7] where RN is an initial value
1831 for the Nth register.
1833 As side effect, each element of REGISTERS holds the value of
1834 the corresponding register after the execution.
1836 See the documentation of `define-ccl-program' for a definition of CCL
1839 Lisp_Object ccl_prog
, reg
;
1841 struct ccl_program ccl
;
1844 if (setup_ccl_program (&ccl
, ccl_prog
) < 0)
1845 error ("Invalid CCL program");
1848 if (ASIZE (reg
) != 8)
1849 error ("Length of vector REGISTERS is not 8");
1851 for (i
= 0; i
< 8; i
++)
1852 ccl
.reg
[i
] = (INTEGERP (AREF (reg
, i
))
1853 ? XINT (AREF (reg
, i
))
1856 ccl_driver (&ccl
, NULL
, NULL
, 0, 0);
1858 if (ccl
.status
!= CCL_STAT_SUCCESS
)
1859 error ("Error in CCL program at %dth code", ccl
.ic
);
1861 for (i
= 0; i
< 8; i
++)
1862 XSETINT (AREF (reg
, i
), ccl
.reg
[i
]);
1866 DEFUN ("ccl-execute-on-string", Fccl_execute_on_string
, Sccl_execute_on_string
,
1868 doc
: /* Execute CCL-PROGRAM with initial STATUS on STRING.
1870 CCL-PROGRAM is a symbol registered by register-ccl-program,
1871 or a compiled code generated by `ccl-compile' (for backward compatibility,
1872 in this case, the execution is slower).
1874 Read buffer is set to STRING, and write buffer is allocated automatically.
1876 STATUS is a vector of [R0 R1 ... R7 IC], where
1877 R0..R7 are initial values of corresponding registers,
1878 IC is the instruction counter specifying from where to start the program.
1879 If R0..R7 are nil, they are initialized to 0.
1880 If IC is nil, it is initialized to head of the CCL program.
1882 If optional 4th arg CONTINUE is non-nil, keep IC on read operation
1883 when read buffer is exausted, else, IC is always set to the end of
1884 CCL-PROGRAM on exit.
1886 It returns the contents of write buffer as a string,
1887 and as side effect, STATUS is updated.
1888 If the optional 5th arg UNIBYTE-P is non-nil, the returned string
1889 is a unibyte string. By default it is a multibyte string.
1891 See the documentation of `define-ccl-program' for the detail of CCL program. */)
1892 (ccl_prog
, status
, str
, contin
, unibyte_p
)
1893 Lisp_Object ccl_prog
, status
, str
, contin
, unibyte_p
;
1896 struct ccl_program ccl
;
1899 unsigned char *outbuf
, *outp
;
1900 int str_chars
, str_bytes
;
1901 #define CCL_EXECUTE_BUF_SIZE 1024
1902 int source
[CCL_EXECUTE_BUF_SIZE
], destination
[CCL_EXECUTE_BUF_SIZE
];
1903 int consumed_chars
, consumed_bytes
, produced_chars
;
1905 if (setup_ccl_program (&ccl
, ccl_prog
) < 0)
1906 error ("Invalid CCL program");
1908 CHECK_VECTOR (status
);
1909 if (ASIZE (status
) != 9)
1910 error ("Length of vector STATUS is not 9");
1912 str_chars
= XSTRING (str
)->size
;
1913 str_bytes
= STRING_BYTES (XSTRING (str
));
1915 for (i
= 0; i
< 8; i
++)
1917 if (NILP (AREF (status
, i
)))
1918 XSETINT (AREF (status
, i
), 0);
1919 if (INTEGERP (AREF (status
, i
)))
1920 ccl
.reg
[i
] = XINT (AREF (status
, i
));
1922 if (INTEGERP (AREF (status
, i
)))
1924 i
= XFASTINT (AREF (status
, 8));
1925 if (ccl
.ic
< i
&& i
< ccl
.size
)
1929 outbufsize
= (ccl
.buf_magnification
1930 ? str_bytes
* ccl
.buf_magnification
+ 256
1932 outp
= outbuf
= (unsigned char *) xmalloc (outbufsize
);
1934 consumed_chars
= consumed_bytes
= 0;
1936 while (consumed_bytes
< str_bytes
)
1938 unsigned char *p
= XSTRING (str
)->data
+ consumed_bytes
;
1939 unsigned char *endp
= XSTRING (str
)->data
+ str_bytes
;
1943 if (endp
- p
== str_chars
- consumed_chars
)
1944 while (i
< CCL_EXECUTE_BUF_SIZE
&& p
< endp
)
1947 while (i
< CCL_EXECUTE_BUF_SIZE
&& p
< endp
)
1948 source
[i
++] = STRING_CHAR_ADVANCE (p
);
1949 consumed_chars
+= i
;
1950 consumed_bytes
= p
- XSTRING (str
)->data
;
1952 if (consumed_bytes
== str_bytes
)
1953 ccl
.last_block
= NILP (contin
);
1958 ccl_driver (&ccl
, src
, destination
, src_size
, CCL_EXECUTE_BUF_SIZE
);
1959 if (ccl
.status
!= CCL_STAT_SUSPEND_BY_DST
)
1961 produced_chars
+= ccl
.produced
;
1962 if (NILP (unibyte_p
))
1964 if (outp
- outbuf
+ MAX_MULTIBYTE_LENGTH
* ccl
.produced
1967 int offset
= outp
- outbuf
;
1968 outbufsize
+= MAX_MULTIBYTE_LENGTH
* ccl
.produced
;
1969 outbuf
= (unsigned char *) xrealloc (outbuf
, outbufsize
);
1970 outp
= outbuf
+ offset
;
1972 for (i
= 0; i
< ccl
.produced
; i
++)
1973 CHAR_STRING_ADVANCE (destination
[i
], outp
);
1977 if (outp
- outbuf
+ ccl
.produced
> outbufsize
)
1979 int offset
= outp
- outbuf
;
1980 outbufsize
+= ccl
.produced
;
1981 outbuf
= (unsigned char *) xrealloc (outbuf
, outbufsize
);
1982 outp
= outbuf
+ offset
;
1984 for (i
= 0; i
< ccl
.produced
; i
++)
1985 *outp
++ = destination
[i
];
1987 src
+= ccl
.consumed
;
1988 src_size
-= ccl
.consumed
;
1991 if (ccl
.status
!= CCL_STAT_SUSPEND_BY_SRC
)
1995 if (ccl
.status
!= CCL_STAT_SUCCESS
1996 && ccl
.status
!= CCL_STAT_SUSPEND_BY_SRC
)
1997 error ("Error in CCL program at %dth code", ccl
.ic
);
1999 for (i
= 0; i
< 8; i
++)
2000 XSET (XVECTOR (status
)->contents
[i
], Lisp_Int
, ccl
.reg
[i
]);
2001 XSETINT (XVECTOR (status
)->contents
[8], ccl
.ic
);
2003 if (NILP (unibyte_p
))
2004 val
= make_multibyte_string ((char *) outbuf
, produced_chars
,
2007 val
= make_unibyte_string ((char *) outbuf
, produced_chars
);
2013 DEFUN ("register-ccl-program", Fregister_ccl_program
, Sregister_ccl_program
,
2015 doc
: /* Register CCL program CCL_PROG as NAME in `ccl-program-table'.
2016 CCL_PROG should be a compiled CCL program (vector), or nil.
2017 If it is nil, just reserve NAME as a CCL program name.
2018 Return index number of the registered CCL program. */)
2020 Lisp_Object name
, ccl_prog
;
2022 int len
= ASIZE (Vccl_program_table
);
2024 Lisp_Object resolved
;
2026 CHECK_SYMBOL (name
);
2028 if (!NILP (ccl_prog
))
2030 CHECK_VECTOR (ccl_prog
);
2031 resolved
= resolve_symbol_ccl_program (ccl_prog
);
2032 if (NILP (resolved
))
2033 error ("Error in CCL program");
2034 if (VECTORP (resolved
))
2036 ccl_prog
= resolved
;
2043 for (idx
= 0; idx
< len
; idx
++)
2047 slot
= AREF (Vccl_program_table
, idx
);
2048 if (!VECTORP (slot
))
2049 /* This is the first unsed slot. Register NAME here. */
2052 if (EQ (name
, AREF (slot
, 0)))
2054 /* Update this slot. */
2055 AREF (slot
, 1) = ccl_prog
;
2056 AREF (slot
, 2) = resolved
;
2057 return make_number (idx
);
2063 /* Extend the table. */
2064 Lisp_Object new_table
;
2067 new_table
= Fmake_vector (make_number (len
* 2), Qnil
);
2068 for (j
= 0; j
< len
; j
++)
2070 = AREF (Vccl_program_table
, j
);
2071 Vccl_program_table
= new_table
;
2077 elt
= Fmake_vector (make_number (3), Qnil
);
2078 AREF (elt
, 0) = name
;
2079 AREF (elt
, 1) = ccl_prog
;
2080 AREF (elt
, 2) = resolved
;
2081 AREF (Vccl_program_table
, idx
) = elt
;
2084 Fput (name
, Qccl_program_idx
, make_number (idx
));
2085 return make_number (idx
);
2088 /* Register code conversion map.
2089 A code conversion map consists of numbers, Qt, Qnil, and Qlambda.
2090 The first element is the start code point.
2091 The other elements are mapped numbers.
2092 Symbol t means to map to an original number before mapping.
2093 Symbol nil means that the corresponding element is empty.
2094 Symbol lambda means to terminate mapping here.
2097 DEFUN ("register-code-conversion-map", Fregister_code_conversion_map
,
2098 Sregister_code_conversion_map
,
2100 doc
: /* Register SYMBOL as code conversion map MAP.
2101 Return index number of the registered map. */)
2103 Lisp_Object symbol
, map
;
2105 int len
= ASIZE (Vcode_conversion_map_vector
);
2109 CHECK_SYMBOL (symbol
);
2112 for (i
= 0; i
< len
; i
++)
2114 Lisp_Object slot
= AREF (Vcode_conversion_map_vector
, i
);
2119 if (EQ (symbol
, XCAR (slot
)))
2121 index
= make_number (i
);
2122 XSETCDR (slot
, map
);
2123 Fput (symbol
, Qcode_conversion_map
, map
);
2124 Fput (symbol
, Qcode_conversion_map_id
, index
);
2131 Lisp_Object new_vector
= Fmake_vector (make_number (len
* 2), Qnil
);
2134 for (j
= 0; j
< len
; j
++)
2135 AREF (new_vector
, j
)
2136 = AREF (Vcode_conversion_map_vector
, j
);
2137 Vcode_conversion_map_vector
= new_vector
;
2140 index
= make_number (i
);
2141 Fput (symbol
, Qcode_conversion_map
, map
);
2142 Fput (symbol
, Qcode_conversion_map_id
, index
);
2143 AREF (Vcode_conversion_map_vector
, i
) = Fcons (symbol
, map
);
2151 staticpro (&Vccl_program_table
);
2152 Vccl_program_table
= Fmake_vector (make_number (32), Qnil
);
2154 Qccl
= intern ("ccl");
2157 Qcclp
= intern ("cclp");
2160 Qccl_program
= intern ("ccl-program");
2161 staticpro (&Qccl_program
);
2163 Qccl_program_idx
= intern ("ccl-program-idx");
2164 staticpro (&Qccl_program_idx
);
2166 Qcode_conversion_map
= intern ("code-conversion-map");
2167 staticpro (&Qcode_conversion_map
);
2169 Qcode_conversion_map_id
= intern ("code-conversion-map-id");
2170 staticpro (&Qcode_conversion_map_id
);
2172 DEFVAR_LISP ("code-conversion-map-vector", &Vcode_conversion_map_vector
,
2173 doc
: /* Vector of code conversion maps. */);
2174 Vcode_conversion_map_vector
= Fmake_vector (make_number (16), Qnil
);
2176 DEFVAR_LISP ("font-ccl-encoder-alist", &Vfont_ccl_encoder_alist
,
2177 doc
: /* Alist of fontname patterns vs corresponding CCL program.
2178 Each element looks like (REGEXP . CCL-CODE),
2179 where CCL-CODE is a compiled CCL program.
2180 When a font whose name matches REGEXP is used for displaying a character,
2181 CCL-CODE is executed to calculate the code point in the font
2182 from the charset number and position code(s) of the character which are set
2183 in CCL registers R0, R1, and R2 before the execution.
2184 The code point in the font is set in CCL registers R1 and R2
2185 when the execution terminated.
2186 If the font is single-byte font, the register R2 is not used. */);
2187 Vfont_ccl_encoder_alist
= Qnil
;
2189 defsubr (&Sccl_program_p
);
2190 defsubr (&Sccl_execute
);
2191 defsubr (&Sccl_execute_on_string
);
2192 defsubr (&Sregister_ccl_program
);
2193 defsubr (&Sregister_code_conversion_map
);