CS132 Fall 2004
« scope of temps »

Welcome Guest. Please Login or Register.
Dec 28, 2009, 12:14pm




CS132 Fall 2004 :: General :: Homework 3: MiniJava - Piglet :: scope of temps
   [Search This Thread][Reply] [Send Topic To Friend] [Print]
 AuthorTopic: scope of temps (Read 514 times)
dave
Guest
 scope of temps
« Thread Started on Nov 10, 2004, 3:28am »
[Quote]

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?
Link to Post - Back to Top  IP: Logged
Victor
New Member
*
member is offline





Joined: Sept 2004
Posts: 29
 Re: scope of temps
« Reply #1 on Nov 12, 2004, 1:20am »
[Quote]

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!
Link to Post - Back to Top  IP: Logged
dave
Guest
 Re: scope of temps
« Reply #2 on Nov 12, 2004, 3:28am »
[Quote]

this is very odd. then recursive function as of we know it won't work properly? unless we implement our own stack? ???
Link to Post - Back to Top  IP: Logged
kchang
Administrator
*****
member is offline

[avatar]



Joined: Sept 2004
Gender: Male
Posts: 52
 Re: scope of temps
« Reply #3 on Nov 12, 2004, 3:43am »
[Quote]

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
Link to Post - Back to Top  IP: Logged
mgp
Guest
 Re: scope of temps
« Reply #4 on Nov 13, 2004, 5:10pm »
[Quote]

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
Link to Post - Back to Top  IP: Logged
dave
Guest
 Re: scope of temps
« Reply #5 on Nov 14, 2004, 1:05am »
[Quote]

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...
Link to Post - Back to Top  IP: Logged
James
Guest
 Re: scope of temps
« Reply #6 on Nov 15, 2004, 12:46am »
[Quote]

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.
Link to Post - Back to Top  IP: Logged
James
Guest
 Re: scope of temps
« Reply #7 on Nov 15, 2004, 1:04am »
[Quote]

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..
Link to Post - Back to Top  IP: Logged
Ben Titzer
Guest
 Re: scope of temps
« Reply #8 on Nov 15, 2004, 12:30pm »
[Quote]

I will look at the source code of the interpreter, run some examples, and post an answer.
Link to Post - Back to Top  IP: Logged
James
Guest
 Re: scope of temps
« Reply #9 on Nov 15, 2004, 7:32pm »
[Quote]

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.
Link to Post - Back to Top  IP: Logged
dave
Guest
 Re: scope of temps
« Reply #10 on Nov 15, 2004, 11:07pm »
[Quote]

actually i think recusive call will save all temps of current function... can TA confirm this?
Link to Post - Back to Top  IP: Logged
armen
Guest
 Re: scope of temps
« Reply #11 on Nov 16, 2004, 2:34am »
[Quote]

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...
Link to Post - Back to Top  IP: Logged
Ben Titzer
Guest
 Re: scope of temps
« Reply #12 on Nov 16, 2004, 5:00pm »
[Quote]

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.
Link to Post - Back to Top  IP: Logged
CS132
Administrator
*****
member is offline




[homepage]

Joined: Sept 2004
Posts: 29
 Re: scope of temps
« Reply #13 on Nov 18, 2004, 2:46am »
[Quote]

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

Link to Post - Back to Top  IP: Logged
hhhhh
Guest
 hhhhh
« Reply #14 on Nov 18, 2005, 4:25am »
[Quote]




手机铃声下载 手机铃声 铃声下载 铃声 免费铃声下载 手机铃声免费下载
手机铃声下载手机铃声 铃声下载铃声 彩信 手机铃声下载 手机铃声 铃声 免费手机铃声下载 免费下载铃声 铃声下载 手机铃声下载 手机铃声 铃声下载 铃声 免费手机铃声下载 免费铃声下载
手机铃声下载 手机铃声 铃声下载 铃声 免费手机铃声下载 免费下载铃声
手机铃声下载 手机铃声 铃声下载 铃声 免费手机铃声下载 免费下载铃声 手机铃声下载 和弦铃声 特效铃声 手机铃声下载 mp3铃声 手机铃声下载 手机铃声下载 手机铃声下载 手机铃声下载 手机铃声下载 手机铃声下载 手机铃声下载 手机铃声下载 手机铃声下载 手机铃声下载 铃声 手机铃声 手机铃声下载 手机铃声下载 手机铃声下载
铃声 铃声 铃声 手机铃声下载手机铃声下载
手机铃声下载 手机铃声下载 手机铃声下载手机铃声下载 手机铃声下载 手机铃声下载
铃声 彩信 铃声 铃声下载 手机铃声下载 手机铃声下载 手机铃声下载 手机铃声下载 手机铃声下载 手机铃声下载 手机铃声下载 手机铃声 铃声 铃声下载 彩信 免费铃声下载 手机铃声免费下载 免费铃声 mp3铃声
mp3铃声下载 手机图片 手机图片下载 待机图片 手机待机图片 手机彩图 手机彩图下载 免费手机铃声 三星手机铃声下载 诺基亚铃声下载
和弦铃声 手机铃声下载
手机铃声下载
手机铃声下载

Link to Post - Back to Top  IP: Logged
   [Search This Thread][Reply] [Send Topic To Friend] [Print]

Click Here To Make This Board Ad-Free


This Board Hosted For FREE By ProBoards
Get Your Own Free Message Boards & Free Forums!