473,657 Members | 2,385 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2596
On Tue, 29 Jul 2003 11:09:18 -0700, John Miles
<jm****@pop.rem ovethistomailme .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
1969
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> class SmartPtr : public OwnershipPolicy<T> { public:
0
1050
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 that take temporaries as parameters are never being called, and function returns are causing references to invalid memory. I was previously using gcc without any problems.
3
1411
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 std::auto_ptr<Window> AutoPtr;
6
3671
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
1522
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
1648
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 blame on "undefined execution of function parameters" in the C/C++ standard and "sequence points"? /////// Code snippet begins /////// #include <iostream> char foo="cat";
10
1650
by: steve yee | last post by:
the code is like this: class Asn_T { public: DataType_T m_data; }; class MyData {
83
3935
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 much"; default: return "Unknown error";
11
1591
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
8420
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8842
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8740
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8516
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7353
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.