ccl.c 63.9 KB
Newer Older
Karl Heuer's avatar
Karl Heuer committed
1
/* CCL (Code Conversion Language) interpreter.
Paul Eggert's avatar
Paul Eggert committed
2
   Copyright (C) 2001-2020 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 <https://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>
Eli Zaretskii's avatar
Eli Zaretskii committed
29
#include <limits.h>
30

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

37
/* Table of registered CCL programs.  Each element is a vector of
38 39 40 41 42 43
   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.  */
44
static Lisp_Object Vccl_program_table;
Karl Heuer's avatar
Karl Heuer committed
45

46 47
/* Return a hash table of id number ID.  */
#define GET_HASH_TABLE(id) \
48
  (XHASH_TABLE (XCDR (AREF (Vtranslation_hash_table_vector, (id)))))
49

Karl Heuer's avatar
Karl Heuer committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
/* 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

66 67
/* 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
68 69 70 71 72 73 74 75 76 77 78 79

	|----------------- 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

80 81
   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
82 83 84
   and `rrr' are CCL register number, `XXXXX' is one of the following
   CCL commands.  */

Paul Eggert's avatar
Paul Eggert committed
85
#define CCL_CODE_MAX ((1 << (28 - 1)) - 1)
86
#define CCL_CODE_MIN (-1 - CCL_CODE_MAX)
Paul Eggert's avatar
Paul Eggert committed
87

Karl Heuer's avatar
Karl Heuer committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 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
/* 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
184
					3:000MSTRIN[0]STRIN[1]STRIN[2]
Karl Heuer's avatar
Karl Heuer committed
185 186
					...
					------------------------------
187 188 189 190
					if (M)
					  write_multibyte_string (STRING, LENGTH);
					else
					  write_string (STRING, LENGTH);
Karl Heuer's avatar
Karl Heuer committed
191 192 193 194 195 196
					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
197 198
					3:ELEMENT[0]
					4:ELEMENT[1]
Karl Heuer's avatar
Karl Heuer committed
199 200 201 202 203 204 205 206 207 208
					...
					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
209
   Nth code (YYYYY == CCL_ReadJump).  */
Karl Heuer's avatar
Karl Heuer committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 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

#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]);
					*/

283
#define CCL_Call		0x13 /* Call the CCL program whose ID is
284 285 286
					CC..C or cc..c.
					1:CCCCCCCCCCCCCCCCCCCCFFFXXXXX
					[2:00000000cccccccccccccccccccc]
Karl Heuer's avatar
Karl Heuer committed
287
					------------------------------
288 289 290 291 292
					if (FFF)
					  call (cc..c)
					  IC++;
					else
					  call (CC..C)
Karl Heuer's avatar
Karl Heuer committed
293 294 295 296
					*/

#define CCL_WriteConstString	0x14 /* Write a constant or a string:
					1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
297
					[2:000MSTRIN[0]STRIN[1]STRIN[2]]
Karl Heuer's avatar
Karl Heuer committed
298 299 300 301 302
					[...]
					-----------------------------
					if (!rrr)
					  write (CC..C)
					else
303 304 305 306
					  if (M)
					    write_multibyte_string (STRING, CC..C);
					  else
					    write_string (STRING, CC..C);
Karl Heuer's avatar
Karl Heuer committed
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 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
					  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;
					*/

414
#define CCL_Extension		0x1F /* Extended CCL code
Karl Heuer's avatar
Karl Heuer committed
415
					1:ExtendedCOMMNDRrrRRRrrrXXXXX
Paul Eggert's avatar
Paul Eggert committed
416
					2:ARGUMENT
Karl Heuer's avatar
Karl Heuer committed
417 418 419 420 421
					3:...
					------------------------------
					extended_command (rrr,RRR,Rrr,ARGS)
				      */

422
/*
Kenichi Handa's avatar
Kenichi Handa committed
423
   Here after, Extended CCL Instructions.
424
   Bit length of extended command is 14.
Kenichi Handa's avatar
Kenichi Handa committed
425
   Therefore, the instruction code range is 0..16384(0x3fff).
426 427
 */

428
/* Read a multibyte character.
Kenichi Handa's avatar
Kenichi Handa committed
429 430 431 432 433 434 435 436 437 438 439 440 441
   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  */

442
/* Translate a character whose code point is reg[rrr] and the charset
443
   ID is reg[RRR] by a translation table whose ID is reg[Rrr].
Kenichi Handa's avatar
Kenichi Handa committed
444

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

448
#define CCL_TranslateCharacter	0x02 /* Translate a multibyte character
Kenichi Handa's avatar
Kenichi Handa committed
449 450
					1:ExtendedCOMMNDRrrRRRrrrXXXXX  */

451
/* Translate a character whose code point is reg[rrr] and the charset
452
   ID is reg[RRR] by a translation table whose ID is ARGUMENT.
Kenichi Handa's avatar
Kenichi Handa committed
453

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

457 458 459 460
#define CCL_TranslateCharacterConstTbl 0x03 /* Translate a multibyte character
					       1:ExtendedCOMMNDRrrRRRrrrXXXXX
					       2:ARGUMENT(Translation Table ID)
					    */
