aboutsummaryrefslogtreecommitdiff
path: root/docs/notes/multiple_return_values.txt
blob: 71b0432f96d8ae0295c1e85c2e69317a8d3ef6bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46


Multiple return values
----------------------

1. use more than one register

    obvious solution, but the number of registers is limited.

2. pass a pointer (as an input parameter), and put the output there

    works, and I think this is how C compilers typically handle large
    return values.
    
3. reserve extra data before the input parameters, and put the output there.

    should work, but why isn't it used?
    
    [wrong*] it's probably better to place the return parameters after the
    input parameters. that way both input and return parameters can have a
    dynamic size. *this is not going to work with tail recursion, and
    dynamically sized return parameters would mean that the return address
    wouuld have to be moved.

4. overwrite the input parameters, and let the caller shrink the stack when
   it no longer needs them.
   
    can work, but the input and output parameters can't be used at the same
    time.

-----------------

things that won't work:

1. return data above the stack (immediately after/above the return value).

    data above the stack pointer can get overwritten in some environments.
    for example, registers may be stored on the stack when a process is
    preempted.


2. returning data anywhere other than the stack/registers.

    it can of course work, but then we need to deal with thread safety.