Hacking a compiler

Lately I’ve had this idea about extending the C language with a few sugar features to facilitate CRUD

Well extending GCC is now possible via plugins but it’s pretty complicated, so much in fact there is a domain language based in-part on scheme called melt who’s sole purpose is for creating plugins for GCC.

llvm is another route specifically hacking on clang but well it’s pretty big and I avoid C++ when possible, especially given this is an amusement hack.

I searched around and finally found by one Fabrice Bellard. It started out as an obfuscated C competition submission for smallest working C compiler, not a good sign I suppose for a maintainable chunk of code but anyway while he no longer maintains it personally a number of individuals do, and they have cleaned up the code considerably and it seems to keep improving.

What’s neat about this compiler is it’s fast, 5x the speed of GCC. I’ll get back to the speed issue in a moment, the second neat feature is the compiler is also shipped as a library, called libtcc which lets you compile and run C code dynamically.

So you can do things like:


#include
#include
#include "libtcc.h"

int add(int a, int b) { return a + b; }

char my_program[] =
"int fib(int n) {\n"
" if (n <= 2) return 1;\n"
" else return fib(n-1) + fib(n-2);\n"
"}\n"
"int foobar(int n) {\n"
" printf(\"fib(%d) = %d\\n\", n, fib(n));\n"
" printf(\"add(%d, %d) = %d\\n\", n, 2 * n, add(n, 2 * n));\n"
" return 1337;\n"
"}\n";

int main(int argc, char **argv)
{
TCCState *s;
int (*foobar_func)(int);
void *mem;

s = tcc_new();
tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
tcc_compile_string(s, my_program);
tcc_add_symbol(s, "add", add);

mem = malloc(tcc_relocate(s, NULL));
tcc_relocate(s, mem);

foobar_func = tcc_get_symbol(s, "foobar");

tcc_delete(s);

printf("foobar returned: %d\n", foobar_func(32));

free(mem);
return 0;
}

The example above compiles the string "my_program" dynamically at runtime and executes it directly from memory. Because tcc is so fast, this sort of functionality is pretty useful and tcc supports shell scripting ala:

#!/usr/bin/tcc -run
#include

int main() {
printf("Word\n");
return 0;
}

You can run the script directly from the command as if it were a shell script.

Another example would be [both taken from the documenation]

echo 'main(){ puts("hello");}' | tcc -run -

Pretty neat.

Posted in Uncategorized | Leave a comment

Developing for Android with NDK on VirtualBox

Quite convoluted I realize but I was able to get this to work successfully.

My system: Windows 7 running latest Virtualbox with Ubuntu 12 [whatever the hell is latest here too] running. My phone: Samsung Galaxy 2S.

The big problem I had was getting adb to talk to my cellphone [Samsung Galazy S2]. Turns out once you know the trick this is pretty easy.

Step 1) Plug your phone into your computer!

Step 2) Open up virtualbox go to your Running VM and select Machine | Settings from the menu. Select USB on the left. After that there is a tiny little “Add icon” on the right hand side. Consult the picture. Clicking on that Icon will show you a list of currently connected devices. Select your phone.

What you are doing is telling Virtualbox to take ownership of the USB device away from windows while Virtualbox is running. If you don’t do this then the OS running in Virtualbox [Ubuntu hopefully cause that's what I'm using] won’t know anything about it and it won’t show up.

You will need to restart your VM. Also Virtualbox will insist on installing a USB driver. This takes a while but let it happen cause it’ needs it for everything to work properly.

Then:


shane@slurp:~/split$ adb devices
List of devices attached
e54910db device

shane@slurp:~/split$

Posted in Uncategorized | Leave a comment

First Cut

Hey – been busy with a new job but I managed to get the Millbox cutting for the first time. My end result deviated from my initial plans but that’s okay. I’m pretty happy with the result. Of course there are improvements I would like to make and I would do things differently now, but that is the point overall I guess.

Here is a video of me cutting a small hole in a piece of scrap aluminum. I just clamped the metal to the base, I don’t have a tooling plate completed. Right before I took the video I drenched the surface with WD40 thats why it looks all gummy. Probably not necessary at the slow speed I was going at.

Posted in Uncategorized | Leave a comment

Acme Rod Assembly

I am using Helical couplers between the acme rod and the stepper motors. These couplers, because they are springy introduce slop into the machine. I addressed the issue in the following manor which is best described in the picture below.

I used a bronze bushing [local hardware store, not big box store] and a shaft collar [again local hardware store].

