ccl.c 63.8 KB
Newer Older
Karl Heuer's avatar
Karl Heuer committed
1
/* CCL (Code Conversion Language) interpreter.
2
   Copyright (C) 2001-2011 Free Software Foundation, Inc.
Kenichi Handa's avatar
Kenichi Handa committed
3
   Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4
     2005, 2006, 2007, 2008, 2009, 2010, 2011
Kenichi Handa's avatar
Kenichi Handa committed
5 6
     National Institute of Advanced Industrial Science and Technology (AIST)
     Registration Number H14PRO021
Kenichi Handa's avatar
Kenichi Handa committed
7
   Copyright (C) 2003
Kenichi Handa's avatar
Kenichi Handa committed
8 9
     National Institute of Advanced Industrial Science and Technology (AIST)
     Registration Number H13PRO009
Karl Heuer's avatar
Karl Heuer committed
10

Karl Heuer's avatar
Karl Heuer committed
11 12
This file is part of GNU Emacs.

13
GNU Emacs is free software: you can redistribute it and/or modify
Karl Heuer's avatar
Karl Heuer committed
14
it under the terms of the GNU General Public License as published by
15 16
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Karl Heuer's avatar
Karl Heuer committed
17

Karl Heuer's avatar
Karl Heuer committed
18 19 20 21
GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
Karl Heuer's avatar
Karl Heuer committed
22

Karl Heuer's avatar
Karl Heuer committed
23
You should have received a copy of the GNU General Public License
24
along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
Karl Heuer's avatar
Karl Heuer committed
25 26

#include <config.h>
Andreas Schwab's avatar
Andreas Schwab committed
27

28
#include <stdio.h>
29
#include <setjmp.h>
Eli Zaretskii's avatar
Eli Zaretskii committed
30
#include <limits.h>
31

Karl Heuer's avatar
Karl Heuer committed
32
#include "lisp.h"
Kenichi Handa's avatar
Kenichi Handa committed
33
#include "character.h"
Karl Heuer's avatar
Karl Heuer committed
34 35 36 37
#include "charset.h"
#include "ccl.h"
#include "coding.h"

Kenichi Handa's avatar
Kenichi Handa committed
38 39
Lisp_Object Qccl, Qcclp;

Juanma Barranquero's avatar
Juanma Barranquero committed
40
/* This symbol is a property which associates with ccl program vector.
Kenichi Handa's avatar
Kenichi Handa committed
41
   Ex: (get 'ccl-big5-encoder 'ccl-program) returns ccl program vector.  */
42
static Lisp_Object Qccl_program;
43

44 45
/* These symbols are properties which associate with code conversion
   map and their ID respectively.  */
46 47
static Lisp_Object Qcode_conversion_map;
static Lisp_Object Qcode_conversion_map_id;
48

Kenichi Handa's avatar
Kenichi Handa committed
49
/* Symbols of ccl program have this property, a value of the property
Paul Eggert's avatar
Paul Eggert committed
50
   is an index for Vccl_program_table. */
51
static Lisp_Object Qccl_program_idx;
Kenichi Handa's avatar
Kenichi Handa committed
52

53
/* Table of registered CCL programs.  Each element is a vector of
54 55 56 57 58 59
   NAME, CCL_PROG, RESOLVEDP, and UPDATEDP, where NAME (symbol) is the
   name of the program, CCL_PROG (vector) is the compiled code of the
   program, RESOLVEDP (t or nil) is the flag to tell if symbols in
   CCL_PROG is already resolved to index numbers or not, UPDATEDP (t
   or nil) is the flat to tell if the CCL program is updated after it
   was once used.  */
60
static Lisp_Object Vccl_program_table;
Karl Heuer's avatar
Karl Heuer committed
61

62 63
/* Return a hash table of id number ID.  */
#define GET_HASH_TABLE(id) \
Juanma Barranquero's avatar
Juanma Barranquero committed
64
  (XHASH_TABLE (XCDR (XVECTOR (Vtranslation_hash_table_vector)->contents[(id)])))
65

Karl Heuer's avatar
Karl Heuer committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
/* CCL (Code Conversion Language) is a simple language which has
   operations on one input buffer, one output buffer, and 7 registers.
   The syntax of CCL is described in `ccl.el'.  Emacs Lisp function
   `ccl-compile' compiles a CCL program and produces a CCL code which
   is a vector of integers.  The structure of this vector is as
   follows: The 1st element: buffer-magnification, a factor for the
   size of output buffer compared with the size of input buffer.  The
   2nd element: address of CCL code to be executed when encountered
   with end of input stream.  The 3rd and the remaining elements: CCL
   codes.  */

/* Header of CCL compiled code */
#define CCL_HEADER_BUF_MAG	0
#define CCL_HEADER_EOF		1
#define CCL_HEADER_MAIN		2

82 83
/* CCL code is a sequence of 28-bit integers.  Each contains a CCL
   command and/or arguments in the following format:
Karl Heuer's avatar
Karl Heuer committed
84 85 86 87 88 89 90 91 92 93 94 95

	|----------------- integer (28-bit) ------------------|
	|------- 17-bit ------|- 3-bit --|- 3-bit --|- 5-bit -|
	|--constant argument--|-register-|-register-|-command-|
	   ccccccccccccccccc      RRR        rrr       XXXXX
  or
	|------- relative address -------|-register-|-command-|
	       cccccccccccccccccccc          rrr       XXXXX
  or
	|------------- constant or other args ----------------|
                     cccccccccccccccccccccccccccc

96 97
   where `cc...c' is a 17-bit, 20-bit, or 28-bit integer indicating a
   constant value or a relative/absolute jump address, `RRR'
Karl Heuer's avatar
Karl Heuer committed
98 99 100
   and `rrr' are CCL register number, `XXXXX' is one of the following
   CCL commands.  */

Paul Eggert's avatar
Paul Eggert committed
101
#define CCL_CODE_MAX ((1 << (28 - 1)) - 1)
102
#define CCL_CODE_MIN (-1 - CCL_CODE_MAX)
Paul Eggert's avatar
Paul Eggert committed
103

Karl Heuer's avatar
Karl Heuer committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
/* CCL commands

   Each comment fields shows one or more lines for command syntax and
   the following lines for semantics of the command.  In semantics, IC
   stands for Instruction Counter.  */

#define CCL_SetRegister		0x00 /* Set register a register value:
					1:00000000000000000RRRrrrXXXXX
					------------------------------
					reg[rrr] = reg[RRR];
					*/

#define CCL_SetShortConst	0x01 /* Set register a short constant value:
					1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
					------------------------------
					reg[rrr] = CCCCCCCCCCCCCCCCCCC;
					*/

#define CCL_SetConst		0x02 /* Set register a constant value:
					1:00000000000000000000rrrXXXXX
					2:CONSTANT
					------------------------------
					reg[rrr] = CONSTANT;
					IC++;
					*/

#define CCL_SetArray		0x03 /* Set register an element of array:
					1:CCCCCCCCCCCCCCCCCRRRrrrXXXXX
					2:ELEMENT[0]
					3:ELEMENT[1]
					...
					------------------------------
					if (0 <= reg[RRR] < CC..C)
					  reg[rrr] = ELEMENT[reg[RRR]];
					IC += CC..C;
					*/

#define CCL_Jump		0x04 /* Jump:
					1:A--D--D--R--E--S--S-000XXXXX
					------------------------------
					IC += ADDRESS;
					*/

/* Note: If CC..C is greater than 0, the second code is omitted.  */

#define CCL_JumpCond		0x05 /* Jump conditional:
					1:A--D--D--R--E--S--S-rrrXXXXX
					------------------------------
					if (!reg[rrr])
					  IC += ADDRESS;
					*/


