prog.tex 100 KB

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