Kenichi Handa's avatar
Kenichi Handa committed
461

462 463
/* 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
464

465
   Each MAP is a Lisp vector whose element is number, nil, t, or
Kenichi Handa's avatar
Kenichi Handa committed
466
   lambda.
467
   If the element is nil, ignore the map and proceed to the next map.
Kenichi Handa's avatar
Kenichi Handa committed
468 469 470
   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
471
   Detail of the map structure is described in the comment for
472
   CCL_MapMultiple below.  */
Kenichi Handa's avatar
Kenichi Handa committed
473

474
#define CCL_IterateMultipleMap	0x10 /* Iterate multiple maps
Kenichi Handa's avatar
Kenichi Handa committed
475
					1:ExtendedCOMMNDXXXRRRrrrXXXXX
476 477 478
					2:NUMBER of MAPs
					3:MAP-ID1
					4:MAP-ID2
Kenichi Handa's avatar
Kenichi Handa committed
479
					...
480
				     */
Kenichi Handa's avatar
Kenichi Handa committed
481

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

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

487 488 489 490 491 492 493 494
   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
495
   the compiled CCL codes has this sequence:
496
	CCL_MapMultiple (CCL code of this command)
497 498
	16 (total number of MAPs and SEPARATORs)
	-7 (1st SEPARATOR)
499 500
	MAP-ID11
	MAP-ID12
501
	-3 (2nd SEPARATOR)
502 503 504 505
	MAP-ID121
	MAP-ID122
	MAP-ID123
	MAP-ID13
506
	-7 (3rd SEPARATOR)
507
	MAP-ID21
508
	-4 (4th SEPARATOR)
509
	MAP-ID211
510
	-1 (5th SEPARATOR)
511 512 513
	MAP_ID2111
	MAP-ID212
	MAP-ID22
Kenichi Handa's avatar
Kenichi Handa committed
514

515
   A value of each SEPARATOR follows this rule:
516 517
	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
518

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

521 522
   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
523

