Page 1 of 1

FPC RTL (AnsiString etc)

Posted: Tue Sep 23, 2014 3:32 pm
by Cjreek
Hi,

After quite a long time without any work on my OS I decided to find my way back.
I'm using Free Pascal for my kernel code and I'd like to use Ansi- and/or WideStrings.
Those types are Pointer types and the compiler needs to know how to allocate memory and how to do reference counting.

If you use an AnsiString variable in your code the compiler hints some "compilerproc"s which have to be implemented before AnsiStrings will work.

I declared those compilerprocs in system.pas which compiles but it doesn't work at runtime because those procedures are empty for now. In order to allocate any memory for those strings I'd need access to my unit which handles memory allocation.

But it seems like I can't add any unit to my uses in system.pas.
If I write it in the uses of the interface section of system.pas I get this:

Code: Select all

unit system;

interface

uses
  memory;
Fatal: Syntax error, "IMPLEMENTATION" expected but "USES" found
If i add my memory unit to implementation uses I get this:

Code: Select all

implementation

uses
  memory in '..\kernel\memory.pas'; // I have to specify the path because else the compiler doesn't find the unit at all.
Fatal: Internal error 200602044
Does anybody know how to do this?
Or is there any source where I can read about implementing the fpc rtl?

Re: FPC RTL (AnsiString etc)

Posted: Wed Sep 24, 2014 6:54 am
by Kevin
The best resource for how to write an FPC RTL is the FPC source itself. Most of the magic isn't documented, so you need to look at how the RTL does things for Linux or other OSes; and when you get something wrong, you get "internal errors" instead of proper error messages, which only make sense after looking at the compiler code. Also, the magic changes from version to version, so after your next compiler upgrade you'll probably have to forward-port your code. In short: Using FPC for OS-Dev comes with some challenges.

As for the problem with including units in system: That's the reason why the original RTL doesn't use units, but C-style header and implementation include files like this:

Code: Select all

unit system;

interface
{$I fooh.inc}

implementation
{$I foo.inc}

end.

Re: FPC RTL (AnsiString etc)

Posted: Wed Sep 24, 2014 12:02 pm
by Cjreek
Thanks for your tips.

I found an open source kernel made with fpc: http://jpaskernel.googlecode.com/svn/trunk/
He also implemented at least most of the RTL so I'll have a look at that.

And I was wrong. You can include units in the implementation part of system.pas.
I just had to include the path of the units in the search path. (-Fu parameter of fpc).
So this was totally my fault.

I'm much more familiar with pascal than C so I'll stick with pascal for now I think.
I'm sure I'll use {$I} as well just for the purpose of a more readable code.

If I fail horribly I might consider switching to C.

Re: FPC RTL (AnsiString etc)

Posted: Wed Sep 24, 2014 12:20 pm
by Kevin
It's definitely possible to write a kernel with FPC, mine is working. It's just not as convenient and straightforward as C at times. On the other hand, you gain some nice features, too, especially with a reasonably complete runtime.