#define CCL_WriteRegisterJump	0x06 /* Write register and jump:
					1:A--D--D--R--E--S--S-rrrXXXXX
					------------------------------
					write (reg[rrr]);
					IC += ADDRESS;
					*/

#define CCL_WriteRegisterReadJump 0x07 /* Write register, read, and jump:
					1:A--D--D--R--E--S--S-rrrXXXXX
					2:A--D--D--R--E--S--S-rrrYYYYY
					-----------------------------
					write (reg[rrr]);
					IC++;
					read (reg[rrr]);
					IC += ADDRESS;
					*/
/* Note: If read is suspended, the resumed execution starts from the
   second code (YYYYY == CCL_ReadJump).  */

#define CCL_WriteConstJump	0x08 /* Write constant and jump:
					1:A--D--D--R--E--S--S-000XXXXX
					2:CONST
					------------------------------
					write (CONST);
					IC += ADDRESS;
					*/

#define CCL_WriteConstReadJump	0x09 /* Write constant, read, and jump:
					1:A--D--D--R--E--S--S-rrrXXXXX
					2:CONST
					3:A--D--D--R--E--S--S-rrrYYYYY
					-----------------------------
					write (CONST);
					IC += 2;
					read (reg[rrr]);
					IC += ADDRESS;
					*/
/* Note: If read is suspended, the resumed execution starts from the
   second code (YYYYY == CCL_ReadJump).  */

#define CCL_WriteStringJump	0x0A /* Write string and jump:
					1:A--D--D--R--E--S--S-000XXXXX
					2:LENGTH
200
					3:000MSTRIN[0]STRIN[1]STRIN[2]
Karl Heuer's avatar
Karl Heuer committed
201 202
					...
					------------------------------
203 204 205 206
					if (M)
					  write_multibyte_string (STRING, LENGTH);
					else
					  write_string (STRING, LENGTH);
Karl Heuer's avatar
Karl Heuer committed
207 208 209 210 211 212
					IC += ADDRESS;
					*/

#define CCL_WriteArrayReadJump	0x0B /* Write an array element, read, and jump:
					1:A--D--D--R--E--S--S-rrrXXXXX
					2:LENGTH
Paul Eggert's avatar
Paul Eggert committed
213 214
					3:ELEMENT[0]
					4:ELEMENT[1]
Karl Heuer's avatar
Karl Heuer committed
215 216 217 218 219 220 221 222 223 224
					...
					N:A--D--D--R--E--S--S-rrrYYYYY
					------------------------------
					if (0 <= reg[rrr] < LENGTH)
					  write (ELEMENT[reg[rrr]]);
					IC += LENGTH + 2; (... pointing at N+1)
					read (reg[rrr]);
					IC += ADDRESS;
					*/
/* Note: If read is suspended, the resumed execution starts from the
225
   Nth code (YYYYY == CCL_ReadJump).  */
Karl Heuer's avatar
Karl Heuer committed
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298

#define CCL_ReadJump		0x0C /* Read and jump:
					1:A--D--D--R--E--S--S-rrrYYYYY
					-----------------------------
					read (reg[rrr]);
					IC += ADDRESS;
					*/

#define CCL_Branch		0x0D /* Jump by branch table:
					1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
					2:A--D--D--R--E-S-S[0]000XXXXX
					3:A--D--D--R--E-S-S[1]000XXXXX
					...
					------------------------------
					if (0 <= reg[rrr] < CC..C)
					  IC += ADDRESS[reg[rrr]];
					else
					  IC += ADDRESS[CC..C];
					*/

#define CCL_ReadRegister	0x0E /* Read bytes into registers:
					1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
					2:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
					...
					------------------------------
					while (CCC--)
					  read (reg[rrr]);
					*/

#define CCL_WriteExprConst	0x0F  /* write result of expression:
					1:00000OPERATION000RRR000XXXXX
					2:CONSTANT
					------------------------------
					write (reg[RRR] OPERATION CONSTANT);
					IC++;
					*/

/* Note: If the Nth read is suspended, the resumed execution starts
   from the Nth code.  */

#define CCL_ReadBranch		0x10 /* Read one byte into a register,
					and jump by branch table:
					1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
					2:A--D--D--R--E-S-S[0]000XXXXX
					3:A--D--D--R--E-S-S[1]000XXXXX
					...
					------------------------------
					read (read[rrr]);
					if (0 <= reg[rrr] < CC..C)
					  IC += ADDRESS[reg[rrr]];
					else
					  IC += ADDRESS[CC..C];
					*/

#define CCL_WriteRegister	0x11 /* Write registers:
					1:CCCCCCCCCCCCCCCCCCCrrrXXXXX
					2:CCCCCCCCCCCCCCCCCCCrrrXXXXX
					...
					------------------------------
					while (CCC--)
					  write (reg[rrr]);
					...
					*/

/* Note: If the Nth write is suspended, the resumed execution
   starts from the Nth code.  */

#define CCL_WriteExprRegister	0x12 /* Write result of expression
					1:00000OPERATIONRrrRRR000XXXXX
					------------------------------
					write (reg[RRR] OPERATION reg[Rrr]);
					*/

299
#define CCL_Call		0x13 /* Call the CCL program whose ID is
300 301 302
					CC..C or cc..c.
					1:CCCCCCCCCCCCCCCCCCCCFFFXXXXX
					[2:00000000cccccccccccccccccccc]
Karl Heuer's avatar
Karl Heuer committed
303
					------------------------------
304 305 306 307 308
					if (FFF)
					  call (cc..c)
					  IC++;
					else
					  call (CC..C)
Karl Heuer's avatar
Karl Heuer committed
309 310 311 312
					*/

#define CCL_WriteConstString	0x14 /* Write a constant or a string:
					1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
313
					[2:000MSTRIN[0]STRIN[1]STRIN[2]]
Karl Heuer's avatar
Karl Heuer committed
314 315 316 317 318
					[...]
					-----------------------------
					if (!rrr)
					  write (CC..C)
					else
319 320 321 322
					  if (M)
					    write_multibyte_string (STRING, CC..C);
					  else
					    write_string (STRING, CC..C);
Karl Heuer's avatar
Karl Heuer committed
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
					  IC += (CC..C + 2) / 3;
					*/

#define CCL_WriteArray		0x15 /* Write an element of array:
					1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
					2:ELEMENT[0]
					3:ELEMENT[1]
					...
					------------------------------
					if (0 <= reg[rrr] < CC..C)
					  write (ELEMENT[reg[rrr]]);
					IC += CC..C;
					*/

#define CCL_End			0x16 /* Terminate:
					1:00000000000000000000000XXXXX
					------------------------------
					terminate ();
					*/

/* The following two codes execute an assignment arithmetic/logical
   operation.  The form of the operation is like REG OP= OPERAND.  */

#define CCL_ExprSelfConst	0x17 /* REG OP= constant:
					1:00000OPERATION000000rrrXXXXX
					2:CONSTANT
					------------------------------
					reg[rrr] OPERATION= CONSTANT;
					*/

#define CCL_ExprSelfReg		0x18 /* REG1 OP= REG2:
					1:00000OPERATION000RRRrrrXXXXX
					------------------------------
					reg[rrr] OPERATION= reg[RRR];
					*/

/* The following codes execute an arithmetic/logical operation.  The
   form of the operation is like REG_X = REG_Y OP OPERAND2.  */

#define CCL_SetExprConst	0x19 /* REG_X = REG_Y OP constant:
					1:00000OPERATION000RRRrrrXXXXX
					2:CONSTANT
					------------------------------
					reg[rrr] = reg[RRR] OPERATION CONSTANT;
					IC++;
					*/