524
   The mapping is iterated for all maps in each map set (set of maps
525 526 527 528 529 530
   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
531 532 533 534
   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.
535 536 537 538

   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
539
   If VALn is nil, the last map is ignored and the mapping of VALm
540 541 542 543 544
   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
545 546 547
   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
548
   If VALn is a symbol, call the CCL program referred by it.
Kenichi Handa's avatar
Kenichi Handa committed
549 550
   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
551

552
   Each map is a Lisp vector of the following format (a) or (b):
Kenichi Handa's avatar
Kenichi Handa committed
553 554 555
	(a)......[STARTPOINT VAL1 VAL2 ...]
	(b)......[t VAL STARTPOINT ENDPOINT],
   where
556
	STARTPOINT is an offset to be used for indexing a map,
557
	ENDPOINT is a maximum index number of a map,
558
	VAL and VALn is a number, nil, t, or lambda.
Kenichi Handa's avatar
Kenichi Handa committed
559

560 561 562
   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:
563
	STARTPOINT <= index < ENDPOINT	*/
Kenichi Handa's avatar
Kenichi Handa committed
564

565
#define CCL_MapMultiple 0x11	/* Mapping by multiple code conversion maps
Kenichi Handa's avatar
Kenichi Handa committed
566 567 568
					 1:ExtendedCOMMNDXXXRRRrrrXXXXX
					 2:N-2
					 3:SEPARATOR_1 (< 0)
569 570
					 4:MAP-ID_1
					 5:MAP-ID_2
Kenichi Handa's avatar
Kenichi Handa committed
571 572
					 ...
					 M:SEPARATOR_x (< 0)
573
					 M+1:MAP-ID_y
Kenichi Handa's avatar
Kenichi Handa committed
574 575 576 577
					 ...
					 N:SEPARATOR_z (< 0)
				      */

Kenichi Handa's avatar
Kenichi Handa committed
578
#define MAX_MAP_SET_LEVEL 30
Kenichi Handa's avatar
Kenichi Handa committed
579 580 581 582 583 584 585

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

586 587
static tr_stack mapping_stack[MAX_MAP_SET_LEVEL];
static tr_stack *mapping_stack_pointer;
Kenichi Handa's avatar
Kenichi Handa committed
588

Kenichi Handa's avatar
Kenichi Handa committed
589 590
/* If this variable is non-zero, it indicates the stack_idx
   of immediately called by CCL_MapMultiple. */
591
static int stack_idx_of_map_multiple;
Kenichi Handa's avatar
Kenichi Handa committed
592 593

#define PUSH_MAPPING_STACK(restlen, orig)		\
594 595
do							\
  {							\
Kenichi Handa's avatar
Kenichi Handa committed
596 597 598
    mapping_stack_pointer->rest_length = (restlen);	\
    mapping_stack_pointer->orig_val = (orig);		\
    mapping_stack_pointer++;				\
599 600
  }							\
while (0)
Kenichi Handa's avatar
Kenichi Handa committed
601 602

#define POP_MAPPING_STACK(restlen, orig)		\
603 604
do							\
  {							\
Kenichi Handa's avatar
Kenichi Handa committed
605 606 607
    mapping_stack_pointer--;				\
    (restlen) = mapping_stack_pointer->rest_length;	\
    (orig) = mapping_stack_pointer->orig_val;		\
608 609
  }							\
while (0)
Kenichi Handa's avatar
Kenichi Handa committed
610

Kenichi Handa's avatar
Kenichi Handa committed
611
#define CCL_CALL_FOR_MAP_INSTRUCTION(symbol, ret_ic)		\
612
do								\
613
  {								\
Kenichi Handa's avatar
Kenichi Handa committed
614 615
    struct ccl_program called_ccl;				\
    if (stack_idx >= 256					\
616
	|| ! setup_ccl_program (&called_ccl, (symbol)))		\
Kenichi Handa's avatar
Kenichi Handa committed
617 618 619 620 621
      {								\
	if (stack_idx > 0)					\
	  {							\
	    ccl_prog = ccl_prog_stack_struct[0].ccl_prog;	\
	    ic = ccl_prog_stack_struct[0].ic;			\
622
	    eof_ic = ccl_prog_stack_struct[0].eof_ic;		\
Kenichi Handa's avatar
Kenichi Handa committed
623 624 625 626 627
	  }							\
	CCL_INVALID_CMD;					\
      }								\
    ccl_prog_stack_struct[stack_idx].ccl_prog = ccl_prog;	\
    ccl_prog_stack_struct[stack_idx].ic = (ret_ic);		\
628
    ccl_prog_stack_struct[stack_idx].eof_ic = eof_ic;		\
Kenichi Handa's avatar
Kenichi Handa committed
629 630 631
    stack_idx++;						\
    ccl_prog = called_ccl.prog;					\
    ic = CCL_HEADER_MAIN;					\
Tom Tromey's avatar
Tom Tromey committed
632
    eof_ic = XFIXNAT (ccl_prog[CCL_HEADER_EOF]);		\
Kenichi Handa's avatar
Kenichi Handa committed
633
    goto ccl_repeat;						\
634
  }								\
635
while (0)
Kenichi Handa's avatar
Kenichi Handa committed
636

637
#define CCL_MapSingle		0x12 /* Map by single code conversion map
Kenichi Handa's avatar
Kenichi Handa committed
638
					1:ExtendedCOMMNDXXXRRRrrrXXXXX
639
					2:MAP-ID
Kenichi Handa's avatar
Kenichi Handa committed
640
					------------------------------
641 642
					Map reg[rrr] by MAP-ID.
					If some valid mapping is found,
Kenichi Handa's avatar
Kenichi Handa committed
643 644 645 646
					  set reg[rrr] to the result,
					else
					  set reg[RRR] to -1.
				     */
Karl Heuer's avatar
Karl Heuer committed
647

648 649
#define CCL_LookupIntConstTbl 0x13 /* Lookup multibyte character by
				      integer key.  Afterwards R7 set
Glenn Morris's avatar
Glenn Morris committed
650
				      to 1 if lookup succeeded.
651 652 653 654 655
				      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
656
				       to 1 if lookup succeeded.
657 658 659
				       1:ExtendedCOMMNDRrrRRRrrrXXXXX
				       2:ARGUMENT(Hash table ID) */

Karl Heuer's avatar
Karl Heuer committed
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
/* 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) */

681
#define CCL_DECODE_SJIS 0x16	/* X = HIGHER_BYTE (DE-SJIS (Y, Z))
Karl Heuer's avatar
Karl Heuer committed
682
				   r[7] = LOWER_BYTE (DE-SJIS (Y, Z)) */
683 684
#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
685 686

/* Terminate CCL program successfully.  */
687
#define CCL_SUCCESS			\
688
do					\
689
  {					\
Karl Heuer's avatar
Karl Heuer committed
690
    ccl->status = CCL_STAT_SUCCESS;	\
691 692
    goto ccl_finish;			\
  }					\
Juanma Barranquero's avatar
Juanma Barranquero committed
693
while (0)
Karl Heuer's avatar
Karl Heuer committed
694 695 696 697

/* 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.  */
698
#define CCL_SUSPEND(stat)	\
699
do				\
700
  {				\
701 702 703
    ic--;			\
    ccl->status = stat;		\
    goto ccl_finish;		\
704
  }				\
705
while (0)
Karl Heuer's avatar
Karl Heuer committed
706 707 708

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

Karl Heuer's avatar
Karl Heuer committed
711
#define CCL_INVALID_CMD		     	\
712
do					\
713
  {				     	\
Karl Heuer's avatar
Karl Heuer committed
714 715
    ccl->status = CCL_STAT_INVALID_CMD;	\
    goto ccl_error_handler;	     	\
716
  }					\
Juanma Barranquero's avatar
Juanma Barranquero committed
717
while (0)
Karl Heuer's avatar
Karl Heuer committed
718

719 720
#else

Karl Heuer's avatar
Karl Heuer committed
721
#define CCL_INVALID_CMD		     	\
722
do					\
723
  {				     	\
724
    ccl_debug_hook (this_ic);		\
Karl Heuer's avatar
Karl Heuer committed
725 726
    ccl->status = CCL_STAT_INVALID_CMD;	\
    goto ccl_error_handler;	     	\
727
  }					\
Juanma Barranquero's avatar
Juanma Barranquero committed
728
while (0)
Karl Heuer's avatar
Karl Heuer committed
729

730 731
#endif

732
/* Use "&" rather than "&&" to suppress a bogus GCC warning; see
733
   <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=43772>.  */
734 735
#define ASCENDING_ORDER(lo, med, hi) (((lo) <= (med)) & ((med) <= (hi)))

Paul Eggert's avatar
Paul Eggert committed
736 737 738
#define GET_CCL_RANGE(var, ccl_prog, ic, lo, hi)		\
  do								\
    {								\
Tom Tromey's avatar
Tom Tromey committed
739
      EMACS_INT prog_word = XFIXNUM ((ccl_prog)[ic]);		\
740
      if (! ASCENDING_ORDER (lo, prog_word, hi))		\
Paul Eggert's avatar
Paul Eggert committed
741 742 743 744 745 746
	CCL_INVALID_CMD;					\
      (var) = prog_word;					\
    }								\
  while (0)

#define GET_CCL_CODE(code, ccl_prog, ic)			\
747
  GET_CCL_RANGE (code, ccl_prog, ic, CCL_CODE_MIN, CCL_CODE_MAX)
Paul Eggert's avatar
Paul Eggert committed
748

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

Karl Heuer's avatar
Karl Heuer committed
751
/* Encode one character CH to multibyte form and write to the current
752
   output buffer.  If CH is less than 256, CH is written as is.  */
Kenichi Handa's avatar
Kenichi Handa committed
753 754 755 756 757 758 759 760
#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);	\
761 762
  } while (0)

