Temp segment really needed for arrays?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Temp segment really needed for arrays?

attachedy
In the coursera video, the general statement  "a[x] = b[y]" is translated into vm-code. The "temp segment" is used to avoid overriding a's alignment of THAT by b's alignment of THAT. To me, it seems that it should be possible to first "resolve" the right (b-) side of the statement and handling the left (a-) side afterwards, so that no temp-segment ist needed. This would also work for nested statements, if you always start at the innermost one.

The above statement would translate to:

// push b[y] to the top of the stack
push b
push y
add
pop pointer 1
push that 0

// write the stack's top value to a
push a
push x
add
pop pointer 1
pop that 0

I'm pretty sure, that i'm missing something here (I did not start with the implementation, yet), but where is the error?
Reply | Threaded
Open this post in threaded view
|

Re: Temp segment really needed for arrays?

WBahn
Administrator
But statements like

push b
push y

are not valid VM commands. The push command requires a named segment followed by a constant index.
Reply | Threaded
Open this post in threaded view
|

Re: Temp segment really needed for arrays?

attachedy
Yes, you're right. It wrote it in pseudo-VM-Code, as it is not clear whether a and b are the first, second, ... local variable. But if we assume, that a = local 0 and b = local 1, as well as x = 4 and y = 3, the code would change to:

// push b[3] to the top of the stack
push local 1
push constant 3
add
pop pointer 1
push that 0

// write the stack's top value to a[4]
push local 0
push constant 4
add
pop pointer 1
pop that 0

The question remains the same, doesn't it?
Reply | Threaded
Open this post in threaded view
|

Re: Temp segment really needed for arrays?

WBahn
Administrator
I'm trying to remember what I used the temp segment for in the compiler I wrote. It's been long enough in the past that I can't recall for sure. I know I did use it and I know I only used it for one or perhaps two things. But I think I only used it for implementing some of the functions in the standard library (can't be sure though).

I think many people (possibly including the authors) use it in instances when it's not really necessary, but rather just because it's convenient.

I don't see anything wrong with what you have done for this case and don't see any need to use the temp segment to accomplish this. It could be that it makes sense to do so based on the order that things naturally get done in a recursive-decent compiler.
Reply | Threaded
Open this post in threaded view
|

Re: Temp segment really needed for arrays?

attachedy
Thank you WBahn for sharing your thoughts.

When implementing the compiler, I realised that my suggestion is at least very complicated if not impossible to implement using the top-down parser logic described in the course. I think the way that is described in the slides has been preferred by the authors to circumvent the problem of implementing a more complicated bottom-up compiler. And I am thankful that they've decided to keep the parsing logic as simple as possible, without forgetting to give the reader enough background knowledge for exploring more sophisticated methods.