ccl.el 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595
  1. ;;; ccl.el --- CCL (Code Conversion Language) compiler -*- lexical-binding:t -*-
  2. ;; Copyright (C) 1997-1998, 2001-2017 Free Software Foundation, Inc.
  3. ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
  4. ;; 2005, 2006, 2007, 2008, 2009, 2010, 2011
  5. ;; National Institute of Advanced Industrial Science and Technology (AIST)
  6. ;; Registration Number H14PRO021
  7. ;; Keywords: CCL, mule, multilingual, character set, coding-system
  8. ;; This file is part of GNU Emacs.
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;; CCL (Code Conversion Language) is a simple programming language to
  21. ;; be used for various kind of code conversion. A CCL program is
  22. ;; compiled to CCL code (vector of integers) and executed by the CCL
  23. ;; interpreter in Emacs.
  24. ;;
  25. ;; CCL is used for code conversion at process I/O and file I/O for
  26. ;; non-standard coding-systems. In addition, it is used for
  27. ;; calculating code points of X fonts from character codes.
  28. ;; However, since CCL is designed as a powerful programming language,
  29. ;; it can be used for more generic calculation. For instance,
  30. ;; combination of three or more arithmetic operations can be
  31. ;; calculated faster than in Emacs Lisp.
  32. ;;
  33. ;; The syntax and semantics of CCL programs are described in the
  34. ;; documentation of `define-ccl-program'.
  35. ;;; Code:
  36. ;; Unused.
  37. ;;; (defgroup ccl nil
  38. ;;; "CCL (Code Conversion Language) compiler."
  39. ;;; :prefix "ccl-"
  40. ;;; :group 'i18n)
  41. (defconst ccl-command-table
  42. [if branch loop break repeat write-repeat write-read-repeat
  43. read read-if read-branch write call end
  44. read-multibyte-character write-multibyte-character
  45. translate-character
  46. iterate-multiple-map map-multiple map-single lookup-integer
  47. lookup-character]
  48. "Vector of CCL commands (symbols).")
  49. ;; Put a property to each symbol of CCL commands for the compiler.
  50. (let (op (i 0) (len (length ccl-command-table)))
  51. (while (< i len)
  52. (setq op (aref ccl-command-table i))
  53. (put op 'ccl-compile-function (intern (format "ccl-compile-%s" op)))
  54. (setq i (1+ i))))
  55. (defconst ccl-code-table
  56. [set-register
  57. set-short-const
  58. set-const
  59. set-array
  60. jump
  61. jump-cond
  62. write-register-jump
  63. write-register-read-jump
  64. write-const-jump
  65. write-const-read-jump
  66. write-string-jump
  67. write-array-read-jump
  68. read-jump
  69. branch
  70. read-register
  71. write-expr-const
  72. read-branch
  73. write-register
  74. write-expr-register
  75. call
  76. write-const-string
  77. write-array
  78. end
  79. set-assign-expr-const
  80. set-assign-expr-register
  81. set-expr-const
  82. set-expr-register
  83. jump-cond-expr-const
  84. jump-cond-expr-register
  85. read-jump-cond-expr-const
  86. read-jump-cond-expr-register
  87. ex-cmd
  88. ]
  89. "Vector of CCL compiled codes (symbols).")
  90. (defconst ccl-extended-code-table
  91. [read-multibyte-character
  92. write-multibyte-character
  93. translate-character
  94. translate-character-const-tbl
  95. nil nil nil nil nil nil nil nil nil nil nil nil ; 0x04-0x0f
  96. iterate-multiple-map
  97. map-multiple
  98. map-single
  99. lookup-int-const-tbl
  100. lookup-char-const-tbl
  101. ]
  102. "Vector of CCL extended compiled codes (symbols).")
  103. ;; Put a property to each symbol of CCL codes for the disassembler.
  104. (let (code (i 0) (len (length ccl-code-table)))
  105. (while (< i len)
  106. (setq code (aref ccl-code-table i))
  107. (put code 'ccl-code i)
  108. (put code 'ccl-dump-function (intern (format "ccl-dump-%s" code)))
  109. (setq i (1+ i))))
  110. (let (code (i 0) (len (length ccl-extended-code-table)))
  111. (while (< i len)
  112. (setq code (aref ccl-extended-code-table i))
  113. (if code
  114. (progn
  115. (put code 'ccl-ex-code i)
  116. (put code 'ccl-dump-function (intern (format "ccl-dump-%s" code)))))
  117. (setq i (1+ i))))
  118. (defconst ccl-jump-code-list
  119. '(jump jump-cond write-register-jump write-register-read-jump
  120. write-const-jump write-const-read-jump write-string-jump
  121. write-array-read-jump read-jump))
  122. ;; Put a property `jump-flag' to each CCL code which execute jump in
  123. ;; some way.
  124. (let ((l ccl-jump-code-list))
  125. (while l
  126. (put (car l) 'jump-flag t)
  127. (setq l (cdr l))))
  128. (defconst ccl-register-table
  129. [r0 r1 r2 r3 r4 r5 r6 r7]
  130. "Vector of CCL registers (symbols).")
  131. ;; Put a property to indicate register number to each symbol of CCL.
  132. ;; registers.
  133. (let (reg (i 0) (len (length ccl-register-table)))
  134. (while (< i len)
  135. (setq reg (aref ccl-register-table i))
  136. (put reg 'ccl-register-number i)
  137. (setq i (1+ i))))
  138. (defconst ccl-arith-table
  139. [+ - * / % & | ^ << >> <8 >8 // nil nil nil
  140. < > == <= >= != de-sjis en-sjis]
  141. "Vector of CCL arithmetic/logical operators (symbols).")
  142. ;; Put a property to each symbol of CCL operators for the compiler.
  143. (let (arith (i 0) (len (length ccl-arith-table)))
  144. (while (< i len)
  145. (setq arith (aref ccl-arith-table i))
  146. (if arith (put arith 'ccl-arith-code i))
  147. (setq i (1+ i))))
  148. (defconst ccl-assign-arith-table
  149. [+= -= *= /= %= &= |= ^= <<= >>= <8= >8= //=]
  150. "Vector of CCL assignment operators (symbols).")
  151. ;; Put a property to each symbol of CCL assignment operators for the compiler.
  152. (let (arith (i 0) (len (length ccl-assign-arith-table)))
  153. (while (< i len)
  154. (setq arith (aref ccl-assign-arith-table i))
  155. (put arith 'ccl-self-arith-code i)
  156. (setq i (1+ i))))
  157. (defvar ccl-program-vector nil
  158. "Working vector of CCL codes produced by CCL compiler.")
  159. (defvar ccl-current-ic 0
  160. "The current index for `ccl-program-vector'.")
  161. (defun ccl-embed-data (data &optional ic)
  162. "Embed integer DATA in `ccl-program-vector' at `ccl-current-ic' and
  163. increment it. If IC is specified, embed DATA at IC."
  164. (if ic
  165. (aset ccl-program-vector ic data)
  166. (let ((len (length ccl-program-vector)))
  167. (if (>= ccl-current-ic len)
  168. (let ((new (make-vector (* len 2) nil)))
  169. (while (> len 0)
  170. (setq len (1- len))
  171. (aset new len (aref ccl-program-vector len)))
  172. (setq ccl-program-vector new))))
  173. (aset ccl-program-vector ccl-current-ic data)
  174. (setq ccl-current-ic (1+ ccl-current-ic))))
  175. (defun ccl-embed-symbol (symbol prop)
  176. "Embed pair of SYMBOL and PROP where (get SYMBOL PROP) should give
  177. proper index number for SYMBOL. PROP should be
  178. `translation-table-id', `translation-hash-table-id'
  179. `code-conversion-map-id', or `ccl-program-idx'."
  180. (ccl-embed-data (cons symbol prop)))
  181. (defun ccl-embed-string (len str)
  182. "Embed string STR of length LEN in `ccl-program-vector' at
  183. `ccl-current-ic'."
  184. (if (> len #xFFFFF)
  185. (error "CCL: String too long: %d" len))
  186. (if (> (string-bytes str) len)
  187. (dotimes (i len)
  188. (ccl-embed-data (logior #x1000000 (aref str i))))
  189. (let ((i 0))
  190. (while (< i len)
  191. (ccl-embed-data (logior (ash (aref str i) 16)
  192. (if (< (1+ i) len)
  193. (ash (aref str (1+ i)) 8)
  194. 0)
  195. (if (< (+ i 2) len)
  196. (aref str (+ i 2))
  197. 0)))
  198. (setq i (+ i 3))))))
  199. (defun ccl-embed-current-address (ic)
  200. "Embed a relative jump address to `ccl-current-ic' in
  201. `ccl-program-vector' at IC without altering the other bit field."
  202. (let ((relative (- ccl-current-ic (1+ ic))))
  203. (aset ccl-program-vector ic
  204. (logior (aref ccl-program-vector ic) (ash relative 8)))))
  205. (defun ccl-embed-code (op reg data &optional reg2)
  206. "Embed CCL code for the operation OP and arguments REG and DATA in
  207. `ccl-program-vector' at `ccl-current-ic' in the following format.
  208. |----------------- integer (28-bit) ------------------|
  209. |------------ 20-bit ------------|- 3-bit --|- 5-bit -|
  210. |------------- DATA -------------|-- REG ---|-- OP ---|
  211. If REG2 is specified, embed a code in the following format.
  212. |------- 17-bit ------|- 3-bit --|- 3-bit --|- 5-bit -|
  213. |-------- DATA -------|-- REG2 --|-- REG ---|-- OP ---|
  214. If REG is a CCL register symbol (e.g. r0, r1...), the register
  215. number is embedded. If OP is one of unconditional jumps, DATA is
  216. changed to a relative jump address."
  217. (if (and (> data 0) (get op 'jump-flag))
  218. ;; DATA is an absolute jump address. Make it relative to the
  219. ;; next of jump code.
  220. (setq data (- data (1+ ccl-current-ic))))
  221. (let ((code (logior (get op 'ccl-code)
  222. (ash
  223. (if (symbolp reg) (get reg 'ccl-register-number) reg) 5)
  224. (if reg2
  225. (logior (ash (get reg2 'ccl-register-number) 8)
  226. (ash data 11))
  227. (ash data 8)))))
  228. (ccl-embed-data code)))
  229. (defun ccl-embed-extended-command (ex-op reg reg2 reg3)
  230. "extended ccl command format
  231. |- 14-bit -|- 3-bit --|- 3-bit --|- 3-bit --|- 5-bit -|
  232. |- EX-OP --|-- REG3 --|-- REG2 --|-- REG ---|-- OP ---|"
  233. (let ((data (logior (ash (get ex-op 'ccl-ex-code) 3)
  234. (if (symbolp reg3)
  235. (get reg3 'ccl-register-number)
  236. 0))))
  237. (ccl-embed-code 'ex-cmd reg data reg2)))
  238. (defun ccl-increment-ic (inc)
  239. "Just advance `ccl-current-ic' by INC."
  240. (setq ccl-current-ic (+ ccl-current-ic inc)))
  241. (defvar ccl-loop-head nil
  242. "If non-nil, index of the start of the current loop.")
  243. (defvar ccl-breaks nil
  244. "If non-nil, list of absolute addresses of the breaking points of
  245. the current loop.")
  246. ;;;###autoload
  247. (defun ccl-compile (ccl-program)
  248. "Return the compiled code of CCL-PROGRAM as a vector of integers."
  249. (unless (and (consp ccl-program)
  250. (integerp (car ccl-program))
  251. (listp (car (cdr ccl-program))))
  252. (error "CCL: Invalid CCL program: %s" ccl-program))
  253. (if (null (vectorp ccl-program-vector))
  254. (setq ccl-program-vector (make-vector 8192 0)))
  255. (setq ccl-loop-head nil ccl-breaks nil)
  256. (setq ccl-current-ic 0)
  257. ;; The first element is the buffer magnification.
  258. (ccl-embed-data (car ccl-program))
  259. ;; The second element is the address of the start CCL code for
  260. ;; processing end of input buffer (we call it eof-processor). We
  261. ;; set it later.
  262. (ccl-increment-ic 1)
  263. ;; Compile the main body of the CCL program.
  264. (ccl-compile-1 (car (cdr ccl-program)))
  265. ;; Embed the address of eof-processor.
  266. (ccl-embed-data ccl-current-ic 1)
  267. ;; Then compile eof-processor.
  268. (if (nth 2 ccl-program)
  269. (ccl-compile-1 (nth 2 ccl-program)))
  270. ;; At last, embed termination code.
  271. (ccl-embed-code 'end 0 0)
  272. (let ((vec (make-vector ccl-current-ic 0))
  273. (i 0))
  274. (while (< i ccl-current-ic)
  275. (aset vec i (aref ccl-program-vector i))
  276. (setq i (1+ i)))
  277. vec))
  278. (defun ccl-syntax-error (cmd)
  279. "Signal syntax error."
  280. (error "CCL: Syntax error: %s" cmd))
  281. (defun ccl-check-register (arg cmd)
  282. "Check if ARG is a valid CCL register."
  283. (if (get arg 'ccl-register-number)
  284. arg
  285. (error "CCL: Invalid register %s in %s" arg cmd)))
  286. (defun ccl-check-compile-function (arg cmd)
  287. "Check if ARG is a valid CCL command."
  288. (or (get arg 'ccl-compile-function)
  289. (error "CCL: Invalid command: %s" cmd)))
  290. ;; In the following code, most ccl-compile-XXXX functions return t if
  291. ;; they end with unconditional jump, else return nil.
  292. (defun ccl-compile-1 (ccl-block)
  293. "Compile CCL-BLOCK (see the syntax above)."
  294. (let (unconditional-jump
  295. cmd)
  296. (if (or (integerp ccl-block)
  297. (stringp ccl-block)
  298. (and ccl-block (symbolp (car ccl-block))))
  299. ;; This block consists of single statement.
  300. (setq ccl-block (list ccl-block)))
  301. ;; Now CCL-BLOCK is a list of statements. Compile them one by
  302. ;; one.
  303. (while ccl-block
  304. (setq cmd (car ccl-block))
  305. (setq unconditional-jump
  306. (cond ((integerp cmd)
  307. ;; SET statement for the register 0.
  308. (ccl-compile-set (list 'r0 '= cmd)))
  309. ((stringp cmd)
  310. ;; WRITE statement of string argument.
  311. (ccl-compile-write-string cmd))
  312. ((listp cmd)
  313. ;; The other statements.
  314. (cond ((eq (nth 1 cmd) '=)
  315. ;; SET statement of the form `(REG = EXPRESSION)'.
  316. (ccl-compile-set cmd))
  317. ((and (symbolp (nth 1 cmd))
  318. (get (nth 1 cmd) 'ccl-self-arith-code))
  319. ;; SET statement with an assignment operation.
  320. (ccl-compile-self-set cmd))
  321. (t
  322. (funcall (ccl-check-compile-function (car cmd) cmd)
  323. cmd))))
  324. (t
  325. (ccl-syntax-error cmd))))
  326. (setq ccl-block (cdr ccl-block)))
  327. unconditional-jump))
  328. (defconst ccl-max-short-const (ash 1 19))
  329. (defconst ccl-min-short-const (ash -1 19))
  330. (defun ccl-compile-set (cmd)
  331. "Compile SET statement."
  332. (let ((rrr (ccl-check-register (car cmd) cmd))
  333. (right (nth 2 cmd)))
  334. (cond ((listp right)
  335. ;; CMD has the form `(RRR = (XXX OP YYY))'.
  336. (ccl-compile-expression rrr right))
  337. ((integerp right)
  338. ;; CMD has the form `(RRR = integer)'.
  339. (if (and (<= right ccl-max-short-const)
  340. (>= right ccl-min-short-const))
  341. (ccl-embed-code 'set-short-const rrr right)
  342. (ccl-embed-code 'set-const rrr 0)
  343. (ccl-embed-data right)))
  344. (t
  345. ;; CMD has the form `(RRR = rrr [ array ])'.
  346. (ccl-check-register right cmd)
  347. (let ((ary (nth 3 cmd)))
  348. (if (vectorp ary)
  349. (let ((i 0) (len (length ary)))
  350. (ccl-embed-code 'set-array rrr len right)
  351. (while (< i len)
  352. (ccl-embed-data (aref ary i))
  353. (setq i (1+ i))))
  354. (ccl-embed-code 'set-register rrr 0 right))))))
  355. nil)
  356. (defun ccl-compile-self-set (cmd)
  357. "Compile SET statement with ASSIGNMENT_OPERATOR."
  358. (let ((rrr (ccl-check-register (car cmd) cmd))
  359. (right (nth 2 cmd)))
  360. (if (listp right)
  361. ;; CMD has the form `(RRR ASSIGN_OP (XXX OP YYY))', compile
  362. ;; the right hand part as `(r7 = (XXX OP YYY))' (note: the
  363. ;; register 7 can be used for storing temporary value).
  364. (progn
  365. (ccl-compile-expression 'r7 right)
  366. (setq right 'r7)))
  367. ;; Now CMD has the form `(RRR ASSIGN_OP ARG)'. Compile it as
  368. ;; `(RRR = (RRR OP ARG))'.
  369. (ccl-compile-expression
  370. rrr
  371. (list rrr (intern (substring (symbol-name (nth 1 cmd)) 0 -1)) right)))
  372. nil)
  373. (defun ccl-compile-expression (rrr expr)
  374. "Compile SET statement of the form `(RRR = EXPR)'."
  375. (let ((left (car expr))
  376. (op (get (nth 1 expr) 'ccl-arith-code))
  377. (right (nth 2 expr)))
  378. (if (listp left)
  379. (progn
  380. ;; EXPR has the form `((EXPR2 OP2 ARG) OP RIGHT)'. Compile
  381. ;; the first term as `(r7 = (EXPR2 OP2 ARG)).'
  382. (ccl-compile-expression 'r7 left)
  383. (setq left 'r7)))
  384. ;; Now EXPR has the form (LEFT OP RIGHT).
  385. (if (and (eq rrr left)
  386. (< op (length ccl-assign-arith-table)))
  387. ;; Compile this SET statement as `(RRR OP= RIGHT)'.
  388. (if (integerp right)
  389. (progn
  390. (ccl-embed-code 'set-assign-expr-const rrr (ash op 3) 'r0)
  391. (ccl-embed-data right))
  392. (ccl-check-register right expr)
  393. (ccl-embed-code 'set-assign-expr-register rrr (ash op 3) right))
  394. ;; Compile this SET statement as `(RRR = (LEFT OP RIGHT))'.
  395. (if (integerp right)
  396. (progn
  397. (ccl-embed-code 'set-expr-const rrr (ash op 3) left)
  398. (ccl-embed-data right))
  399. (ccl-check-register right expr)
  400. (ccl-embed-code 'set-expr-register
  401. rrr
  402. (logior (ash op 3) (get right 'ccl-register-number))
  403. left)))))
  404. (defun ccl-compile-write-string (str)
  405. "Compile WRITE statement with string argument."
  406. (let ((len (length str)))
  407. (ccl-embed-code 'write-const-string 1 len)
  408. (ccl-embed-string len str))
  409. nil)
  410. (defun ccl-compile-if (cmd &optional read-flag)
  411. "Compile IF statement of the form `(if CONDITION TRUE-PART FALSE-PART)'.
  412. If READ-FLAG is non-nil, this statement has the form
  413. `(read-if (REG OPERATOR ARG) TRUE-PART FALSE-PART)'."
  414. (if (and (/= (length cmd) 3) (/= (length cmd) 4))
  415. (error "CCL: Invalid number of arguments: %s" cmd))
  416. (let ((condition (nth 1 cmd))
  417. (true-cmds (nth 2 cmd))
  418. (false-cmds (nth 3 cmd))
  419. jump-cond-address)
  420. (if (and (listp condition)
  421. (listp (car condition)))
  422. ;; If CONDITION is a nested expression, the inner expression
  423. ;; should be compiled at first as SET statement, i.e.:
  424. ;; `(if ((X OP2 Y) OP Z) ...)' is compiled into two statements:
  425. ;; `(r7 = (X OP2 Y)) (if (r7 OP Z) ...)'.
  426. (progn
  427. (ccl-compile-expression 'r7 (car condition))
  428. (setq condition (cons 'r7 (cdr condition)))
  429. (setq cmd (cons (car cmd)
  430. (cons condition (cdr (cdr cmd)))))))
  431. (setq jump-cond-address ccl-current-ic)
  432. ;; Compile CONDITION.
  433. (if (symbolp condition)
  434. ;; CONDITION is a register.
  435. (progn
  436. (ccl-check-register condition cmd)
  437. (ccl-embed-code 'jump-cond condition 0))
  438. ;; CONDITION is a simple expression of the form (RRR OP ARG).
  439. (let ((rrr (car condition))
  440. (op (get (nth 1 condition) 'ccl-arith-code))
  441. (arg (nth 2 condition)))
  442. (ccl-check-register rrr cmd)
  443. (or (integerp op)
  444. (error "CCL: invalid operator: %s" (nth 1 condition)))
  445. (if (integerp arg)
  446. (progn
  447. (ccl-embed-code (if read-flag 'read-jump-cond-expr-const
  448. 'jump-cond-expr-const)
  449. rrr 0)
  450. (ccl-embed-data op)
  451. (ccl-embed-data arg))
  452. (ccl-check-register arg cmd)
  453. (ccl-embed-code (if read-flag 'read-jump-cond-expr-register
  454. 'jump-cond-expr-register)
  455. rrr 0)
  456. (ccl-embed-data op)
  457. (ccl-embed-data (get arg 'ccl-register-number)))))
  458. ;; Compile TRUE-PART.
  459. (let ((unconditional-jump (ccl-compile-1 true-cmds)))
  460. (if (null false-cmds)
  461. ;; This is the place to jump to if condition is false.
  462. (progn
  463. (ccl-embed-current-address jump-cond-address)
  464. (setq unconditional-jump nil))
  465. (let (end-true-part-address)
  466. (if (not unconditional-jump)
  467. (progn
  468. ;; If TRUE-PART does not end with unconditional jump, we
  469. ;; have to jump to the end of FALSE-PART from here.
  470. (setq end-true-part-address ccl-current-ic)
  471. (ccl-embed-code 'jump 0 0)))
  472. ;; This is the place to jump to if CONDITION is false.
  473. (ccl-embed-current-address jump-cond-address)
  474. ;; Compile FALSE-PART.
  475. (setq unconditional-jump
  476. (and (ccl-compile-1 false-cmds) unconditional-jump))
  477. (if end-true-part-address
  478. ;; This is the place to jump to after the end of TRUE-PART.
  479. (ccl-embed-current-address end-true-part-address))))
  480. unconditional-jump)))
  481. (defun ccl-compile-branch (cmd)
  482. "Compile BRANCH statement."
  483. (if (< (length cmd) 3)
  484. (error "CCL: Invalid number of arguments: %s" cmd))
  485. (ccl-compile-branch-blocks 'branch
  486. (ccl-compile-branch-expression (nth 1 cmd) cmd)
  487. (cdr (cdr cmd))))
  488. (defun ccl-compile-read-branch (cmd)
  489. "Compile READ statement of the form `(read-branch EXPR BLOCK0 BLOCK1 ...)'."
  490. (if (< (length cmd) 3)
  491. (error "CCL: Invalid number of arguments: %s" cmd))
  492. (ccl-compile-branch-blocks 'read-branch
  493. (ccl-compile-branch-expression (nth 1 cmd) cmd)
  494. (cdr (cdr cmd))))
  495. (defun ccl-compile-branch-expression (expr cmd)
  496. "Compile EXPRESSION part of BRANCH statement and return register
  497. which holds a value of the expression."
  498. (if (listp expr)
  499. ;; EXPR has the form `(EXPR2 OP ARG)'. Compile it as SET
  500. ;; statement of the form `(r7 = (EXPR2 OP ARG))'.
  501. (progn
  502. (ccl-compile-expression 'r7 expr)
  503. 'r7)
  504. (ccl-check-register expr cmd)))
  505. (defun ccl-compile-branch-blocks (code rrr blocks)
  506. "Compile BLOCKs of BRANCH statement. CODE is 'branch or 'read-branch.
  507. REG is a register which holds a value of EXPRESSION part. BLOCKs
  508. is a list of CCL-BLOCKs."
  509. (let ((branches (length blocks))
  510. branch-idx
  511. jump-table-head-address
  512. empty-block-indexes
  513. block-tail-addresses
  514. block-unconditional-jump)
  515. (ccl-embed-code code rrr branches)
  516. (setq jump-table-head-address ccl-current-ic)
  517. ;; The size of jump table is the number of blocks plus 1 (for the
  518. ;; case RRR is out of range).
  519. (ccl-increment-ic (1+ branches))
  520. (setq empty-block-indexes (list branches))
  521. ;; Compile each block.
  522. (setq branch-idx 0)
  523. (while blocks
  524. (if (null (car blocks))
  525. ;; This block is empty.
  526. (setq empty-block-indexes (cons branch-idx empty-block-indexes)
  527. block-unconditional-jump t)
  528. ;; This block is not empty.
  529. (ccl-embed-data (- ccl-current-ic jump-table-head-address)
  530. (+ jump-table-head-address branch-idx))
  531. (setq block-unconditional-jump (ccl-compile-1 (car blocks)))
  532. (if (not block-unconditional-jump)
  533. (progn
  534. ;; Jump address of the end of branches are embedded later.
  535. ;; For the moment, just remember where to embed them.
  536. (setq block-tail-addresses
  537. (cons ccl-current-ic block-tail-addresses))
  538. (ccl-embed-code 'jump 0 0))))
  539. (setq branch-idx (1+ branch-idx))
  540. (setq blocks (cdr blocks)))
  541. (if (not block-unconditional-jump)
  542. ;; We don't need jump code at the end of the last block.
  543. (setq block-tail-addresses (cdr block-tail-addresses)
  544. ccl-current-ic (1- ccl-current-ic)))
  545. ;; Embed jump address at the tailing jump commands of blocks.
  546. (while block-tail-addresses
  547. (ccl-embed-current-address (car block-tail-addresses))
  548. (setq block-tail-addresses (cdr block-tail-addresses)))
  549. ;; For empty blocks, make entries in the jump table point directly here.
  550. (while empty-block-indexes
  551. (ccl-embed-data (- ccl-current-ic jump-table-head-address)
  552. (+ jump-table-head-address (car empty-block-indexes)))
  553. (setq empty-block-indexes (cdr empty-block-indexes))))
  554. ;; Branch command ends by unconditional jump if RRR is out of range.
  555. nil)
  556. (defun ccl-compile-loop (cmd)
  557. "Compile LOOP statement."
  558. (if (< (length cmd) 2)
  559. (error "CCL: Invalid number of arguments: %s" cmd))
  560. (let* ((ccl-loop-head ccl-current-ic)
  561. (ccl-breaks nil)
  562. unconditional-jump)
  563. (setq cmd (cdr cmd))
  564. (if cmd
  565. (progn
  566. (setq unconditional-jump t)
  567. (while cmd
  568. (setq unconditional-jump
  569. (and (ccl-compile-1 (car cmd)) unconditional-jump))
  570. (setq cmd (cdr cmd)))
  571. (if (not ccl-breaks)
  572. unconditional-jump
  573. ;; Embed jump address for break statements encountered in
  574. ;; this loop.
  575. (while ccl-breaks
  576. (ccl-embed-current-address (car ccl-breaks))
  577. (setq ccl-breaks (cdr ccl-breaks))))
  578. nil))))
  579. (defun ccl-compile-break (cmd)
  580. "Compile BREAK statement."
  581. (if (/= (length cmd) 1)
  582. (error "CCL: Invalid number of arguments: %s" cmd))
  583. (if (null ccl-loop-head)
  584. (error "CCL: No outer loop: %s" cmd))
  585. (setq ccl-breaks (cons ccl-current-ic ccl-breaks))
  586. (ccl-embed-code 'jump 0 0)
  587. t)
  588. (defun ccl-compile-repeat (cmd)
  589. "Compile REPEAT statement."
  590. (if (/= (length cmd) 1)
  591. (error "CCL: Invalid number of arguments: %s" cmd))
  592. (if (null ccl-loop-head)
  593. (error "CCL: No outer loop: %s" cmd))
  594. (ccl-embed-code 'jump 0 ccl-loop-head)
  595. t)
  596. (defun ccl-compile-write-repeat (cmd)
  597. "Compile WRITE-REPEAT statement."
  598. (if (/= (length cmd) 2)
  599. (error "CCL: Invalid number of arguments: %s" cmd))
  600. (if (null ccl-loop-head)
  601. (error "CCL: No outer loop: %s" cmd))
  602. (let ((arg (nth 1 cmd)))
  603. (cond ((integerp arg)
  604. (ccl-embed-code 'write-const-jump 0 ccl-loop-head)
  605. (ccl-embed-data arg))
  606. ((stringp arg)
  607. (let ((len (length arg)))
  608. (ccl-embed-code 'write-string-jump 0 ccl-loop-head)
  609. (ccl-embed-data len)
  610. (ccl-embed-string len arg)))
  611. (t
  612. (ccl-check-register arg cmd)
  613. (ccl-embed-code 'write-register-jump arg ccl-loop-head))))
  614. t)
  615. (defun ccl-compile-write-read-repeat (cmd)
  616. "Compile WRITE-READ-REPEAT statement."
  617. (if (or (< (length cmd) 2) (> (length cmd) 3))
  618. (error "CCL: Invalid number of arguments: %s" cmd))
  619. (if (null ccl-loop-head)
  620. (error "CCL: No outer loop: %s" cmd))
  621. (let ((rrr (ccl-check-register (nth 1 cmd) cmd))
  622. (arg (nth 2 cmd)))
  623. (cond ((null arg)
  624. (ccl-embed-code 'write-register-read-jump rrr ccl-loop-head))
  625. ((integerp arg)
  626. (ccl-embed-code 'write-const-read-jump rrr arg ccl-loop-head))
  627. ((vectorp arg)
  628. (let ((len (length arg))
  629. (i 0))
  630. (ccl-embed-code 'write-array-read-jump rrr ccl-loop-head)
  631. (ccl-embed-data len)
  632. (while (< i len)
  633. (ccl-embed-data (aref arg i))
  634. (setq i (1+ i)))))
  635. (t
  636. (error "CCL: Invalid argument %s: %s" arg cmd)))
  637. (ccl-embed-code 'read-jump rrr ccl-loop-head))
  638. t)
  639. (defun ccl-compile-read (cmd)
  640. "Compile READ statement."
  641. (if (< (length cmd) 2)
  642. (error "CCL: Invalid number of arguments: %s" cmd))
  643. (let* ((args (cdr cmd))
  644. (i (1- (length args))))
  645. (while args
  646. (let ((rrr (ccl-check-register (car args) cmd)))
  647. (ccl-embed-code 'read-register rrr i)
  648. (setq args (cdr args) i (1- i)))))
  649. nil)
  650. (defun ccl-compile-read-if (cmd)
  651. "Compile READ-IF statement."
  652. (ccl-compile-if cmd 'read))
  653. (defun ccl-compile-write (cmd)
  654. "Compile WRITE statement."
  655. (if (< (length cmd) 2)
  656. (error "CCL: Invalid number of arguments: %s" cmd))
  657. (let ((rrr (nth 1 cmd)))
  658. (cond ((integerp rrr)
  659. (if (> rrr #xFFFFF)
  660. (ccl-compile-write-string (string rrr))
  661. (ccl-embed-code 'write-const-string 0 rrr)))
  662. ((stringp rrr)
  663. (ccl-compile-write-string rrr))
  664. ((and (symbolp rrr) (vectorp (nth 2 cmd)))
  665. (ccl-check-register rrr cmd)
  666. ;; CMD has the form `(write REG ARRAY)'.
  667. (let* ((arg (nth 2 cmd))
  668. (len (length arg))
  669. (i 0))
  670. (ccl-embed-code 'write-array rrr len)
  671. (while (< i len)
  672. (if (not (integerp (aref arg i)))
  673. (error "CCL: Invalid argument %s: %s" arg cmd))
  674. (ccl-embed-data (aref arg i))
  675. (setq i (1+ i)))))
  676. ((symbolp rrr)
  677. ;; CMD has the form `(write REG ...)'.
  678. (let* ((args (cdr cmd))
  679. (i (1- (length args))))
  680. (while args
  681. (setq rrr (ccl-check-register (car args) cmd))
  682. (ccl-embed-code 'write-register rrr i)
  683. (setq args (cdr args) i (1- i)))))
  684. ((listp rrr)
  685. ;; CMD has the form `(write (LEFT OP RIGHT))'.
  686. (let ((left (car rrr))
  687. (op (get (nth 1 rrr) 'ccl-arith-code))
  688. (right (nth 2 rrr)))
  689. (if (listp left)
  690. (progn
  691. ;; RRR has the form `((EXPR OP2 ARG) OP RIGHT)'.
  692. ;; Compile the first term as `(r7 = (EXPR OP2 ARG))'.
  693. (ccl-compile-expression 'r7 left)
  694. (setq left 'r7)))
  695. ;; Now RRR has the form `(ARG OP RIGHT)'.
  696. (if (integerp right)
  697. (progn
  698. (ccl-embed-code 'write-expr-const 0 (ash op 3) left)
  699. (ccl-embed-data right))
  700. (ccl-check-register right rrr)
  701. (ccl-embed-code 'write-expr-register 0
  702. (logior (ash op 3)
  703. (get right 'ccl-register-number))
  704. left))))
  705. (t
  706. (error "CCL: Invalid argument: %s" cmd))))
  707. nil)
  708. (defun ccl-compile-call (cmd)
  709. "Compile CALL statement."
  710. (if (/= (length cmd) 2)
  711. (error "CCL: Invalid number of arguments: %s" cmd))
  712. (if (not (symbolp (nth 1 cmd)))
  713. (error "CCL: Subroutine should be a symbol: %s" cmd))
  714. (ccl-embed-code 'call 1 0)
  715. (ccl-embed-symbol (nth 1 cmd) 'ccl-program-idx)
  716. nil)
  717. (defun ccl-compile-end (cmd)
  718. "Compile END statement."
  719. (if (/= (length cmd) 1)
  720. (error "CCL: Invalid number of arguments: %s" cmd))
  721. (ccl-embed-code 'end 0 0)
  722. t)
  723. (defun ccl-compile-read-multibyte-character (cmd)
  724. "Compile read-multibyte-character"
  725. (if (/= (length cmd) 3)
  726. (error "CCL: Invalid number of arguments: %s" cmd))
  727. (let ((RRR (nth 1 cmd))
  728. (rrr (nth 2 cmd)))
  729. (ccl-check-register rrr cmd)
  730. (ccl-check-register RRR cmd)
  731. (ccl-embed-extended-command 'read-multibyte-character rrr RRR 0))
  732. nil)
  733. (defun ccl-compile-write-multibyte-character (cmd)
  734. "Compile write-multibyte-character"
  735. (if (/= (length cmd) 3)
  736. (error "CCL: Invalid number of arguments: %s" cmd))
  737. (let ((RRR (nth 1 cmd))
  738. (rrr (nth 2 cmd)))
  739. (ccl-check-register rrr cmd)
  740. (ccl-check-register RRR cmd)
  741. (ccl-embed-extended-command 'write-multibyte-character rrr RRR 0))
  742. nil)
  743. (defun ccl-compile-translate-character (cmd)
  744. "Compile translate-character."
  745. (if (/= (length cmd) 4)
  746. (error "CCL: Invalid number of arguments: %s" cmd))
  747. (let ((Rrr (nth 1 cmd))
  748. (RRR (nth 2 cmd))
  749. (rrr (nth 3 cmd)))
  750. (ccl-check-register rrr cmd)
  751. (ccl-check-register RRR cmd)
  752. (cond ((and (symbolp Rrr) (not (get Rrr 'ccl-register-number)))
  753. (ccl-embed-extended-command 'translate-character-const-tbl
  754. rrr RRR 0)
  755. (ccl-embed-symbol Rrr 'translation-table-id))
  756. (t
  757. (ccl-check-register Rrr cmd)
  758. (ccl-embed-extended-command 'translate-character rrr RRR Rrr))))
  759. nil)
  760. (defun ccl-compile-lookup-integer (cmd)
  761. "Compile lookup-integer."
  762. (if (/= (length cmd) 4)
  763. (error "CCL: Invalid number of arguments: %s" cmd))
  764. (let ((Rrr (nth 1 cmd))
  765. (RRR (nth 2 cmd))
  766. (rrr (nth 3 cmd)))
  767. (ccl-check-register RRR cmd)
  768. (ccl-check-register rrr cmd)
  769. (cond ((and (symbolp Rrr) (not (get Rrr 'ccl-register-number)))
  770. (ccl-embed-extended-command 'lookup-int-const-tbl
  771. rrr RRR 0)
  772. (ccl-embed-symbol Rrr 'translation-hash-table-id))
  773. (t
  774. (error "CCL: non-constant table: %s" cmd)
  775. ;; not implemented:
  776. (ccl-check-register Rrr cmd)
  777. (ccl-embed-extended-command 'lookup-int rrr RRR 0))))
  778. nil)
  779. (defun ccl-compile-lookup-character (cmd)
  780. "Compile lookup-character."
  781. (if (/= (length cmd) 4)
  782. (error "CCL: Invalid number of arguments: %s" cmd))
  783. (let ((Rrr (nth 1 cmd))
  784. (RRR (nth 2 cmd))
  785. (rrr (nth 3 cmd)))
  786. (ccl-check-register RRR cmd)
  787. (ccl-check-register rrr cmd)
  788. (cond ((and (symbolp Rrr) (not (get Rrr 'ccl-register-number)))
  789. (ccl-embed-extended-command 'lookup-char-const-tbl
  790. rrr RRR 0)
  791. (ccl-embed-symbol Rrr 'translation-hash-table-id))
  792. (t
  793. (error "CCL: non-constant table: %s" cmd)
  794. ;; not implemented:
  795. (ccl-check-register Rrr cmd)
  796. (ccl-embed-extended-command 'lookup-char rrr RRR 0))))
  797. nil)
  798. (defun ccl-compile-iterate-multiple-map (cmd)
  799. (ccl-compile-multiple-map-function 'iterate-multiple-map cmd)
  800. nil)
  801. (defun ccl-compile-map-multiple (cmd)
  802. (if (/= (length cmd) 4)
  803. (error "CCL: Invalid number of arguments: %s" cmd))
  804. (let (func arg)
  805. (setq func
  806. (lambda (arg mp)
  807. (let ((len 0) result add)
  808. (while arg
  809. (if (consp (car arg))
  810. (setq add (funcall func (car arg) t)
  811. result (append result add)
  812. add (+ (- (car add)) 1))
  813. (setq result
  814. (append result
  815. (list (car arg)))
  816. add 1))
  817. (setq arg (cdr arg)
  818. len (+ len add)))
  819. (if mp
  820. (cons (- len) result)
  821. result))))
  822. (setq arg (append (list (nth 0 cmd) (nth 1 cmd) (nth 2 cmd))
  823. (funcall func (nth 3 cmd) nil)))
  824. (ccl-compile-multiple-map-function 'map-multiple arg))
  825. nil)
  826. (defun ccl-compile-map-single (cmd)
  827. (if (/= (length cmd) 4)
  828. (error "CCL: Invalid number of arguments: %s" cmd))
  829. (let ((RRR (nth 1 cmd))
  830. (rrr (nth 2 cmd))
  831. (map (nth 3 cmd)))
  832. (ccl-check-register rrr cmd)
  833. (ccl-check-register RRR cmd)
  834. (ccl-embed-extended-command 'map-single rrr RRR 0)
  835. (cond ((symbolp map)
  836. (if (get map 'code-conversion-map)
  837. (ccl-embed-symbol map 'code-conversion-map-id)
  838. (error "CCL: Invalid map: %s" map)))
  839. (t
  840. (error "CCL: Invalid type of arguments: %s" cmd))))
  841. nil)
  842. (defun ccl-compile-multiple-map-function (command cmd)
  843. (if (< (length cmd) 4)
  844. (error "CCL: Invalid number of arguments: %s" cmd))
  845. (let ((RRR (nth 1 cmd))
  846. (rrr (nth 2 cmd))
  847. (args (nthcdr 3 cmd))
  848. map)
  849. (ccl-check-register rrr cmd)
  850. (ccl-check-register RRR cmd)
  851. (ccl-embed-extended-command command rrr RRR 0)
  852. (ccl-embed-data (length args))
  853. (while args
  854. (setq map (car args))
  855. (cond ((symbolp map)
  856. (if (get map 'code-conversion-map)
  857. (ccl-embed-symbol map 'code-conversion-map-id)
  858. (error "CCL: Invalid map: %s" map)))
  859. ((numberp map)
  860. (ccl-embed-data map))
  861. (t
  862. (error "CCL: Invalid type of arguments: %s" cmd)))
  863. (setq args (cdr args)))))
  864. ;;; CCL dump stuff
  865. (defvar ccl-code)
  866. ;;;###autoload
  867. (defun ccl-dump (code)
  868. "Disassemble compiled CCL-code CODE."
  869. (let* ((ccl-code code)
  870. (len (length ccl-code))
  871. (buffer-mag (aref ccl-code 0)))
  872. (cond ((= buffer-mag 0)
  873. (insert (substitute-command-keys "Don't output anything.\n")))
  874. ((= buffer-mag 1)
  875. (insert "Out-buffer must be as large as in-buffer.\n"))
  876. (t
  877. (insert
  878. (format "Out-buffer must be %d times bigger than in-buffer.\n"
  879. buffer-mag))))
  880. (insert "Main-body:\n")
  881. (setq ccl-current-ic 2)
  882. (if (> (aref ccl-code 1) 0)
  883. (progn
  884. (while (< ccl-current-ic (aref ccl-code 1))
  885. (ccl-dump-1))
  886. (insert "At EOF:\n")))
  887. (while (< ccl-current-ic len)
  888. (ccl-dump-1))
  889. ))
  890. (defun ccl-get-next-code ()
  891. "Return a CCL code in `ccl-code' at `ccl-current-ic'."
  892. (prog1
  893. (aref ccl-code ccl-current-ic)
  894. (setq ccl-current-ic (1+ ccl-current-ic))))
  895. (defun ccl-dump-1 ()
  896. (let* ((code (ccl-get-next-code))
  897. (cmd (aref ccl-code-table (logand code 31)))
  898. (rrr (ash (logand code 255) -5))
  899. (cc (ash code -8)))
  900. (insert (format "%5d:[%s] " (1- ccl-current-ic) cmd))
  901. (funcall (get cmd 'ccl-dump-function) rrr cc)))
  902. (defun ccl-dump-set-register (rrr cc)
  903. (insert (format "r%d = r%d\n" rrr cc)))
  904. (defun ccl-dump-set-short-const (rrr cc)
  905. (insert (format "r%d = %d\n" rrr cc)))
  906. (defun ccl-dump-set-const (rrr _ignore)
  907. (insert (format "r%d = %d\n" rrr (ccl-get-next-code))))
  908. (defun ccl-dump-set-array (rrr cc)
  909. (let ((rrr2 (logand cc 7))
  910. (len (ash cc -3))
  911. (i 0))
  912. (insert (format "r%d = array[r%d] of length %d\n\t"
  913. rrr rrr2 len))
  914. (while (< i len)
  915. (insert (format "%d " (ccl-get-next-code)))
  916. (setq i (1+ i)))
  917. (insert "\n")))
  918. (defun ccl-dump-jump (_ignore cc &optional address)
  919. (insert (format "jump to %d(" (+ (or address ccl-current-ic) cc)))
  920. (if (>= cc 0)
  921. (insert "+"))
  922. (insert (format "%d)\n" (1+ cc))))
  923. (defun ccl-dump-jump-cond (rrr cc)
  924. (insert (format "if (r%d == 0), " rrr))
  925. (ccl-dump-jump nil cc))
  926. (defun ccl-dump-write-register-jump (rrr cc)
  927. (insert (format "write r%d, " rrr))
  928. (ccl-dump-jump nil cc))
  929. (defun ccl-dump-write-register-read-jump (rrr cc)
  930. (insert (format "write r%d, read r%d, " rrr rrr))
  931. (ccl-dump-jump nil cc)
  932. (ccl-get-next-code) ; Skip dummy READ-JUMP
  933. )
  934. (defun ccl-extract-arith-op (cc)
  935. (aref ccl-arith-table (ash cc -6)))
  936. (defun ccl-dump-write-expr-const (_ignore cc)
  937. (insert (format "write (r%d %s %d)\n"
  938. (logand cc 7)
  939. (ccl-extract-arith-op cc)
  940. (ccl-get-next-code))))
  941. (defun ccl-dump-write-expr-register (_ignore cc)
  942. (insert (format "write (r%d %s r%d)\n"
  943. (logand cc 7)
  944. (ccl-extract-arith-op cc)
  945. (logand (ash cc -3) 7))))
  946. (defun ccl-dump-insert-char (cc)
  947. (cond ((= cc ?\t) (insert " \"^I\""))
  948. ((= cc ?\n) (insert " \"^J\""))
  949. (t (insert (format " \"%c\"" cc)))))
  950. (defun ccl-dump-write-const-jump (_ignore cc)
  951. (let ((address ccl-current-ic))
  952. (insert "write char")
  953. (ccl-dump-insert-char (ccl-get-next-code))
  954. (insert ", ")
  955. (ccl-dump-jump nil cc address)))
  956. (defun ccl-dump-write-const-read-jump (rrr cc)
  957. (let ((address ccl-current-ic))
  958. (insert "write char")
  959. (ccl-dump-insert-char (ccl-get-next-code))
  960. (insert (format ", read r%d, " rrr))
  961. (ccl-dump-jump cc address)
  962. (ccl-get-next-code) ; Skip dummy READ-JUMP
  963. ))
  964. (defun ccl-dump-write-string-jump (_ignore cc)
  965. (let ((address ccl-current-ic)
  966. (len (ccl-get-next-code))
  967. (i 0))
  968. (insert "write \"")
  969. (while (< i len)
  970. (let ((code (ccl-get-next-code)))
  971. (insert (ash code -16))
  972. (if (< (1+ i) len) (insert (logand (ash code -8) 255)))
  973. (if (< (+ i 2) len) (insert (logand code 255))))
  974. (setq i (+ i 3)))
  975. (insert "\", ")
  976. (ccl-dump-jump nil cc address)))
  977. (defun ccl-dump-write-array-read-jump (rrr cc)
  978. (let ((address ccl-current-ic)
  979. (len (ccl-get-next-code))
  980. (i 0))
  981. (insert (format "write array[r%d] of length %d,\n\t" rrr len))
  982. (while (< i len)
  983. (ccl-dump-insert-char (ccl-get-next-code))
  984. (setq i (1+ i)))
  985. (insert (format "\n\tthen read r%d, " rrr))
  986. (ccl-dump-jump nil cc address)
  987. (ccl-get-next-code) ; Skip dummy READ-JUMP.
  988. ))
  989. (defun ccl-dump-read-jump (rrr cc)
  990. (insert (format "read r%d, " rrr))
  991. (ccl-dump-jump nil cc))
  992. (defun ccl-dump-branch (rrr len)
  993. (let ((jump-table-head ccl-current-ic)
  994. (i 0))
  995. (insert (format "jump to array[r%d] of length %d\n\t" rrr len))
  996. (while (<= i len)
  997. (insert (format "%d " (+ jump-table-head (ccl-get-next-code))))
  998. (setq i (1+ i)))
  999. (insert "\n")))
  1000. (defun ccl-dump-read-register (rrr cc)
  1001. (insert (format "read r%d (%d remaining)\n" rrr cc)))
  1002. (defun ccl-dump-read-branch (rrr len)
  1003. (insert (format "read r%d, " rrr))
  1004. (ccl-dump-branch rrr len))
  1005. (defun ccl-dump-write-register (rrr cc)
  1006. (insert (format "write r%d (%d remaining)\n" rrr cc)))
  1007. (defun ccl-dump-call (_ignore _cc)
  1008. (let ((subroutine (car (ccl-get-next-code))))
  1009. (insert (format-message "call subroutine `%s'\n" subroutine))))
  1010. (defun ccl-dump-write-const-string (rrr cc)
  1011. (if (= rrr 0)
  1012. (progn
  1013. (insert "write char")
  1014. (ccl-dump-insert-char cc)
  1015. (newline))
  1016. (let ((len cc)
  1017. (i 0))
  1018. (insert "write \"")
  1019. (while (< i len)
  1020. (let ((code (ccl-get-next-code)))
  1021. (if (/= (logand code #x1000000) 0)
  1022. (progn
  1023. (insert (logand code #xFFFFFF))
  1024. (setq i (1+ i)))
  1025. (insert (format "%c" (lsh code -16)))
  1026. (if (< (1+ i) len)
  1027. (insert (format "%c" (logand (lsh code -8) 255))))
  1028. (if (< (+ i 2) len)
  1029. (insert (format "%c" (logand code 255))))
  1030. (setq i (+ i 3)))))
  1031. (insert "\"\n"))))
  1032. (defun ccl-dump-write-array (rrr cc)
  1033. (let ((i 0))
  1034. (insert (format "write array[r%d] of length %d\n\t" rrr cc))
  1035. (while (< i cc)
  1036. (ccl-dump-insert-char (ccl-get-next-code))
  1037. (setq i (1+ i)))
  1038. (insert "\n")))
  1039. (defun ccl-dump-end (&rest _ignore)
  1040. (insert "end\n"))
  1041. (defun ccl-dump-set-assign-expr-const (rrr cc)
  1042. (insert (format "r%d %s= %d\n"
  1043. rrr
  1044. (ccl-extract-arith-op cc)
  1045. (ccl-get-next-code))))
  1046. (defun ccl-dump-set-assign-expr-register (rrr cc)
  1047. (insert (format "r%d %s= r%d\n"
  1048. rrr
  1049. (ccl-extract-arith-op cc)
  1050. (logand cc 7))))
  1051. (defun ccl-dump-set-expr-const (rrr cc)
  1052. (insert (format "r%d = r%d %s %d\n"
  1053. rrr
  1054. (logand cc 7)
  1055. (ccl-extract-arith-op cc)
  1056. (ccl-get-next-code))))
  1057. (defun ccl-dump-set-expr-register (rrr cc)
  1058. (insert (format "r%d = r%d %s r%d\n"
  1059. rrr
  1060. (logand cc 7)
  1061. (ccl-extract-arith-op cc)
  1062. (logand (ash cc -3) 7))))
  1063. (defun ccl-dump-jump-cond-expr-const (rrr cc)
  1064. (let ((address ccl-current-ic))
  1065. (insert (format "if !(r%d %s %d), "
  1066. rrr
  1067. (aref ccl-arith-table (ccl-get-next-code))
  1068. (ccl-get-next-code)))
  1069. (ccl-dump-jump nil cc address)))
  1070. (defun ccl-dump-jump-cond-expr-register (rrr cc)
  1071. (let ((address ccl-current-ic))
  1072. (insert (format "if !(r%d %s r%d), "
  1073. rrr
  1074. (aref ccl-arith-table (ccl-get-next-code))
  1075. (ccl-get-next-code)))
  1076. (ccl-dump-jump nil cc address)))
  1077. (defun ccl-dump-read-jump-cond-expr-const (rrr cc)
  1078. (insert (format "read r%d, " rrr))
  1079. (ccl-dump-jump-cond-expr-const rrr cc))
  1080. (defun ccl-dump-read-jump-cond-expr-register (rrr cc)
  1081. (insert (format "read r%d, " rrr))
  1082. (ccl-dump-jump-cond-expr-register rrr cc))
  1083. (defun ccl-dump-binary (code)
  1084. (let* ((ccl-code code)
  1085. (len (length ccl-code))
  1086. (i 2))
  1087. (while (< i len)
  1088. (let ((code (aref ccl-code i))
  1089. (j 27))
  1090. (while (>= j 0)
  1091. (insert (if (= (logand code (ash 1 j)) 0) ?0 ?1))
  1092. (setq j (1- j)))
  1093. (setq code (logand code 31))
  1094. (if (< code (length ccl-code-table))
  1095. (insert (format ":%s" (aref ccl-code-table code))))
  1096. (insert "\n"))
  1097. (setq i (1+ i)))))
  1098. (defun ccl-dump-ex-cmd (rrr cc)
  1099. (let* ((RRR (logand cc ?\x7))
  1100. (Rrr (logand (ash cc -3) ?\x7))
  1101. (ex-op (aref ccl-extended-code-table (logand (ash cc -6) ?\x3fff))))
  1102. (insert (format "<%s> " ex-op))
  1103. (funcall (get ex-op 'ccl-dump-function) rrr RRR Rrr)))
  1104. (defun ccl-dump-read-multibyte-character (rrr RRR _Rrr)
  1105. (insert (format "read-multibyte-character r%d r%d\n" RRR rrr)))
  1106. (defun ccl-dump-write-multibyte-character (rrr RRR _Rrr)
  1107. (insert (format "write-multibyte-character r%d r%d\n" RRR rrr)))
  1108. (defun ccl-dump-translate-character (rrr RRR Rrr)
  1109. (insert (format "translation table(r%d) r%d r%d\n" Rrr RRR rrr)))
  1110. (defun ccl-dump-translate-character-const-tbl (rrr RRR _Rrr)
  1111. (let ((tbl (ccl-get-next-code)))
  1112. (insert (format "translation table(%S) r%d r%d\n" tbl RRR rrr))))
  1113. (defun ccl-dump-lookup-int-const-tbl (rrr RRR _Rrr)
  1114. (let ((tbl (ccl-get-next-code)))
  1115. (insert (format "hash table(%S) r%d r%d\n" tbl RRR rrr))))
  1116. (defun ccl-dump-lookup-char-const-tbl (rrr RRR _Rrr)
  1117. (let ((tbl (ccl-get-next-code)))
  1118. (insert (format "hash table(%S) r%d r%d\n" tbl RRR rrr))))
  1119. (defun ccl-dump-iterate-multiple-map (rrr RRR _Rrr)
  1120. (let ((notbl (ccl-get-next-code))
  1121. (i 0) id)
  1122. (insert (format "iterate-multiple-map r%d r%d\n" RRR rrr))
  1123. (insert (format "\tnumber of maps is %d .\n\t [" notbl))
  1124. (while (< i notbl)
  1125. (setq id (ccl-get-next-code))
  1126. (insert (format "%S" id))
  1127. (setq i (1+ i)))
  1128. (insert "]\n")))
  1129. (defun ccl-dump-map-multiple (rrr RRR _Rrr)
  1130. (let ((notbl (ccl-get-next-code))
  1131. (i 0) id)
  1132. (insert (format "map-multiple r%d r%d\n" RRR rrr))
  1133. (insert (format "\tnumber of maps and separators is %d\n\t [" notbl))
  1134. (while (< i notbl)
  1135. (setq id (ccl-get-next-code))
  1136. (if (= id -1)
  1137. (insert "]\n\t [")
  1138. (insert (format "%S " id)))
  1139. (setq i (1+ i)))
  1140. (insert "]\n")))
  1141. (defun ccl-dump-map-single (rrr RRR _Rrr)
  1142. (let ((id (ccl-get-next-code)))
  1143. (insert (format "map-single r%d r%d map(%S)\n" RRR rrr id))))
  1144. ;; CCL emulation staffs
  1145. ;; Not yet implemented.
  1146. ;; Auto-loaded functions.
  1147. ;;;###autoload
  1148. (defmacro declare-ccl-program (name &optional vector)
  1149. "Declare NAME as a name of CCL program.
  1150. This macro exists for backward compatibility. In the old version of
  1151. Emacs, to compile a CCL program which calls another CCL program not
  1152. yet defined, it must be declared as a CCL program in advance. But,
  1153. now CCL program names are resolved not at compile time but before
  1154. execution.
  1155. Optional arg VECTOR is a compiled CCL code of the CCL program."
  1156. `(put ',name 'ccl-program-idx (register-ccl-program ',name ,vector)))
  1157. ;;;###autoload
  1158. (defmacro define-ccl-program (name ccl-program &optional doc)
  1159. "Set NAME the compiled code of CCL-PROGRAM.
  1160. CCL-PROGRAM has this form:
  1161. (BUFFER_MAGNIFICATION
  1162. CCL_MAIN_CODE
  1163. [ CCL_EOF_CODE ])
  1164. BUFFER_MAGNIFICATION is an integer value specifying the approximate
  1165. output buffer magnification size compared with the bytes of input data
  1166. text. It is assured that the actual output buffer has 256 bytes
  1167. more than the size calculated by BUFFER_MAGNIFICATION.
  1168. If the value is zero, the CCL program can't execute `read' and
  1169. `write' commands.
  1170. CCL_MAIN_CODE and CCL_EOF_CODE are CCL program codes. CCL_MAIN_CODE
  1171. executed at first. If there's no more input data when `read' command
  1172. is executed in CCL_MAIN_CODE, CCL_EOF_CODE is executed. If
  1173. CCL_MAIN_CODE is terminated, CCL_EOF_CODE is not executed.
  1174. Here's the syntax of CCL program code in BNF notation. The lines
  1175. starting by two semicolons (and optional leading spaces) describe the
  1176. semantics.
  1177. CCL_MAIN_CODE := CCL_BLOCK
  1178. CCL_EOF_CODE := CCL_BLOCK
  1179. CCL_BLOCK := STATEMENT | (STATEMENT [STATEMENT ...])
  1180. STATEMENT :=
  1181. SET | IF | BRANCH | LOOP | REPEAT | BREAK | READ | WRITE | CALL
  1182. | TRANSLATE | MAP | LOOKUP | END
  1183. SET := (REG = EXPRESSION)
  1184. | (REG ASSIGNMENT_OPERATOR EXPRESSION)
  1185. ;; The following form is the same as (r0 = integer).
  1186. | integer
  1187. EXPRESSION := ARG | (EXPRESSION OPERATOR ARG)
  1188. ;; Evaluate EXPRESSION. If the result is nonzero, execute
  1189. ;; CCL_BLOCK_0. Otherwise, execute CCL_BLOCK_1.
  1190. IF := (if EXPRESSION CCL_BLOCK_0 CCL_BLOCK_1)
  1191. ;; Evaluate EXPRESSION. Provided that the result is N, execute
  1192. ;; CCL_BLOCK_N.
  1193. BRANCH := (branch EXPRESSION CCL_BLOCK_0 [CCL_BLOCK_1 ...])
  1194. ;; Execute STATEMENTs until (break) or (end) is executed.
  1195. ;; Create a block of STATEMENTs for repeating. The STATEMENTs
  1196. ;; are executed sequentially until REPEAT or BREAK is executed.
  1197. ;; If REPEAT statement is executed, STATEMENTs are executed from the
  1198. ;; start again. If BREAK statements is executed, the execution
  1199. ;; exits from the block. If neither REPEAT nor BREAK is
  1200. ;; executed, the execution exits from the block after executing the
  1201. ;; last STATEMENT.
  1202. LOOP := (loop STATEMENT [STATEMENT ...])
  1203. ;; Terminate the most inner loop.
  1204. BREAK := (break)
  1205. REPEAT :=
  1206. ;; Jump to the head of the most inner loop.
  1207. (repeat)
  1208. ;; Same as: ((write [REG | integer | string])
  1209. ;; (repeat))
  1210. | (write-repeat [REG | integer | string])
  1211. ;; Same as: ((write REG [ARRAY])
  1212. ;; (read REG)
  1213. ;; (repeat))
  1214. | (write-read-repeat REG [ARRAY])
  1215. ;; Same as: ((write integer)
  1216. ;; (read REG)
  1217. ;; (repeat))
  1218. | (write-read-repeat REG integer)
  1219. READ := ;; Set REG_0 to a byte read from the input text, set REG_1
  1220. ;; to the next byte read, and so on.
  1221. (read REG_0 [REG_1 ...])
  1222. ;; Same as: ((read REG)
  1223. ;; (if (REG OPERATOR ARG) CCL_BLOCK_0 CCL_BLOCK_1))
  1224. | (read-if (REG OPERATOR ARG) CCL_BLOCK_0 CCL_BLOCK_1)
  1225. ;; Same as: ((read REG)
  1226. ;; (branch REG CCL_BLOCK_0 [CCL_BLOCK_1 ...]))
  1227. | (read-branch REG CCL_BLOCK_0 [CCL_BLOCK_1 ...])
  1228. ;; Read a character from the input text while parsing
  1229. ;; multibyte representation, set REG_0 to the charset ID of
  1230. ;; the character, set REG_1 to the code point of the
  1231. ;; character. If the dimension of charset is two, set REG_1
  1232. ;; to ((CODE0 << 7) | CODE1), where CODE0 is the first code
  1233. ;; point and CODE1 is the second code point.
  1234. | (read-multibyte-character REG_0 REG_1)
  1235. WRITE :=
  1236. ;; Write REG_0, REG_1, ... to the output buffer. If REG_N is
  1237. ;; a multibyte character, write the corresponding multibyte
  1238. ;; representation.
  1239. (write REG_0 [REG_1 ...])
  1240. ;; Same as: ((r7 = EXPRESSION)
  1241. ;; (write r7))
  1242. | (write EXPRESSION)
  1243. ;; Write the value of `integer' to the output buffer. If it
  1244. ;; is a multibyte character, write the corresponding multibyte
  1245. ;; representation.
  1246. | (write integer)
  1247. ;; Write the byte sequence of `string' as is to the output
  1248. ;; buffer.
  1249. | (write string)
  1250. ;; Same as: (write string)
  1251. | string
  1252. ;; Provided that the value of REG is N, write Nth element of
  1253. ;; ARRAY to the output buffer. If it is a multibyte
  1254. ;; character, write the corresponding multibyte
  1255. ;; representation.
  1256. | (write REG ARRAY)
  1257. ;; Write a multibyte representation of a character whose
  1258. ;; charset ID is REG_0 and code point is REG_1. If the
  1259. ;; dimension of the charset is two, REG_1 should be ((CODE0 <<
  1260. ;; 7) | CODE1), where CODE0 is the first code point and CODE1
  1261. ;; is the second code point of the character.
  1262. | (write-multibyte-character REG_0 REG_1)
  1263. ;; Call CCL program whose name is ccl-program-name.
  1264. CALL := (call ccl-program-name)
  1265. ;; Terminate the CCL program.
  1266. END := (end)
  1267. ;; CCL registers that can contain any integer value. As r7 is also
  1268. ;; used by CCL interpreter, its value is changed unexpectedly.
  1269. REG := r0 | r1 | r2 | r3 | r4 | r5 | r6 | r7
  1270. ARG := REG | integer
  1271. OPERATOR :=
  1272. ;; Normal arithmetic operators (same meaning as C code).
  1273. + | - | * | / | %
  1274. ;; Bitwise operators (same meaning as C code)
  1275. | & | `|' | ^
  1276. ;; Shifting operators (same meaning as C code)
  1277. | << | >>
  1278. ;; (REG = ARG_0 <8 ARG_1) means:
  1279. ;; (REG = ((ARG_0 << 8) | ARG_1))
  1280. | <8
  1281. ;; (REG = ARG_0 >8 ARG_1) means:
  1282. ;; ((REG = (ARG_0 >> 8))
  1283. ;; (r7 = (ARG_0 & 255)))
  1284. | >8
  1285. ;; (REG = ARG_0 // ARG_1) means:
  1286. ;; ((REG = (ARG_0 / ARG_1))
  1287. ;; (r7 = (ARG_0 % ARG_1)))
  1288. | //
  1289. ;; Normal comparing operators (same meaning as C code)
  1290. | < | > | == | <= | >= | !=
  1291. ;; If ARG_0 and ARG_1 are higher and lower byte of Shift-JIS
  1292. ;; code, and CHAR is the corresponding JISX0208 character,
  1293. ;; (REG = ARG_0 de-sjis ARG_1) means:
  1294. ;; ((REG = CODE0)
  1295. ;; (r7 = CODE1))
  1296. ;; where CODE0 is the first code point of CHAR, CODE1 is the
  1297. ;; second code point of CHAR.
  1298. | de-sjis
  1299. ;; If ARG_0 and ARG_1 are the first and second code point of
  1300. ;; JISX0208 character CHAR, and SJIS is the corresponding
  1301. ;; Shift-JIS code,
  1302. ;; (REG = ARG_0 en-sjis ARG_1) means:
  1303. ;; ((REG = HIGH)
  1304. ;; (r7 = LOW))
  1305. ;; where HIGH is the higher byte of SJIS, LOW is the lower
  1306. ;; byte of SJIS.
  1307. | en-sjis
  1308. ASSIGNMENT_OPERATOR :=
  1309. ;; Same meaning as C code
  1310. += | -= | *= | /= | %= | &= | `|=' | ^= | <<= | >>=
  1311. ;; (REG <8= ARG) is the same as:
  1312. ;; ((REG <<= 8)
  1313. ;; (REG |= ARG))
  1314. | <8=
  1315. ;; (REG >8= ARG) is the same as:
  1316. ;; ((r7 = (REG & 255))
  1317. ;; (REG >>= 8))
  1318. ;; (REG //= ARG) is the same as:
  1319. ;; ((r7 = (REG % ARG))
  1320. ;; (REG /= ARG))
  1321. | //=
  1322. ARRAY := `[' integer ... `]'
  1323. TRANSLATE :=
  1324. ;; Decode character SRC, translate it by translate table
  1325. ;; TABLE, and encode it back to DST. TABLE is specified
  1326. ;; by its id number in REG_0, SRC is specified by its
  1327. ;; charset id number and codepoint in REG_1 and REG_2
  1328. ;; respectively.
  1329. ;; On encoding, the charset of highest priority is selected.
  1330. ;; After the execution, DST is specified by its charset
  1331. ;; id number and codepoint in REG_1 and REG_2 respectively.
  1332. (translate-character REG_0 REG_1 REG_2)
  1333. ;; Same as above except for SYMBOL specifying the name of
  1334. ;; the translate table defined by `define-translation-table'.
  1335. | (translate-character SYMBOL REG_1 REG_2)
  1336. LOOKUP :=
  1337. ;; Look up character SRC in hash table TABLE. TABLE is
  1338. ;; specified by its name in SYMBOL, and SRC is specified by
  1339. ;; its charset id number and codepoint in REG_1 and REG_2
  1340. ;; respectively.
  1341. ;; If its associated value is an integer, set REG_1 to that
  1342. ;; value, and set r7 to 1. Otherwise, set r7 to 0.
  1343. (lookup-character SYMBOL REG_1 REG_2)
  1344. ;; Look up integer value N in hash table TABLE. TABLE is
  1345. ;; specified by its name in SYMBOL and N is specified in
  1346. ;; REG.
  1347. ;; If its associated value is a character, set REG to that
  1348. ;; value, and set r7 to 1. Otherwise, set r7 to 0.
  1349. | (lookup-integer SYMBOL REG(integer))
  1350. MAP :=
  1351. ;; The following statements are for internal use only.
  1352. (iterate-multiple-map REG REG MAP-IDs)
  1353. | (map-multiple REG REG (MAP-SET))
  1354. | (map-single REG REG MAP-ID)
  1355. MAP-IDs := MAP-ID ...
  1356. MAP-SET := MAP-IDs | (MAP-IDs) MAP-SET
  1357. MAP-ID := integer
  1358. "
  1359. (declare (doc-string 3))
  1360. `(let ((prog ,(unwind-protect
  1361. (progn
  1362. ;; To make ,(charset-id CHARSET) works well.
  1363. (fset 'charset-id 'charset-id-internal)
  1364. (ccl-compile (eval ccl-program)))
  1365. (fmakunbound 'charset-id))))
  1366. (defconst ,name prog ,doc)
  1367. (put ',name 'ccl-program-idx (register-ccl-program ',name prog))
  1368. nil))
  1369. ;;;###autoload
  1370. (defmacro check-ccl-program (ccl-program &optional name)
  1371. "Check validity of CCL-PROGRAM.
  1372. If CCL-PROGRAM is a symbol denoting a CCL program, return
  1373. CCL-PROGRAM, else return nil.
  1374. If CCL-PROGRAM is a vector and optional arg NAME (symbol) is supplied,
  1375. register CCL-PROGRAM by name NAME, and return NAME."
  1376. `(if (ccl-program-p ,ccl-program)
  1377. (if (vectorp ,ccl-program)
  1378. (progn
  1379. (register-ccl-program ,name ,ccl-program)
  1380. ,name)
  1381. ,ccl-program)))
  1382. ;;;###autoload
  1383. (defun ccl-execute-with-args (ccl-prog &rest args)
  1384. "Execute CCL-PROGRAM with registers initialized by the remaining args.
  1385. The return value is a vector of resulting CCL registers.
  1386. See the documentation of `define-ccl-program' for the detail of CCL program."
  1387. (let ((reg (make-vector 8 0))
  1388. (i 0))
  1389. (while (and args (< i 8))
  1390. (if (not (integerp (car args)))
  1391. (error "Arguments should be integer"))
  1392. (aset reg i (car args))
  1393. (setq args (cdr args) i (1+ i)))
  1394. (ccl-execute ccl-prog reg)
  1395. reg))
  1396. (provide 'ccl)
  1397. ;;; ccl.el ends here