flat assembler
Message board for the users of flat assembler.

Index > Main > proc/public and name mangling

Author
Thread Post new topic Reply to topic
f0dder



Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
Is there currently any way to have fasm do name mangling automatically?

Ie, I have a "dstr_memcpy" routine defined like
Quote:

proc dstr_memcpy stdcall uses ESI EDI, dst:DWORD, src:DWORD, len:DWORD


Following the standard mangling tradition of win32 tools, the exported symbol should be "_dstr_memcpy@12" - but it's tedious to write this naming by hand, not to mention error-prone if the function signature changes.

I assume it could be done by modifying the 'proc' macro, and perhaps 'public' as well... any macro wizards who need something to play with? Smile
Post 17 Sep 2005, 23:59
View user's profile Send private message Visit poster's website Reply with quote
Vasilev Vjacheslav



Joined: 11 Aug 2004
Posts: 392
Vasilev Vjacheslav
why just not to use 'dstr_memcpy equ _dstr_memcpy' and define proc in this way 'proc dstr_memcpy stdcall uses ESI EDI, dst:DWORD, src:DWORD, len:DWORD'? or maybe i wrong?
Post 19 Sep 2005, 15:55
View user's profile Send private message Reply with quote
Remy Vincent



Joined: 16 Sep 2005
Posts: 155
Location: France
Remy Vincent
If you don't load the registers yourself, and if you let stdcall macro load the right registers, a lot of your neurones will work hard, only to remind which registers are automaticly used... ????Why are you lowering your brain all alone, so much, so fast, ... Embarassed Embarassed Embarassed Embarassed Embarassed
Idea Arrow Embarassed Embarassed Embarassed


proc dstr_memcpy stdcall dst:DWORD, src:DWORD, len:DWORD


or


mov EDI , ...
mov ESI , ...
mov ECX , ...
CALL dstr_memcpy
...
...

dstr_memcpy:
lodsb
stosb
...
ret

_________________
Groups lower your IQ
Post 19 Sep 2005, 17:32
View user's profile Send private message Visit poster's website Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
Vasilev, I guess "equ" or "fix" or something might work, but it's still annoying having to do "dummy" work by hand Smile
Post 19 Sep 2005, 19:50
View user's profile Send private message Visit poster's website Reply with quote
Eoin



Joined: 16 Jun 2003
Posts: 68
Location: Ireland
Eoin
F0dder, the best way to do it would probably be a modification to the proc macro. I tried it there, but can only get FASM to output the number of parameter bytes required as an 8 digit hex value.

If someone knows how to output decimal digits then the rest would be easy...
Post 19 Sep 2005, 21:15
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
Eoín, that's where I thought "attacking" the problem would be easiest, too, but the proc macros are a bit more of a mouthful than I care to play with right now - got other projects as well Smile

Thanks for looking into it!
Post 19 Sep 2005, 21:19
View user's profile Send private message Visit poster's website Reply with quote
rea



Joined: 14 Nov 2004
Posts: 92
rea
What about for the moment write a little parser that do what you whant??? and include the generated file somewhere in the file; for example: main.asm, main.sig and include in main.asn main.sig.

If it is posible get a number in decimal for example 4*3 = 12 instead of C (like I guess is what say Eoin), should be fair easy modify proc for concatenate if necesary 8at request???) "_" + "name_argument" + "@" + arg_c*4 or any that you whant...
Post 20 Sep 2005, 03:12
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7750
Location: Kraków, Poland
Tomasz Grysztar
Eoin, how did you do it that you were able to get hex digits, but not decimal ones??
Nonetheless, it cannot be done at the assembly stage for this case, because the names for procedures must be completed during the preprocessing stage. The problem of getting the count of arguments as a number during the preprocessing was discussed here: http://board.flatassembler.net/topic.php?t=3980

