Browse Source

first commit

main
zapashcanon 1 year ago
commit
5fb16d71d2
Signed by: zapashcanon GPG Key ID: 8981C3C62D1D28F1
  1. 1
      .gitignore
  2. 3
      dune-project
  3. 45
      src/CODE.md
  4. 45
      src/article.tex
  5. 93
      src/bib.bib
  6. 46
      src/dune
  7. 21
      src/list.c
  8. 32
      src/main.tex
  9. 38
      src/packages.tex

1
.gitignore

@ -0,0 +1 @@
_build

3
dune-project

@ -0,0 +1,3 @@
(lang dune 3.0)
(using mdx 0.2)

45
src/CODE.md

@ -0,0 +1,45 @@
# C generic list
<!-- $MDX file=list.c -->
```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
```

45
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}

93
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}
}

46
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))

21
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);
}

32
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}

38
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}
Loading…
Cancel
Save