prog.tex 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291
  1. %
  2. % $Id$
  3. % This file is part of the FPC documentation.
  4. % Copyright (C) 1997, by Michael Van Canneyt
  5. %
  6. % The FPC documentation is free text; you can redistribute it and/or
  7. % modify it under the terms of the GNU Library General Public License as
  8. % published by the Free Software Foundation; either version 2 of the
  9. % License, or (at your option) any later version.
  10. %
  11. % The FPC Documentation is distributed in the hope that it will be useful,
  12. % but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. % Library General Public License for more details.
  15. %
  16. % You should have received a copy of the GNU Library General Public
  17. % License along with the FPC documentation; see the file COPYING.LIB. If not,
  18. % write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. % Boston, MA 02111-1307, USA.
  20. %
  21. \documentclass{report}
  22. \usepackage{a4}
  23. \usepackage{html}
  24. \latex{\usepackage{multicol}}
  25. \latex{\usepackage{fpkman}}
  26. \html{\input{fpk-html.tex}}
  27. % define the version number here, and not in the fpk.sty !!!
  28. \newcommand{\fpkversion}{0.9.7}
  29. \newcommand{\remark}[1]{\par$\rightarrow$\textbf{#1}\par}
  30. \newcommand{\olabel}[1]{\label{option:#1}}
  31. % We should change this to something better. See \seef etc.
  32. \begin{document}
  33. \title{Free Pascal \\ Programmer's manual}
  34. \docdescription{Programmer's manual for \fpk, version \fpkversion}
  35. \docversion{1.0}
  36. \date{July 1997}
  37. \author{Micha\"el Van Canneyt}
  38. \maketitle
  39. \tableofcontents
  40. \newpage
  41. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  42. % Introduction
  43. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  44. \section*{About this document}
  45. This is the programmer's manual for \fpk.
  46. It describes some of the peculiarities of the \fpk compiler, and provides a
  47. glimp of how the compiler generates its code, and how you can change the
  48. generated code. It will not, however, provide you with a detailed account of
  49. the inner workings of the compiler, nor will it tell you how to use the
  50. compiler (described in the \userref). It also will not describe the inner
  51. workings of the Run-Time Library (RTL). The best way to learn about the way
  52. the RTL is implemented is from the sources themselves.
  53. The things described here are useful if you want to do things which need
  54. greater flexibility than the standard Pascal language constructs.
  55. (described in the \refref)
  56. Since the compiler is continuously under development, this document may get
  57. out of date. Wherever possible, the information in this manual will be
  58. updated. If you find something which isn't correct, or you think something
  59. is missing, feel free to contact me\footnote{at
  60. \var{[email protected]}}.
  61. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  62. % Compiler switches
  63. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  64. \chapter{Compiler directives}
  65. \label{ch:CompSwitch}
  66. \fpk supports compiler directives in your source file. They are not the same
  67. as Turbo Pascal directives, although some are supported for compatibility.
  68. There is a distinction between local and global directives; local directives
  69. take effect from the moment they are encountered, global directives have an
  70. effect on all of the compiled code.
  71. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  72. % Local switches
  73. \section{Local directives}
  74. \label{se:LocalSwitch}
  75. Local directives have no command-line counterpart. They influence the
  76. compiler's behaviour from the moment they're encountered until the moment
  77. another switch annihilates their behaviour, or the end of the unit or
  78. program is reached.
  79. \subsection{\var{\$F} : Far or near functions}
  80. This directive is recognized for compatibility with Turbo Pascal. Under the
  81. 32-bit programming model, the concept of near and far calls have no meaning,
  82. hence the directive is ignored. A warning is printed to the screen, telling
  83. you so.
  84. As an example, : the following piece of code :
  85. \begin{verbatim}
  86. {$F+}
  87. Procedure TestProc;
  88. begin
  89. Writeln ('Hello From TestProc');
  90. end;
  91. begin
  92. testProc
  93. end.
  94. \end{verbatim}
  95. Generates the following compiler output:
  96. \begin{verbatim}
  97. malpertuus: >pp -vw testf
  98. Compiler: ppc386
  99. Units are searched in: /home/michael;/usr/bin/;/usr/lib/ppc/0.9.1/linuxunits
  100. Target OS: Linux
  101. Compiling testf.pp
  102. testf.pp(1) Warning: illegal compiler switch
  103. 7739 kB free
  104. Calling assembler...
  105. Assembled...
  106. Calling linker...
  107. 12 lines compiled,
  108. 1.00000000000000E+0000
  109. \end{verbatim}
  110. You can see that the verbosity level was set to display warnings.
  111. If you declare a function as \var{Far} (this has the same effect as setting it
  112. between \var{\{\$F+\}...\{\$F-\}} directives), the compiler also generates a
  113. warning :
  114. \begin{verbatim}
  115. testf.pp(3) Warning: FAR ignored
  116. \end{verbatim}
  117. The same story is true for procedures declared as \var{Near}. The warning
  118. displayed in that case is:
  119. \begin{verbatim}
  120. testf.pp(3) Warning: NEAR ignored
  121. \end{verbatim}
  122. \subsection{\var{\$I} : Input/Output checking}
  123. The \var{\{\$I-\}} directive tells the compiler not to generate input/output
  124. checking code in your program. If you compile using the \var{-Ci} compiler
  125. switch, the \fpk compiler inserts input/output
  126. checking code after every input/output call in your program. If an error
  127. occurred during input or output, then a run-time error will be generated.
  128. Use this switch if you wish to avoid this behavior.
  129. If you still want to check if something went wrong, you can use the
  130. \var{IOResult} function to see if everything went without problems.
  131. Conversely, \var{\{\$I+\}} will turn error-checking back on, until another
  132. directive is encountered which turns it off again.
  133. The most common use for this switch is to check if the opening of a file
  134. went without problems, as in the following piece of code:
  135. \begin{verbatim}
  136. ...
  137. assign (f,'file.txt');
  138. {$I-}
  139. rewrite (f);
  140. {$I+}
  141. if IOResult<>0 then
  142. begin
  143. Writeln ('Error opening file : "file.txt"');
  144. exit
  145. end;
  146. ...
  147. \end{verbatim}
  148. \subsection{\var{\$I} : Include file }
  149. The \var{\{\$I filename\}} directive tells the compiler to read further
  150. statements from the file \var{filename}. The statements read there will be
  151. inserted as if they occurred in the current file.
  152. The compiler will append the \file{.pp} extension to the file if you don't
  153. specify an extension yourself. Do not put the filename between quotes, as
  154. they will be regarded as part of the file's name.
  155. You can nest included files, but not infinitely deep. The number of files is
  156. restricted to the number of file descriptors available to the \fpk compiler.
  157. Contrary to Turbo Pascal, include files can cross blocks. I.e. you can start
  158. a block in one file (with a \var{Begin} keyword) and end it in another (with
  159. a \var{End} keyword). The smallest entity in an include file must be a token,
  160. i.e. an identifier, keyword or operator.
  161. \subsection{\var{\$L} : Link object file}
  162. The \var{\{\$L filename\}} directive tells the compiler that the file \file{filename}
  163. should be linked to your program. You can only use this directive in a
  164. program. If you do use it in a unit, the compiler will not complain, but
  165. simply ignores the directive.
  166. The compiler will {\em not} look for the file in the unit path.
  167. The name will be passed to the linker {\em exactly} as you've typed it.
  168. Since the files name is passed directly to the linker, this means that on
  169. \linux systems, the name is case sensitive, and must be typed exactly as it
  170. appears on your system.
  171. {\em Remark :} Take care that the object file you're linking is in a
  172. format the linker understands. Which format this is, depends on the platform
  173. you're on. Typing \var{ld} on th command line gives a list of formats
  174. \var{ld} knows about.
  175. You can pass other files and options to the linker using the \var{-k}
  176. command-line option. You can specify more than one of these options, and
  177. they
  178. will be passed to the linker, in the order that you specified them on the
  179. command line, just before the names of the object files that must be linked.
  180. % Assembler type
  181. \subsection{\var{\$I386\_XXX} : Specify assembler format}
  182. This switch informs the compiler what kind of assembler it can expect in an
  183. \var{asm} block. The \var{XXX} should be replaced by one of the following:
  184. \begin{description}
  185. \item [att\ ] Indicates that \var{asm} blocks contain AT\&T syntax assembler.
  186. \item [intel\ ] Indicates that \var{asm} blocks contain Intel syntax
  187. assembler.
  188. \item [direct\ ] Tells the compiler that asm blocks should be copied
  189. directly to the assembler file.
  190. \end{description}
  191. These switches are local, and retain their value to the end of the unit that
  192. is compiled, unless they are replaced by another directive of the same type.
  193. The command-line switch that corresponds to this switch is \var{-R}.
  194. \subsection{\var{\$MMX} : MMX support}
  195. As of version 0.9.8, \fpk supports optimization for the \textbf{MMX} Intel
  196. processor (see also \ref{ch:MMXSupport}). This optimizes certain code parts for the \textbf{MMX} Intel
  197. processor, thus greatly improving speed. The speed is noticed mostly when
  198. moving large amounts of data. Things that change are
  199. \begin{itemize}
  200. \item Data with a size that is a multiple of 8 bytes is moved using the
  201. \var{movq} assembler instruction, which moves 8 bytes at a time
  202. \end{itemize}
  203. When \textbf{MMX} support is on, you aren't allowed to do floating point
  204. arithmetic. You are allowed to move floating point data, but no arithmetic
  205. can be done. If you wish to do floating point math anyway, you must first
  206. switch of \textbf{MMX} support and clear the FPU using the \var{emms}
  207. function of the \file{cpu} unit.
  208. The following example will make this more clear:
  209. \begin{verbatim}
  210. Program MMXDemo;
  211. uses cpu;
  212. var
  213. d1 : double;
  214. a : array[0..10000] of double;
  215. i : longint;
  216. begin
  217. d1:=1.0;
  218. {$mmx+}
  219. { floating point data is used, but we do _no_ arithmetic }
  220. for i:=0 to 10000 do
  221. a[i]:=d2; { this is done with 64 bit moves }
  222. {$mmx-}
  223. emms; { clear fpu }
  224. { now we can do floating point arithmetic }
  225. ....
  226. end.
  227. \end{verbatim}
  228. See, however, the chapter on MMX (\ref{ch:MMXSupport}) for more information
  229. on this topic.
  230. \subsection{\var{\$OUTPUT\_FORMAT} : Specify the output format}
  231. \var{\{\$OUTPUT\_FORMAT format\}} has the same functionality as the \var{-A}
  232. command-line option : It tells the compiler what kind of object file must be
  233. generated. You can specify this switch \textbf{only} befor the \var{Program}
  234. or \var{Unit} clause in your source file. The different kinds of formats are
  235. shown in \seet{Formats}.
  236. \begin{FPKltable}{ll}{Formats generated by the compiler}{Formats} \hline
  237. Switch value & Generated format \\ \hline
  238. att & AT\&T assembler file. \\
  239. o & Unix object file.\\
  240. obj & OMF file.\\
  241. wasm & assembler for the Watcom assembler. \\ \hline
  242. \end{FPKltable}
  243. \subsection{\var{\$V} : Var-string checking}
  244. When in the \var{+} state, the compiler checks that strings passed as
  245. parameters are of the same, identical, string type as the declared
  246. parameters of the procedure.
  247. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  248. % Global switches
  249. \section{Global directives}
  250. \label{se:GlobalSwitch}
  251. Global directives affect the whole of the compilation process. That is why
  252. they also have a command - line counterpart. The command-line counterpart is
  253. given for each of the directives.
  254. \subsection{\var{\$A} : Align Data}
  255. This switch is recognized for Turbo Pascal Compatibility, but is not
  256. yet implemented. The alignment of data will be different in any case, since
  257. \fpk is a 32-bit compiler.
  258. \subsection{\var{\$B} : Complete boolean evaluation}
  259. This switch is understood by the \fpk compiler, but is ignored. The compiler
  260. always uses shortcut evaluation, i.e. the evaluation of a boolean expression
  261. is stopped once the result of the total exression is known with certainty.
  262. So, in the following example, the function \var{Bofu}, which has a boolean
  263. result, will never get called.
  264. \begin{verbatim}
  265. If False and Bofu then
  266. ...
  267. \end{verbatim}
  268. \subsection{\var{\$D} : Debugging symbols}
  269. When this switch is on, the compiler inserts GNU debugging information in
  270. the executable. The effect of this switch is the same as the command-line
  271. switch \var{-g}. By default, insertion of debugging information is off.
  272. \subsection{\var{\$E} : Emulation of coprocessor}
  273. This directive controls the emulation of the coprocessor. On the i386
  274. processor, it is supported for
  275. compatibility with Turbo Pascal. The compiler itself doesn't do the emulation
  276. of the coprocessor. Under \dos, the \dos extender does this, and under
  277. \linux, the kernel takes care of the coprocessor support.
  278. If you use the Motorola 680x0 version, then the switch is recognized, as
  279. there is no extender to emulate the coprocessor, so the compiler must do
  280. that by itself.
  281. There is no command-line counterpart for this directive.
  282. \subsection{\var{\$G} : Generate 80286 code}
  283. This option is recognised for Turbo Pascal cmpatibility, but is ignored,
  284. because the compiler needs at least a 386 or higher class processor.
  285. \subsection{\var{\$L} : Local symbol information}
  286. This switch (not to be confused with the \var{\{\$L file\}} file linking
  287. directive) is recognised for Turbo Pascal compatibility, but is ignored.
  288. generation of symbol information is controlled by the \var{\$D} switch.
  289. \subsection{\var{\$N} : Numeric processing }
  290. This switch is recognised for Turbo Pascal compatibility, but is otherwise
  291. ignored, since the compiler always uses the coprocessor for floating point
  292. mathematics.
  293. \subsection{\var{\$O} : Overlay code generation }
  294. This switch is recognised for Turbo Pascal compatibility, but is otherwise
  295. ignored, since the compiler requires a 386 or higher computer, with at
  296. least 4 Mb. of ram.
  297. \subsection{\var{\$Q} : Overflow checking}
  298. The \var{\{\$Q+\}} directive turns on integer overflow checking.
  299. This means that the compiler inserts code to check for overflow when doing
  300. computations with an integer.
  301. When an overflow occurs, the run-time library will print a message
  302. \var{Overflow at xxx}, and exit the program with exit code 1.
  303. Using the \var{\{\$Q-\}} switch switches off the overflow checking code
  304. generation.
  305. The generation of overflow checking code can also be controlled
  306. using the \var{-Co} command line compiler option (see \userref).
  307. \subsection{\var{\$R} : Range checking}
  308. By default, the computer doesn't generate code to check the ranges of array
  309. indices, enumeration types, subrange types, etc. Specifying the
  310. \var{\{\$R+\}} switch tells the computer to generate code to check these
  311. indices. If, at run-time, an index or enumeration type is specified that is
  312. out of the declared range of the compiler, then a run-time error is
  313. generated, and the program exits with exit code 1.
  314. The \var{\{\$R-\}} switch tells the compiler not to generate range checking
  315. code. This may result in faulty program behaviour, but no run-time errors
  316. will be generated.
  317. {\em Remark: } this has not been implemented completely yet.
  318. \subsection{\var{\$S} : Stack checking}
  319. The \var{\{\$S+\}} directive tells the compiler to generate stack checking
  320. code. This generates code to check if a stack overflow occurred, i.e. to see
  321. whether the stack has grown beyond its maximally allowed size. If the stack
  322. grows beyond the maximum size, then a run-time error is generated, and the
  323. program will exit with exit code 1.
  324. Specifying \var{\{\$S-\}} will turn generation of stack-checking code off.
  325. There is no command-line switch which is equivalent to this directive.
  326. {\em Remark: } In principle, the stack is almost unlimited,
  327. i.e. limited to the total free amount of memory on the computer.
  328. \subsection{\var{\$X} : Extended syntax}
  329. Extended syntax allows you to drop the result of a function. This means that
  330. you can use a function call as if it were a procedure. Standard this feature
  331. is on. You can switch it off using the \var{\{\$X-\}} directive.
  332. The following, for instance, will not compile :
  333. \begin{verbatim}
  334. function Func (var Arg : sometype) : longint;
  335. begin
  336. ... { declaration of Func }
  337. end;
  338. ...
  339. {$X-}
  340. Func (A);
  341. \end{verbatim}
  342. The reason this construct is supported is that
  343. you may wish to call a function for certain side-effects it has, but you
  344. don't need the function result. In this case you don't need to assign the
  345. function result, saving you an extra variable.
  346. The command-line compiler switch \var{-Sa1} has the same effect as the
  347. \var{\{\$X+\}} directive.
  348. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  349. % Using conditionals and macros
  350. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  351. \chapter{Using conditionals, Messages and macros}
  352. \label{ch:CondMessageMacro}
  353. The \fpk compiler supports conditionals as in normal Turbo Pascal. It does,
  354. however, more than that. It allows you to make macros which can be used in
  355. your code, and it allows you to define messages or errors which will be
  356. displayed when compiling.
  357. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  358. % Conditionals
  359. \section{Conditionals}
  360. \label{se:Conditionals}
  361. The rules for using conditional symbols are the same as under Turbo Pascal.
  362. Defining a symbol goes as follows:
  363. \begin{verbatim}
  364. {$Define Symbol }
  365. \end{verbatim}
  366. From this point on in your code, the compiler know the symbol \var{Symbol}
  367. Symbols are, like the Pascal language, case insensitive.
  368. You can also define a symbol on the command line. the \var{-dSymbol} option
  369. defines the symbol \var{Symbol}. You can specify as many symbols on the
  370. command line as you want.
  371. Undefining an existing symbol is done in a similar way:
  372. \begin{verbatim}
  373. {$Undefine Symbol }
  374. \end{verbatim}
  375. If the symbol didn't exist yet, this doesn't do anything. If the symbol
  376. existed previously, the symbol will be erased, and will not be recognized
  377. any more in the code following the \verb|{$Undefine ...}| statement.
  378. You can also undefine symbols from the command line with the \var{-u}
  379. command-line switch..
  380. To compile code conditionally, depending on whether a symbol is defined or
  381. not, you can enclose the code in a \verb|{$ifdef Symbol}| .. \verb|{$endif}|
  382. pair. For instance the following code will never be compiled :
  383. \begin{verbatim}
  384. {$Undefine MySymbol}
  385. {$ifdef Mysymbol}
  386. DoSomething;
  387. ...
  388. {$endif}
  389. \end{verbatim}
  390. Similarly, you can enclose your code in a \verb|{$Ifndef Symbol}| .. \verb|{$endif}|
  391. pair. Then the code between the pair will only be compiled when the used
  392. symbol doesn't exist. For example, in the following example, the call to the
  393. \var{DoSomething} will always be compiled:
  394. \begin{verbatim}
  395. {$Undefine MySymbol}
  396. {$ifndef Mysymbol}
  397. DoSomething;
  398. ...
  399. {$endif}
  400. \end{verbatim}
  401. You can combine the two alternatives in one structure, namely as follows
  402. \begin{verbatim}
  403. {$ifdef Mysymbol}
  404. DoSomething;
  405. {$else}
  406. DoSomethingElse
  407. {$endif}
  408. \end{verbatim}
  409. In this example, if \var{MySymbol} exists, then the call to \var{DoSomething}
  410. will be compiled. If it doesn't exist, the call to \var{DoSomethingElse} is
  411. compiled.
  412. The \fpk compiler defines some symbols before starting to compile your
  413. program or unit. You can use these symbols to differentiate between
  414. different versions of the compiler, and between different compilers.
  415. In \seet{Symbols}, a list of pre-defined symbols is given. In that table,
  416. you should change \var{v} with the version number of the compiler
  417. you're using, \var{r} with the release number and \var{p}
  418. with the patch-number of the compiler. 'OS' needs to be changed by the type
  419. of operating system. Currently this can be one of \var{DOS}, \var{GO32V2},
  420. \var{LINUX}, \var{OS2} or \var{WIN32}. This symbol is undefined if you
  421. specify a target that is different from the platform you're compiling on.
  422. the \var{-TSomeOS} option on the command line will define the \var{SomeOS} symbol,
  423. and will undefined the existing platform symbol\footnote{In versions prior to
  424. 0.9.4, this didn't happen, thus making Cross-compiling impossible.}.
  425. \begin{FPKltable}{c}{Symbols defined by the compiler.}{Symbols} \hline
  426. Free \\
  427. VER\var{v} \\
  428. VER\var{v}\_\var{r} \\
  429. VER\var{v}\_\var{r}\_\var{p} \\
  430. OS \\ \hline
  431. \end{FPKltable}
  432. As an example : Version 0.9.1 of the compiler, running on a Linux system,
  433. defines the following symbols before reading the command line arguments:
  434. \var{FPK}, \var{VER0}, \var{VER0\_9}, \var{VER0\_9\_1} and \var{LINUX}.
  435. Specifying \var{-TOS2} on the command-line will undefine the \var{LINUX}
  436. symbol, and will define the \var{OS2} symbol.
  437. {\em Remark: } Symbols, even when they're defined in the interface part of
  438. a unit, are not available outside that unit.
  439. \fpk supports the \var{\{\$IFOPT \}} directive for Turbo Pascal
  440. compatibility, but doesn't act on it. It always rejects the condition, so
  441. code between \var{\{\$IFOPT \}} and \var{\{\$Endif\}} is never compiled.
  442. Except for the Turbo Pascal constructs, from version 0.9.8 and higher,
  443. the \fpk compiler also supports a stronger conditional compile mechanism:
  444. The \var{\{\$If \}} construct.
  445. The prototype of this construct is as follows :
  446. \begin{verbatim}
  447. {$If expr}
  448. CompileTheseLines;
  449. {$else}
  450. BetterCompileTheseLines;
  451. {$endif}
  452. \end{verbatim}
  453. In this directive \var{expr} is a Pascal expression which is evaluated using
  454. strings, unless both parts of a comparision can be evaluated as numbers,
  455. in which case they are evaluated using numbers\footnote{Otherwise
  456. \var{\{\$If 8>54} would evaluate to \var{True}}.
  457. If the complemete expression evaluates to \var{'0'}, then it is considered
  458. false and rejected. Otherwise it is considered true and accepted. This may
  459. have unsexpected consequences :
  460. \begin{verbatim}
  461. {$If 0}
  462. \end{verbatim}
  463. Will evaluate to \var{False} and be rejected, while
  464. \begin{verbatim}
  465. {$If 00}
  466. \end{verbatim}
  467. Will evaluate to \var{True}.
  468. You can use any Pascal operator to construct your expression : \var{=, <>,
  469. >, <, >=, <=, AND, NOT, OR} and you can use round brackets to change the
  470. precedence of the operators.
  471. The following example shows you many of the possibilities:
  472. \begin{verbatim}
  473. {$ifdef fpk}
  474. var
  475. y : longint;
  476. {$else fpk}
  477. var
  478. z : longint;
  479. {$endif fpk}
  480. var
  481. x : longint;
  482. begin
  483. {$if (fpk_version=0) and (fpk_release>6) and (fpk_patch>4)}
  484. {$info At least this is version 0.9.5}
  485. {$else}
  486. {$fatalerror Problem with version check}
  487. {$endif}
  488. {$define x:=1234}
  489. {$if x=1234}
  490. {$info x=1234}
  491. {$else}
  492. {$fatalerror x should be 1234}
  493. {$endif}
  494. {$if 12asdf and 12asdf}
  495. {$info $if 12asdf and 12asdf is ok}
  496. {$else}
  497. {$fatalerror $if 12asdf and 12asdf rejected}
  498. {$endif}
  499. {$if 0 or 1}
  500. {$info $if 0 or 1 is ok}
  501. {$else}
  502. {$fatalerror $if 0 or 1 rejected}
  503. {$endif}
  504. {$if 0}
  505. {$fatalerror $if 0 accepted}
  506. {$else}
  507. {$info $if 0 is ok}
  508. {$endif}
  509. {$if 12=12}
  510. {$info $if 12=12 is ok}
  511. {$else}
  512. {$fatalerror $if 12=12 rejected}
  513. {$endif}
  514. {$if 12<>312}
  515. {$info $if 12<>312 is ok}
  516. {$else}
  517. {$fatalerror $if 12<>312 rejected}
  518. {$endif}
  519. {$if 12<=312}
  520. {$info $if 12<=312 is ok}
  521. {$else}
  522. {$fatalerror $if 12<=312 rejected}
  523. {$endif}
  524. {$if 121234>=312}
  525. {$info $if 121234>=312 is ok}
  526. {$else}
  527. {$fatalerror $if 121234>=312 rejected}
  528. {$endif}
  529. {$if 12<312}
  530. {$info $if 12<312 is ok}
  531. {$else}
  532. {$fatalerror $if 12<312 rejected}
  533. {$endif}
  534. {$if 122134>312}
  535. {$info $if 122134>312 is ok}
  536. {$else}
  537. {$fatalerror $if 122134>312 rejected}
  538. {$endif}
  539. {$if a12=a12}
  540. {$info $if a12=a12 is ok}
  541. {$else}
  542. {$fatalerror $if a12=a12 rejected}
  543. {$endif}
  544. {$if a12<>z312}
  545. {$info $if a12<>z312 is OK}
  546. {$else}
  547. {$fatalerror $if a12<>z312 rejected}
  548. {$endif}
  549. {$if a12<=z312}
  550. {$info $if a12<=z312 is ok}
  551. {$else}
  552. {$fatalerror $if a12<=z312 rejected}
  553. {$endif}
  554. {$if z121234>=a312}
  555. {$info $if z121234>=a312 is OK}
  556. {$else}
  557. {$fatalerror $if z121234>=a312 rejected}
  558. {$endif}
  559. {$if a12<z312}
  560. {$info $if a12<z312 is ok}
  561. {$else}
  562. {$fatalerror $if a12<z312 rejected}
  563. {$endif}
  564. {$if z122134>a312}
  565. {$info $if z122134>a312 is OK}
  566. {$else}
  567. {$fatalerror $if z122134>a312 rejected}
  568. {$endif}
  569. {$if not z122134>a312}
  570. {$fatalerror $if not z122134>a312 accepted}
  571. {$else}
  572. {$info $if not z122134>a312 is OK}
  573. {$endif}
  574. {$if not(0)}
  575. {$info $if not(0) is OK}
  576. {$else}
  577. {$fatalerror $if not(0) rejected}
  578. {$endif}
  579. {$info *************************************************}
  580. {$info * Now have to follow at least 2 error messages: *}
  581. {$info *************************************************}
  582. {$if not(0}
  583. {$endif}
  584. {$if not(<}
  585. {$endif}
  586. end.
  587. \end{verbatim}
  588. As you can see from the example, this construct isn't useful when used
  589. with normal symbols, but it is if you use macros, which are explained in
  590. \sees{Macros}, they can be very useful. When trying this example, you must
  591. switch on macro support, with the \var{-Sm} command-line switch.
  592. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  593. % Macros
  594. \section{Messages}
  595. \label{se:Messages}
  596. \fpk lets you define normal, warning and error messages in your code.
  597. Messages can be used to display useful information, such as copyright
  598. notices, a list of symbols that your code reacts on etc.
  599. Warnings can be used if you think some part of your code is still buggy, or
  600. if you think that a certain combination of symbols isn't useful. In general
  601. anything which may cause problems when compiling.
  602. Error messages can be useful if you need a certain symbol to be defined
  603. to warn that a certain variable isn't defined or so, or when the compiler
  604. version isn't suitable for your code.
  605. The compiler treats these messages as if they were generated by the
  606. compiler. This means that if you haven't turned on warning messages, the
  607. warning will not e displayed. Errors are always displayed, and the compiler
  608. stops as if an error had occurred.
  609. For messages, the syntax is as follows :
  610. \begin{verbatim}
  611. {$Message Message text }
  612. \end{verbatim}
  613. Or
  614. \begin{verbatim}
  615. {$Info Message text }
  616. \end{verbatim}
  617. For notes:
  618. \begin{verbatim}
  619. {$Note Message text }
  620. \end{verbatim}
  621. For warnings:
  622. \begin{verbatim}
  623. {$Warning Warning Message text }
  624. \end{verbatim}
  625. For errors :
  626. \begin{verbatim}
  627. {$Error Error Message text }
  628. \end{verbatim}
  629. Lastly, for fatal errors :
  630. \begin{verbatim}
  631. {$FatalError Error Message text }
  632. \end{verbatim}
  633. or
  634. \begin{verbatim}
  635. {$Stop Error Message text }
  636. \end{verbatim}
  637. The difference between \var{\$Error} and \var{\$FatalError} or \var{\$Stop}
  638. messages is that when the compiler encounters an error, it still continues
  639. to compile. With a fatal error, the compiler stops.
  640. {\em Remark :} You cannot use the '\var{\}}' character in your message, since
  641. this will be treated as the closing brace of the message.
  642. As an example, the following piece of code will generate an error when
  643. the symbol \var{RequiredVar} isn't defined:
  644. \begin{verbatim}
  645. {$ifndef RequiredVar}
  646. {$Error Requiredvar isn't defined !}
  647. {$endif}
  648. \end{verbatim}
  649. But the compiler will continue to compile. It will not, however, generate a
  650. unit file or a program (since an error occurred).
  651. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  652. % Macros
  653. \section{Macros}
  654. \label{se:Macros}
  655. Macros are very much like symbols in their syntax, the difference is that
  656. macros have a value whereas a symbol simply is defined or is not defined.
  657. If you want macro support, you need to specify the \var{-Sm} command-line
  658. switch, otherwise your macro will be regarded as a symbol.
  659. Defining a macro in your program is done in the same way as defining a symbol;
  660. in a \var{\{\$define \}} preprocessor statement\footnote{In compiler
  661. versions older than 0.9.8, the assignment operator for a macros wasn't
  662. \var{:=}, but \var{=}}:
  663. \begin{verbatim}
  664. {$define ident:=expr}
  665. \end{verbatim}
  666. If the compiler encounters \var{ident} in the rest of the source file, it
  667. will be replaced immediately by \var{expr}. This replacement works
  668. recursive, meaning that when the compiler expanded one of your macros, it
  669. will look at the resulting expression again to see if another replacement
  670. can be made. You need to be careful with this, because an infinite loop can
  671. occur in this manner.
  672. Here are two examples which illustrate the use of macros:
  673. \begin{verbatim}
  674. {$define sum:=a:=a+b;}
  675. ...
  676. sum { will be expanded to 'a:=a+b;'
  677. remark the absence of the semicolon}
  678. ...
  679. {$define b:=100}
  680. sum { Will be expanded recursively to a:=a+100; }
  681. ...
  682. \end{verbatim}
  683. The previous example could go wrong :
  684. \begin{verbatim}
  685. {$define sum:=a:=a+b;}
  686. ...
  687. sum { will be expanded to 'a:=a+b;'
  688. remark the absence of the semicolon}
  689. ...
  690. {$define b=sum} { DON'T do this !!!}
  691. sum { Will be infinitely recursively expanded... }
  692. ...
  693. \end{verbatim}
  694. On my system, the last example results in a heap error, causing the compiler
  695. to exit with a run-time error 203.
  696. {\em Remark: } Macros defined in the interface part of a unit are not
  697. available outside that unit ! They can just be used as a notational
  698. convenience, or in conditional compiles.
  699. By default, from version 0.9.8 of the compiler on, the compiler predefines three
  700. macros, containing the version number, the release number and the patch
  701. number. They are listed in \seet{DefMacros}.
  702. \begin{FPKltable}{ll}{Predefined macros}{DefMacros} \hline
  703. Symbol & Contains \\ \hline
  704. \var{FPK\_VERSION} & The version number of the compiler. \\
  705. \var{FPK\_RELEASE} & The release number of the compiler. \\
  706. \var{FPK\_PATCH} & The patch number of the compiler. \\
  707. \hline
  708. \end{FPKltable}
  709. {\em Remark: } Don't forget that macros support isn't on by default. You
  710. need to compile with the \var{-Sm} command-line switch.
  711. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  712. % Using assembly language
  713. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  714. \chapter{Using assembly language}
  715. \label{ch:AsmLang}
  716. \fpk supports inserting of assembler instructions in your code. The
  717. mechanism for this is the same as under Turbo Pascal. There are, however
  718. some substantial differences, as will be explained in the following.
  719. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  720. % AT&T syntax
  721. \section{AT\&T Syntax}
  722. \label{se:AttSyntax}
  723. \fpk uses the \gnu \var{as} assembler to generate its object files. Since
  724. the \gnu assembler uses AT\&T assembly syntax, the code you write should
  725. use the same syntax. The differences between AT\&T and Intel syntax as used
  726. in Turbo Pascal are summarized in the following:
  727. \begin{itemize}
  728. \item The opcode names include the size of the operand. In general, one can
  729. say that the AT\&T opcode name is the Intel opcode name, suffixed with a
  730. '\var{l}', '\var{w}' or '\var{b}' for, respectively, longint (32 bit),
  731. word (16 bit) and byte (8 bit) memory or register references. As an example,
  732. the Intel construct \mbox{'\var{mov al bl}} is equivalent to the AT\&T style '\var{movb
  733. \%bl,\%al}' instruction.
  734. \item AT\&T immediate operands are designated with '\$', while Intel syntax
  735. doesn't use a prefix for immediate operands. Thus the Intel construct
  736. '\var{mov ax, 2}' becomes '\var{movb \$2, \%al}' in AT\&T syntax.
  737. \item AT\&T register names are preceded by a '\var{\%}' sign.
  738. They are undelimited in Intel syntax.
  739. \item AT\&T indicates absolute jump/call operands with '\var{*}', Intel
  740. syntax doesn't delimit these addresses.
  741. \item The order of the source and destination operands are switched. AT\&T
  742. syntax uses '\var{Source, Dest}', while Intel syntax features '\var{Dest,
  743. Source}'. Thus the Intel construct '\var{add eax, 4}' transforms to
  744. '\var{addl \$4, \%eax}' in the AT\&T dialect.
  745. \item Immediate long jumps are prefixed with the '\var{l}' prefix. Thus the
  746. Intel '\var{call/jmp section:offset'} is transformed to '\var{lcall/ljmp
  747. \$section,\$offset}'. Similarly the far return is '\var{lret}', instead of the
  748. Intel '\var{ret far}'.
  749. \item Memory references are specified differently in AT\&T and Intel
  750. assembly. The Intel indirect memory reference
  751. \begin{quote}
  752. \var{Section:[Base + Index*Scale + Offs]}
  753. \end{quote}
  754. is written in AT\&T syntax as :
  755. \begin{quote}
  756. \var{Section:Offs(Base,Index,Scale)}
  757. \end{quote}
  758. Where \var{Base} and \var{Index} are optional 32-bit base and index
  759. registers, and \var{Scale} is used to multiply \var{Index}. It can take the
  760. values 1,2,4 and 8. The \var{Section} is used to specify an optional section
  761. register for the memory operand.
  762. \end{itemize}
  763. More information about the AT\&T syntax can be found in the \var{as} manual,
  764. although the following differences with normal AT\&T assembly must be taken
  765. into account :
  766. \begin{itemize}
  767. \item Only the following directives are presently supported:
  768. \begin{description}
  769. \item[.byte]
  770. \item[.word]
  771. \item[.long]
  772. \item[.ascii]
  773. \item[.asciz]
  774. \item[.globl]
  775. \end{description}
  776. \item The following directives are recognized but are not
  777. supported:
  778. \begin{description}
  779. \item[.align]
  780. \item[.lcomm]
  781. \end{description}
  782. Eventually they will be supported.
  783. \item Directives are case sensitive, other identifiers are not case sensitive.
  784. \item Contrary to GAS local labels/symbols {\em must} start with \var{.L}
  785. \item The nor operator \var{'!'} is not supported.
  786. \item String expressions in operands are not supported.
  787. \item Constant expressions which represent memory references are not
  788. allowed even though constant immediate value expressions are supported. \\
  789. examples:
  790. \begin{verbatim}
  791. const myid = 10;
  792. ...
  793. movl $myid,%eax -- allowed
  794. movl myid(%esi),%eax -- not allowed.
  795. \end{verbatim}
  796. \item When the \var{.globl} directive is found, the symbol following
  797. it is made public and is immediately emitted.
  798. Therefore label names with this name will be ignored.
  799. \item Only Single and Double FPU opcodes are supported.
  800. \end{itemize}
  801. The AT\&T inline assembler supports the following macros :
  802. \begin{description}
  803. \item [\_\_RESULT] represents the function result return value.
  804. \item [\_\_SELF] represents the object method pointer in methods.
  805. \item [\_\_OLDEBP] represents the old base pointer in recusrive routines.
  806. \end{description}
  807. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  808. % Intel syntax
  809. \section{Intel syntax}
  810. \label{se:Intel}
  811. As of version 0.9.7, \fpk supports Intel syntax in it's \var{asm} blocks.
  812. The Intel syntax in your \var{asm} block is converted to AT\&T syntax by the
  813. compiler, after which it is inserted in the compiled source.
  814. The supported assembler constructions are a subset of the normal assembly
  815. syntax. In what follows we specify what constructs are not supported in
  816. \fpk, but which exist in Turbo Pascal:
  817. \begin{itemize}
  818. \item The \var{TBYTE} qualifier is not supported.
  819. \item The \var{\&} identifier override is not supported.
  820. \item The \var{HIGH} operator is not supported.
  821. \item The \var{LOW} operator is not supported.
  822. \item The \var{OFFSET} and \var{SEG} operators are not supported.
  823. use \var{LEA} and the various \var{Lxx} instructions instead.
  824. \item Expressions with constant strings are not allowed.
  825. \item Access to record fields via parenthesis is not allowed
  826. \item Typecasts with normal pascal types are not allowed, only
  827. recognized assembler typecasts are allowed.\\ Example:
  828. \begin{verbatim}
  829. mov al, byte ptr MyWord -- allowed,
  830. mov al, byte(MyWord) -- allowed,
  831. mov al, shortint(MyWord) -- not allowed.
  832. \end{verbatim}
  833. \item Pascal type typecasts on constants are not allowed. \\
  834. Example:
  835. \begin{verbatim}
  836. const s= 10; const t = 32767;
  837. \end{verbatim}
  838. in Turbo Pascal:
  839. \begin{verbatim}
  840. mov al, byte(s) -- useless typecast.
  841. mov al, byte(t) -- syntax error!
  842. \end{verbatim}
  843. In this parser, either of those cases will give out a syntax error.
  844. \item Constant references expressions with constants only are not
  845. allowed (in all cases they do not work in protected mode,
  846. under linux i386). \\ Examples:
  847. \begin{verbatim}
  848. mov al,byte ptr ['c'] -- not allowed.
  849. mov al,byte ptr [100h] -- not allowed.
  850. \end{verbatim}
  851. (This is due to the limitation of Turbo Assembler).
  852. \item Brackets within brackets are not allowed
  853. \item Expressions with segment overrides fully in brackets are
  854. presently not supported, but they can easily be implemented
  855. in BuildReference if requested. \\ Example:
  856. \begin{verbatim}
  857. mov al,[ds:bx] -- not allowed
  858. \end{verbatim}
  859. use instead:
  860. \begin{verbatim}
  861. mov al,ds:[bx]
  862. \end{verbatim}
  863. \item Possible allowed indexing are as follows:
  864. \begin{itemize}
  865. \item \var{Sreg:[REG+REG*SCALING+/-disp]}
  866. \item \var{SReg:[REG+/-disp]}
  867. \item \var{SReg:[REG]}
  868. \item \var{SReg:[REG+REG+/-disp]}
  869. \item \var{SReg:[REG+REG*SCALING]}
  870. \end{itemize}
  871. Where \var{Sreg} is optional and specifies the segment override.
  872. {\em Notes:}
  873. \begin{enumerate}
  874. \item The order of terms is important contrary to Turbo Pascal.
  875. \item The Scaling value must be a value, and not an identifier
  876. to a symbol.\\ Examples:
  877. \begin{verbatim}
  878. const myscale = 1;
  879. ...
  880. mov al,byte ptr [esi+ebx*myscale] -- not allowed.
  881. \end{verbatim}
  882. use:
  883. \begin{verbatim}
  884. mov al, byte ptr [esi+ebx*1]
  885. \end{verbatim}
  886. \end{enumerate}
  887. \item Possible variable identifier syntax is as follows:
  888. (Id = Variable or typed constant identifier.)
  889. \begin{enumerate}
  890. \item \var{ID}
  891. \item \var{[ID]}
  892. \item \var{[ID+expr]}
  893. \item \var{ID[expr]}
  894. \end{enumerate}
  895. Possible fields are as follow:
  896. \begin{enumerate}
  897. \item \var{ID.subfield.subfield ...}
  898. \item \var{[ref].ID.subfield.subfield ...}
  899. \item \var{[ref].typename.subfield ...}
  900. \end{enumerate}
  901. \item Local Labels: Contrary to Turbo Pascal, local labels, must
  902. at least contain one character after the local symbol indicator.\\
  903. Example:
  904. \begin{verbatim}
  905. @: -- not allowed
  906. \end{verbatim}
  907. use instead, for example:
  908. \begin{verbatim}
  909. @1: -- allowed
  910. \end{verbatim}
  911. \item Contrary to Turbo Pascal local references cannot be used as references,
  912. only as displacements. \\ example:
  913. \begin{verbatim}
  914. lds si,@mylabel -- not allowed
  915. \end{verbatim}
  916. \item Contrary to Turbo Pascal, \var{SEGCS}, \var{SEGDS}, \var{SEGES} and
  917. \var{SEGSS} segment overrides are presently not supported.
  918. (This is a planned addition though).
  919. \item Contrary to Turbo Pascal where memory sizes specifiers can
  920. be practically anywhere, the \fpk Intel inline assembler requires
  921. memory size specifiers to be outside the brackets. \\
  922. example:
  923. \begin{verbatim}
  924. mov al,[byte ptr myvar] -- not allowed.
  925. \end{verbatim}
  926. use:
  927. \begin{verbatim}
  928. mov al,byte ptr [myvar] -- allowed.
  929. \end{verbatim}
  930. \item Base and Index registers must be 32-bit registers.
  931. (limitation of the GNU Assembler).
  932. \item \var{XLAT} is equivalent to \var{XLATB}.
  933. \item Only Single and Double FPU opcodes are supported.
  934. \item Floating point opcodes are currently not supported
  935. (except those which involve only floating point registers).
  936. \end{itemize}
  937. The Intel inline assembler supports the following macros :
  938. \begin{description}
  939. \item [@Result] represents the function result return value.
  940. \item [Self] represents the object method pointer in methods.
  941. \end{description}
  942. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  943. % Calling mechanism
  944. \section{Calling mechanism}
  945. \label{se:Calling}
  946. Procedures and Functions are called with their parameters on the stack.
  947. Contrary to Turbo Pascal, {\em all} parameters are pushed on the stack, and
  948. they are pushed {\em right} to {\em left}, instead of left to right for
  949. Turbo Pascal. This is especially important if you have some assembly
  950. subroutines in Turbo Pascal which you would like to translate to \fpk.
  951. Function results are returned in the first register, if they fit in the
  952. register. For more information on this, see \sees{Stack}
  953. The registers are {\em not} saved when calling a function or procedure. If
  954. you want to call a procedure or function from assembly language, you must
  955. save any registers you wish to preserve.
  956. The first thing a procedure does is saving the base pointer, and setting the
  957. base (\var{\%ebp}) pointer equal to the stack pointer (\var{\%esp}).
  958. References to the pushed parameters and local variables are constructed
  959. using the base pointer.
  960. In practice this amounts to the following assembly code as the procedure or
  961. function header :
  962. \begin{verbatim}
  963. pushl %ebp
  964. movl %esp,%ebp
  965. \end{verbatim}
  966. When the procedure or function exits, it clears the stack by means of the
  967. \var{RET xx} call, where \var{xx} is the total size of the pushed parameters
  968. on the stack. Thus, in case parameters with a total size of \var{xx} have
  969. been passed to a function, the generated exit sequence looks as follows:
  970. \begin{verbatim}
  971. leave
  972. ret $xx
  973. \end{verbatim}
  974. When you want your code to be called by a C library or used in a C
  975. program, you will run into trouble because of this calling mechanism. In C,
  976. the calling procedure is expected to clear the stack, not the called
  977. procedure. To avoid this problem, \fpk supports the \var{export} modifier.
  978. Procedures that are defined using the export modifier, use a C-compatible
  979. calling mechanism. This means that they can be called from a C program or
  980. library, or that you can use them as a callback function.
  981. This also means that you cannot call this procedure or function from your
  982. own program, since your program uses the Pascal calling convention.
  983. However, in the exported function, you can of course call other Pascal
  984. routines.
  985. Technically, the C calling mechanism is implemented by generating the
  986. following exit sequence at the end of your function or procedure:
  987. \begin{verbatim}
  988. leave {Copies EBP to ESP, pops EBP from the stack.}
  989. ret
  990. \end{verbatim}
  991. Comparing this exit sequence with the previous one makes it clear why you
  992. cannot call this procedure from within Pascal: The arguments still are on
  993. the stack when the procedure exits.
  994. As of version 0.9.8, the \fpk compiler supports also the \var{cdecl} and
  995. \var{stdcall} modifiers, as found in Delphi. The \var{cdecl} modifier does
  996. the same as the \var{export} modifier, and \var{stdcall} does nothing, since
  997. \fpk pushes the paramaters from right to left by default.
  998. All this is summarized in \seet{Calling}. The first column lists the
  999. modifier you specify for a procedure declaration. The second one lists the
  1000. order the paramaters are pushed on the stack. The third column specifies who
  1001. is responsible for cleaning the stack: the caller or the called function.
  1002. Finally, the last column specifies if registers are used to pass parameters
  1003. to the function.
  1004. \begin{FPKltable}{llll}{Calling mechanisms in \fpk}{Calling}\hline
  1005. Modifier & Pushing order & Stack cleaned by & Parameters in registers \\
  1006. \hline
  1007. (none) & Right-to-left & Function & No \\
  1008. cdecl & Right-to-left & Caller & No \\
  1009. export & Right-to-left & Caller & No \\
  1010. stdcall & Right-to-left & Function & No \\ \hline
  1011. \end{FPKltable}
  1012. More about this can be found in \seec{Linking} on linking.
  1013. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1014. % Telling the compiler what registers have changed
  1015. \section{Telling the compiler what registers have changed}
  1016. \label{se:RegChanges}
  1017. When the compiler uses variables, it sometimes stores them, or the result of
  1018. some calculations, in the processor registers. If you insert assembler code
  1019. in your program that modifies the processor registers, then this may
  1020. interfere with the compiler's idea about the registers. To avoid this
  1021. problem, \fpk allows you to tell the compiler which registers have changed.
  1022. The compiler will then avoid using these registers. Telling the compiler
  1023. which registers have changed, is done by specifying a set of register names
  1024. behind an assembly block, as follows:
  1025. \begin{verbatim}
  1026. asm
  1027. ...
  1028. end ['R1',...,'Rn'];
  1029. \end{verbatim}
  1030. Here \var{R1} to \var{Rn} are the names of the (extended) registers you
  1031. modify in your assembly code. They can be one of \var{'EAX', 'EBX', 'ECX',
  1032. 'EDX', 'EDI', 'ESI'} for the Intel processor.
  1033. As an example :
  1034. \begin{verbatim}
  1035. asm
  1036. movl BP,%eax
  1037. movl 4(%eax),%eax
  1038. movl %eax,__RESULT
  1039. end ['EAX'];
  1040. \end{verbatim}
  1041. This example tells the compiler that the \var{EAX} register was modified.
  1042. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1043. % Linking issues
  1044. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1045. \chapter{Linking issues}
  1046. \label{ch:Linking}
  1047. When you only use Pascal code, and Pascal units, then you will not see much
  1048. of the part that the linker plays in creating your executable.
  1049. The linker is only called when you compile a program. When compiling units,
  1050. the linker isn't invoked.
  1051. However, there are times that you want to C libraries, or to external
  1052. object files that are generated using a C compiler (or even another pascal
  1053. compiler). The \fpk compiler can generate calls to a C function,
  1054. and can generate functions that can be called from C (exported functions).
  1055. However, these exported functions cannot be called from
  1056. inside Pascal anymore. More on these calling conventions can be found in
  1057. \sees{Calling}.
  1058. In general, there are 2 things you must do to use a function that resides in
  1059. an external library or object file:
  1060. \begin{enumerate}
  1061. \item You must make a pascal declaration of the function or procedure you
  1062. want to use.
  1063. \item You must tell the compiler where the function resides, i.e. in what
  1064. object file or what library, so the compiler can link the necessary code in.
  1065. \end{enumerate}
  1066. The following sections attempt to explain how to do this.
  1067. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1068. % Declaring an external function or procedure
  1069. \section{Declaring an external function or procedure}
  1070. \label{se:ExternalDeclaration}
  1071. The first step in using external code blocks is declaring the function you
  1072. want to use. \fpk supports Delphi syntax, i.e. you must use the
  1073. \var{external} directive.
  1074. There exist four variants of the external direcive :
  1075. \begin{enumerate}
  1076. \item A simple external declaration:
  1077. \begin{verbatim}
  1078. Procedure ProcName (Args : TPRocArgs); external;
  1079. \end{verbatim}
  1080. The \var{external} directive tells the compiler that the function resides in
  1081. an external block of code. You can use this together with the \var{\{\$L \}}
  1082. or \var{\{\$LinkLib \}} directives to link to a function or procedure in a
  1083. library or external object file.
  1084. \item You can give the \var{external} directive a library name as an
  1085. argument:
  1086. \begin{verbatim}
  1087. Procedure ProcName (Args : TPRocArgs); external 'Name';
  1088. \end{verbatim}
  1089. This tells the compiler that the procedure resides in a library with name
  1090. \var{'Name'}. This method is equivalent to the following:
  1091. \begin{verbatim}
  1092. Procedure ProcName (Args : TPRocArgs);external;
  1093. {$LinkLib 'Name'}
  1094. \end{verbatim}
  1095. \item The \var{external} can also be used with two arguments:
  1096. \begin{verbatim}
  1097. Procedure ProcName (Args : TPRocArgs); external 'Name'
  1098. name 'OtherProcName';
  1099. \end{verbatim}
  1100. This has the same meaning as the previous declaration, only the compiler
  1101. will use the name \var{'OtherProcName'} when linking to the library. This
  1102. can be used to give different names to procedures and functions in an
  1103. external library.
  1104. This method is equivalent to the following code:
  1105. \begin{verbatim}
  1106. Procedure OtherProcName (Args : TProcArgs); external;
  1107. {$LinkLib 'Name'}
  1108. Procedure ProcName (Args : TPRocArgs);
  1109. begin
  1110. OtherProcName (Args);
  1111. end;
  1112. \end{verbatim}
  1113. \item Lastly, onder \windows and \ostwo, there is a fourth possibility
  1114. to specify an external function: In \file{.DLL} files, functionas also have
  1115. a unique number (their index). It is possible to refer to these fuctions
  1116. using their index:
  1117. \begin{verbatim}
  1118. Procedure ProcName (Args : TPRocArgs); external 'Name' Index SomeIndex;
  1119. \end{verbatim}
  1120. This tells the compiler that the procedure \var{ProcName} resides in a
  1121. dynamic link library, with index {SomeIndex}.
  1122. \em{Remark:} Note that this is ONLY available under \windows and \ostwo.
  1123. \end{enumerate}
  1124. In earlier versions of the \fpk compiler, the following construct was
  1125. also possible :
  1126. \begin{verbatim}
  1127. Procedure ProcName (Args : TPRocArgs); [ C ];
  1128. \end{verbatim}
  1129. This method is equivalent to the following statement:
  1130. \begin{verbatim}
  1131. Procedure ProcName (Args : TPRocArgs); cdecl; external;
  1132. \end{verbatim}
  1133. However, the \var{[ C ]} directive is deprecated, and may no longer be
  1134. supported in future versions of \fpk, therefore you should use the
  1135. \var{external} directive, with the \var{cdecl} directive, if needed.
  1136. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1137. % Linking an object file in your program
  1138. \section{Explicitly linking an object file in your program}
  1139. \label{se:LinkIn}
  1140. Having declared the external function that resides in an object file,
  1141. you can use it as if it was defined in your own program or unit.
  1142. To produce an executable, you must still link the object file in.
  1143. This can be done with the \var{\{\$L 'file.o'\}} directive.
  1144. This will cause the linker to link in the object file \file{file.o}. On
  1145. \linux systems, this filename is case sensitive. Under \dos, case isn't
  1146. important. Note that \var{file.o} must be in the current directory if you
  1147. don't specify a path. The linker will not search for \file{file.o} if it
  1148. isn't found.
  1149. You cannot specify libraries in this way, it is for object files only.
  1150. Here we present an example. Consider that you have some assembly routine that
  1151. calculates the nth Fibonacci number :
  1152. \begin{verbatim}
  1153. .text
  1154. .align 4
  1155. .globl Fibonacci
  1156. .type Fibonacci,@function
  1157. Fibonacci:
  1158. pushl %ebp
  1159. movl %esp,%ebp
  1160. movl 8(%ebp),%edx
  1161. xorl %ecx,%ecx
  1162. xorl %eax,%eax
  1163. movl $1,%ebx
  1164. incl %edx
  1165. loop:
  1166. decl %edx
  1167. je endloop
  1168. movl %ecx,%eax
  1169. addl %ebx,%eax
  1170. movl %ebx,%ecx
  1171. movl %eax,%ebx
  1172. jmp loop
  1173. endloop:
  1174. movl %ebp,%esp
  1175. popl %ebp
  1176. ret
  1177. \end{verbatim}
  1178. Then you can call this function with the following Pascal Program:
  1179. \begin{verbatim}
  1180. Program FibonacciDemo;
  1181. var i : longint;
  1182. Function Fibonacci (L : longint):longint;cdecl;external;
  1183. {$L fib.o}
  1184. begin
  1185. For I:=1 to 40 do
  1186. writeln ('Fib(',i,') : ',Fibonacci (i));
  1187. end.
  1188. \end{verbatim}
  1189. With just two commands, this can be made into a program :
  1190. \begin{verbatim}
  1191. as -o fib.o fib.s
  1192. pp fibo.pp
  1193. \end{verbatim}
  1194. This example supposes that you have your assembler routine in \file{fib.s},
  1195. and your Pascal program in \file{fibo.pp}.
  1196. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1197. % Linking your program to a library
  1198. \section{Linking your program to a library}
  1199. \label{se:LinkOut}
  1200. To link your program to a library, the procedure depends on how you declared
  1201. the external procedure. If you used thediffers a little from the
  1202. procedure when you link in an object file. although the declaration step
  1203. remains the same (see \ref{se:ExternalDeclaration} on how to do that).
  1204. In case you used the follwing syntax to declare your procedure:
  1205. \begin{verbatim}
  1206. Procedure ProcName (Args : TPRocArgs); external 'Name';
  1207. \end{verbatim}
  1208. You don't need to take additional steps to link your file in, the compiler
  1209. will do all that is needed for you.
  1210. In case you used
  1211. \begin{verbatim}
  1212. Procedure ProcName (Args : TPRocArgs); external;
  1213. \end{verbatim}
  1214. Or the older method
  1215. \begin{verbatim}
  1216. Procedure ProcName (Args : TPRocArgs); [ C ];
  1217. \end{verbatim}
  1218. You still need to explicity link to the library. This can be done in 2 ways:
  1219. \begin{enumerate}
  1220. \item You can tell the compiler in the source file what library to link to
  1221. using the \var{\{\$LinkLib 'Name'} directive:
  1222. \begin{verbatim}
  1223. {$LinkLib 'gpm'}
  1224. \end{verbatim}
  1225. This will link to the \file{gpm} library. On \linux systems, you needn't
  1226. specify the extension or 'lib' prefix of the library. The compiler takes
  1227. care of that. On \dos or \windows systems, you need to specify the full
  1228. name.
  1229. \item You can also tell the compiler on the command-line to link in a
  1230. library: The \var{-k} option can be used for that. For example
  1231. \begin{verbatim}
  1232. ppc386 -k'-lgpm' myprog.pp
  1233. \end{verbatim}
  1234. Is equivalent to the above method, and tells the linker to link to the
  1235. \file{gpm} library.
  1236. \end{enumerate}
  1237. As an example; consider the following program :
  1238. \begin{verbatim}
  1239. program printlength;
  1240. { Declaration for the standard C function strlen }
  1241. Function strlen (P : pchar) : longint; cdecl;external;
  1242. begin
  1243. Writeln (strlen('Programming is easy !'));
  1244. end.
  1245. \end{verbatim}
  1246. This program can be compiled with :
  1247. \begin{verbatim}
  1248. pp -k'-lc' prlen.pp
  1249. \end{verbatim}
  1250. Supposing, of course, that the program source resides in \file{prlen.pp}.
  1251. You cannot use procedures or functions that have a variable number of
  1252. arguments in C. Pascal doesn't support this feature of C.
  1253. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1254. % Making a shared library
  1255. \section{Making a shared library}
  1256. \label{se:SharedLib}
  1257. If you want to make your procedures and functions available to C
  1258. programmers, you can do this very easily. All you need to do is declare the
  1259. functions and procedures that you want to make available as \var{Export}, as
  1260. follows:
  1261. \begin{verbatim}
  1262. Procedure ExportedProcedure ; export;
  1263. \end{verbatim}
  1264. This tells the compiler that it shouldn't clear the stack upon exiting the
  1265. procedure (see \sees{Calling}), thus enabling a C program to call your
  1266. function. It also means that your Pascal program can't call this function,
  1267. since it will be using the C calling mechanism.
  1268. {\em Remark :} You can only declare a function as exported in the
  1269. \var{Implementation} section of a unit. This function may {\em not} appear
  1270. in the interface part of a unit. This is logical, since a Pascal routine
  1271. cannot call an exported function, anyway.
  1272. However, the generated object file will not contain the name of the function
  1273. as you declared it. The \fpk compiler ''mangles'' the name you give your
  1274. function. It makes the name all-uppercase, and adds the types of all
  1275. parameters to it. For \fpk units, this doesn't matter, since the \file{.ppu}
  1276. unit file contains all information to map your function declaration onto the
  1277. mangled name in the object file. For a C programmer, who has no access to
  1278. the \var{.ppu} file, this is not very convenient. That is why \fpk
  1279. has the \var{Alias} modifier. The \var{Alias} modifier allows you to specify
  1280. another name (a nickname) for your function or procedure.
  1281. The prototype for an aliased function or procedure is as follows :
  1282. \begin{verbatim}
  1283. Procedure AliasedProc; [ Alias : 'AliasName'];
  1284. \end{verbatim}
  1285. The procedure \var{AliasedProc} will also be known as \var{AliasName}. Take
  1286. care, the name you specify is case sensitive (as C is).
  1287. Of course, you want to combine these two features of \fpk, to export a
  1288. function under a reasonable name; If you want to do that, you must first
  1289. specify that the function is to be exported, and then only declare an alias:
  1290. \begin{verbatim}
  1291. Procedure ExportToCProc; Export; [Alias : 'procname'];
  1292. \end{verbatim}
  1293. After that, any C program will be able to use your procedure or function.
  1294. {\em Remark: }
  1295. If you use in your unit functions that are in other units, or
  1296. system functions, then the C program will need to link in the object files
  1297. from the units too.
  1298. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1299. % Objects
  1300. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1301. \chapter{Objects}
  1302. \label{ch:Objects}
  1303. In this short chapter we give some technical things about objects. For
  1304. instructions on how to use and declare objects, see \refref.
  1305. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1306. % Constructor and Destructor calls.
  1307. \section{Constructor and Destructor calls}
  1308. \label{se:ConsDest}
  1309. When using objects that need virtual methods, the compiler uses two help
  1310. procedures that are in the run-time library. They are called
  1311. \var{Help\_Destructor} and \var{Help\_Constructor}, and they are written in
  1312. assebly language. They are used to allocate the necessary memory if needed,
  1313. and to insert the Virtual Method Table (VMT) pointer in the newly allocated
  1314. object.
  1315. When the compiler encounters a call to an object's constructor,
  1316. it sets up the stack frame for the call, and inserts a call to the
  1317. \var{Help\_Constructor}
  1318. procedure before issuing the call to the real constuctor.
  1319. The helper procedure allocates the needed memory (if needed) and inserts the
  1320. VMT pointer in the object. After that, the real constructor is called.
  1321. A call to \var{Help\_Destructor} is inserted in every destructor declaration,
  1322. just before the destructor's exit sequence.
  1323. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1324. % memory storage of Objects
  1325. \section{Memory storage of objects}
  1326. \label{se:ObjMemory}
  1327. Objects are stored in memory just as ordinary records with an extra field :
  1328. a pointer to the Virtual Method Table (VMT). This field is stored first, and
  1329. all fields in the object are stored in the order they are declared.
  1330. This field is initialized by the call to the object's \var{Constructor} method.
  1331. If the object you defined has no virtual methods, then a \var{nil} is stored
  1332. in the VMT pointer. This ensures that the size of objects is equal, whether
  1333. they have virtual methods ore not.
  1334. The memory allocated looks as in \seet{ObjMem}.
  1335. \begin{FPKltable}{ll}{Object memory layout}{ObjMem} \hline
  1336. Offset & What \\ \hline
  1337. +0 & Pointer to VMT. \\
  1338. +4 & Data. All fields in the order the've been declared. \\
  1339. ... & \\
  1340. \hline
  1341. \end{FPKltable}
  1342. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1343. % The virtual method table.
  1344. \section{The Virtual Method Table}
  1345. \label{se:VMT}
  1346. The Virtual Method Table (VMT) for each object type consists of 2 check
  1347. fields (containing the size of the data), a pointer to the object's anchestor's
  1348. VMT (\var{Nil} if there is no anchestor), and then the pointers to all virtual
  1349. methods. The VMT layout is illustrated in \seet{VMTMem}.
  1350. The VMT is constructed by the compiler. Every instance of an object receives
  1351. a pointer to its VMT.
  1352. \begin{FPKltable}{ll}{Virtual Method Table memory layout}{VMTMem} \hline
  1353. Offset & What \\ \hline
  1354. +0 & Size of object type data \\
  1355. +4 & Minus the size of object type data. Enables determining of valid VMT
  1356. pointers. \\
  1357. +8 & Pointer to ancestor VMT, \var{Nil} if no ancestor available.\\
  1358. +12 & Pointers to the virtual methods. \\
  1359. ... & \\
  1360. \hline
  1361. \end{FPKltable}
  1362. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1363. % Generated code
  1364. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1365. \chapter{Generated code}
  1366. \label{ch:GenCode}
  1367. The \fpk compiler relies on the assembler to make object files. It generates
  1368. just the assembly language file. In the following two sections, we discuss
  1369. what is generated when you compile a unit or a program.
  1370. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1371. % Units
  1372. \section{Units}
  1373. \label{se:Units}
  1374. When you compile a unit, the \fpk compiler generates 2 files :
  1375. \begin{enumerate}
  1376. \item A unit description file (with extension \file{.ppu}).
  1377. \item An assembly language file (with extension \file{.s}).
  1378. \end{enumerate}
  1379. The assembly language file contains the actual source code for the
  1380. statements in your unit, and the necessary memory allocations for any
  1381. variables you use in your unit. This file is converted by the assembler to
  1382. an object file (with extension \file{.o}) which can then be linked to other
  1383. units and your program, to form an executable.
  1384. By default (compiler version 0.9.4 and up), the assembly file is removed
  1385. after it has been compiled. Only in the case of the \var{-s} command-line
  1386. option, the assembly file must be left on disk, so the assembler can be
  1387. called later.
  1388. The unit file contains all the information the compiler needs to use the
  1389. unit:
  1390. \begin{enumerate}
  1391. \item Other used units, both in interface and implementation.
  1392. \item Types and variables from the interface section of the unit.
  1393. \item Function declarations from the interface section of the unit.
  1394. \item Some debugging information, when compiled with debugging.
  1395. \item A date and time stamp.
  1396. \end{enumerate}
  1397. Macros, symbols and compiler directives are {\em not} saved to the unit
  1398. description file. Aliases for functions are also not written to this file,
  1399. which is logical, since they cannot appear in the interface section of a
  1400. unit.
  1401. The detailed contents and structure of this file are described in the first
  1402. appendix. You can examine a unit description file using the \file{dumpppu}
  1403. program, which shows the contents of the file.
  1404. If you want to distribute a unit without source code, you must provide both
  1405. the unit description file and the object file.
  1406. You can also provide a C header file to go with the object file. In that
  1407. case, your unit can be used by someone who wishes to write his programs in
  1408. C. However, you must make this header file yourself since the \fpk compiler
  1409. doesn't make one for you.
  1410. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1411. % Programs
  1412. \section{Programs}
  1413. \label{se:Programs}
  1414. When you compile a program, the compiler produces again 2 files :
  1415. \begin{enumerate}
  1416. \item An assembly language file containing the statements of your program,
  1417. and memory allocations for all used variables.
  1418. \item A linker response file. This file contains a list of object files the
  1419. linker must link together.
  1420. \end{enumerate}
  1421. The link response file is, by default, removed from the disk. Only when you
  1422. specify the \var{-s} command-line option or when linking fails, then the ile
  1423. is left on the disk. It is named \file{link.res}.
  1424. The assembly language file is converted to an object file by the assembler,
  1425. and then linked together with the rest of the units and a program header, to
  1426. form your final program.
  1427. The program header file is a small assembly program which provides the entry
  1428. point for the program. This is where the execution of your program starts,
  1429. so it depends on the operating system, because operating systems pass
  1430. parameters to executables in wildly different ways.
  1431. It's name is \file{prt0.o}, and the
  1432. source file resides in \file{prt0.s} or some variant of this name. It
  1433. usually resided where the system unit source for your system resides.
  1434. It's main function is to save the environment and command-line arguments,
  1435. set up the stack. Then it calls the main program.
  1436. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1437. % MMX Support
  1438. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1439. \chapter{MMX support}
  1440. \label{ch:MMXSupport}
  1441. \section{What is it about ?}
  1442. \label{se:WhatisMMXabout}
  1443. \fpk supports the new MMX (Multi-Media extensions)
  1444. instructions of Intel processors. The idea of MMX is to
  1445. process multiple data with one instruction, for example the processor
  1446. can add simultaneously 4 words. To implement this efficiently, the
  1447. Pascal language needs to be extended. So Free Pascal allows
  1448. to add for example two \var{array[0..3] of word},
  1449. if MMX support is switched on. The operation is done
  1450. by the \var{MMX} unit and allows people without assembler knowledge to take
  1451. advantage of the MMX extensions.
  1452. Here is an example:
  1453. \begin{verbatim}
  1454. uses
  1455. MMX; { include some predefined data types }
  1456. const
  1457. { tmmxword = array[0..3] of word;, declared by unit MMX }
  1458. w1 : tmmxword = (111,123,432,4356);
  1459. w2 : tmmxword = (4213,63456,756,4);
  1460. var
  1461. w3 : tmmxword;
  1462. l : longint;
  1463. begin
  1464. if is_mmx_cpu then { is_mmx_cpu is exported from unit mmx }
  1465. begin
  1466. {$mmx+} { turn mmx on }
  1467. w3:=w1+w2;
  1468. {$mmx-}
  1469. end
  1470. else
  1471. begin
  1472. for i:=0 to 3 do
  1473. w3[i]:=w1[i]+w2[i];
  1474. end;
  1475. end.
  1476. \end{verbatim}
  1477. \section{Saturation support}
  1478. \label{se:SaturationSupport}
  1479. One important point of MMX is the support of saturated operations.
  1480. If a operation would cause an overflow, the value stays at the
  1481. highest or lowest possible value for the data type:
  1482. If you use byte values you get normally 250+12=6. This is very
  1483. annoying when doing color manipulations or changing audio samples,
  1484. when you have to do a word add and check if the value is greater than
  1485. 255. The solution is saturation: 250+12 gives 255.
  1486. Saturated operations are supported by the \var{MMX} unit. If you
  1487. want to use them, you have simple turn the switch saturation on:
  1488. \var{\$saturation+}
  1489. Here is an example:
  1490. \begin{verbatim}
  1491. \end{verbatim}
  1492. \section{Restrictions of MMX support}
  1493. \label{se:MMXrestrictions}
  1494. In the beginning of 1997 the MMX instructions were introduced in the
  1495. Pentium processors, so multitasking systems wouldn't save the
  1496. newly introduced MMX registers. To work around that problem, Intel
  1497. mapped the MMX registers to the FPU register.
  1498. The consequence is that
  1499. you can't mix MMX and floating point operations. After using
  1500. MMX operations and before using floating point operations, you
  1501. have to call the routine \var{EMMS} of the \var{MMX} unit.
  1502. This routine restores the FPU registers.
  1503. \em{Careful:} The compiler doesn't warn, if you mix floating point and
  1504. MMX operations, so be careful.
  1505. The MMX instructions are optimized for multi media (what else?).
  1506. So it isn't possible to perform each operation, some opertions
  1507. give a type mismatch, see section \ref {se:SupportedMMX} for the supported
  1508. MMX operations
  1509. An important restriction is that MMX operations aren't range or overflow
  1510. checked, even when you turn range and overflow checking on. This is due to
  1511. the nature of MMX operations.
  1512. The \var{MMX} unit must be always used when doing MMX operations
  1513. because the exit code of this unit clears the MMX unit. If it wouldn't do
  1514. that, other program will crash. A consequence of this is that you can't use
  1515. MMX operations in the exit code of your units or programs, since they would
  1516. interfere with the exit code of the \var{MMX} unit. The compiler can't
  1517. check this, so you are responsible for this !
  1518. \section{Supported MMX operations}
  1519. \label{se:SupportedMMX}
  1520. \section{Optimizing MMX support}
  1521. \label{se:OptimizingMMX}
  1522. Here are some helpful hints to get optimal performance:
  1523. \begin{itemize}
  1524. \item The \var{EMMS} call takes a lot of time, so try to seperate floating
  1525. point and MMX operations.
  1526. \item Use MMX only in low level routines because the compiler
  1527. saves all used MMX registers when calling a subroutine.
  1528. \item The NOT-operator isn't supported natively by MMX, so the
  1529. compiler has to generate a workaround and this operation
  1530. is inefficient.
  1531. \item Simple assignements of floating point numbers don't access
  1532. floating point registers, so you need no call to the \var{EMMS}
  1533. procedure. Only when doing arithmetic, you need to call the \var{EMMS}
  1534. procedure.
  1535. \end{itemize}
  1536. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1537. % Memory issues
  1538. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1539. \chapter{Memory issues}
  1540. \label{ch:Memory}
  1541. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1542. % The 32-bit model
  1543. \section{The 32-bit model.}
  1544. \label{se:ThirtytwoBit}
  1545. The \fpk Pascal compiler issues 32-bit code. This has several consequences:
  1546. \begin{itemize}
  1547. \item You need a i386 or higher processor to run the generated code. The
  1548. compiler functions on a 286 when you compile it using Turbo Pascal,
  1549. but the generated programs cannot be assembled or executed.
  1550. \item You don't need to bother with segment selectors. Memory can be
  1551. addressed using a single 32-bit pointer.
  1552. The amount of memory is limited only by the available amount of (virtual)
  1553. memory on your machine.
  1554. \item The structures you define are unlimited in size. Arrays can be as long
  1555. as you want. You can request memory blocks from any size.
  1556. \end{itemize}
  1557. The fact that 32-bit code is used, means that some of the older Turbo Pascal
  1558. constructs and functions are obsolete. The following is a list of functions
  1559. which shouldn't be used anymore:
  1560. \begin{description}
  1561. \item [Seg()] : Returned the segment of a memory address. Since segments have
  1562. no more meaning, zero is returned in the \fpk run-time library implementation of
  1563. \var{Seg}.
  1564. \item [Ofs()] : Returned the offset of a memory address. Since segments have
  1565. no more meaning, the complete address is returned in the \fpk implementation
  1566. of this function. This has as a consequence that the return type is
  1567. \var{Longint} instead of \var{Word}.
  1568. \item [Cseg(), Dseg()] : Returned, respectively, the code and data segments
  1569. of your program. This returns zero in the \fpk implementation of the
  1570. system unit, since both code and data are in the same memory space.
  1571. \item [Ptr] accepted a segment and offset from an address, and would return
  1572. a pointer to this address. This has been changed in the run-time library.
  1573. Standard it returns now simply the offset. If you want to retain the old
  1574. functionality, you can recompile the run-time library with the
  1575. \var{DoMapping} symbol defined. This will restore the Turbo Pascal
  1576. behaviour.
  1577. \item [memw and mem] these arrays gave access to the \dos memory. \fpk
  1578. supports them, but they are mapped onto 32-bit flat memory space.
  1579. \end{description}
  1580. You shouldn't use these functions, since they are very non-portable, they're
  1581. specific to \dos and the ix86 processor. The \fpk compiler is designed to be
  1582. portable to other platforms, so you should keep your code as portable as
  1583. possible, and not system specific. That is, unless you're writing some driver
  1584. units, of course.
  1585. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1586. % The stack
  1587. \section{The stack}
  1588. \label{se:Stack}
  1589. The stack is used to pass parameters to procedures or functions,
  1590. to store local variables, and, in some cases, to return function
  1591. results.
  1592. When a function or procedure is called, then the following is done by the
  1593. compiler :
  1594. \begin{enumerate}
  1595. \item If there are any parameters to be passed to the procedure, they are
  1596. pushed from right to left on the stack.
  1597. \item If a function is called that returns a variable of type \var{String},
  1598. \var{Set}, \var{Record}, \var{Object} or \var{Array}, then an address to
  1599. store the function result in, is pushed on the stack.
  1600. \item If the called procedure or function is an object method, then the
  1601. pointer to \var{self} is pushed on the stack.
  1602. \item If the procedure or function is nested in another function or
  1603. procedure, then the frame pointer of the parent procedure is pushed on the
  1604. stack.
  1605. \item The return address is pushed on the stack (by the \var{Call}
  1606. instruction).
  1607. \end{enumerate}
  1608. The resulting stack frame upon entering looks as in \seet{StackFrame}.
  1609. \begin{FPKltable}{llc}{Stack frame when calling a procedure}{StackFrame}
  1610. \hline
  1611. Offset & What is stored & Optional ? \\ \hline
  1612. +x & parameters & Yes \\
  1613. +12 & function result & Yes \\
  1614. +8 & self & Yes \\
  1615. +4 & Frame pointer of parent procedure & Yes \\
  1616. +0 & Return address & No\\ \hline
  1617. \end{FPKltable}
  1618. The stack is cleared with the \var{ret} I386 instruction, meaning that the
  1619. size of all pushed parameters is limited to 64K.
  1620. The stack size is unlimited for all supported platforms. On the \var{GO32V2}
  1621. platform, the minimum guaranteed stack is 128Kb, but this can be set with
  1622. the \var{-Ctxxx} compiler switch.
  1623. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1624. % The Heap
  1625. \section{The heap}
  1626. \label{se:Heap}
  1627. The heap is used to store all dynamic variables, and to store class
  1628. instances. The interface to the heap is the same as in Turbo Pascal,
  1629. although the effects are maybe not the same. On top of that, the \fpk
  1630. run-time library has some extra possibilities, not available in Turbo
  1631. Pascal. These extra possibilities are explained in the next subsections.
  1632. % The heap grows
  1633. \subsection{The heap grows}
  1634. \fpk supports the \var{HeapEerror} procedural variable. If this variable is
  1635. non-nil, then it is called in case you try to allocate memory, and the heap
  1636. is full. By default, \var{HeapError} points to the \var{GrowHeap} function,
  1637. which tries to increase the heap.
  1638. The growheap function issues a system call to try to increase the size of the
  1639. memory available to your program. It first tries to increase memory in a 1 Mb.
  1640. chunk. If this fails, it tries to increase the heap by the amount you
  1641. requested from the heap.
  1642. If the call to \var{GrowHeap} has failed, then a run-time error is generated,
  1643. or nil is returned, depending on the \var{GrowHeap} result.
  1644. If the call to \var{GrowHeap} was successful, then the needed memory will be
  1645. allocated.
  1646. % Using Blocks
  1647. \subsection{Using Blocks}
  1648. If you need to allocate a lot of small block for a small period, then you
  1649. may want to recompile the run-time library with the \var{USEBLOCKS} symbol
  1650. defined. If it is recompiled, then the heap management is done in a
  1651. different way.
  1652. The run-time library keeps a linked list of allocated blocks with size
  1653. up to 256 bytes\footnote{The size can be set using the \var{max\_size}
  1654. constant in the \file{heap.inc} source file.}. By default, it keeps 32 of
  1655. these lists\footnote{The actual size is \var{max\_size div 8}.}.
  1656. When a piece of memory in a block is deallocated, the heap manager doesn't
  1657. really deallocate the occupied memory. The block is simply put in the linked
  1658. list corresponding to its size.
  1659. When you then again request a block of memory, the manager checks in the
  1660. list if there is a non-allocated block which fits the size you need (rounded
  1661. to 8 bytes). If so, the block is used to allocate the memory you requested.
  1662. This method of allocating works faster if the heap is very fragmented, and
  1663. you allocate a lot of small memory chunks.
  1664. Since it is invisible to the program, this provides an easy way of improving
  1665. the performance of the heap manager.
  1666. % The splitheap
  1667. \subsection{Using the split heap}
  1668. {\em Remark : The split heap is still somewhat buggy. Use at your own risk
  1669. for the moment.}
  1670. The split heap can be used to quickly release a lot of blocks you alloated
  1671. previously.
  1672. Suppose that in a part of your program, you allocate a lot of memory chunks
  1673. on the heap. Suppose that you know that you'll release all this memory when
  1674. this particular part of you program is finished.
  1675. In Turbo Pascal, you could foresee this, and mark the position of the heap
  1676. (using the \var{Mark} function) when entering this particular part of your
  1677. program, and release the occupied memory in one call with the \var{Release}
  1678. call.
  1679. For most purposes, this works very good. But sometimes, you may need to
  1680. allocate something on the heap that you {\em don't} want deallocated when you
  1681. release the allocated memory. That is where the split heap comes in.
  1682. When you split the heap, the heap manager keeps 2 heaps: the base heap (the
  1683. normal heap), and the temporary heap. After the call to split the heap,
  1684. memory is allocated from the temporary heap. When you're finished using all
  1685. this memory, you unsplit the heap. This clears all the memory on the split
  1686. heap with one call. After that, memory will be allocated from the base heap
  1687. again.
  1688. So far, nothing special, nothing that can't be done with calls to \var{mark}
  1689. and \var{release}. Suppose now that you have split the heap, and that you've
  1690. come to a point where you need to allocate memory that is to stay allocated
  1691. after you unsplit the heap again. At this point, mark and release are of no
  1692. use. But when using the split heap, you can tell the heap manager to
  1693. --temporarily-- use the base heap again to allocate memory.
  1694. When you've allocated the needed memory, you can tell the heap manager that
  1695. it should start using the temporary heap again.
  1696. When you're finished using the temporary heap, you release it, and the
  1697. memory you allocated on the base heap will still be allocated.
  1698. To use the split-heap, you must recompile the run-time library with the \var{TempHeap}
  1699. symbol defined.
  1700. This means that the following functions are available :
  1701. \begin{verbatim}
  1702. procedure Split_Heap;
  1703. procedure Switch_To_Base_Heap;
  1704. procedure Switch_To_Temp_Heap;
  1705. procedure Switch_Heap;
  1706. procedure ReleaseTempHeap;
  1707. procedure GetempMem(var p : pointer;size : longint);
  1708. \end{verbatim}
  1709. \var{split\_heap} is used to split the heap. It cannot be called two times
  1710. in a row, without a call to \var{releasetempheap}. \var{Releasetempheap}
  1711. completely releases the memory used by the temporary heap.
  1712. Switching temporarily back to the base heap can be done using the
  1713. \var{switch\_to\_base\_heap} call, and returning to the temporary heap is done
  1714. using the \var{switch\_to\_temp\_heap} call. Switching from one to the other
  1715. without knowing on which one your are right now, can be done using the
  1716. \var{switch\_heap} call, which will split the heap first if needed.
  1717. A call to \var{GetTempMem} will allocate a memory block on the temporary
  1718. heap, whatever the current heap is. The current heap after this call will be
  1719. the temporary heap.
  1720. Typically, what will appear in your code is the following sequence :
  1721. \begin{verbatim}
  1722. Split_Heap
  1723. ...
  1724. { Memory allocation }
  1725. ...
  1726. { !! non-volatile memory needed !!}
  1727. Switch_To_Base_Heap;
  1728. getmem (P,size);
  1729. Switch_To_Temp_Heap;
  1730. ...
  1731. {Memory allocation}
  1732. ...
  1733. ReleaseTempHeap;
  1734. {All allocated memory is now freed, except for the memory pointed to by 'P' }
  1735. ...
  1736. \end{verbatim}
  1737. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1738. % Appendices
  1739. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1740. \appendix
  1741. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1742. % Appendix A
  1743. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1744. \chapter{Anatomy of a unit file}
  1745. \label{ch:AppA}
  1746. A unit file consists of basically five parts:
  1747. \begin{enumerate}
  1748. \item A unit header.
  1749. \item A file references part. This contains the references to used units
  1750. and sources with name, checksum and time stamps.
  1751. \item A definition part. Contains all type and procedure definitions.
  1752. \item A Symbol part. Contains all symbol names and references to their
  1753. definitions.
  1754. \item A list of units that are in the implementation part.
  1755. \end{enumerate}
  1756. The header consists of a sequence of 20 bytes, together they give some
  1757. information about the unit file, the compiler version that was used to
  1758. generate the unit file, etc. The complete layout can be found in
  1759. \seet{UnitHeader}. The header is generated by the compiler, and changes only
  1760. when the compiler changes. The current and up-to-date header definition can
  1761. be found in the \file{files.pas} source file of the compiler. Look in this
  1762. file for the \var{unitheader} constant declaration.
  1763. \begin{FPKltable}{ll}{Unit header structure.}{UnitHeader} \hline
  1764. Byte & What is stored \\ \hline
  1765. 0..3 & The letters 'PPU' in upper case. This acts as a check. \\
  1766. 4..6 & The unit format as a 3 letter sequence : e.g. '0','1,'2' for format
  1767. 12. \\
  1768. 7,8 & The compiler version and release numbers as bytes. \\
  1769. 9 & The target OS number. \\
  1770. 10 & Unit flags.\\
  1771. 11..14 & Checksum (as a longint). \\
  1772. 15,16 & unused (equal to 255). \\
  1773. 17..20 & Marks start of unit file. \\ \hline
  1774. \end{FPKltable}
  1775. After the header, in the second part, first the list of all source files for
  1776. the unit is written. Each name is written as a direct copy of the string in
  1777. memory, i.e. a length bytes, and then all characters of the string. This
  1778. list includes any file that was included in the unit source with the
  1779. \var{\{\$i file\}} directive. The list is terminated with a \var{\$ff} byte
  1780. marker.
  1781. After this, the list of units in the \var{uses} clause is written,
  1782. together with their checksums. The file is written as a string, the checksum
  1783. as a longint (i.e. four bytes). Again this list is terminated with a
  1784. \var{\$ff} byte marker.
  1785. After that, in the third part, the definitions of all types, variables,
  1786. constants, procedures and functions are written to the unit file.
  1787. They are written in the following manner: First a byte is written, which
  1788. determines the kind of definition that follows. then follows, as a series of
  1789. bytes, a type-dependent description of the definition. The exact byte order
  1790. for each type can be found in \seet{DefDef}
  1791. \begin{FPKltable}{lccl}{Description of definition fields}{DefDef} \\hline
  1792. Type & Start byte & Size & Stored fields \\ \hline\hline
  1793. Pointer & 3 & 4 & Reference to the type pointer points to. \\ \hline
  1794. Base type & 2 & 9 &
  1795. \begin{tabular}[t]{l}
  1796. 1 byte to indicate base type. \\
  1797. 4-byte start range \\
  1798. 4-byte end range \\
  1799. \end{tabular}\\ \hline
  1800. Array type &5 & 16 &
  1801. \begin{tabular}[t]{l}
  1802. 4-byte reference to element type. \\
  1803. 4-byte reference to range type.\\
  1804. 4-byte start range (longint) \\
  1805. 4-byte end range (longint)\\
  1806. \end{tabular} \\ \hline
  1807. Procedure & 6 & ? &
  1808. \begin{tabular}[t]{l}
  1809. 4-byte reference to the return type definition. \\
  1810. 2 byte Word containing modifiers. \\
  1811. 2 byte Word containing number of parameters. \\
  1812. 5 bytes per parameter.\\
  1813. 1 byte : used registers. \\
  1814. String containing the mangled name. \\
  1815. 8 bytes.
  1816. \end{tabular}
  1817. \\ \hline
  1818. Procedural type & 21 & ? &
  1819. \begin{tabular}[t]{l}
  1820. 4-byte reference to the return type definition. \\
  1821. 2 byte Word containing modifiers. \\
  1822. 2 byte Word containing number of parameters. \\
  1823. 5 bytes per parameter. \\
  1824. \end{tabular}
  1825. \\ \hline
  1826. String & 9 & 1 & 1 byte containing the length of the string. \\
  1827. Record & 15 & variable &
  1828. \begin{tabular}[t]{l}
  1829. Longint indicating record length \\
  1830. list of fields, to be read as unit in itself. \\
  1831. \var{\$ff} end marker.
  1832. \end{tabular} \\ \hline
  1833. Class & 18 & variable &
  1834. \begin{tabular}[t]{l}
  1835. Longint indicating data length \\
  1836. String with mangled name of class.\\
  1837. 4 byte reference to ancestor class.\\
  1838. list of fields, to be read as unit in itself. \\
  1839. \var{\$ff} end marker.
  1840. \end{tabular} \\ \hline
  1841. file & 16 & 1(+4) &
  1842. \begin{tabular}[t]{l}
  1843. 1 byte for type of file. \\
  1844. 4-byte reference to type of typed file.
  1845. \end{tabular}\\ \hline
  1846. Enumeration & 19 & 4 & Biggest element. \\ \hline
  1847. set & 20 & 5 &
  1848. \begin{tabular}[t]{l}
  1849. 4-byte reference to set element type. \\
  1850. 1 byte flag.
  1851. \end{tabular} \\ \hline \hline
  1852. \end{FPKltable}
  1853. This list of definitions is again terminated with a \var{\$ff} byte marker.
  1854. After that, a list of symbols is given, together with a reference to a
  1855. definition. This represents the names of the declarations, and the
  1856. definition they refer to.
  1857. A reference consists of 2 words : the first word indicates the unit number
  1858. (as it appears in the uses clause), and the second word is the number of the
  1859. definition in that unit. A \var{nil} reference is stored as \var{\$ffffffff}.
  1860. After this follows again a \var{\$ff} byte terminated list of filenames: The
  1861. names of the units in the \var{uses} clause of the implementation section.
  1862. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1863. % Appendix B
  1864. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1865. \chapter{List of compiler source files}
  1866. \label{ch:AppB}
  1867. What follows is a list of all compiler files, with a short description
  1868. of what they contain. If you're looking for something in the compiler, this
  1869. list may give you a hint on where to find it.
  1870. The first list is a list of processor dependent files.
  1871. \begin{description}
  1872. \item [aasm.pas] This unit defines some abstract assembler objects and
  1873. routines. It is used by the processor-specific assembly routines.
  1874. \item [alpha.pas, i386.pas, m68k.pas]
  1875. These units describe a processor, its registers, flags etc.
  1876. The basis for every generated instruction. Currently the DEC alpha, Intel
  1877. 386 or higher and Motorola 68000 processors are supported.
  1878. \item [tgen68k.pas, tgeni386.pas]
  1879. These units export help functions for the code generator, which are
  1880. processor specific, they form a buffer between the code generator and the
  1881. machine-specific stuff.
  1882. \item [cg68k.pas, cgi386.pas, cgi3862.pas] These units contain the code
  1883. generators for the different processor types. For the I386 there are 2
  1884. files.
  1885. \item [cga68k.pas cgai386.pas]
  1886. These units contain frequently-used helper functions for the
  1887. processor-specific code generators.
  1888. \item [aopt386.pas]
  1889. These units contain the code optimizers for the different processor-types.
  1890. \item [asmalpha.pas]
  1891. This unit defines some DEC alpha assembly constructs.
  1892. \item [attasmi3.pas, gasasm6.pas, intasmi3.pas]
  1893. These units define processor-specific assembly output classes.
  1894. \item [opts386.pas, opts68k.pas]
  1895. These units process command-line options that are processor specific.
  1896. \item [radi386.pas rai386.pas ratti386.pas]
  1897. These units process inline assembly in different styles (AT\&T style, Intel
  1898. style, and direct style) for the Intel 386 processor.
  1899. \item [rasm386.pas]
  1900. This unit provides some helper routines for the assembly readers.
  1901. \end{description}
  1902. The second list is a list with processor-independent files.
  1903. \begin{description}
  1904. \item [catch.pas]
  1905. This is a \linux specific call. It intercepts a segmentation fault, and lets
  1906. the compiler exit gracefully.
  1907. \item [cobjects.pas]
  1908. This unit provides some basic objects for the compiler: buffered files,
  1909. linked lists, string containers, etc.
  1910. \item [compiler.pas]
  1911. This unit contains the actual compile function.
  1912. \item [errors.pas]
  1913. This unit takes care of error-handling: displaying of error messages,
  1914. reading of error-definitions etc.
  1915. \item [files.pas]
  1916. This unit contains file management routines, such as finding of files etc.
  1917. It is highly OS dependent.
  1918. \item [gdb.pas]
  1919. This unit implements the debugging information generation for the \gnu
  1920. \var{GDB} debugger.
  1921. \item [globals.pas]
  1922. This unit defines some help routines that are used throughout the entire
  1923. compiler, and it does some initializations.
  1924. \item [hcodegen.pas]
  1925. This unit contains processor-independent helper routines for the code
  1926. generator.
  1927. \item [options.pas]
  1928. This unit processes the processor-independent command-line options.
  1929. \item [scanner.pas]
  1930. This unit contains the scanner routines. Here the input file is read and
  1931. split in tokens.
  1932. \item [parser.pas, pass\_1.pas]
  1933. These units contain the actual Pascal parser.
  1934. \item [pp.pas]
  1935. This is the main program. It does some initializations and sets the ball
  1936. rolling.
  1937. \item [symtable.pas]
  1938. This unit contains the code that keeps the symbol tables for the parser. It
  1939. also contains the code to read a unit file.
  1940. \item [systems.pas]
  1941. This unit defines the different operating systems: names, specifications of
  1942. file systems, places where to look for things etc.
  1943. \item [sysutils.pas]
  1944. This unit keeps routines for exception handling.
  1945. \item [tree.pas]
  1946. The main structure for the code generator is a tree of operators and operands,
  1947. and this unit defines the tree structure.
  1948. \item [types.pas]
  1949. This unit contains some helper routines for handling of different Pascal
  1950. types.
  1951. \item [verbose.pas]
  1952. This unit provides the verbosity support. All messages from the compiler are
  1953. put on screen with this unit.
  1954. \end{description}
  1955. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1956. % Appendix C
  1957. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1958. \chapter{Compiler limits}
  1959. \label{ch:AppC}
  1960. Although many of the restrictions imposed by the MS-DOS system are removed
  1961. by use of an extender, or use of another operating system, there still are
  1962. some limitations to the compiler:
  1963. \begin{enumerate}
  1964. \item String constants are limited to 128 characters. All other characters
  1965. are simply dropped from the definition.
  1966. \item The length of generated unit files is limited to 65K for the
  1967. real-mode compiler, and to 1Mb for the 32-bit compiler. This limit can be
  1968. changed by changing the \var{bytearray1} type in \file{cobjects.pas}
  1969. \item Procedure or Function definitions can be nested to a level of 32.
  1970. \item Maximally 255 units can be used in a program when using the real-mode
  1971. compiler. When using the 32-bit compiler, the limit is set to 1024. You can
  1972. change this by redefining the \var{maxunits} constant in the
  1973. \file{files.pas} compiler source file.
  1974. \item Procedures or functions accept parameters with a total size up to
  1975. \var{\$ffff} bytes. This limit is due to the \var{RET} instruction of the I386
  1976. processor. If the calls were made using the C convention this limit would
  1977. disappear.
  1978. \end{enumerate}
  1979. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1980. % Appendix D
  1981. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1982. \chapter{Optimizing techniques used in the compiler.}
  1983. Here follows a listing of the opimizing techniques used in the compiler:
  1984. \begin{enumerate}
  1985. \item When optimizing for a specific Processor (\var{-O3, -O4, -O5 -O6},
  1986. the following is done:
  1987. \begin{itemize}
  1988. \item In \var{case} statements, a check is done whether a jump table
  1989. or a sequence of conditional jumps should be used for optimal performance.
  1990. \item Determines a number of strategies when doing peephole optimization:
  1991. \var{movzbl (\%ebp), \%eax} on PentiumPro and PII systems will be changed
  1992. into \var{xorl \%eax,\%eax; movb (\%ebp),\%al } for lesser systems.
  1993. \end{itemize}
  1994. \item When optimizing for speed (\var{-OG}) or size (\var{-Og}), a choice is
  1995. made between using shorter instructions (for size) such as \var{enter \$4},
  1996. or longer instructions \var{subl \$4,\%esp} for speed. When smaller size is
  1997. requested, things aren't aligned on 4-byte boundaries. When speed is
  1998. requested, things are aligned on 4-byte boundaries as much as possible.
  1999. \item Simple optimization (\var{-Oa}) makes sure the peephole optimizer is
  2000. used.
  2001. \item Maximum optimization (\var{-Ox}) avoid creation of stack frames if
  2002. they aren't required, and unnecessary loading of registers is avoided as
  2003. much as possible. (buggy at the moment (version 0.99.0).
  2004. \item For the future, a reloading optimizer is planned.
  2005. \end{enumerate}
  2006. \end{document}