Here's the modified define@proc macro that uses this trick to obtain the "parmbytes" as a decimal number during the preprocessing. It doesn't work for the parameters of types larger that 4 bytes, though - for this to work the counting would have to be done inside the defargs@proc macro, with checking of the type of parameter to choose the values of increment for the "parmbytes".
Code:
macro define@proc name,statement
 { local params,flag,regs,parmbytes,localbytes,current
   name:
   match =stdcall args, statement \{ params equ args
                                     flag = 11b \}
   match =stdcall, statement \{ params equ
                                flag = 11b \}
   match =c args, statement \{ params equ args
                               flag = 10001b \}
   match =c, statement \{ params equ
                          flag = 10001b \}
   match =params, params \{ params equ statement
                            flag = 0 \}
   parmbytes equ 0
   virtual at ebp+8
   match =uses reglist=,args, params \{ regs equ reglist
                                        params equ args \}
   match =regs =uses reglist, regs params \{ regs equ reglist
                                             params equ \}
   match =regs, regs \{ regs equ \}
   match =,args, params \{ defargs@proc args
                           irp arg, args \\{ match current,parmbytes \\\{ rept 5 i:current \\\\{ parmbytes equ i \\\\} \\\} \\} \}
   match =args@proc args, args@proc params \{ defargs@proc args
                                              irp arg, args \\{ match current,parmbytes \\\{ rept 5 i:current \\\\{ parmbytes equ i \\\\} \\\} \\} \}
   end virtual
   name # % = parmbytes/4
   all@vars equ
   current = 0
   match prologue:parmbytes:reglist, prologue@proc:parmbytes:<regs> \{ prologue name,flag,parmbytes,localbytes,reglist \}
   macro locals
   \{ virtual at ebp-localbytes+current
      macro label . \\{ deflocal@proc .,:, \\}
      struc db [val] \\{ \common deflocal@proc .,db,val \\}
      struc dw [val] \\{ \common deflocal@proc .,dw,val \\}
      struc dp [val] \\{ \common deflocal@proc .,dp,val \\}
      struc dd [val] \\{ \common deflocal@proc .,dd,val \\}
      struc dt [val] \\{ \common deflocal@proc .,dt,val \\}
      struc dq [val] \\{ \common deflocal@proc .,dq,val \\}
      struc rb cnt \\{ deflocal@proc .,rb cnt, \\}
      struc rw cnt \\{ deflocal@proc .,rw cnt, \\}
      struc rp cnt \\{ deflocal@proc .,rp cnt, \\}
      struc rd cnt \\{ deflocal@proc .,rd cnt, \\}
      struc rt cnt \\{ deflocal@proc .,rt cnt, \\}
      struc rq cnt \\{ deflocal@proc .,rq cnt, \\} \}
   macro endl
   \{ purge label
      restruc db,dw,dp,dd,dt,dq
      restruc rb,rw,rp,rd,rt,rq
      restruc byte,word,dword,pword,tword,qword
      current = $-(ebp-localbytes)
      end virtual \}
   macro ret operand
   \{ match any, operand \\{ retn operand \\}
      match , operand \\{ match epilogue:parmbytes:reglist, epilogue@proc:parmbytes:<regs>
                          \\\{ epilogue name,flag,parmbytes,localbytes,reglist \\\} \\} \}
   macro finish@proc \{ localbytes = (((current-1) shr 2)+1) shl 2 \} }    

(I've also removed the "if used" checking in the above, since for "public" procedures this doesn't make sense at all).

And now it's only a matter of customizing the prologue to do the name mangling:
Code:
macro prologue_namemangling procname,flag,parmbytes,localbytes,reglist
 { if flag and 10b ; do mangling only for stdcall
    public procname as '__'#`procname#'@'#`parmbytes
   else
    public procname
   end if
   if parmbytes | localbytes
    push ebp
    mov ebp,esp
    if localbytes
     sub esp,localbytes
    end if
   end if
   irps reg, reglist \{ push reg \} }    
Post 20 Sep 2005, 08:13
View user's profile Send private message Visit poster's website Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
Thanks, Tomasz - I'll have a look at it once I'm done messing with this windows unattended setup Smile
Post 20 Sep 2005, 08:51
View user's profile Send private message Visit poster's website Reply with quote
Eoin



Joined: 16 Jun 2003
Posts: 68
Location: Ireland
Eoin
In fact what I was seeing was something labelname@parmbytes?00065C and mistook it as being the value of parmbytes in hex, instead I guess now its something to do with parmbytes being local to the macro.

My mistake Smile .
Post 20 Sep 2005, 12:42
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-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.