Recent

Author Topic: FPC Compiler Directives Macros... Tips?  (Read 7304 times)

trn76

  • New Member
  • *
  • Posts: 40
FPC Compiler Directives Macros... Tips?
« on: November 15, 2014, 01:12:56 am »
Hi... I was wondering after reading the FPC documentation on compiler directives, if anyone knew some good tips and tricks regarding macros?
I have some units with TONS (seriously... I doubt (no, make that know!) that I am able to actually measure the weight of bits! ..hehe ) of consts following a pattern of a macro.
...Also, defining stuff with macros could make typing errors harder to accomplish.

example:
Code: [Select]
const
  regV3AD       = $13;    // pattern here is "+1" on each const
  regV3SR       = $14;
  regFcLo       = $15;
  regFcHi       = $16;

  kCtrTest      = $08;    // pattern here is "shr" on each const
  kCtrRing      = $04;
  kCtrSync      = $02;
  kCtrGate      = $01;

  // I would love to be able to do something like:

  regV3AD       = {$MACRO PATTERN1};
  regV3SR       = {$MACRO PATTERN1};
  regFcLo       = {$MACRO PATTERN1};
  regFcHi       = {$MACRO PATTERN1};
 
  kCtrTest      = {$MACRO PATTERN2};
  kCtrRing      = {$MACRO PATTERN2};
  kCtrSync      = {$MACRO PATTERN2};
  kCtrGate      = {$MACRO PATTERN2};

  // yeah, you get the point... no, I won't use enums here! ;)


If anyone also have some other good tips on macros, I would love to know...

Thanks

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11951
  • FPC developer.
Re: FPC Compiler Directives Macros... Tips?
« Reply #1 on: November 15, 2014, 03:02:53 pm »
Macros are not parametrisable in FreePascal and related dialects. So if you want that you need to use an external preprocessor like M4.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8777
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: FPC Compiler Directives Macros... Tips?
« Reply #2 on: November 15, 2014, 05:43:38 pm »
...Also, defining stuff with macros could make typing errors harder to accomplish.
And reading errors easier to accomplish. Who would know the value of, say, regFcHi? What if you accidentally change the order someday? Or insert something else in the middle? This is one of many known abuse of macros, which is why FPC doesn't support macros as deep as languages without proper modular compilation support.
Macros are not parametrisable in FreePascal and related dialects. So if you want that you need to use an external preprocessor like M4.
I don't think he asked for parameterised macros. It's more like a macro with a state, so after each invocation of the macro, it's state is modified. As he has stated, the first macro change its state by +1-ing its current value, while the second one shr-ing it. Initial value? No idea.

trn76

  • New Member
  • *
  • Posts: 40
Re: FPC Compiler Directives Macros... Tips?
« Reply #3 on: November 15, 2014, 09:35:28 pm »
Thanks both of you for the replies, atleast I learned a bit more on how macros work in FPC.

I don't think he asked for parameterised macros. It's more like a macro with a state, so after each invocation of the macro, it's state is modified. As he has stated, the first macro change its state by +1-ing its current value, while the second one shr-ing it. Initial value? No idea.
Yeah, I was thinking of a state macro with an initialized macro variable, but FPC does not have macro variables as I just learned.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8777
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: FPC Compiler Directives Macros... Tips?
« Reply #4 on: November 16, 2014, 06:35:29 pm »
Thanks both of you for the replies, atleast I learned a bit more on how macros work in FPC.

I don't think he asked for parameterised macros. It's more like a macro with a state, so after each invocation of the macro, it's state is modified. As he has stated, the first macro change its state by +1-ing its current value, while the second one shr-ing it. Initial value? No idea.
Yeah, I was thinking of a state macro with an initialized macro variable, but FPC does not have macro variables as I just learned.
Yes, FPC's preprocessor (I don't think this is the right term, since we don't really have true preprocessor that runs before compilation) is just not designed to handle such a complex task. Not even the overly sophisticated C's preprocessor can handle it, I guess.

