flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > C Cleaner

Author
Thread Post new topic Reply to topic
ProMiNick



Joined: 24 Mar 2012
Posts: 817
Location: Russian Federation, Sochi
ProMiNick 13 Feb 2025, 22:14
Long time ago we have to deal that ccall convention macros (x64 fastcall) produce wasteful code one instruction changes rsp register than next one changes it again.
But not anymore...
Code:
macro rsp.helper op,arg1,arg2 {
        match any `arg rest,: 'rax' 'rbx' 'rcx' 'rdx' 'rsp' 'rbp' 'rsi' 'rdi' 'r8' 'r9' 'r10' 'r11' 'r12' 'r13' 'r14' 'r15' \{ op arg1,arg2
        rept 0 \{\} rept 1 \{ local i,j
                j=(1+(('sub'-`op) shr 64) shl 1) ;sub=1,add=-1
                if defined i
                        irpv n,%%%rsp \\{ \\common
                                if %%rsp=$
                                        %%%rsp = j*(arg2)+i
                                end if
                        rept 0 \\{\\} rept 1 \\{
                                if arg2+j*(i)
                                        if j*(arg2)+i=8
                                                push    rbp
                                        else
                                                op arg1,arg2+j*(i)
                                        end if
                                end if \\}
                else
                        irpv n,%%%rsp \\{ \\common
                                if %%rsp=$
                                        %%%rsp = j*(arg2)
                                end if
                        rept 0 \\{\\} rept 1 \\{ op arg1,arg2 \\}
                end if
                %%rsp=$
                %%%rsp equ i \}
}

struc rsp args& { local i
        match any =sub =add rest=,arg,:sub . add:  args  \{
                match more],arg \\{ . rsp args
                rept 0 \\{\\} rept 1 \\{ rsp.helper .,rsp,arg \\}
        rept 0 \{\} rept 1 \{ . rsp args \} }    


here is another gift for that who still uses fasm1 not fasm2.
Code:
match any `arg rest,: 'rax' 'rbx' 'rcx' 'rdx' 'rsp' 'rbp' 'rsi' 'rdi' 'r8' 'r9' 'r10' 'r11' 'r12' 'r13' 'r14' 'r15' \{
...
\}    
is preprocessor analog of
Code:
if [:arg:] in <[:rax:],[:rbx:],[:rcx:],[:rdx:],[:rsp:],[:rbp:],[:rsi:],[:rdi:],[:r8:],[:r9:],[:r10:],[:r11:],[:r12:],[:r13:],[:r14:],[:r15:]>
...
end if    


and one more in expression could be used name of mnemonic if there are 2 variant of them
Code:
j=(1+(('sub'-`op) shr 64) shl 1) ;sub=1,add=-1    


no thanks. only critic or suggestions allowed.

