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

Hans-Peter Diettrich DrDiettrich1 at aol.com
Tue Apr 5 14:13:47 CEST 2011


Skybuck Flying schrieb:

> 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.

Only one machine (instruction set) is supported by an FPC executable. 
This is because some conditional compilation is used to handle machine 
or platform specific issues in the parser code.
Look at the various compiler/ppc*.lpi projects, and the related 
subdirectories.

> I took a look at the free pascal sources and there seem to be some 
> assemblers inside the folders.

Multiple assemblers may be usable in one FPC executable.


> 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.

Right.

"make fullcycle" builds all (registered) cross-compilers.
fpc[.exe] is the stub that selects the concrete compiler to use. But 
every single compiler can be invoked directly, too.


> So then my main/subject question: How to add a new assember to free 
> pascal compiler makes sense...

Starting with 
http://wiki.freepascal.org/Porting_Free_Pascal#Porting_to_a_new_Architecture 

you'll find a lack of documentation about this topic :-(

I've been starting once by copying the m68k directory as mycpu, because 
I was more familiar with that cpu. You also can use the "vis" directory 
for your first steps, it's the smallest (and incomplete) port. Then I 
registered the new directory for it (dunno remember how/where). In all 
files and identifiers the old cpu name should be replaced by the new one.

Then the instructions (enum) and register sets have to be specified for 
the new cpu. The vis cpu turned out to be not very helpful, I had to 
look at various cpu's in order to find out what the register data 
structures mean.

When you try to compile the rudimentary cpu, you'll get errors about 
missing units and more, telling you where more has to be done. Have a 
look at the initialization sections of the cpu's, they often call kind 
of registration functions or directly initialize global variables, and 
let them point to cpu-specific tables.


> 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.

Unfortunately I cannot locate my last-year experiments with a new cpu, 
and the docs I wrote then :-(

But if you want to continue with your project, we can work together and 
document what we found out.


> 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.
> 
> 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...)

The pascal code is translated into nodes (TNode), which represent the 
abstract code flow. You must derive your own node classes, and add the 
concrete code generation to them. Your cpunode.pas lists (uses) all your 
node units, typically named n<cpu>*.pas.


> 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...

See above. I'm working more on the front-end side, to add parsers for 
other languages.

> 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 ?!?

See above. New platforms and processors can be added to Free Pascal 
freely, once the related hooks and procedures are known. Modifying 
existing assemblers IMO should be done by conditional compilation, so 
that eventual updates of the original code are integrated automatically.

DoDi




More information about the fpc-devel mailing list