commit 5fb16d71d24fd9b3951b6eaca5b102cca2d1e370 Author: zapashcanon Date: Sat Dec 24 03:39:49 2022 +0100 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e35d885 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +_build diff --git a/dune-project b/dune-project new file mode 100644 index 0000000..09f0e32 --- /dev/null +++ b/dune-project @@ -0,0 +1,3 @@ +(lang dune 3.0) + +(using mdx 0.2) diff --git a/src/CODE.md b/src/CODE.md new file mode 100644 index 0000000..9a6792f --- /dev/null +++ b/src/CODE.md @@ -0,0 +1,45 @@ +# C generic list + + +```c +extern void exit(int); + +// asking for an int version + +#define TYPE int +#define TYPED(X) int_##X + +// generic code + +struct TYPED(list) { + TYPE head; + struct TYPED(list) *tail; +}; + +TYPE hd(struct TYPED(list) *l) { + if (l) { + return l->head; + } + + exit(42); +} +``` + +```sh +$ cpp -P list.c +extern void exit(int); +struct int_list { + int head; + struct int_list *tail; +}; +int hd(struct int_list *l) { + if (l) { + return l->head; + } + exit(42); +} +``` + +```sh +$ gcc -c list.c +``` diff --git a/src/article.tex b/src/article.tex new file mode 100644 index 0000000..28ed729 --- /dev/null +++ b/src/article.tex @@ -0,0 +1,45 @@ +\section{Introduction} + +\paragraph{} A function is said to be \emph{polymorphic} when a single implementation of this function can be used with several different types. A polymorphic function may accept types that need to be treated differently at runtime. It could be because they have different memory representation, use different calling conventions or need a special treatment by the garbage collector. It is thus necessary to keep track of these informations at runtime in order to interpret or to compile a polymorphic function. +% TODO: exemples +\paragraph{} Many polymorphism implementation techniques exist but only some of them have been described in research papers. We describe these techniques extensively, list their advantages and limitations, and compare all of them. + +\section{Monomorphization} + +\subsection{General description} + +Given a polymorphic function, it is (sometimes) possible to \emph{statically} collect all the types combinations it is going to be used with. The \emph{monomorphization} technique consists in producing a different \emph{specialised} function for each combination. This results in having only \emph{monomorphic} functions, hence the name monomorphization. + +To build the set of types combinations for a given function, we iterate on the program's \emph{call graph}. At each \emph{call site}, the combination from this call is added to the set. + +Once the set is computed, the original polymorphic function is removed. All the monomorphic functions are generated and added to the program. Finally, each call site is updated to use the right function. + +\subsection{In the wild} + +Monomorphization can be performed at different stages. + +\paragraph{Source code} It is possible to generate source code using a preprocessor, this is described in~\cite{SY74} under the name \emph{syntax-macro extension}. Its is used~\cite{EBN02} in the C programming language through it's preprocessor~\cite{SW87}. + +\inputminted[bgcolor=darkBackground]{c}{list.c} + +%\begin{minted}[bgcolor=lightBackground]{md} +%The file can be downloaded at url `https://archive.softwareheritage.org/api/1/content/sha1_git:80131a360f0ae3d4d643f9e222591db8d4aa744c/raw/`. +%\end{minted} + +Monomorphization is used by Rust's generics~\cite{Con18}, C++'s templates~\cite{Str88}, Ada's generic packages and subprogram~\cite{ND79}\cite{Bar80}, Coq's extraction~\cite{TAG18}, Why3~\cite{BP11} + +\section{Boxing} + +\section{Pointer-tagging} + +\section{Tagged union} + +\section{Type-passing} + +\section{Runtime monomorphization} + +Prevent empty bib~\cite{KS01} + +\section{Comparison} + +\section{Conclusion} diff --git a/src/bib.bib b/src/bib.bib new file mode 100644 index 0000000..876fd6a --- /dev/null +++ b/src/bib.bib @@ -0,0 +1,93 @@ +@incollection{SY74, + title={A survey of extensible programming languages}, + author={Solntseff, Nf and Yezerski, Alexander}, + booktitle={International Tracts in Computer Science and Technology and Their Application}, + volume={7}, + pages={267--307}, + year={1974}, + publisher={Elsevier} +} + +@techreport{ND79, + title={RED language reference manual}, + author={Nestor, John and Deusen, Mary Van}, + year={1979}, + institution={INTERMETRICS INC CAMBRIDGE MA} +} + +@article{Bar80, + title={An overview of Ada}, + author={Barnes, John G. P.}, + journal={Software: Practice and Experience}, + volume={10}, + number={11}, + pages={851--887}, + year={1980}, + publisher={Wiley Online Library} +} + +@article{SW87, + title={The C preprocessor}, + author={Stallman, Richard M and Weinberg, Zachary}, + journal={Free Software Foundation}, + pages={8}, + year={1987} +} + +@inproceedings{Str88, + title={Parameterized Types for C++.}, + author={Stroustrup, Bjarne}, + booktitle={C++ Conference}, + pages={1--18}, + year={1988} +} + +@article{KS01, + doi = {10.1145/381694.378797}, + url = {https://doi.org/10.1145/381694.378797}, + year = {2001}, + month = may, + publisher = {Association for Computing Machinery ({ACM})}, + volume = {36}, + number = {5}, + pages = {1--12}, + author = {Andrew Kennedy and Don Syme}, + title = {Design and implementation of generics for the .{NET} Common language runtime}, + journal = {{ACM} {SIGPLAN} Notices} +} + +@article{EBN02, + title={An empirical analysis of C preprocessor use}, + author={Ernst, Michael D. and Badros, Greg J. and Notkin, David}, + journal={IEEE Transactions on Software Engineering}, + volume={28}, + number={12}, + pages={1146--1170}, + year={2002}, + publisher={IEEE} +} + +@techreport{BP11, + title = {Expressing Polymorphic Types in a Many-Sorted Language}, + author = {Bobot, François and Paskevich, Andrei}, + url = {https://hal.inria.fr/inria-00591414}, + year = {2011} +} + +@misc{Con18, + title = "Guide to Rustc Development - Monomorphization", + author = {The Guide To Rustc Development Contributors}, + howpublished = "\url{https://rustc-dev-guide.rust-lang.org/backend/monomorph.html}", + year = 2018, + note = "Accessed: 2022-12-23" +} + +@article{TAG18, + title={Safe low-level code generation in Coq using monomorphization and monadification}, + author={Tanaka, Akira and Affeldt, Reynald and Garrigue, Jacques}, + journal={Journal of Information Processing}, + volume={26}, + pages={54--72}, + year={2018}, + publisher={Information Processing Society of Japan} +} diff --git a/src/dune b/src/dune new file mode 100644 index 0000000..7c0b0d2 --- /dev/null +++ b/src/dune @@ -0,0 +1,46 @@ +(rule + (targets main.pdf) + (deps main.bbl article.tex main.tex packages.tex) + (action + (pipe-outputs + (run texfot xelatex -halt-on-error -shell-escape main.tex) + (run sed "/\\/usr\\/bin\\/texfot:/d") + (run sed "/This is XeTeX/d") + (run sed "/LaTeX Warning: There were undefined references/d") + (run sed "/Package biblatex Warning/d") + (run sed "/Output written/d")))) + +(rule + (targets main.bbl) + (deps bib.bib main.first.bcf) + (action + (run biber -quiet main.first.bcf --output-file main.bbl))) + +(rule + (targets main.first.bcf) + (deps main.bcf) + (action + (run mv main.bcf main.first.bcf))) + +(rule + (targets main.bcf) + (deps article.tex main.tex packages.tex) + (action + (pipe-outputs + (run texfot xelatex -halt-on-error -shell-escape main.tex) + (run sed "/\\/usr\\/bin\\/texfot:/d") + (run sed "/This is XeTeX/d") + (run sed "/Package hyperref/d") + (run sed "/LaTeX Font Warning:/d") + (run sed "/(Font)/d") + (run sed "/LaTeX Warning: Empty bibliography/d") + (run sed "/LaTeX Warning: There were undefined references/d") + (run sed "/LaTeX Warning: Citation/d") + (run sed "/Package rerunfilecheck/d") + (run sed "/Package biblatex Warning:/d") + (run sed "/Output written/d")))) + +(mdx + (deps + (file list.c)) + (files CODE.md)) diff --git a/src/list.c b/src/list.c new file mode 100644 index 0000000..95f54f1 --- /dev/null +++ b/src/list.c @@ -0,0 +1,21 @@ +extern void exit(int); + +// asking for an int version + +#define TYPE int +#define TYPED(X) int_##X + +// generic code + +struct TYPED(list) { + TYPE head; + struct TYPED(list) *tail; +}; + +TYPE hd(struct TYPED(list) *l) { + if (l) { + return l->head; + } + + exit(42); +} diff --git a/src/main.tex b/src/main.tex new file mode 100644 index 0000000..5374d39 --- /dev/null +++ b/src/main.tex @@ -0,0 +1,32 @@ +\documentclass{article} + +\input{packages.tex} + +\title{Compiling polymorphism:\\a survey and critical review} + +\author[1,2]{Léo Andrès} + +\affil[1]{OCamlPro SAS, 21 rue de Châtillon, 75014 Paris, France} +\affil[2]{Université Paris-Saclay, CNRS, ENS Paris-Saclay, Inria, Laboratoire Méthodes Formelles, 91190 Gif-sur-Yvette, France} + +\bibliography{bib} + +\begin{document} + + \maketitle + + \begin{abstract} + + Lorem ipsum et dolor sit amet. + + \end{abstract} + + \tableofcontents + + \maketitle + + \input{article.tex} + + \printbibliography{} + +\end{document} diff --git a/src/packages.tex b/src/packages.tex new file mode 100644 index 0000000..964bdc2 --- /dev/null +++ b/src/packages.tex @@ -0,0 +1,38 @@ +\usepackage[english]{babel} + +\usepackage{fontspec} % encoding + +\usepackage{graphicx} % to include img +\usepackage[notransparent]{svg} +\usepackage{color} % color power +\usepackage{hyperref} % internal and external links (e.g. mail, www) +\usepackage{authblk} % author affiliation handling +\renewcommand\Affilfont{\itshape\small} + +\usepackage[ + cache=false, + draft=false +]{minted} % source code +\setminted{autogobble=true,breakanywhere=true,breakautoindent=true,breaklines=true,frame=none,fontseries=m,linenos,stepnumber=1} +\usemintedstyle{onedark} +\usemintedstyle{paraiso-dark} + +\usepackage[ + autolang=other, + backend=biber, % choix de l'outil de traitement + backref=true, % liens dans la bibliographie pour remonter dans le texte + backrefstyle=none, % afficher toutes les utilisations de la référence + bibstyle=alphabetic, % style pour les clés des références dans la bibliographie : [initialesAnnée] + citestyle=alphabetic, % style pour les clés des références dans le texte : [initialesAnnée] + sorting=none, % bibliographie triée par ordre d'utilisation des références +]{biblatex} % support des bibliographies + +\setmainfont{Linux Libertine O} +\setsansfont{Linux Biolinum O} +%\setsansfont{TeX Gyre Heros} +\setmonofont[Scale=MatchLowercase]{RobotoMono Nerd Font} + +\definecolor{darkBackground}{HTML}{282c34} % source code background +\definecolor{lightBackground}{HTML}{D7D3CB} % source code background + +\hypersetup{colorlinks=true, pdfstartview=FitH}