_________________
I don`t like to refer by "you" to one person.
My soul requires acronim "thou" instead.
Post 13 Feb 2025, 22:14
View user's profile Send private message Send e-mail Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1062
Location: Russia
macomics 14 Feb 2025, 05:10
Any usage examples?

And
Code:
match any `arg rest,: 'rax' 'rbx' 'rcx' 'rdx' 'rsp' 'rbp' 'rsi' 'rdi' 'r8' 'r9' 'r10' 'r11' 'r12' 'r13' 'r14' 'r15' \{ op arg1,arg2    
maybe
Code:
match any `arg2 rest,: 'rax' 'rbx' 'rcx' 'rdx' 'rsp' 'rbp' 'rsi' 'rdi' 'r8' 'r9' 'r10' 'r11' 'r12' 'r13' 'r14' 'r15' \{ op arg1,arg2    
Post 14 Feb 2025, 05:10
View user's profile Send private message Reply with quote
ProMiNick



Joined: 24 Mar 2012
Posts: 817
Location: Russian Federation, Sochi
ProMiNick 14 Feb 2025, 08:28
Code:
macro rsp.helper op,arg1,arg2 {
        match any `arg2 rest,: 'rax' 'rbx' 'rcx' 'rdx' 'rsp' 'rbp' 'rsi' 'rdi' 'r8' 'r9' 'r10' 'r11' 'r12' 'r13' 'r14' 'r15' \{ op arg1,arg2
        rept 0 \{\} rept 1 \{ local i,j,k
                j=(1+(('sub'-`op) shr 64) shl 1);sub=1,add=-1
                if defined i
                        k=i
                else
                        k=0
                end if
                irpv n,%%%rsp \\{ \\common
                        if %%rsp=$
                                %%%rsp = j*(arg2)+k
                        else
                                op arg1,arg2+j*(k)
                        end if
                rept 0 \\{\\} rept 1 \\{
                        if arg2+j*(k)
                                if j*(arg2)+k=8
                                        push    rbp
                                else
                                        op arg1,arg2+j*(k)
                                end if
                        end if \\}
                %%rsp=$
                irpv n,%%%rsp { restore %%%rsp \\}
                %%%rsp equ i \}
}    
Code:
struc rsp args& { local i
        match any =sub =add rest=,arg,:sub . add:  args  \{
                match more],arg \\{ . rsp args
                rept 0 \\{\\} rept 1 \\{ rsp.helper .,rsp,arg \\}
        rept 0 \{\} rept 1 \{ . rsp args \} }    
Code:
;usecase
        %%rsp = -500
use64
        sub  rsp,2
        sub  rsp,rax ; no matter what instruction would be here - just not changing rsp with constant
        sub  rsp,-$2    
Code:
;usecase
        %%rsp = -500
use64
        sub  rsp,2
        sub  rsp,[rax] ; no matter what instruction would be here - just not changing rsp with constant
        sub  rsp,$6    
Code:
;usecase
        %%rsp = -500
use64
        sub  rsp,2
        ;sub  rsp,rax
        sub  rsp,-$2    
Code:
;usecase
        %%rsp = -500
use64
        sub  rsp,2
        ;sub  rsp,rax
        sub  rsp,$6    

real code don`t starts with sequence of stack moves so definition " %%rsp = -500" needed only for test.
And thanks for found bugs in previous realization(that was a bit raw).
Post 14 Feb 2025, 08:28
View user's profile Send private message Send e-mail Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1878
Roman 14 Feb 2025, 10:32
Very interesting.
I try this but ...

Code:
macro helper1 arg2 { 
        match `arg2 , 'eax'   \{ display `arg2 \}} 
;in code
helper1 eax ;display 'eax'

macro helper2 arg2 { 
        match `arg2 , 'eax' 'ecx'   \{  display `arg2 \}} 
;in code
helper2 ecx ;not display any. expected 'ecx'
    


Last edited by Roman on 14 Feb 2025, 11:05; edited 1 time in total
Post 14 Feb 2025, 10:32
View user's profile Send private message Reply with quote
ProMiNick



Joined: 24 Mar 2012
Posts: 817
Location: Russian Federation, Sochi
ProMiNick 14 Feb 2025, 10:40
cutoff helper needance. I guess it is final solution
Code:
struc rsp _,args& { ;separating comma from content of args give ability to "match some `args other" directly
        match any =sub =add rest,:sub . add: {
                match more],args { . rsp,args
                rept 0 {\\} rept 1 {
                        match some `args other,: 'rax' 'rbx' 'rcx' 'rdx' 'rsp' 'rbp' 'rsi' 'rdi' 'r8' 'r9' 'r10' 'r11' 'r12' 'r13' 'r14' 'r15' :{ . rsp,args
                        rept 0 {\\\} rept 1 { local i,j,k
                                j=(1+(('sub'-`.) shr 64) shl 1);sub=1,add=-1
                                if defined i
                                        k=i
                                else
                                        k=0
                                end if
                                irpv n,%%%rsp { \\\\common
                                        if %%rsp=$
                                                %%%rsp = j*(args)+k
                                        else
                                                . rsp,args+j*(k)
                                        end if
                                rept 0 {\\\\} rept 1 {
                                        if args+j*(k)
                                                if j*(args)+k=8
                                                        push    rbp
                                                else
                                                        . rsp,args+j*(k)
                                                end if
                                        end if \\\\}
                                %%rsp=$
                                irpv n,%%%rsp { restore %%%rsp \\\\}
                                %%%rsp equ i \\\} \\}
        rept 0 {\} rept 1 {
                match ,args { . rsp ; this additional match is pay for separating comma from args
                rept 0 {\\} rept 1 { . rsp,args \\} \} }    


Last edited by ProMiNick on 14 Feb 2025, 12:44; edited 1 time in total
Post 14 Feb 2025, 10:40
View user's profile Send private message Send e-mail Reply with quote
ProMiNick



Joined: 24 Mar 2012
Posts: 817
Location: Russian Federation, Sochi
ProMiNick 14 Feb 2025, 11:09
Roman wrote:
I try this but ...
Roman, Roman, where logic in thour try?
Thou forgot that both sides of match should match. Remember my variant from both sides of match sequence at right part there are ":"
and at left part there are "some" & "other" around tested fragment.
Code:
macro helper2 arg2 { 
        match any `arg2 more, :'eax' 'ecx':   \{  display `arg2 \}} 
;in code
helper2 ecx ;problem is solved? :)    

_________________
I don`t like to refer by "you" to one person.
My soul requires acronim "thou" instead.
Post 14 Feb 2025, 11:09
View user's profile Send private message Send e-mail Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1878
Roman 14 Feb 2025, 11:29
Thanks.
Work fine.
Post 14 Feb 2025, 11:29
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.