Karl Heuer's avatar
Karl Heuer committed
763 764
/* Write a string at ccl_prog[IC] of length LEN to the current output
   buffer.  */
765 766
#define CCL_WRITE_STRING(len)					\
  do {								\
767
    int ccli;							\
768 769 770 771
    if (!dst)							\
      CCL_INVALID_CMD;						\
    else if (dst + len <= dst_end)				\
      {								\
Tom Tromey's avatar
Tom Tromey committed
772
	if (XFIXNAT (ccl_prog[ic]) & 0x1000000)		\
773
	  for (ccli = 0; ccli < len; ccli++)			\
Tom Tromey's avatar
Tom Tromey committed
774
	    *dst++ = XFIXNAT (ccl_prog[ic + ccli]) & 0xFFFFFF;	\
775
	else							\
776
	  for (ccli = 0; ccli < len; ccli++)			\
Tom Tromey's avatar
Tom Tromey committed
777
	    *dst++ = ((XFIXNAT (ccl_prog[ic + (ccli / 3)]))	\
778
		      >> ((2 - (ccli % 3)) * 8)) & 0xFF;	\
779 780 781
      }								\
    else							\
      CCL_SUSPEND (CCL_STAT_SUSPEND_BY_DST);			\
Karl Heuer's avatar
Karl Heuer committed
782 783
  } while (0)

Kenichi Handa's avatar
Kenichi Handa committed
784 785 786 787 788 789 790 791 792
/* 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)			\
      {						\
793
	r = -1;					\
Kenichi Handa's avatar
Kenichi Handa committed
794 795 796 797 798 799 800
	ic = ccl->eof_ic;			\
	goto ccl_repeat;			\
      }						\
    else					\
      CCL_SUSPEND (CCL_STAT_SUSPEND_BY_SRC);	\
    } while (0)

801 802 803 804 805 806 807 808 809
/* 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
810
   the id of the used charset, ENCODED to the result of encoding.
811 812
   Assume that we can use the variable `charset'.  */

Kenichi Handa's avatar
Kenichi Handa committed
813
#define CCL_ENCODE_CHAR(c, charset_list, id, encoded)		\
Kenichi Handa's avatar
Kenichi Handa committed
814
  do {								\
815
    unsigned ncode;						\
Kenichi Handa's avatar
Kenichi Handa committed
816
								\
817
    charset = char_charset ((c), (charset_list), &ncode);	\
Kenichi Handa's avatar
Kenichi Handa committed
818
    if (! charset && ! NILP (charset_list))			\
819
      charset = char_charset ((c), Qnil, &ncode);	  	\
Kenichi Handa's avatar
Kenichi Handa committed
820 821 822
    if (charset)						\
      {								\
	(id) = CHARSET_ID (charset);				\
823
	(encoded) = ncode;					\
Kenichi Handa's avatar
Kenichi Handa committed
824
      }								\
Kenichi Handa's avatar
Kenichi Handa committed
825
   } while (0)
Kenichi Handa's avatar
Kenichi Handa committed
826

Kenichi Handa's avatar
Kenichi Handa committed
827 828 829 830 831 832 833
/* 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
834 835 836

#ifdef CCL_DEBUG
#define CCL_DEBUG_BACKTRACE_LEN 256
Dave Love's avatar
Dave Love committed
837
int ccl_backtrace_table[CCL_DEBUG_BACKTRACE_LEN];
Karl Heuer's avatar
Karl Heuer committed
838
int ccl_backtrace_idx;
839 840 841 842 843 844 845

int
ccl_debug_hook (int ic)
{
  return ic;
}

Karl Heuer's avatar
Karl Heuer committed
846 847 848 849
#endif

struct ccl_prog_stack
  {
850
    Lisp_Object *ccl_prog;	/* Pointer to an array of CCL code.  */
