3

I wonder if there is some theory/tool available to replace a piece of code that contains function calls, into code where all function call has been replaced by their respective code.

like

main()
{
   fun();
}

fun()
{
   int i;
   fun2();
}

fun2()
{
   int j;
}

into

main()
{
   int i;
   int j;
}

I know there is a lot to take care of, like local variable names, recursive calls, external function calls etc etc. .. ..

I also know that it may not be at all useful, but still does something like this exist? even in theory?

should I call it advance per-processor unit :)

Naftali
  • 144,921
  • 39
  • 244
  • 303
Amar
  • 415
  • 3
  • 16
  • What? What do you want to do exactly?? – Naftali Jun 26 '12 at 16:01
  • 10
    I would call it an optimizing compiler :) – JoeFish Jun 26 '12 at 16:01
  • 1
    The C Preprocessor does stuff eerily similar to this. – robert Jun 26 '12 at 16:02
  • 1
    Handling the local variables is actually pretty easy if you keep the `{}`, i.e. `main() { {int i;} {int j} }` – Fred Foo Jun 26 '12 at 16:02
  • 2
    Compilers can do inline generation of code from functions. Some program verifiers work by basically flattening a program this way as well. The former, however, don't generally make their intermediate results available in readable form. The latter would need *considerable* rewriting to produce the correct kind of result at all. – Jerry Coffin Jun 26 '12 at 16:03
  • @joefish: this will make performance worse on an avarage piece of code, because you loose code locality and reuse. – Daniel Jun 26 '12 at 16:04
  • @Dani: Often it makes performance better (avoiding the overhead of calling functions, and giving a wider scope for optimising), which is why most compilers often do this. It all depends on what the function does and where its called from; you can't really generalise about an "average piece of code". – Mike Seymour Jun 26 '12 at 16:10
  • 1
    Which language is this? Your functions have no return values, so they don't match C or C++. – juanchopanza Jun 26 '12 at 16:37
  • To add to @juanchopanza's remark. The way the code is written, it looks to me that you should learn some more about modern C before starting to ask yourself such very specific performance oriented questions. – Jens Gustedt Jun 26 '12 at 16:48
  • thanks neal for formatting. @jerry "Some program verifiers work by basically flattening a program this way as well", if there exists some program which does only this. pls give link. – Amar Jun 27 '12 at 06:45
  • @Amar: If there's one that does only this, I'm not aware of it. – Jerry Coffin Jun 27 '12 at 07:03

1 Answers1

10

The compiler can usually tell when it's a good idea to do this, and already automatically does inlining whenever needed. You can also suggest that a function should be inlined using the inline keyword before a function (note that it still doesn't actually force it, and the compiler might decide to avoid the inlining).It's generally not such a good idea to do this manually, as modern compilers tend to figure out the best possible inlinings on their own. This article explains inline functions really well, I found it very helpful

Edit 1:

There are several reasons why one might want to do that inlining you speak of. If you feel like your code is divided into many different functions reducing its clarity and making it overly verbose, you could try a refactoring tool, such as the one provided by the VAssist X Visual Studio plugin. Though this plugin doesn't really do what you suggest (I can't think of a tool that does), it can help move functions/ methods around with ease, allowing you to clean up your code.

Andrei Bârsan
  • 3,473
  • 2
  • 22
  • 46
  • One thing: Using `inline` is a suggestion to the compiler; it doesn't force it to inline the function. Question 9.1 in the FAQ-Lite link explains this. – chris Jun 26 '12 at 16:09
  • `inline` keyword does not force inlining. It's used mostly to avoid ODR violations. – Cat Plus Plus Jun 26 '12 at 16:09
  • 1
    The `inline` keyword does not actually force inlining. In fact many compilers ignore it as a hint for inlining and simply rely on their own heuristics. All inlining really does is allow the function to be defined in multiple translation units without causing linker errors: http://stackoverflow.com/a/157929/365496 – bames53 Jun 26 '12 at 16:10
  • 1
    Some compilers have things like _forceinline which will force a function to be inlined. This isn't always possible (think recursive functions) – jcoder Jun 26 '12 at 16:25
  • well andrei, thanks but question is not about use of inline. moreover inline functions are replaced by their respective code in the linked program, we cannot read them inline (they are not inline in high level language). as correctly termed by @jerry, I wanted to flatten the code. – Amar Jun 27 '12 at 06:50