compiler.pas 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit is the interface of the compiler which can be used by
  5. external programs to link in the compiler
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************}
  18. unit compiler;
  19. {$i fpcdefs.inc}
  20. {$ifdef FPC}
  21. { One of Alpha, I386 or M68K must be defined }
  22. {$UNDEF CPUOK}
  23. {$ifdef I386}
  24. {$define CPUOK}
  25. {$endif}
  26. {$ifdef M68K}
  27. {$ifndef CPUOK}
  28. {$DEFINE CPUOK}
  29. {$else}
  30. {$fatal cannot define two CPU switches}
  31. {$endif}
  32. {$endif}
  33. {$ifdef alpha}
  34. {$ifndef CPUOK}
  35. {$DEFINE CPUOK}
  36. {$else}
  37. {$fatal cannot define two CPU switches}
  38. {$endif}
  39. {$endif}
  40. {$ifdef vis}
  41. {$ifndef CPUOK}
  42. {$DEFINE CPUOK}
  43. {$else}
  44. {$fatal cannot define two CPU switches}
  45. {$endif}
  46. {$endif}
  47. {$ifdef powerpc}
  48. {$ifndef CPUOK}
  49. {$DEFINE CPUOK}
  50. {$else}
  51. {$fatal cannot define two CPU switches}
  52. {$endif}
  53. {$endif}
  54. {$ifdef ia64}
  55. {$ifndef CPUOK}
  56. {$DEFINE CPUOK}
  57. {$else}
  58. {$fatal cannot define two CPU switches}
  59. {$endif}
  60. {$endif}
  61. {$ifdef SPARC}
  62. {$ifndef CPUOK}
  63. {$DEFINE CPUOK}
  64. {$else}
  65. {$fatal cannot define two CPU switches}
  66. {$endif}
  67. {$endif}
  68. {$ifdef x86_64}
  69. {$ifndef CPUOK}
  70. {$DEFINE CPUOK}
  71. {$else}
  72. {$fatal cannot define two CPU switches}
  73. {$endif}
  74. {$endif}
  75. {$ifdef ARM}
  76. {$ifndef CPUOK}
  77. {$DEFINE CPUOK}
  78. {$else}
  79. {$fatal cannot define two CPU switches}
  80. {$endif ARM}
  81. {$endif ARM}
  82. {$ifndef CPUOK}
  83. {$fatal One of the switches I386, iA64, Alpha, PowerPC or M68K must be defined}
  84. {$endif}
  85. {$ifdef support_mmx}
  86. {$ifndef i386}
  87. {$fatal I386 switch must be on for MMX support}
  88. {$endif i386}
  89. {$endif support_mmx}
  90. {$endif}
  91. interface
  92. uses
  93. {$ifdef fpc}
  94. {$ifdef GO32V2}
  95. emu387,
  96. {$endif GO32V2}
  97. {$ifdef WATCOM} // wiktor: pewnie nie potrzeba
  98. emu387,
  99. { dpmiexcp, }
  100. {$endif WATCOM}
  101. {$endif}
  102. {$ifdef BrowserLog}
  103. browlog,
  104. {$endif BrowserLog}
  105. {$IFDEF USE_SYSUTILS}
  106. {$ELSE USE_SYSUTILS}
  107. dos,
  108. {$ENDIF USE_SYSUTILS}
  109. {$IFNDEF MACOS_USE_FAKE_SYSUTILS}
  110. sysutils,
  111. {$ENDIF MACOS_USE_FAKE_SYSUTILS}
  112. verbose,comphook,systems,
  113. cutils,cclasses,globals,options,fmodule,parser,symtable,
  114. assemble,link,import,export,tokens,pass_1
  115. { cpu overrides }
  116. ,cpuswtch
  117. { cpu codegenerator }
  118. ,cgcpu
  119. {$ifndef NOPASS2}
  120. ,cpunode
  121. {$endif}
  122. { cpu targets }
  123. ,cputarg
  124. { cpu parameter handling }
  125. ,cpupara
  126. { procinfo stuff }
  127. ,cpupi
  128. { system information for source system }
  129. { the information about the target os }
  130. { are pulled in by the t_* units }
  131. {$ifdef amiga}
  132. ,i_amiga
  133. {$endif amiga}
  134. {$ifdef atari}
  135. ,i_atari
  136. {$endif atari}
  137. {$ifdef beos}
  138. ,i_beos
  139. {$endif beos}
  140. {$ifdef fbds}
  141. ,i_fbsd
  142. {$endif fbds}
  143. {$ifdef go32v2}
  144. ,i_go32v2
  145. {$endif go32v2}
  146. {$ifdef linux}
  147. ,i_linux
  148. {$endif linux}
  149. {$ifdef macos}
  150. ,i_macos
  151. {$endif macos}
  152. {$ifdef nwm}
  153. ,i_nwm
  154. {$endif nwm}
  155. {$ifdef nwl}
  156. ,i_nwl
  157. {$endif nwm}
  158. {$ifdef os2}
  159. {$ifdef emx}
  160. ,i_emx
  161. {$else emx}
  162. ,i_os2
  163. {$endif emx}
  164. {$endif os2}
  165. {$ifdef palmos}
  166. ,i_palmos
  167. {$endif palmos}
  168. {$ifdef sunos}
  169. ,i_sunos
  170. {$endif sunos}
  171. {$ifdef wdosx}
  172. ,i_wdosx
  173. {$endif wdosx}
  174. {$ifdef win32}
  175. ,i_win32
  176. {$endif win32}
  177. { assembler readers }
  178. {$ifdef i386}
  179. {$ifndef NoRa386Int}
  180. ,ra386int
  181. {$endif NoRa386Int}
  182. {$ifndef NoRa386Att}
  183. ,ra386att
  184. {$endif NoRa386Att}
  185. {$else}
  186. ,rasm
  187. {$endif i386}
  188. {$ifdef powerpc}
  189. ,rappcgas
  190. {$endif powerpc}
  191. {$ifdef x86_64}
  192. ,rax64att
  193. {$endif x86_64}
  194. {$ifdef arm}
  195. ,raarmgas
  196. {$endif arm}
  197. {$ifdef SPARC}
  198. ,racpugas
  199. {$endif SPARC}
  200. ;
  201. function Compile(const cmd:string):longint;
  202. implementation
  203. uses
  204. {$IFDEF USE_SYSUTILS}
  205. SysUtils,
  206. {$ENDIF USE_SYSUTILS}
  207. aasmcpu;
  208. {$ifdef EXTDEBUG}
  209. {$define SHOWUSEDMEM}
  210. {$endif}
  211. {$ifdef MEMDEBUG}
  212. {$define SHOWUSEDMEM}
  213. {$endif}
  214. var
  215. CompilerInitedAfterArgs,
  216. CompilerInited : boolean;
  217. olddo_stop : tstopprocedure;
  218. {****************************************************************************
  219. Compiler
  220. ****************************************************************************}
  221. procedure DoneCompiler;
  222. begin
  223. if not CompilerInited then
  224. exit;
  225. { Free compiler if args are read }
  226. {$ifdef BrowserLog}
  227. DoneBrowserLog;
  228. {$endif BrowserLog}
  229. {$ifdef BrowserCol}
  230. do_doneSymbolInfo;
  231. {$endif BrowserCol}
  232. if CompilerInitedAfterArgs then
  233. begin
  234. CompilerInitedAfterArgs:=false;
  235. DoneParser;
  236. DoneImport;
  237. DoneExport;
  238. DoneLinker;
  239. DoneAssembler;
  240. DoneAsm;
  241. end;
  242. { Free memory for the others }
  243. CompilerInited:=false;
  244. DoneSymtable;
  245. DoneGlobals;
  246. donetokens;
  247. end;
  248. procedure InitCompiler(const cmd:string);
  249. begin
  250. if CompilerInited then
  251. DoneCompiler;
  252. { inits which need to be done before the arguments are parsed }
  253. InitSystems;
  254. { globals depends on source_info so it must be after systems }
  255. InitGlobals;
  256. { verbose depends on exe_path and must be after globals }
  257. InitVerbose;
  258. {$ifdef BrowserLog}
  259. InitBrowserLog;
  260. {$endif BrowserLog}
  261. {$ifdef BrowserCol}
  262. do_initSymbolInfo;
  263. {$endif BrowserCol}
  264. inittokens;
  265. InitSymtable; {Must come before read_arguments, to enable macrosymstack}
  266. CompilerInited:=true;
  267. { this is needed here for the IDE
  268. in case of compilation failure
  269. at the previous compile }
  270. current_module:=nil;
  271. { read the arguments }
  272. read_arguments(cmd);
  273. { inits which depend on arguments }
  274. InitParser;
  275. InitImport;
  276. InitExport;
  277. InitLinker;
  278. InitAssembler;
  279. InitAsm;
  280. CompilerInitedAfterArgs:=true;
  281. end;
  282. procedure minimal_stop(err:longint);
  283. begin
  284. DoneCompiler;
  285. olddo_stop(err);
  286. end;
  287. function Compile(const cmd:string):longint;
  288. {$ifdef fpc}
  289. {$maxfpuregisters 0}
  290. {$endif fpc}
  291. procedure writepathlist(w:longint;l:TSearchPathList);
  292. var
  293. hp : tstringlistitem;
  294. begin
  295. hp:=tstringlistitem(l.first);
  296. while assigned(hp) do
  297. begin
  298. Message1(w,hp.str);
  299. hp:=tstringlistitem(hp.next);
  300. end;
  301. end;
  302. function getrealtime : real;
  303. var
  304. {$IFDEF USE_SYSUTILS}
  305. h,m,s,s1000 : word;
  306. {$ELSE USE_SYSUTILS}
  307. h,m,s,s100 : word;
  308. {$ENDIF USE_SYSUTILS}
  309. begin
  310. {$IFDEF USE_SYSUTILS}
  311. DecodeTime(Time,h,m,s,s1000);
  312. getrealtime:=h*3600.0+m*60.0+s+s1000/1000.0;
  313. {$ELSE USE_SYSUTILS}
  314. gettime(h,m,s,s100);
  315. getrealtime:=h*3600.0+m*60.0+s+s100/100.0;
  316. {$ENDIF USE_SYSUTILS}
  317. end;
  318. var
  319. starttime : real;
  320. {$ifdef HASGETHEAPSTATUS}
  321. hstatus : THeapStatus;
  322. {$endif HASGETHEAPSTATUS}
  323. begin
  324. try
  325. try
  326. { Initialize the compiler }
  327. InitCompiler(cmd);
  328. { show some info }
  329. Message1(general_t_compilername,FixFileName(system.paramstr(0)));
  330. Message1(general_d_sourceos,source_info.name);
  331. Message1(general_i_targetos,target_info.name);
  332. Message1(general_t_exepath,exepath);
  333. WritePathList(general_t_unitpath,unitsearchpath);
  334. WritePathList(general_t_includepath,includesearchpath);
  335. WritePathList(general_t_librarypath,librarysearchpath);
  336. WritePathList(general_t_objectpath,objectsearchpath);
  337. starttime:=getrealtime;
  338. { Compile the program }
  339. {$ifdef PREPROCWRITE}
  340. if parapreprocess then
  341. parser.preprocess(inputdir+inputfile+inputextension)
  342. else
  343. {$endif PREPROCWRITE}
  344. parser.compile(inputdir+inputfile+inputextension);
  345. { Show statistics }
  346. if status.errorcount=0 then
  347. begin
  348. starttime:=getrealtime-starttime;
  349. if starttime<0 then
  350. starttime:=starttime+3600.0*24.0;
  351. Message2(general_i_abslines_compiled,tostr(status.compiledlines),tostr(trunc(starttime))+
  352. '.'+tostr(trunc(frac(starttime)*10)));
  353. end;
  354. finally
  355. { no message possible after this !! }
  356. DoneCompiler;
  357. end;
  358. except
  359. Message(general_e_compilation_aborted);
  360. DoneVerbose;
  361. Raise;
  362. end;
  363. {$ifdef SHOWUSEDMEM}
  364. {$ifdef HASGETHEAPSTATUS}
  365. GetHeapStatus(hstatus);
  366. Writeln('Max Memory used/heapsize: ',DStr(hstatus.MaxHeapUsed shr 10),'/',DStr(hstatus.MaxHeapSize shr 10),' Kb');
  367. {$else HASGETHEAPSTATUS}
  368. Writeln('Memory used (heapsize): ',DStr(system.Heapsize shr 10),' Kb');
  369. {$endif HASGETHEAPSTATUS}
  370. {$endif SHOWUSEDMEM}
  371. { Set the return value if an error has occurred }
  372. if status.errorcount=0 then
  373. result:=0
  374. else
  375. result:=1;
  376. end;
  377. end.
  378. {
  379. $Log$
  380. Revision 1.53 2005-01-31 21:30:56 olle
  381. + Added fake Exception classes, only for MACOS.
  382. Revision 1.52 2005/01/26 16:23:28 peter
  383. * detect arithmetic overflows for constants at compile time
  384. * use try..except instead of setjmp
  385. Revision 1.51 2005/01/09 20:24:43 olle
  386. * rework of macro subsystem
  387. + exportable macros for mode macpas
  388. Revision 1.50 2004/11/22 19:34:58 peter
  389. * GetHeapStatus added, removed MaxAvail,MemAvail,HeapSize
  390. Revision 1.49 2004/10/15 09:14:16 mazen
  391. - remove $IFDEF DELPHI and related code
  392. - remove $IFDEF FPCPROCVAR and related code
  393. Revision 1.48 2004/10/14 17:17:25 mazen
  394. * use SysUtils unit instead of Dos Unit
  395. Revision 1.47 2004/09/08 11:23:31 michael
  396. + Check if outputdir exists, Fix exitcode when displaying help pages
  397. Revision 1.46 2004/09/04 21:18:47 armin
  398. * target netwlibc added (libc is preferred for newer netware versions)
  399. Revision 1.45 2004/06/20 08:55:29 florian
  400. * logs truncated
  401. Revision 1.44 2004/01/14 23:39:05 florian
  402. * another bunch of x86-64 fixes mainly calling convention and
  403. assembler reader related
  404. }