flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Reordering structure [fasmg]

Author
Thread Post new topic Reply to topic
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7797
Location: Kraków, Poland
Tomasz Grysztar
A tweet I saw today inspired me to play a little with fasmg's macros again and make a variant of its basic "struct" macro that can be used to define structures where the order of fields is not important and the assembler is then free to reorder them for a better alignment, for example:
Code:
macro reordstruct? name*
        local A
        A = 1
        struc (fieldname)? def
                define name..field fieldname
                define name..fieldname def
                if sizeof name.fieldname > 0 & bsf sizeof name.fieldname >= bsf A
                        A = 1 shl (bsf sizeof name.fieldname)
                end if
        end struc
        macro ends?
                restruc ?
                purge ends?
                macro name.builder
                        struc name args@struct&
                                label . : sizeof.name
                end macro
                local F
                while A
                        irpv fieldname, name..field
                                F = sizeof name.fieldname
                                if F = 0
                                        F = 1
                                end if
                                if F and A & F and (A-1) = 0
                                        match def, name..fieldname
                                                macro name.builder
                                                        name.builder
                                                        .fieldname def
                                                end macro
                                        end match
                                end if
                        end irpv
                        A = A shr 1
                end while
                name.builder
                        iterate definition, args@struct
                                match name:value, definition
                                        store value at .name
                                else match name==value, definition
                                        store value at .name
                                else match value, definition
                                        err 'unnamed values not supported'
                                end match
                        end iterate
                end struc
                virtual at 0
                        name name
                        sizeof.name = $
                end virtual
        end macro
end macro    
This variant reorders structure so that less-aligned fields are at the end (so if the entire structure itself is put on a aligned address, the alignment is preserved for all the fields).

For example this:
Code:
reordstruct tester
        a db 1
        b dd 4
        c dw 2
        d dq 8
        e dd 4
ends    
ends up defining structure equivalent to:
Code:
struct tester
        d dq 8
        b dd 4
        e dd 4
        c dw 2
        a db 1
ends    


The macro is just an experiment and perhaps does some things suboptimally. But it was an interesting idea to play with.
Post 02 Aug 2019, 08:38
View user's profile Send private message Visit poster's website 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-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.