Recent

Author Topic: const declaration  (Read 5702 times)

freemind001

  • Jr. Member
  • **
  • Posts: 51
Re: const declaration
« Reply #45 on: January 16, 2025, 02:00:20 pm »
I do not argue with you, I have understood that it is impossible after very few first posts, so I have just hardcoded the constants needed
Code: Pascal  [Select][+][-]
  1. const
  2.   UI_SET_EVBIT = $40045564;
  3.   UI_SET_KEYBIT = $40045565;
  4.   UI_DEV_CREATE = $00005501;
  5.   UI_DEV_DESTROY = $00005502;
  6.   UI_DEV_SETUP = $405C5503;

But.
When one says "it is very simple" and "for real programmer", it sounds like a challenge.
I just want him to show the muscles.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8204
Re: const declaration
« Reply #46 on: January 16, 2025, 02:26:00 pm »
I do not argue with you, I have understood that it is impossible after very few first posts, so I have just hardcoded the constants needed
Code: Pascal  [Select][+][-]
  1. const
  2.   UI_SET_EVBIT = $40045564;
  3.   UI_SET_KEYBIT = $40045565;
  4.   UI_DEV_CREATE = $00005501;
  5.   UI_DEV_DESTROY = $00005502;
  6.   UI_DEV_SETUP = $405C5503;

But.
When one says "it is very simple" and "for real programmer", it sounds like a challenge.
I just want him to show the muscles.

That's certainly one way of doing it, but you're at risk of getting screwed by changes in word size etc.: I speak from experience.

Look, I agree that Pascal's got a deficiency here. However in the case of FPC it's something that's baked in at a fairly low level: use the forum search facility to find previous discussion of parameterised macros and you'll find an explanation of e.g. why it's not really realistic for the compiler- as implemented- to call an external macro processor (there have been some written for Pascal, but they're pretty weird).

However the bottom line is that if you want to work at that level, the only really robust way to do it is to work in C: and not just any old C, but the same C that's being used to build the kernel. Saying that risks making me unpopular with certain other members of the community but it's a fact, and it's why so many low-level libraries are provided as an integral part of a Linux distro rather than being built on an ad-hoc basis.

Rust, IMO and provided that I understand things correctly, gets it more or less right: provide one kind of macro for text substitution (which is what C tries to do), and another for parameterised substitution where the type of each parameter must be defined in terms of the implementation of the language (i.e. a parameter can be a block of code, but it has to be a consistent and balanced block of code).

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Thaddy

  • Hero Member
  • *****
  • Posts: 16520
  • Kallstadt seems a good place to evict Trump to.
Re: const declaration
« Reply #47 on: January 16, 2025, 02:26:17 pm »
Pointless and clueless.
Anyway defining the consts is a good approach. :-X

Next time check your code, though. We will find out if you have actually tested it.
But I am sure they don't want the Trumps back...

freemind001

  • Jr. Member
  • **
  • Posts: 51
Re: const declaration
« Reply #48 on: January 16, 2025, 03:36:02 pm »
You may want to check it yourself, to find out that the previous code is at least compilable

BrunoK

  • Hero Member
  • *****
  • Posts: 666
  • Retired programmer
Re: const declaration
« Reply #49 on: January 16, 2025, 03:43:44 pm »
I do not argue with you, I have understood that it is impossible after very few first posts, so I have just hardcoded the constants needed
...
But.
When one says "it is very simple" and "for real programmer", it sounds like a challenge.
I just want him to show the muscles.
I'm not a specialist about this stuff, but you would be well inspired to examine the code in 
that shows how the desired values can be programmatically build.
The proposed functions can be inlined and nothing prevents you to build ONCE a var / const list of the values of interest at the start of your program.

Then, study / extract lines of interest from  the link mentionned in
Perhaps this might help.
https://github.com/LongDirtyAnimAlf/FPC-USB-HID/blob/a40a39cb854a1dbaebb04f2139bbb2a73438d49b/usblibs/linux/usbcontroller.pas#L229
specificcally unit usbcontroller.pas for the way of defining constants. That unit has its approach with TObject descendants that might be interesting, but if you want to do things in a classical procedural way pick the bits that are useful to you.

