|
Author | Topic: scope of temps (Read 514 times) |
dave Guest
|  | scope of temps « Thread Started on Nov 10, 2004, 3:28am » | |
Also, another thing the professor didn't confirm in class is the scope of temps. For most of the sample programs TEMPs just kept on increasing through out the program. I'm not sure if this is because you can override the TEMP value of one procedure in another procedure. This seems odd because if this were true, the recursive call of a program will overwirte all the TEMP values of temporaries previously stored. I guess my question comes down to this: does PIGLET have an implicit stack for function calls, so that we can repeat TEMP # from one procedure to another? Can TAs please confirm this?
| |
|
Victor New Member
 member is offline
Joined: Sept 2004 Posts: 29
|  | Re: scope of temps « Reply #1 on Nov 12, 2004, 1:20am » | |
looks like temps are global
MAIN MOVE TEMP 20 1 PRINT TEMP 20 CJUMP CALL Proc (5) L0 PRINT TEMP 20 L0 NOOP END
Proc [1] BEGIN MOVE TEMP 20 TEMP 0 RETURN 1 END prints: 1 5
incidentally, this is my first piglet program!
| |
|
dave Guest
|  | Re: scope of temps « Reply #2 on Nov 12, 2004, 3:28am » | |
this is very odd. then recursive function as of we know it won't work properly? unless we implement our own stack?
| |
|
kchang Administrator
     member is offline