#define CCL_SetExprReg		0x1A /* REG1 = REG2 OP REG3:
					1:00000OPERATIONRrrRRRrrrXXXXX
					------------------------------
					reg[rrr] = reg[RRR] OPERATION reg[Rrr];
					*/

#define CCL_JumpCondExprConst	0x1B /* Jump conditional according to
					an operation on constant:
					1:A--D--D--R--E--S--S-rrrXXXXX
					2:OPERATION
					3:CONSTANT
					-----------------------------
					reg[7] = reg[rrr] OPERATION CONSTANT;
					if (!(reg[7]))
					  IC += ADDRESS;
					else
					  IC += 2
					*/

#define CCL_JumpCondExprReg	0x1C /* Jump conditional according to
					an operation on register:
					1:A--D--D--R--E--S--S-rrrXXXXX
					2:OPERATION
					3:RRR
					-----------------------------
					reg[7] = reg[rrr] OPERATION reg[RRR];
					if (!reg[7])
					  IC += ADDRESS;
					else
					  IC += 2;
					*/

#define CCL_ReadJumpCondExprConst 0x1D /* Read and jump conditional according
					  to an operation on constant:
					1:A--D--D--R--E--S--S-rrrXXXXX
					2:OPERATION
					3:CONSTANT
					-----------------------------
					read (reg[rrr]);
					reg[7] = reg[rrr] OPERATION CONSTANT;
					if (!reg[7])
					  IC += ADDRESS;
					else
					  IC += 2;
					*/

#define CCL_ReadJumpCondExprReg	0x1E /* Read and jump conditional according
					to an operation on register:
					1:A--D--D--R--E--S--S-rrrXXXXX
					2:OPERATION
					3:RRR
					-----------------------------
					read (reg[rrr]);
					reg[7] = reg[rrr] OPERATION reg[RRR];
					if (!reg[7])
					  IC += ADDRESS;
					else
					  IC += 2;
					*/

430
#define CCL_Extension		0x1F /* Extended CCL code
Karl Heuer's avatar
Karl Heuer committed
431
					1:ExtendedCOMMNDRrrRRRrrrXXXXX
Paul Eggert's avatar
Paul Eggert committed
432
					2:ARGUMENT
Karl Heuer's avatar
Karl Heuer committed
433 434 435 436 437
					3:...
					------------------------------
					extended_command (rrr,RRR,Rrr,ARGS)
				      */

438
/*
Kenichi Handa's avatar
Kenichi Handa committed
439
   Here after, Extended CCL Instructions.
440
   Bit length of extended command is 14.
Kenichi Handa's avatar
Kenichi Handa committed
441
   Therefore, the instruction code range is 0..16384(0x3fff).
442 443
 */

444
/* Read a multibyte character.
Kenichi Handa's avatar
Kenichi Handa committed
445 446 447 448 449 450 451 452 453 454 455 456 457
   A code point is stored into reg[rrr].  A charset ID is stored into
   reg[RRR].  */

#define CCL_ReadMultibyteChar2	0x00 /* Read Multibyte Character
					1:ExtendedCOMMNDRrrRRRrrrXXXXX  */

/* Write a multibyte character.
   Write a character whose code point is reg[rrr] and the charset ID
   is reg[RRR].  */

#define CCL_WriteMultibyteChar2	0x01 /* Write Multibyte Character
					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].
Kenichi Handa's avatar
Kenichi Handa committed
460

461
   A translated character is set in reg[rrr] (code point) and reg[RRR]
Kenichi Handa's avatar
Kenichi Handa committed
462 463
   (charset ID).  */

464
#define CCL_TranslateCharacter	0x02 /* Translate a multibyte character
Kenichi Handa's avatar
Kenichi Handa committed
465 466
					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.
Kenichi Handa's avatar
Kenichi Handa committed
469

470
   A translated character is set in reg[rrr] (code point) and reg[RRR]
Kenichi Handa's avatar
Kenichi Handa committed
471 472
   (charset ID).  */

473 474 475 476
#define CCL_TranslateCharacterConstTbl 0x03 /* Translate a multibyte character
					       1:ExtendedCOMMNDRrrRRRrrrXXXXX
					       2:ARGUMENT(Translation Table ID)
					    */
Kenichi Handa's avatar
Kenichi Handa committed
477

478 479
/* Iterate looking up MAPs for reg[rrr] starting from the Nth (N =
   reg[RRR]) MAP until some value is found.
Kenichi Handa's avatar
Kenichi Handa committed
480

481
   Each MAP is a Lisp vector whose element is number, nil, t, or
Kenichi Handa's avatar
Kenichi Handa committed
482
   lambda.
483
   If the element is nil, ignore the map and proceed to the next map.
Kenichi Handa's avatar
Kenichi Handa committed
484 485 486
   If the element is t or lambda, finish without changing reg[rrr].
   If the element is a number, set reg[rrr] to the number and finish.

Paul Eggert's avatar
Paul Eggert committed
487
   Detail of the map structure is described in the comment for
488
   CCL_MapMultiple below.  */
Kenichi Handa's avatar
Kenichi Handa committed
489

490
#define CCL_IterateMultipleMap	0x10 /* Iterate multiple maps
Kenichi Handa's avatar
Kenichi Handa committed
491
					1:ExtendedCOMMNDXXXRRRrrrXXXXX
492 493 494
					2:NUMBER of MAPs
					3:MAP-ID1
					4:MAP-ID2
Kenichi Handa's avatar
Kenichi Handa committed
495
					...
496
				     */
Kenichi Handa's avatar
Kenichi Handa committed
497

