How to use your fridge as a whiteboard
by Leandro Lucarella on 2011- 05- 05 03:09 (updated on 2011- 05- 05 03:09)- with 0 comment(s)
Trying CDGC HOWTO
by Leandro Lucarella on 2010- 10- 10 19:28 (updated on 2010- 10- 10 19:28)- with 0 comment(s)
Here are some details on how to try CDGC, as it needs a very particular setup, specially due to DMD not having precise heap scanning integrated yet.
Here are the steps (in some kind of literate scripting, you can copy&paste to a console ;)
# You probably want to do all this mess in some subdirectory :) mkdir cdgc-test cd cdgc-test # First, checkout the repositories. git clone git://git.llucax.com/software/dgc/cdgc.git # If you have problems with git:// URLs, try HTTP: # git clone https://git.llucax.com/r/software/dgc/cdgc.git svn co http://svn.dsource.org/projects/tango/tags/releases/0.99.9 tango # DMD doesn't care much (as usual) about tags, so you have to use -r to # checkout the 1.063 revision (you might be good with the latest revision # too). svn co -r613 http://svn.dsource.org/projects/dmd/branches/dmd-1.x dmd # Now we have to do some patching, let's start with Tango (only patch 3 is # *really* necessary, but the others won't hurt). cd tango for p in 0001-Fixes-to-be-able-to-parse-the-code-with-Dil.patch \ 0002-Use-the-mutexattr-when-initializing-the-mutex.patch \ 0003-Add-precise-heap-scanning-support.patch \ 0004-Use-the-right-attributes-when-appending-to-an-empty-.patch do wget -O- "https://llucax.com/blog/posts/2010/10/10-trying-cdgc-howto/$p" | patch -p1 done cd .. # Now let's go to DMD cd dmd p=0001-Create-pointer-map-bitmask-to-allow-precise-heap-sca.patch wget -O- "https://llucax.com/blog/posts/2010/10/10-trying-cdgc-howto/$p" | patch -p1 # Since we are in the DMD repo, let's compile it (you may want to add -jN if # you have N CPUs to speed up things a little). make -C src -f linux.mak cd .. # Good, now we have to wire Tango and CDGC together, just create a symbolic # link: cd tango ln -s ../../../../../cdgc/rt/gc/cdgc tango/core/rt/gc/ # Since I don't know very well the Tango build system, I did a Makefile of my # own to compile it, so just grab it and compile Tango with it. It will use # the DMD you just compiled and will compile CDGC by default (you can change # it via the GC Make variable, for example: make GC=basic to compile Tango # with the basic GC). The library will be written to obj/libtango-$GC.a, so # you can have both CDGB and the basic collector easily at hand): wget https://llucax.com/blog/posts/2010/10/10-trying-cdgc-howto/Makefile make # Again add -jN if you have N CPUs to make a little faster # Now all you need now is a decent dmd.conf to put it all together: cd .. echo "[Environment]" > dmd/src/dmd.conf echo -n "DFLAGS=-I$PWD/tango -L-L$PWD/tango/obj " >> dmd/src/dmd.conf echo -n "-defaultlib=tango-cdgc " >> dmd/src/dmd.conf echo "-debuglib=tango-cdgc -version=Tango" >> dmd/src/dmd.conf # Finally, try a Hello World: cat <<EOT > hello.d import tango.io.Console; void main() { Cout("Hello, World").newline; } EOT dmd/src/dmd -run hello.d # If you don't trust me and you want to be completely sure you have CDGC # running, try the collect_stats_file option to generate a log of the # collections: D_GC_OPTS=collect_stats_file=log dmd/src/dmd -run hello.d cat log
Done!
If you want to make this DMD the default, just add dmd/src to the PATH environment variable or do a proper installation ;)
Let me know if you hit any problem...
Naive Garbage Collector
by Leandro Lucarella on 2009- 04- 27 01:49 (updated on 2009- 04- 27 01:49)- with 0 comment(s)
I was working in a naive garbage collector implementation for D, as a way to document the process of writing a GC for D.
From the Naive Garbage Collector documentation:
The idea behind this implementation is to document all the bookkeeping and considerations that has to be taken in order to implement a garbage collector for D.
The garbage collector algorithm itself is extremely simple so focus can be held in the specifics of D, and not the algorithm. A completely naive mark and sweep algorithm is used, with a recursive mark phase. The code is extremely inefficient in order to keep the code clean and easy to read and understand.
Performance is, as expected, horrible, horrible, horrible (2 orders of magnitude slower than the basic GC for the simple Tango GC Benchmark) but I think it's pretty good as documentation =)
I have submitted the implementation to Tango in the hope that it gets accepted. A git repository is up too.
If you want to try it out with LDC, you have to put the files into the naive directory in tango/lib/gc and edit the file runtime/CMakeLists.txt and search/replace "basic" for "naive". Then you have to search for the line:
file(GLOB GC_D ${RUNTIME_GC_DIR}/*.d)
And replace it with:
file(GLOB GC_D ${RUNTIME_GC_DIR}/gc/*.d)
Comments and reviews are welcome, and please let me know if you try it =)
Mercurial is not good enough
by Leandro Lucarella on 2009- 04- 01 02:55 (updated on 2009- 04- 01 02:55)- with 0 comment(s)
I started learning some Mercurial for interacting with the LDC repository, but I disliked it instantly. Sure, it's great when you come from SVN, but it's just too limited if you come from GIT (I can't live anymore without git rebase -i).
Fortunately there is fast-export. With it I can incrementally import the Mercurial repository in a GIT repository as easy as:
hg clone http://hg.dsource.org/projects/ldc ldc-hg mkdir ldc cd ldc git init hg-fast-export.sh -r my_local_hg_repo_clone
I'm very happy to be at home again =)
LDC
by Leandro Lucarella on 2009- 03- 29 18:56 (updated on 2009- 03- 29 18:56)- with 0 comment(s)
My original plan was to use GDC as my compiler of choice. This was mainly because DMD is not free and there is a chance that I need to put my hands in the compiler guts.
This was one or two years ago, now the situation has changed a lot. GDC is dead (there was no activity for a long time, and this added to the fact that GCC hacking is hard, it pretty much removes GDC from the scene for me).
OTOH, DMD now provides full source code of the back-end (the front-end was released under the GPL/Artistic licence long ago), but the license is really unclear about what can you do with it. Most of the license mostly tell you how you can never, never, never sue Digital Mars, but about what you can actually do, it's says almost nothing:
The Software is copyrighted and comes with a single user license, and may not be redistributed. If you wish to obtain a redistribution license, please contact Digital Mars.
You can't redistribute it, that's for sure. It says nothing about modifications. Anyways, I don't think Walter Bright mind to give me permission to modify it and use it for my personal project, but I prefer to have a software with a better license to work with (and I never was a big fan of Walter's coding either, so =P).
Fortunately there is a new alternative now: LDC. You should know by now that LDC is the DMD front-end code glued to the LLVM back-end, that there is an alpha release (with much of the main functionality finished), that it's completely FLOSS and that it's moving fast and getting better every day (a new release is coming soon too).
I didn't play with LLVM so far, but all I hear about it is that's a nice, easy to learn and work, compiler framework that is widely used, and getting better and better very fast too.
To build LDC just follow the nice instructions (I'm using Debian so I just had to aptitude install cmake cmake-curses-gui llvm-dev libconfig++6-dev mercurial and go directly to the LDC specific part). Now I just have to learn a little about Mercurial (coming from GIT it shouldn't be too hard), and maybe a little about LLVM and I'm good to go.
So LDC is my compiler of choice now. And it should be yours too =)
Improved druntime getting started documentation
by Leandro Lucarella on 2008- 12- 02 23:07 (updated on 2008- 12- 02 23:07)- with 0 comment(s)
I've expanded the druntime Getting Started documentation. I basically added all the information I've posted in this blog so far: how to change the GC implementation and rebuild phobos.
Testing druntime modifications
by Leandro Lucarella on 2008- 11- 30 03:51 (updated on 2008- 11- 30 03:51)- with 0 comment(s)
Now that we can compile druntime, we should be able to compile some programs that use our fresh, modified, druntime library.
Since DMD 2.021, druntime is built into phobos, so if we want to test some code we need to rebuild phobos too, to include our new druntime.
Since I'm particularly interested in the GC, let's say we want to use the GC stub implementation (instead of the basic default).
We can add a simple "init" message to see that something is actually happening. For example, open src/gc/stub/gc.d and add this import:
private import core.sys.posix.unistd: write;
Then, in the gc_init() function add this line:
write(1, "init\n".ptr, 5);
Now, we must tell druntime we want to use the stub GC implementation. Edit dmd-posix.mak, search for DIR_GC variable and change it from basic to stub:
DIR_GC=gc/stub
Great, now recompile druntime.
Finally, go to your DMD installation, and edit src/phobos/linux.mak. Search for the DRUNTIME variable and set it to the path to your newly generated libdruntime.a (look in the druntime lib directory). For me it's something like:
DRUNTIME=/home/luca/tesis/druntime/lib/libdruntime.a
Now recompile phobos. I have to do this, because my DMD compiler is named dmd2:
make -f linux.mak DMD=dmd2
Now you can compile some trivial D program (compile it in the src druntime directory so its dmd.conf is used to search for the libraries and imports) and see how "init" get printed when the program starts. For example:
druntime/src$ cat hello.d import core.sys.posix.unistd: write; void main() { write(1, "hello!\n".ptr, 7); } druntime/src$ dmd2 -L-L/home/luca/tesis/dmd2/lib hello.d druntime/src$ ./hello init hello!
Note that I passed my DMD compiler's lib path so it can properly find the newly created libphobos2.a.
Getting started with druntime
by Leandro Lucarella on 2008- 11- 25 02:27 (updated on 2008- 11- 25 02:27)- with 0 comment(s)
I've added a brief draft about how to get started in the druntime wiki, which I plan to expand a little in the future.
I hope somebody find it useful.
BTW, the -version=Posix fix is now included in the main repo.
Hacking druntime
by Leandro Lucarella on 2008- 11- 22 16:38 (updated on 2009- 03- 28 20:17)- with 0 comment(s)
I've been reading the source code of the druntime, and it's time to get my hands dirty and do some real work.
First I have to do to start hacking it is build it and start trying things out. There is no documentation at all yet, so I finally bothered Sean Kelly and asked him how to get started.
Here is what I had to do to get druntime compiled:
First of all, I'll introduce my environment and tools. I'll use DMD because there's no other option for now (druntime doesn't have support for GDC, but Sean says it's coming soon, and LDC will not be included until the support it's added to Tango runtime).
The trunk in the druntime repository is for D2, but there is a branch for D1 too.
I use Debian (so you'll see some apt stuff here) and I love git, and there's is no way I will go back to subversion. Fortunately there is git-svn, so that's what I'm gonna use =)
Now, what I did step by step.
Get the git goodies:
sudo aptitude install git-core git-svn
Make a directory where to put all the D-related stuff:
mkdir ~/d cd ~/d
Get D2 (bleeding edge version) and unpack it:
wget http://ftp.digitalmars.com/dmd.2.026.zip unzip dmd.2.020.zip # "install" the D2 compiler rm -fr dm dmd/linux/bin/{sc.ini,readme.txt,*.{exe,dll,hlp}} # cut the fat chmod a+x dmd/linux/bin/{dmd,rdmd,dumpobj,obj2asm} # make binaries executable mv dmd dmd2 # reserve the dmd directory for D1 compiler
Make it accessible, for example:
echo '#!/bin/sh' > ~/bin/dmd2 # reserve dmd name for the D1 compiler echo 'exec ~/d/dmd2/linux/bin/dmd "$@"' >> ~/bin/dmd2 chmod a+x ~/bin/dmd2
Get D1 and install it too:
wget http://ftp.digitalmars.com/dmd.1.041.zip unzip dmd.1.036.zip rm -fr dm dmd/linux/bin/{sc.ini,readme.txt,*.{exe,dll,hlp}} chmod a+x dmd/linux/bin/{dmd,rdmd,dumpobj,obj2asm} echo '#!/bin/sh' > ~/bin/dmd echo 'exec ~/d/dmd/linux/bin/dmd "$@"' >> ~/bin/dmd chmod a+x ~/bin/dmd
Get druntime for D1 and D2 as separated repositories (you can get all in one git repository using git branches but since I'll work on both at the same time I prefer to use two separated repositories):
git svn clone http://svn.dsource.org/projects/druntime/branches/D1.0 \ druntime git svn clone http://svn.dsource.org/projects/druntime/trunk druntime2
Build druntime for D1:
cd druntime bash build-dmd.sh cd -
Build druntime for D2.
This one is a little trickier. The trunk version have some changes for a feature that is not yet released (this being changed from a pointer to a reference for structs). Fortunately this is well isolated in a single commit, so reverting this change is really easy, first, get the abbreviated hash for the commit 44:
cd druntime2 git log --grep='trunk@44' --pretty=format:%h
This should give you a small string (mine is cae2326). Now, revert that change:
git revert cae2326
Done! You now have that change reverted, we can remove this new commit later when the new version of DMD that implements the this change appear.
But this is not all. Then I find a problem about redefining the Posix version:
Error: version identifier 'Posix' is reserved and cannot be set
To fix this you just have to remove the -version=Posix from build-dmd.sh.
But there is still one more problem, but this is because I have renamed the bianries to have both dmd and dmd2. The compiler we have to use to build things is called dmd2 for me, but build-dmd.sh don't override properly the DC environment variable when calling make, so dmd is used instead.
This is a simple and quick fix:
diff --git a/src/build-dmd.sh b/src/build-dmd.sh old mode 100644 new mode 100755 index d6be599..8f3b163 --- a/src/build-dmd.sh +++ b/src/build-dmd.sh @@ -11,9 +11,10 @@ goerror(){ exit 1 } -make clean -fdmd-posix.mak || goerror -make lib doc install -fdmd-posix.mak || goerror -make clean -fdmd-posix.mak || goerror +test -z "$DC" && DC=dmd +make DC=$DC clean -fdmd-posix.mak || goerror +make DC=$DC lib doc install -fdmd-posix.mak || goerror +make DC=$DC clean -fdmd-posix.mak || goerror chmod 644 ../import/*.di || goerror export HOME=$OLDHOME
(to apply the patch just copy&paste it to fix.patch and then do git apply fix.patch; that should do the trick)
Now you can do something like this to build druntime for D2:
export DC=dmd2 bash build-dmd.sh
That's it for now. I'll be publishing my druntime (git) repository soon with this changes (and probably submitting some patches to upstream) so stay tuned ;)