Native MIPS Systems
The MIPS architecture is unusual in that it was designed for desktop computers, is now overwhelmingly used for embedded systems, but can still occasionally be found in desktops or small servers. The only comparable architecture is ARM, which is the platform of choice for a multitude of "bratphones", tablets etc.
Like almost all computers, MIPS-based systems will contain an internal loader, usually in Flash memory. However there are three basic categories of system:
- Those that boot an operating system from internal Flash, such as the Linksys/Cisco WRT54G [1].
- Those that boot an operating system from a conventional disc, connected via IDE, SCSI or USB, such as an SGI server or workstation, or a Cobalt/Sun Qube or Raq.
- Those running as guests using an emulator such as Qemu on a general-purpose computer.
In any of these cases it is necessary to be able to install a general-purpose operating system such as Linux in order to be able to run development tools. This writer (MarkMLl) favours Debian, since he finds that using it results in a very similar system on a wide range of hardware (x86, SPARC, PPC, MIPS, ARM, zSeries, 68K).
If you intend to build Lazarus, make sure you have at least 512Mb memory (RAM + swap) available.
FPC Implementations for MIPS
As of 2011 there are two implementations of FPC for the MIPS architecture, both targeting Linux.
- A fork of the mainline compiler at 2.0.0 by David Zhang.
- An ongoing effort to reconcile this with the mainline project, partially completed in 2.4.x.
David Zhang's Compiler
The "unofficial" MIPS compiler at [2] was written by David Zhang (possibly [3]) and is licensed under GPL Version 2, it cites but does not include the COPYING.FPC file. The compiler appears to have been originally based on the SPARC port, and there are significant differences between it and the mainline sources. It is discussed for historical interest, and as reference until the mainline compiler is at least as complete and robust.
There are .zip files of both sources and a minimal executable at Sourceforge (link above). The sources are basically a complete reimplementation of fpcsrc/compiler/mips based on FPC 2.0.0, the compiler targets mipsel and is described as being operational on Qemu but not yet supporting shared libraries.
Since this is specifically a little-endian compiler it should presumably be compatible with Linux on a MIPS-based Qube or Raq, or with the Chinese "Loongson" processor of fable. It will not be compatible with SGI MIPS systems, which are big-endian.
There is a system library bundled with the compiler, but no indication of how far the compiler is capable of handling the remainder of the standard libraries. The released files may be downloaded from:
This comprises the binary compiler pp_mipsel32. It successfully compiles a minimal test program which appears to run.
This is both source and binary, dumped into a single directory which appears to correspond to the standard fpc_200/compiler, i.e. there's no separate compiler/mips directory; it also partially reimplements some standard libraries etc.
Inspecting the sources, these files are identical in the Chinese compiler and in fpc_200/compiler, so may be removed from the former:
rabase.pas switches.pas ncal.pas tokens.pas defutil.pas msgidx.inc ogcoff.pas nstate.pas comprsrc.pas nutils.pas cutils.pas nbas.pas verbose.pas gendef.pas msgtxt.inc defcmp.pas aopt.pas nmem.pas script.pas gdb.pas symutil.pas rautils.pas ncnv.pas pmodules.pas pass_2.pas catch.pas pass_1.pas ncgset.pas symbase.pas psub.pas impdef.pas oglx.pas aoptbase.pas symconst.pas ncgmem.pas raatt.pas rgobj.pas regvars.pas fmodule.pas cp8859_1.pas import.pas cp437.pas symnot.pas pbase.pas ppheap.pas aasmbase.pas pdecl.pas aoptcs.pas charset.pas cgutils.pas pexpr.pas ncgcon.pas rasm.pas dwarf.pas nopt.pas tgobj.pas procinfo.pas aasmtai.pas ppu.pas cg64f32.pas pexports.pas ncgadd.pas pdecvar.pas browcol.pas nadd.pas compinnr.inc ncgflw.pas ncgmat.pas nld.pas aggas.pas ncginl.pas ncgbas.pas export.pas finput.pas owbase.pas symtype.pas owar.pas pdecsub.pas cstreams.pas cp850.pas aoptda.pas ogbase.pas aoptobj.pas nflw.pas browlog.pas symtable.pas crc.pas globtype.pas ptconst.pas widestr.pas scandir.pas nobj.pas COPYING nset.pas node.pas ogmap.pas symsym.pas ncon.pas link.pas cclasses.pas htypechk.pas ncgopt.pas comphook.pas rgbase.pas nmat.pas pinline.pas ncgcnv.pas parabase.pas pdecobj.pas cresstr.pas ogelf.pas cgbase.pas ptype.pas fppu.pas scanner.pas parser.pas cmsgs.pas
These files are duplicates of existing files elsewhere in the rtl tree so may be removed:
dati.inc dynarrh.inc sstrings.inc rtti.inc errno.inc stringsi.inc sysinth.inc gensigset.inc genstrs.inc bunxh.inc osutil.inc dos.pp datih.inc except.inc genset.inc systhrdh.inc osmacro.inc bunxovlh.inc unxfunc.inc sysfile.inc sysformt.inc genmath.inc unixtype.pp generic.inc ObjPas.pp (AKA objpas.pp) wstringh.inc syswide.inc sysheap.inc sysuintf.inc real2str.inc heaph.inc threadvr.inc sysdir.inc objpash.inc syspch.inc wstrings.inc genfuncs.inc osdefs.inc int64.inc sysstrh.inc sysansih.inc fina.inc text.inc filutilh.inc dynarr.inc aliasctp.inc astrings.inc fexpand.inc syscall.pp variant.inc settimeo.inc typefile.inc filerec.inc aliasptp.inc file.inc sysutils.inc genfdset.inc sysutils.pp sysstr.inc textrec.inc syspchh.inc mathh.inc sysutilh.inc compproc.inc dos.inc dosh.inc genstr.inc timezone.inc baseunix.pp unxsysc.inc sysosh.inc sysconst.pp innr.inc unxsysch.inc sysint.inc threadh.inc ctypes.inc diskh.inc heap.inc thread.inc unxconst.inc syswideh.inc sysos.inc sysunixh.inc intfh.inc varianth.inc unxovl.inc finah.inc objpas.inc aliases.inc unxovlh.inc osutilsh.inc systhrd.inc ptypes.inc strings.pp variants.pp errors.pp sysansi.inc
These files are exact copies or are equivalents of files in rtl/sparc or rtl/linux/sparc so may be moved to rtl/mips or rtl/linux/mips:
setjumph.inc setjump.inc mipsel.inc sysutilp.inc bsyscall.inc sighnd.inc stat.inc syscall.inc sysnr.inc strings.inc math.inc stringss.inc int64p.inc set.inc syscallh.inc sighndh.inc
These are test files and documentation, so don't have to be considered part of the compiler for build purposes:
bench.pas console_io.pas hello.pas magic.pp WETS.pp *TXT i_sunos.pas t_sunos.pas
These files have no significant changes, so may be discarded:
ossysc.inc assemble.pas
These files have trivial extensions to the standard sources, so probably can overwrite them:
compiler.pas fpcdefs.inc ncgld.pas ncgutil.pas options.pas paramgr.pas pp.pas pstatmnt.pas psystem.pas symdef.pas systems.pas version.pas systemh.inc system.inc i_linux.pas t_linux.pas
These files have non-trivial changes, so must be considered specially:
cgobj.pas globals.pas ncgcal.pas ninl.pas bunxovl.inc unix.pp bunxsysc.inc ostypes.inc signal.inc System.pas (extends system.pp)
These files are unique to the compiler:
aasmcpu.pas aoptcpub.pas aoptcpud.pas aoptcpu.pas cgcpu.pas cpubase.pas cpugas.pas cpuinfo.pas cpunode.pas cpupara.pas cpupi.pas cpuswtch.pas cputarg.pas itcpugas.pas ncpuadd.pas ncpucall.pas ncpucnv.pas ncpuinln.pas ncpumat.pas ncpuset.pas opcode.inc racpu.pas raMIPS.pas raMPSgas.pas rfv32con.inc rfv32dwf.inc rfv32nor.inc rfv32num.inc rfv32rni.inc rfv32sri.inc rfv32sta.inc rfv32std.inc rfv32sup.inc rgcpu.pas strinst.inc sysconst.rst
It should be possible to rebuild the compiler using David Zhang's binary with duplicated files etc. cleaned up, i.e. using the core files as listed above plus suitable search paths etc. However in general effort is better applied to the mainline MIPS port (below).
Mainline MIPS Port
This is part of the trunk development files, but does not yet appear in standard releases; i.e. it's only available via an svn download.
The host is assumed to be a non-MIPS system, for example x86 running Linux (e.g. Debian "Squeeze"). It needs a copy of binutils, and it is probably expedient to make this the same version as is running natively, i.e. 2.20.1 in the above case, downloaded from [4]. In any event, no version older than 2.18 will work since symbolic register names are a prerequisite.
Build and install cross-binutils:
$ tar xjf binutils-2.20.1.tar.bz2 $ cd binutils-2.20.1 $ ./configure --prefix=/usr/local/mipsel-linux mipsel-linux-gnu $ make $ sudo make install $ sudo ln -s /usr/local/mipsel-linux mipsel-linux/bin/as /usr/local/bin/mipsel-linux-as $ sudo ln -s /usr/local/mipsel-linux mipsel-linux/bin/ld /usr/local/bin/mipsel-linux-ld $ mipsel-linux-ld -V
Particularly if working with the trunk sources, consider doing the same for a different CPU (e.g. powerpc-linux-gnu) so as not to be caught out if they won't build due to a temporary inconsistency.
Inspect compiler/psystem.pas and make sure it initialises floating point types:
{$ifdef avr} s32floattype:=tfloatdef.create(s32real); s64floattype:=tfloatdef.create(s64real); s80floattype:=tfloatdef.create(s80real); sc80floattype:=tfloatdef.create(sc80real); s64currencytype:=torddef.create(scurrency,low(int64),high(int64)); {$endif avr} {$ifdef mips} // HIGHLY TENTATIVE, modelled after powerpc. MarkMLl. create_fpu_types; s64currencytype:=torddef.create(scurrency,low(int64),high(int64)); {$endif mips}
Inspect compiler/options.pas and make sure it selects word size etc.:
{$ifdef avr} def_system_macro('CPUAVR'); def_system_macro('CPU16'); def_system_macro('FPC_CURRENCY_IS_INT64'); def_system_macro('FPC_COMP_IS_INT64'); {$endif avr} {$ifdef mips} // HIGHLY TENTATIVE, from David Zhang's options.pas. MarkMLl. def_system_macro('CPUMIPS'); def_system_macro('CPUMIPS32'); def_system_macro('CPU32'); // def_system_macro('FPC_HAS_TYPE_DOUBLE'); // def_system_macro('FPC_HAS_TYPE_SINGLE'); // def_system_macro('FPC_INCLUDE_SOFTWARE_INT64_TO_DOUBLE'); def_system_macro('FPC_CURRENCY_IS_INT64'); def_system_macro('FPC_COMP_IS_INT64'); // def_system_macro('FPC_REQUIRES_PROPER_ALIGNMENT'); {$endif}
In fpcdefs.inc, remove the definition of cpumm associated with mips/mipsel.
Build a compiler which runs on the host but targets mipsel, note debug options which are needed later:
$ cd /usr/local/src/fpc-trunk $ export PP= $ make "OPT=-O- -g" -C compiler mipsel $ mv compiler/ppcmipsel compiler/ppcXmipsel
Renaming the compiler prevents it from being deleted by make clean etc., consider extending that OPT setting with -dEXTDEBUG.
Inspect prt0.as, e.g. in rtl/linux/mipsel/prt0.as. If necessary, close the multi-line comment starting at line 20 and then if line 71 reads as below (arrowed):
addu $a2,$a2,$a1 lui $a3,$hi(operatingsystem_parameter_envp) <===== jal PASCALMAIN sw $a2,%lo(operatingsystem_parameter_envp)($a3)
edit $hi to read %hi. Compile with:
$ export PP=/usr/local/src/fpc-trunk/fpc/compiler/ppcXmipsel $ make CPU_TARGET=mipsel OS_TARGET=linux rtl
cprt0.as, dllprt0.as and gprt0.as aren't needed yet (they're for compiling with linkage to C libraries, compiling DLLs and compiling for gprof respectively) so may all be dummies. I favour a minimal comment in cases like this rather than leaving files blank.
Referring to trunk, David Zhang's original RTL source is in rtl/mips but trying to build for mipsel has the compiler looking in rtl/mipsel. Setting up symlinks for mips.inc, math.inc and set.inc allow compilation to start.
Stall point
Assuming cpumm removed from mips/mipsel section of fpcdefs.inc.
Provided that compiler/psystem.pas, compiler/options.pas, mipsel/prt0.as and dummy files have been fixed as above, the makefile should not warn about missing files and should invoke the compiler. The compiler should run without throwing an exception, but should report missing identifiers etc. in mathh.inc and missing ValReal, fixable by copying from the POWERPC section.
Symlink for setjumph.inc gets us to
ostypes.inc(86,2) Fatal: Can't open include file "stat.inc"
and so on as below. Initially, I'm finding the missing files in ../mips:
signal.inc(183,2) Fatal: Can't open include file "sighndh.inc" sysos.inc(66,4) Fatal: Can't open include file "syscallh.inc" sysos.inc(67,4) Fatal: Can't open include file "syscall.inc"
OK, here's something different:
syscall.inc(28,1) Fatal: Selected assembler reader not supported
In fpc/compiler, grep -rIi asmmode * suggests I might need to add MIPS stuff to systems.pas and CG.
Probably needs a new unit racpugas.pas tailored appropriately.
To be continued (hopefully :-)
As a general point, there's some useful thoughts on binary disassembly at [5] for situations where IDA or equivalent aren't available.