calling-convention

Can Scala call by reference?

让人想犯罪 __ 提交于 2019-11-28 07:32:59
I know that Scala supports call-by-name from ALGOL, and I think I understand what that means, but can Scala do call-by-reference like C#, VB.NET, and C++ can? I know that Java cannot do call-by-reference, but I'm unsure if this limitation is solely due to the language or also the JVM. This would be useful when you want to pass an enormous data structure to a method, but you don't want to make a copy of it. Call-by-reference seems perfect in this case. Java and Scala both use call by value exclusively, except that the value is either a primitive or a pointer to an object. If your object

Is returning a 2-tuple less efficient than std::pair?

限于喜欢 提交于 2019-11-28 06:52:37
Consider this code: #include <utility> #include <tuple> std::pair<int, int> f1() { return std::make_pair(0x111, 0x222); } std::tuple<int, int> f2() { return std::make_tuple(0x111, 0x222); } Clang 3 and 4 generate similar code for both on x86-64: f1(): movabs rax,0x22200000111 ret f2(): movabs rax,0x11100000222 ; opposite packing order, not important ret But Clang 5 generates different code for f2() : f2(): movabs rax,0x11100000222 mov QWORD PTR [rdi],rax mov rax,rdi ret As do GCC 4 through GCC 7: f2(): movabs rdx,0x11100000222 mov rax,rdi mov QWORD PTR [rdi],rdx ; GCC 4-6 use 2 DWORD stores

Calling a function pointer whose assigned function has less arguments then the pointer type

一个人想着一个人 提交于 2019-11-28 06:21:38
问题 Consider the following code: #include <iostream> typedef int (*test_func_t) (int, int, int); int print_integer (int a) { std::cout << "num: " << a << "\n"; return a; } int main (int argc, char * argv[]) { test_func_t func = (test_func_t) &print_integer; std::cout << "calling with 3 parameters func(5,7,9)\n"; func(5,7,9); return 0; } As you can see, a type (test_func_t) is defined as a function with 3 int arguments. A function pointer (func) is assigned with a pointer to "print_integer", which

What is the meaning and usage of __stdcall?

旧街凉风 提交于 2019-11-28 03:07:56
I've come across __stdcall a lot these days. MSDN doesn't explain very clearly what it really means, when and why should it be used, if at all. I would appreciate if someone would provide an explanation, preferably with an example or two. JaredPar All functions in C/C++ have a particular calling convention. The point of a calling convention is to establish how data is passed between the caller and callee and who is responsible for operations such as cleaning out the call stack. The most popular calling conventions on windows are __stdcall , Pushes parameters on the stack, in reverse order

How exactly does the callstack work?

可紊 提交于 2019-11-28 02:36:43
I'm trying to get a deeper understanding of how the low level operations of programming languages work and especially how they interact with the OS/CPU. I've probably read every answer in every stack/heap related thread here on Stack Overflow, and they are all brilliant. But there is still one thing that I didn't fully understand yet. Consider this function in pseudo code which tends to be valid Rust code ;-) fn foo() { let a = 1; let b = 2; let c = 3; let d = 4; // line X doSomething(a, b); doAnotherThing(c, d); } This is how I assume the stack to look like on line X: Stack a +-------------+

Why does IA-32 have a non-intuitive caller and callee register saving convention?

不羁岁月 提交于 2019-11-28 00:28:41
The common calling conventions for IA-32 say: • Callee-save registers %ebx, %esi, %edi, %ebp, %esp Callee must not change these. (Or restore the caller's values before returning.) • Caller-save registers %eax, %edx, %ecx, condition flags Caller saves these if it wants to preserve them. Callee can freely clobber. Why does this strange convention exist? Why not save all the registers before calling another function? Or have the callee save and restore everything with pusha / popa ? Why would you want to write code to save registers in every function that you might not need? That would add extra

How does argument passing work?

房东的猫 提交于 2019-11-28 00:18:29
问题 I want to know how passing arguments to functions in C works. Where are the values being stored and how and they retrieved? How does variadic argument passing work? Also since it's related: what about return values? I have a basic understanding of CPU registers and assembler, but not enough that I thoroughly understand the ASM that GCC spits back at me. Some simple annotated examples would be much appreciated. 回答1: Considering this code: int foo (int a, int b) { return a + b; } int main (void

How to declare an __stdcall function pointer

↘锁芯ラ 提交于 2019-11-27 17:09:04
问题 I tried this typedef void (* __stdcall MessageHandler)(const Task*); This compiles but gives me this warning (VS2003): warning C4229: anachronism used : modifiers on data are ignored I want to declare a pointer to a function with stdcall calling convention? What am I doing wrong? 回答1: As MSDN says, the correct way to write this is typedef void (__stdcall *MessageHandler)(const Task*); 来源: https://stackoverflow.com/questions/5298394/how-to-declare-an-stdcall-function-pointer

What kind of C11 data type is an array according to the AMD64 ABI

社会主义新天地 提交于 2019-11-27 16:22:21
I was researching the calling convention of x86_64 that's used on OSX and was reading the section called "Aggregates and Unions" in the System V x86-64 ABI standard ). It mention arrays and I figured that was like a fixed length c array, e.g. int[5] . I went down to "3.2.3 Parameter Passing" to read about how arrays were passed and if I'm understanding correctly, something like uint8_t[3] should be passed in registers as it's smaller than the four eightbyte limit imposed by rule 1 of the classification of aggregate types (page 18 near the bottom). After compiling I see that instead it's being

Does each PUSH instruction push a multiple of 8 bytes on x64?

霸气de小男生 提交于 2019-11-27 15:38:37
On x64, does each PUSH instruction push a multiple of 8 bytes? If not, how much does it push? Also, how much stack space does each function parameter consume? No, but in practice, one always pushes an 8 byte value onto the stack. Function parameters consuming varying amounts of stack space depending on the size of the function parameter and whether it is passed in the stack, in the registers, or passed by reference. If one passes a function parameter in the stack by pushing , then the fact that there are convenient push instructions that pushes 8 bytes strongly suggests that you pass the