498 499
/* Map the code in reg[rrr] by MAPs starting from the Nth (N =
   reg[RRR]) map.
Kenichi Handa's avatar
Kenichi Handa committed
500

501
   MAPs are supplied in the succeeding CCL codes as follows:
Kenichi Handa's avatar
Kenichi Handa committed
502

503 504 505 506 507 508 509 510
   When CCL program gives this nested structure of map to this command:
	((MAP-ID11
	  MAP-ID12
	  (MAP-ID121 MAP-ID122 MAP-ID123)
	  MAP-ID13)
	 (MAP-ID21
	  (MAP-ID211 (MAP-ID2111) MAP-ID212)
	  MAP-ID22)),
Kenichi Handa's avatar
Kenichi Handa committed
511
   the compiled CCL codes has this sequence:
512
	CCL_MapMultiple (CCL code of this command)
513 514
	16 (total number of MAPs and SEPARATORs)
	-7 (1st SEPARATOR)
515 516
	MAP-ID11
	MAP-ID12
517
	-3 (2nd SEPARATOR)
518 519 520 521
	MAP-ID121
	MAP-ID122
	MAP-ID123
	MAP-ID13
522
	-7 (3rd SEPARATOR)
523
	MAP-ID21
524
	-4 (4th SEPARATOR)
525
	MAP-ID211
526
	-1 (5th SEPARATOR)
527 528 529
	MAP_ID2111
	MAP-ID212
	MAP-ID22
Kenichi Handa's avatar
Kenichi Handa committed
530

531
   A value of each SEPARATOR follows this rule:
532 533
	MAP-SET := SEPARATOR [(MAP-ID | MAP-SET)]+
	SEPARATOR := -(number of MAP-IDs and SEPARATORs in the MAP-SET)
Kenichi Handa's avatar
Kenichi Handa committed
534

535
   (*)....Nest level of MAP-SET must not be over than MAX_MAP_SET_LEVEL.
Kenichi Handa's avatar
Kenichi Handa committed
536

537 538
   When some map fails to map (i.e. it doesn't have a value for
   reg[rrr]), the mapping is treated as identity.
Kenichi Handa's avatar
Kenichi Handa committed
539

540
   The mapping is iterated for all maps in each map set (set of maps
541 542 543 544 545 546
   separated by SEPARATOR) except in the case that lambda is
   encountered.  More precisely, the mapping proceeds as below:

   At first, VAL0 is set to reg[rrr], and it is translated by the
   first map to VAL1.  Then, VAL1 is translated by the next map to
   VAL2.  This mapping is iterated until the last map is used.  The
Kenichi Handa's avatar
Kenichi Handa committed
547 548 549 550
   result of the mapping is the last value of VAL?.  When the mapping
   process reached to the end of the map set, it moves to the next
   map set.  If the next does not exit, the mapping process terminates,
   and regard the last value as a result.
551 552 553 554

   But, when VALm is mapped to VALn and VALn is not a number, the
   mapping proceed as below:

Paul Eggert's avatar
Paul Eggert committed
555
   If VALn is nil, the last map is ignored and the mapping of VALm
556 557 558 559 560
   proceed to the next map.

   In VALn is t, VALm is reverted to reg[rrr] and the mapping of VALm
   proceed to the next map.

Kenichi Handa's avatar
Kenichi Handa committed
561 562 563
   If VALn is lambda, move to the next map set like reaching to the
   end of the current map set.

Paul Eggert's avatar
Paul Eggert committed
564
   If VALn is a symbol, call the CCL program referred by it.
Kenichi Handa's avatar
Kenichi Handa committed
565 566
   Then, use reg[rrr] as a mapped value except for -1, -2 and -3.
   Such special values are regarded as nil, t, and lambda respectively.
Kenichi Handa's avatar
Kenichi Handa committed
567

568
   Each map is a Lisp vector of the following format (a) or (b):
Kenichi Handa's avatar
Kenichi Handa committed
569 570 571
	(a)......[STARTPOINT VAL1 VAL2 ...]
	(b)......[t VAL STARTPOINT ENDPOINT],
   where
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.
Kenichi Handa's avatar
Kenichi Handa committed
575

576 577 578
   Valid index range of a map of type (a) is:
	STARTPOINT <= index < STARTPOINT + map_size - 1
   Valid index range of a map of type (b) is:
579
	STARTPOINT <= index < ENDPOINT	*/
Kenichi Handa's avatar
Kenichi Handa committed
580

581
#define CCL_MapMultiple 0x11	/* Mapping by multiple code conversion maps
Kenichi Handa's avatar
Kenichi Handa committed
582 583 584
					 1:ExtendedCOMMNDXXXRRRrrrXXXXX
					 2:N-2
					 3:SEPARATOR_1 (< 0)
585 586
					 4:MAP-ID_1
					 5:MAP-ID_2
Kenichi Handa's avatar
Kenichi Handa committed
587 588
					 ...
					 M:SEPARATOR_x (< 0)
589
					 M+1:MAP-ID_y
Kenichi Handa's avatar
Kenichi Handa committed
590 591 592 593
					 ...
					 N:SEPARATOR_z (< 0)
				      */

Kenichi Handa's avatar
Kenichi Handa committed
594
#define MAX_MAP_SET_LEVEL 30
Kenichi Handa's avatar
Kenichi Handa committed
595 596 597 598 599 600 601

typedef struct
{
  int rest_length;
  int orig_val;
} tr_stack;

602 603
static tr_stack mapping_stack[MAX_MAP_SET_LEVEL];
static tr_stack *mapping_stack_pointer;
Kenichi Handa's avatar
Kenichi Handa committed
604

Kenichi Handa's avatar
Kenichi Handa committed
605 606
/* If this variable is non-zero, it indicates the stack_idx
   of immediately called by CCL_MapMultiple. */
607
static int stack_idx_of_map_multiple;
Kenichi Handa's avatar
Kenichi Handa committed
608 609

#define PUSH_MAPPING_STACK(restlen, orig)		\
610 611
do							\
  {							\
Kenichi Handa's avatar
Kenichi Handa committed
612 613 614
    mapping_stack_pointer->rest_length = (restlen);	\
    mapping_stack_pointer->orig_val = (orig);		\
    mapping_stack_pointer++;				\
615 616
  }							\
while (0)
Kenichi Handa's avatar
Kenichi Handa committed
617 618

#define POP_MAPPING_STACK(restlen, orig)		\
619 620
do							\
  {							\
Kenichi Handa's avatar
Kenichi Handa committed
621 622 623
    mapping_stack_pointer--;				\
    (restlen) = mapping_stack_pointer->rest_length;	\
    (orig) = mapping_stack_pointer->orig_val;		\
624 625
  }							\
while (0)
Kenichi Handa's avatar
Kenichi Handa committed
626

Kenichi Handa's avatar
Kenichi Handa committed
627
#define CCL_CALL_FOR_MAP_INSTRUCTION(symbol, ret_ic)		\
628
do								\
629
  {								\
Kenichi Handa's avatar
Kenichi Handa committed
630 631 632 633 634 635 636 637
    struct ccl_program called_ccl;				\
    if (stack_idx >= 256					\
	|| (setup_ccl_program (&called_ccl, (symbol)) != 0))	\
      {								\
	if (stack_idx > 0)					\
	  {							\
	    ccl_prog = ccl_prog_stack_struct[0].ccl_prog;	\
	    ic = ccl_prog_stack_struct[0].ic;			\
638
	    eof_ic = ccl_prog_stack_struct[0].eof_ic;		\
Kenichi Handa's avatar
Kenichi Handa committed
639 640 641 642 643
	  }							\
	CCL_INVALID_CMD;					\
      }								\
    ccl_prog_stack_struct[stack_idx].ccl_prog = ccl_prog;	\
    ccl_prog_stack_struct[stack_idx].ic = (ret_ic);		\
644
    ccl_prog_stack_struct[stack_idx].eof_ic = eof_ic;		\
Kenichi Handa's avatar
Kenichi Handa committed
645 646 647
    stack_idx++;						\
    ccl_prog = called_ccl.prog;					\
    ic = CCL_HEADER_MAIN;					\
648
    eof_ic = XFASTINT (ccl_prog[CCL_HEADER_EOF]);		\
Kenichi Handa's avatar
Kenichi Handa committed
649
    goto ccl_repeat;						\
650
  }								\
651
while (0)
Kenichi Handa's avatar
Kenichi Handa committed
652

653
#define CCL_MapSingle		0x12 /* Map by single code conversion map
Kenichi Handa's avatar
Kenichi Handa committed
654
					1:ExtendedCOMMNDXXXRRRrrrXXXXX
655
					2:MAP-ID
Kenichi Handa's avatar
Kenichi Handa committed
656
					------------------------------
657 658
					Map reg[rrr] by MAP-ID.
					If some valid mapping is found,
Kenichi Handa's avatar
Kenichi Handa committed
659 660 661 662
					  set reg[rrr] to the result,
					else
					  set reg[RRR] to -1.
				     */
Karl Heuer's avatar
Karl Heuer committed
663

664 665
#define CCL_LookupIntConstTbl 0x13 /* Lookup multibyte character by
				      integer key.  Afterwards R7 set
Glenn Morris's avatar
Glenn Morris committed
666
				      to 1 if lookup succeeded.
667 668 669 670 671
				      1:ExtendedCOMMNDRrrRRRXXXXXXXX
				      2:ARGUMENT(Hash table ID) */

#define CCL_LookupCharConstTbl 0x14 /* Lookup integer by multibyte
				       character key.  Afterwards R7 set
