473,395 Members | 1,466 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Compiler's use of function parameters as temporaries

Hi --

This is a bit of an implementation-specific problem, but I'd like to
post it here to see if there's a general answer within the auspices of
the language.

I'm developing a high(er)-level scripting language designed explicitly
to complement C++. One of the language's features is the ability to
pass "out" parameters to an MSVC __cdecl function by reference. After
the C++ function returns, but before popping the parameters from the
stack, the scripting-language VM copies any out-parameters back from the
C++ stack frame to the variables they originally came from. In this way
any modifications made by the C++ function to its parameters are
propagated back to the scripting language.

As you might guess, this approach has problems when compiling with
optimization turned on. In theory the C++ compiler may decide that any
write operations to parameters which are never subsequently read can be
omitted. In practice, I haven't seen that happening, but what I *have*
seen is that the compiler likes to reuse function parameters when it
needs to allocate a temporary. Hilarity ensues when the VM retrieves an
unrelated temporary from the stack instead of the parameter it expects.

Is there a general, implementation-independent way to tell the compiler
(a) not to reuse a function parameter as a temporary; and (b) not to
perform dead-code elimination on writes to function parameters?

I experimentally tried applying "volatile" to a parameter ("volatile
Mat4x4 *SRC"), and while MSVC accepted the code, SRC still got reused as
a temporary within the body of the function.

Similarly, passing the parameters by reference rather than as pointers
("Mat4X4 &SRC") doesn't make any difference.

Thanks in advance for any suggestions.

-- jm

------------------------------------------------------
http://www.qsl.net/ke5fx
Note: My E-mail address has been altered to avoid spam
------------------------------------------------------
Jul 19 '05 #1
1 2567
On Tue, 29 Jul 2003 11:09:18 -0700, John Miles
<jm****@pop.removethistomailme.net> wrote:
Hi --

This is a bit of an implementation-specific problem, but I'd like to
post it here to see if there's a general answer within the auspices of
the language.

I'm developing a high(er)-level scripting language designed explicitly
to complement C++. One of the language's features is the ability to
pass "out" parameters to an MSVC __cdecl function by reference.
How are the parameters in the C++ function declared? If parameters are
passed by value, then the compiler is highly likely to modify them
arbitrarily inside the function. In addition, the function might even
operate on a copy of the parameter, so that changes aren't accessible
in the calling stack frame.

Afterthe C++ function returns, but before popping the parameters from the
stack, the scripting-language VM copies any out-parameters back from the
C++ stack frame to the variables they originally came from. In this way
any modifications made by the C++ function to its parameters are
propagated back to the scripting language.

As you might guess, this approach has problems when compiling with
optimization turned on. In theory the C++ compiler may decide that any
write operations to parameters which are never subsequently read can be
omitted. In practice, I haven't seen that happening, but what I *have*
seen is that the compiler likes to reuse function parameters when it
needs to allocate a temporary. Hilarity ensues when the VM retrieves an
unrelated temporary from the stack instead of the parameter it expects.

Is there a general, implementation-independent way to tell the compiler
(a) not to reuse a function parameter as a temporary; and (b) not to
perform dead-code elimination on writes to function parameters?
If the parameter is passed by value there is no reason for the
compiler to leave the variable in the state in which it was passed,
and I doubt there is a way to make it.

I experimentally tried applying "volatile" to a parameter ("volatile
Mat4x4 *SRC"), and while MSVC accepted the code, SRC still got reused as
a temporary within the body of the function.

Similarly, passing the parameters by reference rather than as pointers
("Mat4X4 &SRC") doesn't make any difference.


It sounds like you are doing the wrong thing. You need to have the C++
function take pointers, not values. References might work, bearing in
mind that references are probably implemented as pointers.

Tom
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Tobias Langner | last post by:
compiling this line: SmartPtr<LongWrapper, RefCount> p1=SmartPtr<LongWrapper, RefCount>(new LongWrapper(1)); with SmartPtr being: template <class T, template <class> class OwnershipPolicy>...
0
by: Ryan Mitchley | last post by:
Hi all Before I go chasing ghosts - has anyone encountered any difficulties using the Move Constructor (Mojo) approach with the Intel compiler (7.1 or higher). I am finding that functions...
3
by: Alf P. Steinbach | last post by:
Code as follows: #include <iostream> // std::cout #include <string> // std::string #include <memory> // std::auto_ptr class Window { public: typedef...
6
by: Jason Heyes | last post by:
I am interested in the lifetime of a function argument in two cases. They are: 1. void foo(Bar bar); 2. void foo(const Bar &bar); In each case I call foo like so: foo(Bar());
4
by: Alex Vinokur | last post by:
Compiler GNU gpp.exe (GCC) 3.4.1 Foo(300) = Foo(500); // Foo(300) is const. Why does a compiler compile that? ------ foo.cpp ------ struct Foo { explicit Foo(int) {}
13
by: Generic Usenet Account | last post by:
Compile the following snippet of code and run it. If the program spits out bat:bat instead of bat:zat, what would you say? Would you say that the compiler has a problem, or would you lay the...
10
by: steve yee | last post by:
the code is like this: class Asn_T { public: DataType_T m_data; }; class MyData {
83
by: Anonymous | last post by:
Came across some code summarized as follows: char const* MyClass::errToText(int err) const { switch (err) { case 0: return "No error"; case 1: return "Not enough"; case 2: return "Too...
11
by: =?iso-8859-1?q?Erik_Wikstr=F6m?= | last post by:
struct foo { int i; }; int bar(foo& f) { return f.i++; } int main() { bar(foo());
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.