avra

  • Hero Member
  • *****
  • Posts: 2532
    • Additional info
Re: FPC Compiler Directives Macros... Tips?
« Reply #5 on: November 17, 2014, 10:13:43 am »
You can make a little code generator to automate this, either native or as a pascal script application.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11951
  • FPC developer.
Re: FPC Compiler Directives Macros... Tips?
« Reply #6 on: November 17, 2014, 10:44:36 am »
Yes, FPC's preprocessor (I don't think this is the right term, since we don't really have true preprocessor that runs before compilation)

(It is. It works on "text" level, not on tree level, which is what the "pre" in preprocessor is, before the compilation step. Doesn't mean the intermediate result must be stored or that the preprocessor must be a different process)


Leledumbo

  • Hero Member
  • *****
  • Posts: 8777
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: FPC Compiler Directives Macros... Tips?
« Reply #7 on: November 17, 2014, 10:58:49 am »
Yes, FPC's preprocessor (I don't think this is the right term, since we don't really have true preprocessor that runs before compilation)

(It is. It works on "text" level, not on tree level, which is what the "pre" in preprocessor is, before the compilation step. Doesn't mean the intermediate result must be stored or that the preprocessor must be a different process)
Mmm...I always thought that "pre" means it happens before lexer-parser works, so it should be a separate process, but since it's interleaved along with lexer and parser, I don't consider it a preprocessor. Just a different interpretation ::)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11951
  • FPC developer.
Re: FPC Compiler Directives Macros... Tips?
« Reply #8 on: November 17, 2014, 11:10:58 am »
Yes, FPC's preprocessor (I don't think this is the right term, since we don't really have true preprocessor that runs before compilation)

(It is. It works on "text" level, not on tree level, which is what the "pre" in preprocessor is, before the compilation step. Doesn't mean the intermediate result must be stored or that the preprocessor must be a different process)
Mmm...I always thought that "pre" means it happens before lexer-parser works

That is correct. Basically compilation in my remark is lexer-parse-semantic-codegen-optimization-results writeout.

Before the lexer it is still text.

Quote
so it should be a separate process,

I meant process on OS level, as in separate EXE.

Quote
but since it's interleaved along with lexer and parser, I don't consider it a preprocessor.

Just a different interpretation ::)

I consider the integration an optimization. Since we can't have an external preprocessor the way C has due to the module system, it is a sane one.

So that leaves manual preprocessing, which specially in cases like this isn't so bad, since you can also check in the preprocessed result that one time you modify the header. It probably won't be the case that it changes so often that automatic preprocessing via the compiler is worthwhile.

trn76

  • New Member
  • *
  • Posts: 40
Re: FPC Compiler Directives Macros... Tips?
« Reply #9 on: November 17, 2014, 11:13:14 am »
You can make a little code generator to automate this, either native or as a pascal script application.
You where thinking offline? How would I do this, and with what...?
I'm thinking it would be cool, but too much hassel - it should have been integrated in Lazarus or else it is easy to mess up.

avra

  • Hero Member
  • *****
  • Posts: 2532
    • Additional info
Re: FPC Compiler Directives Macros... Tips?
« Reply #10 on: November 22, 2014, 01:06:20 pm »
You can make a little code generator to automate this, either native or as a pascal script application.
You where thinking offline? How would I do this, and with what...?
It's up to you. You know what are you most familiar with. If it were me I would try it with unit templates which would be parsed and replaced with real content suited to my needs. For templates I would probably try mustache templates from mORMot framework, and for application I would probably make pascal script enabled application that would read my macro files and put them into mustache templates. It would be worth it only if you really have tons of cases like this as you claim.
Quote
I'm thinking it would be cool, but too much hassel - it should have been integrated in Lazarus or else it is easy to mess up.
Do you know of anyone else who has exact needs as you do? This is too specific so I would not count on it. If you need it you will need to implement it your self. Lazarus is open source, remember?
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

 

TinyPortal © 2005-2018