flat assembler
Message board for the users of flat assembler.

Index > Main > [solved] Global namespace clash fasmg

Author
Thread Post new topic Reply to topic
donn



Joined: 05 Mar 2010
Posts: 321
donn 16 Aug 2020, 17:55
Having a slight namespace clash (fasmg). I have two data-structures a list and a table (which I internally call a flow).


The list has been there quite some time, table is new, and both should have a function called getNextItem.

I use getNextItem without a namespace in so many places. In one project where I use this library, I do namespace list so it's called as

Code:
list.getNextItem    


Problems:
    1. list's getNextItem is called from many old source files globally and putting list in a namespace here would take a lot of effort.
    2. list's getNextItem is called within the new table data-structure.
    3. table has a getNextItem function which is called throughout the source and within the table datastructure.


Questions:
    1. Can list remain without a namespace?
    2. Can getNextItem in list have both a list namespace and remain accessible globally?
    3. How should the list and table function calls be defined?
    4. How should they be called within the table datastructure?




The way the table getNextItem is defined:

Code:

table:
namespace table
...
; Gets val at i,j and increments i,j with wrapping
getNextItem:
namespace getNextItem
        push rbp 
        mov rbp, rsp 
        sub rsp, (8*14);+(8*6)
...
end namespace  ; end getNextItem
...
end namespace ;endtable
    


list's getNextItem is global for now since it's used in so many places:
Code:

;------------
;   Gets the next item for a list
;   Uses the index value as a position
;   Takes List address as a parameter.
;------------

