Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 361155 - Code produced by reorganizeCode for FOR statement is not correct
Summary: Code produced by reorganizeCode for FOR statement is not correct
Status: CLOSED FIXED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: EDT (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-10-17 11:08 EDT by Paul Harmon CLA
Modified: 2017-02-23 14:15 EST (History)
1 user (show)

See Also:


Attachments
Java equavalent to EGL source (347 bytes, text/plain)
2011-10-17 11:09 EDT, Paul Harmon CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Paul Harmon CLA 2011-10-17 11:08:36 EDT
For the following EGL code:

program pgm1 type BasicProgram {}
	
	myInt int = 4;
	function main()

		aaa boolean;
		index int;
		
		for (index from 1 to f3(aaa) by 5)
			SysLib.writeStdout("inside loop");
		end
		
	end
	function f3(p1 boolean inout) returns(int)
		SysLib.writeStdout("myint = " + myint);
		myint = myint + 1;	
		return (myInt);
	end
	
end


The for statement produced by java gen looks like this:
		boolean eze$Temp1 = false;
		AnyBoxedObject<Boolean> eze$Temp3;
		eze$Temp3 = EAny.ezeWrap(aaa);
		int eze$Temp2;
		eze$Temp2 = f3(eze$Temp3);
		aaa = eze$Temp3.ezeUnbox();
		for (index = (short) 1; index <= eze$Temp2; index += (short) 5) {
			if (eze$Temp1) {
				AnyBoxedObject<Boolean> eze$Temp6;
				eze$Temp6 = EAny.ezeWrap(aaa);
				eze$Temp2 = f3(eze$Temp6);
				aaa = eze$Temp6.ezeUnbox();
			}
			else {
				eze$Temp1 = true;
			}
			SysLib.writeStdout("inside loop");
		}


However, this is not right. With this logic, F3 will be invoked before the first entry into the loop (which is fine), but F3 will not be invoked again until AFTER the loop in entered the 2nd time. I have set up this scenerio to show that F3 is only invoked 1 time, when in fact it should be invoked 3 times. 

Attached is the hand written java equivalent to the EGL program. When it runs, it produces:
   myint = 4
   inside loop
   myint = 5
   inside loop
   myint = 6

But the generated EGL only produces:
   myint = 4
   inside loop


The generated java should look more like this:

		AnyBoxedObject<Boolean> eze$Temp3;
		eze$Temp3 = EAny.ezeWrap(aaa);
		int eze$Temp2;
		eze$Temp2 = f3(eze$Temp3);
		aaa = eze$Temp3.ezeUnbox();
		for (index = (short) 1; index <= eze$Temp2; index += (short) 5) {
			SysLib.writeStdout("inside loop");
			AnyBoxedObject<Boolean> eze$Temp6;
			eze$Temp6 = EAny.ezeWrap(aaa);
			eze$Temp2 = f3(eze$Temp6);
			aaa = eze$Temp6.ezeUnbox();
		}


The problem is in the IRs produced by ReorganizeCode
Comment 1 Paul Harmon CLA 2011-10-17 11:09:19 EDT
Created attachment 205342 [details]
Java equavalent to EGL source
Comment 2 Paul Harmon CLA 2011-10-17 11:34:32 EDT
As a side note, if you change the for statement in my EGL source to:

		for (index from 1 to 10 by f3(aaa))


The resulting IRs produced by ReorganizeCode contains a boolean temporary variable this is not really needed. This temporary variable would normally be used for the TO expression, but since it does not need to be expanded, the temporary variable is not needed in this case.
Comment 3 Jeff Douglas CLA 2011-10-19 07:32:54 EDT
I agree with what you are saying Paul, however, your example is flawed as well. If the user codes a "continue for" statement, then the call to the function f3 (done in your example after the writestdout) will not get executed, but would in my case.

It seems there is no solution to a for statement, other than breaking the whole thing up into a while(true) and imbedded if statements.
Comment 4 Jeff Douglas CLA 2011-10-19 11:10:15 EDT
fixed
Comment 5 Paul Harmon CLA 2011-11-18 09:17:28 EST
Test works now