% \iffalse meta-comment
%
% Template : look for NAME DATE DESC VERSION !!!
% and 'derived files' !!!
%
% Written in 2009, 2010 by Manuel Pégourié-Gonnard and Élie Roux.
%     <mpg@elzevir.fr>
%     <elie.roux@telecom-bretagne.eu>
%
% This work is under the CC0 license.
%
% This work consists of the main source file luatexbase-regs.dtx
% and the derived files
%   luatexbase-regs.pdf luatexbase-regs.sty
%   test-regs-plain.tex test-regs-latex.tex
%
% Unpacking:
%    tex luatexbase-regs.dtx
% Documentation:
%    pdflatex luatexbase-regs.dtx
%
%    The class ltxdoc loads the configuration file ltxdoc.cfg
%    if available. Here you can specify further options, e.g.
%    use A4 as paper format:
%       \PassOptionsToClass{a4paper}{article}
%
%<*ignore>
\begingroup
  \def\x{LaTeX2e}%
\expandafter\endgroup
\ifcase 0\ifx\install y1\fi\expandafter
         \ifx\csname processbatchFile\endcsname\relax\else1\fi
         \ifx\fmtname\x\else 1\fi\relax
\else\csname fi\endcsname
%</ignore>
%<*install>
\input docstrip.tex

\keepsilent
\askforoverwritefalse

\preamble
This is a generated file.

Written in 2009, 2010 by Manuel P'egouri'e-Gonnard and 'Elie Roux.
    <mpg@elzevir.fr>
    <elie.roux@telecom-bretagne.eu>

This work is under the CC0 license.

This work consists of the main source file luatexbase-regs.dtx
and the derived files
    luatexbase-regs.pdf luatexbase-regs.sty
    test-regs-plain.tex test-regs-latex.tex

\endpreamble

\generate{%
  \usedir{tex/luatex/luatexbase}%
  \file{luatexbase-regs.sty}{\from{luatexbase-regs.dtx}{texpackage}}%
}

\generate{%
  \usedir{doc/luatex/luatexbase}%
  \file{test-regs-plain.tex}{\from{luatexbase-regs.dtx}{testplain}}%
  \file{test-regs-latex.tex}{\from{luatexbase-regs.dtx}{testlatex}}%
}

\obeyspaces
\Msg{************************************************************************}
\Msg{*}
\Msg{* To finish the installation you have to move the following}
\Msg{* files into a directory searched by TeX:}
\Msg{*}
\Msg{*     luatexbase-regs.sty luatexbase-regs-latex.tex ...}
\Msg{*}
\Msg{* Happy TeXing!}
\Msg{*}
\Msg{************************************************************************}

\endbatchfile
%</install>
%<*ignore>
\fi
%</ignore>
%<*driver>
\NeedsTeXFormat{LaTeX2e}
\ProvidesFile{luatexbase-regs.drv}
  [DATE DESC]
\documentclass{ltxdoc}
\makeatletter
\newcommand\eTeX{$\m@th\varepsilon$-\TeX}
\newcommand\LuaTeX{Lua\TeX}
\makeatother
\EnableCrossrefs
\CodelineIndex
\begin{document}
  \DocInput{luatexbase-regs.dtx}%