Good luck.


MarkMLl

  • Hero Member
  • *****
  • Posts: 8204
Re: const declaration
« Reply #50 on: January 16, 2025, 04:58:16 pm »
specificcally unit usbcontroller.pas for the way of defining constants. That unit has its approach with TObject descendants that might be interesting, but if you want to do things in a classical procedural way pick the bits that are useful to you.

Although if I'm reading things correctly investigating the Linux input API (libevdev etc.) might be more useful.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

BrunoK

  • Hero Member
  • *****
  • Posts: 666
  • Retired programmer
Re: const declaration
« Reply #51 on: January 16, 2025, 05:20:13 pm »
Although if I'm reading things correctly investigating the Linux input API (libevdev etc.) might be more useful.

MarkMLl
Sure, but if I read the initial post, the objective is how to make things work with FPC and OP'freemind001 seems to be starting with FPC. OP apparently has knowledge of C and Linux but is not fluent in FPC so proposing some kind of bridge learning with similarly named things is an encouragement for progression.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5858
  • Compiler Developer
Re: const declaration
« Reply #52 on: January 16, 2025, 09:28:14 pm »
purity does not directly imply static evaluation it's more like a contract. Then based on that contract it makes sense to statically evaluate calls to pure functions.

It does in the context of the proposed feature. Because some kind of keyword is required to denote a function that can be evaluated at compile time.

IOCTL macros are pure preprocessor stuff.  Not possible to convert.

Huh? These macros can be easily implemented using inline functions and the compiler will completely collapse them. I've done that already in the past.

LV

  • Full Member
  • ***
  • Posts: 207
Re: const declaration
« Reply #53 on: January 17, 2025, 08:51:40 am »
Can you please provide a snippet of code that implements
Code: C  [Select][+][-]
  1. #define UI_DEV_CREATE           _IO(UINPUT_IOCTL_BASE, 1)
in pascal?

Why weren't you satisfied with reply #32?  :(

Code: Pascal  [Select][+][-]
  1. const
  2.   UINPUT_IOCTL_BASE = Ord('U'); // Equivalent to #define UINPUT_IOCTL_BASE 'U'
  3.   UI_DEV_CREATE = (UINPUT_IOCTL_BASE shl 8) or 1; // Equivalent to _IO(UINPUT_IOCTL_BASE, 1)
  4.  

MarkMLl

  • Hero Member
  • *****
  • Posts: 8204
Re: const declaration
« Reply #54 on: January 17, 2025, 09:17:03 am »
Huh? These macros can be easily implemented using inline functions and the compiler will completely collapse them. I've done that already in the past.

If it obeys the inline directive.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

PascalDragon

  • Hero Member
  • *****
  • Posts: 5858
  • Compiler Developer
Re: const declaration
« Reply #55 on: January 18, 2025, 05:05:55 pm »
Huh? These macros can be easily implemented using inline functions and the compiler will completely collapse them. I've done that already in the past.

If it obeys the inline directive.

The IOCTL macros are easy enough for that.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12033
  • FPC developer.
Re: const declaration
« Reply #56 on: January 19, 2025, 04:51:08 pm »
However the bottom line is that if you want to work at that level, the only really robust way to do it is to work in C: and not just any old C, but the same C that's being used to build the kernel. Saying that risks making me unpopular with certain other members of the community but it's a fact, and it's why so many low-level libraries are provided as an integral part of a Linux distro rather than being built on an ad-hoc basis.

The problem is not Pascal, the problem is Unix/Linux API being defined in essentially everything-goes-C. Even if you have macro functionality in a 3rd party language, you can still not interpret system provided C headers, it only makes conversion marginally easier, but doesn't auto adopt.

Therefore I think a autoconf like solution, automating querying and dumping the C compiler/libc combination would make more sense than trying to mimic C language/preprocessor capabilities. This basically is already how it is done, but on a more ad-hoc case to case basis.

But that is _IF_ you agree that something must be done in the first place, which is a very big IF.   

 

TinyPortal © 2005-2018