A "macro" (assembly language) or an "inline function" (C) is a short piece of code that is substituted literally instead of being called as a subroutine.
One advantage of macros in assembly language is that they allow the programmer intent of a few extremely common instruction sequences to be made explicit. MIPS has quite a few of them and even reserves one register for use by the assembler to expand macros. Likewise in 6502:
An advantage in C is that it allows certain optimizations, such as peephole, common subexpression elimination and elimination of unused results, to be applied between the inline function's body and that of the function calling it.
Other advantages and disadvantages of macros are similar to those of loop unrolling. Macros mean less time spent jumping around. The 6502, for example, has a 12-cycle subroutine call penalty, and in C, pushing a load of arguments to the stack can likewise cause slowdowns. Thus, macros can be a win if the body of the macro isn't much bigger than the code to get in and out of a subroutine. But misused macros can indeed make the code larger, and on architectures with cache and good branch prediction, this can slow the program down more than the jumping penalty if several inclusions of the macro's body make the inner loop not fit into cache.
Did I miss any?
In this post, adam_smasher wrote:
I've always found the value of macros to be overstated.
One advantage of macros in assembly language is that they allow the programmer intent of a few extremely common instruction sequences to be made explicit. MIPS has quite a few of them and even reserves one register for use by the assembler to expand macros. Likewise in 6502:
- ADD is clearer than CLC ADC, and SUB is clearer than SEC SBC.
- RSB (reverse subtract, as seen on ARM) is clearer than EOR #$FF SEC ADC, and RSC (reverse subtract with carry) is clearer than EOR #$FF ADC.
- ASR (arithmetic shift right) is clearer than CMP #$80 ROR.
An advantage in C is that it allows certain optimizations, such as peephole, common subexpression elimination and elimination of unused results, to be applied between the inline function's body and that of the function calling it.
Other advantages and disadvantages of macros are similar to those of loop unrolling. Macros mean less time spent jumping around. The 6502, for example, has a 12-cycle subroutine call penalty, and in C, pushing a load of arguments to the stack can likewise cause slowdowns. Thus, macros can be a win if the body of the macro isn't much bigger than the code to get in and out of a subroutine. But misused macros can indeed make the code larger, and on architectures with cache and good branch prediction, this can slow the program down more than the jumping penalty if several inclusions of the macro's body make the inner loop not fit into cache.
Did I miss any?