flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > How to copy constants associated with structure fields?

Author
Thread Post new topic Reply to topic
CandyMan



Joined: 04 Sep 2009
Posts: 414
Location: film "CandyMan" directed through Bernard Rose OR Candy Shop
CandyMan 28 May 2020, 08:42
I would like to rework the standard macros defining the structure to automatic copy associated constants as in the CopyConstants macro. How to do it?
Code:
include "inc\struct.inc"

struct  One
A       db ?
B       db ?
E       db ?
ends

Associated.One.A = 4
Associated.One.B = 5

struct  Two One
C       db ?
D       db ?
ends


macro   CopyConstants Child,Parent
{
 irp Element,Child
 \{
   if defined Associated.\#Parent\#.Element
     Associated.\#Child\#.Element = Associated.\#Parent\#.Element
   end if
 \}
}

CopyConstants Two,One
;Associated.Two.A = Associated.One.A
;Associated.Two.B = Associated.One.B

dd Associated.Two.A    

_________________
smaller is better
Post 28 May 2020, 08:42
View user's profile Send private message Reply with quote
ProMiNick



Joined: 24 Mar 2012
Posts: 804
Location: Russian Federation, Sochi
ProMiNick 28 May 2020, 11:00
What for association?

struct One
A db 4
B db 5
E db ?
ends

struct Two One
C db ?
D db ?
ends

def values will be there.

when by some address thou ll have to keep union of structs.
Thou could ever define it as
rb sizeof.MAXIMUM_STRUCT_OF_THEM

for moving values from 1 structured data to another one - usualy copyed continuos block, so no matter what members in it.
when structure defined in some place & used in another - in that case moving data could be avoided at all, define struct in same place with help of virtual directive and use it from there.

What practical sense could require associated value with struct fields (default value & field offset in structure are not enought?)?

Never place structs(when it is assumed that they inited by some values) on stack(always in global data) and thou will rich happyest work with structures.

(I always economy stack - it is for thread dependant stuff, but process common shouldn`t go there)
Post 28 May 2020, 11:00
View user's profile Send private message Send e-mail Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
DimonSoft 28 May 2020, 12:10
ProMiNick wrote:
Never place structs(when it is assumed that they inited by some values) on stack(always in global data) and thou will rich happyest work with structures.

(I always economy stack - it is for thread dependant stuff, but process common shouldn`t go there)

What about thread safety then? A good advice comes with rationale behind it. Performance trade-off (due to synchronization of various threads around a commonly used structure “variable” instead of allowing each of them use their own ones) might be significant under pressure and it only removes (?) the need to initialize the structure once.

BTW, you’re suggesting to rely on global data being initialized to a particular value. It really is zero in fact but I doubt it is really documented somewhere. A program should get its pages from the system zeroed-out for security reasons but any non-zero byte value might have worked as well, it might just be a “coincidence” that the value is 0. So, although it’s quite safe to rely on the fact in general, I’d suggest stating explicitly that the guarantee is there only for certain cases (say, static variables in C/C++).
Post 28 May 2020, 12:10
View user's profile Send private message Visit poster's website Reply with quote
CandyMan



Joined: 04 Sep 2009
Posts: 414
Location: film "CandyMan" directed through Bernard Rose OR Candy Shop
CandyMan 28 May 2020, 13:09
I want to copy the constant value associated with the structure field when defining the structure.
I have for example constants:
Associated.One.A = 4
Associated.One.B = 5

I want constants to be automatically defined when defining the "Two" structure
Associated.Two.A = 4.
Associated.Two.B = 5.

And so for each child field.

I don't want to write several times
Associated.Two.A = Associated.One.A
Associated.Two.B = Associated.One.B

These constants can indicate the count of procedure parameters.

_________________
smaller is better
Post 28 May 2020, 13:09
View user's profile Send private message Reply with quote
ProMiNick



Joined: 24 Mar 2012
Posts: 804
Location: Russian Federation, Sochi
ProMiNick 28 May 2020, 13:54
to keep params count is another macro designed interface, NOT STRUCT!
Code:
macro interface definition
{
        match name tail, definition: \{
                virtual
                        db \`name
                        load initial@struct byte from $$
                        if initial@struct = '.'
                                display 'Error: name of interface should not begin with a dot.',0Dh,0Ah
                                err
                        end if
                end virtual

                macro endi \\{
                        if $
                                display 'Error: definition of ',\`name,' contains illegal instructions.',0Dh,0Ah
                                err
                        end if
                        match =@guid,@guid \\\{
                                         display 'Error: missing GUID in definition of ',\`name,0Dh,0Ah
                                         err \\\}
                        restruc stdmethod
                        purge endi
                        irpv methods,method@vtbl \\\{
                                restore method@vtbl
                         \\\common
                                ;define methods@vtbl methods
                                define@vtbl name,methods
                                define methods@\#name methods \\\}
                        IID_\#name equ @guid
                        restore @vtbl, @guid
                        ;define@vtbl methods@vtbl
                        ;match methods,methods@vtbl \\\{ \\\}
                        end virtual \\}
 

                ;match :,tail \\{  \\}
                match =extends parent:, tail \\{ method@vtbl equ methods@\\#parent \\} \}

        struc stdmethod args& \{ define method@vtbl . \}
        macro GUID ID& \{ define @guid ID
                purge GUID \}
        define @vtbl
        virtual at 0 }

macro define@vtbl name,[method]
{
        match no, @vtbl \{
                disply 'Error: define@vtbl is used outside of interface definition.',0Dh,0Ah
                err \}
        name#.#method PTR ?
        local value
 common
        name#.com.interface = $ / sizeof.PTR

        restruc name
        struc name vtbl:? \{
                match , @struct \\{ define field@struct .,name,vtbl \\}
                match no, @struct \\{ . PTR vtbl
                        virtual at 0
 forward
                                .#method PTR ?
 common
                                .\#\\.com.object = name#.com.interface
                        end virtual \\} \}
        restruc name#Vtbl
        struc name#Vtbl value \{ \local \..base
                label \..base
 forward
                .#method PTR value
 common
                label . at \..base \}
}

macro comobject name
{
        virtual
                db `name
                load initial@struct byte from $$
                if initial@struct = '.'
                        display 'Error: name of com object should not begin with a dot.',0Dh,0Ah
                        err
                end if
        end virtual

        macro endo \{
                if $
                        display 'Error: definition of ',`name,' contains illegal instructions.',0Dh,0Ah
                        err
                end if
                match =@guid,@guid \\{
                                 display 'Error: missing GUID in definition of ',`name,0Dh,0Ah
                                 err \\}
                restruc stdmethod
                purge endo
                CLSID_#name equ @guid
                restore @vtbl, @guid
                end virtual \}

        macro CLSID ID& \{ define @guid ID
                purge CLSID \}
        define @vtbl

        virtual at 0 }     


thou could improve that part
Code:
struc stdmethod args& \{ define method@vtbl . \}    

to pass not only method name, but method count.
And make additional processing here:
Code:
irpv methods,method@vtbl \\\{
                                restore method@vtbl
                         \\\common
                                ;define methods@vtbl methods
                                define@vtbl name,methods
                                define methods@\#name methods \\\}    


by the way "macro comobject" - is intermix of struct & interface.
Post 28 May 2020, 13:54
View user's profile Send private message Send e-mail Reply with quote
CandyMan



Joined: 04 Sep 2009
Posts: 414
Location: film "CandyMan" directed through Bernard Rose OR Candy Shop
CandyMan 30 May 2020, 15:48
I've already dealt with it. Thanks to everyone for help.

_________________
smaller is better
Post 30 May 2020, 15:48
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.