\end{document}
%</driver>
% \fi
%
% \CheckSum{0}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
% \GetFileInfo{luatexbase-regs.drv}
%
% \title{The \textsf{luatexbase-regs} package}
% \date{DATE}
% \author{%
%  Manuel P\'egouri\'e-Gonnard \\ \texttt{mpg@elzevir.fr} \and
%   \'Elie Roux \\ \texttt{elie.roux@telecom-bretagne.eu}}
%
% \maketitle
%
% \begin{abstract}
% This package extends the register allocation scheme of Plain~\TeX\ and
% \LaTeX\ to take advantage of the increased number of registers available in
% \LuaTeX.
% \end{abstract}
%
% \section{Documentation}
%
% Since the Plain~\TeX\ and \LaTeX\ formats are both frozen, they fail to take
% into account the extended resources provided by newer \TeX-like engines.
% This package focuses on the allocation scheme for registers. \TeX82 provides
% $6$ kinds or registers: count, dimen, skip, muskip, box and toks and has
% $256$ registers of each kind. \eTeX\ and most of its descendants add one
% kind of register (marks) and offers $2^{15} = 32768$ of each kind. \LuaTeX\
% provides $2^{16} = 65536$ registers of each kind. (It also provides new
% register-like resources, but this package addresses only the resources
% inherited from \eTeX.)
%
% More precisely, \textsf{luatexbase-regs} loads the \textsf{etex} package (or
% makes sure it is preloaded in the format) and then adapts it to the new
% limits of \LuaTeX. Thus, all macros defined by the \textsf{etex} package are
% made available (most notably, \verb+\loccount+, \verb+\globcountblk+,
% \verb+\loccountblk+ and alike). However, if a register of some kind has been
% locally allocated before this package is loaded, then the number of
% allocatable registers of this kind will not be extended to $65536$. To avoid
% this, load \textsf{luatexbase-regs} earlier.
%
% The Plain~\TeX\ and \LaTeX\ formats define a new kind of resource:
% \emph{inserts} which are merely a family (count, dimen, skip, box) of
% registers with the same number. Inserts allocation begins at $255$ and goes
% toward $0$. Thus we can make room for more inserts by making allocation of
% count-, dimen-, skip- and box-registers start from $256$. With real \eTeX,
% it may be a bad idea since registers with index greater than $256$ have
% degraded performance due to implementation details, but with \LuaTeX the
% performance is uniform, so we just do it.
%
% \clearpage
%
%    \section{Implementation}
%
%    \begin{macrocode}
%<*texpackage>
%    \end{macrocode}
%
%    \subsection{Preliminaries}
%
%    Reload protection, especially for Plain TeX.
%
%    \begin{macrocode}
                \csname lltxb@regs@loaded\endcsname
\expandafter\let\csname lltxb@regs@loaded\endcsname\endinput
%    \end{macrocode}
%
%    Package declaration.
%
%    \begin{macrocode}
\begingroup
  \expandafter\ifx\csname ProvidesPackage\endcsname\relax
    \def\x#1[#2]{\immediate\write16{Package: #1 #2}}
  \else
    \let\x\ProvidesPackage
  \fi
\expandafter\endgroup
\x{luatexbase-regs}[2010/01/14  Registers allocation for LuaTeX  (mpg)]
%    \end{macrocode}
%
%    Make sure \LuaTeX\ is used.
%
%    \begin{macrocode}
\begingroup\expandafter\expandafter\expandafter\endgroup
\expandafter\ifx\csname RequirePackage\endcsname\relax
  \input ifluatex.sty
\else
  \RequirePackage{ifluatex}
\fi
\ifluatex\else
  \begingroup
    \expandafter\ifx\csname PackageWarningNoLine\endcsname\relax
      \def\x#1#2{\begingroup\newlinechar10
        \immediate\write16{Package #1 warning: #2}\endgroup}
    \else
      \let\x\PackageWarningNoLine
    \fi
  \expandafter\endgroup
  \x{luatexbase-regs}{LuaTeX is required for this package.^^J
    Aborting package loading.}
  \expandafter\endinput
\fi
%    \end{macrocode}
%
%    Make sure the catcode of @ is correct, especially for Plain~\TeX.
%
%    \begin{macrocode}
\expandafter\edef\csname lltxb@AtEnd\endcsname{%
  \catcode64 \the\catcode64\relax}
\catcode64 11
%    \end{macrocode}
%
%    \subsection{Main content}
%
%    If running \LaTeX, load \texttt{etex.sty}. If not, either
%    \texttt{etex.src} was loaded at format generation time, or we cannot do
%    anything.
%
%    \begin{macrocode}
\begingroup\expandafter\expandafter\expandafter\endgroup
\expandafter\ifx\csname RequirePackage\endcsname\relax \else
  \RequirePackage{etex}[1998/03/26]
\fi
%    \end{macrocode}
%
%    To the best of my (mpg) knowledge, all Plain-based formats built with
%    \eTeX-enabled engines in \TeX\,Live load \texttt{etex.src}. However,
%    let's be careful and check that \texttt{etex.sty} or \texttt{etex.src} is
%    loaded.
%
%    \begin{macrocode}
\begingroup\expandafter\expandafter\expandafter\endgroup
\expandafter\ifx\csname et@xins\endcsname\relax
  \begingroup
    \expandafter\ifx\csname PackageWarningNoLine\endcsname\relax
      \def\x#1#2{\begingroup\newlinechar10
        \immediate\write16{Package #1 warning: #2}\endgroup}
    \else
      \let\x\PackageWarningNoLine
    \fi
  \expandafter\endgroup
  \x{luatexbase-regs}{etex macros not loaded!^^J
    Registers allocation scheme will not be extended.}
\else
%    \end{macrocode}
%
%    First, increase the upper bound for all kinds of registers. Copy code to
%    avoid defining a macro.
%
%    \begin{macrocode}
  \ifnum\count270=32768 \count270=65536 \fi
  \ifnum\count271=32768 \count271=65536 \fi
  \ifnum\count272=32768 \count272=65536 \fi
  \ifnum\count273=32768 \count272=65536 \fi
  \ifnum\count273=32768 \count273=65536 \fi
  \ifnum\count274=32768 \count274=65536 \fi
  \ifnum\count275=32768 \count275=65536 \fi
  \ifnum\count276=32768 \count276=65536 \fi
%    \end{macrocode}
%
%    \verb|\box| registers and \verb|mark|s were previously defined
%    using \verb|\mathchardef| since it had the biggest range under \eTeX
%    (15-bit number).  However, this is not enough for \LuaTeX's extended
%    registers. Fortunately, \verb|chardef|'s range is extended, and now large
%    enough, so use it.
%
%    Take care that the macros remain \verb|\outer| if they previously were
%    (actually, iff \verb+\globbox+ is). The somewhat tricky lines below,
%    besides that, are just equivalent to:
%    \begin{quote}
%      \verb+\def \globbox    {\et@xglob 4\box    \mathchardef}+\\
%      \verb+\def \globmarks  {\et@xglob 6\marks  \mathchardef}+
%    \end{quote}
%
%    \begin{macrocode}
  \begingroup
    \def\z#1{\endgroup
      #1\expandafter\def\csname globbox\endcsname
        {\et@xglob 4\box    \chardef}%
      #1\expandafter\def\csname globmarks\endcsname
        {\et@xglob 6\marks  \chardef}}%
    \edef\x{\meaning\globbox   \string\outer   \relax}%
    \edef\y{\def\noexpand\y ##1\string\outer##2\relax}%
    \y{\ifx\relax#1\relax
      \expandafter\z\expandafter\outer
    \else
      \expandafter\z\expandafter\relax
    \fi}%
  \expandafter\y\x
%    \end{macrocode}
%
%    \verb+\loc...+ versions are never \verb+\outer+.
%
%    \begin{macrocode}
  \def \locbox     {\et@xloc  4\box    \chardef}
  \def \locmarks   {\et@xloc  6\marks  \chardef}
%    \end{macrocode}
%
%    Finally, make allocation of \verb|\count|, \verb|\dimen|, \verb|skip| and
%    \verb|\box| start with numbers $>255$, in order to free the lower numbers
%    for insertions. Be careful with \verb|\new...| macros which are
%    \verb|\outer| in Plain, since we're in the middle of an \verb|\if| test.
%
%    \begin{macrocode}
  \expandafter\let\csname newcount\endcsname\globcount
  \expandafter\let\csname newdimen\endcsname\globdimen
  \expandafter\let\csname newskip\endcsname\globskip
  \expandafter\let\csname newbox\endcsname\globbox
\fi
%    \end{macrocode}
%
%    That's all folks!
%
%    \begin{macrocode}
\lltxb@AtEnd
%</texpackage>
%    \end{macrocode}
%
%    \section{Test files}
%
%    Here we test only the two main formats: Plain~\TeX\ (with
%    \texttt{etex.src}
%    loaded) and \LaTeX, both with the \LuaTeX\ engine. Those correspond to
%    the \texttt{luatex} and \texttt{lualatex} commands in \TeX\,Live.
%
%    We want to make sure we can globally and locally allocate $30000$
%    registers of each kind, and still globally allocate $100$
%    \verb+\insert+s. (Those numbers are not optimal, but they should be
%    enough for testing purposes.)
%
%    \begin{macrocode}
%<testplain>\input luatexbase-regs.sty
%<testlatex>\RequirePackage{luatexbase-regs}
%<*testplain,testlatex>
\def\checkregister#1{%
  \edef\newregister{\expandafter\noexpand\csname new#1\endcsname}%
  \edef\locregister{\expandafter\noexpand\csname loc#1\endcsname}%
  \count0 1
  \loop
    \newregister\dummy
    \locregister\dummy
  \ifnum\count0<30000
    \advance\count0 1
  \repeat}
\checkregister{count}
\checkregister{dimen}
\checkregister{skip}
\checkregister{muskip}
\checkregister{box}
\checkregister{toks}
\checkregister{marks}

\count0 1
\loop \ifnum\count0<100
  \csname newinsert\endcsname\dummy
  \advance\count0 1
\repeat
%</testplain,testlatex>
%<testplain>\bye
%<testlatex>\stop
%    \end{macrocode}
%
%
% \Finale
\endinput
