X-Git-Url: https://git.distorted.org.uk/~mdw/sod/blobdiff_plain/a588c77a273681e3cdc85d15fc44f3ddb7da9224..b492babc8de898bd22d638d7c25f24356896a3a9:/doc/clang.tex diff --git a/doc/clang.tex b/doc/clang.tex index c26ece5..58b8cd2 100644 --- a/doc/clang.tex +++ b/doc/clang.tex @@ -7,7 +7,7 @@ %%%----- Licensing notice --------------------------------------------------- %%% -%%% This file is part of the Sensble Object Design, an object system for C. +%%% This file is part of the Sensible Object Design, an object system for C. %%% %%% SOD is free software; you can redistribute it and/or modify %%% it under the terms of the GNU General Public License as published by @@ -34,7 +34,7 @@ The Sod translator represents C types in a fairly simple and direct way. However, because it spends a fair amount of its time dealing with C types, it provides a number of useful operations and macros. -The class hierarchy is shown in~\xref{fig:proto.c-types}. +The class hierarchy is shown in~\xref{fig:codegen.c-types.classes}. \begin{figure} \centering \parbox{10pt}{\begin{tabbing} @@ -51,7 +51,7 @@ The class hierarchy is shown in~\xref{fig:proto.c-types}. @|c-function-type| \end{tabbing}} \caption{Classes representing C types} -\label{fig:proto.c-types} +\label{fig:codegen.c-types.classes} \end{figure} C type objects are immutable unless otherwise specified. @@ -120,8 +120,9 @@ type specifier. Type specifiers fit into two syntactic categories. \end{describe} \begin{describe}{mac} - {defctype @{ @ @! (@ @^*) @} @ - @> @} + {defctype \=@{ @ @! (@^+) @} @ \+ \\ + @[[ @|:export| @ @]]^* \- + \nlret @} Defines a new symbolic type specifier @; if a list of @s is given, then all are defined in the same way. The type constructed by using any of the @s is as described by the type specifier @. @@ -129,6 +130,13 @@ type specifier. Type specifiers fit into two syntactic categories. The resulting type object is constructed once, at the time that the macro expansion is evaluated; the same (@|eq|) value is used each time any @ is used in a type specifier. + + A variable named @|c-type-@|, for the first @ only, is defined + and initialized to contain the C type object so constructed. Altering or + binding this name is discouraged. + + If @ is true, then the variable name, and all of the @s, + are exported from the current package. \end{describe} \begin{describe}{mac}{c-type-alias @ @^* @> @} @@ -138,7 +146,10 @@ type specifier. Type specifiers fit into two syntactic categories. \end{describe} \begin{describe}{mac} - {define-c-type-syntax @ @ @
^* @> @} + {define-c-type-syntax @ @ \\ \ind + @[[ @^* @! @ @]] \\ + @^* \- + \nlret @} Defines the symbol @ as a new type operator. When a list of the form @|(@ @^*)| is used as a type specifier, the @s are bound to fresh variables according to @ (a destructuring @@ -265,7 +276,9 @@ argument lists for methods. This is done by @|c-type-equal-p|. \end{describe} \begin{describe}{mac} - {maybe-in-parens (@ @) @^*} + {maybe-in-parens (@ @) + @^* + @^*} The @ is evaluated, and then the @s are evaluated in sequence within a pretty-printer logical block writing to the stream named by the symbol @. If the @ evaluates to nil, then @@ -342,43 +355,54 @@ In Sod, the leaf types are and matching qualifiers. A number of symbolic type specifiers for builtin types are predefined as - shown in \xref{tab:proto.c-types.simple}. These are all defined as if by + shown in \xref{tab:codegen.c-types.simple}. These are all defined as if by @|define-simple-c-type|, so can be used to construct qualified types. \end{describe} \begin{table} - \begin{tabular}[C]{|l|l|} \hlx{hv} - \textbf{C type} & \textbf{Specifiers} \\ \hlx{vhv} - @|void| & @|void| \\ \hlx{vhv} - @|char| & @|char| \\ \hlx{v} + \begin{tabular}[C]{ll} \hlx*{hv} + \thd{C type} & \thd{Specifiers} \\ \hlx{vhv} + @|void| & @|void| \\ \hlx{v} + @|_Bool| & @|bool| \\ \hlx{v} + @|char| & @|char| \\ \hlx{} + @|wchar_t| & @|wchar-t| \\ \hlx{v} + @|signed char| & @|signed-char|, @|schar| \\ \hlx{} @|unsigned char| & @|unsigned-char|, @|uchar| \\ \hlx{v} - @|signed char| & @|signed-char|, @|schar| \\ \hlx{vhv} @|short| & @|short|, @|signed-short|, @|short-int|, - @|signed-short-int| @|sshort| \\ \hlx{v} + @|signed-short-int| @|sshort| \\ \hlx{} @|unsigned short| & @|unsigned-short|, @|unsigned-short-int|, - @|ushort| \\ \hlx{vhv} + @|ushort| \\ \hlx{v} @|int| & @|int|, @|signed|, @|signed-int|, - @|sint| \\ \hlx{v} - @|unsigned int| & @|unsigned|, @|unsigned-int|, @|uint| \\ \hlx{vhv} + @|sint| \\ \hlx{} + @|unsigned int| & @|unsigned|, @|unsigned-int|, @|uint| \\ \hlx{v} @|long| & @|long|, @|signed-long|, @|long-int|, - @|signed-long-int|, @|slong| \\ \hlx{v} + @|signed-long-int|, @|slong| \\ \hlx{} @|unsigned long| & @|unsigned-long|, @|unsigned-long-int|, - @|ulong| \\ \hlx{vhv} + @|ulong| \\ \hlx{v} @|long long| & @|long-long|, @|signed-long-long|, - @|long-long-int|, \\ + @|long-long-int|, \\ \hlx{} & \qquad @|signed-long-long-int|, @|llong|, @|sllong| \\ \hlx{v} @|unsigned long long| & @|unsigned-long-long|, @|unsigned-long-long-int|, - @|ullong| \\ \hlx{vhv} - @|float| & @|float| \\ \hlx{v} - @|double| & @|double| \\ \hlx{vhv} - @|va_list| & @|va-list| \\ \hlx{v} - @|size_t| & @|size-t| \\ \hlx{v} - @|ptrdiff_t| & @|ptrdiff-t| \\ \hlx{vh} + @|ullong| \\ \hlx{v} + @|size_t| & @|size-t| \\ \hlx{} + @|ptrdiff_t| & @|ptrdiff-t| \\ \hlx{v} + @|float| & @|float| \\ \hlx{} + @|double| & @|double| \\ \hlx{} + @|long double| & @|long-double| \\ \hlx{v} + @|float _Imaginary| & @|float-imaginary| \\ \hlx{} + @|double _Imaginary|& @|double-imaginary| \\ \hlx{} + @|long double _Imaginary| + & @|long-double-imaginary| \\ \hlx{v} + @|float _Complex| & @|float-complex| \\ \hlx{} + @|double _Complex| & @|double-complex| \\ \hlx{} + @|long double _Complex| + & @|long-double-complex| \\ \hlx{v} + @|va_list| & @|va-list| \\ \hlx*{vh} \end{tabular} \caption{Builtin symbolic type specifiers for simple C types} - \label{tab:proto.c-types.simple} + \label{tab:codegen.c-types.simple} \end{table} \begin{describe}{fun} @@ -394,13 +418,21 @@ In Sod, the leaf types are \end{describe} \begin{describe}{mac} - {define-simple-c-type @{ @ @! (@^*) @} @ @> @} + {define-simple-c-type \=@{ @ @! (@^+) @} @ \+ \\ + @[[ @|:export| @ @]] \- + \nlret @} Define type specifiers for a new simple C type. Each symbol @ is defined as a symbolic type specifier for the (unique interned) simple C type whose name is the value of @. Further, each @ is defined to be a type operator: the type specifier @|(@ @^*)| evaluates to the (unique interned) simple C type whose name is @ and which has the @ (which are evaluated). + + Furthermore, a variable @|c-type-@| is defined, for the first @ + only, and initialized with the newly constructed C type object. + + If @ is true, then the @|c-type-@| variable name, and + all of the @s, are exported from the current package. \end{describe} \begin{describe}{cls}{tagged-c-type (qualifiable-c-type) @@ -513,7 +545,7 @@ to. evaluated). The synonyms @|ptr| and @|pointer| may be used in place of the star @`*'. - The symbol @|string| is a type specifier for the type of pointer to + The symbol @|string| is a type specifier for the type pointer to characters; the symbol @|const-string| is a type specifier for the type pointer to constant characters. \end{describe} @@ -521,15 +553,15 @@ to. \begin{describe}{fun} {make-pointer-type @ \&optional @ @> @} - Return an object describing the type of qualified pointers to @. + Return an object describing the type qualified pointer to @. If @ is interned, then the returned pointer type object is interned also. \end{describe} \subsection{Array types} \label{sec:clang.c-types.array} -Arrays implement the compound-type protocol. The subtype of an array is the -array element type. +Arrays implement the compound-type protocol. The subtype of an array type is +the array element type. \begin{describe}{cls}{c-array-type (c-type) \&key :subtype :dimensions} Represents a multidimensional C array type. The @ are a list @@ -569,51 +601,411 @@ array element type. \subsection{Function types} \label{sec:clang.c-types.fun} +Function types implement the compound-type protocol. The subtype of a +function type is the type of the function's return value. + \begin{describe}{cls}{argument} + Represents an ordinary function argument. \end{describe} \begin{describe}{fun}{argumentp @ @> @} + Decide whether @ is an @ object: if so, return non-nil; if + not return nil. \end{describe} \begin{describe}{fun}{make-argument @ @ @> @} -\end{describe} + Construct and a return a new @ object. The argument has type + @, which must be a @|c-type| object, and is named @. -\begin{describe}{fun}{argument-name @ @> @} + The @ may be nil to indicate that the argument has no name: in this + case the argument will be formatted as an abstract declarator, which is not + suitable for function definitions. If @ is not nil, then the + @'s print representation, with @|*print-escape*| nil, is used as the + argument name. \end{describe} -\begin{describe}{fun}{argument-type @ @> @} +\begin{describe*} + {\dhead{fun}{argument-name @ @> @} + \dhead{fun}{argument-type @ @> @}} + Accessor functions for @|argument| objects. They return the name (for + @|argument-name|) or type (for @|argument-type|) from the object, as passed + to @|make-argument|. +\end{describe*} + +\begin{describe}{gf} + {commentify-argument-name @ @> @} + Convert the argument name @ so that it's suitable to declare the + function in a header file. + + Robust header files shouldn't include literal argument names in + declarations of functions or function types, since this restricts the + including file from defining such names as macros. This generic function + is used to convert names into a safe form. + + \begin{describe}{meth}{commentify-argument-name (@ null) @> nil} + Returns nil: if the argument name is already omitted, it's safe for use + in a header file. + \end{describe} + \begin{describe}{meth}{commentify-argument-name (@ t) @> @} + Returns the print form of @ wrapped in a C comment, as + @`/*@*/'. + \end{describe} \end{describe} \begin{describe}{fun} - {commentify-argument-name @ @> @} + {commentify-argument-names @ @> @} + Convert the @ list so that it's suitable for use in a header + file. + + The @ list should be a list whose items are @|argument| objects + or the keyword @|:ellipsis|. The return value is a list constructed as + follows. For each @|argument| object in the input list, there is a + corresponding @|argument| object in the returned list, with the same type, + and whose name is the result of @|commentify-argument-name| applied to the + input argument name; an @|:ellipsis| in the input list is passed through + unchanged. \end{describe} \begin{describe}{cls}{c-function-type (c-type) \&key :subtype :arguments} - Represents C function types. An instance denotes the C type of a C - function which FIXME + Represents C function types. An instance denotes the type of a C + function which accepts the @ and returns @. + + The @ are a possibly empty list. All but the last element of + the list must be @|argument| objects; the final element may instead be the + keyword @|:ellipsis|, which denotes a variable argument list. + + An @ list consisting of a single argument with type @|void| is + converted into an empty list. On output as C code, an empty argument list + is written as @|void|. It is not possible to represent a pre-ANSI C + function without prototypes. + + Two function types are considered to be the same if their return types are + the same, and their argument lists consist of arguments with the same type, + in the same order, and either both or neither argument list ends with + @|:ellipsis|; argument names are not compared. + + The type specifier + \begin{prog} + (fun @ + @{ (@ @) @}^* + @[:ellipsis @! . @@]) + \end{prog} + constructs a function type. The function has the subtype @. + The remaining items in the type-specifier list are used to construct the + argument list. The argument items are a possibly improper list, beginning + with zero or more \emph{explicit arguments}: two-item + @/@ lists. For each such list, an @|argument| object + is constructed with the given name (evaluated) and type. Following the + explicit arguments, there may be + \begin{itemize} + \item nothing, in which case the function's argument list consists only of + the explicit arguments; + \item the keyword @|:ellipsis|, as the final item in the type-specifier + list, indicating a variable argument list may follow the explicit + arguments; or + \item a possibly-improper list tail, beginning with an atom either as a + list item or as the final list cdr, indicating that the entire list tail + is Lisp expression which is to be evaluated to compute the remaining + arguments. + \end{itemize} + A tail expression may return a list of @|argument| objects, optionally + followed by an @|:ellipsis|. + + For example, + \begin{prog} + (c-type (fun \=(lisp (c-type-subtype other-func)) \+ \\ + ("first" int) . (c-function-arguments other-func)) + \end{prog} + evaluates to a function type like @|other-func|, only with an additional + argument of type @|int| added to the front of its argument list. This + could also have been written + \begin{prog} + (let (\=(args (c-function-arguments other-func)) \+ \\ + (ret (c-type-subtype other-func))) \- \\ \ind + (c-type (fun \=(lisp ret) ("first" int) . args) + \end{prog} \end{describe} \begin{describe}{fun} + {make-function-type @ @ @> @} + Construct and return a new function type, returning @ and + accepting the @. +\end{describe} + +\begin{describe}{gf} {c-function-arguments @ @> @} + Return the arguments list of the @. \end{describe} \begin{describe}{fun} - {make-c-type @ @ @> @} + {commentify-function-type @ @> @} + Return a commentified version of the @. + + The returned type has the same subtype as the given type, and the argument + list of the returned type is the result of applying + @|commentify-argument-names| to the argument list of the given type. \end{describe} +\subsection{Parsing C types} \label{sec:clang.c-types.parsing} + \begin{describe}{fun} - {commentify-argument-names @ @> @} + {parse-c-type @ + @> @ @ @} \end{describe} \begin{describe}{fun} - {commentify-function-type @ @> @} + {parse-declarator @ @ \&key :kernel :abstractp + \nlret @ @ @} \end{describe} -\subsection{Parsing C types} \label{sec:clang.c-types.parsing} +\subsection{Class types} \label{sec:clang.c-types.class} + +\begin{describe}{cls} + {c-class-type (simple-c-type) \&key :class :tag :qualifiers :name} +\end{describe} + +\begin{describe*} + {\dhead{gf}{c-type-class @ @> @} + \dhead{gf}{setf (c-type-class @) @}} +\end{describe*} + +\begin{describe}{fun}{find-class-type @ @> @} +\end{describe} + +\begin{describe}{fun} + {make-class-type @ \&optional @ @> @} +\end{describe} + +\begin{describe}{fun} + {make-class-type @ \&optional @ @> @} +\end{describe} + +\begin{describe}{fun}{find-sod-class @ @> @} +\end{describe} + +\begin{describe}{fun}{record-sod-class @} +\end{describe} %%%-------------------------------------------------------------------------- \section{Generating C code} \label{sec:clang.codegen} +This section deals with Sod's facilities for constructing and manipulating C +expressions, declarations, instructions and definitions. + +\subsection{Temporary names} \label{sec:clang.codegen.temporaries} + +Many C-level objects, especially ones with external linkage or inclusion in a +header file, are assigned names which are simple strings, perhaps fixed ones, +perhaps constructed. Other objects don't need meaningful names, and +suitably unique constructed names would be tedious and most likely rather +opaque. Therefore Sod has an ability to construct \emph{temporary names}. + +These aren't temporary in the sense that they name C objects which have +limited lifetimes at runtime. Rather, the idea is that the names be +significant only to small pieces of Lisp code, which will soon forget about +them. + +\subsubsection{The temporary name protocol} +Temporary names are represented by objects which implement a simple protocol. + +\begin{describe}{gf}{format-temporary-name @ @} +\end{describe} + +\begin{describe*} + {\dhead{gf}{var-in-use-p @ @> @} + \dhead[setf var-in-use-p] + {gf}{setf (var-in-use-p @) @}} +\end{describe*} + +\subsubsection{Temporary name objects} + +\begin{describe}{cls}{temporary-name () \&key :tag} + A temporary name object. This is the root of a small collection of + subclasses, but is also usable on its own. +\end{describe} + +\begin{describe}{meth} + {commentify-argument-name (@ temporary-name) @> nil} +\end{describe} + +\begin{table} + \begin{tabular}[C]{*2{>{\codeface}l}} \hlx*{hv} + \thd{\textbf{Class}} & \thd{\textbf{Name format}} \\ \hlx{vhv} + temporary-name & @ \\ + temporary-argument & sod__a@ \\ + temporary-function & sod__f@ \\ + temporary-variable & sod__v@ \\ \hlx*{vh} + \end{tabular} + \caption{Temporary name formats} + \label{tab:codegen.codegen.temps-format} +\end{table} + +\begin{describe}{cls}{temporary-argument (temporary-name) \&key :tag} +\end{describe} + +\begin{describe}{cls}{temporary-function (temporary-name) \&key :tag} +\end{describe} + +\begin{describe}{fun}{temporary-function @> @} +\end{describe} + +\begin{describe}{cls} + {temporary-variable (temporary-name) \&key :tag :in-use-p} +\end{describe} + +\subsubsection{Well-known `temporary' names} + +\begin{table} + \begin{tabular}[C]{*2{>{\codeface}l}} \hlx*{hv} + \thd{\textbf{Variable}} & \thd{\textbf{Name format}} \\ \hlx{vhv} + {}*sod-ap* & sod__ap \\ + {}*sod-master-ap* & sod__master_ap \\ \hlx*{vh} + \end{tabular} + \caption{Well-known temporary names} + \label{tab:codegen.codegen.well-known-temps} +\end{table} + +\subsection{Instructions} \label{sec:clang.codegen.insts} + +\begin{describe}{cls}{inst () \&key} +\end{describe} + +\begin{describe}{gf}{inst-metric @} +\end{describe} + +\begin{describe}{mac} + {definst @ (@ \&key @) (@^*) \\ \ind + @[[ @^* @! @ @]] \\ + @^* \- + \nlret @} +\end{describe} + +\begin{describe}{mac} + {format-compound-statement + (@ @ \&optional @) \\ \ind + @^* \\ + @^*} +\end{describe} + +\begin{table} + \begin{tabular}[C]{ll>{\codeface}l} \hlx*{hv} + \thd{Class name} & + \thd{Arguments} & + \thd{Output format} \\ \hlx{vhv} + @|var| & @ @ @ & @ @ @[= @@]; + \\ \hlx{v} + @|set| & @ @ & @ = @; \\ \hlx{v} + @|update| & @ @ @ & @ @= @; + \\ \hlx{v} + @|return| & @ & return @[@@]; + \\ \hlx{v} + @|break| & --- & break; \\ \hlx{v} + @|continue| & --- & continue; \\ \hlx{v} + @|expr| & @ & @; \\ \hlx{v} + @|call| & @ @ & @(@_1, + $\ldots$, + @_n) \\ \hlx{vhv} + @|block| & @ @ & \{ @[@@] @ \} + \\ \hlx{v} + @|if| & @ @ @ & if (@) @ + @[else @@] \\ \hlx{v} + @|while| & @ @ & while (@) @ + \\ \hlx{v} + @|do-while| & @ @ & do @ while (@); + \\ \hlx{v} + @|function| & @ @ @ & + @_0 @(@_1 @_1, $\ldots$, + @_n @_n @[, \dots@]) + @ \\ \hlx*{vh} + \end{tabular} + \caption{Instruction classes} + \label{tab:codegen.codegen.insts} +\end{table} + +\subsection{Code generation} \label{sec:clang.codegen.codegen} + +\begin{describe}{gf}{codegen-functions @ @> @} +\end{describe} + +\begin{describe}{gf} + {ensure-var @ @ @ \&optional @} +\end{describe} + +\begin{describe}{gf}{emit-inst @ @} +\end{describe} + +\begin{describe}{gf}{emit-insts @ @} +\end{describe} + +\begin{describe}{gf}{emit-decl @ @} +\end{describe} + +\begin{describe}{gf}{emit-decls @ @} +\end{describe} + +\begin{describe}{gf}{codegen-push @} +\end{describe} + +\begin{describe}{gf}{codegen-pop @ @> @ @} +\end{describe} + +\begin{describe}{gf}{codegen-pop-block @ @> @} +\end{describe} + +\begin{describe}{gf} + {codegen-pop-function @ @ @ @> @} +\end{describe} + +\begin{describe}{gf}{codegen-add-function @ @} +\end{describe} + +\begin{describe}{fun} + {codegen-build-function @ @ @ @ @ + @> @} +\end{describe} + +\begin{describe}{gf}{temporary-var @ @ @> @} +\end{describe} + +\begin{describe}{mac} + {with-temporary-var (@ @ @) \\ \ind + @^* \\ + @^* \- + \nlret @^*} +\end{describe} + +\begin{describe}{fun}{deliver-expr @ @ @} +\end{describe} + +\begin{describe}{fun} + {deliver-call @ @ @ \&rest @} +\end{describe} + +\begin{describe}{fun}{convert-stmts @ @ @ @} +\end{describe} + +\begin{describe}{cls}{codegen () \&key :vars :insts (:temp-index 0)} +\end{describe} + +%%%-------------------------------------------------------------------------- +\section{Literal C code fragments} \label{sec:clang.fragment} + +\begin{describe}{cls}{c-fragment () \&key :location :text} +\end{describe} + +\begin{describe}{gf}{c-fragment-text @ @> @} +\end{describe} + +\begin{describe}{fun} + {scan-c-fragment @ @ + @> @ @ @} +\end{describe} + +\begin{describe}{fun} + {parse-delimited-fragment @ @ @ \&key :keep-end + \nlret @ @ @} +\end{describe} + %%%----- That's all, folks -------------------------------------------------- %%% Local variables: