flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > fasm1 struct - make struct relative variable base

Author
Thread Post new topic Reply to topic
ProMiNick



Joined: 24 Mar 2012
Posts: 677
Location: Russian Federation, Sochi
ProMiNick
What is changed from classical struct?:
added determination of field - is it of base type, or it is structure (fields@RECT is defined, but fields@db is not)
processing of base type changed - cuted off label creation, instead of that fields defined as preprocessor symbols related to structure base, and structure base itself be possibly related to variable base
all labels created for calculation size of assigned fields became local to macro

because all structure (its base and whole tree of fields) now is set of preprocessor symbols they have no limitation that was for labels - they could be defined via another preprocessor symbol that is variable itself.
Code:
; Macroinstructions for defining data structures

macro struct name
 { virtual at 0
   define @struct
   field@struct equ name
   match child parent, name \{ restore field@struct
                               field@struct equ child,fields@\#parent \}
   sub@struct equ
   struc db [val] \{ \common define field@struct .,db,<val> \}
   struc dw [val] \{ \common define field@struct .,dw,<val> \}
   struc du [val] \{ \common define field@struct .,du,<val> \}
   struc dd [val] \{ \common define field@struct .,dd,<val> \}
   struc dp [val] \{ \common define field@struct .,dp,<val> \}
   struc dq [val] \{ \common define field@struct .,dq,<val> \}
   struc dt [val] \{ \common define field@struct .,dt,<val> \}
   struc rb count \{ define field@struct .,db,count dup (?) \}
   struc rw count \{ define field@struct .,dw,count dup (?) \}
   struc rd count \{ define field@struct .,dd,count dup (?) \}
   struc rp count \{ define field@struct .,dp,count dup (?) \}
   struc rq count \{ define field@struct .,dq,count dup (?) \}
   struc rt count \{ define field@struct .,dt,count dup (?) \}
   macro db [val] \{ \common \local anonymous
                     define field@struct anonymous,db,<val> \}
   macro dw [val] \{ \common \local anonymous
                     define field@struct anonymous,dw,<val> \}
   macro du [val] \{ \common \local anonymous
                     define field@struct anonymous,du,<val> \}
   macro dd [val] \{ \common \local anonymous
                     define field@struct anonymous,dd,<val> \}
   macro dp [val] \{ \common \local anonymous
                     define field@struct anonymous,dp,<val> \}
   macro dq [val] \{ \common \local anonymous
                     define field@struct anonymous,dq,<val> \}
   macro dt [val] \{ \common \local anonymous
                     define field@struct anonymous,dt,<val> \}
   macro rb count \{ \local anonymous
                     define field@struct anonymous,db,count dup (?) \}
   macro rw count \{ \local anonymous
                     define field@struct anonymous,dw,count dup (?) \}
   macro rd count \{ \local anonymous
                     define field@struct anonymous,dd,count dup (?) \}
   macro rp count \{ \local anonymous
                     define field@struct anonymous,dp,count dup (?) \}
   macro rq count \{ \local anonymous
                     define field@struct anonymous,dq,count dup (?) \}
   macro rt count \{ \local anonymous
                     define field@struct anonymous,dt,count dup (?) \}
   macro align    \{ \local anonymous
                     define field@struct anonymous,align, \}
   macro union \{ field@struct equ ,union,<
                  sub@struct equ union \}
   macro struct \{ field@struct equ ,substruct,<
                  sub@struct equ substruct \} }

macro ends
 { match , sub@struct \{ restruc db,dw,du,dd,dp,dq,dt
                         restruc rb,rw,rd,rp,rq,rt
                         purge db,dw,du,dd,dp,dq,dt
                         purge rb,rw,rd,rp,rq,rt
                         purge union,struct,align
                         irpv fields,field@struct \\{ restore field@struct
                                                      \\common define fields@struct fields \\}
                         match name tail,fields@struct, \\{ if $
                                                            display 'Error: definition of ',\\`name,' contains illegal instructions.',0Dh,0Ah
                                                            err
                                                            end if \\}
                         match name=,fields,fields@struct \\{ restore @struct
                                                              make@struct name,fields
                                                              define fields@\\#name fields \\}
                         end virtual \}
   match any, sub@struct \{ tmp@struct equ field@struct
                            restore field@struct
                            field@struct equ tmp@struct> \}
   restore sub@struct }

macro make@struct name,[field,type,def]
 { common
    local define
    define equ name
   forward
    local sub
    match , field \{ make@substruct type,name,sub def
                     define equ define,.,sub, \}
    match any, field \{ define equ define,.#field,type,<def> \}
   common
    match fields, define \{ define@struct fields \} }

macro define@struct name,[field,type,def]
 { common
    virtual
    db `name
    load initial@struct byte from 0
    if initial@struct = '.'
    display 'Error: name of structure should not begin with a dot.',0Dh,0Ah
    err
    end if
    end virtual
    local list
    list equ
   forward
    if ~ field eq .
     name#field type def
     sizeof.#name#field = $ - name#field
    else
     label name#.#type
     rb sizeof.#type
    end if
    local value,..field
    match any, list \{ list equ list, \}
    list equ list <value>
   common
    sizeof.#name = $
    restruc name
    match values, list \{
    struc name value \\{ \\local \\..base
     match   , @struct \\\{ define field@struct .,name,<values> \\\}
     match no, @struct \\\{ label \\..base
      match    , @varbase \\\\{ define . \\..base \\\\}
      match any, @varbase \\\\{ define . @varbase+\\..base \\\\}
   forward
      ..field=$
      match complex structure::   ,fields@#type::value \\\\{ field type def \\\\}
      match complex structure::any,fields@#type::value \\\\{ field type value \\\\}
      match     =fields@#type     ,fields@#type        \\\\{  define field .+name#field \\\\}
      match     =fields@#type::   ,fields@#type::value \\\\{  type def \\\\}
      match     =fields@#type::any,fields@#type::value \\\\{  type value \\\\}
      match                    any,              value \\\\{
        if ~ field eq .
          rb sizeof.#name#field - ($-..field)
        end if  \\\\} \\\} \\}
    macro name value \\{
     match   , @struct \\\{ \\\local anonymous
                          define field@struct anonymous,name,<values> \\\}
     match no, @struct \\\{
   forward
      match , value \\\\{ type def \\\\}
      match any, value \\\\{
                           ..field = $
                           type value
                           if ~ field eq .
                            rb sizeof.#name#field - ($-..field)
                           end if \\\\}
   common \\\} \\} \} }

macro enable@substruct
 { macro make@substruct substruct,parent,name,[field,type,def]
    \{ \common
        \local define
        define equ parent,name
       \forward
        \local sub
        match , field \\{ match any, type \\\{ enable@substruct
                                               make@substruct type,parent,sub def
                                               purge make@substruct
                                               define equ define,.,sub, \\\} \\}
        match any, field \\{ define equ define,.\#field,type,<def> \\}
       \common
        match fields, define \\{ define@\#substruct fields \\} \} }

enable@substruct

macro define@union parent,name,[field,type,def]
 { common
    virtual at parent#.#name
   forward
    if ~ field eq .
     virtual at parent#.#name
      parent#field type def
      sizeof.#parent#field = $ - parent#field
     end virtual
     if sizeof.#parent#field > $ - parent#.#name
      rb sizeof.#parent#field - ($ - parent#.#name)
     end if
    else
     virtual at parent#.#name
      label parent#.#type
      type def
     end virtual
     label name#.#type at parent#.#name
     if sizeof.#type > $ - parent#.#name
      rb sizeof.#type - ($ - parent#.#name)
     end if
    end if
   common
    sizeof.#name = $ - parent#.#name
    end virtual
    struc name [value] \{ \common \local \..base
      label \..base
      match , @varbase \\{ define .\#name \..base \\}
      match any, @varbase \\{ define .\#name @varbase+\..base \\}
    last@union equ
   forward
    match     =fields@#type          ,fields@#type                    \\{ define field .+parent#field \\}
    match                    any     ,              last@union        \\{ virtual at \..base \\}
    match complex structure::   ::any,fields@#type::last@union::value \\{ field type value \\}
    match complex structure::any::   ,fields@#type::last@union::      \\{ field type def \\}
    match complex structure::   ::   ,fields@#type::last@union::value \\{ field type def \\}
    match                    any     ,              last@union        \\{ end virtual \\}
    last@union equ field
   common rb sizeof.#name - ($ - \..base) \}
    macro name [value] \{ \common \local ..anonymous
                          ..anonymous name value \} }

macro define@substruct parent,name,[field,type,def]
 { common
    virtual at parent#.#name
   forward
    local value,..field
    if ~ field eq .
     parent#field type def
     sizeof.#parent#field = $ - parent#field
    else
     label parent#.#type
     rb sizeof.#type
    end if
   common
    sizeof.#name = $ - parent#.#name
    end virtual
    struc name value \{ \local \..base
      label \..base
      match , @varbase \\{ define .\#name \..base \\}
      match any, @varbase \\{ define .\#name @varbase+\..base \\}
   forward
      ..field=$
      match complex structure::   ,fields@#type::value \\{ field type def \\}
      match complex structure::any,fields@#type::value \\{ field type value \\}
      match     =fields@#type     ,fields@#type        \\{ define field .+parent#field \\}
      match     =fields@#type::   ,fields@#type::value \\{ type def \\}
      match     =fields@#type::any,fields@#type::value \\{ type value \\}
      match                    any,              value \\{
        if ~ field eq .
          rb sizeof.#parent#field - ($-..field)
        end if \\}
   common \}
    macro name value \{ \local ..anonymous
                        ..anonymous name \} }
    

_________________
I don`t like to refer by "you" to one person.
My soul requires acronim "thou" instead.
Post 23 Jun 2022, 09:45
View user's profile Send private message Send e-mail 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.