1 /* CCL (Code Conversion Language) interpreter.
2 Copyright (C) 1995, 1997, 1998, 1999 Electrotechnical Laboratory, JAPAN.
3 Licensed to the Free Software Foundation.
5 This file is part of XEmacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* Synched up with : FSF Emacs 20.3.10 without ExCCL
23 * (including {Read|Write}MultibyteChar) */
37 #include "mule-charset.h"
39 #include "file-coding.h"
46 #endif /* not emacs */
48 /* This contains all code conversion map available to CCL. */
50 Lisp_Object Vcode_conversion_map_vector;
53 /* Alist of fontname patterns vs corresponding CCL program. */
54 Lisp_Object Vfont_ccl_encoder_alist;
56 /* This symbol is a property which assocates with ccl program vector.
57 Ex: (get 'ccl-big5-encoder 'ccl-program) returns ccl program vector. */
58 Lisp_Object Qccl_program;
60 /* These symbols are properties which associate with code conversion
61 map and their ID respectively. */
63 Lisp_Object Qcode_conversion_map;
64 Lisp_Object Qcode_conversion_map_id;
67 /* Symbols of ccl program have this property, a value of the property
68 is an index for Vccl_protram_table. */
69 Lisp_Object Qccl_program_idx;
71 /* Vector of CCL program names vs corresponding program data. */
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:CCCCCCCCCCCCCCCCCCCC000XXXXX
305 ------------------------------
309 #define CCL_WriteConstString 0x14 /* Write a constant or a string:
310 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
311 [2:0000STRIN[0]STRIN[1]STRIN[2]]
313 -----------------------------
317 write_string (STRING, CC..C);
318 IC += (CC..C + 2) / 3;
321 #define CCL_WriteArray 0x15 /* Write an element of array:
322 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
326 ------------------------------
327 if (0 <= reg[rrr] < CC..C)
328 write (ELEMENT[reg[rrr]]);
332 #define CCL_End 0x16 /* Terminate:
333 1:00000000000000000000000XXXXX
334 ------------------------------
338 /* The following two codes execute an assignment arithmetic/logical
339 operation. The form of the operation is like REG OP= OPERAND. */
341 #define CCL_ExprSelfConst 0x17 /* REG OP= constant:
342 1:00000OPERATION000000rrrXXXXX
344 ------------------------------
345 reg[rrr] OPERATION= CONSTANT;
348 #define CCL_ExprSelfReg 0x18 /* REG1 OP= REG2:
349 1:00000OPERATION000RRRrrrXXXXX
350 ------------------------------
351 reg[rrr] OPERATION= reg[RRR];
354 /* The following codes execute an arithmetic/logical operation. The
355 form of the operation is like REG_X = REG_Y OP OPERAND2. */
357 #define CCL_SetExprConst 0x19 /* REG_X = REG_Y OP constant:
358 1:00000OPERATION000RRRrrrXXXXX
360 ------------------------------
361 reg[rrr] = reg[RRR] OPERATION CONSTANT;
365 #define CCL_SetExprReg 0x1A /* REG1 = REG2 OP REG3:
366 1:00000OPERATIONRrrRRRrrrXXXXX
367 ------------------------------
368 reg[rrr] = reg[RRR] OPERATION reg[Rrr];
371 #define CCL_JumpCondExprConst 0x1B /* Jump conditional according to
372 an operation on constant:
373 1:A--D--D--R--E--S--S-rrrXXXXX
376 -----------------------------
377 reg[7] = reg[rrr] OPERATION CONSTANT;
384 #define CCL_JumpCondExprReg 0x1C /* Jump conditional according to
385 an operation on register:
386 1:A--D--D--R--E--S--S-rrrXXXXX
389 -----------------------------
390 reg[7] = reg[rrr] OPERATION reg[RRR];
397 #define CCL_ReadJumpCondExprConst 0x1D /* Read and jump conditional according
398 to an operation on constant:
399 1:A--D--D--R--E--S--S-rrrXXXXX
402 -----------------------------
404 reg[7] = reg[rrr] OPERATION CONSTANT;
411 #define CCL_ReadJumpCondExprReg 0x1E /* Read and jump conditional according
412 to an operation on register:
413 1:A--D--D--R--E--S--S-rrrXXXXX
416 -----------------------------
418 reg[7] = reg[rrr] OPERATION reg[RRR];
425 #define CCL_Extension 0x1F /* Extended CCL code
426 1:ExtendedCOMMNDRrrRRRrrrXXXXX
429 ------------------------------
430 extended_command (rrr,RRR,Rrr,ARGS)
434 Here after, Extended CCL Instructions.
435 Bit length of extended command is 14.
436 Therefore, the instruction code range is 0..16384(0x3fff).
439 /* Read a multibyte characeter.
440 A code point is stored into reg[rrr]. A charset ID is stored into
443 #define CCL_ReadMultibyteChar2 0x00 /* Read Multibyte Character
444 1:ExtendedCOMMNDRrrRRRrrrXXXXX */
446 /* Write a multibyte character.
447 Write a character whose code point is reg[rrr] and the charset ID
450 #define CCL_WriteMultibyteChar2 0x01 /* Write Multibyte Character
451 1:ExtendedCOMMNDRrrRRRrrrXXXXX */
454 /* Translate a character whose code point is reg[rrr] and the charset
455 ID is reg[RRR] by a translation table whose ID is reg[Rrr].
457 A translated character is set in reg[rrr] (code point) and reg[RRR]
460 #define CCL_TranslateCharacter 0x02 /* Translate a multibyte character
461 1:ExtendedCOMMNDRrrRRRrrrXXXXX */
463 /* Translate a character whose code point is reg[rrr] and the charset
464 ID is reg[RRR] by a translation table whose ID is ARGUMENT.
466 A translated character is set in reg[rrr] (code point) and reg[RRR]
469 #define CCL_TranslateCharacterConstTbl 0x03 /* Translate a multibyte character
470 1:ExtendedCOMMNDRrrRRRrrrXXXXX
471 2:ARGUMENT(Translation Table ID)
474 /* Iterate looking up MAPs for reg[rrr] starting from the Nth (N =
475 reg[RRR]) MAP until some value is found.
477 Each MAP is a Lisp vector whose element is number, nil, t, or
479 If the element is nil, ignore the map and proceed to the next map.
480 If the element is t or lambda, finish without changing reg[rrr].
481 If the element is a number, set reg[rrr] to the number and finish.
483 Detail of the map structure is descibed in the comment for
484 CCL_MapMultiple below. */
486 #define CCL_IterateMultipleMap 0x10 /* Iterate multiple maps
487 1:ExtendedCOMMNDXXXRRRrrrXXXXX
494 /* Map the code in reg[rrr] by MAPs starting from the Nth (N =
497 MAPs are supplied in the succeeding CCL codes as follows:
499 When CCL program gives this nested structure of map to this command:
502 (MAP-ID121 MAP-ID122 MAP-ID123)
505 (MAP-ID211 (MAP-ID2111) MAP-ID212)
507 the compiled CCL codes has this sequence:
508 CCL_MapMultiple (CCL code of this command)
509 16 (total number of MAPs and SEPARATORs)
527 A value of each SEPARATOR follows this rule:
528 MAP-SET := SEPARATOR [(MAP-ID | MAP-SET)]+
529 SEPARATOR := -(number of MAP-IDs and SEPARATORs in the MAP-SET)
531 (*)....Nest level of MAP-SET must not be over than MAX_MAP_SET_LEVEL.
533 When some map fails to map (i.e. it doesn't have a value for
534 reg[rrr]), the mapping is treated as identity.
536 The mapping is iterated for all maps in each map set (set of maps
537 separated by SEPARATOR) except in the case that lambda is
538 encountered. More precisely, the mapping proceeds as below:
540 At first, VAL0 is set to reg[rrr], and it is translated by the
541 first map to VAL1. Then, VAL1 is translated by the next map to
542 VAL2. This mapping is iterated until the last map is used. The
543 result of the mapping is the last value of VAL?.
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, the whole mapping process terminates, and VALm
555 is the result of this mapping.
557 Each map is a Lisp vector of the following format (a) or (b):
558 (a)......[STARTPOINT VAL1 VAL2 ...]
559 (b)......[t VAL STARTPOINT ENDPOINT],
561 STARTPOINT is an offset to be used for indexing a map,
562 ENDPOINT is a maximum index number of a map,
563 VAL and VALn is a number, nil, t, or lambda.
565 Valid index range of a map of type (a) is:
566 STARTPOINT <= index < STARTPOINT + map_size - 1
567 Valid index range of a map of type (b) is:
568 STARTPOINT <= index < ENDPOINT */
570 #define CCL_MapMultiple 0x11 /* Mapping by multiple code conversion maps
571 1:ExtendedCOMMNDXXXRRRrrrXXXXX
583 #define MAX_MAP_SET_LEVEL 20
591 static tr_stack mapping_stack[MAX_MAP_SET_LEVEL];
592 static tr_stack *mapping_stack_pointer;
595 #define PUSH_MAPPING_STACK(restlen, orig) \
597 mapping_stack_pointer->rest_length = (restlen); \
598 mapping_stack_pointer->orig_val = (orig); \
599 mapping_stack_pointer++; \
602 #define POP_MAPPING_STACK(restlen, orig) \
604 mapping_stack_pointer--; \
605 (restlen) = mapping_stack_pointer->rest_length; \
606 (orig) = mapping_stack_pointer->orig_val; \
609 #define CCL_MapSingle 0x12 /* Map by single code conversion map
610 1:ExtendedCOMMNDXXXRRRrrrXXXXX
612 ------------------------------
613 Map reg[rrr] by MAP-ID.
614 If some valid mapping is found,
615 set reg[rrr] to the result,
620 /* CCL arithmetic/logical operators. */
621 #define CCL_PLUS 0x00 /* X = Y + Z */
622 #define CCL_MINUS 0x01 /* X = Y - Z */
623 #define CCL_MUL 0x02 /* X = Y * Z */
624 #define CCL_DIV 0x03 /* X = Y / Z */
625 #define CCL_MOD 0x04 /* X = Y % Z */
626 #define CCL_AND 0x05 /* X = Y & Z */
627 #define CCL_OR 0x06 /* X = Y | Z */
628 #define CCL_XOR 0x07 /* X = Y ^ Z */
629 #define CCL_LSH 0x08 /* X = Y << Z */
630 #define CCL_RSH 0x09 /* X = Y >> Z */
631 #define CCL_LSH8 0x0A /* X = (Y << 8) | Z */
632 #define CCL_RSH8 0x0B /* X = Y >> 8, r[7] = Y & 0xFF */
633 #define CCL_DIVMOD 0x0C /* X = Y / Z, r[7] = Y % Z */
634 #define CCL_LS 0x10 /* X = (X < Y) */
635 #define CCL_GT 0x11 /* X = (X > Y) */
636 #define CCL_EQ 0x12 /* X = (X == Y) */
637 #define CCL_LE 0x13 /* X = (X <= Y) */
638 #define CCL_GE 0x14 /* X = (X >= Y) */
639 #define CCL_NE 0x15 /* X = (X != Y) */
641 #define CCL_DECODE_SJIS 0x16 /* X = HIGHER_BYTE (DE-SJIS (Y, Z))
642 r[7] = LOWER_BYTE (DE-SJIS (Y, Z)) */
643 #define CCL_ENCODE_SJIS 0x17 /* X = HIGHER_BYTE (SJIS (Y, Z))
644 r[7] = LOWER_BYTE (SJIS (Y, Z) */
646 /* Suspend CCL program because of reading from empty input buffer or
647 writing to full output buffer. When this program is resumed, the
648 same I/O command is executed. The `if (1)' is for warning suppression. */
649 #define CCL_SUSPEND(stat) \
652 ccl->status = stat; \
653 if (1) goto ccl_finish; \
656 /* Terminate CCL program because of invalid command. Should not occur
657 in the normal case. The `if (1)' is for warning suppression. */
658 #define CCL_INVALID_CMD \
660 ccl->status = CCL_STAT_INVALID_CMD; \
661 if (1) goto ccl_error_handler; \
664 /* Encode one character CH to multibyte form and write to the current
665 output buffer. If CH is less than 256, CH is written as is. */
666 #define CCL_WRITE_CHAR(ch) do { \
669 ccl->status = CCL_STAT_INVALID_CMD; \
670 goto ccl_error_handler; \
674 Bufbyte work[MAX_EMCHAR_LEN]; \
675 int len = ( ch < ( conversion_mode == CCL_MODE_ENCODING ? \
677 simple_set_charptr_emchar (work, ch) : \
678 non_ascii_set_charptr_emchar (work, ch); \
679 Dynarr_add_many (destination, work, len); \
683 /* Write a string at ccl_prog[IC] of length LEN to the current output
685 #define CCL_WRITE_STRING(len) do { \
688 ccl->status = CCL_STAT_INVALID_CMD; \
689 goto ccl_error_handler; \
693 Bufbyte work[MAX_EMCHAR_LEN]; \
694 for (i = 0; i < len; i++) \
696 int ch = (XINT (ccl_prog[ic + (i / 3)]) \
697 >> ((2 - (i % 3)) * 8)) & 0xFF; \
699 ( ch < ( conversion_mode == CCL_MODE_ENCODING ? \
701 simple_set_charptr_emchar (work, ch) : \
702 non_ascii_set_charptr_emchar (work, ch); \
703 Dynarr_add_many (destination, work, bytes); \
708 /* Read one byte from the current input buffer into Rth register. */
709 #define CCL_READ_CHAR(r) do { \
710 if (!src && !ccl->last_block) \
712 ccl->status = CCL_STAT_INVALID_CMD; \
713 goto ccl_error_handler; \
715 else if (src < src_end) \
717 else if (ccl->last_block) \
723 /* Suspend CCL program because of \
724 reading from empty input buffer or \
725 writing to full output buffer. \
726 When this program is resumed, the \
727 same I/O command is executed. */ \
730 ccl->status = CCL_STAT_SUSPEND_BY_SRC; \
736 /* Execute CCL code on SRC_BYTES length text at SOURCE. The resulting
737 text goes to a place pointed by DESTINATION. The bytes actually
738 processed is returned as *CONSUMED. The return value is the length
739 of the resulting text. As a side effect, the contents of CCL registers
740 are updated. If SOURCE or DESTINATION is NULL, only operations on
741 registers are permitted. */
744 #define CCL_DEBUG_BACKTRACE_LEN 256
745 int ccl_backtrace_table[CCL_BACKTRACE_TABLE];
746 int ccl_backtrace_idx;
749 struct ccl_prog_stack
751 Lisp_Object *ccl_prog; /* Pointer to an array of CCL code. */
752 int ic; /* Instruction Counter. */
755 /* For the moment, we only support depth 256 of stack. */
756 static struct ccl_prog_stack ccl_prog_stack_struct[256];
759 ccl_driver (struct ccl_program *ccl, const unsigned char *source,
760 unsigned_char_dynarr *destination, int src_bytes,
761 int *consumed, int conversion_mode)
765 int code = -1; /* init to illegal value, */
767 Lisp_Object *ccl_prog = ccl->prog;
768 const unsigned char *src = source, *src_end = src + src_bytes;
769 int jump_address = 0; /* shut up the compiler */
771 int stack_idx = ccl->stack_idx;
772 /* Instruction counter of the current CCL code. */
775 if (ic >= ccl->eof_ic)
776 ic = CCL_HEADER_MAIN;
778 #if 0 /* not for XEmacs ? */
779 if (ccl->buf_magnification ==0) /* We can't produce any bytes. */
784 ccl_backtrace_idx = 0;
791 ccl_backtrace_table[ccl_backtrace_idx++] = ic;
792 if (ccl_backtrace_idx >= CCL_DEBUG_BACKTRACE_LEN)
793 ccl_backtrace_idx = 0;
794 ccl_backtrace_table[ccl_backtrace_idx] = 0;
797 if (!NILP (Vquit_flag) && NILP (Vinhibit_quit))
799 /* We can't just signal Qquit, instead break the loop as if
800 the whole data is processed. Don't reset Vquit_flag, it
801 must be handled later at a safer place. */
803 src = source + src_bytes;
804 ccl->status = CCL_STAT_QUIT;
809 code = XINT (ccl_prog[ic]); ic++;
811 field2 = (code & 0xFF) >> 5;
814 #define RRR (field1 & 7)
815 #define Rrr ((field1 >> 3) & 7)
817 #define EXCMD (field1 >> 6)
821 case CCL_SetRegister: /* 00000000000000000RRRrrrXXXXX */
825 case CCL_SetShortConst: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
829 case CCL_SetConst: /* 00000000000000000000rrrXXXXX */
830 reg[rrr] = XINT (ccl_prog[ic]);
834 case CCL_SetArray: /* CCCCCCCCCCCCCCCCCCCCRRRrrrXXXXX */
837 if ((unsigned int) i < j)
838 reg[rrr] = XINT (ccl_prog[ic + i]);
842 case CCL_Jump: /* A--D--D--R--E--S--S-000XXXXX */
846 case CCL_JumpCond: /* A--D--D--R--E--S--S-rrrXXXXX */
851 case CCL_WriteRegisterJump: /* A--D--D--R--E--S--S-rrrXXXXX */
857 case CCL_WriteRegisterReadJump: /* A--D--D--R--E--S--S-rrrXXXXX */
861 CCL_READ_CHAR (reg[rrr]);
865 case CCL_WriteConstJump: /* A--D--D--R--E--S--S-000XXXXX */
866 i = XINT (ccl_prog[ic]);
871 case CCL_WriteConstReadJump: /* A--D--D--R--E--S--S-rrrXXXXX */
872 i = XINT (ccl_prog[ic]);
875 CCL_READ_CHAR (reg[rrr]);
879 case CCL_WriteStringJump: /* A--D--D--R--E--S--S-000XXXXX */
880 j = XINT (ccl_prog[ic]);
882 CCL_WRITE_STRING (j);
886 case CCL_WriteArrayReadJump: /* A--D--D--R--E--S--S-rrrXXXXX */
888 j = XINT (ccl_prog[ic]);
889 if ((unsigned int) i < j)
891 i = XINT (ccl_prog[ic + 1 + i]);
895 CCL_READ_CHAR (reg[rrr]);
896 ic += ADDR - (j + 2);
899 case CCL_ReadJump: /* A--D--D--R--E--S--S-rrrYYYYY */
900 CCL_READ_CHAR (reg[rrr]);
904 case CCL_ReadBranch: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
905 CCL_READ_CHAR (reg[rrr]);
906 /* fall through ... */
907 case CCL_Branch: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
908 if ((unsigned int) reg[rrr] < field1)
909 ic += XINT (ccl_prog[ic + reg[rrr]]);
911 ic += XINT (ccl_prog[ic + field1]);
914 case CCL_ReadRegister: /* CCCCCCCCCCCCCCCCCCCCrrXXXXX */
917 CCL_READ_CHAR (reg[rrr]);
919 code = XINT (ccl_prog[ic]); ic++;
921 field2 = (code & 0xFF) >> 5;
925 case CCL_WriteExprConst: /* 1:00000OPERATION000RRR000XXXXX */
928 j = XINT (ccl_prog[ic]);
933 case CCL_WriteRegister: /* CCCCCCCCCCCCCCCCCCCrrrXXXXX */
939 code = XINT (ccl_prog[ic]); ic++;
941 field2 = (code & 0xFF) >> 5;
945 case CCL_WriteExprRegister: /* 1:00000OPERATIONRrrRRR000XXXXX */
952 case CCL_Call: /* CCCCCCCCCCCCCCCCCCCC000XXXXX */
958 || field1 >= XVECTOR_LENGTH (Vccl_program_table)
959 || (slot = XVECTOR_DATA (Vccl_program_table)[field1],
961 || !VECTORP (XCDR (slot)))
965 ccl_prog = ccl_prog_stack_struct[0].ccl_prog;
966 ic = ccl_prog_stack_struct[0].ic;
968 ccl->status = CCL_STAT_INVALID_CMD;
969 goto ccl_error_handler;
972 ccl_prog_stack_struct[stack_idx].ccl_prog = ccl_prog;
973 ccl_prog_stack_struct[stack_idx].ic = ic;
975 ccl_prog = XVECTOR_DATA (XCDR (slot));
976 ic = CCL_HEADER_MAIN;
980 case CCL_WriteConstString: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
982 CCL_WRITE_CHAR (field1);
985 CCL_WRITE_STRING (field1);
986 ic += (field1 + 2) / 3;
990 case CCL_WriteArray: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
992 if ((unsigned int) i < field1)
994 j = XINT (ccl_prog[ic + i]);
1000 case CCL_End: /* 0000000000000000000000XXXXX */
1001 if (stack_idx-- > 0)
1003 ccl_prog = ccl_prog_stack_struct[stack_idx].ccl_prog;
1004 ic = ccl_prog_stack_struct[stack_idx].ic;
1009 /* ccl->ic should points to this command code again to
1010 suppress further processing. */
1012 /* Terminate CCL program successfully. */
1013 ccl->status = CCL_STAT_SUCCESS;
1016 case CCL_ExprSelfConst: /* 00000OPERATION000000rrrXXXXX */
1017 i = XINT (ccl_prog[ic]);
1022 case CCL_ExprSelfReg: /* 00000OPERATION000RRRrrrXXXXX */
1029 case CCL_PLUS: reg[rrr] += i; break;
1030 case CCL_MINUS: reg[rrr] -= i; break;
1031 case CCL_MUL: reg[rrr] *= i; break;
1032 case CCL_DIV: reg[rrr] /= i; break;
1033 case CCL_MOD: reg[rrr] %= i; break;
1034 case CCL_AND: reg[rrr] &= i; break;
1035 case CCL_OR: reg[rrr] |= i; break;
1036 case CCL_XOR: reg[rrr] ^= i; break;
1037 case CCL_LSH: reg[rrr] <<= i; break;
1038 case CCL_RSH: reg[rrr] >>= i; break;
1039 case CCL_LSH8: reg[rrr] <<= 8; reg[rrr] |= i; break;
1040 case CCL_RSH8: reg[7] = reg[rrr] & 0xFF; reg[rrr] >>= 8; break;
1041 case CCL_DIVMOD: reg[7] = reg[rrr] % i; reg[rrr] /= i; break;
1042 case CCL_LS: reg[rrr] = reg[rrr] < i; break;
1043 case CCL_GT: reg[rrr] = reg[rrr] > i; break;
1044 case CCL_EQ: reg[rrr] = reg[rrr] == i; break;
1045 case CCL_LE: reg[rrr] = reg[rrr] <= i; break;
1046 case CCL_GE: reg[rrr] = reg[rrr] >= i; break;
1047 case CCL_NE: reg[rrr] = reg[rrr] != i; break;
1049 ccl->status = CCL_STAT_INVALID_CMD;
1050 goto ccl_error_handler;
1054 case CCL_SetExprConst: /* 00000OPERATION000RRRrrrXXXXX */
1056 j = XINT (ccl_prog[ic]);
1058 jump_address = ++ic;
1061 case CCL_SetExprReg: /* 00000OPERATIONRrrRRRrrrXXXXX */
1068 case CCL_ReadJumpCondExprConst: /* A--D--D--R--E--S--S-rrrXXXXX */
1069 CCL_READ_CHAR (reg[rrr]);
1070 case CCL_JumpCondExprConst: /* A--D--D--R--E--S--S-rrrXXXXX */
1072 op = XINT (ccl_prog[ic]);
1073 jump_address = ic++ + ADDR;
1074 j = XINT (ccl_prog[ic]);
1079 case CCL_ReadJumpCondExprReg: /* A--D--D--R--E--S--S-rrrXXXXX */
1080 CCL_READ_CHAR (reg[rrr]);
1081 case CCL_JumpCondExprReg:
1083 op = XINT (ccl_prog[ic]);
1084 jump_address = ic++ + ADDR;
1085 j = reg[XINT (ccl_prog[ic])];
1092 case CCL_PLUS: reg[rrr] = i + j; break;
1093 case CCL_MINUS: reg[rrr] = i - j; break;
1094 case CCL_MUL: reg[rrr] = i * j; break;
1095 case CCL_DIV: reg[rrr] = i / j; break;
1096 case CCL_MOD: reg[rrr] = i % j; break;
1097 case CCL_AND: reg[rrr] = i & j; break;
1098 case CCL_OR: reg[rrr] = i | j; break;
1099 case CCL_XOR: reg[rrr] = i ^ j; break;
1100 case CCL_LSH: reg[rrr] = i << j; break;
1101 case CCL_RSH: reg[rrr] = i >> j; break;
1102 case CCL_LSH8: reg[rrr] = (i << 8) | j; break;
1103 case CCL_RSH8: reg[rrr] = i >> 8; reg[7] = i & 0xFF; break;
1104 case CCL_DIVMOD: reg[rrr] = i / j; reg[7] = i % j; break;
1105 case CCL_LS: reg[rrr] = i < j; break;
1106 case CCL_GT: reg[rrr] = i > j; break;
1107 case CCL_EQ: reg[rrr] = i == j; break;
1108 case CCL_LE: reg[rrr] = i <= j; break;
1109 case CCL_GE: reg[rrr] = i >= j; break;
1110 case CCL_NE: reg[rrr] = i != j; break;
1111 case CCL_DECODE_SJIS: DECODE_SJIS (i, j, reg[rrr], reg[7]); break;
1112 case CCL_ENCODE_SJIS: ENCODE_SJIS (i, j, reg[rrr], reg[7]); break;
1114 ccl->status = CCL_STAT_INVALID_CMD;
1115 goto ccl_error_handler;
1118 if (code == CCL_WriteExprConst || code == CCL_WriteExprRegister)
1130 case CCL_ReadMultibyteChar2:
1138 goto ccl_read_multibyte_character_suspend;
1143 if (i == LEADING_CODE_COMPOSITION)
1146 goto ccl_read_multibyte_character_suspend;
1149 ccl->private_state = COMPOSING_WITH_RULE_HEAD;
1153 ccl->private_state = COMPOSING_NO_RULE_HEAD;
1157 if (ccl->private_state != COMPOSING_NO)
1159 /* composite character */
1161 ccl->private_state = COMPOSING_NO;
1164 if (COMPOSING_WITH_RULE_RULE == ccl->private_state)
1166 ccl->private_state = COMPOSING_WITH_RULE_HEAD;
1169 else if (COMPOSING_WITH_RULE_HEAD == ccl->private_state)
1170 ccl->private_state = COMPOSING_WITH_RULE_RULE;
1175 goto ccl_read_multibyte_character_suspend;
1188 reg[RRR] = LEADING_BYTE_ASCII;
1190 else if (i <= MAX_LEADING_BYTE_OFFICIAL_1)
1193 goto ccl_read_multibyte_character_suspend;
1195 reg[rrr] = (*src++ & 0x7F);
1197 else if (i <= MAX_LEADING_BYTE_OFFICIAL_2)
1199 if ((src + 1) >= src_end)
1200 goto ccl_read_multibyte_character_suspend;
1202 i = (*src++ & 0x7F);
1203 reg[rrr] = ((i << 7) | (*src & 0x7F));
1206 else if (i == PRE_LEADING_BYTE_PRIVATE_1)
1208 if ((src + 1) >= src_end)
1209 goto ccl_read_multibyte_character_suspend;
1211 reg[rrr] = (*src++ & 0x7F);
1213 else if (i == PRE_LEADING_BYTE_PRIVATE_2)
1215 if ((src + 2) >= src_end)
1216 goto ccl_read_multibyte_character_suspend;
1218 i = (*src++ & 0x7F);
1219 reg[rrr] = ((i << 7) | (*src & 0x7F));
1224 /* INVALID CODE. Return a single byte character. */
1225 reg[RRR] = LEADING_BYTE_ASCII;
1232 ccl_read_multibyte_character_suspend:
1234 if (ccl->last_block)
1240 CCL_SUSPEND (CCL_STAT_SUSPEND_BY_SRC);
1244 case CCL_WriteMultibyteChar2:
1245 i = reg[RRR]; /* charset */
1246 if (i == LEADING_BYTE_ASCII)
1247 i = reg[rrr] & 0xFF;
1249 else if (i == CHARSET_COMPOSITION)
1250 i = MAKE_COMPOSITE_CHAR (reg[rrr]);
1252 else if (XCHARSET_DIMENSION (CHARSET_BY_LEADING_BYTE (i)) == 1)
1253 i = ((i - FIELD2_TO_OFFICIAL_LEADING_BYTE) << 7)
1254 | (reg[rrr] & 0x7F);
1255 else if (i < MIN_LEADING_BYTE_OFFICIAL_2)
1256 i = ((i - FIELD1_TO_OFFICIAL_LEADING_BYTE) << 14) | reg[rrr];
1258 i = ((i - FIELD1_TO_PRIVATE_LEADING_BYTE) << 14) | reg[rrr];
1265 case CCL_TranslateCharacter:
1266 i = reg[RRR]; /* charset */
1267 if (i == LEADING_BYTE_ASCII)
1269 else if (i == CHARSET_COMPOSITION)
1274 else if (CHARSET_DIMENSION (i) == 1)
1275 i = ((i - 0x70) << 7) | (reg[rrr] & 0x7F);
1276 else if (i < MIN_LEADING_BYTE_OFFICIAL_2)
1277 i = ((i - 0x8F) << 14) | (reg[rrr] & 0x3FFF);
1279 i = ((i - 0xE0) << 14) | (reg[rrr] & 0x3FFF);
1281 op = translate_char (GET_TRANSLATION_TABLE (reg[Rrr]),
1283 SPLIT_CHAR (op, reg[RRR], i, j);
1290 case CCL_TranslateCharacterConstTbl:
1291 op = XINT (ccl_prog[ic]); /* table */
1293 i = reg[RRR]; /* charset */
1294 if (i == LEADING_BYTE_ASCII)
1296 else if (i == CHARSET_COMPOSITION)
1301 else if (CHARSET_DIMENSION (i) == 1)
1302 i = ((i - 0x70) << 7) | (reg[rrr] & 0x7F);
1303 else if (i < MIN_LEADING_BYTE_OFFICIAL_2)
1304 i = ((i - 0x8F) << 14) | (reg[rrr] & 0x3FFF);
1306 i = ((i - 0xE0) << 14) | (reg[rrr] & 0x3FFF);
1308 op = translate_char (GET_TRANSLATION_TABLE (op), i, -1, 0, 0);
1309 SPLIT_CHAR (op, reg[RRR], i, j);
1316 case CCL_IterateMultipleMap:
1318 Lisp_Object map, content, attrib, value;
1319 int point, size, fin_ic;
1321 j = XINT (ccl_prog[ic++]); /* number of maps. */
1324 if ((j > reg[RRR]) && (j >= 0))
1339 size = XVECTOR (Vcode_conversion_map_vector)->size;
1340 point = XINT (ccl_prog[ic++]);
1341 if (point >= size) continue;
1343 XVECTOR (Vcode_conversion_map_vector)->contents[point];
1345 /* Check map varidity. */
1346 if (!CONSP (map)) continue;
1347 map = XCONS(map)->cdr;
1348 if (!VECTORP (map)) continue;
1349 size = XVECTOR (map)->size;
1350 if (size <= 1) continue;
1352 content = XVECTOR (map)->contents[0];
1355 [STARTPOINT VAL1 VAL2 ...] or
1356 [t ELELMENT STARTPOINT ENDPOINT] */
1357 if (NUMBERP (content))
1359 point = XUINT (content);
1360 point = op - point + 1;
1361 if (!((point >= 1) && (point < size))) continue;
1362 content = XVECTOR (map)->contents[point];
1364 else if (EQ (content, Qt))
1366 if (size != 4) continue;
1367 if ((op >= XUINT (XVECTOR (map)->contents[2]))
1368 && (op < XUINT (XVECTOR (map)->contents[3])))
1369 content = XVECTOR (map)->contents[1];
1378 else if (NUMBERP (content))
1381 reg[rrr] = XINT(content);
1384 else if (EQ (content, Qt) || EQ (content, Qlambda))
1389 else if (CONSP (content))
1391 attrib = XCONS (content)->car;
1392 value = XCONS (content)->cdr;
1393 if (!NUMBERP (attrib) || !NUMBERP (value))
1396 reg[rrr] = XUINT (value);
1406 case CCL_MapMultiple:
1408 Lisp_Object map, content, attrib, value;
1409 int point, size, map_vector_size;
1410 int map_set_rest_length, fin_ic;
1412 map_set_rest_length =
1413 XINT (ccl_prog[ic++]); /* number of maps and separators. */
1414 fin_ic = ic + map_set_rest_length;
1415 if ((map_set_rest_length > reg[RRR]) && (reg[RRR] >= 0))
1419 map_set_rest_length -= i;
1427 mapping_stack_pointer = mapping_stack;
1429 PUSH_MAPPING_STACK (0, op);
1431 map_vector_size = XVECTOR (Vcode_conversion_map_vector)->size;
1432 for (;map_set_rest_length > 0;i++, map_set_rest_length--)
1434 point = XINT(ccl_prog[ic++]);
1438 if (mapping_stack_pointer
1439 >= &mapping_stack[MAX_MAP_SET_LEVEL])
1443 PUSH_MAPPING_STACK (map_set_rest_length - point,
1445 map_set_rest_length = point + 1;
1450 if (point >= map_vector_size) continue;
1451 map = (XVECTOR (Vcode_conversion_map_vector)
1454 /* Check map varidity. */
1455 if (!CONSP (map)) continue;
1456 map = XCONS (map)->cdr;
1457 if (!VECTORP (map)) continue;
1458 size = XVECTOR (map)->size;
1459 if (size <= 1) continue;
1461 content = XVECTOR (map)->contents[0];
1464 [STARTPOINT VAL1 VAL2 ...] or
1465 [t ELEMENT STARTPOINT ENDPOINT] */
1466 if (NUMBERP (content))
1468 point = XUINT (content);
1469 point = op - point + 1;
1470 if (!((point >= 1) && (point < size))) continue;
1471 content = XVECTOR (map)->contents[point];
1473 else if (EQ (content, Qt))
1475 if (size != 4) continue;
1476 if ((op >= XUINT (XVECTOR (map)->contents[2])) &&
1477 (op < XUINT (XVECTOR (map)->contents[3])))
1478 content = XVECTOR (map)->contents[1];
1487 else if (NUMBERP (content))
1489 op = XINT (content);
1491 i += map_set_rest_length;
1492 POP_MAPPING_STACK (map_set_rest_length, reg[rrr]);
1494 else if (CONSP (content))
1496 attrib = XCONS (content)->car;
1497 value = XCONS (content)->cdr;
1498 if (!NUMBERP (attrib) || !NUMBERP (value))
1502 i += map_set_rest_length;
1503 POP_MAPPING_STACK (map_set_rest_length, reg[rrr]);
1505 else if (EQ (content, Qt))
1509 i += map_set_rest_length;
1510 POP_MAPPING_STACK (map_set_rest_length, reg[rrr]);
1512 else if (EQ (content, Qlambda))
1526 Lisp_Object map, attrib, value, content;
1528 j = XINT (ccl_prog[ic++]); /* map_id */
1530 if (j >= XVECTOR (Vcode_conversion_map_vector)->size)
1535 map = XVECTOR (Vcode_conversion_map_vector)->contents[j];
1541 map = XCONS(map)->cdr;
1547 size = XVECTOR (map)->size;
1548 point = XUINT (XVECTOR (map)->contents[0]);
1549 point = op - point + 1;
1552 (!((point >= 1) && (point < size))))
1556 content = XVECTOR (map)->contents[point];
1559 else if (NUMBERP (content))
1560 reg[rrr] = XINT (content);
1561 else if (EQ (content, Qt))
1563 else if (CONSP (content))
1565 attrib = XCONS (content)->car;
1566 value = XCONS (content)->cdr;
1567 if (!NUMBERP (attrib) || !NUMBERP (value))
1569 reg[rrr] = XUINT(value);
1585 ccl->status = CCL_STAT_INVALID_CMD;
1586 goto ccl_error_handler;
1593 /* We can insert an error message only if DESTINATION is
1594 specified and we still have a room to store the message
1598 #if 0 /* not for XEmacs ? */
1603 switch (ccl->status)
1605 /* Terminate CCL program because of invalid command.
1606 Should not occur in the normal case. */
1607 case CCL_STAT_INVALID_CMD:
1608 sprintf(msg, "\nCCL: Invalid command %x (ccl_code = %x) at %d.",
1609 code & 0x1F, code, this_ic);
1612 int i = ccl_backtrace_idx - 1;
1615 Dynarr_add_many (destination, (unsigned char *) msg, strlen (msg));
1617 for (j = 0; j < CCL_DEBUG_BACKTRACE_LEN; j++, i--)
1619 if (i < 0) i = CCL_DEBUG_BACKTRACE_LEN - 1;
1620 if (ccl_backtrace_table[i] == 0)
1622 sprintf(msg, " %d", ccl_backtrace_table[i]);
1623 Dynarr_add_many (destination, (unsigned char *) msg, strlen (msg));
1631 sprintf(msg, "\nCCL: Quited.");
1635 sprintf(msg, "\nCCL: Unknown error type (%d).", ccl->status);
1638 Dynarr_add_many (destination, (unsigned char *) msg, strlen (msg));
1643 ccl->stack_idx = stack_idx;
1644 ccl->prog = ccl_prog;
1645 if (consumed) *consumed = src - source;
1647 return Dynarr_length (destination);
1652 /* Setup fields of the structure pointed by CCL appropriately for the
1653 execution of compiled CCL code in VEC (vector of integer).
1654 If VEC is nil, we skip setting ups based on VEC. */
1656 setup_ccl_program (struct ccl_program *ccl, Lisp_Object vec)
1662 ccl->size = XVECTOR_LENGTH (vec);
1663 ccl->prog = XVECTOR_DATA (vec);
1664 ccl->eof_ic = XINT (XVECTOR_DATA (vec)[CCL_HEADER_EOF]);
1665 ccl->buf_magnification = XINT (XVECTOR_DATA (vec)[CCL_HEADER_BUF_MAG]);
1667 ccl->ic = CCL_HEADER_MAIN;
1668 for (i = 0; i < 8; i++)
1670 ccl->last_block = 0;
1671 ccl->private_state = 0;
1676 /* Resolve symbols in the specified CCL code (Lisp vector). This
1677 function converts symbols of code conversion maps and character
1678 translation tables embeded in the CCL code into their ID numbers. */
1681 resolve_symbol_ccl_program (Lisp_Object ccl)
1684 Lisp_Object result, contents /*, prop */;
1687 veclen = XVECTOR_LENGTH (result);
1689 /* Set CCL program's table ID */
1690 for (i = 0; i < veclen; i++)
1692 contents = XVECTOR_DATA (result)[i];
1693 if (SYMBOLP (contents))
1695 if (EQ(result, ccl))
1696 result = Fcopy_sequence (ccl);
1699 prop = Fget (contents, Qtranslation_table_id);
1702 XVECTOR_DATA (result)[i] = prop;
1705 prop = Fget (contents, Qcode_conversion_map_id);
1708 XVECTOR_DATA (result)[i] = prop;
1711 prop = Fget (contents, Qccl_program_idx);
1714 XVECTOR_DATA (result)[i] = prop;
1727 DEFUN ("ccl-execute", Fccl_execute, 2, 2, 0, /*
1728 Execute CCL-PROGRAM with registers initialized by REGISTERS.
1730 CCL-PROGRAM is a symbol registered by register-ccl-program,
1731 or a compiled code generated by `ccl-compile' (for backward compatibility,
1732 in this case, the execution is slower).
1733 No I/O commands should appear in CCL-PROGRAM.
1735 REGISTERS is a vector of [R0 R1 ... R7] where RN is an initial value
1738 As side effect, each element of REGISTER holds the value of
1739 corresponding register after the execution.
1743 struct ccl_program ccl;
1747 if (SYMBOLP (ccl_prog) &&
1748 !NILP (ccl_id = Fget (ccl_prog, Qccl_program_idx, Qnil)))
1750 ccl_prog = XVECTOR_DATA (Vccl_program_table)[XUINT (ccl_id)];
1751 CHECK_LIST (ccl_prog);
1752 ccl_prog = XCDR (ccl_prog);
1753 CHECK_VECTOR (ccl_prog);
1757 CHECK_VECTOR (ccl_prog);
1758 ccl_prog = resolve_symbol_ccl_program (ccl_prog);
1762 if (XVECTOR_LENGTH (reg) != 8)
1763 error ("Invalid length of vector REGISTERS");
1765 setup_ccl_program (&ccl, ccl_prog);
1766 for (i = 0; i < 8; i++)
1767 ccl.reg[i] = (INTP (XVECTOR_DATA (reg)[i])
1768 ? XINT (XVECTOR_DATA (reg)[i])
1771 ccl_driver (&ccl, (const unsigned char *)0, (unsigned_char_dynarr *)0,
1772 0, (int *)0, CCL_MODE_ENCODING);
1774 if (ccl.status != CCL_STAT_SUCCESS)
1775 error ("Error in CCL program at %dth code", ccl.ic);
1777 for (i = 0; i < 8; i++)
1778 XSETINT (XVECTOR_DATA (reg)[i], ccl.reg[i]);
1782 DEFUN ("ccl-execute-on-string", Fccl_execute_on_string, 3, 4, 0, /*
1783 Execute CCL-PROGRAM with initial STATUS on STRING.
1785 CCL-PROGRAM is a symbol registered by register-ccl-program,
1786 or a compiled code generated by `ccl-compile' (for backward compatibility,
1787 in this case, the execution is slower).
1789 Read buffer is set to STRING, and write buffer is allocated automatically.
1791 If IC is nil, it is initialized to head of the CCL program.\n\
1792 STATUS is a vector of [R0 R1 ... R7 IC], where
1793 R0..R7 are initial values of corresponding registers,
1794 IC is the instruction counter specifying from where to start the program.
1795 If R0..R7 are nil, they are initialized to 0.
1796 If IC is nil, it is initialized to head of the CCL program.
1798 If optional 4th arg CONTINUE is non-nil, keep IC on read operation
1799 when read buffer is exausted, else, IC is always set to the end of
1800 CCL-PROGRAM on exit.
1802 It returns the contents of write buffer as a string,
1803 and as side effect, STATUS is updated.
1805 (ccl_prog, status, str, contin))
1808 struct ccl_program ccl;
1810 unsigned_char_dynarr *outbuf;
1811 struct gcpro gcpro1, gcpro2, gcpro3;
1814 if (SYMBOLP (ccl_prog) &&
1815 !NILP (ccl_id = Fget (ccl_prog, Qccl_program_idx, Qnil)))
1817 ccl_prog = XVECTOR (Vccl_program_table)->contents[XUINT (ccl_id)];
1818 CHECK_LIST (ccl_prog);
1819 ccl_prog = XCDR (ccl_prog);
1820 CHECK_VECTOR (ccl_prog);
1824 CHECK_VECTOR (ccl_prog);
1825 ccl_prog = resolve_symbol_ccl_program (ccl_prog);
1828 CHECK_VECTOR (status);
1829 if (XVECTOR_LENGTH (status) != 9)
1830 signal_simple_error ("Vector should be of length 9", status);
1832 GCPRO3 (ccl_prog, status, str);
1834 setup_ccl_program (&ccl, ccl_prog);
1835 for (i = 0; i < 8; i++)
1837 if (NILP (XVECTOR_DATA (status)[i]))
1838 XSETINT (XVECTOR_DATA (status)[i], 0);
1839 if (INTP (XVECTOR_DATA (status)[i]))
1840 ccl.reg[i] = XINT (XVECTOR_DATA (status)[i]);
1842 if (INTP (XVECTOR_DATA (status)[8]))
1844 i = XINT (XVECTOR_DATA (status)[8]);
1845 if (ccl.ic < i && i < ccl.size)
1848 outbuf = Dynarr_new (unsigned_char);
1849 ccl.last_block = NILP (contin);
1850 produced = ccl_driver (&ccl, XSTRING_DATA (str), outbuf,
1851 XSTRING_LENGTH (str), (int *)0, CCL_MODE_DECODING);
1852 for (i = 0; i < 8; i++)
1853 XVECTOR_DATA (status)[i] = make_int(ccl.reg[i]);
1854 XSETINT (XVECTOR_DATA (status)[8], ccl.ic);
1857 val = make_string (Dynarr_atp (outbuf, 0), produced);
1858 Dynarr_free (outbuf);
1860 if (ccl.status != CCL_STAT_SUCCESS
1861 && ccl.status != CCL_STAT_SUSPEND_BY_SRC
1862 && ccl.status != CCL_STAT_SUSPEND_BY_DST)
1863 error ("Error in CCL program at %dth code", ccl.ic);
1868 DEFUN ("register-ccl-program", Fregister_ccl_program, 2, 2, 0, /*
1869 Register CCL program PROGRAM of NAME in `ccl-program-table'.
1870 PROGRAM should be a compiled code of CCL program, or nil.
1871 Return index number of the registered CCL program.
1875 int len = XVECTOR_LENGTH (Vccl_program_table);
1878 CHECK_SYMBOL (name);
1879 if (!NILP (ccl_prog))
1881 CHECK_VECTOR (ccl_prog);
1882 ccl_prog = resolve_symbol_ccl_program (ccl_prog);
1885 for (i = 0; i < len; i++)
1887 Lisp_Object slot = XVECTOR_DATA (Vccl_program_table)[i];
1892 if (EQ (name, XCAR (slot)))
1894 XCDR (slot) = ccl_prog;
1895 return make_int (i);
1901 Lisp_Object new_table = Fmake_vector (make_int (len * 2), Qnil);
1904 for (j = 0; j < len; j++)
1905 XVECTOR_DATA (new_table)[j]
1906 = XVECTOR_DATA (Vccl_program_table)[j];
1907 Vccl_program_table = new_table;
1910 XVECTOR_DATA (Vccl_program_table)[i] = Fcons (name, ccl_prog);
1911 Fput (name, Qccl_program_idx, make_int (i));
1912 return make_int (i);
1916 /* Register code conversion map.
1917 A code conversion map consists of numbers, Qt, Qnil, and Qlambda.
1918 The first element is start code point.
1919 The rest elements are mapped numbers.
1920 Symbol t means to map to an original number before mapping.
1921 Symbol nil means that the corresponding element is empty.
1922 Symbol lambda menas to terminate mapping here.
1925 DEFUN ("register-code-conversion-map", Fregister_code_conversion_map,
1926 Sregister_code_conversion_map,
1928 "Register SYMBOL as code conversion map MAP.\n\
1929 Return index number of the registered map.")
1931 Lisp_Object symbol, map;
1933 int len = XVECTOR (Vcode_conversion_map_vector)->size;
1937 CHECK_SYMBOL (symbol, 0);
1938 CHECK_VECTOR (map, 1);
1940 for (i = 0; i < len; i++)
1942 Lisp_Object slot = XVECTOR (Vcode_conversion_map_vector)->contents[i];
1947 if (EQ (symbol, XCONS (slot)->car))
1949 index = make_int (i);
1950 XCONS (slot)->cdr = map;
1951 Fput (symbol, Qcode_conversion_map, map);
1952 Fput (symbol, Qcode_conversion_map_id, index);
1959 Lisp_Object new_vector = Fmake_vector (make_int (len * 2), Qnil);
1962 for (j = 0; j < len; j++)
1963 XVECTOR (new_vector)->contents[j]
1964 = XVECTOR (Vcode_conversion_map_vector)->contents[j];
1965 Vcode_conversion_map_vector = new_vector;
1968 index = make_int (i);
1969 Fput (symbol, Qcode_conversion_map, map);
1970 Fput (symbol, Qcode_conversion_map_id, index);
1971 XVECTOR (Vcode_conversion_map_vector)->contents[i] = Fcons (symbol, map);
1978 syms_of_mule_ccl (void)
1980 DEFSUBR (Fccl_execute);
1981 DEFSUBR (Fccl_execute_on_string);
1982 DEFSUBR (Fregister_ccl_program);
1984 DEFSUBR (&Fregister_code_conversion_map);
1989 vars_of_mule_ccl (void)
1991 staticpro (&Vccl_program_table);
1992 Vccl_program_table = Fmake_vector (make_int (32), Qnil);
1994 Qccl_program = intern ("ccl-program");
1995 staticpro (&Qccl_program);
1997 Qccl_program_idx = intern ("ccl-program-idx");
1998 staticpro (&Qccl_program_idx);
2001 Qcode_conversion_map = intern ("code-conversion-map");
2002 staticpro (&Qcode_conversion_map);
2004 Qcode_conversion_map_id = intern ("code-conversion-map-id");
2005 staticpro (&Qcode_conversion_map_id);
2007 DEFVAR_LISP ("code-conversion-map-vector", &Vcode_conversion_map_vector /*
2008 Vector of code conversion maps.*/ );
2009 Vcode_conversion_map_vector = Fmake_vector (make_int (16), Qnil);
2012 DEFVAR_LISP ("font-ccl-encoder-alist", &Vfont_ccl_encoder_alist /*
2013 Alist of fontname patterns vs corresponding CCL program.
2014 Each element looks like (REGEXP . CCL-CODE),
2015 where CCL-CODE is a compiled CCL program.
2016 When a font whose name matches REGEXP is used for displaying a character,
2017 CCL-CODE is executed to calculate the code point in the font
2018 from the charset number and position code(s) of the character which are set
2019 in CCL registers R0, R1, and R2 before the execution.
2020 The code point in the font is set in CCL registers R1 and R2
2021 when the execution terminated.
2022 If the font is single-byte font, the register R2 is not used.
2024 Vfont_ccl_encoder_alist = Qnil;