flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > [fasmg] Symbol is undefined or out of scope problem

Author
Thread Post new topic Reply to topic
zhak



Joined: 12 Apr 2005
Posts: 501
Location: Belarus
zhak 03 Apr 2017, 11:44
Tomasz, please take a look at the small template below
Code:
macro datatypes types*&
  irp <n, type>, types
    macro type args:[?]&
      match [?], args
        emit n: ?
      else match [x], args
        emit n: x dup ?
      else match [x] value, args
        match =, v, value
          emit n: x dup ?, v
        else
          emit n: x dup value
        end match
      else
        irp arg, args
          if arg eqtype ''
            emit n: arg
          else
            local value
            value = +arg
            emit n: value
          end if
        end irp
      end match
      type.__size = n
      datatype.type equ ::type::
    end macro
    struc (name) type args&
      label .: n
      type args
      name.__size = n
      name.__length = $ - .
    end struc
  end irp
end macro

datatypes 1,byte?, 2,word?, 4,dword?, 6,fword?, 8,qword?, 10,tbyte?, 16,oword?

macro log.number num
  local n, s
  n = +num
  if n = 0
    s = '0'
  else
    s = 0
    while n > 0
      s = s shl 8 + (n mod 10 + '0')
      n = n / 10
    end while
  end if
  display string s,13,10
end macro

;------------ ^^ just helpers above to compile --------------

macro struct? structname
  macro end?.struct?!
      end namespace
      irp arg, args
        match p:v, arg
          log.number .p.__size
        end match
      end irp
    end struc
    virtual at 0
      structname structname
      structname.__size = $
    end virtual
    purge end?.struct?
  end macro
    struc (name) structname args&
      label .: structname.__size
      namespace .
      name.__size = structname.__size ;<<<<<<< problem line
end macro

struct POINT
  x word
  y byte
end struct

struct POINT2
  z POINT
end struct

my POINT2 z.x:1, z:1

    


I'm trying to get the size of a variable defined as structure. Variable, not structure itself, because later it will be an array of structures.

The problem is when I add
Code:
name.__size = structname.__size
    

I start getting the following error:
Quote:

Processed: label .: POINT.__size
Error: symbol '__size' is undefined or out of scope.

However, both sizes are logged correctly. If line is removed, label .: POINT.__size doesn't generate the error any more.

I thought that it could be processing order, and structname.__size is not initialized yet, when name.__size appears in code, but I guess it would then fail on that very line. Thanks in advance for your assistance!
Post 03 Apr 2017, 11:44
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8356
Location: Kraków, Poland
Tomasz Grysztar 03 Apr 2017, 12:44
The problem is caused by the case when both "name" and "structname" are equal to "POINT", because then
Code:
name.__size = structname.__size    
gets evaluated to:
Code:
POINT.__size = POINT.__size    
Because you then have "POINT.__size" defined with the actual value, this symbols becomes variable and can no longer be forward-referenced. That's why you get the "out of scope" error when trying to access the symbol before the first definition happens.

You can see that this is the problem when you put additional condition around the problematic line:
Code:
if `name <> `structname
      name.__size = structname.__size
end if    

I would suggest to use ":=" instead of "=" to make sure that symbols can always be forward-referenced, but you still have to ensure that each of them is defined exactly once.
Post 03 Apr 2017, 12:44
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.