[LLVMdev] Memory Alignment, Heap allocation.

John Criswell criswell at cs.uiuc.edu
Mon Jun 14 12:43:01 PDT 2004


Tobias Nurmiranta wrote:
> Hi,

Chris and others can give you better ideas on the ideal ways to 
implement what you want, but I'll give some ideas/answers for now.

> 
> 1.
> 
> A small question: How do I ensure memory alignment? I want all malloced
> memory, globals and functions to be 4-byte aligned. Does llvm have any
> ".align" keyword?

No, LLVM does not currently have an alignment keyword (that I know of). 
  It might be useful to have one, though.

> 
> I'm currently implementing a small scheme toy-compiler, and want to use
> the lowest 2 bits for type tags. It's Currently 380 lines of
> scheme-code[1], quite similar to the compiler in SICP[2], which I hope to
> get self-applicable later on.

I think the only reliable way you could do this would be to implement 
your own memory allocator function that returned memory from the heap. 
Your code could ensure that every pointer it returned was on a 4 byte 
boundary.

Any other technique would take advantage of side-effects of the LLVM 
code generators.

> 
> 2.
> 
> Can I change the calling conventions / frame handling, so that call frames
> are allocated on the heap instead of on the stack? Right now all my
> compiled functions take an environment as an argument to lookup variables
> in the scheme-function. It would perhaps be nicer if I could use the call
> frames instead, but I can't since lambdas in it can escape when the
> frame is popped of the stack, for example:

I suppose you could change the LLVM code generators to use whatever 
calling conventions you want, but I believe your current implementation 
is the correct way of passing information from a function back to its 
caller.

Changing the code generators to do what you describe essentially 
"breaks" the LLVM calling convention model (i.e. in LLVM, items on the 
stack disappear when a function returns; you do not want to assume that 
something on the function's stack still exists after the function 
already returns).

Passing a pointer may look a little kludgy, but it is correct and should 
be fine.

-- John T.

> 
>   (lambda (x) (lambda (z) (+ x z))) ; the inner lambda is returned.
> 
> Regards,	Tobias
> 
> [1] www.ida.liu.se/~tobnu/compile.ss
> 
>     Can currently for example compile and run:
> 
>     (compiler '((lambda (x y z)
>                   (if (seteq (car (cdr (cons x (cons y 3)))) z)
>                     (add 1 0)
>                     (sub 2 1)))
>                 1 2 2))
> 
> [2] Structure and Interpretation of Computer Programs, Abelson & Sussman.
> 
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://mail.cs.uiuc.edu/mailman/listinfo/llvmdev


-- 
*********************************************************************
* John T. Criswell                         Email: criswell at uiuc.edu *
* Research Programmer                                               *
* University of Illinois at Urbana-Champaign                        *
*                                                                   *
* "It's today!" said Piglet. "My favorite day," said Pooh.          *
*********************************************************************





More information about the llvm-dev mailing list