Glenn Morris's avatar
Glenn Morris committed
672
				       to 1 if lookup succeeded.
673 674 675
				       1:ExtendedCOMMNDRrrRRRrrrXXXXX
				       2:ARGUMENT(Hash table ID) */

Karl Heuer's avatar
Karl Heuer committed
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
/* CCL arithmetic/logical operators. */
#define CCL_PLUS	0x00	/* X = Y + Z */
#define CCL_MINUS	0x01	/* X = Y - Z */
#define CCL_MUL		0x02	/* X = Y * Z */
#define CCL_DIV		0x03	/* X = Y / Z */
#define CCL_MOD		0x04	/* X = Y % Z */
#define CCL_AND		0x05	/* X = Y & Z */
#define CCL_OR		0x06	/* X = Y | Z */
#define CCL_XOR		0x07	/* X = Y ^ Z */
#define CCL_LSH		0x08	/* X = Y << Z */
#define CCL_RSH		0x09	/* X = Y >> Z */
#define CCL_LSH8	0x0A	/* X = (Y << 8) | Z */
#define CCL_RSH8	0x0B	/* X = Y >> 8, r[7] = Y & 0xFF  */
#define CCL_DIVMOD	0x0C	/* X = Y / Z, r[7] = Y % Z */
#define CCL_LS		0x10	/* X = (X < Y) */
#define CCL_GT		0x11	/* X = (X > Y) */
#define CCL_EQ		0x12	/* X = (X == Y) */
#define CCL_LE		0x13	/* X = (X <= Y) */
#define CCL_GE		0x14	/* X = (X >= Y) */
#define CCL_NE		0x15	/* X = (X != Y) */

697
#define CCL_DECODE_SJIS 0x16	/* X = HIGHER_BYTE (DE-SJIS (Y, Z))
Karl Heuer's avatar
Karl Heuer committed
698
				   r[7] = LOWER_BYTE (DE-SJIS (Y, Z)) */
