Monday, December 27, 2010

Calling Convention

Calling convention refers to how arguments, return values are passed between caller and callee; how a call stack (if it exists) is constructed, how a return address is saved and retrieved; how local variables are assigned memory and how they are accessed. In short - it refers to prologue and epilogue of a function. Below presentation shows how a call stack changes during a function call.






Note: The presentation is not protected by any copyright and can be distributed freely.

Friday, December 24, 2010

Simple parameter passing with gcc

# simple.c

int foo(int a, int b) { return a+b; }
int main() { return foo(10, 20);}

Compile with -fdump-tree-all and -fdump-rtl-all. Analyze the expand and global register allocation passes. For a target like AVR, below snapshot shows expand pass for the foo(10,20) code:





Note that r24 and r22 are chosen to pass the arguments 10 and 20 respectively. Also, arguments are passed from left to right. How the register selection is controlled in gcc?

Target hooks - FUNCTION_ARG and FUNCTION_ARG_ADVANCE decide which register to be used for each argument. So a back-end developer decides the argument register selection algorithm. It is also partially dependent on macros CALL_USED_REGISTERS, ARG_POINTER_REGNUM and wholly dependent on CUMULATIVE_ARGS structure. In this particular case, register r25-r8 are used for passing arguments in descending order. Since an integer in AVR is 2bytes while each register is byte sized, to pass a single argument two registers are used. Also only even-numbered registers are chosen, which means even for char, even number registers are chosen.

The other interesting aspect is register chosen to store return value. Following rtl patterns can be seen:

//return value for foo
(insn 16 24 21 5 simple.c:3 (set (reg/i:HI 24 r24)
        (reg:HI 44 [ <result> ])) -1 (nil))


// return value for main()
(insn 14 22 19 5 simple.c:8 (set (reg/i:HI 24 r24)
        (reg:HI 42 [ <result> ])) -1 (nil))


How the register selection is controlled here? Target hook FUNCTION_VALUE_REGNO_P decides which register to hold the function value which is nothing but return value in the most of the cases (except when register windows exist). In the context of AVR, r24 is used as return register. (Click on source code hyperlinks. Version: GCC 4.6 trunk)
 
#define FUNCTION_VALUE_REGNO_P(N) ((int) (N) == RET_REGISTER) 

/* Returns register number for function return value.*/ 
int avr_ret_register (void) {   return 24; }

Tuesday, September 28, 2010

Say no to "GOTO"

Dijkstra says so. Read this article published in ACM,1968. So does Niklaus Wirth, designer of PASCAL in his Good Ideas article. Donald Knuth, in his war against GOTO, thinks, almost unambiguously, that the appearance of 'GOTO' in any language is a sure sign of bad design.

Wednesday, September 15, 2010

HURD


Maybe I'll deal with GNU HURD in several parts. First let me get off the little excitement of having used Qemu image of Hurd. Ofcourse it is tiny, yet something is up on my screen. Not much, yet something is up.



I  tried to boot hurd iso image using qemu. Incase you haven't heard qemu, it is a portmanteau for quick emulator - pretty fast and neat. Also, for those of you who are still wondering what HURD is, it is a non-linux like kernel designed to replace all linux flavours. It uses mach micro-kernel and is designed to support multi-core-processing and multi-tasking. It hates the orthogonal view of files which was made popular by unix. It tries to do everything via ports.

I tried to capture the boot messages using vnc viewer.  Had to disable AutoSelect option given my gnome settings. One can see mach micro kernel in the first screen shot (enlarge it). I can go pages and pages on its design and on how it is struggling to replace UNIX/Linux (virtually every *ix) but all my talk will still be a poor replica of the Hurd documentation.


Couldn't stop myself from wondering why hurd sounds more like absurd!

Tuesday, September 14, 2010

Super Optimization

Except for the 'super' in superoptimization, there is actually nothing much super in super optimizers. Take a look at wiki and Denali. GNU Super Optimizer - GSO, is another effort.

Thursday, August 19, 2010

Bug

One of my colleagues forwarded me this interesting bug. 

--------------------------------------------------------------------------------------------------

Dear GNOME,

Please Spare my Boyfriend :-(

-- Ilana

Wednesday, August 18, 2010

This and That

This and that about compiler industry and compilers. Among the many e-mails that I receive about compiler job openings, this one which is added in footer kind of format below the job description, is pretty interesting:

Note:
  • Compiler engineers are considered to be top notch computer engineers and scientists.
  • These are masters of algorithm, data structures, pattern matching schemes.
  • The thinking needs to be broad based and since the compiler users will be for any kind of applications
  • They have excellent programmers and have expertise in language design
  • They are usually well versed with parsing techniques and so make good use of pattern matching to automate complex routine jobs.
  • This functions has a R&D orientation
    • Involves being on top of papers published in areas of optimization techniques , algorithms etc
    • Have ability to do deep study of published papers and come up with a design /implementation  to achieve the same. Papers don’t give the details.
  • This cannot be equated to regular software design and development.