flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > including files

Author
Thread Post new topic Reply to topic
Ariethenor



Joined: 21 Jan 2012
Posts: 4
Ariethenor
I am trying to figure out how to write a macro to allow multiple files to "include same file" but only one will get processed by the preprocessor.

eg.

file1.inc ; modular library
include file3.inc ; depends on this file

file2.inc ; modular library
include file3.inc ; depends on this file

prog1.asm ; program being worked on
include file1.inc
include file2.inc

I can't do this normally because file3 gets included twice and all the labels are created twice, creating an assembler warning. I need a macro that I can include at the beginning of every include file so that if it is used in multiple files it gets included once and once only. Trying to simulate c++ header files, for building modular libraries.

Thanks a million.

have tried

if ~ defined file3 | defined _file3
file3:
_file3 = 1

(contents of include file here)

end if

It still includes the file twice.

_________________
The more you know, the more you know that you do not know. Therefore, ignorance is bliss!
Post 21 Jan 2012, 11:00
View user's profile Send private message Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 619
cod3b453
There is a difference between using "_file3 = 1" and "_file3 equ 1"; defined only works on the last one, so if you use that it should work.
Post 21 Jan 2012, 11:35
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3500
Location: Bulgaria
JohnFound
It is not so easy task. Here is my attempt to do this.
Compile the file "MainUnit.asm"

[EDIT]Sorry, but the example is not working with the current versions of FASM. Sad I will remove the attachment.[/EDIT]
Post 21 Jan 2012, 11:56
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
Ariethenor



Joined: 21 Jan 2012
Posts: 4
Ariethenor
cod3b453:
I understand what you are saying, but it unfortunately is not the reason. I understand the reason my code will not work, it is because it tests for initial define, and my dummy define "_file3=1" if it gets processed once it will define the dummy and will always get defined again.
This is because of the OR " | " test. I was wondering if there was some way to construct some kind of XOR test if it would work, but I can't figure how to properly XOR. This is the infinite loop problem discussed in the user manual.

JohnFound:
Does your code work, or is it just another avenue of thinking to help solve the problem?
Post 21 Jan 2012, 12:18
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3500
Location: Bulgaria
JohnFound
The code (I removed) was created with older version of FASM and uses the old behavior of "fix" directive. In the latest versions these tricks does not work anymore.
Though, the idea is valid - create the list with unique names of the modules and then include them at once. The macro "uses" just adds the module name to the list if it is not already there.


Last edited by JohnFound on 21 Jan 2012, 12:56; edited 1 time in total
Post 21 Jan 2012, 12:23
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
Ariethenor



Joined: 21 Jan 2012
Posts: 4
Ariethenor
To get around this problem for now, I am kind of doing that manually, I comment all the dependent files in the modules, and for each prog1.asm i have a prog1.inc that i input all the include files manually so that all gets the required includes once and only once. However it is a pain in the arse, and it would be nice to be able to do it auto"magically". There has to be a way, but it seams that because of the way the assembler preprocesses, it may actually be impossible, because it will always treat the first include file reached on the first pass as a second or third time etc. on the second pass and therefore will not redefine it etc, thus the infinite loop.
Post 21 Jan 2012, 12:32
View user's profile Send private message Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 619
cod3b453
This is my test code X includes A,B which both include C; C is only assembled once.


Description:
Download
Filename: eg.zip
Filesize: 543 Bytes
Downloaded: 160 Time(s)

Post 21 Jan 2012, 13:04
View user's profile Send private message Reply with quote
Ariethenor



Joined: 21 Jan 2012
Posts: 4
Ariethenor
Solution:

All include files must be like this:

if ~ defined FILE3_INC | defined @f ; can use any name unique to the included file
FILE3_INC: ; must be same as above
@@:

(source here)

end if

this works on FASM ver: 1.69.35
Post 21 Jan 2012, 13:37
View user's profile Send private message Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7496
Location: Kraków, Poland
Tomasz Grysztar
Similar question was asked already on the board a few times, there was "includeonce" macro provided in the answer:
http://board.flatassembler.net/topic.php?t=4587
http://board.flatassembler.net/topic.php?p=106846#106846 (revolution's improved version that allows nesting)

EDIT: I found out that this macro is also provided in this nice post about "Principles for creating multi-developer programs with fasm": http://board.flatassembler.net/topic.php?t=8309
Post 23 Jan 2012, 11:52
View user's profile Send private message Visit poster's website Reply with quote
uart777



Joined: 17 Jan 2012
Posts: 369
uart777
[ Post removed by author. ]


Last edited by uart777 on 17 Nov 2013, 00:25; edited 1 time in total
Post 23 Jan 2012, 17:03
View user's profile Send private message Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc
[include duplication Smile]


Last edited by l_inc on 23 Jan 2012, 18:38; edited 1 time in total
Post 23 Jan 2012, 18:32
View user's profile Send private message Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc
Tomasz Grysztar wrote:
Similar question was asked already on the board a few times, there was "includeonce" macro provided in the answer

This is not a very comprehensive solution, because it misses situations when an include file is included in different ways which is actually a common situation even if all include-strings are relative and lowercased. I'd appreciate a fasm feature allowing to resolve such situations.

Anyway disregarding this problem I'd still prefer to overload the original include directive rather than using a differently named macro, because it allows to keep track on including standard (and also other) headers which use the include directive directly:

Code:
;Prevents includes duplication
include fix include_directive_modified
include_directive_original fix include
included_files_list equ 'includecheck.inc'
display "'",'includecheck.inc',"'",' included',13,10
include_file_output_indentation equ ''
macro def_include_directive_modified
{
    macro include_directive_modified arg*
       \{
            define matched -
            match leader arg trailer,'' included_files_list ''
              \\{
                  restore matched
                     define matched +
                    display 'Warning: ',"'",arg,"'",' duplication',13,10
          \\}
          match -,matched
             \\{
                  display include_file_output_indentation,"'",arg,"'",' included',13,10
                       tmp equ included_files_list
                         restore included_files_list
                         included_files_list equ tmp arg
                     restore tmp
                 def_include_directive_modified
                      include_file_output_indentation equ include_file_output_indentation,'   '
                         include_directive_original arg
                      restore include_file_output_indentation
                     purge include_directive_modified
            \\}
          restore matched
     \}
}
def_include_directive_modified
    

As a bonus you also have an include-tree displayed:
Code:
include 'includecheck.inc'
include 'includecheck.inc'
include 'win32a.inc'
include 'general.inc'
include 'macro/resource.inc'
include 'hexencode.inc'
...
    

produces
Code:
'includecheck.inc' included
Warning: 'includecheck.inc' duplication
'win32a.inc' included
   'macro/struct.inc' included
   'macro/proc32.inc' included
   'macro/com32.inc' included
   'macro/import32.inc' included
   'macro/export.inc' included
   'macro/resource.inc' included
   'equates/kernel32.inc' included
   'equates/user32.inc' included
   'equates/gdi32.inc' included
   'equates/comctl32.inc' included
   'equates/comdlg32.inc' included
   'equates/shell32.inc' included
   'equates/wsock32.inc' included
'general.inc' included
Warning: 'macro/resource.inc' duplication
'hexencode.inc' included    
Post 23 Jan 2012, 18:38
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-2019, Tomasz Grysztar.

Powered by rwasa.