Function pointers

OK, I solved it all ( so far :slight_smile: ), mixing in some load-instructions and called on the result of that, which worked.

Here is the skeleton-code:

%kernel = type { int ()* }

int puts_kernel(){...}

; main()

%theKernel = malloc %kernel
%puts_kernelPTR = getelementptr %kernel* %theKernel, long 1, ubyte 0

store int ()* %puts_kernel, int ()** %puts_kernelPTR
%tmp.11 = load int ()** %puts_kernelPTR

%tmp.2 = call int %tmp.11()

free %kernel* %theKernel

:slight_smile:

Anders

OK, I solved it all ( so far :slight_smile: ), mixing in some load-instructions and
called on the result of that, which worked.

Here is the skeleton-code:

%kernel = type { int ()* }

int puts_kernel(){...}

; main()

%theKernel = malloc %kernel
%puts_kernelPTR = getelementptr %kernel* %theKernel, long 1, ubyte 0

store int ()* %puts_kernel, int ()** %puts_kernelPTR
%tmp.11 = load int ()** %puts_kernelPTR

%tmp.2 = call int %tmp.11()

free %kernel* %theKernel

Yup, that's close. You should be able to use an alloca instruction. The
difference between alloca and malloc is that alloca is typically
substantially faster than malloc, but the memory allocated by it is
automatically freed when the function it is in returns. In this case, it
shouldn't matter.

The bigger problem is that you are accessing a piece of memory that has
not been allocated:

   %theKernel = malloc %kernel
   %puts_kernelPTR = getelementptr %kernel* %theKernel, long 1, ubyte 0

The 'long 1' operand of the getelementptr instruction tells it to step to
the 1th kernel element. In terms of C, we have something like this:

  kernel *K = malloc (...)
  K[1].field =

If you use 'long 0' things should work fine.

-Chris

Ok, thanks for the tip! I am still learning the details of the syntax of
llvm...

Anders