ccl.el 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562
  1. ;;; ccl.el --- CCL (Code Conversion Language) compiler
  2. ;; Copyright (C) 1997-1998, 2001-2012 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. (defgroup ccl nil
  37. "CCL (Code Conversion Language) compiler."
  38. :prefix "ccl-"
  39. :group 'i18n)
  40. (defconst ccl-command-table
  41. [if branch loop break repeat write-repeat write-read-repeat
  42. read read-if read-branch write call end
  43. read-multibyte-character write-multibyte-character
  44. translate-character
  45. iterate-multiple-map map-multiple map-single lookup-integer
  46. lookup-character]
  47. "Vector of CCL commands (symbols).")
  48. ;; Put a property to each symbol of CCL commands for the compiler.
  49. (let (op (i 0) (len (length ccl-command-table)))
  50. (while (< i len)
  51. (setq op (aref ccl-command-table i))
  52. (put op 'ccl-compile-function (intern (format "ccl-compile-%s" op)))
  53. (setq i (1+ i))))
  54. (defconst ccl-code-table
  55. [set-register
  56. set-short-const
  57. set-const
  58. set-array
  59. jump
  60. jump-cond
  61. write-register-jump
  62. write-register-read-jump
  63. write-const-jump
  64. write-const-read-jump
  65. write-string-jump
  66. write-array-read-jump
  67. read-jump
  68. branch
  69. read-register
  70. write-expr-const
  71. read-branch
  72. write-register
  73. write-expr-register
  74. call
  75. write-const-string
  76. write-array
  77. end
  78. set-assign-expr-const
  79. set-assign-expr-register
  80. set-expr-const
  81. set-expr-register
  82. jump-cond-expr-const
  83. jump-cond-expr-register
  84. read-jump-cond-expr-const
  85. read-jump-cond-expr-register
  86. ex-cmd
  87. ]
  88. "Vector of CCL compiled codes (symbols).")
  89. (defconst ccl-extended-code-table
  90. [read-multibyte-character
  91. write-multibyte-character
  92. translate-character
  93. translate-character-const-tbl
  94. nil nil nil nil nil nil nil nil nil nil nil nil ; 0x04-0x0f
  95. iterate-multiple-map
  96. map-multiple
  97. map-single
  98. lookup-int-const-tbl
  99. lookup-char-const-tbl
  100. ]
  101. "Vector of CCL extended compiled codes (symbols).")
  102. ;; Put a property to each symbol of CCL codes for the disassembler.
  103. (let (code (i 0) (len (length ccl-code-table)))
  104. (while (< i len)
  105. (setq code (aref ccl-code-table i))
  106. (put code 'ccl-code i)
  107. (put code 'ccl-dump-function (intern (format "ccl-dump-%s" code)))
  108. (setq i (1+ i))))
  109. (let (code (i 0) (len (length ccl-extended-code-table)))
  110. (while (< i len)
  111. (setq code (aref ccl-extended-code-table i))
  112. (if code
  113. (progn
  114. (put code 'ccl-ex-code i)
  115. (put code 'ccl-dump-function (intern (format "ccl-dump-%s" code)))))
  116. (setq i (1+ i))))
  117. (defconst ccl-jump-code-list
  118. '(jump jump-cond write-register-jump write-register-read-jump
  119. write-const-jump write-const-read-jump write-string-jump
  120. write-array-read-jump read-jump))
  121. ;; Put a property `jump-flag' to each CCL code which execute jump in
  122. ;; some way.
  123. (let ((l ccl-jump-code-list))
  124. (while l
  125. (put (car l) 'jump-flag t)
  126. (setq l (cdr l))))
  127. (defconst ccl-register-table
  128. [r0 r1 r2 r3 r4 r5 r6 r7]
  129. "Vector of CCL registers (symbols).")
  130. ;; Put a property to indicate register number to each symbol of CCL.
  131. ;; registers.
  132. (let (reg (i 0) (len (length ccl-register-table)))
  133. (while (< i len)
  134. (setq reg (aref ccl-register-table i))
  135. (put reg 'ccl-register-number i)
  136. (setq i (1+ i))))
  137. (defconst ccl-arith-table
  138. [+ - * / % & | ^ << >> <8 >8 // nil nil nil
  139. < > == <= >= != de-sjis en-sjis]
  140. "Vector of CCL arithmetic/logical operators (symbols).")
  141. ;; Put a property to each symbol of CCL operators for the compiler.
  142. (let (arith (i 0) (len (length ccl-arith-table)))
  143. (while (< i len)
  144. (setq arith (aref ccl-arith-table i))
  145. (if arith (put arith 'ccl-arith-code i))
  146. (setq i (1+ i))))
  147. (defconst ccl-assign-arith-table
  148. [+= -= *= /= %= &= |= ^= <<= >>= <8= >8= //=]
  149. "Vector of CCL assignment operators (symbols).")
  150. ;; Put a property to each symbol of CCL assignment operators for the compiler.
  151. (let (arith (i 0) (len (length ccl-assign-arith-table)))
  152. (while (< i len)
  153. (setq arith (aref ccl-assign-arith-table i))
  154. (put arith 'ccl-self-arith-code i)
  155. (setq i (1+ i))))
  156. (defvar ccl-program-vector nil
  157. "Working vector of CCL codes produced by CCL compiler.")
  158. (defvar ccl-current-ic 0
  159. "The current index for `ccl-program-vector'.")
  160. (defun ccl-embed-data (data &optional ic)
  161. "Embed integer DATA in `ccl-program-vector' at `ccl-current-ic' and
  162. increment it. If IC is specified, embed DATA at IC."
  163. (if ic
  164. (aset ccl-program-vector ic data)
  165. (let ((len (length ccl-program-vector)))
  166. (if (>= ccl-current-ic len)
  167. (let ((new (make-vector (* len 2) nil)))
  168. (while (> len 0)
  169. (setq len (1- len))
  170. (aset new len (aref ccl-program-vector len)))
  171. (setq ccl-program-vector new))))
  172. (aset ccl-program-vector ccl-current-ic data)
  173. (setq ccl-current-ic (1+ ccl-current-ic))))
  174. (defun ccl-embed-symbol (symbol prop)
  175. "Embed pair of SYMBOL and PROP where (get SYMBOL PROP) should give
  176. proper index number for SYMBOL. PROP should be
  177. `translation-table-id', `translation-hash-table-id'
  178. `code-conversion-map-id', or `ccl-program-idx'."
  179. (ccl-embed-data (cons symbol prop)))
  180. (defun ccl-embed-string (len str)
  181. "Embed string STR of length LEN in `ccl-program-vector' at
  182. `ccl-current-ic'."
  183. (if (> len #xFFFFF)
  184. (error "CCL: String too long: %d" len))
  185. (if (> (string-bytes str) len)
  186. (dotimes (i len)
  187. (ccl-embed-data (logior #x1000000 (aref str i))))
  188. (let ((i 0))
  189. (while (< i len)
  190. (ccl-embed-data (logior (ash (aref str i) 16)
  191. (if (< (1+ i) len)
  192. (ash (aref str (1+ i)) 8)
  193. 0)
  194. (if (< (+ i 2) len)
  195. (aref str (+ i 2))
  196. 0)))
  197. (setq i (+ i 3))))))
  198. (defun ccl-embed-current-address (ic)
  199. "Embed a relative jump address to `ccl-current-ic' in
  200. `ccl-program-vector' at IC without altering the other bit field."
  201. (let ((relative (- ccl-current-ic (1+ ic))))
  202. (aset ccl-program-vector ic
  203. (logior (aref ccl-program-vector ic) (ash relative 8)))))
  204. (defun ccl-embed-code (op reg data &optional reg2)
  205. "Embed CCL code for the operation OP and arguments REG and DATA in
  206. `ccl-program-vector' at `ccl-current-ic' in the following format.
  207. |----------------- integer (28-bit) ------------------|
  208. |------------ 20-bit ------------|- 3-bit --|- 5-bit -|
  209. |------------- DATA -------------|-- REG ---|-- OP ---|
  210. If REG2 is specified, embed a code in the following format.
  211. |------- 17-bit ------|- 3-bit --|- 3-bit --|- 5-bit -|
  212. |-------- DATA -------|-- REG2 --|-- REG ---|-- OP ---|
  213. If REG is a CCL register symbol (e.g. r0, r1...), the register
  214. number is embedded. If OP is one of unconditional jumps, DATA is
  215. changed to a relative jump address."
  216. (if (and (> data 0) (get op 'jump-flag))
  217. ;; DATA is an absolute jump address. Make it relative to the
  218. ;; next of jump code.
  219. (setq data (- data (1+ ccl-current-ic))))
  220. (let ((code (logior (get op 'ccl-code)
  221. (ash
  222. (if (symbolp reg) (get reg 'ccl-register-number) reg) 5)
  223. (if reg2
  224. (logior (ash (get reg2 'ccl-register-number) 8)
  225. (ash data 11))
  226. (ash data 8)))))
  227. (ccl-embed-data code)))
  228. (defun ccl-embed-extended-command (ex-op reg reg2 reg3)
  229. "extended ccl command format
  230. |- 14-bit -|- 3-bit --|- 3-bit --|- 3-bit --|- 5-bit -|
  231. |- EX-OP --|-- REG3 --|-- REG2 --|-- REG ---|-- OP ---|"
  232. (let ((data (logior (ash (get ex-op 'ccl-ex-code) 3)
  233. (if (symbolp reg3)
  234. (get reg3 'ccl-register-number)
  235. 0))))
  236. (ccl-embed-code 'ex-cmd reg data reg2)))
  237. (defun ccl-increment-ic (inc)
  238. "Just advance `ccl-current-ic' by INC."
  239. (setq ccl-current-ic (+ ccl-current-ic inc)))
  240. (defvar ccl-loop-head nil
  241. "If non-nil, index of the start of the current loop.")
  242. (defvar ccl-breaks nil
  243. "If non-nil, list of absolute addresses of the breaking points of
  244. the current loop.")
  245. ;;;###autoload
  246. (defun ccl-compile (ccl-program)
  247. "Return the compiled code of CCL-PROGRAM as a vector of integers."
  248. (unless (and (consp ccl-program)
  249. (integerp (car ccl-program))
  250. (listp (car (cdr ccl-program))))
  251. (error "CCL: Invalid CCL program: %s" ccl-program))
  252. (if (null (vectorp ccl-program-vector))
  253. (setq ccl-program-vector (make-vector 8192 0)))
  254. (setq ccl-loop-head nil ccl-breaks nil)
  255. (setq ccl-current-ic 0)
  256. ;; The first element is the buffer magnification.
  257. (ccl-embed-data (car ccl-program))
  258. ;; The second element is the address of the start CCL code for
  259. ;; processing end of input buffer (we call it eof-processor). We
  260. ;; set it later.
  261. (ccl-increment-ic 1)
  262. ;; Compile the main body of the CCL program.
  263. (ccl-compile-1 (car (cdr ccl-program)))
  264. ;; Embed the address of eof-processor.
  265. (ccl-embed-data ccl-current-ic 1)
  266. ;; Then compile eof-processor.
  267. (if (nth 2 ccl-program)
  268. (ccl-compile-1 (nth 2 ccl-program)))
  269. ;; At last, embed termination code.
  270. (ccl-embed-code 'end 0 0)
  271. (let ((vec (make-vector ccl-current-ic 0))
  272. (i 0))
  273. (while (< i ccl-current-ic)
  274. (aset vec i (aref ccl-program-vector i))
  275. (setq i (1+ i)))
  276. vec))
  277. (defun ccl-syntax-error (cmd)
  278. "Signal syntax error."
  279. (error "CCL: Syntax error: %s" cmd))
  280. (defun ccl-check-register (arg cmd)
  281. "Check if ARG is a valid CCL register."
  282. (if (get arg 'ccl-register-number)
  283. arg
  284. (error "CCL: Invalid register %s in %s" arg cmd)))
  285. (defun ccl-check-compile-function (arg cmd)
  286. "Check if ARG is a valid CCL command."
  287. (or (get arg 'ccl-compile-function)
  288. (error "CCL: Invalid command: %s" cmd)))
  289. ;; In the following code, most ccl-compile-XXXX functions return t if
  290. ;; they end with unconditional jump, else return nil.
  291. (defun ccl-compile-1 (ccl-block)
  292. "Compile CCL-BLOCK (see the syntax above)."
  293. (let (unconditional-jump
  294. cmd)
  295. (if (or (integerp ccl-block)
  296. (stringp ccl-block)
  297. (and ccl-block (symbolp (car ccl-block))))
  298. ;; This block consists of single statement.
  299. (setq ccl-block (list ccl-block)))
  300. ;; Now CCL-BLOCK is a list of statements. Compile them one by
  301. ;; one.
  302. (while ccl-block
  303. (setq cmd (car ccl-block))
  304. (setq unconditional-jump
  305. (cond ((integerp cmd)
  306. ;; SET statement for the register 0.
  307. (ccl-compile-set (list 'r0 '= cmd)))
  308. ((stringp cmd)
  309. ;; WRITE statement of string argument.
  310. (ccl-compile-write-string cmd))
  311. ((listp cmd)
  312. ;; The other statements.
  313. (cond ((eq (nth 1 cmd) '=)
  314. ;; SET statement of the form `(REG = EXPRESSION)'.
  315. (ccl-compile-set cmd))
  316. ((and (symbolp (nth 1 cmd))
  317. (get (nth 1 cmd) 'ccl-self-arith-code))
  318. ;; SET statement with an assignment operation.
  319. (ccl-compile-self-set cmd))
  320. (t
  321. (funcall (ccl-check-compile-function (car cmd) cmd)
  322. cmd))))
  323. (t
  324. (ccl-syntax-error cmd))))
  325. (setq ccl-block (cdr ccl-block)))
  326. unconditional-jump))
  327. (defconst ccl-max-short-const (ash 1 19))
  328. (defconst ccl-min-short-const (ash -1 19))
  329. (defun ccl-compile-set (cmd)
  330. "Compile SET statement."
  331. (let ((rrr (ccl-check-register (car cmd) cmd))
  332. (right (nth 2 cmd)))
  333. (cond ((listp right)
  334. ;; CMD has the form `(RRR = (XXX OP YYY))'.
  335. (ccl-compile-expression rrr right))
  336. ((integerp right)
  337. ;; CMD has the form `(RRR = integer)'.
  338. (if (and (<= right ccl-max-short-const)
  339. (>= right ccl-min-short-const))
  340. (ccl-embed-code 'set-short-const rrr right)
  341. (ccl-embed-code 'set-const rrr 0)
  342. (ccl-embed-data right)))
  343. (t
  344. ;; CMD has the form `(RRR = rrr [ array ])'.
  345. (ccl-check-register right cmd)
  346. (let ((ary (nth 3 cmd)))
  347. (if (vectorp ary)
  348. (let ((i 0) (len (length ary)))
  349. (ccl-embed-code 'set-array rrr len right)
  350. (while (< i len)
  351. (ccl-embed-data (aref ary i))
  352. (setq i (1+ i))))
  353. (ccl-embed-code 'set-register rrr 0 right))))))
  354. nil)
  355. (defun ccl-compile-self-set (cmd)
  356. "Compile SET statement with ASSIGNMENT_OPERATOR."
  357. (let ((rrr (ccl-check-register (car cmd) cmd))
  358. (right (nth 2 cmd)))
  359. (if (listp right)
  360. ;; CMD has the form `(RRR ASSIGN_OP (XXX OP YYY))', compile
  361. ;; the right hand part as `(r7 = (XXX OP YYY))' (note: the
  362. ;; register 7 can be used for storing temporary value).
  363. (progn
  364. (ccl-compile-expression 'r7 right)
  365. (setq right 'r7)))
  366. ;; Now CMD has the form `(RRR ASSIGN_OP ARG)'. Compile it as
  367. ;; `(RRR = (RRR OP ARG))'.
  368. (ccl-compile-expression
  369. rrr
  370. (list rrr (intern (substring (symbol-name (nth 1 cmd)) 0 -1)) right)))
  371. nil)
  372. (defun ccl-compile-expression (rrr expr)
  373. "Compile SET statement of the form `(RRR = EXPR)'."
  374. (let ((left (car expr))
  375. (op (get (nth 1 expr) 'ccl-arith-code))
  376. (right (nth 2 expr)))
  377. (if (listp left)
  378. (progn
  379. ;; EXPR has the form `((EXPR2 OP2 ARG) OP RIGHT)'. Compile
  380. ;; the first term as `(r7 = (EXPR2 OP2 ARG)).'
  381. (ccl-compile-expression 'r7 left)
  382. (setq left 'r7)))
  383. ;; Now EXPR has the form (LEFT OP RIGHT).
  384. (if (and (eq rrr left)
  385. (< op (length ccl-assign-arith-table)))
  386. ;; Compile this SET statement as `(RRR OP= RIGHT)'.
  387. (if (integerp right)
  388. (progn
  389. (ccl-embed-code 'set-assign-expr-const rrr (ash op 3) 'r0)
  390. (ccl-embed-data right))
  391. (ccl-check-register right expr)
  392. (ccl-embed-code 'set-assign-expr-register rrr (ash op 3) right))
  393. ;; Compile this SET statement as `(RRR = (LEFT OP RIGHT))'.
  394. (if (integerp right)
  395. (progn
  396. (ccl-embed-code 'set-expr-const rrr (ash op 3) left)
  397. (ccl-embed-data right))
  398. (ccl-check-register right expr)
  399. (ccl-embed-code 'set-expr-register
  400. rrr
  401. (logior (ash op 3) (get right 'ccl-register-number))
  402. left)))))
  403. (defun ccl-compile-write-string (str)
  404. "Compile WRITE statement with string argument."
  405. (let ((len (length str)))
  406. (ccl-embed-code 'write-const-string 1 len)
  407. (ccl-embed-string len str))
  408. nil)
  409. (defun ccl-compile-if (cmd &optional read-flag)
  410. "Compile IF statement of the form `(if CONDITION TRUE-PART FALSE-PART)'.
  411. If READ-FLAG is non-nil, this statement has the form
  412. `(read-if (REG OPERATOR ARG) TRUE-PART FALSE-PART)'."
  413. (if (and (/= (length cmd) 3) (/= (length cmd) 4))
  414. (error "CCL: Invalid number of arguments: %s" cmd))
  415. (let ((condition (nth 1 cmd))
  416. (true-cmds (nth 2 cmd))
  417. (false-cmds (nth 3 cmd))
  418. jump-cond-address
  419. false-ic)
  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. (i 0))
  609. (ccl-embed-code 'write-string-jump 0 ccl-loop-head)
  610. (ccl-embed-data len)
  611. (ccl-embed-string len arg)))
  612. (t
  613. (ccl-check-register arg cmd)
  614. (ccl-embed-code 'write-register-jump arg ccl-loop-head))))
  615. t)
  616. (defun ccl-compile-write-read-repeat (cmd)
  617. "Compile WRITE-READ-REPEAT statement."
  618. (if (or (< (length cmd) 2) (> (length cmd) 3))
  619. (error "CCL: Invalid number of arguments: %s" cmd))
  620. (if (null ccl-loop-head)
  621. (error "CCL: No outer loop: %s" cmd))
  622. (let ((rrr (ccl-check-register (nth 1 cmd) cmd))
  623. (arg (nth 2 cmd)))
  624. (cond ((null arg)
  625. (ccl-embed-code 'write-register-read-jump rrr ccl-loop-head))
  626. ((integerp arg)
  627. (ccl-embed-code 'write-const-read-jump rrr arg ccl-loop-head))
  628. ((vectorp arg)
  629. (let ((len (length arg))
  630. (i 0))
  631. (ccl-embed-code 'write-array-read-jump rrr ccl-loop-head)
  632. (ccl-embed-data len)
  633. (while (< i len)
  634. (ccl-embed-data (aref arg i))
  635. (setq i (1+ i)))))
  636. (t
  637. (error "CCL: Invalid argument %s: %s" arg cmd)))
  638. (ccl-embed-code 'read-jump rrr ccl-loop-head))
  639. t)
  640. (defun ccl-compile-read (cmd)
  641. "Compile READ statement."
  642. (if (< (length cmd) 2)
  643. (error "CCL: Invalid number of arguments: %s" cmd))
  644. (let* ((args (cdr cmd))
  645. (i (1- (length args))))
  646. (while args
  647. (let ((rrr (ccl-check-register (car args) cmd)))
  648. (ccl-embed-code 'read-register rrr i)
  649. (setq args (cdr args) i (1- i)))))
  650. nil)
  651. (defun ccl-compile-read-if (cmd)
  652. "Compile READ-IF statement."
  653. (ccl-compile-if cmd 'read))
  654. (defun ccl-compile-write (cmd)
  655. "Compile WRITE statement."
  656. (if (< (length cmd) 2)
  657. (error "CCL: Invalid number of arguments: %s" cmd))
  658. (let ((rrr (nth 1 cmd)))
  659. (cond ((integerp rrr)
  660. (if (> rrr #xFFFFF)
  661. (ccl-compile-write-string (string rrr))
  662. (ccl-embed-code 'write-const-string 0 rrr)))
  663. ((stringp rrr)
  664. (ccl-compile-write-string rrr))
  665. ((and (symbolp rrr) (vectorp (nth 2 cmd)))
  666. (ccl-check-register rrr cmd)
  667. ;; CMD has the form `(write REG ARRAY)'.
  668. (let* ((arg (nth 2 cmd))
  669. (len (length arg))
  670. (i 0))
  671. (ccl-embed-code 'write-array rrr len)
  672. (while (< i len)
  673. (if (not (integerp (aref arg i)))
  674. (error "CCL: Invalid argument %s: %s" arg cmd))
  675. (ccl-embed-data (aref arg i))
  676. (setq i (1+ i)))))
  677. ((symbolp rrr)
  678. ;; CMD has the form `(write REG ...)'.
  679. (let* ((args (cdr cmd))
  680. (i (1- (length args))))
  681. (while args
  682. (setq rrr (ccl-check-register (car args) cmd))
  683. (ccl-embed-code 'write-register rrr i)
  684. (setq args (cdr args) i (1- i)))))
  685. ((listp rrr)
  686. ;; CMD has the form `(write (LEFT OP RIGHT))'.
  687. (let ((left (car rrr))
  688. (op (get (nth 1 rrr) 'ccl-arith-code))
  689. (right (nth 2 rrr)))
  690. (if (listp left)
  691. (progn
  692. ;; RRR has the form `((EXPR OP2 ARG) OP RIGHT)'.
  693. ;; Compile the first term as `(r7 = (EXPR OP2 ARG))'.
  694. (ccl-compile-expression 'r7 left)
  695. (setq left 'r7)))
  696. ;; Now RRR has the form `(ARG OP RIGHT)'.
  697. (if (integerp right)
  698. (progn
  699. (ccl-embed-code 'write-expr-const 0 (ash op 3) left)
  700. (ccl-embed-data right))
  701. (ccl-check-register right rrr)
  702. (ccl-embed-code 'write-expr-register 0
  703. (logior (ash op 3)
  704. (get right 'ccl-register-number))
  705. left))))
  706. (t
  707. (error "CCL: Invalid argument: %s" cmd))))
  708. nil)
  709. (defun ccl-compile-call (cmd)
  710. "Compile CALL statement."
  711. (if (/= (length cmd) 2)
  712. (error "CCL: Invalid number of arguments: %s" cmd))
  713. (if (not (symbolp (nth 1 cmd)))
  714. (error "CCL: Subroutine should be a symbol: %s" cmd))
  715. (ccl-embed-code 'call 1 0)
  716. (ccl-embed-symbol (nth 1 cmd) 'ccl-program-idx)
  717. nil)
  718. (defun ccl-compile-end (cmd)
  719. "Compile END statement."
  720. (if (/= (length cmd) 1)
  721. (error "CCL: Invalid number of arguments: %s" cmd))
  722. (ccl-embed-code 'end 0 0)
  723. t)
  724. (defun ccl-compile-read-multibyte-character (cmd)
  725. "Compile read-multibyte-character"
  726. (if (/= (length cmd) 3)
  727. (error "CCL: Invalid number of arguments: %s" cmd))
  728. (let ((RRR (nth 1 cmd))
  729. (rrr (nth 2 cmd)))
  730. (ccl-check-register rrr cmd)
  731. (ccl-check-register RRR cmd)
  732. (ccl-embed-extended-command 'read-multibyte-character rrr RRR 0))
  733. nil)
  734. (defun ccl-compile-write-multibyte-character (cmd)
  735. "Compile write-multibyte-character"
  736. (if (/= (length cmd) 3)
  737. (error "CCL: Invalid number of arguments: %s" cmd))
  738. (let ((RRR (nth 1 cmd))
  739. (rrr (nth 2 cmd)))
  740. (ccl-check-register rrr cmd)
  741. (ccl-check-register RRR cmd)
  742. (ccl-embed-extended-command 'write-multibyte-character rrr RRR 0))
  743. nil)
  744. (defun ccl-compile-translate-character (cmd)
  745. "Compile translate-character."
  746. (if (/= (length cmd) 4)
  747. (error "CCL: Invalid number of arguments: %s" cmd))
  748. (let ((Rrr (nth 1 cmd))
  749. (RRR (nth 2 cmd))
  750. (rrr (nth 3 cmd)))
  751. (ccl-check-register rrr cmd)
  752. (ccl-check-register RRR cmd)
  753. (cond ((and (symbolp Rrr) (not (get Rrr 'ccl-register-number)))
  754. (ccl-embed-extended-command 'translate-character-const-tbl
  755. rrr RRR 0)
  756. (ccl-embed-symbol Rrr 'translation-table-id))
  757. (t
  758. (ccl-check-register Rrr cmd)
  759. (ccl-embed-extended-command 'translate-character rrr RRR Rrr))))
  760. nil)
  761. (defun ccl-compile-lookup-integer (cmd)
  762. "Compile lookup-integer."
  763. (if (/= (length cmd) 4)
  764. (error "CCL: Invalid number of arguments: %s" cmd))
  765. (let ((Rrr (nth 1 cmd))
  766. (RRR (nth 2 cmd))
  767. (rrr (nth 3 cmd)))
  768. (ccl-check-register RRR cmd)
  769. (ccl-check-register rrr cmd)
  770. (cond ((and (symbolp Rrr) (not (get Rrr 'ccl-register-number)))
  771. (ccl-embed-extended-command 'lookup-int-const-tbl
  772. rrr RRR 0)
  773. (ccl-embed-symbol Rrr 'translation-hash-table-id))
  774. (t
  775. (error "CCL: non-constant table: %s" cmd)
  776. ;; not implemented:
  777. (ccl-check-register Rrr cmd)
  778. (ccl-embed-extended-command 'lookup-int rrr RRR 0))))
  779. nil)
  780. (defun ccl-compile-lookup-character (cmd)
  781. "Compile lookup-character."
  782. (if (/= (length cmd) 4)
  783. (error "CCL: Invalid number of arguments: %s" cmd))
  784. (let ((Rrr (nth 1 cmd))
  785. (RRR (nth 2 cmd))
  786. (rrr (nth 3 cmd)))
  787. (ccl-check-register RRR cmd)
  788. (ccl-check-register rrr cmd)
  789. (cond ((and (symbolp Rrr) (not (get Rrr 'ccl-register-number)))
  790. (ccl-embed-extended-command 'lookup-char-const-tbl
  791. rrr RRR 0)
  792. (ccl-embed-symbol Rrr 'translation-hash-table-id))
  793. (t
  794. (error "CCL: non-constant table: %s" cmd)
  795. ;; not implemented:
  796. (ccl-check-register Rrr cmd)
  797. (ccl-embed-extended-command 'lookup-char rrr RRR 0))))
  798. nil)
  799. (defun ccl-compile-iterate-multiple-map (cmd)
  800. (ccl-compile-multiple-map-function 'iterate-multiple-map cmd)
  801. nil)
  802. (defun ccl-compile-map-multiple (cmd)
  803. (if (/= (length cmd) 4)
  804. (error "CCL: Invalid number of arguments: %s" cmd))
  805. (let (func arg)
  806. (setq func
  807. (lambda (arg mp)
  808. (let ((len 0) result add)
  809. (while arg
  810. (if (consp (car arg))
  811. (setq add (funcall func (car arg) t)
  812. result (append result add)
  813. add (+ (- (car add)) 1))
  814. (setq result
  815. (append result
  816. (list (car arg)))
  817. add 1))
  818. (setq arg (cdr arg)
  819. len (+ len add)))
  820. (if mp
  821. (cons (- len) result)
  822. result))))
  823. (setq arg (append (list (nth 0 cmd) (nth 1 cmd) (nth 2 cmd))
  824. (funcall func (nth 3 cmd) nil)))
  825. (ccl-compile-multiple-map-function 'map-multiple arg))
  826. nil)
  827. (defun ccl-compile-map-single (cmd)
  828. (if (/= (length cmd) 4)
  829. (error "CCL: Invalid number of arguments: %s" cmd))
  830. (let ((RRR (nth 1 cmd))
  831. (rrr (nth 2 cmd))
  832. (map (nth 3 cmd))
  833. id)
  834. (ccl-check-register rrr cmd)
  835. (ccl-check-register RRR cmd)
  836. (ccl-embed-extended-command 'map-single rrr RRR 0)
  837. (cond ((symbolp map)
  838. (if (get map 'code-conversion-map)
  839. (ccl-embed-symbol map 'code-conversion-map-id)
  840. (error "CCL: Invalid map: %s" map)))
  841. (t
  842. (error "CCL: Invalid type of arguments: %s" cmd))))
  843. nil)
  844. (defun ccl-compile-multiple-map-function (command cmd)
  845. (if (< (length cmd) 4)
  846. (error "CCL: Invalid number of arguments: %s" cmd))
  847. (let ((RRR (nth 1 cmd))
  848. (rrr (nth 2 cmd))
  849. (args (nthcdr 3 cmd))
  850. map)
  851. (ccl-check-register rrr cmd)
  852. (ccl-check-register RRR cmd)
  853. (ccl-embed-extended-command command rrr RRR 0)
  854. (ccl-embed-data (length args))
  855. (while args
  856. (setq map (car args))
  857. (cond ((symbolp map)
  858. (if (get map 'code-conversion-map)
  859. (ccl-embed-symbol map 'code-conversion-map-id)
  860. (error "CCL: Invalid map: %s" map)))
  861. ((numberp map)
  862. (ccl-embed-data map))
  863. (t
  864. (error "CCL: Invalid type of arguments: %s" cmd)))
  865. (setq args (cdr args)))))
  866. ;;; CCL dump stuff
  867. (defvar ccl-code)
  868. ;;;###autoload
  869. (defun ccl-dump (ccl-code)
  870. "Disassemble compiled CCL-CODE."
  871. (let ((len (length ccl-code))
  872. (buffer-mag (aref ccl-code 0)))
  873. (cond ((= buffer-mag 0)
  874. (insert "Don't output anything.\n"))
  875. ((= buffer-mag 1)
  876. (insert "Out-buffer must be as large as in-buffer.\n"))
  877. (t
  878. (insert
  879. (format "Out-buffer must be %d times bigger than in-buffer.\n"
  880. buffer-mag))))
  881. (insert "Main-body:\n")
  882. (setq ccl-current-ic 2)
  883. (if (> (aref ccl-code 1) 0)
  884. (progn
  885. (while (< ccl-current-ic (aref ccl-code 1))
  886. (ccl-dump-1))
  887. (insert "At EOF:\n")))
  888. (while (< ccl-current-ic len)
  889. (ccl-dump-1))
  890. ))
  891. (defun ccl-get-next-code ()
  892. "Return a CCL code in `ccl-code' at `ccl-current-ic'."
  893. (prog1
  894. (aref ccl-code ccl-current-ic)
  895. (setq ccl-current-ic (1+ ccl-current-ic))))
  896. (defun ccl-dump-1 ()
  897. (let* ((code (ccl-get-next-code))
  898. (cmd (aref ccl-code-table (logand code 31)))
  899. (rrr (ash (logand code 255) -5))
  900. (cc (ash code -8)))
  901. (insert (format "%5d:[%s] " (1- ccl-current-ic) cmd))
  902. (funcall (get cmd 'ccl-dump-function) rrr cc)))
  903. (defun ccl-dump-set-register (rrr cc)
  904. (insert (format "r%d = r%d\n" rrr cc)))
  905. (defun ccl-dump-set-short-const (rrr cc)
  906. (insert (format "r%d = %d\n" rrr cc)))
  907. (defun ccl-dump-set-const (rrr ignore)
  908. (insert (format "r%d = %d\n" rrr (ccl-get-next-code))))
  909. (defun ccl-dump-set-array (rrr cc)
  910. (let ((rrr2 (logand cc 7))
  911. (len (ash cc -3))
  912. (i 0))
  913. (insert (format "r%d = array[r%d] of length %d\n\t"
  914. rrr rrr2 len))
  915. (while (< i len)
  916. (insert (format "%d " (ccl-get-next-code)))
  917. (setq i (1+ i)))
  918. (insert "\n")))
  919. (defun ccl-dump-jump (ignore cc &optional address)
  920. (insert (format "jump to %d(" (+ (or address ccl-current-ic) cc)))
  921. (if (>= cc 0)
  922. (insert "+"))
  923. (insert (format "%d)\n" (1+ cc))))
  924. (defun ccl-dump-jump-cond (rrr cc)
  925. (insert (format "if (r%d == 0), " rrr))
  926. (ccl-dump-jump nil cc))
  927. (defun ccl-dump-write-register-jump (rrr cc)
  928. (insert (format "write r%d, " rrr))
  929. (ccl-dump-jump nil cc))
  930. (defun ccl-dump-write-register-read-jump (rrr cc)
  931. (insert (format "write r%d, read r%d, " rrr rrr))
  932. (ccl-dump-jump nil cc)
  933. (ccl-get-next-code) ; Skip dummy READ-JUMP
  934. )
  935. (defun ccl-extract-arith-op (cc)
  936. (aref ccl-arith-table (ash cc -6)))
  937. (defun ccl-dump-write-expr-const (ignore cc)
  938. (insert (format "write (r%d %s %d)\n"
  939. (logand cc 7)
  940. (ccl-extract-arith-op cc)
  941. (ccl-get-next-code))))
  942. (defun ccl-dump-write-expr-register (ignore cc)
  943. (insert (format "write (r%d %s r%d)\n"
  944. (logand cc 7)
  945. (ccl-extract-arith-op cc)
  946. (logand (ash cc -3) 7))))
  947. (defun ccl-dump-insert-char (cc)
  948. (cond ((= cc ?\t) (insert " \"^I\""))
  949. ((= cc ?\n) (insert " \"^J\""))
  950. (t (insert (format " \"%c\"" cc)))))
  951. (defun ccl-dump-write-const-jump (ignore cc)
  952. (let ((address ccl-current-ic))
  953. (insert "write char")
  954. (ccl-dump-insert-char (ccl-get-next-code))
  955. (insert ", ")
  956. (ccl-dump-jump nil cc address)))
  957. (defun ccl-dump-write-const-read-jump (rrr cc)
  958. (let ((address ccl-current-ic))
  959. (insert "write char")
  960. (ccl-dump-insert-char (ccl-get-next-code))
  961. (insert (format ", read r%d, " rrr))
  962. (ccl-dump-jump cc address)
  963. (ccl-get-next-code) ; Skip dummy READ-JUMP
  964. ))
  965. (defun ccl-dump-write-string-jump (ignore cc)
  966. (let ((address ccl-current-ic)
  967. (len (ccl-get-next-code))
  968. (i 0))
  969. (insert "write \"")
  970. (while (< i len)
  971. (let ((code (ccl-get-next-code)))
  972. (insert (ash code -16))
  973. (if (< (1+ i) len) (insert (logand (ash code -8) 255)))
  974. (if (< (+ i 2) len) (insert (logand code 255))))
  975. (setq i (+ i 3)))
  976. (insert "\", ")
  977. (ccl-dump-jump nil cc address)))
  978. (defun ccl-dump-write-array-read-jump (rrr cc)
  979. (let ((address ccl-current-ic)
  980. (len (ccl-get-next-code))
  981. (i 0))
  982. (insert (format "write array[r%d] of length %d,\n\t" rrr len))
  983. (while (< i len)
  984. (ccl-dump-insert-char (ccl-get-next-code))
  985. (setq i (1+ i)))
  986. (insert (format "\n\tthen read r%d, " rrr))
  987. (ccl-dump-jump nil cc address)
  988. (ccl-get-next-code) ; Skip dummy READ-JUMP.
  989. ))
  990. (defun ccl-dump-read-jump (rrr cc)
  991. (insert (format "read r%d, " rrr))
  992. (ccl-dump-jump nil cc))
  993. (defun ccl-dump-branch (rrr len)
  994. (let ((jump-table-head ccl-current-ic)
  995. (i 0))
  996. (insert (format "jump to array[r%d] of length %d\n\t" rrr len))
  997. (while (<= i len)
  998. (insert (format "%d " (+ jump-table-head (ccl-get-next-code))))
  999. (setq i (1+ i)))
  1000. (insert "\n")))
  1001. (defun ccl-dump-read-register (rrr cc)
  1002. (insert (format "read r%d (%d remaining)\n" rrr cc)))
  1003. (defun ccl-dump-read-branch (rrr len)
  1004. (insert (format "read r%d, " rrr))
  1005. (ccl-dump-branch rrr len))
  1006. (defun ccl-dump-write-register (rrr cc)
  1007. (insert (format "write r%d (%d remaining)\n" rrr cc)))
  1008. (defun ccl-dump-call (ignore cc)
  1009. (let ((subroutine (car (ccl-get-next-code))))
  1010. (insert (format "call subroutine `%s'\n" subroutine))))
  1011. (defun ccl-dump-write-const-string (rrr cc)
  1012. (if (= rrr 0)
  1013. (progn
  1014. (insert "write char")
  1015. (ccl-dump-insert-char cc)
  1016. (newline))
  1017. (let ((len cc)
  1018. (i 0))
  1019. (insert "write \"")
  1020. (while (< i len)
  1021. (let ((code (ccl-get-next-code)))
  1022. (if (/= (logand code #x1000000) 0)
  1023. (progn
  1024. (insert (logand code #xFFFFFF))
  1025. (setq i (1+ i)))
  1026. (insert (format "%c" (lsh code -16)))
  1027. (if (< (1+ i) len)
  1028. (insert (format "%c" (logand (lsh code -8) 255))))
  1029. (if (< (+ i 2) len)
  1030. (insert (format "%c" (logand code 255))))
  1031. (setq i (+ i 3)))))
  1032. (insert "\"\n"))))
  1033. (defun ccl-dump-write-array (rrr cc)
  1034. (let ((i 0))
  1035. (insert (format "write array[r%d] of length %d\n\t" rrr cc))
  1036. (while (< i cc)
  1037. (ccl-dump-insert-char (ccl-get-next-code))
  1038. (setq i (1+ i)))
  1039. (insert "\n")))
  1040. (defun ccl-dump-end (&rest ignore)
  1041. (insert "end\n"))
  1042. (defun ccl-dump-set-assign-expr-const (rrr cc)
  1043. (insert (format "r%d %s= %d\n"
  1044. rrr
  1045. (ccl-extract-arith-op cc)
  1046. (ccl-get-next-code))))
  1047. (defun ccl-dump-set-assign-expr-register (rrr cc)
  1048. (insert (format "r%d %s= r%d\n"
  1049. rrr
  1050. (ccl-extract-arith-op cc)
  1051. (logand cc 7))))
  1052. (defun ccl-dump-set-expr-const (rrr cc)
  1053. (insert (format "r%d = r%d %s %d\n"
  1054. rrr
  1055. (logand cc 7)
  1056. (ccl-extract-arith-op cc)
  1057. (ccl-get-next-code))))
  1058. (defun ccl-dump-set-expr-register (rrr cc)
  1059. (insert (format "r%d = r%d %s r%d\n"
  1060. rrr
  1061. (logand cc 7)
  1062. (ccl-extract-arith-op cc)
  1063. (logand (ash cc -3) 7))))
  1064. (defun ccl-dump-jump-cond-expr-const (rrr cc)
  1065. (let ((address ccl-current-ic))
  1066. (insert (format "if !(r%d %s %d), "
  1067. rrr
  1068. (aref ccl-arith-table (ccl-get-next-code))
  1069. (ccl-get-next-code)))
  1070. (ccl-dump-jump nil cc address)))
  1071. (defun ccl-dump-jump-cond-expr-register (rrr cc)
  1072. (let ((address ccl-current-ic))
  1073. (insert (format "if !(r%d %s r%d), "
  1074. rrr
  1075. (aref ccl-arith-table (ccl-get-next-code))
  1076. (ccl-get-next-code)))
  1077. (ccl-dump-jump nil cc address)))
  1078. (defun ccl-dump-read-jump-cond-expr-const (rrr cc)
  1079. (insert (format "read r%d, " rrr))
  1080. (ccl-dump-jump-cond-expr-const rrr cc))
  1081. (defun ccl-dump-read-jump-cond-expr-register (rrr cc)
  1082. (insert (format "read r%d, " rrr))
  1083. (ccl-dump-jump-cond-expr-register rrr cc))
  1084. (defun ccl-dump-binary (ccl-code)
  1085. (let ((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. LOOP := (loop STATEMENT [STATEMENT ...])
  1196. ;; Terminate the most inner loop.
  1197. BREAK := (break)
  1198. REPEAT :=
  1199. ;; Jump to the head of the most inner loop.
  1200. (repeat)
  1201. ;; Same as: ((write [REG | integer | string])
  1202. ;; (repeat))
  1203. | (write-repeat [REG | integer | string])
  1204. ;; Same as: ((write REG [ARRAY])
  1205. ;; (read REG)
  1206. ;; (repeat))
  1207. | (write-read-repeat REG [ARRAY])
  1208. ;; Same as: ((write integer)
  1209. ;; (read REG)
  1210. ;; (repeat))
  1211. | (write-read-repeat REG integer)
  1212. READ := ;; Set REG_0 to a byte read from the input text, set REG_1
  1213. ;; to the next byte read, and so on.
  1214. (read REG_0 [REG_1 ...])
  1215. ;; Same as: ((read REG)
  1216. ;; (if (REG OPERATOR ARG) CCL_BLOCK_0 CCL_BLOCK_1))
  1217. | (read-if (REG OPERATOR ARG) CCL_BLOCK_0 CCL_BLOCK_1)
  1218. ;; Same as: ((read REG)
  1219. ;; (branch REG CCL_BLOCK_0 [CCL_BLOCK_1 ...]))
  1220. | (read-branch REG CCL_BLOCK_0 [CCL_BLOCK_1 ...])
  1221. ;; Read a character from the input text while parsing
  1222. ;; multibyte representation, set REG_0 to the charset ID of
  1223. ;; the character, set REG_1 to the code point of the
  1224. ;; character. If the dimension of charset is two, set REG_1
  1225. ;; to ((CODE0 << 7) | CODE1), where CODE0 is the first code
  1226. ;; point and CODE1 is the second code point.
  1227. | (read-multibyte-character REG_0 REG_1)
  1228. WRITE :=
  1229. ;; Write REG_0, REG_1, ... to the output buffer. If REG_N is
  1230. ;; a multibyte character, write the corresponding multibyte
  1231. ;; representation.
  1232. (write REG_0 [REG_1 ...])
  1233. ;; Same as: ((r7 = EXPRESSION)
  1234. ;; (write r7))
  1235. | (write EXPRESSION)
  1236. ;; Write the value of `integer' to the output buffer. If it
  1237. ;; is a multibyte character, write the corresponding multibyte
  1238. ;; representation.
  1239. | (write integer)
  1240. ;; Write the byte sequence of `string' as is to the output
  1241. ;; buffer.
  1242. | (write string)
  1243. ;; Same as: (write string)
  1244. | string
  1245. ;; Provided that the value of REG is N, write Nth element of
  1246. ;; ARRAY to the output buffer. If it is a multibyte
  1247. ;; character, write the corresponding multibyte
  1248. ;; representation.
  1249. | (write REG ARRAY)
  1250. ;; Write a multibyte representation of a character whose
  1251. ;; charset ID is REG_0 and code point is REG_1. If the
  1252. ;; dimension of the charset is two, REG_1 should be ((CODE0 <<
  1253. ;; 7) | CODE1), where CODE0 is the first code point and CODE1
  1254. ;; is the second code point of the character.
  1255. | (write-multibyte-character REG_0 REG_1)
  1256. ;; Call CCL program whose name is ccl-program-name.
  1257. CALL := (call ccl-program-name)
  1258. ;; Terminate the CCL program.
  1259. END := (end)
  1260. ;; CCL registers that can contain any integer value. As r7 is also
  1261. ;; used by CCL interpreter, its value is changed unexpectedly.
  1262. REG := r0 | r1 | r2 | r3 | r4 | r5 | r6 | r7
  1263. ARG := REG | integer
  1264. OPERATOR :=
  1265. ;; Normal arithmetic operators (same meaning as C code).
  1266. + | - | * | / | %
  1267. ;; Bitwise operators (same meaning as C code)
  1268. | & | `|' | ^
  1269. ;; Shifting operators (same meaning as C code)
  1270. | << | >>
  1271. ;; (REG = ARG_0 <8 ARG_1) means:
  1272. ;; (REG = ((ARG_0 << 8) | ARG_1))
  1273. | <8
  1274. ;; (REG = ARG_0 >8 ARG_1) means:
  1275. ;; ((REG = (ARG_0 >> 8))
  1276. ;; (r7 = (ARG_0 & 255)))
  1277. | >8
  1278. ;; (REG = ARG_0 // ARG_1) means:
  1279. ;; ((REG = (ARG_0 / ARG_1))
  1280. ;; (r7 = (ARG_0 % ARG_1)))
  1281. | //
  1282. ;; Normal comparing operators (same meaning as C code)
  1283. | < | > | == | <= | >= | !=
  1284. ;; If ARG_0 and ARG_1 are higher and lower byte of Shift-JIS
  1285. ;; code, and CHAR is the corresponding JISX0208 character,
  1286. ;; (REG = ARG_0 de-sjis ARG_1) means:
  1287. ;; ((REG = CODE0)
  1288. ;; (r7 = CODE1))
  1289. ;; where CODE0 is the first code point of CHAR, CODE1 is the
  1290. ;; second code point of CHAR.
  1291. | de-sjis
  1292. ;; If ARG_0 and ARG_1 are the first and second code point of
  1293. ;; JISX0208 character CHAR, and SJIS is the corresponding
  1294. ;; Shift-JIS code,
  1295. ;; (REG = ARG_0 en-sjis ARG_1) means:
  1296. ;; ((REG = HIGH)
  1297. ;; (r7 = LOW))
  1298. ;; where HIGH is the higher byte of SJIS, LOW is the lower
  1299. ;; byte of SJIS.
  1300. | en-sjis
  1301. ASSIGNMENT_OPERATOR :=
  1302. ;; Same meaning as C code
  1303. += | -= | *= | /= | %= | &= | `|=' | ^= | <<= | >>=
  1304. ;; (REG <8= ARG) is the same as:
  1305. ;; ((REG <<= 8)
  1306. ;; (REG |= ARG))
  1307. | <8=
  1308. ;; (REG >8= ARG) is the same as:
  1309. ;; ((r7 = (REG & 255))
  1310. ;; (REG >>= 8))
  1311. ;; (REG //= ARG) is the same as:
  1312. ;; ((r7 = (REG % ARG))
  1313. ;; (REG /= ARG))
  1314. | //=
  1315. ARRAY := `[' integer ... `]'
  1316. TRANSLATE :=
  1317. (translate-character REG(table) REG(charset) REG(codepoint))
  1318. | (translate-character SYMBOL REG(charset) REG(codepoint))
  1319. ;; SYMBOL must refer to a table defined by `define-translation-table'.
  1320. LOOKUP :=
  1321. (lookup-character SYMBOL REG(charset) REG(codepoint))
  1322. | (lookup-integer SYMBOL REG(integer))
  1323. ;; SYMBOL refers to a table defined by `define-translation-hash-table'.
  1324. MAP :=
  1325. (iterate-multiple-map REG REG MAP-IDs)
  1326. | (map-multiple REG REG (MAP-SET))
  1327. | (map-single REG REG MAP-ID)
  1328. MAP-IDs := MAP-ID ...
  1329. MAP-SET := MAP-IDs | (MAP-IDs) MAP-SET
  1330. MAP-ID := integer
  1331. "
  1332. (declare (doc-string 3))
  1333. `(let ((prog ,(unwind-protect
  1334. (progn
  1335. ;; To make ,(charset-id CHARSET) works well.
  1336. (fset 'charset-id 'charset-id-internal)
  1337. (ccl-compile (eval ccl-program)))
  1338. (fmakunbound 'charset-id))))
  1339. (defconst ,name prog ,doc)
  1340. (put ',name 'ccl-program-idx (register-ccl-program ',name prog))
  1341. nil))
  1342. ;;;###autoload
  1343. (defmacro check-ccl-program (ccl-program &optional name)
  1344. "Check validity of CCL-PROGRAM.
  1345. If CCL-PROGRAM is a symbol denoting a CCL program, return
  1346. CCL-PROGRAM, else return nil.
  1347. If CCL-PROGRAM is a vector and optional arg NAME (symbol) is supplied,
  1348. register CCL-PROGRAM by name NAME, and return NAME."
  1349. `(if (ccl-program-p ,ccl-program)
  1350. (if (vectorp ,ccl-program)
  1351. (progn
  1352. (register-ccl-program ,name ,ccl-program)
  1353. ,name)
  1354. ,ccl-program)))
  1355. ;;;###autoload
  1356. (defun ccl-execute-with-args (ccl-prog &rest args)
  1357. "Execute CCL-PROGRAM with registers initialized by the remaining args.
  1358. The return value is a vector of resulting CCL registers.
  1359. See the documentation of `define-ccl-program' for the detail of CCL program."
  1360. (let ((reg (make-vector 8 0))
  1361. (i 0))
  1362. (while (and args (< i 8))
  1363. (if (not (integerp (car args)))
  1364. (error "Arguments should be integer"))
  1365. (aset reg i (car args))
  1366. (setq args (cdr args) i (1+ i)))
  1367. (ccl-execute ccl-prog reg)
  1368. reg))
  1369. (provide 'ccl)
  1370. ;;; ccl.el ends here