Author Topic: %repl context  (Read 9704 times)

Klod

  • Guest
%repl context
« on: October 04, 2010, 01:37:20 AM »
I have been playing around with %repl preprocessor instruction:

Code: [Select]
%push __GM_CONTEXT__
%define %$context __GM_CONTEXT__
%define %$previouscontext
%warning We start in:%$context  ;%$$$context context stack is only 1 level deep

    %push __SECOND__
    %define %$context __SECOND__
    %assign %$$gmONE 0            ;define local to __GM_CONTEXT__
    %warning %$context  previous context %$$context %$$gmONE
    %pop

    %push __YET_ANOTHER_CONTEXT__           
    %xdefine %$context __YET ANOTHER_CONTEXT__
    %assign  %$$gmONE %$$gmONE +1  ;reassign local to __GM_CONTEXT__
    %warning %$context %$$gmONE
   
        %push __THIRD__
        %define %$context __THIRD__
        %define %$third var_in_%+%$context
        %repl __GM_CONTEXT__        ;replace context name
        %assign  %$$$gmONE %$$$gmONE +1    ;replacing context name does not expose locals to __GM_CONTEXT__
        %xdefine %$$$previouscontext %$context
        %warning after "repl __GM_CONTEXT__"  %$context %$$$gmONE     ;we are still in context __THIRD__
        %repl __THIRD__
        %warning %$context %$third
        %pop
       
    %pop

%warning %$context %$gmONE %$previouscontext  ;local in __GM_CONTEXT__ assigned in __SECOND__
%pop

Is it possible to switch to a different context and access its locals as %$ ??
As this example demonstrates the "locals" are local to  __THIRD__ and not to __GM_CONTEXT__

What I would like to do is to switch to a named context, use its locals and then switch back to the context I came  from.

Any suggestions are welcome

Regards Klod

Offline cm

  • Jr. Member
  • *
  • Posts: 65
Re: %repl context
« Reply #1 on: October 04, 2010, 01:35:54 PM »
Is it possible to switch to a different context and access its locals as %$ ??
As this example demonstrates the "locals" are local to  __THIRD__ and not to __GM_CONTEXT__

What I would like to do is to switch to a named context, use its locals and then switch back to the context I came  from.

The current NASM contexts don't work that way; there's no way to "switch" contexts or take some from the stack and push them back on the stack later.

However, one workaround I have used is to expect a local smac in each "child context" which was created on top of the one that you want to access, named exactly like the original variable. Preprocessing code that was written with a specific context and its locals in mind will work as previously, it only has to be adapted if it checks for the current context's name. Most importantly, no logic is required to keep track of how far the desired context is away. All translation is done wherever you push and pop contexts. That translation is straightforward with locals that are always defined, here is a simplified example:

Code: [Select]
%push _MAIN
%assign %$local 38
%push _SUB
; To get the current value as defined by ctx _MAIN:
%assign %$local %$$local
db %$local
; To update the smac local to ctx _MAIN, in case %$local was changed:
%assign %$$local %$local
%pop _SUB
%pop _MAIN

This assumes that the context _SUB is pushed after _MAIN, and also assumes %$local to always be defined (and remain defined throughout the text preprocessed with context _SUB), but these can be addressed by little more workaround code if that is an issue.
C. Masloch

Klod

  • Guest
Re: %repl context
« Reply #2 on: October 06, 2010, 03:27:09 AM »
Hi cm, thanks for your post. I picked up on the helper macro and expanded the above example with your suggestion. This example is preprocessor only, compiles and gives me the results I had expected.

Code: [Select]
%macro smac 1
    %defstr temp %
        %rep %{1}
            %strcat temp ,temp ,'$'
        %endrep
    %strcat temp ,temp ,'$'
    %deftok  %$index temp
%endm
%push __MOM__
    %define %$contextname __MOM__
    %assign %$ctxframe 0
    %warning %$contextname %$ctxframe

    %push __FIRST__
        %define %$contextname __FIRST__
        %assign %$ctxframe %$$ctxframe+1
        %warning %$contextname %$ctxframe
   
            %push __SECOND__
                %define %$contextname __SECOND__
                %assign %$ctxframe %$$ctxframe+1
                %warning %$contextname %$ctxframe
               
                %push __THIRD__
                    %define %$contextname __THIRD__
                    %assign %$ctxframe %$$ctxframe+1
                    %warning %$contextname %$ctxframe
                   
                    %push __FORTH__
                        %define %$contextname __FORTH__
                        %assign %$ctxframe %$$ctxframe+1
                        %warning %$contextname %$ctxframe
                       
                        %push __FIFTH__
                            %define %$contextname __FIFTH__
                            %assign %$ctxframe %$$ctxframe+1
                            %warning %$contextname MOM has %$ctxframe punks
                            smac %{$ctxframe}
                            %assign %{$index}newinmom 5
                            %warning %{$index}newinmom  %$$$$$$newinmom    %$contextname                       
                        %pop
                            smac %{$ctxframe}
                            %assign %{$index}newinmom %{$index}newinmom-1
                            %warning %{$index}newinmom  %$$$$$newinmom %$contextname   
                    %pop
                        smac %{$ctxframe}
                        %assign %{$index}newinmom %{$index}newinmom-1
                        %warning %{$index}newinmom  %$$$$newinmom %$contextname   
                %pop
                    smac %{$ctxframe}
                    %assign %{$index}newinmom %{$index}newinmom-1
                    %warning %{$index}newinmom  %$$$newinmom %$contextname   
            %pop
                smac %{$ctxframe}
                %assign %{$index}newinmom %{$index}newinmom-1
                %warning %{$index}newinmom  %$$newinmom %$contextname   
    %pop
        smac %{$ctxframe}
        %assign %{$index}newinmom %{$index}newinmom-1
        %warning %{$index}newinmom  %$newinmom %$contextname   
%pop

Regards,
Klod