Karl Heuer's avatar
Karl Heuer committed
851
    int ic;			/* Instruction Counter.  */
852
    int eof_ic;			/* Instruction Counter to jump on EOF.  */
Karl Heuer's avatar
Karl Heuer committed
853 854
  };

855
/* For the moment, we only support depth 256 of stack.  */
856 857
static struct ccl_prog_stack ccl_prog_stack_struct[256];

858
/* Return a translation table of id number ID.  */
859
static inline Lisp_Object
860 861 862 863 864
GET_TRANSLATION_TABLE (int id)
{
  return XCDR (XVECTOR (Vtranslation_table_vector)->contents[id]);
}

Kenichi Handa's avatar
Kenichi Handa committed
865
void
866
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
867 868 869
{
  register int *reg = ccl->reg;
  register int ic = ccl->ic;
870
  register int code = 0, field1, field2;
871
  register Lisp_Object *ccl_prog = ccl->prog;
Kenichi Handa's avatar
Kenichi Handa committed
872 873
  int *src = source, *src_end = src + src_size;
  int *dst = destination, *dst_end = dst + dst_size;
Karl Heuer's avatar
Karl Heuer committed
874
  int jump_address;
875
  int i = 0, j, op;
876
  int stack_idx = ccl->stack_idx;
877
  /* Instruction counter of the current CCL code. */
878
  int this_ic = 0;
Kenichi Handa's avatar
Kenichi Handa committed
879
  struct charset *charset;
880 881
  int eof_ic = ccl->eof_ic;
  int eof_hit = 0;
Karl Heuer's avatar
Karl Heuer committed
882

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

Kenichi Handa's avatar
Kenichi Handa committed
886 887 888
  /* Set mapping stack pointer. */
  mapping_stack_pointer = mapping_stack;

Karl Heuer's avatar
Karl Heuer committed
889 890 891 892 893 894
#ifdef CCL_DEBUG
  ccl_backtrace_idx = 0;
#endif

  for (;;)
    {
895
    ccl_repeat:
Karl Heuer's avatar
Karl Heuer committed
896 897 898 899 900 901 902 903 904 905 906 907
#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
908 909
	  if (src)
	    src = source + src_size;
Karl Heuer's avatar
Karl Heuer committed
910 911 912 913
	  ccl->status = CCL_STAT_QUIT;
	  break;
	}

914
      this_ic = ic;
Paul Eggert's avatar
Paul Eggert committed
915
      GET_CCL_CODE (code, ccl_prog, ic++);
Karl Heuer's avatar
Karl Heuer committed
916 917 918 919 920 921 922
      field1 = code >> 8;
      field2 = (code & 0xFF) >> 5;

#define rrr field2
#define RRR (field1 & 7)
#define Rrr ((field1 >> 3) & 7)
#define ADDR field1
923
#define EXCMD (field1 >> 6)
Karl Heuer's avatar
Karl Heuer committed
924 925 926 927 928 929 930 931 932 933 934 935

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

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

	case CCL_SetConst:	/* 00000000000000000000rrrXXXXX */
Tom Tromey's avatar
Tom Tromey committed
936
	  reg[rrr] = XFIXNUM (ccl_prog[ic++]);
Karl Heuer's avatar
Karl Heuer committed
937 938 939 940 941
	  break;

	case CCL_SetArray:	/* CCCCCCCCCCCCCCCCCCCCRRRrrrXXXXX */
	  i = reg[RRR];
	  j = field1 >> 3;
942
	  if (0 <= i && i < j)
Tom Tromey's avatar
Tom Tromey committed
943
	    reg[rrr] = XFIXNUM (ccl_prog[ic + i]);
Karl Heuer's avatar
Karl Heuer committed
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
	  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 */
Tom Tromey's avatar
Tom Tromey committed
971
	  i = XFIXNUM (ccl_prog[ic]);
Karl Heuer's avatar
Karl Heuer committed
972 973 974 975 976
	  CCL_WRITE_CHAR (i);
	  ic += ADDR;
	  break;

	case CCL_WriteConstReadJump: /* A--D--D--R--E--S--S-rrrXXXXX */
Tom Tromey's avatar
Tom Tromey committed
977
	  i = XFIXNUM (ccl_prog[ic]);
Karl Heuer's avatar
Karl Heuer committed
978 979 980 981 982 983 984
	  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 */
Tom Tromey's avatar
Tom Tromey committed
985
	  j = XFIXNUM (ccl_prog[ic++]);
Karl Heuer's avatar
Karl Heuer committed
986 987 988 989 990 991
	  CCL_WRITE_STRING (j);
	  ic += ADDR - 1;
	  break;

	case CCL_WriteArrayReadJump: /* A--D--D--R--E--S--S-rrrXXXXX */
	  i = reg[rrr];
Tom Tromey's avatar
Tom Tromey committed
992
	  j = XFIXNUM (ccl_prog[ic]);
993
	  if (0 <= i && i < j)
Karl Heuer's avatar
Karl Heuer committed
994
	    {
Tom Tromey's avatar
Tom Tromey committed
995
	      i = XFIXNUM (ccl_prog[ic + 1 + i]);
Karl Heuer's avatar
Karl Heuer committed
996 997
	      CCL_WRITE_CHAR (i);
	    }
998
	  ic += j + 2;
Karl Heuer's avatar
Karl Heuer committed
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
	  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]);
