(defun fix-kernel ()
  "Do some more translating of the KSOS-11 kernel source from Modula to Modula-2."
  (interactive "*")
  (make-local-variable 'x)
  (make-local-variable 'old-fold)
  (setq old-fold case-fold-search)
  (setq case-fold-search nil)
  (goto-char 0)
  (search-forward "{}" nil nil 3)	; skip nullBits16 & nullBits32
  (replace-string "{}" "nullBits16")
  (goto-char 0)
  (progn (search-forward "beyondPhysMem")
	 (end-of-line)
	 (kill-word 1)
	 (delete-char 1)
	 (forward-word 1)
	 (delete-char 1))
  (teco-fs "4C" "4")				; swap disk types
  (teco-fs "4C" "4")
  (teco-fs "cardinal16" "[0 .. 40601]")		; exceptionType
  (search-forward "integer16")
  (beginning-of-line)
  (insert "\tbits16\t\t= SET OF [0..15];\n\n")
  (insert "\tcardinal16\t= [0..65535];\n\n")
  (insert "\tinteger16\t= [-32768..32767];\n\n")
  (progn (search-forward "bits32")
	 (search-forward "=")
	 (kill-word 4)
	 (upcase-word 1)
	 (delete-char -2)
	 (insert "ET"))
  (progn (search-forward "cardinal32")
	 (search-forward "=")
	 (kill-word 4)
	 (upcase-word 1)
	 (delete-char -2))
  (progn (search-forward "nspType")
	 (beginning-of-line 0)
	 (kill-line 2)
	 (search-forward "seid")
	 (beginning-of-line 0)
	 (yank)
	 (teco-fs "byte" "[nullNsp .. highFileNsp]" -1))
  (teco-fs "byte" "[-1 .. 254]")
  (progn (search-forward "privStruct")
	 (beginning-of-line)
	 (kill-line 2)
	 (search-forward "====")
	 (beginning-of-line)
	 (yank)
	 (teco-fs "bits32" "SET OF privNames" -1))
  (teco-fs "SIZE" "size")
  (progn (search-backward "CONST")
	 (setq x 11)
	 (while (> x 0)
	   (search-forward "CHAR(")
	   (kill-word -1)
	   (search-forward ")")
	   (delete-char -1)
	   (setq x (1- x))
	   ))
  (progn (search-forward "nullBits32")
	 (end-of-line)
	 (search-backward ";")
	 (kill-word -1)
	 (insert "{}"))
  (progn (search-forward "ASSERT")
	 (teco-fs "(*" "")
	 (just-one-space)
	 (teco-fs "halt" "kernelHalt(__FILE__,__LINE__)")
	 (teco-fs " *)" ""))
  (progn (setq x 5)
	 (while (> x 0)
	   (search-forward "cardinal16(")
	   (kill-word -1)
	   (search-forward ")")
	   (delete-char -1)
	   (setq x (1- x))
	   ))
  (progn (search-backward "/ ")
	 (delete-char 1)
	 (insert "DIV"))
  (progn (search-forward "switchRegister")
	 (beginning-of-line)
	 (insert "#ifdef PROF\nCONST\n\tprofileTableMax = 700;\n\nVAR\n\tprofileTable : ARRAY [0..profileTableMax] OF cardinal32;\n#endif PROF\n\n"))
  (progn (search-forward "procContext")
	 (forward-word 1)
	 (forward-word -1)
	 (kill-word 2)
	 (insert "procContextAdr")
	 (beginning-of-line)
	 (insert "\nCONST\tprocContextAdr = ")
	 (yank)
	 (insert ";\nVAR")
	 (setq x 38)
	 (while (> x 0)
	   (teco-fs "ADR( procContext )" "procContextAdr")
	   (setq x (1- x))
	   ))
  (fix-import-list "KSMinit" "cardinal16, integer16,")
  (fix-import-list "KLIB" "signal, bits16, integer16, cardinal16, nullBits16,")
  (fix-import-list "typewriter" "signal, bits16, integer16, cardinal16, nullBits16,")
  (fix-import-list "KUT" "integer16, cardinal16,")
  (fix-import-list "OSAL" "cardinal16, integer16,")
  (fix-import-list "SMX" "bits16, kernelHalt")
  (progn (re-search-forward "MODULE[ \t]*sig")
	 (search-forward "EXPORT")
	 (beginning-of-line)
	 (insert "IMPORT\n\tADR, cardinal16, wait, awaited, send, kernelHalt;\n\n"))
  (fix-import-list "strMap" "cardinal16, integer16, kernelHalt,")
  (fix-import-list "devTime" "signal, cardinal16, integer16, bits16, ADR, SIZE, wait, awaited, send, waitForInterrupt,")
  (fix-import-list "clock" "ADR, SIZE, bits16, cardinal16, wait, awaited, send, waitForInterrupt,")
  (fix-import-list "memAcc" "cardinal16, bits16, kernelHalt,")
  (fix-import-list "nucAdr" "ADR, SIZE, cardinal16, integer16,")
  (fix-import-list "que" "integer16, cardinal16, kernelHalt,")
  (fix-import-list "schPgnt" "integer16, kernelHalt,")
  (fix-import-list "swpMap" "cardinal16, integer16, bits16, kernelHalt,")
  (fix-import-list "KSPrandom" "cardinal16,")
  (fix-import-list "SAC" "bits16, kernelHalt,")
  (fix-import-list "STMG" "cardinal16, integer16, bits16, kernelHalt,")
  (fix-import-list "SPADa" "kernelHalt,")
  (fix-import-list "VMMGa" "cardinal16, integer16, nullBits16, kernelHalt,")
  (fix-import-list "mem" "integer16, cardinal16,")
  (fix-import-list "map" "ADR, SIZE, cardinal16, integer16, signal, send, wait, kernelHalt,")
  (fix-import-list "psdDel" "integer16,")
  (fix-import-list "mesPse" "SIZE, ADR, integer16, cardinal16, bits16, nullBits16, kernelHalt,")
  (fix-import-list "mesBuf" "integer16, nullBits16, kernelHalt,")
  (fix-import-list "pse" "SIZE, ADR, integer16, cardinal16, bits16, nullBits16, kernelHalt,")
  (fix-import-list "mes" "integer16,")
  (fix-import-list "devTrap" "ADR, signal, cardinal16, bits16, integer16, doio, send, wait, kernelHalt,")
  (progn (fix-import-list "drivers" "ADR, bits16, cardinal16, integer16, doio, send, wait, kernelHalt,")
	 (teco-fs ")( integer16( sTrap )" ""))
  (fix-import-list "mesAud" "integer16, cardinal16,")
  (progn (search-forward "terminalPathItem")
	 (beginning-of-line 6)
	 (insert "\nterminalPaths = ARRAY [0..pathsPerTerminal-1] OF terminalPathItem;\n")
	 (search-forward "perPathData: ")
	 (kill-line 1)
	 (insert "terminalPaths;\n"))
  (progn (search-forward "allbits")		; in module IODnew
	 (teco-fs "15" "31")
	 (search-forward "allbits")
	 (forward-word 1)
	 (delete-char 1)
	 (forward-word -1)
	 (kill-word -2))
  (fix-import-list "IODtimerMonitor" "cardinal16, wait, send, kernelHalt,")
  (fix-import-list "IODRWP" "bits16, cardinal16, integer16, nullBits16,, kernelHalt")
  (fix-import-list "initRWP" "bits16, cardinal16, kernelHalt,")
  (fix-import-list "IODRWS04" "nullBits16,")
  (fix-import-list "IODTWE16" "bits16, cardinal16, integer16, nullBits16,")
  (fix-import-list "initTWE16" "bits16,")
  (fix-import-list "IODLP11" "nullBits16,")
  (fix-import-list "IODDL11" "ADR, bits16, cardinal16, nullBits16, signal, doio, send, wait,")
  (fix-import-list "DL11atom" "bits16, cardinal16, signal, doio, send, wait,")
  (fix-import-list "initDL11" "cardinal16,")
  (fix-import-list "IODDH11" "nullBits16,")
  (progn (fix-import-list "IODLHDH" "ADR, SIZE, bits16, cardinal16, integer16, nullBits16, signal, send, doio, wait, kernelHalt,")
	 (kill-lines-containing "makec32" 0 1))
  (fix-import-list "memory" "ADR, SIZE, cardinal16,")
  (fix-import-list "buf" "ADR, SIZE, bits16, cardinal16, kernelHalt,")
  (progn (fix-import-list "events" "bits16, cardinal16, send, doio, wait,")
	 (kill-lines-containing "makec32" 0 1)
	 (insert "\t\tbasePtr[0] := 0;\n")
	 (insert "\t\tbasePtr[1] := phys;\n"))
  (fix-import-list "IODsubt" "cardinal16,")
  (fix-import-list "IOEsys" "ADR, SIZE, cardinal16, kernelHalt,")
  (progn (fix-import-list "IOF" "nullPointer16,")
	 (search-forward "nullPointer16")
	 (beginning-of-line)
	 (kill-line 1)
	 (insert "\t\t\t\t\t(* nullPointer16 in the hi order word of a *)\n")
	 (search-forward "lowMount")
	 (forward-word 1)
	 (kill-word -1)
	 (delete-char 1)
	 (forward-word 1)
	 (kill-word 2))
  (fix-import-list "poftServices" "cardinal16, kernelHalt,")
  (fix-import-list "SWE" "cardinal16, bits16, kernelHalt,")
  (fix-import-list "SIRH" "bits16, kernelHalt,")
  (fix-import-list "SWEswap" "bits16, cardinal16, kernelHalt,")
  (fix-import-list "swpSan" "ADR, SIZE, cardinal16, integer16,")
  (fix-import-list "swpPcs" "signal, cardinal16, integer16, bits16, nullBits16, wait, awaited, send, kernelHalt,")
  (fix-import-list "TSM" "cardinal16, bits16, integer16, nullBits16,")
  (fix-import-list "typeKtype" "cardinal16, bits16, integer16, nullBits16,")
  (fix-import-list "PSMG" "kernelHalt,")
  (fix-import-list "VMMGc" "integer16, cardinal16, bits16, kernelHalt,")
  (fix-import-list "SPADc" "bits16, cardinal16, nullBits16, kernelHalt,")
  (fix-import-list "SURH" "bits16, cardinal16, kernelHalt,")
  (fix-import-list "SMEA" "cardinal16,")
  (progn (fix-import-list "SMCL" "bits16, nullBits16, kernelHalt,")
	 (teco-fs "SIZE:" "size:")
	 (teco-fs "SIZE;" "size;")
	 (teco-fs "SIZE)" "size)"))
  (fix-import-list "SMPC" "cardinal16, bits16, kernelHalt,")
  (fix-import-list "mesEmp" "integer16, nullBits16,")
  (fix-import-list "sch" "signal, cardinal16, integer16, awaited, send, wait, kernelHalt,")
  (fix-import-list "schSem" "integer16, cardinal16, kernelHalt,")
  (fix-import-list "devDel" "cardinal16, signal, integer16, nullBits16, wait, send, kernelHalt,")
  (fix-import-list "nucCntx" "bits16, cardinal16, integer16, nullBits16, kernelHalt,")
  (fix-import-list "access" "bits16, cardinal16, integer16, kernelHalt,")
  (fix-import-list "swp" "integer16,")
  (fix-import-list "gat" "cardinal16, signal, bits16, waitForInterrupt, send, wait, integer16, kernelHalt,")
  (fix-import-list "gatInward" "cardinal16, signal, bits16, waitForInterrupt, send, wait, kernelHalt,")
  (fix-import-list "psd" "ADR, bits16, cardinal16, integer16, nullBits16, signal, send, wait, kernelHalt,")
  (progn (fix-import-list "psdNew" "bits16, cardinal16, integer16, nullBits16, signal, send, wait, kernelHalt,")
	 (teco-fs ")(0" ""))
  (fix-import-list "psdStat" "integer16,")
  (fix-import-list "psdIpc" "integer16, cardinal16, nullBits16, kernelHalt,")
  (fix-import-list "psdCan" "integer16,")
  (fix-import-list "inv" "ADR, integer16, cardinal16, nullBits16, kernelHalt,")
  (teco-fs ")(0" "")			; in module memPar
  (goto-char 0)
  (replace-string "(*0*)" "{0}")
  (goto-char 0)
  (replace-string ".SIZE" ".size")
  (goto-char 0)
  (replace-string "doio;" "waitForInterrupt;")
  (goto-char 0)
  (while (search-forward "IMPORT" nil t)
    (re-search-forward "[,;]")
    (forward-word -1)
    (setq col (current-column))
    (search-backward "IMPORT")
    (forward-word 1)
    (if (equal (following-char) 10)
	(forward-char 1)
      (insert "\n")
      )
    (insert "#ifdef PROF\nprofileTable,\n#endif PROF\n")
    (beginning-of-line -1)
    (indent-to col 0))
  (setq case-fold-search old-fold)
  )

(defun fix-import-list (module symbols)
  (interactive "*sModule name: \nsSymbols to add: ")
  (re-search-forward (concat "MODULE[ \t]*" module))
  (search-forward "IMPORT")
  (re-search-forward "[,;]")
  (forward-word -1)
  (setq col (current-column))
  (search-backward "IMPORT")
  (forward-word 1)
  (if (equal (following-char) 10)
      (forward-char 1)
    (insert "\n")
    )
  (indent-to col 0)
  (insert (format "%s\n" symbols))
  )

(defun kill-lines-containing (string before after)
  (interactive "*sString: \nnLines before: \nnLines after: \n")
  (search-forward string)
  (kill-line (- before))
  (kill-line after)
  )

(defun teco-fs (from-string to-string &optional arg)
  "Perform the TECO \"fs\" (find and substitute) command.
Replaces one occurrence of FROM-STRING with TO-STRING -- the ARGth
occurrence, to be precise.  Negative ARG means search backward.

When calling from a program, nil means \"no arg\",
a number counts as a prefix arg."
  (interactive "*sFind: \nsFind \"%s\" and substitute with: \nP")
  (if (or (equal arg nil) (equal arg 0))
      (setq arg 1))
  (if (< arg 0)
      (search-backward from-string nil nil (- arg))
    (search-forward from-string nil nil arg)
    )
  (replace-match to-string t t)
  )
