5

I am trying to implement C++ function using assembly code -- ARMv7-a, to be specific. Now I encounter a program that I don't know how C++ function template should be implemented in assembly. I try to compile the source code with -S -O1 flag to see the generated assembly but couldn't understand it. Can any one give me a brief idea how the C++ template is translated into assembly code? Just use the following simple function as an example:

template<typename T>
T f(T a) {
   return a + 1;
}

If you found any other function is easier to do the explain, please do so. Thanks!

Da Teng
  • 551
  • 4
  • 21
  • You wanna return a T, not void. – SwiftMango May 28 '14 at 16:15
  • Use your compiler to compile a simple example with a few different types for the templates parameters and look at the assembly it produces. That should give you a good idea of what the assembly should look like. If you don't understand what the output is, your question should be able getting help with that. – mah May 28 '14 at 16:16
  • 1
    @mah a useful thing is to embed comments, eg `asm("#begin template")` which will show up in `.s` file – Anycorn May 28 '14 at 16:17
  • Modern compilers are excellent at generating optimized assembly code. I'm not questioning your personal expertise, but trying to out-optimize the compiler seems like a very difficult task with a small payoff. – Dan Korn May 28 '14 at 16:17
  • @DanKorn he might just want to play around, which is a more-than legitimate reason to do this. – maxywb May 28 '14 at 16:18
  • I'm really amazed at the speed that people respond here. Thank you guys so much and sorry for the mistake in the code. The reason why I ask this is because I will use the NEON instructions for ARM to speed up the program, which operates on vectors of registers instead of just plain register. And the function want to implement is written in function template (thanks Nikos), that's why I ask the question. I think implementing each instance of the template separately is what I will do here. – Da Teng May 28 '14 at 16:39

2 Answers2

11

It would help, to phrase it correctly. It's not template function, it's function template ... noticed the difference?

A template, is to generate code upon instantiation. So in this case, if you instantiate your f for int the assembly would be identical with

int f(int a) { // Note that having a return type void is wrong here
   return a + 1;
}

There's lack of binary code generation for non instantiated templates. That's why lots of errors in template code remain dormant until instantiation for the problematic types is performed.

So for a real example, here are the 2 versions, one generated out of a function template and one out of a function (both for int); if it wasn't for the hint at the right one couldn't tell the differene:

    f2(1);
00BA25BE  push        1  
00BA25C0  call        f2<int> (0BA12F3h)  
00BA25C5  add         esp,4  
    f(1);
00BA25C8  push        1  
00BA25CA  call        f (0BA12EEh)  
00BA25CF  add         esp,4  

More on templates (types this time) and binary code representation here

Community
  • 1
  • 1
Nikos Athanasiou
  • 29,616
  • 15
  • 87
  • 153
  • Implementations are required to check function templates for e.g. syntax errors, even if they are not instantiated. – juanchopanza May 28 '14 at 16:33
  • @juanchopanza Yes, ok basic syntax checking; I stressed the dormant errors to display the lack of code generation for non instantiated types. Would you propose any rephrasing or should I complement with the existence of syntax checking? – Nikos Athanasiou May 28 '14 at 16:34
  • 2
    It is more than basic syntax checking. Non-dependent names are also looked up. It may be better to remove that line entirely, because it is complicated! – juanchopanza May 28 '14 at 16:38
6

You should implement each instance of the template in separate assembly.

Fundamentally, each template instance is a different type. You'll also need to deal with specialisations; partial or otherwise.

(Of course that means that you need to know in advance which set of Ts you need, but that is essentially what a C++ compiler does.)

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • It should be noted that the template types are filled in at compile time, and thus the assembly code that is emitted depends wholly on the type it was generated for. – maxywb May 28 '14 at 16:18