1010
	  FALLTHROUGH;
Karl Heuer's avatar
Karl Heuer committed
1011
	case CCL_Branch:	/* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
Paul Eggert's avatar
Paul Eggert committed
1012
	{
1013
	  int ioff = 0 <= reg[rrr] && reg[rrr] < field1 ? reg[rrr] : field1;
Tom Tromey's avatar
Tom Tromey committed
1014
	  int incr = XFIXNUM (ccl_prog[ic + ioff]);
Paul Eggert's avatar
Paul Eggert committed
1015 1016
	  ic += incr;
	}
Karl Heuer's avatar
Karl Heuer committed
1017 1018 1019 1020 1021 1022 1023
	  break;

	case CCL_ReadRegister:	/* CCCCCCCCCCCCCCCCCCCCrrXXXXX */
	  while (1)
	    {
	      CCL_READ_CHAR (reg[rrr]);
	      if (!field1) break;
Paul Eggert's avatar
Paul Eggert committed
1024
	      GET_CCL_CODE (code, ccl_prog, ic++);
Karl Heuer's avatar
Karl Heuer committed
1025 1026 1027 1028 1029 1030 1031 1032
	      field1 = code >> 8;
	      field2 = (code & 0xFF) >> 5;
	    }
	  break;

	case CCL_WriteExprConst:  /* 1:00000OPERATION000RRR000XXXXX */
	  rrr = 7;
	  i = reg[RRR];
Tom Tromey's avatar
Tom Tromey committed
1033
	  j = XFIXNUM (ccl_prog[ic]);
Karl Heuer's avatar
Karl Heuer committed
1034
	  op = field1 >> 6;
1035
	  jump_address = ic + 1;
Karl Heuer's avatar
Karl Heuer committed
1036 1037 1038 1039 1040 1041 1042 1043
	  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
1044
	      GET_CCL_CODE (code, ccl_prog, ic++);
Karl Heuer's avatar
Karl Heuer committed
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
	      field1 = code >> 8;
	      field2 = (code & 0xFF) >> 5;
	    }
	  break;

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

1058
	case CCL_Call:		/* 1:CCCCCCCCCCCCCCCCCCCCFFFXXXXX */
Karl Heuer's avatar
Karl Heuer committed
1059 1060
	  {
	    Lisp_Object slot;
1061 1062 1063 1064 1065
	    int prog_id;

	    /* If FFF is nonzero, the CCL program ID is in the
               following code.  */
	    if (rrr)
Tom Tromey's avatar
Tom Tromey committed
1066
	      prog_id = XFIXNUM (ccl_prog[ic++]);
1067 1068
	    else
	      prog_id = field1;
Karl Heuer's avatar
Karl Heuer committed
1069 1070

	    if (stack_idx >= 256
1071
		|| prog_id < 0
Stefan Monnier's avatar
Stefan Monnier committed
1072 1073 1074
		|| 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
1075 1076 1077 1078 1079
	      {
		if (stack_idx > 0)
		  {
		    ccl_prog = ccl_prog_stack_struct[0].ccl_prog;
		    ic = ccl_prog_stack_struct[0].ic;
1080
		    eof_ic = ccl_prog_stack_struct[0].eof_ic;
Karl Heuer's avatar
Karl Heuer committed
1081 1082 1083
		  }
		CCL_INVALID_CMD;
	      }
1084

Karl Heuer's avatar
Karl Heuer committed
1085 1086
	    ccl_prog_stack_struct[stack_idx].ccl_prog = ccl_prog;
	    ccl_prog_stack_struct[stack_idx].ic = ic;
1087
	    ccl_prog_stack_struct[stack_idx].eof_ic = eof_ic;
Karl Heuer's avatar
Karl Heuer committed
1088
	    stack_idx++;
Paul Eggert's avatar
Paul Eggert committed
1089
	    ccl_prog = XVECTOR (AREF (slot, 1))->contents;
Karl Heuer's avatar
Karl Heuer committed
1090
	    ic = CCL_HEADER_MAIN;
Tom Tromey's avatar
Tom Tromey committed
1091
	    eof_ic = XFIXNAT (ccl_prog[CCL_HEADER_EOF]);
Karl Heuer's avatar
Karl Heuer committed
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
	  }
	  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];
1107
	  if (0 <= i && i < field1)
Karl Heuer's avatar
Karl Heuer committed
1108
	    {
Tom Tromey's avatar
Tom Tromey committed
1109
	      j = XFIXNUM (ccl_prog[ic + i]);
Karl Heuer's avatar
Karl Heuer committed
1110 1111 1112 1113 1114 1115
	      CCL_WRITE_CHAR (j);
	    }
	  ic += field1;
	  break;

	case CCL_End:		/* 0000000000000000000000XXXXX */
