compiler.pas 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. sysutils,
  110. verbose,comphook,systems,
  111. cutils,cclasses,globals,options,fmodule,parser,symtable,
  112. assemble,link,import,export,tokens,pass_1
  113. { cpu overrides }
  114. ,cpuswtch
  115. { cpu codegenerator }
  116. ,cgcpu
  117. {$ifndef NOPASS2}
  118. ,cpunode
  119. {$endif}
  120. { cpu targets }
  121. ,cputarg
  122. { cpu parameter handling }
  123. ,cpupara
  124. { procinfo stuff }
  125. ,cpupi
  126. { system information for source system }
  127. { the information about the target os }
  128. { are pulled in by the t_* units }
  129. {$ifdef amiga}
  130. ,i_amiga
  131. {$endif amiga}
  132. {$ifdef atari}
  133. ,i_atari
  134. {$endif atari}
  135. {$ifdef beos}
  136. ,i_beos
  137. {$endif beos}
  138. {$ifdef fbds}
  139. ,i_fbsd
  140. {$endif fbds}
  141. {$ifdef go32v2}
  142. ,i_go32v2
  143. {$endif go32v2}
  144. {$ifdef linux}
  145. ,i_linux
  146. {$endif linux}
  147. {$ifdef macos}
  148. ,i_macos
  149. {$endif macos}
  150. {$ifdef nwm}
  151. ,i_nwm
  152. {$endif nwm}
  153. {$ifdef nwl}
  154. ,i_nwl
  155. {$endif nwm}
  156. {$ifdef os2}
  157. {$ifdef emx}
  158. ,i_emx
  159. {$else emx}
  160. ,i_os2
  161. {$endif emx}
  162. {$endif os2}
  163. {$ifdef palmos}
  164. ,i_palmos
  165. {$endif palmos}
  166. {$ifdef sunos}
  167. ,i_sunos
  168. {$endif sunos}
  169. {$ifdef wdosx}
  170. ,i_wdosx
  171. {$endif wdosx}
  172. {$ifdef win32}
  173. ,i_win32
  174. {$endif win32}
  175. { assembler readers }
  176. {$ifdef i386}
  177. {$ifndef NoRa386Int}
  178. ,ra386int
  179. {$endif NoRa386Int}
  180. {$ifndef NoRa386Att}
  181. ,ra386att
  182. {$endif NoRa386Att}
  183. {$else}
  184. ,rasm
  185. {$endif i386}
  186. {$ifdef powerpc}
  187. ,rappcgas
  188. {$endif powerpc}
  189. {$ifdef x86_64}
  190. ,rax64att
  191. {$endif x86_64}
  192. {$ifdef arm}
  193. ,raarmgas
  194. {$endif arm}
  195. {$ifdef SPARC}
  196. ,racpugas
  197. {$endif SPARC}
  198. ;
  199. function Compile(const cmd:string):longint;
  200. implementation
  201. uses
  202. {$IFDEF USE_SYSUTILS}
  203. SysUtils,
  204. {$ENDIF USE_SYSUTILS}
  205. aasmcpu;
  206. {$ifdef EXTDEBUG}
  207. {$define SHOWUSEDMEM}
  208. {$endif}
  209. {$ifdef MEMDEBUG}
  210. {$define SHOWUSEDMEM}
  211. {$endif}
  212. var
  213. CompilerInitedAfterArgs,
  214. CompilerInited : boolean;
  215. olddo_stop : tstopprocedure;
  216. {****************************************************************************
  217. Compiler
  218. ****************************************************************************}
  219. procedure DoneCompiler;
  220. begin
  221. if not CompilerInited then
  222. exit;
  223. { Free compiler if args are read }
  224. {$ifdef BrowserLog}
  225. DoneBrowserLog;
  226. {$endif BrowserLog}
  227. {$ifdef BrowserCol}
  228. do_doneSymbolInfo;
  229. {$endif BrowserCol}
  230. if CompilerInitedAfterArgs then
  231. begin
  232. CompilerInitedAfterArgs:=false;
  233. DoneParser;
  234. DoneImport;
  235. DoneExport;
  236. DoneLinker;
  237. DoneAssembler;
  238. DoneAsm;
  239. end;
  240. { Free memory for the others }
  241. CompilerInited:=false;
  242. DoneSymtable;
  243. DoneGlobals;
  244. donetokens;
  245. end;
  246. procedure InitCompiler(const cmd:string);
  247. begin
  248. if CompilerInited then
  249. DoneCompiler;
  250. { inits which need to be done before the arguments are parsed }
  251. InitSystems;
  252. { globals depends on source_info so it must be after systems }
  253. InitGlobals;
  254. { verbose depends on exe_path and must be after globals }
  255. InitVerbose;
  256. {$ifdef BrowserLog}
  257. InitBrowserLog;
  258. {$endif BrowserLog}
  259. {$ifdef BrowserCol}
  260. do_initSymbolInfo;
  261. {$endif BrowserCol}
  262. inittokens;
  263. InitSymtable; {Must come before read_arguments, to enable macrosymstack}
  264. CompilerInited:=true;
  265. { this is needed here for the IDE
  266. in case of compilation failure
  267. at the previous compile }
  268. current_module:=nil;
  269. { read the arguments }
  270. read_arguments(cmd);
  271. { inits which depend on arguments }
  272. InitParser;
  273. InitImport;
  274. InitExport;
  275. InitLinker;
  276. InitAssembler;
  277. InitAsm;
  278. CompilerInitedAfterArgs:=true;
  279. end;
  280. procedure minimal_stop(err:longint);
  281. begin
  282. DoneCompiler;
  283. olddo_stop(err);
  284. end;
  285. function Compile(const cmd:string):longint;
  286. {$ifdef fpc}
  287. {$maxfpuregisters 0}
  288. {$endif fpc}
  289. procedure writepathlist(w:longint;l:TSearchPathList);
  290. var
  291. hp : tstringlistitem;
  292. begin
  293. hp:=tstringlistitem(l.first);
  294. while assigned(hp) do
  295. begin
  296. Message1(w,hp.str);
  297. hp:=tstringlistitem(hp.next);
  298. end;
  299. end;
  300. function getrealtime : real;
  301. var
  302. {$IFDEF USE_SYSUTILS}
  303. h,m,s,s1000 : word;
  304. {$ELSE USE_SYSUTILS}
  305. h,m,s,s100 : word;
  306. {$ENDIF USE_SYSUTILS}
  307. begin
  308. {$IFDEF USE_SYSUTILS}
  309. DecodeTime(Time,h,m,s,s1000);
  310. getrealtime:=h*3600.0+m*60.0+s+s1000/1000.0;
  311. {$ELSE USE_SYSUTILS}
  312. gettime(h,m,s,s100);
  313. getrealtime:=h*3600.0+m*60.0+s+s100/100.0;
  314. {$ENDIF USE_SYSUTILS}
  315. end;
  316. var
  317. starttime : real;
  318. {$ifdef HASGETHEAPSTATUS}
  319. hstatus : THeapStatus;
  320. {$endif HASGETHEAPSTATUS}
  321. begin
  322. try
  323. try
  324. { Initialize the compiler }
  325. InitCompiler(cmd);
  326. { show some info }
  327. Message1(general_t_compilername,FixFileName(system.paramstr(0)));
  328. Message1(general_d_sourceos,source_info.name);
  329. Message1(general_i_targetos,target_info.name);
  330. Message1(general_t_exepath,exepath);
  331. WritePathList(general_t_unitpath,unitsearchpath);
  332. WritePathList(general_t_includepath,includesearchpath);
  333. WritePathList(general_t_librarypath,librarysearchpath);
  334. WritePathList(general_t_objectpath,objectsearchpath);
  335. starttime:=getrealtime;
  336. { Compile the program }
  337. {$ifdef PREPROCWRITE}
  338. if parapreprocess then
  339. parser.preprocess(inputdir+inputfile+inputextension)
  340. else
  341. {$endif PREPROCWRITE}
  342. parser.compile(inputdir+inputfile+inputextension);
  343. { Show statistics }
  344. if status.errorcount=0 then
  345. begin
  346. starttime:=getrealtime-starttime;
  347. if starttime<0 then
  348. starttime:=starttime+3600.0*24.0;
  349. Message2(general_i_abslines_compiled,tostr(status.compiledlines),tostr(trunc(starttime))+
  350. '.'+tostr(trunc(frac(starttime)*10)));
  351. end;
  352. finally
  353. { no message possible after this !! }
  354. DoneCompiler;
  355. end;
  356. except
  357. Message(general_e_compilation_aborted);
  358. DoneVerbose;
  359. Raise;
  360. end;
  361. {$ifdef SHOWUSEDMEM}
  362. {$ifdef HASGETHEAPSTATUS}
  363. GetHeapStatus(hstatus);
  364. Writeln('Max Memory used/heapsize: ',DStr(hstatus.MaxHeapUsed shr 10),'/',DStr(hstatus.MaxHeapSize shr 10),' Kb');
  365. {$else HASGETHEAPSTATUS}
  366. Writeln('Memory used (heapsize): ',DStr(system.Heapsize shr 10),' Kb');
  367. {$endif HASGETHEAPSTATUS}
  368. {$endif SHOWUSEDMEM}
  369. { Set the return value if an error has occurred }
  370. if status.errorcount=0 then
  371. result:=0
  372. else
  373. result:=1;
  374. end;
  375. end.
  376. {
  377. $Log$
  378. Revision 1.52 2005-01-26 16:23:28 peter
  379. * detect arithmetic overflows for constants at compile time
  380. * use try..except instead of setjmp
  381. Revision 1.51 2005/01/09 20:24:43 olle
  382. * rework of macro subsystem
  383. + exportable macros for mode macpas
  384. Revision 1.50 2004/11/22 19:34:58 peter
  385. * GetHeapStatus added, removed MaxAvail,MemAvail,HeapSize
  386. Revision 1.49 2004/10/15 09:14:16 mazen
  387. - remove $IFDEF DELPHI and related code
  388. - remove $IFDEF FPCPROCVAR and related code
  389. Revision 1.48 2004/10/14 17:17:25 mazen
  390. * use SysUtils unit instead of Dos Unit
  391. Revision 1.47 2004/09/08 11:23:31 michael
  392. + Check if outputdir exists, Fix exitcode when displaying help pages
  393. Revision 1.46 2004/09/04 21:18:47 armin
  394. * target netwlibc added (libc is preferred for newer netware versions)
  395. Revision 1.45 2004/06/20 08:55:29 florian
  396. * logs truncated
  397. Revision 1.44 2004/01/14 23:39:05 florian
  398. * another bunch of x86-64 fixes mainly calling convention and
  399. assembler reader related
  400. }