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

  • SEARCH
  • Home
  • 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 8180979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:22:14+00:00 2026-06-07T00:22:14+00:00

I have an idea for a functional programming language design that makes heavy use

  • 0

I have an idea for a functional programming language design that makes heavy use of bound function parameters. I’m trying to express bound function parameters in x86 assembly as part of the compiler implementation.

var add  = function(x,y) { return x + y; };
var add2 = add.bind({}, 2);
console.log( add2(3) );      // prints 5

For interoperability reasons i’d like to produce bare function pointers, so my first concept idea is to allocate some executable memory on the heap, and copy in a stub that (a) pushes an extra parameter and (b) calls the target function. This would be a part of the standard library and would return a native function pointer that i can use from the rest of the x86 assembly program.

I think i’ve come across a problem with this approach – if the stub uses call to get to the target function, then the stack contains a return address which ends up being interpreted as a function argument! And if the stub uses jmp to get to the target function, then neither the caller nor callee know exactly how to clean up the stack when the function returns.

How can this be resolved? I suppose a register could be permanently reserved as a flag for this behaviour but that’s hardly elegant.

How is bind() implemented in native compilers for functional languages?

  • 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-07T00:22:15+00:00Added an answer on June 7, 2026 at 12:22 am

    On some further thought, i think this can be done by using a callee-cleanup convention and manually managing all my return addresses. It’s similar to stdcall, but not identical, since call/ret can’t be used(?).

    Pseudocode:

    main:
       ; create stub, copy in 0x02 and &add, the resulting function pointer goes in add2
       local add2 = _create_trampoline
    
       ; make the call
       push [return address]
       push 0x03 ;arg1
       jmp  add2
    
    ; the resulting stub, held on the heap somewhere
    add2:
       push 0x02 ;bound argument
       jmp  add
    
    ; var add(x,y)
    add:
       local x = pop
       local y = pop
    
       eax = x + y;       
    
       jmp pop
    

    This way, add knows the stack is laid out as y x [ptr] and execution returns correctly.

    It seems a little drastic to lose call/ret over this, and the stack frame for function add is pretty tenuous, so i’ll leave the question open for at least another 24 hours in hope of a better solution.


    EDIT: On some further thought, you can keep cdecl, caller-cleanup, call/ret and everything by simply carrying along the return address in the bound trampoline (which only requires clobbering one register, or moving it to the stack and back).

    Pseudocode:

    main:
       ; create stub, copy in 0x02 and &add, the resulting function pointer goes in add2
       local add2 = _magic(0x02, &add);
    
       ; make the call
       push 0x03;
       call add2;
    
    add2:
       ebx = pop;     ;the return address goes in a temporary
       push 0x02;
       push ebx;
       jmp add
    
    ; var add(x,y)
    add:
       push ebp;
       mov ebp, esp;
    
       ; local variables are [ebp+8] and [ebp+12]
       perform calculation into eax
    
       leave
       ret
    

    There, the result is a quite concise technique to implement bound function parameters as executable objects on the heap, maintaining cdecl calling conventions. There will undoubtedly be problems with this approach when implementing it, but i expect it is workable and not too horribly inefficient.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been learning functional programming and I come to idea, to assemble mathematical
At work I have a programming language encoded in a database record. I'm trying
I am new to C++/Python mixed language programming and do not have much idea
I have no idea how to properly 'return' this JSON object (in JavaScript): function
I have no idea what's wrong with this code: onupgradeneeded = function(){ z =
If you've bought into the functional programming paradigm, the chances are that you like
I've often heard that functional programming solves a lot of problems that are difficult
I have an idea for a program that will do a particular job for
I recently bought Programming Scala , and have been reading through it. The language
It's generally accepted that copy and paste programming is a bad idea, but what

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.