![[avatar]](http://www.cs.ucla.edu/~kchang/kchang.gif)
Joined: Sept 2004 Gender: Male  Posts: 52
|  | Re: scope of temps « Reply #3 on Nov 12, 2004, 3:43am » | |
Try the same test with arguments, temp 1, temp 2, etc. The interpreter makes assumption about certain things. Try it and see.
In future projects, you may have to worry about caller/callee save in the stack but don't worry about that for now.
Kevin
| |
|
mgp Guest
|  | Re: scope of temps « Reply #4 on Nov 13, 2004, 5:10pm » | |
It appears that if you use recursion, then temps in the recursive function will not be wiped out by recursive calls. In this sense, temps are 'protected'. Replace temp 20 by temp 200 and the result is the same.
However, whenever this is not the case, temps seem to be global. Consider a modified version version of the MoreThan4 example on Kevin's handout:
MAIN PRINT CALL BEGIN MOVE TEMP 23 BEGIN MOVE TEMP 24 HALLOCATE 8 MOVE TEMP 25 HALLOCATE 4 HSTORE TEMP 24 4 MT4_Change HSTORE TEMP 24 0 MT4_Start HSTORE TEMP 25 0 TEMP 24 RETURN TEMP 25 END
HLOAD TEMP 21 TEMP 23 0 HLOAD TEMP 22 TEMP 21 0 RETURN TEMP 22 END (TEMP 23 1 2 3 4 5 6 ) END
MT4_Start [ 7 ]
BEGIN MOVE TEMP 300 TEMP 1 MOVE TEMP 41 TEMP 2 MOVE TEMP 42 TEMP 3 MOVE TEMP 43 TEMP 4 MOVE TEMP 44 TEMP 5 MOVE TEMP 45 TEMP 6 PRINT TEMP 300 PRINT TEMP 41 PRINT TEMP 42 PRINT TEMP 43 PRINT TEMP 44 PRINT TEMP 45 MOVE TEMP 20 CALL BEGIN MOVE TEMP 29 TEMP 0 HLOAD TEMP 27 TEMP 29 0 HLOAD TEMP 28 TEMP 27 4 RETURN TEMP 28 END (TEMP 29 TEMP 6 TEMP 5 TEMP 4 TEMP 3 TEMP 2 TEMP 1 )
PRINT TEMP 300 PRINT TEMP 41 PRINT TEMP 42 PRINT TEMP 43 PRINT TEMP 44 PRINT TEMP 45
RETURN TEMP 20 END
MT4_Change [ 7 ]
BEGIN MOVE TEMP 300 TEMP 1 MOVE TEMP 41 TEMP 2 MOVE TEMP 42 TEMP 3 MOVE TEMP 43 TEMP 4 MOVE TEMP 44 TEMP 5 MOVE TEMP 45 TEMP 6 PRINT TEMP 300 PRINT TEMP 41 PRINT TEMP 42 PRINT TEMP 43 PRINT TEMP 44 PRINT TEMP 45 RETURN 0 END
Note that when MT4_Change is called with MT4_Start's parameters reversed, and they are stored into temp 300, temp 41 - 45, the changes are seen in MT4_Start.
- mgp
| |
|
dave Guest
|  | Re: scope of temps « Reply #5 on Nov 14, 2004, 1:05am » | |
man, after a closer look.. there seems to be a total global ordering for mapping local variables in ALL methods to temps... I don't know if it this is necessary. But since that's what the sample code does... I guess I'll follow... some times I really wish the Piglet spec can be a bit more detail and clear on certain things.. so we don't have to dig through stacks of temp #s just to figure out what, how and why codes are generated... especially when all factors are going against you...
| |
|
James Guest
|  | Re: scope of temps « Reply #6 on Nov 15, 2004, 12:46am » | |
From what I'm seeing, the Temp#0-19 are pushed on a stack and then popped on CALLs. Everything 20+ is global. Sort of wish this was in the specs, as I've spent like 4 hours trying to find out what was causing the prob in my convertor.
| |
|
James Guest
|  | Re: scope of temps « Reply #7 on Nov 15, 2004, 1:04am » | |
Okay, forget what I said, it was an early assessment. It's just as mgp says I think, where it saves the variables if a call to the same function is done. Otherwise, it's all global..
| |
|
Ben Titzer Guest
|  | Re: scope of temps « Reply #8 on Nov 15, 2004, 12:30pm » | |
I will look at the source code of the interpreter, run some examples, and post an answer.
| |
|
James Guest
|  | Re: scope of temps « Reply #9 on Nov 15, 2004, 7:32pm » | |
After finally completing proj#3 I think I have it figured out! Parameter temp's are saved on the stack for sure. So any TEMP that got passed in as a parameter will be saved. Everything else seems to be global, and can be overwritten. Hope this helps, and I hope I'm right this time.
| |
|
dave Guest
|  | Re: scope of temps « Reply #10 on Nov 15, 2004, 11:07pm » | |
actually i think recusive call will save all temps of current function... can TA confirm this?
| |
|
armen Guest
|  | Re: scope of temps « Reply #11 on Nov 16, 2004, 2:34am » | |
what's the bottom line... is this a true statement... "So any TEMP that got passed in as a parameter will be saved(w/respect to func). Everything else seems to be global, and can be overwritten. "
it seems to be true!
so if it's true, then we need to save on stack, but i thought we don't need to worry about calle/caller for this proj...
| |
|
Ben Titzer Guest
|  | Re: scope of temps « Reply #12 on Nov 16, 2004, 5:00pm » | |
I have looked at the source code and confirmed that interpreter is not saving the temporaries across calls. It saves temporaries 0 - 20 across calls!
I have discussed this with the other TAs and the professor. This is not the intended behavior for the interpreter. It is a bug. We are working on fixing it.
For now, my suggestion is the following:
For each procedure, allocate a different set of temporaries, starting with numbers greater than 20.
Do not worry about mutually recursive functions FOR NOW, until the new interpreter is released.
Until that time, work on HW 4.
Hopefully we will have a fix to the interpreter this week.
| |
|
CS132 Administrator
     member is offline
Joined: Sept 2004 Posts: 29
|  | Re: scope of temps « Reply #13 on Nov 18, 2004, 2:46am » | |
I have fixed the interpreter so that it saves all the temporaries across calls. You can download the new interpreter from : http://compilers.cs.ucla.edu/cs132/software/pgi.jar
Here's what the fix does, Consider this piglet program:
MAIN MOVE TEMP 100 100 PRINT TEMP 100 MOVE TEMP 15 HALLOCATE 4 HSTORE TEMP 15 0 A_foo MOVE TEMP 117 CALL A_foo (TEMP 15 1234) PRINT TEMP 100 END
A_foo [ 2 ] BEGIN MOVE TEMP 100 200 PRINT TEMP 100 RETURN 0 END
The old interpreter would output : 100 200 200 for this program
the new one will output : 100 200 100
| |
|
hhhhh Guest
| |
|