getNextItem:
        push rbp 
        mov rbp, rsp 
        sub rsp, (8);((Cool+(Cool)
    



They're called like this:

Code:
        sub rsp, 8*8
        mov rcx, [newAscendingItem.listAddress]
        call getNextItem    ; or list.getNextItem, or table.getNextItem
        add rsp, 8*8
        mov [newAscendingItem.comparisonItem], rax
    


I tried putting a list namespace and label before the list getNextItem label to 'alias' it but didn't work (tried a few variations like this I think):

Code:
list:
...
getNextItem:
namespace list
getNextItem:
namespace getNextItem:
    


I can get it to assemble, but the calls in the table datastructure are getting confused I think when it runs.

I can try any more experiments and provide more details if it helps. I also tried using ..getNextItem but wasn't able to find that. If any of this is unclear, I can clarify, not making much headway at the moment.
Post 16 Aug 2020, 17:55
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4073
Location: vpcmpistri
bitRAKE 16 Aug 2020, 18:26
Code:
getNextItem = 0

T.#..getNextItem = getNextItem

T:
namespace T
getNextItem = 1

display "0"+getNextItem
display "0"+..getNextItem ; value outside of this scope
end namespace
display "0"+getNextItem    
I trip up on this stuff all the time:
https://flatassembler.net/docs.php?article=fasmg_manual#2
(towards the end of section)

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 16 Aug 2020, 18:26
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8359
Location: Kraków, Poland
Tomasz Grysztar 16 Aug 2020, 18:41
I'm not sure if I understood your problem correctly, but it seems that your trouble is simply with defining an alias to a global symbol, so that you would be access it through an alias from inside a namespace where another symbol with the same name exists.

There are actually several ways to do it, you can choose whichever feels the most fitting to your style.

For a static label you can simply make an alias using LABEL directive:
Code:
getNextItem:    ; global

list:
label list.getNextItem at getNextItem

table:
namespace table
        getNextItem:    ; local

        call    getNextItem             ; call to local
        call    list.getNextItem        ; call to global
end namespace    
Or you can use a simplified ":=" syntax for a "label at" definition (the difference is that with LABEL you can also specify size of data associated with label, but that does not matter here):
Code:
list.getNextItem := getNextItem    
Note that you cannot define an alias this way:
Code:
; A WRONG EXAMPLE:
list:
namespace list
        getNextItem := getNextItem      ; this won't work, both identifiers end up referring to local symbol,
                                        ;  so this is a self-dependent (circular) definition, usually gets resolved with value 0
end namespace    
But you can also get an alias through a symbolic link, made with EQU or DEFINE:
Code:
getNextItem:    ; global

define aliasGetNextItem getNextItem

list:
namespace list
        getNextItem := aliasGetNextItem
end namespace    
The symbolic values preserve the namespace context, so even if you use them from inside another namespace, they still refer to the symbol in the context where they were defined. Because this links to the original symbol, and not just copies its value, it can be used to refer to a global symbol that might have changing value, for example:
Code:
define aliasItem item   ; link to global symbol

table:
namespace table
        item = 11h
        db item         ; db 11h
        db aliasItem    ; db 1
end namespace

item = 2                ; redefinition of global symbol

table2:
namespace table2
        item = 22h
        db item         ; db 22h
        db aliasItem    ; db 2
end namespace     
Post 16 Aug 2020, 18:41
View user's profile Send private message Visit poster's website Reply with quote
donn



Joined: 05 Mar 2010
Posts: 321
donn 17 Aug 2020, 03:05
Sorry, realize the best solution here is to just put list's getNextItem in a list namespace and do away with the global getNextItem. I am incurring technical debt by pushing this off. Since this is a compromise, I probably overcomplicated the description of the problem.

Used this:

Code:
define getNextListItem getNextItem
getNextItem:
        push rbp 
        mov rbp, rsp 
        sub rsp, (8);((8)+(8))
... ; Function body
    


and called it within table:

Code:
call getNextListItem
    


leaving the table's getNextItem as it was. Tested it out and it works! I was having some trouble with other flavors of this, each kept saying this:

Code:
/../../Table/Table.inc [365]:
        call list.getNextItem
call [2] parse_jump_operand [25] parse_operand [38] (CALM)
Error: symbol 'getNextItem' is undefined or out of scope.
    


I think in retrospect, this is because table getNextItem is buried beneath TWO namespaces, table and getNextItem. Not sure if not, will study both of your samples and use them as references.
Post 17 Aug 2020, 03:05
View user's profile Send private message Reply with quote
donn



Joined: 05 Mar 2010
Posts: 321
donn 19 Aug 2020, 04:46
Ah, pretty sure figured out why was getting

Code:
/../../Table/Table.inc [365]:
        call list.getNextItem
call [2] parse_jump_operand [25] parse_operand [38] (CALM)
Error: symbol 'getNextItem' is undefined or out of scope.    


In my 'table,' I had local stack vars labeled as so:

Code:

; Get each i val at Table's j index
; Should consider caching the j List in the Table so heapfree is not necessary
getNextJVals:
namespace getNextJVals
        push rbp 
        mov rbp, rsp 
        sub rsp, (8*14);+(8*6)

        label heapAddress qword at rbp-8  
        label handleAllocatedMemory qword at rbp-16
        label list qword at rbp-24 
        label table qword at rbp-32
        label index qword at rbp-40    


And there was a list local var, so list.getNextItem was probably getting confused. I've run into this before when first converting from fasm to fasmg. Squared away.
Post 19 Aug 2020, 04:46
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8359
Location: Kraków, Poland
Tomasz Grysztar 23 Aug 2020, 13:39
donn wrote:
Ah, pretty sure figured out why was getting

Code:
/../../Table/Table.inc [365]:
        call list.getNextItem
call [2] parse_jump_operand [25] parse_operand [38] (CALM)
Error: symbol 'getNextItem' is undefined or out of scope.    
BTW, I think that you are using an older version of fasmg, because with recent versions the error messaging got a bit improved and it shows a complete namespace path to the symbol that caused the problem.
Post 23 Aug 2020, 13:39
View user's profile Send private message Visit poster's website Reply with quote
donn



Joined: 05 Mar 2010
Posts: 321
donn 23 Aug 2020, 17:07
Confirmed, updated..!
Post 23 Aug 2020, 17:07
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.