Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8535285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:23:47+00:00 2026-06-11T10:23:47+00:00

In a stack-based intermediate language, such as CIL or Java bytecode, why are there

  • 0

In a stack-based intermediate language, such as CIL or Java bytecode, why are there local variables? One could just use only the stack. May not be so easy for hand-crafted IL, but a compiler can surely do it. But my C# compiler does not.

Both the stack and the local variables are private to the method and go out of scope when the method returns. So it could not have anything to do with side-effects visible from outside the method (from another thread).

A JIT compiler would eliminate loads and stores to both stack slots and local variables when generating machine code, if I am correct, so the JIT compiler also does not see the need for local variables.

On the other hand, the C# compiler generates loads and stores for local variables, even when compiling with optimizations enabled. Why?


Take for example, the following contrived example code:

static int X()
{
    int a = 3;
    int b = 5;
    int c = a + b;
    int d;
    if (c > 5)
        d = 13;
    else
        d = 14;
    c += d;
    return c;
}

When compiled in C#, with optimizations, it produces:

    ldc.i4.3        # Load constant int 3
    stloc.0         # Store in local var 0
    ldc.i4.5        # Load constant int 5
    stloc.1         # Store in local var 1
    ldloc.0         # Load from local var 0
    ldloc.1         # Load from local var 1
    add             # Add
    stloc.2         # Store in local var 2
    ldloc.2         # Load from local var 2
    ldc.i4.5        # Load constant int 5
    ble.s label1    # If less than, goto label1
    ldc.i4.s 13     # Load constant int 13
    stloc.3         # Store in local var 3
    br.s label2     # Goto label2
label1:
    ldc.i4.s 14     # Load constant int 14
    stloc.3         # Store in local var 3
label2:
    ldloc.2         # Load from local var 2
    ldloc.3         # Load from local var 3
    add             # Add
    stloc.2         # Store in local var 2
    ldloc.2         # Load from local var 2
    ret             # Return the value

Note the loads and stores to the four local variables. I could write the exact same operations (disregarding the obvious constant propagation optimization) without using any local variables.

    ldc.i4.3        # Load constant int 3
    ldc.i4.5        # Load constant int 5
    add             # Add
    dup             # Duplicate top stack element
    ldc.i4.5        # Load constant int 5
    ble.s label1    # If less than, goto label1
    ldc.i4.s 13     # Load constant int 13
    br.s label2     # Goto label2
label1:
    ldc.i4.s 14     # Load constant int 14
label2:
    add             # Add
    ret             # Return the value

It seems correct to me, and a lot shorter and more efficient. So, why do stack-based intermediate languages have local variables? And why does the optimizing compiler use them so extensively?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:23:48+00:00Added an answer on June 11, 2026 at 10:23 am

    Depending on the situation, but especially when calls are involved where the parameters have to be re-ordered to match the call, a pure stack is not enough if you don’t have registers or variables at your disposal. If you wanted to make this stack-only, you’d need additional stack manipulation abilties, such as the ability to exchange/swap the two top items of the stack.

    In the end, while it may be possible to express everything as pure stack-based in that case, it can add a whole lot of complexity to the code, bloating it and making it more difficult to optimize (local variables are ideal candidates for being cached in registers).

    Also remember that in .NET you can pass parameters by reference, how could you create the IL for this method call without a local variable?

    bool TryGet(int key, out string value) {}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to setup a CodeIgniter based project for local development (LAMP stack), and once
Imagine I have a stack-based toy language that comes with the operations Push, Pop,
I've come across a proprietary stack-based scripting language that looks like a somewhat simplified
I'm writing a simple stack-based language in C and was wondering how I should
I'm potentially interested in exploring a stack-based language like Forth (or Factor). What I'd
Any suggestions for my stack based allocator? (Except for suggestions to use a class
My understanding of calculators is that they are stack-based. When you use most calculators,
I am trying to use a jQuery based Photo Stack which uses the following
I'm experiencing a bit of cognitive dissonance between C-style stack-based programming, where automatic variables
I understand that JVM and CLR were designed as stack-based virtual machines. When JIT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.