These bronze bushings are “thrust bushings” and should be available everywhere. You could go higher end and use needle thrust bearings which look like washers except they have roller pins or regular ball bearings in them. These were not available to me on a Sunday morning, so I went for the bronze thrust bushing route.

This setup works really well. I’ll have two shaft collars and bushings on either in, so I’ll loose perhaps an inch of travel but the slop disappears.

Posted in Uncategorized | Leave a comment

z-axis getting there.

I’ve slowly been working on the z-axis. I’ve updated the CAD drawings to include most of the Z-axis assembly. I still have a couple of parts to draft.

Here are a couple of pictures of how it’s looking.

Posted in Uncategorized | Leave a comment

Drawings are now available at github

https://github.com/adamsch1/millbox

I realized these files should be under source control so I chucked them up into github. Enjoy.

Posted in Uncategorized | Leave a comment

Redoing y-axis

I hooked up the y-axis to the Gecko stepper driver and realized I had a few problems that I needed to address before I could continue. The tower was too tall, nearing 11″, and I needed a lower center of gravity. Additionally I needed an additional linear rail. The single rail and acme screw was not sufficient. There was to much flex or bend at the bottom of the cutting tool.

I started over and designed the part in Solid Edge 2D. They have recently upgraded it to ST4. Its still free, which is great.

I’ve attached the part plan in this post. I’m taking my sweet ass time laying out and machining these parts, I don’t want to make any mistakes.

I purchased a granite surface plate from Shars. It’s 12″ x 18″. I got one size larger than I needed. I got it on ebay from discount_machine, which is just Shars I found out.

I also got an angle plate from Enco.

Shipping heavy chunks of steel and granite kinda sucks.

Anyway I’ll post pics soon.

ytower-1

Posted in Uncategorized | Leave a comment

First Movement!

Here I demonstrate the first movement out of the Millbox.

Its coming together and I’m getting really excited to see how everything turns out. I am looking forward to when the Millbox is up and running. I can then rebuild it using itself :)

Posted in Uncategorized | Leave a comment

Z – Axis

I’m building the Z-axis out of the same 2.5″ x .5″ aluminum stock. I’ve decided to use pre-made mechanical fittings in order to get the mill done quickly. I’m quite happy with the progress so far. I will include a line drawing in a follow up post.

Since I still have 16mm linear shaft stock left over, I’m using two pieces of this material sandwiching the 1/2″ acme screw I’ve used throughout the build.

I decided to use these in order to fasten the 16mm shafts:

What I did was cut two pieces of stock 6″ from the 2.5″ x 0.5″ bar. I’m still cutting this material by hand with a hacksaw so I run it through my Taig mill to clean up the ends.

These 16mm shaft supports require two holes. In order to get them to line up perfectly I decided to bolt the two 6″ pieces together with two 10-32 bolts. I put the two pieces of stock in my drill press vise. Aligned them end to end then drilled through the material with my drill press then bolted them together. This way any hole I drilled for my mechanical fasteners, shafts, mounting holes for steppers and so on would be perfectly aligned. I read about this on a website recently and I thought it was a neat trick. Sure you have a couple of spare holes through your stock that might look funky but I’ll take that trade off.

Now I could measure out and drill the holes for each of the linear shaft supports at one time through both pieces of stock. This way the shafts would align perfectly. I measured everything out and used a center drill bit to start the holes on the mill. I then used a regular drill bit [all of this in my mill] and finished out the holes.

Once I had the 4 holes drilled, I hand tapped them for my standard #10-32 bolts.

I unbolted the two pieces then screwed on each shaft support. Everything looks dead on accurate.

I will cut the linear shaft next time I’m home and it’s not raining out [I'm on the west coast, it rains in the winter] as I have to use an angle grinder and its too dangerous to do it inside.

Thats all for now.

Posted in Uncategorized | Leave a comment

Still Alive

Hey. I’ve picked up this project again. I took a new job in December and I’ve been really busy since. I was working crazy hours and came home exhausted every day.

I’ve decided to purchase the remaining components for the Mill to get it completed in a reasonable amount of time. Hopefully I can build a second mill using the first mill, including everything I’ve learned since I began.

I shelled out the bucks and bought a complete 4 axis cnc controller kit from Gecko Drive. It’s pricey and completely circumvents all the work I did on my own drivers, but I realized that I need to get the thing working first and it will let me finish the mill, and debug the hardware first, then once that is working I can migrate back to the electronics.

Posted in Uncategorized | Leave a comment