[fpc-devel] Adding a new assembler to Free Pascal Compiler

Sven Barth pascaldragon at googlemail.com
Tue Apr 5 11:13:32 CEST 2011


Am 05.04.2011 02:09, schrieb Skybuck Flying:
> Hello,
>
> I would like to experiment with the pascal programming language at the
> binary/assembler/machine level.
>
> For example compile pascal sources to virtual machine instruction sets.
>
> I took a look at the free pascal sources and there seem to be some
> assemblers inside the folders.
>
> I see classes with names like TExternalAssember and TInternalAssember
>
> I also see files called: aasmcpu, aasmtai.
>
> Some processor architectures seem to derive from TExternalAssembler but
> then still implement lot's of stuff themselfes inside the class instead
> of calling a real external assembler (perhaps a trick ? ;))
>
> This is confusing me a little bit so I have some question about what
> free pascal exaclty is, how it functions and what it does and can do or
> cannot do:
>
> So some questions:
>
> 1. Are all assemblers compiled into the final free pascal executable ?
> Or is only one assembler possible which is selected by conditional
> defines ?
>
> Suppose the answer to question 1 is: yes, multiple assemblers are
> supported.
>
> (This seems to be the case since free pascal has the -a and -A directive
> with different asm output options... (?!?))
>

Yes and no. One FPC compiler (ppc386, ppcarm, ppcx86_64, etc.) can only 
compile code for its own cpu (i386, arm, x86_64, etc), but multiple 
assemblers for this platform can be used. E.g. the i386 version of the 
compiler supports the GNU assembler, NASM and its own internal 
assembler. Other targets might only support the GNU assembler or some 
other third party assembler.

> Then my next question is 2:
>
> 2. This would mean free pascal is by default a cross compiler, which can
> cross-compile to any assembly/intermediate code.
>
> So then my main/subject question: How to add a new assember to free
> pascal compiler makes sense...
>

As you want to add support for a virtual machine which contains a 
virtual instruction set (aka not compatible to a existing instruction 
set) you'd first need to implement a new CPU target for FPC which will 
contain your assembler as well. Take a look at MIPS or ARM in the 
subdirectories of compiler as those are the "smaller" and perhaps easier 
CPUs to copy.

> Assuming it makes sense I continue my story:
>
> I searched the documentation about how to do this... but there seems to
> be no documentation for this ?
>
> So if somebody could create some (short?) documentation for this or some
> tips how/where to start and how to proceed that would be helpfull.
>
> Perhaps a simple dummy assembler or template might be nice... concerning
> this adding for a new assembler my main question would be where to add
> this unit, how to make free pascal notice it and use it and such ? Where
> to add the compiler directive and perhaps initialization and
> finalization of classes/create/destroy that sort of thing.
>

Best is to dive into the code yourself and try whether you can get a own 
CPU to run (as said: take a look at MIPS and copy what was done to 
implement MIPS for your own (virtual) CPU).

> 3. Assuming free pascal is a cross compiler, and looking at the code...
> it seems free pascal actually compiles pascal code into an abstract
> assembler structure, is this indeed correct ? Can free pascal be
> considered a "pascal to abstract assembler compiler ?" (which would be
> pretty cool) (which ultimately gets assembled into real assembler...)
>

Free Pascal compiles directly native assembler code. It's just nicely 
wrapped up by the backend units (those found in the cpu subdirectories).

>
> So in short what I am looking for is a pascal compiler which takes care
> of all the work like "lexing/tokenizing/parsing/parse tree/syntax
> tree/symantec checks/perhaps even optimizing the intermediate code" and
> gives me something to work with like a syntax tree or some form of
> abstract assembler (structures) like free pascal seems to do...
>
> To me it seems free pascal could be the correct choice... either by
> modifieing it or by extending it... So if I have to modify it... I would
> for example modify the intel or powerpc or sparc source code and pretend
> it's my own thing... but it would probably be better if I can stuff my
> own assembler/platform/units into it to start from scratch just to play
> nice with free pascal and prevent confusing things later on...
>
> So I think I need to some help with: is this possible and how to do
> it/documentation/short tutorial/some tips ?!?

Regards,
Sven



More information about the fpc-devel mailing list