[fpc-devel] Building compiler, rtl, host, target... (cross compiling)
Sven Barth
pascaldragon at googlemail.com
Wed Apr 6 22:25:52 CEST 2011
Wow, wow, wow... you are indeed a bit confused here.
On 06.04.2011 21:54, Skybuck Flying wrote:
> Hello,
>
> First of all I would like to write that I failed to compile the RTL of
> Free Pascal so that might be adding a little bit too the confusion.
> (Just simple i386/x86 tests to see if the sources build)
>
> The compiler however seems to compile fine.
>
> I am now a bit confused about the whole theory of things and I will try
> to explain my confusion:
>
> The confusion pretty much starts with the compiler needing some RTL
> includes and units and especially some types like DWORD or PtrInt to be
> able to compile itself ?!?
>
> This is a little bit weird to me: Why would the compiler need an RTL to
> compile itself ? (Perhaps these are accidental, or unnecessary depedancy ?)
>
RTL is an abbreviation for Runtime Type Library and it's necessary for
EVERY Free Pascal program, because it abstracts the functions of the
operating system, provides the system unit and types and other units. As
the compiler is just a plain Free Pascal program itself (and when FPC
compiles itself it just compiles a plain FPC program) it's absolutely
naturaly that the compiler needs the RTL. I would have been more
surprised if the compiler would not need the RTL...
> I am now a bit confused again about what free pascal is...
>
> As far as I can remember I once build a free pascal compiler which would
> compile from a certain "host" operating system towards another "target"
> operating system.
>
> Is free pascal supposed to be able to do this all naturally by itself
> (as long as it can use external tools) ?
>
Yes. As free pascal is able to cross compile (see below).
> I just consulted my little tutorial on building a free pascal cross
> compiler which I once wrote and I notice the following things:
>
> It mentions a "cross assembler" and a "cross linker" these where
> external GNU related tools.
>
>> From the looks of it I'm thinking right now that "Free Pascal" might
> actually not be a cross compiler and it actually can only compile to
> it's own platform that the compiler executable was compiled for.
>
This assumption is wrong.
The following assumption is right: the compile can only compile for the
one target it was configured for. The target the compiler is compiled to
and the target the compiler compiles for don't need to be the same. E.g.
I could compile a PowerPC cross compiler by compiling an ARM based
compiler that is cross compiled by a i386 cross compiler for ARM.
> So in other words if this "hypothesis" above is correct then what was
> happening at the time might have been the following situation:
>
> 1. "Free Pascal" ("fake cross") compiler compiles the source from
> PPC386.EXE (i386 host) towards Linux/PowerPC (made up target) as "x86
> assembly".
>
> 2. The cross-assembler.exe from GNU tools takes the x86 assembly
> produced by free pascal compiler and actually "cross-assembles" it for
> linux/powerpc into powerpc assembly.
>
> 3. The cross-linker does the rest.
>
Let's assume you want a native ARM compiler and start with an i386
compiler then the steps are like this:
1. compile a i386=>ARM cross compiler by compiling pp.pas using the
correct defines
2. compile the RTL for ARM using the new cross compiler
3. compile a ARM=>ARM compiler by compiling pp.pas using the correct
defines and by using the i386=>ARM compiler and the ARM RTL
=> tada! you have a native ARM compiler (and a i386 => ARM compiler as well)
[of course this assumes that you have binutils and eventually needed
libraries to do the cross compile]
> So suppose that's how it was working at the time then Free Pascal is
> actually not really a "cross compiler".
>
You are supposing wrong ;)
>
>
> However this does not mean it's useless for my project/experiments... I
> need Free Pascal to be a "cross compiler" which compiles to "abstract
> assembly" so I can assemble it further into "any assembler" a true cross
> compiler I would say ;)
>
>
> Perhaps I am wrong and you guys would like to correct me... but for that
> I will help you by asking a question:
>
>
> 1. Would it be possible to build the following compiler:
>
> FreePascalCompiler.exe (runs on windows) (compiles for virtual machine
> instructions/outputs virtual machine assembly).
>
Yes this would be possible. Does that virtual machine you want to target
FPC for provide its own virtual instruction set or does it use the
instruction set of one of the already supported platforms?
If the latter then you don't need to do much.
If the first then you either need to find cross binutils that allow you
to assemble code to that virtual instruction set or you must write an
internal assembler for FPC. In both cases though you must add a new cpu
(parallel to ARM, i386, PowerPC, etc.) that contains the needed methods
for the compiler to call (that generate the assembler code for the
assembler) and handles calling conventions, variable locations, etc.
>
> Or is a "FreePascalCompiler.exe (x86)" only capable of producing "x86
> assembly" like I suspect it might be/do...
>
>
> Suppose I am correct and free pascal compiler can only compile towards
> the target that it itself was compiled for then I have a little problem:
>
> My virtual machine isn't really ment to access files, or serve as an
> operating system or be a memory manager or have great capabilities...
> it's pretty much ment to execute some simple instructions and that's it...
>
>
> So what I need is a "free pascal compiler" which was compiled for
> Windows environment/executable but which can still compile for a virtual
> machine.
>
>
>
> I was under the impression that free pascal was a true cross compiler
> and therefore I was a bit confused about the RTL.... why does the free
> pascal compiler need an RTL ?
>
As said above: every FPC program needs a RTL.
> How do I add a new RTL for another target to the compiler ?!? Is it even
> necessary or can it compile without it with a few modifications (me
> investigating this ;))
>
You could take a look at the "embedded" target. It's for writing code
directly on CPUs without an operating system in between. So once you've
added your new virtual platform you can add an embedded target and then
you should be (nearly) ready to go...
> It's all a bit fuzzy...
>
>
>
> I think most of my questions where already answered in a previous
> posting/reply... people said:
>
> "Nope... free pascal can only compile applications towards it's own
> platform/target that it was compiled for..."
You got the sense of "compiled for" a bit wrong. I didn't mean to say
that a compiler compiled for i386 can only generate i386 code. I meant
that a compiler that has the "i386 code generation" enabled can only
generate i386 code no matter for which processor to run on it was compiled.
>
>
> If that's indeed correct then that's a bit unfortunate...
>
>
> But none-the-less I seem some possibilities to make free pascal do what
> I want...
>
>
> I could simply take the i386/x86 version of it with it's internal
> assemblers... and then simply convert those internal assemblers towards
> a sort of cross-assemblers... which would output "virtual machine
> assembly".
>
>
> I'm looking for some clearification and confirmation on these thoughts
> of mine so that I understand correctly...
>
> Also a little bit more explanation about the roll of RTL in relation to
> the compiler would be nice...
>
I hope my explanation helps you.
>
> Let me ask a question in relation to that...
>
>
> Let's suppose I want to compile free pascal compiler and/or RTL for a
> new target.. since free pascal can only compile towards it's own
> target... it cannot do this correct ?
>
> So to actually "port" pascal towards a new platform would theoretically
> require other compilers written for the other platforms ?
>
> So in a way this makes free pascal not "cross-platform-self-hosting" ?
>
> So it's still a bit stuck in it's own world ???
Perhaps it would help if you'd answer directly to other people's mails
and add your specific questions inline below a statement that you'd like
to comment on like I'm doing it at this very moment...
Regards,
Sven
More information about the fpc-devel
mailing list