1116
	  if (stack_idx > 0)
Karl Heuer's avatar
Karl Heuer committed
1117
	    {
1118
	      stack_idx--;
Karl Heuer's avatar
Karl Heuer committed
1119 1120
	      ccl_prog = ccl_prog_stack_struct[stack_idx].ccl_prog;
	      ic = ccl_prog_stack_struct[stack_idx].ic;
1121 1122 1123
	      eof_ic = ccl_prog_stack_struct[stack_idx].eof_ic;
	      if (eof_hit)
		ic = eof_ic;
Karl Heuer's avatar
Karl Heuer committed
1124 1125
	      break;
	    }
1126 1127 1128 1129 1130
	  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
1131 1132 1133
	  CCL_SUCCESS;

	case CCL_ExprSelfConst: /* 00000OPERATION000000rrrXXXXX */
Tom Tromey's avatar
Tom Tromey committed
1134
	  i = XFIXNUM (ccl_prog[ic++]);
Karl Heuer's avatar
Karl Heuer committed
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
	  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];
Tom Tromey's avatar
Tom Tromey committed
1170
	  j = XFIXNUM (ccl_prog[ic++]);
Karl Heuer's avatar
Karl Heuer committed
1171
	  op = field1 >> 6;
Paul Eggert's avatar
Paul Eggert committed
1172
	  jump_address = ic;
Karl Heuer's avatar
Karl Heuer committed
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
	  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]);
1184
	  FALLTHROUGH;
Karl Heuer's avatar
Karl Heuer committed
1185 1186
	case CCL_JumpCondExprConst: /* A--D--D--R--E--S--S-rrrXXXXX */
	  i = reg[rrr];
Paul Eggert's avatar
Paul Eggert committed
1187
	  jump_address = ic + ADDR;
Tom Tromey's avatar
Tom Tromey committed
1188 1189
	  op = XFIXNUM (ccl_prog[ic++]);
	  j = XFIXNUM (ccl_prog[ic++]);
Karl Heuer's avatar
Karl Heuer committed
1190 1191 1192 1193 1194
	  rrr = 7;
	  goto ccl_set_expr;

	case CCL_ReadJumpCondExprReg: /* A--D--D--R--E--S--S-rrrXXXXX */
	  CCL_READ_CHAR (reg[rrr]);
1195
	  FALLTHROUGH;
Karl Heuer's avatar
Karl Heuer committed
1196 1197
	case CCL_JumpCondExprReg:
	  i = reg[rrr];
Paul Eggert's avatar
Paul Eggert committed
1198
	  jump_address = ic + ADDR;
Tom Tromey's avatar
Tom Tromey committed
1199
	  op = XFIXNUM (ccl_prog[ic++]);
Paul Eggert's avatar
Paul Eggert committed
1200 1201
	  GET_CCL_RANGE (j, ccl_prog, ic++, 0, 7);
	  j = reg[j];
Karl Heuer's avatar
Karl Heuer committed
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
	  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;
1214
	    case CCL_XOR: reg[rrr] = i ^ j; break;
Karl Heuer's avatar
Karl Heuer committed
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
	    case CCL_LSH: reg[rrr] = i << j; break;
	    case CCL_RSH: reg[rrr] = i >> j; break;
	    case CCL_LSH8: reg[rrr] = (i << 8) | j; break;
	    case CCL_RSH8: reg[rrr] = i >> 8; reg[7] = i & 0xFF; break;
	    case CCL_DIVMOD: reg[rrr] = i / j; reg[7] = i % j; break;
	    case CCL_LS: reg[rrr] = i < j; break;
	    case CCL_GT: reg[rrr] = i > j; break;
	    case CCL_EQ: reg[rrr] = i == j; break;
	    case CCL_LE: reg[rrr] = i <= j; break;
	    case CCL_GE: reg[rrr] = i >= j; break;
	    case CCL_NE: reg[rrr] = i != j; break;
Kenichi Handa's avatar
Kenichi Handa committed
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
	    case CCL_DECODE_SJIS:
	      {
		i = (i << 8) | j;
		SJIS_TO_JIS (i);
		reg[rrr] = i >> 8;
		reg[7] = i & 0xFF;
		break;
	      }
	    case CCL_ENCODE_SJIS:
	      {
		i = (i << 8) | j;
		JIS_TO_SJIS (i);
		reg[rrr] = i >> 8;
		reg[7] = i & 0xFF;
		break;
	      }
Karl Heuer's avatar
Karl Heuer committed
1242 1243 1244 1245 1246 1247 1248
	    default: CCL_INVALID_CMD;
	    }
	  code &= 0x1F;
	  if (code == CCL_WriteExprConst || code == CCL_WriteExprRegister)
	    {
	      i = reg[rrr];
	      CCL_WRITE_CHAR (i);
1249
	      ic = jump_address;
Karl Heuer's avatar
Karl Heuer committed
1250 1251 1252 1253 1254
	    }
	  else if (!reg[rrr])
	    ic = jump_address;
	  break;

