Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Bartc <bc@freeuk.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: local arrays and stack |
| Date | 2015-03-23 20:25 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <mepspl$vf3$1@dont-email.me> (permalink) |
| References | (7 earlier) <87fv8vu1sx.fsf@kst-u.example.com> <mepfa1$26r$1@dont-email.me> <1f3963cb-74d2-4b38-9241-fd6ce6c5179c@googlegroups.com> <mepjd8$kb1$1@dont-email.me> <3f401918-2171-4808-bbdb-c86d7b879d07@googlegroups.com> |
On 23/03/2015 19:06, Xcott Craver wrote:
> On Monday, March 23, 2015 at 1:45:05 PM UTC-4, Bart wrote:
>> How? The VLA size is a runtime expression.
>
> Amending the stack pointer is a runtime operation.
But with a constant value, or a variable value? That makes a difference.
> Maybe we're talking about different things here. I'm imagining code like
>
> int func( int n ) {
> int i;
> for( i=0; i<30; i++ ) {
> int barray[i*i+1000*i+n];
> /** code to use barray[] **/
> }
> return 0;
> }
>
> When I compile this and disassemble func(), I see instructions at
> the top of the loop that decrement %rsp by an amount corresponding to
> i*i+1000*i+n ints; and then instructions at the end of the loop that
> reset %rsp to its previous value. In the case of this specific function,
> allocating and deallocating the array is really that simple.
Your system obviously is confident enough to push the stack by any
amount without checking. When I compile with gcc for Windows, I get the
following. Notice the lines marked saved_stack, and the call to
___chkstk_ms. (The 64-bit version is similar.)
_func:
LFB6:
.cfi_startproc
push ebp #
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
mov ebp, esp #,
.cfi_def_cfa_register 5
push ebx #
sub esp, 20 #,
.cfi_offset 3, -12
mov DWORD PTR [ebp-12], 0 # i,
jmp L2 #
L3:
mov eax, esp # tmp76,
mov ecx, eax # saved_stack.1, tmp76
mov eax, DWORD PTR [ebp-12] # tmp77, i
add eax, 1000 # D.1886,
imul eax, DWORD PTR [ebp-12] # D.1886, i
mov edx, eax # D.1886, D.1886
mov eax, DWORD PTR [ebp+8] # tmp78, n
add eax, edx # D.1886, D.1886
lea edx, [eax-1] # D.1886,
mov DWORD PTR [ebp-16], edx # D.1870, D.1886
sal eax, 2 # D.1887,
lea edx, [eax+3] # tmp79,
mov eax, 16 # tmp92,
sub eax, 1 # tmp80,
add eax, edx # tmp81, tmp79
mov ebx, 16 # tmp93,
mov edx, 0 # tmp84,
div ebx # tmp93
imul eax, eax, 16 # tmp85, tmp83,
call ___chkstk_ms
sub esp, eax #, tmp87
mov eax, esp # tmp86,
add eax, 3 # tmp88,
shr eax, 2 # tmp89,
sal eax, 2 # tmp90,
mov DWORD PTR [ebp-20], eax # barray.0, tmp90
mov esp, ecx #, saved_stack.1
add DWORD PTR [ebp-12], 1 # i,
L2:
cmp DWORD PTR [ebp-12], 29 # i,
jle L3 #,
mov eax, 0 # D.1886,
mov ebx, DWORD PTR [ebp-4] #,
leave
.cfi_restore 5
.cfi_restore 3
.cfi_def_cfa 4, 4
ret
> Why would it be awkward to push the stack pointer itself?
It just is, although you can do it indirectly. (For example are you
pushing rsp before or after it's been decremented?)
> In fact, the stack pointer is routinely pushed on the stack, albeit a little
> bit indirectly: at the start of a function it is customary to push the
> current value of the base pointer, and then copy the stack pointer into the
> base pointer.
That's pushing a frame pointer which is very common. Although gcc seems
to have done away with that, having everything as offsets from the stack
pointer instead.
> My above code compiles to assembly that just saves the original value of
> %rsp in a register, but there is no reason why it would be more complicated
> to save %rsp in a spare bit of stack itself.
Suppose you first push the stack pointer (ignoring the problem of when
it is incremented), then allocate N bytes of a VLA.
The trouble is, that saved stack pointer is now the other side of that
VLA! We would need to save N somewhere in order to access it. So we
might as well just store N.
Or maybe the stack pointer is pushed after the VLA, but it is necessary
to push the old value before the VLA is allocated.
So it can be a little tricky. There is also the question of how to
access that array: it's not known at compile-time what the offset will
be, so it might be necessary to store that into a pointer, something
else that needs to go onto the stack (but at a known fixed offset this
time).
But if a simple pointer is to be used, then just using malloc and free
starts to sound attractive, if chkstk_ms() needs to be called anyway.
--
Bartc
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
local arrays and stack fir <profesor.fir@gmail.com> - 2015-03-22 01:51 -0700
local arrays and stack asetofsymbols@gmail.com - 2015-03-22 02:23 -0700
Re: local arrays and stack Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-03-22 13:22 -0700
Re: local arrays and stack fir <profesor.fir@gmail.com> - 2015-03-23 01:55 -0700
Re: local arrays and stack Xcott Craver <xcottcraver@gmail.com> - 2015-03-22 15:07 -0700
Re: local arrays and stack fir <profesor.fir@gmail.com> - 2015-03-23 01:50 -0700
Re: local arrays and stack Robert Wessel <robertwessel2@yahoo.com> - 2015-03-23 04:26 -0500
Re: local arrays and stack fir <profesor.fir@gmail.com> - 2015-03-23 06:53 -0700
Re: local arrays and stack Xcott Craver <xcottcraver@gmail.com> - 2015-03-23 07:29 -0700
Re: local arrays and stack fir <profesor.fir@gmail.com> - 2015-03-23 08:34 -0700
Re: local arrays and stack Robert Wessel <robertwessel2@yahoo.com> - 2015-03-23 19:57 -0500
Re: local arrays and stack fir <profesor.fir@gmail.com> - 2015-03-24 04:01 -0700
Re: local arrays and stack fir <profesor.fir@gmail.com> - 2015-03-24 04:37 -0700
Re: local arrays and stack David Brown <david.brown@hesbynett.no> - 2015-03-24 13:04 +0100
Re: local arrays and stack glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-03-24 17:29 +0000
Re: local arrays and stack David Brown <david.brown@hesbynett.no> - 2015-03-24 20:01 +0100
Re: local arrays and stack Keith Thompson <kst-u@mib.org> - 2015-03-23 08:24 -0700
Re: local arrays and stack fir <profesor.fir@gmail.com> - 2015-03-23 08:42 -0700
Re: local arrays and stack Bartc <bc@freeuk.com> - 2015-03-23 16:07 +0000
Re: local arrays and stack fir <profesor.fir@gmail.com> - 2015-03-23 09:15 -0700
Re: local arrays and stack Keith Thompson <kst-u@mib.org> - 2015-03-23 09:16 -0700
Re: local arrays and stack Bartc <bc@freeuk.com> - 2015-03-23 16:34 +0000
Re: local arrays and stack Xcott Craver <xcottcraver@gmail.com> - 2015-03-23 10:12 -0700
Re: local arrays and stack Bartc <bc@freeuk.com> - 2015-03-23 17:44 +0000
Re: local arrays and stack fir <profesor.fir@gmail.com> - 2015-03-23 11:47 -0700
Re: local arrays and stack Xcott Craver <xcottcraver@gmail.com> - 2015-03-23 12:06 -0700
Re: local arrays and stack Bartc <bc@freeuk.com> - 2015-03-23 20:25 +0000
Re: local arrays and stack Xcott Craver <xcottcraver@gmail.com> - 2015-03-23 14:10 -0700
Re: local arrays and stack Bartc <bc@freeuk.com> - 2015-03-23 22:22 +0000
Re: local arrays and stack fir <profesor.fir@gmail.com> - 2015-03-23 23:57 -0700
Re: local arrays and stack David Brown <david.brown@hesbynett.no> - 2015-03-24 10:28 +0100
Re: local arrays and stack Bartc <bc@freeuk.com> - 2015-03-24 10:44 +0000
Re: local arrays and stack Martin Shobe <martin.shobe@yahoo.com> - 2015-03-24 06:38 -0500
Re: local arrays and stack Martin Shobe <martin.shobe@yahoo.com> - 2015-03-24 09:57 -0500
Re: local arrays and stack David Brown <david.brown@hesbynett.no> - 2015-03-24 13:22 +0100
Re: local arrays and stack Bartc <bc@freeuk.com> - 2015-03-25 16:07 +0000
Re: local arrays and stack David Brown <david.brown@hesbynett.no> - 2015-03-25 17:42 +0100
Re: local arrays and stack Bartc <bc@freeuk.com> - 2015-03-25 19:06 +0000
Re: local arrays and stack fir <profesor.fir@gmail.com> - 2015-03-25 12:26 -0700
Re: local arrays and stack fir <profesor.fir@gmail.com> - 2015-03-25 13:20 -0700
Re: local arrays and stack Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-03-25 12:28 -0700
Re: local arrays and stack David Brown <david.brown@hesbynett.no> - 2015-03-25 21:45 +0100
Re: local arrays and stack Keith Thompson <kst-u@mib.org> - 2015-03-25 12:50 -0700
Re: local arrays and stack Bartc <bc@freeuk.com> - 2015-03-25 22:33 +0000
Re: local arrays and stack David Brown <david.brown@hesbynett.no> - 2015-03-25 23:57 +0100
Re: local arrays and stack David Brown <david.brown@hesbynett.no> - 2015-03-25 21:42 +0100
Re: local arrays and stack fir <profesor.fir@gmail.com> - 2015-03-25 09:55 -0700
Re: local arrays and stack Keith Thompson <kst-u@mib.org> - 2015-03-25 12:36 -0700
Re: local arrays and stack fir <profesor.fir@gmail.com> - 2015-03-25 13:04 -0700
Re: local arrays and stack David Brown <david.brown@hesbynett.no> - 2015-03-25 21:52 +0100
Re: local arrays and stack Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-03-25 15:18 -0700
Re: local arrays and stack David Brown <david.brown@hesbynett.no> - 2015-03-26 00:02 +0100
Re: local arrays and stack Melzzzzz <mel@zzzzz.com> - 2015-03-26 00:47 +0100
Re: local arrays and stack Mel <mel@zzzzz.com> - 2015-03-24 14:58 +0100
Re: local arrays and stack Xcott Craver <xcottcraver@gmail.com> - 2015-03-24 06:58 -0700
Re: local arrays and stack Keith Thompson <kst-u@mib.org> - 2015-03-23 14:34 -0700
Re: local arrays and stack David Brown <david.brown@hesbynett.no> - 2015-03-24 10:16 +0100
Re: local arrays and stack David Brown <david.brown@hesbynett.no> - 2015-03-24 09:55 +0100
Re: local arrays and stack Noob <root@127.0.0.1> - 2015-03-24 10:23 +0100
Re: local arrays and stack Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-24 12:18 -0700
Re: local arrays and stack Keith Thompson <kst-u@mib.org> - 2015-03-24 13:42 -0700
Re: local arrays and stack Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-25 08:47 -0700
Re: local arrays and stack Xcott Craver <xcottcraver@gmail.com> - 2015-03-23 09:32 -0700
Re: local arrays and stack fir <profesor.fir@gmail.com> - 2015-03-23 09:57 -0700
Re: local arrays and stack Keith Thompson <kst-u@mib.org> - 2015-03-23 10:37 -0700
Re: local arrays and stack Xcott Craver <xcottcraver@gmail.com> - 2015-03-23 10:49 -0700
Re: local arrays and stack Keith Thompson <kst-u@mib.org> - 2015-03-23 12:48 -0700
Re: local arrays and stack jt@toerring.de (Jens Thoms Toerring) - 2015-03-23 22:29 +0000
Re: local arrays and stack James Kuyper <jameskuyper@verizon.net> - 2015-03-23 19:03 -0400
Re: local arrays and stack jt@toerring.de (Jens Thoms Toerring) - 2015-03-23 23:36 +0000
Re: local arrays and stack fir <profesor.fir@gmail.com> - 2015-03-24 02:17 -0700
Re: local arrays and stack Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-03-23 11:56 -0700
Re: local arrays and stack Robert Wessel <robertwessel2@yahoo.com> - 2015-03-23 20:00 -0500
Re: local arrays and stack David Brown <david.brown@hesbynett.no> - 2015-03-24 10:39 +0100
csiph-web