Both are used for each compilation. They're instance variables of each
other.

| *Who decides which to use*

In each AST node translation, you know for each value which one to use.

For example, when translating a return, the value to return needs to be
pushed on stack, so the valueTranslator is used:
visitReturnNode: aReturnNode
valueTranslator visitNode: aReturnNode value.
methodBuilder returnTop.

Yet, in #visitMethodNode:, you can see that the effectTranslator is used,
because no value is pushed on stack at the end of the method body:
...effectTranslator visitNode: aMethodNode body...

Then some behavior can be conditional, for example, in OCASTTranslator >>
#visitArrayNode:
...^ self visitLargeArrayNode: anArrayNode ]...
The self here represents either the value or effect translator and
dispatches to the correct method using polymorphism.

*| when do I know that I need to use ?*

If you need the value on stack, use the valueTranslator.
If you need the effect but not the value, use the effectTranslator.

If you're implementing something in the valueTranslator, it needs to push
something on stack at the end.
If you're implementing something in the effectTranslator, it doesn't push
anything on stack at the end.

| * the effect translator only or the value translator?*

I think you always need both.Let's take this method:

MyClass>>return1
     ^ 42.0

The method uses the effect translator to translate is body (^ 42.0).
The body is a sequence node, with one statement only. A sequence node
translates all its statements for effect, except the last one which depends
on how it's called (in this case, it's called with the effectTranslator, so
it's also for effect). Sequence translated for value are used in inlined
control structures.
So the first statement is translated for effect.
The return node asks first the value translator to push the float (42.0) on
stack, generating pushLiteral: 42.0
Then the return node generates the returnTop instruction.

**

I don't know how to explain better. I like to think of this problem as the
difference between procedures and functions in old programming languages.

On Mon, Dec 5, 2016 at 9:36 AM, Nicolai Hess <[email protected]> wrote:

>
>
> 2016-12-05 8:51 GMT+01:00 Nicolas Cellier <nicolas.cellier.aka.nice@
> gmail.com>:
>
>> Translate AST to byte codes?
>>
>
> (For opal, this first creates the intermediate representation (IR), but
> yes translating from AST to (finally) byte codes)
>
>
>> One for the case when we don't care of the result (we will pop it off the
>> stack), we just want the effect.
>>
>
> Ok.
>
>
>> The other case when we want to keep the resulting value on the stack.
>>
>
> Who decides which to use, when do I know that I need to use, the effect
> translator only or the value translator?
>
>
>>
>> 2016-12-05 8:47 GMT+01:00 Nicolai Hess <[email protected]>:
>>
>>> Hi,
>>>
>>> I need a short description for what the OCASTTranslator subclasses
>>> OCASTTranslatorForEffect
>>> OCASTTranslatorForValue
>>> are.
>>>
>>> I don't fully understand the usage.
>>>
>>> thanks in advance
>>> Nicolai
>>>
>>>
>>
>

Reply via email to