1255
	case CCL_Extension:
1256 1257
	  switch (EXCMD)
	    {
Kenichi Handa's avatar
Kenichi Handa committed
1258
	    case CCL_ReadMultibyteChar2:
1259 1260
	      if (!src)
		CCL_INVALID_CMD;
Kenichi Handa's avatar
Kenichi Handa committed
1261
	      CCL_READ_CHAR (i);
1262
	      CCL_ENCODE_CHAR (i, charset_list, reg[RRR], reg[rrr]);
1263 1264
	      break;

Kenichi Handa's avatar
Kenichi Handa committed
1265
	    case CCL_WriteMultibyteChar2:
Kenichi Handa's avatar
Kenichi Handa committed
1266 1267
	      if (! dst)
		CCL_INVALID_CMD;
1268
	      i = CCL_DECODE_CHAR (reg[RRR], reg[rrr]);
Kenichi Handa's avatar
Kenichi Handa committed
1269
	      CCL_WRITE_CHAR (i);
1270 1271
	      break;

1272
	    case CCL_TranslateCharacter:
1273
	      i = CCL_DECODE_CHAR (reg[RRR], reg[rrr]);
Kenichi Handa's avatar
Kenichi Handa committed
1274
	      op = translate_char (GET_TRANSLATION_TABLE (reg[Rrr]), i);
1275
	      CCL_ENCODE_CHAR (op, charset_list, reg[RRR], reg[rrr]);
1276 1277
	      break;

1278
	    case CCL_TranslateCharacterConstTbl:
Paul Eggert's avatar
Paul Eggert committed
1279
	      {
1280
		ptrdiff_t eop;
Paul Eggert's avatar
Paul Eggert committed
1281 1282 1283 1284 1285 1286 1287 1288
		GET_CCL_RANGE (eop, ccl_prog, ic++, 0,
			       (VECTORP (Vtranslation_table_vector)
				? ASIZE (Vtranslation_table_vector)
				: -1));
		i = CCL_DECODE_CHAR (reg[RRR], reg[rrr]);
		op = translate_char (GET_TRANSLATION_TABLE (eop), i);
		CCL_ENCODE_CHAR (op, charset_list, reg[RRR], reg[rrr]);
	      }
1289 1290
	      break;

1291
	    case CCL_LookupIntConstTbl:
1292
	      {
1293
		ptrdiff_t eop;
Paul Eggert's avatar
Paul Eggert committed
1294 1295 1296 1297 1298 1299
		struct Lisp_Hash_Table *h;
		GET_CCL_RANGE (eop, ccl_prog, ic++, 0,
			       (VECTORP (Vtranslation_hash_table_vector)
				? ASIZE (Vtranslation_hash_table_vector)
				: -1));
		h = GET_HASH_TABLE (eop);
1300

1301 1302 1303
		eop = (FIXNUM_OVERFLOW_P (reg[RRR])
		       ? -1
		       : hash_lookup (h, make_fixnum (reg[RRR]), NULL));
1304
		if (eop >= 0)
1305
		  {
Dave Love's avatar
Dave Love committed
1306
		    Lisp_Object opl;
1307 1308
		    opl = HASH_VALUE (h, eop);
		    if (! (IN_INT_RANGE (eop) && CHARACTERP (opl)))
1309
		      CCL_INVALID_CMD;
1310
		    reg[RRR] = charset_unicode;
1311
		    reg[rrr] = eop;
1312 1313 1314 1315 1316 1317 1318 1319
		    reg[7] = 1; /* r7 true for success */
		  }
		else
		  reg[7] = 0;
	      }
	      break;

	    case CCL_LookupCharConstTbl:
1320
	      {
1321
		ptrdiff_t eop;
Paul Eggert's avatar
Paul Eggert committed
1322 1323 1324 1325 1326 1327 1328
		struct Lisp_Hash_Table *h;
		GET_CCL_RANGE (eop, ccl_prog, ic++, 0,
			       (VECTORP (Vtranslation_hash_table_vector)
				? ASIZE (Vtranslation_hash_table_vector)
				: -1));
		i = CCL_DECODE_CHAR (reg[RRR], reg[rrr]);
		h = GET_HASH_TABLE (eop);
1329

1330 1331 1332
		eop = (FIXNUM_OVERFLOW_P (i)
		       ? -1
		       : hash_lookup (h, make_fixnum (i), NULL));
1333
		if (eop >= 0)
1334
		  {
Dave Love's avatar
Dave Love committed
1335
		    Lisp_Object opl;
1336
		    opl = HASH_VALUE (h, eop);
Tom Tromey's avatar
Tom Tromey committed
1337
		    if (! (FIXNUMP (opl) && IN_INT_RANGE (XFIXNUM (opl))))
1338
		      CCL_INVALID_CMD;
Tom Tromey's avatar
Tom Tromey committed
1339
		    reg[RRR] = XFIXNUM (opl);
1340 1341 1342 1343 1344 1345 1346
		    reg[7] = 1; /* r7 true for success */
		  }
		else
		  reg[7] = 0;
	      }
	      break;