flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > [fasmg] get this struc name

Author
Thread Post new topic Reply to topic
zhak



Joined: 12 Apr 2005
Posts: 501
Location: Belarus
zhak 16 Feb 2017, 19:05
Is it somehow possible, inside struc, to get its name? Actually not its name, but a label it is defined with
Code:
struc MY_STRUC
. . .
end struc

my MY_STRUC
    

`my` label is what interests me
Post 16 Feb 2017, 19:05
View user's profile Send private message Reply with quote
zhak



Joined: 12 Apr 2005
Posts: 501
Location: Belarus
zhak 16 Feb 2017, 19:12
Ah, I found this in documentation. struc should be defined with (name) special param
Post 16 Feb 2017, 19:12
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
Tomasz Grysztar 16 Feb 2017, 19:26
The special "(name)" parameter is only necessary when you need the actual text of the parameter (for example when you plan to convert it to string with "`name" or match it with some other text). If you only need to refer to the label, use a lone dot, just as it fasm 1:
Code:
label . : byte    
Post 16 Feb 2017, 19:26
View user's profile Send private message Visit poster's website Reply with quote
zhak



Joined: 12 Apr 2005
Posts: 501
Location: Belarus
zhak 16 Feb 2017, 20:04
Well, while studying the engine, I'm trying to create a struct macro with the ability to partially define elements of the structure.

For now I came up with the following solution:

Code:
macro struct? name, alignment
  macro end?.struct?!
      end namespace
      local param, value, indx, is_seq
      param equ
      value equ
      indx = 0
      is_seq = false
      irp arg, args
        match a: <x>, arg
          if is_seq = false
            param equ a
            value equ x
            indx = 0
          else
            err err_struc
          end if
        else match a: <x, arg
          if is_seq = false
            param equ a
            value equ x
            indx = 0
            is_seq = true
          else
            err err_struc
          end if
        else match a: x, arg
          if is_seq = false
            param equ a
            value equ x
            indx = 0
          else
            err err_struc
          end if
        else match x>, arg
          if is_seq = true
            is_seq = false
            indx = indx + 1
            value equ x
          else
            err err_struc
          end if
        else match x, arg
          if is_seq = true
            indx = indx + 1
            value equ x
          else
            err err_struc
          end if
        end match
        match name, param
          local length
          match type v, value
            virtual at 0
              type v
              length = $
              load value:length from 0
            end virtual
          else if value eqtype ''
            length = lengthof value
          else
            length = .name.__size
          end if
          if length <= (.name.__length / .name.__size - indx) * .name.__size
            store value:length at .name + .name.__size * indx
            indx = indx + length / .name.__size - 1
          else
            err err_struc_value_range
          end if
        end match
      end irp
    end struc
    virtual at 0
      name name
      sizeof.name = $
    end virtual
    purge end?.struct?
  end macro

    struc name args&
      match =align? x, alignment
        align x
      end match
      label .:sizeof.name
      namespace .
end macro
    

For this I added custom data types declarations:
Code:
macro dword? args:[1]&
  match [?], args
    dd ?
  else match [n], args
    dd n dup ?
  else match [n] value, args
    match =, v, value
      dd n dup ?, v
    else
      dd n dup value
    end match
  else
    dd args
  end match
  .__size = 4
  .__length = $ - .
end macro

struc dword? args&
  label .
  dword args
end struc

struc uint32? args&
  align 4
  label .
  dword args
end struc
    

(Decided not to overwrite standard data declaration types)
This allows declaring data as
Code:
a int32 ; the same as rd 1
b int32 [4] ; the same rd 4
c int32 [2] 5, 6 ; the same db 2 dup 5, 6
etc.
    

and structures like
Code:
struct MY_STRUCT
  a int32 [2]
  b int32
end struct

my x:<'1234', 2>, y:3
    

but I find accessing data size and length with __ prefixed variables not very attractive. What is more, it doesn't yet allow expressions in arguments like (3-2), so experimenting to find some more beautiful solution. Maybe something with `element` to access params in a way similar to original `sizeof data` notation but in struc's namespace only
Post 16 Feb 2017, 20:04
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
Tomasz Grysztar 16 Feb 2017, 20:15
I strongly suggest to use the label size metadata to store the size of every entity, for example:
Code:
struc dword? args& 
  label . : dword 
  dword args 
end struc    
The instruction macros that check the size of data label rely on this.
Post 16 Feb 2017, 20:15
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.