699 700
#define CCL_ENCODE_SJIS 0x17	/* X = HIGHER_BYTE (SJIS (Y, Z))
				   r[7] = LOWER_BYTE (SJIS (Y, Z) */
Karl Heuer's avatar
Karl Heuer committed
701 702

/* Terminate CCL program successfully.  */
703
#define CCL_SUCCESS			\
704
do					\
705
  {					\
Karl Heuer's avatar
Karl Heuer committed
706
    ccl->status = CCL_STAT_SUCCESS;	\
707 708
    goto ccl_finish;			\
  }					\
Juanma Barranquero's avatar
Juanma Barranquero committed
709
while (0)
Karl Heuer's avatar
Karl Heuer committed
710 711 712 713

/* Suspend CCL program because of reading from empty input buffer or
   writing to full output buffer.  When this program is resumed, the
   same I/O command is executed.  */
714
#define CCL_SUSPEND(stat)	\
715
do				\
716
  {				\
717 718 719
    ic--;			\
    ccl->status = stat;		\
    goto ccl_finish;		\
720
  }				\
721
while (0)
Karl Heuer's avatar
Karl Heuer committed
722 723 724

/* Terminate CCL program because of invalid command.  Should not occur
   in the normal case.  */
725 726
#ifndef CCL_DEBUG

Karl Heuer's avatar
Karl Heuer committed
727
#define CCL_INVALID_CMD		     	\
728
do					\
729
  {				     	\
Karl Heuer's avatar
Karl Heuer committed
730 731
    ccl->status = CCL_STAT_INVALID_CMD;	\
    goto ccl_error_handler;	     	\
732
  }					\
Juanma Barranquero's avatar
Juanma Barranquero committed
733
while (0)
Karl Heuer's avatar
Karl Heuer committed
734

735 736
#else

Karl Heuer's avatar
Karl Heuer committed
737
#define CCL_INVALID_CMD		     	\
738
do					\
739
  {				     	\
740
    ccl_debug_hook (this_ic);		\
Karl Heuer's avatar
Karl Heuer committed
741 742
    ccl->status = CCL_STAT_INVALID_CMD;	\
    goto ccl_error_handler;	     	\
743
  }					\
Juanma Barranquero's avatar
Juanma Barranquero committed
744
while (0)
Karl Heuer's avatar
Karl Heuer committed
745

746 747
#endif

748 749 750 751
/* Use "&" rather than "&&" to suppress a bogus GCC warning; see
   <http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43772>.  */
#define ASCENDING_ORDER(lo, med, hi) (((lo) <= (med)) & ((med) <= (hi)))

Paul Eggert's avatar
Paul Eggert committed
752 753 754 755
#define GET_CCL_RANGE(var, ccl_prog, ic, lo, hi)		\
  do								\
    {								\
      EMACS_INT prog_word = XINT ((ccl_prog)[ic]);		\
756
      if (! ASCENDING_ORDER (lo, prog_word, hi))		\
Paul Eggert's avatar
Paul Eggert committed
757 758 759 760 761 762
	CCL_INVALID_CMD;					\
      (var) = prog_word;					\
    }								\
  while (0)

#define GET_CCL_CODE(code, ccl_prog, ic)			\
763
  GET_CCL_RANGE (code, ccl_prog, ic, CCL_CODE_MIN, CCL_CODE_MAX)
Paul Eggert's avatar
Paul Eggert committed
764 765 766 767

#define GET_CCL_INT(var, ccl_prog, ic)				\
  GET_CCL_RANGE (var, ccl_prog, ic, INT_MIN, INT_MAX)

768
#define IN_INT_RANGE(val) ASCENDING_ORDER (INT_MIN, val, INT_MAX)
Paul Eggert's avatar
Paul Eggert committed
769

Karl Heuer's avatar
Karl Heuer committed
770
/* Encode one character CH to multibyte form and write to the current
771
   output buffer.  If CH is less than 256, CH is written as is.  */
Kenichi Handa's avatar
Kenichi Handa committed
772 773 774 775 776 777 778 779
#define CCL_WRITE_CHAR(ch)			\
  do {						\
    if (! dst)					\
      CCL_INVALID_CMD;				\
    else if (dst < dst_end)			\
      *dst++ = (ch);				\
    else					\
      CCL_SUSPEND (CCL_STAT_SUSPEND_BY_DST);	\
780 781
  } while (0)

Karl Heuer's avatar
Karl Heuer committed
782 783
/* Write a string at ccl_prog[IC] of length LEN to the current output
   buffer.  */
784 785
#define CCL_WRITE_STRING(len)					\
  do {								\
786
    int ccli;							\
787 788 789 790 791
    if (!dst)							\
      CCL_INVALID_CMD;						\
    else if (dst + len <= dst_end)				\
      {								\
	if (XFASTINT (ccl_prog[ic]) & 0x1000000)		\
792 793
	  for (ccli = 0; ccli < len; ccli++)			\
	    *dst++ = XFASTINT (ccl_prog[ic + ccli]) & 0xFFFFFF;	\
794
	else							\
795 796 797
	  for (ccli = 0; ccli < len; ccli++)			\
	    *dst++ = ((XFASTINT (ccl_prog[ic + (ccli / 3)]))	\
		      >> ((2 - (ccli % 3)) * 8)) & 0xFF;	\
798 799 800
      }								\
    else							\
      CCL_SUSPEND (CCL_STAT_SUSPEND_BY_DST);			\
Karl Heuer's avatar
Karl Heuer committed
801 802
  } while (0)

Kenichi Handa's avatar
Kenichi Handa committed
803 804 805 806 807 808 809 810 811
/* Read one byte from the current input buffer into Rth register.  */
#define CCL_READ_CHAR(r)			\
  do {						\
    if (! src)					\
      CCL_INVALID_CMD;				\
    else if (src < src_end)			\
      r = *src++;				\
    else if (ccl->last_block)			\
      {						\
812
	r = -1;					\
Kenichi Handa's avatar
Kenichi Handa committed
813 814 815 816 817 818 819
	ic = ccl->eof_ic;			\
	goto ccl_repeat;			\
      }						\
    else					\
      CCL_SUSPEND (CCL_STAT_SUSPEND_BY_SRC);	\
    } while (0)

820 821 822 823 824 825 826 827 828
/* Decode CODE by a charset whose id is ID.  If ID is 0, return CODE
   as is for backward compatibility.  Assume that we can use the
   variable `charset'.  */

#define CCL_DECODE_CHAR(id, code)	\
  ((id) == 0 ? (code)			\
   : (charset = CHARSET_FROM_ID ((id)), DECODE_CHAR (charset, (code))))

/* Encode character C by some of charsets in CHARSET_LIST.  Set ID to
Paul Eggert's avatar
Paul Eggert committed
829
   the id of the used charset, ENCODED to the result of encoding.
830 831
   Assume that we can use the variable `charset'.  */

Kenichi Handa's avatar
Kenichi Handa committed
832
#define CCL_ENCODE_CHAR(c, charset_list, id, encoded)		\
Kenichi Handa's avatar
Kenichi Handa committed
833
  do {								\
834
    unsigned ncode;						\
Kenichi Handa's avatar
Kenichi Handa committed
835
								\
836
    charset = char_charset ((c), (charset_list), &ncode);	\
Kenichi Handa's avatar
Kenichi Handa committed
837
    if (! charset && ! NILP (charset_list))			\
838
      charset = char_charset ((c), Qnil, &ncode);	  	\
Kenichi Handa's avatar
Kenichi Handa committed
839 840 841
    if (charset)						\
      {								\
	(id) = CHARSET_ID (charset);				\
842
	(encoded) = ncode;					\
Kenichi Handa's avatar
Kenichi Handa committed
843
      }								\
Kenichi Handa's avatar
Kenichi Handa committed
844
   } while (0)
Kenichi Handa's avatar
Kenichi Handa committed
845

Kenichi Handa's avatar
Kenichi Handa committed
846 847 848 849 850 851 852
/* Execute CCL code on characters at SOURCE (length SRC_SIZE).  The
   resulting text goes to a place pointed by DESTINATION, the length
   of which should not exceed DST_SIZE.  As a side effect, how many
   characters are consumed and produced are recorded in CCL->consumed
   and CCL->produced, and the contents of CCL registers are updated.
   If SOURCE or DESTINATION is NULL, only operations on registers are
   permitted.  */
Karl Heuer's avatar
Karl Heuer committed
853 854 855

#ifdef CCL_DEBUG
#define CCL_DEBUG_BACKTRACE_LEN 256
Dave Love's avatar
Dave Love committed
856
int ccl_backtrace_table[CCL_DEBUG_BACKTRACE_LEN];
Karl Heuer's avatar
Karl Heuer committed
857
int ccl_backtrace_idx;
858 859 860 861 862 863 864

int
ccl_debug_hook (int ic)
{
  return ic;
}

Karl Heuer's avatar
Karl Heuer committed
865 866 867 868
#endif

struct ccl_prog_stack
  {
869
    Lisp_Object *ccl_prog;	/* Pointer to an array of CCL code.  */
Karl Heuer's avatar
Karl Heuer committed
870
    int ic;			/* Instruction Counter.  */
871
    int eof_ic;			/* Instruction Counter to jump on EOF.  */
Karl Heuer's avatar
Karl Heuer committed
872 873
  };

874
/* For the moment, we only support depth 256 of stack.  */
875 876
static struct ccl_prog_stack ccl_prog_stack_struct[256];

Kenichi Handa's avatar
Kenichi Handa committed
877
void
878
ccl_driver (struct ccl_program *ccl, int *source, int *destination, int src_size, int dst_size, Lisp_Object charset_list)
Karl Heuer's avatar
Karl Heuer committed
879 880 881
{
  register int *reg = ccl->reg;
  register int ic = ccl->ic;
882
  register int code = 0, field1, field2;
883
  register Lisp_Object *ccl_prog = ccl->prog;
Kenichi Handa's avatar
Kenichi Handa committed
884 885
  int *src = source, *src_end = src + src_size;
  int *dst = destination, *dst_end = dst + dst_size;
Karl Heuer's avatar
Karl Heuer committed
886
  int jump_address;
887
  int i = 0, j, op;
888
  int stack_idx = ccl->stack_idx;
889
  /* Instruction counter of the current CCL code. */
890
  int this_ic = 0;
Kenichi Handa's avatar
Kenichi Handa committed
891
  struct charset *charset;
892 893
  int eof_ic = ccl->eof_ic;
  int eof_hit = 0;
Karl Heuer's avatar
Karl Heuer committed
894

Kenichi Handa's avatar
Kenichi Handa committed
895
  if (ccl->buf_magnification == 0) /* We can't read/produce any bytes.  */
896 897
    dst = NULL;

Kenichi Handa's avatar
Kenichi Handa committed
898 899 900
  /* Set mapping stack pointer. */
  mapping_stack_pointer = mapping_stack;

Karl Heuer's avatar
Karl Heuer committed
901 902 903 904 905 906
#ifdef CCL_DEBUG
  ccl_backtrace_idx = 0;
#endif

  for (;;)
    {
907
    ccl_repeat:
Karl Heuer's avatar
Karl Heuer committed
908 909 910 911 912 913 914 915 916 917 918 919
#ifdef CCL_DEBUG
      ccl_backtrace_table[ccl_backtrace_idx++] = ic;
      if (ccl_backtrace_idx >= CCL_DEBUG_BACKTRACE_LEN)
	ccl_backtrace_idx = 0;
      ccl_backtrace_table[ccl_backtrace_idx] = 0;
#endif

      if (!NILP (Vquit_flag) && NILP (Vinhibit_quit))
	{
	  /* We can't just signal Qquit, instead break the loop as if
             the whole data is processed.  Don't reset Vquit_flag, it
             must be handled later at a safer place.  */
Kenichi Handa's avatar
Kenichi Handa committed
920 921
	  if (src)
	    src = source + src_size;
Karl Heuer's avatar
Karl Heuer committed
922 923 924 925
	  ccl->status = CCL_STAT_QUIT;
	  break;
	}

926
      this_ic = ic;
Paul Eggert's avatar
Paul Eggert committed
927
      GET_CCL_CODE (code, ccl_prog, ic++);
Karl Heuer's avatar
Karl Heuer committed
928 929 930 931 932 933 934
      field1 = code >> 8;
      field2 = (code & 0xFF) >> 5;

#define rrr field2
#define RRR (field1 & 7)
#define Rrr ((field1 >> 3) & 7)
#define ADDR field1
935
#define EXCMD (field1 >> 6)
Karl Heuer's avatar
Karl Heuer committed
936 937 938 939 940 941 942 943 944 945 946 947

      switch (code & 0x1F)
	{
	case CCL_SetRegister:	/* 00000000000000000RRRrrrXXXXX */
	  reg[rrr] = reg[RRR];
	  break;

	case CCL_SetShortConst:	/* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
	  reg[rrr] = field1;
	  break;

	case CCL_SetConst:	/* 00000000000000000000rrrXXXXX */
Paul Eggert's avatar
Paul Eggert committed
948
	  GET_CCL_INT (reg[rrr], ccl_prog, ic++);
Karl Heuer's avatar
Karl Heuer committed
949 950 951 952 953
	  break;

	case CCL_SetArray:	/* CCCCCCCCCCCCCCCCCCCCRRRrrrXXXXX */
	  i = reg[RRR];
	  j = field1 >> 3;
954
	  if (0 <= i && i < j)
Paul Eggert's avatar
Paul Eggert committed
955
	    GET_CCL_INT (reg[rrr], ccl_prog, ic + i);
Karl Heuer's avatar
Karl Heuer committed
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
	  ic += j;
	  break;

	case CCL_Jump:		/* A--D--D--R--E--S--S-000XXXXX */
	  ic += ADDR;
	  break;

	case CCL_JumpCond:	/* A--D--D--R--E--S--S-rrrXXXXX */
	  if (!reg[rrr])
	    ic += ADDR;
	  break;

	case CCL_WriteRegisterJump: /* A--D--D--R--E--S--S-rrrXXXXX */
	  i = reg[rrr];
	  CCL_WRITE_CHAR (i);
	  ic += ADDR;
	  break;

	case CCL_WriteRegisterReadJump: /* A--D--D--R--E--S--S-rrrXXXXX */
	  i = reg[rrr];
	  CCL_WRITE_CHAR (i);
	  ic++;
	  CCL_READ_CHAR (reg[rrr]);
	  ic += ADDR - 1;
	  break;

	case CCL_WriteConstJump: /* A--D--D--R--E--S--S-000XXXXX */
Paul Eggert's avatar
Paul Eggert committed
983
	  GET_CCL_INT (i, ccl_prog, ic);
Karl Heuer's avatar
Karl Heuer committed
984 985 986 987 988
	  CCL_WRITE_CHAR (i);
	  ic += ADDR;
	  break;

	case CCL_WriteConstReadJump: /* A--D--D--R--E--S--S-rrrXXXXX */
Paul Eggert's avatar
Paul Eggert committed
989
	  GET_CCL_INT (i, ccl_prog, ic);
Karl Heuer's avatar
Karl Heuer committed
990 991 992 993 994 995 996
	  CCL_WRITE_CHAR (i);
	  ic++;
	  CCL_READ_CHAR (reg[rrr]);
	  ic += ADDR - 1;
	  break;

	case CCL_WriteStringJump: /* A--D--D--R--E--S--S-000XXXXX */
Paul Eggert's avatar
Paul Eggert committed
997
	  GET_CCL_INT (j, ccl_prog, ic++);
Karl Heuer's avatar
Karl Heuer committed
998 999 1000 1001 1002 1003
	  CCL_WRITE_STRING (j);
	  ic += ADDR - 1;
	  break;

	case CCL_WriteArrayReadJump: /* A--D--D--R--E--S--S-rrrXXXXX */
	  i = reg[rrr];
Paul Eggert's avatar
Paul Eggert committed
1004
	  GET_CCL_INT (j, ccl_prog, ic);
1005
	  if (0 <= i && i < j)
Karl Heuer's avatar
Karl Heuer committed
1006
	    {
Paul Eggert's avatar
Paul Eggert committed
1007
	      GET_CCL_INT (i, ccl_prog, ic + 1 + i);
Karl Heuer's avatar
Karl Heuer committed
1008 1009
	      CCL_WRITE_CHAR (i);
	    }
1010
	  ic += j + 2;
Karl Heuer's avatar
Karl Heuer committed
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
	  CCL_READ_CHAR (reg[rrr]);
	  ic += ADDR - (j + 2);
	  break;

	case CCL_ReadJump:	/* A--D--D--R--E--S--S-rrrYYYYY */
	  CCL_READ_CHAR (reg[rrr]);
	  ic += ADDR;
	  break;

	case CCL_ReadBranch:	/* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
	  CCL_READ_CHAR (reg[rrr]);
	  /* fall through ... */
	case CCL_Branch:	/* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
Paul Eggert's avatar
Paul Eggert committed
1024 1025 1026
	{
	  int incr;
	  GET_CCL_INT (incr, ccl_prog,
1027
		       ic + (0 <= reg[rrr] && reg[rrr] < field1
Paul Eggert's avatar
Paul Eggert committed
1028 1029 1030 1031
			     ? reg[rrr]
			     : field1));
	  ic += incr;
	}
Karl Heuer's avatar
Karl Heuer committed
1032 1033 1034 1035 1036 1037 1038
	  break;

	case CCL_ReadRegister:	/* CCCCCCCCCCCCCCCCCCCCrrXXXXX */
	  while (1)
	    {
	      CCL_READ_CHAR (reg[rrr]);
	      if (!field1) break;
Paul Eggert's avatar
Paul Eggert committed
1039
	      GET_CCL_CODE (code, ccl_prog, ic++);
Karl Heuer's avatar
Karl Heuer committed
1040 1041 1042 1043 1044 1045 1046 1047
	      field1 = code >> 8;
	      field2 = (code & 0xFF) >> 5;
	    }
	  break;

	case CCL_WriteExprConst:  /* 1:00000OPERATION000RRR000XXXXX */
	  rrr = 7;
	  i = reg[RRR];
Paul Eggert's avatar
Paul Eggert committed
1048
	  GET_CCL_INT (j, ccl_prog, ic);
Karl Heuer's avatar
Karl Heuer committed
1049
	  op = field1 >> 6;
1050
	  jump_address = ic + 1;
Karl Heuer's avatar
Karl Heuer committed
1051 1052 1053 1054 1055 1056 1057 1058
	  goto ccl_set_expr;

	case CCL_WriteRegister:	/* CCCCCCCCCCCCCCCCCCCrrrXXXXX */
	  while (1)
	    {
	      i = reg[rrr];
	      CCL_WRITE_CHAR (i);
	      if (!field1) break;
Paul Eggert's avatar
Paul Eggert committed
1059
	      GET_CCL_CODE (code, ccl_prog, ic++);
Karl Heuer's avatar
Karl Heuer committed
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
	      field1 = code >> 8;
	      field2 = (code & 0xFF) >> 5;
	    }
	  break;

	case CCL_WriteExprRegister: /* 1:00000OPERATIONRrrRRR000XXXXX */
	  rrr = 7;
	  i = reg[RRR];
	  j = reg[Rrr];
	  op = field1 >> 6;
1070
	  jump_address = ic;
Karl Heuer's avatar
Karl Heuer committed
1071 1072
	  goto ccl_set_expr;

1073
	case CCL_Call:		/* 1:CCCCCCCCCCCCCCCCCCCCFFFXXXXX */
Karl Heuer's avatar
Karl Heuer committed
1074 1075
	  {
	    Lisp_Object slot;
1076 1077 1078 1079 1080
	    int prog_id;

	    /* If FFF is nonzero, the CCL program ID is in the
               following code.  */
	    if (rrr)
Paul Eggert's avatar
Paul Eggert committed
1081
	      GET_CCL_INT (prog_id, ccl_prog, ic++);
1082 1083
	    else
	      prog_id = field1;
Karl Heuer's avatar
Karl Heuer committed
1084 1085

	    if (stack_idx >= 256
1086
		|| prog_id < 0
Stefan Monnier's avatar
Stefan Monnier committed
1087 1088 1089
		|| prog_id >= ASIZE (Vccl_program_table)
		|| (slot = AREF (Vccl_program_table, prog_id), !VECTORP (slot))
		|| !VECTORP (AREF (slot, 1)))
Karl Heuer's avatar
Karl Heuer committed
1090 1091 1092 1093 1094
	      {
		if (stack_idx > 0)
		  {
		    ccl_prog = ccl_prog_stack_struct[0].ccl_prog;
		    ic = ccl_prog_stack_struct[0].ic;
1095
		    eof_ic = ccl_prog_stack_struct[0].eof_ic;
Karl Heuer's avatar
Karl Heuer committed
1096 1097 1098
		  }
		CCL_INVALID_CMD;
	      }
1099

Karl Heuer's avatar
Karl Heuer committed
1100 1101
	    ccl_prog_stack_struct[stack_idx].ccl_prog = ccl_prog;
	    ccl_prog_stack_struct[stack_idx].ic = ic;
1102
	    ccl_prog_stack_struct[stack_idx].eof_ic = eof_ic;
Karl Heuer's avatar
Karl Heuer committed
1103
	    stack_idx++;
Stefan Monnier's avatar
Stefan Monnier committed
1104
	    ccl_prog = XVECTOR (AREF (slot, 1))->contents;
Karl Heuer's avatar
Karl Heuer committed
1105
	    ic = CCL_HEADER_MAIN;
1106
	    eof_ic = XFASTINT (ccl_prog[CCL_HEADER_EOF]);
Karl Heuer's avatar
Karl Heuer committed
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
	  }
	  break;

	case CCL_WriteConstString: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
	  if (!rrr)
	    CCL_WRITE_CHAR (field1);
	  else
	    {
	      CCL_WRITE_STRING (field1);
	      ic += (field1 + 2) / 3;
	    }
	  break;

	case CCL_WriteArray:	/* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
	  i = reg[rrr];
1122
	  if (0 <= i && i < field1)
Karl Heuer's avatar
Karl Heuer committed
1123
	    {
Paul Eggert's avatar
Paul Eggert committed
1124
	      GET_CCL_INT (j, ccl_prog, ic + i);
Karl Heuer's avatar
Karl Heuer committed
1125 1126 1127 1128 1129 1130
	      CCL_WRITE_CHAR (j);
	    }
	  ic += field1;
	  break;

	case CCL_End:		/* 0000000000000000000000XXXXX */
1131
	  if (stack_idx > 0)
Karl Heuer's avatar
Karl Heuer committed
1132
	    {
1133
	      stack_idx--;
Karl Heuer's avatar
Karl Heuer committed
1134 1135
	      ccl_prog = ccl_prog_stack_struct[stack_idx].ccl_prog;
	      ic = ccl_prog_stack_struct[stack_idx].ic;
1136 1137 1138
	      eof_ic = ccl_prog_stack_struct[stack_idx].eof_ic;
	      if (eof_hit)
		ic = eof_ic;
Karl Heuer's avatar
Karl Heuer committed
1139 1140
	      break;
	    }
1141 1142 1143 1144 1145
	  if (src)
	    src = src_end;
	  /* ccl->ic should points to this command code again to
             suppress further processing.  */
	  ic--;
Karl Heuer's avatar
Karl Heuer committed
1146 1147 1148
	  CCL_SUCCESS;

	case CCL_ExprSelfConst: /* 00000OPERATION000000rrrXXXXX */
Paul Eggert's avatar
Paul Eggert committed
1149
	  GET_CCL_INT (i, ccl_prog, ic++);
Karl Heuer's avatar
Karl Heuer committed
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
	  op = field1 >> 6;
	  goto ccl_expr_self;

	case CCL_ExprSelfReg:	/* 00000OPERATION000RRRrrrXXXXX */
	  i = reg[RRR];
	  op = field1 >> 6;

	ccl_expr_self:
	  switch (op)
	    {
	    case CCL_PLUS: reg[rrr] += i; break;
	    case CCL_MINUS: reg[rrr] -= i; break;
	    case CCL_MUL: reg[rrr] *= i; break;
	    case CCL_DIV: reg[rrr] /= i; break;
	    case CCL_MOD: reg[rrr] %= i; break;
	    case CCL_AND: reg[rrr] &= i; break;
	    case CCL_OR: reg[rrr] |= i; break;
	    case CCL_XOR: reg[rrr] ^= i; break;
	    case CCL_LSH: reg[rrr] <<= i; break;
	    case CCL_RSH: reg[rrr] >>= i; break;
	    case CCL_LSH8: reg[rrr] <<= 8; reg[rrr] |= i; break;
	    case CCL_RSH8: reg[7] = reg[rrr] & 0xFF; reg[rrr] >>= 8; break;
	    case CCL_DIVMOD: reg[7] = reg[rrr] % i; reg[rrr] /= i; break;
	    case CCL_LS: reg[rrr] = reg[rrr] < i; break;
	    case CCL_GT: reg[rrr] = reg[rrr] > i; break;
	    case CCL_EQ: reg[rrr] = reg[rrr] == i; break;
	    case CCL_LE: reg[rrr] = reg[rrr] <= i; break;
	    case CCL_GE: reg[rrr] = reg[rrr] >= i; break;
	    case CCL_NE: reg[rrr] = reg[rrr] != i; break;
	    default: CCL_INVALID_CMD;
	    }
	  break;

	case CCL_SetExprConst:	/* 00000OPERATION000RRRrrrXXXXX */
	  i = reg[RRR];
Paul Eggert's avatar
Paul Eggert committed
1185
	  GET_CCL_INT (j, ccl_prog, ic++);
Karl Heuer's avatar
Karl Heuer committed
1186
	  op = field1 >> 6;
Paul Eggert's avatar
Paul Eggert committed
1187
	  jump_address = ic;
Karl Heuer's avatar
Karl Heuer committed
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
	  goto ccl_set_expr;

	case CCL_SetExprReg:	/* 00000OPERATIONRrrRRRrrrXXXXX */
	  i = reg[RRR];
	  j = reg[Rrr];
	  op = field1 >> 6;
	  jump_address = ic;
	  goto ccl_set_expr;

	case CCL_ReadJumpCondExprConst: /* A--D--D--R--E--S--S-rrrXXXXX */
	  CCL_READ_CHAR (reg[rrr]);
	case CCL_JumpCondExprConst: /* A--D--D--R--E--S--S-rrrXXXXX */
	  i = reg[rrr];
Paul Eggert's avatar
Paul Eggert committed
1201 1202 1203
	  jump_address = ic + ADDR;
	  GET_CCL_INT (op, ccl_prog, ic++);
	  GET_CCL_INT (j, ccl_prog, ic++);
Karl Heuer's avatar
Karl Heuer committed
1204 1205 1206 1207 1208 1209 1210
	  rrr = 7;
	  goto ccl_set_expr;

	case CCL_ReadJumpCondExprReg: /* A--D--D--R--E--S--S-rrrXXXXX */
	  CCL_READ_CHAR (reg[rrr]);
	case CCL_JumpCondExprReg:
	  i = reg[rrr];
Paul Eggert's avatar
Paul Eggert committed
1211 1212 1213 1214
	  jump_address = ic + ADDR;
	  GET_CCL_INT (op, ccl_prog, ic++);
	  GET_CCL_RANGE (j, ccl_prog, ic++, 0, 7);
	  j = reg[j];
Karl Heuer's avatar
Karl Heuer committed
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
	  rrr = 7;

	ccl_set_expr:
	  switch (op)
	    {
	    case CCL_PLUS: reg[rrr] = i + j; break;
	    case CCL_MINUS: reg[rrr] = i - j; break;
	    case CCL_MUL: reg[rrr] = i * j; break;
	    case CCL_DIV: reg[rrr] = i / j; break;
	    case CCL_MOD: reg[rrr] = i % j; break;
	    case CCL_AND: reg[rrr] = i & j; break;
	    case CCL_OR: reg[rrr] = i | j; break;
1227
	    case CC