Showing posts with label structures. Show all posts
Showing posts with label structures. Show all posts

Thursday, June 4, 2015

Loading into, reading from, structs


I've found that in order to make progress on my language, it helps to have very specific lines of code that I want to make sure my compiler compiles, and focus on those.

The couple lines of code that I've been struggling with off and on lately have been:

  newpos.x = pos.x + vel.x;
  newpos.y = pos.y + vel.y;

Really simple stuff; retrieve member data from an aggregate type, do some arithmetic, store the results into member fields of an aggregate type. It so happens that this is simple physics / animation code, but the compiler doesn't care about that.

I had done some reading of the LLVMlite documentation and was sure that I needed to do some sort of insert_value and extract_value code. And then there's getelementptr, which is tricky, to the point that it's got its own FAQ page: http://llvm.org/releases/3.5.1/docs/GetElementPtr.html

In my previous post, I talked about using Clang as a reference - I might not have to use all of LLVM if Clang's got a way to accomplish what I'm doing, I can do the same thing that it does. And all it does is simple getelementptr instructions. 

So, I started stripping out stuff that I had built to handle stuff on the left hand side of assignments differently from stuff on the right hand side. I put some getelementptr calls into my code. I started seeing errors about "gep not implemented on identified struct type", which I thought I maybe had to work around, but it turns out that somebody else had run into that bug and had submitted a fix for it. So, git pull and reinstall the LLVMlite module, and all of a sudden, things were working a lot better.

There's still some gross bits that are left over from my efforts to set up a complex solution to what was ultimately a simple problem - that's got to get cleaned up before I push this stuff up to my repository. I think I've also found some pointer stuff that now looks ripe for refactoring.

Next short term feature: (static?) array support - being able to create an array of objects, either simple or aggregate objects, and then reading and writing those objects.

Longer term ambition: some sort of Asteroids-ish clone. That'll put the above physics / animation code into context, and having a collection of asteroids floating around the screen will motivate the array support.

Getting away from language features, an Asteroids clone will provide pressure for more OpenGL support, which will be a good thing.

Wednesday, May 27, 2015

Recursive Structs in LLVM experiments, using Clang


You don't get enough science in computer science, like with the whole experimental method.

I'm (currently?) using LLVMlite (https://github.com/numba/llvmlite) in my compiler project. Some things you might want to do aren't well covered in the LLVMlite documentation, or even in the LLVM documentation.

Case in point, I'd like to be able to generate code for something like this:

  Ship bar;
  bar.pos.y = 7;
  bar.vel.y = 3;
  bar.pos.y += bar.vel.y;

I haven't been making a lot of progress lately (you'd know), trying to wrap my head around what I'd even need to keep track of for that to work, including most recently thinking that I'd need to be able to walk the tree to create a path of element offsets, keep track of l-vals and r-vals, and some sort of stack of symbol tables.

But then I recalled that I had used Clang before to figure out how they do things.

  clang -S -emit-llvm teststruct.c 

I just pointed that commandline at some C that does what I want, and it spits out LLVM instructions that I can use as a reference.

For example, some of the instructions generated for the above code is:

  %10 = getelementptr inbounds %struct.Ship* %bar, i32 0, i32 0
  %11 = getelementptr inbounds %struct.Vec2f* %10, i32 0, i32 1
  %12 = load i32* %11, align 4
  %13 = add nsw i32 %12, %9
  store i32 %13, i32* %11, align 4
 
So, there you go. Just get the element pointer, then get the element pointer, and then bam, put the thing in the thing. That's it.

Reverse engineering. One of my top several favorite kinds of engineering.