Can Higher-Level Languages Express Functions With Multiple Entry Points?
Asked by
porridge enthusiast
I've encountered assembly routines where two or more labels act as separate entry points, perform slightly different setup work, and then branch into shared code. For example, one entry point could load 0 into a register while another loads 1, after which both jump to the same code that writes the value to a memory-mapped hardware register and returns.
Can this pattern be written directly in C, C++, or another higher-level language? If so, what would the syntax look like? Or is it usually something a compiler discovers and generates automatically when combining similar functions?
Answers
This is generally called a multiple-entry-point function or multiple-entry routine. Standard C and C++ do not let one function jump directly into the middle of another function. A local label belongs to its own function, and crossing function boundaries with a jump would violate the language and normal calling conventions.
The structured version is usually to put the shared work in a helper function, for example having EnableInterrupts and DisableInterrupts calculate different values and then both call a common routine. An optimizing compiler may later inline or merge parts of those functions, producing assembly that resembles the original pattern.
Reply to this answer
Compilers can sometimes create this layout automatically, especially when two functions share a common tail or differ only in a small initial operation. This is related to tail merging and other interprocedural optimizations. It is also common for compilers to arrange shared code this way when lowering switch statements.
However, the generated layout is an implementation detail. You should write valid, separate high-level functions and inspect the assembly if the exact machine-code arrangement matters.
Reply to this answer
Some older languages had explicit support for multiple entry points; older Fortran, for example, included an ENTRY feature. In modern structured languages this style is generally discouraged because it creates tangled control flow. The main historical motivation was saving bytes or avoiding extra jumps on small machines, while today a helper function or compiler optimization usually gives clearer source code without a meaningful performance cost.
Reply to this answer
You can force something similar with nonstandard extensions, such as computed gotos or label addresses in some C implementations, but jumping between functions is not portable C or C++. It can break assumptions about stack frames, register state, exception handling, debugging, and control-flow analysis, so it is rarely suitable for production code.
For hardware access, a normal implementation would typically have two small functions that pass 0 or 1 to a shared helper, with the memory-mapped address represented through the platformโs appropriate volatile pointer or hardware-access API.
Reply to this answer
Add your answer
Answers are reviewed before publication.
Related Discussions