Interesting CSPro compiler quirk I found today

Other discussions about CSPro
Post Reply
alex_izm
Posts: 21
Joined: December 9th, 2016, 11:31 am

Interesting CSPro compiler quirk I found today

Post by alex_izm »

This is not necessarily a bug. It looks more like a compiler optimization, but it did make me spend half an hour figuring out.

Code:

Code: Select all

function foo()
	errmsg("foo called");
end;
	
function fooCaller(fooResult)
	//!!!if fooCaller() has no instructions here, fooResult is never evaluated and thus foo() is never called
end;

PROC SOME_PROC
	//!!!error message in foo() not shown when calling fooCaller()
	fooCaller(foo());
This probably was there since ISSA. It always amazes me how after 20+ years working with CSPro, I still find these quirks :D
Gregory Martin
Posts: 1883
Joined: December 5th, 2011, 11:27 pm
Location: Washington, DC

Re: Interesting CSPro compiler quirk I found today

Post by Gregory Martin »

Wow, that is a quirk! Without even looking at the C++ code, I know that it's true that if a function has no body, that the arguments aren't evaluated. I'll change that for CSPro 8.1.
josh
Posts: 2403
Joined: May 5th, 2014, 12:49 pm
Location: Washington DC

Re: Interesting CSPro compiler quirk I found today

Post by josh »

I'll change that for CSPro 8.1.
or just lean into lazy evaluation and don't evaluate any expressions until they are used. The set of CSPro users who also program in Haskell will be forever grateful :D
alex_izm
Posts: 21
Joined: December 9th, 2016, 11:31 am

Re: Interesting CSPro compiler quirk I found today

Post by alex_izm »

Wouldn't foo() expression still be evaluated in this case even with lazy evaluation? fooResult is not a pointer to foo() here. It is a different expression altogether. fooResult expression is not used, but foo() expression is clearly used. What CSPro is doing here is double-lazy evaluation :roll:. Where calls into functions with no body are completely skipped even if used.
Gregory Martin
Posts: 1883
Joined: December 5th, 2011, 11:27 pm
Location: Washington, DC

Re: Interesting CSPro compiler quirk I found today

Post by Gregory Martin »

Binding function parameters via lambda expressions could result in the the lazy evaluation that Josh mentions. For instance in C++:
int GetArgument() { return 1; }

void func1(int x) { }

void func2(std::function<int()> get_x) { }

func1(GetArgument());

func2([] { return GetArgument(); });
With func2, with the argument passed using a lambda expression, the actual GetArgument function would only be called on demand. I have no idea how Haskell works, but theoretically a programming language could wrap all function arguments in some object that lends itself to lazy evaluation.
josh
Posts: 2403
Joined: May 5th, 2014, 12:49 pm
Location: Washington DC

Re: Interesting CSPro compiler quirk I found today

Post by josh »

Yeah, in Haskell an expressions is not evaluated until you actually access it. Kind of like if it was wrapped in a lambda in C++.
Post Reply